Advertisement
col_parity

Dyninst Comparator

Dec 2nd, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.80 KB | None | 0 0
  1. bool codeAnalysis(BPatch_addressSpace *app1, BPatch_addressSpace *app2) {
  2.     bool ret = true;
  3.     BPatch_image *app1Image = app1->getImage();
  4.     BPatch_image *app2Image = app2->getImage();
  5.     vector < BPatch_module * >*modules1 = app1Image->getModules ();
  6.     vector < BPatch_module * >*modules2 = app2Image->getModules ();
  7.     vector < BPatch_module * >::iterator module1Iter;
  8.     vector < BPatch_module * >::iterator module2Iter;
  9.     BPatch_module *defaultModule1;
  10.     BPatch_module *defaultModule2;
  11.  
  12.     for (module1Iter = modules1->begin (); module1Iter != modules1->end (); ++module1Iter) {
  13.         char module1Name[1024];
  14.         (*module1Iter)->getFullName (module1Name, 1024);
  15.         for(module2Iter = modules2->begin(); module2Iter != modules2->end(); ++module2Iter) {
  16.         char module2Name[1024];
  17.         (*module2Iter)->getFullName(module2Name,1024);
  18.             if ((*module2Iter)->isSharedLib ()) {
  19.         if (skipLibraries.find (module2Name) != skipLibraries.end ()) {
  20.             cout << "Skipping library: " << module2Name << endl;
  21.             continue;
  22.         }
  23.             }
  24.         if(strcmp(module1Name, module2Name) == 0) {
  25.         // Located Same Module in both images
  26.             if (string (module1Name).find ("DEFAULT_MODULE") != string::npos) {
  27.             defaultModule1 = (*module1Iter);
  28.             defaultModule2 = (*module2Iter);
  29.             continue; // there is no cfg or functions to walk in the DEFAULT?
  30.         }
  31.             cout << "Comparing found equivalent module: " << module1Name << endl;
  32.         vector < BPatch_function * >*all1Functions = (*module1Iter)->getProcedures ();
  33.         vector < BPatch_function * >*all2Functions = (*module2Iter)->getProcedures ();
  34.         vector < BPatch_function * >::iterator func1Iter;
  35.         vector < BPatch_function * >::iterator func2Iter;
  36.         func1Iter = all1Functions->begin();
  37.         func2Iter = all2Functions->begin();
  38.         std::set<BPatch_basicBlock *> blocks1;
  39.         std::set<BPatch_basicBlock *> blocks2;
  40.         BPatch_flowGraph *fg1, *fg2;
  41.         if(func1Iter != all1Functions->end()) fg1 = (*func1Iter)->getCFG();
  42.         else { cerr << "funt1Iter @end" << endl; continue; }
  43.         if(func2Iter != all2Functions->end()) fg2 = (*func2Iter)->getCFG();
  44.         else { cerr << "funt2Iter @end" << endl; continue; }
  45.         if(fg1 != NULL) fg1->getAllBasicBlocks(blocks1);
  46.         else { cerr << "fgraph1 null" << endl; continue; }
  47.         if(fg2 != NULL) fg2->getAllBasicBlocks(blocks2);
  48.         else { cerr << "fgraph2 null" << endl; continue; }
  49.         std::set<BPatch_basicBlock* >::iterator biter1 = blocks1.begin();
  50.         std::set<BPatch_basicBlock* >::iterator biter2 = blocks2.begin();
  51.         for(;biter1 != blocks1.end(), biter2 != blocks2.end(); ++biter1, ++biter2) {
  52.             vector<boost::shared_ptr<Dyninst::InstructionAPI::Instruction> > instVec1, instVec2;
  53.             bool igetres1 = (*biter1)->getInstructions(instVec1);
  54.             bool igetres2 = (*biter2)->getInstructions(instVec2);
  55.             if(igetres1 && igetres2) {
  56.             instIter1 = instVec1.begin();
  57.             instIter2 = instVec2.begin();
  58.             for(; instIter1 != instVec1.end(), instIter2 != instVec2.end(); instIter1++, instIter2++) {
  59.                 vector<InstructionAPI::Operand> operands1, operands2;
  60.                 string fmt1, fmt2;
  61.                 try {
  62.                 fmt1 = (*instIter1).get()->getOperation().format();
  63.                 cerr << "debug: format1=" << fmt1 << " ";
  64.                 fmt2 = (*instIter2).get()->getOperation().format();
  65.                 cerr << "debug: format2=" << fmt2 << " ";
  66.                 } catch (std::exception& e) {
  67.                 cerr << "Exception on format read, " << e.what() << " " << endl;
  68.                 }
  69.                 if(strcmp(fmt1.c_str(),"") == 0) {
  70.                 cerr << "fmt1 empty string!" << endl;
  71.                 }
  72.                 if(strcmp(fmt2.c_str(),"") == 0) {
  73.                 cerr << "fmt2 empty string!" << endl;
  74.                 }
  75.                 entryID id1, id2;// = op1.getID();
  76.                 try {
  77.                 id1 = (*instIter1).get()->getOperation().getID();
  78.                 id2 = (*instIter2).get()->getOperation().getID();
  79.                 } catch (std::exception& e) {
  80.                 cerr << "Exception on getID, " << e.what() << " " << endl;
  81.                 }
  82.                 prefixEntryID pfid1, pfid2;
  83.                 try {
  84.                 pfid1 = (*instIter1).get()->getOperation().getPrefixID();
  85.                 pfid2 = (*instIter2).get()->getOperation().getPrefixID();
  86.                 } catch (std::exception& e) {
  87.                 cerr << "Exception on prefixID read, " << e.what() << " ";
  88.                 }
  89.                 if(!strcmp(fmt1.c_str(),fmt2.c_str()) == 0) {
  90.                 cerr << "operand fmts differ "<< fmt1 << "!=" << fmt2;// << endl;
  91.                 InstructionAPI::Instruction *in = ((*instIter1).get());
  92.                 InstructionAPI::Instruction *in2 = ((*instIter2).get());
  93.                 cerr << " " << hex << &(*instIter1) << "=" << hex << in;
  94.                 cerr << " " << hex << &(*instIter2) << "=" << hex << in2 << endl;
  95.                 ret = false;
  96.                 continue;
  97.                 }
  98.                 if(id1 != id2) {
  99.                 cerr << "entryIDs differ " << id1 << "!=" << id2 << endl;
  100.                 ret = false;
  101.                 continue;
  102.                 }
  103.                 ((*instIter1).get())->getOperands(operands1);
  104.                 ((*instIter2).get())->getOperands(operands2);
  105.                 if(operands1.size() != operands2.size()) {
  106.                 cerr << "operands count mismatch " << operands1.size() << " != " << operands2.size() <<endl;
  107.                 ret = false;
  108.                 continue;
  109.                 }
  110.                 for(unsigned i = 0; i < operands1.size(); i++) {
  111.                 string op1fval = operands1[i].format(((*instIter1).get())->getArch());
  112.                 string op2fval = operands2[i].format(((*instIter2).get())->getArch());
  113.                 cout << "operands[" << i <<"]: " << op1fval << ":" << op2fval <<endl;
  114.                 if(!strcmp(op1fval.c_str(),op2fval.c_str())==0) {
  115.                     cerr << "operands differ: " << op1fval << " isnt " << op2fval << endl;
  116.                     ret = false;
  117.                 }
  118.                 }
  119.                 cout << "formats: " << fmt1 << ", " << fmt2;
  120.                 cout << " entryIDs: " << id1 << ", " << id2;
  121.                 cout << " prefixEIDs: " << pfid1 << ", " << pfid2;// << endl;
  122.                 cout << " ("<< hex << &(*instIter1) << "::" << hex << &(*instIter2) << ")" << endl;
  123.             }
  124.             }
  125.             else cerr << "FALSE reply to getInstructions" << endl;
  126.         }
  127.         }
  128.         }
  129.     }
  130.     return ret;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement