microsoft / wdkmetadata

Tooling to generate metadata for Win32 APIs in the Windows Driver Kit (WDK).
Other
96 stars 10 forks source link

POINTER_ALIGNMENT not applied to Rust structs #60

Open vlabo opened 10 months ago

vlabo commented 10 months ago

Summary

POINTER_ALIGNMENT is not applied to the rust structs for example.

pub struct IO_STACK_LOCATION_0_4 {
    pub OutputBufferLength: u32,
    pub InputBufferLength: u32,
    pub IoControlCode: u32,
    pub Type3InputBuffer: *mut ::core::ffi::c_void,
}

file: libs/sys/src/Windows/Wdk/Foundation/mod.rs

This struct has length 24 bytes in rust. But the C version has more padding with the InputBufferLength and IoControlCode

struct {
    ULONG OutputBufferLength;
    ULONG POINTER_ALIGNMENT InputBufferLength;
    ULONG POINTER_ALIGNMENT IoControlCode;
    PVOID Type3InputBuffer;
} DeviceIoControl;

file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\km\wdm.h

Crate manifest

No response

Crate code

No response

kennykerr commented 10 months ago

Doesn't look like there's any metadata for that - not sure what that would be. I'll pass this question on to the win32metadata repo for discussion.

mikebattista commented 10 months ago

Related to https://github.com/microsoft/win32metadata/issues/1044 where ClangSharp doesn't scrape DECLSPEC_ALIGN.

@tannergooding any updates on this issue?

tannergooding commented 10 months ago

Haven't had a chance to dig into it yet.

This is also a particularly troublesome case since its not packing, but rather individual field adjustments which can't be represented by many languages/runtimes and a case where the layout will differ between 32-bit and 64-bit, so its not something that can necessarily be picked up with a single traversal.

-- Even with this metadata, I'm not sure Rust has an attribute to do this layout itself. It has repr(C) and it has packed(x) and align(x), but those are all for structs, not fields. I think at best it would need to have a nested transparent struct with its own align modifier (differing for 32-bit vs 64-bit).

tannergooding commented 10 months ago

-- If someone else wants to give it a go, contributions are welcome and the relevant attributes should be available off the AST nodes already.

ChrisDenton commented 10 months ago

Even with this metadata, I'm not sure Rust has an attribute to do this layout itself. It has repr(C) and it has packed(x) and align(x), but those are all for structs, not fields. I think at best it would need to have a nested transparent struct with its own align modifier (differing for 32-bit vs 64-bit).

I don't think there's any problem with that, so long as the metadata is there. A PointerAlignment<T> struct makes sense. There are other places we do shenanigans to make things work (e.g. Rust doesn't have inline unions). An alternative would be manually adding padding which is a bit blah.

tannergooding commented 10 months ago

I don't think there's any problem with that, so long as the metadata is there

Sure, just it needs to be clarified where it is relevant for ABI purposes/etc.

T and S<T> (given struct S<T> { T value; }) are different from the ABI perspective. This is why rust has the "transparent struct" attribute -- this is particularly relevant for COM/instance methods on Windows where struct returns are treated differently from primitive returns

Similarly, struct S { int32_t x; int64_t y; } and struct S { int32_t x; int32_t padding; int64_t y; } are different and may be handled differently by some platforms/ABIs/classifiers, so manually declaring padding is not "safe" -- many ABI classification systems ignore padding bits, but do not ignore explicitly declared fields. Thus this can change whether its classified as HFA/HVA or how many registers it can be split across.

An anonymous struct/union is likewise explicitly a different struct/union, just one where language syntax allows direct access and thus struct S { int32_t x; struct { int32_t x; int32_t y; } is different from struct S { int32_t x; int32_t y; int32_t z; } from the ABI/layout perspective -- this becomes particularly relevant in cases such as struct S { struct { int64_t x; int32_t y; } int32_t z; } where the fact that it is a nested anonymous struct means the size is (8 + 4 + 4 padding) + (4 + 4 padding) (and therefore size=24, not size=16). This also often impacts inheritance in C++ where the first field is explicitly Base not the same fields from Base repeated inline.

There are even platforms where the signedness of the type is relevant for ABI purposes and thus int32_t and uint32_t are different, thus causing incorrect results if you declare your interop signature taking i32 where the actual signature took u32 or vice versa.