Skip to content

Ghidra is a software reverse engineering (SRE) framework

added RulePositiveDiv

caheckman992d32dd87f2
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
index 19ea6232c7..3131a73e4a 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
@@ -4542,6 +4542,7 @@ void universal_action(Architecture *conf)
actprop->addRule( new RuleHumptyOr("analysis") );
actprop->addRule( new RuleNegateIdentity("analysis") );
actprop->addRule( new RuleSubNormal("analysis") );
+ actprop->addRule( new RulePositiveDiv("analysis") );
actprop->addRule( new RuleDivTermAdd("analysis") );
actprop->addRule( new RuleDivTermAdd2("analysis") );
actprop->addRule( new RuleDivOpt("analysis") );
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
index 5ea9bbff05..9cd6697cc5 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
@@ -6448,6 +6448,33 @@ int4 RuleSubNormal::applyOp(PcodeOp *op,Funcdata &data)
return 1;
}
+/// \class RulePositiveDiv
+/// \brief Signed division of positive values is unsigned division
+///
+/// If the sign bit of both the numerator and denominator of a signed division (or remainder)
+/// are zero, then convert to the unsigned form of the operation.
+void RulePositiveDiv::getOpList(vector<uint4> &oplist) const
+
+{
+ oplist.push_back(CPUI_INT_SDIV);
+ oplist.push_back(CPUI_INT_SREM);
+}
+
+int4 RulePositiveDiv::applyOp(PcodeOp *op,Funcdata &data)
+
+{
+ int4 sa = op->getOut()->getSize();
+ if (sa > sizeof(uintb)) return 0;
+ sa = sa * 8 - 1;
+ if (((op->getIn(0)->getNZMask() >> sa) & 1) != 0)
+ return 0; // Input 0 may be negative
+ if (((op->getIn(1)->getNZMask() >> sa) & 1) != 0)
+ return 0; // Input 1 may be negative
+ OpCode opc = (op->code() == CPUI_INT_SDIV) ? CPUI_INT_DIV : CPUI_INT_REM;
+ data.opSetOpcode(op, opc);
+ return 1;
+}
+
/// \class RuleDivTermAdd
/// \brief Simplify expressions associated with optimized division expressions
///
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh
index 004786a369..fe6b160a11 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.hh
@@ -1115,6 +1115,17 @@ public:
// virtual int4 applyOp(PcodeOp *op,Funcdata &data);
// };
+class RulePositiveDiv : public Rule {
+public:
+ RulePositiveDiv(const string &g) : Rule( g, 0, "positivediv") {} ///< Constructor
+ virtual Rule *clone(const ActionGroupList &grouplist) const {
+ if (!grouplist.contains(getGroup())) return (Rule *)0;
+ return new RulePositiveDiv(getGroup());
+ }
+ virtual void getOpList(vector<uint4> &oplist) const;
+ virtual int4 applyOp(PcodeOp *op,Funcdata &data);
+};
+
class RuleDivTermAdd : public Rule {
public:
RuleDivTermAdd(const string &g) : Rule( g, 0, "divtermadd") {} ///< Constructor