Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-7038 Do additional symbol reference check if pointer is discovered

caheckman7f538e0affec
diff --git a/Ghidra/Features/Decompiler/certification.manifest b/Ghidra/Features/Decompiler/certification.manifest
index 45c2b37745..03a784f2a4 100644
--- a/Ghidra/Features/Decompiler/certification.manifest
+++ b/Ghidra/Features/Decompiler/certification.manifest
@@ -63,6 +63,7 @@ src/decompile/datatests/nestedoffset.xml||GHIDRA||||END|
src/decompile/datatests/noforloop_alias.xml||GHIDRA||||END|
src/decompile/datatests/noforloop_globcall.xml||GHIDRA||||END|
src/decompile/datatests/noforloop_iterused.xml||GHIDRA||||END|
+src/decompile/datatests/offcut.xml||GHIDRA||||END|
src/decompile/datatests/offsetarray.xml||GHIDRA||||END|
src/decompile/datatests/orcompare.xml||GHIDRA||||END|
src/decompile/datatests/overridedest.xml||GHIDRA||||END|
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
index ed1728bee5..b3831561c9 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/coreaction.cc
@@ -1076,6 +1076,7 @@ SymbolEntry *ActionConstantPtr::isPointer(AddrSpace *spc,Varnode *vn,PcodeOp *op
{
bool needexacthit;
+ vn->setSymbolCheck(Varnode::symcheck_complete);
Architecture *glb = data.getArch();
Varnode *outvn;
if (vn->getTypeReadFacing(op)->getMetatype() == TYPE_PTR) { // Are we explicitly marked as a pointer
@@ -1162,8 +1163,10 @@ SymbolEntry *ActionConstantPtr::isPointer(AddrSpace *spc,Varnode *vn,PcodeOp *op
if (ct->isCharPrint())
needexacthit = false;
}
- if (needexacthit && entry->getAddr() != rampoint)
+ if (needexacthit && entry->getAddr() != rampoint) {
+ vn->setSymbolCheck(Varnode::symcheck_incomplete); // May need to retest if we later discover vn is a pointer
return (SymbolEntry *)0;
+ }
}
return entry;
}
@@ -1190,7 +1193,9 @@ int4 ActionConstantPtr::apply(Funcdata &data)
vn = *begiter++;
if (!vn->isConstant()) break; // New varnodes may get inserted between begiter and enditer
if (vn->getOffset() == 0) continue; // Never make constant 0 into spacebase
- if (vn->isPtrCheck()) continue; // Have we checked this variable before
+ uint4 check = vn->getSymbolCheck();
+ if (check == Varnode::symcheck_complete) continue; // Have we checked this variable before
+ if (check == Varnode::symcheck_incomplete && vn->getType()->getMetatype() != TYPE_PTR) continue;
if (vn->hasNoDescend()) continue;
if (vn->isSpacebase()) continue; // Don't use constant 0 which is already spacebase
// if (vn->getSize() != rspc->getAddrSize()) continue; // Must be size of pointer
@@ -1209,7 +1214,6 @@ int4 ActionConstantPtr::apply(Funcdata &data)
Address rampoint;
uintb fullEncoding;
entry = isPointer(rspc,vn,op,slot,rampoint,fullEncoding,data);
- vn->setPtrCheck(); // Set check flag AFTER searching for symbol
if (entry != (SymbolEntry *)0) {
data.spacebaseConstant(op,slot,entry,rampoint,fullEncoding,vn->getSize());
if ((opc == CPUI_INT_ADD)&&(slot==1))
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.cc
index 6cfe150561..224302f135 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/funcdata.cc
@@ -402,7 +402,7 @@ void Funcdata::spacebaseConstant(PcodeOp *op,int4 slot,SymbolEntry *entry,const
// Make sure newconstant and extra preserve origval in address units
uintb newconstoff = origval - extra; // everything is already in address units
newconst = newConstant(sz,newconstoff);
- newconst->setPtrCheck(); // No longer need to check this constant as a pointer
+ newconst->setSymbolCheck(Varnode::symcheck_complete); // No longer need to check this constant as symbolref
if (spaceid->isTruncated())
addOp->setPtrFlow();
opSetInput(addOp,spacebase_vn,0);
@@ -425,7 +425,7 @@ void Funcdata::spacebaseConstant(PcodeOp *op,int4 slot,SymbolEntry *entry,const
else
opSetOpcode(extraOp,CPUI_INT_ADD);
Varnode *extconst = newConstant(sz,extra);
- extconst->setPtrCheck();
+ extconst->setSymbolCheck(Varnode::symcheck_complete);
opSetInput(extraOp,outvn,0);
opSetInput(extraOp,extconst,1);
outvn = extraOp->getOut();
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.hh
index aa2166aa03..a04b619e14 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/varnode.hh
@@ -120,16 +120,17 @@ public:
writemask = 0x02, ///< Should not be considered a write in heritage calculation
vacconsume = 0x04, ///< Vacuous consume
lisconsume = 0x08, ///< In consume worklist
- ptrcheck = 0x10, ///< The Varnode value is \e NOT a pointer
- ptrflow = 0x20, ///< If this varnode flows to or from a pointer
- unsignedprint = 0x40, ///< Constant that must be explicitly printed as an unsigned token
- longprint = 0x80, ///< Constant that must be explicitly printed as a \e long integer token
- stack_store = 0x100, ///< Created by an explicit STORE
- locked_input = 0x200, ///< Input that exists even if its unused
- spacebase_placeholder = 0x400, ///< This varnode is inserted artificially to track a register
+ symcheck_incomplete = 0x20, ///< Checked for symbol reference, but may require an additional check
+ symcheck_complete = 0x30, ///< Checked for symbol reference
+ ptrflow = 0x40, ///< If this varnode flows to or from a pointer
+ unsignedprint = 0x80, ///< Constant that must be explicitly printed as an unsigned token
+ longprint = 0x100, ///< Constant that must be explicitly printed as a \e long integer token
+ stack_store = 0x200, ///< Created by an explicit STORE
+ locked_input = 0x400, ///< Input that exists even if its unused
+ spacebase_placeholder = 0x800, ///< This varnode is inserted artificially to track a register
///< value at a specific point in the code
- stop_uppropagation = 0x800, ///< Data-types do not propagate from an output into \b this
- has_implied_field = 0x1000 ///< The varnode is implied but also has a data-type that needs resolution
+ stop_uppropagation = 0x1000, ///< Data-types do not propagate from an output into \b this
+ has_implied_field = 0x2000 ///< The varnode is implied but also has a data-type that needs resolution
};
private:
mutable uint4 flags; ///< The collection of boolean attributes for this Varnode
@@ -257,7 +258,7 @@ public:
bool isSpacebase(void) const { return ((flags&Varnode::spacebase)!=0); } ///< Is this location used to store the base point for a virtual address space?
bool isReturnAddress(void) const { return ((flags&Varnode::return_address)!=0); } ///< Is this storage for a calls return address?
bool isProtoPartial(void) const { return ((flags&Varnode::proto_partial)!=0); } ///< Is \b this getting pieced together into a larger whole
- bool isPtrCheck(void) const { return ((addlflags&Varnode::ptrcheck)!=0); } ///< Has \b this been checked as a constant pointer to a mapped symbol?
+ uint4 getSymbolCheck(void) const { return addlflags & symcheck_complete; } ///< Get state of checks for \b this as constant symbol reference
bool isPtrFlow(void) const { return ((addlflags&Varnode::ptrflow)!=0); } ///< Does this varnode flow to or from a known pointer
bool isSpacebasePlaceholder(void) const { return ((addlflags&Varnode::spacebase_placeholder)!=0); } ///< Is \b this used specifically to track stackpointer values?
bool hasNoLocalAlias(void) const { return ((flags&Varnode::nolocalalias)!=0); } ///< Are there (not) any local pointers that might affect \b this?
@@ -313,8 +314,7 @@ public:
void clearExplicit(void) { clearFlags(Varnode::explict); } ///< Clear the \e explicit mark on this Varnode
void setReturnAddress(void) { flags |= Varnode::return_address; } ///< Mark as storage location for a return address
void clearReturnAddress(void) { flags &= ~Varnode::return_address; } ///< Clear return address attribute
- void setPtrCheck(void) { addlflags |= Varnode::ptrcheck; } ///< Set \b this as checked for a constant symbol reference
- void clearPtrCheck(void) { addlflags &= ~Varnode::ptrcheck; } ///< Clear the pointer check mark on this Varnode
+ void setSymbolCheck(uint4 val) { addlflags = (addlflags & ~symcheck_complete) | (val & symcheck_complete); } ///< Set state of checks for a constant symbol reference
void setPtrFlow(void) { addlflags |= Varnode::ptrflow; } ///< Set \b this as flowing to or from pointer
void clearPtrFlow(void) { addlflags &= ~Varnode::ptrflow; } ///< Indicate that this varnode is not flowing to or from pointer
void setSpacebasePlaceholder(void) { addlflags |= Varnode::spacebase_placeholder; } ///< Mark \b this as a special Varnode for tracking stackpointer values
diff --git a/Ghidra/Features/Decompiler/src/decompile/datatests/offcut.xml b/Ghidra/Features/Decompiler/src/decompile/datatests/offcut.xml
new file mode 100644
index 0000000000..e77c158112
--- /dev/null
+++ b/Ghidra/Features/Decompiler/src/decompile/datatests/offcut.xml
@@ -0,0 +1,45 @@
+<decompilertest>
+ <!--
+ Function with offcut references to global data
+ -->
+<binaryimage arch="x86:LE:64:default:gcc">
+<bytechunk space="ram" offset="0x100000" readonly="true">
+55534883ec1889fbbf80001000e8ee0f
+000089c583fb01741d83fb02742683fb
+03742f83fb04743c83fb05744589e848
+83c4185b5dc3bf9c001000e8c00f0000
+01c5ebe9bf9c001000e8ba0f000001c5
+ebdb48c744240890001000b890001000
+8d28ebc9bfa4001000e8a20f000089c5
+ebbbbfaa001000e8940f000089c5ebad
+</bytechunk>
+<bytechunk space="ram" offset="0x1000a0" readonly="true">
+0000000068656c6c6f20776f726c6400
+</bytechunk>
+<symbol space="ram" offset="0x100000" name="offcut"/>
+</binaryimage>
+<script>
+ <com>option readonly on</com>
+ <com>parse line struct substruct { int4 sub1; int4 sub2; };</com>
+ <com>parse line struct mystruct { int4 a; int4 b; float4 c; substruct d; };</com>
+ <com>map addr r0x100080 substruct glob2</com>
+ <com>map addr r0x100090 mystruct glob1</com>
+ <com>map addr r0x1000a4 char globstring[12]</com>
+ <com>map fun r0x101000 read_sub</com>
+ <com>map fun r0x101008 read_int</com>
+ <com>map fun r0x101010 read_string</com>
+ <com>parse line extern int4 read_sub(substruct *);</com>
+ <com>parse line extern int4 read_int(int4 *);</com>
+ <com>parse line extern int4 read_string(char *);</com>
+ <com>lo fu offcut</com>
+ <com>dec</com>
+ <com>print C</com>
+ <com>quit</com>
+</script>
+<stringmatch name="Offcut Symbol Refs #1" min="1" max="1">read_sub\(&amp;glob2\);</stringmatch>
+<stringmatch name="Offcut Symbol Refs #2" min="1" max="1">read_sub\(&amp;glob1\.d\);</stringmatch>
+<stringmatch name="Offcut Symbol Refs #3" min="1" max="1">read_int\(&amp;glob1\.d\.sub1\);</stringmatch>
+<stringmatch name="Offcut Symbol Refs #4" min="1" max="1">iVar. = 0x100090;</stringmatch>
+<stringmatch name="Offcut Symbol Refs #5" min="1" max="1">read_string\("hello world"\);</stringmatch>
+<stringmatch name="Offcut Symbol Refs #6" min="1" max="1">read_string\("world"\);</stringmatch>
+</decompilertest>