Opened 15 years ago

Closed 15 years ago

Last modified 46 years ago

#541 closed bug (Fixed)

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

Reported by: nobody Owned by: simonmar
Priority: normal Milestone:
Component: Driver Version: 5.0
Keywords: Cc:
Operating System: Architecture:
Type of failure: Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:



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)) 

--- 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.


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.

Change History (3)

comment:1 Changed 15 years ago by simonmar

Logged In: YES 

I can't repro this one, but I think I know what's causing 

Could you verify that compiling the stub with -
DIN_STG_CODE=0 also makes the example work?

comment:2 Changed 15 years ago by dominicv

Logged In: YES 

Yup, -DIN_STG_CODE=0 also does the trick.

I've also had problems reproducing this wrinkle on other
platforms -
both of my i686 machines running RedHat 7.0 suffer, but it
seems to be fine on my NT box.

I'm also having problems with foreign imports, which again
show up in the OpenGL binding. Specifically, I can now get
all but one of the supplied examples to run OK. However, in
the AAPoly.hs example when it calls out to the 4 parameter
GL function "drawElements "  it appears to be passing
corrupted parameters. As far as I can tell everything is
marshalled into C side stuff OK, its just the foreign call
that is

Again this is specific to my Linux boxes not NT - could the
problems be related?

Regards, Dominic

comment:3 Changed 15 years ago by simonmar

  • Status changed from assigned to closed
Logged In: YES 

Fixed; thanks.

The problems with reproducing it are almost certainly down 
to the particular version of gcc being used.
Note: See TracTickets for help on using tickets.