xref: /llvm-project/compiler-rt/test/ubsan/TestCases/Misc/monitor.cpp (revision ba148a1c86775bf6e6ab14c65011acc6555ef11e)
1 // RUN: %clangxx -w -fsanitize=bool -fno-sanitize-memory-param-retval %s -o %t
2 // RUN: %run %t 2>&1 | FileCheck %s
3 
4 // __ubsan_on_report is not defined as weak. Redefining it here isn't supported
5 // on Windows.
6 //
7 // UNSUPPORTED: target={{.*windows.*}}
8 // Linkage issue
9 // XFAIL: target={{.*openbsd.*}}
10 
11 #include <cstdio>
12 
13 // Override __ubsan_on_report() from the runtime, just for testing purposes.
14 // Required for dyld macOS 12.0+
15 #if (__APPLE__)
16 __attribute__((weak))
17 #endif
18 extern "C" void
__ubsan_on_report(void)19 __ubsan_on_report(void) {
20   void __ubsan_get_current_report_data(
21       const char **OutIssueKind, const char **OutMessage,
22       const char **OutFilename, unsigned *OutLine, unsigned *OutCol,
23       char **OutMemoryAddr);
24   const char *IssueKind, *Message, *Filename;
25   unsigned Line, Col;
26   char *Addr;
27 
28   __ubsan_get_current_report_data(&IssueKind, &Message, &Filename, &Line, &Col,
29                                   &Addr);
30 
31   printf("Issue: %s\n", IssueKind);
32   printf("Location: %s:%u:%u\n", Filename, Line, Col);
33   printf("Message: %s\n", Message);
34   fflush(stdout);
35 
36   (void)Addr;
37 }
38 
main()39 int main() {
40   char C = 3;
41   bool B = *(bool *)&C;
42   // CHECK: Issue: invalid-bool-load
43   // CHECK-NEXT: Location: {{.*}}monitor.cpp:[[@LINE-2]]:12
44   // CHECK-NEXT: Message: Load of value 3, which is not a valid value for type 'bool'
45   return 0;
46 }
47