Skip to content

Ghidra is a software reverse engineering (SRE) framework

Prevent Symbol storage conflicts

caheckman2ea73a65a0a8
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh
index 5e57bba8e6..176b12572c 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.hh
@@ -87,6 +87,7 @@ class Funcdata {
// Low level Varnode functions
void setVarnodeProperties(Varnode *vn) const; ///< Look-up boolean properties and data-type information
HighVariable *assignHigh(Varnode *vn); ///< Assign a new HighVariable to a Varnode
+ Symbol *handleSymbolConflict(SymbolEntry *entry,Varnode *vn); ///< Handle two variables with matching storage
bool syncVarnodesWithSymbol(VarnodeLocSet::const_iterator &iter,uint4 flags,Datatype *ct);
bool descend2Undef(Varnode *vn); ///< Transform all reads of the given Varnode to a special \b undefined constant
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_varnode.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_varnode.cc
index d18dd0fb41..46e676e9c6 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_varnode.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata_varnode.cc
@@ -866,6 +866,46 @@ bool Funcdata::syncVarnodesWithSymbols(const ScopeLocal *lm,bool typesyes)
return updateoccurred;
}
+/// A Varnode overlaps the given SymbolEntry. Make sure the Varnode is part of the variable
+/// underlying the Symbol. If not, remap things so that the Varnode maps to a distinct Symbol.
+/// In either case, attach the appropriate Symbol to the Varnode
+/// \param entry is the given SymbolEntry
+/// \param vn is the overlapping Varnode
+/// \return the Symbol attached to the Varnode
+Symbol *Funcdata::handleSymbolConflict(SymbolEntry *entry,Varnode *vn)
+
+{
+ if (vn->isInput() || vn->isAddrTied() ||
+ vn->isPersist() || vn->isConstant() || entry->isDynamic()) {
+ vn->setSymbolEntry(entry);
+ return entry->getSymbol();
+ }
+ HighVariable *high = vn->getHigh();
+ Varnode *otherVn;
+ HighVariable *otherHigh = (HighVariable *)0;
+ // Look for a conflicting HighVariable
+ VarnodeLocSet::const_iterator iter = beginLoc(entry->getSize(),entry->getAddr());
+ while(iter != endLoc()) {
+ otherVn = *iter;
+ if (otherVn->getSize() != entry->getSize()) break;
+ if (otherVn->getAddr() != entry->getAddr()) break;
+ HighVariable *tmpHigh = otherVn->getHigh();
+ if (tmpHigh != high) {
+ otherHigh = tmpHigh;
+ break;
+ }
+ ++iter;
+ }
+ if (otherHigh == (HighVariable *)0) {
+ vn->setSymbolEntry(entry);
+ return entry->getSymbol();
+ }
+
+ // If we reach here, we have a conflicting variable
+ buildDynamicSymbol(vn);
+ return vn->getSymbolEntry()->getSymbol();
+}
+
/// \brief Update properties (and the data-type) for a set of Varnodes associated with one Symbol
///
/// The set of Varnodes with the same size and address all have their boolean properties
@@ -994,8 +1034,7 @@ Symbol *Funcdata::linkSymbol(Varnode *vn)
// Find any entry overlapping base address
entry = localmap->queryProperties(vn->getAddr(), 1, usepoint, flags);
if (entry != (SymbolEntry *) 0) {
- sym = entry->getSymbol();
- vn->setSymbolEntry(entry);
+ sym = handleSymbolConflict(entry, vn);
}
else { // Must create a symbol entry
if (!vn->isPersist()) { // Only create local symbol