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