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

source code of compiler-rt/test/msan/use-after-dtor.cpp