xref: /llvm-project/compiler-rt/test/ubsan/TestCases/Misc/monitor.cpp (revision ba148a1c86775bf6e6ab14c65011acc6555ef11e)
1aba150caSVitaly Buka // RUN: %clangxx -w -fsanitize=bool -fno-sanitize-memory-param-retval %s -o %t
2059d2036SVedant Kumar // RUN: %run %t 2>&1 | FileCheck %s
3059d2036SVedant Kumar 
4487bef37SVedant Kumar // __ubsan_on_report is not defined as weak. Redefining it here isn't supported
5487bef37SVedant Kumar // on Windows.
6487bef37SVedant Kumar //
7*ba148a1cSMartin Storsjö // UNSUPPORTED: target={{.*windows.*}}
8c33f35ffSDavid Carlier // Linkage issue
9e6e43629SPaul Robinson // XFAIL: target={{.*openbsd.*}}
10487bef37SVedant Kumar 
11a7c9fe37SJonas Hahnfeld #include <cstdio>
12059d2036SVedant Kumar 
130aefc946SRoy Sundahl // Override __ubsan_on_report() from the runtime, just for testing purposes.
140aefc946SRoy Sundahl // Required for dyld macOS 12.0+
150aefc946SRoy Sundahl #if (__APPLE__)
160aefc946SRoy Sundahl __attribute__((weak))
170aefc946SRoy Sundahl #endif
180aefc946SRoy Sundahl extern "C" void
__ubsan_on_report(void)190aefc946SRoy Sundahl __ubsan_on_report(void) {
200aefc946SRoy Sundahl   void __ubsan_get_current_report_data(
210aefc946SRoy Sundahl       const char **OutIssueKind, const char **OutMessage,
220aefc946SRoy Sundahl       const char **OutFilename, unsigned *OutLine, unsigned *OutCol,
23059d2036SVedant Kumar       char **OutMemoryAddr);
24059d2036SVedant Kumar   const char *IssueKind, *Message, *Filename;
25059d2036SVedant Kumar   unsigned Line, Col;
26059d2036SVedant Kumar   char *Addr;
270aefc946SRoy Sundahl 
28059d2036SVedant Kumar   __ubsan_get_current_report_data(&IssueKind, &Message, &Filename, &Line, &Col,
29059d2036SVedant Kumar                                   &Addr);
30059d2036SVedant Kumar 
31a7c9fe37SJonas Hahnfeld   printf("Issue: %s\n", IssueKind);
32a7c9fe37SJonas Hahnfeld   printf("Location: %s:%u:%u\n", Filename, Line, Col);
33a7c9fe37SJonas Hahnfeld   printf("Message: %s\n", Message);
34a8970dffSFangrui Song   fflush(stdout);
35059d2036SVedant Kumar 
36059d2036SVedant Kumar   (void)Addr;
37059d2036SVedant Kumar }
38059d2036SVedant Kumar 
main()39059d2036SVedant Kumar int main() {
40059d2036SVedant Kumar   char C = 3;
41059d2036SVedant Kumar   bool B = *(bool *)&C;
42059d2036SVedant Kumar   // CHECK: Issue: invalid-bool-load
43059d2036SVedant Kumar   // CHECK-NEXT: Location: {{.*}}monitor.cpp:[[@LINE-2]]:12
44059d2036SVedant Kumar   // CHECK-NEXT: Message: Load of value 3, which is not a valid value for type 'bool'
45059d2036SVedant Kumar   return 0;
46059d2036SVedant Kumar }
47