-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCFunction.cpp
219 lines (193 loc) · 6.13 KB
/
SCFunction.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* =====================================================================================
*
* Filename: SCFunction.cpp
*
* Description: Implementation of SCFunction and SCFunctionList.
*
* Version: 1.0
* Created: 2014/03/21 14时52分31秒
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include "SCFunction.h"
#include <stdlib.h>
#include <iterator>
#include <vector>
#include <algorithm>
#include "SCInstr.h"
#include "SCBlock.h"
#include "symbol.h"
#include "SCEdge.h"
SCFunction::SCFunction() {
this->f_flags = 0;
this->f_first = this->f_last = NULL;
this->f_id = 0; //TODO: global increase
this->f_name = new char[20];
this->f_entry = new SCBlock();
this->f_exit = new SCBlock();
f_entry->setType(BT_ENTRY);
f_exit->setType(BT_EXIT);
f_entry->setFunction(this);
f_exit->setFunction(this);
}
SCFunction::~SCFunction() {
BLOCKLIST->deleteBBLs(f_first, f_last);
}
void SCFunction::setFirstBlock(SCBlock *bbl) {
this->f_first = bbl;
}
void SCFunction::setLastBlock(SCBlock *bbl) {
this->f_last= bbl;
}
void SCFunction::setName(UINT8* name) {
char *tn = (char*) name;
this->f_name = tn;
}
void SCFunction::setName(const char* name) {
strcpy(f_name, name);
}
void SCFunction::setEntryBlock(SCBlock* bbl) {
this->f_entry = bbl;
}
void SCFunction::setExitBlock(SCBlock* bbl) {
this->f_exit = bbl;
}
SCBlock* SCFunction::getFirstBlock() {
return this->f_first;
}
SCBlock* SCFunction::getLastBlock() {
return this->f_last;
}
char* SCFunction::getName() {
return this->f_name;
}
SCBlock* SCFunction::getEntryBlock() {
return this->f_entry;
}
SCBlock* SCFunction::getExitBlock() {
return this->f_exit;
}
int SCFunction::getPos() {
return FUNLIST->getFunctionPos(this);
}
void SCFunction::serialize(const char *prefix) {
SCLog(RL_ZERO, "%s====SCFunction%d(0x%x)====", prefix, FUNLIST->getFunctionPos(this), this);
SCLog(RL_ZERO, "%sFunction name: %s", prefix, f_name);
SCLog(RL_ZERO, "%sFirst bbl: %d", prefix, BLOCKLIST->getBBLPos(f_first));
SCLog(RL_ZERO, "%sLast bbl: %d", prefix, BLOCKLIST->getBBLPos(f_last));
char np[100];
strcpy(np, prefix);
strcat(np, "\t");
// show all the instrs within this bbl
f_first->serialize(np);
SCLog(RL_ZERO, "");
if (f_first != f_last) {
SCBlock* bbl = f_first;
while((bbl=BLOCKLIST->getNextBBL(bbl)) != f_last) {
bbl->serialize(np);
SCLog(RL_ZERO, "");
}
f_last->serialize(np);
}
SCLog(RL_ZERO, "%s====END=SCFunction%d(0x%x)====", prefix, FUNLIST->getFunctionPos(this), this);
}
// ==== SCFunctionList ====
static SCFunctionList* _sharedFunctionList = NULL;
SCFunctionList::SCFunctionList() {
_sharedFunctionList = this;
}
SCFunctionList* SCFunctionList::sharedFunctionList() {
if (_sharedFunctionList == NULL) {
_sharedFunctionList = new SCFunctionList();
}
return _sharedFunctionList;
}
void SCFunctionList::createFunctionList() {
BlockIterT bit;
BlockListT bbls = BLOCKLIST->getBlockList();
SCFunction *fun;
for(bit=bbls.begin(); bit!=bbls.end(); ++bit) {
if ((*bit)->getFirstInstr()->hasFlag(FUN_START)) {
fun = new SCFunction();
fun->setFirstBlock(*bit);
fun->setLastBlock(*bit);
fun->setName(SYMLISTREL->getSymNameByAddr((*bit)->getFirstInstr()->getAddr()));
SCLog(RL_ONE, "fun name: %s", fun->getName());
(this->p_funs).push_back(fun);
}
else if ((*bit)->getLastInstr()->hasFlag(FUN_END)) {
fun->setLastBlock(*bit);
}
(*bit) -> setFunction(fun);
}
return;
}
void SCFunctionList::markFunctions() {
SymListRELT funSyms = SYMLISTREL->getFunSymList();
SCInstr* ins;
for (SymIterRELT it=funSyms.begin(); it!=funSyms.end(); ++it) {
ins = INSTRLIST->addrToInstr((*it)->getSymbolValue());
if (ins == NULL)
continue;
SCLog(RL_ONE, "symbol(%s) at 0x%x, instr pos is %d", (*it)->getSymbolName(), (*it)->getSymbolValue(), ins->getPos());
ins->setFlag(FUN_START);
ins->setFlag(BBL_START);
if (ins = INSTRLIST->getPrevInstr(ins)) {
ins->setFlag(FUN_END);
ins->setFlag(BBL_END);
}
}
}
void SCFunctionList::deleteFunctions(SCFunction* first, SCFunction* last) {
if (!first || !last)
return;
FunIterT fit = p_funs.begin();
FunIterT lit = p_funs.end();
if (fit==p_funs.end() || lit==p_funs.end())
return;
if (std::distance(fit,lit)<0)
return;
++lit;
for(FunIterT it=fit; it!=lit; ++it) {
delete *it;
}
p_funs.erase(fit, lit);
}
void SCFunctionList::resolveEntrylessFunction() {
for (FunIterT fit=p_funs.begin(); fit!=p_funs.end(); ++fit) {
if ((*fit)->getEntryBlock()->getSucc().size() == 0) {
for (BlockIterT bit=BLOCKLIST->getIterByBBL((*fit)->getFirstBlock());
bit!=BLOCKLIST->getIterByBBL((*fit)->getLastBlock()); ++bit)
{
SCInstr* first = (*bit)->getFirstInstr();
SCInstr* second = INSTRLIST->getNextInstr(first);
// TODO: should restrict the oprand: push ebp; mov esp, ebp
if (first->isPushClass() && second->isMovClass())
{
BLOCKLIST->addBBLEdge((*fit)->getEntryBlock(), (*bit), ET_ENTRY);
}
}
}
}
for (FunIterT fit=p_funs.begin(); fit!=p_funs.end(); ++fit) {
if ((*fit)->getEntryBlock()->getSucc().size() == 0) {
BLOCKLIST->addBBLEdge((*fit)->getEntryBlock(), (*fit)->getFirstBlock(), ET_ENTRY);
}
}
}
int SCFunctionList::getFunctionPos(SCFunction* fun) {
FunIterT fit = std::find(p_funs.begin(), p_funs.end(), fun);
return std::distance(p_funs.begin(), fit);
}
void SCFunctionList::serialize() {
for(FunIterT fit=p_funs.begin(); fit!=p_funs.end(); ++fit) {
(*fit)->serialize();
SCLog(RL_ZERO, "");
}
}