Stream: wasmtime

Topic: WASM memory limit


view this post on Zulip Smilet (Oct 16 2023 at 06:04):

Hi there! I'm trying to incorporate WASM into my project, but I seem to be running into some kind of memory limit for the WASM instance.

To test this, I created a WASM module that allocates new memory on an infinite loop:

use std::collections::HashMap;

fn main() {
    println!("Main function");

    let mut dynamic_hashmap: HashMap<u128, Vec<u128>> = HashMap::new();

    for i in 0.. {
        println!("Allocating {}", i);

        dynamic_hashmap.insert(i, vec![0u128; 1]);

        println!("Done with {}", i);
    }
}

Here's the code that I use to execute this WASM file:

use wasmtime::*;
use wasmtime_wasi::sync::WasiCtxBuilder;

fn main() {
    let mut config = Config::default();
    config.wasm_backtrace_details(WasmBacktraceDetails::Enable);

    let engine = Engine::new(&config).unwrap();

    let mut linker = Linker::new(&engine);
    wasmtime_wasi::add_to_linker(&mut linker, |s| s).unwrap();

    let module = Module::from_file(
        &engine,
        "../wasi-module/target/wasm32-wasi/release/wasi-module.wasm",
    )
    .expect("Failed to load WASM code");

    let wasi = WasiCtxBuilder::new()
        .inherit_stdio()
        .inherit_args()
        .unwrap()
        .build();

    let mut store = Store::new(&engine, wasi);
    let instance = linker.instantiate(&mut store, &module).unwrap();
    let start = instance.get_func(&mut store, "_start").unwrap();

    start.call(&mut store, &[], &mut []).unwrap();
}

And this is the output that I get:

Main function
<skipping the middle part of the output>
Done with 446
Allocating 447
Done with 447
Allocating 448
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: wasm trap: out of bounds memory access
wasm backtrace:
    0: 0xa4a6 - <unknown>!memset
    1:  0xaef - <unknown>!hashbrown::raw::RawTable<T,A>::reserve_rehash::h26bc07c215e67f39
    2: 0x11fc - <unknown>!hashbrown::map::HashMap<K,V,S,A>::insert::hcab14c774053cb72
    3:  0xf02 - <unknown>!wasi_module::main::h5008638d866d37b9
    4:  0xcde - <unknown>!std::sys_common::backtrace::__rust_begin_short_backtrace::hce02bccd1261f8e9
    5:  0xd00 - <unknown>!std::rt::lang_start::{{closure}}::h09d9dc18df59a544
    6: 0x332e - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h6e78117b8a9d340b
                    at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/core/src/ops/function.rs:284:13
              - std::panicking::try::do_call::h06e5708aa9e78a41
                    at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/std/src/panicking.rs:500:40
              - std::panicking::try::h30762a74ac0bad1d
                    at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/std/src/panicking.rs:464:19
              - std::panic::catch_unwind::hca60c708f597587b
                    at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/std/src/panic.rs:142:14
              - std::rt::lang_start_internal::{{closure}}::ha8ed0958145e857d
                    at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/std/src/rt.rs:148:48
              - std::panicking::try::do_call::h43211fa41386895b
                    at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/std/src/panicking.rs:500:40
              - std::panicking::try::hb63d2e2c3bcb8e02
                    at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/std/src/panicking.rs:464:19
              - std::panic::catch_unwind::h94d3bed564bf5a92
                    at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/std/src/panic.rs:142:14
              - std::rt::lang_start_internal::h11733158b4539e84
                    at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/std/src/rt.rs:148:20
    7:  0xfbf - <unknown>!__main_void
    8:  0x366 - <unknown>!_start
', src/main.rs:29:42
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

view this post on Zulip Alex Crichton (Oct 16 2023 at 13:39):

Curious! If I run that Rust program via wasmtime run, which i believe is effectively the same as the embedding you've written above, then it fails after a long time with "Hash table capacity overflow" which is what I was expecting. (or an OOM, something like that).

Can you share some more details? For example could you share the exact wasm module that you're running? Additionally could you share details about your OS/Wasmtime version and such?


Last updated: Dec 23 2024 at 13:07 UTC