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

Fix ABI problem when marshalling X509VerifyStatusCode #52789

Merged
merged 1 commit into from
May 18, 2021
Merged

Fix ABI problem when marshalling X509VerifyStatusCode #52789

merged 1 commit into from
May 18, 2021

Conversation

uweigand
Copy link
Contributor

The X509VerifyStatusCode type is defined as an enum in C code, but as
a struct (with a single member) in C# code. This means that marshalling
this type only works correctly on platforms where the ABI treats these
two types as equivalent. This fails e.g. on Linux on s390x.

Fixed by changing all native functions that take X509VerifyStatusCode
as argument or return type to use a plain "int" instead.

The X509VerifyStatusCode type is defined as an enum in C code, but as
a struct (with a single member) in C# code.  This means that marshalling
this type only works correctly on platforms where the ABI treats these
two types as equivalent.  This fails e.g. on Linux on s390x.

Fixed by changing all native functions that take X509VerifyStatusCode
as argument or return type to use a plain "int" instead.
@ghost
Copy link

ghost commented May 14, 2021

Tagging subscribers to this area: @bartonjs, @vcsjones, @krwq, @GrabYourPitchforks
See info in area-owners.md if you want to be subscribed.

Issue Details

The X509VerifyStatusCode type is defined as an enum in C code, but as
a struct (with a single member) in C# code. This means that marshalling
this type only works correctly on platforms where the ABI treats these
two types as equivalent. This fails e.g. on Linux on s390x.

Fixed by changing all native functions that take X509VerifyStatusCode
as argument or return type to use a plain "int" instead.

Author: uweigand
Assignees: -
Labels:

area-System.Security

Milestone: -

@bartonjs
Copy link
Member

I'm not fundamentally opposed here, but I am confused... what ends up happening on s390x?

@uweigand
Copy link
Contributor Author

uweigand commented May 17, 2021

I'm not fundamentally opposed here, but I am confused... what ends up happening on s390x?

For return values, an integer is returned in general purpose register %r2. However, all structs, including a struct that contains only a single integer, are returned on the stack via implicit reference. So if one side of the marshalling assumes we need an implicit reference, and the other side doesn't, the arguments end up all confused.

(This is different on x64 and arm, because there is an ABI special case where structs that contain only a single integer are not returned via implicit reference, but rather in the same register where a plain integer would be returned.)

@bartonjs
Copy link
Member

Ah, so the native library (which sees the type as an enum/integer) still does single register, but since the P/Invoke Marshaller sees the type as a struct it tries unpacking it per struct rules, resulting in a mismatch?

@uweigand
Copy link
Contributor Author

Ah, so the native library (which sees the type as an enum/integer) still does single register, but since the P/Invoke Marshaller sees the type as a struct it tries unpacking it per struct rules, resulting in a mismatch?

Exactly. The marshaller prepares a buffer on the stack, and passes a pointer to that buffer to the native routine, expecting the native routine to fill (what it thinks to be) the struct to be returned in. But native code thinks it returns an integer, and so doesn't expect a buffer pointer but simply returns the integer in register. (Plus the presence of the return pointer shifts the other arguments by one slot, which native code also doesn't expect, so the incoming arguments are already wrong.)

@marek-safar marek-safar requested a review from bartonjs May 17, 2021 19:27
@bartonjs bartonjs merged commit 4fd2aa9 into dotnet:main May 18, 2021
@karelz karelz added this to the 6.0.0 milestone May 20, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jun 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants