Signal floating-point-overflow from bignum-to-float.
[sbcl.git] / tests / foreign.test.sh
blob7fb7578135c1c68f16118619898e0c4123d8df97
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 . ./expect.sh
18 . ./subr.sh
20 use_test_subdirectory
22 echo //entering foreign.test.sh
24 # simple way to make sure we're not punting by accident:
25 # setting PUNT to anything other than 104 will make non-dlopen
26 # and non-linkage-table platforms fail this
27 PUNT=$EXIT_TEST_WIN
29 ## Make some shared object files to test with.
31 build_so() (
32 echo building $1.so
33 /bin/sh ../run-compiler.sh -sbcl-pic -sbcl-shared "$1.c" -o "$1.so"
36 # We want to bail out in case any of these Unix programs fails.
37 set -e
39 cat > $TEST_FILESTEM.c <<EOF
40 int summish(int x, int y) { return 1 + x + y; }
42 int numberish = 42;
44 int nummish(int x) { return numberish + x; }
46 short negative_short() { return -1; }
47 int negative_int() { return -2; }
48 long negative_long() { return -3; }
50 long long powish(unsigned int x, unsigned int y) {
51 long long acc = 1;
52 long long xx = (long long) x;
53 for(; y != 1; y /= 2) {
54 if (y & 1) {
55 acc *= xx;
56 y -= 1;
58 xx *= xx;
60 return xx*acc;
63 float return9th(float f1, float f2, float f3, float f4, float f5,
64 float f6, float f7, float f8, float f9, float f10,
65 float f11, float f12) {
66 return f9;
69 double return9thd(double f1, double f2, double f3, double f4, double f5,
70 double f6, double f7, double f8, double f9, double f10,
71 double f11, double f12) {
72 return f9;
75 int long_test8(int a1, int a2, int a3, int a4, int a5,
76 int a6, int a7, long long l1) {
77 return (l1 == powish(2,34));
80 int long_test9(int a1, int a2, int a3, int a4, int a5,
81 int a6, int a7, long long l1, int a8) {
82 return (l1 == powish(2,35));
85 int long_test2(int i1, int i2, int i3, int i4, int i5, int i6,
86 int i7, int i8, int i9, long long l1, long long l2) {
87 return (l1 == (1 + powish(2,37)));
90 int long_sap_test1(int *p1, long long l1) {
91 return (l1 == (3 + powish(2,*p1)));
94 int long_sap_test2(int *p1, int i1, long long l1) {
95 return (l1 == (3 + powish(2,*p1)));
98 long long return_long_long() {
99 return powish(2,33);
102 signed char return_schar_test(signed char *p) {
103 return *p;
106 unsigned char return_uchar_test(unsigned char *p) {
107 return *p;
110 short return_short_test(short *p) {
111 return *p;
114 unsigned short return_ushort_test(unsigned short *p) {
115 return *p;
118 int return_int_test(int *p) {
119 return *p;
122 unsigned int return_uint_test(unsigned int *p) {
123 return *p;
127 build_so $TEST_FILESTEM
129 echo 'int foo = 13;' > $TEST_FILESTEM-b.c
130 echo 'int bar() { return 42; }' >> $TEST_FILESTEM-b.c
131 build_so $TEST_FILESTEM-b
133 echo 'int foo = 42;' > $TEST_FILESTEM-b2.c
134 echo 'int bar() { return 13; }' >> $TEST_FILESTEM-b2.c
135 build_so $TEST_FILESTEM-b2
137 echo 'int late_foo = 43;' > $TEST_FILESTEM-c.c
138 echo 'int late_bar() { return 14; }' >> $TEST_FILESTEM-c.c
139 build_so $TEST_FILESTEM-c
141 ## Foreign definitions & load
143 cat > $TEST_FILESTEM.base.lisp <<EOF
144 (define-alien-variable environ (* c-string))
145 (defvar *environ* environ)
146 (eval-when (:compile-toplevel :load-toplevel :execute)
147 (handler-case
148 (progn
149 (load-shared-object (truename "$TEST_FILESTEM.so"))
150 (load-shared-object (truename "$TEST_FILESTEM-b.so")))
151 (sb-int:unsupported-operator ()
152 ;; At least as of sbcl-0.7.0.5, LOAD-SHARED-OBJECT isn't
153 ;; supported on every OS. In that case, there's nothing to test,
154 ;; and we can just fall through to success.
155 (sb-ext:exit :code 22)))) ; catch that
156 (define-alien-routine summish int (x int) (y int))
157 (define-alien-variable numberish int)
158 (define-alien-routine nummish int (x int))
159 (define-alien-variable "foo" int)
160 (define-alien-routine "bar" int)
162 (define-alien-routine "negative_short" short)
163 (define-alien-routine "negative_int" int)
164 (define-alien-routine "negative_long" long)
166 (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))
167 (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))
169 (define-alien-routine long-test8 int (int1 int) (int2 int) (int3 int) (int4 int) (int5 int) (int6 int) (int7 int) (long1 (integer 64)))
170 (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))
171 (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)))
172 (define-alien-routine long-sap-test1 int (ptr1 int :copy) (long1 (integer 64)))
173 (define-alien-routine long-sap-test2 int (ptr1 int :copy) (int1 int) (long1 (integer 64)))
174 (define-alien-routine return-long-long (integer 64))
175 (define-alien-routine return-schar-test char (p char :copy))
176 (define-alien-routine return-uchar-test unsigned-char (p unsigned-char :copy))
177 (define-alien-routine return-short-test short (p short :copy))
178 (define-alien-routine return-ushort-test unsigned-short (p unsigned-short :copy))
179 (define-alien-routine return-int-test int (p int :copy))
180 (define-alien-routine return-uint-test unsigned-int (p unsigned-int :copy))
182 ;; compiling this gets us the FOP-FOREIGN-DATAREF-FIXUP on
183 ;; linkage-table ports
184 (defvar *extern* (extern-alien "negative_short" short))
186 ;; Test that loading an object file didn't screw up our records
187 ;; of variables visible in runtime. (This was a bug until
188 ;; Nikodemus Siivola's patch in sbcl-0.8.5.50.)
190 ;; This cannot be tested in a saved core, as there is no guarantee
191 ;; that the location will be the same.
192 (assert (= (sb-sys:sap-int (alien-sap *environ*))
193 (sb-sys:sap-int (alien-sap environ))))
196 echo "(declaim (optimize speed))" > $TEST_FILESTEM.fast.lisp
197 cat $TEST_FILESTEM.base.lisp >> $TEST_FILESTEM.fast.lisp
199 echo "(declaim (optimize space))" > $TEST_FILESTEM.small.lisp
200 cat $TEST_FILESTEM.base.lisp >> $TEST_FILESTEM.small.lisp
202 # Test code
203 cat > $TEST_FILESTEM.test.lisp <<EOF
204 ;; FIXME: currently the start/small case fails on x86/Darwin. Moving
205 ;; this NOTE definition to the base.lisp file fixes that, but obviously
206 ;; it is better fo figure out what is going on instead of doing that...
208 ;; Other trivialish changes that mask the error include:
209 ;; * loading the .lisp file instead of the .fasl at the save test
210 ;; * --eval 'nil' before loading the .fasl at the save test
212 ;; HATE.
213 (defun note (x)
214 (write-line x *standard-output*)
215 (force-output *standard-output*))
216 (note "/initial assertions")
217 (assert (= 31 (summish 10 20)))
218 (assert (= 42 numberish))
219 (setf numberish 13)
220 (assert (= 13 numberish))
221 (assert (= 14 (nummish 1)))
223 (assert (= -1 (negative-short)))
224 (assert (= -2 (negative-int)))
225 (assert (= -3 (negative-long)))
227 (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)))
228 (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)))
230 (assert (= 1 (long-test8 1 2 3 4 5 6 7 (ash 1 34))))
231 (assert (= 1 (long-test9 1 2 3 4 5 6 7 (ash 1 35) 8)))
232 (assert (= 1 (long-test2 1 2 3 4 5 6 7 8 9 (+ 1 (ash 1 37)) 15)))
233 (assert (= 1 (long-sap-test1 38 (+ 3 (ash 1 38)))))
234 (assert (= 1 (long-sap-test2 38 1 (+ 3 (ash 1 38)))))
235 (assert (= (ash 1 33) (return-long-long)))
236 (assert (= -1 (return-schar-test -1)))
237 (assert (= 1 (return-schar-test 1)))
238 (assert (= 255 (return-uchar-test 255)))
239 (assert (= 1 (return-uchar-test 1)))
240 (assert (= -1 (return-short-test -1)))
241 (assert (= 65535 (return-ushort-test 65535)))
242 (assert (= -1 (return-int-test -1)))
243 (assert (= #xffffffff (return-uint-test #xffffffff)))
245 (note "/initial assertions ok")
247 ;; test reloading object file with new definitions
248 (assert (= 13 foo))
249 (assert (= 42 (bar)))
250 (note "/original definitions ok")
251 (rename-file "$TEST_FILESTEM-b.so" "$TEST_FILESTEM-b.bak")
252 (rename-file "$TEST_FILESTEM-b2.so" "$TEST_FILESTEM-b.so")
253 (load-shared-object (truename "$TEST_FILESTEM-b.so"))
254 (note "/reloading ok")
255 (assert (= 42 foo))
256 (assert (= 13 (bar)))
257 (note "/redefined versions ok")
258 (rename-file "$TEST_FILESTEM-b.so" "$TEST_FILESTEM-b2.so")
259 (rename-file "$TEST_FILESTEM-b.bak" "$TEST_FILESTEM-b.so")
260 (note "/renamed back to originals")
262 ;; test late resolution
263 #+linkage-table
264 (progn
265 (note "/starting linkage table tests")
266 (define-alien-variable late-foo int)
267 (define-alien-routine late-bar int)
268 (multiple-value-bind (val err) (ignore-errors late-foo)
269 (assert (not val))
270 (assert (typep err 'undefined-alien-error)))
271 (multiple-value-bind (val err) (ignore-errors (late-bar))
272 (assert (not val))
273 (assert (typep err 'undefined-alien-error)))
274 (load-shared-object (truename "$TEST_FILESTEM-c.so"))
275 (assert (= 43 late-foo))
276 (assert (= 14 (late-bar)))
277 (unload-shared-object (truename "$TEST_FILESTEM-c.so"))
278 (multiple-value-bind (val err) (ignore-errors late-foo)
279 (assert (not val))
280 (assert (typep err 'undefined-alien-error)))
281 (multiple-value-bind (val err) (ignore-errors (late-bar))
282 (assert (not val))
283 (assert (typep err 'undefined-alien-error)))
284 (note "/linkage table ok"))
286 (sb-ext:exit :code $EXIT_LISP_WIN) ; success convention for Lisp program
289 # Files are now set up; toggle errexit off, since we use a custom exit
290 # convention.
291 set +e
293 test_compile() {
294 x="$1"
295 run_sbcl <<EOF
296 (progn (load (compile-file "$TEST_FILESTEM.$x.lisp"))
297 (sb-ext:exit :code $EXIT_LISP_WIN))
299 check_status_maybe_lose "compile $1" $?
302 test_compile fast
303 test_compile small
305 test_use() {
306 run_sbcl --load $TEST_FILESTEM.$1.fasl --load $TEST_FILESTEM.test.lisp
307 check_status_maybe_lose "use $1" $? 22 "(load-shared-object not supported)"
310 test_use small
311 test_use fast
313 test_save() {
314 echo testing save $1
315 x="$1"
316 run_sbcl --load $TEST_FILESTEM.$1.fasl <<EOF
317 #+linkage-table (save-lisp-and-die "$TEST_FILESTEM.$x.core")
318 (sb-ext:exit :code 22) ; catch this
320 check_status_maybe_lose "save $1" $? \
321 0 "(successful save)" 22 "(linkage table not available)"
324 test_save small
325 test_save fast
327 test_start() {
328 if [ -f $TEST_FILESTEM.$1.core ] ; then
329 echo testing start $1
330 run_sbcl_with_core $TEST_FILESTEM.$1.core \
331 --no-sysinit --no-userinit \
332 --eval "(setf sb-ext:*evaluator-mode* :${TEST_SBCL_EVALUATOR_MODE:-compile})" \
333 --load $TEST_FILESTEM.test.lisp
334 check_status_maybe_lose "start $1" $?
335 else
336 echo "not testing start $1 (no core file, possibly due to linkage table above)"
340 test_start fast
341 test_start small
343 if [ -f $TEST_FILESTEM.fast.core ] ; then
344 # missing object file
345 run_sbcl_with_core $TEST_FILESTEM.fast.core --no-sysinit --no-userinit \
346 --eval "(setf sb-ext:*evaluator-mode* :${TEST_SBCL_EVALUATOR_MODE:-compile})" \
347 <<EOF
348 (setf *invoke-debugger-hook*
349 (lambda (condition hook)
350 (declare (ignore hook))
351 (let ((cont (find-restart 'continue condition)))
352 (when cont
353 (invoke-restart cont)))
354 (print :fell-through)
355 (invoke-debugger condition)))
356 #+linkage-table (save-lisp-and-die "$TEST_FILESTEM.missing.core")
357 (sb-ext:exit :code 22) ; catch this
359 check_status_maybe_lose "saving-missing-so-core" $? \
360 0 "(successful save)" 22 "(linkage table not available)"
363 rm $TEST_FILESTEM-b.so $TEST_FILESTEM-b2.so
364 if [ -f $TEST_FILESTEM.missing.core ] ; then
365 run_sbcl_with_core $TEST_FILESTEM.missing.core --no-sysinit --no-userinit \
366 --eval "(setf sb-ext:*evaluator-mode* :${TEST_SBCL_EVALUATOR_MODE:-compile})" \
367 <<EOF
368 (assert (= 22 (summish 10 11)))
369 (multiple-value-bind (val err) (ignore-errors (eval 'foo))
370 (assert (not val))
371 (assert (typep err 'undefined-alien-error)))
372 (multiple-value-bind (val err) (ignore-errors (eval '(bar)))
373 (assert (not val))
374 (assert (typep err 'undefined-alien-error)))
375 (exit :code $EXIT_LISP_WIN)
377 check_status_maybe_lose "missing-so" $?
378 else
379 echo "skipping missing-so test (no core file, possibly due to linkage table above)"
382 # ADDR of a heap-allocated object
383 cat > $TEST_FILESTEM.addr.heap.c <<EOF
384 struct foo
386 int x, y;
387 } a, *b;
390 build_so $TEST_FILESTEM.addr.heap
392 run_sbcl <<EOF
393 (load-shared-object (truename "$TEST_FILESTEM.addr.heap.so"))
394 (define-alien-type foo (struct foo (x int) (y int)))
396 (define-alien-variable a foo)
397 (define-alien-variable b (* foo))
398 (funcall (compile nil '(lambda () (setq b (addr a)))))
399 (assert (sb-sys:sap= (alien-sap a) (alien-sap (deref b))))
400 (exit :code $EXIT_LISP_WIN)
402 check_status_maybe_lose "ADDR of a heap-allocated object" $?
404 run_sbcl <<EOF
405 (define-alien-type inner (struct inner (var (unsigned 32))))
406 (define-alien-type outer (struct outer (one inner) (two inner)))
408 (defvar *outer* (make-alien outer))
409 (defvar *inner* (make-alien inner))
410 (setf (slot *inner* 'var) 20)
411 (setf (slot *outer* 'one) (deref *inner*)) ; lp#1514023 (?)
412 (assert (= (slot (slot *outer* 'one) 'var) 20))
413 (setf (slot *inner* 'var) 40)
414 (setf (slot *outer* 'two) (deref *inner*)) ; lp#1514023 (?)
415 (assert (= (slot (slot *outer* 'two) 'var) 40))
416 (exit :code $EXIT_LISP_WIN)
418 check_status_maybe_lose "struct offsets" $?
420 cat > $TEST_FILESTEM.alien.enum.lisp <<EOF
421 (define-alien-type foo-flag
422 (enum foo-flag-
423 (:a 1)
424 (:b 2)))
426 (define-alien-type bar
427 (struct bar
428 (foo-flag foo-flag)))
430 (define-alien-type barp
431 (* bar))
433 (defun foo (x)
434 (declare (type (alien barp) x))
437 (defun bar (x)
438 (declare (type (alien barp) x))
441 expect_clean_compile $TEST_FILESTEM.alien.enum.lisp
443 # success convention for script
444 exit $EXIT_TEST_WIN