Fix make-array transforms.
[sbcl.git] / tests / foreign.test.sh
1 #!/bin/sh
2
3 # tests related to foreign function interface and loading of shared
4 # libraries
5
6 # This software is part of the SBCL system. See the README file for
7 # more information.
8 #
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.
12 #
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.
16
17 . ./expect.sh
18 . ./subr.sh
19
20 use_test_subdirectory
21
22 echo //entering foreign.test.sh
23
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
28
29 ## Make some shared object files to test with.
30
31 build_so() (
32   echo building $1.so
33   /bin/sh ../run-compiler.sh -sbcl-pic -sbcl-shared "$1.c" -o "$1.so"
34 )
35
36 # We want to bail out in case any of these Unix programs fails.
37 set -e
38
39 cat > $TEST_FILESTEM.c <<EOF
40 int summish(int x, int y) { return 1 + x + y; }
41
42 int numberish = 42;
43
44 int nummish(int x) { return numberish + x; }
45
46 short negative_short() { return -1; }
47 int negative_int()     { return -2; }
48 long negative_long()   { return -3; }
49
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;
57     }
58     xx *= xx;
59   }
60   return xx*acc;
61 }
62
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; 
67 }
68
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; 
73 }
74
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));
78 }
79
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));
83 }
84
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)));
88 }
89
90 int long_sap_test1(int *p1, long long l1) {
91     return (l1 == (3 + powish(2,*p1)));
92 }
93
94 int long_sap_test2(int *p1, int i1, long long l1) {
95     return (l1 == (3 + powish(2,*p1)));
96 }
97
98 long long return_long_long() {
99     return powish(2,33);
100 }
101 EOF
102
103 build_so $TEST_FILESTEM
104
105 echo 'int foo = 13;' > $TEST_FILESTEM-b.c
106 echo 'int bar() { return 42; }' >> $TEST_FILESTEM-b.c
107 build_so $TEST_FILESTEM-b
108
109 echo 'int foo = 42;' > $TEST_FILESTEM-b2.c
110 echo 'int bar() { return 13; }' >> $TEST_FILESTEM-b2.c
111 build_so $TEST_FILESTEM-b2
112
113 echo 'int late_foo = 43;' > $TEST_FILESTEM-c.c
114 echo 'int late_bar() { return 14; }' >> $TEST_FILESTEM-c.c
115 build_so $TEST_FILESTEM-c
116
117 ## Foreign definitions & load
118
119 cat > $TEST_FILESTEM.base.lisp <<EOF
120   (define-alien-variable environ (* c-string))
121   (defvar *environ* environ)
122   (eval-when (:compile-toplevel :load-toplevel :execute)
123     (handler-case
124         (progn
125           (load-shared-object (truename "$TEST_FILESTEM.so"))
126           (load-shared-object (truename "$TEST_FILESTEM-b.so")))
127       (sb-int:unsupported-operator ()
128         ;; At least as of sbcl-0.7.0.5, LOAD-SHARED-OBJECT isn't
129         ;; supported on every OS. In that case, there's nothing to test,
130         ;; and we can just fall through to success.
131         (sb-ext:exit :code 22)))) ; catch that
132   (define-alien-routine summish int (x int) (y int))
133   (define-alien-variable numberish int)
134   (define-alien-routine nummish int (x int))
135   (define-alien-variable "foo" int)
136   (define-alien-routine "bar" int)
137
138   (define-alien-routine "negative_short" short)
139   (define-alien-routine "negative_int" int)
140   (define-alien-routine "negative_long" long)
141
142   (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))
143   (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))
144
145   (define-alien-routine long-test8 int (int1 int) (int2 int) (int3 int) (int4 int) (int5 int) (int6 int) (int7 int) (long1 (integer 64)))
146   (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))
147   (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)))
148   (define-alien-routine long-sap-test1 int (ptr1 int :copy) (long1 (integer 64)))
149   (define-alien-routine long-sap-test2 int (ptr1 int :copy) (int1 int) (long1 (integer 64)))
150   (define-alien-routine return-long-long (integer 64))
151
152   ;; compiling this gets us the FOP-FOREIGN-DATAREF-FIXUP on
153   ;; linkage-table ports
154   (defvar *extern* (extern-alien "negative_short" short))
155
156   ;; Test that loading an object file didn't screw up our records
157   ;; of variables visible in runtime. (This was a bug until
158   ;; Nikodemus Siivola's patch in sbcl-0.8.5.50.)
159   ;;
160   ;; This cannot be tested in a saved core, as there is no guarantee
161   ;; that the location will be the same.
162   (assert (= (sb-sys:sap-int (alien-sap *environ*))
163              (sb-sys:sap-int (alien-sap environ))))
164
165   ;; automagic restarts
166   (setf *invoke-debugger-hook*
167         (lambda (condition hook)
168           (declare (ignore hook))
169           (princ condition)
170           (let ((cont (find-restart 'continue condition)))
171             (when cont
172               (invoke-restart cont)))
173           (print :fell-through)
174           (invoke-debugger condition)))
175 EOF
176
177 echo "(declaim (optimize speed))" > $TEST_FILESTEM.fast.lisp
178 cat $TEST_FILESTEM.base.lisp >> $TEST_FILESTEM.fast.lisp
179
180 echo "(declaim (optimize space))" > $TEST_FILESTEM.small.lisp
181 cat $TEST_FILESTEM.base.lisp >> $TEST_FILESTEM.small.lisp
182
183 # Test code
184 cat > $TEST_FILESTEM.test.lisp <<EOF
185   ;; FIXME: currently the start/small case fails on x86/Darwin. Moving
186   ;; this NOTE definition to the base.lisp file fixes that, but obviously
187   ;; it is better fo figure out what is going on instead of doing that...
188   ;;
189   ;; Other trivialish changes that mask the error include:
190   ;; * loading the .lisp file instead of the .fasl at the save test
191   ;; * --eval 'nil' before loading the .fasl at the save test
192   ;;
193   ;; HATE.
194   (defun note (x)
195      (write-line x *standard-output*)
196      (force-output *standard-output*))
197   (note "/initial assertions")
198   (assert (= 31 (summish 10 20)))
199   (assert (= 42 numberish))
200   (setf numberish 13)
201   (assert (= 13 numberish))
202   (assert (= 14 (nummish 1)))
203
204   (assert (= -1 (negative-short)))
205   (assert (= -2 (negative-int)))
206   (assert (= -3 (negative-long)))
207
208   (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)))
209   (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)))
210
211   (assert (= 1 (long-test8 1 2 3 4 5 6 7 (ash 1 34))))
212   (assert (= 1 (long-test9 1 2 3 4 5 6 7 (ash 1 35) 8)))
213   (assert (= 1 (long-test2 1 2 3 4 5 6 7 8 9 (+ 1 (ash 1 37)) 15)))
214   (assert (= 1 (long-sap-test1 38 (+ 3 (ash 1 38)))))
215   (assert (= 1 (long-sap-test2 38 1 (+ 3 (ash 1 38)))))
216   (assert (= (ash 1 33) (return-long-long)))
217
218   (note "/initial assertions ok")
219
220   ;; test reloading object file with new definitions
221   (assert (= 13 foo))
222   (assert (= 42 (bar)))
223   (note "/original definitions ok")
224   (rename-file "$TEST_FILESTEM-b.so" "$TEST_FILESTEM-b.bak")
225   (rename-file "$TEST_FILESTEM-b2.so" "$TEST_FILESTEM-b.so")
226   (load-shared-object (truename "$TEST_FILESTEM-b.so"))
227   (note "/reloading ok")
228   (assert (= 42 foo))
229   (assert (= 13 (bar)))
230   (note "/redefined versions ok")
231   (rename-file "$TEST_FILESTEM-b.so" "$TEST_FILESTEM-b2.so")
232   (rename-file "$TEST_FILESTEM-b.bak" "$TEST_FILESTEM-b.so")
233   (note "/renamed back to originals")
234
235   ;; test late resolution
236   #+linkage-table
237   (progn
238     (note "/starting linkage table tests")
239     (define-alien-variable late-foo int)
240     (define-alien-routine late-bar int)
241     (multiple-value-bind (val err) (ignore-errors late-foo)
242       (assert (not val))
243       (assert (typep err 'undefined-alien-error)))
244     (multiple-value-bind (val err) (ignore-errors (late-bar))
245       (assert (not val))
246       (assert (typep err 'undefined-alien-error)))
247     (load-shared-object (truename "$TEST_FILESTEM-c.so"))
248     (assert (= 43 late-foo))
249     (assert (= 14 (late-bar)))
250     (unload-shared-object (truename "$TEST_FILESTEM-c.so"))
251     (multiple-value-bind (val err) (ignore-errors late-foo)
252       (assert (not val))
253       (assert (typep err 'undefined-alien-error)))
254     (multiple-value-bind (val err) (ignore-errors (late-bar))
255       (assert (not val))
256       (assert (typep err 'undefined-alien-error)))
257     (note "/linkage table ok"))
258
259   (sb-ext:exit :code $EXIT_LISP_WIN) ; success convention for Lisp program
260 EOF
261
262 # Files are now set up; toggle errexit off, since we use a custom exit
263 # convention.
264 set +e
265
266 test_compile() {
267     x="$1"
268     run_sbcl <<EOF
269 (progn (load (compile-file "$TEST_FILESTEM.$x.lisp"))
270 (sb-ext:exit :code $EXIT_LISP_WIN))
271 EOF
272     check_status_maybe_lose "compile $1" $?
273 }
274
275 test_compile fast
276 test_compile small
277
278 test_use() {
279     run_sbcl --load $TEST_FILESTEM.$1.fasl --load $TEST_FILESTEM.test.lisp
280     check_status_maybe_lose "use $1" $? 22 "(load-shared-object not supported)"
281 }
282
283 test_use small
284 test_use fast
285
286 test_save() {
287     echo testing save $1 
288     x="$1"
289     run_sbcl --load $TEST_FILESTEM.$1.fasl <<EOF
290 #+linkage-table (save-lisp-and-die "$TEST_FILESTEM.$x.core")
291 #-linkage-table nil
292 (sb-ext:exit :code 22) ; catch this
293 EOF
294     check_status_maybe_lose "save $1" $? \
295         0 "(successful save)" 22 "(linkage table not available)"
296 }
297
298 test_save small
299 test_save fast
300
301 test_start() {
302     echo testing start $1
303     run_sbcl_with_core $TEST_FILESTEM.$1.core \
304         --no-sysinit --no-userinit --load $TEST_FILESTEM.test.lisp
305     check_status_maybe_lose "start $1" $?
306 }
307
308 test_start fast
309 test_start small
310
311 # missing object file
312 rm $TEST_FILESTEM-b.so $TEST_FILESTEM-b2.so
313 run_sbcl_with_core $TEST_FILESTEM.fast.core --no-sysinit --no-userinit <<EOF
314   (assert (= 22 (summish 10 11)))
315   (multiple-value-bind (val err) (ignore-errors (eval 'foo))
316     (assert (not val))
317     (assert (typep err 'undefined-alien-error)))
318   (multiple-value-bind (val err) (ignore-errors (eval '(bar)))
319     (assert (not val))
320     (assert (typep err 'undefined-alien-error)))
321   (exit :code $EXIT_LISP_WIN)
322 EOF
323 check_status_maybe_lose "missing-so" $?
324
325 # ADDR of a heap-allocated object
326 cat > $TEST_FILESTEM.addr.heap.c <<EOF
327   struct foo
328   {
329     int x, y;
330   } a, *b;
331 EOF
332
333 build_so $TEST_FILESTEM.addr.heap
334
335 run_sbcl <<EOF
336   (load-shared-object (truename "$TEST_FILESTEM.addr.heap.so"))
337   (define-alien-type foo (struct foo (x int) (y int)))
338
339   (define-alien-variable a foo)
340   (define-alien-variable b (* foo))
341   (funcall (compile nil '(lambda () (setq b (addr a)))))
342   (assert (sb-sys:sap= (alien-sap a) (alien-sap (deref b))))
343   (exit :code $EXIT_LISP_WIN)
344 EOF
345 check_status_maybe_lose "ADDR of a heap-allocated object" $?
346
347 run_sbcl <<EOF
348   (define-alien-type inner (struct inner (var (unsigned 32))))
349   (define-alien-type outer (struct outer (one inner) (two inner)))
350
351   (defvar *outer* (make-alien outer))
352   (defvar *inner* (make-alien inner))
353   (setf (slot *inner* 'var) 20)
354   (setf (slot *outer* 'one) *inner*)
355   (assert (= (slot (slot *outer* 'one) 'var) 20))
356   (setf (slot *inner* 'var) 40)
357   (setf (slot *outer* 'two) *inner*)
358   (assert (= (slot (slot *outer* 'two) 'var) 40))
359   (exit :code $EXIT_LISP_WIN)
360 EOF
361 check_status_maybe_lose "struct offsets" $?
362
363 cat > $TEST_FILESTEM.alien.enum.lisp <<EOF
364 (define-alien-type foo-flag
365   (enum foo-flag-
366     (:a 1)
367     (:b 2)))
368
369 (define-alien-type bar
370   (struct bar
371     (foo-flag foo-flag)))
372
373 (define-alien-type barp
374   (* bar))
375
376 (defun foo (x)
377   (declare (type (alien barp) x))
378   x)
379
380 (defun bar (x)
381   (declare (type (alien barp) x))
382   x)
383 EOF
384 expect_clean_compile $TEST_FILESTEM.alien.enum.lisp
385
386 # success convention for script
387 exit $EXIT_TEST_WIN