forked from johnsonjh/duma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duma_hlp.h
242 lines (217 loc) · 7.74 KB
/
duma_hlp.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* DUMA - Red-Zone memory allocator.
* Copyright (C) 2002-2009 Hayati Ayguen <[email protected]>, Procitec GmbH
* License: GNU LGPL (GNU Lesser General Public License, see COPYING-GPL)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* FILE CONTENTS:
* internal implementation file
* contains helper functions for DUMA
*/
/* Function: reduceProtectedMemory
*
* delete reductionSizekB amount of memory, which has already
* been freed but got protected
* return != 0 when more memory reducable
*/
static int reduceProtectedMemory(size_t reductionSizekB) {
struct _DUMA_Slot *slot = _duma_g.allocList;
size_t count = _duma_s.slotCount;
size_t alreadyReducekB = 0;
#ifndef WIN32
/* Windows VirtualFree(,,MEM_RELEASE) can only free whole allocations. not
* parts */
size_t delSize, newSize;
/* 1- try reducing memory to just keep page(s) with userAddress */
for (; count > 0 && alreadyReducekB < reductionSizekB; --count, ++slot)
if (DUMAST_ALL_PROTECTED == slot->state) {
/* free memory above userAddr; keep userAddr protected */
newSize = (char *)slot->userAddress - (char *)slot->internalAddress;
newSize = (newSize + DUMA_PAGE_SIZE) & ~(DUMA_PAGE_SIZE - 1);
delSize = slot->internalSize - newSize;
Page_Delete((char *)slot->internalAddress + newSize, delSize);
alreadyReducekB += (delSize + 1023) >> 10;
slot->state = DUMAST_BEGIN_PROTECTED;
/* but keep the slot and userAddr */
slot->internalSize = newSize;
if (alreadyReducekB >= reductionSizekB) {
_duma_s.sumProtectedMem -= alreadyReducekB;
_duma_s.sumAllocatedMem -= alreadyReducekB;
return 1;
}
}
#endif
/* 2- deallocate all page(s) with userAddress, empty whole slot */
slot = _duma_g.allocList;
count = _duma_s.slotCount;
for (; count > 0 && alreadyReducekB < reductionSizekB; --count, ++slot)
if (DUMAST_BEGIN_PROTECTED == slot->state
#if defined(WIN32)
|| DUMAST_ALL_PROTECTED == slot->state
#endif
) {
/* free all the memory */
Page_Delete(slot->internalAddress, slot->internalSize);
alreadyReducekB += (slot->internalSize + 1023) >> 10;
/* free slot and userAddr */
slot->internalAddress = slot->userAddress = 0;
slot->internalSize = slot->userSize = 0;
slot->state = DUMAST_EMPTY;
slot->allocator = EFA_INT_ALLOC;
#ifndef DUMA_NO_LEAKDETECTION
slot->fileSource = DUMAFS_EMPTY;
slot->filename = 0;
slot->lineno = 0;
#endif
if (alreadyReducekB >= reductionSizekB) {
_duma_s.sumProtectedMem -= alreadyReducekB;
_duma_s.sumAllocatedMem -= alreadyReducekB;
return 1;
}
}
return 0;
}
/* Function: slotForUserAddress
*
* Find the slot structure for a user address.
*/
static struct _DUMA_Slot *slotForUserAddress(void *address) {
struct _DUMA_Slot *slot = _duma_g.allocList;
size_t count = _duma_s.slotCount;
for (; count > 0; --count, ++slot)
if (slot->userAddress == address)
return slot;
return 0;
}
/* Function: nearestSlotForUserAddress
*
* Find the nearest slot structure for a user address.
*/
static struct _DUMA_Slot *nearestSlotForUserAddress(void *userAddress) {
struct _DUMA_Slot *slot = _duma_g.allocList;
size_t count = _duma_s.slotCount;
for (; count > 0; --count, ++slot)
if ((char *)slot->internalAddress <= (char *)userAddress &&
(char *)userAddress <=
(char *)slot->internalAddress + slot->internalSize)
return slot;
return 0;
}
/* Function: _duma_init_slack
*
* Initialise the no mans land, for a given slot
*/
static void _duma_init_slack(struct _DUMA_Slot *slot) {
char *accBegAddr, *accEndAddr;
char *tmpBegAddr, *tmpEndAddr;
#ifdef DUMA_EXPLICIT_INIT
slot->slackfill = _duma_s.SLACKFILL;
#endif
/* nothing to do for zero userSize */
if (!slot->userSize)
return;
/* calculate accessible non-protectable address area */
if ((char *)slot->protAddress < (char *)slot->userAddress) {
/* DUMA_PROTECT_BELOW was 1 when allocating this piece of memory */
accBegAddr = (char *)slot->userAddress;
accEndAddr = (char *)slot->internalAddress + slot->internalSize;
} else {
/* DUMA_PROTECT_BELOW was 0 when allocating this piece of memory */
accBegAddr = (char *)slot->internalAddress;
accEndAddr = (char *)slot->protAddress;
}
tmpBegAddr = accBegAddr;
tmpEndAddr = (char *)slot->userAddress;
while (tmpBegAddr < tmpEndAddr)
*tmpBegAddr++ = (char)_duma_s.SLACKFILL;
tmpBegAddr = (char *)slot->userAddress + slot->userSize;
tmpEndAddr = accEndAddr;
while (tmpBegAddr < tmpEndAddr)
*tmpBegAddr++ = (char)_duma_s.SLACKFILL;
}
/* Function: _duma_check_slack
*
* Checks the integrity of no mans land, for a given slot
*/
static void _duma_check_slack(struct _DUMA_Slot *slot) {
char *accBegAddr, *accEndAddr;
char *tmpBegAddr, *tmpEndAddr;
char slackfill;
#ifdef DUMA_EXPLICIT_INIT
slackfill = (char)slot->slackfill;
#else
slackfill = (char)_duma_s.SLACKFILL;
#endif
/* nothing to do for zero userSize */
if (!slot->userSize)
return;
if (!slot->internalAddress)
return;
/* calculate accessible non-protectable address area */
if ((char *)slot->protAddress < (char *)slot->userAddress) {
/* DUMA_PROTECT_BELOW was 1 when allocating this piece of memory */
accBegAddr = (char *)slot->userAddress;
accEndAddr = (char *)slot->internalAddress + slot->internalSize;
} else {
/* DUMA_PROTECT_BELOW was 0 when allocating this piece of memory */
accBegAddr = (char *)slot->internalAddress;
accEndAddr = (char *)slot->protAddress;
}
tmpBegAddr = accBegAddr;
tmpEndAddr = (char *)slot->userAddress;
while (tmpBegAddr < tmpEndAddr) {
if ((char)slackfill != *tmpBegAddr++) {
#ifndef DUMA_NO_LEAKDETECTION
DUMA_Abort("ptr=%a: detected overwrite of ptrs no mans land below "
"userSpace, size=%d alloced from %s(%i)",
(DUMA_ADDR)slot->userAddress, (DUMA_SIZE)slot->userSize,
slot->filename, slot->lineno);
#else
DUMA_Abort(
"ptr=%a: detected overwrite of ptrs no mans land below userSpace",
(DUMA_ADDR)slot->userAddress);
#endif
}
}
tmpBegAddr = (char *)slot->userAddress + slot->userSize;
tmpEndAddr = accEndAddr;
while (tmpBegAddr < tmpEndAddr) {
if ((char)slackfill != *tmpBegAddr++) {
#ifndef DUMA_NO_LEAKDETECTION
DUMA_Abort("detected overwrite of no mans land above userSpace: ptr=%a, "
"size=%d\nalloced from %s(%i)",
(DUMA_ADDR)slot->userAddress, (DUMA_SIZE)slot->userSize,
slot->filename, slot->lineno);
#else
DUMA_Abort("detected overwrite of no mans land above userSpace: ptr=%a",
(DUMA_ADDR)slot->userAddress);
#endif
}
}
}
/* Function: _duma_check_all_slacks
*
* Checks the integrity of all no mans land
*/
static void _duma_check_all_slacks(void) {
struct _DUMA_Slot *slot = _duma_g.allocList;
size_t count = _duma_s.slotCount;
for (; count > 0; --count, ++slot) {
/* CHECK INTEGRITY OF NO MANS LAND */
if (DUMAST_IN_USE == slot->state && slot->userSize)
_duma_check_slack(slot);
}
}