-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add sigaltstack syscall for Linux #2546
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these can go in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are, but they're not supported by every platform and that's why they've been duplicated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose that's better than a link error if accidentally used. Makes sense to me. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2433,6 +2433,29 @@ pub fn unexpectedErrno(err: usize) UnexpectedError { | |
return error.Unexpected; | ||
} | ||
|
||
pub const SigaltstackError = error{ | ||
/// The supplied stack size was less than MINSIGSTKSZ. | ||
SizeTooSmall, | ||
|
||
/// Attempted to change the signal stack while it was active. | ||
PermissionDenied, | ||
Unexpected, | ||
}; | ||
|
||
pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void { | ||
if (windows.is_the_target or uefi.is_the_target or wasi.is_the_target) | ||
@compileError("std.os.sigaltstack not available for this target"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this if and explicit compile error is just fine, but not required. everywhere else just lets the natural compile error happen if you call the function on an unsupported target. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explicit is better than implicit, I believe that goes for error messages too. |
||
|
||
switch (errno(system.sigaltstack(ss, old_ss))) { | ||
0 => return, | ||
EFAULT => unreachable, | ||
EINVAL => unreachable, | ||
ENOMEM => return error.SizeTooSmall, | ||
EPERM => return error.PermissionDenied, | ||
else => |err| return unexpectedErrno(err), | ||
} | ||
} | ||
|
||
test "" { | ||
_ = @import("os/darwin.zig"); | ||
_ = @import("os/freebsd.zig"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,3 +149,14 @@ test "realpath" { | |
var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; | ||
testing.expectError(error.FileNotFound, fs.realpath("definitely_bogus_does_not_exist1234", &buf)); | ||
} | ||
|
||
test "sigaltstack" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (builtin.os == .windows or builtin.os == .wasi) return error.SkipZigTest; | ||
|
||
var st: os.stack_t = undefined; | ||
try os.sigaltstack(null, &st); | ||
// Setting a stack size less than MINSIGSTKSZ returns ENOMEM | ||
st.ss_flags = 0; | ||
st.ss_size = 1; | ||
testing.expectError(error.SizeTooSmall, os.sigaltstack(&st, null)); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
// TODO this is where the extern declarations go. For example, see | ||
// inc/efilib.h in gnu-efi-code | ||
|
||
const builtin = @import("builtin"); | ||
|
||
pub const is_the_target = builtin.os == .uefi; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow I didn't catch this the first time.
(the other ones too)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait what? It's signal alternate stack also in FreeBSD OpenBSD and Linux
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, my mistake. I saw
/home/vsts/work/1/s/std/os.zig:2458:25: error: container 'c' has no member called 'sigaltstack'
and made an incorrect assumption.So this looks like a bug with
use
then? damnThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah right of course. it was just the same darwin bug on all 3 of the CI because they each test cross compilation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, I just forgot to add the declaration in
darwin.zig
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well I feel silly. sorry for all the noise