-
Notifications
You must be signed in to change notification settings - Fork 150
/
RelocGraph.C
247 lines (214 loc) · 6.67 KB
/
RelocGraph.C
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* See the dyninst/COPYRIGHT file for copyright information.
*
* We provide the Paradyn Tools (below described as "Paradyn")
* on an AS IS basis, and do not warrant its validity or performance.
* We reserve the right to update, modify, or discontinue this
* software at any time. We shall have no obligation to supply such
* updates or modifications or any other form of support to you.
*
* By your use of Paradyn, you understand and agree that we (or any
* other person or entity with proprietary rights in Paradyn) are
* under no obligation to provide either maintenance services,
* update services, notices of latent defects, or correction of
* defects for Paradyn.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "RelocGraph.h"
#include "RelocBlock.h"
#include <iostream>
using namespace Dyninst;
using namespace Relocation;
using namespace std;
RelocGraph::~RelocGraph() {
for (Edges::iterator iter = edges.begin(); iter != edges.end(); ++iter) {
delete *iter;
}
std::set<func_instance*> funcs_to_clean;
RelocBlock *cur = head;
while (cur) {
RelocBlock *next = cur->next();
funcs_to_clean.insert(cur->func());
delete cur;
cur = next;
}
for(auto f = funcs_to_clean.begin();
f != funcs_to_clean.end();
++f)
{
if(*f) (*f)->freeStackMod();
}
}
void RelocGraph::addRelocBlock(RelocBlock *t) {
if (t->type() == RelocBlock::Relocated) {
springboards[std::make_pair(t->block(), t->func())] = t;
reloc[t->block()][t->func()] = t;
}
if (head == NULL) {
assert(tail == NULL);
head = t;
tail = t;
}
else {
assert(tail != NULL);
link(tail, t);
tail = t;
}
size++;
}
void RelocGraph::addRelocBlockBefore(RelocBlock *cur, RelocBlock *t) {
if (t->type() == RelocBlock::Relocated) {
springboards[std::make_pair(t->block(), t->func())] = t;
reloc[t->block()][t->func()] = t;
}
size++;
if (cur == head) {
head = t;
}
else {
RelocBlock *prev = cur->prev();
link(prev, t);
}
link(t, cur);
}
void RelocGraph::addRelocBlockAfter(RelocBlock *cur, RelocBlock *t) {
if (t->type() == RelocBlock::Relocated) {
springboards[std::make_pair(t->block(), t->func())] = t;
reloc[t->block()][t->func()] = t;
}
size++;
if (cur == tail) {
tail = t;
}
else {
RelocBlock *next = cur->next();
link(t, next);
}
link(cur, t);
}
RelocBlock *RelocGraph::find(block_instance *b, func_instance *f) const {
InstanceMap::const_iterator iter = reloc.find(b);
if (iter == reloc.end()) return NULL;
SubMap::const_iterator iter2 = iter->second.find(f);
if (iter2 == iter->second.end()) return NULL;
return iter2->second;
}
RelocBlock *RelocGraph::findSpringboard(block_instance *b, func_instance *f) const {
Map::const_iterator iter = springboards.find(std::make_pair(b, f));
if (iter == springboards.end()) return NULL;
return iter->second;
}
bool RelocGraph::setSpringboard(block_instance *from, func_instance *func, RelocBlock *to) {
if (springboards.find(std::make_pair(from, func)) == springboards.end()) return false;
springboards[std::make_pair(from, func)] = to;
return true;
}
RelocEdge *RelocGraph::makeEdge(TargetInt *s,
TargetInt *t,
edge_instance* e,
ParseAPI::EdgeTypeEnum et) {
RelocEdge *edge = new RelocEdge(s, t, e, et);
edges.push_back(edge);
s->addTargetEdge(edge);
t->addSourceEdge(edge);
return edge;
}
bool RelocGraph::removeEdge(RelocEdge *e) {
// Pull it from the source and target, but
// don't worry about our vector; we'll just let it
// dangle for now and delete it later.
removeSource(e);
removeTarget(e);
return true;
}
void RelocGraph::removeSource(RelocEdge *e) {
e->src->removeTargetEdge(e);
e->src = NULL;
}
void RelocGraph::removeTarget(RelocEdge *e) {
e->trg->removeSourceEdge(e);
e->trg = NULL;
}
void RelocGraph::link(RelocBlock *s, RelocBlock *t) {
if (s)
s->setNext(t);
if (t)
t->setPrev(s);
}
bool RelocGraph::interpose(RelocEdge *e, RelocBlock *trace) {
// Take s ->(e) t to
// s ->(e) n, n ->(FT) t
TargetInt *s = e->src;
TargetInt *t = e->trg;
// Targets aren't refcounted...
TargetInt *n1 = new Target<RelocBlock *>(trace);
TargetInt *n2 = new Target<RelocBlock *>(trace);
makeEdge(s, n1, e->edge, e->type);
makeEdge(n2, t, e->edge, ParseAPI::FALLTHROUGH);
removeEdge(e);
// So we don't try to free 'em...
e->src = NULL;
e->trg = NULL;
return true;
}
bool RelocGraph::changeTarget(RelocEdge *e, TargetInt *n) {
removeTarget(e);
delete e->trg;
e->trg = n;
n->addSourceEdge(e);
return true;
}
bool RelocGraph::changeSource(RelocEdge *e, TargetInt *n) {
removeSource(e);
delete e->src;
e->src = n;
n->addTargetEdge(e);
return true;
}
bool RelocGraph::changeType(RelocEdge *e, ParseAPI::EdgeTypeEnum t) {
e->type = t;
return true;
}
bool Predicates::Interprocedural::operator()(RelocEdge *e) {
return (e->type == ParseAPI::CALL ||
e->type == ParseAPI::RET);
}
bool Predicates::Intraprocedural::operator()(RelocEdge *e) {
return (e->type != ParseAPI::CALL &&
e->type != ParseAPI::RET);
}
bool Predicates::Fallthrough::operator()(RelocEdge *e) {
return (e->type == ParseAPI::FALLTHROUGH);
}
bool Predicates::Call::operator()(RelocEdge *e) {
return (e->type == ParseAPI::CALL);
}
bool Predicates::NonCall::operator()(RelocEdge *e) {
return (e->type != ParseAPI::CALL);
}
bool Predicates::CallFallthrough::operator()(RelocEdge *e) {
return (e->type == ParseAPI::CALL_FT);
}
bool Predicates::Edge::operator()(RelocEdge *e) {
if (e->type != e_->type()) return false;
// Match up source and target blocks
if (e->src->block() == e_->src() &&
e->trg->block() == e_->trg()) return true;
return false;
}
bool Predicates::Type::operator()(RelocEdge *e) {
return e->type == t_;
}