From 8b1e0eec1dd60f9c07bc9f49c02e4b4eef33dd06 Mon Sep 17 00:00:00 2001 From: Zebediah Figura Date: Mon, 28 Dec 2020 12:36:15 -0600 Subject: [PATCH] ntdll-NtAlertThreadByThreadId: Free the address wait entry on thread exit. --- ...t-Win32-futexes-on-top-of-thread-ID-.patch | 49 ++++++++++++++++--- patches/patchinstall.sh | 8 +-- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/patches/ntdll-NtAlertThreadByThreadId/0009-ntdll-Reimplement-Win32-futexes-on-top-of-thread-ID-.patch b/patches/ntdll-NtAlertThreadByThreadId/0009-ntdll-Reimplement-Win32-futexes-on-top-of-thread-ID-.patch index a765304a..98adf2ef 100644 --- a/patches/ntdll-NtAlertThreadByThreadId/0009-ntdll-Reimplement-Win32-futexes-on-top-of-thread-ID-.patch +++ b/patches/ntdll-NtAlertThreadByThreadId/0009-ntdll-Reimplement-Win32-futexes-on-top-of-thread-ID-.patch @@ -1,18 +1,33 @@ -From c5833a431cf9ba59f1f04081a2be058833b4cbb8 Mon Sep 17 00:00:00 2001 +From 101957dbdaa322a7078e8ff49197e580a8b3a82c Mon Sep 17 00:00:00 2001 From: Zebediah Figura Date: Mon, 2 Nov 2020 20:24:07 -0600 Subject: [PATCH] ntdll: Reimplement Win32 futexes on top of thread-ID alerts. Signed-off-by: Zebediah Figura --- - dlls/ntdll/sync.c | 147 ++++++++++++++++++++++++++++++++++- + dlls/ntdll/ntdll_misc.h | 2 + + dlls/ntdll/sync.c | 154 ++++++++++++++++++++++++++++++++++++- + dlls/ntdll/thread.c | 2 + dlls/ntdll/unix/loader.c | 3 - dlls/ntdll/unix/sync.c | 162 --------------------------------------- dlls/ntdll/unixlib.h | 6 +- - 4 files changed, 145 insertions(+), 173 deletions(-) + 6 files changed, 156 insertions(+), 173 deletions(-) +diff --git a/dlls/ntdll/ntdll_misc.h b/dlls/ntdll/ntdll_misc.h +index 41e8666a25c..8ba5c2e22fd 100644 +--- a/dlls/ntdll/ntdll_misc.h ++++ b/dlls/ntdll/ntdll_misc.h +@@ -88,6 +88,8 @@ extern void init_directories(void) DECLSPEC_HIDDEN; + + extern struct _KUSER_SHARED_DATA *user_shared_data DECLSPEC_HIDDEN; + ++extern void addr_wait_free_entry(void) DECLSPEC_HIDDEN; ++ + /* locale */ + extern LCID user_lcid, system_lcid; + extern DWORD ntdll_umbstowcs( const char* src, DWORD srclen, WCHAR* dst, DWORD dstlen ) DECLSPEC_HIDDEN; diff --git a/dlls/ntdll/sync.c b/dlls/ntdll/sync.c -index 8df7015df9f..be462f27881 100644 +index 8df7015df9f..786289521e7 100644 --- a/dlls/ntdll/sync.c +++ b/dlls/ntdll/sync.c @@ -37,6 +37,13 @@ @@ -29,7 +44,7 @@ index 8df7015df9f..be462f27881 100644 /****************************************************************** * RtlRunOnceInitialize (NTDLL.@) -@@ -531,13 +538,116 @@ NTSTATUS WINAPI RtlSleepConditionVariableSRW( RTL_CONDITION_VARIABLE *variable, +@@ -531,13 +538,123 @@ NTSTATUS WINAPI RtlSleepConditionVariableSRW( RTL_CONDITION_VARIABLE *variable, return status; } @@ -93,6 +108,13 @@ index 8df7015df9f..be462f27881 100644 + } +} + ++void addr_wait_free_entry(void) ++{ ++ struct addr_wait_entry *entry = NtCurrentTeb()->ReservedForPerf; ++ if (entry) ++ InterlockedExchangePointer( &entry->tid, NULL ); ++} ++ +static BOOL compare_addr( const void *addr, const void *cmp, SIZE_T size ) +{ + switch (size) @@ -147,7 +169,7 @@ index 8df7015df9f..be462f27881 100644 } /*********************************************************************** -@@ -545,7 +655,21 @@ NTSTATUS WINAPI RtlWaitOnAddress( const void *addr, const void *cmp, SIZE_T size +@@ -545,7 +662,21 @@ NTSTATUS WINAPI RtlWaitOnAddress( const void *addr, const void *cmp, SIZE_T size */ void WINAPI RtlWakeAddressAll( const void *addr ) { @@ -170,7 +192,7 @@ index 8df7015df9f..be462f27881 100644 } /*********************************************************************** -@@ -553,5 +677,22 @@ void WINAPI RtlWakeAddressAll( const void *addr ) +@@ -553,5 +684,22 @@ void WINAPI RtlWakeAddressAll( const void *addr ) */ void WINAPI RtlWakeAddressSingle( const void *addr ) { @@ -194,6 +216,19 @@ index 8df7015df9f..be462f27881 100644 + } + } } +diff --git a/dlls/ntdll/thread.c b/dlls/ntdll/thread.c +index 8e5a3a3a3a3..3a74b55aa1e 100644 +--- a/dlls/ntdll/thread.c ++++ b/dlls/ntdll/thread.c +@@ -86,6 +86,8 @@ void WINAPI RtlExitUserThread( ULONG status ) + if (last) RtlExitUserProcess( status ); + LdrShutdownThread(); + RtlFreeThreadActivationContextStack(); ++ /* must be done last, in particular after any heap allocations */ ++ addr_wait_free_entry(); + for (;;) NtTerminateThread( GetCurrentThread(), status ); + } + diff --git a/dlls/ntdll/unix/loader.c b/dlls/ntdll/unix/loader.c index 31cfb43d11f..5ab3121dd2c 100644 --- a/dlls/ntdll/unix/loader.c diff --git a/patches/patchinstall.sh b/patches/patchinstall.sh index ba431d3b..8721a2b9 100755 --- a/patches/patchinstall.sh +++ b/patches/patchinstall.sh @@ -3292,10 +3292,10 @@ fi # | * [#50292] Process-local synchronization objects use private interfaces into the Unix library # | # | Modified files: -# | * dlls/ntdll/Makefile.in, dlls/ntdll/critsection.c, dlls/ntdll/ntdll.spec, dlls/ntdll/sync.c, -# | dlls/ntdll/tests/Makefile.in, dlls/ntdll/tests/om.c, dlls/ntdll/tests/sync.c, dlls/ntdll/unix/loader.c, -# | dlls/ntdll/unix/sync.c, dlls/ntdll/unix/thread.c, dlls/ntdll/unix/unix_private.h, dlls/ntdll/unix/virtual.c, -# | dlls/ntdll/unixlib.h, include/winbase.h, include/winternl.h +# | * dlls/ntdll/Makefile.in, dlls/ntdll/critsection.c, dlls/ntdll/ntdll.spec, dlls/ntdll/ntdll_misc.h, dlls/ntdll/sync.c, +# | dlls/ntdll/tests/Makefile.in, dlls/ntdll/tests/om.c, dlls/ntdll/tests/sync.c, dlls/ntdll/thread.c, +# | dlls/ntdll/unix/loader.c, dlls/ntdll/unix/sync.c, dlls/ntdll/unix/thread.c, dlls/ntdll/unix/unix_private.h, +# | dlls/ntdll/unix/virtual.c, dlls/ntdll/unixlib.h, include/winbase.h, include/winternl.h # | if test "$enable_ntdll_NtAlertThreadByThreadId" -eq 1; then patch_apply ntdll-NtAlertThreadByThreadId/0001-ntdll-tests-Move-some-tests-to-a-new-sync.c-file.patch