-
Notifications
You must be signed in to change notification settings - Fork 195
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
c#: dotnet Runtime not started correctly when using strings (or heap objects) as first call to exported function #777
Comments
So presumably we need some initialization to happen in the start function that sets up the allocator. |
It's not clear to me what is actually failing here. In frame 9 we can see that it is attempting to initialize the runtime, but it fails in wasmtime as part of calling the c++ class ctors. Would need to attach lldb to a debug build of wasmtime I think to understand more. |
@SteveSandersonMS we discussed this a bit in our meeting today. Do you have a sample that you used to produce this error? |
@jsturtevant Yes, it's this sample: https://github.com/SteveSandersonMS/wasm-component-sdk/blob/main/samples/calculator/CalculatorHost/Program.cs If you were to move line 8 to before line 5, so it calls the string function before the int function, then it will fail with the error above. The sample only works because it calls the int function before the string function, even though they shouldn't affect each other. That repo should be reasonably straightforwards to build (just clone and |
One suggestion (by @ricochet) that came up during the call was we might be using the wrong adapter type (command vs reactor). It looks like this might be using reactor by default https://github.com/SteveSandersonMS/wasm-component-sdk/blob/4866a47faebd92db8e7450c4d17ae9d55129b663/src/WasmComponent.Sdk/build/WasmComponent.Sdk.targets#L31 |
For NativeAOT-LLVM reactor is what we should use I think, as we have no main. We can't call back to the component functions like getting environment variables from within |
I would expect to use command for |
For reference, this is how the Rust generator addresses it: https://github.com/bytecodealliance/wit-bindgen/blob/main/crates/guest-rust/src/lib.rs#L25-L52 |
This was another example mentioned today: https://github.com/bytecodealliance/javy/pull/481/files |
Confirmed! SDK works great. I had a minor issue with download emsdk, maybe because I already has the EMSDK env set up. Make sure to use wasmtime 14 and not 15 as some function types have changed. I will try the |
I was trying to investigate a bit more, and found the following. Is this issue actually just a duplicate of dotnet/runtimelab#2359 ? |
I don't think so. That initialization problem with the dotnet runtime was resolved. The problem here is that the dotnet runtime initialises the Wasi SDK, and it's the WASI SDK that cannot be initialised from cabi_realloc. Other languages either don't use the WASI SDK, or they treat cabi_realloc as "special". I suspect we would get push pack if we tried to treat the UCO (UnmanagedCallersOnly) cabi_realloc as special from upstream and I'm not convinced we could make |
I've asked at WebAssembly/wasi-sdk#365 whether Wasi SDK would be open to accepting a change for this. Alternatively we might be able to disable https://learn.microsoft.com/en-us/dotnet/api/system.environment.getenvironmentvariables?view=net-8.0. I think the real solution is to use |
This should be fixed by #791 where we are generating |
One thing that doesn't seem to work quite right is the current .NET implementation of
cabi_realloc
. It doesn't seem to start the runtime correctly. So, if you're exporting a function that accepts a string, and the very first call into your preview2 component is to that method, the very first thing it will do is callcabi_realloc
which will fail with something like this:As a workaround, if you first call any other export that only deals with
int
or other non-heap-objects, that will initialize the runtime correctly, and thencabi_realloc
will no longer crash so you can then call exports that acceptstring
.Originally posted by @SteveSandersonMS in #760 (comment)
The text was updated successfully, but these errors were encountered: