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