0.8.17.1: reloading shared object files
[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 echo 'int summish(int x, int y) { return 1 + x + y; }' > $testfilestem.c
29 echo 'int numberish = 42;' >> $testfilestem.c
30 echo 'int nummish(int x) { return numberish + x; }' >> $testfilestem.c
31 cc -c $testfilestem.c -o $testfilestem.o
32 ld -shared -o $testfilestem.so $testfilestem.o
33
34 echo 'int foo = 13;' > $testfilestem-foobar.c
35 echo 'int bar() { return 42; }' >> $testfilestem-foobar.c
36 cc -c $testfilestem-foobar.c -o $testfilestem-foobar.o
37 ld -shared -o $testfilestem-foobar.so $testfilestem-foobar.o
38
39 echo 'int foo = 42;' > $testfilestem-foobar2.c
40 echo 'int bar() { return 13; }' >> $testfilestem-foobar2.c
41 cc -c $testfilestem-foobar2.c -o $testfilestem-foobar2.o
42 ld -shared -o $testfilestem-foobar2.so $testfilestem-foobar2.o
43
44 ## Foreign definitions & load
45
46 cat > $testfilestem.def.lisp <<EOF
47   (define-alien-variable environ (* c-string))
48   (defvar *environ* environ)
49   (eval-when (:compile-toplevel :load-toplevel :execute)
50     (handler-case
51         (progn
52           (load-shared-object "$testfilestem.so")
53           (load-shared-object "$testfilestem-foobar.so"))
54       (sb-int:unsupported-operator ()
55         ;; At least as of sbcl-0.7.0.5, LOAD-SHARED-OBJECT isn't
56         ;; supported on every OS. In that case, there's nothing to test,
57         ;; and we can just fall through to success.
58         (sb-ext:quit :unix-status 22)))) ; catch that
59   (define-alien-routine summish int (x int) (y int))
60   (define-alien-variable numberish int)
61   (define-alien-routine nummish int (x int))
62   (define-alien-variable "foo" int)
63   (define-alien-routine "bar" int)
64
65   ;; Test that loading an object file didn't screw up our records
66   ;; of variables visible in runtime. (This was a bug until 
67   ;; Nikodemus Siivola's patch in sbcl-0.8.5.50.)
68   ;;
69   ;; This cannot be tested in a saved core, as there is no guarantee
70   ;; that the location will be the same.
71   (assert (= (sb-sys:sap-int (alien-sap *environ*))
72              (sb-sys:sap-int (alien-sap environ))))
73 EOF
74
75 # Test code
76 cat > $testfilestem.test.lisp <<EOF
77   (assert (= (summish 10 20) 31))
78   (assert (= 42 numberish))
79   (setf numberish 13)
80   (assert (= 13 numberish))
81   (assert (= 14 (nummish 1)))
82
83   (assert (= 13 foo))
84   (assert (= 42 (bar)))
85   ;; test realoading object file with new definitions
86   (rename-file "$testfilestem-foobar.so" "$testfilestem-foobar.bak")
87   (rename-file "$testfilestem-foobar2.so" "$testfilestem-foobar.so")
88   (load-shared-object "$testfilestem-foobar.so")
89   (assert (= 42 foo))
90   (assert (= 13 (bar)))
91   (rename-file "$testfilestem-foobar.so" "$testfilestem-foobar2.so")
92   (rename-file "$testfilestem-foobar.bak" "$testfilestem-foobar.so")
93
94   (sb-ext:quit :unix-status 52) ; success convention for Lisp program
95 EOF
96
97 ${SBCL:-sbcl} --eval "(progn (compile-file #p\"$testfilestem.def.lisp\") (sb-ext:quit :unix-status 52))"
98 if [ $? = 52 ] ; then :
99 else
100     # we can't compile the test file. something's wrong.
101     rm $testfilestem.*
102     echo test failed: $?
103     exit 1
104 fi
105
106 ${SBCL:-sbcl} --load $testfilestem.def.fasl --load $testfilestem.test.lisp
107 RET=$?
108 if [ $RET = 22 ]; then
109     rm $testfilestem.*
110     exit $PUNT # success -- load-shared-object not supported
111 elif [ $RET != 52 ]; then
112     rm $testfilestem.*
113     echo test failed: $?
114     exit 1 
115 fi
116
117 ${SBCL:-sbcl} --load $testfilestem.def.fasl --eval "(when (member :linkage-table *features*) (save-lisp-and-die \"$testfilestem.core\"))" <<EOF
118   (sb-ext:quit :unix-status 22) ; catch this
119 EOF
120 if [ $? = 22 ]; then
121     rm $testfilestem.*
122     exit $PUNT # success -- linkage-table not available
123 fi
124
125 $SBCL_ALLOWING_CORE --core $testfilestem.core --sysinit /dev/null --userinit /dev/null --load $testfilestem.test.lisp
126 if [ $? != 52 ]; then
127     rm $testfilestem.*
128     echo test failed: $?
129     exit 1 # Failure
130 fi
131
132 rm $testfilestem.*
133
134 # success convention for script
135 exit 104