Skip to content

Commit

Permalink
[ASan] Allow new/delete replacement by making interceptors weak
Browse files Browse the repository at this point in the history
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
hctim committed Apr 9, 2021
1 parent 50979de commit 7df30e7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler-rt/lib/asan/asan_new_delete.cpp
Expand Up @@ -45,7 +45,7 @@ COMMENT_EXPORT("??_V@YAXPAX@Z") // operator delete[]
#endif
#undef COMMENT_EXPORT
#else
#define CXX_OPERATOR_ATTRIBUTE INTERCEPTOR_ATTRIBUTE
#define CXX_OPERATOR_ATTRIBUTE INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
#endif

using namespace __asan;
Expand Down
28 changes: 28 additions & 0 deletions compiler-rt/test/asan/TestCases/replaceable_new_delete.cpp
@@ -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;
}

0 comments on commit 7df30e7

Please sign in to comment.