Ghidra is a software reverse engineering (SRE) framework
GP-2436_fixed_args_varags_functions
James109696fad153
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.ccindex 2513d3cb02..245a31e771 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc@@ -1443,6 +1443,9 @@ void ActionFuncLink::funcLinkInput(FuncCallSpecs *fc,Funcdata &data)ProtoParameter *param = fc->getParam(i);active->registerTrial(param->getAddress(),param->getSize());active->getTrial(i).markActive(); // Parameter is not optional+ if (varargs){+ active->getTrial(i).setFixedPosition(i);+ }AddrSpace *spc = param->getAddress().getSpace();uintb off = param->getAddress().getOffset();int4 sz = param->getSize();diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.ccindex 0526ed04c7..64c6379777 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.cc@@ -1701,6 +1701,25 @@ bool ParamTrial::operator<(const ParamTrial &b) constreturn (size < b.size);}+/// Sort by fixed position then by ParamTrial::operator<+/// \param a trial+/// \param b trial+/// \return \b true if \b a should be ordered before \b b+bool ParamTrial::fixedPositionCompare(const ParamTrial &a, const ParamTrial &b)++{+ if (a.fixedPosition == -1 && b.fixedPosition == -1){+ return a < b;+ }+ if (a.fixedPosition == -1){+ return false;+ }+ if (b.fixedPosition == -1){+ return true;+ }+ return a.fixedPosition < b.fixedPosition;+}+/// \param recoversub selects whether a sub-function or the active function is being testedParamActive::ParamActive(bool recoversub)@@ -5326,6 +5345,12 @@ void FuncCallSpecs::buildInputFromTrials(Funcdata &data)newparam.push_back(op->getIn(0)); // Preserve the fspec parameter+ if (isDotdotdot() && isInputLocked()){+ //if varargs, move the fixed args to the beginning of the list in order+ //preserve relative order of variable args+ activeinput.sortFixedPosition();+ }+for(int4 i=0;i<activeinput.getNumTrials();++i) {const ParamTrial ¶mtrial( activeinput.getTrial(i) );if (!paramtrial.isUsed()) continue; // Don't keep unused parametersdiff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.hhindex 7a22c1b390..86c6af060f 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/fspec.hh@@ -221,9 +221,10 @@ private:int4 slot; ///< Slot assigned to this trialconst ParamEntry *entry; ///< PrototypeModel entry matching this trialint4 offset; ///< "justified" offset into entry+ int4 fixedPosition; ///< argument position if a fixed arg of a varargs function, else -1public:/// \brief Construct from components- ParamTrial(const Address &ad,int4 sz,int4 sl) { addr = ad; size = sz; slot = sl; flags=0; entry=(ParamEntry *)0; offset=-1; }+ ParamTrial(const Address &ad,int4 sz,int4 sl) { addr = ad; size = sz; slot = sl; flags=0; entry=(ParamEntry *)0; offset=-1; fixedPosition = -1; }const Address &getAddress(void) const { return addr; } ///< Get the starting address of \b this trialint4 getSize(void) const { return size; } ///< Get the number of bytes in \b this trialint4 getSlot(void) const { return slot; } ///< Get the \e slot associated with \b this trial@@ -259,6 +260,8 @@ public:ParamTrial splitLo(int4 sz) const; ///< Create a trial representing the last part of \b thisbool testShrink(const Address &newaddr,int4 sz) const; ///< Test if \b this trial can be made smallerbool operator<(const ParamTrial &b) const; ///< Sort trials in formal parameter order+ void setFixedPosition(int4 pos) { fixedPosition = pos; } ///< Set fixed position+ static bool fixedPositionCompare(const ParamTrial &a, const ParamTrial &b); ///< Sort by fixed position; stable for fixedPosition = -1};/// \brief Container class for ParamTrial objects@@ -319,6 +322,8 @@ public:/// \param addr is the new range's starting address/// \param sz is the new range's size in bytesvoid shrink(int4 i,const Address &addr,int4 sz) { trial[i].setAddress(addr,sz); }++ void sortFixedPosition(void) {sort(trial.begin(),trial.end(),ParamTrial::fixedPositionCompare);} ///< sort the trials by fixed position then <};/// \brief A special space for encoding FuncCallSpecs