185 lines · cpp
1// This test program creates a very large number of unique histories.2 3// Heap origin.4// RUN: %clangxx_msan -fsanitize-memory-track-origins=2 -O3 %s -o %t5 6// RUN: env MSAN_OPTIONS=origin_history_size=7 not %run %t >%t.out 2>&17// RUN: FileCheck %s --check-prefix=CHECK7 < %t.out8 9// RUN: env MSAN_OPTIONS=origin_history_size=2 not %run %t >%t.out 2>&110// RUN: FileCheck %s --check-prefix=CHECK2 < %t.out11 12// RUN: env MSAN_OPTIONS=origin_history_per_stack_limit=1 not %run %t >%t.out 2>&113// RUN: FileCheck %s --check-prefix=CHECK-PER-STACK --check-prefix=CHECK-%short-stack < %t.out14 15// RUN: env MSAN_OPTIONS=origin_history_size=7,origin_history_per_stack_limit=0 not %run %t >%t.out 2>&116// RUN: FileCheck %s --check-prefix=CHECK7 < %t.out17 18// Stack origin.19// RUN: %clangxx_msan -DSTACK -fsanitize-memory-track-origins=2 -O3 %s -o %t20 21// RUN: env MSAN_OPTIONS=origin_history_size=7 not %run %t >%t.out 2>&122// RUN: FileCheck %s --check-prefix=CHECK7 < %t.out23 24// RUN: env MSAN_OPTIONS=origin_history_size=2 not %run %t >%t.out 2>&125// RUN: FileCheck %s --check-prefix=CHECK2 < %t.out26 27// RUN: env MSAN_OPTIONS=origin_history_per_stack_limit=1 not %run %t >%t.out 2>&128// RUN: FileCheck %s --check-prefix=CHECK-PER-STACK --check-prefix=CHECK-%short-stack < %t.out29 30// RUN: env MSAN_OPTIONS=origin_history_size=7,origin_history_per_stack_limit=0 not %run %t >%t.out 2>&131// RUN: FileCheck %s --check-prefix=CHECK7 < %t.out32 33// Heap origin, with calls.34// RUN: %clangxx_msan -mllvm -msan-instrumentation-with-call-threshold=0 -fsanitize-memory-track-origins=2 -O3 %s -o %t35 36// RUN: env MSAN_OPTIONS=origin_history_size=7 not %run %t >%t.out 2>&137// RUN: FileCheck %s --check-prefix=CHECK7 < %t.out38 39// RUN: env MSAN_OPTIONS=origin_history_size=2 not %run %t >%t.out 2>&140// RUN: FileCheck %s --check-prefix=CHECK2 < %t.out41 42// RUN: env MSAN_OPTIONS=origin_history_per_stack_limit=1 not %run %t >%t.out 2>&143// RUN: FileCheck %s --check-prefix=CHECK-PER-STACK --check-prefix=CHECK-%short-stack < %t.out44 45// RUN: env MSAN_OPTIONS=origin_history_size=7,origin_history_per_stack_limit=0 not %run %t >%t.out 2>&146// RUN: FileCheck %s --check-prefix=CHECK7 < %t.out47 48// Stack origin, with calls.49// RUN: %clangxx_msan -DSTACK -mllvm -msan-instrumentation-with-call-threshold=0 -fsanitize-memory-track-origins=2 -O3 %s -o %t50 51// RUN: env MSAN_OPTIONS=origin_history_size=7 not %run %t >%t.out 2>&152// RUN: FileCheck %s --check-prefix=CHECK7 < %t.out53 54// RUN: env MSAN_OPTIONS=origin_history_size=2 not %run %t >%t.out 2>&155// RUN: FileCheck %s --check-prefix=CHECK2 < %t.out56 57// RUN: env MSAN_OPTIONS=origin_history_per_stack_limit=1 not %run %t >%t.out 2>&158// RUN: FileCheck %s --check-prefix=CHECK-PER-STACK --check-prefix=CHECK-%short-stack < %t.out59 60// RUN: env MSAN_OPTIONS=origin_history_size=7,origin_history_per_stack_limit=0 not %run %t >%t.out 2>&161// RUN: FileCheck %s --check-prefix=CHECK7 < %t.out62 63#include <stdio.h>64#include <stdlib.h>65#include <string.h>66#include <unistd.h>67 68static char *buf, *cur, *end;69void init() {70 buf = new char[1000];71#ifdef STACK72 char stackbuf[1000];73 char *volatile p = stackbuf;74 memcpy(buf, p, 1000);75#endif76 cur = buf;77 end = buf + 1000;78}79 80void line_flush() {81 char *p;82 for (p = cur - 1; p >= buf; --p)83 if (*p == '\n')84 break;85 if (p >= buf) {86 size_t write_sz = p - buf + 1;87 // write(2, buf, write_sz);88 memmove(buf, p + 1, end - p - 1);89 cur -= write_sz;90 }91}92 93void buffered_write(const char *p, size_t sz) {94 while (sz > 0) {95 size_t copy_sz = end - cur;96 if (sz < copy_sz) copy_sz = sz;97 memcpy(cur, p, copy_sz);98 cur += copy_sz;99 sz -= copy_sz;100 line_flush();101 }102}103 104void fn1() {105 buffered_write("a\n", 2);106}107 108void fn2() {109 buffered_write("a\n", 2);110}111 112void fn3() {113 buffered_write("a\n", 2);114}115 116int main(void) {117 init();118 for (int i = 0; i < 2000; ++i) {119 fn1();120 fn2();121 fn3();122 }123 return buf[50];124}125 126// CHECK7: WARNING: MemorySanitizer: use-of-uninitialized-value127// CHECK7-NOT: Uninitialized value was stored to memory at128// CHECK7: Uninitialized value was stored to memory at129// CHECK7-NOT: Uninitialized value was stored to memory at130// CHECK7: Uninitialized value was stored to memory at131// CHECK7-NOT: Uninitialized value was stored to memory at132// CHECK7: Uninitialized value was stored to memory at133// CHECK7-NOT: Uninitialized value was stored to memory at134// CHECK7: Uninitialized value was stored to memory at135// CHECK7-NOT: Uninitialized value was stored to memory at136// CHECK7: Uninitialized value was stored to memory at137// CHECK7-NOT: Uninitialized value was stored to memory at138// CHECK7: Uninitialized value was stored to memory at139// CHECK7-NOT: Uninitialized value was stored to memory at140// CHECK7: Uninitialized value was created141 142// CHECK2: WARNING: MemorySanitizer: use-of-uninitialized-value143// CHECK2-NOT: Uninitialized value was stored to memory at144// CHECK2: Uninitialized value was stored to memory at145// CHECK2-NOT: Uninitialized value was stored to memory at146// CHECK2: Uninitialized value was created147 148// For architectures with short stack all the stacks in the chain are same149// because the stack trace does not contain frames upto the functions fn1, fn2,150// fn3 from where the uninitialized stores actually originate. Since we report151// uninitialized value store once for each stack frame152// (origin_history_per_stack_limit = 1) we expect only one instance of153// "Uninitialized value was stored to memory at".154 155// CHECK-PER-STACK: WARNING: MemorySanitizer: use-of-uninitialized-value156// CHECK-PER-STACK: Uninitialized value was stored to memory at157// CHECK-SHORT-STACK: in __msan_memmove158// CHECK-FULL-STACK: in fn3159// CHECK-FULL-STACK: Uninitialized value was stored to memory at160// CHECK-FULL-STACK: in fn2161// CHECK-FULL-STACK: Uninitialized value was stored to memory at162// CHECK-FULL-STACK: in fn1163// CHECK-PER-STACK: Uninitialized value was created164 165// CHECK-UNLIMITED: WARNING: MemorySanitizer: use-of-uninitialized-value166// CHECK-UNLIMITED: Uninitialized value was stored to memory at167// CHECK-UNLIMITED: Uninitialized value was stored to memory at168// CHECK-UNLIMITED: Uninitialized value was stored to memory at169// CHECK-UNLIMITED: Uninitialized value was stored to memory at170// CHECK-UNLIMITED: Uninitialized value was stored to memory at171// CHECK-UNLIMITED: Uninitialized value was stored to memory at172// CHECK-UNLIMITED: Uninitialized value was stored to memory at173// CHECK-UNLIMITED: Uninitialized value was stored to memory at174// CHECK-UNLIMITED: Uninitialized value was stored to memory at175// CHECK-UNLIMITED: Uninitialized value was stored to memory at176// CHECK-UNLIMITED: Uninitialized value was stored to memory at177// CHECK-UNLIMITED: Uninitialized value was stored to memory at178// CHECK-UNLIMITED: Uninitialized value was stored to memory at179// CHECK-UNLIMITED: Uninitialized value was stored to memory at180// CHECK-UNLIMITED: Uninitialized value was stored to memory at181// CHECK-UNLIMITED: Uninitialized value was stored to memory at182// CHECK-UNLIMITED: Uninitialized value was stored to memory at183// CHECK-UNLIMITED: Uninitialized value was stored to memory at184// CHECK-UNLIMITED: Uninitialized value was created185