mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-29 08:42:28 +00:00
Improve signals and memory protection
- Document sigaction() - Simplify New Technology fork() code - Testing and many bug fixes for mprotect() - Distribute Intel Xed ILD in the amalgamation - Turn Xed enums into defines to avoid DWARF bloat - Improve polyfilling of SA_SIGINFO on BSDs and fix bugs - setpgid(getpid(), getpid()) on Windows will ignore CTRL-C - Work around issues relating to NT mappings being executable - Permit automatic executable stack override via `ape_stack_pf`
This commit is contained in:
parent
c95c9d9508
commit
f684e348d4
76 changed files with 1844 additions and 1121 deletions
|
@ -1,66 +0,0 @@
|
|||
/*-*- 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/calls.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
#include "libc/sysv/consts/sa.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
jmp_buf jb;
|
||||
bool gotsegv;
|
||||
struct sigaction old[2];
|
||||
|
||||
void OnSigSegv(int sig) {
|
||||
gotsegv = true;
|
||||
longjmp(jb, 1);
|
||||
}
|
||||
|
||||
void SetUp(void) {
|
||||
sigaction(SIGBUS, NULL, &old[0]);
|
||||
sigaction(SIGSEGV, NULL, &old[1]);
|
||||
}
|
||||
|
||||
void TearDown(void) {
|
||||
sigaction(SIGBUS, &old[0], NULL);
|
||||
sigaction(SIGSEGV, &old[1], NULL);
|
||||
}
|
||||
|
||||
TEST(mprotect, test) {
|
||||
char *p = gc(memalign(PAGESIZE, PAGESIZE));
|
||||
p[0] = 0;
|
||||
ASSERT_NE(-1, mprotect(p, PAGESIZE, PROT_READ | PROT_WRITE));
|
||||
p[0] = 1;
|
||||
EXPECT_EQ(1, p[0]);
|
||||
}
|
||||
|
||||
TEST(mprotect, testSegfault) {
|
||||
char *p;
|
||||
struct sigaction ss = {.sa_handler = OnSigSegv, .sa_flags = SA_NODEFER};
|
||||
p = gc(memalign(PAGESIZE, PAGESIZE));
|
||||
EXPECT_NE(-1, sigaction(SIGBUS, &ss, NULL));
|
||||
EXPECT_NE(-1, sigaction(SIGSEGV, &ss, NULL));
|
||||
if (!setjmp(jb)) p[0] = 1;
|
||||
EXPECT_FALSE(gotsegv);
|
||||
EXPECT_NE(-1, mprotect(p, sizeof(p), PROT_READ));
|
||||
if (!setjmp(jb)) p[0] = 2;
|
||||
EXPECT_TRUE(gotsegv);
|
||||
EXPECT_EQ(1, p[0]);
|
||||
EXPECT_NE(-1, mprotect(p, sizeof(p), PROT_READ | PROT_WRITE));
|
||||
}
|
|
@ -124,11 +124,15 @@ TEST(sigaction, debugBreak_handlerCanReadCpuState) {
|
|||
// test fpu crash (unrecoverable)
|
||||
// test signal handler can modify cpu registers (now it's recoverable!)
|
||||
|
||||
void OnFpe(int sig, struct siginfo *si, struct ucontext *ctx) {
|
||||
void SkipOverFaultingInstruction(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;
|
||||
}
|
||||
|
||||
void OnFpe(int sig, struct siginfo *si, struct ucontext *ctx) {
|
||||
SkipOverFaultingInstruction(ctx);
|
||||
ctx->uc_mcontext.rax = 42;
|
||||
ctx->uc_mcontext.rdx = 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue