bytecodealliance / wasm-tools

CLI and Rust libraries for low-level manipulation of WebAssembly modules
Apache License 2.0
1.29k stars 230 forks source link

Wasm components: implementing cabi_reallock in managed dotnet code presents a problem #1234

Closed yowl closed 3 months ago

yowl commented 11 months ago

Due to cabi_realloc being used from class constructors at initialization, there is a chicken and egg problem when initialising the dotnet runtime. If cabi_realloc is implemented in managed code, the dotnet runtime must be initialised first, however the class ctors for wasi_snapshot_preview1 also want to call cabi_realloc resulting in the following stack and crash

testing "C:\\github\\wit-bindgen-csharp\\target\\runtime-tests\\many_arguments\\csharp-many-arguments\\csharp-wasm.component.wasm"
Error: error while executing at wasm backtrace:
    0: 0x10b9258 - <unknown>!abort
    1: 0x1051e - Assert(char const*, char const*, unsigned int, char const*)
                    at C:/github/runtimelab/artifacts/obj/coreclr/wasi.wasm.Debug/C:/github/runtimelab/src/coreclr/nativeaot/Runtime/rhassert.cpp:24:5
    2: 0x25605 - SList<Thread, DefaultSListTraits<Thread, DoNothingFailFastPolicy> >::Iterator::_Validate(SList<Thread, DefaultSListTraits<Thread, DoNothingFailFastPolicy> >::Iterator::e_ValidateOperation) const
                    at C:/github/runtimelab/artifacts/obj/coreclr/wasi.wasm.Debug/C:/github/runtimelab/src/coreclr/nativeaot/Runtime/./slist.inl:291:9
    3: 0x24034 - SList<Thread, DefaultSListTraits<Thread, DoNothingFailFastPolicy> >::Iterator::Insert(Thread*)
                    at C:/github/runtimelab/artifacts/obj/coreclr/wasi.wasm.Debug/C:/github/runtimelab/src/coreclr/nativeaot/Runtime/./slist.inl:244:5
    4: 0x23e9d - SList<Thread, DefaultSListTraits<Thread, DoNothingFailFastPolicy> >::PushHead(Thread*)
                    at C:/github/runtimelab/artifacts/obj/coreclr/wasi.wasm.Debug/C:/github/runtimelab/src/coreclr/nativeaot/Runtime/./slist.inl:85:13
    5: 0x23d3b - ThreadStore::AttachCurrentThread(bool)
                    at C:/github/runtimelab/artifacts/obj/coreclr/wasi.wasm.Debug/C:/github/runtimelab/src/coreclr/nativeaot/Runtime/threadstore.cpp:144:23
    6: 0x24104 - ThreadStore::AttachCurrentThread()
                    at C:/github/runtimelab/artifacts/obj/coreclr/wasi.wasm.Debug/C:/github/runtimelab/src/coreclr/nativeaot/Runtime/threadstore.cpp:150:5
    7: 0x1eed9 - Thread::ReversePInvokeAttachOrTrapThread(ReversePInvokeFrame*)
                    at C:/github/runtimelab/artifacts/obj/coreclr/wasi.wasm.Debug/C:/github/runtimelab/src/coreclr/nativeaot/Runtime/thread.cpp:1185:9
    8: 0x1f335 - RhpReversePInvokeAttachOrTrapThread2
                    at C:/github/runtimelab/artifacts/obj/coreclr/wasi.wasm.Debug/C:/github/runtimelab/src/coreclr/nativeaot/Runtime/thread.cpp:1318:28
    9: 0x1f59d - RhpReversePInvoke
                    at C:/github/runtimelab/artifacts/obj/coreclr/wasi.wasm.Debug/C:/github/runtimelab/src/coreclr/nativeaot/Runtime/thread.cpp:1332:5
   10: 0xdb57e3 - csharp_wasm_wit_many_arguments_Intrinsics__cabi_realloc
                    at C:\github\wit-bindgen-csharp\target\runtime-tests\many_arguments\csharp-many-arguments\ManyArguments.cs:30
   11: 0x1b27436 - wit-component:adapter:wasi_snapshot_preview1!wasi_snapshot_preview1::State::new::h7ba8d3029d40ab1e
   12: 0x1b204a8 - wit-component:adapter:wasi_snapshot_preview1!wasi_snapshot_preview1::State::ptr::hca605544a6643e5c
   13: 0x1b20f28 - wit-component:adapter:wasi_snapshot_preview1!environ_sizes_get
   14: 0x1b2b597 - wit-component:shim!adapt-wasi_snapshot_preview1-environ_sizes_get
   15: 0x10b9070 - <unknown>!__wasi_environ_sizes_get
   16: 0x10b9900 - <unknown>!__wasilibc_initialize_environ
   17: 0x10b99a0 - <unknown>!__wasilibc_initialize_environ_eagerly
   18: 0x10476 - <unknown>!__wasm_call_ctors
   19: 0x104dc - <unknown>!_initialize
   20: 0x1d423 - InitializeRuntime()
                    at C:/github/runtimelab/artifacts/obj/coreclr/wasi.wasm.Debug/C:/github/runtimelab/src/coreclr/nativeaot/Bootstrap/main.cpp:197:9
   21: 0x1f0d9 - Thread::EnsureRuntimeInitialized()
                    at C:/github/runtimelab/artifacts/obj/coreclr/wasi.wasm.Debug/C:/github/runtimelab/src/coreclr/nativeaot/Runtime/thread.cpp:1220:13
   22: 0x1eed2 - Thread::ReversePInvokeAttachOrTrapThread(ReversePInvokeFrame*)
                    at C:/github/runtimelab/artifacts/obj/coreclr/wasi.wasm.Debug/C:/github/runtimelab/src/coreclr/nativeaot/Runtime/thread.cpp:1182:13
   23: 0x1f335 - RhpReversePInvokeAttachOrTrapThread2
                    at C:/github/runtimelab/artifacts/obj/coreclr/wasi.wasm.Debug/C:/github/runtimelab/src/coreclr/nativeaot/Runtime/thread.cpp:1318:28
   24: 0x1f59d - RhpReversePInvoke
                    at C:/github/runtimelab/artifacts/obj/coreclr/wasi.wasm.Debug/C:/github/runtimelab/src/coreclr/nativeaot/Runtime/thread.cpp:1332:5
   25: 0xdb548c - csharp_wasm_wit_many_arguments_exports_ManyArgumentsWorld__wasmExportManyArguments
                    at C:\github\wit-bindgen-csharp\target\runtime-tests\many_arguments\csharp-many-arguments\ManyArguments.cs:20

Possibly memory.grow can be used instead (which is the idea behind solving the same problem but for the stack allocation)

alexcrichton commented 11 months ago

To complete the breadcrumbs here I'm following up on Zulip

(thanks for opening the issue here though!)

alexcrichton commented 3 months ago

I believe this has since been resolved so I'm going to close, but let me know if there's still lingering issues.