Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool codeAnalysis(BPatch_addressSpace *app1, BPatch_addressSpace *app2) {
- bool ret = true;
- BPatch_image *app1Image = app1->getImage();
- BPatch_image *app2Image = app2->getImage();
- vector < BPatch_module * >*modules1 = app1Image->getModules ();
- vector < BPatch_module * >*modules2 = app2Image->getModules ();
- vector < BPatch_module * >::iterator module1Iter;
- vector < BPatch_module * >::iterator module2Iter;
- BPatch_module *defaultModule1;
- BPatch_module *defaultModule2;
- for (module1Iter = modules1->begin (); module1Iter != modules1->end (); ++module1Iter) {
- char module1Name[1024];
- (*module1Iter)->getFullName (module1Name, 1024);
- for(module2Iter = modules2->begin(); module2Iter != modules2->end(); ++module2Iter) {
- char module2Name[1024];
- (*module2Iter)->getFullName(module2Name,1024);
- if ((*module2Iter)->isSharedLib ()) {
- if (skipLibraries.find (module2Name) != skipLibraries.end ()) {
- cout << "Skipping library: " << module2Name << endl;
- continue;
- }
- }
- if(strcmp(module1Name, module2Name) == 0) {
- // Located Same Module in both images
- if (string (module1Name).find ("DEFAULT_MODULE") != string::npos) {
- defaultModule1 = (*module1Iter);
- defaultModule2 = (*module2Iter);
- continue; // there is no cfg or functions to walk in the DEFAULT?
- }
- cout << "Comparing found equivalent module: " << module1Name << endl;
- vector < BPatch_function * >*all1Functions = (*module1Iter)->getProcedures ();
- vector < BPatch_function * >*all2Functions = (*module2Iter)->getProcedures ();
- vector < BPatch_function * >::iterator func1Iter;
- vector < BPatch_function * >::iterator func2Iter;
- func1Iter = all1Functions->begin();
- func2Iter = all2Functions->begin();
- std::set<BPatch_basicBlock *> blocks1;
- std::set<BPatch_basicBlock *> blocks2;
- BPatch_flowGraph *fg1, *fg2;
- if(func1Iter != all1Functions->end()) fg1 = (*func1Iter)->getCFG();
- else { cerr << "funt1Iter @end" << endl; continue; }
- if(func2Iter != all2Functions->end()) fg2 = (*func2Iter)->getCFG();
- else { cerr << "funt2Iter @end" << endl; continue; }
- if(fg1 != NULL) fg1->getAllBasicBlocks(blocks1);
- else { cerr << "fgraph1 null" << endl; continue; }
- if(fg2 != NULL) fg2->getAllBasicBlocks(blocks2);
- else { cerr << "fgraph2 null" << endl; continue; }
- std::set<BPatch_basicBlock* >::iterator biter1 = blocks1.begin();
- std::set<BPatch_basicBlock* >::iterator biter2 = blocks2.begin();
- for(;biter1 != blocks1.end(), biter2 != blocks2.end(); ++biter1, ++biter2) {
- vector<boost::shared_ptr<Dyninst::InstructionAPI::Instruction> > instVec1, instVec2;
- bool igetres1 = (*biter1)->getInstructions(instVec1);
- bool igetres2 = (*biter2)->getInstructions(instVec2);
- if(igetres1 && igetres2) {
- instIter1 = instVec1.begin();
- instIter2 = instVec2.begin();
- for(; instIter1 != instVec1.end(), instIter2 != instVec2.end(); instIter1++, instIter2++) {
- vector<InstructionAPI::Operand> operands1, operands2;
- string fmt1, fmt2;
- try {
- fmt1 = (*instIter1).get()->getOperation().format();
- cerr << "debug: format1=" << fmt1 << " ";
- fmt2 = (*instIter2).get()->getOperation().format();
- cerr << "debug: format2=" << fmt2 << " ";
- } catch (std::exception& e) {
- cerr << "Exception on format read, " << e.what() << " " << endl;
- }
- if(strcmp(fmt1.c_str(),"") == 0) {
- cerr << "fmt1 empty string!" << endl;
- }
- if(strcmp(fmt2.c_str(),"") == 0) {
- cerr << "fmt2 empty string!" << endl;
- }
- entryID id1, id2;// = op1.getID();
- try {
- id1 = (*instIter1).get()->getOperation().getID();
- id2 = (*instIter2).get()->getOperation().getID();
- } catch (std::exception& e) {
- cerr << "Exception on getID, " << e.what() << " " << endl;
- }
- prefixEntryID pfid1, pfid2;
- try {
- pfid1 = (*instIter1).get()->getOperation().getPrefixID();
- pfid2 = (*instIter2).get()->getOperation().getPrefixID();
- } catch (std::exception& e) {
- cerr << "Exception on prefixID read, " << e.what() << " ";
- }
- if(!strcmp(fmt1.c_str(),fmt2.c_str()) == 0) {
- cerr << "operand fmts differ "<< fmt1 << "!=" << fmt2;// << endl;
- InstructionAPI::Instruction *in = ((*instIter1).get());
- InstructionAPI::Instruction *in2 = ((*instIter2).get());
- cerr << " " << hex << &(*instIter1) << "=" << hex << in;
- cerr << " " << hex << &(*instIter2) << "=" << hex << in2 << endl;
- ret = false;
- continue;
- }
- if(id1 != id2) {
- cerr << "entryIDs differ " << id1 << "!=" << id2 << endl;
- ret = false;
- continue;
- }
- ((*instIter1).get())->getOperands(operands1);
- ((*instIter2).get())->getOperands(operands2);
- if(operands1.size() != operands2.size()) {
- cerr << "operands count mismatch " << operands1.size() << " != " << operands2.size() <<endl;
- ret = false;
- continue;
- }
- for(unsigned i = 0; i < operands1.size(); i++) {
- string op1fval = operands1[i].format(((*instIter1).get())->getArch());
- string op2fval = operands2[i].format(((*instIter2).get())->getArch());
- cout << "operands[" << i <<"]: " << op1fval << ":" << op2fval <<endl;
- if(!strcmp(op1fval.c_str(),op2fval.c_str())==0) {
- cerr << "operands differ: " << op1fval << " isnt " << op2fval << endl;
- ret = false;
- }
- }
- cout << "formats: " << fmt1 << ", " << fmt2;
- cout << " entryIDs: " << id1 << ", " << id2;
- cout << " prefixEIDs: " << pfid1 << ", " << pfid2;// << endl;
- cout << " ("<< hex << &(*instIter1) << "::" << hex << &(*instIter2) << ")" << endl;
- }
- }
- else cerr << "FALSE reply to getInstructions" << endl;
- }
- }
- }
- }
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement