-
Notifications
You must be signed in to change notification settings - Fork 257
fix(csharp): Add runtime initialization guard for reactor components #1489
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
base: main
Are you sure you want to change the base?
fix(csharp): Add runtime initialization guard for reactor components #1489
Conversation
Adds infrastructure to call runtime initialization before exported functions execute. This addresses the "uninitialized element" trap that occurs when calling exports on standalone .NET library components. Note: This fix adds the correct initialization call pattern, but full resolution requires additional changes in the NativeAOT-LLVM runtime or componentize-dotnet adapter layer to handle the circular dependency where RhpReversePInvoke needs initialized tables before initialization can complete. Changes: - Add `has_exports` flag to track when exports are generated - Generate `WasmInteropInitializer` class with thread-safe init guard - Call `EnsureInitialized()` at the start of every export function - Initializer calls `__wasm_call_ctors` to trigger runtime init Related: bytecodealliance/componentize-dotnet#103 Related: bytecodealliance/jco#1173
|
@dirkwa looking at ci failures it seems some of the examples need to bring in the proper imports for this new functionality or it needs to be added to the import list in the binding generator |
|
Is there a repo or a stack trace? |
See here: |
|
So would this be fixed by moving to wasi SDK 29 ? |
|
Note that NAOT-LLVM upgraded to wasi sdk 29 today, so the nugets for that should be out in a few hours. |
I hope so, if the commit will be accepted by the maintainers. Nevertheless this PR has a reason.If you look at the full bytecodealliance/jco#1173 there is my test-repo to locally verify it. With the workaround in the comment I was able to sucessfully build it. |
Summary
This PR adds initialization guard infrastructure to the C# wit-bindgen code generator for reactor (library) components. It ensures
__wasm_call_ctorsis called before any exported function executes.Problem
When building .NET WASM components as libraries (reactors without a
_startentry point), calling exports directly fails with:wasmtime:
wasm trap: uninitialized element wasm backtrace: 0: InitializeGlobalTablesForModule
jco/Node.js:
RuntimeError: null function or function signature mismatch at InitializeGlobalTablesForModule
Root Cause Analysis
The issue involves a circular dependency in the .NET NativeAOT-LLVM runtime:
_startto trigger runtime initializationRhpReversePInvokeattempts to initialize the runtimeRhpReversePInvokeitself requires initialized function tablesThis PR's Contribution
This PR adds the correct initialization pattern at the wit-bindgen layer:
Each generated export function calls EnsureInitialized() first:
Limitations
This fix alone does not fully resolve the issue. The complete solution requires changes in:
This PR establishes the correct pattern so that when the runtime-level fix is implemented, the initialization will be properly triggered.
Testing
Related Issues
Both issues report the same root cause - .NET reactor components crash when exports are called directly because runtime initialization hasn't occurred properly.