GHCI/FFI/GMP/Me madness

Sigbjorn Finne sof at galois.com
Sun Aug 8 10:34:04 EDT 2004


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.

--sigbjorn

----- Original Message ----- 
From: "Remi Turk" <buran at xs4all.nl>
To: <glasgow-haskell-users at haskell.org>
Sent: Sunday, August 08, 2004 05:52
Subject: GHCI/FFI/GMP/Me madness


> [second attempt, this time from my bulk mailinglist address
>  instead of the normal one.]
>
> Hi all,
>
> I recently tried to create a ffi-binding to gmp in ghc, and
> failed miserably. After a few days of debugging, simplifying the
> code and tearing my hear out, I'm slightly completely stumped,
> and crying for help ;)
>
> In short: calling gmp-functions from GHCI *with a prompt between*
> them seems to do Really Bad Things. (read: memory corruption)
>
>
> The long story:
> ---------------
>
> mpz_t p;
>
> str_test()
> {
> gmp_printf("%Zd\n", p);
> }
>
> void mpz_new()
> {
> mpz_init_set_si(p, 1);
> }
>
> foreign import ccall mpz_new    :: IO ()
> foreign import ccall str_test   :: IO ()
>
>
> Prelude Main> mpz_new
> Prelude Main> str_test
> 1
> Prelude Main> str_test
> 1
> Prelude Main> str_test
> 1
> Prelude Main> str_test
> 1
> Prelude Main> str_test
> 1
> Prelude Main> str_test
> 1
> Prelude Main> str_test
> 1
> Prelude Main> str_test
> 1
> Prelude Main> str_test
> 1
> Prelude Main> str_test
> 1
> Prelude Main> str_test
> 1
> Prelude Main> str_test
> 1
> Prelude Main> str_test
> 1
> Prelude Main> str_test
> 142833060
> Prelude Main> str_test
> 142833060
>
>
> Using other flags, importing extra modules, using CVS 6.3 (a few
> weeks old) or not compiling it before loading it in GHCI slightly
> changes the symptoms (other wrong numbers or make it happen
> later/earlier) but copypasting the code from main some 10 to 20
> times seems to be a sure way to reproduce it.
>
> Simply running main doesn't seem to expose the problem.
> Now of course, GHCI uses Integer-ops during it's REPL, which I
> suspect is exactly what causes/exposes the problem.
>
> Am I doing (Un)Officially Forbidden Things? Is it time for a
> bug-report? Do I finally have to learn drinking coffee? ;)
> I'd be delighted to know.
>
> The full code is attached.
>
> TIA,
> Remi
>
> -- 
> Nobody can be exactly like me. Even I have trouble doing it.
>


----------------------------------------------------------------------------
----


> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at haskell.org
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
>



More information about the Glasgow-haskell-users mailing list