2021-04-01 21:03:15 -07:00
|
|
|
From 7357cb7a11d785aca554e255bc5c672c279d948f 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
|
|
|
|
|
|
|
---
|
2020-06-30 15:11:27 -07:00
|
|
|
dlls/kernel32/tests/loader.c | 75 ++++++++++++++++++++++++++++++++++++
|
2020-09-02 15:40:02 -07:00
|
|
|
dlls/ntdll/loader.c | 65 +++++++++++++++++++++++++++++++
|
2020-04-16 15:42:47 -07:00
|
|
|
include/winternl.h | 2 +-
|
2020-09-02 15:40:02 -07:00
|
|
|
3 files changed, 141 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
|
2021-03-01 14:31:44 -08:00
|
|
|
index 1db68a191a3..bec46088518 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"
|
|
|
|
|
2021-03-01 14:31:44 -08:00
|
|
|
@@ -4031,6 +4032,79 @@ static void test_Wow64Transition(void)
|
|
|
|
debugstr_wn(name->SectionFileName.Buffer, name->SectionFileName.Length / sizeof(WCHAR)));
|
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;
|
2020-04-24 14:35:33 -07:00
|
|
|
+ LDR_DATA_TABLE_ENTRY *module;
|
2017-04-08 05:23:17 -07:00
|
|
|
+ BOOL found;
|
|
|
|
+
|
|
|
|
+ entry = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
|
|
|
|
+ entry = entry->Flink;
|
|
|
|
+
|
2020-04-24 14:35:33 -07:00
|
|
|
+ module = CONTAINING_RECORD(entry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);
|
2017-04-08 05:23:17 -07:00
|
|
|
+ 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)
|
|
|
|
+ {
|
2020-04-24 14:35:33 -07:00
|
|
|
+ module = CONTAINING_RECORD(entry, LDR_DATA_TABLE_ENTRY, HashLinks);
|
2017-04-08 05:23:17 -07:00
|
|
|
+ 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)
|
|
|
|
+ {
|
2020-04-24 14:35:33 -07:00
|
|
|
+ module = CONTAINING_RECORD(entry, LDR_DATA_TABLE_ENTRY, HashLinks);
|
2017-04-08 05:23:17 -07:00
|
|
|
+ if (!lstrcmpiW(module->BaseDllName.Buffer, kernel32W))
|
|
|
|
+ {
|
|
|
|
+ found = TRUE;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ ok(found, "Could not find kernel32\n");
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
START_TEST(loader)
|
|
|
|
{
|
|
|
|
int argc;
|
2021-03-01 14:31:44 -08:00
|
|
|
@@ -4103,6 +4177,7 @@ 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();
|
2020-09-28 19:58:11 -07:00
|
|
|
test_dll_file( "ntdll.dll" );
|
|
|
|
test_dll_file( "kernel32.dll" );
|
|
|
|
test_dll_file( "advapi32.dll" );
|
2017-04-08 05:23:17 -07:00
|
|
|
diff --git a/dlls/ntdll/loader.c b/dlls/ntdll/loader.c
|
2021-04-01 21:03:15 -07:00
|
|
|
index daee7675196..659cba51768 100644
|
2017-04-08 05:23:17 -07:00
|
|
|
--- a/dlls/ntdll/loader.c
|
|
|
|
+++ b/dlls/ntdll/loader.c
|
2021-04-01 21:03:15 -07:00
|
|
|
@@ -113,6 +113,9 @@ struct file_id
|
2020-06-22 15:21:09 -07:00
|
|
|
BYTE ObjectId[16];
|
|
|
|
};
|
2017-04-08 05:23:17 -07:00
|
|
|
|
|
|
|
+#define HASH_MAP_SIZE 32
|
|
|
|
+static LIST_ENTRY hash_table[HASH_MAP_SIZE];
|
|
|
|
+
|
2020-06-22 15:21:09 -07:00
|
|
|
/* internal representation of loaded modules */
|
2017-04-08 05:23:17 -07:00
|
|
|
typedef struct _wine_modref
|
|
|
|
{
|
2021-04-01 21:03:15 -07:00
|
|
|
@@ -451,6 +454,52 @@ static void call_ldr_notifications( ULONG reason, LDR_DATA_TABLE_ENTRY *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)
|
|
|
|
+{
|
2020-04-24 14:35:33 -07:00
|
|
|
+ LIST_ENTRY *mark, *entry;
|
|
|
|
+ LDR_DATA_TABLE_ENTRY *mod;
|
2017-04-08 05:23:17 -07:00
|
|
|
+
|
|
|
|
+ mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
|
|
|
|
+ for (entry = mark->Flink; entry != mark; entry = entry->Flink)
|
|
|
|
+ {
|
2020-04-24 14:35:33 -07:00
|
|
|
+ mod = CONTAINING_RECORD(entry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);
|
2017-04-08 05:23:17 -07:00
|
|
|
+ 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
|
|
|
*
|
2021-04-01 21:03:15 -07:00
|
|
|
@@ -1197,7 +1246,12 @@ static WINE_MODREF *alloc_module( HMODULE hModule, const UNICODE_STRING *nt_name
|
2020-04-24 14:35:33 -07:00
|
|
|
&wm->ldr.InLoadOrderLinks);
|
2017-04-08 05:23:17 -07:00
|
|
|
InsertTailList(&NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList,
|
2020-04-24 14:35:33 -07:00
|
|
|
&wm->ldr.InMemoryOrderLinks);
|
2017-04-08 05:23:17 -07:00
|
|
|
+ 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 */
|
2020-04-24 14:35:33 -07:00
|
|
|
+ wm->ldr.InInitializationOrderLinks.Flink = NULL;
|
|
|
|
+ wm->ldr.InInitializationOrderLinks.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))
|
|
|
|
{
|
2021-04-01 21:03:15 -07:00
|
|
|
@@ -1877,6 +1931,7 @@ static NTSTATUS build_module( LPCWSTR load_path, const UNICODE_STRING *nt_name,
|
2017-04-08 05:23:17 -07:00
|
|
|
/* the module has only be inserted in the load & memory order lists */
|
2020-04-24 14:35:33 -07:00
|
|
|
RemoveEntryList(&wm->ldr.InLoadOrderLinks);
|
|
|
|
RemoveEntryList(&wm->ldr.InMemoryOrderLinks);
|
2017-04-08 05:23:17 -07:00
|
|
|
+ RemoveEntryList(&wm->ldr.HashLinks);
|
|
|
|
|
|
|
|
/* FIXME: there are several more dangling references
|
|
|
|
* left. Including dlls loaded by this dll before the
|
2021-04-01 21:03:15 -07:00
|
|
|
@@ -3307,6 +3362,7 @@ static void free_modref( WINE_MODREF *wm )
|
2017-04-08 05:23:17 -07:00
|
|
|
{
|
2020-04-24 14:35:33 -07:00
|
|
|
RemoveEntryList(&wm->ldr.InLoadOrderLinks);
|
|
|
|
RemoveEntryList(&wm->ldr.InMemoryOrderLinks);
|
2017-04-08 05:23:17 -07:00
|
|
|
+ RemoveEntryList(&wm->ldr.HashLinks);
|
2020-04-24 14:35:33 -07:00
|
|
|
if (wm->ldr.InInitializationOrderLinks.Flink)
|
|
|
|
RemoveEntryList(&wm->ldr.InInitializationOrderLinks);
|
2017-04-08 05:23:17 -07:00
|
|
|
|
2021-04-01 21:03:15 -07:00
|
|
|
@@ -4024,6 +4080,7 @@ static NTSTATUS process_init(void)
|
|
|
|
{
|
2020-06-29 16:56:48 -07:00
|
|
|
TEB *teb = NtCurrentTeb();
|
|
|
|
PEB *peb = teb->Peb;
|
2017-04-08 05:23:17 -07:00
|
|
|
+ DWORD i;
|
|
|
|
|
2020-06-29 16:56:48 -07:00
|
|
|
peb->LdrData = &ldr;
|
|
|
|
peb->FastPebLock = &peb_lock;
|
2021-04-01 21:03:15 -07:00
|
|
|
@@ -4047,6 +4104,10 @@ static NTSTATUS process_init(void)
|
2021-03-24 14:48:35 -07:00
|
|
|
InitializeListHead( &ldr.InMemoryOrderModuleList );
|
|
|
|
InitializeListHead( &ldr.InInitializationOrderModuleList );
|
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
|
|
|
+
|
2021-04-01 21:03:15 -07:00
|
|
|
init_user_process_params();
|
|
|
|
load_global_options();
|
|
|
|
version_init();
|
|
|
|
@@ -4064,6 +4125,10 @@ static NTSTATUS process_init(void)
|
|
|
|
map_wow64cpu();
|
|
|
|
}
|
2021-03-24 14:48:35 -07:00
|
|
|
#endif
|
2020-09-02 07:38:53 -07:00
|
|
|
+
|
2019-02-14 16:18:02 -08:00
|
|
|
+ /* the windows version was not set yet when ntdll and kernel32 were loaded */
|
|
|
|
+ recompute_hash_map();
|
2020-09-02 15:40:02 -07:00
|
|
|
+
|
|
|
|
return STATUS_SUCCESS;
|
2019-11-07 15:13:27 -08:00
|
|
|
}
|
2020-05-14 17:00:49 -07:00
|
|
|
|
2017-04-08 05:23:17 -07:00
|
|
|
diff --git a/include/winternl.h b/include/winternl.h
|
2021-04-01 21:03:15 -07:00
|
|
|
index 9e6bde25569..0fa75e676fe 100644
|
2017-04-08 05:23:17 -07:00
|
|
|
--- a/include/winternl.h
|
|
|
|
+++ b/include/winternl.h
|
2021-04-01 21:03:15 -07:00
|
|
|
@@ -3181,8 +3181,8 @@ typedef struct _LDR_DATA_TABLE_ENTRY
|
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;
|
2020-04-23 21:15:23 -07:00
|
|
|
void* Lock;
|
2017-04-08 05:23:17 -07:00
|
|
|
--
|
2021-03-24 14:48:35 -07:00
|
|
|
2.30.2
|
2018-02-28 17:28:01 -08:00
|
|
|
|