Back then at least, stack space could not be altered, which made sure I had to rework an application I was working on to prevent stack overflows.
As for limiting heap space, other than with VMs or containers (both of which are Rust-agnostic) I'm not aware of any way to limit that on any platform.
i found the wasm file build by rust contains the memory segment, which is (memory (;0;) 19),
In my opinion, maybe we just need add a build flag, eg. --max-page=20, then, when we build rust file to wasm file, the compiler just modify (memory (;0;) 19) to (memory (;0;) 19 20), and this will crashed when the memory reached 20 pages.
What confuses me is why the rust compiler doesn't set max page size in the memory segment and doesn't provide other method to update this?
It's likely because without the explicit cooperation of the various WASM VMs, you can put whatever you want in the wasm file, and it will simply be ignored. The easiest way to ensure such cooperation is by getting it into the WASM spec, but even that doesn't seem to be exactly easy.
--initial-memory and --max-memory are arguments for the linker. Try `cargo rustc --target wasm32-wasi -- -Clink-arg=--initial-memory=65536 -Clink-arg=--max-memory=65536
I don't know of a way to tell the Rust compiler to generate a .wasm file with the memory limit set.
However, I would suggest you to enforce this on the runtime level. If you are using wasmtime, you can embed their runtime into a wrapper app and control memory & CPU usage precisely. This way you don't even need to trust the value in the .wasm file you are running.
If you don't feel like providing your runtime, lunatic even allows you to write rust code that compiles to wasm and then loads other .wasm files dynamically with specific memory/cpu/syscalls constraints. You can even run just a closure from the currently executing .wasm file in a separate "process" (wasm instance), with specific memory and CPU constraints.
Btw. When compiling to wasm, Rust uses a shadow-stack on the wasm heap that will eat part of your heap memory. You can configure how big this stack is with a flag (I think it's actually a LLVM flag and I can't remember it now). You need to account for this too if you are setting a small wasm page size (like ~20).