-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeatureManager.cpp
105 lines (90 loc) · 2.87 KB
/
FeatureManager.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
#include "FeatureManager.h"
#include "CFG.h"
#include "CodeObject.h"
#include "CodeSource.h"
struct FuncSort {
bool operator() (ParseAPI::Function *a, ParseAPI::Function *b) {
return a->num_blocks() > b->num_blocks();
}
};
void FeatureManager::parse() {
SymtabCodeSource *sts = new SymtabCodeSource(symObj);
co = new CodeObject(sts);
co->parse();
}
void FeatureManager::sortFuncs() {
for (auto f: co->funcs()) {
if (InTextSection(f))
fvec.push_back(f);
}
sort(fvec.begin(), fvec.end(), FuncSort());
}
int FeatureManager::setup(const char *bPath, int fSize, const char *oPre) {
featSize = fSize;
outPrefix = string(oPre);
symObj = NULL;
if (!SymtabAPI::Symtab::openFile(symObj, bPath)) {
fprintf(stderr, "Cannot open file %s\n", bPath);
return -1;
}
featFile = fopen(string(outPrefix + ".instances").c_str(), "w");
consumer = new std::thread(&FeatureManager::Consume, this);
return 0;
}
void FeatureManager::printFeatureList() {
string listFile = outPrefix + ".featlist";
FILE *f = fopen(listFile.c_str(), "w");
vector< pair<int, string> > featList;
for (auto fit = featureIndex.begin(); fit != featureIndex.end(); ++fit)
featList.push_back(make_pair(fit->second, fit->first));
sort(featList.begin(), featList.end());
for (auto fit = featList.begin(); fit != featList.end(); ++fit)
fprintf(f, "%d %s\n", fit->first, fit->second.c_str());
fclose(f);
}
int FeatureManager::GetFeatureIndex(const std::string &feat) {
auto it = featureIndex.find(feat);
if (it == featureIndex.end()) {
int newIndex = featureIndex.size() + 1;
featureIndex[feat] = newIndex;
return newIndex;
} else {
return it->second;
}
}
void FeatureManager::finish() {
q.finish();
consumer->join();
delete consumer;
fclose(featFile);
}
void FeatureManager::Consume() {
while (true) {
InstanceDataType* it = (InstanceDataType*) q.dequeue();
if (it == NULL) break;
fprintf(featFile, "%lx", it->f->addr());
for (auto pair : it->featPair) {
int index = GetFeatureIndex(pair.first);
fprintf(featFile, " %d:%.3lf", index, pair.second);
}
fprintf(featFile, "\n");
delete it;
}
}
void FeatureManager::runPass(int pass) {
FeatureAnalyzer *p = passes[pass];
#pragma omp parallel for schedule(dynamic)
for (size_t i = 0; i < fvec.size(); ++i) {
InstanceDataType* idt = new InstanceDataType();
idt->f = fvec[i];
p->ProduceAFunction(idt);
q.enqueue((void*)idt);
}
}
bool FeatureManager::InTextSection(Dyninst::ParseAPI::Function *f) {
if (co->cs()->linkage().find(f->addr()) == co->cs()->linkage().end()) return true;
return false;
}
void FeatureManager::addPass(FeatureAnalyzer* fa) {
passes.push_back(fa);
}