def check(): print "" print "Check heap regions......................." n = gdb.parse_and_eval("num_regions") blks = gdb.parse_and_eval("regions") i = 0 while i < n: blk = blks + i addr = int(blk['p'].cast(gdb.lookup_type('long'))) my_blk = gdb.heap_block(addr) match = True if blk['inuse']: if blk['p'] != my_blk.address or blk['size'] != my_blk.size or not my_blk.inuse: match = False else: if my_blk.inuse: match = False if not match: print "[%d] addr=0x%x size=%u inuse=%d" % (i, blk['p'], blk['size'], blk['inuse']) print "[%d] addr=0x%x size=%u inuse=%d" % (i, my_blk.address, my_blk.size, my_blk.inuse) raise Exception('core analyzer returns wrong heap block info') i = i + 1 print "%d heap memory blocks are verified" % (n) print "Passed ......................." print ""
def check_ref(): print "[ca_test] Checking object reference ..." ulong_type = gdb.lookup_type('long') var_hidden_object = gdb.parse_and_eval("hidden_object") var_addr = int(var_hidden_object.address.cast(ulong_type)) obj_addr = int(var_hidden_object) if obj_addr == 0: print "[ca_test] Object address is NULL" raise Exception('Test Failed') # "hidden_object" is a gloval variable that points to a heap object blk = gdb.heap_block(obj_addr) if blk == None or blk.address != obj_addr: print "[ca_test] Failed to query heap object of address 0x%x" % ( obj_addr) raise Exception('Test Failed') # Search in global sections refs = gdb.ref(obj_addr, 1, gdb.ENUM_MODULE_TEXT | gdb.ENUM_MODULE_DATA) if refs == None or len(refs) != 1 or refs[0].address != var_addr: print "[ca_test] Failed to find the global reference (var \"hidden_object\" " \ "at 0x%x) to object at address 0x%x" % (var_addr, obj_addr) raise Exception('Test Failed') print "[ca_test]\tFound the global reference: var \"hidden_object\" " \ "at 0x%x" % (var_addr) # Search in heap refs = gdb.ref(obj_addr, 1, gdb.ENUM_HEAP) if (refs == None or not refs[0].heap_inuse): print "[ca_test] Failed to find the heap reference to object at address 0x%x" \ % (obj_addr) raise Exception('Test Failed') if len(refs) != 1: print("[ca_test]\tFound %d references in heap" % len(refs)) print "[ca_test]\tFound heap reference: addr=0x%x size=%u to object at address 0x%x" \ % (refs[0].heap_addr, refs[0].heap_size, obj_addr)
def check_heap_blocks(known_blks, count): print "[ca_test] Checking heap blocks ..." ulong_type = gdb.lookup_type('long') user_blks = [] i = 0 while i < count: blk = known_blks + i blk_addr = int(blk['p'].cast(ulong_type)) blk_size = int(blk['size'].cast(ulong_type)) my_blk = gdb.heap_block(blk_addr) match = True if blk['inuse']: if blk_addr != my_blk.address or blk_size != my_blk.size or not my_blk.inuse: match = False py_blk = Block(blk_addr, blk_size, True) user_blks.append(py_blk) else: if my_blk.inuse: match = False if not match: print "[ca_test] core analyzer returns wrong heap info of block [%d]" % (i) print "[ca_test] \ttrue: addr=0x%x size=%u inuse=%d" \ % (blk['p'], blk['size'], blk['inuse']) print "[ca_test] \twrong: addr=0x%x size=%u inuse=%d" \ % (my_blk.address, my_blk.size, my_blk.inuse) raise Exception('Test Failed') i = i + 1 print "[ca_test]\tVerified %d heap blocks" % (count) return user_blks
def check(): print "" print "Check heap regions......................." n = gdb.parse_and_eval("num_regions") blks = gdb.parse_and_eval("regions") i = 0 while i < n: blk = blks + i addr = int(blk['p'].cast(gdb.lookup_type('long'))) my_blk = gdb.heap_block(addr) match = True if blk['inuse']: if blk['p'] != my_blk.address or blk[ 'size'] != my_blk.size or not my_blk.inuse: match = False else: if my_blk.inuse: match = False if not match: print "[%d] addr=0x%x size=%u inuse=%d" % ( i, blk['p'], blk['size'], blk['inuse']) print "[%d] addr=0x%x size=%u inuse=%d" % ( i, my_blk.address, my_blk.size, my_blk.inuse) raise Exception('core analyzer returns wrong heap block info') i = i + 1 print "%d heap memory blocks are verified" % (n) print "Passed ......................." print ""
def check_heap_blocks(known_blks, count): print("[ca_test] Checking heap blocks ...") ulong_type = gdb.lookup_type('long') user_blks = [] i = 0 while i < count: blk = known_blks + i if not blk: raise Exception('block[%d] is unexpectedly NIL' % i) blk_addr = int(blk['p'].cast(ulong_type)) blk_size = int(blk['size'].cast(ulong_type)) my_blk = gdb.heap_block(blk_addr) if not my_blk: raise Exception('Failed to query block at 0x%x' % blk_addr) match = True if blk['inuse']: if blk_addr != my_blk.address or blk_size != my_blk.size or not my_blk.inuse: match = False py_blk = Block(blk_addr, blk_size, True) user_blks.append(py_blk) else: if my_blk.inuse: match = False if not match: print( "[ca_test] core analyzer returns wrong heap info of block [%d]" % (i)) print("[ca_test] \texpected: addr=0x%x size=%u inuse=%d" \ % (blk_addr, blk_size, blk['inuse'])) print("[ca_test] \tgot: addr=0x%x size=%u inuse=%d" \ % (my_blk.address, my_blk.size, my_blk.inuse)) raise Exception('Failed to check block at 0x%x' % blk_addr) i = i + 1 print("[ca_test]\tVerified %d heap blocks" % (count)) return user_blks
gdb.execute('file ./mallocTest') gdb.execute ('run') print "" print "Check heap regions......................." #def check(): n = gdb.parse_and_eval("num_regions") blks = gdb.parse_and_eval("regions") i = 0 while i < n: blk = blks + i addr = int(blk['p'].cast(gdb.lookup_type('long'))) my_blk = gdb.heap_block(addr) match = True if blk['inuse']: if blk['p'] != my_blk.address or blk['size'] != my_blk.size or not my_blk.inuse: match = False else: if my_blk.inuse: match = False if not match: print "[%d] addr=0x%x size=%u inuse=%d" % (i, blk['p'], blk['size'], blk['inuse']) print "[%d] addr=0x%x size=%u inuse=%d" % (i, my_blk.address, my_blk.size, my_blk.inuse) raise Exception('core analyzer returns wrong heap block info') i = i + 1 print "%d heap memory blocks are verified" % (n)
gdb.execute('file ./mallocTest') gdb.execute('run') print "" print "Check heap regions......................." #def check(): n = gdb.parse_and_eval("num_regions") blks = gdb.parse_and_eval("regions") i = 0 while i < n: blk = blks + i addr = int(blk['p'].cast(gdb.lookup_type('long'))) my_blk = gdb.heap_block(addr) match = True if blk['inuse']: if blk['p'] != my_blk.address or blk[ 'size'] != my_blk.size or not my_blk.inuse: match = False else: if my_blk.inuse: match = False if not match: print "[%d] addr=0x%x size=%u inuse=%d" % (i, blk['p'], blk['size'], blk['inuse']) print "[%d] addr=0x%x size=%u inuse=%d" % (i, my_blk.address, my_blk.size, my_blk.inuse) raise Exception('core analyzer returns wrong heap block info')