Skip to content

FFI stubs cpd w/out -DSTOLEN-X86-REGS=?

Symptom: 

Callbacks with more than one parameter exported using
"foreign export dynamic" cause a segmentation fault. 

This fault was first detected when using the OpenGL
binding in hslibs with the supplied redbook examples.

Version Information:

OS: Linux, RedHat 7.0
GCC: 2.96 (rpm gcc-2.96-69)
CLIB: glibc 2.2 (rpm glibc-2.2-12)
GHC: 5.00 (from rpm ghc-5.00-2)

Example: (compiled / linked using flags "-fglasgow-exts
-package lang")

--- Main.hsc (preprocessed to Main.hs using hsc2hs) ---

#include <test.h>

module Main where

import Int
import Addr
import Foreign

main :: IO ()
main = do
  testPrintAddr <- mkTestPrint testPrint
  test testPrintAddr
  freeHaskellFunctionPtr testPrintAddr  

foreign import ccall "test_main" test :: Addr -> IO ()

type CBType = #{type int} -> #{type int} -> IO ()

testPrint :: CBType
testPrint i j = putStrLn ("Hello number " ++ (show i)
++ " and " ++ (show j))

foreign export dynamic mkTestPrint :: CBType -> IO Addr
  

--- test.c ---

#include <stdio.h>

void test_main(void *testPrint(int,int)) 
{
  testPrint(1,2);
}


--- test.h ---

void test_main(void *testPrint(int,int));

--- Cut here ---

Fix: It appears that the new ghc driver does not pass
the flag -DSTOLEN_X86_REGS=num to gcc when compiling
FFI stubs. In the case above, adding the flag
"-optc-DSTOLEN_X86_REGS=4" when compiling Main.hs
causes Main_stub.c to be compiled correctly and fixes
the segmentation fault.


Regards

Dominic Verity

P.S. It would also be nice if the stub generator were
to add newlines at the end of stubs and their headers,
to avoid unsightly "warning: no newline at end of file"
massages from gcc.
Trac metadata
Trac field Value
Version 5.0
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution ResolvedFixed
Component Driver
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information