Skip to content

Ghidra is a software reverse engineering (SRE) framework

Rename JumpTable mostcommon

caheckman6a3d7cd03e06
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_block.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_block.cc
index 3d733273d4..63be7c2183 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_block.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_block.cc
@@ -610,8 +610,8 @@ void Funcdata::installSwitchDefaults(void)
PcodeOp *indop = jt->getIndirectOp();
BlockBasic *ind = indop->getParent();
// Mark any switch blocks default edge
- if (jt->getMostCommon() != -1) // If a mostcommon was found
- ind->setDefaultSwitch(jt->getMostCommon());
+ if (jt->getDefaultBlock() != -1) // If a default case is present
+ ind->setDefaultSwitch(jt->getDefaultBlock());
}
}
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.cc
index da671f4779..1ef9d7111e 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.cc
@@ -1193,7 +1193,7 @@ bool JumpBasic::foldInOneGuard(Funcdata *fd,GuardRecord &guard,JumpTable *jump)
indpath = 1 - indpath; // get actual path to indirect block
BlockBasic *guardtarget = (BlockBasic *)cbranchblock->getOut(1-indpath);
bool change = false;
- uint4 pos;
+ int4 pos;
// Its possible the guard branch has been converted between the switch recovery and now
if (cbranchblock->sizeOut() != 2) return false; // In which case, we can't fold it in
@@ -1218,7 +1218,7 @@ bool JumpBasic::foldInOneGuard(Funcdata *fd,GuardRecord &guard,JumpTable *jump)
// is a good indicator that there are none
uintb val = ((indpath==0)!=(cbranch->isBooleanFlip())) ? 0 : 1;
fd->opSetInput(cbranch,fd->newConstant(cbranch->getIn(0)->getSize(),val),1);
- jump->setMostCommonBlock(pos); // A guard branch must be most common
+ jump->setDefaultBlock(pos); // A guard branch generally targets the default case
guard.clear();
change = true;
}
@@ -2003,9 +2003,9 @@ Varnode *JumpAssisted::foldInNormalization(Funcdata *fd,PcodeOp *indop)
bool JumpAssisted::foldInGuards(Funcdata *fd,JumpTable *jump)
{
- int4 origVal = jump->getMostCommon();
+ int4 origVal = jump->getDefaultBlock();
jump->setLastAsMostCommon(); // Default case is always the last block
- return (origVal != jump->getMostCommon());
+ return (origVal != jump->getDefaultBlock());
}
JumpModel *JumpAssisted::clone(JumpTable *jt) const
@@ -2148,7 +2148,7 @@ JumpTable::JumpTable(Architecture *g,Address ad)
origmodel = (JumpModel *)0;
indirect = (PcodeOp *)0;
switchVarConsume = ~((uintb)0);
- mostcommon = -1;
+ defaultBlock = -1;
lastBlock = -1;
maxtablesize = 1024;
maxaddsub = 1;
@@ -2169,7 +2169,7 @@ JumpTable::JumpTable(const JumpTable *op2)
origmodel = (JumpModel *)0;
indirect = (PcodeOp *)0;
switchVarConsume = ~((uintb)0);
- mostcommon = -1;
+ defaultBlock = -1;
lastBlock = op2->lastBlock;
maxtablesize = op2->maxtablesize;
maxaddsub = op2->maxaddsub;
@@ -2265,7 +2265,7 @@ int4 JumpTable::getIndexByBlock(const FlowBlock *bl,int4 i) const
void JumpTable::setLastAsMostCommon(void)
{
- mostcommon = lastBlock;
+ defaultBlock = lastBlock;
}
/// This is used to add address targets from guard branches if they are
@@ -2312,7 +2312,7 @@ void JumpTable::switchOver(const FlowInfo &flow)
lastBlock = block2addr.back().blockPosition; // Out-edge of last address in table
sort(block2addr.begin(),block2addr.end());
- mostcommon = -1; // There is no "mostcommon"
+ defaultBlock = -1; // There is no default case initially
int4 maxcount = 1; // If the maxcount is less than 2
vector<IndexPair>::const_iterator iter = block2addr.begin();
while(iter != block2addr.end()) {
@@ -2326,7 +2326,7 @@ void JumpTable::switchOver(const FlowInfo &flow)
iter = nextiter;
if (count > maxcount) {
maxcount = count;
- mostcommon = curPos;
+ defaultBlock = curPos;
}
}
}
@@ -2368,7 +2368,7 @@ void JumpTable::trivialSwitchOver(void)
for(uint4 i=0;i<parent->sizeOut();++i)
block2addr.push_back(IndexPair(i,i)); // Addresses corresponds exactly to out-edges of switch block
lastBlock = parent->sizeOut()-1;
- mostcommon = -1; // There is no "mostcommon"
+ defaultBlock = -1; // Trivial case does not have default case
}
/// The addresses that the raw BRANCHIND op might branch to itself are recovered,
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.hh
index e3a295d412..f8a631ed6e 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.hh
@@ -510,7 +510,7 @@ class JumpTable {
Address opaddress; ///< Absolute address of the BRANCHIND jump
PcodeOp *indirect; ///< CPUI_BRANCHIND linked to \b this jump-table
uintb switchVarConsume; ///< Bits of the switch variable being consumed
- int4 mostcommon; ///< The out-edge corresponding to the most common address in the address table
+ int4 defaultBlock; ///< The out-edge corresponding to the \e default switch destination (-1 = undefined)
int4 lastBlock; ///< Block out-edge corresponding to last entry in the address table
uint4 maxtablesize; ///< Maximum table size we allow to be built (sanity check)
uint4 maxaddsub; ///< Maximum ADDs or SUBs to normalize
@@ -534,7 +534,7 @@ public:
int4 getStage(void) const { return recoverystage; } ///< Return what stage of recovery this jump-table is in.
int4 numEntries(void) const { return addresstable.size(); } ///< Return the size of the address table for \b this jump-table
uintb getSwitchVarConsume(void) const { return switchVarConsume; } ///< Get bits of switch variable consumed by \b this table
- int4 getMostCommon(void) const { return mostcommon; } ///< Get the out-edge corresponding to the most common address table entry
+ int4 getDefaultBlock(void) const { return defaultBlock; } ///< Get the out-edge corresponding to the \e default switch destination
const Address &getOpAddress(void) const { return opaddress; } ///< Get the address of the BRANCHIND for the switch
PcodeOp *getIndirectOp(void) const { return indirect; } ///< Get the BRANCHIND PcodeOp
void setIndirectOp(PcodeOp *ind) { opaddress = ind->getAddr(); indirect = ind; } ///< Set the BRANCHIND PcodeOp
@@ -546,7 +546,7 @@ public:
int4 getIndexByBlock(const FlowBlock *bl,int4 i) const;
Address getAddressByIndex(int4 i) const { return addresstable[i]; } ///< Get the i-th address table entry
void setLastAsMostCommon(void); ///< Set the most common jump-table target to be the last address in the table
- void setMostCommonBlock(uint4 bl) { mostcommon = bl; } ///< Set the most common jump-table target by out-edge
+ void setDefaultBlock(int4 bl) { defaultBlock = bl; } ///< Set out-edge of the switch destination considered to be \e default
void setLoadCollect(bool val) { collectloads = val; } ///< Set whether LOAD records should be collected
void addBlockToSwitch(BlockBasic *bl,uintb lab); ///< Force a given basic-block to be a switch destination
void switchOver(const FlowInfo &flow); ///< Convert absolute addresses to block indices