0.9.6.26: preserve stack alignment in callbacks on ppc
[sbcl.git] / tests / foreign-stack-alignment.impure.lisp
1 ;;;; Testing the stack alignment of foreign calls. Uses stack-alignment-offset.c.
2
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.
13
14 (use-package :sb-alien)
15
16 ;;; Callbacks are not part of the exported interface yet -- when they are this can
17 ;;; go away.
18 (import 'sb-alien::alien-lambda)
19
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                                     :search (not (eql #\. (char program 0)))
26                                     :output s)))))
27     (unless (zerop (process-exit-code proc))
28       (error "Bad exit code: ~S~%Output:~% ~S"
29              (process-exit-code proc)
30              output))
31     output))
32
33 (defvar *required-alignment*
34   #+(and ppc darwin) 16
35   #+(and ppc linux) 16
36   #+(or mips x86-64) 8
37   #+x86 4
38   #-(or x86 x86-64 mips (and ppc (or darwin linux))) (error "Unknown platform"))
39
40 ;;;; Build the offset-tool as regular excutable, and run it with
41 ;;;; fork/exec, so that no lisp is on the stack. This is our known-good
42 ;;;; number.
43
44 (run "cc" "stack-alignment-offset.c" "-o" "stack-alignment-offset")
45
46 (defparameter *good-offset*
47   (parse-integer (run "./stack-alignment-offset"
48                       (princ-to-string *required-alignment*))))
49
50 ;;;; Build the tool again, this time as a shared object, and load it
51
52 (run "cc" "stack-alignment-offset.c"
53      #+darwin "-bundle" #-darwin "-shared"
54      "-o" "stack-alignment-offset.so")
55
56 (load-shared-object "stack-alignment-offset.so")
57
58 (define-alien-routine stack-alignment-offset int (alignment int))
59 (define-alien-routine trampoline int (callback (function int)))
60
61 ;;;; Now get the offset by calling from lisp, first with a regular foreign function
62 ;;;; call, then with an intervening callback.
63
64 (assert (= *good-offset* (stack-alignment-offset *required-alignment*)))
65
66 (assert (= *good-offset* (trampoline (alien-lambda int ()
67                                        (stack-alignment-offset *required-alignment*)))))
68
69 ;;;; success!