3 # tests related to foreign function interface and loading of shared
6 # This software is part of the SBCL system. See the README file for
9 # While most of SBCL is derived from the CMU CL system, the test
10 # files (like this one) were written from scratch after the fork
13 # This software is in the public domain and is provided with
14 # absolutely no warranty. See the COPYING and CREDITS files for
17 echo //entering foreign.test.sh
19 # simple way to make sure we're not punting by accident:
20 # setting PUNT to anything other than 104 will make non-dlopen
21 # and non-linkage-table platforms fail this
24 testfilestem=${TMPDIR:-/tmp}/sbcl-foreign-test-$$
26 # Make a little shared object file to test with.
27 echo 'int summish(int x, int y) { return 1 + x + y; }' > $testfilestem.c
28 echo 'int numberish = 42;' >> $testfilestem.c
29 echo 'int nummish(int x) { return numberish + x; }' >> $testfilestem.c
30 cc -c $testfilestem.c -o $testfilestem.o
31 ld -shared -o $testfilestem.so $testfilestem.o
33 # Foreign definitions & load
34 cat > $testfilestem.def.lisp <<EOF
35 (define-alien-variable environ (* c-string))
36 (defvar *environ* environ)
37 (eval-when (:compile-toplevel :load-toplevel :execute)
39 (load-shared-object "$testfilestem.so")
40 (sb-int:unsupported-operator ()
41 ;; At least as of sbcl-0.7.0.5, LOAD-SHARED-OBJECT isn't
42 ;; supported on every OS. In that case, there's nothing to test,
43 ;; and we can just fall through to success.
44 (sb-ext:quit :unix-status 22)))) ; catch that
45 (define-alien-routine summish int (x int) (y int))
46 (define-alien-variable numberish int)
47 (define-alien-routine nummish int (x int))
49 ;; Test that loading an object file didn't screw up our records
50 ;; of variables visible in runtime. (This was a bug until
51 ;; Nikodemus Siivola's patch in sbcl-0.8.5.50.)
53 ;; This cannot be tested in a saved core, as there is no guarantee
54 ;; that the location will be the same.
55 (assert (= (sb-sys:sap-int (alien-sap *environ*))
56 (sb-sys:sap-int (alien-sap environ))))
60 cat > $testfilestem.test.lisp <<EOF
61 (assert (= (summish 10 20) 31))
62 (assert (= 42 numberish))
64 (assert (= 13 numberish))
65 (assert (= 14 (nummish 1)))
66 (sb-ext:quit :unix-status 52) ; success convention for Lisp program
69 ${SBCL:-sbcl} --eval "(progn (compile-file #p\"$testfilestem.def.lisp\") (sb-ext:quit :unix-status 52))"
70 if [ $? = 52 ] ; then :
72 # we can't compile the test file. something's wrong.
78 ${SBCL:-sbcl} --load $testfilestem.def.fasl --load $testfilestem.test.lisp
80 if [ $RET = 22 ]; then
82 exit $PUNT # success -- load-shared-object not supported
83 elif [ $RET != 52 ]; then
89 ${SBCL:-sbcl} --load $testfilestem.def.fasl --eval "(when (member :linkage-table *features*) (save-lisp-and-die \"$testfilestem.core\"))" <<EOF
90 (sb-ext:quit :unix-status 22) ; catch this
94 exit $PUNT # success -- linkage-table not available
97 $SBCL_ALLOWING_CORE --core $testfilestem.core --sysinit /dev/null --userinit /dev/null --load $testfilestem.test.lisp
106 # success convention for script