[PATCH 03/22] ntdll: Use helpers to lock and unlock loader_section.

Paul Gofman pgofman at codeweavers.com
Fri Oct 1 13:20:48 CDT 2021


Signed-off-by: Paul Gofman <pgofman at codeweavers.com>
---
 dlls/ntdll/loader.c | 120 ++++++++++++++++++++++++++------------------
 1 file changed, 70 insertions(+), 50 deletions(-)

diff --git a/dlls/ntdll/loader.c b/dlls/ntdll/loader.c
index f3b9837194b..1c4faf2fba2 100644
--- a/dlls/ntdll/loader.c
+++ b/dlls/ntdll/loader.c
@@ -208,6 +208,26 @@ static inline BOOL contains_path( LPCWSTR name )
     return ((*name && (name[1] == ':')) || wcschr(name, '/') || wcschr(name, '\\'));
 }
 
+/*************************************************************************
+ *		lock_loader_exclusive
+ *
+ * Take exclusive loader lock.
+ */
+static void lock_loader_exclusive(void)
+{
+    RtlEnterCriticalSection( &loader_section );
+}
+
+/*************************************************************************
+ *		unlock_loader
+ *
+ * Release loader lock.
+ */
+static void unlock_loader(void)
+{
+    RtlLeaveCriticalSection( &loader_section );
+}
+
 #define RTL_UNLOAD_EVENT_TRACE_NUMBER 64
 
 typedef struct _RTL_UNLOAD_EVENT_TRACE
@@ -481,7 +501,7 @@ static void call_ldr_notifications( ULONG reason, LDR_DATA_TABLE_ENTRY *module )
  *		get_modref
  *
  * Looks for the referenced HMODULE in the current process
