blob: 0683e391cf23259dfaf24b97fca1b3f5faca2bf4 [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//
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08005// XFAIL: arm
Stephen Hines6d186232014-11-26 17:56:19 -08006#include <new>
7#include <stdlib.h>
8#include <stdint.h>
9#include <stdio.h>
10#include <assert.h>
11struct 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
22Foo::~Foo() {}
23void *Foo::allocated;
24
25Foo *getFoo(size_t n) {
26 return new Foo[n];
27}
28
29int 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}