[LLVMdev] Critical sections cleanup

Martinez, Javier E javier.e.martinez at intel.com
Wed Dec 21 16:36:52 PST 2011

I haven't received any comments regarding the cleanup of critical section or the solution I proposed. I'm still interested in implementing a solution but would like feedback on the patches I sent with the original email.


From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] On Behalf Of Martinez, Javier E
Sent: Tuesday, December 06, 2011 3:27 PM
To: llvmdev at cs.uiuc.edu
Subject: [LLVMdev] Critical sections cleanup


I have an application that uses Clang and LLVM wrapped in a DLL. Last week I was debugging reported issues with Microsoft Application Verifier<http://www.microsoft.com/download/en/details.aspx?id=20028> that were related to how critical sections are handled. The issues are caused by critical sections that are either still active when heap blocks are deleted or the DLL is unloaded. Something to keep in mind, our codebase is still using LLVM 2.7 but from what I was able to tell I think my findings also apply to the trunk.

The first issue had to do with an active critical section in a heap that's being deleted. The problem was caused by a leak of the PassRegistrar object in Pass.cpp. I added a cleanup function to delete the PassRegistrar object before shutting LLVM down. The cleanup function also sets the PassRegistrar object to NULL so it can be reinitialized again. I see that in the trunk things work a bit different, the PassRegistrar object is wrapped in a ManagedStatic object which gets deleted at shutdown.

The second issue is that the critical section created in Signals.inc was still active when the DLL was unloaded. There are three places where critical sections are used in Windows, these are Mutex.inc/h, RWMutex.inc/h and Signals.inc/h. The first two define classes that use the critical sections and have destructors that delete them. The critical section in Signals has no mechanism for deleting. I again added a function to my local copy a function to delete the critical section. This cleanup function is called before shutting LLVM down.

There's a note on PassRegistry.cpp discussing the need for a solution that prevents the PassRegistrar object leak and works after shutdown. I think what I did fits the bill but it does need to be called explicitly at shutdown. I've attached my changes for review along with a sample of a modified llc.cpp to show how the changes work. If people are interested in them I can polish them up and get them checked in.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20111221/9274bb4b/attachment.html>

More information about the llvm-dev mailing list