Ghidra is a software reverse engineering (SRE) framework
GP-2956 Adjustment to RuleSubExtComm
caheckman2345c1289bbb
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.ccindex 524eecfce3..716f659d0f 100644--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc@@ -4019,6 +4019,10 @@ int4 RuleStoreVarnode::applyOp(PcodeOp *op,Funcdata &data)/// This is in keeping with the philosophy to push SUBPIECE back earlier in the expression./// The original SUBPIECE is changed into the INT_ZEXT, but the original INT_ZEXT is/// not changed, a new SUBPIECE is created.+/// There are corner cases, if the SUBPIECE doesn't hit extended bits or is ultimately unnecessary.+/// - `sub(zext(V),c) => sub(V,C)`+/// - `sub(zext(V),0) => zext(V)`+////// This rule also works with INT_SEXT.void RuleSubExtComm::getOpList(vector<uint4> &oplist) const@@ -4029,24 +4033,37 @@ void RuleSubExtComm::getOpList(vector<uint4> &oplist) constint4 RuleSubExtComm::applyOp(PcodeOp *op,Funcdata &data){- int4 subcut = (int4)op->getIn(1)->getOffset();Varnode *base = op->getIn(0);- // Make sure subpiece preserves most sig bytes- if (op->getOut()->getSize()+subcut != base->getSize()) return 0;if (!base->isWritten()) return 0;PcodeOp *extop = base->getDef();if ((extop->code()!=CPUI_INT_ZEXT)&&(extop->code()!=CPUI_INT_SEXT))return 0;Varnode *invn = extop->getIn(0);if (invn->isFree()) return 0;+ int4 subcut = (int4)op->getIn(1)->getOffset();+ if (op->getOut()->getSize() + subcut <= invn->getSize()) {+ // SUBPIECE doesn't hit the extended bits at all+ data.opSetInput(op,invn,0);+ if (invn->getSize() == op->getOut()->getSize()) {+ data.opRemoveInput(op, 1);+ data.opSetOpcode(op, CPUI_COPY);+ }+ return 1;+ }+if (subcut >= invn->getSize()) return 0;- PcodeOp *newop = data.newOp(2,op->getAddr());- data.opSetOpcode(newop,CPUI_SUBPIECE);- Varnode *newvn = data.newUniqueOut(invn->getSize()-subcut,newop);- data.opSetInput(newop,data.newConstant(op->getIn(1)->getSize(),(uintb)subcut),1);- data.opSetInput(newop,invn,0);- data.opInsertBefore(newop,op);+ Varnode *newvn;+ if (subcut != 0) {+ PcodeOp *newop = data.newOp(2,op->getAddr());+ data.opSetOpcode(newop,CPUI_SUBPIECE);+ newvn = data.newUniqueOut(invn->getSize()-subcut,newop);+ data.opSetInput(newop,data.newConstant(op->getIn(1)->getSize(),(uintb)subcut),1);+ data.opSetInput(newop,invn,0);+ data.opInsertBefore(newop,op);+ }+ else+ newvn = invn;data.opRemoveInput(op,1);data.opSetOpcode(op,extop->code());