Home | History | Annotate | Download | only in msan
      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 -m64 -O3 %s -o %t
      5 
      6 // RUN: MSAN_OPTIONS=origin_history_size=7 not %run %t >%t.out 2>&1
      7 // RUN: FileCheck %s --check-prefix=CHECK7 < %t.out
      8 
      9 // RUN: MSAN_OPTIONS=origin_history_size=2 not %run %t >%t.out 2>&1
     10 // RUN: FileCheck %s --check-prefix=CHECK2 < %t.out
     11 
     12 // RUN: MSAN_OPTIONS=origin_history_per_stack_limit=1 not %run %t >%t.out 2>&1
     13 // RUN: FileCheck %s --check-prefix=CHECK-PER-STACK < %t.out
     14 
     15 // Stack origin.
     16 // RUN: %clangxx_msan -DSTACK -fsanitize-memory-track-origins=2 -m64 -O3 %s -o %t
     17 
     18 // RUN: MSAN_OPTIONS=origin_history_size=7 not %run %t >%t.out 2>&1
     19 // RUN: FileCheck %s --check-prefix=CHECK7 < %t.out
     20 
     21 // RUN: MSAN_OPTIONS=origin_history_size=2 not %run %t >%t.out 2>&1
     22 // RUN: FileCheck %s --check-prefix=CHECK2 < %t.out
     23 
     24 // RUN: MSAN_OPTIONS=origin_history_per_stack_limit=1 not %run %t >%t.out 2>&1
     25 // RUN: FileCheck %s --check-prefix=CHECK-PER-STACK < %t.out
     26 
     27 
     28 // Heap origin, with calls.
     29 // RUN: %clangxx_msan -mllvm -msan-instrumentation-with-call-threshold=0 -fsanitize-memory-track-origins=2 -m64 -O3 %s -o %t
     30 
     31 // RUN: MSAN_OPTIONS=origin_history_size=7 not %run %t >%t.out 2>&1
     32 // RUN: FileCheck %s --check-prefix=CHECK7 < %t.out
     33 
     34 // RUN: MSAN_OPTIONS=origin_history_size=2 not %run %t >%t.out 2>&1
     35 // RUN: FileCheck %s --check-prefix=CHECK2 < %t.out
     36 
     37 // RUN: MSAN_OPTIONS=origin_history_per_stack_limit=1 not %run %t >%t.out 2>&1
     38 // RUN: FileCheck %s --check-prefix=CHECK-PER-STACK < %t.out
     39 
     40 
     41 // Stack origin, with calls.
     42 // RUN: %clangxx_msan -DSTACK -mllvm -msan-instrumentation-with-call-threshold=0 -fsanitize-memory-track-origins=2 -m64 -O3 %s -o %t
     43 
     44 // RUN: MSAN_OPTIONS=origin_history_size=7 not %run %t >%t.out 2>&1
     45 // RUN: FileCheck %s --check-prefix=CHECK7 < %t.out
     46 
     47 // RUN: MSAN_OPTIONS=origin_history_size=2 not %run %t >%t.out 2>&1
     48 // RUN: FileCheck %s --check-prefix=CHECK2 < %t.out
     49 
     50 // RUN: MSAN_OPTIONS=origin_history_per_stack_limit=1 not %run %t >%t.out 2>&1
     51 // RUN: FileCheck %s --check-prefix=CHECK-PER-STACK < %t.out
     52 
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <unistd.h>
     57 
     58 static char *buf, *cur, *end;
     59 void init() {
     60   buf = new char[1000];
     61 #ifdef STACK
     62   char stackbuf[1000];
     63   char *volatile p = stackbuf;
     64   memcpy(buf, p, 1000);
     65 #endif
     66   cur = buf;
     67   end = buf + 1000;
     68 }
     69 
     70 void line_flush() {
     71   char *p;
     72   for (p = cur - 1; p >= buf; --p)
     73     if (*p == '\n')
     74       break;
     75   if (p >= buf) {
     76     size_t write_sz = p - buf + 1;
     77     // write(2, buf, write_sz);
     78     memmove(buf, p + 1, end - p - 1);
     79     cur -= write_sz;
     80   }
     81 }
     82 
     83 void buffered_write(const char *p, size_t sz) {
     84   while (sz > 0) {
     85     size_t copy_sz = end - cur;
     86     if (sz < copy_sz) copy_sz = sz;
     87     memcpy(cur, p, copy_sz);
     88     cur += copy_sz;
     89     sz -= copy_sz;
     90     line_flush();
     91   }
     92 }
     93 
     94 void fn1() {
     95   buffered_write("a\n", 2);
     96 }
     97 
     98 void fn2() {
     99   buffered_write("a\n", 2);
    100 }
    101 
    102 void fn3() {
    103   buffered_write("a\n", 2);
    104 }
    105 
    106 int main(void) {
    107   init();
    108   for (int i = 0; i < 2000; ++i) {
    109     fn1();
    110     fn2();
    111     fn3();
    112   }
    113   return buf[50];
    114 }
    115 
    116 // CHECK7: WARNING: MemorySanitizer: use-of-uninitialized-value
    117 // CHECK7-NOT: Uninitialized value was stored to memory at
    118 // CHECK7: Uninitialized value was stored to memory at
    119 // CHECK7-NOT: Uninitialized value was stored to memory at
    120 // CHECK7: Uninitialized value was stored to memory at
    121 // CHECK7-NOT: Uninitialized value was stored to memory at
    122 // CHECK7: Uninitialized value was stored to memory at
    123 // CHECK7-NOT: Uninitialized value was stored to memory at
    124 // CHECK7: Uninitialized value was stored to memory at
    125 // CHECK7-NOT: Uninitialized value was stored to memory at
    126 // CHECK7: Uninitialized value was stored to memory at
    127 // CHECK7-NOT: Uninitialized value was stored to memory at
    128 // CHECK7: Uninitialized value was stored to memory at
    129 // CHECK7-NOT: Uninitialized value was stored to memory at
    130 // CHECK7: Uninitialized value was created
    131 
    132 // CHECK2: WARNING: MemorySanitizer: use-of-uninitialized-value
    133 // CHECK2-NOT: Uninitialized value was stored to memory at
    134 // CHECK2: Uninitialized value was stored to memory at
    135 // CHECK2-NOT: Uninitialized value was stored to memory at
    136 // CHECK2: Uninitialized value was created
    137 
    138 // CHECK-PER-STACK: WARNING: MemorySanitizer: use-of-uninitialized-value
    139 // CHECK-PER-STACK: Uninitialized value was stored to memory at
    140 // CHECK-PER-STACK: in fn3
    141 // CHECK-PER-STACK: Uninitialized value was stored to memory at
    142 // CHECK-PER-STACK: in fn2
    143 // CHECK-PER-STACK: Uninitialized value was stored to memory at
    144 // CHECK-PER-STACK: in fn1
    145 // CHECK-PER-STACK: Uninitialized value was created
    146