aboutsummaryrefslogtreecommitdiff
path: root/lib/std/start.zig
diff options
context:
space:
mode:
authorMichael Dusan <michael.dusan@gmail.com>2023-07-25 21:44:11 -0400
committerAndrew Kelley <andrew@ziglang.org>2023-07-27 10:17:46 -0700
commit2dd7c6b268a838d4a130ac2eb88f4267598bb42e (patch)
tree420af4c56dc1db2c6816ece9ba60756981b61b21 /lib/std/start.zig
parente66190025ffab39527da601980b7e3211069b6f5 (diff)
downloadzig-2dd7c6b268a838d4a130ac2eb88f4267598bb42e.tar.gz
zig-2dd7c6b268a838d4a130ac2eb88f4267598bb42e.zip
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.
Diffstat (limited to 'lib/std/start.zig')
-rw-r--r--lib/std/start.zig39
1 files changed, 23 insertions, 16 deletions
diff --git a/lib/std/start.zig b/lib/std/start.zig
index d81eb4f9e9..d580a3ec2b 100644
--- a/lib/std/start.zig
+++ b/lib/std/start.zig
@@ -459,22 +459,29 @@ fn expandStackSize(phdrs: []elf.Phdr) void {
for (phdrs) |*phdr| {
switch (phdr.p_type) {
elf.PT_GNU_STACK => {
- const wanted_stack_size = phdr.p_memsz;
- assert(wanted_stack_size % std.mem.page_size == 0);
-
- std.os.setrlimit(.STACK, .{
- .cur = wanted_stack_size,
- .max = wanted_stack_size,
- }) catch {
- // Because we could not increase the stack size to the upper bound,
- // depending on what happens at runtime, a stack overflow may occur.
- // However it would cause a segmentation fault, thanks to stack probing,
- // so we do not have a memory safety issue here.
- // This is intentional silent failure.
- // This logic should be revisited when the following issues are addressed:
- // https://github.com/ziglang/zig/issues/157
- // https://github.com/ziglang/zig/issues/1006
- };
+ assert(phdr.p_memsz % std.mem.page_size == 0);
+
+ // Silently fail if we are unable to get limits.
+ const limits = std.os.getrlimit(.STACK) catch break;
+
+ // Clamp to limits.max .
+ const wanted_stack_size = @min(phdr.p_memsz, limits.max);
+
+ if (wanted_stack_size > limits.cur) {
+ std.os.setrlimit(.STACK, .{
+ .cur = wanted_stack_size,
+ .max = limits.max,
+ }) catch {
+ // Because we could not increase the stack size to the upper bound,
+ // depending on what happens at runtime, a stack overflow may occur.
+ // However it would cause a segmentation fault, thanks to stack probing,
+ // so we do not have a memory safety issue here.
+ // This is intentional silent failure.
+ // This logic should be revisited when the following issues are addressed:
+ // https://github.com/ziglang/zig/issues/157
+ // https://github.com/ziglang/zig/issues/1006
+ };
+ }
break;
},
else => {},