llvm-project
124 строки · 4.0 Кб
1//===-- dfsan_new_delete.cpp ----------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a part of DataflowSanitizer.
10//
11// Interceptors for operators new and delete.
12//===----------------------------------------------------------------------===//
13
14#include <stddef.h>
15
16#include "dfsan.h"
17#include "interception/interception.h"
18#include "sanitizer_common/sanitizer_allocator.h"
19#include "sanitizer_common/sanitizer_allocator_report.h"
20
21using namespace __dfsan;
22
23// Fake std::nothrow_t and std::align_val_t to avoid including <new>.
24namespace std {
25struct nothrow_t {};
26enum class align_val_t : size_t {};
27} // namespace std
28
29// TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
30#define OPERATOR_NEW_BODY(nothrow) \
31void *res = dfsan_malloc(size); \
32if (!nothrow && UNLIKELY(!res)) { \
33BufferedStackTrace stack; \
34ReportOutOfMemory(size, &stack); \
35} \
36return res
37#define OPERATOR_NEW_BODY_ALIGN(nothrow) \
38void *res = dfsan_memalign((uptr)align, size); \
39if (!nothrow && UNLIKELY(!res)) { \
40BufferedStackTrace stack; \
41ReportOutOfMemory(size, &stack); \
42} \
43return res;
44
45INTERCEPTOR_ATTRIBUTE
46void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
47INTERCEPTOR_ATTRIBUTE
48void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
49INTERCEPTOR_ATTRIBUTE
50void *operator new(size_t size, std::nothrow_t const &) {
51OPERATOR_NEW_BODY(true /*nothrow*/);
52}
53INTERCEPTOR_ATTRIBUTE
54void *operator new[](size_t size, std::nothrow_t const &) {
55OPERATOR_NEW_BODY(true /*nothrow*/);
56}
57INTERCEPTOR_ATTRIBUTE
58void *operator new(size_t size, std::align_val_t align) {
59OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/);
60}
61INTERCEPTOR_ATTRIBUTE
62void *operator new[](size_t size, std::align_val_t align) {
63OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/);
64}
65INTERCEPTOR_ATTRIBUTE
66void *operator new(size_t size, std::align_val_t align,
67std::nothrow_t const &) {
68OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/);
69}
70INTERCEPTOR_ATTRIBUTE
71void *operator new[](size_t size, std::align_val_t align,
72std::nothrow_t const &) {
73OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/);
74}
75
76#define OPERATOR_DELETE_BODY \
77if (ptr) \
78dfsan_deallocate(ptr)
79
80INTERCEPTOR_ATTRIBUTE
81void operator delete(void *ptr)NOEXCEPT { OPERATOR_DELETE_BODY; }
82INTERCEPTOR_ATTRIBUTE
83void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
84INTERCEPTOR_ATTRIBUTE
85void operator delete(void *ptr, std::nothrow_t const &) {
86OPERATOR_DELETE_BODY;
87}
88INTERCEPTOR_ATTRIBUTE
89void operator delete[](void *ptr, std::nothrow_t const &) {
90OPERATOR_DELETE_BODY;
91}
92INTERCEPTOR_ATTRIBUTE
93void operator delete(void *ptr, size_t size)NOEXCEPT { OPERATOR_DELETE_BODY; }
94INTERCEPTOR_ATTRIBUTE
95void operator delete[](void *ptr, size_t size) NOEXCEPT {
96OPERATOR_DELETE_BODY;
97}
98INTERCEPTOR_ATTRIBUTE
99void operator delete(void *ptr, std::align_val_t align)NOEXCEPT {
100OPERATOR_DELETE_BODY;
101}
102INTERCEPTOR_ATTRIBUTE
103void operator delete[](void *ptr, std::align_val_t align) NOEXCEPT {
104OPERATOR_DELETE_BODY;
105}
106INTERCEPTOR_ATTRIBUTE
107void operator delete(void *ptr, std::align_val_t align,
108std::nothrow_t const &) {
109OPERATOR_DELETE_BODY;
110}
111INTERCEPTOR_ATTRIBUTE
112void operator delete[](void *ptr, std::align_val_t align,
113std::nothrow_t const &) {
114OPERATOR_DELETE_BODY;
115}
116INTERCEPTOR_ATTRIBUTE
117void operator delete(void *ptr, size_t size, std::align_val_t align)NOEXCEPT {
118OPERATOR_DELETE_BODY;
119}
120INTERCEPTOR_ATTRIBUTE
121void operator delete[](void *ptr, size_t size,
122std::align_val_t align) NOEXCEPT {
123OPERATOR_DELETE_BODY;
124}
125