- * The loader_section must be locked while calling this function.
+ * The loader must be locked while calling this function.
  */
 static WINE_MODREF *get_modref( HMODULE hmod )
 {
@@ -505,7 +525,7 @@ static WINE_MODREF *get_modref( HMODULE hmod )
  *	    find_basename_module
  *
  * Find a module from its base name.
- * The loader_section must be locked while calling this function
+ * The loader must be locked while calling this function
  */
 static WINE_MODREF *find_basename_module( LPCWSTR name )
 {
@@ -535,7 +555,7 @@ static WINE_MODREF *find_basename_module( LPCWSTR name )
  *	    find_fullname_module
  *
  * Find a module from its full path name.
- * The loader_section must be locked while calling this function
+ * The loader must be locked while calling this function
  */
 static WINE_MODREF *find_fullname_module( const UNICODE_STRING *nt_name )
 {
@@ -567,7 +587,7 @@ static WINE_MODREF *find_fullname_module( const UNICODE_STRING *nt_name )
  *	    find_fileid_module
  *
  * Find a module from its file id.
- * The loader_section must be locked while calling this function
+ * The loader must be locked while calling this function
  */
 static WINE_MODREF *find_fileid_module( const struct file_id *id )
 {
@@ -616,7 +636,7 @@ static WINE_MODREF **grow_module_deps( WINE_MODREF *wm, int count )
  *		find_forwarded_export
  *
  * Find the final function pointer for a forwarded function.
- * The loader_section must be locked while calling this function.
+ * The loader must be locked while calling this function.
  */
 static FARPROC find_forwarded_export( HMODULE module, const char *forward, LPCWSTR load_path )
 {
@@ -694,7 +714,7 @@ static FARPROC find_forwarded_export( HMODULE module, const char *forward, LPCWS
  *
  * Find an exported function by ordinal.
  * The exports base must have been subtracted from the ordinal already.
- * The loader_section must be locked while calling this function.
+ * The loader must be locked while calling this function.
  */
 static FARPROC find_ordinal_export( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
                                     DWORD exp_size, DWORD ordinal, LPCWSTR load_path )
@@ -757,7 +777,7 @@ static int find_name_in_exports( HMODULE module, const IMAGE_EXPORT_DIRECTORY *e
  *		find_named_export
  *
  * Find an exported function by name.
- * The loader_section must be locked while calling this function.
+ * The loader must be locked while calling this function.
  */
 static FARPROC find_named_export( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
                                   DWORD exp_size, const char *name, int hint, LPCWSTR load_path )
@@ -806,7 +826,7 @@ void * WINAPI RtlFindExportedRoutineByName( HMODULE module, const char *name )
  *		import_dll
  *
  * Import the dll specified by the given import descriptor.
- * The loader_section must be locked while calling this function.
+ * The loader must be locked while calling this function.
  */
 static BOOL import_dll( HMODULE module, const IMAGE_IMPORT_DESCRIPTOR *descr, LPCWSTR load_path, WINE_MODREF **pwm )
 {
@@ -1015,7 +1035,7 @@ static BOOL is_dll_native_subsystem( LDR_DATA_TABLE_ENTRY *mod, const IMAGE_NT_H
  *		alloc_tls_slot
  *
  * Allocate a TLS slot for a newly-loaded module.
- * The loader_section must be locked while calling this function.
+ * The loader must be locked while calling this function.
  */
 static SHORT alloc_tls_slot( LDR_DATA_TABLE_ENTRY *mod )
 {
@@ -1099,7 +1119,7 @@ static SHORT alloc_tls_slot( LDR_DATA_TABLE_ENTRY *mod )
  *		free_tls_slot
  *
  * Free the module TLS slot on unload.
- * The loader_section must be locked while calling this function.
+ * The loader must be locked while calling this function.
  */
 static void free_tls_slot( LDR_DATA_TABLE_ENTRY *mod )
 {
@@ -1115,7 +1135,7 @@ static void free_tls_slot( LDR_DATA_TABLE_ENTRY *mod )
  *       fixup_imports_ilonly
  *
  * Fixup imports for an IL-only module. All we do is import mscoree.
- * The loader_section must be locked while calling this function.
+ * The loader must be locked while calling this function.
  */
 static NTSTATUS fixup_imports_ilonly( WINE_MODREF *wm, LPCWSTR load_path, void **entry )
 {
@@ -1154,7 +1174,7 @@ static NTSTATUS fixup_imports_ilonly( WINE_MODREF *wm, LPCWSTR load_path, void *
  *       fixup_imports
  *
  * Fixup all imports of a given module.
- * The loader_section must be locked while calling this function.
+ * The loader must be locked while calling this function.
  */
 static NTSTATUS fixup_imports( WINE_MODREF *wm, LPCWSTR load_path )
 {
@@ -1210,7 +1230,7 @@ static NTSTATUS fixup_imports( WINE_MODREF *wm, LPCWSTR load_path )
  *		alloc_module
  *
  * Allocate a WINE_MODREF structure and add it to the process list
- * The loader_section must be locked while calling this function.
+ * The loader must be locked while calling this function.
  */
 static WINE_MODREF *alloc_module( HMODULE hModule, const UNICODE_STRING *nt_name, BOOL builtin )
 {
@@ -1430,7 +1450,7 @@ static NTSTATUS MODULE_InitDLL( WINE_MODREF *wm, UINT reason, LPVOID lpReserved
  * detach notifications are called in the reverse of the sequence the attach
  * notifications *returned*.
  *
- * The loader_section must be locked while calling this function.
+ * The loader must be locked while calling this function.
  */
 static NTSTATUS process_attach( WINE_MODREF *wm, LPVOID lpReserved )
 {
@@ -1542,7 +1562,7 @@ static void process_detach(void)
  *
  * Send DLL thread attach notifications. These are sent in the
  * reverse sequence of process detach notification.
- * The loader_section must be locked while calling this function.
+ * The loader must be locked while calling this function.
  */
 static void thread_attach(void)
 {
@@ -1572,7 +1592,7 @@ NTSTATUS WINAPI LdrDisableThreadCalloutsForDll(HMODULE hModule)
     WINE_MODREF *wm;
     NTSTATUS    ret = STATUS_SUCCESS;
 
-    RtlEnterCriticalSection( &loader_section );
+    lock_loader_exclusive();
 
     wm = get_modref( hModule );
     if (!wm || wm->ldr.TlsIndex != -1)
@@ -1580,7 +1600,7 @@ NTSTATUS WINAPI LdrDisableThreadCalloutsForDll(HMODULE hModule)
     else
         wm->ldr.Flags |= LDR_NO_DLL_CALLS;
 
-    RtlLeaveCriticalSection( &loader_section );
+    unlock_loader();
 
     return ret;
 }
@@ -1626,7 +1646,7 @@ NTSTATUS WINAPI LdrEnumerateLoadedModules( void *unknown, LDRENUMPROC callback,
     if (unknown || !callback)
         return STATUS_INVALID_PARAMETER;
 
-    RtlEnterCriticalSection( &loader_section );
+    lock_loader_exclusive();
 
     mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
     for (entry = mark->Flink; entry != mark; entry = entry->Flink)
@@ -1636,7 +1656,7 @@ NTSTATUS WINAPI LdrEnumerateLoadedModules( void *unknown, LDRENUMPROC callback,
         if (stop) break;
     }
 
-    RtlLeaveCriticalSection( &loader_section );
+    unlock_loader();
     return STATUS_SUCCESS;
 }
 
@@ -1661,9 +1681,9 @@ NTSTATUS WINAPI LdrRegisterDllNotification(ULONG flags, PLDR_DLL_NOTIFICATION_FU
     notify->callback = callback;
     notify->context = context;
 
-    RtlEnterCriticalSection( &loader_section );
+    lock_loader_exclusive();
     list_add_tail( &ldr_notifications, &notify->entry );
-    RtlLeaveCriticalSection( &loader_section );
+    unlock_loader();
 
     *cookie = notify;
     return STATUS_SUCCESS;
@@ -1680,9 +1700,9 @@ NTSTATUS WINAPI LdrUnregisterDllNotification( void *cookie )
 
     if (!notify) return STATUS_INVALID_PARAMETER;
 
-    RtlEnterCriticalSection( &loader_section );
+    lock_loader_exclusive();
     list_remove( &notify->entry );
-    RtlLeaveCriticalSection( &loader_section );
+    unlock_loader();
 
     RtlFreeHeap( GetProcessHeap(), 0, notify );
     return STATUS_SUCCESS;
@@ -1747,7 +1767,7 @@ NTSTATUS WINAPI LdrGetProcedureAddress(HMODULE module, const ANSI_STRING *name,
     DWORD exp_size;
     NTSTATUS ret = STATUS_PROCEDURE_NOT_FOUND;
 
-    RtlEnterCriticalSection( &loader_section );
+    lock_loader_exclusive();
 
     /* check if the module itself is invalid to return the proper error */
     if (!get_modref( module )) ret = STATUS_DLL_NOT_FOUND;
@@ -1763,7 +1783,7 @@ NTSTATUS WINAPI LdrGetProcedureAddress(HMODULE module, const ANSI_STRING *name,
         }
     }
 
-    RtlLeaveCriticalSection( &loader_section );
+    unlock_loader();
     return ret;
 }
 
@@ -2882,7 +2902,7 @@ done:
  *	load_dll  (internal)
  *
  * Load a PE style module according to the load order.
- * The loader_section must be locked while calling this function.
+ * The loader must be locked while calling this function.
  */
 static NTSTATUS load_dll( const WCHAR *load_path, const WCHAR *libname, const WCHAR *default_ext,
                           DWORD flags, WINE_MODREF** pwm )
@@ -2958,12 +2978,12 @@ NTSTATUS __cdecl __wine_init_unix_lib( HMODULE module, DWORD reason, const void
     WINE_MODREF *wm;
     NTSTATUS ret;
 
-    RtlEnterCriticalSection( &loader_section );
+    lock_loader_exclusive();
 
     if ((wm = get_modref( module ))) ret = unix_funcs->init_unix_lib( module, reason, ptr_in, ptr_out );
     else ret = STATUS_INVALID_HANDLE;
 
-    RtlLeaveCriticalSection( &loader_section );
+    unlock_loader();
     return ret;
 }
 
@@ -2988,7 +3008,7 @@ NTSTATUS WINAPI DECLSPEC_HOTPATCH LdrLoadDll(LPCWSTR path_name, DWORD flags,
     WINE_MODREF *wm;
     NTSTATUS nts;
 
-    RtlEnterCriticalSection( &loader_section );
+    lock_loader_exclusive();
 
     nts = load_dll( path_name, libname->Buffer, L".dll", flags, &wm );
 
@@ -3003,7 +3023,7 @@ NTSTATUS WINAPI DECLSPEC_HOTPATCH LdrLoadDll(LPCWSTR path_name, DWORD flags,
     }
     *hModule = (wm) ? wm->ldr.DllBase : NULL;
 
-    RtlLeaveCriticalSection( &loader_section );
+    unlock_loader();
     return nts;
 }
 
@@ -3020,7 +3040,7 @@ NTSTATUS WINAPI LdrGetDllFullName( HMODULE module, UNICODE_STRING *name )
 
     if (!module) module = NtCurrentTeb()->Peb->ImageBaseAddress;
 
-    RtlEnterCriticalSection( &loader_section );
+    lock_loader_exclusive();
     wm = get_modref( module );
     if (wm)
     {
@@ -3028,7 +3048,7 @@ NTSTATUS WINAPI LdrGetDllFullName( HMODULE module, UNICODE_STRING *name )
         if (name->MaximumLength < wm->ldr.FullDllName.Length + sizeof(WCHAR)) status = STATUS_BUFFER_TOO_SMALL;
         else status = STATUS_SUCCESS;
     } else status = STATUS_DLL_NOT_FOUND;
-    RtlLeaveCriticalSection( &loader_section );
+    unlock_loader();
 
     return status;
 }
@@ -3063,7 +3083,7 @@ NTSTATUS WINAPI LdrGetDllHandleEx( ULONG flags, LPCWSTR load_path, ULONG *dll_ch
     if (flags & ~supported_flags) FIXME( "Unsupported flags %#x.\n", flags );
     if (dll_characteristics) FIXME( "dll_characteristics unsupported.\n" );
 
-    RtlEnterCriticalSection( &loader_section );
+    lock_loader_exclusive();
 
     status = find_dll_file( load_path, name->Buffer, L".dll", &nt_name, &wm, &mapping, &image_info, &id );
 
@@ -3083,7 +3103,7 @@ NTSTATUS WINAPI LdrGetDllHandleEx( ULONG flags, LPCWSTR load_path, ULONG *dll_ch
             LdrAddRefDll( 0, *base );
     }
 
-    RtlLeaveCriticalSection( &loader_section );
+    unlock_loader();
     TRACE( "%s -> %p (load path %s)\n", debugstr_us(name), status ? NULL : *base, debugstr_w(load_path) );
     return status;
 }
@@ -3108,7 +3128,7 @@ NTSTATUS WINAPI LdrAddRefDll( ULONG flags, HMODULE module )
 
     if (flags & ~LDR_ADDREF_DLL_PIN) FIXME( "%p flags %x not implemented\n", module, flags );
 
-    RtlEnterCriticalSection( &loader_section );
+    lock_loader_exclusive();
 
     if ((wm = get_modref( module )))
     {
@@ -3120,7 +3140,7 @@ NTSTATUS WINAPI LdrAddRefDll( ULONG flags, HMODULE module )
     }
     else ret = STATUS_INVALID_PARAMETER;
 
-    RtlLeaveCriticalSection( &loader_section );
+    unlock_loader();
     return ret;
 }
 
@@ -3206,7 +3226,7 @@ NTSTATUS WINAPI LdrQueryProcessModuleInformation(RTL_PROCESS_MODULES *smi,
 
     smi->ModulesCount = 0;
 
-    RtlEnterCriticalSection( &loader_section );
+    lock_loader_exclusive();
     mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
     for (entry = mark->Flink; entry != mark; entry = entry->Flink)
     {
@@ -3234,7 +3254,7 @@ NTSTATUS WINAPI LdrQueryProcessModuleInformation(RTL_PROCESS_MODULES *smi,
         }
         else nts = STATUS_INFO_LENGTH_MISMATCH;
     }
-    RtlLeaveCriticalSection( &loader_section );
+    unlock_loader();
 
     if (req_size) *req_size = size;
 
@@ -3452,7 +3472,7 @@ void WINAPI LdrShutdownProcess(void)
  */
 void WINAPI RtlExitUserProcess( DWORD status )
 {
-    RtlEnterCriticalSection( &loader_section );
+    lock_loader_exclusive();
     RtlAcquirePebLock();
     NtTerminateProcess( 0, status );
     LdrShutdownProcess();
@@ -3478,7 +3498,7 @@ void WINAPI LdrShutdownThread(void)
 
     RtlProcessFlsData( NtCurrentTeb()->FlsSlots, 1 );
 
-    RtlEnterCriticalSection( &loader_section );
+    lock_loader_exclusive();
     wm = get_modref( NtCurrentTeb()->Peb->ImageBaseAddress );
 
     mark = &NtCurrentTeb()->Peb->LdrData->InInitializationOrderModuleList;
@@ -3510,7 +3530,7 @@ void WINAPI LdrShutdownThread(void)
     NtCurrentTeb()->TlsExpansionSlots = NULL;
     RtlReleasePebLock();
 
-    RtlLeaveCriticalSection( &loader_section );
+    unlock_loader();
     /* don't call DbgUiGetThreadDebugObject as some apps hook it and terminate if called */
     if (NtCurrentTeb()->DbgSsReserved[1]) NtClose( NtCurrentTeb()->DbgSsReserved[1] );
     RtlFreeThreadActivationContextStack();
@@ -3551,7 +3571,7 @@ static void free_modref( WINE_MODREF *wm )
  * Remove all unused modrefs and call the internal unloading routines
  * for the library type.
  *
- * The loader_section must be locked while calling this function.
+ * The loader must be locked while calling this function.
  */
 static void MODULE_FlushModrefs(void)
 {
@@ -3582,7 +3602,7 @@ static void MODULE_FlushModrefs(void)
 /***********************************************************************
  *           MODULE_DecRefCount
  *
- * The loader_section must be locked while calling this function.
+ * The loader must be locked while calling this function.
  */
 static void MODULE_DecRefCount( WINE_MODREF *wm )
 {
@@ -3625,7 +3645,7 @@ NTSTATUS WINAPI LdrUnloadDll( HMODULE hModule )
 
     TRACE("(%p)\n", hModule);
 
-    RtlEnterCriticalSection( &loader_section );
+    lock_loader_exclusive();
 
     free_lib_count++;
     if ((wm = get_modref( hModule )) != NULL)
@@ -3649,7 +3669,7 @@ NTSTATUS WINAPI LdrUnloadDll( HMODULE hModule )
 
     free_lib_count--;
 
-    RtlLeaveCriticalSection( &loader_section );
+    unlock_loader();
 
     return retv;
 }
@@ -3762,7 +3782,7 @@ static void init_wow64( CONTEXT *context )
         imports_fixup_done = TRUE;
     }
 
-    RtlLeaveCriticalSection( &loader_section );
+    unlock_loader();
     pWow64LdrpInitialize( context );
 }
 
@@ -3872,7 +3892,7 @@ void WINAPI LdrInitializeThunk( CONTEXT *context, ULONG_PTR unknown2, ULONG_PTR
         RtlInitializeSRWLock( &ldr_data_srw_lock );
     }
 
-    RtlEnterCriticalSection( &loader_section );
+    lock_loader_exclusive();
 
     if (!imports_fixup_done)
     {
@@ -3985,7 +4005,7 @@ void WINAPI LdrInitializeThunk( CONTEXT *context, ULONG_PTR unknown2, ULONG_PTR
         if (wm->ldr.TlsIndex != -1) call_tls_callbacks( wm->ldr.DllBase, DLL_THREAD_ATTACH );
     }
 
-    RtlLeaveCriticalSection( &loader_section );
+    unlock_loader();
     signal_start_thread( context );
 }
 
@@ -4075,9 +4095,9 @@ PVOID WINAPI RtlPcToFileHeader( PVOID pc, PVOID *address )
     LDR_DATA_TABLE_ENTRY *module;
     PVOID ret = NULL;
 
-    RtlEnterCriticalSection( &loader_section );
+    lock_loader_exclusive();
     if (!LdrFindEntryForAddress( pc, &module )) ret = module->DllBase;
-    RtlLeaveCriticalSection( &loader_section );
+    unlock_loader();
     *address = ret;
     return ret;
 }
-- 
2.31.1




More information about the wine-devel mailing list