Skip to content

Instantly share code, notes, and snippets.

@michaeleisel
Last active January 9, 2024 10:19
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save michaeleisel/8eddd0082b4fd7f2bd118d97e79bf12e to your computer and use it in GitHub Desktop.
#import "fishhook.h" // from https://github.com/facebook/fishhook
// Replace write and writev, the two "chokepoint" functions that writes to stderr and stdout will probably pass through
static int (*originalWritev)(int fd, const struct iovec *v, int n);
static int (*originalWrite)(int fd, const void *buf, size_t size);
void checkForBadConstraintsMessage(int fd, const char *string, size_t size) {
if (strnstr(string, "UIViewAlertForUnsatisfiableConstraints", size)) {
// Write it to the console anyways before crashing
originalWrite(fd, string, size);
abort();
}
}
void writeWillOccur(int fd, const char *string, size_t size) {
if (fd != STDERR_FILENO && fd != STDOUT_FILENO) {
return;
}
// #########################################
// #########################################
// ######### Insert code at this point. for example, you could call checkForBadConstraintsMessage(string, size)
// #########################################
// #########################################
}
int myWritev(int fd, const struct iovec *v, int n) {
for (int i = 0; i < n; i++) {
writeWillOccur(fd, v[i].iov_base, v[i].iov_len);
}
// Keep this as the last line, or else make sure to restore errno right before this function returns
return originalWritev(fd, v, n);
}
int myWrite(int fd, const void *buf, size_t size) {
writeWillOccur(fd, buf, size);
// Keep this as the last line, or else make sure to restore errno right before this function returns
return originalWrite(fd, buf, size);
}
...
// This does the function replacement. Any calls before this will call the original functions and not ours
rebind_symbols((struct rebinding[2]){{"write", myWrite, (void *)&originalWrite}, {"writev", myWritev, (void *)&originalWritev}}, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment