|
| 1 | +""" |
| 2 | +Undo Automation extension module for tracking calls to malloc() and free() and checking for |
| 3 | +leaked memory. |
| 4 | +
|
| 5 | +This script only support the x86-64 architecture. |
| 6 | +
|
| 7 | +Contributors: Chris Croft-White, Magne Hov |
| 8 | +""" |
| 9 | + |
| 10 | +import collections |
| 11 | +import re |
| 12 | + |
| 13 | +import gdb |
| 14 | + |
| 15 | +from undodb.debugger_extensions import udb |
| 16 | +from undodb.debugger_extensions.debugger_io import redirect_to_launcher_output |
| 17 | + |
| 18 | + |
| 19 | +def leak_check(): |
| 20 | + """ |
| 21 | + Implements breakpoints and stops on all calls to malloc() and free(), capturing the |
| 22 | + timestamp, size and returned pointer for malloc(), then confirms the address pointer is later |
| 23 | + seen in a free() call. |
| 24 | +
|
| 25 | + If a subsequent free() is not seen, then at the end of execution, output the timestamp and |
| 26 | + details of the memory which was never freed. |
| 27 | + """ |
| 28 | + # Set a breakpoint for the specified function. |
| 29 | + gdb.Breakpoint("malloc") |
| 30 | + gdb.Breakpoint("free") |
| 31 | + |
| 32 | + # Declare allocations dictionary structure. |
| 33 | + allocations = collections.OrderedDict() |
| 34 | + |
| 35 | + # Do "continue" until we have gone through the whole recording, potentially |
| 36 | + # hitting the breakpoints several times. |
| 37 | + end_of_time = udb.get_event_log_extent().end |
| 38 | + while True: |
| 39 | + gdb.execute("continue") |
| 40 | + |
| 41 | + # Rather than having the check directly in the while condition we have |
| 42 | + # it here as we don't want to print the backtrace when we hit the end of |
| 43 | + # the recording but only when we stop at a breakpoint. |
| 44 | + if udb.time.get().bbcount >= end_of_time: |
| 45 | + break |
| 46 | + |
| 47 | + # Use the $PC output to get the symbol and idenfity whether execution has stopped |
| 48 | + # at a malloc() or free() call. |
| 49 | + mypc = format(gdb.parse_and_eval("$pc")) |
| 50 | + if re.search("malloc", mypc): |
| 51 | + # In malloc(), set a FinishBreakpoint to capture the pointer returned later. |
| 52 | + mfbp = gdb.FinishBreakpoint() |
| 53 | + |
| 54 | + # For now, capture the timestamp and size of memory requested. |
| 55 | + time = udb.time.get() |
| 56 | + size = gdb.parse_and_eval("$rdi") |
| 57 | + |
| 58 | + gdb.execute("continue") |
| 59 | + |
| 60 | + # Should stop at the finish breakpoint, so capture the pointer. |
| 61 | + addr = mfbp.return_value |
| 62 | + |
| 63 | + if addr: |
| 64 | + # Store details in the dictionary. |
| 65 | + allocations[format(addr)] = time, size |
| 66 | + else: |
| 67 | + print(f"-- INFO: Malloc called for {size} byte(s) but null returned.") |
| 68 | + |
| 69 | + print(f"{time}: malloc() called: {size} byte(s) allocated at {addr}.") |
| 70 | + |
| 71 | + elif re.search("free", mypc): |
| 72 | + # In free(), get the pointer address. |
| 73 | + addr = gdb.parse_and_eval("$rdi") |
| 74 | + |
| 75 | + time = udb.time.get() |
| 76 | + |
| 77 | + # Delete entry from the dictionary as this memory was released. |
| 78 | + if addr > 0: |
| 79 | + if allocations[hex(int(format(addr)))]: |
| 80 | + del allocations[hex(int(format(addr)))] |
| 81 | + else: |
| 82 | + print("--- INFO: Free called with unknown address") |
| 83 | + else: |
| 84 | + print("--- INFO: Free called with null address") |
| 85 | + |
| 86 | + #with redirect_to_launcher_output(): |
| 87 | + print(f"{time}: free() called for {int(addr):#x}") |
| 88 | + |
| 89 | + |
| 90 | + # If Allocations has any entries remaining, they were not released. |
| 91 | + with redirect_to_launcher_output(): |
| 92 | + print () |
| 93 | + print (f"{len(allocations)} unmatched memory allocation(s):") |
| 94 | + print () |
| 95 | + |
| 96 | + total = 0 |
| 97 | + |
| 98 | + # Increase the amount of source from default (10) to 16 lines for more context. |
| 99 | + gdb.execute("set listsize 16") |
| 100 | + for addr in allocations: |
| 101 | + time, size = allocations[addr] |
| 102 | + total += size |
| 103 | + print("===============================================================================") |
| 104 | + print(f"{time}: {size} bytes was allocated at {addr}, but never freed.") |
| 105 | + print("===============================================================================") |
| 106 | + udb.time.goto(time) |
| 107 | + print("Backtrace:") |
| 108 | + gdb.execute("backtrace") |
| 109 | + print() |
| 110 | + print("Source (if available):") |
| 111 | + gdb.execute("finish") |
| 112 | + gdb.execute("list") |
| 113 | + print() |
| 114 | + print("Locals (after malloc returns):") |
| 115 | + gdb.execute("info locals") |
| 116 | + print() |
| 117 | + print() |
| 118 | + print("===============================================================================") |
| 119 | + print(f" In total, {total} byte(s) were allocated and not released") |
| 120 | + print() |
| 121 | + |
| 122 | + return len(allocations) |
| 123 | + |
| 124 | + |
| 125 | +# UDB will automatically load the modules passed to UdbLauncher.add_extension and, if present, |
| 126 | +# automatically execute any function (with no arguments) called "run". |
| 127 | +def run(): |
| 128 | + # Needed to allow GDB to fixup breakpoints properly after glibc has been loaded |
| 129 | + gdb.Breakpoint("main") |
| 130 | + |
| 131 | + unmatched = leak_check() |
| 132 | + |
| 133 | + # Pass the number of time we hit the breakpoint back to the outer script. |
| 134 | + udb.result_data["unmatched"] = unmatched |
0 commit comments