Fix WASM memory allocation failure (#4989) (#7887) · RustPython/RustPython@c5f555e · GitHub
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

.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

0 commit comments

Comments
 (0)