Search code examples
haskellshared-librariesdynamic-linkingffihaskell-ffi

How do I use a FunPtr from Haskell?


Assuming the following files:

test2.h:

typedef int (*signature) ();

extern const signature lol2;

test2.c:

#include "test2.h"

int lol() {
  return 42;
}

const signature lol2 = lol;

Test2.hs:

module Main where

import Foreign.C
import Foreign.Ptr

type Fun =
  IO CInt

foreign import ccall
  "test2.h lol2"
  fun_ptr
  :: FunPtr Fun

foreign import ccall "dynamic" mkFun :: FunPtr Fun -> Fun

lol = mkFun fun_ptr

main = do
  fortytwo <- lol
  putStrLn $ show $ fortytwo

With the following compilation:

gcc -shared test2.c -Wall -Wextra -o libtest2.so -g3 -ggdb3
ghc -o test2 Test2.hs -ltest2 -optl-Wl,-rpath,. -L. -g

(GHC emits a warning about a missing "&" before the lol2 declaration, but I think the warning is wrong, so I ignore it. Also, note that I am not using -dynamic. If I do, the results are the same)

But, I get a SIGSEGV while running:

(gdb) break scheduleWaitThread
Breakpoint 1 at 0x468150: file rts/Schedule.c, line 2509.
(gdb) r
Starting program: [...]/test2 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Breakpoint 1, scheduleWaitThread (tso=0x4200105388, ret=ret@entry=0x0, pcap=pcap@entry=0x7fffffffd7f8) at rts/Schedule.c:2509
2509    rts/Schedule.c: No such file or directory.
(gdb) bt
#0  scheduleWaitThread (tso=0x4200105388, ret=ret@entry=0x0, pcap=pcap@entry=0x7fffffffd7f8) at rts/Schedule.c:2509
#1  0x0000000000483864 in rts_evalLazyIO (cap=cap@entry=0x7fffffffd7f8, p=p@entry=0x4a5420, ret=ret@entry=0x0) at rts/RtsAPI.c:530
#2  0x00000000004707ae in hs_main (argc=1, argv=0x7fffffffd9e8, main_closure=0x4a5420, rts_config=...) at rts/RtsMain.c:72
#3  0x0000000000406b46 in main ()
(gdb) finish
Run till exit from #0  scheduleWaitThread (tso=0x4200105388, ret=ret@entry=0x0, pcap=pcap@entry=0x7fffffffd7f8) at rts/Schedule.c:2509

Program received signal SIGSEGV, Segmentation fault.
0x00000000004a4d90 in lol2 ()
(gdb) 

After the crash the stack seems unusable:

(gdb) bt
#0  0x00000000004a4d90 in lol2 ()
#1  0x000000000040669d in r2ad_info ()
#2  0x0000000000000000 in ?? ()

What am I doing wrong? How can I debug this?


Solution

  • ccall can only import functions, but lol2 is no function. Use a capi import with a value qualification:

    {-# LANGUAGE CApiFFI #-}
    module Main where
    -- ... etc ...
    foreign import capi "test2.h value lol2" fun_ptr :: FunPtr Fun
    -- ... etc ...
    

    It's not immediately obvious, but the manual says to do this, and it works. The warning message you've seen still comes up; I think you may want to report that as a bug.