forked from voipmonitor/sniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap_safe.h
192 lines (163 loc) · 6.73 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
#ifndef HEAP_SAFE_H
#define HEAP_SAFE_H
#if ( defined( __FreeBSD__ ) || defined ( __NetBSD__ ) )
# ifndef FREEBSD
# define FREEBSD
# endif
#endif
#ifndef FREEBSD
#include <alloca.h>
#else
#include <sys/types.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include "tools_inline.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_STACK HeapSafeCheck & _HeapSafeStack
#define SIZEOF_MCB (MCB_STACK ? sizeof(sHeapSafeMemoryControlBlockEx) : sizeof(sHeapSafeMemoryControlBlock))
enum eHeapSafeErrors {
_HeapSafeErrorNotEnoughMemory = 1,
_HeapSafeErrorBeginEnd = 2,
_HeapSafeErrorFreed = 4,
_HeapSafeErrorInAllocFce = 8,
_HeapSafeErrorAllocReserve = 16,
_HeapSafeErrorFillFF = 32,
_HeapSafeErrorInHeap = 64,
_HeapSafeSafeReserve = 128,
_HeapSafeStack = 256
};
struct sHeapSafeMemoryControlBlock {
char stringInfo[3];
u_int32_t length;
u_int32_t memory_type;
};
struct sHeapSafeMemoryControlBlockEx : public sHeapSafeMemoryControlBlock {
u_int32_t memory_type_other;
};
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) {
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);
}
}
}
}
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) {
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);
}
}
}
}
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 addThousandSeparators(u_int64_t num);
void printMemoryStat(bool all = false);
void * operator new(size_t sizeOfObject, const char *memory_type1, int memory_type2 = 0);
void * operator new[](size_t sizeOfObject, const char *memory_type1, int memory_type2 = 0);
#define FILE_LINE (__FILE__, __LINE__)
#endif //HEAP_SAFE_H