Skip to content
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

std::env::var from guest gives an error: cannot leave component instance #8835

Closed
pimeys opened this issue Jun 18, 2024 · 6 comments
Closed
Labels
bug Incorrect behavior in the current implementation that needs fixing

Comments

@pimeys
Copy link

pimeys commented Jun 18, 2024

Test Case

So, this is pretty weird, and kind of hard to reproduce... But I was able to get my wasm component to panic with a simple call in the guest:

impl Guest for Component {
    fn on_gateway_request(headers: Headers, request: GatewayRequest) -> Result<(), ErrorResponse> {
        dbg!(std::env::var("TEST"));

        Ok(())
    }
}

The host has wasm component model enabled, async support is on and so is fuel consumption. Enabled WASI calls:

  • inherit_env
  • inherit_stdout
  • inherit_stderr

In addition,

Steps to Reproduce

Expected Results

Test would fail normally or at least not panic in guest.

Actual Results

The guest panics on std::env::var call:

thread 'tests::simple_no_io' panicked at gateway/crates/wasi-component-loader/src/tests.rs:28:10:
called `Result::unwrap()` on an `Err` value: Internal(error while executing at wasm backtrace:
    0: 0x1e3cf8 - wit-component:adapter:wasi_snapshot_preview1!wasi_snapshot_preview1::macros::print::h7cf3bf67e3d70272
    1: 0x1e28c9 - wit-component:adapter:wasi_snapshot_preview1!wasi_snapshot_preview1::BumpArena::alloc::he1fbf182c547cac8
    2: 0x1e246e - wit-component:adapter:wasi_snapshot_preview1!cabi_import_realloc
    3: 0x1e6374 - wit-component:shim!indirect-wasi:cli/environment@0.2.0-get-environment
    4: 0x1e2f80 - wit-component:adapter:wasi_snapshot_preview1!wasi_snapshot_preview1::State::get_environment::h3dc87564e4fc1678
    5: 0x1e318a - wit-component:adapter:wasi_snapshot_preview1!environ_sizes_get
    6: 0x1e639c - wit-component:shim!adapt-wasi_snapshot_preview1-environ_sizes_get
    7: 0xf881 - simple.wasm!__wasi_environ_sizes_get
    8: 0xf7cb - simple.wasm!__wasilibc_initialize_environ
    9: 0xf79f - simple.wasm!__wasilibc_ensure_environ
   10: 0xf956 - simple.wasm!getenv
   11: 0xb34c - std::sys::pal::wasi::os::getenv::{{closure}}::h33bc44affa161e82
                    at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys/pal/wasi/os.rs:232:26              - std::sys::pal::common::small_c_string::run_with_cstr_stack::h23ef8d478e26889e
                    at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys/pal/common/small_c_string.rs:49:18              - std::sys::pal::common::small_c_string::run_with_cstr::hd4d5aa4218b3b0e3
                    at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys/pal/common/small_c_string.rs:29:18              - std::sys::pal::wasi::os::getenv::h4705807cae3a5746
                    at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys/pal/wasi/os.rs:230:5              - std::env::_var_os::h2ae2b5d04ee9b40f
                    at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/env.rs:271:5
   12: 0xb4b0 - std::env::var_os::hada13acc4a42ce7a
                    at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/env.rs:267:5              - std::env::_var::h2b1d5a2954263ac1
                    at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/env.rs:234:11
   13: 0x8f83 - std::env::var::hafe1dcfdc420c7d8
                    at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/env.rs:230:5
   14: 0x5efb - <simple::Component as simple::bindings::Guest>::on_gateway_request::he08741f1ce21dd99
                    at /home/pimeys/code/grafbase/grafbase/gateway/crates/wasi-component-loader/examples/crates/simple/src/lib.rs:18:26
   15: 0x6fd0 - simple::bindings::_export_on_gateway_request_cabi::h09965d60ff594d81
                    at /home/pimeys/code/grafbase/grafbase/gateway/crates/wasi-component-loader/examples/crates/simple/src/bindings.rs:11:19
   16: 0x620b - on-gateway-request
                    at /home/pimeys/code/grafbase/grafbase/gateway/crates/wasi-component-loader/examples/crates/simple/src/bindings.rs:68:76

Caused by:
    cannot leave component instance

Stack backtrace:
   0: anyhow::error::<impl anyhow::Error>::msg
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/anyhow-1.0.86/src/error.rs:83:36
   1: anyhow::__private::format_err
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/anyhow-1.0.86/src/lib.rs:689:13
   2: wasmtime::runtime::component::func::host::call_host
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/host.rs:182:9
   3: wasmtime::runtime::component::func::host::HostFunc::entrypoint::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/host.rs:60:17
   4: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panic/unwind_safe.rs:272:9
   5: std::panicking::try::do_call
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:552:40
   6: __rust_try
   7: std::panicking::try
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:516:19
   8: std::panic::catch_unwind
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panic.rs:146:14
   9: wasmtime::runtime::vm::traphandlers::catch_unwind_and_longjmp
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/vm/traphandlers.rs:123:15
  10: wasmtime::runtime::component::func::host::handle_result
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/host.rs:294:11
  11: wasmtime::runtime::component::func::host::HostFunc::entrypoint
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/host.rs:59:13
  12: <unknown>
  13: <unknown>
  14: <unknown>
  15: <unknown>
  16: <unknown>
  17: <(A1,A2,A3,A4) as wasmtime::runtime::func::typed::WasmParams>::invoke::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/func/typed.rs:851:21
  18: <(A1,) as wasmtime::runtime::func::HostAbi>::call
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/func.rs:1898:18
  19: <(A1,A2,A3,A4) as wasmtime::runtime::func::typed::WasmParams>::invoke
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/func/typed.rs:850:17
  20: wasmtime::runtime::func::typed::TypedFunc<Params,Results>::call_raw::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/func/typed.rs:209:17
  21: wasmtime::runtime::vm::traphandlers::catch_traps::call_closure
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/vm/traphandlers.rs:267:18
  22: wasmtime_setjmp_21_0_1
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/vm/helpers.c:66:3
  23: wasmtime::runtime::vm::traphandlers::catch_traps::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/vm/traphandlers.rs:244:13
  24: wasmtime::runtime::vm::traphandlers::<impl wasmtime::runtime::vm::traphandlers::call_thread_state::CallThreadState>::with::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/vm/traphandlers.rs:387:44
  25: wasmtime::runtime::vm::traphandlers::tls::set
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/vm/traphandlers.rs:759:13
  26: wasmtime::runtime::vm::traphandlers::<impl wasmtime::runtime::vm::traphandlers::call_thread_state::CallThreadState>::with
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/vm/traphandlers.rs:387:19
  27: wasmtime::runtime::vm::traphandlers::catch_traps
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/vm/traphandlers.rs:242:18
  28: wasmtime::runtime::func::invoke_wasm_and_catch_traps
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/func.rs:1567:22
  29: wasmtime::runtime::func::typed::TypedFunc<Params,Results>::call_raw
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/func/typed.rs:205:22
  30: wasmtime::runtime::component::func::options::Options::realloc
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/options.rs:107:31
  31: wasmtime::runtime::component::func::options::LowerContext<T>::realloc
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/options.rs:252:9
  32: wasmtime::runtime::component::func::typed::lower_string
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/typed.rs:1148:23
  33: <str as wasmtime::runtime::component::func::typed::Lower>::store
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/typed.rs:1112:26
  34: <alloc::string::String as wasmtime::runtime::component::func::typed::Lower>::store
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/typed.rs:696:17
  35: <(A1,A2) as wasmtime::runtime::component::func::typed::Lower>::store
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/typed.rs:2410:21
  36: wasmtime::runtime::component::func::typed::Lower::store_list
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/typed.rs:576:13
  37: wasmtime::runtime::component::func::typed::lower_list
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/typed.rs:1496:5
  38: <[T] as wasmtime::runtime::component::func::typed::Lower>::store
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/typed.rs:1460:26
  39: <alloc::vec::Vec<T> as wasmtime::runtime::component::func::typed::Lower>::store
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/typed.rs:696:17
  40: <(A1,) as wasmtime::runtime::component::func::typed::Lower>::store
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/typed.rs:2410:21
  41: wasmtime::runtime::component::func::host::call_host::Storage<P,R>::lower_results
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/host.rs:266:21
  42: wasmtime::runtime::component::func::host::call_host
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/host.rs:219:5
  43: wasmtime::runtime::component::func::host::HostFunc::entrypoint::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/host.rs:60:17
  44: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panic/unwind_safe.rs:272:9
  45: std::panicking::try::do_call
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:552:40
  46: __rust_try
  47: std::panicking::try
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:516:19
  48: std::panic::catch_unwind
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panic.rs:146:14
  49: wasmtime::runtime::vm::traphandlers::catch_unwind_and_longjmp
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/vm/traphandlers.rs:123:15
  50: wasmtime::runtime::component::func::host::handle_result
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/host.rs:294:11
  51: wasmtime::runtime::component::func::host::HostFunc::entrypoint
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/host.rs:59:13
  52: <unknown>
  53: <unknown>
  54: <unknown>
  55: <unknown>
  56: <unknown>
  57: <unknown>
  58: <unknown>
  59: <unknown>
  60: <unknown>
  61: <unknown>
  62: <unknown>
  63: <unknown>
  64: <unknown>
  65: <unknown>
  66: <unknown>
  67: <unknown>
  68: wasmtime::runtime::func::Func::call_unchecked_raw::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/func.rs:1045:13
  69: wasmtime::runtime::vm::traphandlers::catch_traps::call_closure
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/vm/traphandlers.rs:267:18
  70: wasmtime_setjmp_21_0_1
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/vm/helpers.c:66:3
  71: wasmtime::runtime::vm::traphandlers::catch_traps::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/vm/traphandlers.rs:244:13
  72: wasmtime::runtime::vm::traphandlers::<impl wasmtime::runtime::vm::traphandlers::call_thread_state::CallThreadState>::with::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/vm/traphandlers.rs:387:44
  73: wasmtime::runtime::vm::traphandlers::tls::set
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/vm/traphandlers.rs:759:13
  74: wasmtime::runtime::vm::traphandlers::<impl wasmtime::runtime::vm::traphandlers::call_thread_state::CallThreadState>::with
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/vm/traphandlers.rs:387:19
  75: wasmtime::runtime::vm::traphandlers::catch_traps
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/vm/traphandlers.rs:242:18
  76: wasmtime::runtime::func::invoke_wasm_and_catch_traps
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/func.rs:1567:22
  77: wasmtime::runtime::func::Func::call_unchecked_raw
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/func.rs:1043:9
  78: wasmtime::runtime::component::func::Func::call_raw
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func.rs:526:13
  79: wasmtime::runtime::component::func::typed::TypedFunc<Params,Return>::call_impl
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/typed.rs:215:17
  80: wasmtime::runtime::component::func::typed::TypedFunc<Params,Return>::call_async::{{closure}}::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/typed.rs:190:31
  81: wasmtime::runtime::store::<impl wasmtime::runtime::store::context::StoreContextMut<T>>::on_fiber::{{closure}}::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/store.rs:2061:34
  82: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/alloc/src/boxed.rs:2020:9
  83: wasmtime_fiber::Suspend<Resume,Yield,Return>::execute::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-fiber-21.0.1/src/lib.rs:189:62
  84: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panic/unwind_safe.rs:272:9
  85: std::panicking::try::do_call
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:552:40
  86: __rust_try
  87: std::panicking::try
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:516:19
  88: std::panic::catch_unwind
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panic.rs:146:14
  89: wasmtime_fiber::Suspend<Resume,Yield,Return>::execute
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-fiber-21.0.1/src/lib.rs:189:22
  90: wasmtime_fiber::unix::fiber_start
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-fiber-21.0.1/src/unix.rs:203:9
  91: wasmtime_fiber_start_21_0_1
  92: wasmtime_fiber::unix::asan_disabled::fiber_switch
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-fiber-21.0.1/src/unix.rs:476:9
  93: wasmtime_fiber::unix::Fiber::resume
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-fiber-21.0.1/src/unix.rs:229:13
  94: wasmtime_fiber::Fiber<Resume,Yield,Return>::resume
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-fiber-21.0.1/src/lib.rs:142:9
  95: wasmtime::runtime::store::<impl wasmtime::runtime::store::context::StoreContextMut<T>>::on_fiber::{{closure}}::FiberFuture::resume
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/store.rs:2177:28
  96: <wasmtime::runtime::store::<impl wasmtime::runtime::store::context::StoreContextMut<T>>::on_fiber::{{closure}}::FiberFuture as core::future::future::Future>::poll
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/store.rs:2224:27
  97: wasmtime::runtime::store::<impl wasmtime::runtime::store::context::StoreContextMut<T>>::on_fiber::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/store.rs:2076:16
  98: wasmtime::runtime::component::func::typed::TypedFunc<Params,Return>::call_async::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-21.0.1/src/runtime/component/func/typed.rs:191:14
  99: wasi_component_loader::callbacks::gateway::GatewayCallbackInstance::call::{{closure}}
             at ./src/callbacks/gateway.rs:98:18
 100: wasi_component_loader::ComponentLoader::on_gateway_request::{{closure}}
             at ./src/lib.rs:89:25
 101: wasi_component_loader::tests::simple_no_io::{{closure}}
             at ./src/tests.rs:27:10
 102: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/future/future.rs:123:9
 103: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/future/future.rs:123:9
 104: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:659:57
 105: tokio::runtime::coop::with_budget
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/coop.rs:107:5
 106: tokio::runtime::coop::budget
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/coop.rs:73:5
 107: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:659:25
 108: tokio::runtime::scheduler::current_thread::Context::enter
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:404:19
 109: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:658:36
 110: tokio::runtime::scheduler::current_thread::CoreGuard::enter::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:737:68
 111: tokio::runtime::context::scoped::Scoped<T>::set
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/context/scoped.rs:40:9
 112: tokio::runtime::context::set_scheduler::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/context.rs:180:26
 113: std::thread::local::LocalKey<T>::try_with
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/thread/local.rs:284:16
 114: std::thread::local::LocalKey<T>::with
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/thread/local.rs:260:9
 115: tokio::runtime::context::set_scheduler
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/context.rs:180:9
 116: tokio::runtime::scheduler::current_thread::CoreGuard::enter
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:737:27
 117: tokio::runtime::scheduler::current_thread::CoreGuard::block_on
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:646:19
 118: tokio::runtime::scheduler::current_thread::CurrentThread::block_on::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:175:28
 119: tokio::runtime::context::runtime::enter_runtime
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/context/runtime.rs:65:16
 120: tokio::runtime::scheduler::current_thread::CurrentThread::block_on
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:167:9
 121: tokio::runtime::runtime::Runtime::block_on
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/runtime.rs:347:47
 122: wasi_component_loader::tests::simple_no_io
             at ./src/tests.rs:34:5
 123: wasi_component_loader::tests::simple_no_io::{{closure}}
             at ./src/tests.rs:6:24
 124: core::ops::function::FnOnce::call_once
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ops/function.rs:250:5
 125: core::ops::function::FnOnce::call_once
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ops/function.rs:250:5
 126: test::__rust_begin_short_backtrace
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/test/src/lib.rs:621:18
 127: test::run_test_in_process::{{closure}}
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/test/src/lib.rs:644:60
 128: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panic/unwind_safe.rs:272:9
 129: std::panicking::try::do_call
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:552:40
 130: std::panicking::try
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:516:19
 131: std::panic::catch_unwind
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panic.rs:146:14
 132: test::run_test_in_process
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/test/src/lib.rs:644:27
 133: test::run_test::{{closure}}
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/test/src/lib.rs:567:43
 134: test::run_test::{{closure}}
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/test/src/lib.rs:595:41
 135: std::sys_common::backtrace::__rust_begin_short_backtrace
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:155:18
 136: std::thread::Builder::spawn_unchecked_::{{closure}}::{{closure}}
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/thread/mod.rs:528:17
 137: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panic/unwind_safe.rs:272:9
 138: std::panicking::try::do_call
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:552:40
 139: std::panicking::try
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:516:19
 140: std::panic::catch_unwind
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panic.rs:146:14
 141: std::thread::Builder::spawn_unchecked_::{{closure}}
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/thread/mod.rs:527:30
 142: core::ops::function::FnOnce::call_once{{vtable.shim}}
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ops/function.rs:250:5
 143: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/alloc/src/boxed.rs:2020:9
 144: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/alloc/src/boxed.rs:2020:9
 145: std::sys::pal::unix::thread::Thread::new::thread_start
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys/pal/unix/thread.rs:108:17
 146: start_thread
 147: __GI___clone3)
