libc++ and unloading shared libraries

Status
Not open for further replies.
Any libc++ experts here?

I've recently run into a bug on code developed mainly on Linux in which delete'ing a class instance provided by a shared library leads to a SEGV after that library has been unloaded.

The most parsimonious explanation would be that this happens because the class dtor address has gone stale - but then it should happen on Linux too. So unless the shared library isn't actually unloaded on Linux (which I cannot verify easily), I must be looking at another explanation.

That explanation might be that libc++ somehow tracks all instances "new'ed" by a given shared library and deletes them when that library is unloaded, turning all registered copies into stale pointers.

Is that possible? It's certainly something that would make sense in light of the disappearing dtor thing but it would also introduce an incitation to bad programming practices (no?).
 

ComradeXavier

Ars Scholae Palatinae
882
Subscriptor++
This is how I interpret your description:
  1. Load library A;
  2. Create instance of class B provided by library A;
  3. Unload library A;
  4. Delete instance of class B.
Is this correct?

If so, the problem is that deleting the instance calls B's destructor. When you unloaded A, the memory pages that contained that destructor were unmapped. When the processor tries to load the first instruction from the destructor's address, you get a SEGV.

Edit: On second reading, I see that this is what you mean by "the destructor address has gone stale." I don't know why you wouldn't get the error on Linux. But in any case, code that keeps an instance after unloading the library that provides it should not be expected to work.
 
Yes, your analysis is correct (and the code that does this isn't mine ;))

I have not been able to find any evidence that unloading the library deletes dynamically allocated objects (the dtor of the class in question isn't called). Kind of reassuring actually.

This is KDE4 code, and the library unloading is done by a KDE function (KCModuleLoader::loadModule()). As far as I can see it does exactly the same thing on Linux, so why I'm not getting a crash there is a mystery to me.

Edit: when delete is called the library is loaded *again*; the delete is the result of (re)registering an attribute instance which involves deleting the previous instance if one is found. If on Linux the shared library is reloaded with its functions at the same address you shouldn't get a crash.
 

ComradeXavier

Ars Scholae Palatinae
882
Subscriptor++
I'm no expert on KDE. I think I wrote one toy program using it about 15 years ago.

However, I did think of one possibility. If the problematic class is derived from a class in another module (that was not unloaded), and the problematic class's destructor is optimizably empty, then hypothetically the compiler could generate no code for the destructor, and just leave the base class destructor's pointer in the vtable. Then deleting the instance wouldn't actually call any code from the unloaded module.

It wouldn't surprise me to discover that one platform's compiler performed that optimization and another did not (or even different versions of the same compiler, or different optimization flags).
 

ComradeXavier

Ars Scholae Palatinae
882
Subscriptor++
I don't know what contemporary Linux does, but Windows DLLs have a base address baked in when they're linked. Pre-ASLR, a DLL was always loaded at that base address if possible, because if it's relocated then the loader has to fix up all of the internal symbol addresses in addition to the exports. ASLR says, "security is more important than speed, so just always relocate to a random base address." It helps to have a 64-bit address space so you don't care so much about efficient packing.

Circa 2006 I was using a Microsoft tool that was part of the platform SDK to rebase all the DLLs in an application to non-conflicting base addresses so they could be loaded more efficiently. It made a noticeable difference in startup times with 50-100 DLLs containing most of that application's code.
 
Status
Not open for further replies.