Passing Structs by Value using cffi-libffi doesn't work well

Bug #1946568 reported by clsper03
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
CFFI
New
Undecided
Unassigned

Bug Description

C codes in the file test.c as below:
struct complex {
  double real;
  double imag;
};
double magnitude_squared (struct complex c) {
  return c.real * c.real + c.imag * c.imag;
};

Compile this into a shared library:
gcc -fpic -shared test.c -o test.so

then typing in REPL:
shows below:

CFFI-LIBFFI-TEST> (ql:quickload :cffi-libffi)
To load "cffi-libffi":
  Load 1 ASDF system:
    cffi-libffi
; Loading "cffi-libffi"

(:CFFI-LIBFFI)
CFFI-LIBFFI-TEST> (cffi:load-foreign-library "/root/code/cffi-libffi-test/test.so")
#<CFFI:FOREIGN-LIBRARY TEST.SO-653 "test.so">
CFFI-LIBFFI-TEST> (cffi:defcstruct c-complex
(real :double)
(imag :double))
(:STRUCT C-COMPLEX)
CFFI-LIBFFI-TEST> (cffi:defcfun ("magnitude_squared" ms) :double
(c (:struct c-complex)))
WARNING: redefining CFFI-LIBFFI-TEST::MS in DEFUN
MS
CFFI-LIBFFI-TEST> (cffi:with-foreign-object (c '(:struct c-complex))
(setf (cffi:foreign-slot-value c '(:struct c-complex)
'real)
3d0
(cffi:foreign-slot-value c '(:struct c-complex)
'imag)
4d0)
(sqrt (ms c)))

Then comes with error:
There is no applicable method for the generic function
  #<STANDARD-GENERIC-FUNCTION CFFI:TRANSLATE-INTO-FOREIGN-MEMORY (5)>
when called with arguments
  (#.(SB-SYS:INT-SAP #X7F51FF2B7FE8)
   #<C-COMPLEX-TCLASS C-COMPLEX>
   #.(SB-SYS:INT-SAP #X7F51FF2B7FC8)).
   [Condition of type SB-PCL::NO-APPLICABLE-METHOD-ERROR]
See also:
  Common Lisp Hyperspec, 7.6.6 [:section]

Restarts:
 0: [RETRY] Retry calling the generic function.
 1: [RETRY] Retry SLIME REPL evaluation request.
 2: [*ABORT] Return to SLIME's top level.
 3: [ABORT] abort thread (#<THREAD "repl-thread" RUNNING {1003039B63}>)
.

And I have tested that the subsystem CFFI-LIBFFI has been loaded and I have installed LIBFFI and it works well.
cffi_0.24.1 + SBCL 1.4.16.debian x8664 + GNU Emacs 26.1 + slime

clsper03 (clsper03)
summary: - Passing Structs by Value using cffi-libffi doesn't work wel
+ Passing Structs by Value using cffi-libffi doesn't work well
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.