-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[mono][wasm] DllImport with value-type args that contain unsupported marshalled fields crashes AOT compiler #104463
Comments
cc @steveisok |
I modified a test for our mobile targets and the aot compiler produces the same error. I believe this might be a linker issue. I think the challenge is easily detecting when it's a COM interop scenario vs a legit native library import. @vitek-karas, is that correct? |
There's some heuristic in the trimmer to detect incompatible COM interop, but it's just a heuristic: runtime/src/tools/illink/src/linker/Linker.Dataflow/ReflectionMethodBodyScanner.cs Line 193 in 99ec785
In this case it fails to detect the problem because the struct is passed by-ref, and we didn't implement the heuristic to look "inside" references. But there are many other ways this can fail. Writing a full solution would be more work and it would need to copy lot of rules otherwise only known at runtime. Regardless, WASM has trim warnings disabled by default anyway, so I would expect that this would not be visible to the user even if it worked. Ultimately, I don't think this is something the trimmer should try to solve - we added this to only help people detect possible issues - by producing diagnostics. The AOT compiler should not fail in this case, and that has nothing to do with the trimmer. If the compiler can't compile a PInvoke, it should instead replace it with throwing method - like it already does in several other cases. I don't know if we have some custom steps which should do this at trimmer time, but I'd say they would also be a "best-guess", as they won't know the full set of capabilities the AOT compiler has. |
…_CONV_OBJECT_IUNKNOWN This change prevents the aot compiler from erroring out when dealing with COM types that were not marked with MarshalAs attributes. In most scenarios that we support, we want to allow pinvokes to aot compile as in cases like anything COM interop, will end up erroring out if you try to use it. Fixes dotnet#104463
Description
Normally, Mono WASM replaces unsupported DllImports with stubs, that throw an exception in runtime.
For example, if I declare any parameter with
[MarshalAs(UnmanagedType.IUnknown)]
in the DllImport:It will work as expected - compiler won't crash, and code is successfully executed unless this DllImport method is actually called in runtime.
But if I move
[MarshalAs(UnmanagedType.IUnknown)]
to the argument struct, Mono AOT compiler crashes withmarshalling conversion 27 not implemented
error.In the example below, I used
ReleaseStgMedium
andSTGMEDIUM
just to reproduce the issue.It crashes because STGMEDIUM struct is defined with unsupported marshalling type:
Reproduction Steps
WasmRepro.zip
Expected behavior
Assembly and the app are compiled successfully.
Ideally, compiler outputs a warning/message about unsupported DllImport.
Faulty DllImport is replaced with a stub that throws in runtime, instead of crashing the compiler.
Actual behavior
Regression?
As far as I can tell, the same behavior was in .NET 8 and .NET 7.
Known Workarounds
Use Cecil or trimming descriptor files to exclude these specific dll imports from compilation.
Configuration
.NET 9 preview5
Windows 11
browser wasm
any browser
Other information
Other platform RIDs, like linux, macOS as well as mobile mono do support such DllImports without issues as far as I can tell.
It causes inconsistency
In ideal world, where we have access to the fauly code, we would replace these DllImports with safer alternatives or added OperatingSystem.IsWindows checks. But we can't do that, as in real world this code is hidden somewhere inside closed source third party dependency, making it completely unsable.
The text was updated successfully, but these errors were encountered: