[LLVMdev] JIT allocates global data in function body memory

Dale Johannesen dalej at apple.com
Mon Jun 29 17:50:18 PDT 2009


On Jun 29, 2009, at 5:41 PMPDT, Reid Kleckner wrote:

> So I (think I) found a bug in the JIT:
> http://llvm.org/bugs/show_bug.cgi?id=4483
>
> 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:
> http://llvm.org/viewvc/llvm-project?view=rev&revision=54442
>
> 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.

That's me (and I'm not on IRC because I like messages to be  
archived).  The reason everything needs to go in the same buffer is  
that we're JITting code on one machine, then sending it to another to  
be executed, and references from one buffer to another won't work in  
that environment.  So that model needs to continue to work.  If you  
want to generalize it so other models work as well, go ahead.

> There's even a patch to work around this behavior on Apple ARM
> platforms:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/JIT/JIT.cpp?view=diff&pathrev=72630&r1=58687&r2=58688
>
> 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.
>
> Reid
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev




More information about the llvm-dev mailing list