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.
30 if [ $(uname -p) = x86_64 ]; then
31 CFLAGS="$CFLAGS -fPIC"
33 if [ $(uname) = Darwin ]; then
38 cc -c $1.c -o $1.o $CFLAGS
39 ld $SO_FLAGS -o $1.so $1.o
42 echo 'int summish(int x, int y) { return 1 + x + y; }' > $testfilestem.c
43 echo 'int numberish = 42;' >> $testfilestem.c
44 echo 'int nummish(int x) { return numberish + x; }' >> $testfilestem.c
45 echo 'short negative_short() { return -1; }' >> $testfilestem.c
46 echo 'int negative_int() { return -2; }' >> $testfilestem.c
47 echo 'long negative_long() { return -3; }' >> $testfilestem.c
48 build_so $testfilestem
50 echo 'int foo = 13;' > $testfilestem-b.c
51 echo 'int bar() { return 42; }' >> $testfilestem-b.c
52 build_so $testfilestem-b
54 echo 'int foo = 42;' > $testfilestem-b2.c
55 echo 'int bar() { return 13; }' >> $testfilestem-b2.c
56 build_so $testfilestem-b2
58 echo 'int late_foo = 43;' > $testfilestem-c.c
59 echo 'int late_bar() { return 14; }' >> $testfilestem-c.c
60 build_so $testfilestem-c
62 ## Foreign definitions & load
64 cat > $testfilestem.def.lisp <<EOF
65 (define-alien-variable environ (* c-string))
66 (defvar *environ* environ)
67 (eval-when (:compile-toplevel :load-toplevel :execute)
70 (load-shared-object "$testfilestem.so")
71 (load-shared-object "$testfilestem-b.so"))
72 (sb-int:unsupported-operator ()
73 ;; At least as of sbcl-0.7.0.5, LOAD-SHARED-OBJECT isn't
74 ;; supported on every OS. In that case, there's nothing to test,
75 ;; and we can just fall through to success.
76 (sb-ext:quit :unix-status 22)))) ; catch that
77 (define-alien-routine summish int (x int) (y int))
78 (define-alien-variable numberish int)
79 (define-alien-routine nummish int (x int))
80 (define-alien-variable "foo" int)
81 (define-alien-routine "bar" int)
83 (define-alien-routine "negative_short" short)
84 (define-alien-routine "negative_int" int)
85 (define-alien-routine "negative_long" long)
87 ;; Test that loading an object file didn't screw up our records
88 ;; of variables visible in runtime. (This was a bug until
89 ;; Nikodemus Siivola's patch in sbcl-0.8.5.50.)
91 ;; This cannot be tested in a saved core, as there is no guarantee
92 ;; that the location will be the same.
93 (assert (= (sb-sys:sap-int (alien-sap *environ*))
94 (sb-sys:sap-int (alien-sap environ))))
98 (lambda (condition hook)
99 (print (list :debugger-hook condition))
100 (let ((cont (find-restart 'continue condition)))
102 (invoke-restart cont)))
103 (print :fell-through)
104 (invoke-debugger condition)))
108 cat > $testfilestem.test.lisp <<EOF
109 (assert (= (summish 10 20) 31))
110 (assert (= 42 numberish))
112 (assert (= 13 numberish))
113 (assert (= 14 (nummish 1)))
115 (assert (= -1 (negative-short)))
116 (assert (= -2 (negative-int)))
117 (assert (= -3 (negative-long)))
121 ;; test realoading object file with new definitions
123 (assert (= 42 (bar)))
124 (rename-file "$testfilestem-b.so" "$testfilestem-b.bak")
125 (rename-file "$testfilestem-b2.so" "$testfilestem-b.so")
126 (load-shared-object "$testfilestem-b.so")
128 (assert (= 13 (bar)))
129 (rename-file "$testfilestem-b.so" "$testfilestem-b2.so")
130 (rename-file "$testfilestem-b.bak" "$testfilestem-b.so")
134 ;; test late resolution
135 (define-alien-variable late-foo int)
136 (define-alien-routine late-bar int)
137 (multiple-value-bind (val err) (ignore-errors late-foo)
139 (assert (typep err 'undefined-alien-error)))
140 (multiple-value-bind (val err) (ignore-errors (late-bar))
142 (assert (typep err 'undefined-alien-error)))
143 (load-shared-object "$testfilestem-c.so")
144 (assert (= 43 late-foo))
145 (assert (= 14 (late-bar)))
149 (sb-ext:quit :unix-status 52) ; success convention for Lisp program
152 ${SBCL:-sbcl} --eval "(progn (compile-file #p\"$testfilestem.def.lisp\") (sb-ext:quit :unix-status 52))"
153 if [ $? = 52 ] ; then :
155 # we can't compile the test file. something's wrong.
163 ${SBCL:-sbcl} --load $testfilestem.def.fasl --load $testfilestem.test.lisp
165 if [ $RET = 22 ]; then
167 exit $PUNT # success -- load-shared-object not supported
168 elif [ $RET != 52 ]; then
176 ${SBCL:-sbcl} --load $testfilestem.def.fasl --eval "(when (member :linkage-table *features*) (save-lisp-and-die \"$testfilestem.core\"))" <<EOF
177 (sb-ext:quit :unix-status 22) ; catch this
181 exit $PUNT # success -- linkage-table not available
186 $SBCL_ALLOWING_CORE --core $testfilestem.core --sysinit /dev/null --userinit /dev/null --load $testfilestem.test.lisp
187 if [ $? != 52 ]; then
195 # missing object file
196 rm $testfilestem-b.so $testfilestem-b2.so
197 $SBCL_ALLOWING_CORE --core $testfilestem.core --sysinit /dev/null --userinit /dev/null <<EOF
198 (assert (= 22 (summish 10 11)))
199 (multiple-value-bind (val err) (ignore-errors (eval 'foo))
201 (assert (typep err 'undefined-alien-error)))
202 (multiple-value-bind (val err) (ignore-errors (eval '(bar)))
204 (assert (typep err 'undefined-alien-error)))
205 (quit :unix-status 52)
207 if [ $? != 52 ]; then
217 # success convention for script