xref: /aosp_15_r20/external/compiler-rt/lib/msan/msan_new_delete.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- msan_new_delete.cc ------------------------------------------------===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot //                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is a part of MemorySanitizer.
11*7c3d14c8STreehugger Robot //
12*7c3d14c8STreehugger Robot // Interceptors for operators new and delete.
13*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
14*7c3d14c8STreehugger Robot 
15*7c3d14c8STreehugger Robot #include "msan.h"
16*7c3d14c8STreehugger Robot #include "interception/interception.h"
17*7c3d14c8STreehugger Robot 
18*7c3d14c8STreehugger Robot #if MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
19*7c3d14c8STreehugger Robot 
20*7c3d14c8STreehugger Robot #include <stddef.h>
21*7c3d14c8STreehugger Robot 
22*7c3d14c8STreehugger Robot using namespace __msan;  // NOLINT
23*7c3d14c8STreehugger Robot 
24*7c3d14c8STreehugger Robot // Fake std::nothrow_t to avoid including <new>.
25*7c3d14c8STreehugger Robot namespace std {
26*7c3d14c8STreehugger Robot   struct nothrow_t {};
27*7c3d14c8STreehugger Robot }  // namespace std
28*7c3d14c8STreehugger Robot 
29*7c3d14c8STreehugger Robot 
30*7c3d14c8STreehugger Robot #define OPERATOR_NEW_BODY \
31*7c3d14c8STreehugger Robot   GET_MALLOC_STACK_TRACE; \
32*7c3d14c8STreehugger Robot   return MsanReallocate(&stack, 0, size, sizeof(u64), false)
33*7c3d14c8STreehugger Robot 
34*7c3d14c8STreehugger Robot INTERCEPTOR_ATTRIBUTE
operator new(size_t size)35*7c3d14c8STreehugger Robot void *operator new(size_t size) { OPERATOR_NEW_BODY; }
36*7c3d14c8STreehugger Robot INTERCEPTOR_ATTRIBUTE
operator new[](size_t size)37*7c3d14c8STreehugger Robot void *operator new[](size_t size) { OPERATOR_NEW_BODY; }
38*7c3d14c8STreehugger Robot INTERCEPTOR_ATTRIBUTE
operator new(size_t size,std::nothrow_t const &)39*7c3d14c8STreehugger Robot void *operator new(size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
40*7c3d14c8STreehugger Robot INTERCEPTOR_ATTRIBUTE
operator new[](size_t size,std::nothrow_t const &)41*7c3d14c8STreehugger Robot void *operator new[](size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
42*7c3d14c8STreehugger Robot 
43*7c3d14c8STreehugger Robot #define OPERATOR_DELETE_BODY \
44*7c3d14c8STreehugger Robot   GET_MALLOC_STACK_TRACE; \
45*7c3d14c8STreehugger Robot   if (ptr) MsanDeallocate(&stack, ptr)
46*7c3d14c8STreehugger Robot 
47*7c3d14c8STreehugger Robot INTERCEPTOR_ATTRIBUTE
operator delete(void * ptr)48*7c3d14c8STreehugger Robot void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
49*7c3d14c8STreehugger Robot INTERCEPTOR_ATTRIBUTE
operator delete[](void * ptr)50*7c3d14c8STreehugger Robot void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
51*7c3d14c8STreehugger Robot INTERCEPTOR_ATTRIBUTE
operator delete(void * ptr,std::nothrow_t const &)52*7c3d14c8STreehugger Robot void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
53*7c3d14c8STreehugger Robot INTERCEPTOR_ATTRIBUTE
operator delete[](void * ptr,std::nothrow_t const &)54*7c3d14c8STreehugger Robot void operator delete[](void *ptr, std::nothrow_t const&) {
55*7c3d14c8STreehugger Robot   OPERATOR_DELETE_BODY;
56*7c3d14c8STreehugger Robot }
57*7c3d14c8STreehugger Robot 
58*7c3d14c8STreehugger Robot #endif // MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
59