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