Commit bcdbd8d169

LemonBoy <thatlemon@gmail.com>
2019-05-28 15:22:19
Add sigaltstack syscall
1 parent d1b6f29
std/c/freebsd.zig
@@ -1,4 +1,8 @@
+const std = @import("../std.zig");
+use std.c;
+
 extern "c" fn __error() *c_int;
 pub const _errno = __error;
 
 pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize;
+pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
std/c/linux.zig
@@ -25,3 +25,5 @@ pub extern "c" fn getauxval(__type: c_ulong) c_ulong;
 
 pub const dl_iterate_phdr_callback = extern fn (info: *dl_phdr_info, size: usize, data: ?*c_void) c_int;
 pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int;
+
+pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
std/c/netbsd.zig
@@ -1,4 +1,8 @@
+const std = @import("../std.zig");
+use std.c;
+
 extern "c" fn __errno() *c_int;
 pub const _errno = __errno;
 
 pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize;
+pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
std/os/bits/darwin.zig
@@ -1078,3 +1078,15 @@ pub const EQFULL = 106;
 
 /// Must be equal largest errno
 pub const ELAST = 106;
+
+pub const SIGSTKSZ = 131072;
+pub const MINSIGSTKSZ = 32768;
+
+pub const SS_ONSTACK = 1;
+pub const SS_DISABLE = 4;
+
+pub const stack_t = extern struct {
+    ss_sp: [*]u8,
+    ss_size: isize,
+    ss_flags: i32,
+};
std/os/bits/freebsd.zig
@@ -835,3 +835,19 @@ pub const ENOTRECOVERABLE = 95; // State not recoverable
 pub const EOWNERDEAD = 96; // Previous owner died
 
 pub const ELAST = 96; // Must be equal largest errno
+
+pub const MINSIGSTKSZ = switch (builtin.arch) {
+    .i386, .x86_64 => 2048,
+    .arm, .aarch64 => 4096,
+    else => @compileError("MINSIGSTKSZ not defined for this architecture"),
+};
+pub const SIGSTKSZ = MINSIGSTKSZ + 32768;
+
+pub const SS_ONSTACK = 1;
+pub const SS_DISABLE = 4;
+
+pub const stack_t = extern struct {
+    ss_sp: [*]u8,
+    ss_size: isize,
+    ss_flags: i32,
+};
std/os/bits/linux.zig
@@ -929,3 +929,24 @@ pub fn CPU_COUNT(set: cpu_set_t) cpu_count_t {
 //#define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t),set)
 //#define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t),set)
 //#define CPU_EQUAL(s1,s2) CPU_EQUAL_S(sizeof(cpu_set_t),s1,s2)
+
+pub const MINSIGSTKSZ = switch (builtin.arch) {
+    .i386, .x86_64, .arm => 2048,
+    .aarch64 => 5120,
+    else => @compileError("MINSIGSTKSZ not defined for this architecture"),
+};
+pub const SIGSTKSZ = switch (builtin.arch) {
+    .i386, .x86_64, .arm => 8192,
+    .aarch64 => 16384,
+    else => @compileError("SIGSTKSZ not defined for this architecture"),
+};
+
+pub const SS_ONSTACK = 1;
+pub const SS_DISABLE = 2;
+pub const SS_AUTODISARM = 1 << 31;
+
+pub const stack_t = extern struct {
+    ss_sp: [*]u8,
+    ss_flags: i32,
+    ss_size: isize,
+};
std/os/bits/netbsd.zig
@@ -723,3 +723,15 @@ pub const ENOLINK = 95; // Link has been severed
 pub const EPROTO = 96; // Protocol error
 
 pub const ELAST = 96; // Must equal largest errno
+
+pub const MINSIGSTKSZ = 8192;
+pub const SIGSTKSZ = MINSIGSTKSZ + 32768;
+
+pub const SS_ONSTACK = 1;
+pub const SS_DISABLE = 4;
+
+pub const stack_t = extern struct {
+    ss_sp: [*]u8,
+    ss_size: isize,
+    ss_flags: i32,
+};
std/os/linux/test.zig
@@ -83,3 +83,12 @@ test "dl_iterate_phdr" {
     expect(linux.dl_iterate_phdr(usize, iter_fn, &counter) != 0);
     expect(counter != 0);
 }
+
+test "sigaltstack" {
+    var st: linux.stack_t = undefined;
+    expect(linux.sigaltstack(null, &st) == 0);
+    // Setting a stack size less than MINSIGSTKSZ returns ENOMEM
+    st.ss_flags = 0;
+    st.ss_size = 1;
+    expect(linux.getErrno(linux.sigaltstack(&st, null)) == linux.ENOMEM);
+}
std/os/linux.zig
@@ -818,6 +818,10 @@ pub fn capset(hdrp: *cap_user_header_t, datap: *const cap_user_data_t) usize {
     return syscall2(SYS_capset, @ptrToInt(hdrp), @ptrToInt(datap));
 }
 
+pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) usize {
+    return syscall2(SYS_sigaltstack, @ptrToInt(ss), @ptrToInt(old_ss));
+}
+
 // XXX: This should be weak
 extern const __ehdr_start: elf.Ehdr = undefined;