Commit 1201ec2e authored by Gaurav Kukreja's avatar Gaurav Kukreja

rewrote map_cfg.py in match_cfg.py, working for all o1 cases with gdbMapping

Signed-off-by: Gaurav Kukreja's avatarGaurav Kukreja <gaurav@gauravk.in>
parent 74d89116
import re
import logging
from optparse import OptionParser
from subprocess import call
from instrument import *
from map_cfg import map_cfg
def find(f, seq):
"""Return first item in sequence where f(item) == True."""
for item in seq:
if f(item):
return item
def debugListGlobalVariables(listGlobalVariables):
print ""
for globVar in listGlobalVariables:
print ("%s\t\t0x%x\t\t(type=%s; length=%d) - %s:%d" %
(globVar.name, globVar.address, globVar.type, globVar.length,
globVar.file, globVar.lineNum))
print ""
def getGlobalVariablesInfoFromGDB(listBinaryFileNames):
re_AllDefinedVariables = re.compile("All Defined Variables:")
re_File = re.compile("File\s(.*):")
re_Variable = re.compile("((?:[\w_]*\s)*)([\w_]*)(?:\[([0-9]*)\])*;")
re_VarAdd = re.compile("Symbol \"([\w_]*)\" is static storage at address ([0-9a-fA-Fx]*).")
listGlobalVariables = []
for fileName in listBinaryFileNames:
# Fetch Global Variable Names from this file
gdbXFileName = fileName + ".globalVarNames.gdbx"
gdbXFile = open(gdbXFileName, 'w')
command = "info variables\n"
gdbXFile.write(command)
gdbXFile.write("quit\n")
gdbXFile.close()
gdbGlobalVarNamesOutputFileName = fileName + ".globalVarNames.gdbo"
gdbGlobalVarNamesOutputFile = open(gdbGlobalVarNamesOutputFileName, 'w')
call(args=["gdb", "--quiet", "--command="+gdbXFileName, fileName],
stdout=gdbGlobalVarNamesOutputFile)
gdbGlobalVarNamesOutputFile.close()
gdbGlobalVarNamesOutputFile = open(gdbGlobalVarNamesOutputFileName, 'r')
currFileName = ""
currListGlobalVariables = []
for line in gdbGlobalVarNamesOutputFile:
m = re_File.match(line)
if m is not None:
currFileName = m.group(1)
m = re_Variable.match(line)
if m is not None:
dataType = m.group(1)
varName = m.group(2)
if m.group(3) is not None:
varLen = int(m.group(3))
else:
varLen = 0
currListGlobalVariables.append(GlobalVariable(name=varName,
type=dataType,
length=varLen,
file=currFileName))
gdbGlobalVarNamesOutputFile.close()
# Fetch addresses for Global Variables in this file
gdbXGlobalVarAddFileName = fileName + ".globalVarAdd.gdbx"
gdbXGlobalVarAddFile = open(gdbXGlobalVarAddFileName, 'w')
for var in currListGlobalVariables:
gdbXGlobalVarAddFile.write("info address %s\n" % (var.name))
gdbXGlobalVarAddFile.write("quit\n")
gdbXGlobalVarAddFile.close()
gdbGlobalVarAddOutputFileName = fileName + ".globalVarAdd.gdbo"
gdbGlobalVarAddOutputFile = open(gdbGlobalVarAddOutputFileName, 'w')
call(args=["gdb", "--quiet", "--command="+gdbXGlobalVarAddFileName, fileName],
stdout=gdbGlobalVarAddOutputFile)
gdbGlobalVarAddOutputFile.close()
gdbGlobalVarAddOutputFile = open(gdbGlobalVarAddOutputFileName, 'r')
for line in gdbGlobalVarAddOutputFile:
m = re_VarAdd.match(line)
if m is not None:
var = find(lambda v: v.name == m.group(1), currListGlobalVariables)
var.setAddress(int(m.group(2), 16))
listGlobalVariables = listGlobalVariables + currListGlobalVariables
debugListGlobalVariables(listGlobalVariables)
return listGlobalVariables
re_loadPCRelative = re.compile("\s*ldr\s*[\w]\2,\s*\[pc,\s#[\d]*\]\s*;\s*[a-fA-F0-9]*\s*<\w*+0x[a-fA-F0-9]*>")
def instrument_cache(listISCFileNames, listISCFunctions,
listObjdumpFileNames, listObjdumpFunctions,
listBinaryFileNames):
'''
Algorithm
'''
getGlobalVariablesInfoFromGDB(listBinaryFileNames)
if __name__ == "__main__":
# listISCFileNames = []
# listObjdumpFileNames = []
# app = QtGui.QApplication(sys.argv)
logging.basicConfig(level=logging.DEBUG)
optparser = OptionParser()
optparser.add_option("-i", "--isc", action="append", dest="listISCFileNames",
type="string", help="ISC Filename. For multiple files, use -i <filename> multiple times.",
metavar="FILE")
optparser.add_option("-o", "--objdump", action="append",
type="string", dest="listObjdumpFileNames",
help="Objdump Filename. For multiple files, use -o <filename> multiple times.",
metavar="FILE")
optparser.add_option("-b", "--binary", action="append",
type="string", dest="listBinaryFileNames",
help="Binary Filename. For multiple files, use -b <filename> multiple times.",
metavar="FILE")
(options, args) = optparser.parse_args()
if (len(args) > 0):
print "Additional arguments are being ignored"
listISCFileNames = options.listISCFileNames
listObjdumpFileNames = options.listObjdumpFileNames
listBinaryFileNames = options.listBinaryFileNames
# (listISCFunctions, listObjdumpFunctions) = map_cfg(listISCFileNames,
# listObjdumpFileNames,
# listBinaryFileNames)
getGlobalVariablesInfoFromGDB(listBinaryFileNames)
# instrument_cache(listISCFileNames, listISCFunctions,
# listObjdumpFileNames, listObjdumpFunctions,
# listBinaryFileNames)
#
\ No newline at end of file
......@@ -189,10 +189,14 @@ class ControlFlowGraph:
self.listBlocks[currBlockIndex].flow = currBlockFlow
class FunctionDesc:
def __init__(self, functionName, fileName, startLine, endLine, cfg):
def __init__(self, functionName, fileName, startLine, endLine, cfg, stackSize = -1):
self.functionName = functionName
self.fileName = fileName
self.startLine = startLine
self.endLine = endLine
self.cfg = cfg
self.stackSize = stackSize
def setStackSize(self, stackSize):
self.stackSize = stackSize
\ No newline at end of file
......@@ -6,6 +6,8 @@ import sys
import re
from cfg import *
sizeOfRegisters = 4
re_sectionStart = re.compile('Disassembly of section .(.*):')
re_funcDef = re.compile('\s*([0-9a-f]*)\s*<(.*)>:')
re_instruction = re.compile('\s*([0-9a-f]*):\s*([0-9a-f]*)\s*(.*)')
......@@ -13,6 +15,8 @@ re_branchInst = re.compile('\s*(b(?!ic)(?:l|x|lx|xj)?(?:eq|ne|cs|hs|lo|cc|mi|pl|
re_unconditionalBranchInst = re.compile('\s*(b(?:l|x|lx|xj)?)\s*([0-9a-f]*)\s*<(.*)>')
re_conditionalBranchInst = re.compile('\s*(b(?:l|x|lx|xj)?(?:eq|ne|cs|hs|lo|cc|mi|pl|hi|ls|ge|lt|gt|le))\s*([0-9a-f]*)\s*<(.*)>')
re_returnInst = re.compile('\s*(bx)\s*(lr)')
re_stackPointerSubInst = re.compile("\s*sub\s*sp, sp, #([0-9]*)\s*.*")
re_pushInst = re.compile("\s*push\s*\{((?:\w*,\s)*\w*)\}")
listFunctionsIgnore = ['__cs3_interrupt_vector',
'__cs3_reset',
......@@ -48,6 +52,7 @@ def parse_binary(fileName, listFunctionNames = []):
inFuncBody = 0 # is 1, when inside Function Body
currFuncName = ""
currFuncFileName = ""
currFuncStackSize = 0
currFuncStartLine = 0
listCurrFuncBlockStartLineNum = []
listCurrFuncBlockEndLineNum = []
......@@ -103,6 +108,7 @@ def parse_binary(fileName, listFunctionNames = []):
m.group(2) not in listFunctionsIgnore)):
inFuncBody = 1
currFuncName = m.group(2)
currFuncStackSize = 0
currFuncFileName = fileName
currFuncStartLine = lineNum + 1
listCurrFuncBlockStartLineNum.append(currFuncStartLine)
......@@ -116,6 +122,24 @@ def parse_binary(fileName, listFunctionNames = []):
opcode = m.group(2)
inst = m.group(3)
# Look for sub instruction on sp (stack pointer) to find the
# stack size for the current function.
# Sometimes, the sub instruction on sp occurs multiple
# times. To incorporate such corner cases, increment the
# currFuncStackSize variable each time.
m = re_stackPointerSubInst.match(inst)
if m is not None:
stackSize = int(m.group(1))
currFuncStackSize = currFuncStackSize + stackSize
# Look for push instruction inside function, to add to the
# stack size of the function.
m = re_pushInst.match(inst)
if m is not None:
pushInstOperand = m.group(1)
numRegistersPushed = pushInstOperand.count(',') + 1
currFuncStackSize = currFuncStackSize + numRegistersPushed * sizeOfRegisters
m = re_branchInst.match(inst)
if m is not None and m.group(3).startswith(currFuncName):
# Branch Instruction
......@@ -251,11 +275,13 @@ def parse_binary(fileName, listFunctionNames = []):
currFuncStartLine,
currFuncEndLine,
ControlFlowGraph(listBlocks,
listEdges)))
listEdges),
currFuncStackSize))
# reset the state management variables
currFuncName = ""
currFuncFileName = ""
currFuncStackSize = 0
currFuncStartLine = 0
currFuncEndLine = 0
listCurrFuncBlockStartLineNum = []
......
......@@ -329,15 +329,16 @@ def draw_cfg(cfg, isISC, v):
continue
else:
# 4.c. Successor has not yet been plotted
predBlocks = cfg.predecessorBlocksWOBackEdges(succBlock)
predBlocks = cfg.predecessorBlocks(succBlock)
if len(predBlocks) > 1:
posSuccBlockX = 0
posSuccBlockY = 0
for predBlock in predBlocks:
posSuccBlockX = pos[posOfBlock[predBlock]][0] + posSuccBlockX
if posSuccBlockY > pos[posOfBlock[predBlock]][1] - verticalGap:
posSuccBlockY = pos[posOfBlock[predBlock]][1] - verticalGap
if predBlock in posOfBlock:
posSuccBlockX = pos[posOfBlock[predBlock]][0] + posSuccBlockX
if posSuccBlockY > pos[posOfBlock[predBlock]][1] - verticalGap:
posSuccBlockY = pos[posOfBlock[predBlock]][1] - verticalGap
posSuccBlockX = posSuccBlockX / len(predBlocks)
posSuccBlock = [posSuccBlockX, posSuccBlockY]
else:
......
import sys
class GlobalVariable:
def __init__(self):
self.name = ""
self.address = 0
self.type = ""
self.length = -1
self.file = ""
self.lineNum = -1
def __init__(self, name, type, length, file):
self.name = name
self.address = -1
self.type = type
self.length = length
self.file = file
self.lineNum = -1
def setAddress(self, address):
self.address = address
\ No newline at end of file
......@@ -24,9 +24,9 @@ COND_EXEC_BLOCKLEN_THRESH = 4
app = None
listISCFileNames = []
listObjdumpFileNames = []
listBinaryFileNames = []
# listISCFileNames = []
# listObjdumpFileNames = []
# listBinaryFileNames = []
class GDBMapTarget:
def __init__(self, fileName, lineNum):
......@@ -37,6 +37,7 @@ def printDebugMapCFG(listISCFunctions, listObjdumpFunctions, gdbMapping):
for func in listObjdumpFunctions:
print("\nFileName : %s" % (func.fileName))
print("Function : %s" % (func.functionName))
print "\t Stack Size = %d" % func.stackSize
ISCFuncCfg = find(lambda fn: fn.functionName == func.functionName, listISCFunctions).cfg
i = 0
for block in func.cfg.listBlocks:
......@@ -207,17 +208,18 @@ def mapping(cfgISC, blockIndISC, cfgObj, blockIndObj, mergedLevelsISC, gdbMappin
'''
# a = input("Press Enter to continue...")
mappingStackISC.append(blockIndISC)
mappingStackObj.append(blockIndObj)
a = raw_input("Press Enter to continue...")
mappingStackISC.append((blockIndISC, [blockIndObj]))
mappingStackObj.append((blockIndObj, [blockIndISC]))
blockISC = cfgISC.listBlocks[blockIndISC]
blockObj = cfgObj.listBlocks[blockIndObj]
logging.debug("\tMapping blocks ISC:%s and OBJ:%d" % (blockISC.name, blockIndObj))
logging.debug( "\tmergedLevelsISC = %d" % (mergedLevelsISC))
logging.debug("")
logging.debug(" Mapping blocks ISC:%s and OBJ:%d" % (blockISC.name, blockIndObj))
logging.debug("\t mergedLevelsISC = %d" % (mergedLevelsISC))
if (blockISC.isReturning == 1 and
blockObj.isReturning == 1):
logging.debug( "\t\tBoth Blocks return!!!")
logging.debug("\t\t Both Blocks return!!!")
blockISC.mapsTo.append(blockIndObj)
blockObj.mapsTo.append(blockIndISC)
mappingStackISC.pop()
......@@ -229,10 +231,11 @@ def mapping(cfgISC, blockIndISC, cfgObj, blockIndObj, mergedLevelsISC, gdbMappin
listSuccWOBackEdgeISC = cfgISC.successorBlocksWOBackEdges(blockIndISC)
listSuccWOBackEdgeObj = cfgObj.successorBlocksWOBackEdges(blockIndObj)
logging.debug("\t Checking if blockISC returns, and successor of blockObj returns!")
if (blockISC.isReturning == 1 and
len(listSuccObj) == 1 and
cfgObj.listBlocks[listSuccObj[0]].isReturning == 1):
print "Here"
logging.debug("\t\t It does!")
blockISC.mapsTo.append(blockIndObj)
blockObj.mapsTo.append(blockIndISC)
cfgObj.listBlocks[listSuccObj[0]].mapsTo.append(blockIndISC)
......@@ -241,10 +244,8 @@ def mapping(cfgISC, blockIndISC, cfgObj, blockIndObj, mergedLevelsISC, gdbMappin
return 0
if (blockISC.flow != blockObj.flow or
# (blockISC.nestingLevel - mergedLevelsISC) != blockObj.nestingLevel or
blockISC.isReturning != blockObj.isReturning):
logging.debug( "\t\tFlow did not match or only one of them returns!")
logging.debug( "")
logging.debug("\t\t Flow did not match or only one of them returns!")
# logging.debug( "\t\tblockISC.nestingLevel - mergedLevelsISC = %d; blockObj.nestingLevel = %d" % ((blockISC.nestingLevel-mergedLevelsISC), blockObj.nestingLevel))
mappingStackISC.pop()
mappingStackObj.pop()
......@@ -264,7 +265,7 @@ def mapping(cfgISC, blockIndISC, cfgObj, blockIndObj, mergedLevelsISC, gdbMappin
minSuccBlockLength = blockLength
if minSuccBlockLength < COND_EXEC_BLOCKLEN_THRESH:
logging.debug( "\t\t Conditional Execution Found!")
logging.debug("\t\t Conditional Execution Found!")
# Conditional Execution!
for succ1BlockISC in listSuccISC:
if succ1BlockISC in mappingStackISC:
......@@ -277,9 +278,9 @@ def mapping(cfgISC, blockIndISC, cfgObj, blockIndObj, mergedLevelsISC, gdbMappin
continue
if succSucc1BlockISC == succ2BlockISC:
# case 1
logging.debug( "\t\t case 1")
mappingStackISC.append(succ1BlockISC)
mappingStackObj.pop() # popping blockIndObj, because mapping it again
logging.debug("\t\t case 1")
mappingStackISC.append((succ1BlockISC, [blockIndObj]))
blockObjStackEntry = mappingStackObj.pop() # popping blockIndObj, because mapping it again
if mapping(cfgISC, succ2BlockISC, cfgObj, blockIndObj, mergedLevelsISC + 1, gdbMapping) == 0:
cfgISC.listBlocks[blockIndISC].mapsTo.append(blockIndObj)
cfgISC.listBlocks[succ1BlockISC].mapsTo.append(blockIndObj)
......@@ -289,10 +290,9 @@ def mapping(cfgISC, blockIndISC, cfgObj, blockIndObj, mergedLevelsISC, gdbMappin
mappingStackISC.pop()
return 0
else:
print "HERE!!"
mappingStackObj.append(blockIndObj) # Adding what was removed above
# mappingStackISC.append(succ2BlockISC) # was already done above, no need to do again
mappingStackISC.append(succ2BlockISC)
mappingStackObj.append(blockObjStackEntry) # Adding what was removed above
# mappingStackISC.append((succ2BlockISC, [blockIndObj])) # was already done above, no need to do again
mappingStackISC.append((succ2BlockISC, [blockIndObj]))
listSuccSucc2BlockISC = cfgISC.successorBlocks(succ2BlockISC)
for succSucc2BlockISC in listSuccSucc2BlockISC:
if succSucc2BlockISC in mappingStackISC:
......@@ -324,9 +324,9 @@ def mapping(cfgISC, blockIndISC, cfgObj, blockIndObj, mergedLevelsISC, gdbMappin
if succSucc1BlockISC == succSucc2BlockISC:
# case 2
logging.debug( "\t\t case 2")
mappingStackISC.append(succ1BlockISC)
mappingStackISC.append(succ2BlockISC)
mappingStackObj.pop() # popping blockIndObj, because mapping it again
mappingStackISC.append((succ1BlockISC, [blockIndObj]))
mappingStackISC.append((succ2BlockISC, [blockIndObj]))
blockObjStackEntry = mappingStackObj.pop() # popping blockIndObj, because mapping it again
if mapping(cfgISC, succSucc1BlockISC, cfgObj, blockIndObj, mergedLevelsISC+2, gdbMapping) == 0:
cfgISC.listBlocks[blockIndISC].mapsTo.append(blockIndObj)
cfgISC.listBlocks[succ1BlockISC].mapsTo.append(blockIndObj)
......@@ -340,8 +340,8 @@ def mapping(cfgISC, blockIndISC, cfgObj, blockIndObj, mergedLevelsISC, gdbMappin
else:
# mappingStackISC.append(succ1BlockISC) # Was already done above, no need twice
# mappingStackISC.append(succ2BlockISC) # Was already done above, no need twice
mappingStackISC.append(succSucc1BlockISC)
mappingStackObj.append(blockIndObj) # was popped above, restoring it
mappingStackISC.append((succSucc1BlockISC, [blockIndObj]))
mappingStackObj.append(blockObjStackEntry) # was popped above, restoring it
listSuccSuccSucc1BlockISC = cfgISC.successorBlocks(succSucc1BlockISC)
for succSuccSucc1BlockISC in listSuccSuccSucc1BlockISC:
if succSuccSucc1BlockISC in mappingStackISC:
......@@ -365,54 +365,72 @@ def mapping(cfgISC, blockIndISC, cfgObj, blockIndObj, mergedLevelsISC, gdbMappin
mappingStackISC.pop() # succSucc1BlockISC
# Should not come here!
logging.warning ("Expected Conditional Execution, but not of the matches were valid!")
logging.warning ("\t\t Expected Conditional Execution, but not of the matches were valid!")
#TODO: Add more information about warning
else:
logging.warning("Conditional Execution found, but suspecting it to be last node, since length of successor block is more than threshold")
logging.warning("\t\t Conditional Execution found, but suspecting it to be last node, since length of successor block is more than threshold")
#TODO: Add more information about warning
logging.debug("\t Checking if length of successors is same!")
logging.debug("\t len(listSuccISC) = %d; len(listSuccObj) = %d" % (len(listSuccISC), len(listSuccObj)))
# Trying to map using Depth First Search traversal of each successor edge,
# if the number of successors is same.
numSuccBlocksMapped = 0
if len(listSuccISC) == len(listSuccObj):
logging.debug ("\t\t length of successors lists is same!")
logging.debug("\t Matching ISC:%s and OBJ:%s using DFT (number of successors is same)" % (blockISC.name, blockObj.name))
for succBlockISC in listSuccISC:
if succBlockISC in mappingStackISC:
logging.debug("%d in mappingStackISC" % succBlockISC)
numSuccBlocksMapped = numSuccBlocksMapped + 1
continue
succBlockISCMapped = 0
for succBlockObj in listSuccObj:
if succBlockObj in mappingStackObj:
logging.debug("%d in mappingStackISC" % succBlockObj)
# numSuccBlocksMapped = numSuccBlocksMapped + 1
continue
if mapping(cfgISC, succBlockISC, cfgObj, succBlockObj, mergedLevelsISC, gdbMapping) == 0:
blockISC.mapsTo.append(blockIndObj)
blockObj.mapsTo.append(blockIndISC)
mappingStackISC.pop()
mappingStackObj.pop()
return 0
# numSuccBlocksMapped = numSuccBlocksMapped + 1
succBlockISCMapped = 1
break
else:
continue
if succBlockISCMapped == 1:
numSuccBlocksMapped = numSuccBlocksMapped + 1
print "len(listSuccISC) = %d; len(listSuccObj) = %d" % (len(listSuccISC), len(listSuccObj))
print "len(listSuccWOBEISC) = %d; len(listSuccWOBEObj) = %d" % (len(listSuccWOBackEdgeISC), len(listSuccWOBackEdgeObj))
print "numSuccBlocksMapped = %d" % numSuccBlocksMapped
numBackEdgesISC = (len(listSuccISC) - len(listSuccWOBackEdgeISC))
numBackEdgesObj = (len(listSuccObj) - len(listSuccWOBackEdgeObj))
if len(listSuccISC) == numSuccBlocksMapped:
# All successor blocks were mapped!
blockISC.mapsTo.append(blockIndObj)
blockObj.mapsTo.append(blockIndISC)
mappingStackISC.pop()
mappingStackObj.pop()
return 0
# Trying to map using gdbMapping
deepestISCBlock = -1
deepestISCBlockNestingLevel = -1
logging.debug ("GDBMAPPING: using gdbMapping to map blockObj %d:%d-%d" % (blockIndObj, blockObj.startLine, blockObj.endLine))
logging.debug ("\t Matching ISC:%s and OBJ:%s using gdbMapping" % (blockISC.name, blockObj.name))
blockObj.mapsTo = []
for lineNum in range(blockObj.startLine, blockObj.endLine):
if lineNum in gdbMapping:
ISCFileName = gdbMapping[lineNum].fileName
ISCLineNum = gdbMapping[lineNum].lineNum
# logging.debug("GDBMAPPING: objline %d maps to %s:%d" % (lineNum, ISCFileName, ISClineNum))
for i in range(len(cfgISC.listBlocks)):
if (cfgISC.listBlocks[i].startLine <= ISCLineNum and
cfgISC.listBlocks[i].endLine >= ISCLineNum):
if i not in blockObj.mapsTo:
blockObj.mapsTo.append(i)
if blockIndObj not in cfgISC.listBlocks[i].mapsTo:
cfgISC.listBlocks[i].mapsTo.append(blockIndObj)
if deepestISCBlockNestingLevel < cfgISC.listBlocks[i].nestingLevel and i != blockIndISC:
# The deepest ISC Block is not inserted in mappingStackISC
# because mapping will be called on this block, and
# it will be inserted by the next iteration of the
# mapping function. Insert the block in stack, which
# was previously thought of being deepest.
logging.debug ("GDBMAPPING: deepestISCBlock = %s" % cfgISC.listBlocks[i].name)
if deepestISCBlock != -1:
mappingStackISC.append(deepestISCBlock)
deepestISCBlock = i
......@@ -468,17 +486,14 @@ def map_cfg(listISCFileNames, listObjdumpFileNames, listBinaryFileNames):
for function in listObjdumpFunctions:
logging.debug("Computing flow for function %s from file %s" % (function.functionName, function.fileName))
function.cfg.computeFlow()
for funcISC in listISCFunctions:
funcObj = find(lambda fn: fn.functionName == funcISC.functionName, listObjdumpFunctions)
display_cfgs(app, funcISC.cfg, funcObj.cfg, "%s" % funcISC.functionName)
for binaryFileName in listBinaryFileNames:
gdbMapping = getGDBMapping(binaryFileName, objdumpLineNumForAddress)
# print_debug_isc (listISCFunctions)
# print_debug_binary (listObjdumpFunctions, gdbMapping)
printDebugMapCFG(listISCFunctions, listObjdumpFunctions, gdbMapping)
# display_cfgs(app, listISCFunctions[0].cfg, listObjdumpFunctions[0].cfg, "%s" % listISCFunctions[0].functionName)
for fnISC in listISCFunctions:
mappingStackISC = []
mappingStackObj = []
......@@ -494,21 +509,16 @@ def map_cfg(listISCFileNames, listObjdumpFileNames, listBinaryFileNames):
else:
logging.debug( "Fuck my life!!!")
# print_debug_isc (listISCFunctions)
# print_debug_binary (listObjdumpFunctions, gdbMapping)
printDebugMapCFG(listISCFunctions, listObjdumpFunctions, gdbMapping)
#
for funcISC in listISCFunctions:
funcObj = find(lambda fn: fn.functionName == funcISC.functionName, listObjdumpFunctions)
display_cfgs(app, funcISC.cfg, funcObj.cfg, "%s" % fnISC.functionName)
display_cfgs(app, funcISC.cfg, funcObj.cfg, "%s" % funcISC.functionName)
return listISCFunctions, listObjdumpFunctions
if __name__ == "__main__":
# listISCFileNames = []
# listObjdumpFileNames = []
app = QtGui.QApplication(sys.argv)
logging.basicConfig(level=logging.DEBUG)
......
from optparse import OptionParser
from subprocess import call
import logging
import re
from collections import deque
import sys
from PyQt4 import QtGui, QtCore
from cfg_binary import parse_binary, print_debug_binary
from cfg_isc import parse_isc, print_debug_isc
from display_cfg import display_cfgs
######################################################
## Global Variables
######################################################
COND_EXEC_BLOCKLEN_THRESH = 4
app = None
# listISCFileNames = []
# listObjdumpFileNames = []
# listBinaryFileNames = []
class GDBMapTarget:
def __init__(self, fileName, lineNum):
self.fileName = fileName
self.lineNum = lineNum
def printDebugMapCFG(listISCFunctions, listObjdumpFunctions, gdbMapping):
for func in listObjdumpFunctions:
print("\nFileName : %s" % (func.fileName))
print("Function : %s" % (func.functionName))
print "\t Stack Size = %d" % func.stackSize
ISCFuncCfg = find(lambda fn: fn.functionName == func.functionName, listISCFunctions).cfg
i = 0
for block in func.cfg.listBlocks:
print("\t Block %d: line %d - %d, flow = %f, nestingLevel = %d" %
(i, block.startLine, block.endLine,
block.flow, block.nestingLevel))
print "\t Maps to ",
block.mapsTo = list(set(block.mapsTo))
for blockIndISC in block.mapsTo:
print ISCFuncCfg.listBlocks[blockIndISC].name+", ",
print ""
for funcCall in block.listFunctionCalls:
print("\t\t calls %s()" % (funcCall))
if block.hasConditionalExec == 1:
print("\t\t Conditional Execution Instruction!")
if block.isReturning == 1:
print("\t\t returns")
for edge in func.cfg.listEdges:
if edge.fromBlockIndex == i:
print("\t\t Edge to block %d" % (edge.toBlockIndex))
for lineNum in range(block.startLine, block.endLine):
if lineNum in gdbMapping:
ISCFileName = gdbMapping[lineNum].fileName
ISCLineNum = gdbMapping[lineNum].lineNum
ISCBlock = ISCFuncCfg.find(lineNum = ISCLineNum)
if ISCBlock is not None:
ISCBlockName = ISCFuncCfg.find(lineNum = ISCLineNum).name
else:
ISCBlockName = "%d" % (ISCLineNum)
print("\t\t Line %d from %s:%s" % (lineNum,
gdbMapping[lineNum].fileName,
ISCBlockName))
i = i + 1
for func in listISCFunctions:
print("\nFileName : %s" % (func.fileName))
print("Function : %s" % (func.functionName))
ObjFuncCfg = find(lambda fn: fn.functionName == func.functionName, listObjdumpFunctions).cfg
i = 0
for block in func.cfg.listBlocks:
print("\t Block %s: line %d - %d, flow = %f, nestingLevel = %d" %
(func.cfg.listBlocks[i].name, block.startLine, block.endLine,
block.flow, block.nestingLevel))
print "\t Maps to ",
print list(set(block.mapsTo))
for funcCall in block.listFunctionCalls:
print("\t\t calls %s()" % (funcCall))
if block.hasConditionalExec == 1:
print("\t\t Conditional Execution Instruction!")
if block.isReturning == 1:
print("\t\t returns")
for edge in func.cfg.listEdges:
if edge.fromBlockIndex == i:
print("\t\t Edge to block %s" % (func.cfg.listBlocks[edge.toBlockIndex].name))
i = i + 1
def gdbMappingDebug(gdbMapping):
for lineNum in gdbMapping:
print ("line %d maps to %s:%d" % (lineNum, gdbMapping[lineNum].fileName, gdbMapping[lineNum].lineNum))
def find(f, seq):
"""Return first item in sequence where f(item) == True."""
for item in seq:
if f(item):
return item
def getGDBMapping(binFileName, objdumpLineNumForAddress):
gdbMapping = {}
re_gdbInfoLineOutput = re.compile('Line (\d*) of "(.*)" starts at address 0x([0-9a-f]*).*')
# File Name for GDB Command file
gdbXFileName = binFileName+".gdbx"
gdbXFile = open(gdbXFileName, 'w')
for address in objdumpLineNumForAddress:
line = "info line *0x%x\n" % (int(address, 16))
gdbXFile.write(line)
gdbXFile.write("quit\n")
gdbXFile.close()
gdbOutputFileName = binFileName+".gdbo"
gdbOutputFile = open(gdbOutputFileName, 'w')
call(args=["gdb", "--quiet", "--command="+gdbXFileName, binFileName],
stdout=gdbOutputFile)
gdbOutputFile.close()
gdbOutputFile = open(gdbOutputFileName, 'r')
for line in gdbOutputFile:
m = re_gdbInfoLineOutput.match(line)
if m is not None:
targetLineNum = int(m.group(1), 10)
targetFileName = m.group(2)
objdumpAddress = m.group(3)
objdumpLineNum = objdumpLineNumForAddress[objdumpAddress]
gdbMapping[objdumpLineNum] = GDBMapTarget(targetFileName, targetLineNum)
gdbOutputFile.close()
# gdbMappingDebug(gdbMapping)
return gdbMapping
mappingStackISC = []
mappingStackObj = []
gdbMapping = {}
def mapping(cfgISC, blockIndISC, cfgObj, blockIndObj, mergedLevelsISC):
# raw_input("Press any key to continue ...")
blockISC = cfgISC.listBlocks[blockIndISC]
blockObj = cfgObj.listBlocks[blockIndObj]
listSuccBlocksISC = cfgISC.successorBlocks(blockIndISC)
listSuccBlocksObj = cfgObj.successorBlocks(blockIndObj)
listSuccBlocksWOBackEdgeISC = cfgISC.successorBlocksWOBackEdges(blockIndISC)
listSuccBlocksWOBackEdgeObj = cfgObj.successorBlocksWOBackEdges(blockIndObj)
logging.debug("Matching ISC: %s and OBJ: %s" % (blockISC.name, blockObj.name))
# If both blocks return, mapping found!
if (blockISC.isReturning == 1 and blockObj.isReturning == 1):
logging.debug("\t %s::%s Both blocks return! Matched!" % (blockISC.name, blockObj.name))
blockISC.mapsTo.append(blockIndObj)
blockObj.mapsTo.append(blockIndISC)
return 0
# If one of the block returns, and other block has only one successor, which returns, mapping found!
if (blockISC.isReturning != blockObj.isReturning):
if (blockISC.isReturning == 1):
if (len(listSuccBlocksObj) == 1):
succBlockObj = cfgObj.listBlocks[listSuccBlocksObj[0]]
if (succBlockObj.isReturning == 1):
logging.debug("/t ISC:%s returns, and is mapped to both OBJ:%s and OBJ:%s"
% (blockISC.name, blockObj.name, succBlockObj.name))
blockISC.mapsTo.append(blockIndObj)
blockObj.mapsTo.append(blockIndISC)
succBlockObj.mapsTo.append(blockIndISC)
return 0
else:
# Mapping not found!
return -1
else:
# Mapping not found!
return -1
elif (blockObj.isReturning == 1):
if (len(listSuccBlocksISC) == 1):
succBlockISC = cfgISC.listBlocks[listSuccBlocksISC[0]]
if (succBlockISC.isReturning == 1):
logging.debug("/t OBJ:%s returns, and is mapped to ISC:%s"
% (blockObj.name, succBlockObj.name))
blockISC.mapsTo.append(blockIndObj)
succBlockISC.mapsTo.append(blockIndObj)
blockObj.mapsTo.append(blockIndISC)
return 0
else:
# Mapping not found!
return -1
else:
# Mapping not found!
return -1
# Checking if current blockISC or blockObj is already in stack, ie. has already been seen, ie. this is a back edge
stackEntryISC = find(lambda stackEntry: stackEntry[0] == blockIndISC, mappingStackISC[:-1])
if stackEntryISC != None:
# Back Edge Found!
stackEntryObj = find(lambda stackEntry: stackEntry[0] == blockIndObj, mappingStackObj[:-1])
if stackEntryObj != None:
# Back edge found in Obj too. Do these match?
if blockIndISC in stackEntryObj[1] and blockIndObj in stackEntryISC[1]:
logging.debug("\t %s::%s Back Edge Found!" %
(blockISC.name, blockObj.name))
# TODO: Is there something else to do here?
return 0
else:
logging.debug("\t %s::%s Back Edge in ISC could not be matched with Back Edge in Obj!" %
(blockISC.name, blockObj.name))
return -1
else:
# It may be that a block has been split in Obj
if len(listSuccBlocksObj) == 1:
succBlockIndObj = listSuccBlocksObj[0]
stackEntryObj = find(lambda stackEntry: stackEntry[0] == succBlockIndObj, mappingStackObj[:-1])
if stackEntryObj != None:
if blockIndISC in stackEntryObj[1] and succBlockIndObj in stackEntryISC[1]:
logging.debug("\t %s::%s Back Edge Found through split block in Obj!" %
(blockISC.name, blockObj.name))
# TODO: Is there something else to do here?
return 0
else:
logging.debug("\t %s::%s Back Edge in ISC could not be matched with Back Edge in Obj!" %
(blockISC.name, blockObj.name))
return -1
else:
logging.debug("\t %s::%s Back Edge in ISC but not in Obj!" %
(blockISC.name, blockObj.name))
return -1
else:
logging.debug("\t %s::%s Back Edge in ISC but not in Obj!" %
(blockISC.name, blockObj.name))
return -1
# Checking if current blockISC or blockObj is already in stack, ie. has already been seen, ie. this is a back edge
stackEntryObj = find(lambda stackEntry: stackEntry[0] == blockIndObj, mappingStackObj[:-1])
if stackEntryObj != None:
# Back Edge Found!
logging.debug("\t %s::%s Back Edge in Obj!" %
(blockISC.name, blockObj.name))
stackEntryISC = find(lambda stackEntry: stackEntry[0] == blockIndISC, mappingStackISC[:-1])
if stackEntryISC != None:
# Back edge found in Obj too. Do these match?
if blockIndISC in stackEntryObj[1] and blockIndObj in stackEntryISC[1]:
logging.debug("\t %s::%s Back Edge Found!" %
(blockISC.name, blockObj.name))
# TODO: Is there something else to do here?
return 0
else:
logging.debug("\t %s::%s Back Edge in Obj could not be matched with Back Edge in ISC!" %
(blockISC.name, blockObj.name))
return -1
else:
logging.debug("\t\t %s::%s No Back Edge in ISC, looking for split ISC block!" %
(blockISC.name, blockObj.name))
# It may be that a block has been split in Obj
for succBlockIndISC in listSuccBlocksISC:
# succBlockIndISC = listSuccBlocksISC[0]
stackEntryISC = find(lambda stackEntry: stackEntry[0] == succBlockIndISC, mappingStackISC[:-1])
if stackEntryISC != None:
logging.debug("\t %s::%s Back Edge in Obj and split block back edge in ISC!" %
(blockISC.name, blockObj.name))
if succBlockIndISC in stackEntryObj[1] and blockIndObj in stackEntryISC[1]:
logging.debug("\t %s::%s Back Edge Found through split block in ISC!" %
(blockISC.name, blockObj.name))
# Match current ISC block (split block) to predecessor of current Obj Block
blockISC.mapsTo.append(mappingStackObj[-1][0])
# TODO: Is there something else to do here?
return 0
else:
logging.debug("\t %s::%s Back Edge in Obj could not be matched with Back Edge in ISC!" %
(blockISC.name, blockObj.name))
# return -1
else:
logging.debug("\t %s::%s Back Edge in Obj but not in ISC!" %
(blockISC.name, blockObj.name))
# return -1
# else:
logging.debug("\t %s::%s Back Edge in Obj but not in ISC!" %
(blockISC.name, blockObj.name))
return -1
if (blockISC.flow != blockObj.flow):
logging.debug("\t %s::%s Flow Values don't match!")
return -1
# If none of the blocks return, it means we have to continue the DFT
if(len(listSuccBlocksWOBackEdgeISC) != len(listSuccBlocksWOBackEdgeObj)) and blockObj.hasConditionalExec == 1:
# Check that length of each successor block is less than threshold for Conditional Execution
lenLongestSuccBlock = 0
for succBlockIndISC in listSuccBlocksWOBackEdgeISC:
succBlockISC = cfgISC.listBlocks[succBlockIndISC]
if lenLongestSuccBlock < succBlockISC.endLine - succBlockISC.startLine:
lenLongestSuccBlock = succBlockISC.endLine - succBlockISC.startLine
if lenLongestSuccBlock > COND_EXEC_BLOCKLEN_THRESH:
logging.debug("\t %s::%s Length of successor blocks ISC greater than threshold to be considered as Conditional Execution" %
(blockISC.name, blockObj.name))
else:
logging.debug("\t %s::%s Found Conditional Execution" % (blockISC.name, blockObj.name))
# Case 1: 2 branches, one merges into other
# Case 2: 2 branches with same successor
# for each successor of blockISC
case1Found = 0
case2Found = 0
for succ1BlockIndISC in listSuccBlocksWOBackEdgeISC:
# list of successors of successor of blockISC
listSuccSucc1BlocksISC = cfgISC.successorBlocks(succ1BlockIndISC)
# for each successor of successor of blockISC
for succSucc1BlockIndISC in listSuccSucc1BlocksISC:
# for each successor of blockISC other than succ1BlockIndISC
for succ2BlockIndISC in list(set(listSuccBlocksWOBackEdgeISC) - {succ1BlockIndISC}):
if succSucc1BlockIndISC == succ2BlockIndISC:
case1Found = 1
break
# list of successors of other successor of blockIndISC
listSuccSucc2BlocksISC = cfgISC.successorBlocks(succ2BlockIndISC)
# for each successor of other successor of blockIndISC
for succSucc2BlockIndISC in listSuccSucc2BlocksISC:
# if successor of both successors of blockIndISC is same
if succSucc1BlockIndISC == succSucc2BlockIndISC:
case2Found = 1
break
if case2Found == 1:
break
if case1Found == 1 or case2Found == 1:
break
if case1Found == 1 or case2Found == 1:
break
if case1Found == 1:
logging.debug("\t\t %s::%s Conditional Execution Case 1" % (blockISC.name, blockObj.name))
# call mapping on succ2BlockIndISC and blockObj
mappingStackISC.append((succ1BlockIndISC, [blockIndObj]))
mappingStackISC.append((succ2BlockIndISC, [blockIndObj]))
mappingStackObj[-1][1].append(succ1BlockIndISC)
mappingStackObj[-1][1].append(succ2BlockIndISC)
if (mapping(cfgISC, succ2BlockIndISC, cfgObj, blockIndObj, mergedLevelsISC + 1) == 0):
logging.debug("\t\t\t %s::%s Mapping Found (CondExec: Case 1)!" %
(blockISC.name, blockObj.name))
blockISC.mapsTo.append(blockIndObj)
cfgISC.listBlocks[succ1BlockIndISC].mapsTo.append(blockIndObj)
cfgISC.listBlocks[succ2BlockIndISC].mapsTo.append(blockIndObj)
blockObj.mapsTo.append(blockIndISC)
mappingStackISC.pop()
mappingStackISC.pop()
return 0
else:
logging.warning("\t\t\t %s::%s Conditional Execution Case 1 was found, but could not be successfully mapped" % (blockISC.name, blockObj.name))
# return -1
elif case2Found == 1:
logging.debug("\t\t %s::%s Conditional Execution Case 2" % (blockISC.name, blockObj.name))
# Call Mapping on succSucc1BlockIndISC and blockIndObj
mappingStackISC.append((succ1BlockIndISC, [blockIndObj]))
mappingStackISC.append((succ2BlockIndISC, [blockIndObj]))
mappingStackISC.append((succSucc1BlockIndISC, [blockIndObj]))
mappingStackObj[-1][1].append(succ1BlockIndISC)
mappingStackObj[-1][1].append(succ2BlockIndISC)
mappingStackObj[-1][1].append(succSucc1BlockIndISC)
if (mapping(cfgISC, succSucc1BlockIndISC,
cfgObj, blockIndObj, mergedLevelsISC + 2) == 0):
logging.debug("\t\t\t %s::%s Mapping Found (CondExec: Case 2)!" %
(blockISC.name, blockObj.name))
blockISC.mapsTo.append(blockIndObj)
cfgISC.listBlocks[succ1BlockIndISC].mapsTo.append(blockIndObj)
cfgISC.listBlocks[succ2BlockIndISC].mapsTo.append(blockIndObj)
cfgISC.listBlocks[succSucc1BlockIndISC].mapsTo.append(blockIndObj)
blockObj.mapsTo.append(blockIndISC)
mappingStackISC.pop()
mappingStackISC.pop()
mappingStackISC.pop()
return 0
else:
logging.warning("\t\t\t %s::%s Conditional Execution Case 2 was found, but could not be successfully mapped" % (blockISC.name, blockObj.name))
# return -1
else:
logging.warning("\t\t %s::%s Conditional Execution was found, but neither case matched!" % (blockISC.name, blockObj.name))
# return -1
# Conditional Execution not found
if len(listSuccBlocksISC) == len(listSuccBlocksObj):
logging.debug("\t %s::%s length of successors is same, trying DFT" %
(blockISC.name, blockObj.name))
succBlocISCkMatchingFound = 0
for succBlockIndISC in listSuccBlocksISC:
# stackEntryISC = find(lambda stackEntry: stackEntry[0] == succBlockIndISC, mappingStackISC)
# if stackEntryISC != None:
# # look for all successors of blockObj which are in the stack,
# # and check if the succBlockIndISC maps to the obj node
# backEdgeMatched = 0
# for succBlockIndObj in listSuccBlocksObj:
# stackEntryObj = find(lambda stackEntry: stackEntry[0] == succBlockIndObj, mappingStackObj)
# if stackEntryObj != None:
# if (succBlockIndObj in stackEntryISC[1] and
# succBlockIndISC in stackEntryObj[1]):
# # back edge matched!
# logging.debug("%s::%s Back Edge to %s::%s matched!" %
# (blockISC.name, blockObj.name,
# cfgISC.listBlocks[succBlockIndISC].name,
# cfgObj.listBlocks[succBlockIndObj].name))
# backEdgeMatched = 1
# succBlocISCkMatchingFound = 1
# break
# if backEdgeMatched == 1:
# continue
# else:
# logging.error("%s::%s ISC block has back edge, that could not be matched to Obj Block" %
# (blockISC.name, blockObj.name))
# succBlocISCkMatchingFound = 0
# break
succBlockISCMatchFoundUsingDFT = 0
for succBlockIndObj in listSuccBlocksObj:
# stackEntryObj = find(lambda stackEntry: stackEntry[0] == succBlockIndObj, mappingStackObj)
# if stackEntryObj != None:
# # TODO: Check for some trouble
# # We should just skip this back edge
# continue
logging.debug("\t\t %s::%s Trying DFT on %s::%s" %
(blockISC.name, blockObj.name,
cfgISC.listBlocks[succBlockIndISC].name,
cfgObj.listBlocks[succBlockIndObj].name))
mappingStackISC.append((succBlockIndISC, [succBlockIndObj]))
mappingStackObj.append((succBlockIndObj, [succBlockIndISC]))
if (mapping(cfgISC, succBlockIndISC,
cfgObj, succBlockIndObj,
mergedLevelsISC) == 0):
cfgISC.listBlocks[succBlockIndISC].mapsTo.append(blockIndObj)
cfgObj.listBlocks[succBlockIndObj].mapsTo.append(blockIndISC)
mappingStackISC.pop()
mappingStackObj.pop()
succBlockISCMatchFoundUsingDFT = 1
succBlocISCkMatchingFound = 1
break
else:
mappingStackISC.pop()
mappingStackObj.pop()
continue # to try to match next successor of blockObj with current successor of blockISC
if succBlockISCMatchFoundUsingDFT == 1:
logging.debug("\t\t %s::%s DFT: Found matching for %s::%s" %
(blockISC.name, blockObj.name,
cfgISC.listBlocks[succBlockIndISC].name,
cfgObj.listBlocks[succBlockIndObj].name))
continue # to match next blockISC
else:
logging.error("\t\t %s::%s DFT: Matching obj block for ISC block not found!" %
(blockISC.name, blockObj.name))
succBlocISCkMatchingFound = 0
break
if succBlocISCkMatchingFound == 1:
# Mapping was found for each successor block of blockISC
blockISC.mapsTo.append(blockIndObj)
blockObj.mapsTo.append(blockIndISC)
return 0
else:
logging.debug("\t %s::%s Could not be matched using DFT" %
(blockISC.name, blockObj.name))
return -1
# # Trying to map using gdbMapping
# deepestISCBlock = -1
# deepestISCBlockNestingLevel = -1
# logging.debug ("\t %s::%s Trying to match using gdb info" % (blockISC.name, blockObj.name))
# blockObj.mapsTo = []
# for lineNum in range(blockObj.startLine, blockObj.endLine):
# if lineNum in gdbMapping:
# ISCLineNum = gdbMapping[lineNum].lineNum
# for i in range(len(cfgISC.listBlocks)):
# if (cfgISC.listBlocks[i].startLine <= ISCLineNum and
# cfgISC.listBlocks[i].endLine >= ISCLineNum):
# if i not in blockObj.mapsTo:
# blockObj.mapsTo.append(i)
# if blockIndObj not in cfgISC.listBlocks[i].mapsTo:
# cfgISC.listBlocks[i].mapsTo.append(blockIndObj)
# if deepestISCBlockNestingLevel < cfgISC.listBlocks[i].nestingLevel and i != blockIndISC:
# # The deepest ISC Block is not inserted in mappingStackISC
# # because mapping will be called on this block, and
# # it will be inserted by the next iteration of the
# # mapping function. Insert the block in stack, which
# # was previously thought of being deepest.
# if deepestISCBlock != -1:
# mappingStackISC.append(deepestISCBlock)
# deepestISCBlock = i
# deepestISCBlockNestingLevel < cfgISC.listBlocks[i].nestingLevel
# break
# else:
# mappingStackISC.append(i)
# continue
#
# if deepestISCBlock != -1:
# mappingStackObj.pop() # popping blockIndObj from stack, as mapping is called on it again.
# mergedLevelsISC = mergedLevelsISC + cfgISC.listBlocks[deepestISCBlock].nestingLevel - blockISC.nestingLevel
# if mapping(cfgISC, deepestISCBlock, cfgObj, blockIndObj, mergedLevelsISC) == 0:
# for i in range(len(blockObj.mapsTo)-1):
# # pop each entry from ISC to which the blockIndObj maps to
# mappingStackISC.pop()
# return 0 # successful mapping
def map_cfg(listISCFileNames, listObjdumpFileNames, listBinaryFileNames):
global mappingStackISC
global mappingStackObj
listISCFunctions = []
listFunctionNames = []
listObjdumpFunctions = []
# Parse the ISC files
for ISCFileName in listISCFileNames:
listISCFunctions = listISCFunctions + parse_isc(ISCFileName)
for function in listISCFunctions:
listFunctionNames.append(function.functionName)
logging.debug("parsed "+ISCFileName)
# Parse the objdump files
for ObjdumpFileName in listObjdumpFileNames:
(tempListObjdumpFunctions, objdumpLineNumForAddress) = parse_binary(ObjdumpFileName,
listFunctionNames)
listObjdumpFunctions = listObjdumpFunctions + tempListObjdumpFunctions
# Check that we found all functions in ISC in Objdump
if len(listISCFunctions) != len(listObjdumpFunctions):
raise ParseError("all functions in ISC file not found in Objdump file!")
for function in listISCFunctions:
logging.debug("Computing flow for function %s from file %s" % (function.functionName, function.fileName))
function.cfg.computeFlow()
for function in listObjdumpFunctions:
logging.debug("Computing flow for function %s from file %s" % (function.functionName, function.fileName))
function.cfg.computeFlow()
# for funcISC in listISCFunctions:
# funcObj = find(lambda fn: fn.functionName == funcISC.functionName, listObjdumpFunctions)
# display_cfgs(app, funcISC.cfg, funcObj.cfg, "%s" % funcISC.functionName)
for binaryFileName in listBinaryFileNames:
gdbMapping = getGDBMapping(binaryFileName, objdumpLineNumForAddress)
for fnISC in listISCFunctions:
cfgISC = fnISC.cfg
fnObj = find(lambda fn: fn.functionName == fnISC.functionName, listObjdumpFunctions)
cfgObj = fnObj.cfg
mappingStackISC = [(0, [0])]
mappingStackObj = [(0, [0])]
if mapping(cfgISC=cfgISC, blockIndISC=0, cfgObj=cfgObj, blockIndObj=0, mergedLevelsISC=0) == 0:
logging.debug("Mapping Found!!!!")
print mappingStackISC
print mappingStackObj
else:
logging.debug("Fuck my life!!!")
mappingStackISC.pop()
mappingStackObj.pop()
if mappingStackISC or mappingStackObj:
logging.error("*** Stack is not empty after mapping function returns ***")
printDebugMapCFG(listISCFunctions, listObjdumpFunctions, gdbMapping)
#
for funcISC in listISCFunctions:
funcObj = find(lambda fn: fn.functionName == funcISC.functionName, listObjdumpFunctions)
display_cfgs(app, funcISC.cfg, funcObj.cfg, "%s" % funcISC.functionName)
return listISCFunctions, listObjdumpFunctions
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
logging.basicConfig(level=logging.DEBUG)
optparser = OptionParser()
optparser.add_option("-i", "--isc", action="append", dest="listISCFileNames",
type="string", help="ISC Filename. For multiple files, use -i <filename> multiple times.",
metavar="FILE")
optparser.add_option("-o", "--objdump", action="append",
type="string", dest="listObjdumpFileNames",
help="Objdump Filename. For multiple files, use -o <filename> multiple times.",
metavar="FILE")
optparser.add_option("-b", "--binary", action="append",
type="string", dest="listBinaryFileNames",
help="Binary Filename. For multiple files, use -b <filename> multiple times.",
metavar="FILE")
(options, args) = optparser.parse_args()
if (len(args) > 0):
print "Addtional arguments are being ignored"
listISCFileNames = options.listISCFileNames
listObjdumpFileNames = options.listObjdumpFileNames
listBinaryFileNames = options.listBinaryFileNames
map_cfg(listISCFileNames, listObjdumpFileNames, listBinaryFileNames)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment