1 // Test that we do not poison the array cookie if the operator new is defined
2 // inside the class.
3 // RUN: %clangxx_asan  %s -o %t && %run %t
4 //
5 // XFAIL: arm
6 
7 // UNSUPPORTED: ios
8 
9 #include <new>
10 #include <stdlib.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <assert.h>
14 struct Foo {
operator newFoo15   void *operator new(size_t s) { return Allocate(s); }
operator new[]Foo16   void *operator new[] (size_t s) { return Allocate(s); }
17   ~Foo();
18   static void *allocated;
AllocateFoo19   static void *Allocate(size_t s) {
20     assert(!allocated);
21     return allocated = ::new char[s];
22   }
23 };
24 
~Foo()25 Foo::~Foo() {}
26 void *Foo::allocated;
27 
getFoo(size_t n)28 Foo *getFoo(size_t n) {
29   return new Foo[n];
30 }
31 
main()32 int main() {
33   Foo *foo = getFoo(10);
34   fprintf(stderr, "foo  : %p\n", foo);
35   fprintf(stderr, "alloc: %p\n", Foo::allocated);
36   assert(reinterpret_cast<uintptr_t>(foo) ==
37          reinterpret_cast<uintptr_t>(Foo::allocated) + sizeof(void*));
38   *reinterpret_cast<uintptr_t*>(Foo::allocated) = 42;
39   return 0;
40 }
41