Ghidra is a software reverse engineering (SRE) framework
GP-5863 Check for input intersection before creating unreferenced param
caheckmancc0203307d6b
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.ccindex bed14fc3ea..9ee6bbfcc8 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc@@ -4585,11 +4585,17 @@ int4 ActionInputPrototype::apply(Funcdata &data)for(int4 i=0;i<active.getNumTrials();++i) {ParamTrial ¶mtrial(active.getTrial(i));if (paramtrial.isUnref() && paramtrial.isUsed()) {- vn = data.newVarnode(paramtrial.getSize(),paramtrial.getAddress());- vn = data.setInputVarnode(vn);- int4 slot = triallist.size();- triallist.push_back(vn);- paramtrial.setSlot(slot + 1);+ if (data.hasInputIntersection(paramtrial.getSize(), paramtrial.getAddress())) {+ // There is something in the way of the unreferenced parameter, don't create it+ paramtrial.markNoUse();+ }+ else {+ vn = data.newVarnode(paramtrial.getSize(),paramtrial.getAddress());+ vn = data.setInputVarnode(vn);+ int4 slot = triallist.size();+ triallist.push_back(vn);+ paramtrial.setSlot(slot + 1);+ }}}if (data.isHighOn())diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hhindex 168fcc4d65..083056d7f3 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh@@ -309,6 +309,13 @@ public:/// \return the matching Varnode or NULLVarnode *findCoveringInput(int4 s,const Address &loc) const { return vbank.findCoveringInput(s,loc); }+ /// \brief Check if an input Varnode exists that overlaps the given range+ ///+ /// \param s is the size of the range in bytes+ /// \param loc is the starting address of the given range+ /// \return \b true if there is an input Varnode that overlaps the range+ bool hasInputIntersection(int4 s,const Address &loc) const { return vbank.hasInputIntersection(s, loc); }+/// \brief Find the input Varnode with the given size and storage address////// \param s is the size in bytesdiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.ccindex 8df64ca548..0344bcb46b 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc@@ -1757,6 +1757,7 @@ int4 RuleAndCompare::applyOp(PcodeOp *op,Funcdata &data)switch(subop->code()) {case CPUI_SUBPIECE:basevn = subop->getIn(0);+ if (basevn->getSize() > sizeof(uintb)) return 0;baseconst = andop->getIn(1)->getOffset();andconst = baseconst << subop->getIn(1)->getOffset() * 8;break;diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.ccindex 147eff5fd9..eb0814267d 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.cc@@ -1530,6 +1530,29 @@ Varnode *VarnodeBank::findCoveringInput(int4 s,const Address &loc) constreturn (Varnode *)0;}+/// \param s is the number of bytes in the given range+/// \param loc is the starting address of the given range+/// \return \b true if there is an input Varnode that overlaps the range+bool VarnodeBank::hasInputIntersection(int4 s,const Address &loc) const++{+ VarnodeDefSet::const_iterator iter;+ Varnode *vn;+ iter = beginDef(Varnode::input,loc);+ if (iter != def_tree.end()) {+ vn = *iter;+ if (vn->isInput() && vn->intersects(loc, s))+ return true;+ }+ if (iter != def_tree.begin()) {+ --iter;+ vn = *iter;+ if (vn->isInput() && vn->intersects(loc,s))+ return true;+ }+ return false;+}+/// \brief Beginning of Varnodes in given address space sorted by location////// \param spaceid is the given address spacediff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.hhindex 4fb7357259..7360cf41ef 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.hh@@ -390,6 +390,7 @@ public:Varnode *findInput(int4 s,const Address &loc) const; ///< Find an input VarnodeVarnode *findCoveredInput(int4 s,const Address &loc) const; ///< Find an input Varnode contained within this rangeVarnode *findCoveringInput(int4 s,const Address &loc) const; ///< Find an input Varnode covering a range+ bool hasInputIntersection(int4 s,const Address &loc) const; ///< Check for input Varnode that overlaps the given rangeuint4 getCreateIndex(void) const { return create_index; } ///< Get the next creation index to be assignedVarnodeLocSet::const_iterator beginLoc(void) const { return loc_tree.begin(); } ///< Beginning of location listVarnodeLocSet::const_iterator endLoc(void) const { return loc_tree.end(); } ///< End of location list