45 lines · cpp
1// REQUIRES: asan-64-bits2// RUN: %clangxx_asan -O3 %s -o %t3// RUN: %env_asan_opts=poison_array_cookie=1 not %run %t 2>&1 | FileCheck %s --check-prefix=COOKIE4// RUN: %env_asan_opts=poison_array_cookie=0 not %run %t 2>&1 | FileCheck %s --check-prefix=NO_COOKIE5 6// UNSUPPORTED: ios7 8// Poisoning C++ array redzones is not implemented on arm9// XFAIL: target=arm{{.*}}10 11#include <stdio.h>12#include <stdlib.h>13#include <assert.h>14int dtor_counter;15struct C {16 int x;17 ~C() {18 dtor_counter++;19 fprintf(stderr, "DTOR %d\n", dtor_counter);20 }21};22 23__attribute__((noinline)) void Delete(C *c) { delete[] c; }24__attribute__((no_sanitize_address)) void Write42ToCookie(C *c) {25 long *p = reinterpret_cast<long*>(c);26 p[-1] = 42;27}28 29int main(int argc, char **argv) {30 C *buffer = new C[argc];31 delete [] buffer;32 Write42ToCookie(buffer);33 delete [] buffer;34// COOKIE: DTOR 135// COOKIE-NOT: DTOR 236// COOKIE: AddressSanitizer: loaded array cookie from free-d memory37// COOKIE: AddressSanitizer: attempting double-free38// NO_COOKIE: DTOR 139// NO_COOKIE: DTOR 4340// NO_COOKIE-NOT: DTOR 4441// NO_COOKIE-NOT: AddressSanitizer: loaded array cookie from free-d memory42// NO_COOKIE: AddressSanitizer: attempting double-free43 44}45