1 // RUN: %clangxx_msan %s -fsanitize=memory -fsanitize-memory-use-after-dtor -o %t && MSAN_OPTIONS=poison_in_dtor=1 not %run %t >%t.out 2>&1
2 // RUN: FileCheck %s --check-prefix=CHECK-UAD < %t.out
3
4 // RUN: %clangxx_msan %s -O1 -fsanitize=memory -fsanitize-memory-use-after-dtor -o %t && MSAN_OPTIONS=poison_in_dtor=1 not %run %t >%t.out 2>&1
5 // RUN: FileCheck %s --check-prefix=CHECK-UAD < %t.out
6
7 // RUN: %clangxx_msan %s -O2 -fsanitize=memory -fsanitize-memory-use-after-dtor -o %t && MSAN_OPTIONS=poison_in_dtor=1 not %run %t >%t.out 2>&1
8 // RUN: FileCheck %s --check-prefix=CHECK-UAD < %t.out
9
10 // RUN: %clangxx_msan %s -O1 -fsanitize=memory -fsanitize-memory-use-after-dtor -fsanitize-memory-track-origins -o %t && MSAN_OPTIONS=poison_in_dtor=1 not %run %t >%t.out 2>&1
11 // RUN: FileCheck %s --check-prefixes=CHECK-UAD,CHECK-ORIGINS < %t.out
12
13 // RUN: %clangxx_msan %s -fno-sanitize-memory-use-after-dtor -o %t && MSAN_OPTIONS=poison_in_dtor=1 not %run %t > %t.out 2>&1
14 // RUN: FileCheck %s --check-prefix=CHECK-UAD-OFF < %t.out
15
16 #include <sanitizer/msan_interface.h>
17 #include <assert.h>
18 #include <stdio.h>
19 #include <new>
20
21 struct Simple {
22 int x_;
SimpleSimple23 Simple() {
24 x_ = 5;
25 }
~SimpleSimple26 ~Simple() {
27 x_ += 1;
28 }
29 };
30
main()31 int main() {
32 unsigned long buf[1];
33 assert(sizeof(Simple) <= sizeof(buf));
34
35 Simple *s = new(&buf) Simple();
36 s->~Simple();
37
38 fprintf(stderr, "\n"); // Need output to parse for CHECK-UAD-OFF case
39 return s->x_;
40
41 // CHECK-UAD: WARNING: MemorySanitizer: use-of-uninitialized-value
42 // CHECK-UAD: {{#0 0x.* in main.*use-after-dtor.cpp:}}[[@LINE-3]]
43
44 // CHECK-ORIGINS: Memory was marked as uninitialized
45 // CHECK-ORIGINS: {{#0 0x.* in __sanitizer_dtor_callback}}
46 // CHECK-ORIGINS: {{#1 0x.* in .*~Simple}}
47
48 // CHECK-UAD: SUMMARY: MemorySanitizer: use-of-uninitialized-value {{.*main}}
49 // CHECK-UAD-OFF-NOT: SUMMARY: MemorySanitizer: use-of-uninitialized-value
50 }
51