Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 46e980e

Browse files
authored
[3.1 port] Fix debugger crash during unload of assemblies in ALC (#28023)
1 parent 9d9b579 commit 46e980e

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

src/debug/ee/debugger.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5392,6 +5392,7 @@ DebuggerModule* Debugger::LookupOrCreateModule(Module* pModule, AppDomain *pAppD
53925392
// If it doesn't exist, create it.
53935393
if (dmod == NULL)
53945394
{
5395+
LOG((LF_CORDB, LL_INFO1000, "D::LOCM dmod for m=0x%x ad=0x%x not found, creating.\n", pModule, pAppDomain));
53955396
HRESULT hr = S_OK;
53965397
EX_TRY
53975398
{
@@ -5406,7 +5407,8 @@ DebuggerModule* Debugger::LookupOrCreateModule(Module* pModule, AppDomain *pAppD
54065407
// The module must be in the AppDomain that was requested
54075408
_ASSERTE( (dmod == NULL) || (dmod->GetAppDomain() == pAppDomain) );
54085409

5409-
LOG((LF_CORDB, LL_INFO1000, "D::LOCM m=0x%x ad=0x%x -> dm=0x%x\n", pModule, pAppDomain, dmod));
5410+
LOG((LF_CORDB, LL_INFO1000, "D::LOCM m=0x%x ad=0x%x -> dm=0x%x(Mod=0x%x, DomFile=0x%x, AD=0x%x)\n",
5411+
pModule, pAppDomain, dmod, dmod->GetRuntimeModule(), dmod->GetDomainFile(), dmod->GetAppDomain()));
54105412
return dmod;
54115413
}
54125414

@@ -10014,8 +10016,10 @@ void Debugger::UnloadModule(Module* pRuntimeModule,
1001410016
}
1001510017
_ASSERTE(module != NULL);
1001610018

10017-
STRESS_LOG3(LF_CORDB, LL_INFO10000, "D::UM: Unloading Mod:%#08x, %#08x, %#08x\n",
10018-
pRuntimeModule, pAppDomain, pRuntimeModule->IsIStream());
10019+
STRESS_LOG6(LF_CORDB, LL_INFO10000,
10020+
"D::UM: Unloading RTMod:%#08x (DomFile: %#08x, IsISStream:%#08x); DMod:%#08x(RTMod:%#08x DomFile: %#08x)\n",
10021+
pRuntimeModule, pRuntimeModule->GetDomainFile(), pRuntimeModule->IsIStream(),
10022+
module, module->GetRuntimeModule(), module->GetDomainFile());
1001910023

1002010024
// Note: the appdomain the module was loaded in must match the appdomain we're unloading it from. If it doesn't,
1002110025
// then we've either found the wrong DebuggerModule in LookupModule or we were passed bad data.

src/debug/ee/debuggermodule.cpp

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,10 @@ void DebuggerModuleTable::AddModule(DebuggerModule *pModule)
226226
}
227227

228228
//-----------------------------------------------------------------------------
229-
// Remove a DebuggerModule from the module table.
230-
// This occurs in response to AppDomain unload.
231-
// Note that this doesn't necessarily mean the EE Module is being unloaded (it may be shared)
229+
// Remove a DebuggerModule from the module table when it gets notified.
230+
// This occurs in response to the finalization of an unloaded AssemblyLoadContext.
232231
//-----------------------------------------------------------------------------
233-
void DebuggerModuleTable::RemoveModule(Module* module, AppDomain *pAppDomain)
232+
void DebuggerModuleTable::RemoveModule(Module* pModule, AppDomain *pAppDomain)
234233
{
235234
CONTRACTL
236235
{
@@ -239,12 +238,33 @@ void DebuggerModuleTable::RemoveModule(Module* module, AppDomain *pAppDomain)
239238
}
240239
CONTRACTL_END;
241240

242-
// If this is a domain neutral module, then scan the complete list of DebuggerModules looking
243-
// for the one with a matching appdomain id.
244-
// Note: we have to make sure to lookup the module with the app domain parameter if the module lives in a shared
245-
// assembly or the system assembly. <BUGNUM>Bugs 65943 & 81728.</BUGNUM>
246-
_ASSERTE( FALSE );
247-
241+
LOG((LF_CORDB, LL_INFO1000, "DMT::RM Attempting to remove Module:0x%x AD:0x%x\n", pModule, pAppDomain));
242+
243+
_ASSERTE(ThreadHoldsLock());
244+
_ASSERTE(pModule != NULL);
245+
246+
HASHFIND hf;
247+
248+
for (DebuggerModuleEntry *pDME = (DebuggerModuleEntry *) FindFirstEntry(&hf);
249+
pDME != NULL;
250+
pDME = (DebuggerModuleEntry*) FindNextEntry(&hf))
251+
{
252+
DebuggerModule *pDM = pDME->module;
253+
Module* pRuntimeModule = pDM->GetRuntimeModule();
254+
255+
if ((pRuntimeModule == pModule) && (pDM->GetAppDomain() == pAppDomain))
256+
{
257+
LOG((LF_CORDB, LL_INFO1000, "DMT::RM Removing DebuggerMod:0x%x - Module:0x%x DF:0x%x AD:0x%x\n",
258+
pDM, pModule, pDM->GetDomainFile(), pAppDomain));
259+
TRACE_FREE(pDM);
260+
DeleteInteropSafe(pDM);
261+
Delete(HASH(pRuntimeModule), (HASHENTRY *) pDME);
262+
_ASSERTE(GetModule(pModule, pAppDomain) == NULL);
263+
return;
264+
}
265+
}
266+
267+
LOG((LF_CORDB, LL_INFO1000, "DMT::RM No debugger module found for Module:0x%x AD:0x%x\n", pModule, pAppDomain));
248268
}
249269

250270

@@ -337,5 +357,3 @@ DebuggerModule *DebuggerModuleTable::GetNextModule(HASHFIND *info)
337357
else
338358
return entry->module;
339359
}
340-
341-

0 commit comments

Comments
 (0)