1 //===-- hwasan_new_delete.cpp ---------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file is a part of HWAddressSanitizer. 10 // 11 // Interceptors for operators new and delete. 12 //===----------------------------------------------------------------------===// 13 14 #include "hwasan.h" 15 #include "interception/interception.h" 16 #include "sanitizer_common/sanitizer_allocator.h" 17 #include "sanitizer_common/sanitizer_allocator_report.h" 18 19 #include <stddef.h> 20 #include <stdlib.h> 21 22 #if HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE 23 24 // TODO(alekseys): throw std::bad_alloc instead of dying on OOM. 25 #define OPERATOR_NEW_BODY(nothrow) \ 26 GET_MALLOC_STACK_TRACE; \ 27 void *res = hwasan_malloc(size, &stack);\ 28 if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\ 29 return res 30 31 #define OPERATOR_DELETE_BODY \ 32 GET_MALLOC_STACK_TRACE; \ 33 if (ptr) hwasan_free(ptr, &stack) 34 35 #elif defined(__ANDROID__) 36 37 // We don't actually want to intercept operator new and delete on Android, but 38 // since we previously released a runtime that intercepted these functions, 39 // removing the interceptors would break ABI. Therefore we simply forward to 40 // malloc and free. 41 #define OPERATOR_NEW_BODY(nothrow) return malloc(size) 42 #define OPERATOR_DELETE_BODY free(ptr) 43 44 #endif 45 46 #ifdef OPERATOR_NEW_BODY 47 48 using namespace __hwasan; 49 50 // Fake std::nothrow_t to avoid including <new>. 51 namespace std { 52 struct nothrow_t {}; 53 } // namespace std 54 55 56 57 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE 58 void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); } 59 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE 60 void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); } 61 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE 62 void *operator new(size_t size, std::nothrow_t const&) { 63 OPERATOR_NEW_BODY(true /*nothrow*/); 64 } 65 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE 66 void *operator new[](size_t size, std::nothrow_t const&) { 67 OPERATOR_NEW_BODY(true /*nothrow*/); 68 } 69 70 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE 71 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; } 72 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE 73 void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; } 74 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE 75 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; } 76 INTERCEPTOR_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE 77 void operator delete[](void *ptr, std::nothrow_t const&) { 78 OPERATOR_DELETE_BODY; 79 } 80 81 #endif // OPERATOR_NEW_BODY 82