Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The value "xxx" is not of type SB-SYS:SYSTEM-AREA-POINTER when binding SB-ALIEN::VALUE #11

Closed
funatsufumiya opened this issue Nov 27, 2024 · 17 comments · Fixed by #12
Closed

Comments

@funatsufumiya
Copy link

funatsufumiya commented Nov 27, 2024

README's examples gives me the same error when returning some value from C++ function.

debugger invoked on a TYPE-ERROR @70063008B4 in thread
#<THREAD tid=259 "main thread" RUNNING {70083C05B3}>:
The value
  "xxx"
is not of type
  SB-SYS:SYSTEM-AREA-POINTER
when binding SB-ALIEN::VALUE

For example, on SDL2 example, (init) works well, but (create-window ...) gives this error.


$ sbcl --version
SBCL 2.4.10

$ neofetch --off
OS: macOS 15.1.1 24B91 arm64
Host: MacBookAir10,1
Kernel: 24.1.0
Packages: 512 (brew)
Shell: zsh 5.9
Resolution: 1280x800
DE: Aqua
WM: Quartz Compositor
WM Theme: Blue (Dark)
Terminal: iTerm2
Terminal Font: CaskaydiaCoveNFM-Regular 12
CPU: Apple M1
GPU: Apple M1
Memory: 1564MiB / 8192MiB
@Islam0mar
Copy link
Owner

Islam0mar commented Nov 28, 2024

I can't reproduce.

Could you give stack trace?

You may also try these simpler examples first:

