[Ecls-list] Possible bug: segfault when setting slots in CFFI

Waldek Hebisch hebisch at math.uni.wroc.pl
Sat Jun 21 00:47:36 UTC 2014

Andrew Lyon wrote:
> Hey everyone (again). Since my last email, I found what appears to be a
> weird CFFI/ECL bug. This was the cause of the problem I reported earlier
> today. It seems like the code in the function runs fine, but on return it
> barfs a segfault:
> https://gist.github.com/orthecreedence/15dea89a0a7d591bba51
> Note that I ended up working around this problem using ffi:c-inline to set
> the values in the struct manually, but I consider this an ugly fix that I'd
> rather not have there.
> Any ideas on what's wrong? Thanks!

Probably not the cause of your problem, but AFAICS you have wrong types
in foreign calls: 'memset' needs 'size_t' as third (last) argument
and you pass ':unsigned-char'.  Similarly 'htons' has unsigned short
as argument. 'htonl' need 32-bit unsigned integer as argument
and returns 32-bit unsigned integer.  On Windows 'long' gives
right size, but on other systems long may be too big (long typically
has 64-bits on non-Windows 64-bit systems).

My method to debug such problem is to put Lisp source in a file and
use :c-file :h-file :data-file keyword arguments to 'compile-file'
to capture resulting C code.  Then I look at C code to check if
it is right -- that should localize the problem to specific plase.
Once you know problem spot you may trace what 'cffi:with-foreign-slots'
is doing and what wnet wrong.

Note: native ecl FFI is UFFI and it allows you to assign to foreign
data.  So you may manually assign slots at UFFI level.  I did not
use CFFI but probably its constructs are translated (macro-expanded)
to native (UFFI for ecl) constructs.  In such case looking at UFFI
translation you should be able to determine if ecl has bug in its
implementation of UFFI or bug is in CFFI.

                              Waldek Hebisch
hebisch at math.uni.wroc.pl 

More information about the ecl-devel mailing list