Crash.hs:
------------------------------------------------------------------------
import FFI
main:: IO ()
main = do
f <- exportF $ \x -> do
a <- mallocElem x
poke a x
fo <- makeForeignObj a (return ())
look fo
putStrLn "f"
loop f
foreign export dynamic exportF :: (Int -> IO ()) -> IO Addr
foreign import loop :: Addr -> IO ()
foreign import look :: ForeignObj -> IO ()
------------------------------------------------------------------------
crash.c:
------------------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
void loop (void (*f) (int))
{
int i = 0;
while (1)
{
printf ("%d\n", ++i);
f (i);
}
}
void look (int *x) {}
------------------------------------------------------------------------
ghc -c -fglasgow-exts Crash.hs crash.c
ghc -fglasgow-exts Crash.o Crash_stub.o crash.o -o crash
./crash
It crashes with a segmentation fault after 617 iterations.
Minor changes to the program (even altering the string printed in f)
change the behavior, including preventing the crash.
Linux, ghc-4.06, gcc-2.95.1.
------------------------------------------------------------------------
The story (not part of the bug report).
I've made most of a Haskell<->Perl interface for fun. Closures
are convertible in both directions, Perl values can be created and
examined from the Haskell side, abstract values from each language
can be manipulated in the other one, objects not having cycles across
languages are garbage collected. Unfortunately the first non-trivial
example, a Haskell sorting function of values of arbitrary type,
implemented using Perl's sort builtin, crashes when the sorted list
is big enough for the garbage collection to occur. I could not find
my error nor prove that ghc produces wrong code.
I temporarily gave up and made most of a Haskell<->OCaml interface
instead. Closures are convertible in both directions, OCaml values
can be created and examined from the Haskell side, abstract values
from each language can be manipulated in the other one, objects not
having cycles across languages are garbage collected. Unfortunately
the first non-trivial example, a Haskell sorting function of values
of arbitrary type, implemented using OCaml's Sort.array function,
crashes when the sorted list is big enough...
Arrgh! Fortunately I succeeded in extracting a simple Haskell code
that demonstrates the crash.
--
__("< Marcin Kowalczyk * [EMAIL PROTECTED] http://qrczak.ids.net.pl/
\__/ GCS/M d- s+:-- a22 C+++$ UL++>++++$ P+++ L++>++++$ E-
^^ W++ N+++ o? K? w(---) O? M- V? PS-- PE++ Y? PGP+ t
QRCZAK 5? X- R tv-- b+>++ DI D- G+ e>++++ h! r--%>++ y-