[asan] add a test for array cookie if the operator new is defined inside the class (the cookie should not be poisoned in such case); update the related comment in asan_poisoning.cc

llvm-svn: 218620
This commit is contained in:
Kostya Serebryany
2014-09-29 19:40:56 +00:00
parent 59e4e1b5fe
commit da9d495d03
2 changed files with 36 additions and 1 deletions

View File

@@ -252,7 +252,8 @@ uptr __asan_load_cxx_array_cookie(uptr *p) {
"expect a double-free report\n");
return 0;
}
// FIXME: apparently it can be something else; need to find a reproducer.
// The cookie may remain unpoisoned if e.g. it comes from a custom
// operator new defined inside a class.
return *p;
}

View File

@@ -0,0 +1,34 @@
// Test that we do not poison the array cookie if the operator new is defined
// inside the class.
// RUN: %clangxx_asan %s -o %t && %run %t
#include <new>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
struct Foo {
void *operator new(size_t s) { return Allocate(s); }
void *operator new[] (size_t s) { return Allocate(s); }
~Foo();
static void *allocated;
static void *Allocate(size_t s) {
assert(!allocated);
return allocated = ::new char[s];
}
};
Foo::~Foo() {}
void *Foo::allocated;
Foo *getFoo(size_t n) {
return new Foo[n];
}
int main() {
Foo *foo = getFoo(10);
fprintf(stderr, "foo : %p\n", foo);
fprintf(stderr, "alloc: %p\n", Foo::allocated);
assert(reinterpret_cast<uintptr_t>(foo) ==
reinterpret_cast<uintptr_t>(Foo::allocated) + sizeof(void*));
*reinterpret_cast<uintptr_t*>(Foo::allocated) = 42;
}