2020-04-22 15:09:11 -07:00
|
|
|
From 25bb10aa7f39e1586812802589a42c9cfa7d2513 Mon Sep 17 00:00:00 2001
|
2017-04-08 05:23:17 -07:00
|
|
|
From: =?UTF-8?q?Michael=20M=C3=BCller?= <michael@fds-team.de>
|
|
|
|
Date: Mon, 3 Apr 2017 05:30:27 +0200
|
2018-02-28 17:28:01 -08:00
|
|
|
Subject: [PATCH] ntdll: Implement HashLinks field in LDR module data.
|
2017-04-08 05:23:17 -07:00
|
|
|
|
|
|
|
---
|
2019-06-17 15:39:24 -07:00
|
|
|
dlls/kernel32/tests/loader.c | 76 ++++++++++++++++++++++++++++++++++++
|
|
|
|
dlls/ntdll/loader.c | 65 ++++++++++++++++++++++++++++++
|
2020-04-16 15:42:47 -07:00
|
|
|
include/winternl.h | 2 +-
|
|
|
|
3 files changed, 142 insertions(+), 1 deletion(-)
|
2017-04-08 05:23:17 -07:00
|
|
|
|
|
|
|
diff --git a/dlls/kernel32/tests/loader.c b/dlls/kernel32/tests/loader.c
|
2020-04-16 15:42:47 -07:00
|
|
|
index 35c6b3c0996..17d742daaa7 100644
|
2017-04-08 05:23:17 -07:00
|
|
|
--- a/dlls/kernel32/tests/loader.c
|
|
|
|
+++ b/dlls/kernel32/tests/loader.c
|
2019-01-28 15:30:19 -08:00
|
|
|
@@ -30,6 +30,7 @@
|
2017-04-08 05:23:17 -07:00
|
|
|
#include "winbase.h"
|
|
|
|
#include "winternl.h"
|
2019-01-28 15:30:19 -08:00
|
|
|
#include "winnls.h"
|
2017-04-08 05:23:17 -07:00
|
|
|
+#include "winuser.h"
|
|
|
|
#include "wine/test.h"
|
|
|
|
#include "delayloadhandler.h"
|
|
|
|
|
2020-04-16 15:42:47 -07:00
|
|
|
@@ -3934,6 +3935,79 @@ static void test_LoadPackagedLibrary(void)
|
2020-02-12 15:33:55 -08:00
|
|
|
h, GetLastError());
|
2017-04-08 05:23:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
+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_MODULE *module;
|
|
|
|
+ BOOL found;
|
|
|
|
+
|
|
|
|
+ entry = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
|
|
|
|
+ entry = entry->Flink;
|
|
|
|
+
|
|
|
|
+ module = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
|
|
|
|
+ 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_MODULE, 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_MODULE, HashLinks);
|
|
|
|
+ if (!lstrcmpiW(module->BaseDllName.Buffer, kernel32W))
|
|
|
|
+ {
|
|
|
|
+ found = TRUE;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ ok(found, "Could not find kernel32\n");
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
START_TEST(loader)
|
|
|
|
{
|
|
|
|
int argc;
|
2020-04-16 15:42:47 -07:00
|
|
|
@@ -4006,10 +4080,12 @@ START_TEST(loader)
|
2017-04-08 05:23:17 -07:00
|
|
|
test_InMemoryOrderModuleList();
|
2020-02-12 15:33:55 -08:00
|
|
|
test_LoadPackagedLibrary();
|
2019-10-21 14:28:23 -07:00
|
|
|
test_wow64_redirection();
|
2017-04-08 05:23:17 -07:00
|
|
|
+ test_HashLinks();
|
2019-02-18 15:16:26 -08:00
|
|
|
test_dll_file( "ntdll.dll" );
|
|
|
|
test_dll_file( "kernel32.dll" );
|
|
|
|
test_dll_file( "advapi32.dll" );
|
|
|
|
test_dll_file( "user32.dll" );
|
2019-01-28 15:30:19 -08:00
|
|
|
+
|
|
|
|
/* loader test must be last, it can corrupt the internal loader state on Windows */
|
|
|
|
test_Loader();
|
2017-04-08 05:23:17 -07:00
|
|
|
}
|
|
|
|
diff --git a/dlls/ntdll/loader.c b/dlls/ntdll/loader.c
|
2020-04-22 15:09:11 -07:00
|
|
|
index 04ea7ea121a..2a8b4b3ad97 100644
|
2017-04-08 05:23:17 -07:00
|
|
|
--- a/dlls/ntdll/loader.c
|
|
|
|
+++ b/dlls/ntdll/loader.c
|
2020-04-22 15:09:11 -07:00
|
|
|
@@ -122,6 +122,9 @@ static const char * const reason_names[] =
|
2017-04-08 05:23:17 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2020-04-22 15:09:11 -07:00
|
|
|
@@ -462,6 +465,52 @@ static void call_ldr_notifications( ULONG reason, LDR_MODULE *module )
|
2019-06-17 15:39:24 -07:00
|
|
|
}
|
2018-06-27 15:09:07 -07:00
|
|
|
}
|
2017-04-08 05:23:17 -07:00
|
|
|
|
2019-06-17 15:39:24 -07:00
|
|
|
+/*************************************************************************
|
2017-04-08 05:23:17 -07:00
|
|
|
+ * 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++)
|
2020-04-01 16:08:55 -07:00
|
|
|
+ hash = hash * 65599 + towupper(*basename);
|
2017-04-08 05:23:17 -07:00
|
|
|
+ }
|
|
|
|
+ else if (version == 0x0601)
|
|
|
|
+ {
|
|
|
|
+ for (; *basename; basename++)
|
2020-04-01 16:08:55 -07:00
|
|
|
+ hash = hash + 65599 * towupper(*basename);
|
2017-04-08 05:23:17 -07:00
|
|
|
+ }
|
|
|
|
+ else
|
2020-04-01 16:08:55 -07:00
|
|
|
+ hash = towupper(basename[0]) - 'A';
|
2017-04-08 05:23:17 -07:00
|
|
|
+
|
|
|
|
+ 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)
|
|
|
|
+{
|
|
|
|
+ PLIST_ENTRY mark, entry;
|
|
|
|
+ PLDR_MODULE mod;
|
|
|
|
+
|
|
|
|
+ mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
|
|
|
|
+ for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
|
|
|
+ {
|
|
|
|
+ mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
|
|
|
|
+ RemoveEntryList( &mod->HashLinks );
|
|
|
|
+ InsertTailList( &hash_table[hash_basename(mod->BaseDllName.Buffer)], &mod->HashLinks );
|
|
|
|
+ }
|
|
|
|
+}
|
2018-02-28 17:28:01 -08:00
|
|
|
+
|
2019-06-17 15:39:24 -07:00
|
|
|
/*************************************************************************
|
2017-04-08 05:23:17 -07:00
|
|
|
* get_modref
|
2018-02-17 19:09:37 -08:00
|
|
|
*
|
2020-04-22 15:09:11 -07:00
|
|
|
@@ -1227,7 +1276,12 @@ static WINE_MODREF *alloc_module( HMODULE hModule, const UNICODE_STRING *nt_name
|
2017-04-08 05:23:17 -07:00
|
|
|
&wm->ldr.InLoadOrderModuleList);
|
|
|
|
InsertTailList(&NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList,
|
|
|
|
&wm->ldr.InMemoryOrderModuleList);
|
|
|
|
+ InsertTailList(&hash_table[hash_basename(wm->ldr.BaseDllName.Buffer)],
|
|
|
|
+ &wm->ldr.HashLinks);
|
2018-02-28 17:28:01 -08:00
|
|
|
+
|
|
|
|
/* wait until init is called for inserting into InInitializationOrderModuleList */
|
|
|
|
+ wm->ldr.InInitializationOrderModuleList.Flink = NULL;
|
|
|
|
+ wm->ldr.InInitializationOrderModuleList.Blink = NULL;
|
2017-04-08 05:23:17 -07:00
|
|
|
|
2018-02-28 17:28:01 -08:00
|
|
|
if (!(nt->OptionalHeader.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_NX_COMPAT))
|
|
|
|
{
|
2020-04-22 15:09:11 -07:00
|
|
|
@@ -2030,6 +2084,7 @@ static NTSTATUS build_so_dll_module( const WCHAR *load_path, const UNICODE_STRIN
|
2020-04-16 15:42:47 -07:00
|
|
|
/* the module has only been inserted in the load & memory order lists */
|
2017-04-08 05:23:17 -07:00
|
|
|
RemoveEntryList(&wm->ldr.InLoadOrderModuleList);
|
|
|
|
RemoveEntryList(&wm->ldr.InMemoryOrderModuleList);
|
|
|
|
+ RemoveEntryList(&wm->ldr.HashLinks);
|
|
|
|
/* FIXME: free the modref */
|
2020-04-16 15:42:47 -07:00
|
|
|
return status;
|
|
|
|
}
|
2020-04-22 15:09:11 -07:00
|
|
|
@@ -2573,6 +2628,7 @@ static NTSTATUS load_native_dll( LPCWSTR load_path, const UNICODE_STRING *nt_nam
|
2017-04-08 05:23:17 -07:00
|
|
|
/* the module has only be inserted in the load & memory order lists */
|
|
|
|
RemoveEntryList(&wm->ldr.InLoadOrderModuleList);
|
|
|
|
RemoveEntryList(&wm->ldr.InMemoryOrderModuleList);
|
|
|
|
+ RemoveEntryList(&wm->ldr.HashLinks);
|
|
|
|
|
|
|
|
/* FIXME: there are several more dangling references
|
|
|
|
* left. Including dlls loaded by this dll before the
|
2020-04-22 15:09:11 -07:00
|
|
|
@@ -3791,6 +3847,7 @@ static void free_modref( WINE_MODREF *wm )
|
2017-04-08 05:23:17 -07:00
|
|
|
{
|
|
|
|
RemoveEntryList(&wm->ldr.InLoadOrderModuleList);
|
|
|
|
RemoveEntryList(&wm->ldr.InMemoryOrderModuleList);
|
|
|
|
+ RemoveEntryList(&wm->ldr.HashLinks);
|
|
|
|
if (wm->ldr.InInitializationOrderModuleList.Flink)
|
|
|
|
RemoveEntryList(&wm->ldr.InInitializationOrderModuleList);
|
|
|
|
|
2020-04-22 15:09:11 -07:00
|
|
|
@@ -4488,6 +4545,7 @@ void __wine_process_init(void)
|
2019-11-07 15:13:27 -08:00
|
|
|
SIZE_T info_size;
|
|
|
|
TEB *teb = thread_init();
|
|
|
|
PEB *peb = teb->Peb;
|
2017-04-08 05:23:17 -07:00
|
|
|
+ DWORD i;
|
|
|
|
|
2019-11-07 15:13:27 -08:00
|
|
|
/* setup the server connection */
|
|
|
|
server_init_process();
|
2020-04-22 15:09:11 -07:00
|
|
|
@@ -4510,6 +4568,10 @@ void __wine_process_init(void)
|
2017-04-08 05:23:17 -07:00
|
|
|
load_global_options();
|
2019-11-07 15:13:27 -08:00
|
|
|
version_init();
|
2018-02-28 17:28:01 -08:00
|
|
|
|
2017-04-08 05:23:17 -07:00
|
|
|
+ /* initialize hash table */
|
|
|
|
+ for (i = 0; i < HASH_MAP_SIZE; i++)
|
|
|
|
+ InitializeListHead(&hash_table[i]);
|
2018-02-28 17:28:01 -08:00
|
|
|
+
|
2017-04-08 05:23:17 -07:00
|
|
|
/* setup the load callback and create ntdll modref */
|
2020-04-14 17:14:15 -07:00
|
|
|
RtlInitUnicodeString( &nt_name, ntdllW );
|
2020-04-22 15:09:11 -07:00
|
|
|
map_so_dll( &__wine_spec_nt_header, ntdll_module );
|
|
|
|
@@ -4586,5 +4648,8 @@ void __wine_process_init(void)
|
2019-11-07 15:13:27 -08:00
|
|
|
teb->Tib.StackLimit = stack.StackLimit;
|
|
|
|
teb->DeallocationStack = stack.DeallocationStack;
|
2019-02-14 16:18:02 -08:00
|
|
|
|
|
|
|
+ /* the windows version was not set yet when ntdll and kernel32 were loaded */
|
|
|
|
+ recompute_hash_map();
|
|
|
|
+
|
2019-11-07 15:13:27 -08:00
|
|
|
server_init_process_done();
|
|
|
|
}
|
2017-04-08 05:23:17 -07:00
|
|
|
diff --git a/include/winternl.h b/include/winternl.h
|
2020-04-16 15:42:47 -07:00
|
|
|
index b77ab8fe03a..3d4863d8b04 100644
|
2017-04-08 05:23:17 -07:00
|
|
|
--- a/include/winternl.h
|
|
|
|
+++ b/include/winternl.h
|
2020-04-16 15:42:47 -07:00
|
|
|
@@ -2268,8 +2268,8 @@ typedef struct _LDR_MODULE
|
2017-04-08 05:23:17 -07:00
|
|
|
ULONG Flags;
|
|
|
|
SHORT LoadCount;
|
|
|
|
SHORT TlsIndex;
|
|
|
|
- HANDLE SectionHandle;
|
2019-04-23 16:36:21 -07:00
|
|
|
ULONG CheckSum;
|
2017-04-08 05:23:17 -07:00
|
|
|
+ LIST_ENTRY HashLinks;
|
|
|
|
ULONG TimeDateStamp;
|
|
|
|
HANDLE ActivationContext;
|
|
|
|
PVOID PatchInformation;
|
|
|
|
--
|
2020-04-16 15:42:47 -07:00
|
|
|
2.26.0
|
2018-02-28 17:28:01 -08:00
|
|
|
|