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
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
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
18 (import 'sb-alien::alien-lambda)
20 (defun run (program &rest arguments)
23 (with-output-to-string (s)
24 (setf proc (run-program program arguments
25 :search (not (eql #\. (char program 0)))
27 (unless (zerop (process-exit-code proc))
28 (error "Bad exit code: ~S~%Output:~% ~S"
29 (process-exit-code proc)
33 (defvar *required-alignment*
38 #+(and x86 (not darwin)) 4
40 #-(or x86 x86-64 mips (and ppc (or darwin linux))) (error "Unknown platform"))
42 ;;;; Build the offset-tool as regular excutable, and run it with
43 ;;;; fork/exec, so that no lisp is on the stack. This is our known-good
47 #+(and (or linux freebsd) (or x86-64 ppc mips)) "-fPIC"
48 #+(and x86-64 darwin) "-arch" #+(and x86-64 darwin) "x86_64"
49 "stack-alignment-offset.c" "-o" "stack-alignment-offset")
51 (defparameter *good-offset*
52 (parse-integer (run "./stack-alignment-offset"
53 (princ-to-string *required-alignment*))))
55 ;;;; Build the tool again, this time as a shared object, and load it
57 (run "cc" "stack-alignment-offset.c"
58 #+(and (or linux freebsd) (or x86-64 ppc mips)) "-fPIC"
59 #+(and x86-64 darwin) "-arch" #+(and x86-64 darwin) "x86_64"
60 #+darwin "-bundle" #-darwin "-shared"
61 "-o" "stack-alignment-offset.so")
63 (load-shared-object "stack-alignment-offset.so")
65 (define-alien-routine stack-alignment-offset int (alignment int))
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)
72 (assert (= *good-offset* (stack-alignment-offset *required-alignment*))))
74 (with-test (:name :callback)
75 (assert (= *good-offset* (trampoline (alien-lambda int ()
76 (stack-alignment-offset *required-alignment*))))))
78 (delete-file "stack-alignment-offset")
79 (delete-file "stack-alignment-offset.so")