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 some shared object files to test with.
30 if [ "`uname -m`" = x86_64
-o "`uname -m`" = amd64
-o \
31 "`uname -m`" = mips
-o "`uname -m`" = mips64
]; then
32 CFLAGS
="$CFLAGS -fPIC"
34 if [ "`uname`" = Darwin
]; then
39 cc
-c $1.c
-o $1.o
$CFLAGS
40 ld
$SO_FLAGS -o $1.so
$1.o
43 cat > $testfilestem.c
<<EOF
44 int summish(int x, int y) { return 1 + x + y; }
48 int nummish(int x) { return numberish + x; }
50 short negative_short() { return -1; }
51 int negative_int() { return -2; }
52 long negative_long() { return -3; }
54 long long powish(unsigned int x, unsigned int y) {
56 long long xx = (long long) x;
57 for(; y != 1; y /= 2) {
67 float return9th(float f1, float f2, float f3, float f4, float f5,
68 float f6, float f7, float f8, float f9, float f10,
69 float f11, float f12) {
73 double return9thd(double f1, double f2, double f3, double f4, double f5,
74 double f6, double f7, double f8, double f9, double f10,
75 double f11, double f12) {
79 int long_test8(int a1, int a2, int a3, int a4, int a5,
80 int a6, int a7, long long l1) {
81 return (l1 == powish(2,34));
84 int long_test9(int a1, int a2, int a3, int a4, int a5,
85 int a6, int a7, long long l1, int a8) {
86 return (l1 == powish(2,35));
89 int long_test2(int i1, int i2, int i3, int i4, int i5, int i6,
90 int i7, int i8, int i9, long long l1, long long l2) {
91 return (l1 == (1 + powish(2,37)));
94 long long return_long_long() {
99 build_so
$testfilestem
101 echo 'int foo = 13;' > $testfilestem-b.c
102 echo 'int bar() { return 42; }' >> $testfilestem-b.c
103 build_so
$testfilestem-b
105 echo 'int foo = 42;' > $testfilestem-b2.c
106 echo 'int bar() { return 13; }' >> $testfilestem-b2.c
107 build_so
$testfilestem-b2
109 echo 'int late_foo = 43;' > $testfilestem-c.c
110 echo 'int late_bar() { return 14; }' >> $testfilestem-c.c
111 build_so
$testfilestem-c
113 ## Foreign definitions & load
115 cat > $testfilestem.base.lisp
<<EOF
116 (define-alien-variable environ (* c-string))
117 (defvar *environ* environ)
118 (eval-when (:compile-toplevel :load-toplevel :execute)
121 (load-shared-object "$testfilestem.so")
122 (load-shared-object "$testfilestem-b.so"))
123 (sb-int:unsupported-operator ()
124 ;; At least as of sbcl-0.7.0.5, LOAD-SHARED-OBJECT isn't
125 ;; supported on every OS. In that case, there's nothing to test,
126 ;; and we can just fall through to success.
127 (sb-ext:quit :unix-status 22)))) ; catch that
128 (define-alien-routine summish int (x int) (y int))
129 (define-alien-variable numberish int)
130 (define-alien-routine nummish int (x int))
131 (define-alien-variable "foo" int)
132 (define-alien-routine "bar" int)
134 (define-alien-routine "negative_short" short)
135 (define-alien-routine "negative_int" int)
136 (define-alien-routine "negative_long" long)
138 (define-alien-routine return9th float (input1 float) (input2 float) (input3 float) (input4 float) (input5 float) (input6 float) (input7 float) (input8 float) (input9 float) (input10 float) (input11 float) (input12 float))
139 (define-alien-routine return9thd double (f1 double) (f2 double) (f3 double) (f4 double) (f5 double) (f6 double) (f7 double) (f8 double) (f9 double) (f10 double) (f11 double) (f12 double))
141 (define-alien-routine long-test8 int (int1 int) (int2 int) (int3 int) (int4 int) (int5 int) (int6 int) (int7 int) (long1 (integer 64)))
142 (define-alien-routine long-test9 int (int1 int) (int2 int) (int3 int) (int4 int) (int5 int) (int6 int) (int7 int) (long1 (integer 64)) (int8 int))
143 (define-alien-routine long-test2 int (int1 int) (int2 int) (int3 int) (int4 int) (int5 int) (int6 int) (int7 int) (int8 int) (int9 int) (long1 (integer 64)) (long2 (integer 64)))
144 (define-alien-routine return-long-long (integer 64))
146 ;; compiling this gets us the FOP-FOREIGN-DATAREF-FIXUP on
147 ;; linkage-table ports
148 (defvar *extern* (extern-alien "negative_short" short))
150 ;; Test that loading an object file didn't screw up our records
151 ;; of variables visible in runtime. (This was a bug until
152 ;; Nikodemus Siivola's patch in sbcl-0.8.5.50.)
154 ;; This cannot be tested in a saved core, as there is no guarantee
155 ;; that the location will be the same.
156 (assert (= (sb-sys:sap-int (alien-sap *environ*))
157 (sb-sys:sap-int (alien-sap environ))))
159 ;; automagic restarts
160 (setf *invoke-debugger-hook*
161 (lambda (condition hook)
163 (let ((cont (find-restart 'continue condition)))
165 (invoke-restart cont)))
166 (print :fell-through)
167 (invoke-debugger condition)))
170 echo "(declaim (optimize speed))" > $testfilestem.fast.lisp
171 cat $testfilestem.base.lisp
>> $testfilestem.fast.lisp
173 echo "(declaim (optimize space))" > $testfilestem.small.lisp
174 cat $testfilestem.base.lisp
>> $testfilestem.small.lisp
177 cat > $testfilestem.
test.lisp
<<EOF
178 ;; FIXME: currently the start/small case fails on x86/Darwin. Moving
179 ;; this NOTE definition to the base.lisp file fixes that, but obviously
180 ;; it is better fo figure out what is going on instead of doing that...
182 ;; Other trivialish changes that mask the error include:
183 ;; * loading the .lisp file instead of the .fasl at the save test
184 ;; * --eval 'nil' before loading the .fasl at the save test
188 (write-line x *standard-output*)
189 (force-output *standard-output*))
190 (note "/initial assertions")
191 (assert (= 31 (summish 10 20)))
192 (assert (= 42 numberish))
194 (assert (= 13 numberish))
195 (assert (= 14 (nummish 1)))
197 (assert (= -1 (negative-short)))
198 (assert (= -2 (negative-int)))
199 (assert (= -3 (negative-long)))
201 (assert (= 9.0s0 (return9th 1.0s0 2.0s0 3.0s0 4.0s0 5.0s0 6.0s0 7.0s0 8.0s0 9.0s0 10.0s0 11.0s0 12.0s0)))
202 (assert (= 9.0d0 (return9thd 1.0d0 2.0d0 3.0d0 4.0d0 5.0d0 6.0d0 7.0d0 8.0d0 9.0d0 10.0d0 11.0d0 12.0d0)))
204 (assert (= 1 (long-test8 1 2 3 4 5 6 7 (ash 1 34))))
205 (assert (= 1 (long-test9 1 2 3 4 5 6 7 (ash 1 35) 8)))
206 (assert (= 1 (long-test2 1 2 3 4 5 6 7 8 9 (+ 1 (ash 1 37)) 15)))
207 (assert (= (ash 1 33) (return-long-long)))
209 (note "/initial assertions ok")
211 ;; test reloading object file with new definitions
213 (assert (= 42 (bar)))
214 (note "/original definitions ok")
215 (rename-file "$testfilestem-b.so" "$testfilestem-b.bak")
216 (rename-file "$testfilestem-b2.so" "$testfilestem-b.so")
217 (load-shared-object "$testfilestem-b.so")
218 (note "/reloading ok")
220 (assert (= 13 (bar)))
221 (note "/redefined versions ok")
222 (rename-file "$testfilestem-b.so" "$testfilestem-b2.so")
223 (rename-file "$testfilestem-b.bak" "$testfilestem-b.so")
224 (note "/renamed back to originals")
226 ;; test late resolution
229 (note "/starting linkage table tests")
230 (define-alien-variable late-foo int)
231 (define-alien-routine late-bar int)
232 (multiple-value-bind (val err) (ignore-errors late-foo)
234 (assert (typep err 'undefined-alien-error)))
235 (multiple-value-bind (val err) (ignore-errors (late-bar))
237 (assert (typep err 'undefined-alien-error)))
238 (load-shared-object "$testfilestem-c.so")
239 (assert (= 43 late-foo))
240 (assert (= 14 (late-bar)))
241 (note "/linkage table ok"))
243 (sb-ext:quit :unix-status 52) ; success convention for Lisp program
247 ${SBCL:-sbcl} --eval "(progn (load (compile-file #p\"$testfilestem.$1.lisp\")) (sb-ext:quit :unix-status 52))"
249 echo test compile
$1 ok
251 # we can't compile the test file. something's wrong.
253 echo test compile
$1 failed
: $?
262 ${SBCL:-sbcl} --load $testfilestem.
$1.fasl
--load $testfilestem.
test.lisp
264 if [ $RET = 22 ]; then
266 exit $PUNT # success -- load-shared-object not supported
267 elif [ $RET != 52 ]; then
269 echo test use
$1 failed
: $?
281 ${SBCL:-sbcl} --load $testfilestem.
$1.fasl
--eval "#+linkage-table (save-lisp-and-die \"$testfilestem.$1.core\") #-linkage-table nil" <<EOF
282 (sb-ext:quit :unix-status 22) ; catch this
286 exit $PUNT # success -- linkage-table not available
296 echo testing start
$1
297 ${SBCL_ALLOWING_CORE:-sbcl} --core $testfilestem.
$1.core
--sysinit /dev
/null
--userinit /dev
/null
--load $testfilestem.
test.lisp
298 if [ $?
!= 52 ]; then
303 echo test start
$1 ok
310 # missing object file
311 rm $testfilestem-b.so
$testfilestem-b2.so
312 ${SBCL_ALLOWING_CORE:-sbcl} --core $testfilestem.fast.core
--sysinit /dev
/null
--userinit /dev
/null
<<EOF
313 (assert (= 22 (summish 10 11)))
314 (multiple-value-bind (val err) (ignore-errors (eval 'foo))
316 (assert (typep err 'undefined-alien-error)))
317 (multiple-value-bind (val err) (ignore-errors (eval '(bar)))
319 (assert (typep err 'undefined-alien-error)))
320 (quit :unix-status 52)
322 if [ $?
!= 52 ]; then
330 rm -f $testfilestem.
* $testfilestem-*
332 # success convention for script