Evgenii Stepanov 8a570a873b [hwasan] Support malloc in atfork.
Before this change we were locking the StackDepot in the fork()
interceptor. This results in a deadlock when allocator functions are
used in a pthread_atfork() callback.

Instead, set up a pthread_atfork() callback at init that locks/unlocks
both StackDepot and the allocator. Since our callback is set up very
early, the pre-fork callback is executed late, and both post-fork ones
are executed early, which works perfect for us.

Differential Revision: https://reviews.llvm.org/D108063
2021-08-17 15:29:49 -07:00

35 lines
753 B
C++

// RUN: %clang_hwasan -O0 %s -o %t && %run %t 2>&1
// REQUIRES: aarch64-target-arch || x86_64-target-arch
// REQUIRES: pointer-tagging
#include <assert.h>
#include <pthread.h>
#include <sanitizer/hwasan_interface.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
void *volatile sink;
int main(int argc, char **argv) {
pthread_atfork(nullptr, nullptr, []() {
alarm(5);
sink = malloc(10);
});
int pid = fork();
if (pid) {
int wstatus;
do {
waitpid(pid, &wstatus, 0);
} while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus)) {
fprintf(stderr, "abnormal exit\n");
return 1;
}
}
return 0;
}