-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wasm has a max stack size of ~66K #3735
Comments
I found this in the outputted wasm: (memory (;0;) 2)
(global (;0;) (mut i32) (i32.const 68752)) Manually dialing up memory=3 and global=69000 fixes this. Edit: |
Possible solutions:
|
The LLVM stack is actually not the same as the wasm stack. It's only being for local variables that do not have native representation in the wasm machine (i32, i64, f32, f64) so only arrays and structs. This explains why certain types of tests (ones with large stack buffers) blow out the stack but stdlib tests mostly run fine. |
I was going to file a new issue, but this one seems on point, or at least related to stack problems with Currently, in
Increasing the stack size by passing an additional extension option to
@fengb if you have any ideas about this, feel free to shout out! The issue aside, I think it'd still be a good idea to increase the default stack size to something more reasonable than just 1 Wasm page of memory as @fengb suggested. If we take cue from Rust, Alex increased the default to approx 1MB (relevant PR in rustlang). This won't fix the problem I mentioned, but will hopefully prevent some silly overflows in the future. @andrewrk @fengb lemme know what you guys think, and if you're happy with this, I'll supply a patch. Taking this a bit further, I think that acting on option 1. is also worth doing. Perhaps we could even give the Zig's programmer "raw" access to the linker, and pass whatever flags they want, much like it's done in Rust with Finally, option 3. seems really interesting, but also definitely non-trivial, so in the meantime, I'd suggest we go with the above, and when 3. lands, we can rethink our strategy. Thoughts? |
Option 3 is the longterm goal to eliminate stack overflow, but I don't think we're there yet. I think we can expose a flag to set the stack value. There's a |
My two cents: There is no reason for the default stack size to be so low, regardless of these issues. I'll open a separate proposal if you want, but I honestly think we should, at minimum, double it. Different platforms should have different defaults, based on reasonable memory expectations. on x64 for instance, we can assume a bare minimum of 2GiB of RAM, and so using even 1MiB for the stack is just not a big deal. |
I've had a poke around
However, because it is a drop-in replacement for |
Sorry, I meant that there exists plumbing for the elf linker to overwrite the stack size. This is only exposed via |
Makes sense! Thanks for clarifying! |
I think this can be closed. 1 + 2 have been implemented and 3 is a longstanding issue that'll be tackled by the umbrella issue. Thanks! |
The stack size is 1MB when using ~/test/stack$ zig version
0.10.0-dev.3629+2ccaa5414
~/test/stack$ uname -a
Darwin max.local 21.6.0 Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:23 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T6000 arm64
~/test/stack$ cat b.zig
const std = @import("std");
pub fn main() void {
std.debug.print("hello world\n", .{});
}
~/test/stack$ zig build-exe -target wasm32-wasi b.zig
~/test/stack$ wasm2wat b.wasm |grep const |grep stack_pointer
(global $__stack_pointer (mut i32) (i32.const 1048576))
~/test/stack$ zig build-lib -dynamic -rdynamic -target wasm32-wasi b.zig
~/test/stack$ wasm2wat b.wasm |grep const |grep stack_pointer
(global $__stack_pointer (mut i32) (i32.const 65536))
~/test/stack$ zig build-lib -dynamic -rdynamic -target wasm32-wasi --stack 1048576 b.zig
~/test/stack$ wasm2wat b.wasm |grep const |grep stack_pointer
(global $__stack_pointer (mut i32) (i32.const 1048576)) I also tested with build from zig-bootstrap, so self-hosted (?) and the output is identical. ~/test/stack$ zig version
0.10.0-dev.3653+7152a58c1 |
Could you take a look @Luukdegram? |
It seems this was deliberately only implemented for |
Sounds good! Could you look into adding this missing bit then please? In the meantime, I will reopen this issue. |
Will do! |
Easiest to reproduce with the WASI test patches:
$ zig test wasi.zig -target wasm32-wasi --test-cmd wasmtime --test-cmd-bin
The text was updated successfully, but these errors were encountered: