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 files to test with.
28 echo 'int summish(int x, int y) { return 1 + x + y; }' > $testfilestem.c
29 echo 'int numberish = 42;' >> $testfilestem.c
30 echo 'int nummish(int x) { return numberish + x; }' >> $testfilestem.c
31 cc -c $testfilestem.c -o $testfilestem.o
32 ld -shared -o $testfilestem.so $testfilestem.o
34 echo 'int foo = 13;' > $testfilestem-foobar.c
35 echo 'int bar() { return 42; }' >> $testfilestem-foobar.c
36 cc -c $testfilestem-foobar.c -o $testfilestem-foobar.o
37 ld -shared -o $testfilestem-foobar.so $testfilestem-foobar.o
39 echo 'int foo = 42;' > $testfilestem-foobar2.c
40 echo 'int bar() { return 13; }' >> $testfilestem-foobar2.c
41 cc -c $testfilestem-foobar2.c -o $testfilestem-foobar2.o
42 ld -shared -o $testfilestem-foobar2.so $testfilestem-foobar2.o
44 ## Foreign definitions & load
46 cat > $testfilestem.def.lisp <<EOF
47 (define-alien-variable environ (* c-string))
48 (defvar *environ* environ)
49 (eval-when (:compile-toplevel :load-toplevel :execute)
52 (load-shared-object "$testfilestem.so")
53 (load-shared-object "$testfilestem-foobar.so"))
54 (sb-int:unsupported-operator ()
55 ;; At least as of sbcl-0.7.0.5, LOAD-SHARED-OBJECT isn't
56 ;; supported on every OS. In that case, there's nothing to test,
57 ;; and we can just fall through to success.
58 (sb-ext:quit :unix-status 22)))) ; catch that
59 (define-alien-routine summish int (x int) (y int))
60 (define-alien-variable numberish int)
61 (define-alien-routine nummish int (x int))
62 (define-alien-variable "foo" int)
63 (define-alien-routine "bar" int)
65 ;; Test that loading an object file didn't screw up our records
66 ;; of variables visible in runtime. (This was a bug until
67 ;; Nikodemus Siivola's patch in sbcl-0.8.5.50.)
69 ;; This cannot be tested in a saved core, as there is no guarantee
70 ;; that the location will be the same.
71 (assert (= (sb-sys:sap-int (alien-sap *environ*))
72 (sb-sys:sap-int (alien-sap environ))))
76 cat > $testfilestem.test.lisp <<EOF
77 (assert (= (summish 10 20) 31))
78 (assert (= 42 numberish))
80 (assert (= 13 numberish))
81 (assert (= 14 (nummish 1)))
85 ;; test realoading object file with new definitions
86 (rename-file "$testfilestem-foobar.so" "$testfilestem-foobar.bak")
87 (rename-file "$testfilestem-foobar2.so" "$testfilestem-foobar.so")
88 (load-shared-object "$testfilestem-foobar.so")
91 (rename-file "$testfilestem-foobar.so" "$testfilestem-foobar2.so")
92 (rename-file "$testfilestem-foobar.bak" "$testfilestem-foobar.so")
94 (sb-ext:quit :unix-status 52) ; success convention for Lisp program
97 ${SBCL:-sbcl} --eval "(progn (compile-file #p\"$testfilestem.def.lisp\") (sb-ext:quit :unix-status 52))"
98 if [ $? = 52 ] ; then :
100 # we can't compile the test file. something's wrong.
106 ${SBCL:-sbcl} --load $testfilestem.def.fasl --load $testfilestem.test.lisp
108 if [ $RET = 22 ]; then
110 exit $PUNT # success -- load-shared-object not supported
111 elif [ $RET != 52 ]; then
117 ${SBCL:-sbcl} --load $testfilestem.def.fasl --eval "(when (member :linkage-table *features*) (save-lisp-and-die \"$testfilestem.core\"))" <<EOF
118 (sb-ext:quit :unix-status 22) ; catch this
122 exit $PUNT # success -- linkage-table not available
125 $SBCL_ALLOWING_CORE --core $testfilestem.core --sysinit /dev/null --userinit /dev/null --load $testfilestem.test.lisp
126 if [ $? != 52 ]; then
134 # success convention for script