File: msan_copy_shadow.cc

package info (click to toggle)
llvm-toolchain-7 1%3A7.0.1-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 733,456 kB
  • sloc: cpp: 3,776,651; ansic: 633,271; asm: 350,301; python: 142,716; objc: 107,612; sh: 22,626; lisp: 11,056; perl: 7,999; pascal: 6,742; ml: 5,537; awk: 3,536; makefile: 2,557; cs: 2,027; xml: 841; ruby: 156
file content (35 lines) | stat: -rw-r--r-- 1,444 bytes parent folder | download | duplicates (7)
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
// Test that __msan_copy_shadow copies shadow, updates origin and does not touch
// the application memory.
// RUN: %clangxx_msan -fsanitize-memory-track-origins=0 -O0 %s -o %t && not %run %t 2>&1
// RUN: %clangxx_msan -fsanitize-memory-track-origins=2 -O0 %s -o %t && not %run %t 2>&1 | FileCheck --check-prefix=CHECK --check-prefix=CHECK-%short-stack %s

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

int main() {
  char *a = new char[4];
  char *b = new char[4];
  a[1] = 1;
  a[3] = 2;
  memset(b, 42, 4);

  // Test that __msan_copy_shadow does not touch the contents of b[].
  __msan_copy_shadow(b, a, 4);
  __msan_unpoison(b, 4);
  assert(b[0] == 42 && b[1] == 42 && b[2] == 42 && b[3] == 42);

  // Test that __msan_copy_shadow correctly updates shadow and origin of b[].
  __msan_copy_shadow(b, a, 4);
  assert(__msan_test_shadow(b, 4) == 0);
  assert(__msan_test_shadow(b + 1, 3) == 1);
  assert(__msan_test_shadow(b + 3, 1) == -1);
  __msan_check_mem_is_initialized(b, 4);
  // CHECK: use-of-uninitialized-value
  // CHECK:   {{in main.*msan_copy_shadow.cc:}}[[@LINE-2]]
  // CHECK: Uninitialized value was stored to memory at
  // CHECK-FULL-STACK:   {{in main.*msan_copy_shadow.cc:}}[[@LINE-8]]
  // CHECK-SHORT-STACK:   {{in __msan_copy_shadow .*msan_interceptors.cc:}}
  // CHECK: Uninitialized value was created by a heap allocation
  // CHECK:   {{in main.*msan_copy_shadow.cc:}}[[@LINE-23]]
}