Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#612 Segfault when running CFFI

segfault
open
nobody
ffi (23)
1
2011-09-28
2011-09-27
Chenguang Wang
No

Honestly I don't know if it's a bug of CFFI, so I also reported this problem to the CFFI developing group.
This program works perfectly under SBCL. But it seems that both clisp 2.48(gentoo ebuild) and 2.49(clean version) do not work.

I built a clean clisp with `./configure --cbc build`.
###############################################
wecing@D5 ~/source/clisp-2.49 $ ./build/clisp --version
GNU CLISP 2.49 (2010-07-07) (built 3526138098) (memory 3526138232)
Software: GNU C 4.4.5
gcc -g -O2 -W -Wswitch -Wcomment -Wpointer-arith -Wimplicit -Wreturn-type -Wmissing-declarations -Wno-sign-compare -Wno-format-nonliteral -O -falign-functions=4 -DENABLE_UNICODE -DDYNAMIC_FFI -DDYNAMIC_MODULES -I. -lreadline -lncurses -ldl -lavcall -lcallback -lsigsegv libgnu_cl.a
SAFETY=0 TYPECODES WIDE_HARD GENERATIONAL_GC SPVW_BLOCKS SPVW_MIXED TRIVIALMAP_MEMORY
libsigsegv 2.8
libreadline 6.1
Features:
(READLINE REGEXP SYSCALLS I18N LOOP COMPILER CLOS MOP CLISP ANSI-CL COMMON-LISP
LISP=CL INTERPRETER SOCKETS GENERIC-STREAMS LOGICAL-PATHNAMES SCREEN FFI
GETTEXT UNICODE BASE-CHAR=CHARACTER WORD-SIZE=64 PC386 UNIX)
C Modules: (clisp i18n syscalls regexp readline)
Installation directory: /home/wecing/source/clisp-2.49/build/
User language: ENGLISH
Machine: X86_64 (X86_64) D5
###############################################
wecing@D5 ~/source/clisp-2.49 $ gdb ./build/clisp
GNU gdb (Gentoo 7.2 p1) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.gentoo.org/>...
Reading symbols from /home/wecing/source/clisp-2.49/build/clisp...done.
(gdb) run /home/wecing/program/cffi/h2.lisp
Starting program: /home/wecing/source/clisp-2.49/build/clisp /home/wecing/program/cffi/h2.lisp
process 3271 is executing new program: /home/wecing/source/clisp-2.49/build/base/lisp.run

Program received signal SIGSEGV, Segmentation fault.
0x00000000004853a7 in closed_buffered (stream=<value optimized out>)
at ../src/stream.d:8321
8321 BufferedStream_channel(stream) = NIL; /* Handle becomes invalid */
(gdb) bt
#0 0x00000000004853a7 in closed_buffered (stream=<value optimized out>)
at ../src/stream.d:8321
#1 0x000000000048556d in closed_all_files () at ../src/stream.d:16262
#2 0x000000000044c646 in loadmem_from_handle (handle=5578144,
filename=<value optimized out>) at ../src/spvw_memfile.d:1748
#3 0x000000000044c920 in loadmem (
filename=0x7fffffffe1eb "/home/wecing/source/clisp-2.49/build/base/lispinit.mem") at ../src/spvw_memfile.d:977
#4 0x000000000044cdf2 in init_memory (p=0x8603c0) at ../src/spvw.d:3267
#5 0x000000000044e047 in main (argc=<value optimized out>,
argv=<value optimized out>) at ../src/spvw.d:3721
(gdb)
###############################################
And the source files:
h2.lisp:
(load "/home/wecing/source/asdf/asdf.lisp")
(push "/home/wecing/.asdf-registry/" asdf:*central-registry*)

