/
shade.py
executable file
·1226 lines (1007 loc) · 39 KB
/
shade.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import copy
import math
import os
import re
import struct
def execute_output(command):
# create temporary file for the output
filename = os.getenv('HOME') + os.sep + 'gdb_output_' + str(os.getpid())
# set gdb logging
gdb.execute("set logging file " + filename)
gdb.execute("set logging overwrite on")
gdb.execute("set logging redirect on")
gdb.execute("set logging on")
# execute command
try:
gdb.execute(command)
except:
pass
# restore normal gdb behaviour
gdb.execute("set logging off")
gdb.execute("set logging redirect off")
# read output and close temporary file
outfile = open(filename, 'r')
output = outfile.read()
outfile.close()
# delete file
os.remove(filename)
# split lines
output = output.splitlines()
return output
def parse_disassembled_output(output, regex=''):
instructions = dict()
# parse output
for line in output:
# delete program counter mark
line = line.replace('=>', ' ')
# get only instruction lines
if line.startswith(' '):
field = re.compile('\s+').split(line)
# parse
if field[1].endswith(':'):
addr = int(field[1].replace(':',''), 16)
code = ' '.join(field[2:])
else:
addr = int(field[1], 16)
code = ' '.join(field[3:])
# apply regex
if regex != '':
if not re.search(regex, code):
continue
# add to instructions
instructions[addr] = code
return instructions
def ascii_table(tuples_list, header=True):
# find the bigger tuple in the list
max_len = max(len(t) for t in tuples_list)
# find out the max len of each column element
max_element_len = [0] * max_len
for i in range(0, max_len):
# str(t[i]) gets the wrong length of DMLStrings
# values = (str(t[i]) for t in tuples_list)
values = (str(t[i]) for t in tuples_list)
max_element_len[i] = max(len(v) for v in values)
list_body = tuples_list
out = ""
# table header
if header is True:
list_body = tuples_list[1:]
line_len = 0
header_tuple = tuples_list[0]
for i in range(0, len(header_tuple)):
if i == 0:
spacing = 0
else:
spacing = 4
align = (max_element_len[i] - len(str(header_tuple[i])))
out += " " * spacing + str(header_tuple[i]) + " " * align
out += "\n"
# print header seperator line
line_len = sum(l + 4 for l in max_element_len)
out += "-" * line_len
out += "\n"
# print the rest of the table
for t in list_body:
for i in range(0, len(t)):
if i == 0:
spacing = 0
else:
spacing = 4
# align = (max_element_len[i] - len(str(t[i])))
align = max_element_len[i] - len(str(t[i]))
out += " " * spacing
out += str(t[i])
out += " " * align
out += "\n"
return out
def readmem(address, length):
return gdb.selected_inferior().read_memory(address, length)
def read32(address):
return struct.unpack("I", readmem(address, 4))[0]
def rshift(val, n):
return (val % 0x100000000) >> n
NSMALLBINS = 32
NTREEBINS = 32
PINUSE_BIT = 0x1
CINUSE_BIT = 0x2
FLAG4_BIT = 0x4
FLAG_BITS = PINUSE_BIT | CINUSE_BIT | FLAG4_BIT
INUSE_BITS = PINUSE_BIT | CINUSE_BIT
SMALLBIN_SHIFT = 3
TREEBIN_SHIFT = 8
MIN_LARGE_SIZE = 1 << TREEBIN_SHIFT
MAX_SMALL_SIZE = MIN_LARGE_SIZE - 1
MALLOC_ALIGNMENT = 2 * 4
CHUNK_OVERHEAD = 4
CHUNK_ALIGN_MASK = MALLOC_ALIGNMENT - 1
MIN_CHUNK_SIZE = (4 * 4 + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK
MIN_REQUEST = MIN_CHUNK_SIZE - CHUNK_OVERHEAD - 1
MAX_REQUEST = ((-MIN_CHUNK_SIZE) << 2) & 0xffffffff
class Segment:
def __init__(self, addr):
self.addr = addr
self.parse()
def parse(self):
self.base, self.size, self.next, self.sflags = struct.unpack("IIII", readmem(self.addr,
4 * 4))
def __repr__(self):
return "0x%08x - 0x%08x [%08x]" % (self.base, self.base + self.size, self.sflags)
class ChunkRegistry:
def __init__(self):
self.chunks = {}
def get(self, addr, size=0, as_tree_chunk=False, do_parse=True):
if not addr in self.chunks:
if as_tree_chunk:
self.chunks[addr] = TreeBinChunk(self, addr, size)
else:
self.chunks[addr] = Chunk(self, addr, size)
if as_tree_chunk and not isinstance(self.chunks[addr], TreeBinChunk):
prev_chunk = self.chunks[addr]
self.chunks[addr] = TreeBinChunk(self, addr, prev_chunk.size |
prev_chunk.flags)
self.chunks[addr].parent = self.chunks[addr].left_child = self.chunks[addr].right_child = None
self.chunks[addr].from_dv = prev_chunk.from_dv
self.chunks[addr].from_top = prev_chunk.from_top
self.chunks[addr].from_larger_chunk = prev_chunk.from_larger_chunk
if do_parse:
self.chunks[addr].parse()
else:
self.chunks[addr].fd = self.chunks[addr].bk = addr
return self.chunks[addr]
def create_temp(self, addr, size, from_dv=False, from_top=False,
from_larger_chunk=False, new_dv=False):
assert(addr not in self.chunks)
chunk = Chunk(self, addr, size)
chunk.from_dv = from_dv
chunk.from_top = from_top
chunk.from_larger_chunk = from_larger_chunk
chunk.new_dv = new_dv
return chunk
class Chunk(object):
def __init__(self, registry, addr, size=0):
self.registry = registry
self.addr = addr
self.data_addr = addr + 8
self.size = size & ~FLAG_BITS
self.flags = size & FLAG_BITS
self.from_dv = False
self.from_top = False
self.from_larger_chunk = False
self.new_dv = False
def parse(self):
self.prev_size, size, self.fd, self.bk = struct.unpack("IIII", readmem(self.addr,
4 * 4))
self.flags = size & FLAG_BITS
self.size = size & ~FLAG_BITS
def next_chunk(self):
return chunk_registry.get(self.addr + self.size)
def is_free(self):
return (self.flags & CINUSE_BIT) == 0
def get_status(self):
status = ""
if self.is_free():
status = "free"
else:
status = "in use"
if self.from_dv:
status += " (from dv)"
elif self.from_top:
status += " (from top)"
elif self.from_larger_chunk:
status += " (from larger)"
return status
def is_mmaped(self):
return (self.flags & INUSE_BITS) == 0
def as_tree_chunk(self):
new = self.registry.get(self.addr, self.size, as_tree_chunk=True, do_parse=False)
new.parent = new.left_child = new.right_child = None
new.siblings = []
return new
class SmallBin:
def __init__(self, index, bin_header_addr, smallmap):
self.index = index
self.bin_header_addr = bin_header_addr
self.smallmap = smallmap
self.chunks = []
self.size = self.index << SMALLBIN_SHIFT
def parse(self):
# based on dlmalloc, unlink_first_small_chunk
b = self.bin_header_addr
p = chunk_registry.get(b).fd
while ((self.smallmap >> self.index) & 1) != 0:
current_chunk = chunk_registry.get(p)
f = current_chunk.fd
assert (p != b)
assert (p != f)
self.chunks.append(current_chunk)
if b == f:
break
p = f
def __deepcopy__(self, memodict):
new = SmallBin(self.index, self.bin_header_addr, self.smallmap)
new.chunks = self.chunks[:]
return new
class TreeBinChunk(Chunk):
def __init__(self, registry, addr, size=0):
super(TreeBinChunk, self).__init__(registry, addr, size)
def parse(self):
super(TreeBinChunk, self).parse()
self.left_child_addr, self.right_child_addr, self.parent_addr, self.bin_index = \
struct.unpack("IIII", readmem(self.addr + 4 * 4, 4 * 4))
def build_tree(self, registry, bin_addr=0, recursive=True):
self.siblings = [self]
current_address = self.bk
while current_address != self.addr:
chunk = registry.get(current_address)
self.siblings.append(chunk)
current_address = chunk.bk
self.left_child = self.right_child = self.parent = None
if self.left_child_addr != 0:
self.left_child = registry.get(self.left_child_addr,
as_tree_chunk=True)
self.left_child.parse()
if recursive:
self.left_child.build_tree(registry)
if self.right_child_addr != 0:
self.right_child = registry.get(self.right_child_addr,
as_tree_chunk=True)
self.right_child.parse()
if recursive:
self.right_child.build_tree(registry)
if self.parent_addr != 0 and self.parent_addr != bin_addr:
self.parent = registry.get(self.parent_addr,
as_tree_chunk=True)
self.parent.parse()
if recursive:
self.parent.build_tree(registry, recursive=False)
def leftmost_child(self):
if self.left_child:
return self.left_child
return self.right_child
def get_children(self):
children = []
# first chunks smaller than us
if self.left_child:
children.extend(self.left_child.get_children())
# our chunk
children.append(self)
# chunks of the same size
current_address = self.fd
while current_address != self.addr:
current_chunk = chunk_registry.get(current_address)
children.append(current_chunk)
current_address = current_chunk.fd
# lastly, chunks bigger than us
if self.right_child:
children.extend(self.right_child.get_children())
return children
class TreeBin:
def __init__(self, index, addr, root_addr):
self.index = index
self.addr = addr
self.root_addr = root_addr
self.min_size = (1 << ((self.index >> 1) + TREEBIN_SHIFT)) | \
((self.index & 1) << ((self.index >> 1) + TREEBIN_SHIFT - 1))
self.max_size = (1 << (((self.index + 1) >> 1) + TREEBIN_SHIFT)) | \
(((self.index + 1) & 1) << (((self.index + 1) >> 1) + TREEBIN_SHIFT - 1))
def parse(self):
if self.root_addr == 0x0:
self.root = None
self.chunks = []
return
self.root = chunk_registry.get(self.root_addr, as_tree_chunk=True)
self.root.parse()
self.root.build_tree(chunk_registry, bin_addr=self.addr)
#assert(self.root.parent.addr == self.addr)
self.chunks = self.root.get_children()
def __deepcopy__(self, memodict):
new = TreeBin(self.index, self.addr, self.root_addr)
new.chunk_registry = ChunkRegistry()
if new.root_addr != 0x0:
new.root = new.chunk_registry.get(new.root_addr, as_tree_chunk=True)
new.root.parse()
new.root.build_tree(new.chunk_registry, bin_addr=self.addr)
else:
new.root = None
return new
class MallocParams:
def __init__(self):
disassm = parse_disassembled_output(execute_output("disassemble dlmalloc,+16"))
offset = -1
self.addr = -1
for addr in sorted(disassm.keys()):
if offset != -1 and self.addr != -1:
break
if disassm[addr].startswith("ldr.w"):
m = re.search("#([0-9]+)", disassm[addr])
offset = m.group(1)
offset = read32(addr + 4 + int(offset))
if disassm[addr] == "add r3, pc":
self.addr = addr + 4 + offset
self.parse()
def parse(self):
self.magic, self.page_size, self.granularity, self.mmap_threshold, \
self.trim_threshold, self.default_mflags = \
struct.unpack("IIIIII", readmem(self.addr, 6 * 4))
class MallocState:
def __init__(self):
self.malloc_params = MallocParams()
# find the libc's data segment
segments = execute_output("info proc mappings")
start_addr = end_addr = -1
count_libc_segments = 0
want_next_segment = False
for segment in segments:
segment = segment.split()
if len(segment) < 4:
continue
if want_next_segment:
start_addr, end_addr = map(lambda y: int(y, 16), segment[:2])
break
elif len(segment) >= 5 and segment[4].endswith("libc.so"):
count_libc_segments += 1
if count_libc_segments == 3:
want_next_segment = True
# find the magic in the libc data segment
locations = execute_output("find 0x%08x, 0x%08x, (int)0x%08x" % (start_addr,
end_addr, self.malloc_params.magic))
for location in locations:
addr = int(location, 16)
if addr != self.malloc_params.addr:
break
self.addr = addr - (9 * 4)
self.parse()
def parse(self):
self.smallmap, self.treemap, self.dvsize, self.topsize, self.least_addr, self.dv, \
self.top, self.trim_check, self.release_checks, self.magic = \
struct.unpack("IIIIIIIIII", readmem(self.addr, 10 * 4))
offset = 10 * 4
self.smallbins = {}
for i in range(NSMALLBINS):
smallbin = SmallBin(i, self.addr + offset, self.smallmap)
smallbin.parse()
self.smallbins[i] = smallbin
offset += 8
offset += 8
self.treebins = {}
for i in range(NTREEBINS):
self.treebins[i] = TreeBin(i, self.addr + offset + i * 4, read32(self.addr + offset + i * 4))
self.treebins[i].parse()
offset += NTREEBINS * 4
self.footprint, self.max_footprint, self.footprint_limit, self.mflags = \
struct.unpack("IIII", readmem(self.addr + offset, 4 * 4))
offset += 4 * 4
# mutex
offset += 4
self.segments = []
addr = self.addr + offset
while addr != 0:
current_segment = Segment(addr)
self.segments.append(current_segment)
addr = current_segment.next
def segment_from_chunk(self, chunk):
for segment in self.segments:
if chunk.addr >= segment.base and chunk.addr < segment.base + segment.size:
return segment
return None
def dump_address(self, addr):
chunk = chunk_registry.get(addr - 8)
print("[dl] Info about 0x%08x" % addr)
print("[dl] chunk: 0x%08x" % chunk.addr)
print("[dl] size: 0x%08x" % chunk.size)
print("[dl] usable size: 0x%08x" % (chunk.size - 8))
if chunk.is_free():
print("[dl] status: free")
else:
print("[dl] status: in use")
segment = self.segment_from_chunk(chunk)
print("[dl] segment base: 0x%08x" % segment.base)
def dump_segment(self, addr):
for segment in self.segments:
if segment.base == addr:
break
if segment.base != addr:
print("[dl] passed address is not a segment base address")
return
table = [("data address", "size", "status")]
current_address = segment.base
while current_address < segment.base + segment.size:
current_chunk = chunk_registry.get(current_address)
if ((current_chunk.fd == segment.base and current_chunk.bk == segment.size) or
current_address + current_chunk.size >= segment.base + segment.size):
break
if current_chunk.is_free():
status = "free"
else:
status = "in use"
table.append((hex(current_chunk.data_addr), hex(current_chunk.size), status))
current_address += current_chunk.size
print(ascii_table(table))
def dump_segment_around(self, addr, count):
our_chunk = chunk_registry.get(addr - 8)
segment = self.segment_from_chunk(our_chunk)
chunks = []
current_address = segment.base
while current_address < segment.base + segment.size:
current_chunk = chunk_registry.get(current_address)
chunks.append(current_chunk)
if ((current_chunk.fd == segment.base and current_chunk.bk == segment.size) or
current_address + current_chunk.size >= segment.base + segment.size):
break
current_address += current_chunk.size
our_chunk_index = chunks.index(our_chunk)
chunks = chunks[our_chunk_index - count:our_chunk_index + count + 1]
table = [("data address", "size", "status")]
for current_chunk in chunks:
if current_chunk.is_free():
status = "free"
else:
status = "in use"
table.append((hex(current_chunk.data_addr), hex(current_chunk.size), status))
print(ascii_table(table))
def dump_segments_from(self, addr):
segments_to_dump = []
for segment in self.segments:
segments_to_dump.append(segment)
if segment.base == addr:
break
if segment.base != addr:
print("[dl] passed address is not a segment base address")
return
table = [("data address", "size", "status")]
for segment in reversed(segments_to_dump):
current_address = segment.base
while current_address < segment.base + segment.size:
current_chunk = chunk_registry.get(current_address)
if (current_chunk.fd == segment.base or
current_address + current_chunk.size >= segment.base + segment.size):
break
if current_chunk.is_free():
status = "free"
else:
status = "in use"
table.append((hex(current_chunk.data_addr), hex(current_chunk.size), status))
current_address += current_chunk.size
print(ascii_table(table))
def dump_bins(self):
print("[dl] small bins:")
table = [("index", "size", "num chunks")]
for index, smallbin in self.smallbins.items():
if smallbin != None:
table.append((index, hex(smallbin.size), len(smallbin.chunks)))
print(ascii_table(table))
print("[dl] tree bins:")
table = [("index", "min size","max size", "num chunks")]
for index, treebin in self.treebins.items():
if treebin != None:
table.append((index, hex(treebin.min_size), hex(treebin.max_size),
len(treebin.chunks)))
print(ascii_table(table))
def dump_bin(self, size):
if size < MAX_SMALL_SIZE:
index = size >> SMALLBIN_SHIFT
if self.smallbins[index] == None:
print("[dl] small bin with index %d is empty" % index)
return
print("[dl] dumping small bin with index %d:" % index)
table = [("data address", "size", "status")]
for chunk in self.smallbins[index].chunks:
if chunk.is_free():
status = "free"
else:
status = "in use"
table.append((hex(chunk.data_addr), hex(chunk.size), status))
print(ascii_table(table))
else:
table = [("data address", "size", "status")]
for index, treebin in self.treebins.items():
if treebin is None:
continue
if size >= treebin.min_size and size < treebin.max_size:
for chunk in treebin.chunks:
if chunk.is_free():
status = "free"
else:
status = "in use"
table.append((hex(chunk.data_addr), hex(chunk.size), status))
break
print(ascii_table(table))
def dump_free(self, size):
allocator = Allocator(self)
table = [("data address", "size", "status", "notes")]
while True:
chunk = allocator.allocate(size)
if chunk == None:
break
notes = ""
if chunk.addr == self.dv:
notes += "dv"
if chunk.addr == self.top:
notes += "top"
if chunk.new_dv:
notes += "new dv"
table.append((hex(chunk.data_addr), hex(chunk.size), chunk.get_status(), notes))
print(ascii_table(table))
return
class Allocator:
def __init__(self, malloc_state):
self.smallmap = malloc_state.smallmap
self.smallbins = copy.deepcopy(malloc_state.smallbins)
self.topsize = malloc_state.topsize
self.top_addr = malloc_state.top
self.treemap = malloc_state.treemap
self.treebins = copy.deepcopy(malloc_state.treebins)
dv_addr = malloc_state.dv
self.dvsize = malloc_state.dvsize
if self.dvsize != 0:
self.dvchunk = chunk_registry.get(dv_addr)
else:
self.dvchunk = None
def pad_request(self, req):
return (req + CHUNK_OVERHEAD + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK
def smallmap_is_marked(self, index):
return self.smallmap & (1 << index)
def mark_smallmap(self, index):
self.smallmap |= (1 << index)
def clear_smallmap(self, index):
self.smallmap &= ~(1 << index)
def treemap_is_marked(self, index):
return self.treemap & (1 << index)
def mark_treemap(self, index):
self.treemap |= (1 << index)
def clear_treemap(self, index):
self.treemap &= ~(1 << index)
def left_bits(self, x):
b = (x << 1)
return (b | -b)
def least_bit(self, x):
return (x & -x)
def compute_bit2idx(self, x):
# this is essentially finding the first set bit
return int(round(math.log(x, 2)))
def compute_tree_index(self, size):
X = rshift(size, TREEBIN_SHIFT)
if X == 0:
return 0
elif X > 0xffff:
return NTREEBINS - 1
else:
n = X
for i in range(31, 0, -1):
if n & (1 << i):
break
return (i << 1) + (size >> (i + (TREEBIN_SHIFT - 1)) & 1)
def leftshift_for_tree_index(self, index):
if index == NTREEBINS - 1:
return 0
return (32 - 1) - ((index >> 1) + TREEBIN_SHIFT - 1)
def replace_dv(self, chunk):
if self.dvchunk:
self.insert_small_chunk(self.dvchunk)
self.dvchunk = chunk
self.dvsize = self.dvchunk.size
def insert_small_chunk(self, chunk):
index = chunk.size >> SMALLBIN_SHIFT
smallbin = self.smallbins[index]
if not self.smallmap_is_marked(index):
self.mark_smallmap(index)
smallbin.chunks.insert(0, chunk)
def insert_chunk(self, chunk):
if chunk.size <= MAX_SMALL_SIZE:
self.insert_small_chunk(chunk)
else:
self.insert_large_chunk(chunk)
def insert_large_chunk(self, chunk):
chunk = chunk.as_tree_chunk()
index = self.compute_tree_index(chunk.size)
treebin = self.treebins[index]
chunk.bin_index = index
chunk.left_child = chunk.right_child = None
if not self.treemap_is_marked(index):
self.mark_treemap(index)
treebin.root = chunk
chunk.parent_addr = treebin.addr
chunk.siblings = [chunk]
else:
t = treebin.root
K = chunk.size << self.leftshift_for_tree_index(index)
while True:
if t.size != chunk.size:
C = None
if (K >> (32 - 1)) & 1 == 0:
C = t.left_child
else:
C = t.right_child
if C:
t = C
else:
if (K >> (32 - 1)) & 1 == 0:
t.left_child = chunk
else:
t.right_child = chunk
chunk.parent = t
chunk.siblings = [chunk]
break
else:
t.siblings.append(chunk)
chunk.parent = None
break
def unlink_large_chunk(self, node):
new_node = None
if len(node.siblings) >= 2:
node.siblings.pop(0)
new_node = node.siblings[0].as_tree_chunk()
new_node.bin_index = node.bin_index
new_node.siblings = node.siblings[:]
else:
if node.right_child:
new_node = node.right_child
elif node.left_child:
new_node = node.left_child
if new_node:
prev_new_node_is_right = False
parent = None
while True:
temp_node = None
if new_node.right_child:
parent = new_node
temp_node = new_node.right_child
prev_new_node_is_right = True
elif new_node.left_child:
parent = new_node
temp_node = new_node.left_child
prev_new_node_is_right = False
if temp_node:
new_node = temp_node
else:
break
if parent:
if prev_new_node_is_right:
parent.right_child = None
else:
parent.left_child = None
treebin = self.treebins[node.bin_index]
if treebin.root == node:
if not new_node:
self.clear_treemap(node.bin_index)
treebin.root = new_node
else:
if node.parent.left_child == node:
node.parent.left_child = new_node
else:
node.parent.right_child = new_node
if new_node:
new_node.parent = node.parent
if node.left_child:
if node.left_child != new_node:
new_node.left_child = node.left_child
new_node.left_child.parent = new_node
else:
new_node.left_child = None
if node.right_child:
if node.right_child != new_node:
new_node.right_child = node.right_child
new_node.right_child.parent = new_node
else:
new_node.right_child = None
def tmalloc_small(self, size):
# Get the smallest non-empty treebin
leastbit = self.least_bit(self.treemap)
i = self.compute_bit2idx(leastbit)
treebin = self.treebins[i]
rsize = treebin.root.size - size
v = t = treebin.root
while True:
t = t.leftmost_child()
if not t:
break
trem = t.size - size
if (trem < rsize):
rsize = trem
v = t
if v:
self.unlink_large_chunk(v)
if rsize >= MIN_CHUNK_SIZE:
remainder_chunk = chunk_registry.create_temp(v.addr + size, rsize
| PINUSE_BIT,
from_larger_chunk = True,
new_dv = True)
self.replace_dv(remainder_chunk)
return v
# Error occurred
return None
def tmalloc_large(self, size):
v = None
rsize = -size & 0xffffffff
index = self.compute_tree_index(size)
treebin = self.treebins[index]
t = treebin.root
v = None
if treebin.root != None:
# traverse tree for this bin looking for node with size == size
sizebits = size << self.leftshift_for_tree_index(index)
rst = None
while True:
trem = (t.size - size) & 0xffffffff
if trem < rsize:
v = t
rsize = trem
if (rsize == 0):
break
right = t.right_child
if (sizebits >> (32 - 1)) & 1 == 0:
t = t.left_child
else:
t = t.right_child
if right and right != t:
rst = right
if not t:
t = rst
break
sizebits <<= 1
if not t and not v:
# set t to root of next non-empty treebin
leftbits = self.left_bits(1 << index) & self.treemap
if leftbits != 0:
leastbit = self.least_bit(leftbits)
i = self.compute_bit2idx(leastbit)
t = self.treebins[i].root
while t:
# find smallest of tree or subtree
trem = t.size - size
if trem < rsize:
rsize = trem
v = t
t = t.leftmost_child()
# if dv is a better fit, return 0 so malloc will use it
if not v or rsize >= ((self.dvsize - size) & 0xffffffff):
return None
self.unlink_large_chunk(v)
if rsize >= MIN_CHUNK_SIZE:
remainder_chunk = chunk_registry.create_temp(v.addr + size, rsize
| PINUSE_BIT,
from_larger_chunk = True)
self.insert_chunk(remainder_chunk)
return v
def allocate(self, size):
if size < MIN_REQUEST:
size = MIN_CHUNK_SIZE
else:
size = self.pad_request(size)
if size < MAX_SMALL_SIZE:
index = size >> SMALLBIN_SHIFT
smallbits = self.smallmap >> index
# Remainderless fit to a smallbin
if (smallbits & 3) != 0:
index += ~smallbits & 1
chunk = smallbin.chunks.pop(0)
if len(smallbin.chunks) == 0:
self.clear_smallmap(index)
return chunk
elif size > self.dvsize:
# Use chunk in next nonempty smallbin
if smallbits != 0:
# get the next smallest bin which is non-empty:
leftbits = (smallbits << index) & self.left_bits(1 << index)
leastbit = self.least_bit(leftbits)
i = self.compute_bit2idx(leastbit)
smallbin = self.smallbins[i]
chunk = smallbin.chunks.pop(0)
if len(smallbin.chunks) == 0:
self.clear_smallmap(i)
rsize = (i << SMALLBIN_SHIFT) - size
remainder_chunk = chunk_registry.create_temp(chunk.addr + size, rsize
| PINUSE_BIT,
from_larger_chunk = True,
new_dv = True)
self.replace_dv(remainder_chunk)
return chunk
elif self.treemap != 0:
chunk = self.tmalloc_small(size)
if chunk:
return chunk
elif size >= MAX_REQUEST:
raise ValueError("Size too large")
else:
if self.treemap != 0:
chunk = self.tmalloc_large(size)
if chunk:
return chunk
if size <= self.dvsize:
rsize = self.dvchunk.size - size
if rsize >= MIN_CHUNK_SIZE:
# split dv
chunk = self.dvchunk
remainder_chunk = chunk_registry.create_temp(chunk.addr + size,
rsize | PINUSE_BIT,
from_dv = True)
self.dvchunk = remainder_chunk
self.dvsize = self.dvchunk.size
else:
# exhaust dv:
chunk = self.dvchunk
self.dvchunk = None
self.dvsize = 0
return chunk
elif size < self.topsize:
self.topsize -= size
chunk = chunk_registry.create_temp(self.top_addr, size |
PINUSE_BIT, from_top = True)
self.top_addr += size
return chunk