-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
(Unmanaged) Structs are not considered blittable by Emscripten / Blazor WASM AOT #61146
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to 'arch-wasm': @lewing Issue DetailsDescriptionUnmanaged structs are not considered blittable and therefore cannot be used in pinvoke callbacks. Reproduction StepsAttempt to compile code like public struct S { public int Value; }
[UnmanagedCallersOnly]
public void M(S myStruct) { } using Emscripten / WASM AOT Expected behaviorThe above code should compile. Actual behaviorAn error is shown, structs are not considered blittable. Regression?No response Known WorkaroundsNo response ConfigurationTested on .NET 6 RC2 Other informationNo response
|
This is an error from the AOT tasks, maybe? Console sample using Mono works (see https://gist.github.com/lambdageek/f289259398a33cdf19811d2ab4eefd35): $ dotnet run
Hello, World! from Mono
Called with 42 /cc @radical |
Note that this is also related to #60639 - in particular we will need to follow the runtime's (updated) per-assembly rules for what is blittable |
This could be considered an enhancement. One current workaround is to pass a pointer to the struct: public struct S {
public int i;
}
public class P {
public static void Main () {
unsafe {
Q (&M);
}
}
[UnmanagedCallersOnly]
public unsafe static void M (IntPtr myStruct) {
ref S s = ref Unsafe.AsRef<S>((void*)myStruct);
Console.WriteLine ($"Called with {s.i}");
}
[DllImport("Foo")]
public static extern unsafe void Q (delegate *unmanaged<IntPtr,void> fn);
} And correspondingly use a pointer on the native side: typedef struct S {
int i;
} S;
typedef void (*fn)(S* s);
void Q (fn f);
void Q (fn f) {
S s;
s.i = 42;
f (&s);
} |
Given the workaround requires changes to the native side, which isn't always possible (ie binding to standardized or otherwise externally defined APIs), I would not consider this as an enhancement, but rather a missing feature that is important to making interop in WASM fully functional. |
The error still persists. Tried in Blazor wasm app with |
I think we should. /cc @lewing @radical @ilonatommy can you try to get a binlog and the error message? It's presumably something in the |
runtime/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs Lines 344 to 349 in d24bf09
Assigning to @maraf as he was working on the table generation. |
Sure. So my publish Debug produced:
repro: https://github.com/ilonatommy/StructsAot_61146_BlazorWasm_net7p4 |
Description
Unmanaged structs are not considered blittable and therefore cannot be used in pinvoke callbacks.
Reproduction Steps
Attempt to compile code like
using Emscripten / WASM AOT
Expected behavior
The above code should compile.
Actual behavior
An error is shown, structs are not considered blittable.
Regression?
No response
Known Workarounds
No response
Configuration
Tested on .NET 6 RC2
Other information
No response
The text was updated successfully, but these errors were encountered: