xref: /aosp_15_r20/external/compiler-rt/test/msan/dtor-member.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx_msan %s -fsanitize=memory -fsanitize-memory-use-after-dtor -o %t && MSAN_OPTIONS=poison_in_dtor=1 %run %t >%t.out 2>&1
2*7c3d14c8STreehugger Robot // RUN: FileCheck %s < %t.out
3*7c3d14c8STreehugger Robot 
4*7c3d14c8STreehugger Robot // RUN: %clangxx_msan %s -O1 -fsanitize=memory -fsanitize-memory-use-after-dtor -o %t && MSAN_OPTIONS=poison_in_dtor=1 %run %t >%t.out 2>&1
5*7c3d14c8STreehugger Robot // RUN: FileCheck %s < %t.out
6*7c3d14c8STreehugger Robot 
7*7c3d14c8STreehugger Robot // RUN: %clangxx_msan %s -O2 -fsanitize=memory -fsanitize-memory-use-after-dtor -o %t && MSAN_OPTIONS=poison_in_dtor=1  %run %t >%t.out 2>&1
8*7c3d14c8STreehugger Robot // RUN: FileCheck %s < %t.out
9*7c3d14c8STreehugger Robot 
10*7c3d14c8STreehugger Robot // RUN: %clangxx_msan %s -fsanitize=memory -o %t && MSAN_OPTIONS=poison_in_dtor=1  %run %t >%t.out 2>&1
11*7c3d14c8STreehugger Robot // RUN: FileCheck %s --check-prefix=CHECK-NO-FLAG < %t.out
12*7c3d14c8STreehugger Robot 
13*7c3d14c8STreehugger Robot // RUN: %clangxx_msan -fsanitize=memory -fsanitize-memory-use-after-dtor %s -o %t && MSAN_OPTIONS=poison_in_dtor=0 %run %t >%t.out 2>&1
14*7c3d14c8STreehugger Robot // RUN: FileCheck %s --check-prefix=CHECK-NO-FLAG < %t.out
15*7c3d14c8STreehugger Robot 
16*7c3d14c8STreehugger Robot #include <sanitizer/msan_interface.h>
17*7c3d14c8STreehugger Robot #include <assert.h>
18*7c3d14c8STreehugger Robot #include <stdio.h>
19*7c3d14c8STreehugger Robot #include <new>
20*7c3d14c8STreehugger Robot 
21*7c3d14c8STreehugger Robot struct Simple {
22*7c3d14c8STreehugger Robot   int x_;
SimpleSimple23*7c3d14c8STreehugger Robot   Simple() {
24*7c3d14c8STreehugger Robot     x_ = 5;
25*7c3d14c8STreehugger Robot   }
~SimpleSimple26*7c3d14c8STreehugger Robot   ~Simple() { }
27*7c3d14c8STreehugger Robot };
28*7c3d14c8STreehugger Robot 
main()29*7c3d14c8STreehugger Robot int main() {
30*7c3d14c8STreehugger Robot   unsigned long buf[1];
31*7c3d14c8STreehugger Robot   assert(sizeof(Simple) <= sizeof(buf));
32*7c3d14c8STreehugger Robot 
33*7c3d14c8STreehugger Robot   // The placement new operator forces the object to be constructed in the
34*7c3d14c8STreehugger Robot   // memory location &buf. Since objects made in this way must be explicitly
35*7c3d14c8STreehugger Robot   // destroyed, there are no implicit calls inserted that would interfere with
36*7c3d14c8STreehugger Robot   // test behavior.
37*7c3d14c8STreehugger Robot   Simple *s = new(&buf) Simple();
38*7c3d14c8STreehugger Robot   s->~Simple();
39*7c3d14c8STreehugger Robot 
40*7c3d14c8STreehugger Robot   if (__msan_test_shadow(s, sizeof(*s)) != -1)
41*7c3d14c8STreehugger Robot     printf("s is poisoned\n");
42*7c3d14c8STreehugger Robot   else
43*7c3d14c8STreehugger Robot     printf("s is not poisoned\n");
44*7c3d14c8STreehugger Robot   // CHECK: s is poisoned
45*7c3d14c8STreehugger Robot   // CHECK-NO-FLAG: s is not poisoned
46*7c3d14c8STreehugger Robot 
47*7c3d14c8STreehugger Robot   return 0;
48*7c3d14c8STreehugger Robot }
49