Ghidra is a software reverse engineering (SRE) framework
Decompiler: lane split follows a left shift forward Decompiler: lane division runs again as laned storage appears Decompiler: a piece or zero extension of a laned register that is not a lane size suggests the lane size that tiles it Decompiler: lane split follows a zero extension forward
ai-hakzarov7082093eb7fd
diff --git a/Ghidra/Features/Decompiler/certification.manifest b/Ghidra/Features/Decompiler/certification.manifestindex dbaf56ed40..249a1158a0 100644--- a/Ghidra/Features/Decompiler/certification.manifest+++ b/Ghidra/Features/Decompiler/certification.manifest@@ -51,6 +51,7 @@ src/decompile/datatests/indproto.xml||GHIDRA||||END|src/decompile/datatests/injectoverride.xml||GHIDRA||||END|src/decompile/datatests/inline.xml||GHIDRA||||END|src/decompile/datatests/inlinetarget.xml||GHIDRA||||END|+src/decompile/datatests/laneleft.xml||GHIDRA||||END|src/decompile/datatests/longdouble.xml||GHIDRA||||END|src/decompile/datatests/loopcomment.xml||GHIDRA||||END|src/decompile/datatests/lzcount.xml||GHIDRA||||END|@@ -85,6 +86,7 @@ src/decompile/datatests/revisit.xml||GHIDRA||||END|src/decompile/datatests/sbyte.xml||GHIDRA||||END|src/decompile/datatests/skipnext2.xml||GHIDRA||||END|src/decompile/datatests/stackcorner.xml||GHIDRA||||END|+src/decompile/datatests/stackmatrix.xml||GHIDRA||||END|src/decompile/datatests/stackreturn.xml||GHIDRA||||END|src/decompile/datatests/stackspill.xml||GHIDRA||||END|src/decompile/datatests/stackstring.xml||GHIDRA||||END|@@ -101,6 +103,8 @@ src/decompile/datatests/union_datatype.xml||GHIDRA||||END|src/decompile/datatests/varcross.xml||GHIDRA||||END|src/decompile/datatests/wayoffarray.xml||GHIDRA||||END|src/decompile/datatests/wraprange.xml||GHIDRA||||END|+src/decompile/datatests/zeroload.xml||GHIDRA||||END|+src/decompile/datatests/zerotop.xml||GHIDRA||||END|src/decompile/zlib/README.txt||GHIDRA||||END|src/main/doc/commonprofile.xsl||GHIDRA||||END|src/main/doc/cspec.xml||GHIDRA||||END|diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.ccindex 56bae633f7..a414be1467 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc@@ -505,11 +505,42 @@ int4 ActionStackPtrFlow::apply(Funcdata &data)return 0;}+/// \brief Register a lane size suggested by a piece of a laned register+///+/// If the piece's size is itself an allowed lane size, it is registered. Otherwise the piece+/// is a run of smaller lanes (three floats taken from a four float vector, for instance) and+/// the largest allowed lane size that tiles both the piece and its offset within the register+/// is registered, provided the piece holds at least two such lanes.+/// \param size is the size of the piece in bytes+/// \param offset is the byte offset of the piece within the register+/// \param allowedLanes is used to determine if a putative lane size is allowed+/// \param checkLanes collects the possible lane sizes+void ActionLaneDivide::addPieceLaneSize(int4 size,int4 offset,const LanedRegister &allowedLanes,LanedRegister &checkLanes)++{+ if (allowedLanes.allowedLane(size)) {+ checkLanes.addLaneSize(size); // Register this possible size+ return;+ }+ int4 best = 0;+ LanedRegister::const_iterator enditer = allowedLanes.end();+ for(LanedRegister::const_iterator iter=allowedLanes.begin();iter!=enditer;++iter) {+ int4 laneSize = *iter;+ if (laneSize * 2 > size) break; // Sizes come smallest first+ if ((size % laneSize) == 0 && (offset % laneSize) == 0)+ best = laneSize;+ }+ if (best > 0)+ checkLanes.addLaneSize(best);+}+/// \brief Examine the PcodeOps using the given Varnode to determine possible lane sizes////// Run through the defining op and any descendant ops of the given Varnode, looking for/// CPUI_PIECE and CPUI_SUBPIECE. Use these to determine possible lane sizes and-/// register them with the given LanedRegister object.+/// register them with the given LanedRegister object. A piece whose size is not itself a lane+/// size suggests the largest lane size that tiles it (addPieceLaneSize); so does the input of+/// a defining CPUI_INT_ZEXT when it is not itself a lane size (a run of lanes under zero lanes)./// \param vn is the given Varnode/// \param allowedLanes is used to determine if a putative lane size is allowed/// \param checkLanes collects the possible lane sizes@@ -517,32 +548,26 @@ void ActionLaneDivide::collectLaneSizes(Varnode *vn,const LanedRegister &allowed{list<PcodeOp *>::const_iterator iter = vn->beginDescend();- int4 step = 0; // 0 = descendants, 1 = def, 2 = done- if (iter == vn->endDescend()) {- step = 1;+ list<PcodeOp *>::const_iterator enditer = vn->endDescend();+ while(iter != enditer) {+ PcodeOp *op = *iter;+ ++iter;+ if (op->code() != CPUI_SUBPIECE) continue; // Is the big register split into pieces+ addPieceLaneSize(op->getOut()->getSize(),(int4)op->getIn(1)->getOffset(),allowedLanes,checkLanes);}- while(step < 2) {- int4 curSize; // Putative lane size- if (step == 0) {- PcodeOp *op = *iter;- ++iter;- if (iter == vn->endDescend())- step = 1;- if (op->code() != CPUI_SUBPIECE) continue; // Is the big register split into pieces- curSize = op->getOut()->getSize();- }- else {- step = 2;- if (!vn->isWritten()) continue;- PcodeOp *op = vn->getDef();- if (op->code() != CPUI_PIECE) continue; // Is the big register formed from smaller pieces- curSize = op->getIn(0)->getSize();- int4 tmpSize = op->getIn(1)->getSize();- if (tmpSize < curSize)- curSize = tmpSize;- }- if (allowedLanes.allowedLane(curSize))- checkLanes.addLaneSize(curSize); // Register this possible size+ if (!vn->isWritten()) return;+ PcodeOp *op = vn->getDef();+ if (op->code() == CPUI_PIECE) { // Is the big register formed from smaller pieces+ int4 loSize = op->getIn(1)->getSize();+ addPieceLaneSize(loSize,0,allowedLanes,checkLanes);+ addPieceLaneSize(op->getIn(0)->getSize(),loSize,allowedLanes,checkLanes);+ }+ else if (op->code() == CPUI_INT_ZEXT) {+ // Is the big register a run of lanes with zero lanes above it. A single lane extended is left alone:+ // it suggests no lane size on its own.+ int4 inSize = op->getIn(0)->getSize();+ if (!allowedLanes.allowedLane(inSize))+ addPieceLaneSize(inSize,0,allowedLanes,checkLanes);}}@@ -595,7 +620,9 @@ bool ActionLaneDivide::processVarnode(Funcdata &data,Varnode *vn,const LanedRegiint4 ActionLaneDivide::apply(Funcdata &data){- data.setLanedRegGenerated();+ // Varnodes keep registering as laned accesses after this pass: a stack location of a laned size+ // only appears once the stack analysis has turned its LOADs and STOREs into Varnodes, so the pass+ // runs again on each round of the main loop over whatever registered since its last run.map<VarnodeData,const LanedRegister *>::const_iterator iter;for(int4 mode=0;mode<3;++mode) {bool allStorageProcessed = true;diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hhindex ad8cbfcd93..8929f42f0d 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hh+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.hh@@ -111,10 +111,11 @@ public:/// if a particular lane scheme makes sense in terms of the function's data-flow, and then/// rewrites the data-flow so that the lanes become explicit Varnodes.class ActionLaneDivide : public Action {+ static void addPieceLaneSize(int4 size,int4 offset,const LanedRegister &allowedLanes,LanedRegister &checkLanes);void collectLaneSizes(Varnode *vn,const LanedRegister &allowedLanes,LanedRegister &checkLanes);bool processVarnode(Funcdata &data,Varnode *vn,const LanedRegister &lanedRegister,int4 mode);public:- ActionLaneDivide(const string &g) : Action(rule_onceperfunc,"lanedivide",g) {} ///< Constructor+ ActionLaneDivide(const string &g) : Action(0,"lanedivide",g) {} ///< Constructorvirtual Action *clone(const ActionGroupList &grouplist) const {if (!grouplist.contains(getGroup())) return (Action *)0;return new ActionLaneDivide(getGroup());diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/subflow.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/subflow.ccindex 5769939f8d..7622abf085 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/subflow.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/subflow.cc@@ -4012,6 +4012,7 @@ bool LaneDivide::traceForward(TransformVar *rvn,int4 numLanes,int4 skipLanes)break;}case CPUI_INT_RIGHT:+ case CPUI_INT_LEFT:{if (!op->getIn(1)->isConstant()) return false; // Trace must come through op->getIn(0)TransformVar *outRvn = setReplacement(outvn, numLanes, skipLanes);@@ -4019,6 +4020,17 @@ bool LaneDivide::traceForward(TransformVar *rvn,int4 numLanes,int4 skipLanes)// Don't create the placeholder ops, let traceBackward make thembreak;}+ case CPUI_INT_ZEXT:+ {+ // The traced lanes are the low lanes of the extension; the rest are zero lanes, which traceBackward builds+ int4 outLanes,outSkip;+ if (!description.extension(numLanes, skipLanes, 0, outvn->getSize(), outLanes, outSkip))+ return false;+ TransformVar *outRvn = setReplacement(outvn,outLanes,outSkip);+ if (outRvn == (TransformVar *)0) return false;+ // Don't create the placeholder ops, let traceBackward make them+ break;+ }case CPUI_STORE:if (op->getIn(2) != origvn) return false; // Can only propagate through value being storedif (!buildStore(op,numLanes,skipLanes))diff --git a/Ghidra/Features/Decompiler/src/decompile/datatests/laneleft.xml b/Ghidra/Features/Decompiler/src/decompile/datatests/laneleft.xmlnew file mode 100644index 0000000000..e53c32d001--- /dev/null+++ b/Ghidra/Features/Decompiler/src/decompile/datatests/laneleft.xml@@ -0,0 +1,31 @@+<decompilertest>+<binaryimage arch="x86:LE:64:default:gcc">+<!--+ One function compiled from C with SSE (clang -O1): a loaded vector used twice, scaled lane by lane and slid one lane up with PSLLDQ:+ void scaleandslide(float *out, const float *in, float s) {+ __m128 v = _mm_loadu_ps(in);+ __m128 w = _mm_mul_ps(v, _mm_set1_ps(s));+ asm("pslldq $4, %0" : "+x"(v));+ _mm_storeu_ps(out, w);+ _mm_storeu_ps(out + 4, v);+ }+-->+<bytechunk space="ram" offset="0x100000" readonly="true">+554889e50f100e0fc6c0000f59c1660f73f9040f11070f114f105dc3+</bytechunk>+<symbol space="ram" offset="0x100000" name="scaleandslide"/>+</binaryimage>+<script>+ <com>parse line extern void scaleandslide(float4 *out, float4 *in, float4 s);</com>+ <com>lo fu scaleandslide</com>+ <com>decompile</com>+ <com>print C</com>+</script>+<stringmatch name="LaneLeft #1" min="1" max="1">out\[1\] = s \* fVar2;</stringmatch>+<stringmatch name="LaneLeft #2" min="1" max="1">out\[4\] = 0.0;</stringmatch>+<stringmatch name="LaneLeft #3" min="1" max="1">out\[5\] = fVar1;</stringmatch>+<stringmatch name="LaneLeft #4" min="1" max="1">out\[7\] = fVar3;</stringmatch>+<stringmatch name="LaneLeft #5" min="0" max="0"><< 0x20</stringmatch>+<stringmatch name="LaneLeft #6" min="0" max="0">_0_4_</stringmatch>+<stringmatch name="LaneLeft #7" min="0" max="0">axVar</stringmatch>+</decompilertest>diff --git a/Ghidra/Features/Decompiler/src/decompile/datatests/stackmatrix.xml b/Ghidra/Features/Decompiler/src/decompile/datatests/stackmatrix.xmlnew file mode 100644index 0000000000..e21a5f1765--- /dev/null+++ b/Ghidra/Features/Decompiler/src/decompile/datatests/stackmatrix.xml@@ -0,0 +1,47 @@+<decompilertest>+<binaryimage arch="x86:LE:64:default:gcc">+<!--+#include <emmintrin.h>+#include <math.h>+/* three rows of a matrix built in a vector register (a scalar zero-extended, then slid a lane at a time) and stored whole to+ the stack; the matrix's address escapes to a call, and after it the rows are read back a float at a time */+void fill(float *m);+float rotate(float a, float s) {+ float m[12] __attribute__((aligned(16)));+ __m128 e = _mm_set_ss(s);+ _mm_store_ps(m, e);+ _mm_store_ps(m + 4, _mm_castsi128_ps(_mm_slli_si128(_mm_castps_si128(e), 4)));+ _mm_store_ps(m + 8, _mm_castsi128_ps(_mm_slli_si128(_mm_castps_si128(e), 8)));+ fill(m);+ float sn = sinf(a), cs = cosf(a);+ return m[0] * cs + m[5] * sn + m[10];+}++ The three calls are to one-byte RET stubs placed after the function.+-->+<bytechunk space="ram" offset="0x100000" readonly="true">+554889e54883ec40f30f1145fc0f57c00f57d2f30f10d10f2955c00f16c2660f73fa04660f7f55d00f2945e0488d7dc0e83b000000f30f1045fce841000000f30f1145f8f30f1045fce842000000f30f104df8f30f594dd4f30f5945c0f30f58c1f30f5845e84883c4405dc390909090c3909090909090909090909090909090c3909090909090909090909090909090c3909090909090909090909090909090+</bytechunk>+<symbol space="ram" offset="0x100000" name="rotate"/>+<symbol space="ram" offset="0x100070" name="fill"/>+<symbol space="ram" offset="0x100080" name="sinf"/>+<symbol space="ram" offset="0x100090" name="cosf"/>+</binaryimage>+<script>+ <com>parse line extern float4 rotate(float4 a, float4 s);</com>+ <com>parse line extern void fill(float4 *m);</com>+ <com>parse line extern float4 sinf(float4 a);</com>+ <com>parse line extern float4 cosf(float4 a);</com>+ <com>lo fu rotate</com>+ <com>decompile</com>+ <com>print C</com>+</script>+<stringmatch name="StackLanes #1" min="1" max="1">fStack_48 = s;</stringmatch>+<stringmatch name="StackLanes #2" min="1" max="1">fStack_34 = s;</stringmatch>+<stringmatch name="StackLanes #3" min="1" max="1">fStack_20 = s;</stringmatch>+<stringmatch name="StackLanes #4" min="1" max="1">fill\(&fStack_48\);</stringmatch>+<stringmatch name="StackLanes #5" min="1" max="1">return fVar1 \* fStack_48 \+ fStack_10 \* fStack_34 \+ fStack_20;</stringmatch>+<stringmatch name="StackLanes #6" min="0" max="0"><< 0x20</stringmatch>+<stringmatch name="StackLanes #7" min="0" max="0">ZEXT416</stringmatch>+<stringmatch name="StackLanes #8" min="0" max="0">_4_4_</stringmatch>+</decompilertest>diff --git a/Ghidra/Features/Decompiler/src/decompile/datatests/zeroload.xml b/Ghidra/Features/Decompiler/src/decompile/datatests/zeroload.xmlnew file mode 100644index 0000000000..f3a8774246--- /dev/null+++ b/Ghidra/Features/Decompiler/src/decompile/datatests/zeroload.xml@@ -0,0 +1,33 @@+<decompilertest>+<binaryimage arch="x86:LE:64:default:gcc">+<!--+#include <xmmintrin.h>+/* a vector loaded whole, its first lane stored alone (so the vector is known to have four-byte lanes), its top lane+ replaced by zero through two shuffles against a zero register, the result stored whole: the decompiler folds the+ shuffles into a zero extension of the low three lanes, which the lane split must follow forward from the loaded vector */+void zeroload(float *out, float *first, const float *in) {+ __m128 v = _mm_loadu_ps(in);+ _mm_store_ss(first, v);+ __m128 t = _mm_shuffle_ps(v, _mm_setzero_ps(), 0x0a);+ v = _mm_shuffle_ps(v, t, 0x84);+ _mm_storeu_ps(out, v);+}+-->+<bytechunk space="ram" offset="0x100000" readonly="true">+554889e50f1002f30f11060f57c90fc6c8200fc6c1240f11075dc3+</bytechunk>+<symbol space="ram" offset="0x100000" name="zeroload"/>+</binaryimage>+<script>+ <com>parse line extern void zeroload(float4 *out, float4 *first, float4 *in);</com>+ <com>lo fu zeroload</com>+ <com>decompile</com>+ <com>print C</com>+</script>+<stringmatch name="ZeroLoad #1" min="1" max="1">\*first = fVar1;</stringmatch>+<stringmatch name="ZeroLoad #2" min="1" max="1">\*out = fVar1;</stringmatch>+<stringmatch name="ZeroLoad #3" min="1" max="1">out\[1\] = fVar2;</stringmatch>+<stringmatch name="ZeroLoad #4" min="1" max="1">out\[2\] = fVar3;</stringmatch>+<stringmatch name="ZeroLoad #5" min="1" max="1">out\[3\] = 0.0;</stringmatch>+<stringmatch name="ZeroLoad #6" min="0" max="0">axVar</stringmatch>+</decompilertest>diff --git a/Ghidra/Features/Decompiler/src/decompile/datatests/zerotop.xml b/Ghidra/Features/Decompiler/src/decompile/datatests/zerotop.xmlnew file mode 100644index 0000000000..d7650c1659--- /dev/null+++ b/Ghidra/Features/Decompiler/src/decompile/datatests/zerotop.xml@@ -0,0 +1,32 @@+<decompilertest>+<binaryimage arch="x86:LE:64:default:gcc">+<!--+#include <xmmintrin.h>+/* a vector scaled lane by lane, its top lane replaced by zero through two shuffles against a zero register (the way a+ compiler clears one lane without a mask constant), stored; the decompiler folds the shuffles into a zero extension of+ the low three lanes, which the lane split must follow forward */+void zerotop(float *out, const float *in, float s) {+ __m128 v = _mm_mul_ps(_mm_loadu_ps(in), _mm_set1_ps(s));+ __m128 t = _mm_shuffle_ps(v, _mm_setzero_ps(), 0x0a);+ v = _mm_shuffle_ps(v, t, 0x84);+ _mm_storeu_ps(out, v);+}+-->+<bytechunk space="ram" offset="0x100000" readonly="true">+554889e50f100e0fc6c0000f59c10f57c90fc6c8200fc6c1240f11075dc3+</bytechunk>+<symbol space="ram" offset="0x100000" name="zerotop"/>+</binaryimage>+<script>+ <com>parse line extern void zerotop(float4 *out, float4 *in, float4 s);</com>+ <com>lo fu zerotop</com>+ <com>decompile</com>+ <com>print C</com>+</script>+<stringmatch name="ZeroTop #1" min="1" max="1">\*out = s \* \*in;</stringmatch>+<stringmatch name="ZeroTop #2" min="1" max="1">out\[1\] = s \* fVar1;</stringmatch>+<stringmatch name="ZeroTop #3" min="1" max="1">out\[2\] = s \* fVar2;</stringmatch>+<stringmatch name="ZeroTop #4" min="1" max="1">out\[3\] = 0.0;</stringmatch>+<stringmatch name="ZeroTop #5" min="0" max="0">axVar</stringmatch>+<stringmatch name="ZeroTop #6" min="0" max="0">_12_4_</stringmatch>+</decompilertest>