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: target=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>
14struct Foo {
15 void *operator new(size_t s) { return Allocate(s); }
16 void *operator new[] (size_t s) { return Allocate(s); }
17 ~Foo();
18 static void *allocated;
19 static void *Allocate(size_t s) {
20 assert(!allocated);
21 return allocated = ::new char[s];
22 }
23};
24
25Foo::~Foo() {}
26void *Foo::allocated;
27
28Foo *getFoo(size_t n) {
29 return new Foo[n];
30}
31
32int main() {
33 Foo *foo = getFoo(n: 10);
34 fprintf(stderr, format: "foo : %p\n", foo);
35 fprintf(stderr, format: "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

source code of compiler-rt/test/asan/TestCases/Posix/new_array_cookie_with_new_from_class.cpp