Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-2568 InheritResolution after insertPtrsubZero

caheckman45347bff870b
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
index 3a0b813366..60be9b2c39 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
@@ -2436,7 +2436,7 @@ int4 ActionSetCasts::castOutput(PcodeOp *op,Funcdata &data,CastStrategy *castStr
if (tokenct->needsResolution())
data.forceFacingType(tokenct, -1, newop, 0);
if (outHighType->needsResolution())
- data.inheritWriteResolution(outHighType, newop, op);
+ data.inheritResolution(outHighType, newop, -1, op, -1); // Inherit write resolution
return 1;
}
@@ -2510,7 +2510,9 @@ int4 ActionSetCasts::castInput(PcodeOp *op,int4 slot,Funcdata &data,CastStrategy
}
else if (testStructOffset0(vn, op, ct, castStrategy)) {
// Insert a PTRSUB(vn,#0) instead of a CAST
- insertPtrsubZero(op, slot, ct, data);
+ newop = insertPtrsubZero(op, slot, ct, data);
+ if (vn->getHigh()->getType()->needsResolution())
+ data.inheritResolution(vn->getHigh()->getType(),newop, 0, op, slot);
return 1;
}
else if (tryResolutionAdjustment(op, slot, data)) {
@@ -2531,7 +2533,7 @@ int4 ActionSetCasts::castInput(PcodeOp *op,int4 slot,Funcdata &data,CastStrategy
data.forceFacingType(ct, -1, newop, -1);
}
if (vn->getHigh()->getType()->needsResolution()) {
- data.inheritReadResolution(newop, 0, op, slot);
+ data.inheritResolution(vn->getHigh()->getType(),newop, 0, op, slot);
}
return 1;
}
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.cc
index f187cdd571..e628cc8037 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.cc
@@ -908,39 +908,22 @@ void Funcdata::forceFacingType(Datatype *parent,int4 fieldNum,PcodeOp *op,int4 s
setUnionField(parent, op, slot, resolve);
}
-/// \brief Copy a Varnode's read facing resolve to another PcodeOp
-///
-/// \param op is the new PcodeOp reading the Varnode
-/// \param slot is the new read slot
-/// \param oldOp is the PcodeOp to inherit the resolve from
-/// \param oldSlot is the old read slot
-void Funcdata::inheritReadResolution(const PcodeOp *op,int4 slot,PcodeOp *oldOp,int4 oldSlot)
-
-{
- Datatype *ct = op->getIn(slot)->getType();
- if (!ct->needsResolution()) return;
- map<ResolveEdge,ResolvedUnion>::const_iterator iter;
- ResolveEdge edge(ct,oldOp,oldSlot);
- iter = unionMap.find(edge);
- if (iter == unionMap.end()) return;
- setUnionField(ct,op,slot,(*iter).second);
-}
-
-/// \brief Copy any write facing for a specific data-type from one PcodeOp to another
+/// \brief Copy a read/write facing resolution for a specific data-type from one PcodeOp to another
///
/// \param parent is the data-type that needs resolution
-/// \param op is the destination PcodeOp
-/// \param oldOp is the source PcodeOp
-/// \return the resolution index that was copied or -1 if there was no resolution
-int4 Funcdata::inheritWriteResolution(Datatype *parent,const PcodeOp *op,PcodeOp *oldOp)
+/// \param op is the new reading PcodeOp
+/// \param slot is the new slot (-1 for write, >=0 for read)
+/// \param oldOp is the PcodeOp to inherit the resolution from
+/// \param oldSlot is the old slot (-1 for write, >=0 for read)
+int4 Funcdata::inheritResolution(Datatype *parent,const PcodeOp *op,int4 slot,PcodeOp *oldOp,int4 oldSlot)
{
map<ResolveEdge,ResolvedUnion>::const_iterator iter;
- ResolveEdge edge(parent,oldOp,-1);
+ ResolveEdge edge(parent,oldOp,oldSlot);
iter = unionMap.find(edge);
if (iter == unionMap.end())
return -1;
- setUnionField(parent,op,-1,(*iter).second);
+ setUnionField(parent,op,slot,(*iter).second);
return (*iter).second.getFieldNum();
}
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh
index baf13a159c..9ffe1e3a3c 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh
@@ -499,8 +499,7 @@ public:
const ResolvedUnion *getUnionField(const Datatype *parent,const PcodeOp *op,int4 slot) const;
bool setUnionField(const Datatype *parent,const PcodeOp *op,int4 slot,const ResolvedUnion &resolve);
void forceFacingType(Datatype *parent,int4 fieldNum,PcodeOp *op,int4 slot);
- void inheritReadResolution(const PcodeOp *op,int4 slot,PcodeOp *oldOp,int4 oldSlot);
- int4 inheritWriteResolution(Datatype *parent,const PcodeOp *op,PcodeOp *oldOp);
+ int4 inheritResolution(Datatype *parent,const PcodeOp *op,int4 slot,PcodeOp *oldOp,int4 oldSlot);
// Jumptable routines
JumpTable *linkJumpTable(PcodeOp *op); ///< Link jump-table with a given BRANCHIND
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/merge.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/merge.cc
index 20fe5c18b0..cb41351e25 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/merge.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/merge.cc
@@ -355,7 +355,7 @@ PcodeOp *Merge::allocateCopyTrim(Varnode *inVn,const Address &addr,PcodeOp *trim
Datatype *ct = inVn->getType();
if (ct->needsResolution()) { // If the data-type needs resolution
if (inVn->isWritten()) {
- int4 fieldNum = data.inheritWriteResolution(ct, copyOp, inVn->getDef());
+ int4 fieldNum = data.inheritResolution(ct, copyOp, -1, inVn->getDef(), -1);
data.forceFacingType(ct, fieldNum, copyOp, 0);
}
else {
@@ -575,7 +575,7 @@ void Merge::trimOpOutput(PcodeOp *op)
Datatype *ct = vn->getType();
copyop = data.newOp(1,op->getAddr());
if (ct->needsResolution()) {
- int4 fieldNum = data.inheritWriteResolution(ct, copyop, op);
+ int4 fieldNum = data.inheritResolution(ct, copyop, -1, op, -1);
data.forceFacingType(ct, fieldNum, copyop, 0);
if (ct->getMetatype() == TYPE_PARTIALUNION)
ct = vn->getTypeDefFacing();
@@ -806,7 +806,7 @@ void Merge::mergeIndirect(PcodeOp *indop)
newop = allocateCopyTrim(invn0, indop->getAddr(), indop);
SymbolEntry *entry = outvn->getSymbolEntry();
if (entry != (SymbolEntry *)0 && entry->getSymbol()->getType()->needsResolution()) {
- data.inheritWriteResolution(entry->getSymbol()->getType(), newop, indop);
+ data.inheritResolution(entry->getSymbol()->getType(), newop, -1, indop, -1);
}
data.opSetInput(indop,newop->getOut(),0);
data.opInsertBefore(newop,indop);
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
index c85263b665..2331526a86 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ruleaction.cc
@@ -6156,7 +6156,8 @@ void AddTreeState::buildTree(void)
// Create PTRADD portion of operation
if (multNode != (Varnode *)0) {
newop = data.newOpBefore(baseOp,CPUI_PTRADD,ptr,multNode,data.newConstant(ptrsize,size));
- data.inheritReadResolution(newop, 0, baseOp, baseSlot);
+ if (ptr->getType()->needsResolution())
+ data.inheritResolution(ptr->getType(),newop, 0, baseOp, baseSlot);
multNode = newop->getOut();
}
else
@@ -6165,7 +6166,8 @@ void AddTreeState::buildTree(void)
// Create PTRSUB portion of operation
if (isSubtype) {
newop = data.newOpBefore(baseOp,CPUI_PTRSUB,multNode,data.newConstant(ptrsize,offset));
- data.inheritReadResolution(newop, 0, baseOp, baseSlot);
+ if (multNode->getType()->needsResolution())
+ data.inheritResolution(multNode->getType(),newop, 0, baseOp, baseSlot);
if (size != 0)
newop->setStopTypePropagation();
multNode = newop->getOut();
@@ -6340,7 +6342,8 @@ int4 RuleStructOffset0::applyOp(PcodeOp *op,Funcdata &data)
else
return 0;
- Datatype *ct = op->getIn(1)->getTypeReadFacing(op);
+ Varnode *ptrVn = op->getIn(1);
+ Datatype *ct = ptrVn->getTypeReadFacing(op);
if (ct->getMetatype() != TYPE_PTR) return 0;
Datatype *baseType = ((TypePointer *)ct)->getPtrTo();
uintb offset = 0;
@@ -6378,8 +6381,9 @@ int4 RuleStructOffset0::applyOp(PcodeOp *op,Funcdata &data)
else
return 0;
- PcodeOp *newop = data.newOpBefore(op,CPUI_PTRSUB,op->getIn(1),data.newConstant(op->getIn(1)->getSize(),0));
- data.inheritReadResolution(newop, 0, op, 1);
+ PcodeOp *newop = data.newOpBefore(op,CPUI_PTRSUB,ptrVn,data.newConstant(ptrVn->getSize(),0));
+ if (ptrVn->getType()->needsResolution())
+ data.inheritResolution(ptrVn->getType(),newop, 0, op, 1);
newop->setStopTypePropagation();
data.opSetInput(op,newop->getOut(),1);
return 1;