Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-7259 PcodeCacher pages

caheckmanf49e00569c8a
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/sleigh.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/sleigh.cc
index f6b718d6e0..6a2c8a549c 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/sleigh.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/sleigh.cc
@@ -22,62 +22,31 @@ PcodeCacher::PcodeCacher(void)
{
// We aim to allocate this array only once
- uint4 maxsize = 600;
- poolstart = new VarnodeData[ maxsize ];
- endpool = poolstart + maxsize;
- curpool = poolstart;
+ expandPool(PAGE_SIZE);
}
PcodeCacher::~PcodeCacher(void)
{
- delete [] poolstart;
+ for(list<VarnodeData *>::iterator iter=pools.begin();iter!=pools.end();++iter)
+ delete [] *iter;
}
-/// Expand the VarnodeData pool so that \e size more elements fit, and return
-/// a pointer to first available element.
-/// \param size is the number of elements to expand the pool by
-/// \return the first available VarnodeData
-VarnodeData *PcodeCacher::expandPool(uint4 size)
+/// Expand the VarnodeData pool so that at least \b size more elements are available.
+/// \param size is the minimum number of elements to expand the pool by
+void PcodeCacher::expandPool(uint4 size)
{
- uint4 curmax = endpool - poolstart;
- uint4 cursize = curpool - poolstart;
- if (cursize + size <= curmax)
- return curpool; // No expansion necessary
- uint4 increase = (cursize + size) - curmax;
- if (increase < 100) // Increase by at least 100
- increase = 100;
-
- uint4 newsize = curmax + increase;
-
- VarnodeData *newpool = new VarnodeData[newsize];
- for(uint4 i=0;i<cursize;++i)
- newpool[i] = poolstart[i]; // Copy old data
- // Update references to the old pool
- for(deque<PcodeData>::iterator diter=issued.begin();diter!=issued.end();++diter) {
- VarnodeData *outvar = (*diter).outvar;
- if (outvar != (VarnodeData *)0) {
- outvar = newpool + (outvar - poolstart);
- (*diter).outvar = outvar;
- }
- VarnodeData *invar = (*diter).invar;
- if (invar != (VarnodeData *)0) {
- invar = newpool + (invar - poolstart);
- (*diter).invar = invar;
- }
- }
- list<RelativeRecord>::iterator iter;
- for(iter=label_refs.begin();iter!=label_refs.end();++iter) {
- VarnodeData *ref = (*iter).dataptr;
- (*iter).dataptr = newpool + (ref - poolstart);
+ if (size > PAGE_SIZE) {
+ if (size > MAX_ALLOCATION)
+ throw LowlevelError("Maximum parameters exceeded for a single p-code op");
}
-
- delete [] poolstart; // Free up old pool
- poolstart = newpool;
- curpool = newpool + (cursize + size);
- endpool = newpool + newsize;
- return newpool + cursize;
+ else
+ size = PAGE_SIZE;
+ VarnodeData *poolstart = new VarnodeData[ size ];
+ pools.push_back(poolstart);
+ endpool = poolstart + size;
+ curpool = poolstart;
}
/// Store off a reference to the Varnode and the absolute index of the next
@@ -108,7 +77,14 @@ void PcodeCacher::addLabel(uint4 id)
void PcodeCacher::clear(void)
{
- curpool = poolstart;
+ list<VarnodeData *>::iterator iter = pools.begin();
+ ++iter;
+ while(iter != pools.end()) {
+ delete [] *iter;
+ iter = pools.erase(iter);
+ }
+ curpool = pools.front();
+ endpool = curpool + PAGE_SIZE;
issued.clear();
label_refs.clear();
labels.clear();
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/sleigh.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/sleigh.hh
index 95249cf18d..8fabcd2c2a 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/sleigh.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/sleigh.hh
@@ -56,29 +56,34 @@ struct PcodeData {
/// The class acts as a pool of memory for PcodeData and VarnodeData objects
/// that can be reused repeatedly to emit multiple instructions.
class PcodeCacher {
- VarnodeData *poolstart; ///< Start of the pool of VarnodeData objects
+ static constexpr uint4 PAGE_SIZE = 512;///< Initial size of VarnodeData pool
+ static constexpr uint4 MAX_ALLOCATION = 4096; ///< Maximum VarnodeData in a single instruction
VarnodeData *curpool; ///< First unused VarnodeData
VarnodeData *endpool; ///< End of the pool of VarnodeData objects
deque<PcodeData> issued; ///< P-code ops issued for the current instruction
list<RelativeRecord> label_refs; ///< References to labels
vector<uintb> labels; ///< Locations of labels
- VarnodeData *expandPool(uint4 size); ///< Expand the memory pool
+ void expandPool(uint4 size); ///< Expand the memory pool
+ list<VarnodeData *> pools; ///< All allocated arrays of VarnodeData
public:
PcodeCacher(void); ///< Constructor
~PcodeCacher(void); ///< Destructor
/// \brief Allocate data objects for a new set of Varnodes
///
+ /// Allocate an array of VarnodeData as parameters to a single p-code op.
+ /// Allocation size may not exceed MAX_ALLOCATION objects.
/// \param size is the number of objects to allocate
/// \return a pointer to the array of available VarnodeData objects
VarnodeData *allocateVarnodes(uint4 size) {
VarnodeData *newptr = curpool + size;
- if (newptr <= endpool) {
- VarnodeData *res = curpool;
- curpool = newptr;
- return res;
+ if (newptr > endpool) {
+ expandPool(size);
+ newptr = curpool + size;
}
- return expandPool(size);
+ VarnodeData *res = curpool;
+ curpool = newptr;
+ return res;
}
/// \brief Allocate a data object for a new p-code operation