File: foreign-stack-alignment.impure.lisp

package info (click to toggle)
sbcl 1%3A0.9.16.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 19,960 kB
  • ctags: 16,537
  • sloc: lisp: 231,164; ansic: 19,558; asm: 2,539; sh: 1,925; makefile: 308
file content (75 lines) | stat: -rw-r--r-- 2,515 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
;;;; Testing the stack alignment of foreign calls. Uses stack-alignment-offset.c.

;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; While most of SBCL is derived from the CMU CL system, the test
;;;; files (like this one) were written from scratch after the fork
;;;; from CMU CL.
;;;;
;;;; This software is in the public domain and is provided with
;;;; absolutely no warranty. See the COPYING and CREDITS files for
;;;; more information.

(use-package :sb-alien)

;;; Callbacks are not part of the exported interface yet -- when they are this can
;;; go away.
(import 'sb-alien::alien-lambda)

(defun run (program &rest arguments)
  (let* ((proc nil)
         (output
          (with-output-to-string (s)
            (setf proc (run-program program arguments
                                    :search (not (eql #\. (char program 0)))
                                    :output s)))))
    (unless (zerop (process-exit-code proc))
      (error "Bad exit code: ~S~%Output:~% ~S"
             (process-exit-code proc)
             output))
    output))

(defvar *required-alignment*
  #+(and ppc darwin) 16
  #+(and ppc linux) 16
  #+x86-64 16
  #+mips 8
  #+x86 4
  #-(or x86 x86-64 mips (and ppc (or darwin linux))) (error "Unknown platform"))

;;;; Build the offset-tool as regular excutable, and run it with
;;;; fork/exec, so that no lisp is on the stack. This is our known-good
;;;; number.

(run "cc"
     #+x86-64 "-fPIC"
     "stack-alignment-offset.c" "-o" "stack-alignment-offset")

(defparameter *good-offset*
  (parse-integer (run "./stack-alignment-offset"
                      (princ-to-string *required-alignment*))))

;;;; Build the tool again, this time as a shared object, and load it

(run "cc" "stack-alignment-offset.c"
     #+x86-64 "-fPIC"
     #+darwin "-bundle" #-darwin "-shared"
     "-o" "stack-alignment-offset.so")

(load-shared-object "stack-alignment-offset.so")

(define-alien-routine stack-alignment-offset int (alignment int))
(define-alien-routine trampoline int (callback (function int)))

;;;; Now get the offset by calling from lisp, first with a regular foreign function
;;;; call, then with an intervening callback.

(with-test (:name :regular)
  (assert (= *good-offset* (stack-alignment-offset *required-alignment*))))

(with-test (:name :callback)
  (assert (= *good-offset* (trampoline (alien-lambda int ()
                                       (stack-alignment-offset *required-alignment*))))))

;;;; success!