Skip to content

Ghidra is a software reverse engineering (SRE) framework

adjustments to high index calculations

caheckman3433400d76ba
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc
index 3d7ed8e039..4317436fb6 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/heritage.cc
@@ -636,7 +636,7 @@ void LoadGuard::finalizeRange(const ValueSetRead &valueSet)
if (rangeSize > 2)
step = range.getStep();
minimumOffset = range.getMin();
- maximumOffset = range.getMax();
+ maximumOffset = (range.getEnd() - 1) & range.getMask(); // NOTE: Don't subtract a whole step
if (maximumOffset < minimumOffset) // Values extend into what is usually stack parameters
maximumOffset = spc->getHighest();
}
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.cc
index 92d3e14eea..88a978f579 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.cc
@@ -647,12 +647,12 @@ void MapState::gatherOpen(const Funcdata &fd)
continue; // Don't manufacture primitives bigger than 8-bytes
ct = fd.getArch()->types->getBase(step, TYPE_UNKNOWN);
}
- int4 hi;
- if (guard.isRangeLocked())
- hi = ((guard.getMaximum() - guard.getMinimum()) + 1) / step;
+ if (guard.isRangeLocked()) {
+ int4 minItems = ((guard.getMaximum() - guard.getMinimum()) + 1) / step;
+ addRange(guard.getMinimum(),ct,0,MapRange::boundArray,minItems-1);
+ }
else
- hi = 3;
- addRange(guard.getMinimum(),ct,0,MapRange::isAnArray,hi);
+ addRange(guard.getMinimum(),ct,0,MapRange::isAnArray,3);
}
}
@@ -803,12 +803,12 @@ void ScopeLocal::rangeUnion(MapRange *a,MapRange *b,bool warning)
Datatype *restype;
uint4 flags;
bool reconcile;
- int4 hi;
+ int4 highestIndex;
aend = spaceid->wrapOffset(a->start+a->size);
bend = spaceid->wrapOffset(b->start+b->size);
MapRange::ArrayType arrayType = MapRange::notAnArray;
- hi = -1;
+ highestIndex = -1;
if ((aend==0)||(bend==0))
end = 0;
else
@@ -820,19 +820,28 @@ void ScopeLocal::rangeUnion(MapRange *a,MapRange *b,bool warning)
restype = a->type;
flags = a->flags;
arrayType = a->arrayType;
- hi = a->highind;
+ highestIndex = a->highind;
}
else {
restype = b->type;
flags = b->flags;
arrayType = b->arrayType;
- hi = b->highind;
+ highestIndex = b->highind;
}
if ((a->start==b->start)&&(a->size==b->size)) {
arrayType = MapRange::notAnArray;
if (a->isArray() || b->isArray()) {
arrayType = MapRange::isAnArray;
- hi = (a->highind < b->highind) ? b->highind : a->highind;
+ if (a->highind < b->highind) {
+ highestIndex = b->highind;
+ if (b->arrayType == MapRange::boundArray)
+ arrayType = b->arrayType;
+ }
+ else {
+ highestIndex = a->highind;
+ if (a->arrayType == MapRange::boundArray)
+ arrayType = a->arrayType;
+ }
}
}
if (warning && (!reconcile)) { // See if two types match up
@@ -858,7 +867,7 @@ void ScopeLocal::rangeUnion(MapRange *a,MapRange *b,bool warning)
a->type = restype;
a->flags = flags;
a->arrayType = arrayType;
- a->highind = hi;
+ a->highind = highestIndex;
if ((!reconcile)&&(a->start != b->start)) { // Truncation is forced
if ((a->flags & Varnode::typelock)!=0) { // If a is locked
return; // Discard b entirely in favor of a
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.hh
index 450b4010f9..e83fc30ef8 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/varmap.hh
@@ -40,8 +40,9 @@ public:
// Structure for internal map layout
struct MapRange {
enum ArrayType {
- notAnArray = 0,
- isAnArray = 1
+ notAnArray = 0, ///< Not an array
+ isAnArray = 1, ///< Some kind of array
+ boundArray = 2 ///< An array with an established set of items
};
uintb start; // Start of range
int4 size;
@@ -49,7 +50,7 @@ struct MapRange {
Datatype *type;
uint4 flags;
ArrayType arrayType;
- int4 highind; // Minimum number of items in array
+ int4 highind; // Minimum upper bound on array index
MapRange(void) {}
MapRange(uintb st,int4 sz,intb sst,Datatype *ct,uint4 fl,ArrayType at,int4 hi) {
start=st; size=sz; sstart=sst; type=ct; flags=fl; arrayType = at; highind=hi; }