1 //===-- dfsan_new_delete.cpp ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of DataflowSanitizer.
11 // Interceptors for operators new and delete.
12 //===----------------------------------------------------------------------===//
17 #include "interception/interception.h"
18 #include "sanitizer_common/sanitizer_allocator.h"
19 #include "sanitizer_common/sanitizer_allocator_report.h"
21 using namespace __dfsan
;
23 // Fake std::nothrow_t and std::align_val_t to avoid including <new>.
26 enum class align_val_t
: size_t {};
29 // TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
30 #define OPERATOR_NEW_BODY(nothrow) \
31 void *res = dfsan_malloc(size); \
32 if (!nothrow && UNLIKELY(!res)) { \
33 BufferedStackTrace stack; \
34 ReportOutOfMemory(size, &stack); \
37 #define OPERATOR_NEW_BODY_ALIGN(nothrow) \
38 void *res = dfsan_memalign((uptr)align, size); \
39 if (!nothrow && UNLIKELY(!res)) { \
40 BufferedStackTrace stack; \
41 ReportOutOfMemory(size, &stack); \
46 void *operator new(size_t size
) { OPERATOR_NEW_BODY(false /*nothrow*/); }
48 void *operator new[](size_t size
) { OPERATOR_NEW_BODY(false /*nothrow*/); }
50 void *operator new(size_t size
, std::nothrow_t
const &) {
51 OPERATOR_NEW_BODY(true /*nothrow*/);
54 void *operator new[](size_t size
, std::nothrow_t
const &) {
55 OPERATOR_NEW_BODY(true /*nothrow*/);
58 void *operator new(size_t size
, std::align_val_t align
) {
59 OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/);
62 void *operator new[](size_t size
, std::align_val_t align
) {
63 OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/);
66 void *operator new(size_t size
, std::align_val_t align
,
67 std::nothrow_t
const &) {
68 OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/);
71 void *operator new[](size_t size
, std::align_val_t align
,
72 std::nothrow_t
const &) {
73 OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/);
76 #define OPERATOR_DELETE_BODY \
81 void operator delete(void *ptr
)NOEXCEPT
{ OPERATOR_DELETE_BODY
; }
83 void operator delete[](void *ptr
) NOEXCEPT
{ OPERATOR_DELETE_BODY
; }
85 void operator delete(void *ptr
, std::nothrow_t
const &) {
89 void operator delete[](void *ptr
, std::nothrow_t
const &) {
93 void operator delete(void *ptr
, size_t size
)NOEXCEPT
{ OPERATOR_DELETE_BODY
; }
95 void operator delete[](void *ptr
, size_t size
) NOEXCEPT
{
99 void operator delete(void *ptr
, std::align_val_t align
)NOEXCEPT
{
100 OPERATOR_DELETE_BODY
;
102 INTERCEPTOR_ATTRIBUTE
103 void operator delete[](void *ptr
, std::align_val_t align
) NOEXCEPT
{
104 OPERATOR_DELETE_BODY
;
106 INTERCEPTOR_ATTRIBUTE
107 void operator delete(void *ptr
, std::align_val_t align
,
108 std::nothrow_t
const &) {
109 OPERATOR_DELETE_BODY
;
111 INTERCEPTOR_ATTRIBUTE
112 void operator delete[](void *ptr
, std::align_val_t align
,
113 std::nothrow_t
const &) {
114 OPERATOR_DELETE_BODY
;
116 INTERCEPTOR_ATTRIBUTE
117 void operator delete(void *ptr
, size_t size
, std::align_val_t align
)NOEXCEPT
{
118 OPERATOR_DELETE_BODY
;
120 INTERCEPTOR_ATTRIBUTE
121 void operator delete[](void *ptr
, size_t size
,
122 std::align_val_t align
) NOEXCEPT
{
123 OPERATOR_DELETE_BODY
;