Ghidra is a software reverse engineering (SRE) framework
GP-7423 Add findCbranch to checkCommonCbranch
caheckman6b05f2a6685d
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.ccindex ad746815f8..9d775a1823 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc@@ -3633,7 +3633,7 @@ int4 ActionDoNothing::apply(Funcdata &data)}}else if (bb->unblockedMulti(0)) {- if (data.isNormalizationOn() || bb->hasNoImmediateCopy(0)) {+ if (data.isNormalizationOn() || data.isJumptableRecoveryOn() || bb->hasNoImmediateCopy(0)) {data.removeDoNothingBlock(bb);count += 1;return 0;diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.ccindex e22e1ab8b5..d319832427 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.cc@@ -1339,34 +1339,57 @@ bool JumpBasic::duplicateVarnodes(const vector<Varnode *> &arr)return true;}+/// \brief Find CBRANCH with an out-edge that flows into the given block and in-edge+///+/// If a CBRANCH controls flow through the given edge, return it, otherwise return null.+/// Flow may be through blocks with 1 input and 1 output edge.+/// \param bl is destination block+/// \param inslot is the in-edge index+/// \param path will hold the out-edge index from the CBRANCH+/// \return the controlling CBRANCH or null+PcodeOp *JumpBasic::findCbranch(BlockBasic *bl,int4 inslot,int4 &path)++{+ BlockBasic *inBlock = (BlockBasic *)bl->getIn(inslot);+ PcodeOp *cbranch = inBlock->lastOp();+ while(cbranch == (PcodeOp *)0 || cbranch->code() != CPUI_CBRANCH) {+ if (inBlock->sizeOut() != 1) return (PcodeOp *)0;+ if (inBlock->sizeIn() != 1) return (PcodeOp *)0;+ inslot = 0;+ bl = inBlock;+ inBlock = (BlockBasic *)bl->getIn(inslot);+ cbranch = inBlock->lastOp();+ }+ path = bl->getInRevIndex(inslot);+ return cbranch;+}+/// All CBRANCHs in addition to flowing to the given block, must also flow to another common block,/// and each boolean value must select between the given block and the common block in the same way.-/// If this flow exists, \b true is returned and the boolean Varnode inputs to each CBRANCH are passed back.-/// \param varArray will hold the input Varnodes being passed back+/// If this flow exists, a representative CBRANCH is returned and the boolean Varnode inputs to each+/// CBRANCH are passed back. Otherwise NULL is returned./// \param bl is the given block-/// \return \b true if the common CBRANCH flow exists across all incoming blocks-bool JumpBasic::checkCommonCbranch(vector<Varnode *> &varArray,BlockBasic *bl)+/// \param varArray will hold the input Varnodes being passed back+/// \param path will hold the out index from the representative CBRANCH to the given block+/// \return a representative CBRANCH or NULL+PcodeOp *JumpBasic::checkCommonCbranch(BlockBasic *bl,vector<Varnode *> &varArray,int4 &path){- BlockBasic *curBlock = (BlockBasic *)bl->getIn(0);- PcodeOp *op = curBlock->lastOp();- if (op == (PcodeOp *)0 || op->code() != CPUI_CBRANCH)- return false;- int4 outslot = bl->getInRevIndex(0);- bool isOpFlip = op->isBooleanFlip();- varArray.push_back(op->getIn(1)); // Pass back boolean input to CBRANCH+ PcodeOp *cbranch = findCbranch(bl, 0, path);+ if (cbranch == (PcodeOp *)0) return cbranch;+ bool isOpFlip = cbranch->isBooleanFlip();+ varArray.push_back(cbranch->getIn(1)); // Pass back boolean input to CBRANCHfor(int4 i=1;i<bl->sizeIn();++i) {- curBlock = (BlockBasic *)bl->getIn(i);- op = curBlock->lastOp();- if (op == (PcodeOp *)0 || op->code() != CPUI_CBRANCH)- return false; // All blocks must end with CBRANCH- if (op->isBooleanFlip() != isOpFlip)- return false;- if (outslot != bl->getInRevIndex(i))- return false; // Boolean value must have some meaning- varArray.push_back(op->getIn(1)); // Pass back boolean input to CBRANCH+ int4 altpath;+ PcodeOp *altBranch = findCbranch(bl,i, altpath);+ if (altBranch == (PcodeOp *)0) return altBranch;+ if (altBranch->isBooleanFlip() != isOpFlip)+ return (PcodeOp *)0;+ if (path != altpath) // Boolean value must be consistent across incoming blocks+ return (PcodeOp *)0;+ varArray.push_back(altBranch->getIn(1)); // Pass back boolean input to CBRANCH}- return true;+ return cbranch;}/// \brief Check for a guard that has been unrolled across multiple blocks@@ -1381,16 +1404,15 @@ bool JumpBasic::checkCommonCbranch(vector<Varnode *> &varArray,BlockBasic *bl)void JumpBasic::checkUnrolledGuard(BlockBasic *bl,int4 maxpullback,bool usenzmask){+ int4 indpath;vector<Varnode *> varArray;- if (!checkCommonCbranch(varArray,bl))- return;- int4 indpath = bl->getInRevIndex(0);+ PcodeOp *cbranch = checkCommonCbranch(bl,varArray,indpath);+ if (cbranch == (PcodeOp *)0) return;bool toswitchval = (indpath == 1);- PcodeOp *cbranch = ((BlockBasic *)bl->getIn(0))->lastOp();if (cbranch->isBooleanFlip())toswitchval = !toswitchval;CircleRange rng(toswitchval);- int4 indpathstore = bl->getIn(0)->getFlipPath() ? 1-indpath : indpath;+ int4 indpathstore = cbranch->getParent()->getFlipPath() ? 1-indpath : indpath;PcodeOp *readOp = cbranch;for(int4 j=0;j<maxpullback;++j) {if (duplicateVarnodes(varArray)) {@@ -1405,7 +1427,7 @@ void JumpBasic::checkUnrolledGuard(BlockBasic *bl,int4 maxpullback,bool usenzmasVarnode *markup; // Throw away markup informationVarnode *vn = varArray[0];if (!vn->isWritten()) break;- PcodeOp *readOp = vn->getDef();+ readOp = vn->getDef();vn = rng.pullBack(readOp,&markup,usenzmask);if (vn == (Varnode *)0) break;if (rng.isEmpty()) break;diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.hhindex 6ce41bcd50..b15062d7f4 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.hh@@ -384,6 +384,7 @@ protected:static int4 getStride(Varnode *vn); ///< Get the step/stride associated with the Varnodestatic uintb backup2Switch(Funcdata *fd,uintb output,Varnode *outvn,Varnode *invn);static bool duplicateVarnodes(const vector<Varnode *> &arr); ///< Return \b true if all array elements are the same Varnode+ static PcodeOp *findCbranch(BlockBasic *bl,int4 inslot,int4 &path);void getInitialRange(Varnode *vn,CircleRange &rng) const; ///< Get initial range of values the given Varnode can takevoid findDeterminingVarnodes(PcodeOp *op,int4 slot);void analyzeGuards(BlockBasic *bl,int4 pathout);@@ -394,7 +395,7 @@ protected:void markFoldableGuards();void markModel(bool val); ///< Mark (or unmark) all PcodeOps involved in the modelbool flowsOnlyToModel(Varnode *vn,PcodeOp *trailOp); ///< Check if the given Varnode flows to anything other than \b this model- bool checkCommonCbranch(vector<Varnode *> &varArray,BlockBasic *bl); ///< Check that all incoming blocks end with a CBRANCH+ PcodeOp *checkCommonCbranch(BlockBasic *bl,vector<Varnode *> &varArray,int4 &path); ///< Check that all incoming blocks end with a CBRANCHvoid checkUnrolledGuard(BlockBasic *bl,int4 maxpullback,bool usenzmask);/// \brief Eliminate the given guard to \b this switch