2008-08-20 19:27:03 +00:00
|
|
|
#define ALLOCATESTRUCT 0x0
|
|
|
|
#define REALLOCATESTRUCT 0x1
|
|
|
|
#define FREESTRUCT 0x2
|
|
|
|
#define CREATESTRUCT 0x3
|
|
|
|
#define DESTROYSTRUCT 0x4
|
|
|
|
#define COALESCESTRUCT 0x5
|
|
|
|
|
2009-08-06 20:09:38 +00:00
|
|
|
#define SPACEBETWEEN 0x18
|
|
|
|
|
2008-08-20 19:27:03 +00:00
|
|
|
#define CHUNK(x) (heap->chunks[x])
|
2009-08-06 20:09:38 +00:00
|
|
|
|
|
|
|
#define NEXTADDR(x) (PVOID)(*((ULONG *)(&(CHUNK(i).addr))) + CHUNK(i).size + SPACEBETWEEN)
|
|
|
|
|
2008-08-20 19:27:03 +00:00
|
|
|
#define NULLNODE 0xffffffff
|
|
|
|
|
2009-08-06 20:09:38 +00:00
|
|
|
#define MODEL 1
|
|
|
|
#define LOG 2
|
|
|
|
#define RUNNING 4
|
|
|
|
|
2008-08-20 19:27:03 +00:00
|
|
|
// Space between chunks on vista
|
|
|
|
|
|
|
|
// Dont use a direct access list here because looping should
|
|
|
|
// be faster for our number of heaps (I think!)
|
|
|
|
struct HeapState {
|
2009-08-06 20:09:38 +00:00
|
|
|
BYTE state;
|
2008-08-20 19:27:03 +00:00
|
|
|
ULONG numHeaps;
|
|
|
|
ULONG hPoolListLen;
|
|
|
|
struct HPool *heaps;
|
2009-08-06 20:09:38 +00:00
|
|
|
|
2008-08-20 19:27:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct HPool {
|
|
|
|
PVOID base;
|
|
|
|
ULONG numChunks;
|
|
|
|
ULONG chunkListLen;
|
|
|
|
ULONG flags;
|
|
|
|
ULONG reserve;
|
|
|
|
ULONG commit;
|
|
|
|
BOOLEAN lock;
|
|
|
|
ULONG *map;
|
|
|
|
struct HeapChunk *chunks;
|
|
|
|
ULONG inUseHead;
|
|
|
|
ULONG lastInUse;
|
|
|
|
BOOLEAN inUse;
|
|
|
|
};
|
|
|
|
|
2009-08-06 20:09:38 +00:00
|
|
|
struct LookAsideList {
|
|
|
|
DWORD placeHolder;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct HeapCache {
|
|
|
|
ULONG NumBuckets;
|
|
|
|
unsigned __int8 *pBitmap;
|
|
|
|
ULONG **pBuckets;
|
|
|
|
};
|
|
|
|
|
2008-08-20 19:27:03 +00:00
|
|
|
struct HeapChunk {
|
|
|
|
PVOID addr;
|
|
|
|
PVOID heapHandle;
|
|
|
|
ULONG size;
|
|
|
|
ULONG flags;
|
|
|
|
ULONG nextBucket;
|
|
|
|
ULONG nextInUse;
|
2009-08-06 20:09:38 +00:00
|
|
|
ULONG nextFreeListChunk;
|
2008-08-20 19:27:03 +00:00
|
|
|
BOOLEAN free;
|
|
|
|
BOOLEAN inUse;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AllocateStruct {
|
|
|
|
BYTE type;
|
|
|
|
PVOID heapHandle;
|
|
|
|
ULONG flags;
|
|
|
|
ULONG size;
|
|
|
|
PVOID ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ReallocateStruct {
|
|
|
|
BYTE type;
|
|
|
|
PVOID heapHandle;
|
|
|
|
ULONG flags;
|
|
|
|
PVOID memoryPointer;
|
|
|
|
ULONG size;
|
|
|
|
PVOID ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FreeStruct {
|
|
|
|
BYTE type;
|
|
|
|
PVOID heapHandle;
|
|
|
|
ULONG flags;
|
|
|
|
PVOID memoryPointer;
|
|
|
|
PVOID ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CreateStruct {
|
|
|
|
BYTE type;
|
|
|
|
ULONG flags;
|
|
|
|
PVOID base;
|
|
|
|
ULONG reserve;
|
|
|
|
ULONG commit;
|
|
|
|
BOOLEAN lock;
|
|
|
|
PVOID RtlHeapParams; // Wont get this info back now - maybe later
|
|
|
|
PVOID ret; // if we think we really need it?
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DestroyStruct {
|
|
|
|
BYTE type;
|
|
|
|
PVOID heapHandle;
|
|
|
|
NTSTATUS ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CoalesceStruct {
|
|
|
|
BYTE type;
|
|
|
|
PVOID heapHandle;
|
|
|
|
ULONG arg2;
|
|
|
|
ULONG arg3;
|
|
|
|
ULONG arg4;
|
|
|
|
PVOID ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
void initializeHeapModel(struct HeapState *);
|
|
|
|
void heapAllocate(struct HeapState *heapModel, struct AllocateStruct *aStruct);
|
2009-08-06 20:09:38 +00:00
|
|
|
void logAllocate(struct HeapState *heapModel, struct AllocateStruct *aStruct);
|
|
|
|
|
2008-08-20 19:27:03 +00:00
|
|
|
void heapReallocate(struct HeapState *heapModel, struct ReallocateStruct *aStruct);
|
2009-08-06 20:09:38 +00:00
|
|
|
void logReallocate(struct HeapState *heapModel, struct ReallocateStruct *aStruct);
|
|
|
|
|
2008-08-20 19:27:03 +00:00
|
|
|
void heapFree(struct HeapState *heapModel, struct FreeStruct *fStruct);
|
2009-08-06 20:09:38 +00:00
|
|
|
void logFree(struct HeapState *heapModel, struct FreeStruct *fStruct);
|
|
|
|
|
2008-08-20 19:27:03 +00:00
|
|
|
void heapCreate(struct HeapState *heapModel, struct CreateStruct *cStruct);
|
|
|
|
void heapDestroy(struct HeapState *heapModel, struct DestroyStruct *dStruct);
|
|
|
|
void heapCoalesce(struct HeapState *heapModel, struct CoalesceStruct *cfbStruct);
|
|
|
|
struct HPool *getHeap(struct HeapState *heapModel, PVOID heapHandle);
|
|
|
|
struct HeapChunk *getChunk(struct HPool *heap, PVOID memoryPointer, ULONG inAfter);
|
|
|
|
int FindOffsetForChunk(struct HPool *heap, PVOID memoryPointer); //quickly match a (heap, chunkAddress) into an offset in heap.chunks
|
|
|
|
|