authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2023-07-25 21:44:11-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-27 10:17:46-07:00
log2dd7c6b268a838d4a130ac2eb88f4267598bb42e
tree420af4c56dc1db2c6816ece9ba60756981b61b21
parente66190025ffab39527da601980b7e3211069b6f5

linux: do not set stack size hard limit

At main startup, if the ELF auxiliary vector contains a stacksize value, use it as a hint for the minimum stacksize required by the executable. 1. Never lower the hard-limit. Once a hard-limit is lowered, then it can never be increased (including child processes). 2. If hint exceeds hard-limit then clamp hint to hard-limit. 3. If soft-limit exceeds hint then do nothing.

1 files changed, 23 insertions(+), 16 deletions(-)

lib/std/start.zig+23-16
...@@ -459,22 +459,29 @@ fn expandStackSize(phdrs: []elf.Phdr) void {...@@ -459,22 +459,29 @@ fn expandStackSize(phdrs: []elf.Phdr) void {
459 for (phdrs) |*phdr| {459 for (phdrs) |*phdr| {
460 switch (phdr.p_type) {460 switch (phdr.p_type) {
461 elf.PT_GNU_STACK => {461 elf.PT_GNU_STACK => {
462 const wanted_stack_size = phdr.p_memsz;462 assert(phdr.p_memsz % std.mem.page_size == 0);
463 assert(wanted_stack_size % std.mem.page_size == 0);463
464464 // Silently fail if we are unable to get limits.
465 std.os.setrlimit(.STACK, .{465 const limits = std.os.getrlimit(.STACK) catch break;
466 .cur = wanted_stack_size,466
467 .max = wanted_stack_size,467 // Clamp to limits.max .
468 }) catch {468 const wanted_stack_size = @min(phdr.p_memsz, limits.max);
469 // Because we could not increase the stack size to the upper bound,469
470 // depending on what happens at runtime, a stack overflow may occur.470 if (wanted_stack_size > limits.cur) {
471 // However it would cause a segmentation fault, thanks to stack probing,471 std.os.setrlimit(.STACK, .{
472 // so we do not have a memory safety issue here.472 .cur = wanted_stack_size,
473 // This is intentional silent failure.473 .max = limits.max,
474 // This logic should be revisited when the following issues are addressed:474 }) catch {
475 // https://github.com/ziglang/zig/issues/157475 // Because we could not increase the stack size to the upper bound,
476 // https://github.com/ziglang/zig/issues/1006476 // depending on what happens at runtime, a stack overflow may occur.
477 };477 // However it would cause a segmentation fault, thanks to stack probing,
478 // so we do not have a memory safety issue here.
479 // This is intentional silent failure.
480 // This logic should be revisited when the following issues are addressed:
481 // https://github.com/ziglang/zig/issues/157
482 // https://github.com/ziglang/zig/issues/1006
483 };
484 }
478 break;485 break;
479 },486 },
480 else => {},487 else => {},