|
|
|
@ -24,14 +24,14 @@
|
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var _ = require('underscore-node'); |
|
|
|
|
var _ = require('underscore-node'), |
|
|
|
|
logger = require('./logger').logger; |
|
|
|
|
|
|
|
|
|
function ControlFlowGraph(rules){ |
|
|
|
|
this.rules = rules; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function seperateCodeFromData(asmArr) { |
|
|
|
|
function separateCodeFromData(asmArr) { |
|
|
|
|
|
|
|
|
|
var isCode = function(asmArr, index){ |
|
|
|
|
return (((asmArr[index].source !== null) && |
|
|
|
@ -63,7 +63,7 @@ function seperateCodeFromData(asmArr) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var gccX86 = { |
|
|
|
|
filterData: seperateCodeFromData, |
|
|
|
|
filterData: separateCodeFromData, |
|
|
|
|
isFunctionEnd:function(x) { return ((x[0] != ' ') && (x[0] != '.') && |
|
|
|
|
(x.indexOf(':') != -1)) ;}, |
|
|
|
|
|
|
|
|
@ -190,36 +190,42 @@ ControlFlowGraph.prototype.splitToCanonicalBasicBlock = function (basicBlock) {
|
|
|
|
|
ControlFlowGraph.prototype.concatInstructions = function (asmArr, first, last) { |
|
|
|
|
|
|
|
|
|
var makeShorter = function(instruction){ |
|
|
|
|
if(instruction.length > 50){ |
|
|
|
|
return instruction.substr(0, 50); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return instruction; |
|
|
|
|
return instruction.substr(0, 50); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if(first == last) return "";//if last -1 is changed to last this line is no longuer needed
|
|
|
|
|
console.log("look"); |
|
|
|
|
var result = ""; |
|
|
|
|
//TODO: include \n for long templated instructions
|
|
|
|
|
while(first != last-1) {//added to delete last \n and handle the last concat outside loop
|
|
|
|
|
console.log(asmArr[first].text); |
|
|
|
|
logger.debug(asmArr[first].text); |
|
|
|
|
result += makeShorter(asmArr[first].text) + "\n"; |
|
|
|
|
++first; |
|
|
|
|
} |
|
|
|
|
//last concat withou \n
|
|
|
|
|
result += makeShorter(asmArr[first].text); |
|
|
|
|
console.log(asmArr[first].text); |
|
|
|
|
logger.debug(asmArr[first].text); |
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
*
|
|
|
|
|
* current function is buggy need investigation |
|
|
|
|
ControlFlowGraph.prototype.concatInstructions = function (asmArr, first, last) { |
|
|
|
|
return _.chain(asmArr.slice(first, last)) |
|
|
|
|
.map(function (x) { return x.text.substr(0, 50); } |
|
|
|
|
.value() |
|
|
|
|
.join("\n")); |
|
|
|
|
};
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
ControlFlowGraph.prototype.makeNodes = function (asmArr, arrOfCanonicalBasicBlock) { |
|
|
|
|
var node = {}; |
|
|
|
|
var nodes = []; |
|
|
|
|
|
|
|
|
|
_.each(arrOfCanonicalBasicBlock, _.bind(function(x){ |
|
|
|
|
console.log("node name:"); |
|
|
|
|
console.log(x.nameId); |
|
|
|
|
logger.debug("node name:"); |
|
|
|
|
logger.debug(x.nameId); |
|
|
|
|
node.id = x.nameId; |
|
|
|
|
node.label = x.nameId + ((x.nameId.indexOf(":") !== -1)? "":":") +"\n"+ |
|
|
|
|
this.concatInstructions(asmArr,x.start, x.end); |
|
|
|
@ -279,28 +285,28 @@ ControlFlowGraph.prototype.makeEdges = function (asmArr, arrOfCanonicalBasicBloc
|
|
|
|
|
|
|
|
|
|
//we have to deal only with jmp destination, jmp instruction are always taken.
|
|
|
|
|
//edge from jump inst
|
|
|
|
|
console.log("jmp"); |
|
|
|
|
logger.debug("jmp"); |
|
|
|
|
targetNode = extractNodeNameFromInstruction(lastInst); |
|
|
|
|
setEdge(edge, x.nameId, targetNode, 'blue'); |
|
|
|
|
|
|
|
|
|
edges.push(JSON.parse(JSON.stringify(edge))); |
|
|
|
|
console.log(edge); |
|
|
|
|
logger.debug(edge); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case conditionalJmpInst: |
|
|
|
|
{ |
|
|
|
|
console.log("condit jmp"); |
|
|
|
|
logger.debug("condit jmp"); |
|
|
|
|
//deal with : branche taken, branch not taken
|
|
|
|
|
targetNode = extractNodeNameFromInstruction(lastInst); |
|
|
|
|
setEdge(edge, x.nameId, targetNode, 'green'); |
|
|
|
|
edges.push(JSON.parse(JSON.stringify(edge))); |
|
|
|
|
console.log(edge); |
|
|
|
|
logger.debug(edge); |
|
|
|
|
|
|
|
|
|
targetNode = hasName(asmArr,x)? asmArr[x.end].text: generateName(x.nameId, x.end); |
|
|
|
|
setEdge(edge, x.nameId, targetNode, "red"); |
|
|
|
|
edges.push(JSON.parse(JSON.stringify(edge))); |
|
|
|
|
|
|
|
|
|
console.log(edge); |
|
|
|
|
logger.debug(edge); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case notRetInst: |
|
|
|
@ -311,17 +317,17 @@ ControlFlowGraph.prototype.makeEdges = function (asmArr, arrOfCanonicalBasicBloc
|
|
|
|
|
var nextNodeName = asmArr[x.end].text; |
|
|
|
|
setEdge(edge, x.nameId, nextNodeName, 'grey'); |
|
|
|
|
edges.push(JSON.parse(JSON.stringify(edge))); |
|
|
|
|
console.log("not ret inst"); |
|
|
|
|
console.log(edge); |
|
|
|
|
logger.debug("not ret inst"); |
|
|
|
|
logger.debug(edge); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
console.log("expect ret instruction or it's variants(rep ret): "+ lastInst); |
|
|
|
|
logger.debug("expect ret instruction or it's variants(rep ret): "+ lastInst); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
console.log(edges); |
|
|
|
|
logger.debug(edges); |
|
|
|
|
|
|
|
|
|
return edges; |
|
|
|
|
}; |
|
|
|
|