From 04516b246cbfd79f00a89eee7f12d77f2d346f04 Mon Sep 17 00:00:00 2001 From: Luke Street Date: Sat, 4 Oct 2025 21:26:46 -0600 Subject: [PATCH] More logging & some hacks --- dll/kernel32/processthreadsapi.cpp | 7 ++++++- dll/kernel32/synchapi.cpp | 20 ++++++++++---------- src/module_registry.cpp | 7 +++++++ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/dll/kernel32/processthreadsapi.cpp b/dll/kernel32/processthreadsapi.cpp index 624c7e3..f179160 100644 --- a/dll/kernel32/processthreadsapi.cpp +++ b/dll/kernel32/processthreadsapi.cpp @@ -144,11 +144,16 @@ void *threadTrampoline(void *param) { { std::unique_lock lk(data.obj->m); data.obj->tib = threadTib; - data.obj->cv.wait(lk, [&] { return data.obj->suspendCount == 0; }); + if (data.obj->suspendCount) { + DEBUG_LOG("Thread is suspended at start; waiting...\n"); + data.obj->cv.wait(lk, [&] { return data.obj->suspendCount == 0; }); + } } wibo::notifyDllThreadAttach(); + DEBUG_LOG("Calling thread entry %p with userData %p\n", data.entry, data.userData); DWORD result = data.entry ? data.entry(data.userData) : 0; + DEBUG_LOG("Thread exiting with code %u\n", result); { std::lock_guard lk(data.obj->m); data.obj->exitCode = result; diff --git a/dll/kernel32/synchapi.cpp b/dll/kernel32/synchapi.cpp index db3d27a..e35b9f5 100644 --- a/dll/kernel32/synchapi.cpp +++ b/dll/kernel32/synchapi.cpp @@ -43,7 +43,7 @@ void WIN_FUNC Sleep(DWORD dwMilliseconds) { } HANDLE WIN_FUNC CreateMutexW(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, LPCWSTR lpName) { - DEBUG_LOG("CreateMutexW(%p, %d, %ls)\n", lpMutexAttributes, static_cast(bInitialOwner), + DEBUG_LOG("CreateMutexW(%p, %d, %s)\n", lpMutexAttributes, static_cast(bInitialOwner), wideStringToString(lpName).c_str()); std::u16string name = makeU16String(lpName); const uint32_t grantedAccess = MUTEX_ALL_ACCESS; @@ -110,7 +110,7 @@ BOOL WIN_FUNC ReleaseMutex(HANDLE hMutex) { HANDLE WIN_FUNC CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCWSTR lpName) { - DEBUG_LOG("CreateEventW(%p, %d, %d, %ls)\n", lpEventAttributes, static_cast(bManualReset), + DEBUG_LOG("CreateEventW(%p, %d, %d, %s)\n", lpEventAttributes, static_cast(bManualReset), static_cast(bInitialState), wideStringToString(lpName).c_str()); std::u16string name = makeU16String(lpName); const uint32_t grantedAccess = EVENT_ALL_ACCESS; @@ -118,12 +118,9 @@ HANDLE WIN_FUNC CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManu if (lpEventAttributes && lpEventAttributes->bInheritHandle) { handleFlags |= HANDLE_FLAG_INHERIT; } - auto [ev, alreadyExists] = wibo::g_namespace.getOrCreate(name, [&]() { - auto e = new EventObject(!!bManualReset); - if (bInitialState) { - std::lock_guard lk(e->m); - e->signaled.store(true, std::memory_order_relaxed); - } + auto [ev, created] = wibo::g_namespace.getOrCreate(name, [&]() { + auto e = new EventObject(bManualReset); + e->signaled.store(bInitialState, std::memory_order_relaxed); return e; }); if (!ev) { @@ -132,7 +129,8 @@ HANDLE WIN_FUNC CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManu return nullptr; } HANDLE h = wibo::handles().alloc(std::move(ev), grantedAccess, handleFlags); - wibo::lastError = alreadyExists ? ERROR_ALREADY_EXISTS : ERROR_SUCCESS; + DEBUG_LOG("-> %p (created=%d)\n", h, created ? 1 : 0); + wibo::lastError = created ? ERROR_SUCCESS : ERROR_ALREADY_EXISTS; return h; } @@ -147,7 +145,7 @@ HANDLE WIN_FUNC CreateEventA(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManu HANDLE WIN_FUNC CreateSemaphoreW(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, LONG lInitialCount, LONG lMaximumCount, LPCWSTR lpName) { - DEBUG_LOG("CreateSemaphoreW(%p, %ld, %ld, %ls)\n", lpSemaphoreAttributes, lInitialCount, lMaximumCount, + DEBUG_LOG("CreateSemaphoreW(%p, %ld, %ld, %s)\n", lpSemaphoreAttributes, lInitialCount, lMaximumCount, wideStringToString(lpName).c_str()); auto name = makeU16String(lpName); const uint32_t granted = SEMAPHORE_ALL_ACCESS; @@ -266,6 +264,8 @@ DWORD WIN_FUNC WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds) { } }; + DEBUG_LOG("Waiting on object with type %d\n", static_cast(obj->type)); + switch (obj->type) { case ObjectType::Event: { auto ev = std::move(obj).downcast(); diff --git a/src/module_registry.cpp b/src/module_registry.cpp index 5b4b498..bf3a37b 100644 --- a/src/module_registry.cpp +++ b/src/module_registry.cpp @@ -854,11 +854,13 @@ ModuleInfo *loadModule(const char *dllName) { registerExternalModuleAliases(*reg, requested, files::canonicalPath(path), info); return info; } + reg.lock.unlock(); DEBUG_LOG(" loading external module from %s\n", path.c_str()); FILE *file = fopen(path.c_str(), "rb"); if (!file) { perror("loadModule"); + reg.lock.lock(); diskError = ERROR_MOD_NOT_FOUND; return nullptr; } @@ -867,6 +869,7 @@ ModuleInfo *loadModule(const char *dllName) { if (!executable->loadPE(file, true)) { DEBUG_LOG(" loadPE failed for %s\n", path.c_str()); fclose(file); + reg.lock.lock(); diskError = ERROR_BAD_EXE_FORMAT; return nullptr; } @@ -881,16 +884,20 @@ ModuleInfo *loadModule(const char *dllName) { info->executable = std::move(executable); info->refCount = 1; + reg.lock.lock(); ModuleInfo *raw = info.get(); reg->modulesByKey[key] = std::move(info); registerExternalModuleAliases(*reg, requested, raw->resolvedPath, raw); + reg.lock.unlock(); ensureExportsInitialized(*raw); if (!raw->executable->resolveImports()) { DEBUG_LOG(" resolveImports failed for %s\n", raw->originalName.c_str()); + reg.lock.lock(); reg->modulesByKey.erase(key); diskError = wibo::lastError; return nullptr; } + reg.lock.lock(); if (!callDllMain(*raw, DLL_PROCESS_ATTACH, nullptr)) { DEBUG_LOG(" DllMain failed for %s\n", raw->originalName.c_str()); runPendingOnExit(*raw);