what is a loader lock?
Asked Answered
A

2

9

I was dealing with threads and have a potential deadlock problem. Someone mentioned to me about a loader lock.

I couldn't find much information online. Can someone please help me and explain, "What is a Loader Lock" ?

Ambala answered 14/12, 2012 at 7:19 Comment(5)
Ask .. the person who said that? (Although, perhaps they were talking about LoaderLock, which I found via "google".)Nidify
Loader lock doesn't actually have anything to do with threads, although they don't exactly help avoid it. Deadlock is a generic threading problem.Convey
@HansPassant Yes i understand what a dead lock is. I am just trying to wrap my head around, what exactly is LoaderLock. Is it a special lock or whatAmbala
It is just a lock inside the Windows loader, held when it calls a DllMain() entrypoint and ensures DllMain() calls are serialized. It is not re-entrant so doesn't require a thread to bite. You can always see it back in the stack trace, LdrLockLoaderLock is at the top of the stack if you really have a problem.Convey
See Microsoft's Dynamic-Link Library Best Practices. It explains what Loader Lock is and discusses possible deadlock issues.Fugacity
J
9

For example, review this question:

Loader lock error

The general idea of loader lock: The system runs the code in DllMain inside a lock (as in - synchronization lock). Therefore, running non-trivial code inside DllMain is "asking for a deadlock"

Answer I've mentioned is based on this article:

Another reason not to do anything scary in your DllMain: Inadvertent deadlock

Your DllMain function runs inside the loader lock, one of the few times the OS lets you run code while one of its internal locks is held. This means that you must be extra careful not to violate a lock hierarchy in your DllMain; otherwise, you are asking for a deadlock.

The loader lock is taken by any function that needs to access the list of DLLs loaded into the process. This includes functions like GetModuleHandle and GetModuleFileName. If your DllMain enters a critical section or waits on a synchronization object, and that critical section or synchronization object is owned by some code that is in turn waiting for the loader lock, you just created a deadlock:

// global variable
CRITICAL_SECTION g_csGlobal;

// some code somewhere
EnterCriticalSection(&g_csGlobal);
... GetModuleFileName(MyInstance, ..);
LeaveCriticalSection(&g_csGlobal);

BOOL WINAPI
DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
        LPVOID lpvReserved)
{
  switch (fdwReason) {
  ...
  case DLL_THREAD_DETACH:
   EnterCriticalSection(&g_csGlobal);
   ...
  }
  ...
}

Please review the whole article for full understanding.

Journeywork answered 14/12, 2012 at 7:49 Comment(3)
can you elaborate on running non-trivial code ? What would classify as non-trivial ? Why will it cause deadlock ?Ambala
@brainydexter: in this context, non-trivial basically means "uses a Win32 API function" or "might take a long time to execute". There are only a few known safe API functions: "DllMain can create synchronization objects such as critical sections and mutexes, and use TLS." See msdn.microsoft.com/en-us/library/windows/desktop/…Sessoms
I do not feel that this answer answers the question. I've reviewed all links here and I still do not have a solid idea of exactly what the loader lock actually is. I know what it's used for, and I know some common pitfalls around it, but nowhere does it say exactly which primitive the loader lock is and the scope at which the lock applies. There's a reason why dictionaries don't define terms with examples and no definition.Gaddis
P
2

Loader refers to the OS (module) loader. Loader Lock is a system lock used by the loader to synchronize calls to DllMain. This way, the loader ensures that initialization / cleanup tasks required by DLLs are performed in a thread-safe manner.

This system lock is used for static initialization, static destruction and thread creation. It is also shared by various Win32 APIs.

Preventing Hangs in Windows Applications

(...) the operating system has its own internal process-specific lock that sometimes is held while your code executes. This lock is acquired when DLLs are loaded into the process, and is therefore called the 'loader lock.' The DllMain function always executes under the loader lock; (...)

Calling certain Win32 APIs might also acquire the loader lock on your behalf - functions like LoadLibraryEx, GetModuleHandle, and especially CoCreateInstance

Psychodrama answered 19/1, 2021 at 23:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.