Skip to content

Commit 1c1757c

Browse files
authored
Removing g_hmodCoreCLR and g_hThisInst (#43735)
* g_hmodCoreCLR is Windows-only * Use 42 in MarkAsThrownByUsWorker() * replaced GetCLRModule with GetClrModulePathName * Addded PAL_GetPalHostModule to mscordac_unixexports * replaced a few cases of g_hThisInst where used to get module path. We can uise `GetClrModulePathName` instead. * more cases where used to get module path * unused `GetModuleInst()` * not using HMODULE in StressLog * removed mscoree GetModuleInst * Deleted both `g_hThisInst` and `g_hmodCoreCLR` * fix Linux build * rename `GetModuleBase` ->` GetClrModuleBase`, it was confusing with other `GetClrModuleBase` * Fix Linux build * PR feedback * Removed hMod variables in Debugger areas * standalone GC * cache GetClrModuleBase * Prevent double-read of `pImageBase` * fix linker issue * couple more link order fixes. * Remove `g_thisModule` in daccess.cpp. Fix typos.
1 parent fd3ae61 commit 1c1757c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+126
-614
lines changed

src/coreclr/src/debug/daccess/daccess.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ extern bool TryGetSymbol(ICorDebugDataTarget* dataTarget, uint64_t baseAddress,
4040

4141
CRITICAL_SECTION g_dacCritSec;
4242
ClrDataAccess* g_dacImpl;
43-
HINSTANCE g_thisModule;
4443

4544
EXTERN_C
4645
#ifdef TARGET_UNIX
@@ -75,9 +74,6 @@ BOOL WINAPI DllMain(HANDLE instance, DWORD reason, LPVOID reserved)
7574
#endif
7675
InitializeCriticalSection(&g_dacCritSec);
7776

78-
// Save the module handle.
79-
g_thisModule = (HINSTANCE)instance;
80-
8177
g_procInitialized = true;
8278
break;
8379
}
@@ -95,12 +91,6 @@ BOOL WINAPI DllMain(HANDLE instance, DWORD reason, LPVOID reserved)
9591
return TRUE;
9692
}
9793

98-
HINSTANCE
99-
GetModuleInst(void)
100-
{
101-
return g_thisModule;
102-
}
103-
10494
HRESULT
10595
ConvertUtf8(__in LPCUTF8 utf8,
10696
ULONG32 bufLen,
@@ -5622,16 +5612,6 @@ ClrDataAccess::Initialize(void)
56225612
// Do some validation
56235613
IfFailRet(VerifyDlls());
56245614

5625-
// To support EH SxS, utilcode requires the base address of the runtime
5626-
// as part of its initialization so that functions like "WasThrownByUs" work correctly since
5627-
// they use the CLR base address to check if an exception was raised by a given instance of the runtime
5628-
// or not.
5629-
//
5630-
// Thus, when DAC is initialized, initialize utilcode with the base address of the runtime loaded in the
5631-
// target process. This is similar to work done in CorDB::SetTargetCLR for mscordbi.
5632-
5633-
g_hmodCoreCLR = (HINSTANCE)m_globalBase; // Base address of the runtime in the target process
5634-
56355615
return S_OK;
56365616
}
56375617

src/coreclr/src/debug/daccess/dacimpl.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,6 @@ enum DAC_USAGE_TYPE
127127
DAC_PAL,
128128
};
129129

130-
// mscordacwks's module handle
131-
extern HINSTANCE g_thisModule;
132-
133130
class ReflectionModule;
134131

135132
struct DAC_MD_IMPORT

src/coreclr/src/debug/daccess/request.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3889,12 +3889,22 @@ HRESULT ClrDataAccess::GetTLSIndex(ULONG *pIndex)
38893889
return hr;
38903890
}
38913891

3892+
#ifndef TARGET_UNIX
3893+
extern "C" IMAGE_DOS_HEADER __ImageBase;
3894+
#endif
3895+
38923896
HRESULT ClrDataAccess::GetDacModuleHandle(HMODULE *phModule)
38933897
{
38943898
if(phModule == NULL)
38953899
return E_INVALIDARG;
3896-
*phModule = GetModuleInst();
3900+
3901+
#ifndef TARGET_UNIX
3902+
*phModule = (HMODULE)&__ImageBase;
38973903
return S_OK;
3904+
#else
3905+
// hModule is not available under TARGET_UNIX
3906+
return E_FAIL;
3907+
#endif
38983908
}
38993909

39003910
HRESULT ClrDataAccess::GetRCWData(CLRDATA_ADDRESS addr, struct DacpRCWData *rcwData)

