Correctly disassemble method functions.
[sbcl.git] / tests / foreign.test.sh
blobff7e6a3b0b4980b7e35f65c4bfef2f608dcbac94
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 run_compiler=`pwd`/run-compiler.sh
18 . ./expect.sh
19 . ./subr.sh
21 use_test_subdirectory
23 echo //entering foreign.test.sh
25 ## Make some shared object files to test with.
27 build_so() (
28 echo building $1.so
29 /bin/sh ${run_compiler} -sbcl-pic -sbcl-shared "$1.c" -o "$1.so"
32 # We want to bail out in case any of these Unix programs fails.
33 set -e
35 cat > $TEST_FILESTEM.c <<EOF
36 int summish(int x, int y) { return 1 + x + y; }
38 int numberish = 42;
40 int nummish(int x) { return numberish + x; }
42 short negative_short() { return -1; }
43 int negative_int() { return -2; }
44 long negative_long() { return -3; }
46 long long powish(unsigned int x, unsigned int y) {
47 long long acc = 1;
48 long long xx = (long long) x;
49 for(; y != 1; y /= 2) {
50 if (y & 1) {
51 acc *= xx;
52 y -= 1;
54 xx *= xx;
56 return xx*acc;
59 float return9th(float f1, float f2, float f3, float f4, float f5,
60 float f6, float f7, float f8, float f9, float f10,
61 float f11, float f12) {
62 return f9;
65 double return9thd(double f1, double f2, double f3, double f4, double f5,
66 double f6, double f7, double f8, double f9, double f10,
67 double f11, double f12) {
68 return f9;
71 int long_test8(int a1, int a2, int a3, int a4, int a5,
72 int a6, int a7, long long l1) {
73 return (l1 == powish(2,34));
76 int long_test9(int a1, int a2, int a3, int a4, int a5,
77 int a6, int a7, long long l1, int a8) {
78 return (l1 == powish(2,35));
81 int long_test2(int i1, int i2, int i3, int i4, int i5, int i6,
82 int i7, int i8, int i9, long long l1, long long l2) {
83 return (l1 == (1 + powish(2,37)));
86 int long_sap_test1(int *p1, long long l1) {
87 return (l1 == (3 + powish(2,*p1)));
90 int long_sap_test2(int *p1, int i1, long long l1) {
91 return (l1 == (3 + powish(2,*p1)));
94 long long return_long_long() {
95 return powish(2,33);
98 signed char return_schar_test(signed char *p) {
99 return *p;
102 unsigned char return_uchar_test(unsigned char *p) {
103 return *p;
106 short return_short_test(short *p) {
107 return *p;
110 unsigned short return_ushort_test(unsigned short *p) {
111 return *p;
114 int return_int_test(int *p) {
115 return *p;
118 unsigned int return_uint_test(unsigned int *p) {
119 return *p;
123 build_so $TEST_FILESTEM
125 echo 'int foo = 13;' > $TEST_FILESTEM-b.c
126 echo 'int bar() { return 42; }' >> $TEST_FILESTEM-b.c
127 build_so $TEST_FILESTEM-b
129 echo 'int foo = 42;' > $TEST_FILESTEM-b2.c
130 # Try not to produce two more-or-less physically identical '.so' files (modulo
131 # some constants in the functions), so that the test will fail if we accidentally
132 # call the new bar() at the old address - as might happen if the OS maps both '.so'
133 # files at the same place - as a consequence of failing to update the linkage table.
134 echo 'int bar(void);' >> $TEST_FILESTEM-b2.c
135 echo 'int somerandomfun(int x) { return x?-x:bar(); }' >> $TEST_FILESTEM-b2.c
136 echo 'int bar() { return 13; }' >> $TEST_FILESTEM-b2.c
137 build_so $TEST_FILESTEM-b2
139 echo 'int late_foo = 43;' > $TEST_FILESTEM-c.c
140 echo 'int late_bar() { return 14; }' >> $TEST_FILESTEM-c.c
141 build_so $TEST_FILESTEM-c
143 cat > $TEST_FILESTEM-noop-dlclose-test.c <<EOF
144 #include <dlfcn.h>
145 #include <stddef.h>
147 int dlclose_is_noop () {
148 #ifdef RTLD_NOLOAD
149 void * handle = dlopen("./$TEST_FILESTEM-noop-dlclose-test-helper.so", RTLD_NOW | RTLD_GLOBAL);
150 dlclose(handle);
152 handle = dlopen("./$TEST_FILESTEM-noop-dlclose-test-helper.so", RTLD_NOW | RTLD_NOLOAD);
153 if (handle != NULL) {
154 return 1;
156 #endif
157 return 0;
160 build_so $TEST_FILESTEM-noop-dlclose-test
162 cat > $TEST_FILESTEM-noop-dlclose-test-helper.c <<EOF
163 int sbcl_dlclose_test = 42;
165 build_so $TEST_FILESTEM-noop-dlclose-test-helper
167 ## Foreign definitions & load
169 cat > $TEST_FILESTEM.base.lisp <<EOF
170 (define-alien-variable environ (* c-string))
171 (defvar *environ* environ)
172 (eval-when (:compile-toplevel :load-toplevel :execute)
173 (handler-case
174 (progn
175 (load-shared-object (truename "$TEST_FILESTEM.so"))
176 (load-shared-object (truename "$TEST_FILESTEM-b.so")))
177 (sb-int:unsupported-operator ()
178 ;; At least as of sbcl-0.7.0.5, LOAD-SHARED-OBJECT isn't
179 ;; supported on every OS. In that case, there's nothing to test,
180 ;; and we can just fall through to success.
181 (sb-ext:exit :code 22)))) ; catch that
182 (define-alien-routine summish int (x int) (y int))
183 (define-alien-variable numberish int)
184 (define-alien-routine nummish int (x int))
185 (define-alien-variable "foo" int)
186 (define-alien-routine "bar" int)
188 (define-alien-routine "negative_short" short)
189 (define-alien-routine "negative_int" int)
190 (define-alien-routine "negative_long" long)
192 (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))
193 (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))
195 (define-alien-routine long-test8 int (int1 int) (int2 int) (int3 int) (int4 int) (int5 int) (int6 int) (int7 int) (long1 (integer 64)))
196 (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))
197 (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)))
198 (define-alien-routine long-sap-test1 int (ptr1 int :copy) (long1 (integer 64)))
199 (define-alien-routine long-sap-test2 int (ptr1 int :copy) (int1 int) (long1 (integer 64)))
200 (define-alien-routine return-long-long (integer 64))
201 (define-alien-routine return-schar-test char (p char :copy))
202 (define-alien-routine return-uchar-test unsigned-char (p unsigned-char :copy))
203 (define-alien-routine return-short-test short (p short :copy))
204 (define-alien-routine return-ushort-test unsigned-short (p unsigned-short :copy))
205 (define-alien-routine return-int-test int (p int :copy))
206 (define-alien-routine return-uint-test unsigned-int (p unsigned-int :copy))
208 ;; Test that loading an object file didn't screw up our records
209 ;; of variables visible in runtime. (This was a bug until
210 ;; Nikodemus Siivola's patch in sbcl-0.8.5.50.)
212 ;; This cannot be tested in a saved core, as there is no guarantee
213 ;; that the location will be the same.
214 (assert (= (sb-sys:sap-int (alien-sap *environ*))
215 (sb-sys:sap-int (alien-sap environ))))
218 echo "(declaim (optimize speed))" > $TEST_FILESTEM.fast.lisp
219 cat $TEST_FILESTEM.base.lisp >> $TEST_FILESTEM.fast.lisp
221 echo "(declaim (optimize space))" > $TEST_FILESTEM.small.lisp
222 cat $TEST_FILESTEM.base.lisp >> $TEST_FILESTEM.small.lisp
224 # Test code
225 cat > $TEST_FILESTEM.test.lisp <<EOF
226 ;; FIXME: currently the start/small case fails on x86/Darwin. Moving
227 ;; this NOTE definition to the base.lisp file fixes that, but obviously
228 ;; it is better fo figure out what is going on instead of doing that...
230 ;; Other trivialish changes that mask the error include:
231 ;; * loading the .lisp file instead of the .fasl at the save test
232 ;; * --eval 'nil' before loading the .fasl at the save test
234 ;; HATE.
235 (defun note (x)
236 (write-line x *standard-output*)
237 (force-output *standard-output*))
238 (note "/initial assertions")
239 (assert (= 31 (summish 10 20)))
240 (assert (= 42 numberish))
241 (setf numberish 13)
242 (assert (= 13 numberish))
243 (assert (= 14 (nummish 1)))
245 (assert (= -1 (negative-short)))
246 (assert (= -2 (negative-int)))
247 (assert (= -3 (negative-long)))
249 (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)))
250 (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)))
252 (assert (= 1 (long-test8 1 2 3 4 5 6 7 (ash 1 34))))
253 (assert (= 1 (long-test9 1 2 3 4 5 6 7 (ash 1 35) 8)))
254 (assert (= 1 (long-test2 1 2 3 4 5 6 7 8 9 (+ 1 (ash 1 37)) 15)))
255 (assert (= 1 (long-sap-test1 38 (+ 3 (ash 1 38)))))
256 (assert (= 1 (long-sap-test2 38 1 (+ 3 (ash 1 38)))))
257 (assert (= (ash 1 33) (return-long-long)))
258 (assert (= -1 (return-schar-test -1)))
259 (assert (= 1 (return-schar-test 1)))
260 (assert (= 255 (return-uchar-test 255)))
261 (assert (= 1 (return-uchar-test 1)))
262 (assert (= -1 (return-short-test -1)))
263 (assert (= 65535 (return-ushort-test 65535)))
264 (assert (= -1 (return-int-test -1)))
265 (assert (= #xffffffff (return-uint-test #xffffffff)))
267 (note "/initial assertions ok")
269 ;; determine if dlclose is a noop.
270 (load-shared-object (truename "$TEST_FILESTEM-noop-dlclose-test.so"))
271 (define-alien-routine dlclose-is-noop int)
272 (defparameter *dlclose-noop-p* (plusp (dlclose-is-noop)))
274 ;; test reloading object file with new definitions
275 (assert (= 13 foo))
276 (assert (= 42 (bar)))
277 (note "/original definitions ok")
278 (if *dlclose-noop-p*
279 (note "/skipping reloading tests")
280 (progn
281 (rename-file "$TEST_FILESTEM-b.so" "$TEST_FILESTEM-b.bak")
282 (rename-file "$TEST_FILESTEM-b2.so" "$TEST_FILESTEM-b.so")
283 (load-shared-object (truename "$TEST_FILESTEM-b.so"))
284 (note "/reloading ok")
285 (assert (= 42 foo))
286 (assert (= 13 (bar)))
287 (note "/redefined versions ok")
288 (rename-file "$TEST_FILESTEM-b.so" "$TEST_FILESTEM-b2.so")
289 (rename-file "$TEST_FILESTEM-b.bak" "$TEST_FILESTEM-b.so")
290 (note "/renamed back to originals")))
292 ;; test late resolution
293 (eval-when (:compile-toplevel :load-toplevel :execute)
294 (setq *features* (union *features* sb-impl:+internal-features+)))
295 (progn
296 (note "/starting linkage table tests")
297 (define-alien-variable late-foo int)
298 (define-alien-routine late-bar int)
299 (multiple-value-bind (val err) (ignore-errors late-foo)
300 (assert (not val))
301 (assert (typep err 'undefined-alien-error)))
302 (multiple-value-bind (val err) (ignore-errors (late-bar))
303 (assert (not val))
304 (assert (typep err 'undefined-alien-error)))
305 (load-shared-object (truename "$TEST_FILESTEM-c.so"))
306 (assert (= 43 late-foo))
307 (assert (= 14 (late-bar)))
308 (if *dlclose-noop-p*
309 (note "/skipping linkage table unloading tests")
310 (progn
311 (unload-shared-object (truename "$TEST_FILESTEM-c.so"))
312 (multiple-value-bind (val err) (ignore-errors late-foo)
313 (assert (not val))
314 (assert (typep err 'undefined-alien-error)))
315 (multiple-value-bind (val err) (ignore-errors (late-bar))
316 (assert (not val))
317 (assert (typep err 'undefined-alien-error)))))
318 (note "/linkage table ok"))
320 (sb-ext:exit :code $EXIT_LISP_WIN) ; success convention for Lisp program
323 # Files are now set up; toggle errexit off, since we use a custom exit
324 # convention.
325 set +e
327 test_compile() {
328 x="$1"
329 run_sbcl <<EOF
330 (progn (load (compile-file "$TEST_FILESTEM.$x.lisp"))
331 (sb-ext:exit :code $EXIT_LISP_WIN))
333 check_status_maybe_lose "compile $1" $?
336 test_compile fast
337 test_compile small
339 test_use() {
340 run_sbcl --load $TEST_FILESTEM.$1.fasl --load $TEST_FILESTEM.test.lisp
341 check_status_maybe_lose "use $1" $? 22 "(load-shared-object not supported)"
344 test_use small
345 test_use fast
347 test_save() {
348 echo testing save $1
349 x="$1"
350 run_sbcl --load $TEST_FILESTEM.$1.fasl <<EOF
351 (eval-when (:compile-toplevel :load-toplevel :execute)
352 (setq *features* (union *features* sb-impl:+internal-features+)))
353 (save-lisp-and-die "$TEST_FILESTEM.$x.core")
354 (sb-ext:exit :code 22) ; catch this
356 check_status_maybe_lose "save $1" $? \
357 0 "(successful save)" 22 "(linkage table not available)"
360 test_save small
361 test_save fast
363 test_start() {
364 if [ -f $TEST_FILESTEM.$1.core ] ; then
365 echo testing start $1
366 run_sbcl_with_core $TEST_FILESTEM.$1.core \
367 --no-sysinit --no-userinit \
368 --eval "(setf sb-ext:*evaluator-mode* :${TEST_SBCL_EVALUATOR_MODE:-compile})" \
369 --load $TEST_FILESTEM.test.lisp
370 check_status_maybe_lose "start $1" $?
371 else
372 echo "not testing start $1 (no core file, possibly due to linkage table above)"
376 test_start fast
377 test_start small
379 if [ -f $TEST_FILESTEM.fast.core ] ; then
380 # missing object file
381 run_sbcl_with_core $TEST_FILESTEM.fast.core --no-sysinit --no-userinit \
382 --eval "(setf sb-ext:*evaluator-mode* :${TEST_SBCL_EVALUATOR_MODE:-compile})" \
383 <<EOF
384 (setf *invoke-debugger-hook*
385 (lambda (condition hook)
386 (declare (ignore hook))
387 (let ((cont (find-restart 'continue condition)))
388 (when cont
389 (invoke-restart cont)))
390 (print :fell-through)
391 (invoke-debugger condition)))
392 (save-lisp-and-die "$TEST_FILESTEM.missing.core")
393 (sb-ext:exit :code 22) ; catch this
395 check_status_maybe_lose "saving-missing-so-core" $? \
396 0 "(successful save)" 22 "(linkage table not available)"
399 rm $TEST_FILESTEM-b.so $TEST_FILESTEM-b2.so
400 if [ -f $TEST_FILESTEM.missing.core ] ; then
401 run_sbcl_with_core $TEST_FILESTEM.missing.core --no-sysinit --no-userinit \
402 --eval "(setf sb-ext:*evaluator-mode* :${TEST_SBCL_EVALUATOR_MODE:-compile})" \
403 <<EOF
404 (assert (= 22 (summish 10 11)))
405 (multiple-value-bind (val err) (ignore-errors (eval 'foo))
406 (assert (not val))
407 (assert (typep err 'undefined-alien-error)))
408 (multiple-value-bind (val err) (ignore-errors (eval '(bar)))
409 (assert (not val))
410 (assert (typep err 'undefined-alien-error)))
411 (exit :code $EXIT_LISP_WIN)
413 check_status_maybe_lose "missing-so" $?
414 else
415 echo "skipping missing-so test (no core file, possibly due to linkage table above)"
418 # ADDR of a heap-allocated object
419 cat > $TEST_FILESTEM.addr.heap.c <<EOF
420 struct foo
422 int x, y;
423 } a, *b;
426 build_so $TEST_FILESTEM.addr.heap
428 run_sbcl <<EOF
429 (load-shared-object (truename "$TEST_FILESTEM.addr.heap.so"))
430 (define-alien-type foo (struct foo (x int) (y int)))
432 (define-alien-variable a foo)
433 (define-alien-variable b (* foo))
434 (funcall (compile nil '(lambda () (setq b (addr a)))))
435 (assert (sb-sys:sap= (alien-sap a) (alien-sap (deref b))))
436 (exit :code $EXIT_LISP_WIN)
438 check_status_maybe_lose "ADDR of a heap-allocated object" $?
440 run_sbcl <<EOF
441 (define-alien-type inner (struct inner (var (unsigned 32))))
442 (define-alien-type outer (struct outer (one inner) (two inner)))
444 (defvar *outer* (make-alien outer))
445 (defvar *inner* (make-alien inner))
446 (setf (slot *inner* 'var) 20)
447 (setf (slot *outer* 'one) (deref *inner*)) ; lp#1514023 (?)
448 (assert (= (slot (slot *outer* 'one) 'var) 20))
449 (setf (slot *inner* 'var) 40)
450 (setf (slot *outer* 'two) (deref *inner*)) ; lp#1514023 (?)
451 (assert (= (slot (slot *outer* 'two) 'var) 40))
452 (exit :code $EXIT_LISP_WIN)
454 check_status_maybe_lose "struct offsets" $?
456 cat > $TEST_FILESTEM.alien.enum.lisp <<EOF
457 (define-alien-type foo-flag
458 (enum foo-flag-
459 (:a 1)
460 (:b 2)))
462 (define-alien-type bar
463 (struct bar
464 (foo-flag foo-flag)))
466 (define-alien-type barp
467 (* bar))
469 (defun foo (x)
470 (declare (type (alien barp) x))
473 (defun bar (x)
474 (declare (type (alien barp) x))
477 expect_clean_compile $TEST_FILESTEM.alien.enum.lisp
479 # success convention for script
480 exit $EXIT_TEST_WIN