Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-7003 Use non-zero mask in jumptable recovery

caheckman64606271a560
diff --git a/Ghidra/Features/Decompiler/certification.manifest b/Ghidra/Features/Decompiler/certification.manifest
index ab4d90c4ba..1fd0b4c13f 100644
--- a/Ghidra/Features/Decompiler/certification.manifest
+++ b/Ghidra/Features/Decompiler/certification.manifest
@@ -88,6 +88,7 @@ src/decompile/datatests/statuscmp.xml||GHIDRA||||END|
src/decompile/datatests/switchhide.xml||GHIDRA||||END|
src/decompile/datatests/switchind.xml||GHIDRA||||END|
src/decompile/datatests/switchloop.xml||GHIDRA||||END|
+src/decompile/datatests/switchmask.xml||GHIDRA||||END|
src/decompile/datatests/switchmulti.xml||GHIDRA||||END|
src/decompile/datatests/switchreturn.xml||GHIDRA||||END|
src/decompile/datatests/threedim.xml||GHIDRA||||END|
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
index 8e2af7ad46..990176252b 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
@@ -1280,6 +1280,62 @@ int4 ActionDeindirect::apply(Funcdata &data)
return 0;
}
+/// \brief Try to eliminate constant extended to be larger than sizeof(uintb)
+///
+/// If given varnode is defined as zext(#0), simplify operations that read it.
+/// \param vn is the big varnode
+/// \param data is the function
+void ActionVarnodeProps::handleExtendedZero(Varnode *vn,Funcdata &data)
+
+{
+ PcodeOp *zext = vn->getDef();
+ if (zext->code() != CPUI_INT_ZEXT) return;
+ if (!zext->getIn(0)->constantMatch(0)) return;
+ list<PcodeOp *>::const_iterator iter;
+ iter = vn->beginDescend();
+ while(iter != vn->endDescend()) {
+ PcodeOp *subop = *iter;
+ ++iter;
+ switch(subop->code()) {
+ case CPUI_INT_ADD:
+ case CPUI_INT_OR:
+ case CPUI_INT_XOR:
+ data.opRemoveInput(subop, subop->getSlot(vn)); // Convert to COPY of other slot
+ data.opSetOpcode(subop, CPUI_COPY);
+ iter = vn->beginDescend();
+ count += 1;
+ break;
+ case CPUI_INT_2COMP:
+ data.opSetOpcode(subop, CPUI_COPY);
+ count += 1;
+ break;
+ case CPUI_INT_LEFT:
+ case CPUI_INT_RIGHT:
+ case CPUI_INT_SRIGHT:
+ case CPUI_INT_DIV:
+ case CPUI_INT_SDIV:
+ case CPUI_INT_REM:
+ case CPUI_INT_SREM:
+ if (subop->getIn(0) != vn) // Check if zero is first input
+ break;
+ data.opRemoveInput(subop, 1); // Convert to COPY of zero
+ data.opSetOpcode(subop, CPUI_COPY);
+ iter = vn->beginDescend();
+ count += 1;
+ break;
+ case CPUI_INT_MULT:
+ case CPUI_INT_AND:
+ data.opRemoveInput(subop, 1-subop->getSlot(vn)); // Convert to COPY of zero
+ data.opSetOpcode(subop, CPUI_COPY);
+ iter = vn->beginDescend();
+ count += 1;
+ break;
+ default:
+ break;
+ }
+ }
+}
+
int4 ActionVarnodeProps::apply(Funcdata &data)
{
@@ -1325,10 +1381,14 @@ int4 ActionVarnodeProps::apply(Funcdata &data)
if (data.replaceVolatile(vn))
count += 1; // Try to replace vn with pcode op
}
- else if (((vn->getNZMask() & vn->getConsume())==0)&&(vnSize<=sizeof(uintb))) {
+ else if ((vn->getNZMask() & vn->getConsume())==0) {
// FIXME: uintb should be arbitrary precision
if (vn->isConstant()) continue; // Don't replace a constant
if (vn->isWritten()) {
+ if (vnSize > sizeof(uintb)) {
+ handleExtendedZero(vn, data);
+ continue;
+ }
if (vn->getDef()->code() == CPUI_COPY) {
if (vn->getDef()->getIn(0)->isConstant()) {
// Don't replace a COPY 0, with a zero, let
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hh
index 55b8793869..b1cc32f523 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hh
@@ -220,6 +220,7 @@ public:
/// - Volatile Varnodes are converted read/write functions
/// - Varnodes whose values are not consumed are replaced with constant 0 Varnodes
class ActionVarnodeProps : public Action {
+ void handleExtendedZero(Varnode *vn,Funcdata &data);
public:
ActionVarnodeProps(const string &g) : Action(0,"varnodeprops",g) {} ///< Constructor
virtual Action *clone(const ActionGroupList &grouplist) const {
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.cc
index b4a2a5e766..f0d81cac65 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.cc
@@ -386,11 +386,11 @@ JumpValues *JumpValuesRangeDefault::clone(void) const
return res;
}
-bool JumpModelTrivial::recoverModel(Funcdata *fd,PcodeOp *indop,uint4 matchsize,uint4 maxtablesize)
+bool JumpModelTrivial::recoverModel(Funcdata *fd,PcodeOp *indop,const JumpModel *previous,uint4 maxtablesize)
{
size = indop->getParent()->sizeOut();
- return ((size != 0)&&(size<=matchsize));
+ return ((size != 0)&&(size<=jumptable->numEntries()));
}
void JumpModelTrivial::buildAddresses(Funcdata *fd,PcodeOp *indop,vector<Address> &addresstable,
@@ -505,44 +505,58 @@ uintb JumpBasic::backup2Switch(Funcdata *fd,uintb output,Varnode *outvn,Varnode
return output;
}
-/// If the Varnode has a restricted range due to masking via INT_AND, the maximum value of this range is returned.
-/// Otherwise, 0 is returned, indicating that the Varnode can take all possible values.
/// \param vn is the given Varnode
-/// \return the maximum value or 0
-uintb JumpBasic::getMaxValue(Varnode *vn)
+/// \param rng passes back the initial range
+void JumpBasic::getInitialRange(Varnode *vn,CircleRange &rng) const
{
- uintb maxValue = 0; // 0 indicates maximum possible value
- if (!vn->isWritten())
- return maxValue;
+ if (vn->isConstant()) {
+ rng = CircleRange(vn->getOffset(),vn->getSize());
+ return;
+ }
+ if (vn->isWritten() && vn->getDef()->isBoolOutput()) {
+ rng = CircleRange(0,2,1,1); // Only 0 or 1 possible
+ return;
+ }
+ int4 step = getStride(vn);
+ if (!vn->isWritten()) {
+ rng = CircleRange(0,0,vn->getSize(),step);
+ return;
+ }
+ if (jumptable->isPartial())
+ rng.setRange(0,0,vn->getSize(),step); // Don't restrict with non-zero mask if we restricted too far previously
+ else
+ rng.setRange(0,vn->getNZMask() + step, vn->getSize(),step);
PcodeOp *op = vn->getDef();
if (op->code() == CPUI_INT_AND) {
- Varnode *constvn = op->getIn(1);
- if (constvn->isConstant()) {
- maxValue = coveringmask( constvn->getOffset() );
- maxValue = (maxValue + 1) & calc_mask(vn->getSize());
- }
- }
- else if (op->code() == CPUI_MULTIEQUAL) { // Its possible the AND is duplicated across multiple blocks
- int4 i;
- for(i=0;i<op->numInput();++i) {
- Varnode *subvn = op->getIn(i);
- if (!subvn->isWritten()) break;
- PcodeOp *andOp = subvn->getDef();
- if (andOp->code() != CPUI_INT_AND) break;
- Varnode *constvn = andOp->getIn(1);
- if (!constvn->isConstant()) break;
- if (maxValue < constvn->getOffset())
- maxValue = constvn->getOffset();
- }
- if (i == op->numInput()) {
- maxValue = coveringmask( maxValue );
- maxValue = (maxValue + 1) & calc_mask(vn->getSize());
+ Varnode *cvn = op->getIn(1);
+ if (cvn->isConstant()) {
+ CircleRange tmprange(0,cvn->getOffset()+step,vn->getSize(),step);
+ if (tmprange.getSize() < rng.getSize())
+ rng = tmprange;
+ }
+ }
+ else if (op->code() == CPUI_INT_REM) {
+ Varnode *cvn = op->getIn(1);
+ if (cvn->isConstant()) {
+ CircleRange tmprange(0,cvn->getOffset(),vn->getSize(),step);
+ if (tmprange.getSize() < rng.getSize())
+ rng = tmprange;
+ }
+ }
+ else if (op->code() == CPUI_INT_SREM) {
+ Varnode *cvn = op->getIn(1);
+ if (cvn->isConstant()) {
+ uintb val = cvn->getOffset();
+ if (signbit_negative(val, cvn->getSize())) {
+ val = uintb_negate(val,cvn->getSize());
+ val += 1;
+ }
+ CircleRange tmprange(-val+step,val,vn->getSize(),step);
+ if (tmprange.getSize() < rng.getSize())
+ rng = tmprange;
}
- else
- maxValue = 0;
}
- return maxValue;
}
/// \brief Calculate the initial set of Varnodes that might be switch variables
@@ -1135,17 +1149,7 @@ void JumpBasic::analyzeGuards(BlockBasic *bl,int4 pathout)
void JumpBasic::calcRange(Varnode *vn,CircleRange &rng) const
{
- // Get an initial range, based on the size/type of -vn-
- int4 stride = 1;
- if (vn->isConstant())
- rng = CircleRange(vn->getOffset(),vn->getSize());
- else if (vn->isWritten() && vn->getDef()->isBoolOutput())
- rng = CircleRange(0,2,1,1); // Only 0 or 1 possible
- else { // Should we go ahead and use nzmask in all cases?
- uintb maxValue = getMaxValue(vn);
- stride = getStride(vn);
- rng = CircleRange(0,maxValue,vn->getSize(),stride);
- }
+ getInitialRange(vn,rng);
// Intersect any guard ranges which apply to -vn-
int4 bitsPreserved;
@@ -1163,49 +1167,73 @@ void JumpBasic::calcRange(Varnode *vn,CircleRange &rng) const
// in which case the guard might not check for it. If the
// size is too big, we try only positive values
if (rng.getSize() > 0x10000) {
- CircleRange positive(0,(rng.getMask()>>1)+1,vn->getSize(),stride);
+ CircleRange positive(0,(rng.getMask()>>1)+1,vn->getSize(),rng.getStep());
positive.intersect(rng);
if (!positive.isEmpty())
rng = positive;
}
}
+/// \brief Return \b true if the \b newRange is preferred over the \b oldRange
+///
+/// If the ranges are equal keep the old. Prefer a range with a smaller step,min,or size.
+/// \param oldRange is the old range
+/// \param newRange is the new range
+/// \return \b true if the new range is preferred
+bool JumpBasic::isPreferredRange(int4 pos,const CircleRange &newRange)
+
+{
+ const CircleRange &oldRange(jrange->getRange());
+ if (oldRange.getStep() != newRange.getStep())
+ return (newRange.getStep() < oldRange.getStep());
+ if (oldRange.getMin() != newRange.getMin())
+ return (newRange.getMin() < oldRange.getMin());
+ int4 oldSize = pathMeld.getVarnode(varnodeIndex)->getSize();
+ int4 newSize = pathMeld.getVarnode(pos)->getSize();
+ return (newSize < oldSize);
+}
+
/// \brief Find the putative switch variable with the smallest range of values reaching the switch
///
/// The Varnode with the smallest range and closest to the BRANCHIND is assumed to be the normalized
/// switch variable. If an expected range size is provided, it is used to \e prefer a particular
/// Varnode as the switch variable. Whatever Varnode is selected,
/// the JumpValue object is set up to iterator over its range.
-/// \param matchsize optionally gives an expected size of the range, or it can be 0
-void JumpBasic::findSmallestNormal(uint4 matchsize)
+/// \param previous (if not NULL) is a previously recovered model for the same switch
+void JumpBasic::findSmallestNormal(const JumpBasic *previous)
{
CircleRange rng;
+ CircleRange expectedRange;
uintb sz,maxsize;
+ if (previous != (const JumpBasic *)0 && previous->jrange != (JumpValuesRange *)0) {
+ expectedRange = previous->jrange->getRange();
+ }
varnodeIndex = 0;
calcRange(pathMeld.getVarnode(0),rng);
- jrange->setRange(rng);
- jrange->setStartVn(pathMeld.getVarnode(0));
- jrange->setStartOp(pathMeld.getOp(0));
+ jrange->setRange(rng,pathMeld.getVarnode(0),pathMeld.getOp(0));
maxsize = rng.getSize();
for(uint4 i=1;i<pathMeld.numCommonVarnode();++i) {
- if (maxsize == matchsize) // Found variable that gives (already recovered) size
+ if (rng == expectedRange) // Found variable that gives (already recovered) range
return;
calcRange(pathMeld.getVarnode(i),rng);
sz = rng.getSize();
- if (sz < maxsize) {
- // Don't accept a 1-byte switch variable unless there is an explicit guard
- // or a table lookup between the byte and the indirect jump.
- // "goto *(#const + byteVar)" should not be interpreted as 256 case switch
- if (sz != 256 || pathMeld.getVarnode(i)->getSize()!=1 || pathMeld.isLoadInPath(i)) {
- varnodeIndex = i;
- maxsize = sz;
- jrange->setRange(rng);
- jrange->setStartVn(pathMeld.getVarnode(i));
- jrange->setStartOp(pathMeld.getEarliestOp(i));
- }
+ if (sz > maxsize) continue;
+ if (sz == maxsize) { // If range sizes are the same
+ if (!isPreferredRange(i, rng)) // Prefer a smaller step, min, or size
+ continue;
}
+ varnodeIndex = i;
+ maxsize = sz;
+ jrange->setRange(rng,pathMeld.getVarnode(i),pathMeld.getEarliestOp(i));
+ }
+ if (maxsize == 256 && pathMeld.getVarnode(varnodeIndex)->getSize()==1 && !pathMeld.isLoadInPath(varnodeIndex)) {
+ // Don't accept a 1-byte switch variable unless there is an explicit guard
+ // or a table lookup between the byte and the indirect jump.
+ // "goto *(#const + byteVar)" should not be interpreted as 256 case switch
+ rng.setFull(pathMeld.getVarnode(0)->getSize());
+ jrange->setRange(rng,pathMeld.getVarnode(0),pathMeld.getOp(0));
}
}
@@ -1216,15 +1244,15 @@ void JumpBasic::findSmallestNormal(uint4 matchsize)
/// \param fd is the function containing the switch
/// \param rootbl is the basic-block
/// \param pathout is the (optional) path to the BRANCHIND or -1
-/// \param matchsize is an (optional) size to expect for the normalized switch variable range
+/// \param previous is an (optional) previous model to compare with (or NULL)
/// \param maxtablesize is the maximum size expected for the normalized switch variable range
-void JumpBasic::findNormalized(Funcdata *fd,BlockBasic *rootbl,int4 pathout,uint4 matchsize,uint4 maxtablesize)
+void JumpBasic::findNormalized(Funcdata *fd,BlockBasic *rootbl,int4 pathout,const JumpModel *previous,uint4 maxtablesize)
{
uintb sz;
analyzeGuards(rootbl,pathout);
- findSmallestNormal(matchsize);
+ findSmallestNormal(dynamic_cast<const JumpBasic *>(previous));
sz = jrange->getSize();
if ((sz > maxtablesize)&&(pathMeld.numCommonVarnode()==1)) {
// Check for jump through readonly variable
@@ -1242,9 +1270,7 @@ void JumpBasic::findNormalized(Funcdata *fd,BlockBasic *rootbl,int4 pathout,uint
MemoryImage mem(vn->getSpace(),4,16,glb->loader);
uintb val = mem.getValue(vn->getOffset(),vn->getSize());
varnodeIndex = 0;
- jrange->setRange(CircleRange(val,vn->getSize()));
- jrange->setStartVn(vn);
- jrange->setStartOp(pathMeld.getOp(0));
+ jrange->setRange(CircleRange(val,vn->getSize()),vn,pathMeld.getOp(0));
}
}
}
@@ -1432,7 +1458,7 @@ JumpBasic::~JumpBasic(void)
delete jrange;
}
-bool JumpBasic::recoverModel(Funcdata *fd,PcodeOp *indop,uint4 matchsize,uint4 maxtablesize)
+bool JumpBasic::recoverModel(Funcdata *fd,PcodeOp *indop,const JumpModel *previous,uint4 maxtablesize)
{
// Basically there needs to be a straight line calculation from a switch variable to the final
@@ -1441,7 +1467,7 @@ bool JumpBasic::recoverModel(Funcdata *fd,PcodeOp *indop,uint4 matchsize,uint4 m
// location.
jrange = new JumpValuesRange();
findDeterminingVarnodes(indop,0);
- findNormalized(fd,indop->getParent(),-1,matchsize,maxtablesize);
+ findNormalized(fd,indop->getParent(),-1,previous,maxtablesize);
if (jrange->getSize() > maxtablesize)
return false;
markFoldableGuards();
@@ -1677,7 +1703,7 @@ void JumpBasic2::initializeStart(const PathMeld &pMeld)
origPathMeld.set(pMeld);
}
-bool JumpBasic2::recoverModel(Funcdata *fd,PcodeOp *indop,uint4 matchsize,uint4 maxtablesize)
+bool JumpBasic2::recoverModel(Funcdata *fd,PcodeOp *indop,const JumpModel *previous,uint4 maxtablesize)
{ // Try to recover a jumptable using the second model
// Basically there is a guard on the main switch variable,
@@ -1719,7 +1745,7 @@ bool JumpBasic2::recoverModel(Funcdata *fd,PcodeOp *indop,uint4 matchsize,uint4
jdef->setDefaultOp(origPathMeld.getOp(origPathMeld.numOps()-1));
findDeterminingVarnodes(multiop,1-path);
- findNormalized(fd,rootbl,pathout,matchsize,maxtablesize);
+ findNormalized(fd,rootbl,pathout,previous,maxtablesize);
if (jrange->getSize() > maxtablesize)
return false; // We didn't find a good range
@@ -1966,7 +1992,7 @@ void JumpBasicOverride::clearCopySpecific(void)
switchvn = (Varnode *)0;
}
-bool JumpBasicOverride::recoverModel(Funcdata *fd,PcodeOp *indop,uint4 matchsize,uint4 maxtablesize)
+bool JumpBasicOverride::recoverModel(Funcdata *fd,PcodeOp *indop,const JumpModel *previous,uint4 maxtablesize)
{
clearCopySpecific();
@@ -2105,7 +2131,7 @@ void JumpBasicOverride::decode(Decoder &decoder)
throw LowlevelError("Empty jumptable override");
}
-bool JumpAssisted::recoverModel(Funcdata *fd,PcodeOp *indop,uint4 matchsize,uint4 maxtablesize)
+bool JumpAssisted::recoverModel(Funcdata *fd,PcodeOp *indop,const JumpModel *previous,uint4 maxtablesize)
{
// Look for the special "jumpassist" pseudo-op
@@ -2137,7 +2163,7 @@ bool JumpAssisted::recoverModel(Funcdata *fd,PcodeOp *indop,uint4 matchsize,uint
inputs.push_back(assistOp->getIn(i+1)->getOffset());
sizeIndices = pcodeScript->evaluate(inputs);
}
- if (matchsize !=0 && matchsize-1 != sizeIndices) // matchsize has 1 added to it for the default case
+ if (previous != (const JumpModel *)0 && previous->getTableSize()-1 != sizeIndices) // table size has 1 added to it for the default case
return false; // Not matching the size we saw previously
if (sizeIndices > maxtablesize)
return false;
@@ -2266,15 +2292,16 @@ void JumpTable::clearSavedModel(void)
}
}
-/// Try to recover each model in turn, until we find one that matches the specific BRANCHIND.
+/// Try to recover each model in turn, until we find one that matches data-flow around the specific BRANCHIND.
/// \param fd is the function containing the switch
-void JumpTable::recoverModel(Funcdata *fd)
+/// \param matchModel (if not NULL) is a previously calculated model that we are trying to match
+void JumpTable::recoverModel(Funcdata *fd,const JumpModel *matchModel)
{
uint4 maxTableSize = fd->getArch()->max_jumptable_size;
if (jmodel != (JumpModel *)0) {
if (jmodel->isOverride()) { // If preexisting model is override
- jmodel->recoverModel(fd,indirect,0,maxTableSize);
+ jmodel->recoverModel(fd,indirect,(const JumpModel *)0,maxTableSize);
return;
}
delete jmodel; // Otherwise this is an old attempt we should remove
@@ -2285,18 +2312,18 @@ void JumpTable::recoverModel(Funcdata *fd)
if (op->code() == CPUI_CALLOTHER) {
JumpAssisted *jassisted = new JumpAssisted(this);
jmodel = jassisted;
- if (jmodel->recoverModel(fd,indirect,addresstable.size(),maxTableSize))
+ if (jmodel->recoverModel(fd,indirect,matchModel,maxTableSize))
return;
}
}
JumpBasic *jbasic = new JumpBasic(this);
jmodel = jbasic;
- if (jmodel->recoverModel(fd,indirect,addresstable.size(),maxTableSize))
+ if (jmodel->recoverModel(fd,indirect,matchModel,maxTableSize))
return;
jmodel = new JumpBasic2(this);
((JumpBasic2 *)jmodel)->initializeStart(jbasic->getPathMeld());
delete jbasic;
- if (jmodel->recoverModel(fd,indirect,addresstable.size(),maxTableSize))
+ if (jmodel->recoverModel(fd,indirect,matchModel,maxTableSize))
return;
delete jmodel;
jmodel = (JumpModel *)0;
@@ -2640,7 +2667,7 @@ void JumpTable::trivialSwitchOver(void)
void JumpTable::recoverAddresses(Funcdata *fd)
{
- recoverModel(fd);
+ recoverModel(fd,(const JumpModel *)0);
if (jmodel == (JumpModel *)0) {
ostringstream err;
err << "Could not recover jumptable at " << opaddress << ". Too many branches";
@@ -2712,7 +2739,7 @@ void JumpTable::matchModel(Funcdata *fd)
fd->warning("Switch is manually overridden",opaddress);
}
}
- recoverModel(fd); // Create a current instance of the model
+ recoverModel(fd,origmodel); // Create a current instance of the model (matching the previous model)
if (jmodel != (JumpModel *)0 && jmodel->getTableSize() != addresstable.size()) {
if ((addresstable.size()==1)&&(jmodel->getTableSize() > 1)) {
// The jumptable was not fully recovered during flow analysis, try to issue a restart
@@ -2743,7 +2770,7 @@ void JumpTable::recoverLabels(Funcdata *fd)
}
else {
jmodel = new JumpModelTrivial(this);
- jmodel->recoverModel(fd,indirect,addresstable.size(),fd->getArch()->max_jumptable_size);
+ jmodel->recoverModel(fd,indirect,origmodel,fd->getArch()->max_jumptable_size);
jmodel->buildAddresses(fd,indirect,addresstable,(vector<LoadTable> *)0,(vector<int4> *)0);
trivialSwitchOver();
jmodel->buildLabels(fd,addresstable,label,origmodel);
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.hh
index 86489092e4..6ce41bcd50 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/jumptable.hh
@@ -192,9 +192,9 @@ protected:
PcodeOp *startop; ///< First PcodeOp in the jump-table calculation
mutable uintb curval; ///< The current value pointed to be the iterator
public:
- void setRange(const CircleRange &rng) { range = rng; } ///< Set the range of values explicitly
- void setStartVn(Varnode *vn) { normqvn = vn; } ///< Set the normalized switch Varnode explicitly
- void setStartOp(PcodeOp *op) { startop = op; } ///< Set the starting PcodeOp explicitly
+ /// \brief Set the range of values explicitly
+ void setRange(const CircleRange &rng,Varnode *vn,PcodeOp *op) { range = rng; normqvn = vn; startop = op; }
+ const CircleRange &getRange(void) const { return range; } ///< Get the full range of values
virtual void truncate(int4 nm);
virtual uintb getSize(void) const;
virtual bool contains(uintb val) const;
@@ -254,10 +254,10 @@ public:
/// This generally recovers the normalized switch variable and any guards.
/// \param fd is the function containing the switch
/// \param indop is the given BRANCHIND
- /// \param matchsize is the expected number of address table entries to recover, or 0 for no expectation
+ /// \param previous (if not NULL) is a previous version of the model we are trying to match
/// \param maxtablesize is maximum number of address table entries to allow in the model
/// \return \b true if details of the model were successfully recovered
- virtual bool recoverModel(Funcdata *fd,PcodeOp *indop,uint4 matchsize,uint4 maxtablesize)=0;
+ virtual bool recoverModel(Funcdata *fd,PcodeOp *indop,const JumpModel *previous,uint4 maxtablesize)=0;
/// \brief Construct the explicit list of target addresses (the Address Table) from \b this model
///
@@ -353,7 +353,7 @@ public:
JumpModelTrivial(JumpTable *jt) : JumpModel(jt) { size = 0; } ///< Construct given a parent JumpTable
virtual bool isOverride(void) const { return false; }
virtual int4 getTableSize(void) const { return size; }
- virtual bool recoverModel(Funcdata *fd,PcodeOp *indop,uint4 matchsize,uint4 maxtablesize);
+ virtual bool recoverModel(Funcdata *fd,PcodeOp *indop,const JumpModel *previous,uint4 maxtablesize);
virtual void buildAddresses(Funcdata *fd,PcodeOp *indop,vector<Address> &addresstable,
vector<LoadTable> *loadpoints,vector<int4> *loadcounts) const;
virtual void findUnnormalized(uint4 maxaddsub,uint4 maxleftright,uint4 maxext) {}
@@ -383,13 +383,14 @@ protected:
static bool ispoint(Varnode *vn); ///< Is it possible for the given Varnode to be a switch variable?
static int4 getStride(Varnode *vn); ///< Get the step/stride associated with the Varnode
static uintb backup2Switch(Funcdata *fd,uintb output,Varnode *outvn,Varnode *invn);
- static uintb getMaxValue(Varnode *vn); ///< Get maximum value associated with the given Varnode
static bool duplicateVarnodes(const vector<Varnode *> &arr); ///< Return \b true if all array elements are the same Varnode
+ void getInitialRange(Varnode *vn,CircleRange &rng) const; ///< Get initial range of values the given Varnode can take
void findDeterminingVarnodes(PcodeOp *op,int4 slot);
void analyzeGuards(BlockBasic *bl,int4 pathout);
void calcRange(Varnode *vn,CircleRange &rng) const;
- void findSmallestNormal(uint4 matchsize);
- void findNormalized(Funcdata *fd,BlockBasic *rootbl,int4 pathout,uint4 matchsize,uint4 maxtablesize);
+ bool isPreferredRange(int4 pos,const CircleRange &newRange);
+ void findSmallestNormal(const JumpBasic *previous);
+ void findNormalized(Funcdata *fd,BlockBasic *rootbl,int4 pathout,const JumpModel *previous,uint4 maxtablesize);
void markFoldableGuards();
void markModel(bool val); ///< Mark (or unmark) all PcodeOps involved in the model
bool flowsOnlyToModel(Varnode *vn,PcodeOp *trailOp); ///< Check if the given Varnode flows to anything other than \b this model
@@ -413,7 +414,7 @@ public:
virtual ~JumpBasic(void);
virtual bool isOverride(void) const { return false; }
virtual int4 getTableSize(void) const { return jrange->getSize(); }
- virtual bool recoverModel(Funcdata *fd,PcodeOp *indop,uint4 matchsize,uint4 maxtablesize);
+ virtual bool recoverModel(Funcdata *fd,PcodeOp *indop,const JumpModel *previous,uint4 maxtablesize);
virtual void buildAddresses(Funcdata *fd,PcodeOp *indop,vector<Address> &addresstable,
vector<LoadTable> *loadpoints,vector<int4> *loadcounts) const;
virtual void findUnnormalized(uint4 maxaddsub,uint4 maxleftright,uint4 maxext);
@@ -446,7 +447,7 @@ class JumpBasic2 : public JumpBasic {
public:
JumpBasic2(JumpTable *jt) : JumpBasic(jt) {} ///< Constructor
void initializeStart(const PathMeld &pMeld); ///< Pass in the prior PathMeld calculation
- virtual bool recoverModel(Funcdata *fd,PcodeOp *indop,uint4 matchsize,uint4 maxtablesize);
+ virtual bool recoverModel(Funcdata *fd,PcodeOp *indop,const JumpModel *previous,uint4 maxtablesize);
virtual void findUnnormalized(uint4 maxaddsub,uint4 maxleftright,uint4 maxext);
virtual JumpModel *clone(JumpTable *jt) const;
virtual void clear(void);
@@ -478,7 +479,7 @@ public:
void setStartingValue(uintb val) { startingvalue = val; } ///< Set the starting value for the normalized range
virtual bool isOverride(void) const { return true; }
virtual int4 getTableSize(void) const { return addrtable.size(); }
- virtual bool recoverModel(Funcdata *fd,PcodeOp *indop,uint4 matchsize,uint4 maxtablesize);
+ virtual bool recoverModel(Funcdata *fd,PcodeOp *indop,const JumpModel *previous,uint4 maxtablesize);
virtual void buildAddresses(Funcdata *fd,PcodeOp *indop,vector<Address> &addresstable,
vector<LoadTable> *loadpoints,vector<int4> *loadcounts) const;
// findUnnormalized inherited from JumpBasic
@@ -517,7 +518,7 @@ public:
// virtual ~JumpAssisted(void);
virtual bool isOverride(void) const { return false; }
virtual int4 getTableSize(void) const { return sizeIndices+1; }
- virtual bool recoverModel(Funcdata *fd,PcodeOp *indop,uint4 matchsize,uint4 maxtablesize);
+ virtual bool recoverModel(Funcdata *fd,PcodeOp *indop,const JumpModel *previous,uint4 maxtablesize);
virtual void buildAddresses(Funcdata *fd,PcodeOp *indop,vector<Address> &addresstable,
vector<LoadTable> *loadpoints,vector<int4> *loadcounts) const;
virtual void findUnnormalized(uint4 maxaddsub,uint4 maxleftright,uint4 maxext) {}
@@ -579,7 +580,7 @@ private:
void saveModel(void); ///< Save off current model (if any) and prepare for instantiating a new model
void restoreSavedModel(void); ///< Restore any saved model as the current model
void clearSavedModel(void); ///< Clear any saved model
- void recoverModel(Funcdata *fd); ///< Attempt recovery of the jump-table model
+ void recoverModel(Funcdata *fd,const JumpModel *matchModel); ///< Attempt recovery of the jump-table model
void trivialSwitchOver(void); ///< Switch \b this table over to a trivial model
bool isThunk(Funcdata *fd) const; ///< Return \b true if the function looks like a thunk
void sanityCheck(Funcdata *fd,vector<int4> *loadpoints); ///< Perform sanity check on recovered address targets
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/rangeutil.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/rangeutil.cc
index 4613c6002e..e4681225a9 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/rangeutil.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/rangeutil.cc
@@ -181,8 +181,8 @@ CircleRange::CircleRange(uintb lft,uintb rgt,int4 size,int4 stp)
{
mask = calc_mask(size);
step = stp;
- left = lft;
- right = rgt;
+ left = lft & mask;
+ right = rgt & mask;
isempty = false;
}
@@ -220,8 +220,8 @@ void CircleRange::setRange(uintb lft,uintb rgt,int4 size,int4 stp)
{
mask = calc_mask(size);
- left = lft;
- right = rgt;
+ left = lft & mask;
+ right = rgt & mask;
step = stp;
isempty = false;
}
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
index e0fb9c92a6..cc79bf9e77 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
@@ -4509,18 +4509,18 @@ bool RuleSubCommute::cancelExtensions(PcodeOp *longform,PcodeOp *subOp,Varnode *
if (outvn->loneDescend() != subOp) return false; // Must be exactly one output to SUBPIECE
if (ext0In->getSize() == ext1In->getSize()) {
maxSize = ext0In->getSize();
- if (ext0In->isFree()) return false; // Must be able to propagate inputs
- if (ext1In->isFree()) return false;
+ if (ext0In->isFree() && !ext0In->isConstant()) return false; // Must be able to propagate inputs
+ if (ext1In->isFree() && !ext1In->isConstant()) return false;
}
else if (ext0In->getSize() < ext1In->getSize()) {
maxSize = ext1In->getSize();
- if (ext1In->isFree()) return false;
+ if (ext1In->isFree() && !ext1In->isConstant()) return false;
if (longform->getIn(0)->loneDescend() != longform) return false;
ext0In = shortenExtension(longform->getIn(0)->getDef(), maxSize, data);
}
else {
maxSize = ext0In->getSize();
- if (ext0In->isFree()) return false;
+ if (ext0In->isFree() && !ext0In->isConstant()) return false;
if (longform->getIn(1)->loneDescend() != longform) return false;
ext1In = shortenExtension(longform->getIn(1)->getDef(), maxSize, data);
}
diff --git a/Ghidra/Features/Decompiler/src/decompile/datatests/switchmask.xml b/Ghidra/Features/Decompiler/src/decompile/datatests/switchmask.xml
new file mode 100644
index 0000000000..b2ecc8bf22
--- /dev/null
+++ b/Ghidra/Features/Decompiler/src/decompile/datatests/switchmask.xml
@@ -0,0 +1,114 @@
+<decompilertest>
+<binaryimage arch="x86:LE:64:default:gcc">
+<!--
+ 2 switches with no guards and inferred bounds
+-->
+<bytechunk space="ram" offset="0xffd00" readonly="true">
+89f88d0c068d3c1141b8cdcccccc4c0f
+afc749c1e8234501c0478d0480412bf8
+4c8d052d00000049633cb84903f8ffe7
+b801000000c389c8c3b80a000000c383
+c20c89d0c383c00ac383c60b89f0c301
+f289d0c3dcffffffeafffffff8ffffff
+eeffffffe2fffffff1fffffff5ffffff
+ebfffffffbffffffe5ffffff00000000
+</bytechunk>
+<bytechunk space="ram" offset="0x100000" readonly="true">
+4154555389fd89f34189d483ff097f24
+83fe01741389f7e8e40f000083e00f89
+c0ff24c5e000100089d7e8d10f000083
+e00febeb83ff137f1f83fe01740d8d3c
+37e8ba0f000083e00febd48d3c17e8ad
+0f000083e00febc783ff1d7f0d8d7e0a
+e89b0f000083e00febb58d7a0ae88e0f
+000083e00feba889e85b5d415cc389d8
+ebf74489e0ebf28d441d00ebec8d450a
+ebe78d430bebe2418d44240cebdb418d
+041cebd5b80a000000ebce89e831d8eb
+c84489e031d8ebc189e84431e0ebba8d
+5cad008d041bebb16bc564ebac69c3e8
+030000eba4b801000000eb9d
+</bytechunk>
+<bytechunk space="ram" offset="0x1000e0" readonly="true">
+d5001000000000007700100000000000
+7e001000000000008200100000000000
+87001000000000008d00100000000000
+92001000000000009700100000000000
+9e00100000000000a400100000000000
+ab00100000000000b100100000000000
+b800100000000000bf00100000000000
+c800100000000000cd00100000000000
+</bytechunk>
+<symbol space="ram" offset="0xffd00" name="switchrem"/>
+<symbol space="ram" offset="0x100000" name="switchmask"/>
+<symbol space="ram" offset="0x101000" name="getvalue"/>
+</binaryimage>
+<script>
+ <com>option readonly on</com>
+ <com>lo fu switchrem</com>
+ <com>decompile</com>
+ <com>print C</com>
+ <com>lo fu switchmask</com>
+ <com>decompile</com>
+ <com>print C</com>
+ <com>quit</com>
+</script>
+<stringmatch name="Switch Mask #1" min="1" max="1">switch\(.*\(param_. \+ param_. \+ param_.\) % 10\)</stringmatch>
+
+<stringmatch name="Switch Mask #2" min="1" max="1">case 0:
+ return 1;</stringmatch>
+<stringmatch name="Switch Mask #3" min="1" max="1">case 1:
+ return param_1;</stringmatch>
+<stringmatch name="Switch Mask #4" min="1" max="1">case 2:
+ return param_2;</stringmatch>
+<stringmatch name="Switch Mask #5" min="1" max="1">case 3:
+ return param_3;</stringmatch>
+<stringmatch name="Switch Mask #6" min="1" max="1">case 4:
+ return param_. \+ param_.;</stringmatch>
+<stringmatch name="Switch Mask #7" min="1" max="1">case 5:
+ return param_1 \+ 10;</stringmatch>
+<stringmatch name="Switch Mask #8" min="1" max="1">case 6:
+ return param_2 \+ 0xb;</stringmatch>
+<stringmatch name="Switch Mask #9" min="1" max="1">case 7:
+ return param_3 \+ 0xc;</stringmatch>
+<stringmatch name="Switch Mask #10" min="1" max="1">case 8:
+ return param_. \+ param_.;</stringmatch>
+<stringmatch name="Switch Mask #11" min="1" max="1">case 9:
+ return 10;</stringmatch>
+<stringmatch name="Switch Mask #12" min="1" max="1">switch\(uVar.\)</stringmatch>
+
+<stringmatch name="Switch Mask #13" min="1" max="1">case 0:
+ .* = 1;</stringmatch>
+<stringmatch name="Switch Mask #14" min="1" max="1">case 1:
+ break;</stringmatch>
+<stringmatch name="Switch Mask #15" min="1" max="1">case 2:
+ .* = param_2;</stringmatch>
+<stringmatch name="Switch Mask #16" min="1" max="1">case 3:
+ .* = param_3;</stringmatch>
+<stringmatch name="Switch Mask #17" min="1" max="1">case 4:
+ .* = param_. \+ param_.;</stringmatch>
+<stringmatch name="Switch Mask #18" min="1" max="1">case 5:
+ .* = param_1 \+ 10;</stringmatch>
+<stringmatch name="Switch Mask #19" min="1" max="1">case 6:
+ .* = param_2 \+ 0xb;</stringmatch>
+<stringmatch name="Switch Mask #20" min="1" max="1">case 7:
+ .* = param_3 \+ 0xc;</stringmatch>
+<stringmatch name="Switch Mask #21" min="1" max="1">case 8:
+ .* = param_. \+ param_.;</stringmatch>
+<stringmatch name="Switch Mask #22" min="1" max="1">case 9:
+ .* = 10;</stringmatch>
+<stringmatch name="Switch Mask #23" min="1" max="1">case 10:
+ .* = param_. \^ param_.;</stringmatch>
+<stringmatch name="Switch Mask #24" min="1" max="1">case 0xb:
+ .* = param_. \^ param_.;</stringmatch>
+<stringmatch name="Switch Mask #25" min="1" max="1">case 0xc:
+ .* = param_. \^ param_.;</stringmatch>
+<stringmatch name="Switch Mask #26" min="1" max="1">case 0xd:
+ .* = param_1 \* 10;</stringmatch>
+<stringmatch name="Switch Mask #27" min="1" max="1">case 0xe:
+ .* = param_1 \* 100;</stringmatch>
+<stringmatch name="Switch Mask #28" min="1" max="1">case 0xf:
+ .* = param_2 \* 1000;</stringmatch>
+<stringmatch name="Switch Mask #29" min="26" max="26">case</stringmatch>
+<stringmatch name="Switch Mask #30" min="0" max="0">default</stringmatch>
+</decompilertest>