168d75effSDimitry Andric //===-- wrappers_cpp.cpp ----------------------------------------*- C++ -*-===// 268d75effSDimitry Andric // 368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 668d75effSDimitry Andric // 768d75effSDimitry Andric //===----------------------------------------------------------------------===// 868d75effSDimitry Andric 968d75effSDimitry Andric #include "platform.h" 1068d75effSDimitry Andric 1168d75effSDimitry Andric // Skip this compilation unit if compiled as part of Bionic. 1268d75effSDimitry Andric #if !SCUDO_ANDROID || !_BIONIC 1368d75effSDimitry Andric 1468d75effSDimitry Andric #include "allocator_config.h" 15*5f757f3fSDimitry Andric #include "internal_defs.h" 16*5f757f3fSDimitry Andric #include "platform.h" 17*5f757f3fSDimitry Andric #include "scudo/interface.h" 1881ad6265SDimitry Andric #include "wrappers_c.h" 1968d75effSDimitry Andric 2068d75effSDimitry Andric #include <stdint.h> 2168d75effSDimitry Andric 2268d75effSDimitry Andric namespace std { 2368d75effSDimitry Andric struct nothrow_t {}; 2468d75effSDimitry Andric enum class align_val_t : size_t {}; 2568d75effSDimitry Andric } // namespace std 2668d75effSDimitry Andric 27*5f757f3fSDimitry Andric static void reportAllocation(void *ptr, size_t size) { 28*5f757f3fSDimitry Andric if (SCUDO_ENABLE_HOOKS) 29*5f757f3fSDimitry Andric if (__scudo_allocate_hook && ptr) 30*5f757f3fSDimitry Andric __scudo_allocate_hook(ptr, size); 31*5f757f3fSDimitry Andric } 32*5f757f3fSDimitry Andric static void reportDeallocation(void *ptr) { 33*5f757f3fSDimitry Andric if (SCUDO_ENABLE_HOOKS) 34*5f757f3fSDimitry Andric if (__scudo_deallocate_hook) 35*5f757f3fSDimitry Andric __scudo_deallocate_hook(ptr); 36*5f757f3fSDimitry Andric } 37*5f757f3fSDimitry Andric 3868d75effSDimitry Andric INTERFACE WEAK void *operator new(size_t size) { 39*5f757f3fSDimitry Andric void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New); 40*5f757f3fSDimitry Andric reportAllocation(Ptr, size); 41*5f757f3fSDimitry Andric return Ptr; 4268d75effSDimitry Andric } 4368d75effSDimitry Andric INTERFACE WEAK void *operator new[](size_t size) { 44*5f757f3fSDimitry Andric void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray); 45*5f757f3fSDimitry Andric reportAllocation(Ptr, size); 46*5f757f3fSDimitry Andric return Ptr; 4768d75effSDimitry Andric } 4868d75effSDimitry Andric INTERFACE WEAK void *operator new(size_t size, 4968d75effSDimitry Andric std::nothrow_t const &) NOEXCEPT { 50*5f757f3fSDimitry Andric void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New); 51*5f757f3fSDimitry Andric reportAllocation(Ptr, size); 52*5f757f3fSDimitry Andric return Ptr; 5368d75effSDimitry Andric } 5468d75effSDimitry Andric INTERFACE WEAK void *operator new[](size_t size, 5568d75effSDimitry Andric std::nothrow_t const &) NOEXCEPT { 56*5f757f3fSDimitry Andric void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray); 57*5f757f3fSDimitry Andric reportAllocation(Ptr, size); 58*5f757f3fSDimitry Andric return Ptr; 5968d75effSDimitry Andric } 6068d75effSDimitry Andric INTERFACE WEAK void *operator new(size_t size, std::align_val_t align) { 61*5f757f3fSDimitry Andric void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New, 6268d75effSDimitry Andric static_cast<scudo::uptr>(align)); 63*5f757f3fSDimitry Andric reportAllocation(Ptr, size); 64*5f757f3fSDimitry Andric return Ptr; 6568d75effSDimitry Andric } 6668d75effSDimitry Andric INTERFACE WEAK void *operator new[](size_t size, std::align_val_t align) { 67*5f757f3fSDimitry Andric void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray, 6868d75effSDimitry Andric static_cast<scudo::uptr>(align)); 69*5f757f3fSDimitry Andric reportAllocation(Ptr, size); 70*5f757f3fSDimitry Andric return Ptr; 7168d75effSDimitry Andric } 7268d75effSDimitry Andric INTERFACE WEAK void *operator new(size_t size, std::align_val_t align, 7368d75effSDimitry Andric std::nothrow_t const &) NOEXCEPT { 74*5f757f3fSDimitry Andric void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New, 7568d75effSDimitry Andric static_cast<scudo::uptr>(align)); 76*5f757f3fSDimitry Andric reportAllocation(Ptr, size); 77*5f757f3fSDimitry Andric return Ptr; 7868d75effSDimitry Andric } 7968d75effSDimitry Andric INTERFACE WEAK void *operator new[](size_t size, std::align_val_t align, 8068d75effSDimitry Andric std::nothrow_t const &) NOEXCEPT { 81*5f757f3fSDimitry Andric void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray, 8268d75effSDimitry Andric static_cast<scudo::uptr>(align)); 83*5f757f3fSDimitry Andric reportAllocation(Ptr, size); 84*5f757f3fSDimitry Andric return Ptr; 8568d75effSDimitry Andric } 8668d75effSDimitry Andric 8768d75effSDimitry Andric INTERFACE WEAK void operator delete(void *ptr) NOEXCEPT { 88*5f757f3fSDimitry Andric reportDeallocation(ptr); 895ffd83dbSDimitry Andric Allocator.deallocate(ptr, scudo::Chunk::Origin::New); 9068d75effSDimitry Andric } 9168d75effSDimitry Andric INTERFACE WEAK void operator delete[](void *ptr) NOEXCEPT { 92*5f757f3fSDimitry Andric reportDeallocation(ptr); 935ffd83dbSDimitry Andric Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray); 9468d75effSDimitry Andric } 9581ad6265SDimitry Andric INTERFACE WEAK void operator delete(void *ptr, 9681ad6265SDimitry Andric std::nothrow_t const &) NOEXCEPT { 97*5f757f3fSDimitry Andric reportDeallocation(ptr); 985ffd83dbSDimitry Andric Allocator.deallocate(ptr, scudo::Chunk::Origin::New); 9968d75effSDimitry Andric } 10068d75effSDimitry Andric INTERFACE WEAK void operator delete[](void *ptr, 10168d75effSDimitry Andric std::nothrow_t const &) NOEXCEPT { 102*5f757f3fSDimitry Andric reportDeallocation(ptr); 1035ffd83dbSDimitry Andric Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray); 10468d75effSDimitry Andric } 10568d75effSDimitry Andric INTERFACE WEAK void operator delete(void *ptr, size_t size) NOEXCEPT { 106*5f757f3fSDimitry Andric reportDeallocation(ptr); 1075ffd83dbSDimitry Andric Allocator.deallocate(ptr, scudo::Chunk::Origin::New, size); 10868d75effSDimitry Andric } 10968d75effSDimitry Andric INTERFACE WEAK void operator delete[](void *ptr, size_t size) NOEXCEPT { 110*5f757f3fSDimitry Andric reportDeallocation(ptr); 1115ffd83dbSDimitry Andric Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, size); 11268d75effSDimitry Andric } 11381ad6265SDimitry Andric INTERFACE WEAK void operator delete(void *ptr, 11481ad6265SDimitry Andric std::align_val_t align) NOEXCEPT { 115*5f757f3fSDimitry Andric reportDeallocation(ptr); 1165ffd83dbSDimitry Andric Allocator.deallocate(ptr, scudo::Chunk::Origin::New, 0, 11768d75effSDimitry Andric static_cast<scudo::uptr>(align)); 11868d75effSDimitry Andric } 11968d75effSDimitry Andric INTERFACE WEAK void operator delete[](void *ptr, 12068d75effSDimitry Andric std::align_val_t align) NOEXCEPT { 121*5f757f3fSDimitry Andric reportDeallocation(ptr); 1225ffd83dbSDimitry Andric Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, 0, 12368d75effSDimitry Andric static_cast<scudo::uptr>(align)); 12468d75effSDimitry Andric } 12568d75effSDimitry Andric INTERFACE WEAK void operator delete(void *ptr, std::align_val_t align, 12668d75effSDimitry Andric std::nothrow_t const &) NOEXCEPT { 127*5f757f3fSDimitry Andric reportDeallocation(ptr); 1285ffd83dbSDimitry Andric Allocator.deallocate(ptr, scudo::Chunk::Origin::New, 0, 12968d75effSDimitry Andric static_cast<scudo::uptr>(align)); 13068d75effSDimitry Andric } 13168d75effSDimitry Andric INTERFACE WEAK void operator delete[](void *ptr, std::align_val_t align, 13268d75effSDimitry Andric std::nothrow_t const &) NOEXCEPT { 133*5f757f3fSDimitry Andric reportDeallocation(ptr); 1345ffd83dbSDimitry Andric Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, 0, 13568d75effSDimitry Andric static_cast<scudo::uptr>(align)); 13668d75effSDimitry Andric } 13768d75effSDimitry Andric INTERFACE WEAK void operator delete(void *ptr, size_t size, 13868d75effSDimitry Andric std::align_val_t align) NOEXCEPT { 139*5f757f3fSDimitry Andric reportDeallocation(ptr); 1405ffd83dbSDimitry Andric Allocator.deallocate(ptr, scudo::Chunk::Origin::New, size, 14168d75effSDimitry Andric static_cast<scudo::uptr>(align)); 14268d75effSDimitry Andric } 14368d75effSDimitry Andric INTERFACE WEAK void operator delete[](void *ptr, size_t size, 14468d75effSDimitry Andric std::align_val_t align) NOEXCEPT { 145*5f757f3fSDimitry Andric reportDeallocation(ptr); 1465ffd83dbSDimitry Andric Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, size, 14768d75effSDimitry Andric static_cast<scudo::uptr>(align)); 14868d75effSDimitry Andric } 14968d75effSDimitry Andric 15068d75effSDimitry Andric #endif // !SCUDO_ANDROID || !_BIONIC 151