1.0.23.37: more CLOS and classoid thread safety
[sbcl/tcr.git] / tests / foreign.test.sh
blobd34a9007a316c3b8822a7d72167d4da736e8e2db
1 #!/bin/sh
3 # tests related to foreign function interface and loading of shared
4 # libraries
6 # This software is part of the SBCL system. See the README file for
7 # more information.
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
11 # from CMU CL.
13 # This software is in the public domain and is provided with
14 # absolutely no warranty. See the COPYING and CREDITS files for
15 # more information.
17 . ./subr.sh
18 use_test_subdirectory
20 echo //entering foreign.test.sh
22 # simple way to make sure we're not punting by accident:
23 # setting PUNT to anything other than 104 will make non-dlopen
24 # and non-linkage-table platforms fail this
25 PUNT=$EXIT_TEST_WIN
27 ## Make some shared object files to test with.
29 build_so() (
30 echo building $1.so
31 set +u
32 case "`uname -m`" in
33 x86_64|amd64|mips|mips64)
34 CFLAGS="$CFLAGS -fPIC"
36 esac
37 if [ "`uname`" = Darwin ]; then
38 SO_FLAGS="-bundle"
39 if run_sbcl --eval '(sb-ext:quit :unix-status #+x86-64 0 #-x86-64 1)'; then
40 CFLAGS="$CFLAGS -arch x86_64"
42 else
43 SO_FLAGS="-shared"
45 cc -c $1.c -o $1.o $CFLAGS
46 ld $SO_FLAGS -o $1.so $1.o
49 # We want to bail out in case any of these Unix programs fails.
50 set -e
52 cat > $TEST_FILESTEM.c <<EOF
53 int summish(int x, int y) { return 1 + x + y; }
55 int numberish = 42;
57 int nummish(int x) { return numberish + x; }
59 short negative_short() { return -1; }
60 int negative_int() { return -2; }
61 long negative_long() { return -3; }
63 long long powish(unsigned int x, unsigned int y) {
64 long long acc = 1;
65 long long xx = (long long) x;
66 for(; y != 1; y /= 2) {
67 if (y & 1) {
68 acc *= xx;
69 y -= 1;
71 xx *= xx;
73 return xx*acc;
76 float return9th(float f1, float f2, float f3, float f4, float f5,
77 float f6, float f7, float f8, float f9, float f10,
78 float f11, float f12) {
79 return f9;
82 double return9thd(double f1, double f2, double f3, double f4, double f5,
83 double f6, double f7, double f8, double f9, double f10,
84 double f11, double f12) {
85 return f9;
88 int long_test8(int a1, int a2, int a3, int a4, int a5,
89 int a6, int a7, long long l1) {
90 return (l1 == powish(2,34));
93 int long_test9(int a1, int a2, int a3, int a4, int a5,
94 int a6, int a7, long long l1, int a8) {
95 return (l1 == powish(2,35));
98 int long_test2(int i1, int i2, int i3, int i4, int i5, int i6,
99 int i7, int i8, int i9, long long l1, long long l2) {
100 return (l1 == (1 + powish(2,37)));
103 int long_sap_test1(int *p1, long long l1) {
104 return (l1 == (3 + powish(2,*p1)));
107 int long_sap_test2(int *p1, int i1, long long l1) {
108 return (l1 == (3 + powish(2,*p1)));
111 long long return_long_long() {
112 return powish(2,33);
116 build_so $TEST_FILESTEM
118 echo 'int foo = 13;' > $TEST_FILESTEM-b.c
119 echo 'int bar() { return 42; }' >> $TEST_FILESTEM-b.c
120 build_so $TEST_FILESTEM-b
122 echo 'int foo = 42;' > $TEST_FILESTEM-b2.c
123 echo 'int bar() { return 13; }' >> $TEST_FILESTEM-b2.c
124 build_so $TEST_FILESTEM-b2
126 echo 'int late_foo = 43;' > $TEST_FILESTEM-c.c
127 echo 'int late_bar() { return 14; }' >> $TEST_FILESTEM-c.c
128 build_so $TEST_FILESTEM-c
130 ## Foreign definitions & load
132 cat > $TEST_FILESTEM.base.lisp <<EOF
133 (define-alien-variable environ (* c-string))
134 (defvar *environ* environ)
135 (eval-when (:compile-toplevel :load-toplevel :execute)
136 (handler-case
137 (progn
138 (load-shared-object (truename "$TEST_FILESTEM.so"))
139 (load-shared-object (truename "$TEST_FILESTEM-b.so")))
140 (sb-int:unsupported-operator ()
141 ;; At least as of sbcl-0.7.0.5, LOAD-SHARED-OBJECT isn't
142 ;; supported on every OS. In that case, there's nothing to test,
143 ;; and we can just fall through to success.
144 (sb-ext:quit :unix-status 22)))) ; catch that
145 (define-alien-routine summish int (x int) (y int))
146 (define-alien-variable numberish int)
147 (define-alien-routine nummish int (x int))
148 (define-alien-variable "foo" int)
149 (define-alien-routine "bar" int)
151 (define-alien-routine "negative_short" short)
152 (define-alien-routine "negative_int" int)
153 (define-alien-routine "negative_long" long)
155 (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))
156 (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))
158 (define-alien-routine long-test8 int (int1 int) (int2 int) (int3 int) (int4 int) (int5 int) (int6 int) (int7 int) (long1 (integer 64)))
159 (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))
160 (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)))
161 (define-alien-routine long-sap-test1 int (ptr1 int :copy) (long1 (integer 64)))
162 (define-alien-routine long-sap-test2 int (ptr1 int :copy) (int1 int) (long1 (integer 64)))
163 (define-alien-routine return-long-long (integer 64))
165 ;; compiling this gets us the FOP-FOREIGN-DATAREF-FIXUP on
166 ;; linkage-table ports
167 (defvar *extern* (extern-alien "negative_short" short))
169 ;; Test that loading an object file didn't screw up our records
170 ;; of variables visible in runtime. (This was a bug until
171 ;; Nikodemus Siivola's patch in sbcl-0.8.5.50.)
173 ;; This cannot be tested in a saved core, as there is no guarantee
174 ;; that the location will be the same.
175 (assert (= (sb-sys:sap-int (alien-sap *environ*))
176 (sb-sys:sap-int (alien-sap environ))))
178 ;; automagic restarts
179 (setf *invoke-debugger-hook*
180 (lambda (condition hook)
181 (declare (ignore hook))
182 (princ condition)
183 (let ((cont (find-restart 'continue condition)))
184 (when cont
185 (invoke-restart cont)))
186 (print :fell-through)
187 (invoke-debugger condition)))
190 echo "(declaim (optimize speed))" > $TEST_FILESTEM.fast.lisp
191 cat $TEST_FILESTEM.base.lisp >> $TEST_FILESTEM.fast.lisp
193 echo "(declaim (optimize space))" > $TEST_FILESTEM.small.lisp
194 cat $TEST_FILESTEM.base.lisp >> $TEST_FILESTEM.small.lisp
196 # Test code
197 cat > $TEST_FILESTEM.test.lisp <<EOF
198 ;; FIXME: currently the start/small case fails on x86/Darwin. Moving
199 ;; this NOTE definition to the base.lisp file fixes that, but obviously
200 ;; it is better fo figure out what is going on instead of doing that...
202 ;; Other trivialish changes that mask the error include:
203 ;; * loading the .lisp file instead of the .fasl at the save test
204 ;; * --eval 'nil' before loading the .fasl at the save test
206 ;; HATE.
207 (defun note (x)
208 (write-line x *standard-output*)
209 (force-output *standard-output*))
210 (note "/initial assertions")
211 (assert (= 31 (summish 10 20)))
212 (assert (= 42 numberish))
213 (setf numberish 13)
214 (assert (= 13 numberish))
215 (assert (= 14 (nummish 1)))
217 (assert (= -1 (negative-short)))
218 (assert (= -2 (negative-int)))
219 (assert (= -3 (negative-long)))
221 (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)))
222 (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)))
224 (assert (= 1 (long-test8 1 2 3 4 5 6 7 (ash 1 34))))
225 (assert (= 1 (long-test9 1 2 3 4 5 6 7 (ash 1 35) 8)))
226 (assert (= 1 (long-test2 1 2 3 4 5 6 7 8 9 (+ 1 (ash 1 37)) 15)))
227 (assert (= 1 (long-sap-test1 38 (+ 3 (ash 1 38)))))
228 (assert (= 1 (long-sap-test2 38 1 (+ 3 (ash 1 38)))))
229 (assert (= (ash 1 33) (return-long-long)))
231 (note "/initial assertions ok")
233 ;; test reloading object file with new definitions
234 (assert (= 13 foo))
235 (assert (= 42 (bar)))
236 (note "/original definitions ok")
237 (rename-file "$TEST_FILESTEM-b.so" "$TEST_FILESTEM-b.bak")
238 (rename-file "$TEST_FILESTEM-b2.so" "$TEST_FILESTEM-b.so")
239 (load-shared-object (truename "$TEST_FILESTEM-b.so"))
240 (note "/reloading ok")
241 (assert (= 42 foo))
242 (assert (= 13 (bar)))
243 (note "/redefined versions ok")
244 (rename-file "$TEST_FILESTEM-b.so" "$TEST_FILESTEM-b2.so")
245 (rename-file "$TEST_FILESTEM-b.bak" "$TEST_FILESTEM-b.so")
246 (note "/renamed back to originals")
248 ;; test late resolution
249 #+linkage-table
250 (progn
251 (note "/starting linkage table tests")
252 (define-alien-variable late-foo int)
253 (define-alien-routine late-bar int)
254 (multiple-value-bind (val err) (ignore-errors late-foo)
255 (assert (not val))
256 (assert (typep err 'undefined-alien-error)))
257 (multiple-value-bind (val err) (ignore-errors (late-bar))
258 (assert (not val))
259 (assert (typep err 'undefined-alien-error)))
260 (load-shared-object (truename "$TEST_FILESTEM-c.so"))
261 (assert (= 43 late-foo))
262 (assert (= 14 (late-bar)))
263 (unload-shared-object (truename "$TEST_FILESTEM-c.so"))
264 (multiple-value-bind (val err) (ignore-errors late-foo)
265 (assert (not val))
266 (assert (typep err 'undefined-alien-error)))
267 (multiple-value-bind (val err) (ignore-errors (late-bar))
268 (assert (not val))
269 (assert (typep err 'undefined-alien-error)))
270 (note "/linkage table ok"))
272 (sb-ext:quit :unix-status $EXIT_LISP_WIN) ; success convention for Lisp program
275 # Files are now set up; toggle errexit off, since we use a custom exit
276 # convention.
277 set +e
279 test_compile() {
280 run_sbcl <<EOF
281 (progn (load (compile-file "$TEST_FILESTEM.$1.lisp"))
282 (sb-ext:quit :unix-status $EXIT_LISP_WIN))
284 check_status_maybe_lose "compile $1" $?
287 test_compile fast
288 test_compile small
290 test_use() {
291 run_sbcl --load $TEST_FILESTEM.$1.fasl --load $TEST_FILESTEM.test.lisp
292 check_status_maybe_lose "use $1" $? 22 "(load-shared-object not supported)"
295 test_use small
296 test_use fast
298 test_save() {
299 echo testing save $1
300 run_sbcl --load $TEST_FILESTEM.$1.fasl <<EOF
301 #+linkage-table (save-lisp-and-die "$TEST_FILESTEM.$1.core")
302 #-linkage-table nil
303 (sb-ext:quit :unix-status 22) ; catch this
305 check_status_maybe_lose "save $1" $? \
306 0 "(successful save)" 22 "(linkage table not available)"
309 test_save small
310 test_save fast
312 test_start() {
313 echo testing start $1
314 run_sbcl_with_core $TEST_FILESTEM.$1.core \
315 --no-sysinit --no-userinit --load $TEST_FILESTEM.test.lisp
316 check_status_maybe_lose "start $1" $?
319 test_start fast
320 test_start small
322 # missing object file
323 rm $TEST_FILESTEM-b.so $TEST_FILESTEM-b2.so
324 run_sbcl_with_core $TEST_FILESTEM.fast.core --no-sysinit --no-userinit <<EOF
325 (assert (= 22 (summish 10 11)))
326 (multiple-value-bind (val err) (ignore-errors (eval 'foo))
327 (assert (not val))
328 (assert (typep err 'undefined-alien-error)))
329 (multiple-value-bind (val err) (ignore-errors (eval '(bar)))
330 (assert (not val))
331 (assert (typep err 'undefined-alien-error)))
332 (quit :unix-status $EXIT_LISP_WIN)
334 check_status_maybe_lose "missing-so" $?
336 # ADDR of a heap-allocated object
337 cat > $TEST_FILESTEM.addr.heap.c <<EOF
338 struct foo
340 int x, y;
341 } a, *b;
344 build_so $TEST_FILESTEM.addr.heap
346 run_sbcl <<EOF
347 (load-shared-object (truename "$TEST_FILESTEM.addr.heap.so"))
348 (define-alien-type foo (struct foo (x int) (y int)))
350 (define-alien-variable a foo)
351 (define-alien-variable b (* foo))
352 (funcall (compile nil '(lambda () (setq b (addr a)))))
353 (assert (sb-sys:sap= (alien-sap a) (alien-sap (deref b))))
354 (quit :unix-status $EXIT_LISP_WIN)
356 check_status_maybe_lose "ADDR of a heap-allocated object" $?
359 # success convention for script
360 exit $EXIT_TEST_WIN