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

[ffi.compile] Sizes are not properly resolved when a referenced type X has a pointer to type which includes X #147

Closed
droptopx opened this issue Dec 8, 2024 · 5 comments

Comments

@droptopx
Copy link

droptopx commented Dec 8, 2024

The following files cause a crash due to

TypeError: field 'struct Outer.inner' has ctype 'struct Inner' of unknown size

when they are run in sequence:

gen_cffi_test.py:

import cffi

ffi = cffi.FFI()
ffi.set_source("bug_repro", None)
ffi.cdef(
    """\
struct Inner {
  struct Outer *outer_ref;
};

struct Outer {
  struct Inner inner;
};
"""
)
ffi.compile(verbose=True, debug=True, tmpdir="./bug_repro_autogen")

cffi_test.py:

from bug_repro_autogen.bug_repro import ffi

ffi.callback("void (struct Inner *)")

python gen_cffi_test.py && python cffi_test.py

I have not figured out what the root cause is and will probably not be able to find time to look further. Any analysis by someone else is welcome.

EDIT: It's not just ffi.callback, ffi.sizeof("struct Inner") also gives the same error.
EDIT2: The error doesn't happen when the erroring method is called in the first file (i.e. no compilation)

@droptopx droptopx changed the title FFI.callback doesn't resolve sizes properly when a referenced type X has a pointer to type which includes X [ffi.compile] Sizes are not properly resolved when a referenced type X has a pointer to type which includes X Dec 8, 2024
@arigo
Copy link
Contributor

arigo commented Dec 9, 2024

It works for me and I remember fixing something like that (maybe last year?). Can you check which version of CFFI you're testing?

@arigo
Copy link
Contributor

arigo commented Dec 9, 2024

Sorry, I got it to fail too now. My mistake. It only occurs in out-of-line ABI mode, when you specify None for the C sources in ffi.set_source().

@arigo
Copy link
Contributor

arigo commented Dec 9, 2024

There is an order dependency, which suggests a workaround for you for now: if you first do ffi.sizeof("struct Outer") then any following usage of struct Inner works fine.

@arigo
Copy link
Contributor

arigo commented Dec 9, 2024

See #148.

@arigo arigo closed this as completed Dec 9, 2024
@droptopx
Copy link
Author

droptopx commented Dec 9, 2024

@arigo #148 does fix #147, thanks a lot for the fix and the great turn-around time!

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

No branches or pull requests

2 participants