File: scoped-interceptors.cpp

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,418,840 kB
  • sloc: cpp: 5,290,826; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,898; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,913; xml: 953; cs: 573; fortran: 539
file content (52 lines) | stat: -rw-r--r-- 1,856 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// RUN: %clangxx_msan %s -o %t
// RUN: %run %t --disable-checks 0 2>&1 | FileCheck --check-prefix=DISABLED --allow-empty %s
// RUN: %run %t --disable-checks 1 2>&1 | FileCheck --check-prefix=DISABLED --allow-empty %s
// RUN: %run %t --disable-checks 2 2>&1 | FileCheck --check-prefix=DISABLED --allow-empty %s
// RUN: %run %t --disable-checks 3 2>&1 | FileCheck --check-prefix=DISABLED --allow-empty %s
// RUN: not %run %t --reenable-checks 0 2>&1 | FileCheck --check-prefix=CASE-0 %s
// RUN: not %run %t --reenable-checks 1 2>&1 | FileCheck --check-prefix=CASE-1 %s
// RUN: not %run %t --reenable-checks 2 2>&1 | FileCheck --check-prefix=CASE-2 %s
// RUN: not %run %t --reenable-checks 3 2>&1 | FileCheck --check-prefix=CASE-3 %s

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sanitizer/msan_interface.h>

int main(int argc, char *argv[]) {
  assert(argc == 3);
  __msan_scoped_disable_interceptor_checks();
  if (strcmp(argv[1], "--reenable-checks") == 0)
    __msan_scoped_enable_interceptor_checks();

  char uninit[7];
  switch (argv[2][0]) {
    case '0': {
      char *copy = strndup(uninit, sizeof(uninit));  // BOOM
      free(copy);
      break;
      // CASE-0: Uninitialized bytes in __interceptor_strndup
    }
    case '1': {
      puts(uninit);  // BOOM
      puts(uninit);  // Ensure previous call did not enable interceptor checks.
      break;
      // CASE-1: Uninitialized bytes in __interceptor_puts
    }
    case '2': {
      int cmp = memcmp(uninit, uninit, sizeof(uninit));  // BOOM
      break;
      // CASE-2: Uninitialized bytes in MemcmpInterceptorCommon
    }
    case '3': {
      size_t len = strlen(uninit);  // BOOM
      break;
      // CASE-3: Uninitialized bytes in __interceptor_strlen
    }
    default: assert(0);
  }
  // DISABLED-NOT: Uninitialized bytes
  return 0;
}