This repository has been archived by the owner on Jan 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackmemory.cpp
162 lines (148 loc) · 5.06 KB
/
stackmemory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <algorithm>
#include <iostream>
#include "stackmemory.h"
StackChunk::StackChunk(unsigned long startAddr, unsigned long size,
std::string funcStartAddr, std::vector<HowardType *>*types) {
this->startAddr = startAddr;
this->setSize(size);
this->funcStartAddr = funcStartAddr;
this->types = types;
}
unsigned long StackChunk::getStartAddr() {
return this->startAddr;
}
unsigned long StackChunk::getEndAddr() {
return this->endAddr;
}
void StackChunk::setSize(unsigned long size) {
this->size = size;
this->endAddr = this->startAddr - size;
}
unsigned long StackChunk::getSize() {
return this->size;
}
std::string StackChunk::getFuncStartAddr() {
return this->funcStartAddr;
}
std::vector<HowardType *>* StackChunk::getTypes() {
return this->types;
}
std::vector<HowardType*>* StackChunk::findTypeForOffset(unsigned long offset) {
std::cout << "StackChunk::findTypeForOffset: offset: " << offset
<< std::endl;
if (this->types == NULL) {
std::cout << "StackChunk::findTypeForOffset: no types present at all: "
<< offset << std::endl;
return NULL;
}
std::vector<HowardType*>::iterator it = this->types->begin();
std::vector<HowardType*>*curTypes = new std::vector<HowardType*>();
std::cout << "StackChunk::findTypeForOffset: checking types for offset: "
<< offset << std::endl;
for (; this->types->end() != it; it++) {
HowardType *curType = (*it);
unsigned long curTypeOffset = curType->getOffset();
unsigned long curTypeSize = curType->getSize();
std::cout << "StackChunk::findTypeForOffset: testing offset: " << offset
<< " against curTypeOffset: " << curTypeOffset
<< " curTypeSize: " << curTypeSize << std::endl;
if (curType->getIsCompound()) {
std::cout << "StackChunk::findTypeForOffset: is compound"
<< std::endl;
if (curTypeOffset - curTypeSize <= offset
&& offset <= curTypeOffset) {
std::cout
<< "StackChunk::findTypeForOffset: offset inside range"
<< std::endl;
return curType->findTypeForOffset(curTypeOffset - offset);
} else {
std::cout
<< "StackChunk::findTypeForOffset: is compound data type -> offset not in range!"
<< std::endl;
}
} else {
// For primitive types: offsets must match exactly
if (curTypeOffset == offset) {
std::cout
<< "StackChunk::findTypeForOffset: is primitive data type"
<< std::endl;
// We have a primitive data type
curTypes->push_back(curType);
return curTypes;
} else {
std::cout
<< "StackChunk::findTypeForOffset: is primitive data type -> no exact offset match!"
<< std::endl;
}
}
}
std::cout << "StackChunk::findTypeForOffset: nothing found, returning NULL"
<< std::endl;
return NULL;
}
/* Stack */
Stack::Stack() {
this->stackChunks = new std::vector<StackChunk*>();
}
// Functor: Helper to find stack chunks
struct FindStackChunkByAddress: std::unary_function<StackChunk*, bool> {
private:
unsigned long address;
public:
FindStackChunkByAddress(const unsigned long addr) :
address(addr) {
}
bool operator()(StackChunk* chunk) {
// Stack grows downward: startAddr > endAddr
return chunk->getStartAddr() >= address
&& address >= chunk->getEndAddr();
}
};
std::vector<StackChunk*>::iterator Stack::findStackChunkByStartAddress(
unsigned long address) {
return std::find_if(this->stackChunks->begin(), this->stackChunks->end(),
FindStackChunkByAddress(address));
}
bool Stack::popStackChunk() {
std::cout << "Stack::popStackChunk(): stackChunks->size(): " << std::dec
<< this->stackChunks->size() << std::endl;
this->stackChunks->pop_back();
std::cout << "Stack::popStackChunk(): stackChunks->size() after: "
<< std::dec << this->stackChunks->size() << std::endl;
return true;
}
bool Stack::pushStackChunk(StackChunk*chunk) {
std::cout << "Stack::pushStackChunk(): stackChunks->size(): " << std::dec
<< this->stackChunks->size() << std::endl;
this->stackChunks->push_back(chunk);
std::cout << "Stack::pushStackChunk(): stackChunks->size() after: "
<< std::dec << this->stackChunks->size() << std::endl;
return true;
}
StackChunk * Stack::back() {
return this->stackChunks->back();
}
void Stack::printStack() {
std::vector<StackChunk*>::iterator it = this->stackChunks->begin();
std::cout << "Stack:" << std::endl;
for (; this->stackChunks->end() != it; it++) {
std::cout << "\tmem chunk:" << std::endl;
std::cout << "\t\tstartAddr: " << std::hex << (*it)->getStartAddr()
<< " endAddr: " << std::hex << (*it)->getEndAddr() << " size: "
<< std::dec << (*it)->getSize() << " types: "
<< (*it)->getTypes()->size()
<< " func start addr: " << (*it)->getFuncStartAddr()
<< std::endl;
}
}
std::vector<StackChunk*>::iterator Stack::getStackChunksEndIt() {
return this->stackChunks->end();
}
bool Stack::addrInRedZone(unsigned long address) {
StackChunk *lastStackFrame = this->stackChunks->back();
std::cout << "Stack::addrInRedZone: lastStackFrame: " << std::hex
<< lastStackFrame << std::endl;
std::cout << "Stack::addrInRedZone: lastStackFrame->getEndAddr: "
<< std::hex << lastStackFrame->getEndAddr() << std::endl;
return lastStackFrame->getEndAddr() > address;
}