rpav / cl-autowrap

(c-include "file.h") => complete FFI wrapper
BSD 2-Clause "Simplified" License
210 stars 41 forks source link

Incorrect accessors generated in the presence of an anonymous union containing nested structures #129

Open KislyjKisel opened 5 months ago

KislyjKisel commented 5 months ago

C definition: https://github.com/blend2d/blend2d/blob/597ea5d5c35c6d3dfdc2329316f1d8ba25929404/src/blend2d/format.h#L118-L141

(let ((format (autowrap:alloc 'format-info)))
  (format t "DEPTH   ~a~%" (format-info.depth& format))
  (format t "FLAGS   ~a~%" (format-info.flags& format))
  (format t "SIZES   ~a~%" (format-info.sizes[]& format))
  (format t "R-SIZE  ~a~%" (format-info.r-size& format))
  (format t "PALETTE ~a~%" (format-info.palette& format)))

r-size and sizes are expected to have the same address as palette because they are in the same union, but instead have the address of the parent struct, same as depth.

DEPTH   #.(SB-SYS:INT-SAP #X00D7BCB0)
FLAGS   #.(SB-SYS:INT-SAP #X00D7BCB4)
SIZES   #.(SB-SYS:INT-SAP #X00D7BCB0)
R-SIZE  #.(SB-SYS:INT-SAP #X00D7BCB0)
PALETTE #.(SB-SYS:INT-SAP #X00D7BCB8)

Platform: Linux x86-64, SBCL.

rpav commented 2 months ago

A million years later, but is this still an issue? Did you check the c2ffi output, or try with a newer clang? I don't think autowrap/sffi itself does any computation for field offsets (outside of array indexing).

KislyjKisel commented 2 months ago

c2ffi outputs fields with offsets relative to the nearest parent record. autowrap seems to account for this with pre-offset parameter. Here, if I understand correctly, nested records are parsed, but their pre-offset is set to the field's bit-offset. If the field's bit-offset isn't absolute there, than it should be (+ pre-offset (or bit-offset 0)) instead.