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