Skip to content

Commit 6f49233

Browse files
committed
Merge pull request #10572 from Luukdegram/wasm-linker-stack
Stage2: wasm-linker - Place stack at the beginning of the linear memory
1 parent bb8eef8 commit 6f49233

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

src/link/Wasm.zig

+26-12
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,21 @@ fn setupMemory(self: *Wasm) !void {
495495
log.debug("Setting up memory layout", .{});
496496
const page_size = 64 * 1024;
497497
const stack_size = self.base.options.stack_size_override orelse page_size * 1;
498-
const stack_alignment = 16;
499-
var memory_ptr: u64 = self.base.options.global_base orelse 1024;
500-
memory_ptr = std.mem.alignForwardGeneric(u64, memory_ptr, stack_alignment);
498+
const stack_alignment = 16; // wasm's stack alignment as specified by tool-convention
499+
// Always place the stack at the start by default
500+
// unless the user specified the global-base flag
501+
var place_stack_first = true;
502+
var memory_ptr: u64 = if (self.base.options.global_base) |base| blk: {
503+
place_stack_first = false;
504+
break :blk base;
505+
} else 0;
506+
507+
if (place_stack_first) {
508+
memory_ptr = std.mem.alignForwardGeneric(u64, memory_ptr, stack_alignment);
509+
memory_ptr += stack_size;
510+
// We always put the stack pointer global at index 0
511+
self.globals.items[0].init.i32_const = @bitCast(i32, @intCast(u32, memory_ptr));
512+
}
501513

502514
var offset: u32 = @intCast(u32, memory_ptr);
503515
for (self.segments.items) |*segment, i| {
@@ -511,8 +523,11 @@ fn setupMemory(self: *Wasm) !void {
511523
offset += segment.size;
512524
}
513525

514-
memory_ptr = std.mem.alignForwardGeneric(u64, memory_ptr, stack_alignment);
515-
memory_ptr += stack_size;
526+
if (!place_stack_first) {
527+
memory_ptr = std.mem.alignForwardGeneric(u64, memory_ptr, stack_alignment);
528+
memory_ptr += stack_size;
529+
self.globals.items[0].init.i32_const = @bitCast(i32, @intCast(u32, memory_ptr));
530+
}
516531

517532
// Setup the max amount of pages
518533
// For now we only support wasm32 by setting the maximum allowed memory size 2^32-1
@@ -555,9 +570,6 @@ fn setupMemory(self: *Wasm) !void {
555570
self.memories.limits.max = @intCast(u32, max_memory / page_size);
556571
log.debug("Maximum memory pages: {d}", .{self.memories.limits.max});
557572
}
558-
559-
// We always put the stack pointer global at index 0
560-
self.globals.items[0].init.i32_const = @bitCast(i32, @intCast(u32, memory_ptr));
561573
}
562574

563575
fn resetState(self: *Wasm) void {
@@ -1105,6 +1117,12 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
11051117
if (self.base.options.global_base) |global_base| {
11061118
const arg = try std.fmt.allocPrint(arena, "--global-base={d}", .{global_base});
11071119
try argv.append(arg);
1120+
} else {
1121+
// We prepend it by default, so when a stack overflow happens the runtime will trap correctly,
1122+
// rather than silently overwrite all global declarations. See https://github.com/ziglang/zig/issues/4496
1123+
//
1124+
// The user can overwrite this behavior by setting the global-base
1125+
try argv.append("--stack-first");
11081126
}
11091127

11101128
// Users are allowed to specify which symbols they want to export to the wasm host.
@@ -1125,10 +1143,6 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
11251143
const arg = try std.fmt.allocPrint(arena, "stack-size={d}", .{stack_size});
11261144
try argv.append(arg);
11271145

1128-
// Put stack before globals so that stack overflow results in segfault immediately
1129-
// before corrupting globals. See https://github.com/ziglang/zig/issues/4496
1130-
try argv.append("--stack-first");
1131-
11321146
if (self.base.options.wasi_exec_model == .reactor) {
11331147
// Reactor execution model does not have _start so lld doesn't look for it.
11341148
try argv.append("--no-entry");

0 commit comments

Comments
 (0)