[compiler-rt] 0ae2ea8 - hwasan: Bring back operator {new,delete} interceptors on Android.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 10 16:05:44 PST 2020


Author: Peter Collingbourne
Date: 2020-11-10T16:05:24-08:00
New Revision: 0ae2ea8f83e3b79c587f0461b5cac98de1be220d

URL: https://github.com/llvm/llvm-project/commit/0ae2ea8f83e3b79c587f0461b5cac98de1be220d
DIFF: https://github.com/llvm/llvm-project/commit/0ae2ea8f83e3b79c587f0461b5cac98de1be220d.diff

LOG: hwasan: Bring back operator {new,delete} interceptors on Android.

It turns out that we can't remove the operator new and delete
interceptors on Android without breaking ABI, so bring them back
as forwards to the malloc and free functions.

Differential Revision: https://reviews.llvm.org/D91219

Added: 
    

Modified: 
    compiler-rt/lib/hwasan/hwasan_new_delete.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/hwasan/hwasan_new_delete.cpp b/compiler-rt/lib/hwasan/hwasan_new_delete.cpp
index 191c17e56a74..8d01d3944f2b 100644
--- a/compiler-rt/lib/hwasan/hwasan_new_delete.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_new_delete.cpp
@@ -16,9 +16,34 @@
 #include "sanitizer_common/sanitizer_allocator.h"
 #include "sanitizer_common/sanitizer_allocator_report.h"
 
+#include <stddef.h>
+#include <stdlib.h>
+
 #if HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE
 
-#include <stddef.h>
+// TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
+#define OPERATOR_NEW_BODY(nothrow) \
+  GET_MALLOC_STACK_TRACE; \
+  void *res = hwasan_malloc(size, &stack);\
+  if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
+  return res
+
+#define OPERATOR_DELETE_BODY \
+  GET_MALLOC_STACK_TRACE; \
+  if (ptr) hwasan_free(ptr, &stack)
+
+#elif defined(__ANDROID__)
+
+// We don't actually want to intercept operator new and delete on Android, but
+// since we previously released a runtime that intercepted these functions,
+// removing the interceptors would break ABI. Therefore we simply forward to
+// malloc and free.
+#define OPERATOR_NEW_BODY(nothrow) return malloc(size)
+#define OPERATOR_DELETE_BODY free(ptr)
+
+#endif
+
+#ifdef OPERATOR_NEW_BODY
 
 using namespace __hwasan;
 
@@ -28,12 +53,6 @@ namespace std {
 }  // namespace std
 
 
-// TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
-#define OPERATOR_NEW_BODY(nothrow) \
-  GET_MALLOC_STACK_TRACE; \
-  void *res = hwasan_malloc(size, &stack);\
-  if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
-  return res
 
 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
 void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
@@ -48,10 +67,6 @@ void *operator new[](size_t size, std::nothrow_t const&) {
   OPERATOR_NEW_BODY(true /*nothrow*/);
 }
 
-#define OPERATOR_DELETE_BODY \
-  GET_MALLOC_STACK_TRACE; \
-  if (ptr) hwasan_free(ptr, &stack)
-
 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
@@ -63,4 +78,4 @@ void operator delete[](void *ptr, std::nothrow_t const&) {
   OPERATOR_DELETE_BODY;
 }
 
-#endif // HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE
+#endif // OPERATOR_NEW_BODY


        


More information about the llvm-commits mailing list