Commit 238ffdd7 authored by Gaurav Kukreja's avatar Gaurav Kukreja

Created copy of cfg_generator and named it map_cfg

Signed-off-by: Gaurav Kukreja's avatarGaurav Kukreja <gaurav@gauravk.in>
parent d94fa667
#-----------------------------------------------------------------
# map_cfg.py: Map Control Flow Graphs from Binary and ISC
#-----------------------------------------------------------------
from optparse import OptionParser
from cfg_binary import parse_binary, print_debug_binary
from cfg_isc import parse_isc, print_debug_isc
import logging
from collections import deque
listISCFileNames = []
listObjdumpFileNames = []
def map_cfg(listISCFileNames, listObjdumpFileNames):
listISCFunctions = []
listFunctionNames = []
listObjdumpFunctions = []
# Parse the ISC files
for ISCFileName in listISCFileNames:
listISCFunctions = listISCFunctions + parse_isc(ISCFileName)
for function in listISCFunctions:
listFunctionNames.append(function.functionName)
# Parse the objdump files
for ObjdumpFileName in listObjdumpFileNames:
listObjdumpFunctions = listObjdumpFunctions + parse_binary(ObjdumpFileName,
listFunctionNames)
print_debug_isc (listISCFunctions)
print_debug_binary (listObjdumpFunctions)
# 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()
if __name__ == "__main__":
# listISCFileNames = []
# listObjdumpFileNames = []
logging.basicConfig(level=logging.DEBUG)
optparser = OptionParser()
optparser.add_option("-i", "--isc", action="append", dest="listISCFileNames",
type="string", help="ISC Filenamel. 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")
(options, args) = optparser.parse_args()
if (len(args) > 0):
print "Addtional arguments are being ignored"
listISCFileNames = options.listISCFileNames
listObjdumpFileNames = options.listObjdumpFileNames
map_cfg(listISCFileNames, listObjdumpFileNames)
\ No newline at end of file
import sys
from collections import deque
import logging
class ParseError(Exception):
def __init__(self, str):
self.value = str
def __str__(self):
return repr(self.value)
class BBEdge:
def __init__(self, fromBlockIndex, toBlockIndex):
self.fromBlockIndex = fromBlockIndex;
self.toBlockIndex = toBlockIndex;
class BasicBlock:
def __init__(self, startLine, endLine, isReturning=0, listFuncCalls = None, name = None):
if name is None:
self.name = ""
else:
self.name = name
self.startLine = startLine
self.endLine = endLine
self.isReturning = isReturning
if listFuncCalls is None:
self.listFunctionCalls = []
else:
self.listFunctionCalls = listFuncCalls
self.flow = 0.0
class ControlFlowGraph:
def __init__(self, listBlocks, listEdges):
self.listBlocks = listBlocks
self.listEdges = listEdges
self.listBackEdges = []
def successorBlocks(self, blockIndex):
listSuccBlockIndices = []
for edge in self.listEdges:
if edge.fromBlockIndex == blockIndex:
listSuccBlockIndices.append(edge.toBlockIndex)
#logging.debug("Returns from successorBlocks(blockIndex)")
return listSuccBlockIndices
def successorBlocksWOBackEdges(self, blockIndex):
listSuccBlockIndices = []
edgeIndex = 0
for edge in self.listEdges:
if edge.fromBlockIndex == blockIndex and edgeIndex not in self.listBackEdges:
listSuccBlockIndices.append(edge.toBlockIndex)
edgeIndex = edgeIndex + 1
return listSuccBlockIndices
def successorBlocks(self, blockIndex):
listSuccBlockIndices = []
for edge in self.listEdges:
if edge.fromBlockIndex == blockIndex:
listSuccBlockIndices.append(edge.toBlockIndex)
return listSuccBlockIndices
def predecessorBlocksWOBackEdges(self, blockIndex):
listPredBlockIndices = []
edgeIndex = 0
for edge in self.listEdges:
if edge.toBlockIndex == blockIndex and edgeIndex not in self.listBackEdges:
listPredBlockIndices.append(edge.toBlockIndex)
edgeIndex = edgeIndex + 1
logging.debug("returns from predecessorBlocksWOBackEdges(blockIndex)")
return listPredBlockIndices
def findEdgeIndex(self, fromBlockIndex, toBlockIndex):
'''
Returns index of edge in listEdges with given starting and ending blocks
Return -1 if edge not found
'''
index = -1
for edge in self.listEdges:
index = index + 1
if edge.fromBlockIndex == fromBlockIndex and edge.toBlockIndex == toBlockIndex:
break
return index
# TODO Use a better name for this
def dft(self, blockIndex):
self.dft_stack.append(blockIndex)
for succBlock in self.successorBlocksWOBackEdges(blockIndex):
if succBlock not in self.dft_stack:
self.dft(succBlock)
else:
self.listBackEdges.append(self.findEdgeIndex(blockIndex, succBlock))
self.dft_stack.pop()
def findBackEdges(self):
'''
Populate the list of Back Edges in the graph by traversing in
Depth First Fashion
'''
self.dft_stack = []
self.dft(0)
self.listBackEdges = list(set(self.listBackEdges))
for edgeIndex in self.listBackEdges:
print self.listEdges[edgeIndex].fromBlockIndex, self.listEdges[edgeIndex].toBlockIndex
return self.listBackEdges
def computeFlow(self):
'''
Compute the flow value of each block in CFG of each function
'''
currBlockPredNotVisited = 0
queuePending = deque([])
self.findBackEdges()
self.listBlocks[0].flow = 1.0
for blockIndex in self.successorBlocksWOBackEdges(0):
queuePending.append(blockIndex)
while queuePending:
currBlockIndex = queuePending.popleft()
currBlockFlow = 0.0
for predBlockIndex in self.predecessorBlocksWOBackEdges(currBlockIndex):
if self.listBlocks[predBlockIndex].flow == 0:
# predecessor block still not visited
# add the current block back into queue, set flag and break from loop.
queuePending.append(currBlockIndex)
currBlockPredNotVisited = 1
break
else:
currBlockFlow = currBlockFlow + (self.listBlocks[predBlockIndex].flow / len(self.successorBlocksWOBackEdges(predBlockIndex)))
if currBlockPredNotVisited == 1:
currBlockPredNotVisited = 0
continue
else:
if currBlockFlow > 1.0:
raise ParseError("Flow in graph greater than 1!!")
exit(1)
else:
self.listBlocks[currBlockIndex].flow = currBlockFlow
class FunctionDesc:
def __init__(self, functionName, fileName, startLine, endLine, cfg):
self.functionName = functionName
self.fileName = fileName
self.startLine = startLine
self.endLine = endLine
self.cfg = cfg
\ No newline at end of file
File added
This diff is collapsed.
This diff is collapsed.
/***********************************************************
Intermediate representation of
sieve/app_dir/erat_sieve_no_print.c
Converted by ir2c v0.1
***********************************************************/
#include <limits.h>
#include <stdint.h>
#include "ir2c.h"
#include <stdio.h>
#define N 500000
unsigned int results[N];
struct test {
unsigned int v;
unsigned int k;
} m = { 1, 1 };
void sieve() {
uintptr_t ivtmp_84;
int j_82;
int i_81;
uintptr_t ivtmp_77;
uintptr_t D_2271;
uintptr_t ivtmp_67;
uintptr_t D_2260;
uintptr_t ivtmp_58;
uintptr_t D_2248;
uintptr_t ivtmp_45;
int j;
int i;
unsigned int sieve[500000];
sievebb_2:
// # PRED: ENTRY [100.0%] (fallthru,exec)
ivtmp_77 = 0;
// # SUCC: 3 [100.0%] (fallthru,exec)
sievebb_3:
// # PRED: 3 [99.0%] (dfs_back,true,exec) 2 [100.0%] (fallthru,exec)
*(unsigned int*)((uintptr_t)&results + (uintptr_t)ivtmp_77) = 0;
*(unsigned int*)((uintptr_t)&sieve + (uintptr_t)ivtmp_77) = 1;
ivtmp_77 = ivtmp_77 + 4;
if (ivtmp_77 != 2000000)
goto sievebb_3;
else
goto sievebb_15;
// # SUCC: 3 [99.0%] (dfs_back,true,exec) 15 [1.0%] (false,exec)
sievebb_15:
// # PRED: 3 [1.0%] (false,exec)
ivtmp_84 = 6;
ivtmp_67 = 4;
i_81 = 2;
// # SUCC: 4 [100.0%] (fallthru)
sievebb_4:
// # PRED: 15 [100.0%] (fallthru) 7 [99.0%] (dfs_back,true,exec)
D_2271 = (unsigned int) i_81;
if (*(unsigned int*)((uintptr_t)&sieve + (uintptr_t)D_2271 * 4) != 0)
goto sievebb_5;
else
goto sievebb_7;
// # SUCC: 5 [50.0%] (true,exec) 7 [50.0%] (false,exec)
sievebb_5:
// # PRED: 4 [50.0%] (true,exec)
j_82 = (int) ivtmp_67;
if (j_82 <= 499999)
goto sievebb_16;
else
goto sievebb_7;
// # SUCC: 16 [91.0%] (true,exec) 7 [9.0%] (false,exec)
sievebb_16:
// # PRED: 5 [91.0%] (true,exec)
ivtmp_58 = ivtmp_84;
// # SUCC: 6 [100.0%] (fallthru)
sievebb_6:
// # PRED: 6 [91.0%] (dfs_back,true,exec) 16 [100.0%] (fallthru)
sieve[j_82] = 0;
D_2260 = (unsigned int) j_82 + D_2271;
j_82 = (int) D_2260;
ivtmp_58 = ivtmp_58 + D_2271;
if ((int) (ivtmp_58 - D_2271) <= 499999)
goto sievebb_6;
else
goto sievebb_7;
// # SUCC: 6 [91.0%] (dfs_back,true,exec) 7 [9.0%] (false,exec)
sievebb_7:
// # PRED: 4 [50.0%] (false,exec) 6 [9.0%] (false,exec) 5 [9.0%] (false,exec)
i_81 = i_81 + 1;
ivtmp_67 = ivtmp_67 + 2;
ivtmp_84 = ivtmp_84 + 3;
if (i_81 * i_81 <= 499999)
goto sievebb_4;
else
goto sievebb_8;
// # SUCC: 4 [99.0%] (dfs_back,true,exec) 8 [1.0%] (false,exec)
sievebb_8:
// # PRED: 7 [1.0%] (false,exec)
j = 2;
i = 0;
// # SUCC: 9 [100.0%] (fallthru,exec)
sievebb_9:
// # PRED: 11 [99.0%] (dfs_back,true,exec) 8 [100.0%] (fallthru,exec)
D_2248 = (unsigned int) j;
if (*(unsigned int*)((uintptr_t)&sieve + (uintptr_t)D_2248 * 4) != 0)
goto sievebb_10;
else
goto sievebb_11;
// # SUCC: 10 [50.0%] (true,exec) 11 [50.0%] (false,exec)
sievebb_10:
// # PRED: 9 [50.0%] (true,exec)
results[i] = D_2248;
i = i + 1;
// # SUCC: 11 [100.0%] (fallthru,exec)
sievebb_11:
// # PRED: 9 [50.0%] (false,exec) 10 [100.0%] (fallthru,exec)
j = j + 1;
if (j != 500000)
goto sievebb_9;
else
goto sievebb_17;
// # SUCC: 9 [99.0%] (dfs_back,true,exec) 17 [1.0%] (false,exec)
sievebb_17:
// # PRED: 11 [1.0%] (false,exec)
ivtmp_45 = 0;
// # SUCC: 12 [100.0%] (fallthru)
sievebb_12:
// # PRED: 17 [100.0%] (fallthru) 13 [98.9%] (dfs_back,true,exec)
if (*(unsigned int*)((uintptr_t)&results + (uintptr_t)ivtmp_45) == 0)
goto sievebb_14;
else
goto sievebb_13;
// # SUCC: 14 [4.5%] (true,exec) 13 [95.5%] (false,exec)
sievebb_13:
// # PRED: 12 [95.5%] (false,exec)
ivtmp_45 = ivtmp_45 + 4;
if (ivtmp_45 != 2000000)
goto sievebb_12;
else
goto sievebb_14;
// # SUCC: 12 [98.9%] (dfs_back,true,exec) 14 [1.1%] (false,exec)
sievebb_14:
// # PRED: 12 [4.5%] (true,exec) 13 [1.1%] (false,exec)
m.v = 0;
return;
// # SUCC: EXIT [100.0%]
}
int main(void) {
mainbb_2:
// # PRED: ENTRY [100.0%] (fallthru,exec)
sieve ();
return 0;
// # SUCC: EXIT [100.0%]
}
/***********************************************************
Intermediate representation of
simple/app_dir/simple.c
Converted by ir2c v0.1
***********************************************************/
#include <limits.h>
#include <stdint.h>
#include "ir2c.h"
#define FIB_MAX_NUM 15
int a=0, b=1;
int i;
int main(int argc, char* argv) {
int prephitmp_52;
int prephitmp_51;
int prephitmp_50;
uintptr_t ivtmp_43;
int prephitmp_28;
mainbb_2:
// # PRED: ENTRY [100.0%] (fallthru,exec)
prephitmp_51 = b;
prephitmp_50 = a;
ivtmp_43 = 0;
// # SUCC: 3 [100.0%] (fallthru,exec)
mainbb_3:
// # PRED: 5 [100.0%] (fallthru) 2 [100.0%] (fallthru,exec)
prephitmp_28 = prephitmp_50 + prephitmp_51;
ivtmp_43 = ivtmp_43 + 1;
if (ivtmp_43 != 98)
goto mainbb_5;
else
goto mainbb_6;
// # SUCC: 5 [91.0%] (dfs_back,true,exec) 6 [9.0%] (false,exec)
mainbb_5:
// # PRED: 3 [91.0%] (dfs_back,true,exec)
prephitmp_50 = prephitmp_51;
prephitmp_51 = prephitmp_28;
goto mainbb_3;
// # SUCC: 3 [100.0%] (fallthru)
mainbb_6:
// # PRED: 3 [9.0%] (false,exec)
prephitmp_52 = prephitmp_28;
// # SUCC: 4 [100.0%] (fallthru)
mainbb_4:
// # PRED: 6 [100.0%] (fallthru)
b = prephitmp_28;
a = prephitmp_51;
i = 101;
return prephitmp_52;
// # SUCC: EXIT [100.0%]
}
#-----------------------------------------------------------------
# map_cfg.py: Map Control Flow Graphs from Binary and ISC
#-----------------------------------------------------------------
from optparse import OptionParser
from cfg_binary import parse_binary, print_debug_binary
from cfg_isc import parse_isc, print_debug_isc
import logging
from collections import deque
listISCFileNames = []
listObjdumpFileNames = []
def map_cfg(listISCFileNames, listObjdumpFileNames):
listISCFunctions = []
listFunctionNames = []
listObjdumpFunctions = []
# Parse the ISC files
for ISCFileName in listISCFileNames:
listISCFunctions = listISCFunctions + parse_isc(ISCFileName)
for function in listISCFunctions:
listFunctionNames.append(function.functionName)
# Parse the objdump files
for ObjdumpFileName in listObjdumpFileNames:
listObjdumpFunctions = listObjdumpFunctions + parse_binary(ObjdumpFileName,
listFunctionNames)
print_debug_isc (listISCFunctions)
print_debug_binary (listObjdumpFunctions)
# 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()
if __name__ == "__main__":
# listISCFileNames = []
# listObjdumpFileNames = []
logging.basicConfig(level=logging.DEBUG)
optparser = OptionParser()
optparser.add_option("-i", "--isc", action="append", dest="listISCFileNames",
type="string", help="ISC Filenamel. 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")
(options, args) = optparser.parse_args()
if (len(args) > 0):
print "Addtional arguments are being ignored"
listISCFileNames = options.listISCFileNames
listObjdumpFileNames = options.listObjdumpFileNames
map_cfg(listISCFileNames, listObjdumpFileNames)
\ 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