[LLVMdev] JIT allocates global data in function body memory
rnk at mit.edu
Mon Jun 29 17:41:39 PDT 2009
So I (think I) found a bug in the JIT:
Basically, globals used by a function are allocated in the same buffer
as the first code that uses it. However, when you free the machine
code, you also free the memory holding the global's data. The address
is still in the GlobalValue map, so any other code using that global
will access freed memory, which will cause problems as soon as you
reallocate that memory for something else.
I tracked down the commit that introduced the bug:
It very nicely explains what it does, but not why it does it, which
I'd like to know before I change it. I couldn't find the author
(johannes) on IRC so ssen told me to ask LLVMdev about this behavior.
There's even a patch to work around this behavior on Apple ARM
So what should the right long-term behavior be? It makes sense to me
to use the JITMemoryManager for this so that clients of the JIT can
customize allocation instead of using malloc or new char. On the
other hand, that complicates the API and requires a homegrown malloc
implementation in the DefaultMemoryManager.
More information about the llvm-dev