From 875ab1e7ff6b708f15b07537a91406fe83dec681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20M=C3=BCller?= Date: Mon, 3 Apr 2017 05:30:27 +0200 Subject: [PATCH] ntdll: Implement HashLinks field in LDR module data. --- dlls/kernel32/tests/loader.c | 76 ++++++++++++++++++++++++++++++++++++ dlls/ntdll/loader.c | 65 ++++++++++++++++++++++++++++++ include/winternl.h | 2 +- 3 files changed, 142 insertions(+), 1 deletion(-) diff --git a/dlls/kernel32/tests/loader.c b/dlls/kernel32/tests/loader.c index 5474d543248..2d382d4f326 100644 --- a/dlls/kernel32/tests/loader.c +++ b/dlls/kernel32/tests/loader.c @@ -30,6 +30,7 @@ #include "winbase.h" #include "winternl.h" #include "winnls.h" +#include "winuser.h" #include "wine/test.h" #include "delayloadhandler.h" @@ -3934,6 +3935,79 @@ static void test_LoadPackagedLibrary(void) h, GetLastError()); } +static inline WCHAR toupperW(WCHAR c) +{ + WCHAR tmp = c; + CharUpperBuffW(&tmp, 1); + return tmp; +} + +static ULONG hash_basename(const WCHAR *basename) +{ + WORD version = MAKEWORD(NtCurrentTeb()->Peb->OSMinorVersion, + NtCurrentTeb()->Peb->OSMajorVersion); + ULONG hash = 0; + + if (version >= 0x0602) + { + for (; *basename; basename++) + hash = hash * 65599 + toupperW(*basename); + } + else if (version == 0x0601) + { + for (; *basename; basename++) + hash = hash + 65599 * toupperW(*basename); + } + else + hash = toupperW(basename[0]) - 'A'; + + return hash & 31; +} + +static void test_HashLinks(void) +{ + static WCHAR ntdllW[] = {'n','t','d','l','l','.','d','l','l',0}; + static WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0}; + + LIST_ENTRY *hash_map, *entry, *mark; + LDR_DATA_TABLE_ENTRY *module; + BOOL found; + + entry = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList; + entry = entry->Flink; + + module = CONTAINING_RECORD(entry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks); + entry = module->HashLinks.Blink; + + hash_map = entry - hash_basename(module->BaseDllName.Buffer); + + mark = &hash_map[hash_basename(ntdllW)]; + found = FALSE; + for (entry = mark->Flink; entry != mark; entry = entry->Flink) + { + module = CONTAINING_RECORD(entry, LDR_DATA_TABLE_ENTRY, HashLinks); + if (!lstrcmpiW(module->BaseDllName.Buffer, ntdllW)) + { + found = TRUE; + break; + } + } + ok(found, "Could not find ntdll\n"); + + mark = &hash_map[hash_basename(kernel32W)]; + found = FALSE; + for (entry = mark->Flink; entry != mark; entry = entry->Flink) + { + module = CONTAINING_RECORD(entry, LDR_DATA_TABLE_ENTRY, HashLinks); + if (!lstrcmpiW(module->BaseDllName.Buffer, kernel32W)) + { + found = TRUE; + break; + } + } + ok(found, "Could not find kernel32\n"); +} + START_TEST(loader) { int argc; @@ -4006,10 +4080,12 @@ START_TEST(loader) test_InMemoryOrderModuleList(); test_LoadPackagedLibrary(); test_wow64_redirection(); + test_HashLinks(); test_dll_file( "ntdll.dll" ); test_dll_file( "kernel32.dll" ); test_dll_file( "advapi32.dll" ); test_dll_file( "user32.dll" ); + /* loader test must be last, it can corrupt the internal loader state on Windows */ test_Loader(); } diff --git a/dlls/ntdll/loader.c b/dlls/ntdll/loader.c index 35dc7e1eaa4..d1e5fdeb5b8 100644 --- a/dlls/ntdll/loader.c +++ b/dlls/ntdll/loader.c @@ -120,6 +120,9 @@ static const char * const reason_names[] = static const WCHAR dllW[] = {'.','d','l','l',0}; +#define HASH_MAP_SIZE 32 +static LIST_ENTRY hash_table[HASH_MAP_SIZE]; + /* internal representation of 32bit modules. per process. */ typedef struct _wine_modref { @@ -460,6 +463,52 @@ static void call_ldr_notifications( ULONG reason, LDR_DATA_TABLE_ENTRY *module ) } } +/************************************************************************* + * hash_basename + * + * Calculates the bucket index of a dll using the basename. + */ +static ULONG hash_basename(const WCHAR *basename) +{ + WORD version = MAKEWORD(NtCurrentTeb()->Peb->OSMinorVersion, + NtCurrentTeb()->Peb->OSMajorVersion); + ULONG hash = 0; + + if (version >= 0x0602) + { + for (; *basename; basename++) + hash = hash * 65599 + towupper(*basename); + } + else if (version == 0x0601) + { + for (; *basename; basename++) + hash = hash + 65599 * towupper(*basename); + } + else + hash = towupper(basename[0]) - 'A'; + + return hash & (HASH_MAP_SIZE-1); +} + +/************************************************************************* + * recompute_hash_maps + * + * Recomputes the LDR hash map (necessary when windows version changes). + */ +static void recompute_hash_map(void) +{ + LIST_ENTRY *mark, *entry; + LDR_DATA_TABLE_ENTRY *mod; + + mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList; + for (entry = mark->Flink; entry != mark; entry = entry->Flink) + { + mod = CONTAINING_RECORD(entry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks); + RemoveEntryList( &mod->HashLinks ); + InsertTailList( &hash_table[hash_basename(mod->BaseDllName.Buffer)], &mod->HashLinks ); + } +} + /************************************************************************* * get_modref * @@ -1225,7 +1274,12 @@ static WINE_MODREF *alloc_module( HMODULE hModule, const UNICODE_STRING *nt_name &wm->ldr.InLoadOrderLinks); InsertTailList(&NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList, &wm->ldr.InMemoryOrderLinks); + InsertTailList(&hash_table[hash_basename(wm->ldr.BaseDllName.Buffer)], + &wm->ldr.HashLinks); + /* wait until init is called for inserting into InInitializationOrderModuleList */ + wm->ldr.InInitializationOrderLinks.Flink = NULL; + wm->ldr.InInitializationOrderLinks.Blink = NULL; if (!(nt->OptionalHeader.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_NX_COMPAT)) { @@ -1850,6 +1904,7 @@ static NTSTATUS build_so_dll_module( const WCHAR *load_path, const UNICODE_STRIN /* the module has only been inserted in the load & memory order lists */ RemoveEntryList(&wm->ldr.InLoadOrderLinks); RemoveEntryList(&wm->ldr.InMemoryOrderLinks); + RemoveEntryList(&wm->ldr.HashLinks); /* FIXME: free the modref */ return status; } @@ -2393,6 +2448,7 @@ static NTSTATUS load_native_dll( LPCWSTR load_path, const UNICODE_STRING *nt_nam /* the module has only be inserted in the load & memory order lists */ RemoveEntryList(&wm->ldr.InLoadOrderLinks); RemoveEntryList(&wm->ldr.InMemoryOrderLinks); + RemoveEntryList(&wm->ldr.HashLinks); /* FIXME: there are several more dangling references * left. Including dlls loaded by this dll before the @@ -3609,6 +3665,7 @@ static void free_modref( WINE_MODREF *wm ) { RemoveEntryList(&wm->ldr.InLoadOrderLinks); RemoveEntryList(&wm->ldr.InMemoryOrderLinks); + RemoveEntryList(&wm->ldr.HashLinks); if (wm->ldr.InInitializationOrderLinks.Flink) RemoveEntryList(&wm->ldr.InInitializationOrderLinks); @@ -4333,6 +4390,7 @@ void __wine_process_init(void) SIZE_T info_size; TEB *teb; PEB *peb; + DWORD i; if (!unix_funcs) load_ntdll_so( ntdll_module, &__wine_spec_nt_header ); @@ -4360,6 +4418,10 @@ void __wine_process_init(void) load_global_options(); version_init(); + /* initialize hash table */ + for (i = 0; i < HASH_MAP_SIZE; i++) + InitializeListHead(&hash_table[i]); + /* setup the load callback and create ntdll modref */ RtlInitUnicodeString( &nt_name, ntdllW ); status = build_so_dll_module( params->DllPath.Buffer, &nt_name, ntdll_module, 0, &wm ); @@ -4435,6 +4497,9 @@ void __wine_process_init(void) teb->Tib.StackLimit = stack.StackLimit; teb->DeallocationStack = stack.DeallocationStack; + /* the windows version was not set yet when ntdll and kernel32 were loaded */ + recompute_hash_map(); + server_init_process_done(); } diff --git a/include/winternl.h b/include/winternl.h index 9df1f0bb800..8625b192cf1 100644 --- a/include/winternl.h +++ b/include/winternl.h @@ -2330,8 +2330,8 @@ typedef struct _LDR_DATA_TABLE_ENTRY ULONG Flags; SHORT LoadCount; SHORT TlsIndex; - HANDLE SectionHandle; ULONG CheckSum; + LIST_ENTRY HashLinks; ULONG TimeDateStamp; HANDLE ActivationContext; void* Lock; -- 2.26.2