Last active
January 1, 2024 09:30
-
-
Save dlevi309/ebde046d73788b25d5066fa5e485120a to your computer and use it in GitHub Desktop.
Interpose-able code to catch crashes, print, and exit cleanly. Check near line 106 https://opensource.apple.com/source/libclosure/libclosure-67/objectTests/test.pl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <signal.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <execinfo.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
// from dyld-interposing.h | |
#define DYLD_INTERPOSE(_replacement,_replacee) __attribute__((used)) static struct{ const void* replacement; const void* replacee; } _interpose_##_replacee __attribute__ ((section ("__DATA,__interpose"))) = { (const void*)(unsigned long)&_replacement, (const void*)(unsigned long)&_replacee }; | |
static void catchcrash(int sig) | |
{ | |
const char *msg; | |
switch (sig) { | |
case SIGILL: msg = "CRASHED: SIGILL"; break; | |
case SIGBUS: msg = "CRASHED: SIGBUS"; break; | |
case SIGSYS: msg = "CRASHED: SIGSYS"; break; | |
case SIGSEGV: msg = "CRASHED: SIGSEGV"; break; | |
case SIGTRAP: msg = "CRASHED: SIGTRAP"; break; | |
case SIGABRT: msg = "CRASHED: SIGABRT"; break; | |
default: msg = "unknown signal"; break; | |
} | |
fprintf(stderr, "%s\n", msg); | |
void *array[10]; | |
size_t size; | |
size = backtrace(array, 10); | |
fprintf(stderr, "Stack trace:\n"); | |
backtrace_symbols_fd(array, size, STDERR_FILENO); | |
_exit(1); | |
} | |
static void setupcrash(void) __attribute__((constructor)); | |
static void setupcrash(void) | |
{ | |
signal(SIGILL, &catchcrash); | |
signal(SIGBUS, &catchcrash); | |
signal(SIGSYS, &catchcrash); | |
signal(SIGSEGV, &catchcrash); | |
signal(SIGTRAP, &catchcrash); | |
signal(SIGABRT, &catchcrash); | |
} | |
static int hacked = 0; | |
ssize_t hacked_write(int fildes, const void *buf, size_t nbyte) | |
{ | |
if (!hacked) { | |
setupcrash(); | |
hacked = 1; | |
} | |
return write(fildes, buf, nbyte); | |
} | |
DYLD_INTERPOSE(hacked_write, write); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
either compile as an individual library or an object
an example of what to expect: