logo       

bug with -O -ffi and multiple module: msg#00067

lang.haskell.glasgow.bugs

Subject: bug with -O -ffi and multiple module

Okay, here's a weird one. There's something wrong with the ffi when using
-O and the foreign imports are from another module. For example, our
foreign module, foo.c contains functions:

void* openFile(char*fn);
void closeFile(void*f);
float readFloat(void*f);

which are interfaced from foo.h. We have a FooIntr.hs interface file,
which looks like:

foreign import ccall "foo.h openFile" c__openFile :: Ptr CChar -> IO
(Ptr ())
foreign import ccall "foo.h closeFile" c__closeFile :: Ptr () -> IO ()
foreign import ccall "foo.h readFloat" c__readFloat :: Ptr () -> IO
CFloat

openFile fn = cstring fn >>= c__openFile
closeFile = c__closeFile
readFloat h = do CFloat f <- c__readFloat h; return f

(plus helper definition of cstring)

Now, in our Foo.hs main file, we have:

main = do
[fn] <- getArgs
h <- openFile fn
f1 <- readFloat h
f2 <- readFloat h
closeFile h
print (f1,f2)

In a file "bar", we have "1.234 5.678".

We compile foo.c via 'gcc -c foo.c' which works fine, then compile Foo.hs
using

ghc --make -ffi Foo.hs foo.o -o foo

we then run

./foo bar

and everything works fine. We erase the intermediate files and recompile
with:

ghc --make -ffi Foo.hs foo.o -o foo -O

and then run

./foo bar

and the results are random (seemingly) numbers.

Interestingly, if we include FooIntr into Foo and don't have Foo import it
anymore, everything works fine with -O on.

I've attached the actual files in a tar so someone can take a look.

- Hal

--
Hal Daume III

"Computer science is no more about computers | hdaume@xxxxxxx
than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume

Attachment: bug.tar.gz
Description: Binary data

<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise