16d46ebefSNico Weber //===-- wrappers_cpp.cpp ----------------------------------------*- C++ -*-===//
26d46ebefSNico Weber //
36d46ebefSNico Weber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
46d46ebefSNico Weber // See https://llvm.org/LICENSE.txt for license information.
56d46ebefSNico Weber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66d46ebefSNico Weber //
76d46ebefSNico Weber //===----------------------------------------------------------------------===//
86d46ebefSNico Weber
96d46ebefSNico Weber #include "platform.h"
106d46ebefSNico Weber
116d46ebefSNico Weber // Skip this compilation unit if compiled as part of Bionic.
126d46ebefSNico Weber #if !SCUDO_ANDROID || !_BIONIC
136d46ebefSNico Weber
146d46ebefSNico Weber #include "allocator_config.h"
15*88852964SChia-hung Duan #include "internal_defs.h"
16*88852964SChia-hung Duan #include "platform.h"
174f76810dSChia-hung Duan #include "scudo/interface.h"
186ba8c8abSDominic Chen #include "wrappers_c.h"
196d46ebefSNico Weber
206d46ebefSNico Weber #include <stdint.h>
216d46ebefSNico Weber
226d46ebefSNico Weber namespace std {
236d46ebefSNico Weber struct nothrow_t {};
246d46ebefSNico Weber enum class align_val_t : size_t {};
256d46ebefSNico Weber } // namespace std
266d46ebefSNico Weber
reportAllocation(void * ptr,size_t size)274f76810dSChia-hung Duan static void reportAllocation(void *ptr, size_t size) {
28*88852964SChia-hung Duan if (SCUDO_ENABLE_HOOKS)
294f76810dSChia-hung Duan if (__scudo_allocate_hook && ptr)
304f76810dSChia-hung Duan __scudo_allocate_hook(ptr, size);
314f76810dSChia-hung Duan }
reportDeallocation(void * ptr)324f76810dSChia-hung Duan static void reportDeallocation(void *ptr) {
33*88852964SChia-hung Duan if (SCUDO_ENABLE_HOOKS)
344f76810dSChia-hung Duan if (__scudo_deallocate_hook)
354f76810dSChia-hung Duan __scudo_deallocate_hook(ptr);
364f76810dSChia-hung Duan }
374f76810dSChia-hung Duan
operator new(size_t size)386d46ebefSNico Weber INTERFACE WEAK void *operator new(size_t size) {
394f76810dSChia-hung Duan void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New);
404f76810dSChia-hung Duan reportAllocation(Ptr, size);
414f76810dSChia-hung Duan return Ptr;
426d46ebefSNico Weber }
operator new[](size_t size)436d46ebefSNico Weber INTERFACE WEAK void *operator new[](size_t size) {
444f76810dSChia-hung Duan void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray);
454f76810dSChia-hung Duan reportAllocation(Ptr, size);
464f76810dSChia-hung Duan return Ptr;
476d46ebefSNico Weber }
operator new(size_t size,std::nothrow_t const &)486d46ebefSNico Weber INTERFACE WEAK void *operator new(size_t size,
496d46ebefSNico Weber std::nothrow_t const &) NOEXCEPT {
504f76810dSChia-hung Duan void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New);
514f76810dSChia-hung Duan reportAllocation(Ptr, size);
524f76810dSChia-hung Duan return Ptr;
536d46ebefSNico Weber }
operator new[](size_t size,std::nothrow_t const &)546d46ebefSNico Weber INTERFACE WEAK void *operator new[](size_t size,
556d46ebefSNico Weber std::nothrow_t const &) NOEXCEPT {
564f76810dSChia-hung Duan void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray);
574f76810dSChia-hung Duan reportAllocation(Ptr, size);
584f76810dSChia-hung Duan return Ptr;
596d46ebefSNico Weber }
operator new(size_t size,std::align_val_t align)606d46ebefSNico Weber INTERFACE WEAK void *operator new(size_t size, std::align_val_t align) {
614f76810dSChia-hung Duan void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New,
626d46ebefSNico Weber static_cast<scudo::uptr>(align));
634f76810dSChia-hung Duan reportAllocation(Ptr, size);
644f76810dSChia-hung Duan return Ptr;
656d46ebefSNico Weber }
operator new[](size_t size,std::align_val_t align)666d46ebefSNico Weber INTERFACE WEAK void *operator new[](size_t size, std::align_val_t align) {
674f76810dSChia-hung Duan void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray,
686d46ebefSNico Weber static_cast<scudo::uptr>(align));
694f76810dSChia-hung Duan reportAllocation(Ptr, size);
704f76810dSChia-hung Duan return Ptr;
716d46ebefSNico Weber }
operator new(size_t size,std::align_val_t align,std::nothrow_t const &)726d46ebefSNico Weber INTERFACE WEAK void *operator new(size_t size, std::align_val_t align,
736d46ebefSNico Weber std::nothrow_t const &) NOEXCEPT {
744f76810dSChia-hung Duan void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::New,
756d46ebefSNico Weber static_cast<scudo::uptr>(align));
764f76810dSChia-hung Duan reportAllocation(Ptr, size);
774f76810dSChia-hung Duan return Ptr;
786d46ebefSNico Weber }
operator new[](size_t size,std::align_val_t align,std::nothrow_t const &)796d46ebefSNico Weber INTERFACE WEAK void *operator new[](size_t size, std::align_val_t align,
806d46ebefSNico Weber std::nothrow_t const &) NOEXCEPT {
814f76810dSChia-hung Duan void *Ptr = Allocator.allocate(size, scudo::Chunk::Origin::NewArray,
826d46ebefSNico Weber static_cast<scudo::uptr>(align));
834f76810dSChia-hung Duan reportAllocation(Ptr, size);
844f76810dSChia-hung Duan return Ptr;
856d46ebefSNico Weber }
866d46ebefSNico Weber
operator delete(void * ptr)876d46ebefSNico Weber INTERFACE WEAK void operator delete(void *ptr) NOEXCEPT {
884f76810dSChia-hung Duan reportDeallocation(ptr);
89681773f2SPeter Collingbourne Allocator.deallocate(ptr, scudo::Chunk::Origin::New);
906d46ebefSNico Weber }
operator delete[](void * ptr)916d46ebefSNico Weber INTERFACE WEAK void operator delete[](void *ptr) NOEXCEPT {
924f76810dSChia-hung Duan reportDeallocation(ptr);
93681773f2SPeter Collingbourne Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray);
946d46ebefSNico Weber }
operator delete(void * ptr,std::nothrow_t const &)95e831ea69SMitch Phillips INTERFACE WEAK void operator delete(void *ptr,
96e831ea69SMitch Phillips std::nothrow_t const &) NOEXCEPT {
974f76810dSChia-hung Duan reportDeallocation(ptr);
98681773f2SPeter Collingbourne Allocator.deallocate(ptr, scudo::Chunk::Origin::New);
996d46ebefSNico Weber }
operator delete[](void * ptr,std::nothrow_t const &)1006d46ebefSNico Weber INTERFACE WEAK void operator delete[](void *ptr,
1016d46ebefSNico Weber std::nothrow_t const &) NOEXCEPT {
1024f76810dSChia-hung Duan reportDeallocation(ptr);
103681773f2SPeter Collingbourne Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray);
1046d46ebefSNico Weber }
operator delete(void * ptr,size_t size)1056d46ebefSNico Weber INTERFACE WEAK void operator delete(void *ptr, size_t size) NOEXCEPT {
1064f76810dSChia-hung Duan reportDeallocation(ptr);
107681773f2SPeter Collingbourne Allocator.deallocate(ptr, scudo::Chunk::Origin::New, size);
1086d46ebefSNico Weber }
operator delete[](void * ptr,size_t size)1096d46ebefSNico Weber INTERFACE WEAK void operator delete[](void *ptr, size_t size) NOEXCEPT {
1104f76810dSChia-hung Duan reportDeallocation(ptr);
111681773f2SPeter Collingbourne Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, size);
1126d46ebefSNico Weber }
operator delete(void * ptr,std::align_val_t align)113e831ea69SMitch Phillips INTERFACE WEAK void operator delete(void *ptr,
114e831ea69SMitch Phillips std::align_val_t align) NOEXCEPT {
1154f76810dSChia-hung Duan reportDeallocation(ptr);
116681773f2SPeter Collingbourne Allocator.deallocate(ptr, scudo::Chunk::Origin::New, 0,
1176d46ebefSNico Weber static_cast<scudo::uptr>(align));
1186d46ebefSNico Weber }
operator delete[](void * ptr,std::align_val_t align)1196d46ebefSNico Weber INTERFACE WEAK void operator delete[](void *ptr,
1206d46ebefSNico Weber std::align_val_t align) NOEXCEPT {
1214f76810dSChia-hung Duan reportDeallocation(ptr);
122681773f2SPeter Collingbourne Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, 0,
1236d46ebefSNico Weber static_cast<scudo::uptr>(align));
1246d46ebefSNico Weber }
operator delete(void * ptr,std::align_val_t align,std::nothrow_t const &)1256d46ebefSNico Weber INTERFACE WEAK void operator delete(void *ptr, std::align_val_t align,
1266d46ebefSNico Weber std::nothrow_t const &) NOEXCEPT {
1274f76810dSChia-hung Duan reportDeallocation(ptr);
128681773f2SPeter Collingbourne Allocator.deallocate(ptr, scudo::Chunk::Origin::New, 0,
1296d46ebefSNico Weber static_cast<scudo::uptr>(align));
1306d46ebefSNico Weber }
operator delete[](void * ptr,std::align_val_t align,std::nothrow_t const &)1316d46ebefSNico Weber INTERFACE WEAK void operator delete[](void *ptr, std::align_val_t align,
1326d46ebefSNico Weber std::nothrow_t const &) NOEXCEPT {
1334f76810dSChia-hung Duan reportDeallocation(ptr);
134681773f2SPeter Collingbourne Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, 0,
1356d46ebefSNico Weber static_cast<scudo::uptr>(align));
1366d46ebefSNico Weber }
operator delete(void * ptr,size_t size,std::align_val_t align)1376d46ebefSNico Weber INTERFACE WEAK void operator delete(void *ptr, size_t size,
1386d46ebefSNico Weber std::align_val_t align) NOEXCEPT {
1394f76810dSChia-hung Duan reportDeallocation(ptr);
140681773f2SPeter Collingbourne Allocator.deallocate(ptr, scudo::Chunk::Origin::New, size,
1416d46ebefSNico Weber static_cast<scudo::uptr>(align));
1426d46ebefSNico Weber }
operator delete[](void * ptr,size_t size,std::align_val_t align)1436d46ebefSNico Weber INTERFACE WEAK void operator delete[](void *ptr, size_t size,
1446d46ebefSNico Weber std::align_val_t align) NOEXCEPT {
1454f76810dSChia-hung Duan reportDeallocation(ptr);
146681773f2SPeter Collingbourne Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, size,
1476d46ebefSNico Weber static_cast<scudo::uptr>(align));
1486d46ebefSNico Weber }
1496d46ebefSNico Weber
1506d46ebefSNico Weber #endif // !SCUDO_ANDROID || !_BIONIC
151