diff --git a/.cargo/config.toml b/.cargo/config.toml index 635229119f8..ceb8134791c 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -6,3 +6,19 @@ rustflags = "-C link-args=-Wl,--stack,8000000" [target.wasm32-unknown-unknown] rustflags = ["--cfg=getrandom_backend=\"wasm_js\""] + +# Enforce a 64 MB memory cap and 1 MB stack limit for WASI targets. +# Without these, the WASM heap can grow unboundedly (e.g., a simple +# `a = 1` allocated ~79 MB) and crash on constrained runtimes like wasmi. +# See issue #4989. +[target.wasm32-wasip1] +rustflags = [ + "-C", "link-arg=--max-memory=67108864", + "-C", "link-arg=-zstack-size=1048576", +] + +[target.wasm32-wasip2] +rustflags = [ + "-C", "link-arg=--max-memory=67108864", + "-C", "link-arg=-zstack-size=1048576", +] diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 2a6a5264c21..1da4cb1ba64 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -790,11 +790,11 @@ jobs: clang: true - name: build rustpython - run: cargo build --release --target wasm32-wasip1 --no-default-features --features freeze-stdlib,stdlib,stdio,importlib,host_env --verbose + run: cargo build --profile wasm-release --target wasm32-wasip1 --no-default-features --features freeze-stdlib,stdlib,stdio,importlib,host_env --verbose - name: run snippets - run: wasmer run --dir "$(pwd)" target/wasm32-wasip1/release/rustpython.wasm -- "$(pwd)/extra_tests/snippets/stdlib_random.py" + run: wasmer run --dir "$(pwd)" target/wasm32-wasip1/wasm-release/rustpython.wasm -- "$(pwd)/extra_tests/snippets/stdlib_random.py" - name: run cpython unittest - run: wasmer run --dir "$(pwd)" target/wasm32-wasip1/release/rustpython.wasm -- "$(pwd)/Lib/test/test_int.py" + run: wasmer run --dir "$(pwd)" target/wasm32-wasip1/wasm-release/rustpython.wasm -- "$(pwd)/Lib/test/test_int.py" cargo_doc: needs: @@ -831,3 +831,4 @@ jobs: - name: cargo doc run: cargo doc --locked + diff --git a/Cargo.toml b/Cargo.toml index a63656ebea7..c0ec19e0fa1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -117,6 +117,14 @@ opt-level = 3 [profile.release] lto = "thin" +[profile.wasm-release] +inherits = "release" +opt-level = "s" +lto = true +codegen-units = 1 +strip = true +panic = "abort" + [patch.crates-io] parking_lot_core = { git = "https://github.com/youknowone/parking_lot", branch = "rustpython" } # REDOX START, Uncomment when you want to compile/check with redoxer diff --git a/crates/vm/src/datastack.rs b/crates/vm/src/datastack.rs index f2ffb74894a..7529263ac7e 100644 --- a/crates/vm/src/datastack.rs +++ b/crates/vm/src/datastack.rs @@ -9,7 +9,12 @@ use core::alloc::Layout; use core::ptr; /// Minimum chunk size in bytes (`_PY_DATA_STACK_CHUNK_SIZE`). -const MIN_CHUNK_SIZE: usize = 16 * 1024; +/// Smaller on WASM (4 KB) to reduce initial memory footprint; 16 KB otherwise. +const MIN_CHUNK_SIZE: usize = if cfg!(target_arch = "wasm32") { + 4 * 1024 +} else { + 16 * 1024 +}; /// Extra headroom (in bytes) to avoid allocating a new chunk for the next /// frame right after growing.