/
main.cpp
290 lines (255 loc) · 7.51 KB
/
main.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
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
#include <sqlite3.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <unordered_set>
#include <string>
#include <cassert>
#include <thread>
#include <mutex>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/syscall.h>
#include <sys/user.h>
#include <sys/reg.h> /* For constants ORIG_EAX etc */
class SqlModelling
{
void exec_query(sqlite3 * db, const char * sql)
{
char *zErrMsg = 0;
int rc = sqlite3_exec(db, sql, NULL, 0, &zErrMsg);
if( rc != SQLITE_OK ){
std::cerr << "SQL error: " << zErrMsg << std::endl;
sqlite3_free(zErrMsg);
exit(2);
}
}
void torture_database(sqlite3 * db)
{
exec_query(db,
"CREATE TABLE IF NOT EXISTS COMPANY(" \
"ID INT NOT NULL," \
"NAME TEXT NOT NULL," \
"AGE INT NOT NULL," \
"ADDRESS CHAR(50)," \
"SALARY REAL );");
exec_query(db,
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " \
"VALUES (1, 'Paul', 32, 'California', 20000.00 ); " \
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " \
"VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); " \
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \
"VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );" \
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \
"VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );");
exec_query(db, "SELECT * from COMPANY");
}
struct CallbackData
{
CallbackData(std::vector<std::string>& d, sqlite3* db)
: tortured_databases_(d), db_(db)
{}
std::vector<std::string> & tortured_databases_;
sqlite3* db_;
};
static void trace_callback(void * arg, const char * query)
{
CallbackData * d = static_cast<CallbackData*>(arg);
std::string n(sqlite3_db_filename(d->db_, "main"));
d->tortured_databases_.push_back(n);
}
sqlite3* open_database(const char * dbname)
{
sqlite3 *db;
int rc = sqlite3_open(dbname, &db);
if( rc ){
std::cerr << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
exit(1);
}
return db;
}
public:
std::vector<std::string> process()
{
std::srand(std::time(0));
std::vector<CallbackData> dbs;
std::vector<std::string> tortured_databases;
dbs.emplace_back(CallbackData(tortured_databases, open_database("test1.db")));
dbs.emplace_back(CallbackData(tortured_databases, open_database("test2.db")));
dbs.emplace_back(CallbackData(tortured_databases, open_database("test3.db")));
dbs.emplace_back(CallbackData(tortured_databases, open_database("test4.db")));
dbs.emplace_back(CallbackData(tortured_databases, open_database("test5.db")));
for (size_t i = 0; i<dbs.size(); ++i) {
sqlite3_trace(dbs[i].db_, trace_callback, &dbs[i]);
}
for (size_t i = 0; i<dbs.size(); ++i) {
torture_database(dbs[std::rand() % dbs.size()].db_);
}
for (size_t i = 0; i<dbs.size(); ++i) {
sqlite3_close(dbs[i].db_);
}
return tortured_databases;
}
};
#define FORKING_ENABLED
#define TRACING_ENABLED
#ifdef TRACING_ENABLED
void trace(pid_t child, std::vector<std::string>& files, std::mutex & mutex)
{
waitpid(-1, NULL, __WALL);
ptrace(PTRACE_ATTACH, child, 0, 0);
ptrace(PTRACE_SETOPTIONS, child, NULL, PTRACE_O_TRACEFORK
| PTRACE_O_TRACEVFORK
| PTRACE_O_TRACECLONE
| PTRACE_O_TRACEEXIT
| PTRACE_O_EXITKILL);
pid_t pid = child;
while (true) {
int status;
ptrace(PTRACE_SYSCALL, pid, 0, 0);
pid = waitpid(-1, &status, __WALL);
if(WIFEXITED(status))
{
break;
}
else if (WIFSTOPPED(status))
{
struct user_regs_struct regs;
ptrace(PTRACE_GETREGS, pid, 0, ®s);
struct regs {
#ifdef __x86_64__
unsigned long
#endif
long
err,
syscall,
arg0;
} regs_ = {
#ifdef __x86_64__
regs.rax, regs.orig_rax, regs.rdi
#else
regs.eax, regs.orig_eax, regs.ebx
#endif
};
if (regs_.err == (unsigned long)-ENOSYS) { //syscall_enter stop
if (regs_.syscall == __NR_open) {
std::string file_name;
long word = ptrace(PTRACE_PEEKDATA, pid, regs_.arg0, NULL);
char * tmp = (char *)&word;
while (*tmp != '\0') {
file_name.push_back(*tmp);
++tmp;
if (tmp == (char *)(&word + 1)) {
regs_.arg0 += sizeof(word);
word = ptrace(PTRACE_PEEKDATA, pid, regs_.arg0, NULL);
tmp = (char *)&word;
}
}
{
std::lock_guard<std::mutex> l(mutex);
files.push_back(file_name);
}
}
}
else { //syscall_exit stop
}
}
}
}
#endif
#ifdef FORKING_ENABLED
std::vector<std::string> read_from_pipe(int pipefd)
{
std::vector<std::string> ret;
char buf;
if (read(pipefd, &buf, 1) <= 0)
return ret;
assert(buf != 0);
ret.push_back(std::string());
ret.back().push_back(buf);
while (read(pipefd, &buf, 1) > 0) {
if (buf == 0) {
ret.push_back(std::string());
}
else {
ret.back().push_back(buf);
}
}
return ret;
}
void write_to_pipe(int pipefd, const std::vector<std::string>& files)
{
for (std::vector<std::string>::const_iterator iter = files.begin();
iter != files.end();
++iter) {
write(pipefd, iter->c_str(), iter->size() + 1);
}
}
void search_unnecessary_openings(const std::vector<std::string>& opened_files,
const std::vector<std::string>& tortured)
{
std::unordered_set<std::string> tort(tortured.begin(), tortured.end());
std::unordered_set<std::string> opened(opened_files.begin(), opened_files.end());
for (std::unordered_set<std::string>::const_iterator iter = opened.begin();
iter != opened.end();
++iter) {
std::unordered_set<std::string>::const_iterator find_it = tort.find(*iter);
if (find_it == tort.end()) {
std::cout << "file: " << *iter << " unnecessary opened" << std::endl;
}
}
}
#endif
#ifdef FORKING_ENABLED
void reader_func(int child_socket, const std::vector<std::string>& opened_files, std::mutex& mutex)
{
std::vector<std::string> tortured = read_from_pipe(child_socket);
std::lock_guard<std::mutex> lk(mutex);
search_unnecessary_openings(opened_files, tortured);
}
#endif
int main()
{
#ifdef FORKING_ENABLED
int pipefd[2];
pipe(pipefd);
pid_t child = fork();
if (child == 0) {
close(pipefd[0]);
#ifdef TRACING_ENABLED
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
raise(SIGSTOP);
#endif
#endif
SqlModelling modelling;
std::vector<std::string> tortured = modelling.process();
#ifdef FORKING_ENABLED
write_to_pipe(pipefd[1], tortured);
close(pipefd[1]);
}
else {
close(pipefd[1]);
#ifdef TRACING_ENABLED
std::vector<std::string> files;
std::mutex mutex;
std::thread thread(reader_func, pipefd[0], std::ref(files), std::ref(mutex));
trace(child, files, mutex);
close(pipefd[0]);
thread.join();
#else
for (;;) {
int status;
waitpid(child, &status, __WNOTHREAD);
if (WIFEXITED(status)) {
break;
}
}
#endif
}
#endif
return 0;
}