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
This commit is contained in:
Alok Pandey authored and GitHub committed 2026-06-03 20:32:24 +09:00
1 parent fe6b9e8403
commit 5f789c6146
4 files changed
+31 -4

No files matched your search

+13
View File
@@ -6,3 +6,16 @@ rustflags = "-C link-args=-Wl,--stack,8000000"
[target.wasm32-unknown-unknown]
rustflags = ["--cfg=getrandom_backend=\"wasm_js\""]
# Enforce a 1 MB stack limit for WASI targets.
# (Runaway heap growth is fixed by the smaller DataStack chunk
# and the size‑optimized wasm‑release profile.)
[target.wasm32-wasip1]
rustflags = [
"-C", "link-arg=-zstack-size=1048576",
]
[target.wasm32-wasip2]
rustflags = [
"-C", "link-arg=-zstack-size=1048576",
]
+4 -3
View File
@@ -779,11 +779,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:
@@ -820,3 +820,4 @@ jobs:
- name: cargo doc
run: cargo doc --locked
+8
View File
@@ -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
+6 -1
View File
@@ -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.