[racket] FFI crashing Racket

From: Evgeny Odegov (evgeny-odegov at mail.ru)
Date: Mon Oct 13 11:16:00 EDT 2014

Some results on my machine (Ubuntu 12.04):

user$ uname -a
Linux home_pc 3.2.0-70-generic #105-Ubuntu SMP Wed Sep 24 19:49:46 UTC 
2014 i686 i686 i386 GNU/Linux


user$ racket -v
Welcome to Racket v6.1.


user$ cat /home/user/clang_ffi_crash.rkt
#lang racket

(require ffi/unsafe
          ffi/unsafe/define)

(define libclang (ffi-lib "libclang.so.1"))

(define-ffi-definer defclang libclang)

(defclang clang_createIndex (_fun _int _int -> _pointer))

(clang_createIndex 0 0)


user$ racket /home/user/clang_ffi_crash.rkt
#<cpointer>


user$ racket -e "(require (file \"/home/user/clang_ffi_crash.rkt\"))"
zsh: abort (core dumped)


user$ gdb -q --args racket -e "(require (file 
\"/home/user/clang_ffi_crash.rkt\"))"
Reading symbols from /usr/bin/racket...(no debugging symbols found)...done.
(gdb) run
Starting program: /usr/bin/racket -e \(require\ \(file\ 
\"/home/user/clang_ffi_crash.rkt\"\)\)
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".
[New Thread 0xb74c3b40 (LWP 27680)]

Program received signal SIGSEGV, Segmentation fault.
0x0828d5f6 in scheme_gmp_tls_unload ()
(gdb) backtrace
#0  0x0828d5f6 in scheme_gmp_tls_unload ()
#1  0x08257af8 in ?? ()
#2  0x082967c2 in ?? ()
#3  0x082971ef in ?? ()
#4  0x0829a9c1 in GC_malloc_atomic ()
#5  0x08075daf in scheme_malloc_fail_ok ()
#6  0x0821c4e4 in scheme_make_sized_offset_byte_string ()
#7  0x0821c5a7 in scheme_make_sized_byte_string ()
#8  0x081cc12d in ?? ()
#9  0x081cc7b5 in ?? ()
#10 0x00336eda in ?? ()
#11 0x0809a630 in scheme_do_eval ()
#12 0x080ad0ca in ?? ()
#13 0x080c1039 in scheme_dynamic_wind ()
#14 0x080c1789 in ?? ()
#15 0x0033f1a0 in ?? ()
#16 0x0809a630 in scheme_do_eval ()
#17 0x080bddc8 in scheme_finish_apply_for_prompt ()
#18 0x080bdfb9 in scheme_apply_for_prompt ()
#19 0x080c382e in ?? ()
#20 0x00330967 in ?? ()
#21 0x00336d34 in ?? ()
#22 0x0809a630 in scheme_do_eval ()
---Type <return> to continue, or q <return> to quit---




12.10.2014 20:03, Matthew Flatt пишет:
> I wasn't able to provoke a crash with your example on my machine, but
> try dropping `_void` from `(_cpointer _void)`.
>
> A type passed to `_cpointer` isn't the type of data that the pointer
> references. It's the representation of the pointer itself (so that you
> can pick NULL and GC handling, for example). I think that `_cpointer`
> should probably have raised on error for a non-pointer type like
> `_void`.
>
> At Fri, 10 Oct 2014 18:38:19 +0200, Adriaan Leijnse wrote:
>> I'm trying to make use of Clang's C-API, but I might be missing
>> something in getting it to work with the FFI. Here's a C-program which
>> runs fine after compilation with "clang -o test test.c -lclang
>> -L/usr/lib/llvm-3.4/lib":
>>
>>
>> #include <stdio.h>
>>
>> typedef void *CXIndex;
>> extern CXIndex clang_createIndex(int, int);
>>
>> int main() {
>>    clang_createIndex(0, 0);
>>    return 0;
>> }
>>
>>
>> However, running the following Racket program results in a crash
>> (probably a segfault but Racket just prints "aborted").
>>
>>
>> #lang racket
>>
>> (require ffi/unsafe
>>           ffi/unsafe/define)
>>
>> (define libclang (ffi-lib "libclang"
>>                            #:global? #t
>>                            #:get-lib-dirs (λ _ '("/usr/lib/llvm-3.4/lib"))))
>>
>> (define-ffi-definer defclang libclang)
>>
>> (define _CXIndex (_cpointer _void))
>>
>> (defclang clang_createIndex
>>    (_fun _int _int -> _CXIndex))
>>
>> (clang_createIndex 0 0)
>>
>>
>> These programs look identical to me and I haven't had problems like
>> this using the FFI before, so I'm at a loss here. Any pointers?
>>
>> ____________________
>>    Racket Users list:
>>    http://lists.racket-lang.org/users
> ____________________
>    Racket Users list:
>    http://lists.racket-lang.org/users


Posted on the users mailing list.