Unexpected non-local transfer on doing SAP to pointer coercion

Michael Kappert via Sbcl-help <[email protected]>
Newsgroups gmane.lisp.steel-bank.general
Message-ID <[email protected]>
Dear all,

I'm getting the below error when calling inet_ntop via CFFI.
The error occurs with both tested version SBCL 2.3.10 and 2.4.5, on
up-to-date Fedora 40, latest CFFI via quickload (cffi-20231021).

Source compiled with (optimize speed (space 0) (safety 1) (debug 1))).

==========================
Backtrace:
--------------------------

doing SAP to pointer coercion (cost 20) to "<return value>"
BREAK was entered because of *BREAK-ON-SIGNALS* (now rebound to NIL).
    [Condition of type SIMPLE-CONDITION]

Restarts:
  0: [CONTINUE] Return from BREAK.
  1: [RESET] Set *BREAK-ON-SIGNALS* to NIL and continue.
  2: [REASSIGN] Return from BREAK and assign a new value to
*BREAK-ON-SIGNALS*.
  3: [ABORT] abort thread (#<THREAD tid=3012 "ON-DEMAND-LOOP" RUNNING
{1007E8A0D3}>)

Backtrace:
   0: (SB-C:COMPILER-NOTIFY "doing ~A (cost ~W)~@[ from ~S~]~@[ to ~S~]"
"SAP to pointer coercion" 20 NIL "<return value>")
   1: (SB-C::MAYBE-EMIT-COERCE-EFFICIENCY-NOTE #<SB-C::VOP-INFO :NAME
SB-VM::MOVE-FROM-SAP :ARG-TYPES (*) :RESULT-TYPES (*) :LTN-POLICY :FAST
:COST 20 :NOTE "SAP to pointer coercion"> #<SB-C:TN-REF :TN #<SB..
   2: (SB-C::EMIT-MOVES-AND-COERCIONS #<SB-C::IR2-BLOCK :START-VOP
#<SB-C::VOP :INFO SB-VM::MOVE-TO-WORD/FIXNUM :ARGS #<SB-C:TN-REF :TN
#<SB-C:TN #:G338!3 :NORMAL> :WRITE-P NIL :VOP SB-VM::MOVE-TO-WORD/FIX..
   3: (SB-C::SELECT-REPRESENTATIONS #<SB-C:COMPONENT :NAME "<unknown>"
{100E0DC343}>)
   4: (SB-C::%COMPILE-COMPONENT #<SB-C:COMPONENT :NAME "<unknown>"
{100E0DC343}>)
   5: (SB-C::COMPILE-COMPONENT #<SB-C:COMPONENT :NAME "<unknown>"
{100E0DC343}>)
   6: (SB-C::%COMPILE (LAMBDA (#1=#:FUN334 . #2=(#:G335 #:G336 #:G337
#:G338)) (DECLARE (OPTIMIZE #)) (DECLARE (TYPE # #1#)) (ALIEN-FUNCALL
#1# . #2#)) NIL NIL)
   7: ((LAMBDA NIL :IN SB-C:COMPILE-IN-LEXENV))
   8: ((FLET SB-C::WITH-IT :IN SB-C::%WITH-COMPILATION-UNIT))
   9: (SB-C:COMPILE-IN-LEXENV (LAMBDA (#1=#:FUN334 . #2=(#:G335 #:G336
#:G337 #:G338)) (DECLARE (OPTIMIZE #)) (DECLARE (TYPE # #1#))
(ALIEN-FUNCALL #1# . #2#)) #<NULL-LEXENV> NIL NIL NIL NIL NIL)
  10: (COMPILE NIL (LAMBDA (#1=#:FUN334 . #2=(#:G335 #:G336 #:G337
#:G338)) (DECLARE (OPTIMIZE #)) (DECLARE (TYPE # #1#)) (ALIEN-FUNCALL
#1# . #2#)))
  11: (ALIEN-FUNCALL #<SB-ALIEN-INTERNALS:ALIEN-VALUE :SAP #X545015B0
:TYPE (FUNCTION SB-SYS:SYSTEM-AREA-POINTER (SB-ALIEN:SIGNED 32)
SB-SYS:SYSTEM-AREA-POINTER SB-SYS:SYSTEM-AREA-POINTER (SB-ALIEN:SIGNED 3..
  12: (MBEDTLS::INET-NTOP #<unavailable argument> #<unavailable
argument> #<unavailable argument> #<unavailable argument>)
  13: (MBEDTLS::MBEDTLS-NET-ACCEPT #<unavailable argument> #<unavailable
argument>)
  14: (MBEDTLS::%ACCEPT #<MBEDTLS::PLAIN-SOCKET-SERVER {1007CF88E3}>)
  15: ((:METHOD MBEDTLS:ACCEPT (MBEDTLS::PLAIN-SOCKET-SERVER))
#<MBEDTLS::PLAIN-SOCKET-SERVER {1007CF88E3}>) [fast-method]
  16: ((FLET POLARCL::ON-DEMAND-LOOP% :IN POLARCL::SERVER-LOOP-ONDEMAND))
  17: ((LABELS BORDEAUX-THREADS::%BINDING-DEFAULT-SPECIALS-WRAPPER :IN
BORDEAUX-THREADS::BINDING-DEFAULT-SPECIALS))
  18: ((FLET SB-UNIX::BODY :IN SB-THREAD::RUN))
  19: ((FLET "WITHOUT-INTERRUPTS-BODY-" :IN SB-THREAD::RUN))
  20: ((FLET SB-UNIX::BODY :IN SB-THREAD::RUN))
  21: ((FLET "WITHOUT-INTERRUPTS-BODY-" :IN SB-THREAD::RUN))
  22: (SB-THREAD::RUN)
  23: ("foreign function: call_into_lisp_")
  24: ("foreign function: funcall1")

==========================

Compiling just the CFFI definition with (safety 3) (debug 1) (space 1)
(speed 0) eliminates the error:

(locally
     (declare (optimize (safety 3) (debug 1) (space 1) (speed 0)))
   (defcfun "inet_ntop" :string
     (af :int)
     (src :pointer)
     (dst :pointer)
     (size :int))
   )

I tried to isolate the problem, without success (see attachment - but it
doesn't show the problem).
In the actual program, the IP address passed to inet_ntop comes an
mbedtls_net_accept library call.

I'd be grateful for any ideas what might be the cause or what I should
look at?

Thanks,
Michael

_______________________________________________
Sbcl-help mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sbcl-help
test.cl (text/x-opencl-src, 1.1 KB)
(eval-when (:compile-toplevel :execute)
  (use-package :cffi))

;; Generated from
;; #include <netinet/in.h>
;; fprintf(fp, "(defconstant INET_ADDRSTRLEN %i)\n", INET_ADDRSTRLEN);
;; etc.
(defconstant INET6_ADDRSTRLEN 46)
(defconstant AF_INET6 10)

;; (locally
;; (declare (optimize (safety 3) (debug 1) (space 1) (speed 0)))
;; (declare (optimize (safety 0) (debug 0) (space 1) (speed 3)))
(defcfun "inet_ntop" :string
  (af :int)
  (src :pointer)
  (dst :pointer)
  (size :int))
;; )

(defcfun "inet_pton" :int
  (af :int)
  (src :string)
  (dst :pointer))

(defun test (&aux (client-ip-bufsize (1+ INET6_ADDRSTRLEN)))
  (with-foreign-objects
      ((client-ip :int8 client-ip-bufsize)
       (destn '(:pointer :int))
       (destp :unsigned-char (1+ INET6_ADDRSTRLEN)))
    (let ((res1
            (inet-pton AF_INET6 "::1" destn)))
      (format t "inet-pton ==> ~a~%" res1)
      (cond
        ((eql res1 1)
         (let ((res2
                 (inet-ntop AF_INET6 destn destp (1+ INET6_ADDRSTRLEN))))
           (format t "inet-ntop ==> ~a~%" res2)
           res2))))))

;; (bordeaux-threads:make-thread #'test :name "TEST")
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.