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