]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/asan/TestCases/Posix/new_array_cookie_uaf_test.cc
Vendor import of compiler-rt trunk r256633:
[FreeBSD/FreeBSD.git] / test / asan / TestCases / Posix / new_array_cookie_uaf_test.cc
1 // REQUIRES: asan-64-bits
2 // RUN: %clangxx_asan -O3 %s -o %t
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
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 }