[cffi-devel] Re: [Sbcl-devel] Re: CFFI Callbacks on SBCL

Juho Snellman jsnell at iki.fi
Sun Jan 1 11:29:07 UTC 2006


On Sun, Jan 01, 2006 at 12:04:40PM +0100, Thomas F. Burdick wrote:
> On 12/31/05, Juho Snellman <jsnell at iki.fi> wrote:
> > Thanks. The problem is that the callback support assumes that
> > #'SB-VM::ENTER-ALIEN-CALLBACK can never move [*]. Which is no
> > longer true on x86 and x86-64 as of a few releases ago, since
> > everything is in the dynamic space by default, and will get
> > relocated when the core is saved [**].
> 
> Okay, that makes sense now.  Any pointers to the discussion of why
> purify doesn't happen by default anymore on those platforms? 
> (curious).

It's a performance thing. The static space doesn't have a write
barrier, and so needs to be scavenged completely on every GC.

<http://ww.telent.net/diary/2003/12/#21.34500>
<http://www.iki.fi/jsnell/blog/archive/2005-05-12.html>

> The layout of symbols is unlikely to change soon, so that will work
> fine. But another option would be to cater to what the trampolines do
> now and move the ENTER-ALIEN-CALLBACK function to static space.

I don't think we have any simple way of moving a single function into
the static space. Attached patch implements my suggestion on x86 and
x86-64 (with the exception that it uses the symbol-value slot, since
the symbol-function slot doesn't actually exist).

-- 
Juho Snellman
-------------- next part --------------
Index: package-data-list.lisp-expr
===================================================================
RCS file: /cvsroot/sbcl/sbcl/package-data-list.lisp-expr,v
retrieving revision 1.342
diff -u -r1.342 package-data-list.lisp-expr
--- package-data-list.lisp-expr	28 Dec 2005 22:37:14 -0000	1.342
+++ package-data-list.lisp-expr	1 Jan 2006 10:52:37 -0000
@@ -109,7 +109,7 @@
                "COMPUTE-NATURALIZE-LAMBDA" "DEFINE-ALIEN-TYPE-CLASS"
                "DEFINE-ALIEN-TYPE-METHOD" "DEFINE-ALIEN-TYPE-TRANSLATOR" "DEPORT"
                "DEPOSIT-ALIEN-VALUE" "DISPOSE-LOCAL-ALIEN"
-               "ENTER-ALIEN-CALLBACK"
+               "*ENTER-ALIEN-CALLBACK*" "ENTER-ALIEN-CALLBACK"
                "EXTRACT-ALIEN-VALUE"
                "HEAP-ALIEN-INFO" "HEAP-ALIEN-INFO-P" "HEAP-ALIEN-INFO-SAP-FORM"
                "HEAP-ALIEN-INFO-TYPE" "INVOKE-ALIEN-TYPE-METHOD" "LOCAL-ALIEN"
Index: src/code/target-alieneval.lisp
===================================================================
RCS file: /cvsroot/sbcl/sbcl/src/code/target-alieneval.lisp,v
retrieving revision 1.37
diff -u -r1.37 target-alieneval.lisp
--- src/code/target-alieneval.lisp	15 Oct 2005 12:55:53 -0000	1.37
+++ src/code/target-alieneval.lisp	1 Jan 2006 10:52:38 -0000
@@ -864,6 +864,11 @@
            return
            arguments))
 
