Skip to content

Ghidra is a software reverse engineering (SRE) framework

Attempt at adjusting processing of free stores

caheckman6a15520aa53e
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc
index 3e52bb1d6b..3520b2383b 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc
@@ -800,10 +800,15 @@ bool Heritage::protectFreeStores(AddrSpace *spc,vector<PcodeOp *> &freeStores)
++iter;
if (op->isDead()) continue;
Varnode *vn = op->getIn(1);
- if (vn->isWritten()) {
- PcodeOp *copyOp = vn->getDef();
- if (copyOp->code() == CPUI_COPY)
- vn = copyOp->getIn(0);
+ while (vn->isWritten()) {
+ PcodeOp *defOp = vn->getDef();
+ OpCode opc = defOp->code();
+ if (opc == CPUI_COPY)
+ vn = defOp->getIn(0);
+ else if (opc == CPUI_INT_ADD && defOp->getIn(1)->isConstant())
+ vn = defOp->getIn(0);
+ else
+ break;
}
if (vn->isFree() && vn->getSpace() == spc) {
fd->opMarkSpacebasePtr(op); // Mark op as spacebase STORE, even though we're not sure
@@ -913,8 +918,17 @@ bool Heritage::discoverIndexedStackPointers(AddrSpace *spc,vector<PcodeOp *> &fr
}
case CPUI_STORE:
{
- if (curNode.traversals != 0) {
- generateStoreGuard(curNode, op, spc);
+ if (op->getIn(1) == curNode.vn) { // Make sure the STORE pointer comes from our path
+ if (curNode.traversals != 0) {
+ generateStoreGuard(curNode, op, spc);
+ }
+ else {
+ // If there were no traversals (of non-constant ADD or MULTIEQUAL) then the
+ // pointer is equal to the stackpointer plus a constant (through an indirect is possible)
+ // This will likely get resolved in the next heritage pass, but we leave the
+ // spacebaseptr mark on, so that that the indirects don't get removed
+ fd->opMarkSpacebasePtr(op);
+ }
}
break;
}
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
index 156b042720..b797731c72 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
@@ -2932,10 +2932,17 @@ int4 RuleIndirectCollapse::applyOp(PcodeOp *op,Funcdata &data)
return 0;
}
else if (indop->usesSpacebasePtr()) {
- const LoadGuard *guard = data.getStoreGuard(indop);
- if (guard != (const LoadGuard *)0) {
- if (guard->isGuarded(op->getOut()->getAddr()))
+ if (indop->code() == CPUI_STORE) {
+ const LoadGuard *guard = data.getStoreGuard(indop);
+ if (guard != (const LoadGuard *)0) {
+ if (guard->isGuarded(op->getOut()->getAddr()))
+ return 0;
+ }
+ else {
+ // A marked STORE that is not guarded should eventually get converted to a COPY
+ // so we keep the INDIRECT until that happens
return 0;
+ }
}
}
else