(asdf:oos 'asdf:load-op :cffi)

(pushnew "/home/wecing/program/cffi/" cffi:*foreign-library-directories*)
(cffi:define-foreign-library libhello
(:unix "libtest.so"))
(cffi:use-foreign-library libhello)

(cffi:defcfun ("hello" hello) :void)
(cffi:defcfun ("echo" echo) :void
(str :string))
(cffi:defcfun ("putnum" putnum) :void
(n :int))

(hello)
(putnum 2)
(echo "Hello?")
###############################################
test.c:
#include <stdio.h>

void hello(void)
{
printf("Hello, world!\n");
}

void putnum(int n)
{
printf("%d\n", n);
}

void echo(const char *str)
{
printf("%s\n", str);
}
###############################################
And the output without debugging:

wecing@D5 ~/source/clisp-2.49 $ ./build/clisp ~/program/cffi/h2.lisp
0 errors, 0 warningsHello, world!
2

*** - handle_fault error2 ! address = 0x2fc not in [0x333ab9000,0x333d59c28) !
SIGSEGV cannot be cured. Fault address = 0x2fc.
GC count: 39
Space collected by GC: 42003288
Run time: 0 970000
Real time: 1 602938
GC time: 0 120000
Permanently allocated: 164640 bytes.
Currently in use: 6333328 bytes.
Free space: 833598 bytes.
Segmentation fault
###############################################
I also tried it with SBCL:

wecing@D5 ~/source/clisp-2.49 $ sbcl --load ~/program/cffi/h2.lisp
This is SBCL 1.0.19-gentoo, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses. See the CREDITS and COPYING files in the
distribution for more information.
; loading system definition from
; /usr/share/common-lisp/systems/asdf-binary-locations.asd into
; #<PACKAGE "ASDF0">
; registering #<SYSTEM ASDF-BINARY-LOCATIONS {1002B425E1}> as
; ASDF-BINARY-LOCATIONS
STYLE-WARNING: redefining REMOVE-KEYS in DEFUN
STYLE-WARNING: redefining REMOVE-KEYWORD in DEFUN
; in: LAMBDA NIL
; '(IGNORE-ERRORS (TRUENAME ASDF::P))
;
; note: deleting unreachable code
;
; compilation unit finished
; printed 1 note
STYLE-WARNING: redefining RESOLVE-SYMLINKS in DEFUN
STYLE-WARNING: redefining INPUT-FILES in DEFGENERIC
STYLE-WARNING: redefining COMPONENT-SYSTEM in DEFGENERIC
STYLE-WARNING: redefining COMPONENT-PATHNAME in DEFGENERIC
STYLE-WARNING: redefining COMPONENT-PROPERTY in DEFGENERIC
STYLE-WARNING: redefining (SETF COMPONENT-PROPERTY) in DEFGENERIC
STYLE-WARNING: redefining VERSION-SATISFIES in DEFGENERIC
STYLE-WARNING: redefining OPERATION-ANCESTOR in DEFGENERIC
STYLE-WARNING: redefining COMPONENT-VISITED-P in DEFGENERIC
STYLE-WARNING: redefining VISIT-COMPONENT in DEFGENERIC
STYLE-WARNING: redefining (SETF VISITING-COMPONENT) in DEFGENERIC
STYLE-WARNING: redefining COMPONENT-VISITING-P in DEFGENERIC
STYLE-WARNING: redefining COMPONENT-DEPENDS-ON in DEFGENERIC
STYLE-WARNING: redefining COMPONENT-SELF-DEPENDENCIES in DEFGENERIC
STYLE-WARNING: redefining TRAVERSE in DEFGENERIC
STYLE-WARNING:
redefining PRINT-OBJECT (#<STANDARD-CLASS COMPONENT>
#<BUILT-IN-CLASS T>) in DEFMETHOD
STYLE-WARNING:
redefining PRINT-OBJECT (#<SB-PCL::CONDITION-CLASS MISSING-DEPENDENCY>
#<BUILT-IN-CLASS T>) in DEFMETHOD
STYLE-WARNING: redefining SYSDEF-ERROR in DEFUN
STYLE-WARNING:
redefining PRINT-OBJECT (#<SB-PCL::CONDITION-CLASS MISSING-COMPONENT>
#<BUILT-IN-CLASS T>) in DEFMETHOD
STYLE-WARNING:
redefining COMPONENT-SYSTEM (#<STANDARD-CLASS COMPONENT>) in DEFMETHOD
STYLE-WARNING: redefining COMPONENT-PARENT-PATHNAME in DEFUN
STYLE-WARNING:
redefining COMPONENT-PATHNAME (#<STANDARD-CLASS COMPONENT>) in DEFMETHOD
STYLE-WARNING:
redefining COMPONENT-PROPERTY (#<STANDARD-CLASS COMPONENT>
#<BUILT-IN-CLASS T>) in DEFMETHOD
STYLE-WARNING:
redefining (SETF COMPONENT-PROPERTY) (#<BUILT-IN-CLASS T>
#<STANDARD-CLASS COMPONENT>
#<BUILT-IN-CLASS T>) in DEFMETHOD
STYLE-WARNING:
redefining VERSION-SATISFIES (#<STANDARD-CLASS COMPONENT>
#<BUILT-IN-CLASS T>) in DEFMETHOD
STYLE-WARNING: redefining COERCE-NAME in DEFUN
STYLE-WARNING: redefining SYSTEM-REGISTERED-P in DEFUN
STYLE-WARNING: redefining REGISTER-SYSTEM in DEFUN
STYLE-WARNING: redefining SYSDEF-CENTRAL-REGISTRY-SEARCH in DEFUN
STYLE-WARNING: redefining MAKE-TEMPORARY-PACKAGE in DEFUN
STYLE-WARNING:
redefining PRINT-OBJECT (#<STANDARD-CLASS OPERATION>
#<BUILT-IN-CLASS T>) in DEFMETHOD
STYLE-WARNING:
redefining SHARED-INITIALIZE :AFTER (#<STANDARD-CLASS OPERATION>
#<BUILT-IN-CLASS T>) in DEFMETHOD
STYLE-WARNING: redefining NODE-FOR in DEFUN
STYLE-WARNING:
redefining OPERATION-ANCESTOR (#<STANDARD-CLASS OPERATION>) in DEFMETHOD
STYLE-WARNING: redefining MAKE-SUB-OPERATION in DEFUN
STYLE-WARNING:
redefining VISIT-COMPONENT (#<STANDARD-CLASS OPERATION>
#<STANDARD-CLASS COMPONENT>
#<BUILT-IN-CLASS T>) in DEFMETHOD
STYLE-WARNING:
redefining COMPONENT-VISITED-P (#<STANDARD-CLASS OPERATION>
#<STANDARD-CLASS COMPONENT>) in DEFMETHOD
STYLE-WARNING:
redefining (SETF VISITING-COMPONENT) (#<BUILT-IN-CLASS T>
#<BUILT-IN-CLASS T>
#<BUILT-IN-CLASS T>) in DEFMETHOD
STYLE-WARNING:
redefining (SETF VISITING-COMPONENT) (#<BUILT-IN-CLASS T>
#<STANDARD-CLASS OPERATION>
#<STANDARD-CLASS COMPONENT>) in DEFMETHOD
STYLE-WARNING:
redefining COMPONENT-VISITING-P (#<STANDARD-CLASS OPERATION>
#<STANDARD-CLASS COMPONENT>) in DEFMETHOD
STYLE-WARNING:
redefining COMPONENT-DEPENDS-ON (#<BUILT-IN-CLASS SYMBOL>
#<STANDARD-CLASS COMPONENT>) in DEFMETHOD
STYLE-WARNING:
redefining COMPONENT-DEPENDS-ON (#<STANDARD-CLASS OPERATION>
#<STANDARD-CLASS COMPONENT>) in DEFMETHOD
STYLE-WARNING:
redefining COMPONENT-SELF-DEPENDENCIES (#<STANDARD-CLASS OPERATION>
#<STANDARD-CLASS COMPONENT>) in DEFMETHOD
STYLE-WARNING:
redefining INPUT-FILES (#<STANDARD-CLASS OPERATION>
#<STANDARD-CLASS COMPONENT>) in DEFMETHOD
STYLE-WARNING:
redefining INPUT-FILES (#<STANDARD-CLASS OPERATION>
#<STANDARD-CLASS MODULE>) in DEFMETHOD
STYLE-WARNING:
redefining TRAVERSE (#<STANDARD-CLASS OPERATION>
#<STANDARD-CLASS COMPONENT>) in DEFMETHOD
STYLE-WARNING:
redefining INPUT-FILES (#<STANDARD-CLASS COMPILE-OP>
#<STANDARD-CLASS STATIC-FILE>) in DEFMETHOD
STYLE-WARNING:
redefining COMPONENT-DEPENDS-ON (#<STANDARD-CLASS LOAD-OP>
#<STANDARD-CLASS COMPONENT>) in DEFMETHOD
STYLE-WARNING:
redefining COMPONENT-DEPENDS-ON (#<STANDARD-CLASS LOAD-SOURCE-OP>
#<STANDARD-CLASS COMPONENT>) in DEFMETHOD
STYLE-WARNING: redefining OOS in DEFUN
STYLE-WARNING: redefining CLASS-FOR-TYPE in DEFUN
STYLE-WARNING: redefining MAYBE-ADD-TREE in DEFUN
STYLE-WARNING: redefining UNION-OF-DEPENDENCIES in DEFUN
STYLE-WARNING: redefining SYSDEF-ERROR-COMPONENT in DEFUN
STYLE-WARNING: redefining CHECK-COMPONENT-INPUT in DEFUN
STYLE-WARNING: redefining %REMOVE-COMPONENT-INLINE-METHODS in DEFUN
STYLE-WARNING: redefining PARSE-COMPONENT-FORM in DEFUN
STYLE-WARNING: redefining RUN-SHELL-COMMAND in DEFUN
STYLE-WARNING: redefining SYSTEM-DEFINITION-PATHNAME in DEFUN
STYLE-WARNING: redefining SYSTEM-SOURCE-DIRECTORY in DEFUN
STYLE-WARNING: redefining MODULE-PROVIDE-ASDF in DEFUN
Hello, world!
2
Hello?
*

###############################################
Though came up with a lot of warnings, it did not raise segfault.
h2.lisp is also attached with this report.

Discussion

  • Chenguang Wang
    Chenguang Wang
    2011-09-27

    the lisp code.

     
    Attachments
  • Chenguang Wang
    Chenguang Wang
    2011-09-27

    • labels: --> ffi
    • milestone: --> segfault
     
  • Chenguang Wang
    Chenguang Wang
    2011-09-27

     
    Attachments
  • Chenguang Wang
    Chenguang Wang
    2011-09-27

    Oh, forgot to mention that I'm running a 64 bit linux.

     
  • Chenguang Wang
    Chenguang Wang
    2011-09-28

    Today I tried with clisp's native ffi, and it works well:

    (ffi:default-foreign-language :stdc)
    (ffi:def-call-out hello
    (:library "libtest.so")
    (:language :stdc)
    (:name "hello")
    (:arguments)
    (:return-type NIL))

    (ffi:def-call-out echo
    (:library "libtest.so")
    (:language :stdc)
    (:name "echo")
    (:arguments (str ffi:c-string))
    (:return-type NIL))

    (hello)
    (echo "Hello?")

    So it seems that this problem is indeedly caused by CFFI.

     
  • Chenguang Wang
    Chenguang Wang
    2011-09-28

    • priority: 5 --> 1