xref: /aosp_15_r20/external/compiler-rt/test/msan/dtor-base-access.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx_msan %s -O0 -fsanitize=memory -fsanitize-memory-use-after-dtor -o %t && MSAN_OPTIONS=poison_in_dtor=1 %run %t >%t.out 2>&1
2*7c3d14c8STreehugger Robot 
3*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
4*7c3d14c8STreehugger Robot 
5*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
6*7c3d14c8STreehugger Robot 
7*7c3d14c8STreehugger Robot #include <sanitizer/msan_interface.h>
8*7c3d14c8STreehugger Robot #include <assert.h>
9*7c3d14c8STreehugger Robot 
10*7c3d14c8STreehugger Robot class Base {
11*7c3d14c8STreehugger Robot  public:
12*7c3d14c8STreehugger Robot   int *x_ptr;
Base(int * y_ptr)13*7c3d14c8STreehugger Robot   Base(int *y_ptr) {
14*7c3d14c8STreehugger Robot     // store value of subclass member
15*7c3d14c8STreehugger Robot     x_ptr = y_ptr;
16*7c3d14c8STreehugger Robot   }
17*7c3d14c8STreehugger Robot   virtual ~Base();
18*7c3d14c8STreehugger Robot };
19*7c3d14c8STreehugger Robot 
20*7c3d14c8STreehugger Robot class Derived : public Base {
21*7c3d14c8STreehugger Robot  public:
22*7c3d14c8STreehugger Robot   int y;
Derived()23*7c3d14c8STreehugger Robot   Derived():Base(&y) {
24*7c3d14c8STreehugger Robot     y = 10;
25*7c3d14c8STreehugger Robot   }
26*7c3d14c8STreehugger Robot   ~Derived();
27*7c3d14c8STreehugger Robot };
28*7c3d14c8STreehugger Robot 
~Base()29*7c3d14c8STreehugger Robot Base::~Base() {
30*7c3d14c8STreehugger Robot   // ok access its own member
31*7c3d14c8STreehugger Robot   assert(__msan_test_shadow(&this->x_ptr, sizeof(this->x_ptr)) == -1);
32*7c3d14c8STreehugger Robot   // bad access subclass member
33*7c3d14c8STreehugger Robot   assert(__msan_test_shadow(this->x_ptr, sizeof(*this->x_ptr)) != -1);
34*7c3d14c8STreehugger Robot }
35*7c3d14c8STreehugger Robot 
~Derived()36*7c3d14c8STreehugger Robot Derived::~Derived() {
37*7c3d14c8STreehugger Robot   // ok to access its own members
38*7c3d14c8STreehugger Robot   assert(__msan_test_shadow(&this->y, sizeof(this->y)) == -1);
39*7c3d14c8STreehugger Robot   // ok access base class members
40*7c3d14c8STreehugger Robot   assert(__msan_test_shadow(&this->x_ptr, sizeof(this->x_ptr)) == -1);
41*7c3d14c8STreehugger Robot }
42*7c3d14c8STreehugger Robot 
main()43*7c3d14c8STreehugger Robot int main() {
44*7c3d14c8STreehugger Robot   Derived *d = new Derived();
45*7c3d14c8STreehugger Robot   assert(__msan_test_shadow(&d->x_ptr, sizeof(d->x_ptr)) == -1);
46*7c3d14c8STreehugger Robot   d->~Derived();
47*7c3d14c8STreehugger Robot   assert(__msan_test_shadow(&d->x_ptr, sizeof(d->x_ptr)) != -1);
48*7c3d14c8STreehugger Robot   return 0;
49*7c3d14c8STreehugger Robot }
50