1// Checks that the __sanitizer_on_print hook gets the exact same sanitizer
2// report as what is printed to stderr.
3//
4// RUN: %clangxx %s -o %t
5// RUN: %run %t %t-onprint.txt 2>%t-stderr.txt || true
6// RUN: diff %t-onprint.txt %t-stderr.txt
7//
8// UNSUPPORTED: android
9
10#include <cassert>
11#include <cstdlib>
12#include <fcntl.h>
13#include <string.h>
14#include <sys/stat.h>
15#include <sys/types.h>
16#include <unistd.h>
17
18int f;
19volatile void *buf;
20volatile char sink;
21
22__attribute__((disable_sanitizer_instrumentation)) extern "C" void
23__sanitizer_on_print(const char *str) {
24 write(fd: f, buf: str, n: strlen(s: str));
25}
26
27int main(int argc, char *argv[]) {
28 assert(argc >= 2);
29 f = open(file: argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
30
31 // Use-after-free to trigger ASan/TSan reports.
32 void *ptr = malloc(1);
33 buf = ptr;
34 free(ptr);
35 sink = *static_cast<char *>(ptr);
36 return 0;
37}
38

source code of compiler-rt/test/sanitizer_common/TestCases/onprint.cpp