Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ASan] Allow new/delete replacement by making interceptors weak
ASan declares these functions as strongly-defined, which results in 'duplicate symbol' errors when trying to replace them in user code when linking the runtimes statically. Reviewed By: eugenis Differential Revision: https://reviews.llvm.org/D100220
- Loading branch information
Showing
2 changed files
with
29 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
compiler-rt/test/asan/TestCases/replaceable_new_delete.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Ensure that operator new/delete are still replaceable. | ||
|
||
// RUN: %clangxx %s -o %t -fsanitize=address -shared-libsan && not %run %t 2>&1 | FileCheck %s | ||
// RUN: %clangxx %s -o %t -fsanitize=address -static-libsan && not %run %t 2>&1 | FileCheck %s | ||
|
||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <new> | ||
|
||
void *operator new[](size_t size) { | ||
fprintf(stderr, "replaced new\n"); | ||
return malloc(size); | ||
} | ||
|
||
void operator delete[](void *ptr) noexcept { | ||
fprintf(stderr, "replaced delete\n"); | ||
return free(ptr); | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
// CHECK: replaced new | ||
char *x = new char[5]; | ||
// CHECK: replaced delete | ||
delete[] x; | ||
// CHECK: ERROR: AddressSanitizer | ||
*x = 13; | ||
return 0; | ||
} |