-
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
[wasm] Marshaling bool within struct with [MarshalAs(UnmanagedType.U1)] throws System.InvalidProgramException #107546
Comments
Tagging subscribers to 'arch-wasm': @lewing |
/cc @pavelsavara |
cc @kg |
I believe the root cause here is that Also note that As a workaround, consider wrapping the bytes with bool properties. That would maintain a blittable struct (with private byte fields) that has a public interface with bools in it. |
you might be able to set the DisableRuntimeMarshalingAttribute on the assembly to make a struct containing a bool blittable (the MarshalAsAttribute must not be used then - it will use the underlying type (ie byte) as the marshaling type) #60639 this is assuming all your pinvokes in the assembly are using the source generator |
this is by design, right ? |
Description
I am still determining if this is a runtime bug, but since this code works everywhere except Blazor WASM, I have to ask.
I am working on a program with a native library. It includes the following struct:
https://github.com/drasticactions/DA.Whisper/blob/wasm-bug/src/DA.Whisper/NativeMethods.g.cs#L380C5-L391C6
This can be retrieved by calling
NativeMethods.whisper_context_default_params();
to get the default values. I have built the native library on Windows, Mac, and Linux and invoked it in .NET (with and without NativeAOT enabled), and it has worked fine. Nothing is thrown, and I can manipulate the object and send it back though theNativeMethods
.However, when I built Whisper.cpp with Emscripten and ran it in web assembly, any time I called for any struct that included a
bool
, I would get aSystem.InvalidProgramException
with a blank message.I am unsure how to get better exceptions out of this, this is all I've been able to get. Other functions of accessing the library that don't involve these structs works as expected. Once I saw that it only through this message when I accessed the struct, I changed it to remove the Marshaling and set it to
byte
https://github.com/drasticactions/DA.Whisper/blob/wasm-bug/src/DA.Whisper/NativeMethods.g.cs#L367-L378
Everything started working! I got the struct, manipulated it, and sent it back through
NativeMethods
and Whisper loaded it fine.So I think that means there is a bug with
[MarshalAs(UnmanagedType.U1)]
and WASM. That should a byte (and indeed, it works on the other platforms I've tried, although maybe that's a mistake too?). I wish to keep my code the same to use the marshal value instead of byte (since it works everywhere else). Is this a bug, or did I make a mistake?I checked with both
net8.0
andnet9.0-preview7
and both fail with the same exception.Reproduction Steps
wasm-bug
branchmake wasm
(or use my provided runtimes, replace theruntime
folder with thisruntime.zip)
src\DA.WhisperBlazor
There are two buttons, one which invokes the Marshaled byte struct, the other which does not. Clicking on the First button should go through right, clicking the second should throw an exception.
Expected behavior
I can access Bool values in Native code via
[MarshalAs(UnmanagedType.U1)]
Actual behavior
System.InvalidProgramException
thrown.Regression?
No response
Known Workarounds
Don't use
[MarshalAs(UnmanagedType.U1)]
but use the literal typebyte
instead.Configuration
Other information
No response
The text was updated successfully, but these errors were encountered: