1// REQUIRES: asan-64-bits
2// RUN: %clangxx_asan -O3 %s -o %t
3// RUN: ASAN_OPTIONS=poison_array_cookie=1 not %run %t 2>&1  | FileCheck %s --check-prefix=COOKIE
4// RUN: ASAN_OPTIONS=poison_array_cookie=0 not %run %t 2>&1  | FileCheck %s --check-prefix=NO_COOKIE
5#include <stdio.h>
6#include <stdlib.h>
7#include <assert.h>
8int dtor_counter;
9struct C {
10  int x;
11  ~C() {
12    dtor_counter++;
13    fprintf(stderr, "DTOR %d\n", dtor_counter);
14  }
15};
16
17__attribute__((noinline)) void Delete(C *c) { delete[] c; }
18__attribute__((no_sanitize_address)) void Write42ToCookie(C *c) {
19  long *p = reinterpret_cast<long*>(c);
20  p[-1] = 42;
21}
22
23int main(int argc, char **argv) {
24  C *buffer = new C[argc];
25  delete [] buffer;
26  Write42ToCookie(buffer);
27  delete [] buffer;
28// COOKIE: DTOR 1
29// COOKIE-NOT: DTOR 2
30// COOKIE: AddressSanitizer: loaded array cookie from free-d memory
31// COOKIE: AddressSanitizer: attempting double-free
32// NO_COOKIE: DTOR 1
33// NO_COOKIE: DTOR 43
34// NO_COOKIE-NOT: DTOR 44
35// NO_COOKIE-NOT: AddressSanitizer: loaded array cookie from free-d memory
36// NO_COOKIE: AddressSanitizer: attempting double-free
37
38}
39