WebAssembly Troubles Part 3: The Stack Is Not the Stack
Accessing the stack is suppposed to be extremely fast, and in fact if you compile the Rust code directly to native it looks something like this :
Contrast this assembly output with the assembly generated by Firefox for the above Wasm (simplified):
Some of this is important for WebAssembly’s sandboxing, but I hope you can see that the global is being loaded and stored to memory every time. If you want the stack pointers to refer what I call the “physical stack” you’re out of luck (what I call the physical stack is the area of memory around , like we saw LLVM generate code to access when generating native code). Separated stack and heap pointers means that this is impossible – you either need the heap to be typed or you need to allow manipulating the pointer to the stack value, the former is extremely slow and the latter could cause the sandboxed code to overwrite memory that it shouldn’t have access to.
Source: troubles.md