-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjexplore-scan.cpp
More file actions
352 lines (295 loc) · 9.02 KB
/
jexplore-scan.cpp
File metadata and controls
352 lines (295 loc) · 9.02 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <sstream>
#include <fstream>
#include <sys/wait.h>
#include <sys/types.h>
#include <map>
#include <vector>
#include <algorithm>
#define _FILE_OFFSET_BITS 64
std::string help = "USAGE ./jexplore-scan -[f|s|c] coredump vtable-file|vtable-addr section1 [section2 ...]\n"
"\n"
"Program scans the coredump file searching for occurences of vptrs for respective polymorphic classes.\n"
"The address of vptr is calculated as vtable address + 0x10 (can be checked with -fdump-class-hierarchy g++ flag).\n"
"\n"
"Program has three modes: -f(default), -s, -c.\n"
"1) In the full mode the occurences of vptrs are printed as '0xaddress classname(vtable address if no typename available)'.\n"
"2) In the stats mode the number of occurences for each type is printed in sorted order to stderr.\n"
"3) Combined mode is the same as full, only that stats are written as in -s mode to stderr.\n"
"Instead of -stat, you can write -s[NUM], in this case only vptrs with occurences more than NUM will be printed\n"
"Same holds for -c[NUM]\n"
"\n"
"Coredump should be of a format parsible by objdump -h.\n"
"\n"
"Vtable-file must have a format with 0xaddress of vtable being the first word and classname following word 'for'.\n"
"It can be generated by:\n"
"1) gdb -ex 'info variable vtable*' -ex 'quit' binary coredump\n"
"2) nm binary | c++filt | grep vtable\n"
"\n"
"Vtable-addr should be of format 0xhex, if filename is of format 0xhex it is used as single vptr.\n"
"\n"
"Section# should be the name of objdump -h section.\n";
struct ELF_Section {
std::string name;
size_t size;
size_t vma;
size_t file_oft;
ELF_Section(std::string n) : name(n) {}
};
struct Vptr {
long vptr;
std::string classname;
size_t occur;
Vptr(long vtable, std::string name) :
vptr(vtable+0x10), classname(name), occur(0) {}
bool operator < (const Vptr& v) const {
return (v.occur < occur);
}
};
bool is_hex_notation(std::string const& s)
{
return s.compare(0, 2, "0x") == 0
&& s.size() > 2
&& s.find_first_not_of("0123456789abcdefABCDEF", 2) == std::string::npos;
}
int parse_arguments(int argc, char** argv, int& full, size_t& limit, std::string& core_filename,
std::vector<Vptr>& vptrs, std::vector<ELF_Section>& sections) {
if (argc < 4) {
std::cout << help;
return -1;
}
full = 1;
bool is_vptr_in_file = false;
std::string vtable_filename;
if (argv[1][0] == '-') {
if (argv[1][1] == 'f') {
full = 1;
} else if (argv[1][1] == 's') {
full = -1;
std::stringstream ss;
ss << &argv[1][2];
ss >> limit;
} else if (argv[1][1] == 'c') {
full = 0;
std::stringstream ss;
ss << &argv[1][2];
ss >> limit;
} else {
std::cout << help;
return -1;
}
core_filename = std::string(argv[2]);
if (is_hex_notation(argv[3])) {
long vtable_addr;
std::stringstream ss;
ss << std::hex << argv[3];
ss >> vtable_addr;
vptrs.push_back(Vptr(vtable_addr, std::string("Unknown")));
is_vptr_in_file = false;
} else {
vtable_filename = argv[3];
is_vptr_in_file = true;
}
for (int i=4; i < argc; i++)
sections.push_back(ELF_Section(argv[i]));
} else {
core_filename = std::string(argv[1]);
if (is_hex_notation(argv[2])) {
long vtable_addr;
std::stringstream ss;
ss << std::hex << argv[2];
ss >> vtable_addr;
vptrs.push_back(Vptr(vtable_addr, std::string("Unknown")));
is_vptr_in_file = false;
} else {
vtable_filename = argv[2];
is_vptr_in_file = true;
}
for (int i=3; i < argc; i++)
sections.push_back(ELF_Section(argv[i]));
}
if (is_vptr_in_file) {
std::ifstream file;
std::string line;
int for_pos;
file.open(vtable_filename.c_str());
if (file.is_open()) {
while (getline(file, line)) {
std::istringstream iss(line);
long vtable_addr;
iss >> std::hex >> vtable_addr;
for_pos = line.find(std::string("for"));
vptrs.push_back(Vptr(vtable_addr, line.substr(for_pos + 4))); // "for " = 4 char
}
file.close();
} else {
std::cerr << "Could not open the file: " << vtable_filename << std::endl;
return -1;
}
}
return 0;
}
void readelf_header(std::string const& core_filename, std::vector<ELF_Section>& sections) {
int objdump_awk[2];
int awk_parent[2];
pid_t obj_pid, awk_pid;
if (pipe(objdump_awk) == -1) {
perror("Pipe objdump | awk");
exit(EXIT_FAILURE);
}
if ((obj_pid = fork()) == 0) { // objdump
close(STDOUT_FILENO);
dup(objdump_awk[1]);
close(objdump_awk[0]);
close(objdump_awk[1]);
const char* filename = core_filename.c_str();
char* objdump[] = { const_cast<char*>("objdump"), const_cast<char*>("-h"), const_cast<char*>(core_filename.c_str()), 0 };
execvp(objdump[0], objdump);
perror("objdump fail to start");
exit(EXIT_FAILURE);
}
if (pipe(awk_parent) == -1) {
perror("Pipe awk | parent");
exit(EXIT_FAILURE);
}
if ((awk_pid = fork()) == 0) { // awk '/^[ ]+[0-9]+/ {print $2" "$3" "$4" "$6}'
close(STDIN_FILENO);
dup(objdump_awk[0]);
close(objdump_awk[1]);
close(objdump_awk[0]);
close(STDOUT_FILENO);
dup(awk_parent[1]);
close(awk_parent[0]);
close(awk_parent[1]);
char* awk[] = { const_cast<char*>("awk"),
const_cast<char*>("/^[ ]+[0-9]+/ {print $2\" \"$3\" \"$4\" \"$6}"), 0 };
execvp(awk[0], awk);
perror("awk fail to start");
exit(EXIT_FAILURE);
}
close(objdump_awk[0]);
close(objdump_awk[1]);
close(awk_parent[1]);
waitpid(obj_pid, NULL, 0);
std::map<std::string, size_t> name_2_index;
for (size_t i=0; i < sections.size(); i++)
name_2_index[sections[i].name] = i;
std::string name;
size_t size;
size_t vma;
size_t file_oft;
char c;
char buf[50];
int bp = 0; //buffer pointer
int step = 0;
while (read(awk_parent[0], &c, 1) > 0) {
buf[bp++] = c;
if (c == '\n' || c == ' ') {
std::stringstream ss;
if (step == 0) {
ss << buf;
ss >> name;
if (name_2_index.find(name) == name_2_index.end()) {
bp = 0;
continue;
}
bp = 0;
step++;
} else if (step == 1) {
ss << std::hex << buf;
ss >> size;
bp = 0;
step++;
} else if (step == 2) {
ss << std::hex << buf;
ss >> vma;
bp = 0;
step++;
} else {
ss << std::hex << buf;
ss >> file_oft;
bp = 0;
step = 0;
int idx = name_2_index[name];
sections[idx].size = size;
sections[idx].vma = vma;
sections[idx].file_oft = file_oft;
}
}
}
close(awk_parent[0]);
waitpid(awk_pid, NULL, 0);
}
int main(int argc, char** argv) {
int full;
size_t limit = 0;
std::string core_filename;
std::vector<Vptr> vptrs;
std::vector<ELF_Section> sections;
if (parse_arguments(argc, argv, full, limit, core_filename, vptrs, sections) < 0) {
exit(EXIT_FAILURE);
}
readelf_header(core_filename, sections);
//std::cout << "Full: " << full << std::endl;
//std::cout << "Core_filename: " << core_filename << std::endl;
//std::cout << "Vptrs:\n";
//for (size_t i=0; i < vptrs.size(); i++) {
// std::cout << std::hex << vptrs[i].vptr << " " << vptrs[i].classname << std::endl;
//}
//std::cout << "Sections:\n";
//for (size_t i=0; i < sections.size(); i++) {
// std::cout << std::hex << sections[i].name << " " << sections[i].size << " " << sections[i].vma <<
// " " << sections[i].file_oft << std::endl;
//}
FILE *fcore;
size_t read_size = 0;
long* buffer;
size_t page_size = getpagesize();
size_t buffer_size = 8*page_size;
size_t bc = 0; // byte count
if ((fcore = fopen(core_filename.c_str(), "rb")) == NULL) {
perror("Open fcore");
exit(EXIT_FAILURE);
}
buffer = (long*)malloc(buffer_size); // equal to PAGESIZE
for (size_t s = 0; s < sections.size(); s++) {
if ((fseeko(fcore, sections[s].file_oft, SEEK_SET)) < 0)
{
perror("Seeking in coredump");
exit(EXIT_FAILURE);
}
for (size_t i = 0; i < sections[s].size; i += buffer_size) {
if ((read_size = fread(buffer, page_size, 8, fcore)) < 0) {
perror("Reading coredump");
exit(EXIT_FAILURE);
}
for (size_t j = 0; j < page_size; j++, bc += 0x8) {
for (size_t v = 0; v < vptrs.size(); v++) {
if (buffer[j] == vptrs[v].vptr) {
if (full >= 0) {
std::cout << std::hex << bc+sections[s].vma << " " << vptrs[v].classname << std::endl;
} else {
vptrs[v].occur++;
}
}
}
}
}
}
free(buffer);
if (full <= 0) {
std::sort(vptrs.begin(), vptrs.end());
for (size_t i=0; i < vptrs.size(); i++) {
if (vptrs[i].occur < limit) {
break;
} else {
std::cerr << vptrs[i].occur << " " << vptrs[i].classname << " " << std::hex << vptrs[i].vptr << std::endl;
}
}
}
return 0;
}