Ghidra is a software reverse engineering (SRE) framework
New guardStore
caheckman171602e1ce50
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hhindex dd0170517b..518db02574 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh@@ -234,6 +234,7 @@ public:const list<LoadGuard> &getLoadGuards(void) const { return heritage.getLoadGuards(); } ///< Get the list of guarded LOADsconst list<LoadGuard> &getStoreGuards(void) const { return heritage.getStoreGuards(); } ///< Get the list of guarded STOREs+ const LoadGuard *getStoreGuard(PcodeOp *op) const { return heritage.getStoreGuard(op); } ///< Get LoadGuard associated with STORE op// Function prototype and call specification routinesint4 numCalls(void) const { return qlst.size(); } ///< Get the number of calls made by \b this function@@ -420,6 +421,7 @@ public:void opMarkNoCollapse(PcodeOp *op) { op->setFlag(PcodeOp::nocollapse); } ///< Mark PcodeOp as not collapsiblevoid opMarkCpoolTransformed(PcodeOp *op) { op->setFlag(PcodeOp::is_cpool_transformed); } ///< Mark cpool record was visitedvoid opMarkCalculatedBool(PcodeOp *op) { op->setFlag(PcodeOp::calculated_bool); } ///< Mark PcodeOp as having boolean output+ void opMarkSpacebaseStore(PcodeOp *op) { op->setFlag(PcodeOp::spacebase_ptr); } ///< Mark PcodeOp as STOREing from spacebase ptrvoid opFlipCondition(PcodeOp *op) { op->flipFlag(PcodeOp::boolean_flip); } ///< Flip output condition of given CBRANCHPcodeOp *target(const Address &addr) const { return obank.target(addr); } ///< Look up a PcodeOp by an instruction AddressVarnode *createStackRef(AddrSpace *spc,uintb off,PcodeOp *op,Varnode *stackptr,bool insertafter);diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.ccindex 854de11329..094c1a0e85 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc@@ -653,6 +653,18 @@ void LoadGuard::finalizeRange(const ValueSetRead &valueSet)maximumOffset = spc->getHighest();}+/// Check if the address falls within the range defined by \b this+/// \param addr is the given address+/// \return \b true if the address is contained+bool LoadGuard::isGuarded(const Address &addr) const++{+ if (addr.getSpace() != spc) return false;+ if (addr.getOffset() < minimumOffset) return false;+ if (addr.getOffset() > maximumOffset) return false;+ return true;+}+void Heritage::analyzeNewLoadGuards(void){@@ -747,6 +759,7 @@ void Heritage::generateStoreGuard(StackNode &node,PcodeOp *op,AddrSpace *spc){storeGuard.push_back(LoadGuard());storeGuard.back().set(op,spc,node.offset);+ fd->opMarkSpacebaseStore(op);}/// \brief Trace input stackpointer to any indexed loads@@ -1012,12 +1025,16 @@ void Heritage::guardStores(const Address &addr,int4 size,vector<Varnode *> &writ{list<PcodeOp *>::const_iterator iter,iterend;PcodeOp *op,*indop;+ AddrSpace *spc = addr.getSpace();+ AddrSpace *container = spc->getContain();iterend = fd->endOp(CPUI_STORE);for(iter=fd->beginOp(CPUI_STORE);iter!=iterend;++iter) {op = *iter;if (op->isDead()) continue;- if (addr.getSpace()->contain(Address::getSpaceFromConst(op->getIn(0)->getAddr()))) { // Does store affect same space+ AddrSpace *storeSpace = Address::getSpaceFromConst(op->getIn(0)->getAddr());+ if ((container == storeSpace && op->usesSpacebasePtr()) ||+ (spc == storeSpace)) {indop = fd->newIndirectOp(op,addr,size);indop->getIn(0)->setActiveHeritage();indop->getOut()->setActiveHeritage();@@ -2149,6 +2166,19 @@ void Heritage::heritage(void)pass += 1;}+/// \param op is the given PcodeOp+/// \return the associated LoadGuard or NULL+const LoadGuard *Heritage::getStoreGuard(PcodeOp *op) const++{+ list<LoadGuard>::const_iterator iter;+ for(iter=storeGuard.begin();iter!=storeGuard.end();++iter) {+ if ((*iter).op == op)+ return &(*iter);+ }+ return (const LoadGuard *)0;+}+/// \brief Get the number times heritage was performed for the given address space////// A negative number indicates the number of passes to be wait before the firstdiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.hhindex cfe062ebc0..84999712cc 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.hh@@ -125,6 +125,7 @@ public:uintb getMinimum(void) const { return minimumOffset; } ///< Get minimum offset of the guarded rangeuintb getMaximum(void) const { return maximumOffset; } ///< Get maximum offset of the guarded rangeint4 getStep(void) const { return step; } ///< Get the calculated step associated with the range (or 0)+ bool isGuarded(const Address &addr) const; ///< Does \b this guard apply to the given addressbool isRangeLocked(void) const { return (analysisState == 2); } ///< Return \b true if the range is fully determinedbool isValid(OpCode opc) const { return (!op->isDead() && op->code() == opc); } ///< Return \b true if the record still describes an active LOAD};@@ -273,6 +274,7 @@ public:void heritage(void); ///< Perform one pass of heritageconst list<LoadGuard> &getLoadGuards(void) const { return loadGuard; } ///< Get list of LOAD ops that are guardedconst list<LoadGuard> &getStoreGuards(void) const { return storeGuard; } ///< Get list of STORE ops that are guarded+ const LoadGuard *getStoreGuard(PcodeOp *op) const; ///< Get LoadGuard record associated with given PcodeOp};#endifdiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.ccindex 427739fc55..fbac25d4b0 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc@@ -2805,6 +2805,13 @@ int4 RuleIndirectCollapse::applyOp(PcodeOp *op,Funcdata &data)if (!op->getOut()->hasNoLocalAlias())return 0;}+ else if (indop->usesSpacebasePtr()) {+ const LoadGuard *guard = data.getStoreGuard(indop);+ if (guard != (const LoadGuard *)0) {+ if (guard->isGuarded(op->getOut()->getAddr()))+ return 0;+ }+ }elsereturn 0;}diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/space.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/space.ccindex c144fae7d6..61676809c4 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/space.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/space.cc@@ -112,20 +112,6 @@ void AddrSpace::truncateSpace(uint4 newsize)calcScaleMask();}-/// Check if this space contains \b id2.-/// \param id2 is the space to check-/// \return \b true if \b id2 is contained-bool AddrSpace::contain(AddrSpace *id2) const--{- while(this != id2) {- id2 = id2->getContain();- if (id2 == (AddrSpace *)0)- return false; // No containment- }- return true;-}-/// Write the main XML attributes for an address within this space/// The caller provides only the \e offset, and this routine fills/// in other details pertaining to this particular space.diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/space.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/space.hhindex 2105023c2a..c254edfa27 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/space.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/space.hh@@ -121,7 +121,6 @@ public:uintb getHighest(void) const; ///< Get the highest byte-scaled addressuintb wrapOffset(uintb off) const; ///< Wrap -off- to the offset that fits into this spacechar getShortcut(void) const; ///< Get the shortcut character- bool contain(AddrSpace *id2) const; ///< Determine if this space contains anotherbool isHeritaged(void) const; ///< Return \b true if dataflow has been tracedbool doesDeadcode(void) const; ///< Return \b true if dead code analysis should be done on this spacebool hasPhysical(void) const; ///< Return \b true if data is physically stored in this