stack backtrace:
   0: rust_begin_unwind
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panicking.rs:72:14
   2: core::result::unwrap_failed
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/result.rs:1654:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/result.rs:1077:23
   4: wasi_component_loader::tests::simple_no_io::{{closure}}
             at ./src/tests.rs:25:30
   5: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/future/future.rs:123:9
   6: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/future/future.rs:123:9
   7: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:659:57
   8: tokio::runtime::coop::with_budget
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/coop.rs:107:5
   9: tokio::runtime::coop::budget
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/coop.rs:73:5
  10: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:659:25
  11: tokio::runtime::scheduler::current_thread::Context::enter
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:404:19
  12: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:658:36
  13: tokio::runtime::scheduler::current_thread::CoreGuard::enter::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:737:68
  14: tokio::runtime::context::scoped::Scoped<T>::set
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/context/scoped.rs:40:9
  15: tokio::runtime::context::set_scheduler::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/context.rs:180:26
  16: std::thread::local::LocalKey<T>::try_with
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/thread/local.rs:284:16
  17: std::thread::local::LocalKey<T>::with
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/thread/local.rs:260:9
  18: tokio::runtime::context::set_scheduler
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/context.rs:180:9
  19: tokio::runtime::scheduler::current_thread::CoreGuard::enter
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:737:27
  20: tokio::runtime::scheduler::current_thread::CoreGuard::block_on
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:646:19
  21: tokio::runtime::scheduler::current_thread::CurrentThread::block_on::{{closure}}
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:175:28
  22: tokio::runtime::context::runtime::enter_runtime
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/context/runtime.rs:65:16
  23: tokio::runtime::scheduler::current_thread::CurrentThread::block_on
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/scheduler/current_thread/mod.rs:167:9
  24: tokio::runtime::runtime::Runtime::block_on
             at /home/pimeys/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.38.0/src/runtime/runtime.rs:347:47
  25: wasi_component_loader::tests::simple_no_io
             at ./src/tests.rs:34:5
  26: wasi_component_loader::tests::simple_no_io::{{closure}}
             at ./src/tests.rs:6:24
  27: core::ops::function::FnOnce::call_once
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ops/function.rs:250:5
  28: core::ops::function::FnOnce::call_once
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Versions and Environment