+;;; To ensure that callback wrapper functions continue working even
+;;; if #'ENTER-ALIEN-CALLBACK moves in memory, access to it is indirected
+;;; through the *ENTER-ALIEN-CALLBACK* static symbol. -- JES, 2006-01-01
+(defvar *enter-alien-callback* #'enter-alien-callback)
+
 ;;;; interface (not public, yet) for alien callbacks
 
 (defmacro alien-callback (specifier function &environment env)
Index: src/compiler/x86/c-call.lisp
===================================================================
RCS file: /cvsroot/sbcl/sbcl/src/compiler/x86/c-call.lisp,v
retrieving revision 1.27
diff -u -r1.27 c-call.lisp
--- src/compiler/x86/c-call.lisp	14 Jul 2005 19:13:47 -0000	1.27
+++ src/compiler/x86/c-call.lisp	1 Jan 2006 10:52:38 -0000
@@ -380,7 +380,16 @@
               (inst add  eax 16)                    ; arguments
               (inst push eax)                       ; arg1
               (inst push (ash index 2))             ; arg0
-              (inst push (get-lisp-obj-address #'enter-alien-callback)) ; function
+
+              ;; Indirect the access to ENTER-ALIEN-CALLBACK through
+              ;; the symbol-value slot of SB-ALIEN::*ENTER-ALIEN-CALLBACK*
+              ;; to ensure it'll work even if the GC moves ENTER-ALIEN-CALLBACK.
+              ;; Skip any SB-THREAD TLS magic, since we don't expecte anyone
+              ;; to rebind the variable. -- JES, 2006-01-01
+              (inst mov eax (+ nil-value (static-symbol-offset
+                                          'sb!alien::*enter-alien-callback*)))
+              (loadw eax eax symbol-value-slot other-pointer-lowtag)
+              (inst push eax) ; function
               (inst mov  eax (foreign-symbol-address "funcall3"))
               (inst call eax)
               ;; now put the result into the right register
Index: src/compiler/x86/parms.lisp
===================================================================
RCS file: /cvsroot/sbcl/sbcl/src/compiler/x86/parms.lisp,v
retrieving revision 1.53
diff -u -r1.53 parms.lisp
--- src/compiler/x86/parms.lisp	14 Dec 2005 03:39:23 -0000	1.53
+++ src/compiler/x86/parms.lisp	1 Jan 2006 10:52:38 -0000
@@ -345,6 +345,10 @@
     ;; For GC-AND-SAVE
     *restart-lisp-function*
 
+    ;; Needed for callbacks to work across saving cores. see
+    ;; ALIEN-CALLBACK-ASSEMBLER-WRAPPER in c-call.lisp for gory details.
+    sb!alien::*enter-alien-callback*
+
     ;; The ..SLOT-UNBOUND.. symbol is static in order to optimise the
     ;; common slot unbound check.
     ;;
Index: src/compiler/x86-64/c-call.lisp
===================================================================
RCS file: /cvsroot/sbcl/sbcl/src/compiler/x86-64/c-call.lisp,v
retrieving revision 1.14
diff -u -r1.14 c-call.lisp
--- src/compiler/x86-64/c-call.lisp	1 Dec 2005 04:16:00 -0000	1.14
+++ src/compiler/x86-64/c-call.lisp	1 Jan 2006 10:52:38 -0000
@@ -408,7 +408,15 @@
                    (bug "Unknown alien floating point type: ~S" type)))))
 
         ;; arg0 to FUNCALL3 (function)
-        (inst mov rdi (get-lisp-obj-address #'enter-alien-callback))
+        ;;
+        ;; Indirect the access to ENTER-ALIEN-CALLBACK through
+        ;; the symbol-value slot of SB-ALIEN::*ENTER-ALIEN-CALLBACK*
+        ;; to ensure it'll work even if the GC moves ENTER-ALIEN-CALLBACK.
+        ;; Skip any SB-THREAD TLS magic, since we don't expecte anyone
+        ;; to rebind the variable. -- JES, 2006-01-01
+        (inst mov rdi (+ nil-value (static-symbol-offset
+                                    'sb!alien::*enter-alien-callback*)))
+        (loadw rdi rdi symbol-value-slot other-pointer-lowtag)
         ;; arg0 to ENTER-ALIEN-CALLBACK (trampoline index)
         (inst mov rsi (fixnumize index))
         ;; arg1 to ENTER-ALIEN-CALLBACK (pointer to argument vector)
Index: src/compiler/x86-64/parms.lisp
===================================================================
RCS file: /cvsroot/sbcl/sbcl/src/compiler/x86-64/parms.lisp,v
retrieving revision 1.16
diff -u -r1.16 parms.lisp
--- src/compiler/x86-64/parms.lisp	14 Dec 2005 03:39:23 -0000	1.16
+++ src/compiler/x86-64/parms.lisp	1 Jan 2006 10:52:38 -0000
@@ -225,6 +225,10 @@
     ;; For GC-AND-SAVE
     *restart-lisp-function*
 
+    ;; Needed for callbacks to work across saving cores. see
+    ;; ALIEN-CALLBACK-ASSEMBLER-WRAPPER in c-call.lisp for gory details.
+    sb!alien::*enter-alien-callback*
+
     ;; The ..SLOT-UNBOUND.. symbol is static in order to optimise the
     ;; common slot unbound check.
     ;;


More information about the cffi-devel mailing list