[Mesa-dev] [RFC][PATCH] gallivm: Clean up llvm data structures upon destruction.

David Riley davidriley at chromium.org
Wed Jul 18 20:34:42 UTC 2018

Without this change, dlopen()/dlclose() of any driver results in memory
being leaked which becomes problematic if things are being reinitialized
repeatedly (eg from a fuzzer).

Even with this change, repeated dlopen()/dlclose() results in a single
LLVM mutex being allocated and never freed (used to synchronize 
ManagedStatic).  I've spoken to some LLVM folks and haven't come up with
a great answer on avoiding that memory leak and not running into issues
with signal handlers and/or global destructors being called after

With regards to the RFC, there's potentially some issues here with
multiple drivers being loaded with separate LLVM instances and being
shared due to the dlopen() being done with RTLD_GLOBAL, but this falls
squarely outside my realm of confidence.

 src/gallium/auxiliary/gallivm/lp_bld_misc.cpp | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/gallium/auxiliary/gallivm/lp_bld_misc.cpp b/src/gallium/auxiliary/gallivm/lp_bld_misc.cpp
index 79dbedbb56..d537ae6029 100644
--- a/src/gallium/auxiliary/gallivm/lp_bld_misc.cpp
+++ b/src/gallium/auxiliary/gallivm/lp_bld_misc.cpp
@@ -68,6 +68,7 @@
 #include <llvm/Support/CommandLine.h>
 #include <llvm/Support/Host.h>
+#include <llvm/Support/ManagedStatic.h>
 #include <llvm/Support/PrettyStackTrace.h>
 #include <llvm/Support/TargetSelect.h>
@@ -813,3 +814,12 @@ lp_is_function(LLVMValueRef v)
 	return llvm::isa<llvm::Function>(llvm::unwrap(v));
+ * Attempt to clean up to allow drivers to be loaded/unloaded without
+ * leaking excessive amounts of memory.
+ */
+__attribute__((destructor)) static void llvm_fini()
+   llvm::llvm_shutdown();

More information about the mesa-dev mailing list