Wasmtime version or commit: 21.0.1

Operating system: NixOS unstable

Architecture: x86-64 and wasm32-wasi for the guest

@pimeys pimeys added the bug Incorrect behavior in the current implementation that needs fixing label Jun 18, 2024
@pimeys
Copy link
Author

pimeys commented Jun 18, 2024

To be fair, I have a separate test project that does not call the guest from rust tests. I tried to load the env var in there inside the guest, and it worked as expected.

@bjorn3
Copy link
Contributor

bjorn3 commented Jun 18, 2024

If I understand it correctly, the issue is here:

    0: 0x1e3cf8 - wit-component:adapter:wasi_snapshot_preview1!wasi_snapshot_preview1::macros::print::h7cf3bf67e3d70272
    1: 0x1e28c9 - wit-component:adapter:wasi_snapshot_preview1!wasi_snapshot_preview1::BumpArena::alloc::he1fbf182c547cac8
    2: 0x1e246e - wit-component:adapter:wasi_snapshot_preview1!cabi_import_realloc
    3: 0x1e6374 - wit-component:shim!indirect-wasi:cli/environment@0.2.0-get-environment
    4: 0x1e2f80 - wit-component:adapter:wasi_snapshot_preview1!wasi_snapshot_preview1::State::get_environment::h3dc87564e4fc1678

