2020-06-15 14:18:57 +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│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
2021-01-28 03:34:02 +00:00
|
|
|
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2021-01-28 03:34:02 +00:00
|
|
|
#include "libc/calls/calls.h"
|
|
|
|
#include "libc/calls/sigbits.h"
|
|
|
|
#include "libc/calls/struct/sigaction.h"
|
2021-02-26 02:30:17 +00:00
|
|
|
#include "libc/calls/struct/siginfo.h"
|
|
|
|
#include "libc/calls/ucontext.h"
|
2021-01-28 03:34:02 +00:00
|
|
|
#include "libc/dce.h"
|
|
|
|
#include "libc/errno.h"
|
|
|
|
#include "libc/runtime/runtime.h"
|
|
|
|
#include "libc/sysv/consts/sa.h"
|
|
|
|
#include "libc/sysv/consts/sig.h"
|
|
|
|
#include "libc/testlib/testlib.h"
|
2021-02-26 02:30:17 +00:00
|
|
|
#include "third_party/xed/x86.h"
|
|
|
|
|
|
|
|
struct sigaction oldsa;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2021-02-08 17:19:00 +00:00
|
|
|
volatile bool gotsigint;
|
2021-01-28 03:34:02 +00:00
|
|
|
|
|
|
|
void OnSigInt(int sig) {
|
|
|
|
gotsigint = true;
|
|
|
|
}
|
|
|
|
|
2021-02-05 17:44:54 +00:00
|
|
|
void SetUp(void) {
|
|
|
|
gotsigint = false;
|
2021-02-26 02:30:17 +00:00
|
|
|
/* TODO(jart): Windows needs huge signal overhaul */
|
|
|
|
if (IsWindows()) exit(0);
|
2021-02-05 17:44:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 03:34:02 +00:00
|
|
|
TEST(sigaction, test) {
|
|
|
|
int pid, status;
|
|
|
|
sigset_t block, ignore, oldmask;
|
|
|
|
struct sigaction saint = {.sa_handler = OnSigInt};
|
|
|
|
sigemptyset(&block);
|
|
|
|
sigaddset(&block, SIGINT);
|
|
|
|
EXPECT_NE(-1, sigprocmask(SIG_BLOCK, &block, &oldmask));
|
|
|
|
sigfillset(&ignore);
|
|
|
|
sigdelset(&ignore, SIGINT);
|
2021-02-26 02:30:17 +00:00
|
|
|
EXPECT_NE(-1, sigaction(SIGINT, &saint, &oldsa));
|
2021-01-28 03:34:02 +00:00
|
|
|
ASSERT_NE(-1, (pid = fork()));
|
|
|
|
if (!pid) {
|
|
|
|
EXPECT_NE(-1, kill(getppid(), SIGINT));
|
|
|
|
EXPECT_EQ(-1, sigsuspend(&ignore));
|
|
|
|
EXPECT_EQ(EINTR, errno);
|
|
|
|
EXPECT_TRUE(gotsigint);
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
EXPECT_EQ(-1, sigsuspend(&ignore));
|
|
|
|
EXPECT_NE(-1, kill(pid, SIGINT));
|
|
|
|
EXPECT_NE(-1, waitpid(pid, &status, 0));
|
|
|
|
EXPECT_EQ(1, WIFEXITED(status));
|
|
|
|
EXPECT_EQ(0, WEXITSTATUS(status));
|
|
|
|
EXPECT_EQ(0, WTERMSIG(status));
|
2021-02-08 17:19:00 +00:00
|
|
|
EXPECT_NE(-1, sigprocmask(SIG_SETMASK, &oldmask, NULL));
|
2021-02-26 02:30:17 +00:00
|
|
|
EXPECT_NE(-1, sigaction(SIGINT, &oldsa, NULL));
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2021-02-05 17:44:54 +00:00
|
|
|
|
|
|
|
TEST(sigaction, raise) {
|
|
|
|
struct sigaction saint = {.sa_handler = OnSigInt};
|
2021-02-26 02:30:17 +00:00
|
|
|
EXPECT_NE(-1, sigaction(SIGINT, &saint, &oldsa));
|
2021-02-05 17:44:54 +00:00
|
|
|
ASSERT_FALSE(gotsigint);
|
|
|
|
EXPECT_NE(-1, raise(SIGINT));
|
|
|
|
ASSERT_TRUE(gotsigint);
|
2021-02-26 02:30:17 +00:00
|
|
|
EXPECT_NE(-1, sigaction(SIGINT, &oldsa, NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
volatile int trapeax;
|
|
|
|
|
|
|
|
void OnTrap(int sig, struct siginfo *si, struct ucontext *ctx) {
|
|
|
|
trapeax = ctx->uc_mcontext.rax;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sigaction, debugBreak_handlerCanReadCpuState) {
|
|
|
|
struct sigaction saint = {.sa_sigaction = OnTrap, .sa_flags = SA_SIGINFO};
|
|
|
|
EXPECT_NE(-1, sigaction(SIGTRAP, &saint, &oldsa));
|
|
|
|
asm("int3" : /* no outputs */ : "a"(0x31337));
|
|
|
|
EXPECT_EQ(0x31337, trapeax);
|
|
|
|
EXPECT_NE(-1, sigaction(SIGTRAP, &oldsa, NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnFpe(int sig, struct siginfo *si, struct ucontext *ctx) {
|
|
|
|
struct XedDecodedInst xedd;
|
|
|
|
xed_decoded_inst_zero_set_mode(&xedd, XED_MACHINE_MODE_LONG_64);
|
|
|
|
xed_instruction_length_decode(&xedd, (void *)ctx->uc_mcontext.rip, 15);
|
|
|
|
ctx->uc_mcontext.rip += xedd.length;
|
|
|
|
ctx->uc_mcontext.rax = 42;
|
|
|
|
ctx->uc_mcontext.rdx = 0;
|
|
|
|
}
|
|
|
|
|
2021-05-16 18:16:28 +00:00
|
|
|
noubsan void ubsanTrumpsSystemsEngineering(void) {
|
2021-02-26 02:30:17 +00:00
|
|
|
struct sigaction saint = {.sa_sigaction = OnFpe, .sa_flags = SA_SIGINFO};
|
|
|
|
EXPECT_NE(-1, sigaction(SIGFPE, &saint, &oldsa));
|
|
|
|
volatile long x = 0;
|
|
|
|
EXPECT_EQ(42, 666 / x); /* systems engineering trumps math */
|
|
|
|
EXPECT_NE(-1, sigaction(SIGFPE, &oldsa, NULL));
|
2021-02-05 17:44:54 +00:00
|
|
|
}
|
2021-05-16 18:16:28 +00:00
|
|
|
|
|
|
|
TEST(sigaction, sigFpe_handlerCanEditProcessStateAndRecoverExecution) {
|
|
|
|
ubsanTrumpsSystemsEngineering();
|
|
|
|
}
|