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