FFI program seg faulting

Ben Gaster brg at clearspeed.com
Fri Sep 21 08:21:54 EDT 2007



I am trying to build a very simple Haskell function (foo) which I can

call from C, using an example I got from the GHC FFI Wiki. The problem
is that

the resulting program fails to run with a seg fault. 


Here is the code:


---Haskell Module start (Foo.hs)

module Foo where


foreign export ccall foo :: Int -> IO Int


foo :: Int -> IO Int

foo n = return (length (f n))


f :: Int -> [Int]

f 0 = []

f n = n:(f (n-1))

---Haskell Module end


/*- C module start foo.c*/

#include <stdio.h>


#include "Foo_stub.h"


int main(void)


  int x = foo(100);


  //printf("x = %d\n", x);


  return 0;


/*- C module end */


The I build the a.out with the commands:


ghc -fglasgow-exts -fffi -c Foo.hs

ghc -fglasgow-exts -fffi Foo.o Foo_stub.o foo.c


Now if I run the resulting a.out then I get:


Segmentation fault


Quickly putting this through GDB I get:


gdb ./a.out 

GNU gdb Red Hat Linux (6.5-16.el5rh)

Copyright (C) 2006 Free Software Foundation, Inc.

GDB is free software, covered by the GNU General Public License, and you

welcome to change it and/or distribute copies of it under certain

Type "show copying" to see the conditions.

There is absolutely no warranty for GDB.  Type "show warranty" for

This GDB was configured as "i386-redhat-linux-gnu"...

(no debugging symbols found)

Using host libthread_db library "/lib/libthread_db.so.1".



Starting program: /home/brg/dev/haskell/ffi/a.out 

(no debugging symbols found)

(no debugging symbols found)

(no debugging symbols found)

[Thread debugging using libthread_db enabled]

[New Thread 7870144 (LWP 22448)]


Program received signal SIGSEGV, Segmentation fault.

[Switching to Thread 7870144 (LWP 22448)]

0x0805c313 in allocateLocal ()

(gdb) where

#0  0x0805c313 in allocateLocal ()

#1  0x0000005c in ?? ()

#2  0x08075c97 in base_DataziTypeable_zdLr42klvl4_srt ()

#3  0x4ee1dca0 in _dl_argv_internal () from /lib/ld-linux.so.2

#4  0x0807c920 in last_free_capability ()

#5  0x4ee1dca0 in _dl_argv_internal () from /lib/ld-linux.so.2

#6  0x00000000 in ?? ()


I am using GHC 6.6.1 built from sources on RHE5.


Many regards,




Benedict R. Gaster
S/W Architecture Team Lead
ClearSpeed Technology Plc
3110 Great Western Court 
Hunts Ground Road
Stoke Gifford 
BS34 8HP

Phone: <blocked::mailto:karen at clearspeed.com>  + 44 117 3172016
Fax: + 44 117 3172002
brg at clearspeed.com <mailto:brg at clearspeed.com> 


The contents of this email and any attachments are confidential and may be legally privileged.  If you have received this email in error please notify the sender immediately and refrain from copying or disclosing the contents of the email to any third party.  ClearSpeed accepts no liability for any viruses which may be transmitted by this email or its attachments.

ClearSpeed Technology PLC is a company registered in England under company number 05159262 whose registered office is at 3110 Great Western Court, Hunts Ground Road, Bristol BS34 8HP, UK. 
ClearSpeed Technology INC is a wholly owned subsidiary of ClearSpeed Technology PLC and is incorporated in the United States of America, with its principle place of business at 3031 Tisch Way, Suite 200, San Jose, CA 95128, US. 

Additional Company information can be found at the following: http://www.clearspeed.com/aboutus/company/index.html

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/glasgow-haskell-users/attachments/20070921/b090ecd2/attachment-0001.htm

More information about the Glasgow-haskell-users mailing list