#4059: possible error in foreign wrapper reentering Haskell
-----------------------+----------------------------------------------------
    Reporter:  aruiz   |       Owner:                             
        Type:  bug     |      Status:  new                        
    Priority:  normal  |   Component:  Compiler (FFI)             
     Version:  6.12.2  |    Keywords:                             
          Os:  Linux   |    Testcase:                             
Architecture:  x86     |     Failure:  Incorrect result at runtime
-----------------------+----------------------------------------------------
 The following simple code works as expected in 6.12.1, 6.10, and all
 previous ghc versions:

 fun.c:
 {{{
 double deriv(double f(double), double x, double h)
 {
     return (f(x+h)-f(x))/h;
 }

 }}}



 pru.hs:
 {{{
 {-# LANGUAGE ForeignFunctionInterface #-}

 import Foreign

 d f x = unsafePerformIO $ do
     g <- mkfun f
     r <- deriv g x 0.01
     return r

 main = do
     print $ d sin 0
     print $ d (\x-> x * d (\y-> x+y) 1) 1


 foreign import ccall safe "deriv" deriv :: FunPtr (Double->Double) ->
 Double -> Double -> IO Double

 foreign import ccall safe "wrapper" mkfun:: (Double -> Double) -> IO(
 FunPtr (Double -> Double))

 }}}

 $ ghc -V

 The Glorious Glasgow Haskell Compilation System, version 6.12.1

 $ ghc --make pru.hs fun.c -fforce-recomp

 [1 of 1] Compiling Main             ( pru.hs, pru.o )

 Linking pru ...

 $ ./pru

 0.9999833334166665

 1.000000000004464



 But it gives a wrong result in ghc-6.12.2:

 $ ghc -V

 The Glorious Glasgow Haskell Compilation System, version 6.12.2

 $ ghc --make pru.hs fun.c -fforce-recomp

 [1 of 1] Compiling Main             ( pru.hs, pru.o )

 Linking pru ...

 $ ./pru

 0.9999833334166665

 0.0

 I have discovered this problem when a similar test in my hmatrix library
 failed in 6.12.2. As shown above, it seems that the wrapper only fails on
 C functions which call back to Haskell.

 Linux 2.6.28-17-generic #58-Ubuntu SMP

 ghc-6.12.2-i386-unknown-linux-n.tar.bz2

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/4059>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to