Produce only one warning for (typep x 'bad-type)
[sbcl.git] / tests / foreign-stack-alignment.impure.lisp
blobe033f9e42cfffd2b8abe7046d6676c285160a3d7
1 ;;;; Testing the stack alignment of foreign calls. Uses stack-alignment-offset.c.
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;;
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (use-package :sb-alien)
16 ;;; Callbacks are not part of the exported interface yet -- when they are this can
17 ;;; go away.
18 (import 'sb-alien::alien-lambda)
20 (defun run (program &rest arguments)
21 (let* ((proc nil)
22 (output
23 (with-output-to-string (s)
24 (setf proc (run-program program arguments
25 :output s)))))
26 (unless (zerop (process-exit-code proc))
27 (error "Bad exit code: ~S~%Output:~% ~S"
28 (process-exit-code proc)
29 output))
30 output))
32 (defvar *required-alignment*
33 #+arm 8
34 #+mips 8
35 #+(and ppc darwin) 16
36 #+(and ppc (not darwin)) 8
37 #+(or arm64 x86 x86-64) 16
38 #+sparc 8
39 #+alpha 16
40 #+hppa 64
41 #-(or arm arm64 x86 x86-64 mips ppc sparc alpha hppa)
42 (error "Unknown platform"))
44 ;;;; Build the offset-tool as regular excutable, and run it with
45 ;;;; fork/exec, so that no lisp is on the stack. This is our known-good
46 ;;;; number.
48 #-win32
49 (progn
50 (run "/bin/sh" "run-compiler.sh" "-sbcl-pic"
51 "stack-alignment-offset.c" "-o" "stack-alignment-offset")
53 (defparameter *good-offset*
54 (parse-integer (run "./stack-alignment-offset"
55 (princ-to-string *required-alignment*))))
57 ;; Build the tool again, this time as a shared object, and load it
59 (run "/bin/sh" "run-compiler.sh" "-sbcl-pic" "-sbcl-shared"
60 "stack-alignment-offset.c" "-o" "stack-alignment-offset.so")
62 (load-shared-object (truename "stack-alignment-offset.so"))
64 (define-alien-routine stack-alignment-offset int (alignment int))
65 #+alien-callbacks
66 (define-alien-routine trampoline int (callback (function int))))
68 ;;;; Now get the offset by calling from lisp, first with a regular foreign function
69 ;;;; call, then with an intervening callback.
71 (with-test (:name :regular :fails-on :win32)
72 (assert (= *good-offset* (stack-alignment-offset *required-alignment*))))
74 #+alien-callbacks
75 (with-test (:name :callback :fails-on :win32)
76 (assert (= *good-offset*
77 (trampoline (alien-lambda int ()
78 (stack-alignment-offset *required-alignment*))))))
80 (when (probe-file "stack-alignment-offset.so")
81 (delete-file "stack-alignment-offset")
82 (delete-file "stack-alignment-offset.so"))
84 ;;;; success!