This is an archive of the discontinued LLVM Phabricator instance.

ManagedStatic: Destroy from destructor
AbandonedPublic

Authored by nhaehnle on Jun 20 2022, 2:28 AM.

Details

Summary

This allows LLVM to be safely loaded and unloaded as a shared library
without leaking memory:

Consider the scenario where shared library Foo links against LLVM as
a shared library, and Foo may be loaded and unloaded multiple times
in the same process. Should Foo call llvm_shutdown or not? If it does
not, and LLVM is also loaded and unloaded multiple times, then
memory is leaked. If it does call llvm_shutdown and LLVM *isn't*
also unloaded, the state of LLVM's global statics is corrupted because
while the ManagedStatics may be re-initialized, *other* global
constructors aren't re-run.

Keep in mind that Foo itself may use ManagedStatic. If Foo is unloaded
but LLVM isn't, those statics should be destroyed.

The safe solution is to skip llvm_shutdown() altogether and destroy
managed statics from their destructor.

LLD relies on being able to call _exit() and still get some side-effects
of managed static destructors. We make this expectation much more
explicit.

Diff Detail

Event Timeline

nhaehnle created this revision.Jun 20 2022, 2:28 AM
Herald added a reviewer: bollu. · View Herald Transcript
Herald added a reviewer: MaskRay. · View Herald Transcript
Herald added a reviewer: rafauler. · View Herald Transcript
Herald added a reviewer: Amir. · View Herald Transcript
Herald added a reviewer: maksfb. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
nhaehnle requested review of this revision.Jun 20 2022, 2:28 AM
Herald added projects: Restricted Project, Restricted Project, Restricted Project, Restricted Project. · View Herald TranscriptJun 20 2022, 2:28 AM
lamb-j added a subscriber: lamb-j.Jun 20 2022, 12:46 PM
simoll added a subscriber: simoll.Jun 21 2022, 3:17 AM

Abandoning this in favor of removing ManagedStatic entirely via the stack of changes that ends in D129134

nhaehnle abandoned this revision.Jul 5 2022, 2:27 AM