1 // Ensure that operator new/delete are still replaceable.
2
3 // FIXME: Weak symbols aren't supported on Windows, although some code in
4 // compiler-rt already exists to solve this problem. We should probably define
5 // the new/delete interceptors as "weak" using those workarounds as well.
6 // UNSUPPORTED: windows
7
8 // RUN: %clangxx %s -o %t -fsanitize=address -shared-libsan && not %run %t 2>&1 | FileCheck %s
9 // RUN: %clangxx %s -o %t -fsanitize=address -static-libsan && not %run %t 2>&1 | FileCheck %s
10
11 #include <cstdio>
12 #include <cstdlib>
13 #include <new>
14
operator new[](size_t size)15 void *operator new[](size_t size) {
16 fprintf(stderr, "replaced new\n");
17 return malloc(size);
18 }
19
operator delete[](void * ptr)20 void operator delete[](void *ptr) noexcept {
21 fprintf(stderr, "replaced delete\n");
22 return free(ptr);
23 }
24
main(int argc,char ** argv)25 int main(int argc, char **argv) {
26 // CHECK: replaced new
27 char *x = new char[5];
28 // CHECK: replaced delete
29 delete[] x;
30 // CHECK: ERROR: AddressSanitizer
31 *x = 13;
32 return 0;
33 }
34