Formatting, fixes, deduplication

This commit is contained in:
Luke Street 2025-09-28 17:00:38 -06:00
parent 2732bd584a
commit bc33bae659
11 changed files with 293 additions and 345 deletions

View File

@ -1,7 +1,8 @@
#include "common.h" #include "common.h"
#include "handles.h" #include "handles.h"
#include "strutil.h"
#include <algorithm>
#include <sys/random.h> #include <sys/random.h>
#include <cwchar>
#include <vector> #include <vector>
namespace { namespace {
@ -294,7 +295,8 @@ namespace advapi32 {
return TRUE; return TRUE;
} }
BOOL WIN_FUNC CryptAcquireContextW(void** phProv, const wchar_t* pszContainer, const wchar_t* pszProvider, unsigned int dwProvType, unsigned int dwFlags){ BOOL WIN_FUNC CryptAcquireContextW(void **phProv, const uint16_t *pszContainer, const uint16_t *pszProvider,
unsigned int dwProvType, unsigned int dwFlags) {
DEBUG_LOG("STUB: CryptAcquireContextW(%p)\n", phProv); DEBUG_LOG("STUB: CryptAcquireContextW(%p)\n", phProv);
// to quote the guy above me: screw them for now // to quote the guy above me: screw them for now
@ -494,7 +496,7 @@ namespace advapi32 {
if (TokenInformationClass == TokenUserClass) { if (TokenInformationClass == TokenUserClass) {
constexpr size_t sidSize = sizeof(Sid); constexpr size_t sidSize = sizeof(Sid);
constexpr size_t tokenUserSize = sizeof(TokenUserData); constexpr size_t tokenUserSize = sizeof(TokenUserData);
const unsigned int required = static_cast<unsigned int>(tokenUserSize + sidSize); const auto required = static_cast<unsigned int>(tokenUserSize + sidSize);
*ReturnLength = required; *ReturnLength = required;
if (!TokenInformation || TokenInformationLength < required) { if (!TokenInformation || TokenInformationLength < required) {
wibo::lastError = ERROR_INSUFFICIENT_BUFFER; wibo::lastError = ERROR_INSUFFICIENT_BUFFER;
@ -544,8 +546,11 @@ namespace advapi32 {
return FALSE; return FALSE;
} }
BOOL WIN_FUNC LookupAccountSidW(const wchar_t *lpSystemName, const void *sidPointer, wchar_t *Name, unsigned long *cchName, wchar_t *ReferencedDomainName, unsigned long *cchReferencedDomainName, SID_NAME_USE *peUse) { BOOL WIN_FUNC LookupAccountSidW(const uint16_t *lpSystemName, const void *sidPointer, uint16_t *Name,
DEBUG_LOG("LookupAccountSidW(system=%ls, sid=%p)\n", lpSystemName ? lpSystemName : L"(null)", sidPointer); unsigned long *cchName, uint16_t *ReferencedDomainName,
unsigned long *cchReferencedDomainName, SID_NAME_USE *peUse) {
std::string systemName = lpSystemName ? wideStringToString(lpSystemName) : std::string("(null)");
DEBUG_LOG("LookupAccountSidW(system=%s, sid=%p)\n", systemName.c_str(), sidPointer);
(void) lpSystemName; // Only local lookup supported (void) lpSystemName; // Only local lookup supported
if (!sidPointer || !cchName || !cchReferencedDomainName || !peUse) { if (!sidPointer || !cchName || !cchReferencedDomainName || !peUse) {
wibo::lastError = ERROR_INVALID_PARAMETER; wibo::lastError = ERROR_INVALID_PARAMETER;
@ -556,18 +561,18 @@ namespace advapi32 {
wibo::lastError = ERROR_NONE_MAPPED; wibo::lastError = ERROR_NONE_MAPPED;
return FALSE; return FALSE;
} }
const wchar_t *accountName = L"SYSTEM"; static constexpr uint16_t accountName[] = {u'S', u'Y', u'S', u'T', u'E', u'M', u'\0'};
const wchar_t *domainName = L"NT AUTHORITY"; static constexpr uint16_t domainName[] = {u'N', u'T', u' ', u'A', u'U', u'T', u'H', u'O', u'R', u'I', u'T', u'Y', u'\0'};
unsigned long requiredAccount = static_cast<unsigned long>(std::wcslen(accountName) + 1); unsigned long requiredAccount = wstrlen(accountName) + 1;
unsigned long requiredDomain = static_cast<unsigned long>(std::wcslen(domainName) + 1); unsigned long requiredDomain = wstrlen(domainName) + 1;
if (!Name || *cchName < requiredAccount || !ReferencedDomainName || *cchReferencedDomainName < requiredDomain) { if (!Name || *cchName < requiredAccount || !ReferencedDomainName || *cchReferencedDomainName < requiredDomain) {
*cchName = requiredAccount; *cchName = requiredAccount;
*cchReferencedDomainName = requiredDomain; *cchReferencedDomainName = requiredDomain;
wibo::lastError = ERROR_INSUFFICIENT_BUFFER; wibo::lastError = ERROR_INSUFFICIENT_BUFFER;
return FALSE; return FALSE;
} }
std::wmemcpy(Name, accountName, requiredAccount); std::copy_n(accountName, requiredAccount, Name);
std::wmemcpy(ReferencedDomainName, domainName, requiredDomain); std::copy_n(domainName, requiredDomain, ReferencedDomainName);
*peUse = SidTypeWellKnownGroup; *peUse = SidTypeWellKnownGroup;
*cchName = requiredAccount - 1; *cchName = requiredAccount - 1;
*cchReferencedDomainName = requiredDomain - 1; *cchReferencedDomainName = requiredDomain - 1;

View File

@ -5,17 +5,18 @@
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <vector>
#include <unistd.h> #include <unistd.h>
#include <vector>
typedef void (*_PVFV)(); typedef void (*_PVFV)();
typedef int (*_PIFV)(); typedef int (*_PIFV)();
typedef void (*_invalid_parameter_handler)(const wchar_t *, const wchar_t *, const wchar_t *, unsigned int, uintptr_t); typedef void (*_invalid_parameter_handler)(const uint16_t *, const uint16_t *, const uint16_t *, unsigned int,
uintptr_t);
extern char **environ; extern char **environ;
namespace msvcrt { namespace msvcrt {
int WIN_ENTRY puts(const char *str); int WIN_ENTRY puts(const char *str);
} }
typedef enum _crt_app_type { typedef enum _crt_app_type {
@ -195,11 +196,13 @@ void *WIN_ENTRY __acrt_iob_func(unsigned int index) {
return nullptr; return nullptr;
} }
int WIN_ENTRY __stdio_common_vfprintf(unsigned long long /*options*/, FILE *stream, const char *format, void * /*locale*/, va_list args) { int WIN_ENTRY __stdio_common_vfprintf(unsigned long long /*options*/, FILE *stream, const char *format,
void * /*locale*/, va_list args) {
return vfprintf(stream, format, args); return vfprintf(stream, format, args);
} }
int WIN_ENTRY __stdio_common_vsprintf(unsigned long long /*options*/, char *buffer, size_t len, const char *format, void * /*locale*/, va_list args) { int WIN_ENTRY __stdio_common_vsprintf(unsigned long long /*options*/, char *buffer, size_t len, const char *format,
void * /*locale*/, va_list args) {
if (!buffer || !format) if (!buffer || !format)
return -1; return -1;
int result = vsnprintf(buffer, len, format, args); int result = vsnprintf(buffer, len, format, args);

View File

@ -349,37 +349,11 @@ namespace kernel32 {
std::memset(lpSystemInfo, 0, sizeof(*lpSystemInfo)); std::memset(lpSystemInfo, 0, sizeof(*lpSystemInfo));
WORD architecture = PROCESSOR_ARCHITECTURE_UNKNOWN; lpSystemInfo->wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
DWORD processorType = 0;
WORD processorLevel = 0;
#if defined(__x86_64__) || defined(_M_X64)
architecture = PROCESSOR_ARCHITECTURE_AMD64;
processorType = PROCESSOR_AMD_X8664;
processorLevel = 6;
#elif defined(__i386__) || defined(_M_IX86)
architecture = PROCESSOR_ARCHITECTURE_INTEL;
processorType = PROCESSOR_INTEL_PENTIUM;
processorLevel = 6;
#elif defined(__aarch64__)
architecture = PROCESSOR_ARCHITECTURE_ARM64;
processorType = 0;
processorLevel = 8;
#elif defined(__arm__)
architecture = PROCESSOR_ARCHITECTURE_ARM;
processorType = 0;
processorLevel = 7;
#else
architecture = PROCESSOR_ARCHITECTURE_UNKNOWN;
processorType = 0;
processorLevel = 0;
#endif
lpSystemInfo->wProcessorArchitecture = architecture;
lpSystemInfo->wReserved = 0; lpSystemInfo->wReserved = 0;
lpSystemInfo->dwOemId = lpSystemInfo->wProcessorArchitecture; lpSystemInfo->dwOemId = lpSystemInfo->wProcessorArchitecture;
lpSystemInfo->dwProcessorType = processorType; lpSystemInfo->dwProcessorType = PROCESSOR_INTEL_PENTIUM;
lpSystemInfo->wProcessorLevel = processorLevel; lpSystemInfo->wProcessorLevel = 6; // Pentium
lpSystemInfo->wProcessorRevision = 0; lpSystemInfo->wProcessorRevision = 0;
long pageSize = sysconf(_SC_PAGESIZE); long pageSize = sysconf(_SC_PAGESIZE);

View File

@ -163,10 +163,8 @@ namespace msvcrt {
} }
} }
if (!exeDir.empty()) { if (!exeDir.empty()) {
std::string loweredResult = result; std::string loweredResult = stringToLower(result);
std::string loweredExe = exeDir; std::string loweredExe = stringToLower(exeDir);
std::transform(loweredResult.begin(), loweredResult.end(), loweredResult.begin(), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
std::transform(loweredExe.begin(), loweredExe.end(), loweredExe.begin(), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
bool present = false; bool present = false;
size_t start = 0; size_t start = 0;
while (start <= loweredResult.size()) { while (start <= loweredResult.size()) {
@ -648,18 +646,6 @@ char* WIN_ENTRY setlocale(int category, const char *locale){
return 0; return 0;
} }
static uint16_t toLower(uint16_t ch) {
if (ch >= 'A' && ch <= 'Z') {
return static_cast<uint16_t>(ch + ('a' - 'A'));
}
wchar_t wide = static_cast<wchar_t>(ch);
wchar_t lowered = std::towlower(wide);
if (lowered < 0 || lowered > 0xFFFF) {
return ch;
}
return static_cast<uint16_t>(lowered);
}
int WIN_ENTRY _wcsicmp(const uint16_t *lhs, const uint16_t *rhs) { int WIN_ENTRY _wcsicmp(const uint16_t *lhs, const uint16_t *rhs) {
if (lhs == rhs) { if (lhs == rhs) {
return 0; return 0;
@ -672,15 +658,15 @@ char* WIN_ENTRY setlocale(int category, const char *locale){
} }
while (*lhs && *rhs) { while (*lhs && *rhs) {
uint16_t a = toLower(*lhs++); uint16_t a = wcharToLower(*lhs++);
uint16_t b = toLower(*rhs++); uint16_t b = wcharToLower(*rhs++);
if (a != b) { if (a != b) {
return static_cast<int>(a) - static_cast<int>(b); return static_cast<int>(a) - static_cast<int>(b);
} }
} }
uint16_t a = toLower(*lhs); uint16_t a = wcharToLower(*lhs);
uint16_t b = toLower(*rhs); uint16_t b = wcharToLower(*rhs);
return static_cast<int>(a) - static_cast<int>(b); return static_cast<int>(a) - static_cast<int>(b);
} }

View File

@ -2,7 +2,7 @@
#include "handles.h" #include "handles.h"
namespace psapi { namespace psapi {
BOOL WIN_FUNC EnumProcessModules(HANDLE hProcess, HMODULE *lphModule, DWORD cb, DWORD *lpcbNeeded) { BOOL WIN_FUNC EnumProcessModules(HANDLE hProcess, HMODULE *lphModule, DWORD cb, DWORD *lpcbNeeded) {
DEBUG_LOG("EnumProcessModules(hProcess=%p, cb=%u)\n", hProcess, cb); DEBUG_LOG("EnumProcessModules(hProcess=%p, cb=%u)\n", hProcess, cb);
bool recognizedHandle = false; bool recognizedHandle = false;
@ -36,21 +36,21 @@ namespace psapi {
lphModule[0] = currentModule; lphModule[0] = currentModule;
wibo::lastError = ERROR_SUCCESS; wibo::lastError = ERROR_SUCCESS;
return TRUE; return TRUE;
}
} }
} // namespace psapi
static void *resolveByName(const char *name) { static void *resolveByName(const char *name) {
DEBUG_LOG("psapi resolveByName(%s)\n", name); if (strcmp(name, "EnumProcessModules") == 0)
if (strcmp(name, "EnumProcessModules") == 0) return (void *) psapi::EnumProcessModules; return (void *)psapi::EnumProcessModules;
if (strcmp(name, "K32EnumProcessModules") == 0) return (void *) psapi::EnumProcessModules; if (strcmp(name, "K32EnumProcessModules") == 0)
return (void *)psapi::EnumProcessModules;
return nullptr; return nullptr;
} }
static void *resolveByOrdinal(uint16_t ordinal) { static void *resolveByOrdinal(uint16_t ordinal) {
DEBUG_LOG("psapi resolveByOrdinal(%u)\n", ordinal);
switch (ordinal) { switch (ordinal) {
case 4: // EnumProcessModules case 4: // EnumProcessModules
return (void *) psapi::EnumProcessModules; return (void *)psapi::EnumProcessModules;
default: default:
return nullptr; return nullptr;
} }

View File

@ -128,14 +128,8 @@ BindingHandleData *getBinding(RPC_BINDING_HANDLE handle) {
extern "C" { extern "C" {
RPC_STATUS WIN_FUNC RpcStringBindingComposeW( RPC_STATUS WIN_FUNC RpcStringBindingComposeW(RPC_WSTR objUuid, RPC_WSTR protSeq, RPC_WSTR networkAddr,
RPC_WSTR objUuid, RPC_WSTR endpoint, RPC_WSTR options, RPC_WSTR *stringBinding) {
RPC_WSTR protSeq,
RPC_WSTR networkAddr,
RPC_WSTR endpoint,
RPC_WSTR options,
RPC_WSTR *stringBinding
) {
BindingComponents components; BindingComponents components;
components.objectUuid = toU16(objUuid); components.objectUuid = toU16(objUuid);
components.protocolSequence = toU16(protSeq); components.protocolSequence = toU16(protSeq);
@ -187,15 +181,10 @@ RPC_STATUS WIN_FUNC RpcBindingFromStringBindingW(RPC_WSTR stringBinding, RPC_BIN
return RPC_S_OK; return RPC_S_OK;
} }
RPC_STATUS WIN_FUNC RpcBindingSetAuthInfoExW( RPC_STATUS WIN_FUNC RpcBindingSetAuthInfoExW(RPC_BINDING_HANDLE binding, RPC_WSTR serverPrincName,
RPC_BINDING_HANDLE binding, unsigned long authnLevel, unsigned long authnSvc,
RPC_WSTR serverPrincName, RPC_AUTH_IDENTITY_HANDLE authIdentity, unsigned long authzSvc,
unsigned long authnLevel, RPC_SECURITY_QOS *securityQos) {
unsigned long authnSvc,
RPC_AUTH_IDENTITY_HANDLE authIdentity,
unsigned long authzSvc,
RPC_SECURITY_QOS *securityQos
) {
BindingHandleData *data = getBinding(binding); BindingHandleData *data = getBinding(binding);
if (!data) { if (!data) {
return RPC_S_INVALID_BINDING; return RPC_S_INVALID_BINDING;
@ -260,9 +249,7 @@ NdrClientCall2(PMIDL_STUB_DESC stubDescriptor, PFORMAT_STRING format, ...) {
return result; return result;
} }
void WIN_FUNC NdrServerCall2(PRPC_MESSAGE message) { void WIN_FUNC NdrServerCall2(PRPC_MESSAGE message) { DEBUG_LOG("STUB: NdrServerCall2 message=%p\n", message); }
DEBUG_LOG("STUB: NdrServerCall2 message=%p\n", message);
}
} // extern "C" } // extern "C"
@ -270,19 +257,19 @@ namespace {
void *resolveByName(const char *name) { void *resolveByName(const char *name) {
if (std::strcmp(name, "RpcStringBindingComposeW") == 0) if (std::strcmp(name, "RpcStringBindingComposeW") == 0)
return (void *) RpcStringBindingComposeW; return (void *)RpcStringBindingComposeW;
if (std::strcmp(name, "RpcBindingFromStringBindingW") == 0) if (std::strcmp(name, "RpcBindingFromStringBindingW") == 0)
return (void *) RpcBindingFromStringBindingW; return (void *)RpcBindingFromStringBindingW;
if (std::strcmp(name, "RpcStringFreeW") == 0) if (std::strcmp(name, "RpcStringFreeW") == 0)
return (void *) RpcStringFreeW; return (void *)RpcStringFreeW;
if (std::strcmp(name, "RpcBindingFree") == 0) if (std::strcmp(name, "RpcBindingFree") == 0)
return (void *) RpcBindingFree; return (void *)RpcBindingFree;
if (std::strcmp(name, "RpcBindingSetAuthInfoExW") == 0) if (std::strcmp(name, "RpcBindingSetAuthInfoExW") == 0)
return (void *) RpcBindingSetAuthInfoExW; return (void *)RpcBindingSetAuthInfoExW;
if (std::strcmp(name, "NdrClientCall2") == 0) if (std::strcmp(name, "NdrClientCall2") == 0)
return (void *) NdrClientCall2; return (void *)NdrClientCall2;
if (std::strcmp(name, "NdrServerCall2") == 0) if (std::strcmp(name, "NdrServerCall2") == 0)
return (void *) NdrServerCall2; return (void *)NdrServerCall2;
return nullptr; return nullptr;
} }

View File

@ -3,8 +3,6 @@
#include "resources.h" #include "resources.h"
#include "strutil.h" #include "strutil.h"
#include <algorithm>
#include <cctype>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <filesystem> #include <filesystem>
@ -15,7 +13,7 @@ namespace {
constexpr uint32_t RT_VERSION = 16; constexpr uint32_t RT_VERSION = 16;
static uint16_t read_u16(const uint8_t *ptr) { static uint16_t readU16(const uint8_t *ptr) {
return static_cast<uint16_t>(ptr[0] | (ptr[1] << 8)); return static_cast<uint16_t>(ptr[0] | (ptr[1] << 8));
} }
@ -49,9 +47,9 @@ static bool parseVersionBlock(const uint8_t *block, size_t available, VersionBlo
return false; return false;
} }
uint16_t totalLength = read_u16(block); uint16_t totalLength = readU16(block);
uint16_t valueLength = read_u16(block + sizeof(uint16_t)); uint16_t valueLength = readU16(block + sizeof(uint16_t));
uint16_t type = read_u16(block + sizeof(uint16_t) * 2); uint16_t type = readU16(block + sizeof(uint16_t) * 2);
if (totalLength == 0 || totalLength > available) { if (totalLength == 0 || totalLength > available) {
DEBUG_LOG("invalid totalLength=%u available=%zu\n", totalLength, available); DEBUG_LOG("invalid totalLength=%u available=%zu\n", totalLength, available);
return false; return false;
@ -61,7 +59,7 @@ static bool parseVersionBlock(const uint8_t *block, size_t available, VersionBlo
const uint8_t *cursor = block + sizeof(uint16_t) * 3; const uint8_t *cursor = block + sizeof(uint16_t) * 3;
out.key.clear(); out.key.clear();
while (cursor + sizeof(uint16_t) <= end) { while (cursor + sizeof(uint16_t) <= end) {
uint16_t ch = read_u16(cursor); uint16_t ch = readU16(cursor);
cursor += sizeof(uint16_t); cursor += sizeof(uint16_t);
if (!ch) if (!ch)
break; break;
@ -101,11 +99,6 @@ static bool parseVersionBlock(const uint8_t *block, size_t available, VersionBlo
return true; return true;
} }
static std::string toLowerCopy(std::string str) {
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
return str;
}
static bool queryVersionBlock(const uint8_t *block, size_t available, static bool queryVersionBlock(const uint8_t *block, size_t available,
const std::vector<std::string> &segments, const std::vector<std::string> &segments,
size_t depth, size_t depth,
@ -126,7 +119,7 @@ static bool queryVersionBlock(const uint8_t *block, size_t available,
return true; return true;
} }
const std::string targetLower = toLowerCopy(segments[depth]); const std::string targetLower = stringToLower(segments[depth]);
const uint8_t *cursor = view.childrenPtr; const uint8_t *cursor = view.childrenPtr;
const uint8_t *end = view.childrenPtr + view.childrenBytes; const uint8_t *end = view.childrenPtr + view.childrenBytes;
@ -137,12 +130,12 @@ static bool queryVersionBlock(const uint8_t *block, size_t available,
break; break;
if (child.totalLength == 0) if (child.totalLength == 0)
break; break;
std::string childKeyLower = toLowerCopy(narrowKey(child.key)); std::string childKeyLower = stringToLower(narrowKey(child.key));
if (childKeyLower == targetLower) { if (childKeyLower == targetLower) {
if (queryVersionBlock(childStart, child.totalLength, segments, depth + 1, outPtr, outLen, outType)) if (queryVersionBlock(childStart, child.totalLength, segments, depth + 1, outPtr, outLen, outType))
return true; return true;
} }
size_t offset = static_cast<size_t>(child.totalLength); const auto offset = static_cast<size_t>(child.totalLength);
cursor = childStart + align4(offset); cursor = childStart + align4(offset);
if (cursor <= childStart || cursor > end) if (cursor <= childStart || cursor > end)
break; break;
@ -252,7 +245,7 @@ static unsigned int VerQueryValueImpl(const void *pBlock, const std::string &sub
return 0; return 0;
const uint8_t *base = static_cast<const uint8_t *>(pBlock); const uint8_t *base = static_cast<const uint8_t *>(pBlock);
uint16_t totalLength = read_u16(base); uint16_t totalLength = readU16(base);
if (totalLength < 6) if (totalLength < 6)
return 0; return 0;

View File

@ -1,8 +1,8 @@
#include "common.h" #include "common.h"
#include "files.h" #include "files.h"
#include "handles.h" #include "handles.h"
#include "strutil.h"
#include <algorithm> #include <algorithm>
#include <cctype>
#include <map> #include <map>
#include <optional> #include <optional>
#include <strings.h> #include <strings.h>
@ -182,13 +182,13 @@ namespace files {
return std::nullopt; return std::nullopt;
} }
std::string needle = filename; std::string needle = filename;
std::transform(needle.begin(), needle.end(), needle.begin(), [](unsigned char ch) { return std::tolower(ch); }); toLowerInPlace(needle);
for (const auto &entry : std::filesystem::directory_iterator(directory, ec)) { for (const auto &entry : std::filesystem::directory_iterator(directory, ec)) {
if (ec) { if (ec) {
break; break;
} }
std::string candidate = entry.path().filename().string(); std::string candidate = entry.path().filename().string();
std::transform(candidate.begin(), candidate.end(), candidate.begin(), [](unsigned char ch) { return std::tolower(ch); }); toLowerInPlace(candidate);
if (candidate == needle) { if (candidate == needle) {
return canonicalPath(entry.path()); return canonicalPath(entry.path());
} }

View File

@ -4,7 +4,6 @@
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <cctype>
#include <cerrno> #include <cerrno>
#include <climits> #include <climits>
#include <cstdio> #include <cstdio>
@ -50,45 +49,29 @@ struct PEExportDirectory {
uint32_t addressOfNameOrdinals; uint32_t addressOfNameOrdinals;
}; };
#define FOR_256_3(a, b, c, d) FOR_ITER((a << 6 | b << 4 | c << 2 | d)) using StubFuncType = void (*)();
#define FOR_256_2(a, b) \ constexpr size_t MAX_STUBS = 0x100;
FOR_256_3(a, b, 0, 0) \ size_t stubIndex = 0;
FOR_256_3(a, b, 0, 1) \ std::array<std::string, MAX_STUBS> stubDlls;
FOR_256_3(a, b, 0, 2) \ std::array<std::string, MAX_STUBS> stubFuncNames;
FOR_256_3(a, b, 0, 3) FOR_256_3(a, b, 1, 0) FOR_256_3(a, b, 1, 1) FOR_256_3(a, b, 1, 2) FOR_256_3(a, b, 1, 3) \ std::unordered_map<std::string, StubFuncType> stubCache;
FOR_256_3(a, b, 2, 0) FOR_256_3(a, b, 2, 1) FOR_256_3(a, b, 2, 2) FOR_256_3(a, b, 2, 3) FOR_256_3(a, b, 3, 0) \
FOR_256_3(a, b, 3, 1) FOR_256_3(a, b, 3, 2) FOR_256_3(a, b, 3, 3)
#define FOR_256 \
FOR_256_2(0, 0) \
FOR_256_2(0, 1) \
FOR_256_2(0, 2) \
FOR_256_2(0, 3) FOR_256_2(1, 0) FOR_256_2(1, 1) FOR_256_2(1, 2) FOR_256_2(1, 3) FOR_256_2(2, 0) FOR_256_2(2, 1) \
FOR_256_2(2, 2) FOR_256_2(2, 3) FOR_256_2(3, 0) FOR_256_2(3, 1) FOR_256_2(3, 2) FOR_256_2(3, 3)
static constexpr size_t MAX_STUBS = 0x100; std::string makeStubKey(const char *dllName, const char *funcName) {
static int stubIndex = 0;
static std::array<std::string, MAX_STUBS> stubDlls;
static std::array<std::string, MAX_STUBS> stubFuncNames;
static std::unordered_map<std::string, void *> stubCache;
static std::string makeStubKey(const char *dllName, const char *funcName) {
std::string key; std::string key;
if (dllName) { if (dllName) {
key.assign(dllName); key.assign(dllName);
std::transform(key.begin(), key.end(), key.begin(), toLowerInPlace(key);
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
} }
key.push_back(':'); key.push_back(':');
if (funcName) { if (funcName) {
std::string func(funcName); std::string func(funcName);
std::transform(func.begin(), func.end(), func.begin(), toLowerInPlace(func);
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
key += func; key += func;
} }
return key; return key;
} }
static void stubBase(int index) { void stubBase(size_t index) {
const char *func = stubFuncNames[index].empty() ? "<unknown>" : stubFuncNames[index].c_str(); const char *func = stubFuncNames[index].empty() ? "<unknown>" : stubFuncNames[index].c_str();
const char *dll = stubDlls[index].empty() ? "<unknown>" : stubDlls[index].c_str(); const char *dll = stubDlls[index].empty() ? "<unknown>" : stubDlls[index].c_str();
fprintf(stderr, "wibo: call reached missing import %s from %s\n", func, dll); fprintf(stderr, "wibo: call reached missing import %s from %s\n", func, dll);
@ -96,47 +79,42 @@ static void stubBase(int index) {
abort(); abort();
} }
void (*stubFuncs[MAX_STUBS])(void) = { template <size_t Index> void stubThunk() { stubBase(Index); }
#define FOR_ITER(i) []() { stubBase(i); },
FOR_256
#undef FOR_ITER
};
#undef FOR_256_3 template <size_t... Indices>
#undef FOR_256_2 constexpr std::array<void (*)(void), sizeof...(Indices)> makeStubTable(std::index_sequence<Indices...>) {
#undef FOR_256 return {{stubThunk<Indices>...}};
}
void *resolveMissingFuncName(const char *dllName, const char *funcName) { constexpr auto stubFuncs = makeStubTable(std::make_index_sequence<MAX_STUBS>{});
StubFuncType resolveMissingFuncName(const char *dllName, const char *funcName) {
DEBUG_LOG("Missing function: %s (%s)\n", dllName, funcName); DEBUG_LOG("Missing function: %s (%s)\n", dllName, funcName);
std::string key = makeStubKey(dllName, funcName); std::string key = makeStubKey(dllName, funcName);
auto existing = stubCache.find(key); auto existing = stubCache.find(key);
if (existing != stubCache.end()) { if (existing != stubCache.end()) {
return existing->second; return existing->second;
} }
if (stubIndex >= static_cast<int>(MAX_STUBS)) { if (stubIndex >= MAX_STUBS) {
fprintf(stderr, fprintf(stderr, "wibo: too many missing functions encountered (>%zu). Last failure: %s (%s)\n", MAX_STUBS,
"Too many missing functions encountered (>%zu). Last failure: %s (%s)\n", funcName, dllName);
MAX_STUBS, funcName, dllName); fflush(stderr);
exit(1); abort();
} }
stubFuncNames[stubIndex] = funcName ? funcName : ""; stubFuncNames[stubIndex] = funcName ? funcName : "";
stubDlls[stubIndex] = dllName ? dllName : ""; stubDlls[stubIndex] = dllName ? dllName : "";
void *stub = (void *)stubFuncs[stubIndex]; StubFuncType stub = stubFuncs[stubIndex];
stubCache.emplace(std::move(key), stub); stubCache.emplace(std::move(key), stub);
stubIndex++; stubIndex++;
return stub; return stub;
} }
void *resolveMissingFuncOrdinal(const char *dllName, uint16_t ordinal) { StubFuncType resolveMissingFuncOrdinal(const char *dllName, uint16_t ordinal) {
char buf[16]; char buf[16];
sprintf(buf, "%d", ordinal); sprintf(buf, "%d", ordinal);
return resolveMissingFuncName(dllName, buf); return resolveMissingFuncName(dllName, buf);
} }
} // namespace
namespace {
using ModulePtr = std::unique_ptr<wibo::ModuleInfo>; using ModulePtr = std::unique_ptr<wibo::ModuleInfo>;
struct ModuleRegistry { struct ModuleRegistry {
@ -152,23 +130,45 @@ struct ModuleRegistry {
std::unordered_set<wibo::ModuleInfo *> pinnedModules; std::unordered_set<wibo::ModuleInfo *> pinnedModules;
}; };
ModuleRegistry &registry() { struct LockedRegistry {
static ModuleRegistry reg; ModuleRegistry *reg;
return reg; std::unique_lock<std::recursive_mutex> lock;
}
std::string toLowerCopy(const std::string &value) { LockedRegistry(ModuleRegistry &registryRef, std::unique_lock<std::recursive_mutex> &&guard)
std::string out = value; : reg(&registryRef), lock(std::move(guard)) {}
std::transform(out.begin(), out.end(), out.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); }); LockedRegistry(const LockedRegistry &) = delete;
return out; LockedRegistry &operator=(const LockedRegistry &) = delete;
LockedRegistry(LockedRegistry &&) = default;
LockedRegistry &operator=(LockedRegistry &&) = default;
[[nodiscard]] ModuleRegistry &get() const { return *reg; }
ModuleRegistry *operator->() const { return reg; }
ModuleRegistry &operator*() const { return *reg; }
};
void registerBuiltinModule(ModuleRegistry &reg, const wibo::Module *module);
LockedRegistry registry() {
static ModuleRegistry reg;
std::unique_lock<std::recursive_mutex> guard(reg.mutex);
if (!reg.initialized) {
reg.initialized = true;
const wibo::Module *builtins[] = {
&lib_advapi32, &lib_bcrypt, &lib_crt, &lib_kernel32, &lib_lmgr, &lib_mscoree, &lib_msvcrt,
&lib_ntdll, &lib_ole32, &lib_rpcrt4, &lib_user32, &lib_vcruntime, &lib_version, nullptr,
};
for (const wibo::Module **module = builtins; *module; ++module) {
registerBuiltinModule(reg, *module);
}
}
return {reg, std::move(guard)};
} }
std::string normalizeAlias(const std::string &value) { std::string normalizeAlias(const std::string &value) {
std::string out = value; std::string out = value;
std::replace(out.begin(), out.end(), '/', '\\'); std::replace(out.begin(), out.end(), '/', '\\');
std::transform(out.begin(), out.end(), out.begin(), toLowerInPlace(out);
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
return out; return out;
} }
@ -211,7 +211,7 @@ std::vector<std::string> candidateModuleNames(const ParsedModuleName &parsed) {
std::string normalizedBaseKey(const ParsedModuleName &parsed) { std::string normalizedBaseKey(const ParsedModuleName &parsed) {
if (parsed.base.empty()) { if (parsed.base.empty()) {
return std::string(); return {};
} }
std::string base = parsed.base; std::string base = parsed.base;
if (!parsed.hasExtension && !parsed.endsWithDot) { if (!parsed.hasExtension && !parsed.endsWithDot) {
@ -231,7 +231,7 @@ std::optional<std::filesystem::path> combineAndFind(const std::filesystem::path
return files::findCaseInsensitiveFile(directory, filename); return files::findCaseInsensitiveFile(directory, filename);
} }
std::vector<std::filesystem::path> collectSearchDirectories(bool alteredSearchPath) { std::vector<std::filesystem::path> collectSearchDirectories(ModuleRegistry &reg, bool alteredSearchPath) {
std::vector<std::filesystem::path> dirs; std::vector<std::filesystem::path> dirs;
std::unordered_set<std::string> seen; std::unordered_set<std::string> seen;
auto addDirectory = [&](const std::filesystem::path &dir) { auto addDirectory = [&](const std::filesystem::path &dir) {
@ -246,33 +246,21 @@ std::vector<std::filesystem::path> collectSearchDirectories(bool alteredSearchPa
return; return;
if (!std::filesystem::exists(canonical, ec) || ec) if (!std::filesystem::exists(canonical, ec) || ec)
return; return;
std::string key = toLowerCopy(canonical.string()); std::string key = stringToLower(canonical.string());
if (seen.insert(key).second) { if (seen.insert(key).second) {
dirs.push_back(canonical); dirs.push_back(canonical);
} }
}; };
auto &reg = registry();
if (wibo::argv && wibo::argc > 0 && wibo::argv[0]) {
std::filesystem::path mainBinary = std::filesystem::absolute(wibo::argv[0]);
if (mainBinary.has_parent_path()) {
addDirectory(mainBinary.parent_path());
}
}
if (reg.dllDirectory.has_value()) { if (reg.dllDirectory.has_value()) {
addDirectory(*reg.dllDirectory); addDirectory(*reg.dllDirectory);
} }
addDirectory(files::pathFromWindows("Z:/Windows/System32"));
addDirectory(files::pathFromWindows("Z:/Windows"));
if (!alteredSearchPath) { if (!alteredSearchPath) {
addDirectory(std::filesystem::current_path()); addDirectory(std::filesystem::current_path());
} }
if (const char *envPath = std::getenv("PATH")) { if (const char *envPath = std::getenv("WIBO_PATH")) {
std::string pathList = envPath; std::string pathList = envPath;
size_t start = 0; size_t start = 0;
while (start <= pathList.size()) { while (start <= pathList.size()) {
@ -302,7 +290,9 @@ std::vector<std::filesystem::path> collectSearchDirectories(bool alteredSearchPa
return dirs; return dirs;
} }
std::optional<std::filesystem::path> resolveModuleOnDisk(const std::string &requestedName, bool alteredSearchPath) {
std::optional<std::filesystem::path> resolveModuleOnDisk(ModuleRegistry &reg, const std::string &requestedName,
bool alteredSearchPath) {
ParsedModuleName parsed = parseModuleName(requestedName); ParsedModuleName parsed = parseModuleName(requestedName);
auto names = candidateModuleNames(parsed); auto names = candidateModuleNames(parsed);
@ -321,7 +311,7 @@ std::optional<std::filesystem::path> resolveModuleOnDisk(const std::string &requ
return std::nullopt; return std::nullopt;
} }
auto dirs = collectSearchDirectories(alteredSearchPath); auto dirs = collectSearchDirectories(reg, alteredSearchPath);
for (const auto &dir : dirs) { for (const auto &dir : dirs) {
for (const auto &candidate : names) { for (const auto &candidate : names) {
auto resolved = combineAndFind(dir, candidate); auto resolved = combineAndFind(dir, candidate);
@ -340,8 +330,7 @@ std::string storageKeyForPath(const std::filesystem::path &path) {
std::string storageKeyForBuiltin(const std::string &normalizedName) { return normalizedName; } std::string storageKeyForBuiltin(const std::string &normalizedName) { return normalizedName; }
wibo::ModuleInfo *findByAlias(const std::string &alias) { wibo::ModuleInfo *findByAlias(ModuleRegistry &reg, const std::string &alias) {
auto &reg = registry();
auto it = reg.modulesByAlias.find(alias); auto it = reg.modulesByAlias.find(alias);
if (it != reg.modulesByAlias.end()) { if (it != reg.modulesByAlias.end()) {
return it->second; return it->second;
@ -349,11 +338,10 @@ wibo::ModuleInfo *findByAlias(const std::string &alias) {
return nullptr; return nullptr;
} }
void registerAlias(const std::string &alias, wibo::ModuleInfo *info) { void registerAlias(ModuleRegistry &reg, const std::string &alias, wibo::ModuleInfo *info) {
if (alias.empty() || !info) { if (alias.empty() || !info) {
return; return;
} }
auto &reg = registry();
auto it = reg.modulesByAlias.find(alias); auto it = reg.modulesByAlias.find(alias);
if (it == reg.modulesByAlias.end()) { if (it == reg.modulesByAlias.end()) {
reg.modulesByAlias[alias] = info; reg.modulesByAlias[alias] = info;
@ -368,7 +356,7 @@ void registerAlias(const std::string &alias, wibo::ModuleInfo *info) {
} }
} }
void registerBuiltinModule(const wibo::Module *module) { void registerBuiltinModule(ModuleRegistry &reg, const wibo::Module *module) {
if (!module) { if (!module) {
return; return;
} }
@ -380,7 +368,6 @@ void registerBuiltinModule(const wibo::Module *module) {
entry->exportsInitialized = true; entry->exportsInitialized = true;
auto storageKey = storageKeyForBuiltin(entry->normalizedName); auto storageKey = storageKeyForBuiltin(entry->normalizedName);
auto raw = entry.get(); auto raw = entry.get();
auto &reg = registry();
reg.modulesByKey[storageKey] = std::move(entry); reg.modulesByKey[storageKey] = std::move(entry);
reg.builtinAliasLists[module] = {}; reg.builtinAliasLists[module] = {};
@ -395,7 +382,7 @@ void registerBuiltinModule(const wibo::Module *module) {
if (pinModule) { if (pinModule) {
reg.pinnedAliases.insert(alias); reg.pinnedAliases.insert(alias);
} }
registerAlias(alias, raw); registerAlias(reg, alias, raw);
reg.builtinAliasMap[alias] = raw; reg.builtinAliasMap[alias] = raw;
ParsedModuleName parsed = parseModuleName(module->names[i]); ParsedModuleName parsed = parseModuleName(module->names[i]);
std::string baseAlias = normalizedBaseKey(parsed); std::string baseAlias = normalizedBaseKey(parsed);
@ -404,7 +391,7 @@ void registerBuiltinModule(const wibo::Module *module) {
if (pinModule) { if (pinModule) {
reg.pinnedAliases.insert(baseAlias); reg.pinnedAliases.insert(baseAlias);
} }
registerAlias(baseAlias, raw); registerAlias(reg, baseAlias, raw);
reg.builtinAliasMap[baseAlias] = raw; reg.builtinAliasMap[baseAlias] = raw;
} }
} }
@ -447,35 +434,17 @@ void callDllMain(wibo::ModuleInfo &info, DWORD reason) {
} }
} }
void ensureInitialized() { void registerExternalModuleAliases(ModuleRegistry &reg, const std::string &requestedName,
auto &reg = registry(); const std::filesystem::path &resolvedPath, wibo::ModuleInfo *info) {
if (reg.initialized) {
return;
}
reg.initialized = true;
const wibo::Module *builtins[] = {
&lib_advapi32, &lib_bcrypt, &lib_crt, &lib_kernel32, &lib_lmgr, &lib_mscoree, &lib_msvcrt,
&lib_ntdll, &lib_ole32, &lib_rpcrt4, &lib_user32, &lib_vcruntime, &lib_version, nullptr,
};
for (const wibo::Module **module = builtins; *module; ++module) {
registerBuiltinModule(*module);
}
}
void registerExternalModuleAliases(const std::string &requestedName, const std::filesystem::path &resolvedPath,
wibo::ModuleInfo *info) {
ParsedModuleName parsed = parseModuleName(requestedName); ParsedModuleName parsed = parseModuleName(requestedName);
registerAlias(normalizedBaseKey(parsed), info); registerAlias(reg, normalizedBaseKey(parsed), info);
registerAlias(normalizeAlias(requestedName), info); registerAlias(reg, normalizeAlias(requestedName), info);
registerAlias(storageKeyForPath(resolvedPath), info); registerAlias(reg, storageKeyForPath(resolvedPath), info);
} }
wibo::ModuleInfo *moduleFromAddress(void *addr) { wibo::ModuleInfo *moduleFromAddress(ModuleRegistry &reg, void *addr) {
if (!addr) if (!addr)
return nullptr; return nullptr;
auto &reg = registry();
for (auto &pair : reg.modulesByKey) { for (auto &pair : reg.modulesByKey) {
wibo::ModuleInfo *info = pair.second.get(); wibo::ModuleInfo *info = pair.second.get();
if (!info) if (!info)
@ -491,7 +460,7 @@ wibo::ModuleInfo *moduleFromAddress(void *addr) {
} }
if (!base || size == 0) if (!base || size == 0)
continue; continue;
uint8_t *ptr = static_cast<uint8_t *>(addr); auto *ptr = static_cast<uint8_t *>(addr);
if (ptr >= base && ptr < base + size) { if (ptr >= base && ptr < base + size) {
return info; return info;
} }
@ -523,7 +492,8 @@ void ensureExportsInitialized(wibo::ModuleInfo &info) {
} }
if (rva >= exe->exportDirectoryRVA && rva < exe->exportDirectoryRVA + exe->exportDirectorySize) { if (rva >= exe->exportDirectoryRVA && rva < exe->exportDirectoryRVA + exe->exportDirectorySize) {
const char *forward = exe->fromRVA<const char>(rva); const char *forward = exe->fromRVA<const char>(rva);
info.exportsByOrdinal[i] = resolveMissingFuncName(info.originalName.c_str(), forward); info.exportsByOrdinal[i] =
reinterpret_cast<void *>(resolveMissingFuncName(info.originalName.c_str(), forward));
} else { } else {
info.exportsByOrdinal[i] = exe->fromRVA<void>(rva); info.exportsByOrdinal[i] = exe->fromRVA<void>(rva);
} }
@ -536,7 +506,7 @@ void ensureExportsInitialized(wibo::ModuleInfo &info) {
auto *ordinals = exe->fromRVA<uint16_t>(dir->addressOfNameOrdinals); auto *ordinals = exe->fromRVA<uint16_t>(dir->addressOfNameOrdinals);
for (uint32_t i = 0; i < nameCount; ++i) { for (uint32_t i = 0; i < nameCount; ++i) {
uint16_t index = ordinals[i]; uint16_t index = ordinals[i];
uint16_t ordinal = static_cast<uint16_t>(dir->base + index); auto ordinal = static_cast<uint16_t>(dir->base + index);
if (index < info.exportsByOrdinal.size()) { if (index < info.exportsByOrdinal.size()) {
const char *namePtr = exe->fromRVA<const char>(names[i]); const char *namePtr = exe->fromRVA<const char>(names[i]);
info.exportNameToOrdinal[std::string(namePtr)] = ordinal; info.exportNameToOrdinal[std::string(namePtr)] = ordinal;
@ -550,14 +520,11 @@ void ensureExportsInitialized(wibo::ModuleInfo &info) {
namespace wibo { namespace wibo {
void initializeModuleRegistry() { void initializeModuleRegistry() { registry(); }
std::lock_guard<std::recursive_mutex> lock(registry().mutex);
ensureInitialized();
}
void shutdownModuleRegistry() { void shutdownModuleRegistry() {
std::lock_guard<std::recursive_mutex> lock(registry().mutex); auto reg = registry();
for (auto &pair : registry().modulesByKey) { for (auto &pair : reg->modulesByKey) {
ModuleInfo *info = pair.second.get(); ModuleInfo *info = pair.second.get();
if (!info || info->module) { if (!info || info->module) {
continue; continue;
@ -567,40 +534,38 @@ void shutdownModuleRegistry() {
callDllMain(*info, DLL_PROCESS_DETACH); callDllMain(*info, DLL_PROCESS_DETACH);
} }
} }
registry().modulesByKey.clear(); reg->modulesByKey.clear();
registry().modulesByAlias.clear(); reg->modulesByAlias.clear();
registry().dllDirectory.reset(); reg->dllDirectory.reset();
registry().initialized = false; reg->initialized = false;
registry().onExitTables.clear(); reg->onExitTables.clear();
} }
ModuleInfo *moduleInfoFromHandle(HMODULE module) { return static_cast<ModuleInfo *>(module); } ModuleInfo *moduleInfoFromHandle(HMODULE module) { return static_cast<ModuleInfo *>(module); }
void setDllDirectoryOverride(const std::filesystem::path &path) { void setDllDirectoryOverride(const std::filesystem::path &path) {
auto canonical = files::canonicalPath(path); auto canonical = files::canonicalPath(path);
std::lock_guard<std::recursive_mutex> lock(registry().mutex); auto reg = registry();
registry().dllDirectory = canonical; reg->dllDirectory = canonical;
} }
void clearDllDirectoryOverride() { void clearDllDirectoryOverride() {
std::lock_guard<std::recursive_mutex> lock(registry().mutex); auto reg = registry();
registry().dllDirectory.reset(); reg->dllDirectory.reset();
} }
std::optional<std::filesystem::path> dllDirectoryOverride() { std::optional<std::filesystem::path> dllDirectoryOverride() {
std::lock_guard<std::recursive_mutex> lock(registry().mutex); auto reg = registry();
return registry().dllDirectory; return reg->dllDirectory;
} }
void registerOnExitTable(void *table) { void registerOnExitTable(void *table) {
if (!table) if (!table)
return; return;
std::lock_guard<std::recursive_mutex> lock(registry().mutex); auto reg = registry();
ensureInitialized(); if (reg->onExitTables.find(table) == reg->onExitTables.end()) {
auto &reg = registry(); if (auto *info = moduleFromAddress(*reg, table)) {
if (reg.onExitTables.find(table) == reg.onExitTables.end()) { reg->onExitTables[table] = info;
if (auto *info = moduleFromAddress(table)) {
reg.onExitTables[table] = info;
} }
} }
} }
@ -608,16 +573,15 @@ void registerOnExitTable(void *table) {
void addOnExitFunction(void *table, void (*func)()) { void addOnExitFunction(void *table, void (*func)()) {
if (!func) if (!func)
return; return;
std::lock_guard<std::recursive_mutex> lock(registry().mutex); auto reg = registry();
auto &reg = registry();
ModuleInfo *info = nullptr; ModuleInfo *info = nullptr;
auto it = reg.onExitTables.find(table); auto it = reg->onExitTables.find(table);
if (it != reg.onExitTables.end()) { if (it != reg->onExitTables.end()) {
info = it->second; info = it->second;
} else if (table) { } else if (table) {
info = moduleFromAddress(table); info = moduleFromAddress(*reg, table);
if (info) if (info)
reg.onExitTables[table] = info; reg->onExitTables[table] = info;
} }
if (info) { if (info) {
info->onExitFunctions.push_back(reinterpret_cast<void *>(func)); info->onExitFunctions.push_back(reinterpret_cast<void *>(func));
@ -635,16 +599,15 @@ void runPendingOnExit(ModuleInfo &info) {
} }
void executeOnExitTable(void *table) { void executeOnExitTable(void *table) {
std::lock_guard<std::recursive_mutex> lock(registry().mutex); auto reg = registry();
auto &reg = registry();
ModuleInfo *info = nullptr; ModuleInfo *info = nullptr;
if (table) { if (table) {
auto it = reg.onExitTables.find(table); auto it = reg->onExitTables.find(table);
if (it != reg.onExitTables.end()) { if (it != reg->onExitTables.end()) {
info = it->second; info = it->second;
reg.onExitTables.erase(it); reg->onExitTables.erase(it);
} else { } else {
info = moduleFromAddress(table); info = moduleFromAddress(*reg, table);
} }
} }
if (info) { if (info) {
@ -656,13 +619,12 @@ HMODULE findLoadedModule(const char *name) {
if (!name) { if (!name) {
return nullptr; return nullptr;
} }
std::lock_guard<std::recursive_mutex> lock(registry().mutex); auto reg = registry();
ensureInitialized();
ParsedModuleName parsed = parseModuleName(name); ParsedModuleName parsed = parseModuleName(name);
std::string alias = normalizedBaseKey(parsed); std::string alias = normalizedBaseKey(parsed);
ModuleInfo *info = findByAlias(alias); ModuleInfo *info = findByAlias(*reg, alias);
if (!info) { if (!info) {
info = findByAlias(normalizeAlias(name)); info = findByAlias(*reg, normalizeAlias(name));
} }
return info; return info;
} }
@ -675,23 +637,21 @@ HMODULE loadModule(const char *dllName) {
std::string requested(dllName); std::string requested(dllName);
DEBUG_LOG("loadModule(%s)\n", requested.c_str()); DEBUG_LOG("loadModule(%s)\n", requested.c_str());
std::lock_guard<std::recursive_mutex> lock(registry().mutex); auto reg = registry();
ensureInitialized();
ParsedModuleName parsed = parseModuleName(requested); ParsedModuleName parsed = parseModuleName(requested);
auto &reg = registry();
DWORD diskError = ERROR_SUCCESS; DWORD diskError = ERROR_SUCCESS;
auto tryLoadExternal = [&](const std::filesystem::path &path) -> ModuleInfo * { auto tryLoadExternal = [&](const std::filesystem::path &path) -> ModuleInfo * {
std::string key = storageKeyForPath(path); std::string key = storageKeyForPath(path);
auto existingIt = reg.modulesByKey.find(key); auto existingIt = reg->modulesByKey.find(key);
if (existingIt != reg.modulesByKey.end()) { if (existingIt != reg->modulesByKey.end()) {
ModuleInfo *info = existingIt->second.get(); ModuleInfo *info = existingIt->second.get();
if (info->refCount != UINT_MAX) { if (info->refCount != UINT_MAX) {
info->refCount++; info->refCount++;
} }
registerExternalModuleAliases(requested, files::canonicalPath(path), info); registerExternalModuleAliases(*reg, requested, files::canonicalPath(path), info);
return info; return info;
} }
@ -725,15 +685,15 @@ HMODULE loadModule(const char *dllName) {
info->dontResolveReferences = false; info->dontResolveReferences = false;
ModuleInfo *raw = info.get(); ModuleInfo *raw = info.get();
reg.modulesByKey[key] = std::move(info); reg->modulesByKey[key] = std::move(info);
registerExternalModuleAliases(requested, raw->resolvedPath, raw); registerExternalModuleAliases(*reg, requested, raw->resolvedPath, raw);
ensureExportsInitialized(*raw); ensureExportsInitialized(*raw);
callDllMain(*raw, DLL_PROCESS_ATTACH); callDllMain(*raw, DLL_PROCESS_ATTACH);
return raw; return raw;
}; };
auto resolveAndLoadExternal = [&]() -> ModuleInfo * { auto resolveAndLoadExternal = [&]() -> ModuleInfo * {
auto resolvedPath = resolveModuleOnDisk(requested, false); auto resolvedPath = resolveModuleOnDisk(*reg, requested, false);
if (!resolvedPath) { if (!resolvedPath) {
DEBUG_LOG(" module not found on disk\n"); DEBUG_LOG(" module not found on disk\n");
return nullptr; return nullptr;
@ -742,9 +702,9 @@ HMODULE loadModule(const char *dllName) {
}; };
std::string alias = normalizedBaseKey(parsed); std::string alias = normalizedBaseKey(parsed);
ModuleInfo *existing = findByAlias(alias); ModuleInfo *existing = findByAlias(*reg, alias);
if (!existing) { if (!existing) {
existing = findByAlias(normalizeAlias(requested)); existing = findByAlias(*reg, normalizeAlias(requested));
} }
if (existing) { if (existing) {
DEBUG_LOG(" found existing module alias %s (builtin=%d)\n", alias.c_str(), existing->module != nullptr); DEBUG_LOG(" found existing module alias %s (builtin=%d)\n", alias.c_str(), existing->module != nullptr);
@ -756,7 +716,7 @@ HMODULE loadModule(const char *dllName) {
lastError = ERROR_SUCCESS; lastError = ERROR_SUCCESS;
return existing; return existing;
} }
bool pinned = reg.pinnedModules.count(existing) != 0; bool pinned = reg->pinnedModules.count(existing) != 0;
if (!pinned) { if (!pinned) {
if (ModuleInfo *external = resolveAndLoadExternal()) { if (ModuleInfo *external = resolveAndLoadExternal()) {
DEBUG_LOG(" replaced builtin module %s with external copy\n", requested.c_str()); DEBUG_LOG(" replaced builtin module %s with external copy\n", requested.c_str());
@ -777,13 +737,13 @@ HMODULE loadModule(const char *dllName) {
auto fallbackAlias = normalizedBaseKey(parsed); auto fallbackAlias = normalizedBaseKey(parsed);
ModuleInfo *builtin = nullptr; ModuleInfo *builtin = nullptr;
auto builtinIt = reg.builtinAliasMap.find(fallbackAlias); auto builtinIt = reg->builtinAliasMap.find(fallbackAlias);
if (builtinIt != reg.builtinAliasMap.end()) { if (builtinIt != reg->builtinAliasMap.end()) {
builtin = builtinIt->second; builtin = builtinIt->second;
} }
if (!builtin) { if (!builtin) {
builtinIt = reg.builtinAliasMap.find(normalizeAlias(requested)); builtinIt = reg->builtinAliasMap.find(normalizeAlias(requested));
if (builtinIt != reg.builtinAliasMap.end()) { if (builtinIt != reg->builtinAliasMap.end()) {
builtin = builtinIt->second; builtin = builtinIt->second;
} }
} }
@ -801,7 +761,7 @@ void freeModule(HMODULE module) {
if (!module) { if (!module) {
return; return;
} }
std::lock_guard<std::recursive_mutex> lock(registry().mutex); auto reg = registry();
ModuleInfo *info = moduleInfoFromHandle(module); ModuleInfo *info = moduleInfoFromHandle(module);
if (!info || info->refCount == UINT_MAX) { if (!info || info->refCount == UINT_MAX) {
return; return;
@ -811,10 +771,9 @@ void freeModule(HMODULE module) {
} }
info->refCount--; info->refCount--;
if (info->refCount == 0) { if (info->refCount == 0) {
auto &reg = registry(); for (auto it = reg->onExitTables.begin(); it != reg->onExitTables.end();) {
for (auto it = reg.onExitTables.begin(); it != reg.onExitTables.end();) {
if (it->second == info) { if (it->second == info) {
it = reg.onExitTables.erase(it); it = reg->onExitTables.erase(it);
} else { } else {
++it; ++it;
} }
@ -823,10 +782,10 @@ void freeModule(HMODULE module) {
callDllMain(*info, DLL_PROCESS_DETACH); callDllMain(*info, DLL_PROCESS_DETACH);
std::string key = info->resolvedPath.empty() ? storageKeyForBuiltin(info->normalizedName) std::string key = info->resolvedPath.empty() ? storageKeyForBuiltin(info->normalizedName)
: storageKeyForPath(info->resolvedPath); : storageKeyForPath(info->resolvedPath);
reg.modulesByKey.erase(key); reg->modulesByKey.erase(key);
for (auto it = reg.modulesByAlias.begin(); it != reg.modulesByAlias.end();) { for (auto it = reg->modulesByAlias.begin(); it != reg->modulesByAlias.end();) {
if (it->second == info) { if (it->second == info) {
it = reg.modulesByAlias.erase(it); it = reg->modulesByAlias.erase(it);
} else { } else {
++it; ++it;
} }
@ -852,7 +811,7 @@ void *resolveFuncByName(HMODULE module, const char *funcName) {
return resolveFuncByOrdinal(module, it->second); return resolveFuncByOrdinal(module, it->second);
} }
} }
return resolveMissingFuncName(info->originalName.c_str(), funcName); return reinterpret_cast<void *>(resolveMissingFuncName(info->originalName.c_str(), funcName));
} }
void *resolveFuncByOrdinal(HMODULE module, uint16_t ordinal) { void *resolveFuncByOrdinal(HMODULE module, uint16_t ordinal) {
@ -869,7 +828,7 @@ void *resolveFuncByOrdinal(HMODULE module, uint16_t ordinal) {
if (!info->module) { if (!info->module) {
ensureExportsInitialized(*info); ensureExportsInitialized(*info);
if (!info->exportsByOrdinal.empty() && ordinal >= info->exportOrdinalBase) { if (!info->exportsByOrdinal.empty() && ordinal >= info->exportOrdinalBase) {
size_t index = static_cast<size_t>(ordinal - info->exportOrdinalBase); auto index = static_cast<size_t>(ordinal - info->exportOrdinalBase);
if (index < info->exportsByOrdinal.size()) { if (index < info->exportsByOrdinal.size()) {
void *addr = info->exportsByOrdinal[index]; void *addr = info->exportsByOrdinal[index];
if (addr) { if (addr) {
@ -878,22 +837,20 @@ void *resolveFuncByOrdinal(HMODULE module, uint16_t ordinal) {
} }
} }
} }
return resolveMissingFuncOrdinal(info->originalName.c_str(), ordinal); return reinterpret_cast<void *>(resolveMissingFuncOrdinal(info->originalName.c_str(), ordinal));
} }
void *resolveMissingImportByName(const char *dllName, const char *funcName) { void *resolveMissingImportByName(const char *dllName, const char *funcName) {
const char *safeDll = dllName ? dllName : ""; const char *safeDll = dllName ? dllName : "";
const char *safeFunc = funcName ? funcName : ""; const char *safeFunc = funcName ? funcName : "";
std::lock_guard<std::recursive_mutex> lock(registry().mutex); [[maybe_unused]] auto reg = registry();
ensureInitialized(); return reinterpret_cast<void *>(resolveMissingFuncName(safeDll, safeFunc));
return resolveMissingFuncName(safeDll, safeFunc);
} }
void *resolveMissingImportByOrdinal(const char *dllName, uint16_t ordinal) { void *resolveMissingImportByOrdinal(const char *dllName, uint16_t ordinal) {
const char *safeDll = dllName ? dllName : ""; const char *safeDll = dllName ? dllName : "";
std::lock_guard<std::recursive_mutex> lock(registry().mutex); [[maybe_unused]] auto reg = registry();
ensureInitialized(); return reinterpret_cast<void *>(resolveMissingFuncOrdinal(safeDll, ordinal));
return resolveMissingFuncOrdinal(safeDll, ordinal);
} }
Executable *executableFromModule(HMODULE module) { Executable *executableFromModule(HMODULE module) {

View File

@ -1,10 +1,47 @@
#include "strutil.h" #include "strutil.h"
#include "common.h" #include "common.h"
#include <algorithm>
#include <cctype>
#include <cwctype>
#include <cstdint> #include <cstdint>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
void toLowerInPlace(std::string &str) {
std::transform(str.begin(), str.end(), str.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
}
void toUpperInPlace(std::string &str) {
std::transform(str.begin(), str.end(), str.begin(),
[](unsigned char c) { return static_cast<char>(std::toupper(c)); });
}
std::string stringToLower(std::string_view str) {
std::string result(str);
toLowerInPlace(result);
return result;
}
std::string stringToUpper(std::string_view str) {
std::string result(str);
toUpperInPlace(result);
return result;
}
uint16_t wcharToLower(uint16_t ch) {
if (ch >= 'A' && ch <= 'Z') {
return static_cast<uint16_t>(ch + ('a' - 'A'));
}
wchar_t wide = static_cast<wchar_t>(ch);
wchar_t lowered = std::towlower(wide);
if (lowered < 0 || lowered > 0xFFFF) {
return ch;
}
return static_cast<uint16_t>(lowered);
}
size_t wstrlen(const uint16_t *str) { size_t wstrlen(const uint16_t *str) {
if (!str) if (!str)
return 0; return 0;

View File

@ -2,6 +2,7 @@
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <string_view>
#include <vector> #include <vector>
size_t wstrlen(const uint16_t *str); size_t wstrlen(const uint16_t *str);
@ -18,3 +19,8 @@ std::string wideStringToString(const uint16_t *src, int len = -1);
std::vector<uint16_t> stringToWideString(const char *src); std::vector<uint16_t> stringToWideString(const char *src);
long wstrtol(const uint16_t *string, uint16_t **end_ptr, int base); long wstrtol(const uint16_t *string, uint16_t **end_ptr, int base);
unsigned long wstrtoul(const uint16_t *string, uint16_t **end_ptr, int base); unsigned long wstrtoul(const uint16_t *string, uint16_t **end_ptr, int base);
void toLowerInPlace(std::string &str);
void toUpperInPlace(std::string &str);
std::string stringToLower(std::string_view str);
std::string stringToUpper(std::string_view str);
uint16_t wcharToLower(uint16_t ch);