Closed t-mustafin closed 2 years ago
Tagging subscribers to this area: @JulieLeeMSFT See info in area-owners.md if you want to be subscribed.
Author: | t-mustafin |
---|---|
Assignees: | - |
Labels: | `area-CodeGen-coreclr`, `untriaged` |
Milestone: | - |
This is bug in the clrmd interop signature definition. There is not much runtime can do about it. It needs to be fixed in clrmd.
@t-mustafin This is annoying but alas a well-known issue with the clever HRESULT
wrapped in a struct
pattern that is all too common. It works as an artifact of bad specifications and being clever. Unfortunately there is little that can be done from the runtime side as @jkotas mentions.
@leculver might be able to help here from the clrmd side of things.
Feel free to close this issue and open a pull request in ClrMD. It's no big deal to submit a breaking change to ClrMD, I just released 2.1 for example. If there's reason for a breaking change we can just release 2.2.
Do we even support x86 Linux?
While using an HResult struct might be overly clever, it did eliminate a ton of coding issues in ClrMD. I'm not sure I'm super eager to replace it back with an int, but I'm happy to take a closer look if it's blocking.
@leculver Another approach that does help with the typing issues that people like to mitigate is to use an enum
instead. This permits typing but alas you need to create standalone functions instead of adding them to the type.
Do we even support x86 Linux?
I honestly don't think we do, but there are some vendors that rely upon it I believe. @jkotas or @richlander would recall the specific details.
Do we even support x86 Linux?
Linux x86 is supported by Samsung for Tizen OS.
@leculver Another approach that does help with the typing issues that people like to mitigate is to use an enum instead. This permits typing but alas you need to create standalone functions instead of adding them to the type.
Extension methods work fine with enum for this scenario. Unfortunately there's no extension properties, so one would have to convert the properties to methods, but that would be the extent of a source breaking change to make it an enum.
Runtime related code has different HResult definitions: managed part defines HResult as
struct {int Value}
while native part defines HResult asint
. Generally it is different types and native<->managed conversion should be done explicitly. But in many cases this conversion is processed implicitly relying on expectation of same types representation on specific platform. It could produce bugs on some platforms in some cases.So happened on Linux x86 while
dotnet dump analyze
execution. In particular example native SOSInitializeByHost functioncalls managed QueryInterface method
Native SOSInitializeByHost expects QueryInterface would return
int
:while managed ReversePInvoke for QueryInterface returns
struct {int Value}
:Linux x86 platform processes structure returning in common way, no matter how large the structure is. So native code expects ret value in eax, while QueryInterface expects additional implicit arg with reference to ret structure provided by caller to fill the structure with values and mov to eax reference to the structure.
Linux x86 asm for SOSInitializeByHost and ReversePInvoke
```asm Dump of assembler code for function SOSInitializeByHost: 0xac0335f0 <+0>: push ebp 0xac0335f1 <+1>: mov ebp,esp 0xac0335f3 <+3>: push ebx 0xac0335f4 <+4>: push edi 0xac0335f5 <+5>: push esi 0xac0335f6 <+6>: and esp,0xfffffff0 0xac0335f9 <+9>: sub esp,0x20 0xac0335fc <+12>: mov eax,DWORD PTR [ebp+0x8] 0xac0335ff <+15>: mov ecx,DWORD PTR gs:0x14 0xac033606 <+22>: call 0xac03360bOn other architectures such little structures are returned via standard return value registers (
eax
,r0
). This non-standard platform specific feature allows implicitly convert managed HResultstruct{int}
to native HResultint
anywhere except Linux x86. Feature demonstration on godbolt.I can propose fix for this particular case by https://github.com/t-mustafin/clrmd/commit/98016eb854860b3b4910d720a7dbe243d28eb5ba, but it changes clrmd Public API. Maybe exists another better solution?
cc @jkotas @jakobbotsch @BruceForstall @alpencolt