https://git.reactos.org/?p=reactos.git;a=commitdiff;h=e302bacd7eec26abda16ee3674f61a7cf5588df6

commit e302bacd7eec26abda16ee3674f61a7cf5588df6
Author:     Timo Kreuzer <timo.kreu...@reactos.org>
AuthorDate: Mon Jun 3 10:06:57 2024 +0300
Commit:     Timo Kreuzer <timo.kreu...@reactos.org>
CommitDate: Sat Jan 25 21:50:32 2025 +0200

    [NTDLL] Acquire LdrpLoaderLock in LdrpInitializeThread
    
    This is required to protect against a race with LdrShutdownThread, which 
can lead to all kinds of problems, including deadlocks.
---
 dll/ntdll/ldr/ldrinit.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/dll/ntdll/ldr/ldrinit.c b/dll/ntdll/ldr/ldrinit.c
index 5ee01e3e68d..4fbc4f293aa 100644
--- a/dll/ntdll/ldr/ldrinit.c
+++ b/dll/ntdll/ldr/ldrinit.c
@@ -517,6 +517,9 @@ LdrpInitializeThread(IN PCONTEXT Context)
             NtCurrentTeb()->RealClientId.UniqueProcess,
             NtCurrentTeb()->RealClientId.UniqueThread);
 
+    /* Acquire the loader Lock */
+    RtlEnterCriticalSection(&LdrpLoaderLock);
+
     /* Allocate an Activation Context Stack */
     DPRINT("ActivationContextStack %p\n", 
NtCurrentTeb()->ActivationContextStackPointer);
     Status = 
RtlAllocateActivationContextStack(&NtCurrentTeb()->ActivationContextStackPointer);
@@ -526,7 +529,7 @@ LdrpInitializeThread(IN PCONTEXT Context)
     }
 
     /* Make sure we are not shutting down */
-    if (LdrpShutdownInProgress) return;
+    if (LdrpShutdownInProgress) goto Exit;
 
     /* Allocate TLS */
     LdrpAllocateTls();
@@ -633,6 +636,11 @@ LdrpInitializeThread(IN PCONTEXT Context)
         RtlDeactivateActivationContextUnsafeFast(&ActCtx);
     }
 
+Exit:
+
+    /* Release the loader lock */
+    RtlLeaveCriticalSection(&LdrpLoaderLock);
+
     DPRINT("LdrpInitializeThread() done\n");
 }
 

Reply via email to