[Haskell-cafe] FFI and LD_PRELOAD -> segfault

Anatoly Zaretsky anatoly.zaretsky at gmail.com
Sun Sep 24 13:20:55 EDT 2006


On 9/23/06, Stephan Walter <news at stephan.walter.name> wrote:
> I'm trying to use a shared lib written in Haskell to overload C functions
> via LD_PRELOAD.
[snip]
> This aborts with a segfault in scheduleWaitThread() from ./libtestffi.so
>
> The test program doesn't use threads, so I'm wondering what I did wrong?

>From http://www.haskell.org/ghc/docs/latest/html/users_guide/sec-ffi-ghc.html#using-own-main:
  The call to hs_init()  initializes GHC's runtime system. Do NOT try to invoke
  any Haskell functions before calling hs_init(): strange things will
undoubtedly happen.

If you are using gcc you can add something like this when linking libtestffi.so:

#include <HsFFI.h>

extern void __stginit_Socks(void);

static void __attribute__ ((constructor)) my_init(void) {
  int argc = 1;
  char *argv[] = {"Haskell shared object"};
  char **argvp = argv;
  hs_init(&argc, &argvp);
  hs_add_root(__stginit_Socks);
}

static void __attribute__ ((destructor)) my_fini(void) {
  hs_exit();
}

--
Tolik


More information about the Haskell-Cafe mailing list