1 //===-- scudo_new_delete.cpp ------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// Interceptors for operators new and delete. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "scudo_allocator.h" 15 16 #include "interception/interception.h" 17 18 #include <cstddef> 19 20 using namespace __scudo; 21 22 #define CXX_OPERATOR_ATTRIBUTE INTERCEPTOR_ATTRIBUTE 23 24 // Fake std::nothrow_t to avoid including <new>. 25 namespace std { 26 struct nothrow_t {}; 27 } // namespace std 28 29 // TODO(alekseys): throw std::bad_alloc instead of dying on OOM. 30 CXX_OPERATOR_ATTRIBUTE 31 void *operator new(size_t size) { 32 void *res = scudoMalloc(size, FromNew); 33 if (UNLIKELY(!res)) DieOnFailure::OnOOM(); 34 return res; 35 } 36 CXX_OPERATOR_ATTRIBUTE 37 void *operator new[](size_t size) { 38 void *res = scudoMalloc(size, FromNewArray); 39 if (UNLIKELY(!res)) DieOnFailure::OnOOM(); 40 return res; 41 } 42 CXX_OPERATOR_ATTRIBUTE 43 void *operator new(size_t size, std::nothrow_t const&) { 44 return scudoMalloc(size, FromNew); 45 } 46 CXX_OPERATOR_ATTRIBUTE 47 void *operator new[](size_t size, std::nothrow_t const&) { 48 return scudoMalloc(size, FromNewArray); 49 } 50 51 CXX_OPERATOR_ATTRIBUTE 52 void operator delete(void *ptr) NOEXCEPT { 53 return scudoFree(ptr, FromNew); 54 } 55 CXX_OPERATOR_ATTRIBUTE 56 void operator delete[](void *ptr) NOEXCEPT { 57 return scudoFree(ptr, FromNewArray); 58 } 59 CXX_OPERATOR_ATTRIBUTE 60 void operator delete(void *ptr, std::nothrow_t const&) NOEXCEPT { 61 return scudoFree(ptr, FromNew); 62 } 63 CXX_OPERATOR_ATTRIBUTE 64 void operator delete[](void *ptr, std::nothrow_t const&) NOEXCEPT { 65 return scudoFree(ptr, FromNewArray); 66 } 67 CXX_OPERATOR_ATTRIBUTE 68 void operator delete(void *ptr, size_t size) NOEXCEPT { 69 scudoSizedFree(ptr, size, FromNew); 70 } 71 CXX_OPERATOR_ATTRIBUTE 72 void operator delete[](void *ptr, size_t size) NOEXCEPT { 73 scudoSizedFree(ptr, size, FromNewArray); 74 } 75