Show crash reports on SIGSYS

This will help make it easier to troubleshoot ABI breakages with on
operating systems that, unlike Linux don't have ironclad guarantees
to not break userspace.
This commit is contained in:
Justine Tunney 2022-06-23 13:01:01 -07:00
parent a4601a24d3
commit 44da16255a
11 changed files with 83 additions and 19 deletions

View file

@ -12,6 +12,7 @@ int __notziposat(int, const char *);
int gethostname_bsd(char *, size_t) hidden;
int gethostname_linux(char *, size_t) hidden;
int gethostname_nt(char *, size_t, int) hidden;
long sys_bogus(void);
void *__vdsosym(const char *, const char *) hidden;
void __onfork(void) hidden;
void __restore_rt() hidden;

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/itoa.h"
#include "libc/log/log.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/sicode.h"
@ -29,13 +30,17 @@ static bool IsSiUser(int si_code) {
}
}
static void NameIt(char p[17], const char *s, int si_code) {
p = stpcpy(p, s);
FormatInt32(p, si_code);
}
/**
* Returns symbolic name for siginfo::si_code value.
*/
const char *GetSiCodeName(int sig, int si_code) {
static char b[16];
bzero(b, sizeof(b));
strcpy(b, "SI_???");
static char b[17];
NameIt(b, "SI_", si_code);
if (si_code == SI_QUEUE) {
strcpy(b + 3, "QUEUE"); /* sent by sigqueue(2) */
} else if (si_code == SI_TIMER) {
@ -55,7 +60,7 @@ const char *GetSiCodeName(int sig, int si_code) {
} else if (IsSiUser(si_code)) {
strcpy(b + 3, "USER"); /* sent by kill(2) i.e. from userspace */
} else if (sig == SIGCHLD) {
strcpy(b, "CLD_???");
NameIt(b, "CLD_", si_code);
if (si_code == CLD_EXITED) {
strcpy(b + 4, "EXITED"); /* child exited */
} else if (si_code == CLD_KILLED) {
@ -70,14 +75,14 @@ const char *GetSiCodeName(int sig, int si_code) {
strcpy(b + 4, "CONTINUED"); /* stopped child continued */
}
} else if (sig == SIGTRAP) {
strcpy(b, "TRAP_???");
NameIt(b, "TRAP_", si_code);
if (si_code == TRAP_BRKPT) {
strcpy(b + 5, "BRKPT"); /* process breakpoint */
} else if (si_code == TRAP_TRACE) {
strcpy(b + 5, "TRACE"); /* process trace trap */
}
} else if (sig == SIGSEGV) {
strcpy(b, "SEGV_???");
NameIt(b, "SEGV_", si_code);
if (si_code == SEGV_MAPERR) {
strcpy(b + 5, "MAPERR"); /* address not mapped to object */
} else if (si_code == SEGV_ACCERR) {
@ -86,7 +91,7 @@ const char *GetSiCodeName(int sig, int si_code) {
strcpy(b + 5, "PKUERR"); /* FreeBSD: x86: PKU violation */
}
} else if (sig == SIGFPE) {
strcpy(b, "FPE_???");
NameIt(b, "FPE_???", si_code);
if (si_code == FPE_INTDIV) {
strcpy(b + 4, "INTDIV"); /* integer divide by zero */
} else if (si_code == FPE_INTOVF) {
@ -105,7 +110,7 @@ const char *GetSiCodeName(int sig, int si_code) {
strcpy(b + 4, "FLTSUB"); /* subscript out of range */
}
} else if (sig == SIGILL) {
strcpy(b, "ILL_???");
NameIt(b, "ILL_", si_code);
if (si_code == ILL_ILLOPC) {
strcpy(b + 4, "ILLOPC"); /* illegal opcode */
} else if (si_code == ILL_ILLOPN) {
@ -124,7 +129,7 @@ const char *GetSiCodeName(int sig, int si_code) {
strcpy(b + 4, "BADSTK"); /* internal stack error */
}
} else if (sig == SIGBUS) {
strcpy(b, "BUS_???");
NameIt(b, "BUS_", si_code);
if (si_code == BUS_ADRALN) {
strcpy(b + 4, "ADRALN"); /* invalid address alignment */
} else if (si_code == BUS_ADRERR) {
@ -139,7 +144,7 @@ const char *GetSiCodeName(int sig, int si_code) {
strcpy(b + 4, "MCEERR_AO"); /* Linux 2.6.32+ */
}
} else if (sig == SIGIO) {
strcpy(b, "POLL_???");
NameIt(b, "POLL_", si_code);
if (si_code == POLL_IN) {
strcpy(b + 5, "IN"); /* data input available */
} else if (si_code == POLL_OUT) {

View file

@ -8,7 +8,7 @@
COSMOPOLITAN_C_START_
extern hidden bool __nocolor;
extern hidden int kCrashSigs[7];
extern hidden int kCrashSigs[8];
extern hidden bool g_isrunningundermake;
void __start_fatal(const char *, int) hidden;

View file

@ -58,7 +58,7 @@ static const char kGregNames[17][4] forcealign(1) = {
static const char kCpuFlags[12] forcealign(1) = "CVPRAKZSTIDO";
static const char kFpuExceptions[6] forcealign(1) = "IDZOUP";
int kCrashSigs[7];
int kCrashSigs[8];
relegated static void ShowFunctionCalls(ucontext_t *ctx) {
struct StackFrame *bp;

View file

@ -24,7 +24,7 @@
__oncrash_thunks:
// <SYNC-LIST>: showcrashreports.c, oncrashthunks.S, oncrash.c
// <SYNC-LIST>: showcrashreports.c, oncrashthunks.S, oncrash.c, internal.h
.org 11*0
__oncrash_sigquit:
@ -89,6 +89,15 @@ __oncrash_sigbus:
ret
.endfn __oncrash_sigbus,globl
// </SYNC-LIST>: showcrashreports.c, oncrashthunks.S, oncrash.c
.org 11*7
__oncrash_sigsys:
push %rbp
mov %rsp,%rbp
call __oncrash
pop %rbp
ret
.endfn __oncrash_sigsys,globl
// </SYNC-LIST>: showcrashreports.c, oncrashthunks.S, oncrash.c, internal.h
.endobj __oncrash_thunks,globl

View file

@ -39,7 +39,7 @@ STATIC_YOINK("__get_symbol_by_addr"); // for asan memory origin
extern const unsigned char __oncrash_thunks[8][11];
static struct sigaltstack g_oldsigaltstack;
static struct sigaction g_oldcrashacts[7];
static struct sigaction g_oldcrashacts[8];
static void InstallCrashHandlers(int extraflags) {
size_t i;
@ -104,6 +104,7 @@ void ShowCrashReports(void) {
kCrashSigs[4] = SIGTRAP; /* bad system call */
kCrashSigs[5] = SIGABRT; /* abort() called */
kCrashSigs[6] = SIGBUS; /* misaligned, noncanonical ptr, etc. */
kCrashSigs[7] = SIGSYS; /* bad system call */
/* </SYNC-LIST>: showcrashreports.c, oncrashthunks.S, oncrash.c */
if (!IsWindows()) {
bzero(&ss, sizeof(ss));

View file

@ -0,0 +1,35 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2022 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/macros.internal.h"
// Normally we call ShowCrashReports() from main, but if
// there's a crash in a constructor, this will help with
// troubleshooting it. You need to add:
//
// STATIC_YOINK("ShowCrashReportsEarly");
//
// To the top of your main module to use this.
.init.start 400,ShowCrashReportsEarly
push %rdi
push %rsi
call ShowCrashReports
pop %rsi
pop %rdi
.init.end 400,ShowCrashReportsEarly

View file

@ -0,0 +1,2 @@
.include "o/libc/sysv/macros.internal.inc"
.scall sys_bogus,0x5005005002500500,globl

View file

@ -375,6 +375,7 @@ scall io_uring_register 0xfffffffffffff1ab globl
#────────────────────────RHEL CLOUD────────────────────────── # ←┬─ red hat terminates community release of enterprise linux circa 2020
scall sys_pledge 0xfff06cffffffffff globl # └─ online linux services ban the president of united states of america
scall msyscall 0xfff025ffffffffff globl
scall sys_bogus 0x5005005002500500 globl
# The Fifth Bell System Interface, Community Edition
# » besiyata dishmaya

View file

@ -164,8 +164,17 @@ systemfive_bsdscrub:
systemfive_bsd:
cmp $0xfff,%ax
je systemfive_enosys
mov %rcx,%r10 # note: we do not create a stack frame
mov %rcx,%r10
push %rbx
push 32(%rsp)
push 24(%rsp)
push 16(%rsp)
mov %rax,%rbx # save ordinal for SIGSYS crash report
syscall # bsd will need arg on stack sometimes
pop %rbx
pop %rbx
pop %rbx
pop %rbx
jc systemfive_errno # bsd sets carry flag if %rax is errno
ret
.endfn systemfive_bsd

View file

@ -29,6 +29,7 @@
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/stat.h"
#include "libc/calls/struct/termios.h"
#include "libc/dce.h"
#include "libc/dns/dns.h"
#include "libc/dns/hoststxt.h"
#include "libc/dos.h"
@ -114,6 +115,9 @@
STATIC_STACK_SIZE(0x40000);
STATIC_YOINK("zip_uri_support");
#if !IsTiny()
STATIC_YOINK("ShowCrashReportsEarly");
#endif
/**
* @fileoverview redbean - single-file distributable web server
@ -7310,9 +7314,6 @@ void RedBean(int argc, char *argv[]) {
int main(int argc, char *argv[]) {
LoadZipArgs(&argc, &argv);
if (!IsTiny()) {
ShowCrashReports();
}
RedBean(argc, argv);
if (IsModeDbg()) {
CheckForMemoryLeaks();