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