forked from cloudburst/libheap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppr.py
419 lines (333 loc) · 15.4 KB
/
ppr.py
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
################################################################################
# GDB PRETTY PRINTERS
################################################################################
from ptmalloc import *
from util import *
try:
import gdb
except ImportError:
print "Not running inside of GDB, exiting..."
exit()
class malloc_par_printer:
"pretty print the malloc parameters (mp_)"
def __init__(self, val):
self.val = val
def to_string(self):
return "%s%s%lx%s%lx%s%lx%s%x%s%x%s%x%s%x%s%x%s%lx%s%lx%s%lx%s%lx%s" % \
(c_title + "struct malloc_par {", \
c_none + "\ntrim_threshold = " + c_value + "0x", \
self.val['trim_threshold'], \
c_none + "\ntop_pad = " + c_value + "0x", \
self.val['top_pad'], \
c_none + "\nmmap_threshold = " + c_value + "0x", \
self.val['mmap_threshold'], \
c_none + "\nn_mmaps = " + c_value + "0x", \
self.val['n_mmaps'], \
c_none + "\nn_mmaps_max = " + c_value + "0x", \
self.val['n_mmaps_max'], \
c_none + "\nmax_n_mmaps = " + c_value + "0x", \
self.val['max_n_mmaps'], \
c_none + "\nno_dyn_threshold = " + c_value + "0x", \
self.val['no_dyn_threshold'], \
c_none + "\npagesize = " + c_value + "0x", \
self.val['pagesize'], \
c_none + "\nmmapped_mem = " + c_value + "0x", \
self.val['mmapped_mem'], \
c_none + "\nmax_mmapped_mem = " + c_value + "0x", \
self.val['max_mmapped_mem'], \
c_none + "\nmax_total_mem = " + c_value + "0x", \
self.val['max_total_mem'], \
c_none + "\nsbrk_base = " + c_value + "0x", \
self.val['sbrk_base'], \
c_none)
def display_string(self):
return "string"
################################################################################
class malloc_state_printer:
"pretty print a struct malloc_state (ar_ptr)"
def __init__(self, val):
self.val = val
def to_string(self):
return "%s%s%x%s%x%s%s%lx%s%lx%s%s%s%lx%s%lx%s%lx%s" % \
(c_title + "struct malloc_state {", \
c_none + "\nmutex = " + c_value + "0x", \
self.val['mutex'], \
c_none + "\nflags = " + c_value + "0x", \
self.val['flags'], \
c_none + "\nfastbinsY = " + c_value + "{...}", \
c_none + "\ntop = " + c_value + "0x", \
self.val['top'], \
c_none + "\nlast_remainder = " + c_value + "0x", \
self.val['last_remainder'], \
c_none + "\nbins = " + c_value + "{...}", \
c_none + "\nbinmap = " + c_value + "{...}", \
c_none + "\nnext = " + c_value + "0x", \
self.val['next'], \
c_none + "\nsystem_mem = " + c_value + "0x", \
self.val['system_mem'], \
c_none + "\nmax_system_mem = " + c_value + "0x", \
self.val['max_system_mem'], \
c_none)
def display_string(self):
return "string"
################################################################################
class malloc_chunk_printer:
"pretty print a struct malloc_chunk"
def __init__(self, val):
self.val = val
def to_string(self):
return "%s%s%x%s%x%s%lx%s%lx%s%lx%s%lx%s" % \
(c_title + "struct malloc_chunk {", \
c_none + "\nprev_size = " + c_value + "0x", \
self.val['prev_size'], \
c_none + "\nsize = " + c_value + "0x", \
self.val['size'], \
c_none + "\nfd = " + c_value + "0x", \
self.val['fd'], \
c_none + "\nbk = " + c_value + "0x", \
self.val['bk'], \
c_none + "\nfd_nextsize = " + c_value + "0x", \
self.val['fd_nextsize'], \
c_none + "\nbk_nextsize = " + c_value + "0x", \
self.val['bk_nextsize'], \
c_none)
def display_string(self):
return "string"
################################################################################
class heap_info_printer:
"pretty print a struct heap_info"
def __init__(self, val):
self.val = val
def to_string(self):
return "%s%s%lx%s%lx%s%lx%s%lx%s" % \
(c_title + "struct heap_info {", \
c_none + "\nar_ptr = " + c_value + "0x", \
self.val['ar_ptr'], \
c_none + "\nprev = " + c_value + "0x", \
self.val['prev'], \
c_none + "\nsize = " + c_value + "0x", \
self.val['size'], \
c_none + "\nmprotect_size = " + c_value + "0x", \
self.val['mprotect_size'], \
c_none)
def display_string(self):
return "string"
################################################################################
def pretty_print_heap_lookup(val):
"Look-up and return a pretty-printer that can print val."
# Get the type.
type = val.type
# If it points to a reference, get the reference.
if type.code == gdb.TYPE_CODE_REF:
type = type.target()
# Get the unqualified type, stripped of typedefs.
type = type.unqualified().strip_typedefs()
# Get the type name.
typename = type.tag
if typename == None:
return None
elif typename == "malloc_par":
return malloc_par_printer(val)
elif typename == "malloc_state":
return malloc_state_printer(val)
elif typename == "malloc_chunk":
return malloc_chunk_printer(val)
elif typename == "_heap_info":
return heap_info_printer(val)
else:
print typename
# Cannot find a pretty printer. Return None.
return None
################################################################################
def print_fastbins(inferior, fb_base, fb_num):
"walk and print the fast bins"
print_title("Fastbins")
for fb in xrange(0,NFASTBINS):
if fb_num != None:
fb = fb_num
offset = fb_base + fb*SIZE_SZ
try:
mem = inferior.read_memory(offset, SIZE_SZ)
if SIZE_SZ == 4:
fd = struct.unpack("<I", mem)[0]
elif SIZE_SZ == 8:
fd = struct.unpack("<Q", mem)[0]
except RuntimeError:
print c_error + " ERROR: Invalid fb addr 0x%lx" % offset + c_none
return
print "%s%s%d%s%s0x%08lx%s%s%s0x%08lx%s%s" % \
(c_header,"[ fb ",fb," ] ",c_none,offset,\
" -> ",c_value,"[ ",fd," ]",c_none),
if fd == 0: #fastbin is empty
print ""
else:
print "(%d)" % ((MIN_CHUNK_SIZE) +(MALLOC_ALIGNMENT)*fb)
chunk = malloc_chunk(fd, inuse=False)
while (chunk.fd != 0):
print "%s%26s0x%08lx%s%s%s" % \
(c_value,"[ ",chunk.fd," ] ",c_none,"(%d)" % \
((MIN_CHUNK_SIZE) + (MALLOC_ALIGNMENT)*fb))
chunk = malloc_chunk(chunk.fd, inuse=False)
if fb_num != None: #only print one fastbin
return
################################################################################
def print_smallbins(inferior, sb_base, sb_num):
"walk and print the small bins"
print("Smallbins")
for sb in xrange(2,NBINS+2,2):
if sb_num != None and sb_num!=0:
sb = sb_num*2
offset = sb_base + (sb-2)*SIZE_SZ
try:
mem = inferior.read_memory(offset, 2*SIZE_SZ)
if SIZE_SZ == 4:
fd,bk = struct.unpack("<II", mem)
elif SIZE_SZ == 8:
fd,bk = struct.unpack("<QQ", mem)
except RuntimeError:
print c_error + " ERROR: Invalid sb addr 0x%lx" % offset + c_none
return
print "%s%s%02d%s%s0x%08lx%s%s%s0x%08lx%s0x%08lx%s%s" % \
(c_header,"[ sb ",sb/2," ] ",c_none,offset, \
" -> ",c_value,"[ ", fd, " | ", bk, " ] ", \
c_none)
while (1):
if fd == (offset-2*SIZE_SZ):
break
chunk = malloc_chunk(fd, inuse=False)
print "%s%26s0x%08lx%s0x%08lx%s%s" % \
(c_value,"[ ",chunk.fd," | ",chunk.bk," ] ",c_none),
print "(%d)" % chunk.chunksize()
fd = chunk.fd
if sb_num != None: #only print one smallbin
return
################################################################################
def print_bins(inferior, fb_base, sb_base):
"walk and print the nonempty free bins, modified from jp"
print_title("Heap Dump")
for fb in xrange(0,NFASTBINS):
print_once = True
p = malloc_chunk(fb_base-(2*SIZE_SZ)+fb*SIZE_SZ, inuse=False)
while (p.fd != 0):
if print_once:
print_once = False
print c_header + " fast bin %d @ 0x%lx" % \
(fb,p.fd) + c_none
print " free chunk @ " + c_value + "0x%lx" % p.fd + c_none + \
" - size" + c_value,
p = malloc_chunk(p.fd, inuse=False)
print "0x%lx" % p.chunksize() + c_none
for i in xrange(1, NBINS):
print_once = True
b = sb_base + i*2*SIZE_SZ - 4*SIZE_SZ
p = malloc_chunk(first(malloc_chunk(b, inuse=False)), inuse=False)
while p.address != b:
if print_once:
print_once = False
if i==1:
try:
print c_header + " unsorted bin @ 0x%lx" % \
(b.cast(gdb.lookup_type("unsigned long")) \
+ 2*SIZE_SZ) + c_none
except:
print c_header + " unsorted bin @ 0x%lx" % \
(b + 2*SIZE_SZ) + c_none
else:
try:
print c_header + " small bin %d @ 0x%lx" % \
(i,b.cast(gdb.lookup_type("unsigned long")) \
+ 2*SIZE_SZ) + c_none
except:
print c_header + " small bin %d @ 0x%lx" % \
(i,b + 2*SIZE_SZ) + c_none
print c_none + " free_chunk @ " + c_value \
+ "0x%lx " % p.address + c_none \
+ "- size " + c_value + "0x%lx" % p.chunksize() + c_none
p = malloc_chunk(first(p), inuse=False)
################################################################################
def print_containing(ar_ptr,sbr_base,addr):
"print heap chunk containgig address"
for h in enum_heaps(ar_ptr):
m = chunk2mem(h)
s = h.chunksize()
if m <= addr and addr < h.address +s:
print "Address 0x%x belong to: "%addr
print "%s%14s%17s%15s%s" % (c_header, "ADDR", "SIZE", "STATUS", c_none)
print "%schunk %s0x%-14lx 0x%-10lx%s" % \
(c_none, c_value, h.address, s, c_none),
if ar_ptr.top == h.address:
print "(top)"
elif inuse(h):
print "(inuse)"
else:
print "(free)"
def print_heap_dump(ar_ptr):
print_title("Heap Dump")
print c_title + "Arena(s) found:" + c_none
try: #arena address obtained via read_var
print "\t arena @ 0x%x" % ar_ptr.address
except: #arena address obtained via -a
print "\t arena @ 0x%x" % ar_ptr.address
if ar_ptr.address != ar_ptr.next:
#we have more than one arena
curr_arena = malloc_state(ar_ptr.next)
while (ar_ptr.address != curr_arena.address):
print "\t arena @ 0x%x" % curr_arena.address
curr_arena = malloc_state(curr_arena.next)
if curr_arena.address == 0:
print c_error + \
"ERROR: No arenas could be correctly found." + c_none
break #breaking infinite loop
print ""
def print_flat_listing(ar_ptr, sbrk_base):
"print a flat listing of an arena, modified from jp and arena.c"
print_title("Heap Dump")
print "%s%14s%17s%15s%s" % (c_header, "ADDR", "SIZE", "STATUS", c_none)
print "sbrk_base " + c_value + "0x%lx" % sbrk_base
# p = malloc_chunk(sbrk_base, inuse=True, read_data=False)
# while(1):
for p in enum_heaps(ar_ptr):
print "%schunk %s0x%-14lx 0x%-10lx%s" % \
(c_none, c_value, p.address, p.chunksize(), c_none),
if p.address == ar_ptr.top:
print "(top)"
elif p.size == (0|PREV_INUSE):
print "(fence)"
elif inuse(p):
print "(inuse)"
else:
p = malloc_chunk(p.address, inuse=False)
print "(F) FD %s0x%lx%s BK %s0x%lx%s" % \
(c_value, p.fd, c_none,c_value,p.bk,c_none),
if ((p.fd == ar_ptr.last_remainder) \
and (p.bk == ar_ptr.last_remainder) \
and (ar_ptr.last_remainder != 0)):
print "(LR)"
elif ((p.fd == p.bk) & ~inuse(p)):
print "(LC)"
else:
print ""
#p = malloc_chunk(next_chunk(p), inuse=True, read_data=False)
print c_none + "sbrk_end " + c_value \
+ "0x%lx" % (sbrk_base + ar_ptr.system_mem) + c_none
################################################################################
def print_compact_listing(ar_ptr, sbrk_base):
"print a compact layout of the heap, modified from jp"
print_title("Heap Dump")
#p = malloc_chunk(sbrk_base, inuse=True, read_data=False)
#while(1):
for p in enum_heaps(ar_ptr):
if p.address == ar_ptr.top:
sys.stdout.write("|T|\n")
elif inuse(p):
sys.stdout.write("|A|")
else:
p = malloc_chunk(p.address, inuse=False)
if ((p.fd == ar_ptr.last_remainder) \
and (p.bk == ar_ptr.last_remainder) \
and (ar_ptr.last_remainder != 0)):
sys.stdout.write("|L|")
else:
sys.stdout.write("|%d|" % bin_index(p.size))
#p = malloc_chunk(next_chunk(p), inuse=True, read_data=False)
################################################################################