[racket] FFI crashing Racket

From: Adriaan Leijnse (aleijnse at vub.ac.be)
Date: Mon Oct 13 05:28:58 EDT 2014

According to the documentation for `_cpointer`, the first argument is
a type tag and the second argument is the pointer representation
(`_pointer` by default), so that should be fine. Changing the whole
type to `_pointer` doesn't help either.

Curiously it seems to work as far as returning from the
clang_createIndex call, but then Racket crashes anyway after the
program terminates. Commenting out the call to clang_createIndex
results in a consistently crash-free program. Another strange thing is
that adding

    (define-cstruct _AStruct
      ([x _int]
       [y _int]))

before the definition of `clang_createIndex` also results in a clean
run on both machines available to me (Ubuntu 14.04 and Racket 5.3.6
and 6.1). Any cstruct with at least two fields of any type seems to
work.

On Sun, Oct 12, 2014 at 3:03 PM, Matthew Flatt <mflatt at cs.utah.edu> wrote:
> 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


Posted on the users mailing list.