mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 03:27:39 +00:00
Make some quick fixes
This commit is contained in:
parent
94dc7a684e
commit
820c3599ed
10 changed files with 50 additions and 358 deletions
|
@ -143,33 +143,27 @@ static void GetRandomArnd(char *p, size_t n) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t GetRandomBsd(char *p, size_t n, void impl(char *, size_t)) {
|
static ssize_t GetRandomBsd(char *p, size_t n, void impl(char *, size_t)) {
|
||||||
errno_t e;
|
|
||||||
size_t m, i;
|
size_t m, i;
|
||||||
if (_weaken(pthread_testcancel_np) &&
|
|
||||||
(e = _weaken(pthread_testcancel_np)())) {
|
|
||||||
errno = e;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
for (i = 0;;) {
|
for (i = 0;;) {
|
||||||
m = MIN(n - i, 256);
|
m = MIN(n - i, 256);
|
||||||
impl(p + i, m);
|
impl(p + i, m);
|
||||||
if ((i += m) == n) {
|
if ((i += m) == n) {
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
if (_weaken(pthread_testcancel)) {
|
|
||||||
_weaken(pthread_testcancel)();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t GetDevUrandom(char *p, size_t n) {
|
static ssize_t GetDevUrandom(char *p, size_t n) {
|
||||||
int fd;
|
int fd;
|
||||||
ssize_t rc;
|
ssize_t rc;
|
||||||
|
BLOCK_SIGNALS;
|
||||||
fd = sys_openat(AT_FDCWD, "/dev/urandom", O_RDONLY | O_CLOEXEC, 0);
|
fd = sys_openat(AT_FDCWD, "/dev/urandom", O_RDONLY | O_CLOEXEC, 0);
|
||||||
if (fd == -1) return -1;
|
if (fd != -1) {
|
||||||
pthread_cleanup_push((void *)sys_close, (void *)(intptr_t)fd);
|
rc = sys_read(fd, p, n);
|
||||||
rc = sys_read(fd, p, n);
|
} else {
|
||||||
pthread_cleanup_pop(1);
|
rc = -1;
|
||||||
|
}
|
||||||
|
ALLOW_SIGNALS;
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -252,7 +246,7 @@ ssize_t getrandom(void *p, size_t n, unsigned f) {
|
||||||
ssize_t rc;
|
ssize_t rc;
|
||||||
if ((!p && n) || (IsAsan() && !__asan_is_valid(p, n))) {
|
if ((!p && n) || (IsAsan() && !__asan_is_valid(p, n))) {
|
||||||
rc = efault();
|
rc = efault();
|
||||||
} else if ((f & ~(GRND_RANDOM | GRND_NONBLOCK))) {
|
} else if (f & ~(GRND_RANDOM | GRND_NONBLOCK)) {
|
||||||
rc = einval();
|
rc = einval();
|
||||||
} else {
|
} else {
|
||||||
rc = __getrandom(p, n, f);
|
rc = __getrandom(p, n, f);
|
||||||
|
|
|
@ -133,11 +133,12 @@ struct Keystrokes {
|
||||||
struct Dll *line;
|
struct Dll *line;
|
||||||
struct Dll *free;
|
struct Dll *free;
|
||||||
bool end_of_file;
|
bool end_of_file;
|
||||||
|
bool ohno_decckm;
|
||||||
unsigned char pc;
|
unsigned char pc;
|
||||||
uint16_t utf16hs;
|
uint16_t utf16hs;
|
||||||
pthread_mutex_t lock;
|
pthread_mutex_t lock;
|
||||||
struct Keystroke pool[512];
|
|
||||||
const struct VirtualKey *vkt;
|
const struct VirtualKey *vkt;
|
||||||
|
struct Keystroke pool[512];
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct Keystrokes __keystroke;
|
static struct Keystrokes __keystroke;
|
||||||
|
@ -352,11 +353,11 @@ static textwindows int ProcessMouseEvent(const struct NtInputRecord *r,
|
||||||
// we disable mouse highlighting when the tty is put in raw mode
|
// we disable mouse highlighting when the tty is put in raw mode
|
||||||
// to mouse wheel events with widely understood vt100 arrow keys
|
// to mouse wheel events with widely understood vt100 arrow keys
|
||||||
*p++ = 033;
|
*p++ = 033;
|
||||||
*p++ = '[';
|
*p++ = !__keystroke.ohno_decckm ? '[' : 'O';
|
||||||
if (isup) {
|
if (isup) {
|
||||||
*p++ = 'A'; // \e[A up
|
*p++ = 'A';
|
||||||
} else {
|
} else {
|
||||||
*p++ = 'B'; // \e[B down
|
*p++ = 'B';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -637,6 +638,7 @@ textwindows void InterceptTerminalCommands(const char *data, size_t size) {
|
||||||
} else if (data[i] == 'h') {
|
} else if (data[i] == 'h') {
|
||||||
if (x == 1) {
|
if (x == 1) {
|
||||||
__keystroke.vkt = kDecckm; // \e[?1h decckm on
|
__keystroke.vkt = kDecckm; // \e[?1h decckm on
|
||||||
|
__keystroke.ohno_decckm = true;
|
||||||
} else if ((ismouse |= IsMouseModeCommand(x))) {
|
} else if ((ismouse |= IsMouseModeCommand(x))) {
|
||||||
__ttyconf.magic |= kTtyXtMouse;
|
__ttyconf.magic |= kTtyXtMouse;
|
||||||
cm2 |= kNtEnableMouseInput;
|
cm2 |= kNtEnableMouseInput;
|
||||||
|
@ -646,6 +648,7 @@ textwindows void InterceptTerminalCommands(const char *data, size_t size) {
|
||||||
} else if (data[i] == 'l') {
|
} else if (data[i] == 'l') {
|
||||||
if (x == 1) {
|
if (x == 1) {
|
||||||
__keystroke.vkt = kVirtualKey; // \e[?1l decckm off
|
__keystroke.vkt = kVirtualKey; // \e[?1l decckm off
|
||||||
|
__keystroke.ohno_decckm = false;
|
||||||
} else if ((ismouse |= IsMouseModeCommand(x))) {
|
} else if ((ismouse |= IsMouseModeCommand(x))) {
|
||||||
__ttyconf.magic &= ~kTtyXtMouse;
|
__ttyconf.magic &= ~kTtyXtMouse;
|
||||||
cm2 |= kNtEnableQuickEditMode; // release mouse
|
cm2 |= kNtEnableQuickEditMode; // release mouse
|
||||||
|
@ -709,6 +712,7 @@ static textwindows int WaitForConsole(struct Fd *f, sigset_t waitmask) {
|
||||||
sigset_t m;
|
sigset_t m;
|
||||||
int64_t sem;
|
int64_t sem;
|
||||||
uint32_t ms = -1u;
|
uint32_t ms = -1u;
|
||||||
|
struct PosixThread *pt;
|
||||||
if (!__ttyconf.vmin) {
|
if (!__ttyconf.vmin) {
|
||||||
if (!__ttyconf.vtime) {
|
if (!__ttyconf.vtime) {
|
||||||
return 0; // non-blocking w/o raising eagain
|
return 0; // non-blocking w/o raising eagain
|
||||||
|
@ -719,7 +723,7 @@ static textwindows int WaitForConsole(struct Fd *f, sigset_t waitmask) {
|
||||||
if (f->flags & O_NONBLOCK) {
|
if (f->flags & O_NONBLOCK) {
|
||||||
return eagain(); // standard unix non-blocking
|
return eagain(); // standard unix non-blocking
|
||||||
}
|
}
|
||||||
struct PosixThread *pt = _pthread_self();
|
pt = _pthread_self();
|
||||||
pt->pt_flags |= PT_RESTARTABLE;
|
pt->pt_flags |= PT_RESTARTABLE;
|
||||||
pt->pt_semaphore = sem = CreateSemaphore(0, 0, 1, 0);
|
pt->pt_semaphore = sem = CreateSemaphore(0, 0, 1, 0);
|
||||||
pthread_cleanup_push((void *)CloseHandle, (void *)sem);
|
pthread_cleanup_push((void *)CloseHandle, (void *)sem);
|
||||||
|
@ -733,8 +737,8 @@ static textwindows int WaitForConsole(struct Fd *f, sigset_t waitmask) {
|
||||||
}
|
}
|
||||||
__sig_finishwait(m);
|
__sig_finishwait(m);
|
||||||
atomic_store_explicit(&pt->pt_blocker, PT_BLOCKER_CPU, memory_order_release);
|
atomic_store_explicit(&pt->pt_blocker, PT_BLOCKER_CPU, memory_order_release);
|
||||||
pthread_cleanup_pop(true);
|
|
||||||
pt->pt_flags &= ~PT_RESTARTABLE;
|
pt->pt_flags &= ~PT_RESTARTABLE;
|
||||||
|
pthread_cleanup_pop(true);
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,6 @@
|
||||||
#include "libc/log/libfatal.internal.h"
|
#include "libc/log/libfatal.internal.h"
|
||||||
#include "libc/nt/struct/context.h"
|
#include "libc/nt/struct/context.h"
|
||||||
#include "libc/str/str.h"
|
#include "libc/str/str.h"
|
||||||
|
|
||||||
#ifdef __x86_64__
|
#ifdef __x86_64__
|
||||||
|
|
||||||
textwindows void _ntcontext2linux(ucontext_t *ctx, const struct NtContext *cr) {
|
textwindows void _ntcontext2linux(ucontext_t *ctx, const struct NtContext *cr) {
|
||||||
|
|
|
@ -42,8 +42,6 @@ static struct {
|
||||||
* @note this function takes 5 cycles (30 if `__threaded`)
|
* @note this function takes 5 cycles (30 if `__threaded`)
|
||||||
* @note this function is not intended for cryptography
|
* @note this function is not intended for cryptography
|
||||||
* @note this function passes bigcrush and practrand
|
* @note this function passes bigcrush and practrand
|
||||||
* @asyncsignalsafe
|
|
||||||
* @vforksafe
|
|
||||||
*/
|
*/
|
||||||
uint64_t _rand64(void) {
|
uint64_t _rand64(void) {
|
||||||
void *p;
|
void *p;
|
||||||
|
|
|
@ -136,7 +136,7 @@ static abi wontreturn void WinInit(const char16_t *cmdline) {
|
||||||
m |= kNtEnableMouseInput | kNtEnableWindowInput |
|
m |= kNtEnableMouseInput | kNtEnableWindowInput |
|
||||||
kNtEnableProcessedInput;
|
kNtEnableProcessedInput;
|
||||||
} else {
|
} else {
|
||||||
m |= kNtEnableVirtualTerminalProcessing;
|
m |= kNtEnableProcessedOutput | kNtEnableVirtualTerminalProcessing;
|
||||||
}
|
}
|
||||||
__imp_SetConsoleMode(h, m);
|
__imp_SetConsoleMode(h, m);
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,11 +16,13 @@
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/assert.h"
|
||||||
#include "libc/errno.h"
|
#include "libc/errno.h"
|
||||||
#include "libc/mem/gc.internal.h"
|
#include "libc/mem/gc.internal.h"
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
#include "libc/proc/ntspawn.h"
|
#include "libc/proc/ntspawn.h"
|
||||||
#include "libc/str/str.h"
|
#include "libc/str/str.h"
|
||||||
|
#include "libc/testlib/ezbench.h"
|
||||||
#include "libc/testlib/testlib.h"
|
#include "libc/testlib/testlib.h"
|
||||||
|
|
||||||
char16_t cmdline[32767];
|
char16_t cmdline[32767];
|
||||||
|
@ -103,3 +105,11 @@ TEST(mkntcmdline, testWut) {
|
||||||
EXPECT_NE(-1, mkntcmdline(cmdline, argv));
|
EXPECT_NE(-1, mkntcmdline(cmdline, argv));
|
||||||
EXPECT_STREQ(u"C:\\Users\\jart\\𝑟𝑒𝑑𝑏𝑒𝑎𝑛.com --strace", cmdline);
|
EXPECT_STREQ(u"C:\\Users\\jart\\𝑟𝑒𝑑𝑏𝑒𝑎𝑛.com --strace", cmdline);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BENCH(mkntcmdline, lotsOfArgs) {
|
||||||
|
static char *argv[1000];
|
||||||
|
for (int i = 0; i < 999; ++i) {
|
||||||
|
argv[i] = "hello there hello there";
|
||||||
|
}
|
||||||
|
EZBENCH2("mkntcmdline", donothing, unassert(!mkntcmdline(cmdline, argv)));
|
||||||
|
}
|
||||||
|
|
|
@ -1,106 +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 2023 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/rlimit.h"
|
|
||||||
#include "libc/calls/struct/sigaction.h"
|
|
||||||
#include "libc/calls/struct/sigaltstack.h"
|
|
||||||
#include "libc/calls/struct/siginfo.h"
|
|
||||||
#include "libc/dce.h"
|
|
||||||
#include "libc/intrin/kprintf.h"
|
|
||||||
#include "libc/limits.h"
|
|
||||||
#include "libc/mem/gc.internal.h"
|
|
||||||
#include "libc/mem/mem.h"
|
|
||||||
#include "libc/runtime/runtime.h"
|
|
||||||
#include "libc/runtime/sysconf.h"
|
|
||||||
#include "libc/sysv/consts/rlimit.h"
|
|
||||||
#include "libc/sysv/consts/sa.h"
|
|
||||||
#include "libc/sysv/consts/sig.h"
|
|
||||||
#include "libc/sysv/consts/ss.h"
|
|
||||||
#include "libc/testlib/testlib.h"
|
|
||||||
#include "libc/thread/thread.h"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* stack overflow recovery technique #1
|
|
||||||
* overflow the gigantic main process stack
|
|
||||||
* simple but it can upset kernels / libraries
|
|
||||||
*/
|
|
||||||
|
|
||||||
jmp_buf recover;
|
|
||||||
volatile bool smashed_stack;
|
|
||||||
|
|
||||||
void CrashHandler(int sig, siginfo_t *si, void *ctx) {
|
|
||||||
struct sigaltstack ss;
|
|
||||||
ASSERT_SYS(0, 0, sigaltstack(0, &ss));
|
|
||||||
ASSERT_EQ(SS_ONSTACK, ss.ss_flags);
|
|
||||||
kprintf("kprintf avoids overflowing %G %p\n", si->si_signo, si->si_addr);
|
|
||||||
smashed_stack = true;
|
|
||||||
ASSERT_TRUE(__is_stack_overflow(si, ctx));
|
|
||||||
longjmp(recover, 123);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SetUp(void) {
|
|
||||||
|
|
||||||
// tune down the main process's stack size to a reasonable amount
|
|
||||||
// some operating systems, e.g. freebsd, will do things like have
|
|
||||||
// 500mb RLIMIT_STACK by default, even on machines with 400mb RAM
|
|
||||||
struct rlimit rl = {2 * 1024 * 1024, 2 * 1024 * 1024};
|
|
||||||
if (!IsWindows()) setrlimit(RLIMIT_STACK, &rl);
|
|
||||||
|
|
||||||
// set up the signal handler and alternative stack
|
|
||||||
struct sigaction sa;
|
|
||||||
struct sigaltstack ss;
|
|
||||||
ss.ss_flags = 0;
|
|
||||||
ss.ss_size = sysconf(_SC_MINSIGSTKSZ) + 8192;
|
|
||||||
ss.ss_sp = _mapanon(ss.ss_size);
|
|
||||||
ASSERT_SYS(0, 0, sigaltstack(&ss, 0));
|
|
||||||
sa.sa_flags = SA_SIGINFO | SA_ONSTACK; // <-- important
|
|
||||||
sigemptyset(&sa.sa_mask);
|
|
||||||
sa.sa_sigaction = CrashHandler;
|
|
||||||
sigaction(SIGBUS, &sa, 0);
|
|
||||||
sigaction(SIGSEGV, &sa, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int StackOverflow(int f(), int n) {
|
|
||||||
if (n < INT_MAX) {
|
|
||||||
return f(f, n + 1) - 1;
|
|
||||||
} else {
|
|
||||||
return INT_MAX;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int (*pStackOverflow)(int (*)(), int) = StackOverflow;
|
|
||||||
|
|
||||||
TEST(stackoverflow, standardStack_altStack_process_longjmp) {
|
|
||||||
if (IsTiny()) return; // TODO(jart): why?
|
|
||||||
|
|
||||||
int jumpcode;
|
|
||||||
if (!(jumpcode = setjmp(recover))) {
|
|
||||||
exit(pStackOverflow(pStackOverflow, 0));
|
|
||||||
}
|
|
||||||
ASSERT_EQ(123, jumpcode);
|
|
||||||
ASSERT_TRUE(smashed_stack);
|
|
||||||
|
|
||||||
// here's where longjmp() gets us into trouble
|
|
||||||
struct sigaltstack ss;
|
|
||||||
ASSERT_SYS(0, 0, sigaltstack(0, &ss));
|
|
||||||
if (IsXnu() || IsNetbsd()) {
|
|
||||||
ASSERT_EQ(SS_ONSTACK, ss.ss_flags); // wut
|
|
||||||
} else {
|
|
||||||
ASSERT_EQ(0, ss.ss_flags);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,105 +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 2023 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/sigaction.h"
|
|
||||||
#include "libc/calls/struct/sigaltstack.h"
|
|
||||||
#include "libc/calls/struct/siginfo.h"
|
|
||||||
#include "libc/dce.h"
|
|
||||||
#include "libc/intrin/kprintf.h"
|
|
||||||
#include "libc/limits.h"
|
|
||||||
#include "libc/mem/gc.internal.h"
|
|
||||||
#include "libc/mem/mem.h"
|
|
||||||
#include "libc/runtime/runtime.h"
|
|
||||||
#include "libc/runtime/sysconf.h"
|
|
||||||
#include "libc/sysv/consts/sa.h"
|
|
||||||
#include "libc/sysv/consts/sig.h"
|
|
||||||
#include "libc/sysv/consts/ss.h"
|
|
||||||
#include "libc/testlib/testlib.h"
|
|
||||||
#include "libc/thread/thread.h"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* stack overflow recovery technique #2
|
|
||||||
* longjmp out of signal back into thread
|
|
||||||
* simple but it can upset kernels / libraries
|
|
||||||
*/
|
|
||||||
|
|
||||||
jmp_buf recover;
|
|
||||||
volatile bool smashed_stack;
|
|
||||||
|
|
||||||
void CrashHandler(int sig, siginfo_t *si, void *ctx) {
|
|
||||||
struct sigaltstack ss;
|
|
||||||
ASSERT_SYS(0, 0, sigaltstack(0, &ss));
|
|
||||||
ASSERT_EQ(SS_ONSTACK, ss.ss_flags);
|
|
||||||
kprintf("kprintf avoids overflowing %G %p\n", si->si_signo, si->si_addr);
|
|
||||||
smashed_stack = true;
|
|
||||||
ASSERT_TRUE(__is_stack_overflow(si, ctx));
|
|
||||||
longjmp(recover, 123);
|
|
||||||
}
|
|
||||||
|
|
||||||
int StackOverflow(int f(), int n) {
|
|
||||||
if (n < INT_MAX) {
|
|
||||||
return f(f, n + 1) - 1;
|
|
||||||
} else {
|
|
||||||
return INT_MAX;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int (*pStackOverflow)(int (*)(), int) = StackOverflow;
|
|
||||||
|
|
||||||
void *MyPosixThread(void *arg) {
|
|
||||||
int jumpcode;
|
|
||||||
struct sigaction sa, o1, o2;
|
|
||||||
struct sigaltstack ss;
|
|
||||||
ss.ss_flags = 0;
|
|
||||||
ss.ss_size = sysconf(_SC_MINSIGSTKSZ) + 4096;
|
|
||||||
ss.ss_sp = gc(malloc(ss.ss_size));
|
|
||||||
ASSERT_SYS(0, 0, sigaltstack(&ss, 0));
|
|
||||||
sa.sa_flags = SA_SIGINFO | SA_ONSTACK; // <-- important
|
|
||||||
sigemptyset(&sa.sa_mask);
|
|
||||||
sa.sa_sigaction = CrashHandler;
|
|
||||||
sigaction(SIGBUS, &sa, &o1);
|
|
||||||
sigaction(SIGSEGV, &sa, &o2);
|
|
||||||
if (!(jumpcode = setjmp(recover))) {
|
|
||||||
exit(pStackOverflow(pStackOverflow, 0));
|
|
||||||
}
|
|
||||||
ASSERT_EQ(123, jumpcode);
|
|
||||||
sigaction(SIGSEGV, &o2, 0);
|
|
||||||
sigaction(SIGBUS, &o1, 0);
|
|
||||||
// here's where longjmp() gets us into trouble
|
|
||||||
ASSERT_SYS(0, 0, sigaltstack(0, &ss));
|
|
||||||
if (IsXnu() || IsNetbsd()) {
|
|
||||||
ASSERT_EQ(SS_ONSTACK, ss.ss_flags); // wut
|
|
||||||
} else {
|
|
||||||
ASSERT_EQ(0, ss.ss_flags);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(stackoverflow, standardStack_altStack_thread_longjmp) {
|
|
||||||
pthread_t th;
|
|
||||||
struct sigaltstack ss;
|
|
||||||
for (int i = 0; i < 2; ++i) {
|
|
||||||
smashed_stack = false;
|
|
||||||
pthread_create(&th, 0, MyPosixThread, 0);
|
|
||||||
pthread_join(th, 0);
|
|
||||||
ASSERT_TRUE(smashed_stack);
|
|
||||||
// this should be SS_DISABLE but ShowCrashReports() creates an alt stack
|
|
||||||
ASSERT_SYS(0, 0, sigaltstack(0, &ss));
|
|
||||||
ASSERT_EQ(0, ss.ss_flags);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,116 +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 2023 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/sigaction.h"
|
|
||||||
#include "libc/calls/struct/sigaltstack.h"
|
|
||||||
#include "libc/calls/struct/siginfo.h"
|
|
||||||
#include "libc/calls/struct/ucontext.internal.h"
|
|
||||||
#include "libc/calls/ucontext.h"
|
|
||||||
#include "libc/intrin/kprintf.h"
|
|
||||||
#include "libc/limits.h"
|
|
||||||
#include "libc/mem/gc.internal.h"
|
|
||||||
#include "libc/mem/mem.h"
|
|
||||||
#include "libc/runtime/runtime.h"
|
|
||||||
#include "libc/runtime/sysconf.h"
|
|
||||||
#include "libc/sysv/consts/sa.h"
|
|
||||||
#include "libc/sysv/consts/sig.h"
|
|
||||||
#include "libc/sysv/consts/ss.h"
|
|
||||||
#include "libc/testlib/testlib.h"
|
|
||||||
#include "libc/thread/thread.h"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* stack overflow recovery technique #3
|
|
||||||
* rewrite thread cpu state to call pthread_exit
|
|
||||||
* this method returns gracefully from signal handlers
|
|
||||||
* unfortunately it relies on cpu architecture knowledge
|
|
||||||
*/
|
|
||||||
|
|
||||||
volatile bool smashed_stack;
|
|
||||||
|
|
||||||
void Exiter(void *rc) {
|
|
||||||
struct sigaltstack ss;
|
|
||||||
ASSERT_SYS(0, 0, sigaltstack(0, &ss));
|
|
||||||
ASSERT_EQ(0, ss.ss_flags);
|
|
||||||
pthread_exit(rc);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CrashHandler(int sig, siginfo_t *si, void *arg) {
|
|
||||||
ucontext_t *ctx = arg;
|
|
||||||
struct sigaltstack ss;
|
|
||||||
ASSERT_SYS(0, 0, sigaltstack(0, &ss));
|
|
||||||
ASSERT_EQ(SS_ONSTACK, ss.ss_flags);
|
|
||||||
kprintf("kprintf avoids overflowing %G %p\n", si->si_signo, si->si_addr);
|
|
||||||
smashed_stack = true;
|
|
||||||
ASSERT_TRUE(__is_stack_overflow(si, ctx));
|
|
||||||
//
|
|
||||||
// the backtrace will look like this
|
|
||||||
//
|
|
||||||
// 0x000000000042561d: pthread_exit at pthread_exit.c:99
|
|
||||||
// 0x0000000000418777: Exiter at stackoverflow2_test.c:40
|
|
||||||
// 0x00000000004186d8: CrashHandler at stackoverflow2_test.c:49
|
|
||||||
// 0x000000000041872a: StackOverflow at stackoverflow2_test.c:53
|
|
||||||
// 0x000000000041872a: StackOverflow at stackoverflow2_test.c:53
|
|
||||||
// 0x000000000041872a: StackOverflow at stackoverflow2_test.c:53
|
|
||||||
// ...
|
|
||||||
//
|
|
||||||
ctx->uc_mcontext.ARG0 = 123;
|
|
||||||
ctx->uc_mcontext.PC = (long)Exiter;
|
|
||||||
ctx->uc_mcontext.SP += 32768;
|
|
||||||
ctx->uc_mcontext.SP &= -16;
|
|
||||||
ctx->uc_mcontext.SP -= 8;
|
|
||||||
}
|
|
||||||
|
|
||||||
int StackOverflow(int f(), int n) {
|
|
||||||
if (n < INT_MAX) {
|
|
||||||
return f(f, n + 1) - 1;
|
|
||||||
} else {
|
|
||||||
return INT_MAX;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int (*pStackOverflow)(int (*)(), int) = StackOverflow;
|
|
||||||
|
|
||||||
void *MyPosixThread(void *arg) {
|
|
||||||
struct sigaction sa;
|
|
||||||
struct sigaltstack ss;
|
|
||||||
ss.ss_flags = 0;
|
|
||||||
ss.ss_size = sysconf(_SC_MINSIGSTKSZ) + 4096;
|
|
||||||
ss.ss_sp = gc(malloc(ss.ss_size));
|
|
||||||
ASSERT_SYS(0, 0, sigaltstack(&ss, 0));
|
|
||||||
sa.sa_flags = SA_SIGINFO | SA_ONSTACK; // <-- important
|
|
||||||
sigemptyset(&sa.sa_mask);
|
|
||||||
sa.sa_sigaction = CrashHandler;
|
|
||||||
sigaction(SIGBUS, &sa, 0);
|
|
||||||
sigaction(SIGSEGV, &sa, 0);
|
|
||||||
exit(pStackOverflow(pStackOverflow, 0));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(stackoverflow, standardStack_altStack_thread_teleport) {
|
|
||||||
void *res;
|
|
||||||
pthread_t th;
|
|
||||||
struct sigaltstack ss;
|
|
||||||
smashed_stack = false;
|
|
||||||
pthread_create(&th, 0, MyPosixThread, 0);
|
|
||||||
pthread_join(th, &res);
|
|
||||||
ASSERT_EQ((void *)123L, res);
|
|
||||||
ASSERT_TRUE(smashed_stack);
|
|
||||||
// this should be SS_DISABLE but ShowCrashReports() creates an alt stack
|
|
||||||
ASSERT_SYS(0, 0, sigaltstack(0, &ss));
|
|
||||||
ASSERT_EQ(0, ss.ss_flags);
|
|
||||||
}
|
|
|
@ -16,20 +16,33 @@
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/calls/calls.h"
|
||||||
#include "libc/dce.h"
|
#include "libc/dce.h"
|
||||||
#include "libc/intrin/kprintf.h"
|
#include "libc/intrin/describeflags.internal.h"
|
||||||
#include "libc/nt/console.h"
|
#include "libc/nt/console.h"
|
||||||
#include "libc/nt/enum/consolemodeflags.h"
|
#include "libc/nt/enum/consolemodeflags.h"
|
||||||
#include "libc/nt/runtime.h"
|
#include "libc/nt/runtime.h"
|
||||||
#include "libc/runtime/runtime.h"
|
|
||||||
#include "libc/stdio/stdio.h"
|
|
||||||
#if defined(__x86_64__) && SupportsWindows()
|
#if defined(__x86_64__) && SupportsWindows()
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
if (!IsWindows()) {
|
if (!IsWindows()) {
|
||||||
kprintf("%s is intended for windows%n", argv[0]);
|
tinyprint(2, argv[0], " is intended for windows\n", NULL);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// clear console and print old config
|
||||||
|
uint32_t cm;
|
||||||
|
tinyprint(1, "\e[H\e[J", NULL);
|
||||||
|
tinyprint(1, "broken console settings were\r\n", NULL);
|
||||||
|
GetConsoleMode(GetStdHandle(kNtStdInputHandle), &cm);
|
||||||
|
tinyprint(1, "stdin: ", DescribeNtConsoleInFlags(cm), "\r\n", NULL);
|
||||||
|
GetConsoleMode(GetStdHandle(kNtStdOutputHandle), &cm);
|
||||||
|
tinyprint(1, "stdout: ", DescribeNtConsoleOutFlags(cm), "\r\n", NULL);
|
||||||
|
GetConsoleMode(GetStdHandle(kNtStdErrorHandle), &cm);
|
||||||
|
tinyprint(1, "stderr: ", DescribeNtConsoleOutFlags(cm), "\r\n", NULL);
|
||||||
|
|
||||||
|
// fix console settings
|
||||||
SetConsoleMode(GetStdHandle(kNtStdInputHandle),
|
SetConsoleMode(GetStdHandle(kNtStdInputHandle),
|
||||||
kNtEnableProcessedInput | kNtEnableLineInput |
|
kNtEnableProcessedInput | kNtEnableLineInput |
|
||||||
kNtEnableEchoInput | kNtEnableMouseInput |
|
kNtEnableEchoInput | kNtEnableMouseInput |
|
||||||
|
@ -40,13 +53,14 @@ int main(int argc, char *argv[]) {
|
||||||
SetConsoleMode(GetStdHandle(kNtStdErrorHandle),
|
SetConsoleMode(GetStdHandle(kNtStdErrorHandle),
|
||||||
kNtEnableProcessedOutput | kNtEnableWrapAtEolOutput |
|
kNtEnableProcessedOutput | kNtEnableWrapAtEolOutput |
|
||||||
kNtEnableVirtualTerminalProcessing);
|
kNtEnableVirtualTerminalProcessing);
|
||||||
_Exit(0);
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
fprintf(stderr,
|
tinyprint(2, "fixconsole not supported on this cpu arch or build config\n",
|
||||||
"fixconsole not supported on this cpu arch or build config\n");
|
NULL);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
#endif /* __x86_64__ && SupportsWindows() */
|
#endif /* __x86_64__ && SupportsWindows() */
|
||||||
|
|
Loading…
Reference in a new issue