Skip to content

Ghidra is a software reverse engineering (SRE) framework

Xor swap rule

caheckmana25b7d217871
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
index 012b7478fd..b6fc42db88 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
@@ -5018,6 +5018,7 @@ void ActionDatabase::universalAction(Architecture *conf)
actprop->addRule( new RulePiece2Zext("analysis") );
actprop->addRule( new RulePiece2Sext("analysis") );
actprop->addRule( new RulePopcountBoolXor("analysis") );
+ actprop->addRule( new RuleXorSwap("analysis") );
actprop->addRule( new RuleSubvarAnd("subvar") );
actprop->addRule( new RuleSubvarSubpiece("subvar") );
actprop->addRule( new RuleSplitFlow("subvar") );
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
index 06d98ec41f..41617b7cab 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
@@ -9081,3 +9081,35 @@ int4 RulePiecePathology::applyOp(PcodeOp *op,Funcdata &data)
return tracePathologyForward(op, data);
}
+void RuleXorSwap::getOpList(vector<uint4> &oplist) const
+
+{
+ oplist.push_back(CPUI_INT_XOR);
+}
+
+int4 RuleXorSwap::applyOp(PcodeOp *op,Funcdata &data)
+
+{
+ for(int4 i=0;i<2;++i) {
+ Varnode *vn = op->getIn(i);
+ if (!vn->isWritten()) continue;
+ PcodeOp *op2 = vn->getDef();
+ if (op2->code() != CPUI_INT_XOR) continue;
+ Varnode *othervn = op->getIn(1-i);
+ Varnode *vn0 = op2->getIn(0);
+ Varnode *vn1 = op2->getIn(1);
+ if (othervn == vn0 && !vn1->isFree()) {
+ data.opRemoveInput(op, 1);
+ data.opSetOpcode(op, CPUI_COPY);
+ data.opSetInput(op, vn1, 0);
+ return 1;
+ }
+ else if (othervn == vn1 && !vn0->isFree()) {
+ data.opRemoveInput(op, 1);
+ data.opSetOpcode(op, CPUI_COPY);
+ data.opSetInput(op, vn0, 0);
+ return 1;
+ }
+ }
+ return 0;
+}
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh
index a9ef933fe6..7ec3afcfc6 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh
@@ -1477,4 +1477,16 @@ public:
virtual void getOpList(vector<uint4> &oplist) const;
virtual int4 applyOp(PcodeOp *op,Funcdata &data);
};
+
+class RuleXorSwap : public Rule {
+public:
+ RuleXorSwap(const string &g) : Rule(g,0,"xorswap") {} ///< Constructor
+ virtual Rule *clone(const ActionGroupList &grouplist) const {
+ if (!grouplist.contains(getGroup())) return (Rule *)0;
+ return new RuleXorSwap(getGroup());
+ }
+ virtual void getOpList(vector<uint4> &oplist) const;
+ virtual int4 applyOp(PcodeOp *op,Funcdata &data);
+};
+
#endif