wine-staging/patches/ntdll-HashLinks/0001-ntdll-Implement-HashLinks-field-in-LDR-module-data.patch

210 lines
6.9 KiB
Diff

From c933925dbe192c65d00bfa9bccc26bb7b21984fa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michael=20M=C3=BCller?= <michael@fds-team.de>
Date: Mon, 3 Apr 2017 05:30:27 +0200
Subject: [PATCH] ntdll: Implement HashLinks field in LDR module data.
---
dlls/kernel32/tests/loader.c | 75 ++++++++++++++++++++++++++++++++++++
dlls/ntdll/loader.c | 42 ++++++++++++++++++++
2 files changed, 117 insertions(+)
diff --git a/dlls/kernel32/tests/loader.c b/dlls/kernel32/tests/loader.c
index 365f4465fc7..aca2b0f24cb 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"
@@ -4056,6 +4057,79 @@ static void test_Wow64Transition(void)
debugstr_wn(name->SectionFileName.Buffer, name->SectionFileName.Length / sizeof(WCHAR)));
}
+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;
@@ -4128,6 +4202,7 @@ 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" );
diff --git a/dlls/ntdll/loader.c b/dlls/ntdll/loader.c
index 7661227e951..1087cf72d99 100644
--- a/dlls/ntdll/loader.c
+++ b/dlls/ntdll/loader.c
@@ -128,6 +128,9 @@ struct file_id
BYTE ObjectId[16];
};
+#define HASH_MAP_SIZE 32
+static LIST_ENTRY hash_table[HASH_MAP_SIZE];
+
/* internal representation of loaded modules */
typedef struct _wine_modref
{
@@ -497,6 +500,33 @@ 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);
+}
+
/*************************************************************************
* get_modref
*
@@ -1476,7 +1506,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))
{
@@ -2154,6 +2189,7 @@ static NTSTATUS build_module( LPCWSTR load_path, const UNICODE_STRING *nt_name,
/* 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
@@ -3749,6 +3785,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);
@@ -4117,6 +4154,7 @@ void WINAPI LdrInitializeThunk( CONTEXT *context, ULONG_PTR unknown2, ULONG_PTR
ANSI_STRING ctrl_routine = RTL_CONSTANT_STRING( "CtrlRoutine" );
WINE_MODREF *kernel32;
PEB *peb = NtCurrentTeb()->Peb;
+ unsigned int i;
NtQueryVirtualMemory( GetCurrentProcess(), LdrInitializeThunk, MemoryBasicInformation,
&meminfo, sizeof(meminfo), NULL );
@@ -4135,6 +4173,10 @@ void WINAPI LdrInitializeThunk( CONTEXT *context, ULONG_PTR unknown2, ULONG_PTR
sizeof(peb->TlsExpansionBitmapBits) * 8 );
RtlSetBits( peb->TlsBitmap, 0, 1 ); /* TLS index 0 is reserved and should be initialized to NULL. */
+ /* initialize hash table */
+ for (i = 0; i < HASH_MAP_SIZE; i++)
+ InitializeListHead( &hash_table[i] );
+
init_user_process_params();
load_global_options();
version_init();
--
2.39.0