src/coreclr/src/debug/di/cordb.cpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@
2626
#define SUPPORT_LOCAL_DEBUGGING 1
2727
#endif
2828

29-
//********** Globals. *********************************************************
30-
#ifndef HOST_UNIX
31-
HINSTANCE g_hInst; // Instance handle to this piece of code.
32-
#endif
33-
3429
//-----------------------------------------------------------------------------
3530
// SxS Versioning story for Mscordbi (ICorDebug + friends)
3631
//-----------------------------------------------------------------------------
@@ -200,9 +195,7 @@ BOOL WINAPI DbgDllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
200195

201196
case DLL_PROCESS_ATTACH:
202197
{
203-
#ifndef HOST_UNIX
204-
g_hInst = hInstance;
205-
#else
198+
#ifdef HOST_UNIX
206199
int err = PAL_InitializeDLL();
207200
if(err != 0)
208201
{
@@ -438,17 +431,6 @@ HRESULT STDMETHODCALLTYPE CClassFactory::LockServer(
438431
}
439432

440433

441-
//*****************************************************************************
442-
// This helper provides access to the instance handle of the loaded image.
443-
//*****************************************************************************
444-
#ifndef TARGET_UNIX
445-
HINSTANCE GetModuleInst()
446-
{
447-
return g_hInst;
448-
}
449-
#endif
450-
451-
452434
//-----------------------------------------------------------------------------
453435
// Substitute for mscoree
454436
//

src/coreclr/src/debug/di/module.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2565,7 +2565,7 @@ HRESULT CordbModule::CreateReaderForInMemorySymbols(REFIID riid, void** ppObj)
25652565
#ifndef TARGET_UNIX
25662566
// PDB format - use diasymreader.dll with COM activation
25672567
InlineSString<_MAX_PATH> ssBuf;
2568-
IfFailThrow(GetHModuleDirectory(GetModuleInst(), ssBuf));
2568+
IfFailThrow(GetClrModuleDirectory(ssBuf));
25692569
IfFailThrow(FakeCoCreateInstanceEx(CLSID_CorSymBinder_SxS,
25702570
ssBuf.GetUnicode(),
25712571
IID_ISymUnmanagedBinder,

src/coreclr/src/debug/di/rsmain.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ void LeftSideResourceCleanupList::SweepNeuterLeftSideResources(CordbProcess * pP
456456
/* ------------------------------------------------------------------------- *
457457
* CordbBase class
458458
* ------------------------------------------------------------------------- */
459+
extern void* GetClrModuleBase();
459460

460461
// Do any initialization necessary for both CorPublish and CorDebug
461462
// This includes enabling logging and adding the SEDebug priv.
@@ -488,11 +489,7 @@ void CordbCommonBase::InitializeCommon()
488489
unsigned level = REGUTIL::GetConfigDWORD_DontUse_(CLRConfig::EXTERNAL_LogLevel, LL_INFO1000);
489490
unsigned bytesPerThread = REGUTIL::GetConfigDWORD_DontUse_(CLRConfig::UNSUPPORTED_StressLogSize, STRESSLOG_CHUNK_SIZE * 2);
490491
unsigned totalBytes = REGUTIL::GetConfigDWORD_DontUse_(CLRConfig::UNSUPPORTED_TotalStressLogSize, STRESSLOG_CHUNK_SIZE * 1024);
491-
#ifndef TARGET_UNIX
492-
StressLog::Initialize(facilities, level, bytesPerThread, totalBytes, GetModuleInst());
493-
#else
494-
StressLog::Initialize(facilities, level, bytesPerThread, totalBytes, NULL);
495-
#endif
492+
StressLog::Initialize(facilities, level, bytesPerThread, totalBytes, GetClrModuleBase());
496493
}
497494
}
498495

@@ -1427,13 +1424,6 @@ HRESULT Cordb::SetTargetCLR(HMODULE hmodTargetCLR)
14271424
m_targetCLR = hmodTargetCLR;
14281425
#endif
14291426

1430-
// @REVIEW: are we happy with this workaround? It allows us to use the existing
1431-
// infrastructure for instance name decoration, but it really doesn't fit
1432-
// the same model because coreclr.dll isn't in this process and hmodTargetCLR
1433-
// is the debuggee target, not the coreclr.dll to bind utilcode to..
1434-
1435-
g_hmodCoreCLR = hmodTargetCLR;
1436-
14371427
return S_OK;
14381428
}
14391429

src/coreclr/src/debug/di/rspriv.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,6 @@ class DbgTransportSession;
134134
// These hooks must be removed before shipping.
135135
class ShimProcess;
136136

137-
138-
#ifndef TARGET_UNIX
139-
extern HINSTANCE GetModuleInst();
140-
#endif
141-
142-
143137
template <class T>
144138
class CordbSafeHashTable;
145139

src/coreclr/src/debug/di/shimprocess.cpp

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,34 +1824,19 @@ HMODULE ShimProcess::GetDacModule()
18241824
HModuleHolder hDacDll;
18251825
PathString wszAccessDllPath;
18261826

1827-
#ifdef TARGET_UNIX
1828-
if (!PAL_GetPALDirectoryWrapper(wszAccessDllPath))
1829-
{
1830-
ThrowLastError();
1831-
}
1832-
PCWSTR eeFlavor = MAKEDLLNAME_W(W("mscordaccore"));
1833-
#else
18341827
//
18351828
// Load the access DLL from the same directory as the the current CLR Debugging Services DLL.
18361829
//
1837-
1838-
if (!WszGetModuleFileName(GetModuleInst(), wszAccessDllPath))
1830+
if (GetClrModuleDirectory(wszAccessDllPath) != S_OK)
18391831
{
18401832
ThrowLastError();
18411833
}
18421834

1843-
if (!SUCCEEDED(CopySystemDirectory(wszAccessDllPath, wszAccessDllPath)))
1844-
{
1845-
ThrowHR(E_INVALIDARG);
1846-
}
1847-
18481835
// Dac Dll is named:
18491836
// mscordaccore.dll <-- coreclr
18501837
// mscordacwks.dll <-- desktop
1851-
PCWSTR eeFlavor =
1852-
W("mscordaccore.dll");
1838+
PCWSTR eeFlavor = MAKEDLLNAME_W(W("mscordaccore"));
18531839

1854-
#endif // TARGET_UNIX
18551840
wszAccessDllPath.Append(eeFlavor);
18561841

18571842
hDacDll.Assign(WszLoadLibrary(wszAccessDllPath));

src/coreclr/src/debug/ee/debugger.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,7 +1750,7 @@ void Debugger::SendRawEvent(const DebuggerIPCEvent * pManagedEvent)
17501750
// The debugger can then use ReadProcessMemory to read through this array.
17511751
ULONG_PTR rgData [] = {
17521752
CLRDBG_EXCEPTION_DATA_CHECKSUM,
1753-
(ULONG_PTR) g_hThisInst,
1753+
(ULONG_PTR)GetClrModuleBase(),
17541754
(ULONG_PTR) pManagedEvent
17551755
};
17561756

@@ -5668,7 +5668,7 @@ bool Debugger::FirstChanceNativeException(EXCEPTION_RECORD *exception,
56685668
// Ignore any notification exceptions sent from code:Debugger.SendRawEvent.
56695669
// This is not a common case, but could happen in some cases described
56705670
// in SendRawEvent. Either way, Left-Side and VM should just ignore these.
5671-
if (IsEventDebuggerNotification(exception, PTR_TO_CORDB_ADDRESS(g_hThisInst)))
5671+
if (IsEventDebuggerNotification(exception, PTR_TO_CORDB_ADDRESS(GetClrModuleBase())))
56725672
{
56735673
return true;
56745674
}
@@ -12363,7 +12363,7 @@ void Debugger::GetAndSendTransitionStubInfo(CORDB_ADDRESS_TYPE *stubAddress)
1236312363
// If its not a stub, then maybe its an address in mscoree?
1236412364
if (result == false)
1236512365
{
12366-
result = (IsIPInModule(g_hThisInst, (PCODE)stubAddress) == TRUE);
12366+
result = (IsIPInModule(GetClrModuleBase(), (PCODE)stubAddress) == TRUE);
1236712367
}
1236812368

1236912369
// This is a synchronous event (reply required)

src/coreclr/src/dlls/mscordac/mscordac_unixexports.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ nativeStringResourceTable_mscorrc
3030
#PAL_fwprintf
3131
#PAL_GetLogicalCpuCountFromOS
3232
#PAL_GetNumaProcessorNode
33-
#PAL_GetPALDirectoryW
3433
#PAL_get_stdout
3534
#PAL_get_stderr
3635
#PAL_GetApplicationGroupId
3736
#PAL_GetTransportName
3837
#PAL_GetCurrentThread
3938
#PAL_GetCpuLimit
4039
#PAL_GetNativeExceptionHolderHead
40+
#PAL_GetPalHostModule
4141
#PAL_GetSymbolModuleBase
4242
#PAL_GetTransportPipeName
4343
#PAL_InitializeDLL

0 commit comments

Comments
 (0)