0.9.11.19:
[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 echo 'int summish(int x, int y) { return 1 + x + y; }' > $testfilestem.c
43 echo 'int numberish = 42;' >> $testfilestem.c
44 echo 'int nummish(int x) { return numberish + x; }' >> $testfilestem.c
45 echo 'short negative_short() { return -1; }' >> $testfilestem.c
46 echo 'int negative_int()     { return -2; }' >> $testfilestem.c
47 echo 'long negative_long()   { return -3; }' >> $testfilestem.c
48 build_so $testfilestem
49
50 echo 'int foo = 13;' > $testfilestem-b.c
51 echo 'int bar() { return 42; }' >> $testfilestem-b.c
52 build_so $testfilestem-b
53
54 echo 'int foo = 42;' > $testfilestem-b2.c
55 echo 'int bar() { return 13; }' >> $testfilestem-b2.c
56 build_so $testfilestem-b2
57
58 echo 'int late_foo = 43;' > $testfilestem-c.c
59 echo 'int late_bar() { return 14; }' >> $testfilestem-c.c
60 build_so $testfilestem-c
61
62 ## Foreign definitions & load
63
64 cat > $testfilestem.def.lisp <<EOF
65   (define-alien-variable environ (* c-string))
66   (defvar *environ* environ)
67   (eval-when (:compile-toplevel :load-toplevel :execute)
68     (handler-case
69         (progn
70           (load-shared-object "$testfilestem.so")
71           (load-shared-object "$testfilestem-b.so"))
72       (sb-int:unsupported-operator ()
73         ;; At least as of sbcl-0.7.0.5, LOAD-SHARED-OBJECT isn't
74         ;; supported on every OS. In that case, there's nothing to test,
75         ;; and we can just fall through to success.
76         (sb-ext:quit :unix-status 22)))) ; catch that
77   (define-alien-routine summish int (x int) (y int))
78   (define-alien-variable numberish int)
79   (define-alien-routine nummish int (x int))
80   (define-alien-variable "foo" int)
81   (define-alien-routine "bar" int)
82
83   (define-alien-routine "negative_short" short)
84   (define-alien-routine "negative_int" int)
85   (define-alien-routine "negative_long" long)
86
87   ;; compiling this gets us the FOP-FOREIGN-DATAREF-FIXUP on
88   ;; linkage-table ports
89   (defvar *extern* (extern-alien "negative_short" short))
90
91   ;; Test that loading an object file didn't screw up our records
92   ;; of variables visible in runtime. (This was a bug until
93   ;; Nikodemus Siivola's patch in sbcl-0.8.5.50.)
94   ;;
95   ;; This cannot be tested in a saved core, as there is no guarantee
96   ;; that the location will be the same.
97   (assert (= (sb-sys:sap-int (alien-sap *environ*))
98              (sb-sys:sap-int (alien-sap environ))))
99
100   (enable-debugger)
101   ;; automagic restarts
102   (setf *debugger-hook*
103         (lambda (condition hook)
104           (print (list :debugger-hook condition))
105           (let ((cont (find-restart 'continue condition)))
106             (when cont
107               (invoke-restart cont)))
108           (print :fell-through)
109           (invoke-debugger condition)))
110 EOF
111
112 # Test code
113 cat > $testfilestem.test.lisp <<EOF
114   (assert (= (summish 10 20) 31))
115   (assert (= 42 numberish))
116   (setf numberish 13)
117   (assert (= 13 numberish))
118   (assert (= 14 (nummish 1)))
119
120   (assert (= -1 (negative-short)))
121   (assert (= -2 (negative-int)))
122   (assert (= -3 (negative-long)))
123
124   (print :stage-1)
125
126   ;; test reloading object file with new definitions
127   (assert (= 13 foo))
128   (assert (= 42 (bar)))
129   (rename-file "$testfilestem-b.so" "$testfilestem-b.bak")
130   (rename-file "$testfilestem-b2.so" "$testfilestem-b.so")
131   (load-shared-object "$testfilestem-b.so")
132   (assert (= 42 foo))
133   (assert (= 13 (bar)))
134   (rename-file "$testfilestem-b.so" "$testfilestem-b2.so")
135   (rename-file "$testfilestem-b.bak" "$testfilestem-b.so")
136
137   (print :stage-2)
138
139   ;; test late resolution
140   #+linkage-table
141   (progn
142     (define-alien-variable late-foo int)
143     (define-alien-routine late-bar int)
144     (multiple-value-bind (val err) (ignore-errors late-foo)
145       (assert (not val))
146       (assert (typep err 'undefined-alien-error)))
147     (multiple-value-bind (val err) (ignore-errors (late-bar))
148       (assert (not val))
149       (assert (typep err 'undefined-alien-error)))
150     (load-shared-object "$testfilestem-c.so")
151     (assert (= 43 late-foo))
152     (assert (= 14 (late-bar))))
153
154   (print :stage-3)
155
156   (sb-ext:quit :unix-status 52) ; success convention for Lisp program
157 EOF
158
159 ${SBCL:-sbcl} --eval "(progn (load (compile-file #p\"$testfilestem.def.lisp\")) (sb-ext:quit :unix-status 52))"
160 if [ $? = 52 ]; then
161     true # nop
162 else
163     # we can't compile the test file. something's wrong.
164     rm $testfilestem.*
165     echo test failed: $?
166     exit 1
167 fi
168
169 echo compile ok
170
171 ${SBCL:-sbcl} --load $testfilestem.def.fasl --load $testfilestem.test.lisp
172 RET=$?
173 if [ $RET = 22 ]; then
174     rm $testfilestem.*
175     exit $PUNT # success -- load-shared-object not supported
176 elif [ $RET != 52 ]; then
177     rm $testfilestem.*
178     echo test failed: $?
179     exit 1
180 fi
181
182 echo load ok
183
184 ${SBCL:-sbcl} --load $testfilestem.def.fasl --eval "(when (member :linkage-table *features*) (save-lisp-and-die \"$testfilestem.core\"))" <<EOF
185   (sb-ext:quit :unix-status 22) ; catch this
186 EOF
187 if [ $? = 22 ]; then
188     rm $testfilestem.*
189     exit $PUNT # success -- linkage-table not available
190 fi
191
192 echo table ok
193
194 ${SBCL_ALLOWING_CORE:-sbcl} --core $testfilestem.core --sysinit /dev/null --userinit /dev/null --load $testfilestem.test.lisp
195 if [ $? != 52 ]; then
196     rm $testfilestem.*
197     echo test failed: $?
198     exit 1 # Failure
199 fi
200
201 echo start ok
202
203 # missing object file
204 rm $testfilestem-b.so $testfilestem-b2.so
205 ${SBCL_ALLOWING_CORE:-sbcl} --core $testfilestem.core --sysinit /dev/null --userinit /dev/null <<EOF
206   (assert (= 22 (summish 10 11)))
207   (multiple-value-bind (val err) (ignore-errors (eval 'foo))
208     (assert (not val))
209     (assert (typep err 'undefined-alien-error)))
210   (multiple-value-bind (val err) (ignore-errors (eval '(bar)))
211     (assert (not val))
212     (assert (typep err 'undefined-alien-error)))
213   (quit :unix-status 52)
214 EOF
215 if [ $? != 52 ]; then
216     rm $testfilestem.*
217     echo test failed: $?
218     exit 1 # Failure
219 fi
220
221 echo missing ok
222
223 rm -f $testfilestem.* $testfilestem-*
224
225 # success convention for script
226 exit 104