Ghidra is a software reverse engineering (SRE) framework
GP-2917 Move INDIRECTs into separate container
caheckman590c949265cb
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/action.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/action.ccindex e02dbc38e8..0ac689536e 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/action.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/action.cc@@ -700,18 +700,6 @@ void Rule::printStatistics(ostream &s) consts << name << dec << " Tested=" << count_tests << " Applied=" << count_apply << endl;}-/// Populate the given array with all possible OpCodes this Rule might apply to.-/// By default, this method returns all possible OpCodes-/// \param oplist is the array to populate-void Rule::getOpList(vector<uint4> &oplist) const--{- uint4 i;-- for(i=0;i<CPUI_MAX;++i)- oplist.push_back(i);-}-/// This method is called every time the Rule successfully applies. If it returns/// \b true, this indicates to the system that an action breakpoint has occurred./// \return true if an action breakpoint should occur because of this Rule@@ -878,11 +866,11 @@ int4 ActionPool::apply(Funcdata &data){if (status != status_mid) {- op_state = data.beginOpAll(); // Initialize the derived action+ op_state = data.beginOpMain(); // Initialize the derived actionrule_index = 0;}- for(;op_state!=data.endOpAll();)- if (0!=processOp((*op_state).second,data)) return -1;+ for(;op_state!=data.endOpMain();)+ if (0!=processOp((*op_state).second,data)) return -1;return 0; // Indicate successful completion}diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/action.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/action.hhindex e929dbb98e..df90a9dac9 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/action.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/action.hh@@ -234,7 +234,10 @@ public:/// \param grouplist is the list of groups being cloned/// \return the cloned Rule or NULLvirtual Rule *clone(const ActionGroupList &grouplist) const=0;- virtual void getOpList(vector<uint4> &oplist) const; ///< List of op codes this rule operates on++ /// Populate the given array with all possible OpCodes this Rule might apply to.+ /// \param oplist is the array to populate+ virtual void getOpList(vector<uint4> &oplist) const=0; ///< Provide OpCodes \b this rule operates on/// \brief Attempt to apply \b this Rule///diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/bitfield.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/bitfield.ccindex 5d7d650365..0adeebad89 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/bitfield.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/bitfield.cc@@ -1695,7 +1695,6 @@ void RuleBitFieldOut::getOpList(vector<uint4> &oplist) const{oplist.insert(oplist.end(),BitFieldInsertTransform::allowedFinalWrites.begin(),BitFieldInsertTransform::allowedFinalWrites.end());- oplist.push_back(CPUI_INDIRECT);}int4 RuleBitFieldOut::applyOp(PcodeOp *op,Funcdata &data)@@ -1703,12 +1702,28 @@ int4 RuleBitFieldOut::applyOp(PcodeOp *op,Funcdata &data){Varnode *outvn = op->getOut();Datatype *dt = outvn->getTypeDefFacing();- if (!dt->hasBitfields()) return 0;- BitFieldInsertTransform transform(&data,op,dt,0);- if (!transform.doTrace())- return 0;- transform.apply();- return 1;+ if (dt->hasBitfields()) {+ BitFieldInsertTransform transform(&data,op,dt,0);+ if (transform.doTrace()) {+ transform.apply();+ return 1;+ }+ }+ list<PcodeOp *>::const_iterator iter = outvn->beginDescend();+ while(iter != outvn->endDescend()) {+ PcodeOp *indOp = *iter;+ ++iter;+ if (indOp->code() != CPUI_INDIRECT) continue;+ dt = indOp->getOut()->getTypeDefFacing();+ if (dt->hasBitfields()) {+ BitFieldInsertTransform transform(&data,indOp,dt,0);+ if (transform.doTrace()) {+ transform.apply();+ return 1;+ }+ }+ }+ return 0;}void RuleBitFieldLoad::getOpList(vector<uint4> &oplist) constdiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/comment.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/comment.ccindex d9d2000e55..6ea6b1fedb 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/comment.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/comment.cc@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -280,9 +280,9 @@ bool CommentSorter::findPosition(Subsort &subsort,Comment *comm,const Funcdata *// Try to find block containing comment// Find op at lowest address greater or equal to comment's address- PcodeOpTree::const_iterator opiter = fd->beginOp(comm->getAddr());+ PcodeOpTree::const_iterator opiter = fd->beginOpMain(comm->getAddr());PcodeOp *backupOp = (PcodeOp *)0;- if (opiter != fd->endOpAll()) { // If there is an op at or after the comment+ if (opiter != fd->endOpMain()) { // If there is an op at or after the commentPcodeOp *op = (*opiter).second;BlockBasic *block = op->getParent();if (block == (BlockBasic *)0)@@ -295,7 +295,7 @@ bool CommentSorter::findPosition(Subsort &subsort,Comment *comm,const Funcdata *if (comm->getAddr() == op->getAddr())backupOp = op;}- if (opiter != fd->beginOpAll()) { // If there is a previous op+ if (opiter != fd->beginOpMain()) { // If there is a previous op--opiter;PcodeOp *op = (*opiter).second;BlockBasic *block = op->getParent();@@ -313,7 +313,7 @@ bool CommentSorter::findPosition(Subsort &subsort,Comment *comm,const Funcdata *subsort.setBlock(backupOp->getParent()->getIndex(),(uint4)backupOp->getSeqNum().getOrder());return true;}- if (fd->beginOpAll() == fd->endOpAll()) { // If there are no ops at all+ if (fd->beginOpMain() == fd->endOpMain()) { // If there are no ops at allsubsort.setBlock(0,0); // Put comment at the beginning of the first blockreturn true;}diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/constseq.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/constseq.ccindex 5029faff88..153d1fc0dd 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/constseq.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/constseq.cc@@ -453,10 +453,8 @@ void StringSequence::removeCopyOps(PcodeOp *replaceOp)Varnode *vn = op->getIn((*iter).slot);if (vn->getDef()->code() != CPUI_INDIRECT) {Varnode *newIn = data.newConstant(vn->getSize(),0);- PcodeOp *indOp = data.newOp(2, replaceOp->getAddr());- data.opSetOpcode(indOp,CPUI_INDIRECT);+ PcodeOp *indOp = data.newIndirect(replaceOp);data.opSetInput(indOp,newIn,0);- data.opSetInput(indOp,data.newVarnodeIop(replaceOp),1);data.opSetOutput(indOp, vn);data.markIndirectCreation(indOp, false);data.opInsertBefore(indOp,replaceOp);@@ -911,11 +909,9 @@ void HeapSequence::removeStoreOps(vector<PcodeOp *> &indirects,vector<IndirectPa}for(int4 i=0;i<indirectPairs.size();++i) {if (indirectPairs[i].isDuplicate()) continue;- PcodeOp *newInd = data.newOp(2,replaceOp->getAddr());- data.opSetOpcode(newInd, CPUI_INDIRECT);+ PcodeOp *newInd = data.newIndirect(replaceOp);data.opSetOutput(newInd,indirectPairs[i].outVn);data.opSetInput(newInd,indirectPairs[i].inVn,0);- data.opSetInput(newInd,data.newVarnodeIop(replaceOp),1);data.opInsertBefore(newInd, replaceOp);}}diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.ccindex ad746815f8..d63fdf395e 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc@@ -1515,18 +1515,19 @@ int4 ActionExtraPopSetup::apply(Funcdata &data)for(int4 i=0;i<data.numCalls();++i) {fc = data.getCallSpecs(i);if (fc->getExtraPop() == 0) continue; // Stack pointer is undisturbed- op = data.newOp(2,fc->getOp()->getAddr());- data.newVarnodeOut(sb_size,sb_addr,op);- data.opSetInput(op,data.newVarnode(sb_size,sb_addr),0);if (fc->getExtraPop() != ProtoModel::extrapop_unknown) { // We know exactly how stack pointer is changedfc->setEffectiveExtraPop(fc->getExtraPop());+ op = data.newOp(2,fc->getOp()->getAddr());+ data.newVarnodeOut(sb_size,sb_addr,op);+ data.opSetInput(op,data.newVarnode(sb_size,sb_addr),0);data.opSetOpcode(op,CPUI_INT_ADD);data.opSetInput(op,data.newConstant(sb_size,fc->getExtraPop()),1);data.opInsertAfter(op,fc->getOp());}else { // We don't know exactly, so we create INDIRECT- data.opSetOpcode(op,CPUI_INDIRECT);- data.opSetInput(op,data.newVarnodeIop(fc->getOp()),1);+ op = data.newIndirect(fc->getOp());+ data.newVarnodeOut(sb_size,sb_addr,op);+ data.opSetInput(op,data.newVarnode(sb_size,sb_addr),0);data.opInsertBefore(op,fc->getOp());}}diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/double.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/double.ccindex 0f2d4c76c1..4c32157ad4 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/double.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/double.cc@@ -1364,11 +1364,9 @@ void SplitVarnode::replaceIndirectOp(Funcdata &data,SplitVarnode &out,SplitVarnoout.createJoinedWhole(data);in.findCreateWhole(data);- PcodeOp *newop = data.newOp(2,affector->getAddr());- data.opSetOpcode(newop,CPUI_INDIRECT);+ PcodeOp *newop = data.newIndirect(affector);data.opSetOutput(newop,out.getWhole());data.opSetInput(newop,in.getWhole(),0);- data.opSetInput(newop,data.newVarnodeIop(affector),1);data.opInsertBefore(newop,affector);out.buildLoFromWhole(data);out.buildHiFromWhole(data);diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/dynamic.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/dynamic.ccindex 5ba36b30eb..ae7bcfb3fb 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/dynamic.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/dynamic.cc@@ -499,7 +499,7 @@ void DynamicHash::uniqueHash(const PcodeOp *op,int4 slot,Funcdata *fd)addrresult = Address(); // Hash cannot be calculatedreturn;}- gatherOpsAtAddress(oplist,fd,op->getAddr());+ fd->listOps(oplist,op->getAddr());for(method=4;method<7;++method) {clear();calcHash(op,slot,method);@@ -600,7 +600,7 @@ PcodeOp *DynamicHash::findOp(const Funcdata *fd,const Address &addr,uint8 h)clearTotalPosition(h);vector<PcodeOp *> oplist;vector<PcodeOp *> oplist2;- gatherOpsAtAddress(oplist,fd,addr);+ fd->listOps(oplist, addr);for(uint4 i=0;i<oplist.size();++i) {PcodeOp *tmpop = oplist[i];if (slot >= tmpop->numInput()) continue;@@ -648,12 +648,11 @@ void DynamicHash::gatherFirstLevelVars(vector<Varnode *> &varlist,const Funcdatauint4 opcVal = getOpCodeFromHash(h);int4 slot = getSlotFromHash(h);bool isnotattached = getIsNotAttached(h);- PcodeOpTree::const_iterator iter = fd->beginOp(addr);- PcodeOpTree::const_iterator enditer = fd->endOp(addr);+ vector<PcodeOp *> opList;+ fd->listOps(opList, addr);- while(iter!=enditer) {- PcodeOp *op = (*iter).second;- ++iter;+ for(int4 i=0;i<opList.size();++i) {+ PcodeOp *op = opList[i];if (op->isDead()) continue;if (transtable[op->code()] != opcVal) continue;if (slot <0) {@@ -684,23 +683,6 @@ void DynamicHash::gatherFirstLevelVars(vector<Varnode *> &varlist,const FuncdatadedupVarnodes(varlist);}-/// \brief Place all PcodeOps at the given address in the provided container-///-/// \param opList is the container to hold the PcodeOps-/// \param fd is the function-/// \param addr is the given address-void DynamicHash::gatherOpsAtAddress(vector<PcodeOp *> &opList,const Funcdata *fd,const Address &addr)--{- PcodeOpTree::const_iterator iter,enditer;- enditer = fd->endOp(addr);- for(iter = fd->beginOp(addr); iter != enditer; ++iter) {- PcodeOp *op = (*iter).second;- if (op->isDead()) continue;- opList.push_back(op);- }-}-/// The hash encodes the input \e slot the root Varnode was attached to in its PcodeOp./// \param h is the hash value/// \return the slot index or -1 if the Varnode was attached as outputdiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/dynamic.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/dynamic.hhindex ca150ff5ef..a6db9831ed 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/dynamic.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/dynamic.hh@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -92,7 +92,6 @@ public:const Address &getAddress(void) const { return addrresult; } ///< Get the (current) addressstatic void gatherFirstLevelVars(vector<Varnode *> &varlist,const Funcdata *fd,const Address &addr,uint8 h);- static void gatherOpsAtAddress(vector<PcodeOp *> &opList,const Funcdata *fd,const Address &addr);static int4 getSlotFromHash(uint8 h); ///< Retrieve the encoded slot from a hashstatic uint4 getMethodFromHash(uint8 h); ///< Retrieve the encoded method from a hashstatic uint4 getOpCodeFromHash(uint8 h); ///< Retrieve the encoded op-code from a hashdiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.ccindex 9d21071611..95fbca921e 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.cc@@ -212,7 +212,7 @@ void Funcdata::printRaw(ostream &s) constthrow RecovError("No operations to print");PcodeOpTree::const_iterator iter;s << "Raw operations: \n";- for(iter=obank.beginAll();iter!=obank.endAll();++iter) {+ for(iter=obank.beginMain();iter!=obank.endMain();++iter) {s << (*iter).second->getSeqNum() << ":\t";(*iter).second->printRaw(s);s << endl;diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hhindex 2c6cecf885..5007fb226e 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh@@ -453,6 +453,7 @@ public:PcodeOp *newOp(int4 inputs,const SeqNum &sq); /// Allocate a new PcodeOp with sequence numberPcodeOp *newOpBefore(PcodeOp *follow,OpCode opc,Varnode *in1,Varnode *in2,Varnode *in3=(Varnode *)0);PcodeOp *cloneOp(const PcodeOp *op,const SeqNum &seq); /// Clone a PcodeOp into \b this function+ PcodeOp *newIndirect(PcodeOp *target); ///< Allocate a new CPUI_INDIRECTPcodeOp *getFirstReturnOp(void) const; /// Find a representative CPUI_RETURN op for \b this functionPcodeOp *newIndirectOp(PcodeOp *indeffect,const Address &addr,int4 sz,uint4 extraFlags);PcodeOp *newIndirectCreation(PcodeOp *indeffect,const Address &addr,int4 sz,bool possibleout);@@ -525,19 +526,20 @@ public:/// \brief End of PcodeOp objects in the \e dead listlist<PcodeOp *>::const_iterator endOpDead(void) const { return obank.endDead(); }- /// \brief Start of all (alive) PcodeOp objects sorted by sequence number- PcodeOpTree::const_iterator beginOpAll(void) const { return obank.beginAll(); }+ /// \brief Start of PcodeOp objects sorted by sequence number, excluding CPUI_INDIRECT+ PcodeOpTree::const_iterator beginOpMain(void) const { return obank.beginMain(); }- /// \brief End of all (alive) PcodeOp objects sorted by sequence number- PcodeOpTree::const_iterator endOpAll(void) const { return obank.endAll(); }+ /// \brief End of PcodeOp objects sorted by sequence number, excluding CPUI_INDIRECT+ PcodeOpTree::const_iterator endOpMain(void) const { return obank.endMain(); }/// \brief Start of all (alive) PcodeOp objects attached to a specific Address- PcodeOpTree::const_iterator beginOp(const Address &addr) const { return obank.begin(addr); }+ PcodeOpTree::const_iterator beginOpMain(const Address &addr) const { return obank.beginMain(addr); }/// \brief End of all (alive) PcodeOp objects attached to a specific Address- PcodeOpTree::const_iterator endOp(const Address &addr) const { return obank.end(addr); }+ PcodeOpTree::const_iterator endOpMain(const Address &addr) const { return obank.endMain(addr); }PcodeOp *findPrimaryBranch(const Address &addr,bool findBranch,bool findCall,bool findCallother,bool findReturn);+ void listOps(vector<PcodeOp *> &res,const Address &addr) const;bool moveRespectingCover(PcodeOp *op,PcodeOp *lastOp); ///< Move given op past \e lastOp respecting covers if possiblediff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_op.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_op.ccindex e791f4be75..d29c4a1476 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_op.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_op.cc@@ -335,6 +335,20 @@ PcodeOp *Funcdata::newOp(int4 inputs,const SeqNum &sq)return obank.create(inputs,sq);}+/// The new INDIRECT is registered in alternate storage, quarantined from the+/// main sequence number sort. The first input and output to the INDIRECT, need to be filled in.+/// The second input is populated with a Varnode that points to the \b target op.+/// \param target is the PcodeOp causing the indirect effect+/// \return the new INDIRECT op+PcodeOp *Funcdata::newIndirect(PcodeOp *target)++{+ PcodeOp *op = obank.createIndirect(2,target->getAddr());+ obank.changeOpcode(op, glb->inst[CPUI_INDIRECT]);+ opSetInput(op,newVarnodeIop(target),1);+ return op;+}+/// The given PcodeOp is inserted \e immediately before the \e follow op except:/// - MULTIEQUALS in a basic block all occur first/// - INDIRECTs occur immediately before their op@@ -687,12 +701,10 @@ PcodeOp *Funcdata::newIndirectOp(PcodeOp *indeffect,const Address &addr,int4 sz,PcodeOp *newop;newin = newVarnode(sz,addr);- newop = newOp(2,indeffect->getAddr());+ newop = newIndirect(indeffect);newop->flags |= extraFlags;newVarnodeOut(sz,addr,newop);- opSetOpcode(newop,CPUI_INDIRECT);opSetInput(newop,newin,0);- opSetInput(newop,newVarnodeIop(indeffect),1);opInsertBefore(newop,indeffect);return newop;}@@ -714,15 +726,13 @@ PcodeOp *Funcdata::newIndirectCreation(PcodeOp *indeffect,const Address &addr,inPcodeOp *newop;newin = newConstant(sz,0);- newop = newOp(2,indeffect->getAddr());+ newop = newIndirect(indeffect);newop->flags |= PcodeOp::indirect_creation;newout = newVarnodeOut(sz,addr,newop);if (!possibleout)newin->flags |= Varnode::indirect_creation;newout->flags |= Varnode::indirect_creation;- opSetOpcode(newop,CPUI_INDIRECT);opSetInput(newop,newin,0);- opSetInput(newop,newVarnodeIop(indeffect),1);opInsertBefore(newop,indeffect);return newop;}@@ -927,8 +937,8 @@ int4 Funcdata::inlineFlow(Funcdata *inlinefd,FlowInfo &flow,PcodeOp *callop)/// \return the first branching PcodeOp that matches the criteria or NULLPcodeOp *Funcdata::findPrimaryBranch(const Address &addr,bool findBranch,bool findCall,bool findCallother,bool findReturn){- PcodeOpTree::const_iterator iter = beginOp(addr);- PcodeOpTree::const_iterator enditer = endOp(addr);+ PcodeOpTree::const_iterator iter = beginOpMain(addr);+ PcodeOpTree::const_iterator enditer = endOpMain(addr);while(iter != enditer) {PcodeOp *op = (*iter).second;if (op->isCallOrBranch() || op->isFlowBreak()) {@@ -951,6 +961,33 @@ PcodeOp *Funcdata::findPrimaryBranch(const Address &addr,bool findBranch,bool fireturn (PcodeOp *)0;}+/// \brief Collect all ops at the given Address in one container+///+/// All ops at the address, including INDIRECTs, that are currently alive are placed in the container.+/// \param res is the container to hold the ops+/// \param addr is the given Address+void Funcdata::listOps(vector<PcodeOp *> &res,const Address &addr) const++{+ PcodeOpTree::const_iterator iter,enditer;+ iter = obank.beginMain(addr);+ enditer = obank.endMain(addr);+ while(iter != enditer) {+ PcodeOp *op = (*iter).second;+ ++iter;+ if (!op->isDead())+ res.push_back(op);+ }+ iter = obank.beginIndirect(addr);+ enditer = obank.endIndirect(addr);+ while(iter != enditer) {+ PcodeOp *op = (*iter).second;+ ++iter;+ if (!op->isDead())+ res.push_back(op);+ }+}+/// Do in-place replacement of/// - `c <= x` with `c-1 < x` OR/// - `x <= c` with `x < c+1`diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.ccindex aeebe9dfc4..c90327a4a7 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.cc@@ -123,6 +123,8 @@ void IfaceDecompCapability::registerCommands(IfaceStatus *status)status->registerCom(new IfcLockPrototype(),"prototype","lock");status->registerCom(new IfcUnlockPrototype(),"prototype","unlock");status->registerCom(new IfcCommentInstr(),"comment","instruction");+ status->registerCom(new IfcFindVarnodeHash(),"find","varnode","hash");+ status->registerCom(new IfcFindOpHash(),"find","op","hash");status->registerCom(new IfcDuplicateHash(),"duplicate","hash");status->registerCom(new IfcCallGraphBuild(),"callgraph","build");status->registerCom(new IfcCallGraphBuildQuick(),"callgraph","build","quick");@@ -2697,6 +2699,55 @@ void IfcDuplicateHash::check(Funcdata *fd,ostream &s)}}+/// \class IfcFindVarnodeHash+/// \brief Find a Varnode in the current function given a hash and address+///+/// If a matching Varnode is found, info is displayed. Otherwise "Varnode not found" is displayed.+void IfcFindVarnodeHash::execute(istream &s)++{+ if (dcp->conf == (Architecture *)0)+ throw IfaceExecutionError("Image not loaded");+ if (dcp->fd == (Funcdata *)0)+ throw IfaceExecutionError("No function selected");+ int4 size;+ Address addr = parse_machaddr(s,size,*dcp->conf->types); // Read required address+ uint8 hash = 0;+ s >> ws >> hex >> hash;+ DynamicHash dynamic;+ Varnode *vn = dynamic.findVarnode(dcp->fd,addr,hash);+ if (vn == (Varnode *)0)+ *status->optr << "Varnode not found" << endl;+ else {+ vn->printInfo(*status->optr);+ }+}++/// \class IfcFindOpHash+/// \brief Find a PcodeOp in the current function given a hash and address+///+/// If a matching PcodeOp is found, info is displayed. Otherwise "Op not found" is displayed.+void IfcFindOpHash::execute(istream &s)++{+ if (dcp->conf == (Architecture *)0)+ throw IfaceExecutionError("Image not loaded");+ if (dcp->fd == (Funcdata *)0)+ throw IfaceExecutionError("No function selected");+ int4 size;+ Address addr = parse_machaddr(s,size,*dcp->conf->types); // Read required address+ uint8 hash = 0;+ s >> ws >> hex >> hash;+ DynamicHash dynamic;+ PcodeOp *op = dynamic.findOp(dcp->fd,addr,hash);+ if (op == (PcodeOp *)0)+ *status->optr << "Op not found" << endl;+ else {+ op->printRaw(*status->optr);+ *status->optr << endl;+ }+}+/// \class IfcDuplicateHash/// \brief Check for duplicate hashes in functions: `duplicate hash`///diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.hhindex 60fc62ada4..9651c79f74 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ifacedecomp.hh@@ -500,12 +500,21 @@ public:virtual void execute(istream &s);};+class IfcFindVarnodeHash : public IfaceDecompCommand {+public:+ virtual void execute(istream &s);+};++class IfcFindOpHash : public IfaceDecompCommand {+public:+ virtual void execute(istream &s);+};+class IfcDuplicateHash : public IfaceDecompCommand {public:virtual void execute(istream &s);virtual void iterationCallback(Funcdata *fd);static void check(Funcdata *fd,ostream &s); ///< Check for duplicate hashes in given function-};class IfcCallGraphDump : public IfaceDecompCommand {diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/op.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/op.ccindex 547d87fa36..55de3ff93b 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/op.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/op.cc@@ -1010,6 +1010,22 @@ PcodeOp *PcodeOpBank::create(int4 inputs,const SeqNum &sq)return op;}+/// A new PcodeOp is allocated, in alternate storage, with the indicated number+/// of input slots, which start out empty. A sequence number is assigned, and+/// the op is added to the end of the \e dead list.+/// \param inputs is the number of input slots+/// \param pc is the Address to associate with the PcodeOp+/// \return the newly allocated PcodeOp+PcodeOp *PcodeOpBank::createIndirect(int4 inputs,const Address &addr)++{+ PcodeOp *op = new PcodeOp(2,SeqNum(addr,uniqid++));+ alttree[op->getSeqNum()] = op;+ op->setFlag(PcodeOp::dead); // Start out life as dead+ op->insertiter = deadlist.insert(deadlist.end(),op);+ return op;+}+void PcodeOpBank::destroyDead(void){@@ -1034,7 +1050,10 @@ void PcodeOpBank::destroy(PcodeOp *op)if (!op->isDead())throw LowlevelError("Deleting integrated op");- optree.erase(op->getSeqNum());+ if (op->code() == CPUI_INDIRECT)+ alttree.erase(op->getSeqNum());+ else+ optree.erase(op->getSeqNum());deadlist.erase(op->insertiter);removeFromCodeList(op);deadandgone.push_back(op);@@ -1047,8 +1066,13 @@ void PcodeOpBank::destroy(PcodeOp *op)void PcodeOpBank::changeOpcode(PcodeOp *op,TypeOp *newopc){- if (op->opcode != (TypeOp *)0)+ if (op->opcode != (TypeOp *)0) {removeFromCodeList(op);+ if (op->code() == CPUI_INDIRECT) {+ alttree.erase(op->start);+ optree[op->start] = op;+ }+ }op->setOpcode( newopc );addToCodeList(op);}@@ -1142,8 +1166,9 @@ PcodeOp *PcodeOpBank::findOp(const SeqNum &num) const{PcodeOpTree::const_iterator iter = optree.find(num);- if (iter == optree.end()) return (PcodeOp *)0;- return (*iter).second;+ if (iter != optree.end())+ return (*iter).second;+ return (PcodeOp *)0;}/// If PcodeOps exist at the address, the one with the biggest getTime() is returned.@@ -1201,18 +1226,30 @@ PcodeOp *PcodeOpBank::fallthru(const PcodeOp *op) constreturn op->nextOp();}-PcodeOpTree::const_iterator PcodeOpBank::begin(const Address &addr) const+PcodeOpTree::const_iterator PcodeOpBank::beginMain(const Address &addr) const{return optree.lower_bound(SeqNum(addr,0));}-PcodeOpTree::const_iterator PcodeOpBank::end(const Address &addr) const+PcodeOpTree::const_iterator PcodeOpBank::endMain(const Address &addr) const{return optree.upper_bound(SeqNum(addr,~((uintm)0)));}+PcodeOpTree::const_iterator PcodeOpBank::beginIndirect(const Address &addr) const++{+ return alttree.lower_bound(SeqNum(addr,0));+}++PcodeOpTree::const_iterator PcodeOpBank::endIndirect(const Address &addr) const++{+ return alttree.upper_bound(SeqNum(addr,~((uintm)0)));+}+list<PcodeOp *>::const_iterator PcodeOpBank::begin(OpCode opc) const{@@ -1261,6 +1298,7 @@ void PcodeOpBank::clear(void)for(iter=deadandgone.begin();iter!=deadandgone.end();++iter)delete *iter;optree.clear();+ alttree.clear();alivelist.clear();deadlist.clear();clearCodeLists();diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/op.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/op.hhindex a485fdd4ae..e1d4817fce 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/op.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/op.hh@@ -295,6 +295,7 @@ typedef map<SeqNum,PcodeOp *> PcodeOpTree;/// Several lists group PcodeOps with important op-codes (like STORE and RETURN).class PcodeOpBank {PcodeOpTree optree; ///< The main sequence number sort+ PcodeOpTree alttree; ///< Sequence number sort of the quarantined CPUI_INDIRECT opslist<PcodeOp *> deadlist; ///< List of \e dead PcodeOpslist<PcodeOp *> alivelist; ///< List of \e alive PcodeOpslist<PcodeOp *> storelist; ///< List of STORE PcodeOps@@ -314,6 +315,7 @@ public:uintm getUniqId(void) const { return uniqid; } ///< Get the next unique idPcodeOp *create(int4 inputs,const Address &pc); ///< Create a PcodeOp with at a given AddressPcodeOp *create(int4 inputs,const SeqNum &sq); ///< Create a PcodeOp with a given sequence number+ PcodeOp *createIndirect(int4 inputs,const Address &pc); ///< Create a PcodeOp in alternate storagevoid destroy(PcodeOp *op); ///< Destroy/retire the given PcodeOpvoid destroyDead(void); ///< Destroy/retire all PcodeOps in the \e dead listvoid changeOpcode(PcodeOp *op,TypeOp *newopc); ///< Change the op-code for the given PcodeOp@@ -328,17 +330,29 @@ public:PcodeOp *findLastOp(const Address &addr) const; ///< Find the last PcodeOp in sequence for the given addressPcodeOp *fallthru(const PcodeOp *op) const; ///< Find the PcodeOp considered a \e fallthru of the given PcodeOp- /// \brief Start of all PcodeOps in sequence number order- PcodeOpTree::const_iterator beginAll(void) const { return optree.begin(); }+ /// \brief Start of PcodeOps in sequence number order (excluding CPUI_INDIRECT)+ PcodeOpTree::const_iterator beginMain(void) const { return optree.begin(); }- /// \brief End of all PcodeOps in sequence number order- PcodeOpTree::const_iterator endAll(void) const { return optree.end(); }+ /// \brief End of PcodeOps in sequence number order (excluding CPUI_INDIRECT)+ PcodeOpTree::const_iterator endMain(void) const { return optree.end(); }- /// \brief Start of all PcodeOps at one Address- PcodeOpTree::const_iterator begin(const Address &addr) const;+ /// \brief Start of PcodeOps at one Address (excluding CPUI_INDIRECT)+ PcodeOpTree::const_iterator beginMain(const Address &addr) const;- /// \brief End of all PcodeOps at one Address- PcodeOpTree::const_iterator end(const Address &addr) const;+ /// \brief End of PcodeOps at one Address (excluding CPUI_INDIRECT)+ PcodeOpTree::const_iterator endMain(const Address &addr) const;++ /// \brief Start of INDIRECT ops in sequence number order+ PcodeOpTree::const_iterator beginIndirect(void) const { return alttree.begin(); }++ /// \brief End of INDIRECT ops in sequence number order+ PcodeOpTree::const_iterator endIndirect(void) const { return alttree.end(); }++ /// \brief Start of INDIRECT ops at one Address+ PcodeOpTree::const_iterator beginIndirect(const Address &addr) const;++ /// \brief End of INDIRECT ops at one Address+ PcodeOpTree::const_iterator endIndirect(const Address &addr) const;/// \brief Start of all PcodeOps marked as \e alivelist<PcodeOp *>::const_iterator beginAlive(void) const { return alivelist.begin(); }diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.ccindex 21c026852d..735484399e 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc@@ -22,12 +22,31 @@ namespace ghidra {/// \class RuleEarlyRemoval/// \brief Get rid of unused PcodeOp objects where we can guarantee the output is unused+void RuleEarlyRemoval::getOpList(vector<uint4> &oplist) const++{+ uint4 list[] = {+ CPUI_COPY, CPUI_LOAD, CPUI_CALLOTHER,+ CPUI_INT_EQUAL, CPUI_INT_NOTEQUAL, CPUI_INT_SLESS, CPUI_INT_SLESSEQUAL, CPUI_INT_LESS, CPUI_INT_LESSEQUAL,+ CPUI_INT_ZEXT, CPUI_INT_SEXT, CPUI_INT_ADD, CPUI_INT_SUB, CPUI_INT_CARRY, CPUI_INT_SCARRY, CPUI_INT_SBORROW,+ CPUI_INT_2COMP, CPUI_INT_NEGATE, CPUI_INT_XOR, CPUI_INT_AND, CPUI_INT_OR, CPUI_INT_LEFT, CPUI_INT_RIGHT,+ CPUI_INT_SRIGHT, CPUI_INT_MULT, CPUI_INT_DIV, CPUI_INT_SDIV, CPUI_INT_REM, CPUI_INT_SREM,+ CPUI_BOOL_NEGATE, CPUI_BOOL_XOR, CPUI_BOOL_AND, CPUI_BOOL_OR, CPUI_FLOAT_EQUAL, CPUI_FLOAT_NOTEQUAL,+ CPUI_FLOAT_LESS, CPUI_FLOAT_LESSEQUAL, CPUI_FLOAT_NAN, CPUI_FLOAT_ADD, CPUI_FLOAT_DIV, CPUI_FLOAT_MULT,+ CPUI_FLOAT_SUB, CPUI_FLOAT_NEG, CPUI_FLOAT_ABS, CPUI_FLOAT_SQRT, CPUI_FLOAT_INT2FLOAT, CPUI_FLOAT_FLOAT2FLOAT,+ CPUI_FLOAT_TRUNC, CPUI_FLOAT_CEIL, CPUI_FLOAT_FLOOR, CPUI_FLOAT_ROUND,+ CPUI_MULTIEQUAL, CPUI_PIECE, CPUI_SUBPIECE, CPUI_CAST, CPUI_PTRADD, CPUI_PTRSUB, CPUI_SEGMENTOP, CPUI_CPOOLREF,+ CPUI_NEW, CPUI_INSERT, CPUI_ZPULL, CPUI_POPCOUNT, CPUI_LZCOUNT, CPUI_SPULL+ };+ oplist.insert(oplist.end(),list,list+65);+}+int4 RuleEarlyRemoval::applyOp(PcodeOp *op,Funcdata &data){Varnode *vn;- if (op->isCall()) return 0; // Functions automatically consumed+ if (op->isCall()) return 0; // CALLOTHER treated as a call is not removedif (op->isIndirectSource()) return 0;vn = op->getOut();if (vn == (Varnode *)0) return 0;@@ -43,35 +62,6 @@ int4 RuleEarlyRemoval::applyOp(PcodeOp *op,Funcdata &data)return 1;}-// void RuleAddrForceRelease::getOpList(vector<uint4> &oplist) const--// {-// oplist.push_back(CPUI_COPY);-// }--// int4 RuleAddrForceRelease::applyOp(PcodeOp *op,Funcdata &data)--// { // Clear addrforce if op->Output is contained in input-// if (!op->Output()->isAddrForce()) return 0;-// Varnode *outvn,*invn;-// PcodeOp *subop;--// outvn = op->Input(0)();-// if (outvn->getAddr() != op->Output()->getAddr()) return 0;-// if (!outvn->isWritten()) return 0;-// subop = outvn->Def();-// invn = subop->Input(0);-// if (subop->code() == CPUI_SUBPIECE) {-// if (0!=invn->contains(*outvn)) return 0;-// if (!invn->terminated()) return 0; // Bigger thing is already terminated-// }-// else-// return 0;--// data.clear_addrforce(invn); // Clear addrforce for anything contained by input-// return 1;-// }-/// Given a Varnode term in the expression, check if the last operation producing it/// is to multiply by a constant. If so pass back the constant coefficient and/// return the underlying Varnode. Otherwise pass back the constant 1, and return@@ -1006,11 +996,9 @@ int4 RulePullsubIndirect::applyOp(PcodeOp *op,Funcdata &data)if (small1 == (Varnode *)0)small1 = RulePullsubMulti::buildSubpiece(basevn,newSize,op->getIn(1)->getOffset(),data);// Create new indirect near original indirect- new_ind = data.newOp(2,indir->getAddr());- data.opSetOpcode(new_ind,CPUI_INDIRECT);+ new_ind = data.newIndirect(targ_op);small2 = data.newVarnodeOut(newSize,smalladdr2,new_ind);data.opSetInput(new_ind,small1,0);- data.opSetInput(new_ind,data.newVarnodeIop(targ_op),1);data.opInsertBefore(new_ind,indir);}@@ -3829,6 +3817,23 @@ int4 RuleShiftPiece::applyOp(PcodeOp *op,Funcdata &data)/// \class RuleCollapseConstants/// \brief Collapse constant expressions+void RuleCollapseConstants::getOpList(vector<uint4> &oplist) const++{+ uint4 list[] = {+ CPUI_INT_EQUAL, CPUI_INT_NOTEQUAL, CPUI_INT_SLESS, CPUI_INT_SLESSEQUAL, CPUI_INT_LESS, CPUI_INT_LESSEQUAL,+ CPUI_INT_ZEXT, CPUI_INT_SEXT, CPUI_INT_ADD, CPUI_INT_SUB, CPUI_INT_CARRY, CPUI_INT_SCARRY, CPUI_INT_SBORROW,+ CPUI_INT_2COMP, CPUI_INT_NEGATE, CPUI_INT_XOR, CPUI_INT_AND, CPUI_INT_OR, CPUI_INT_LEFT, CPUI_INT_RIGHT,+ CPUI_INT_SRIGHT, CPUI_INT_MULT, CPUI_INT_DIV, CPUI_INT_SDIV, CPUI_INT_REM, CPUI_INT_SREM,+ CPUI_BOOL_NEGATE, CPUI_BOOL_XOR, CPUI_BOOL_AND, CPUI_BOOL_OR, CPUI_FLOAT_EQUAL, CPUI_FLOAT_NOTEQUAL,+ CPUI_FLOAT_LESS, CPUI_FLOAT_LESSEQUAL, CPUI_FLOAT_NAN, CPUI_FLOAT_ADD, CPUI_FLOAT_DIV, CPUI_FLOAT_MULT,+ CPUI_FLOAT_SUB, CPUI_FLOAT_NEG, CPUI_FLOAT_ABS, CPUI_FLOAT_SQRT, CPUI_FLOAT_INT2FLOAT, CPUI_FLOAT_FLOAT2FLOAT,+ CPUI_FLOAT_TRUNC, CPUI_FLOAT_CEIL, CPUI_FLOAT_FLOOR, CPUI_FLOAT_ROUND,+ CPUI_PIECE, CPUI_SUBPIECE, CPUI_POPCOUNT, CPUI_LZCOUNT+ };+ oplist.insert(oplist.end(),list,list+52);+}+int4 RuleCollapseConstants::applyOp(PcodeOp *op,Funcdata &data){@@ -3837,15 +3842,8 @@ int4 RuleCollapseConstants::applyOp(PcodeOp *op,Funcdata &data)if (!op->isCollapsible()) return 0; // Expression must be collapsible- Address newval;bool markedInput = false;- try {- newval = data.getArch()->getConstant(op->collapse(markedInput));- }- catch(LowlevelError &err) {- data.opMarkNoCollapse(op); // Dont know how or dont want to collapse further- return 0;- }+ Address newval = data.getArch()->getConstant(op->collapse(markedInput));vn = data.newVarnode(op->getOut()->getSize(),newval); // Create new collapsed constantif (markedInput) {@@ -4713,62 +4711,6 @@ int4 RuleConcatCommute::applyOp(PcodeOp *op,Funcdata &data)return 0;}-// void RuleIndirectConcat::getOpList(vector<uint4> &oplist) const--// {-// oplist.push_back(CPUI_INDIRECT);-// }--// int4 RuleIndirectConcat::applyOp(PcodeOp *op,Funcdata &data)--// {-// Varnode *vn = op->getIn(0);-// if (!vn->isWritten()) return 0;-// PcodeOp *concatop = vn->getDef();-// if (concatop->code() != CPUI_PIECE) return 0;-// Varnode *vnhi = concatop->getIn(0);-// Varnode *vnlo = concatop->getIn(1);-// if (vnhi->isFree() || vnhi->isVolatile() || vnhi->isSpacebase()) return 0;-// if (vnlo->isFree() || vnlo->isVolatile() || vnlo->isSpacebase()) return 0;-// if (op->getIn(1)->getSpace()->getType() != IPTR_IOP) return 0;-// PcodeOp *indop = PcodeOp::getOpFromConst(op->getIn(1)->getAddr());-// Varnode *newvnhi,*newvnlo;-// Varnode *outvn = op->getOut();-// data.splitVarnode(outvn,vnlo->getSize(),newvnlo,newvnhi);-// PcodeOp *newophi,*newoplo;--// newophi = data.newOp(2,indop->getAddr());-// newoplo = data.newOp(2,indop->getAddr());-// data.opSetOpcode(newophi,CPUI_INDIRECT);-// data.opSetOpcode(newoplo,CPUI_INDIRECT);-// data.opSetOutput(newophi,newvnhi);-// data.opSetOutput(newoplo,newvnlo);-// data.opSetInput(newophi,vnhi,0);-// data.opSetInput(newoplo,vnlo,0);-// data.opSetInput(newophi,data.newVarnodeIop(indop),1);-// data.opSetInput(newoplo,data.newVarnodeIop(indop),1);-// data.opInsertBefore(newophi,indop);-// data.opInsertBefore(newoplo,indop);--// // The original INDIRECT is basically dead at this point, so we clear any addrforce so it can be-// // removed as deadcode-// outvn->clearAddrForce();-// if (outvn->hasNoDescend()) { // If nobody else uses the value-// // Prepare op for deletion, and so that the same rule won't trigger again-// data.opSetOpcode(op,CPUI_COPY);-// data.opRemoveInput(op,1);-// }-// else { // If the original INDIRECT output was used by other ops-// // We recycle the op as the commuted concatenation-// data.opUninsert(op); // Remove op from before (simultaneous) execution with indop-// data.opSetOpcode(op,CPUI_PIECE);-// data.opSetInput(op,newvnhi,0);-// data.opSetInput(op,newvnlo,1);-// data.opInsertAfter(op,indop); // Insert recycled PIECE after the indop-// }-// return 1;-// }-/// \class RuleConcatZext/// \brief Commute PIECE with INT_ZEXT: `concat(zext(V),W) => zext(concat(V,W))`void RuleConcatZext::getOpList(vector<uint4> &oplist) const@@ -9054,14 +8996,40 @@ void RulePtrFlow::getOpList(vector<uint4> &oplist) constoplist.push_back(CPUI_LOAD);oplist.push_back(CPUI_COPY);oplist.push_back(CPUI_MULTIEQUAL);- oplist.push_back(CPUI_INDIRECT);oplist.push_back(CPUI_INT_ADD);+ oplist.push_back(CPUI_CALL);oplist.push_back(CPUI_CALLIND);oplist.push_back(CPUI_BRANCHIND);oplist.push_back(CPUI_PTRSUB);oplist.push_back(CPUI_PTRADD);}+/// Propagate \e ptrflow property across INDIRECTs for the given op+///+/// \param op is the op causing the indirect effects+/// \return \b true if a change was made+bool RulePtrFlow::walkIndirects(PcodeOp *op)++{+ list<PcodeOp *>::const_iterator iter = op->getBasicIter();+ list<PcodeOp *>::const_iterator beginIter = op->getParent()->beginOp();+ bool madeChange = false;+ while(iter != beginIter) {+ --iter;+ PcodeOp *indop = *iter;+ if (indop->code() != CPUI_INDIRECT) break;+ if (!indop->isPtrFlow()) continue;+ Varnode *vn = indop->getOut();+ if (propagateFlowToReads(vn))+ madeChange = true;+ vn = indop->getIn(0);+ if (propagateFlowToDef(vn))+ madeChange = true;+ break;+ }+ return madeChange;+}+/// Set \e ptrflow property on PcodeOp only if it is propagating////// \param op is the PcodeOp@@ -9169,6 +9137,15 @@ int4 RulePtrFlow::applyOp(PcodeOp *op,Funcdata &data)switch(op->code()) {case CPUI_LOAD:+ vn = op->getIn(1);+ spc = op->getIn(0)->getSpaceFromConst();+ if (vn->getSize() > spc->getAddrSize()) {+ vn = truncatePointer(spc,op,vn,1,data);+ madeChange = 1;+ }+ if (propagateFlowToDef(vn))+ madeChange = 1;+ break;case CPUI_STORE:vn = op->getIn(1);spc = op->getIn(0)->getSpaceFromConst();@@ -9178,9 +9155,14 @@ int4 RulePtrFlow::applyOp(PcodeOp *op,Funcdata &data)}if (propagateFlowToDef(vn))madeChange = 1;+ if (walkIndirects(op))+ madeChange = 1;+ break;+ case CPUI_CALL:+ if (walkIndirects(op))+ madeChange = 1;break;case CPUI_CALLIND:- case CPUI_BRANCHIND:vn = op->getIn(0);spc = data.getArch()->getDefaultCodeSpace();if (vn->getSize() > spc->getAddrSize()) {@@ -9189,20 +9171,23 @@ int4 RulePtrFlow::applyOp(PcodeOp *op,Funcdata &data)}if (propagateFlowToDef(vn))madeChange = 1;+ if (walkIndirects(op))+ madeChange = 1;break;- case CPUI_NEW:- vn = op->getOut();- if (propagateFlowToReads(vn))+ case CPUI_BRANCHIND:+ vn = op->getIn(0);+ spc = data.getArch()->getDefaultCodeSpace();+ if (vn->getSize() > spc->getAddrSize()) {+ vn = truncatePointer(spc,op,vn,0,data);+ madeChange = 1;+ }+ if (propagateFlowToDef(vn))madeChange = 1;break;- case CPUI_INDIRECT:- if (!op->isPtrFlow()) return 0;+ case CPUI_NEW:vn = op->getOut();if (propagateFlowToReads(vn))madeChange = 1;- vn = op->getIn(0);- if (propagateFlowToDef(vn))- madeChange = 1;break;case CPUI_COPY:case CPUI_PTRSUB:diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hhindex fff63200e9..0f14b574b2 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh@@ -89,15 +89,9 @@ public:if (!grouplist.contains(getGroup())) return (Rule *)0;return new RuleEarlyRemoval(getGroup());}- // This rule applies to all ops+ virtual void getOpList(vector<uint4> &oplist) const;virtual int4 applyOp(PcodeOp *op,Funcdata &data);};-// class RuleAddrForceRelease : public Rule {-// public:-// RuleAddrForceRelease(const string &g) : Rule(g, 0, "addrforcerelease") {} ///< Constructor-// virtual void getOpList(vector<uint4> &oplist) const;-// virtual int4 applyOp(PcodeOp *op,Funcdata &data);-// };class RuleCollectTerms : public Rule {static Varnode *getMultCoeff(Varnode *vn,uintb &coef); ///< Get the multiplicative coefficientpublic:@@ -707,7 +701,7 @@ public:if (!grouplist.contains(getGroup())) return (Rule *)0;return new RuleCollapseConstants(getGroup());}- // applies to all opcodes+ virtual void getOpList(vector<uint4> &oplist) const;virtual int4 applyOp(PcodeOp *op,Funcdata &data);};class RuleTransformCpool : public Rule {@@ -856,16 +850,6 @@ public:virtual void getOpList(vector<uint4> &oplist) const;virtual int4 applyOp(PcodeOp *op,Funcdata &data);};-// class RuleIndirectConcat : public Rule {-// public:-// RuleIndirectConcat(const string &g) : Rule(g, 0, "indirectconcat") {} ///< Constructor-// virtual Rule *clone(const ActionGroupList &grouplist) const {-// if (!grouplist.contains(getGroup())) return (Rule *)0;-// return new RuleIndirectConcat(getGroup());-// }-// virtual void getOpList(vector<uint4> &oplist) const;-// virtual int4 applyOp(PcodeOp *op,Funcdata &data);-// };class RuleConcatZext : public Rule {public:RuleConcatZext(const string &g) : Rule(g, 0, "concatzext") {} ///< Constructor@@ -1411,6 +1395,7 @@ public:class RulePtrFlow : public Rule {Architecture *glb; ///< The address space managerbool hasTruncations; ///< \b true if this architecture needs truncated pointers+ bool walkIndirects(PcodeOp *op);bool trialSetPtrFlow(PcodeOp *op);bool propagateFlowToDef(Varnode *vn);bool propagateFlowToReads(Varnode *vn);diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/subflow.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/subflow.ccindex 4e9f9a2b5d..c0e7084098 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/subflow.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/subflow.cc@@ -1799,16 +1799,18 @@ bool SplitFlow::addOp(PcodeOp *op,TransformVar *rvn,int4 slot)if (outvn->getDef() != (TransformOp *)0)return true; // Already traversed- TransformOp *loOp = newOpReplace(op->numInput(), op->code(), op);- TransformOp *hiOp = newOpReplace(op->numInput(), op->code(), op);+ TransformOp *loOp;+ TransformOp *hiOp;int4 numParam = op->numInput();if (op->code() == CPUI_INDIRECT) {- opSetInput(loOp,newIop(op->getIn(1)),1);- opSetInput(hiOp,newIop(op->getIn(1)),1);- loOp->inheritIndirect(op);- hiOp->inheritIndirect(op);+ loOp = newIndirectReplace(op);+ hiOp = newIndirectReplace(op);numParam = 1;}+ else {+ loOp = newOpReplace(op->numInput(), op->code(), op);+ hiOp = newOpReplace(op->numInput(), op->code(), op);+ }for(int4 i=0;i<numParam;++i) {TransformVar *invn;if (i == slot)@@ -3716,11 +3718,9 @@ bool LaneDivide::buildIndirect(PcodeOp *op,TransformVar *outVars,int4 numLanes,iTransformVar *inVn = setReplacement(op->getIn(0), numLanes, skipLanes);if (inVn == (TransformVar *)0) return false;for(int4 i=0;i<numLanes;++i) {- TransformOp *rop = newOpReplace(2, CPUI_INDIRECT, op);+ TransformOp *rop = newIndirectReplace(op);opSetOutput(rop, outVars + i);opSetInput(rop,inVn + i, 0);- opSetInput(rop,newIop(op->getIn(1)),1);- rop->inheritIndirect(op);}return true;}diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/transform.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/transform.ccindex 692e3061b1..33d98e9504 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/transform.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/transform.cc@@ -208,12 +208,6 @@ void TransformVar::createReplacement(Funcdata *fd)fd->transferVarnodeProperties(vn,replacement,bytePos);break;}- case TransformVar::constant_iop:- {- PcodeOp *indeffect = PcodeOp::getOpFromConst(Address(fd->getArch()->getIopSpace(),val));- replacement = fd->newVarnodeIop(indeffect);- break;- }default:throw LowlevelError("Bad TransformVar type");}@@ -235,6 +229,12 @@ void TransformOp::createReplacement(Funcdata *fd)while(op->numInput() < input.size())fd->opInsertInput(op, (Varnode *)0, op->numInput()-1);}+ else if ((special & TransformOp::op_indirect)!=0) {+ PcodeOp *target = PcodeOp::getOpFromConst(op->getIn(1)->getAddr());+ replacement = fd->newIndirect(target);+ output->createReplacement(fd);+ fd->opInsertBefore(replacement,op);+ }else {replacement = fd->newOp(input.size(),op->getAddr());fd->opSetOpcode(replacement, opc);@@ -268,19 +268,6 @@ bool TransformOp::attemptInsertion(Funcdata *fd)return true; // Already inserted}-/// Prepare to build the transformed INDIRECT PcodeOp based on settings from the given INDIRECT.-/// \param indOp is the given INDIRECT-void TransformOp::inheritIndirect(PcodeOp *indOp)--{- if (indOp->isIndirectCreation()) {- if (indOp->getIn(0)->isIndirectZero())- special |= TransformOp::indirect_creation;- else- special |= TransformOp::indirect_creation_possible_out;- }-}-void LanedRegister::LanedIterator::normalize(void){@@ -405,18 +392,6 @@ TransformVar *TransformManager::newConstant(int4 size,int4 lsbOffset,uintb val)return res;}-/// Used for creating INDIRECT placeholders.-/// \param vn is the original iop parameter to the INDIRECT-/// \return the new placeholder node-TransformVar *TransformManager::newIop(Varnode *vn)--{- newVarnodes.emplace_back();- TransformVar *res = &newVarnodes.back();- res->initialize(TransformVar::constant_iop,(Varnode *)0,vn->getSize()*8,vn->getSize(),vn->getOffset());- return res;-}-/// Given a single logical value within a larger Varnode, create a placeholder for/// that logical value./// \param vn is the large Varnode@@ -527,6 +502,33 @@ TransformOp *TransformManager::newOpReplace(int4 numParams,OpCode opc,PcodeOp *rreturn &rop;}+/// \brief Create a new placeholder op to replace an existing INDIRECT op+///+/// A placeholder for the new op is created. The replacement will have the same+/// target op as the original.+/// \param replace is the existing INDIRECT the new op will replace+/// \return the new placeholder node+TransformOp *TransformManager::newIndirectReplace(PcodeOp *replace)++{+ newOps.emplace_back();+ TransformOp &rop(newOps.back());+ rop.op = replace;+ rop.replacement = (PcodeOp *)0;+ rop.opc = CPUI_INDIRECT;+ rop.special = TransformOp::op_replacement | TransformOp::op_indirect;+ if (replace->isIndirectCreation()) {+ if (replace->getIn(0)->isIndirectZero())+ rop.special |= TransformOp::indirect_creation;+ else+ rop.special |= TransformOp::indirect_creation_possible_out;+ }+ rop.output = (TransformVar *)0;+ rop.follow = (TransformOp *)0;+ rop.input.resize(1,(TransformVar *)0);+ return &rop;+}+/// \brief Create a new placeholder op that will not replace an existing op////// An uninitialized placeholder for the new op is created. When (if) the new op is createddiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/transform.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/transform.hhindex 58bdecb44c..0271143839 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/transform.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/transform.hh@@ -38,8 +38,7 @@ public:preexisting = 2, ///< Varnode preexisted in the original data-flownormal_temp = 3, ///< A new temporary (unique space) Varnodepiece_temp = 4, ///< A temporary representing a piece of an original Varnode- constant = 5, ///< A new constant Varnode- constant_iop = 6, ///< Special iop constant encoding a PcodeOp reference+ constant = 5 ///< A new constant Varnode};/// \brief Flags for a TransformVarenum {@@ -71,8 +70,9 @@ public:enum {op_replacement = 1, ///< Op replaces an existing opop_preexisting = 2, ///< Op already exists (but will be transformed)- indirect_creation = 4, ///< Mark op as indirect creation- indirect_creation_possible_out = 8 ///< Mark op as indirect creation and possible call output+ op_indirect = 4, ///< Op is a new INDIRECT+ indirect_creation = 8, ///< Mark op as indirect creation+ indirect_creation_possible_out = 0x10 ///< Mark op as indirect creation and possible call output};private:PcodeOp *op; ///< Original op which \b this is splitting (or null)@@ -87,7 +87,6 @@ private:public:TransformVar *getOut(void) const { return output; } ///< Get the output placeholder variable for \b this operatorTransformVar *getIn(int4 i) const { return input[i]; } ///< Get the i-th input placeholder variable for \b this- void inheritIndirect(PcodeOp *indOp); ///< Set \e indirect \e creation flags for \b this based on given INDIRECT};/// \brief Describes a (register) storage location and the ways it might be split into lanes@@ -174,11 +173,11 @@ public:TransformVar *newPreexistingVarnode(Varnode *vn); ///< Make placeholder for preexisting VarnodeTransformVar *newUnique(int4 size); ///< Make placeholder for new unique space VarnodeTransformVar *newConstant(int4 size,int4 lsbOffset,uintb val); ///< Make placeholder for constant Varnode- TransformVar *newIop(Varnode *vn); ///< Make placeholder for special iop constantTransformVar *newPiece(Varnode *vn,int4 bitSize,int4 lsbOffset); ///< Make placeholder for piece of a VarnodeTransformVar *newSplit(Varnode *vn,const LaneDescription &description);TransformVar *newSplit(Varnode *vn,const LaneDescription &description,int4 numLanes,int4 startLane);TransformOp *newOpReplace(int4 numParams,OpCode opc,PcodeOp *replace);+ TransformOp *newIndirectReplace(PcodeOp *replace);TransformOp *newOp(int4 numParams,OpCode opc,TransformOp *follow);TransformOp *newPreexistingOp(int4 numParams,OpCode opc,PcodeOp *originalOp);