| 1 | //===-- copyprof_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 implements interceptors for C++ new and delete operators, tracking |
| 10 | /// dynamic object lifecycle events in the CopyProf runtime. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | // Routines to intercept C and C++ (de-)allocation functions. |
| 15 | |
| 16 | #include <stddef.h> |
| 17 | |
| 18 | #include "copyprof_allocator.h" |
| 19 | #include "interception/interception.h" |
| 20 | #include "sanitizer_common/sanitizer_allocator.h" |
| 21 | #include "sanitizer_common/sanitizer_allocator_report.h" |
| 22 | #include "sanitizer_common/sanitizer_common.h" |
| 23 | #include "sanitizer_common/sanitizer_flags.h" |
| 24 | #include "sanitizer_common/sanitizer_internal_defs.h" |
| 25 | #include "sanitizer_common/sanitizer_stacktrace.h" |
| 26 | |
| 27 | // Fake std::nothrow_t and std::align_val_t to avoid including <new>. |
| 28 | namespace std { |
| 29 | struct nothrow_t {}; |
| 30 | enum class align_val_t : size_t {}; |
| 31 | } // namespace std |
| 32 | |
| 33 | using namespace __copyprof; |
| 34 | |
| 35 | ALWAYS_INLINE |
| 36 | void* OperatorNew(size_t size, std::align_val_t align, bool no_throw) { |
| 37 | void* result = CopyProfAllocate(size, align: static_cast<uptr>(align)); |
| 38 | if (!no_throw && UNLIKELY(!result)) { |
| 39 | UNINITIALIZED BufferedStackTrace stack; |
| 40 | stack.Unwind(pc: StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), |
| 41 | /*context=*/nullptr, request_fast: common_flags()->fast_unwind_on_fatal); |
| 42 | ReportOutOfMemory(requested_size: size, stack: &stack); |
| 43 | } |
| 44 | return result; |
| 45 | } |
| 46 | |
| 47 | ALWAYS_INLINE |
| 48 | void OperatorDelete(void* ptr, size_t size, std::align_val_t align) { |
| 49 | CopyProfDeallocate(ptr, size, align: static_cast<uptr>(align)); |
| 50 | } |
| 51 | |
| 52 | INTERCEPTOR_ATTRIBUTE |
| 53 | void* operator new(size_t size) { |
| 54 | return OperatorNew(size, align: std::align_val_t{kMinAlignment}, |
| 55 | /*no_throw=*/false); |
| 56 | } |
| 57 | INTERCEPTOR_ATTRIBUTE |
| 58 | void* operator new(size_t size, std::align_val_t align) { |
| 59 | return OperatorNew(size, align, /*no_throw=*/false); |
| 60 | } |
| 61 | INTERCEPTOR_ATTRIBUTE |
| 62 | void* operator new(size_t size, std::nothrow_t const&) { |
| 63 | return OperatorNew(size, align: std::align_val_t{kMinAlignment}, |
| 64 | /*no_throw=*/true); |
| 65 | } |
| 66 | INTERCEPTOR_ATTRIBUTE |
| 67 | void* operator new(size_t size, std::align_val_t align, std::nothrow_t const&) { |
| 68 | return OperatorNew(size, align, /*no_throw=*/true); |
| 69 | } |
| 70 | INTERCEPTOR_ATTRIBUTE |
| 71 | void* operator new[](size_t size) { |
| 72 | return OperatorNew(size, align: std::align_val_t{kMinAlignment}, |
| 73 | /*no_throw=*/false); |
| 74 | } |
| 75 | INTERCEPTOR_ATTRIBUTE |
| 76 | void* operator new[](size_t size, std::align_val_t align) { |
| 77 | return OperatorNew(size, align, /*no_throw=*/false); |
| 78 | } |
| 79 | INTERCEPTOR_ATTRIBUTE |
| 80 | void* operator new[](size_t size, std::nothrow_t const&) { |
| 81 | return OperatorNew(size, align: std::align_val_t{kMinAlignment}, |
| 82 | /*no_throw=*/true); |
| 83 | } |
| 84 | INTERCEPTOR_ATTRIBUTE |
| 85 | void* operator new[](size_t size, std::align_val_t align, |
| 86 | std::nothrow_t const&) { |
| 87 | return OperatorNew(size, align, /*no_throw=*/true); |
| 88 | } |
| 89 | |
| 90 | INTERCEPTOR_ATTRIBUTE |
| 91 | void operator delete(void* ptr) NOEXCEPT { |
| 92 | return OperatorDelete(ptr, /*size=*/0, align: std::align_val_t{0}); |
| 93 | } |
| 94 | INTERCEPTOR_ATTRIBUTE |
| 95 | void operator delete(void* ptr, size_t size) NOEXCEPT { |
| 96 | return OperatorDelete(ptr, size, align: std::align_val_t{0}); |
| 97 | } |
| 98 | INTERCEPTOR_ATTRIBUTE |
| 99 | void operator delete(void* ptr, std::align_val_t align) NOEXCEPT { |
| 100 | return OperatorDelete(ptr, /*size=*/0, align); |
| 101 | } |
| 102 | INTERCEPTOR_ATTRIBUTE |
| 103 | void operator delete(void* ptr, std::nothrow_t const&) { |
| 104 | return OperatorDelete(ptr, /*size=*/0, align: std::align_val_t{0}); |
| 105 | } |
| 106 | INTERCEPTOR_ATTRIBUTE |
| 107 | void operator delete(void* ptr, std::align_val_t align, std::nothrow_t const&) { |
| 108 | return OperatorDelete(ptr, /*size=*/0, align); |
| 109 | } |
| 110 | INTERCEPTOR_ATTRIBUTE |
| 111 | void operator delete(void* ptr, size_t size, std::align_val_t align) NOEXCEPT { |
| 112 | return OperatorDelete(ptr, size, align); |
| 113 | } |
| 114 | INTERCEPTOR_ATTRIBUTE |
| 115 | void operator delete[](void* ptr) NOEXCEPT { |
| 116 | return OperatorDelete(ptr, /*size=*/0, align: std::align_val_t{0}); |
| 117 | } |
| 118 | INTERCEPTOR_ATTRIBUTE |
| 119 | void operator delete[](void* ptr, size_t size) NOEXCEPT { |
| 120 | return OperatorDelete(ptr, size, align: std::align_val_t{0}); |
| 121 | } |
| 122 | INTERCEPTOR_ATTRIBUTE |
| 123 | void operator delete[](void* ptr, std::align_val_t align) NOEXCEPT { |
| 124 | return OperatorDelete(ptr, /*size=*/0, align); |
| 125 | } |
| 126 | INTERCEPTOR_ATTRIBUTE |
| 127 | void operator delete[](void* ptr, std::nothrow_t const&) { |
| 128 | return OperatorDelete(ptr, /*size=*/0, align: std::align_val_t{0}); |
| 129 | } |
| 130 | INTERCEPTOR_ATTRIBUTE |
| 131 | void operator delete[](void* ptr, std::align_val_t align, |
| 132 | std::nothrow_t const&) { |
| 133 | return OperatorDelete(ptr, /*size=*/0, align); |
| 134 | } |
| 135 | INTERCEPTOR_ATTRIBUTE |
| 136 | void operator delete[](void* ptr, size_t size, |
| 137 | std::align_val_t align) NOEXCEPT { |
| 138 | return OperatorDelete(ptr, size, align); |
| 139 | } |
| 140 | |