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