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

Why is struct DROPFILES packed(1)? #669

Closed
knopp opened this issue Apr 4, 2021 · 3 comments
Closed

Why is struct DROPFILES packed(1)? #669

knopp opened this issue Apr 4, 2021 · 3 comments

Comments

@knopp
Copy link

knopp commented Apr 4, 2021

This causes warning (borrow of packed field is unsafe and requires unsafe function or block (error E0133)) when accessing fields.

@tim-weis
Copy link
Contributor

tim-weis commented Apr 5, 2021

This has me confused, too. First, the technical reason is that the ShlObj_core.h SDK header file has an

#include <pshpack1.h>

directive right at the top, so the packing winds up in the metadata, and re-surfaces in the generated code. In the specific case of DROPFILES it appears, that packed(1) doesn't change the layout, at least for Intel's x86 and x86_64 architectures. I'm not sure this holds for all architectures, though I believe it is required by Windows' ABI.

It would certainly be nice to have a more versatile structure definition, in case the stronger packing requirement doesn't change the structure layout. The code generator could omit the packing attribute, if it can determine, that it is superfluous. The Layout type seems to provide all the required building blocks to determine this.

Except, there's more to structure packing than packing: packed(1) also implies align(1) (as hinted to under Type Layout, although there is quite some confusion on that topic, and the lack of a formal specification doesn't help much). While the code generator can verify that a packed structure has the same layout as its non-packed variant, it cannot retroactively enforce stricter alignment.

I suppose the only safe option here is to follow C, which relaxes the alignment requirements for packed structures on all 3 major C++ compilers. A corollary of this is that you cannot have references into a packed structure without exhibiting undefined behavior. This tracking issue illustrates workarounds.

@kennykerr
Copy link
Collaborator

Right, the packing comes from metadata. If you can make a case for the packing not affecting the struct's layout you can create an issue here to request this be dropped:

https://github.com/microsoft/win32metadata

@kennykerr
Copy link
Collaborator

I also created an issue to improve trait coverage for such types. #656

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

3 participants