Introduce reusable TLS in x64dbg.dll

This commit is contained in:
Duncan Ogilvie 2024-10-07 22:34:37 +02:00
parent 5ec6023cac
commit 6e976ab67b
3 changed files with 66 additions and 6 deletions

View File

@ -5,10 +5,13 @@
*/
#include "debugger.h"
#include "threading.h"
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
if(fdwReason == DLL_PROCESS_ATTACH)
switch(fdwReason)
{
case DLL_PROCESS_ATTACH:
{
hInst = hinstDLL;
@ -27,9 +30,12 @@ extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
szProgramDir[len] = 0;
}
}
}
// https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-disablethreadlibrarycalls
DisableThreadLibraryCalls(hinstDLL);
case DLL_THREAD_ATTACH:
{
}
}
return TRUE;
}

View File

@ -145,3 +145,48 @@ void SectionLockerGlobal::Deinitialize()
m_Initialized = false;
}
static DWORD gTlsIndex = TLS_OUT_OF_INDEXES;
TLSData::TLSData()
{
moduleHashLower.reserve(MAX_MODULE_SIZE);
}
bool TLSData::notify(DWORD fdwReason)
{
switch(fdwReason)
{
case DLL_PROCESS_ATTACH:
gTlsIndex = TlsAlloc();
return gTlsIndex != TLS_OUT_OF_INDEXES;
case DLL_THREAD_DETACH:
{
auto data = (TLSData*)TlsGetValue(gTlsIndex);
delete data;
}
return true;
case DLL_PROCESS_DETACH:
{
auto data = (TLSData*)TlsGetValue(gTlsIndex);
delete data;
TlsFree(gTlsIndex);
}
return true;
}
return false;
}
TLSData* TLSData::get()
{
auto data = (TLSData*)TlsGetValue(gTlsIndex);
if(data == nullptr)
{
data = new TLSData();
TlsSetValue(gTlsIndex, data);
}
return data;
}

View File

@ -1,5 +1,4 @@
#ifndef _THREADING_H
#define _THREADING_H
#pragma once
#include "_global.h"
@ -256,4 +255,14 @@ private:
using Internal = SectionLockerGlobal;
};
#endif // _THREADING_H
struct TLSData
{
String moduleHashLower;
TLSData();
TLSData(const TLSData &) = delete;
TLSData & operator=(const TLSData &) = delete;
static bool notify(DWORD fdwReason);
static TLSData* get();
};