To be precise the adapter for indirect-wasi:cli/environment@0.2.0-get-environment calls back into your wasm component to allocate memory. While this happens it is not allowed to call any imports, but wasi_snapshot_preview1::macros::print is called here. As for why that happens, it is likely that BumpArena::alloc ran out of memory and tried to panic. If so this should have been fixed by #8594. You will need to update cargo-component to get this fix though as the bug is inside the wasip1 to wasip2 adapter which cargo-component includes in the wasm component itself.

@alexcrichton
Copy link
Member

I believe that @bjorn3 is correct (thanks!), and this is definitely a frustration I've had in the past with debugging the adapter too.

I've confirmed that by default if the environment variable block is too big then cargo-component-produced components fail with this error (as the allocation is too big), but if I use package.metadata.component.adapter to point to the latest version of the adapter it works.

Given that I think that this should be fixable by updating the adapter you're using. Would you be able to test that out @pimeys?

@pimeys
Copy link
Author

pimeys commented Jun 18, 2024

Sure, my work on this continues tomorrow. Could you point me to instructions on how exactly I should do this? Eventually our users will also need to build these modules, so we might need something simpler for them to manage.

Maybe if we let the users configure exactly the variables they need, they could go around the issues until the fix lands into stable.

@alexcrichton
Copy link
Member

Sure yeah, this was a relatively recent bug fix so you'll want to grab the adapter from here. For your use case you'll want the *.reactor.wasm one (link). Next you'll add this to the Cargo.toml in the project that's built with cargo component:

[package.metadata.component]
adapter = "path/to/wasi_snapshot_preview1.reactor.wasm"

Then cargo component build will pick up that adapter and use it which should have the fix. The built-in one to cargo-component can also be updated soon too.

alexcrichton added a commit to alexcrichton/cargo-component that referenced this issue Jun 18, 2024
Help to fix issues like bytecodealliance/wasmtime#8835 by fixing a bug
in the adapter where over-large environment blocks could cause a panic.
@pimeys
Copy link
Author

pimeys commented Jun 19, 2024

Thanks @alexcrichton, this solved my test issues.

calvinrp pushed a commit to bytecodealliance/cargo-component that referenced this issue Jun 19, 2024
Help to fix issues like bytecodealliance/wasmtime#8835 by fixing a bug
in the adapter where over-large environment blocks could cause a panic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Incorrect behavior in the current implementation that needs fixing
Projects
None yet
Development

No branches or pull requests

3 participants