process_log.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import struct
  2. import sys
  3. addresses = {}
  4. sources = {}
  5. last_touched = {}
  6. def find_nearby_allocations(addr,size):
  7. results = []
  8. for key in addresses.keys():
  9. if abs(addr - key) < size*2 and addresses[key]:
  10. results.append(key)
  11. results = sorted(results)
  12. for key in results:
  13. if key == addr:
  14. print(" self: 0x%x (size %d, allocated by 0x%x)" % (key, addresses[key], sources[key]))
  15. else:
  16. print(" nearby: 0x%x (size %d, allocated by 0x%x)" % (key, addresses[key], sources[key]))
  17. count = 0
  18. while 1:
  19. data = sys.stdin.read(17)
  20. t, = struct.unpack_from("c",data,0)
  21. addr, = struct.unpack_from("I",data,1)
  22. size, = struct.unpack_from("I",data,5)
  23. extra, = struct.unpack_from("I",data,9)
  24. fault, = struct.unpack_from("I",data,13)
  25. count += 1
  26. if count % 1000 == 0:
  27. print(count)
  28. if t == 'm':
  29. addresses[addr] = size
  30. sources[addr] = fault
  31. last_touched[addr] = t
  32. elif t == 'v':
  33. addresses[addr] = size
  34. sources[addr] = fault
  35. last_touched[addr] = t
  36. elif t == 'c':
  37. addresses[addr] = 'c'
  38. sources[addr] = 0
  39. last_touched[addr] = t
  40. elif t == 'r':
  41. if addr not in addresses:
  42. print("Bad realloc: 0x%x" % addr)
  43. else:
  44. addresses[addr] = None
  45. addresses[extra] = size
  46. sources[extra] = fault
  47. last_touched[addr] = t
  48. last_touched[extra] = t
  49. elif t == 'f':
  50. if addr not in addresses:
  51. print("Bad free detected: 0x%x" % addr)
  52. elif addresses[addr] is None:
  53. print("Double free detected: 0x%x (allocated by 0x%x)" % (addr,sources[addr]))
  54. elif addresses[addr] == 'c':
  55. print("freeing something that was calloced...")
  56. addresses[addr] = None
  57. elif extra != 0xDEADBEEF:
  58. print("Large buffer has bad value: 0x%x (0x%x) expected size is %d, supposed is %d" % (addr, extra, addresses[addr], size))
  59. elif addresses[addr] != size:
  60. 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]))
  61. find_nearby_allocations(addr,addresses[addr])
  62. else:
  63. addresses[addr] = None
  64. elif t == 'h':
  65. if addr not in addresses:
  66. print("Spurious halt: 0x%x" % (addr))
  67. else:
  68. print("Halting on suspected bug: 0x%x size was %d" % (addr, addresses[addr]))
  69. else:
  70. print("Garbage data detected: %c 0x%x %d 0xx" % (t, addr, size, extra))
  71. break