1.0.13.1: Various cleanups and touchups in tests/
[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 . ./subr.sh
18 use_test_subdirectory
19
20 echo //entering foreign.test.sh
21
22 # simple way to make sure we're not punting by accident:
23 # setting PUNT to anything other than 104 will make non-dlopen
24 # and non-linkage-table platforms fail this
25 PUNT=$EXIT_TEST_WIN
26
27 ## Make some shared object files to test with.
28
29 build_so() (
30   echo building $1.so
31   set +u
32   case "`uname -m`" in
33       x86_64|amd64|mips|mips64)
34           CFLAGS="$CFLAGS -fPIC"
35           ;;
36   esac
37   if [ "`uname`" = Darwin ]; then
38     SO_FLAGS="-bundle"
39   else
40     SO_FLAGS="-shared"
41   fi
42   cc -c $1.c -o $1.o $CFLAGS
43   ld $SO_FLAGS -o $1.so $1.o  
44 )
45
46 # We want to bail out in case any of these Unix programs fails.
47 set -e
48
49 cat > $TEST_FILESTEM.c <<EOF
50 int summish(int x, int y) { return 1 + x + y; }
51
52 int numberish = 42;
53
54 int nummish(int x) { return numberish + x; }
55
56 short negative_short() { return -1; }
57 int negative_int()     { return -2; }
58 long negative_long()   { return -3; }
59
60 long long powish(unsigned int x, unsigned int y) {
61   long long acc = 1;
62   long long xx = (long long) x;
63   for(; y != 1; y /= 2) {
64     if (y & 1) {
65       acc *= xx;
66       y -= 1;
67     }
68     xx *= xx;
69   }
70   return xx*acc;
71 }
72
73 float return9th(float f1, float f2, float f3, float f4, float f5, 
74                 float f6, float f7, float f8, float f9, float f10, 
75                 float f11, float f12) { 
76     return f9; 
77 }
78
79 double return9thd(double f1, double f2, double f3, double f4, double f5, 
80                   double f6, double f7, double f8, double f9, double f10,
81                   double f11, double f12) { 
82     return f9; 
83 }
84
85 int long_test8(int a1, int a2, int a3, int a4, int a5, 
86                int a6, int a7, long long l1) { 
87     return (l1 == powish(2,34));
88 }
89
90 int long_test9(int a1, int a2, int a3, int a4, int a5, 
91                int a6, int a7, long long l1, int a8) { 
92     return (l1 == powish(2,35));
93 }
94
95 int long_test2(int i1, int i2, int i3, int i4, int i5, int i6,
96                int i7, int i8, int i9, long long l1, long long l2) {
97     return (l1 == (1 + powish(2,37)));
98 }
99
100 long long return_long_long() {
101     return powish(2,33);
102 }
103 EOF
104
105 build_so $TEST_FILESTEM
106
107 echo 'int foo = 13;' > $TEST_FILESTEM-b.c
108 echo 'int bar() { return 42; }' >> $TEST_FILESTEM-b.c
109 build_so $TEST_FILESTEM-b
110
111 echo 'int foo = 42;' > $TEST_FILESTEM-b2.c
112 echo 'int bar() { return 13; }' >> $TEST_FILESTEM-b2.c
113 build_so $TEST_FILESTEM-b2
114
115 echo 'int late_foo = 43;' > $TEST_FILESTEM-c.c
116 echo 'int late_bar() { return 14; }' >> $TEST_FILESTEM-c.c
117 build_so $TEST_FILESTEM-c
118
119 ## Foreign definitions & load
120
121 cat > $TEST_FILESTEM.base.lisp <<EOF
122   (define-alien-variable environ (* c-string))
123   (defvar *environ* environ)
124   (eval-when (:compile-toplevel :load-toplevel :execute)
125     (handler-case
126         (progn
127           (load-shared-object "$TEST_FILESTEM.so")
128           (load-shared-object "$TEST_FILESTEM-b.so"))
129       (sb-int:unsupported-operator ()
130         ;; At least as of sbcl-0.7.0.5, LOAD-SHARED-OBJECT isn't
131         ;; supported on every OS. In that case, there's nothing to test,
132         ;; and we can just fall through to success.
133         (sb-ext:quit :unix-status 22)))) ; catch that
134   (define-alien-routine summish int (x int) (y int))
135   (define-alien-variable numberish int)
136   (define-alien-routine nummish int (x int))
137   (define-alien-variable "foo" int)
138   (define-alien-routine "bar" int)
139
140   (define-alien-routine "negative_short" short)
141   (define-alien-routine "negative_int" int)
142   (define-alien-routine "negative_long" long)
143
144   (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))
145   (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))
146
147   (define-alien-routine long-test8 int (int1 int) (int2 int) (int3 int) (int4 int) (int5 int) (int6 int) (int7 int) (long1 (integer 64)))
148   (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))
149   (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)))
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 (= (ash 1 33) (return-long-long)))
215
216   (note "/initial assertions ok")
217
218   ;; test reloading object file with new definitions
219   (assert (= 13 foo))
220   (assert (= 42 (bar)))
221   (note "/original definitions ok")
222   (rename-file "$TEST_FILESTEM-b.so" "$TEST_FILESTEM-b.bak")
223   (rename-file "$TEST_FILESTEM-b2.so" "$TEST_FILESTEM-b.so")
224   (load-shared-object "$TEST_FILESTEM-b.so")
225   (note "/reloading ok")
226   (assert (= 42 foo))
227   (assert (= 13 (bar)))
228   (note "/redefined versions ok")
229   (rename-file "$TEST_FILESTEM-b.so" "$TEST_FILESTEM-b2.so")
230   (rename-file "$TEST_FILESTEM-b.bak" "$TEST_FILESTEM-b.so")
231   (note "/renamed back to originals")
232
233   ;; test late resolution
234   #+linkage-table
235   (progn
236     (note "/starting linkage table tests")
237     (define-alien-variable late-foo int)
238     (define-alien-routine late-bar int)
239     (multiple-value-bind (val err) (ignore-errors late-foo)
240       (assert (not val))
241       (assert (typep err 'undefined-alien-error)))
242     (multiple-value-bind (val err) (ignore-errors (late-bar))
243       (assert (not val))
244       (assert (typep err 'undefined-alien-error)))
245     (load-shared-object "$TEST_FILESTEM-c.so")
246     (assert (= 43 late-foo))
247     (assert (= 14 (late-bar)))
248     (note "/linkage table ok"))
249
250   (sb-ext:quit :unix-status $EXIT_LISP_WIN) ; success convention for Lisp program
251 EOF
252
253 # Files are now set up; toggle errexit off, since we use a custom exit
254 # convention.
255 set +e
256
257 test_compile() {
258     run_sbcl <<EOF
259 (progn (load (compile-file "$TEST_FILESTEM.$1.lisp"))
260 (sb-ext:quit :unix-status $EXIT_LISP_WIN))
261 EOF
262     check_status_maybe_lose "compile $1" $?
263 }
264
265 test_compile fast
266 test_compile small
267
268 test_use() {
269     run_sbcl --load $TEST_FILESTEM.$1.fasl --load $TEST_FILESTEM.test.lisp
270     check_status_maybe_lose "use $1" $? 22 "(load-shared-object not supported)"
271 }
272
273 test_use small
274 test_use fast
275
276 test_save() {
277     echo testing save $1 
278     run_sbcl --load $TEST_FILESTEM.$1.fasl <<EOF
279 #+linkage-table (save-lisp-and-die "$TEST_FILESTEM.$1.core")
280 #-linkage-table nil
281 (sb-ext:quit :unix-status 22) ; catch this
282 EOF
283     check_status_maybe_lose "save $1" $? \
284         0 "(successful save)" 22 "(linkage table not available)"
285 }
286
287 test_save small
288 test_save fast
289
290 test_start() {
291     echo testing start $1
292     run_sbcl_with_core $TEST_FILESTEM.$1.core \
293         --no-sysinit --no-userinit --load $TEST_FILESTEM.test.lisp
294     check_status_maybe_lose "start $1" $?
295 }
296
297 test_start fast
298 test_start small
299
300 # missing object file
301 rm $TEST_FILESTEM-b.so $TEST_FILESTEM-b2.so
302 run_sbcl_with_core $TEST_FILESTEM.fast.core --no-sysinit --no-userinit <<EOF
303   (assert (= 22 (summish 10 11)))
304   (multiple-value-bind (val err) (ignore-errors (eval 'foo))
305     (assert (not val))
306     (assert (typep err 'undefined-alien-error)))
307   (multiple-value-bind (val err) (ignore-errors (eval '(bar)))
308     (assert (not val))
309     (assert (typep err 'undefined-alien-error)))
310   (quit :unix-status $EXIT_LISP_WIN)
311 EOF
312 check_status_maybe_lose "missing-so" $?
313
314 # success convention for script
315 exit $EXIT_TEST_WIN