GHCI/FFI/GMP/Me madness

Simon Marlow simonmar at
Tue Aug 10 07:59:46 EDT 2004

On 09 August 2004 16:57, Remi Turk wrote:

> On Sun, Aug 08, 2004 at 07:34:04AM -0700, Sigbjorn Finne wrote:
>> Hi,
>> please be aware that the RTS uses GMP as well, and upon
>> initialisation it sets GMP's 'memory functions' to allocate memory
>> from the RTS' heap. So, in the code below, the global variable
>> 'p' will end up having components pointing into the heap.
>> Which is fine, until a GC occurs and the pointed-to
>> GMP allocated value is eventually stomped on by the storage
>> manager for some other purpose.
>> I'm _guessing_ that's the reason for the behaviour you're seeing.
> Hm, I _was_ aware of mp_set_memory_functions being used by the RTS.
> I've seen it often enough in ltrace's ;)
> It does indeed sound rather plausible (and making big allocations
> and such does indeed cause it to happen earlier).
> At which point my next question is: what now? I don't feel really
> confident about my GHC-hacking skills (huh? skills? where? ;) so
> does that mean I'm out of luck?
> *looks* Am I correct that I'd have to copy any GMP-allocated
> memory to my own memory before returning from C and vice-versa?
> I hope not :(

GHC's use of GMP does cause problems if you want to use GMP for your own
purposes, or if you link with external code that wants to use GMP.  The
real problem is that GMP has internal state, which means it can't be
used in a modular way.  But there's not much we can do about that.


  - Rename all the symbols in our GMP to be unique. (sounds hard)

  - Replace GMP with something else (someone is working on this, 
    I believe).

  - try to get two copies of GMP into your program by pre-linking
    the RTS with one copy, then linking the rest of the program
    with the other copy.  I'm being intentionally vague here - I
    feel that this ought to be possible, but it's an ugly hack
    at best.

  - reset GMP's memory allocators before calling it from your code,
    and set them back to the RTS allocators afterward.  Slow, but it
    should work.  It doesn't solve the problem properly though: external
    libraries which use GMP are still broken.


More information about the Glasgow-haskell-users mailing list