2021-10-14 00:27:13 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
|
|
│ │
|
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/calls/struct/sigaltstack.h"
|
2023-04-27 03:45:01 +00:00
|
|
|
#include "libc/calls/struct/metasigaltstack.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/calls/struct/sigaltstack.internal.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/dce.h"
|
|
|
|
#include "libc/intrin/asan.internal.h"
|
2022-05-18 23:41:29 +00:00
|
|
|
#include "libc/intrin/describeflags.internal.h"
|
2022-10-11 00:52:41 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
|
|
|
#include "libc/sysv/consts/ss.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
2022-05-18 23:41:29 +00:00
|
|
|
static void sigaltstack2bsd(struct sigaltstack_bsd *bsd,
|
|
|
|
const struct sigaltstack *linux) {
|
2021-10-14 00:27:13 +00:00
|
|
|
void *sp;
|
|
|
|
int flags;
|
|
|
|
size_t size;
|
|
|
|
sp = linux->ss_sp;
|
|
|
|
flags = linux->ss_flags;
|
|
|
|
size = linux->ss_size;
|
|
|
|
bsd->ss_sp = sp;
|
|
|
|
bsd->ss_flags = flags;
|
|
|
|
bsd->ss_size = size;
|
|
|
|
}
|
|
|
|
|
2022-05-18 23:41:29 +00:00
|
|
|
static void sigaltstack2linux(struct sigaltstack *linux,
|
|
|
|
const struct sigaltstack_bsd *bsd) {
|
2021-10-14 00:27:13 +00:00
|
|
|
void *sp;
|
|
|
|
int flags;
|
|
|
|
size_t size;
|
|
|
|
sp = bsd->ss_sp;
|
|
|
|
flags = bsd->ss_flags;
|
|
|
|
size = bsd->ss_size;
|
|
|
|
linux->ss_sp = sp;
|
|
|
|
linux->ss_flags = flags;
|
|
|
|
linux->ss_size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets and/or gets alternate signal stack, e.g.
|
|
|
|
*
|
|
|
|
* struct sigaction sa;
|
|
|
|
* struct sigaltstack ss;
|
|
|
|
* ss.ss_flags = 0;
|
2022-05-19 23:57:49 +00:00
|
|
|
* ss.ss_size = GetStackSize();
|
|
|
|
* ss.ss_sp = mmap(0, GetStackSize(), PROT_READ | PROT_WRITE,
|
|
|
|
* MAP_STACK | MAP_ANONYMOUS, -1, 0);
|
2021-10-14 00:27:13 +00:00
|
|
|
* sa.sa_flags = SA_ONSTACK;
|
|
|
|
* sa.sa_handler = OnStackOverflow;
|
|
|
|
* __cxa_atexit(free, ss[0].ss_sp, 0);
|
|
|
|
* sigemptyset(&sa.ss_mask);
|
|
|
|
* sigaltstack(&ss, 0);
|
|
|
|
* sigaction(SIGSEGV, &sa, 0);
|
|
|
|
*
|
2022-05-19 23:57:49 +00:00
|
|
|
* It's strongly recommended that you allocate a stack with the same
|
|
|
|
* size as GetStackSize() and that it have GetStackSize() alignment.
|
|
|
|
* Otherwise some of your runtime support code (e.g. ftrace stack use
|
|
|
|
* logging, kprintf() memory safety) won't be able to work as well.
|
|
|
|
*
|
2021-10-14 00:27:13 +00:00
|
|
|
* @param neu if non-null will install new signal alt stack
|
|
|
|
* @param old if non-null will receive current signal alt stack
|
|
|
|
* @return 0 on success, or -1 w/ errno
|
2022-10-11 00:52:41 +00:00
|
|
|
* @raise EFAULT if bad memory was supplied
|
|
|
|
* @raise ENOMEM if `neu->ss_size` is less than `MINSIGSTKSZ`
|
2021-10-14 00:27:13 +00:00
|
|
|
*/
|
2022-05-18 23:41:29 +00:00
|
|
|
int sigaltstack(const struct sigaltstack *neu, struct sigaltstack *old) {
|
2021-10-14 00:27:13 +00:00
|
|
|
int rc;
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
void *b;
|
|
|
|
const void *a;
|
2021-10-14 00:27:13 +00:00
|
|
|
struct sigaltstack_bsd bsd;
|
|
|
|
if (IsAsan() && ((old && __asan_check(old, sizeof(*old)).kind) ||
|
|
|
|
(neu && (__asan_check(neu, sizeof(*neu)).kind ||
|
|
|
|
__asan_check(neu->ss_sp, neu->ss_size).kind)))) {
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
rc = efault();
|
2022-10-11 00:52:41 +00:00
|
|
|
} else if (neu && neu->ss_size < MINSIGSTKSZ) {
|
|
|
|
rc = enomem();
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
} else if (IsLinux() || IsBsd()) {
|
|
|
|
if (IsLinux()) {
|
|
|
|
a = neu;
|
|
|
|
b = old;
|
2021-10-14 00:27:13 +00:00
|
|
|
} else {
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
if (neu) {
|
|
|
|
sigaltstack2bsd(&bsd, neu);
|
|
|
|
a = &bsd;
|
|
|
|
} else {
|
|
|
|
a = 0;
|
|
|
|
}
|
|
|
|
if (old) {
|
|
|
|
b = &bsd;
|
|
|
|
} else {
|
|
|
|
b = 0;
|
|
|
|
}
|
2021-10-14 00:27:13 +00:00
|
|
|
}
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
if ((rc = sys_sigaltstack(a, b)) != -1) {
|
|
|
|
if (IsBsd() && old) {
|
|
|
|
sigaltstack2linux(old, &bsd);
|
|
|
|
}
|
|
|
|
rc = 0;
|
2021-10-14 00:27:13 +00:00
|
|
|
} else {
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
rc = -1;
|
2021-10-14 00:27:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
rc = enosys();
|
2021-10-14 00:27:13 +00:00
|
|
|
}
|
2022-06-26 01:17:31 +00:00
|
|
|
STRACE("sigaltstack(%s, [%s]) → %d% m", DescribeSigaltstk(0, neu),
|
|
|
|
DescribeSigaltstk(0, old), rc);
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
return rc;
|
2021-10-14 00:27:13 +00:00
|
|
|
}
|