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 #include <new> 7 #include <stdlib.h> 8 #include <stdint.h> 9 #include <stdio.h> 10 #include <assert.h> 11 struct Foo { 12 void *operator new(size_t s) { return Allocate(s); } 13 void *operator new[] (size_t s) { return Allocate(s); } 14 ~Foo(); 15 static void *allocated; 16 static void *Allocate(size_t s) { 17 assert(!allocated); 18 return allocated = ::new char[s]; 19 } 20 }; 21 22 Foo::~Foo() {} 23 void *Foo::allocated; 24 25 Foo *getFoo(size_t n) { 26 return new Foo[n]; 27 } 28 29 int main() { 30 Foo *foo = getFoo(10); 31 fprintf(stderr, "foo : %p\n", foo); 32 fprintf(stderr, "alloc: %p\n", Foo::allocated); 33 assert(reinterpret_cast<uintptr_t>(foo) == 34 reinterpret_cast<uintptr_t>(Foo::allocated) + sizeof(void*)); 35 *reinterpret_cast<uintptr_t*>(Foo::allocated) = 42; 36 return 0; 37 } 38