WebAssembly / wasi-sdk

WASI-enabled WebAssembly C/C++ toolchain
Apache License 2.0
1.29k stars 192 forks source link

Question: Would the maintainers be receptive to a pull request to remove the eager loading of the environment? #365

Closed yowl closed 11 months ago

yowl commented 11 months ago

Hi,

For the component model, eagerly loading the environment presents a problem for runtimes that call _initialize and __wasm_call_ctors This sequence results in the stack and failure:

Caused by:
    0: failed to invoke `run` function
    1: error while executing at wasm backtrace:
           0: 0x18fa7d7 - wit-component:shim!indirect-wasi:cli/environment@0.2.0-rc-2023-10-18-get-environment
           1: 0x18ef789 - wit-component:adapter:wasi_snapshot_preview1!wasi_snapshot_preview1::State::get_environment::h3923f56a626a4100
           2: 0x18ef996 - wit-component:adapter:wasi_snapshot_preview1!environ_sizes_get
           3: 0x18fa80d - wit-component:shim!adapt-wasi_snapshot_preview1-environ_sizes_get
           4: 0x145aac8 - <unknown>!__wasi_environ_sizes_get
           5: 0x145b358 - <unknown>!__wasilibc_initialize_environ
           6: 0x145b3f8 - <unknown>!__wasilibc_initialize_environ_eagerly
           7: 0xcc784c - <unknown>!__wasm_call_ctors
           8: 0xcc787f - <unknown>!_initialize
           9: 0xccb10c - <unknown>!InitializeRuntime()
          10: 0xccb4c6 - <unknown>!Thread::ReversePInvokeAttachOrTrapThread(ReversePInvokeFrame*)
          11: 0xccb5e1 - <unknown>!RhpReversePInvokeAttachOrTrapThread2
          12: 0xccb658 - <unknown>!RhpReversePInvoke
          13: 0x13a86eb - Adder_wit_computer_Intrinsics__cabi_realloc

When calling the component function cabi_realloc it is not permitted to make another component call , but that is what is happening here. Eagerly loading the environment (as I understand it), depends on the presence of extern char** environ which the dotnet runtime includes to support https://learn.microsoft.com/en-us/dotnet/api/system.environment.getenvironmentvariables?view=net-8.0 .

If I make the change here, would it be likely to be flat out rejected?

yowl commented 11 months ago

Sorry, I think this should be in wasi-libc, not here.