Skip to content

Commit c5f555e

Browse files
authored
Fix WASM memory allocation failure (#4989) (#7887)
* Fix WASM memory allocation failure (#4989) This commit addresses the unbounded memory allocation issue on WASM targets that caused minimal programs (like � = 1) to fail with ~79MB allocation errors in constrained runtimes like wasmi. The fixes include: 1. Capping the max memory limit at 64MB and restricting stack size to 1MB via linker flags in .cargo/config.toml. 2. Reducing the minimum chunk size of the DataStack from 16KB to 4KB on WASM targets to shrink the initial memory footprint. 3. Adding a wasm-release profile in the root Cargo.toml optimized for size with opt-level = s, LTO enabled, and symbols stripped. * Trigger CI rerun * Address review feedback: add comments, use cfg! for MIN_CHUNK_SIZE * Remove duplicate MIN_CHUNK_SIZE definition * Fix duplicate MIN_CHUNK_SIZE and format for prek lint * Update CI to use wasm-release profile for WASM tests * Remove leftover conflict markers * Clean remaining conflict markers in CI workflow * Clean remaining conflict markers in CI workflow * Trigger CI to pick up new required check * Remove --max-memory flag, keep stack limit only
1 parent 376c4f1 commit c5f555e

4 files changed

Lines changed: 31 additions & 4 deletions

File tree

‎.cargo/config.toml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,16 @@ rustflags = "-C link-args=-Wl,--stack,8000000"
66

77
[target.wasm32-unknown-unknown]
88
rustflags = ["--cfg=getrandom_backend=\"wasm_js\""]
9+
10+
# Enforce a 1 MB stack limit for WASI targets.
11+
# (Runaway heap growth is fixed by the smaller DataStack chunk
12+
# and the size‑optimized wasm‑release profile.)
13+
[target.wasm32-wasip1]
14+
rustflags = [
15+
"-C", "link-arg=-zstack-size=1048576",
16+
]
17+
18+
[target.wasm32-wasip2]
19+
rustflags = [
20+
"-C", "link-arg=-zstack-size=1048576",
21+
]

‎.github/workflows/ci.yaml‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,11 +779,11 @@ jobs:
779779
clang: true
780780

781781
- name: build rustpython
782-
run: cargo build --release --target wasm32-wasip1 --no-default-features --features freeze-stdlib,stdlib,stdio,importlib,host_env --verbose
782+
run: cargo build --profile wasm-release --target wasm32-wasip1 --no-default-features --features freeze-stdlib,stdlib,stdio,importlib,host_env --verbose
783783
- name: run snippets
784-
run: wasmer run --dir "$(pwd)" target/wasm32-wasip1/release/rustpython.wasm -- "$(pwd)/extra_tests/snippets/stdlib_random.py"
784+
run: wasmer run --dir "$(pwd)" target/wasm32-wasip1/wasm-release/rustpython.wasm -- "$(pwd)/extra_tests/snippets/stdlib_random.py"
785785
- name: run cpython unittest
786-
run: wasmer run --dir "$(pwd)" target/wasm32-wasip1/release/rustpython.wasm -- "$(pwd)/Lib/test/test_int.py"
786+
run: wasmer run --dir "$(pwd)" target/wasm32-wasip1/wasm-release/rustpython.wasm -- "$(pwd)/Lib/test/test_int.py"
787787

788788
cargo_doc:
789789
needs:
@@ -820,3 +820,4 @@ jobs:
820820
821821
- name: cargo doc
822822
run: cargo doc --locked
823+

‎Cargo.toml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ opt-level = 3
117117
[profile.release]
118118
lto = "thin"
119119

120+
[profile.wasm-release]
121+
inherits = "release"
122+
opt-level = "s"
123+
lto = true
124+
codegen-units = 1
125+
strip = true
126+
panic = "abort"
127+
120128
[patch.crates-io]
121129
parking_lot_core = { git = "https://github.com/youknowone/parking_lot", branch = "rustpython" }
122130
# REDOX START, Uncomment when you want to compile/check with redoxer

‎crates/vm/src/datastack.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ use core::alloc::Layout;
99
use core::ptr;
1010

1111
/// Minimum chunk size in bytes (`_PY_DATA_STACK_CHUNK_SIZE`).
12-
const MIN_CHUNK_SIZE: usize = 16 * 1024;
12+
/// Smaller on WASM (4 KB) to reduce initial memory footprint; 16 KB otherwise.
13+
const MIN_CHUNK_SIZE: usize = if cfg!(target_arch = "wasm32") {
14+
4 * 1024
15+
} else {
16+
16 * 1024
17+
};
1318

1419
/// Extra headroom (in bytes) to avoid allocating a new chunk for the next
1520
/// frame right after growing.

0 commit comments

Comments
 (0)