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

packed structs #352

Closed
dcharkes opened this issue Apr 15, 2021 · 8 comments · Fixed by dart-archive/ffigen#205
Closed

packed structs #352

dcharkes opened this issue Apr 15, 2021 · 8 comments · Fixed by dart-archive/ffigen#205
Labels
package:ffigen type-enhancement A request for a change that isn't a bug

Comments

@dcharkes
Copy link
Collaborator

2.13.0-211.6.beta has support for packed structs (dart-lang/sdk#38158).

Can we detect with clang a struct is packed? and add the right annotation to it?

@dcharkes dcharkes added the type-enhancement A request for a change that isn't a bug label Apr 15, 2021
@mannprerak2
Copy link
Contributor

mannprerak2 commented Apr 15, 2021

We can get the alignment of a struct using this function

long long clang_Type_getAlignOf(CXType T);

and use it for annotation@Packed(<align-value>).

Do we need any other config for this?

EDIT: Will this be platform-independent though? Can this value be different on different platforms?
Maybe we only apply @Packed annotation if align value is exactly 1.

@dcharkes
Copy link
Collaborator Author

Will this be platform-independent though? Can this value be different on different platforms?

I don't think always emitting @PACKED aways is platform independent.

Ideally, we would only emit @Packed(1, 2, 4, 8, or 16) if packing is used on the struct. For example https://github.com/dart-lang/sdk/blob/master/runtime/bin/ffi_test/ffi_test_functions_generated.cc#L470, but not the structs before that one in the file.

If we can't detect if packing is used from clang, then we might want to use a configuration option to specify packing instead.

@mannprerak2
Copy link
Contributor

mannprerak2 commented Apr 15, 2021

We can only use the align value returned by clang_Type_getAlignOf if the struct was packed, otherwise the generated code is not platform-independant.

There are 3 ways to specify how a struct is packed in C -

  1. Using __attribute__((__packed__)) or__attribute__((__packed__, aligned(X))) on a struct -> This can be easily detected with libclang.
  2. Using #pragma pack(...) -> Unfortunately libclang doesn't expose this (See Bindgen can't translate #pragma pack(n) into #[repr(packed = "n")] rust-lang/rust-bindgen#537 (comment))
    1. Workaround -> We can check if any of the members of the struct has an align value greater than the align value of the parent struct, and if it is then we know for sure that the struct was packed.
  3. There's also __attribute__((aligned(X))) which sets the minimum alignment for a struct, (i.e alignment could be more if a member has a value greater than X -> For this we can pack if the maximum align value of any child is less than the align value of parent struct.

I think this covers everything, so there is no need for adding a config to specify the packing of a struct explicitly.

@dcharkes
Copy link
Collaborator Author

dcharkes commented Apr 15, 2021

P.S. A good stresstest would be to compare the generated bindings with the ones that @timsneath has been generating from the win32 meta-data repository in https://github.com/timsneath/win32. (I'm sure the metadata repository has more information than the C header files, so ffigen might not be as precise, but it would be cool to see if we end up with the same generated bindings.)

@mannprerak2
Copy link
Contributor

We will have to also provide users a way to specify the packing via the config since -

  1. It's possible to pack structs via some command-line argument when compiling a shared library.
  2. To work around some missing edge cases, plus it's better to have the flexibility.

The config can be something like this -

structs:
  pack:
    'ShortStruct': 2 # value can be [0, 1, 2, 4, 8 ,16]. 0 means no packing is applied.
    '.*': 1
    'NotPackedStruct': 0 

This allows users to override the generated values, so this config will only be needed if the generated Packed annotation is incorrect.

@dcharkes
Copy link
Collaborator Author

Maybe use none instead of 0.

And yes an option for .* (or other regex). Great thinking! 🚀

@timsneath
Copy link

In my Win32 code, structs.g.dart represents all the structs I'm able to generate automatically today; structs.dart represents all the structs I still have to generate manually (mostly because of the lack of union support).

@mannprerak2
Copy link
Contributor

Thanks @timsneath I'll take a look.

@liamappelbe liamappelbe transferred this issue from dart-archive/ffigen Nov 15, 2023
parlough pushed a commit to parlough/native that referenced this issue Apr 8, 2024
* WIP: NSData bug repro

* Fix forward declaration bug

* Remove unused clang_isDeclaration function

* Format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package:ffigen type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants