-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDBInterpreter.cpp
More file actions
567 lines (467 loc) · 16.1 KB
/
DBInterpreter.cpp
File metadata and controls
567 lines (467 loc) · 16.1 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
* DBInterpreter.cpp
*
* Created on: Aug 28, 2014
* Author: wilhelma
*/
#include "DBInterpreter.h"
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include "Event.h"
#include "EventService.h"
#include "ShadowThread.h"
#include "ShadowVar.h"
#include "ShadowLock.h"
#include "LockMgr.h"
#include "ThreadMgr.h"
#include "DBTable.h"
DBInterpreter::DBInterpreter(const char* DBPath,
const char* logFile,
EventService *service,
LockMgr *lockMgr,
ThreadMgr *threadMgr)
: Interpreter(lockMgr, threadMgr, logFile), _dbPath(DBPath), _logFile(logFile),
_eventService(service) { }
DBInterpreter::~DBInterpreter(){ }
EventService* DBInterpreter::getEventService() {
return _eventService;
}
Instruction::type DBInterpreter::transformInstrType(const instruction_t &ins) {
if (strcmp( ins.instruction_type, "CALL") == 0)
return Instruction::CALL;
else if (strcmp( ins.instruction_type, "ACCESS" ) == 0)
return Instruction::MEMACCESS;
else if (strcmp( ins.instruction_type, "CSENTER" ) == 0)
return Instruction::ACQUIRE;
else if (strcmp( ins.instruction_type, "CSLEAVE" ) == 0)
return Instruction::RELEASE;
else if (strcmp( ins.instruction_type, "THRCREATE" ) == 0)
return Instruction::FORK;
return Instruction::OTHER;
}
int DBInterpreter::loadDB(const char* path, sqlite3 **db) {
if (sqlite3_open_v2(path, db,
SQLITE_OPEN_READONLY | SQLITE_OPEN_NOMUTEX, NULL) != SQLITE_OK) {
BOOST_LOG_TRIVIAL(fatal) << "Can't open " << path << " - error: "
<< sqlite3_errmsg(*db);
sqlite3_close(*db);
return IN_ABORT;
} else {
BOOST_LOG_TRIVIAL(trace) << "successfully opened " << path;
return IN_OK;
}
}
int DBInterpreter::closeDB(sqlite3 **db) {
if ( sqlite3_close(*db) != SQLITE_OK)
BOOST_LOG_TRIVIAL(fatal) << "Can't close database - error: "
<< sqlite3_errmsg(*db);
return IN_OK;
}
int DBInterpreter::process() {
sqlite3 *db;
// open the database
if ( loadDB(_dbPath, &db) != IN_OK ) {
return IN_ABORT;
}
// fill the internal maps with database entries
int rc = fillStructures(&db);
if ( rc != IN_OK) {
BOOST_LOG_TRIVIAL(error) << "Can't fill internal structures."
<< " Error code: " << rc;
return IN_ABORT;
}
// process database entries
for (auto instruction : instructionT_)
processInstruction(instruction.second);
closeDB(&db);
return 0;
}
int DBInterpreter::processInstruction(const instruction_t& ins) {
processAccess_t accessFunc = nullptr;
segment_t* segment = nullptr;
call_t* call = nullptr;
if ( segmentT_.get(ins.segment_id, &segment) == IN_OK) {
switch( transformInstrType(ins) ) {
case Instruction::CALL:
processSegment(ins.segment_id, *segment, ins);
break;
case Instruction::MEMACCESS:
if ( callT_.get(segment->call_id, &call) == IN_OK) {
accessFunc = &DBInterpreter::processMemAccess;
}
break;
case Instruction::ACQUIRE:
if ( callT_.get(segment->call_id, &call) == IN_OK) {
accessFunc = &DBInterpreter::processAcqAccess;
}
break;
case Instruction::RELEASE:
if ( callT_.get(segment->call_id, &call) == IN_OK) {
accessFunc = &DBInterpreter::processRelAccess;
}
break;
case Instruction::FORK:
{
thread_t *thread;
if ( threadT_.get(ins.instruction_id, &thread ) == IN_OK)
if (callT_.get(segment->call_id, &call) == IN_OK)
processFork(ins,
*segment,
*call,
*thread);
break;
}
case Instruction::JOIN:
{
thread_t *thread;
if ( threadT_.get(ins.instruction_id, &thread ) == IN_OK)
if (callT_.get(segment->call_id, &call) == IN_OK)
processJoin(ins,
*segment,
*call,
*thread);
break;
}
default:
return IN_NO_ENTRY;
}
}
if (accessFunc != nullptr) {
auto search = _insAccessMap.find(ins.instruction_id);
accessVector_t::iterator it;
for (it = search->second.begin(); it != search->second.end(); it++) {
auto searchAccess = accessT_.find(*it);
if (searchAccess != accessT_.end()) {
processAccessGeneric(search->first,
searchAccess->second,
ins,
*segment,
*call,
accessFunc);
} else {
BOOST_LOG_TRIVIAL(error) << "Access not found: " << *it;
return IN_NO_ENTRY;
}
}
}
return IN_OK;
}
int DBInterpreter::processSegment(unsigned int segmentId,
const segment_t& seg,
const instruction_t& ins) {
auto search = callT_.find(seg.call_id);
if (search != callT_.end()) {
if (!processCall(seg.call_id.c_str(), search->second, seg, ins))
return 1;
} else {
BOOST_LOG_TRIVIAL(error) << "Call not found: " << seg.call_id;
return 1;
}
return 0;
}
int DBInterpreter::processCall(const char* callId,
const call_t& call,
const segment_t& seg,
const instruction_t& ins) {
auto search = functionT_.find(call.function_id);
if (search != functionT_.end()) {
switch(function_t::getFunctionType(search->second.type)) {
case Function::FUNCTION:
case Function::METHOD:
{
auto searchFile = fileT_.find(search->second.file_id);
if (searchFile != fileT_.end()) {
CallInfo info( std::atoi(call.end_time), // todo: use runtime!
search->second.signature,
function_t::getFunctionType(search->second.type),
searchFile->second.file_name,
searchFile->second.file_path);
ShadowThread* thread = threadMgr_->getThread(call.thread_id);
CallEvent event(thread, &info);
_eventService->publish(&event);
} else {
BOOST_LOG_TRIVIAL(error) << "File not found: " << search->second.file_id;
return 1;
}
break;
}
default:
break;
}
} else {
BOOST_LOG_TRIVIAL(error) << "Function not found: " << call.function_id;
return IN_NO_ENTRY;
}
return IN_OK;
}
int DBInterpreter::processAccessGeneric(ACC_ID accessId,
const access_t& access,
const instruction_t& instruction,
const segment_t& segment,
const call_t& call,
processAccess_t func) {
std::string refId = std::string(access.reference_id);
auto search = referenceT_.find(_refNoIdMap[refId]);
if ( search != referenceT_.end() ) {
(this->* func)(accessId, access, instruction, segment,
call, search->second);
} else {
BOOST_LOG_TRIVIAL(error) << "Reference not found: " << access.reference_id;
return IN_NO_ENTRY;
}
return IN_OK;
}
int DBInterpreter::processMemAccess(ACC_ID accessId,
const access_t& access,
const instruction_t& instruction,
const segment_t& segment,
const call_t& call,
const reference_t& reference) {
ShadowVar *var = 0;
auto searchVar = _shadowVarMap.find(reference.id);
if ( searchVar != _shadowVarMap.end() ) {
var = searchVar->second;
} else {
var = new ShadowVar( getVarType(reference.memory_type),
reference.id,
//reference.address,
reference.size,
reference.name);
_shadowVarMap[reference.id] = var;
}
ShadowThread* thread = threadMgr_->getThread(call.thread_id);
AccessInfo info( access_t::getAccessType(access.access_type),
var,
instruction.instruction_id);
AccessEvent event( thread, &info );
_eventService->publish( &event );
return 0;
}
int DBInterpreter::processAcqAccess(ACC_ID accessId,
const access_t& access,
const instruction_t& instruction,
const segment_t& segment,
const call_t& call,
const reference_t& reference) {
ShadowThread* thread = threadMgr_->getThread(call.thread_id);
ShadowLock *lock = lockMgr_->getLock(std::string(reference.reference_id));
AcquireInfo info(lock);
AcquireEvent event( thread, &info );
_eventService->publish( &event );
return 0;
}
int DBInterpreter::processRelAccess(ACC_ID accessId,
const access_t& access,
const instruction_t& instruction,
const segment_t& segment,
const call_t& call,
const reference_t& reference) {
ShadowThread* thread = threadMgr_->getThread(call.thread_id);
ShadowLock *lock = lockMgr_->getLock(std::string(reference.reference_id));
ReleaseInfo info(lock);
ReleaseEvent event( thread, &info );
_eventService->publish( &event );
return 0;
}
int DBInterpreter::processFork(const instruction_t& instruction,
const segment_t& segment,
const call_t& call,
const thread_t& thread) {
ShadowThread *pT = threadMgr_->getThread(call.thread_id);
ShadowThread *cT = threadMgr_->getThread(thread.child_thread_id);
NewThreadInfo info(cT);
NewThreadEvent event( pT, &info );
_eventService->publish( &event );
return 0;
}
int DBInterpreter::processJoin(const instruction_t& instruction,
const segment_t& segment,
const call_t& call,
const thread_t& thread) {
ShadowThread *pT = threadMgr_->getThread(call.thread_id);
ShadowThread *cT = threadMgr_->getThread(thread.child_thread_id);
JoinInfo info(cT);
JoinEvent event( pT, &info );
_eventService->publish( &event );
return 0;
}
ShadowVar::VarType DBInterpreter::getVarType(REF_MTYP memType) {
switch (memType) {
case reference_t::LOCAL:
return ShadowVar::STACK;
case reference_t::HEAP:
return ShadowVar::HEAP;
case reference_t::GLOBAL:
return ShadowVar::GLOBAL;
case reference_t::STATIC:
return ShadowVar::STATIC;
default:
BOOST_LOG_TRIVIAL(error) << "No valid memory type: " << memType;
return ShadowVar::ERROR;
}
}
int DBInterpreter::fillStructures(sqlite3 **db) {
int rc;
if ((rc = fillGeneric("SELECT * from ACCESS_TABLE;",
db, &DBInterpreter::fillAccess)) != 0) return rc;
if ((rc = fillGeneric("SELECT * from CALL_TABLE;",
db, &DBInterpreter::fillCall)) != 0) return rc;
if ((rc = fillGeneric("SELECT * from FILE_TABLE;",
db, &DBInterpreter::fillFile)) != 0) return rc;
if ((rc = fillGeneric("SELECT * from FUNCTION_TABLE;",
db, &DBInterpreter::fillFunction)) != 0) return rc;
if ((rc = fillGeneric("SELECT * from INSTRUCTION_TABLE;",
db, &DBInterpreter::fillInstruction)) != 0) return rc;
if ((rc = fillGeneric("SELECT * from REFERENCE_TABLE;",
db, &DBInterpreter::fillReference)) != 0) return rc;
if ((rc = fillGeneric("SELECT * from SEGMENT_TABLE;",
db, &DBInterpreter::fillSegment)) != 0) return rc;
if ((rc = fillGeneric("SELECT * from THREAD_TABLE;",
db, &DBInterpreter::fillThread)) != 0) return rc;
BOOST_LOG_TRIVIAL(trace) << "Rows in ACCESS_TABLE: " << accessT_.size();
BOOST_LOG_TRIVIAL(trace) << "Rows in CALL_TABLE: " << callT_.size();
BOOST_LOG_TRIVIAL(trace) << "Rows in FILE_TABLE: " << fileT_.size();
BOOST_LOG_TRIVIAL(trace) << "Rows in FUNCTION_TABLE: " << functionT_.size();
BOOST_LOG_TRIVIAL(trace) << "Rows in INSTRUCTION_TABLE: " << instructionT_.size();
BOOST_LOG_TRIVIAL(trace) << "Rows in REFERENCE_TABLE: " << referenceT_.size();
BOOST_LOG_TRIVIAL(trace) << "Rows in SEGMENT_TABLE: " << segmentT_.size();
BOOST_LOG_TRIVIAL(trace) << "Rows in THREAD_TABLE: " << threadT_.size();
return 0;
}
int DBInterpreter::fillGeneric(const char *sql, sqlite3 **db, fillFunc_t func) {
sqlite3_stmt *sqlstmt = 0;
/* Execute SQL statement */
if (sqlite3_prepare_v2(*db, sql, strlen(sql), &sqlstmt, NULL) != SQLITE_OK) {
BOOST_LOG_TRIVIAL(error) << "Error preparing db: " << sqlite3_errmsg(*db);
return 1;
}
bool reading = true;
while (reading) {
switch(sqlite3_step(sqlstmt)) {
case SQLITE_ROW:
(this->* func)(sqlstmt);
break;
case SQLITE_DONE:
reading = false;
break;
default:
BOOST_LOG_TRIVIAL(trace) << "Iterating db failed!";
return 2;
}
}
return 0;
}
int DBInterpreter::fillAccess(sqlite3_stmt *sqlstmt) {
ACC_ID id = sqlite3_column_int(sqlstmt, 0);
INS_ID instruction_id = sqlite3_column_int(sqlstmt, 1);
int position = sqlite3_column_int(sqlstmt, 2);
const unsigned char *reference_id = sqlite3_column_text(sqlstmt, 3);
const unsigned char *access_type = sqlite3_column_text(sqlstmt, 4);
const unsigned char *memory_state = sqlite3_column_text(sqlstmt, 5);
access_t *tmp = new access_t(instruction_id,
position,
reference_id,
access_type,
memory_state);
accessT_.fill(id, *tmp);
_insAccessMap[instruction_id].push_back(id); // create 1:n associations
return 0;
}
int DBInterpreter::fillCall(sqlite3_stmt *sqlstmt) {
const unsigned char *id = sqlite3_column_text(sqlstmt, 0);
int process_id = sqlite3_column_int(sqlstmt, 1);
int thread_id = sqlite3_column_int(sqlstmt, 2);
int function_id = sqlite3_column_int(sqlstmt, 3);
int instruction_id = sqlite3_column_int(sqlstmt, 4);
const unsigned char *start_time = sqlite3_column_text(sqlstmt, 5);
const unsigned char *end_time = sqlite3_column_text(sqlstmt, 6);
call_t *tmp = new call_t(process_id,
thread_id,
function_id,
instruction_id,
start_time,
end_time);
callT_.fill(std::string((const char*)id), *tmp);
return 0;
}
int DBInterpreter::fillFile(sqlite3_stmt *sqlstmt) {
int id = sqlite3_column_int(sqlstmt, 0);
const unsigned char *file_name = sqlite3_column_text(sqlstmt, 1);
const unsigned char *file_path = sqlite3_column_text(sqlstmt, 2);
file_t *tmp = new file_t(file_name,
file_path);
fileT_.fill(id, *tmp);
return 0;
}
int DBInterpreter::fillFunction(sqlite3_stmt *sqlstmt) {
int id = sqlite3_column_int(sqlstmt, 0);
const unsigned char *signature = sqlite3_column_text(sqlstmt, 1);
const unsigned char *type = sqlite3_column_text(sqlstmt, 2);
int file_id = sqlite3_column_int(sqlstmt, 3);
function_t *tmp = new function_t(signature,
type,
file_id);
functionT_.fill(id, *tmp);
return 0;
}
int DBInterpreter::fillInstruction(sqlite3_stmt *sqlstmt) {
int id = sqlite3_column_int(sqlstmt, 0);
int segment_id = sqlite3_column_int(sqlstmt, 1);
const unsigned char *instruction_type = sqlite3_column_text(sqlstmt, 2);
int line_number = sqlite3_column_int(sqlstmt, 3);
instruction_t *tmp = new instruction_t(id,
segment_id,
instruction_type,
line_number);
instructionT_.fill(id, *tmp);
return 0;
}
int DBInterpreter::fillReference(sqlite3_stmt *sqlstmt) {
int id = sqlite3_column_int(sqlstmt, 0);
const unsigned char *reference_id = sqlite3_column_text(sqlstmt, 1);
//int address = sqlite3_column_int(sqlstmt, 2);
int size = sqlite3_column_int(sqlstmt, 2);
const unsigned char *memory_type = sqlite3_column_text(sqlstmt, 3);
const unsigned char *name = sqlite3_column_text(sqlstmt, 4);
int allocinstr = sqlite3_column_int(sqlstmt, 5);
reference_t *tmp = new reference_t(reference_id,
id,
//address,
size,
memory_type,
name,
allocinstr);
referenceT_.fill(id, *tmp);
REF_NO no = REF_NO((const char*)reference_id);
_refNoIdMap[no] = id; // create association between no and id
return 0;
}
int DBInterpreter::fillSegment(sqlite3_stmt *sqlstmt) {
int id = sqlite3_column_int(sqlstmt, 0);
const unsigned char *call_id = sqlite3_column_text(sqlstmt, 1);
int segment_no = sqlite3_column_int(sqlstmt, 2);
const unsigned char *segment_type = sqlite3_column_text(sqlstmt, 3);
int loop_pointer = sqlite3_column_int(sqlstmt, 4);
segment_t *tmp = new segment_t(call_id,
segment_no,
segment_type,
loop_pointer);
segmentT_.fill(id, *tmp);
return 0;
}
int DBInterpreter::fillThread(sqlite3_stmt *sqlstmt) {
int id = sqlite3_column_int(sqlstmt, 0);
int instruction_id = sqlite3_column_int(sqlstmt, 1);
int parent_thread_id = sqlite3_column_int(sqlstmt, 2);
int child_thread_id = sqlite3_column_int(sqlstmt, 3);
thread_t *tmp = new thread_t(id,
instruction_id,
parent_thread_id,
child_thread_id);
threadT_.fill(instruction_id, *tmp);
return 0;
}