Ghidra is a software reverse engineering (SRE) framework
GP-7406 Align ProtoParameters and Varnodes in FuncCallSpecs
caheckmana23522dc1fda
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.ccindex 4642049149..56bae633f7 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc@@ -1562,11 +1562,14 @@ void ActionFuncLink::funcLinkInput(FuncCallSpecs *fc,Funcdata &data)bool setplaceholder = varargs;for(int4 i=0;i<numparam;++i) {ProtoParameter *param = fc->getParam(i);- if (!param->hasStorage()) continue;- active->registerTrial(param->getAddress(),param->getSize());- active->getTrial(i).markActive(); // Parameter is not optional+ if (!param->hasStorage()) {+ data.opInsertInput(op,data.newConstant(param->getSize(),0),op->numInput()); // Put #0 in unassigned slot+ continue;+ }+ ParamTrial ¶mtrial(active->registerTrial(param->getAddress(),param->getSize()));+ paramtrial.markActive(); // Parameter is not optionalif (varargs){- active->getTrial(i).setFixedPosition(i);+ paramtrial.setFixedPosition(i);}AddrSpace *spc = param->getAddress().getSpace();uintb off = param->getAddress().getOffset();@@ -5025,10 +5028,8 @@ void ActionInputPrototype::markActiveInputRefs(ParamActive &active,list<InputRefcontinue;if (!data.getFuncProto().possibleInputParam((*iter).addr, (*iter).dataType->getSize()))continue;- int4 slot = active.getNumTrials();- active.registerTrial((*iter).addr,(*iter).dataType->getSize());+ ParamTrial &trial(active.registerTrial((*iter).addr,(*iter).dataType->getSize()));typeList.push_back((*iter).dataType);- ParamTrial &trial(active.getTrial(slot));trial.markActive();}}@@ -5068,7 +5069,6 @@ int4 ActionInputPrototype::apply(Funcdata &data)vn = *iter;++iter;if (data.getFuncProto().possibleInputParam(vn->getAddr(),vn->getSize())) {- int4 slot = active.getNumTrials();if (vn->isPersist()) {int4 sz;Address addr = data.findDisjointCover(vn, sz);@@ -5077,16 +5077,18 @@ int4 ActionInputPrototype::apply(Funcdata &data)ct = useHigh ? vn->getHigh()->getType() : vn->getType();elsect = data.getArch()->types->getBase(sz, TYPE_UNKNOWN);- active.registerTrial(addr,sz);+ ParamTrial ¶mtrial(active.registerTrial(addr,sz));typeList.push_back(ct);+ if (!vn->hasNoDescend())+ paramtrial.markActive(); // Mark as active if it has descendants}else {- active.registerTrial(vn->getAddr(),vn->getSize());+ ParamTrial ¶mtrial(active.registerTrial(vn->getAddr(),vn->getSize()));Datatype *ct = useHigh ? vn->getHigh()->getType() : vn->getType();typeList.push_back(ct);+ if (!vn->hasNoDescend())+ paramtrial.markActive(); // Mark as active if it has descendants}- if (!vn->hasNoDescend())- active.getTrial(slot).markActive(); // Mark as active if it has descendants}}markActiveInputRefs(active, inputRefs, typeList, data);diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.ccindex c9236cd9b6..9ba9444a0e 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.cc@@ -882,9 +882,7 @@ void ParamListStandard::buildTrialMap(ParamActive *active) constint4 sz = curentry->isExclusion() ? curentry->getSize() : curentry->getAlign();int4 nextslot = 0;Address addr = curentry->getAddrBySlot(nextslot,sz,1,glb);- int4 trialpos = active->getNumTrials();- active->registerTrial(addr,sz);- ParamTrial ¶mtrial(active->getTrial(trialpos));+ ParamTrial ¶mtrial(active->registerTrial(addr,sz));paramtrial.markUnref();paramtrial.setEntry(curentry,0);}@@ -913,9 +911,7 @@ void ParamListStandard::buildTrialMap(ParamActive *active) constif (slotlist[j] == 0) {int4 nextslot = j; // Make copy of j, so that getAddrBySlot can change itAddress addr = curentry->getAddrBySlot(nextslot,curentry->getAlign(),1,glb);- int4 trialpos = active->getNumTrials();- active->registerTrial(addr,curentry->getAlign());- ParamTrial ¶mtrial(active->getTrial(trialpos));+ ParamTrial ¶mtrial(active->registerTrial(addr,curentry->getAlign()));paramtrial.markUnref();paramtrial.setEntry(curentry,0);}@@ -1952,10 +1948,12 @@ void ParamActive::clear(void)/// A ParamTrial object is created and a slot is assigned./// \param addr is the starting address of the memory range/// \param sz is the number of bytes in the range-void ParamActive::registerTrial(const Address &addr,int4 sz)+/// \return the new ParamTrial+ParamTrial &ParamActive::registerTrial(const Address &addr,int4 sz){- trial.push_back(ParamTrial(addr,sz,slotbase));+ trial.emplace_back(addr,sz,slotbase);+ ParamTrial &res(trial.back());// It would require too much work to calculate whether a specific data location is changed// by a subfunction, but a fairly strong assumption is that (unless it is explicitly saved) a// register may change and is thus unlikely to be used as a location for passing parameters.@@ -1963,8 +1961,9 @@ void ParamActive::registerTrial(const Address &addr,int4 sz)// Since this all a heuristic for recovering parameters, we assume this rule is always true// to get an efficient testif (addr.getSpace()->getType() != IPTR_SPACEBASE)- trial.back().markKilledByCall();+ res.markKilledByCall();slotbase += 1;+ return res;}/// Copy the memory range of the trial and its boolean properties but assign a new slot.@@ -5047,7 +5046,10 @@ bool FuncCallSpecs::transferLockedInput(vector<Varnode *> &newinput,const FuncPrVarnode *stackref = (Varnode *)0;for(int4 i=0;i<numparams;++i) {ProtoParameter *param = source.getParam(i);- if (!param->hasStorage()) continue;+ if (!param->hasStorage()) {+ newinput.push_back((Varnode *)0);+ continue;+ }int4 reuse = transferLockedInputParam(param);if (reuse == 0) return false;if (reuse > 0)@@ -5126,17 +5128,18 @@ void FuncCallSpecs::commitNewInputs(Funcdata &data,vector<Varnode *> &newinput)activeinput.clear();int4 numparams = numParams();- uint4 slot = 1;+ if (numparams+1 > newinput.size())+ throw LowlevelError("Mismatched Varnode and ProtoParameter");for(int4 i=0;i<numparams;++i) {ProtoParameter *param = getParam(i);- if (!param->hasStorage()) continue;- if (slot >= newinput.size())- throw LowlevelError("Mismatched Varnode and ProtoParameter");- Varnode *vn = buildParam(data,newinput[slot],param,stackref);- newinput[slot] = vn;- slot += 1;- activeinput.registerTrial(param->getAddress(),param->getSize());- activeinput.getTrial(i).markActive(); // Parameter is not optional+ if (!param->hasStorage()) {+ newinput[i+1] = data.newConstant(param->getSize(), 0); // Put #0 in unassigned slot+ continue;+ }+ Varnode *vn = buildParam(data,newinput[i+1],param,stackref);+ newinput[i+1] = vn;+ ParamTrial ¶mtrial(activeinput.registerTrial(param->getAddress(),param->getSize()));+ paramtrial.markActive(); // Parameter is not optionalif (noplacehold&&(param->getAddress().getSpace()->getType() == IPTR_SPACEBASE)) {// We have a locked stack parameter, use it to recover the stack offsetvn->setSpacebasePlaceholder();diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.hhindex efd0d5a3e6..89464af75f 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.hh@@ -300,7 +300,7 @@ class ParamActive {public:ParamActive(bool recoversub); ///< Construct an empty containervoid clear(void); ///< Reset to an empty container- void registerTrial(const Address &addr,int4 sz); ///< Add a new trial to the container+ ParamTrial ®isterTrial(const Address &addr,int4 sz); ///< Add a new trial to the containervoid reregisterTrial(const ParamTrial &oldTrial); ///< Register a previously existing trialint4 getNumTrials(void) const { return trial.size(); } ///< Get the number of trials in \b this containerParamTrial &getTrial(int4 i) { return trial[i]; } ///< Get the i-th trialdiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/typeop.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/typeop.ccindex c688db2a5d..74cc3e4631 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/typeop.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/typeop.cc@@ -699,9 +699,6 @@ Datatype *TypeOpCall::getInputLocal(const PcodeOp *op,int4 slot) const// Get types of call input parametersfc = FuncCallSpecs::getFspecFromConst(vn->getAddr());- // Its false to assume that the parameter symbol corresponds- // to the varnode in the same slot, but this is easiest until- // we get giant sized parameters working properlyProtoParameter *param = fc->getParam(slot - 1);if (param != (ProtoParameter*) 0) {if (param->isTypeLocked()) {diff --git a/Ghidra/Features/Decompiler/src/decompile/unittests/testfuncproto.cc b/Ghidra/Features/Decompiler/src/decompile/unittests/testfuncproto.ccindex 963aae19c7..3a754f4d62 100644--- a/Ghidra/Features/Decompiler/src/decompile/unittests/testfuncproto.cc+++ b/Ghidra/Features/Decompiler/src/decompile/unittests/testfuncproto.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.@@ -459,17 +459,15 @@ TEST(funcproto_mixedmeta) {void registerActive(ParamActive ¶mActive,const string &nm,int4 sz){- paramActive.registerTrial(glb->translate->getRegister(nm).getAddr(),sz);- int4 num = paramActive.getNumTrials();- paramActive.getTrial(num-1).markActive();+ ParamTrial ¶mtrial(paramActive.registerTrial(glb->translate->getRegister(nm).getAddr(),sz));+ paramtrial.markActive();}void stackActive(ParamActive ¶mActive,uintb off,int4 sz){- paramActive.registerTrial(Address(glb->getStackSpace(),off),sz);- int4 num = paramActive.getNumTrials();- paramActive.getTrial(num-1).markActive();+ ParamTrial ¶mtrial(paramActive.registerTrial(Address(glb->getStackSpace(),off),sz));+ paramtrial.markActive();}bool register_used(ParamTrial &trial,const string &nm)