-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcheck.cpp
298 lines (265 loc) · 8.21 KB
/
check.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "addr2sym.hpp"
#include "alloc_action.hpp"
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <mutex>
#include <new>
namespace {
struct GlobalData {
std::map<void *, AllocAction> allocated;
std::mutex lock;
bool enable = false;
GlobalData() {
enable = true;
}
~GlobalData() {
enable = false;
for (auto &&[ptr, info]: allocated) {
printf("检测到内存泄漏 ptr = %p, size = %zd, caller = %s\n", ptr,
info.size, addr2sym(info.caller).c_str());
}
}
void on(AllocOp op, void *ptr, size_t size, size_t align, void *caller) {
if (!ptr) return;
// printf("%s(ptr=%p, size=%zd, align=%zd, caller=%p)\n",
// kAllocOpNames[(size_t)op], ptr, size, align, caller);
std::lock_guard<std::mutex> guard(lock);
if (kAllocOpIsAllocation[(size_t)op]) {
auto result = allocated.insert({ptr, AllocAction{op, 0, ptr, size, align, caller, 0}});
if (!result.second) {
printf("检测到内存多次分配同一个地址 ptr = %p, size = %zd, "
"caller = %s\n",
ptr, size, addr2sym(caller).c_str());
}
} else {
auto it = allocated.find(ptr);
if (it == allocated.end()) {
printf("检测到尝试释放不存在的内存 ptr = %p, size = %zd, "
"caller = %s\n",
ptr, size, addr2sym(caller).c_str());
return;
}
const AllocAction& action = it->second;
bool wrong_free_func = kAllocOpFreeFunction[(size_t)action.op] != op;
bool wrong_size = (size != kNone && action.size != size);
bool wrong_align = (align != kNone && action.align != align);
if (wrong_free_func || wrong_size || wrong_align) {
printf("检测到内存释放错误 ptr = %p, size = %zd, caller = %s\n",
ptr, action.size, addr2sym(caller).c_str());
if (wrong_free_func) {
printf("使用了错误的释放函数\n");
}
if (wrong_size) {
printf("指定了错误的大小 size = %zd, 正确大小 = %zd\n", size, action.size);
}
if (wrong_align) {
printf("指定了错误的对齐 align = %zd, 正确对齐 = %zd\n", align, action.align);
}
}
allocated.erase(it);
}
}
} global;
struct EnableGuard {
bool was_enable;
EnableGuard() {
was_enable = global.enable;
global.enable = false;
}
explicit operator bool() {
return was_enable;
}
~EnableGuard() {
global.enable = was_enable;
}
};
} // namespace
void operator delete(void *ptr) noexcept {
EnableGuard ena;
if (ena) {
global.on(AllocOp::Delete, ptr, kNone, kNone,
__builtin_return_address(0));
}
free(ptr);
}
void operator delete[](void *ptr) noexcept {
EnableGuard ena;
if (ena) {
global.on(AllocOp::DeleteArray, ptr, kNone, kNone,
__builtin_return_address(0));
}
free(ptr);
}
void operator delete(void *ptr, std::nothrow_t const &) noexcept {
EnableGuard ena;
if (ena) {
global.on(AllocOp::Delete, ptr, kNone, kNone,
__builtin_return_address(0));
}
free(ptr);
}
void operator delete[](void *ptr, std::nothrow_t const &) noexcept {
EnableGuard ena;
if (ena) {
global.on(AllocOp::DeleteArray, ptr, kNone, kNone,
__builtin_return_address(0));
}
free(ptr);
}
void *operator new(size_t size) noexcept(false) {
EnableGuard ena;
void *ptr = malloc(size);
if (ena) {
global.on(AllocOp::New, ptr, size, kNone, __builtin_return_address(0));
}
if (ptr == nullptr) {
throw std::bad_alloc();
}
return ptr;
}
void *operator new[](size_t size) noexcept(false) {
EnableGuard ena;
void *ptr = malloc(size);
if (ena) {
global.on(AllocOp::NewArray, ptr, size, kNone,
__builtin_return_address(0));
}
if (ptr == nullptr) {
throw std::bad_alloc();
}
return ptr;
}
void *operator new(size_t size, std::nothrow_t const &) noexcept {
EnableGuard ena;
void *ptr = malloc(size);
if (ena) {
global.on(AllocOp::New, ptr, size, kNone, __builtin_return_address(0));
}
return ptr;
}
void *operator new[](size_t size, std::nothrow_t const &) noexcept {
EnableGuard ena;
void *ptr = malloc(size);
if (ena) {
global.on(AllocOp::NewArray, ptr, size, kNone,
__builtin_return_address(0));
}
return ptr;
}
#if (__cplusplus >= 201402L || _MSC_VER >= 1916)
void operator delete(void *ptr, size_t size) noexcept {
EnableGuard ena;
if (ena) {
global.on(AllocOp::Delete, ptr, size, kNone,
__builtin_return_address(0));
}
free(ptr);
}
void operator delete[](void *ptr, size_t size) noexcept {
EnableGuard ena;
if (ena) {
global.on(AllocOp::DeleteArray, ptr, size, kNone,
__builtin_return_address(0));
}
free(ptr);
}
#endif
#if (__cplusplus > 201402L || defined(__cpp_aligned_new))
void operator delete(void *ptr, std::align_val_t align) noexcept {
EnableGuard ena;
if (ena) {
global.on(AllocOp::Delete, ptr, kNone, (size_t)align,
__builtin_return_address(0));
}
free(ptr);
}
void operator delete[](void *ptr, std::align_val_t align) noexcept {
EnableGuard ena;
if (ena) {
global.on(AllocOp::DeleteArray, ptr, kNone, (size_t)align,
__builtin_return_address(0));
}
free(ptr);
}
void operator delete(void *ptr, size_t size, std::align_val_t align) noexcept {
EnableGuard ena;
if (ena) {
global.on(AllocOp::Delete, ptr, size, (size_t)align,
__builtin_return_address(0));
}
free(ptr);
}
void operator delete[](void *ptr, size_t size,
std::align_val_t align) noexcept {
EnableGuard ena;
if (ena) {
global.on(AllocOp::DeleteArray, ptr, size, (size_t)align,
__builtin_return_address(0));
}
free(ptr);
}
void operator delete(void *ptr, std::align_val_t align,
std::nothrow_t const &) noexcept {
EnableGuard ena;
if (ena) {
global.on(AllocOp::Delete, ptr, kNone, (size_t)align,
__builtin_return_address(0));
}
free(ptr);
}
void operator delete[](void *ptr, std::align_val_t align,
std::nothrow_t const &) noexcept {
EnableGuard ena;
if (ena) {
global.on(AllocOp::DeleteArray, ptr, kNone, (size_t)align,
__builtin_return_address(0));
}
free(ptr);
}
void *operator new(size_t size, std::align_val_t align) noexcept(false) {
EnableGuard ena;
void *ptr = aligned_alloc((size_t)align, size);
if (ena) {
global.on(AllocOp::New, ptr, size, (size_t)align,
__builtin_return_address(0));
}
if (ptr == nullptr) {
throw std::bad_alloc();
}
return ptr;
}
void *operator new[](size_t size, std::align_val_t align) noexcept(false) {
EnableGuard ena;
void *ptr = aligned_alloc((size_t)align, size);
if (ena) {
global.on(AllocOp::NewArray, ptr, size, (size_t)align,
__builtin_return_address(0));
}
if (ptr == nullptr) {
throw std::bad_alloc();
}
return ptr;
}
void *operator new(size_t size, std::align_val_t align,
std::nothrow_t const &) noexcept {
EnableGuard ena;
void *ptr = aligned_alloc((size_t)align, size);
if (ena) {
global.on(AllocOp::New, ptr, size, (size_t)align,
__builtin_return_address(0));
}
return ptr;
}
void *operator new[](size_t size, std::align_val_t align,
std::nothrow_t const &) noexcept {
EnableGuard ena;
void *ptr = aligned_alloc((size_t)align, size);
if (ena) {
global.on(AllocOp::NewArray, ptr, size, (size_t)align,
__builtin_return_address(0));
}
return ptr;
}
#endif