Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 1 | // REQUIRES: asan-64-bits |
| 2 | // RUN: %clangxx_asan -O3 %s -o %t |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 3 | // 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <assert.h> |
| 8 | int dtor_counter; |
| 9 | struct 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 | |
| 23 | int 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 | } |