(from '("<string>") 'import '("[](std::string x){return \"Hi, \"+x;}" . "hi"))
(hi "there!")

(from '("<cmath>") 'import '("static_cast<double(*)(double)>(std::sin)" . "cpp-sin"))
(cpp-sin 0d0)
(cpp-sin pi)
(cpp-sin (/ pi 2))

@funatsufumiya
Copy link
Author

funatsufumiya commented Nov 28, 2024

@Islam0mar
I also tried it. Just same as SDL2 case, (from ...) passed well, but (hi "there!") returned the same error (The value "there!" is not of type ...)

(I'll prepare full stacktrace. Please wait a moment.)

@funatsufumiya
Copy link
Author

This is full log of above.

$ sbcl
This is SBCL 2.4.10, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.
* (ql:quickload :cxx-jit)
To load "cxx-jit":
  Load 1 ASDF system:
    cxx-jit
; Loading "cxx-jit"
..........
(:CXX-JIT)
* (setf cxx-jit:*cxx-compiler-link-libs* "-L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main")
"-L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main"
* (setf cxx-jit:*cxx-compiler-flags* "-std=c++17 -Wall -Wextra -I/opt/homebrew/include/")
"-std=c++17 -Wall -Wextra -I/opt/homebrew/include/"
*
(in-package cxx-jit)
#<PACKAGE "CXX-JIT">
*

(from '("<string>") 'import '("[](std::string x){return \"Hi, \"+x;}" . "hi"))

"/usr/bin/g++ -shared -fPIC -Wl,-undefined,error -std=c++17 -Wall -Wextra -I/opt/homebrew/include/ /tmp/plugin.cpp -o /tmp/plugin.so -L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main"
"ld: warning: -undefined error is deprecated"
* (hi "there!")

debugger invoked on a TYPE-ERROR @70058BFB74 in thread
#<THREAD tid=259 "main thread" RUNNING {70083C05B3}>:
  The value
    "there!"
  is not of type
    SB-SYS:SYSTEM-AREA-POINTER
  when binding SB-ALIEN::VALUE

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [ABORT] Exit debugger, returning to top level.

(HI "there!")
   source: (FOREIGN-FUNCALL-POINTER #.(SB-SYS:INT-SAP #X1057A6F6C) NIL :POINTER
                                    V0 :POINTER)
0]

@funatsufumiya
Copy link
Author

funatsufumiya commented Nov 28, 2024

And this is same for SDL2.

$ sbcl
This is SBCL 2.4.10, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.
* (ql:quickload :cxx-jit)
To load "cxx-jit":
  Load 1 ASDF system:
    cxx-jit
; Loading "cxx-jit"
..........
(:CXX-JIT)
* (setf cxx-jit:*cxx-compiler-link-libs* "-L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main")
"-L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main"
* (setf cxx-jit:*cxx-compiler-flags* "-std=c++17 -Wall -Wextra -I/opt/homebrew/include/")
"-std=c++17 -Wall -Wextra -I/opt/homebrew/include/"
*
(cxx-jit:from '(
                "<SDL2/SDL.h>")
              'import
              '("[](){return SDL_Init(SDL_Init(SDL_INIT_VIDEO));}" . "init")
              '("SDL_CreateWindow" . "create-window")
              '("SDL_CreateRenderer" . "create-renderer")
              '("SDL_SetRenderDrawColor" . "set-color")
              '("SDL_DestroyWindow" . "destroy-window")
              '("SDL_RenderClear" . "clear-renderer")
              '("SDL_RenderPresent" . "renderer-render")
              '("SDL_Quit" . "sdl-quit"))

"/usr/bin/g++ -shared -fPIC -Wl,-undefined,error -std=c++17 -Wall -Wextra -I/opt/homebrew/include/ /tmp/plugin.cpp -o /tmp/plugin.so -L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main"
"ld: warning: -undefined error is deprecated"
* (init)
0
* (setf wind (create-window "create-window" 0 0 600 700 0))
; in: SETF WIND
;     (SETF WIND (CREATE-WINDOW "create-window" 0 0 600 700 0))
;
; caught WARNING:
;   undefined variable: COMMON-LISP-USER::WIND
;
; compilation unit finished
;   Undefined variable:
;     WIND
;   caught 1 WARNING condition

debugger invoked on a TYPE-ERROR @70058BFC34 in thread
#<THREAD tid=259 "main thread" RUNNING {70083C05B3}>:
  The value
    "create-window"
  is not of type
    SB-SYS:SYSTEM-AREA-POINTER
  when binding SB-ALIEN::VALUE

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [ABORT] Exit debugger, returning to top level.

(CREATE-WINDOW "create-window" 0 0 600 700 0)
   source: (CFFI:FOREIGN-FUNCALL-POINTER #.(SB-SYS:INT-SAP #X101A042B4) NIL
                                         :POINTER V0 :INT V1 :INT V2 :INT V3
                                         :INT ...)
0]

@Islam0mar
Copy link
Owner

can you try this code please:

(cffi:with-foreign-pointer-as-string (str 255)
          (hi (cffi:lisp-string-to-foreign "blablabla" str 10)))

after (cxx-jit:from '("<string>") 'import '("[](std::string x){return \"Hi, \"+x;}" . "hi"))

@funatsufumiya
Copy link
Author

@Islam0mar
It passed well.

* (ql:quickload :cxx-jit)
To load "cxx-jit":
  Load 1 ASDF system:
    cxx-jit
; Loading "cxx-jit"
..........
(:CXX-JIT)
* (setf cxx-jit:*cxx-compiler-link-libs* "-L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main")
"-L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main"
* (setf cxx-jit:*cxx-compiler-flags* "-std=c++17 -Wall -Wextra -I/opt/homebrew/include/")
"-std=c++17 -Wall -Wextra -I/opt/homebrew/include/"
* (in-package cxx-jit)
#<PACKAGE "CXX-JIT">
* (from '("<string>") 'import '("[](std::string x){return \"Hi, \"+x;}" . "hi"))

"/usr/bin/g++ -shared -fPIC -Wl,-undefined,error -std=c++17 -Wall -Wextra -I/opt/homebrew/include/ /tmp/plugin.cpp -o /tmp/plugin.so -L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main"
"ld: warning: -undefined error is deprecated"
* (cffi:with-foreign-pointer-as-string (str 255)
          (hi (cffi:lisp-string-to-foreign "blablabla" str 10)))
"blablabla"
*

@Islam0mar
Copy link
Owner

Islam0mar commented Nov 28, 2024

This deduced type is not string which is quite strange.

It should match here and return :string not a :pointer. I think your compiler or cffi is causing this.

If (foreign-funcall "printf" :string (format nil "%s: %d.~%") :string "So long and thanks for all the fish" :int 42 :int) works, we can look into c++ code that made this deduction failure.

otherwise you would convert every string to c pointer for now :(.

@funatsufumiya
Copy link
Author

funatsufumiya commented Nov 28, 2024

@Islam0mar
I see. This is result of (foreign-funcall "printf" :string (format nil "%s: %d.~%") :string "So long and thanks for all the fish" :int 42 :int)

* (cffi:with-foreign-pointer-as-string (str 255)
          (hi (cffi:lisp-string-to-foreign "blablabla" str 10)))
"blablabla"

* (foreign-funcall "printf" :string (format nil "%s: %d.~%") :string "So long and thanks for all the fish" :int 42 :int)
��������: 50473579.
20

I also write here my clang version.

$ /usr/bin/g++ --version
Apple clang version 16.0.0 (clang-1600.0.26.4)
Target: arm64-apple-darwin24.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

$ /usr/bin/clang --version
Apple clang version 16.0.0 (clang-1600.0.26.4)
Target: arm64-apple-darwin24.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

@Islam0mar
Copy link
Owner

can you try these:

(cffi:foreign-funcall "strlen" :string "foo" :int)

*default-foreign-encoding*

You can change compiler *cxx-compiler-executable-path* to use clang++.

@funatsufumiya
Copy link
Author

* (ql:quickload :cxx-jit)
To load "cxx-jit":
  Load 1 ASDF system:
    cxx-jit
; Loading "cxx-jit"
..........
(:CXX-JIT)
* (setf cxx-jit:*cxx-compiler-link-libs* "-L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main")
"-L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main"
* (setf cxx-jit:*cxx-compiler-flags* "-std=c++17 -Wall -Wextra -I/opt/homebrew/include/")
"-std=c++17 -Wall -Wextra -I/opt/homebrew/include/"
* (in-package cxx-jit)
#<PACKAGE "CXX-JIT">
* (cffi:foreign-funcall "strlen" :string "foo" :int)
3
* *default-foreign-encoding*
:UTF-8

You can change compiler cxx-compiler-executable-path to use clang++.

Thank you. I'll try this next :)

@Islam0mar
Copy link
Owner

I don't have access to mac ATM.

when you have time, you can change cffi-type to:

(defun cffi-type (type)
  "Returns cffi-type as a keyword"
  (declare (type string type))
  (print type) ;; added THIS LINE <=================================
  (let ((type-symbol (cdr (assoc type
                                 *cxx-type-name-to-cffi-type-symbol-alist*
                                 :test #'string-equal))))
    (cond
      (type-symbol type-symbol)
      ((eq #\* (elt type (1- (length type)))) :pointer)
      (t (format t "No Known conversion for type ~S. default to pointer~%" type) :pointer))))

Thus we know how c++ deduced string type.

@funatsufumiya
Copy link
Author

I tried another clang compiler with Homebrew, but nothing seems to be changed.
(Perhaps a much older clang might work, I will try it when I have time.)

* (ql:quickload :cxx-jit)
To load "cxx-jit":
  Load 1 ASDF system:
    cxx-jit
; Loading "cxx-jit"
..........
(:CXX-JIT)
* (setf cxx-jit:*cxx-compiler-link-libs* "-L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main")
"-L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main"
* (setf cxx-jit:*cxx-compiler-flags* "-std=c++17 -Wall -Wextra -I/opt/homebrew/include/")
"-std=c++17 -Wall -Wextra -I/opt/homebrew/include/"
* (in-package cxx-jit)
#<PACKAGE "CXX-JIT">
* (defparameter *cxx-compiler-executable-path* "/opt/homebrew/opt/llvm/bin/clang++")
*CXX-COMPILER-EXECUTABLE-PATH*
* (from '("<string>") 'import '("[](std::string x){return \"Hi, \"+x;}" . "hi"))

"/opt/homebrew/opt/llvm/bin/clang++ -shared -fPIC -Wl,-undefined,error -std=c++17 -Wall -Wextra -I/opt/homebrew/include/ /tmp/plugin.cpp -o /tmp/plugin.so -L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main"
"ld: warning: -undefined error is deprecated"
"ld: warning: -undefined error is deprecated"
* (hi "there!")

debugger invoked on a TYPE-ERROR @70058BFB74 in thread
#<THREAD tid=259 "main thread" RUNNING {70083C05B3}>:
  The value
    "there!"
  is not of type
    SB-SYS:SYSTEM-AREA-POINTER
  when binding SB-ALIEN::VALUE

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [ABORT] Exit debugger, returning to top level.

(HI "there!")
   source: (FOREIGN-FUNCALL-POINTER #.(SB-SYS:INT-SAP #X101653FD0) NIL :POINTER
                                    V0 :POINTER)
0]
# clang versions used above:
$ /opt/homebrew/opt/llvm/bin/clang++ --version
Homebrew clang version 19.1.2
Target: arm64-apple-darwin24.1.0
Thread model: posix
InstalledDir: /opt/homebrew/Cellar/llvm/19.1.2/bin

# system clang versions

$ /usr/bin/g++ --version
Apple clang version 16.0.0 (clang-1600.0.26.4)
Target: arm64-apple-darwin24.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

$ /usr/bin/clang --version
Apple clang version 16.0.0 (clang-1600.0.26.4)
Target: arm64-apple-darwin24.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

@funatsufumiya
Copy link
Author

when you have time, you can change cffi-type to:

OK. I also try this next time when I have time!

@funatsufumiya
Copy link
Author

I also tried llvm@13 and llvm@14, nothing seemed to be changed.

* (ql:quickload :cxx-jit)
To load "cxx-jit":
  Load 1 ASDF system:
    cxx-jit
; Loading "cxx-jit"
..........
(:CXX-JIT)
* (setf cxx-jit:*cxx-compiler-link-libs* "-L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main")
"-L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main"
* (setf cxx-jit:*cxx-compiler-flags* "-std=c++17 -Wall -Wextra -I/opt/homebrew/include/")
"-std=c++17 -Wall -Wextra -I/opt/homebrew/include/"
* (in-package cxx-jit)
#<PACKAGE "CXX-JIT">
* (defparameter *cxx-compiler-executable-path* "/opt/homebrew/opt/llvm@14/bin/clang++")
*CXX-COMPILER-EXECUTABLE-PATH*
* (from '("<string>") 'import '("[](std::string x){return \"Hi, \"+x;}" . "hi"))

"/opt/homebrew/opt/llvm@14/bin/clang++ -shared -fPIC -Wl,-undefined,error -std=c++17 -Wall -Wextra -I/opt/homebrew/include/ /tmp/plugin.cpp -o /tmp/plugin.so -L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main"
"/tmp/plugin.cpp:98:23: warning: unused variable 'always_false' [-Wunused-const-variable]"
"static constexpr bool always_false = false;"
"                      ^"
"1 warning generated."
"ld: warning: -undefined error is deprecated"
"ld: warning: -undefined error is deprecated"
* (hi "there!")

debugger invoked on a TYPE-ERROR @70058BFB74 in thread
#<THREAD tid=259 "main thread" RUNNING {70083C05B3}>:
  The value
    "there!"
  is not of type
    SB-SYS:SYSTEM-AREA-POINTER
  when binding SB-ALIEN::VALUE

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [ABORT] Exit debugger, returning to top level.

(HI "there!")
   source: (FOREIGN-FUNCALL-POINTER #.(SB-SYS:INT-SAP #X1038B3E88) NIL :POINTER
                                    V0 :POINTER)
0]

( llvm@13 had almost same result as above. )

# clang versions used above:

$ /opt/homebrew/opt/llvm@14/bin/clang --version
Homebrew clang version 14.0.6
Target: arm64-apple-darwin24.1.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm@14/bin

$ /opt/homebrew/opt/llvm@13/bin/clang --version
Homebrew clang version 13.0.1
Target: arm64-apple-darwin24.1.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm@13/bin

@Islam0mar
Copy link
Owner

Islam0mar commented Nov 28, 2024

My bad. godbolt

Clang parse it as "char *" while here we use "char*".

@funatsufumiya Add ("char *" . :string) and it should work.

Thanks for reporting. I will fix it with PR tomorrow ISA.

@Islam0mar Islam0mar linked a pull request Nov 29, 2024 that will close this issue
@funatsufumiya
Copy link
Author

Awesome! I confirmed this issue was solved!

Thanks to this (?), I confirmed SDL2 case showing another error, but this is maybe related to my environment. So I'll check this next time! (And this probably not directly related to this issue)

* (ql:quickload :cxx-jit)
To load "cxx-jit":
  Load 1 ASDF system:
    cxx-jit
; Loading "cxx-jit"
...........
(:CXX-JIT)
* (setf cxx-jit:*cxx-compiler-link-libs* "-L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main")
"-L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main"
* (setf cxx-jit:*cxx-compiler-flags* "-std=c++17 -Wall -Wextra -I/opt/homebrew/include/")
"-std=c++17 -Wall -Wextra -I/opt/homebrew/include/"
* (setf cxx-jit:*cxx-compiler-internal-flags* "-shared -fPIC")
"-shared -fPIC"
* (in-package cxx-jit)
#<PACKAGE "CXX-JIT">
* (from '("<string>") 'import '("[](std::string x){return \"Hi, \"+x;}" . "hi"))

"/usr/bin/g++ -shared -fPIC -std=c++17 -Wall -Wextra -I/opt/homebrew/include/ /tmp/plugin.cpp -o /tmp/plugin.so -L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main"
* (hi "there!")
"Hi, there!"
* (cxx-jit:from '(
                "<SDL2/SDL.h>")
              'import
              '("[](){return SDL_Init(SDL_Init(SDL_INIT_VIDEO));}" . "init")
              '("SDL_CreateWindow" . "create-window")
              '("SDL_CreateRenderer" . "create-renderer")
              '("SDL_SetRenderDrawColor" . "set-color")
              '("SDL_DestroyWindow" . "destroy-window")
              '("SDL_RenderClear" . "clear-renderer")
              '("SDL_RenderPresent" . "renderer-render")
              '("SDL_Quit" . "sdl-quit"))

"/usr/bin/g++ -shared -fPIC -std=c++17 -Wall -Wextra -I/opt/homebrew/include/ /tmp/plugin.cpp -o /tmp/plugin.so -L/opt/homebrew/lib -framework OpenGL -lSDL2 -lSDL2main"
* (init)
0
* (setf wind (create-window "create-window" 0 0 600 700 0))
; in: SETF WIND
;     (SETF CXX-JIT::WIND (CXX-JIT:CREATE-WINDOW "create-window" 0 0 600 700 0))
;
; caught WARNING:
;   undefined variable: CXX-JIT::WIND
;
; compilation unit finished
;   Undefined variable:
;     WIND
;   caught 1 WARNING condition

debugger invoked on a FLOATING-POINT-INVALID-OPERATION in thread
#<THREAD tid=259 "main thread" RUNNING {70083D0513}>:
  arithmetic error FLOATING-POINT-INVALID-OPERATION signalled

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [ABORT] Exit debugger, returning to top level.

((FLET SB-UNIX::RUN-HANDLER :IN SB-UNIX::%INSTALL-HANDLER) 8 #.(SB-SYS:INT-SAP #X16D8F5420) #.(SB-SYS:INT-SAP #X16D8F5488))
0]

@Islam0mar
Copy link
Owner

@funatsufumiya if the issue still exist kindly open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants