-
Notifications
You must be signed in to change notification settings - Fork 1
/
heap_safe.h
218 lines (186 loc) · 7.55 KB
/
heap_safe.h
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
#ifndef HEAP_SAFE_H
#define HEAP_SAFE_H
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <string>
#include "voipmonitor_define.h"
#define HEAPSAFE_ALLOC_RESERVE 20
#define HEAPSAFE_SAFE_ALLOC_RESERVE 4
#define HEAPSAFE_BEGIN_MEMORY_CONTROL_BLOCK "BMB"
#define HEAPSAFE_FREED_MEMORY_CONTROL_BLOCK "FMB"
#define HEAPSAFE_END_MEMORY_CONTROL_BLOCK "EMB"
#define HEAPSAFE_COPY_BEGIN_MEMORY_CONTROL_BLOCK(stringInfo) { \
stringInfo[0] = HEAPSAFE_BEGIN_MEMORY_CONTROL_BLOCK[0]; \
stringInfo[1] = HEAPSAFE_BEGIN_MEMORY_CONTROL_BLOCK[1]; \
stringInfo[2] = HEAPSAFE_BEGIN_MEMORY_CONTROL_BLOCK[2]; }
#define HEAPSAFE_COPY_FREED_MEMORY_CONTROL_BLOCK(stringInfo) { \
stringInfo[0] = HEAPSAFE_FREED_MEMORY_CONTROL_BLOCK[0]; \
stringInfo[1] = HEAPSAFE_FREED_MEMORY_CONTROL_BLOCK[1]; \
stringInfo[2] = HEAPSAFE_FREED_MEMORY_CONTROL_BLOCK[2]; }
#define HEAPSAFE_COPY_END_MEMORY_CONTROL_BLOCK(stringInfo) { \
stringInfo[0] = HEAPSAFE_END_MEMORY_CONTROL_BLOCK[0]; \
stringInfo[1] = HEAPSAFE_END_MEMORY_CONTROL_BLOCK[1]; \
stringInfo[2] = HEAPSAFE_END_MEMORY_CONTROL_BLOCK[2]; }
#define HEAPSAFE_CMP_BEGIN_MEMORY_CONTROL_BLOCK(stringInfo) \
(stringInfo[0] == HEAPSAFE_BEGIN_MEMORY_CONTROL_BLOCK[0] && \
stringInfo[1] == HEAPSAFE_BEGIN_MEMORY_CONTROL_BLOCK[1] && \
stringInfo[2] == HEAPSAFE_BEGIN_MEMORY_CONTROL_BLOCK[2])
#define HEAPSAFE_CMP_FREED_MEMORY_CONTROL_BLOCK(stringInfo) \
(stringInfo[0] == HEAPSAFE_FREED_MEMORY_CONTROL_BLOCK[0] && \
stringInfo[1] == HEAPSAFE_FREED_MEMORY_CONTROL_BLOCK[1] && \
stringInfo[2] == HEAPSAFE_FREED_MEMORY_CONTROL_BLOCK[2])
#define HEAPSAFE_CMP_END_MEMORY_CONTROL_BLOCK(stringInfo) \
(stringInfo[0] == HEAPSAFE_END_MEMORY_CONTROL_BLOCK[0] && \
stringInfo[1] == HEAPSAFE_END_MEMORY_CONTROL_BLOCK[1] && \
stringInfo[2] == HEAPSAFE_END_MEMORY_CONTROL_BLOCK[2])
#define MCB_PLUS (HeapSafeCheck & _HeapSafePlus)
#define MCB_STACK (HeapSafeCheck & _HeapSafeStack)
#define SIZEOF_MCB (MCB_PLUS ? sizeof(sHeapSafeMemoryControlBlockPlus) : \
MCB_STACK ? sizeof(sHeapSafeMemoryControlBlockEx) : \
sizeof(sHeapSafeMemoryControlBlock))
enum eHeapSafeErrors {
_HeapSafeErrorNotEnoughMemory = 1,
_HeapSafeErrorBeginEnd = 2,
_HeapSafeErrorFreed = 4,
_HeapSafeErrorInAllocFce = 8,
_HeapSafeErrorAllocReserve = 16,
_HeapSafeErrorFillFF = 32,
_HeapSafeErrorInHeap = 64,
_HeapSafeSafeReserve = 128,
_HeapSafePlus = 256,
_HeapSafeStack = 512
};
struct sHeapSafeMemoryControlBlock {
char stringInfo[3];
u_int32_t length;
u_int32_t memory_type;
};
struct sHeapSafeMemoryControlBlockPlus : public sHeapSafeMemoryControlBlock {
void *block_addr;
char memory_type1[20];
u_int16_t memory_type2;
u_int16_t check_sum;
};
struct sHeapSafeMemoryControlBlockEx : public sHeapSafeMemoryControlBlock {
u_int32_t memory_type_other;
};
struct sMemoryStatQuickBlock {
u_int32_t alloc_number;
u_int32_t size;
};
void HeapSafeAllocError(int error);
void HeapSafeMemcpyError(const char *errorString, const char *file = NULL, unsigned int line = 0);
void HeapSafeMemsetError(const char *errorString, const char *file = NULL, unsigned int line = 0);
inline void *memcpy_heapsafe(void *destination, const void *destination_begin, const void *source, const void *source_begin, size_t length,
const char *file = NULL, unsigned int line = 0) {
#if HEAPSAFE
extern unsigned int HeapSafeCheck;
if(HeapSafeCheck & _HeapSafeErrorBeginEnd) {
bool error = false;
sHeapSafeMemoryControlBlock *destination_beginMemoryBlock;
u_int32_t destinationLength = 0;
if(destination_begin) {
destination_beginMemoryBlock = (sHeapSafeMemoryControlBlock*)((unsigned char*)destination_begin - SIZEOF_MCB);
if(HEAPSAFE_CMP_BEGIN_MEMORY_CONTROL_BLOCK(destination_beginMemoryBlock->stringInfo)) {
destinationLength = destination_beginMemoryBlock->length;
} else {
error = true;
HeapSafeMemcpyError("destination corrupted (bad begin memory block)", file, line);
}
}
sHeapSafeMemoryControlBlock *source_beginMemoryBlock;
u_int32_t sourceLength = 0;
if(source_begin) {
source_beginMemoryBlock = (sHeapSafeMemoryControlBlock*)((unsigned char*)source_begin - SIZEOF_MCB);
if(HEAPSAFE_CMP_BEGIN_MEMORY_CONTROL_BLOCK(source_beginMemoryBlock->stringInfo)) {
sourceLength = source_beginMemoryBlock->length;
} else {
error = true;
HeapSafeMemcpyError("source corrupted (bad begin memory block)", file, line);
}
}
if(!error) {
if(destination_begin) {
if((unsigned char*)destination < (unsigned char*)destination_begin) {
HeapSafeMemcpyError("negative offset of destination", file, line);
}
if((unsigned char*)destination - (unsigned char*)destination_begin + length > destinationLength) {
HeapSafeMemcpyError("write after destination length", file, line);
}
}
if(source_begin) {
if((unsigned char*)source < (unsigned char*)source_begin) {
HeapSafeMemcpyError("negative offset of source", file, line);
}
if((unsigned char*)source - (unsigned char*)source_begin + length > sourceLength) {
HeapSafeMemcpyError("write after source length", file, line);
}
}
}
}
#endif
return(memcpy(destination, source, length));
}
inline void *memcpy_heapsafe(void *destination, const void *source, size_t length,
const char *file = NULL, unsigned int line = 0) {
return(memcpy_heapsafe(destination, destination, source, source, length,
file, line));
}
inline void *memset_heapsafe(void *ptr, void *ptr_begin, int value, size_t length,
const char *file = NULL, unsigned int line = 0) {
#if HEAPSAFE
extern unsigned int HeapSafeCheck;
if(HeapSafeCheck & _HeapSafeErrorBeginEnd) {
bool error = false;
sHeapSafeMemoryControlBlock *ptr_beginMemoryBlock;
u_int32_t ptrLength = 0;
if(ptr_begin) {
ptr_beginMemoryBlock = (sHeapSafeMemoryControlBlock*)((unsigned char*)ptr_begin - SIZEOF_MCB);
if(HEAPSAFE_CMP_BEGIN_MEMORY_CONTROL_BLOCK(ptr_beginMemoryBlock->stringInfo)) {
ptrLength = ptr_beginMemoryBlock->length;
} else {
error = true;
HeapSafeMemsetError("ptr corrupted (bad begin memory block)", file, line);
}
}
if(!error) {
if(ptr_begin) {
if((unsigned char*)ptr < (unsigned char*)ptr_begin) {
HeapSafeMemsetError("negative offset of ptr", file, line);
}
if((unsigned char*)ptr - (unsigned char*)ptr_begin + length > ptrLength) {
HeapSafeMemsetError("write after ptr length", file, line);
}
}
}
}
#endif
return(memset(ptr, value, length));
}
inline void *memset_heapsafe(void *ptr, int value, size_t length,
const char *file = NULL, unsigned int line = 0) {
return(memset_heapsafe(ptr, ptr, value, length,
file, line));
}
std::string getMemoryStat(bool all = false);
std::string getMemoryStatQuick(bool all = false);
std::string addThousandSeparators(u_int64_t num);
void printMemoryStat(bool all = false);
void memoryStatInit();
#if HEAPSAFE
#define FILE_LINE(alloc_number) (__FILE__, __LINE__, alloc_number)
void * operator new(size_t sizeOfObject, const char *memory_type1, int memory_type2 = 0, int alloc_number = 0);
void * operator new[](size_t sizeOfObject, const char *memory_type1, int memory_type2 = 0, int alloc_number = 0);
void delete_object(void *pointerToObject);
void *realloc_object(void *pointerToObject, size_t sizeOfObject, const char *memory_type1, int memory_type2, int alloc_number);
#else
#define FILE_LINE(alloc_number)
#endif
struct sFileLine {
char file[1000];
unsigned line;
unsigned alloc_number;
};
void parse_heapsafeplus_coredump(const char *corefile, const char *outfile);
#endif //HEAP_SAFE_H