Skip to content

Commit

Permalink
Add TLS data process slot
Browse files Browse the repository at this point in the history
  • Loading branch information
tishion committed Dec 13, 2021
1 parent ef5a205 commit c2b8daa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
5 changes: 0 additions & 5 deletions demo/demo-mmloader/demo-mmloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,6 @@ main() {

unsigned char buf[MAX_PATH] = {0};

//Load the module with LoadLibrary for debug
HMODULE m = ::LoadLibraryA(szDllPath);
Type_TargetFunction f = (Type_TargetFunction)::GetProcAddress(m, "demoFunction");
f(buf, MAX_PATH);

if (pfnFunction(buf, MAX_PATH)) {
char *p = "{f56fee02-16d1-44a3-b191-4d7535f92ca5}";
iRet = ::memcmp(buf, p, strlen(p));
Expand Down
15 changes: 11 additions & 4 deletions src/mmLoader/mmLoader.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ BOOL
SetMemProtectStatus(PMEM_MODULE pMemModule);

BOOL
ExecuteTLSCallback(PMEM_MODULE pMemModule);
HandleTlsData(PMEM_MODULE pMemModule);

BOOL
CallModuleEntry(PMEM_MODULE pMemModule, DWORD dwReason);
Expand Down Expand Up @@ -178,7 +178,8 @@ LoadMemModuleInternal(PMEM_MODULE pMemModule, LPVOID lpPeModuleBuffer, BOOL bCal
return FALSE;
}

if (FALSE == ExecuteTLSCallback(pMemModule))
// process tls data
if (FALSE == HandleTlsData(pMemModule))
return FALSE;

if (bCallEntry) {
Expand Down Expand Up @@ -889,12 +890,12 @@ SetMemProtectStatus(PMEM_MODULE pMemModule) {
}

/// <summary>
/// Executes the TLS callback function.
/// Processes TLS data
/// </summary>
/// <param name="pMemModule">The <see cref="MemModule" /> instance.</param>
/// <returns>True if successful.</returns>
BOOL
ExecuteTLSCallback(PMEM_MODULE pMemModule) {
HandleTlsData(PMEM_MODULE pMemModule) {
if (NULL == pMemModule || NULL == pMemModule->pImageDosHeader)
return FALSE;

Expand All @@ -906,6 +907,12 @@ ExecuteTLSCallback(PMEM_MODULE pMemModule) {
return TRUE;

PIMAGE_TLS_DIRECTORY tls = (PIMAGE_TLS_DIRECTORY)(pMemModule->iBase + imageDirectoryEntryTls.VirtualAddress);

// TO-DO
// here we need to process the TLS data for all running threads, this is very heavy and danger operation
// refer to: http://www.nynaeve.net/?p=189

// execute tls callback if any
PIMAGE_TLS_CALLBACK *callback = (PIMAGE_TLS_CALLBACK *)tls->AddressOfCallBacks;
if (callback) {
while (*callback) {
Expand Down

0 comments on commit c2b8daa

Please sign in to comment.