2018-12-11 13:43:13 +03:00
import struct
import sys
addresses = { }
2018-12-11 17:10:43 +03:00
sources = { }
last_touched = { }
2018-12-12 11:38:39 +03:00
def find_nearby_allocations ( addr , size ) :
results = [ ]
2018-12-11 17:10:43 +03:00
for key in addresses . keys ( ) :
2018-12-12 11:38:39 +03:00
if abs ( addr - key ) < size * 2 and addresses [ key ] :
results . append ( key )
results = sorted ( results )
for key in results :
if key == addr :
print ( " self: 0x %x (size %d , allocated by 0x %x ) " % ( key , addresses [ key ] , sources [ key ] ) )
else :
2018-12-11 17:10:43 +03:00
print ( " nearby: 0x %x (size %d , allocated by 0x %x ) " % ( key , addresses [ key ] , sources [ key ] ) )
2018-12-11 13:43:13 +03:00
2018-12-12 04:23:19 +03:00
count = 0
2018-12-11 13:43:13 +03:00
while 1 :
2018-12-11 17:10:43 +03:00
data = sys . stdin . read ( 17 )
2018-12-11 13:43:13 +03:00
t , = struct . unpack_from ( " c " , data , 0 )
addr , = struct . unpack_from ( " I " , data , 1 )
size , = struct . unpack_from ( " I " , data , 5 )
extra , = struct . unpack_from ( " I " , data , 9 )
2018-12-11 17:10:43 +03:00
fault , = struct . unpack_from ( " I " , data , 13 )
2018-12-11 13:43:13 +03:00
2018-12-12 04:23:19 +03:00
count + = 1
if count % 1000 == 0 :
print ( count )
2018-12-11 13:43:13 +03:00
if t == ' m ' :
addresses [ addr ] = size
2018-12-11 17:10:43 +03:00
sources [ addr ] = fault
last_touched [ addr ] = t
2018-12-11 13:43:13 +03:00
elif t == ' v ' :
addresses [ addr ] = size
2018-12-11 17:10:43 +03:00
sources [ addr ] = fault
last_touched [ addr ] = t
2018-12-11 13:43:13 +03:00
elif t == ' c ' :
addresses [ addr ] = ' c '
2018-12-11 17:10:43 +03:00
sources [ addr ] = 0
last_touched [ addr ] = t
2018-12-11 13:43:13 +03:00
elif t == ' r ' :
if addr not in addresses :
print ( " Bad realloc: 0x %x " % addr )
else :
addresses [ addr ] = None
addresses [ extra ] = size
2018-12-11 17:10:43 +03:00
sources [ extra ] = fault
last_touched [ addr ] = t
last_touched [ extra ] = t
2018-12-11 13:43:13 +03:00
elif t == ' f ' :
if addr not in addresses :
print ( " Bad free detected: 0x %x " % addr )
elif addresses [ addr ] is None :
2018-12-12 04:54:39 +03:00
print ( " Double free detected: 0x %x (allocated by 0x %x ) " % ( addr , sources [ addr ] ) )
2018-12-11 13:43:13 +03:00
elif addresses [ addr ] == ' c ' :
print ( " freeing something that was calloced... " )
addresses [ addr ] = None
elif extra != 0xDEADBEEF :
2018-12-11 17:10:43 +03:00
print ( " Large buffer has bad value: 0x %x (0x %x ) expected size is %d , supposed is %d " % ( addr , extra , addresses [ addr ] , size ) )
elif addresses [ addr ] != size :
print ( " Size on free is incorrect: 0x %x %d %d 0x %x allocated by 0x %x last touched by %c " % ( addr , addresses [ addr ] , size , fault , sources [ addr ] , last_touched [ addr ] ) )
2018-12-12 11:38:39 +03:00
find_nearby_allocations ( addr , addresses [ addr ] )
2018-12-11 13:43:13 +03:00
else :
addresses [ addr ] = None
2018-12-11 17:10:43 +03:00
elif t == ' h ' :
if addr not in addresses :
print ( " Spurious halt: 0x %x " % ( addr ) )
else :
print ( " Halting on suspected bug: 0x %x size was %d " % ( addr , addresses [ addr ] ) )
2018-12-11 13:43:13 +03:00
else :
2018-12-11 17:10:43 +03:00
print ( " Garbage data detected: %c 0x %x %d 0xx " % ( t , addr , size , extra ) )
2018-12-11 13:43:13 +03:00
break