Opened 4 years ago

Closed 4 years ago

#4059 closed bug (duplicate)

possible error in foreign wrapper reentering Haskell

Reported by: aruiz Owned by:
Priority: normal Milestone:
Component: Compiler (FFI) Version: 6.12.2
Keywords: Cc:
Operating System: Linux Architecture: x86
Type of failure: Incorrect result at runtime Difficulty:
Test Case: Blocked By:
Blocking: Related Tickets:

Description

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

Change History (1)

comment:1 Changed 4 years ago by simonmar

  • Resolution set to duplicate
  • Status changed from new to closed

Thanks for the detailed report. This is another instance of #4038, and as its a serious regression we'll be putting out another release shortly. Sorry for the inconvenience.

Note: See TracTickets for help on using tickets.