blob: f36da2b5438d7860284959012e408f3c3791029f [file] [log] [blame]
Stephen Hines6d186232014-11-26 17:56:19 -08001// REQUIRES: asan-64-bits
2// RUN: %clangxx_asan -O3 %s -o %t
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08003// RUN: %env_asan_opts=poison_array_cookie=1 not %run %t 2>&1 | FileCheck %s --check-prefix=COOKIE
4// RUN: %env_asan_opts=poison_array_cookie=0 not %run %t 2>&1 | FileCheck %s --check-prefix=NO_COOKIE
Stephen Hines6d186232014-11-26 17:56:19 -08005#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}