039d1210f576fc49d74d437b78c2fc4dbc0a0d5a
[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 echo //entering foreign.test.sh
18
19 # simple way to make sure we're not punting by accident:
20 # setting PUNT to anything other than 104 will make non-dlopen
21 # and non-linkage-table platforms fail this
22 PUNT=104
23
24 testfiledir=sbcl-foreign-test-$$
25 testfilestem=`pwd`/$testfiledir/sbcl-foreign-test
26
27 mkdir $testfiledir 
28 ## Make some shared object files to test with.
29
30 build_so() {
31   echo building $1.so
32   if [ "`uname -m`" = x86_64 -o "`uname -m`" = amd64 -o \
33        "`uname -m`" = mips -o "`uname -m`" = mips64 ]; then
34     CFLAGS="$CFLAGS -fPIC"
35   fi
36   if [ "`uname`" = Darwin ]; then
37     SO_FLAGS="-bundle"
38   else
39     SO_FLAGS="-shared"
40   fi
41   cc -c $1.c -o $1.o $CFLAGS
42   ld $SO_FLAGS -o $1.so $1.o  
43 }
44
45 cat > $testfilestem.c <<EOF
46 int summish(int x, int y) { return 1 + x + y; }
47
48 int numberish = 42;
49
50 int nummish(int x) { return numberish + x; }
51
52 short negative_short() { return -1; }
53 int negative_int()     { return -2; }
54 long negative_long()   { return -3; }
55
56 long long powish(unsigned int x, unsigned int y) {
57   long long acc = 1;
58   long long xx = (long long) x;
59   for(; y != 1; y /= 2) {
60     if (y & 1) {
61       acc *= xx;
62       y -= 1;
63     }
64     xx *= xx;
65   }
66   return xx*acc;
67 }
68
69 float return9th(float f1, float f2, float f3, float f4, float f5, 
70                 float f6, float f7, float f8, float f9, float f10, 
71                 float f11, float f12) { 
72     return f9; 
73 }
74
75 double return9thd(double f1, double f2, double f3, double f4, double f5, 
76                   double f6, double f7, double f8, double f9, double f10,
77                   double f11, double f12) { 
78     return f9; 
79 }
80
81 int long_test8(int a1, int a2, int a3, int a4, int a5, 
82                int a6, int a7, long long l1) { 
83     return (l1 == powish(2,34));
84 }
85
86 int long_test9(int a1, int a2, int a3, int a4, int a5, 
87                int a6, int a7, long long l1, int a8) { 
88     return (l1 == powish(2,35));
89 }
90
91 int long_test2(int i1, int i2, int i3, int i4, int i5, int i6,
92                int i7, int i8, int i9, long long l1, long long l2) {
93     return (l1 == (1 + powish(2,37)));
94 }
95
96 long long return_long_long() {
97     return powish(2,33);
98 }
99 EOF
100
101 build_so $testfilestem
102
103 echo 'int foo = 13;' > $testfilestem-b.c
104 echo 'int bar() { return 42; }' >> $testfilestem-b.c
105 build_so $testfilestem-b
106
107 echo 'int foo = 42;' > $testfilestem-b2.c
108 echo 'int bar() { return 13; }' >> $testfilestem-b2.c
109 build_so $testfilestem-b2
110
111 echo 'int late_foo = 43;' > $testfilestem-c.c
112 echo 'int late_bar() { return 14; }' >> $testfilestem-c.c
113 build_so $testfilestem-c
114
115 ## Foreign definitions & load
116
117 cat > $testfilestem.base.lisp <<EOF
118   (define-alien-variable environ (* c-string))
119   (defvar *environ* environ)
120   (eval-when (:compile-toplevel :load-toplevel :execute)
121     (handler-case
122         (progn
123           (load-shared-object "$testfilestem.so")
124           (load-shared-object "$testfilestem-b.so"))
125       (sb-int:unsupported-operator ()
126         ;; At least as of sbcl-0.7.0.5, LOAD-SHARED-OBJECT isn't
127         ;; supported on every OS. In that case, there's nothing to test,
128         ;; and we can just fall through to success.
129         (sb-ext:quit :unix-status 22)))) ; catch that
130   (define-alien-routine summish int (x int) (y int))
131   (define-alien-variable numberish int)
132   (define-alien-routine nummish int (x int))
133   (define-alien-variable "foo" int)
134   (define-alien-routine "bar" int)
135
136   (define-alien-routine "negative_short" short)
137   (define-alien-routine "negative_int" int)
138   (define-alien-routine "negative_long" long)
139
140   (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))
141   (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))
142
143   (define-alien-routine long-test8 int (int1 int) (int2 int) (int3 int) (int4 int) (int5 int) (int6 int) (int7 int) (long1 (integer 64)))
144   (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))
145   (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)))
146   (define-alien-routine return-long-long (integer 64))
147
148   ;; compiling this gets us the FOP-FOREIGN-DATAREF-FIXUP on
149   ;; linkage-table ports
150   (defvar *extern* (extern-alien "negative_short" short))
151
152   ;; Test that loading an object file didn't screw up our records
153   ;; of variables visible in runtime. (This was a bug until
154   ;; Nikodemus Siivola's patch in sbcl-0.8.5.50.)
155   ;;
156   ;; This cannot be tested in a saved core, as there is no guarantee
157   ;; that the location will be the same.
158   (assert (= (sb-sys:sap-int (alien-sap *environ*))
159              (sb-sys:sap-int (alien-sap environ))))
160
161   ;; automagic restarts
162   (setf *invoke-debugger-hook*
163         (lambda (condition hook)
164           (princ condition)
165           (let ((cont (find-restart 'continue condition)))
166             (when cont
167               (invoke-restart cont)))
168           (print :fell-through)
169           (invoke-debugger condition)))
170 EOF
171
172 echo "(declaim (optimize speed))" > $testfilestem.fast.lisp
173 cat $testfilestem.base.lisp >> $testfilestem.fast.lisp
174
175 echo "(declaim (optimize space))" > $testfilestem.small.lisp
176 cat $testfilestem.base.lisp >> $testfilestem.small.lisp
177
178 # Test code
179 cat > $testfilestem.test.lisp <<EOF
180   ;; FIXME: currently the start/small case fails on x86/Darwin. Moving
181   ;; this NOTE definition to the base.lisp file fixes that, but obviously
182   ;; it is better fo figure out what is going on instead of doing that...
183   ;;
184   ;; Other trivialish changes that mask the error include:
185   ;; * loading the .lisp file instead of the .fasl at the save test
186   ;; * --eval 'nil' before loading the .fasl at the save test
187   ;;
188   ;; HATE.
189   (defun note (x)
190      (write-line x *standard-output*)
191      (force-output *standard-output*))
192   (note "/initial assertions")
193   (assert (= 31 (summish 10 20)))
194   (assert (= 42 numberish))
195   (setf numberish 13)
196   (assert (= 13 numberish))
197   (assert (= 14 (nummish 1)))
198
199   (assert (= -1 (negative-short)))
200   (assert (= -2 (negative-int)))
201   (assert (= -3 (negative-long)))
202
203   (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)))
204   (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)))
205
206   (assert (= 1 (long-test8 1 2 3 4 5 6 7 (ash 1 34))))
207   (assert (= 1 (long-test9 1 2 3 4 5 6 7 (ash 1 35) 8)))
208   (assert (= 1 (long-test2 1 2 3 4 5 6 7 8 9 (+ 1 (ash 1 37)) 15)))
209   (assert (= (ash 1 33) (return-long-long)))
210
211   (note "/initial assertions ok")
212
213   ;; test reloading object file with new definitions
214   (assert (= 13 foo))
215   (assert (= 42 (bar)))
216   (note "/original definitions ok")
217   (rename-file "$testfilestem-b.so" "$testfilestem-b.bak")
218   (rename-file "$testfilestem-b2.so" "$testfilestem-b.so")
219   (load-shared-object "$testfilestem-b.so")
220   (note "/reloading ok")
221   (assert (= 42 foo))
222   (assert (= 13 (bar)))
223   (note "/redefined versions ok")
224   (rename-file "$testfilestem-b.so" "$testfilestem-b2.so")
225   (rename-file "$testfilestem-b.bak" "$testfilestem-b.so")
226   (note "/renamed back to originals")
227
228   ;; test late resolution
229   #+linkage-table
230   (progn
231     (note "/starting linkage table tests")
232     (define-alien-variable late-foo int)
233     (define-alien-routine late-bar int)
234     (multiple-value-bind (val err) (ignore-errors late-foo)
235       (assert (not val))
236       (assert (typep err 'undefined-alien-error)))
237     (multiple-value-bind (val err) (ignore-errors (late-bar))
238       (assert (not val))
239       (assert (typep err 'undefined-alien-error)))
240     (load-shared-object "$testfilestem-c.so")
241     (assert (= 43 late-foo))
242     (assert (= 14 (late-bar)))
243     (note "/linkage table ok"))
244
245   (sb-ext:quit :unix-status 52) ; success convention for Lisp program
246 EOF
247
248 test_compile() {
249     ${SBCL:-sbcl} --eval "(progn (load (compile-file #p\"$testfilestem.$1.lisp\")) (sb-ext:quit :unix-status 52))"
250     if [ $? = 52 ]; then
251         echo test compile $1 ok
252     else
253         # we can't compile the test file. something's wrong.
254         # rm $testfilestem.*
255         echo test compile $1 failed: $?
256         exit 1
257     fi
258 }
259
260 test_compile fast
261 test_compile small
262
263 test_use() {
264     ${SBCL:-sbcl} --load $testfilestem.$1.fasl --load $testfilestem.test.lisp
265     RET=$?
266     if [ $RET = 22 ]; then
267         rm $testfilestem.*
268         exit $PUNT # success -- load-shared-object not supported
269     elif [ $RET != 52 ]; then
270         rm $testfilestem.*
271         echo test use $1 failed: $?
272         exit 1
273     else
274         echo test use $1 ok
275     fi
276 }
277
278 test_use small
279 test_use fast
280
281 test_save() {
282     echo testing save $1 
283     ${SBCL:-sbcl} --load $testfilestem.$1.fasl --eval "#+linkage-table (save-lisp-and-die \"$testfilestem.$1.core\") #-linkage-table nil" <<EOF
284   (sb-ext:quit :unix-status 22) ; catch this
285 EOF
286     if [ $? = 22 ]; then
287         rm $testfilestem.*
288         exit $PUNT # success -- linkage-table not available
289     else
290         echo save $1 ok
291     fi
292 }
293
294 test_save small
295 test_save fast
296
297 test_start() {
298     echo testing start $1
299     ${SBCL_ALLOWING_CORE:-sbcl} --core $testfilestem.$1.core --sysinit /dev/null --userinit /dev/null --load $testfilestem.test.lisp
300     if [ $? != 52 ]; then
301         rm $testfilestem.*
302         echo test failed: $?
303         exit 1 # Failure
304     else
305         echo test start $1 ok
306     fi
307 }
308
309 test_start fast
310 test_start small
311
312 # missing object file
313 rm $testfilestem-b.so $testfilestem-b2.so
314 ${SBCL_ALLOWING_CORE:-sbcl} --core $testfilestem.fast.core --sysinit /dev/null --userinit /dev/null <<EOF
315   (assert (= 22 (summish 10 11)))
316   (multiple-value-bind (val err) (ignore-errors (eval 'foo))
317     (assert (not val))
318     (assert (typep err 'undefined-alien-error)))
319   (multiple-value-bind (val err) (ignore-errors (eval '(bar)))
320     (assert (not val))
321     (assert (typep err 'undefined-alien-error)))
322   (quit :unix-status 52)
323 EOF
324 if [ $? != 52 ]; then
325     rm $testfilestem.*
326     echo test failed: $?
327     exit 1 # Failure
328 fi
329
330 echo missing .so ok
331
332 rm -r $testfiledir 
333
334 # success convention for script
335 exit 104