xref: /llvm-project/compiler-rt/test/msan/scoped-interceptors.cpp (revision 5732cdc1be519eba3fe176cae2c8fea1e0ce7ea7)
1 // RUN: %clangxx_msan %s -o %t
2 // RUN: %run %t --disable-checks 0 2>&1 | FileCheck --check-prefix=DISABLED --allow-empty %s
3 // RUN: %run %t --disable-checks 1 2>&1 | FileCheck --check-prefix=DISABLED --allow-empty %s
4 // RUN: %run %t --disable-checks 2 2>&1 | FileCheck --check-prefix=DISABLED --allow-empty %s
5 // RUN: %run %t --disable-checks 3 2>&1 | FileCheck --check-prefix=DISABLED --allow-empty %s
6 // RUN: not %run %t --reenable-checks 0 2>&1 | FileCheck --check-prefix=CASE-0 %s
7 // RUN: not %run %t --reenable-checks 1 2>&1 | FileCheck --check-prefix=CASE-1 %s
8 // RUN: not %run %t --reenable-checks 2 2>&1 | FileCheck --check-prefix=CASE-2 %s
9 // RUN: not %run %t --reenable-checks 3 2>&1 | FileCheck --check-prefix=CASE-3 %s
10 
11 #include <assert.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sanitizer/msan_interface.h>
16 
main(int argc,char * argv[])17 int main(int argc, char *argv[]) {
18   assert(argc == 3);
19   __msan_scoped_disable_interceptor_checks();
20   if (strcmp(argv[1], "--reenable-checks") == 0)
21     __msan_scoped_enable_interceptor_checks();
22 
23   char uninit[7];
24   switch (argv[2][0]) {
25     case '0': {
26       char *copy = strndup(uninit, sizeof(uninit));  // BOOM
27       free(copy);
28       break;
29       // CASE-0: Uninitialized bytes in strndup
30     }
31     case '1': {
32       puts(uninit);  // BOOM
33       puts(uninit);  // Ensure previous call did not enable interceptor checks.
34       break;
35       // CASE-1: Uninitialized bytes in puts
36     }
37     case '2': {
38       int cmp = memcmp(uninit, uninit, sizeof(uninit));  // BOOM
39       break;
40       // CASE-2: Uninitialized bytes in MemcmpInterceptorCommon
41     }
42     case '3': {
43       size_t len = strlen(uninit);  // BOOM
44       break;
45       // CASE-3: Uninitialized bytes in strlen
46     }
47     default: assert(0);
48   }
49   // DISABLED-NOT: Uninitialized bytes
50   return 0;
51 }
52 
53