0.9.18.61:
[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 ]; then
31     CFLAGS="$CFLAGS -fPIC"
32   fi
33   if [ "`uname`" = Darwin ]; then
34     SO_FLAGS="-bundle"
35   else
36     SO_FLAGS="-shared"
37   fi
38   cc -c $1.c -o $1.o $CFLAGS
39   ld $SO_FLAGS -o $1.so $1.o  
40 }
41
42 cat > $testfilestem.c <<EOF
43 int summish(int x, int y) { return 1 + x + y; }
44
45 int numberish = 42;
46
47 int nummish(int x) { return numberish + x; }
48
49 short negative_short() { return -1; }
50 int negative_int()     { return -2; }
51 long negative_long()   { return -3; }
52
53 long long powish(unsigned int x, unsigned int y) {
54   long long acc = 1;
55   long long xx = (long long) x;
56   for(; y != 1; y /= 2) {
57     if (y & 1) {
58       acc *= xx;
59       y -= 1;
60     }
61     xx *= xx;
62   }
63   return xx*acc;
64 }
65
66 float return9th(float f1, float f2, float f3, float f4, float f5, 
67                 float f6, float f7, float f8, float f9, float f10, 
68                 float f11, float f12) { 
69     return f9; 
70 }
71
72 double return9thd(double f1, double f2, double f3, double f4, double f5, 
73                   double f6, double f7, double f8, double f9, double f10,
74                   double f11, double f12) { 
75     return f9; 
76 }
77
78 int long_test8(int a1, int a2, int a3, int a4, int a5, 
79                int a6, int a7, long long l1) { 
80     return (l1 == powish(2,34));
81 }
82
83 int long_test9(int a1, int a2, int a3, int a4, int a5, 
84                int a6, int a7, long long l1, int a8) { 
85     return (l1 == powish(2,35));
86 }
87
88 int long_test2(int i1, int i2, int i3, int i4, int i5, int i6,
89                int i7, int i8, int i9, long long l1, long long l2) {
90     return (l1 == (1 + powish(2,37)));
91 }
92
93 long long return_long_long() {
94     return powish(2,33);
95 }
96 EOF
97
98 build_so $testfilestem
99
100 echo 'int foo = 13;' > $testfilestem-b.c
101 echo 'int bar() { return 42; }' >> $testfilestem-b.c
102 build_so $testfilestem-b
103
104 echo 'int foo = 42;' > $testfilestem-b2.c
105 echo 'int bar() { return 13; }' >> $testfilestem-b2.c
106 build_so $testfilestem-b2
107
108 echo 'int late_foo = 43;' > $testfilestem-c.c
109 echo 'int late_bar() { return 14; }' >> $testfilestem-c.c
110 build_so $testfilestem-c
111
112 ## Foreign definitions & load
113
114 cat > $testfilestem.def.lisp <<EOF
115   (define-alien-variable environ (* c-string))
116   (defvar *environ* environ)
117   (eval-when (:compile-toplevel :load-toplevel :execute)
118     (handler-case
119         (progn
120           (load-shared-object "$testfilestem.so")
121           (load-shared-object "$testfilestem-b.so"))
122       (sb-int:unsupported-operator ()
123         ;; At least as of sbcl-0.7.0.5, LOAD-SHARED-OBJECT isn't
124         ;; supported on every OS. In that case, there's nothing to test,
125         ;; and we can just fall through to success.
126         (sb-ext:quit :unix-status 22)))) ; catch that
127   (define-alien-routine summish int (x int) (y int))
128   (define-alien-variable numberish int)
129   (define-alien-routine nummish int (x int))
130   (define-alien-variable "foo" int)
131   (define-alien-routine "bar" int)
132
133   (define-alien-routine "negative_short" short)
134   (define-alien-routine "negative_int" int)
135   (define-alien-routine "negative_long" long)
136
137   (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))
138   (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))
139
140   (define-alien-routine long-test8 int (int1 int) (int2 int) (int3 int) (int4 int) (int5 int) (int6 int) (int7 int) (long1 (integer 64)))
141   (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))
142   (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)))
143   (define-alien-routine return-long-long (integer 64))
144
145   ;; compiling this gets us the FOP-FOREIGN-DATAREF-FIXUP on
146   ;; linkage-table ports
147   (defvar *extern* (extern-alien "negative_short" short))
148
149   ;; Test that loading an object file didn't screw up our records
150   ;; of variables visible in runtime. (This was a bug until
151   ;; Nikodemus Siivola's patch in sbcl-0.8.5.50.)
152   ;;
153   ;; This cannot be tested in a saved core, as there is no guarantee
154   ;; that the location will be the same.
155   (assert (= (sb-sys:sap-int (alien-sap *environ*))
156              (sb-sys:sap-int (alien-sap environ))))
157
158   (enable-debugger)
159   ;; automagic restarts
160   (setf *debugger-hook*
161         (lambda (condition hook)
162           (print (list :debugger-hook 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 # Test code
171 cat > $testfilestem.test.lisp <<EOF
172   (assert (= (summish 10 20) 31))
173   (assert (= 42 numberish))
174   (setf numberish 13)
175   (assert (= 13 numberish))
176   (assert (= 14 (nummish 1)))
177
178   (assert (= -1 (negative-short)))
179   (assert (= -2 (negative-int)))
180   (assert (= -3 (negative-long)))
181
182   (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)))
183   (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)))
184
185   (assert (= 1 (long-test8 1 2 3 4 5 6 7 (ash 1 34))))
186   (assert (= 1 (long-test9 1 2 3 4 5 6 7 (ash 1 35) 8)))
187   (assert (= 1 (long-test2 1 2 3 4 5 6 7 8 9 (+ 1 (ash 1 37)) 15)))
188   (assert (= (ash 1 33) (return-long-long)))
189
190   (print :stage-1)
191
192   ;; test reloading object file with new definitions
193   (assert (= 13 foo))
194   (assert (= 42 (bar)))
195   (rename-file "$testfilestem-b.so" "$testfilestem-b.bak")
196   (rename-file "$testfilestem-b2.so" "$testfilestem-b.so")
197   (load-shared-object "$testfilestem-b.so")
198   (assert (= 42 foo))
199   (assert (= 13 (bar)))
200   (rename-file "$testfilestem-b.so" "$testfilestem-b2.so")
201   (rename-file "$testfilestem-b.bak" "$testfilestem-b.so")
202
203   (print :stage-2)
204
205   ;; test late resolution
206   #+linkage-table
207   (progn
208     (define-alien-variable late-foo int)
209     (define-alien-routine late-bar int)
210     (multiple-value-bind (val err) (ignore-errors late-foo)
211       (assert (not val))
212       (assert (typep err 'undefined-alien-error)))
213     (multiple-value-bind (val err) (ignore-errors (late-bar))
214       (assert (not val))
215       (assert (typep err 'undefined-alien-error)))
216     (load-shared-object "$testfilestem-c.so")
217     (assert (= 43 late-foo))
218     (assert (= 14 (late-bar))))
219
220   (print :stage-3)
221
222   (sb-ext:quit :unix-status 52) ; success convention for Lisp program
223 EOF
224
225 ${SBCL:-sbcl} --eval "(progn (load (compile-file #p\"$testfilestem.def.lisp\")) (sb-ext:quit :unix-status 52))"
226 if [ $? = 52 ]; then
227     true # nop
228 else
229     # we can't compile the test file. something's wrong.
230     # rm $testfilestem.*
231     echo test failed: $?
232     exit 1
233 fi
234
235 echo compile ok
236
237 ${SBCL:-sbcl} --load $testfilestem.def.fasl --load $testfilestem.test.lisp
238 RET=$?
239 if [ $RET = 22 ]; then
240     rm $testfilestem.*
241     exit $PUNT # success -- load-shared-object not supported
242 elif [ $RET != 52 ]; then
243     rm $testfilestem.*
244     echo test failed: $?
245     exit 1
246 fi
247
248 echo load ok
249
250 ${SBCL:-sbcl} --load $testfilestem.def.fasl --eval "(when (member :linkage-table *features*) (save-lisp-and-die \"$testfilestem.core\"))" <<EOF
251   (sb-ext:quit :unix-status 22) ; catch this
252 EOF
253 if [ $? = 22 ]; then
254     rm $testfilestem.*
255     exit $PUNT # success -- linkage-table not available
256 fi
257
258 echo table ok
259
260 ${SBCL_ALLOWING_CORE:-sbcl} --core $testfilestem.core --sysinit /dev/null --userinit /dev/null --load $testfilestem.test.lisp
261 if [ $? != 52 ]; then
262     rm $testfilestem.*
263     echo test failed: $?
264     exit 1 # Failure
265 fi
266
267 echo start ok
268
269 # missing object file
270 rm $testfilestem-b.so $testfilestem-b2.so
271 ${SBCL_ALLOWING_CORE:-sbcl} --core $testfilestem.core --sysinit /dev/null --userinit /dev/null <<EOF
272   (assert (= 22 (summish 10 11)))
273   (multiple-value-bind (val err) (ignore-errors (eval 'foo))
274     (assert (not val))
275     (assert (typep err 'undefined-alien-error)))
276   (multiple-value-bind (val err) (ignore-errors (eval '(bar)))
277     (assert (not val))
278     (assert (typep err 'undefined-alien-error)))
279   (quit :unix-status 52)
280 EOF
281 if [ $? != 52 ]; then
282     rm $testfilestem.*
283     echo test failed: $?
284     exit 1 # Failure
285 fi
286
287 echo missing ok
288
289 rm -f $testfilestem.* $testfilestem-*
290
291 # success convention for script
292 exit 104