-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSBFLLocalizer.cpp
More file actions
193 lines (166 loc) · 8.22 KB
/
SBFLLocalizer.cpp
File metadata and controls
193 lines (166 loc) · 8.22 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
#include <clang/AST/AST.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/AST/Stmt.h>
#include <iostream>
#include <cjson/cJSON.h>
#include "SBFLLocalizer.h"
#include "Utils.h"
using namespace clang;
class ProfileLocationParser: public RecursiveASTVisitor<ProfileLocationParser> {
std::map<unsigned int,double> &sbflScores;
std::vector<std::pair<SourcePositionTy,double>> result;
const ASTContext *ctxt;
std::string file;
public:
ProfileLocationParser(std::map<unsigned int,double> &scores,const ASTContext *ctxt,std::string fileName): sbflScores(scores),result(),ctxt(ctxt),file(fileName) {}
std::vector<std::pair<SourcePositionTy,double>> getResult() {
return result;
}
bool VisitCompoundStmt(CompoundStmt *stmt){
const SourceManager &manager=ctxt->getSourceManager();
for (CompoundStmt::const_body_iterator it=stmt->body_begin();it!=stmt->body_end();it++){
unsigned int line=manager.getExpansionLineNumber((*it)->getBeginLoc());
if (sbflScores.count(line)!=0){
SourcePositionTy pos;
pos.expFilename=manager.getFilename(manager.getExpansionLoc((*it)->getBeginLoc())).str();
pos.expLine=manager.getExpansionLineNumber((*it)->getBeginLoc());
pos.expColumn=manager.getExpansionColumnNumber((*it)->getBeginLoc());
pos.spellFilename=manager.getFilename(manager.getSpellingLoc((*it)->getBeginLoc())).str();
pos.spellLine=manager.getSpellingLineNumber((*it)->getBeginLoc());
pos.spellColumn=manager.getSpellingColumnNumber((*it)->getBeginLoc());
result.push_back(std::make_pair(pos,sbflScores[line]));
}
}
return true;
}
bool VisitIfStmt(IfStmt *stmt){
if (stmt == NULL) return true;
const SourceManager &manager=ctxt->getSourceManager();
Stmt* thenS = stmt->getThen();
Stmt* elseS = stmt->getElse();
if (thenS!=nullptr && !CompoundStmt::classof(thenS)){
unsigned int line=manager.getExpansionLineNumber(thenS->getBeginLoc());
if (sbflScores.count(line)!=0){
SourcePositionTy pos;
pos.expFilename=manager.getFilename(manager.getExpansionLoc(thenS->getBeginLoc())).str();
pos.expLine=manager.getExpansionLineNumber(thenS->getBeginLoc());
pos.expColumn=manager.getExpansionColumnNumber(thenS->getBeginLoc());
pos.spellFilename=manager.getFilename(manager.getSpellingLoc(thenS->getBeginLoc())).str();
pos.spellLine=manager.getSpellingLineNumber(thenS->getBeginLoc());
pos.spellColumn=manager.getSpellingColumnNumber(thenS->getBeginLoc());
result.push_back(std::make_pair(pos,sbflScores[line]));
}
}
if (elseS!=nullptr && !CompoundStmt::classof(elseS)){
unsigned int line=manager.getExpansionLineNumber(elseS->getBeginLoc());
if (sbflScores.count(line)!=0){
SourcePositionTy pos;
pos.expFilename=manager.getFilename(manager.getExpansionLoc(elseS->getBeginLoc())).str();
pos.expLine=manager.getExpansionLineNumber(elseS->getBeginLoc());
pos.expColumn=manager.getExpansionColumnNumber(elseS->getBeginLoc());
pos.spellFilename=manager.getFilename(manager.getSpellingLoc(elseS->getBeginLoc())).str();
pos.spellLine=manager.getSpellingLineNumber(elseS->getBeginLoc());
pos.spellColumn=manager.getSpellingColumnNumber(elseS->getBeginLoc());
result.push_back(std::make_pair(pos,sbflScores[line]));
}
}
return true;
}
};
bool skipFile(std::string &fileName){
return fileName=="crypto/des/ncbc_enc.c" || fileName.find(".inc")!=std::string::npos || fileName.find(".y") != std::string::npos || fileName.find(".l") != std::string::npos;
}
SBFLLocalizer::SBFLLocalizer(std::string fileName,BenchProgram *program): SBFLFile(fileName),program(program),result() {
// Get sbfl result
std::ifstream file(SBFLFile);
std::string rootString="";
std::string line;
while (std::getline(file,line)){
rootString+=line+"\n";
}
file.close();
std::map<std::string,std::map<unsigned int,double>> sbflResult;
sbflResult.clear();
cJSON *root=cJSON_Parse(rootString.c_str());
cJSON *location=NULL;
cJSON_ArrayForEach(location,root){
cJSON *file_elem=cJSON_GetArrayItem(location,0);
std::string file_name(cJSON_GetStringValue(file_elem));
unsigned int line=(unsigned int)cJSON_GetNumberValue(cJSON_GetArrayItem(location,1));
double score=cJSON_GetNumberValue(cJSON_GetArrayItem(location,2));
if (score!=0.0){
if (sbflResult.count(file_name)==0)
sbflResult[file_name]=std::map<unsigned int,double>();
sbflResult[file_name][line]=score;
}
}
// Get Profile location from sbfl
// We use <score,location> for sorting descending order(high score = high priority)
std::vector<std::pair<double,SourcePositionTy>> sortedResult;
sortedResult.clear();
for (std::map<std::string,std::map<unsigned int,double>>::iterator it=sbflResult.begin();it!=sbflResult.end();it++){
std::string fileName=it->first;
if (fileName.find("//")!=std::string::npos){
size_t loc=fileName.find("//");
fileName=fileName.substr(loc+2);
}
if (is_header(fileName)) continue; // Skip header files
if (skipFile(fileName)) continue; // Skip invalid files
std::string fullPath=program->getSrcdir()+"/"+fileName;
std::string code;
readCodeToString(fullPath,code);
std::unique_ptr<ASTUnit> unit=program->buildClangASTUnit(fileName,code);
ASTContext &ctxt=unit->getASTContext();
ProfileLocationParser parser(it->second,&ctxt,fileName);
TranslationUnitDecl *decl=ctxt.getTranslationUnitDecl();
bool res=parser.TraverseDecl(decl);
std::vector<std::pair<SourcePositionTy,double>> result=parser.getResult();
for (size_t i=0;i<result.size();i++){
sortedResult.push_back(std::make_pair(result[i].second,result[i].first));
}
}
// Sort FL results
std::sort(sortedResult.begin(),sortedResult.end(),std::greater<std::pair<double,SourcePositionTy>>());
result=sortedResult;
printResult(program->getLocalizationResultFilename(),program->getLocalizationResultBackupFilename());
}
std::vector<SourcePositionTy> SBFLLocalizer::getCandidateLocations(){
std::vector<SourcePositionTy> ret;
for (size_t i=0;i<result.size();i++){
ret.push_back(result[i].second);
}
return ret;
}
std::vector<ProfileErrorLocalizer::ResRecordTy> SBFLLocalizer::getCandidates(){
std::vector<ProfileErrorLocalizer::ResRecordTy> ret;
for (size_t i=0;i<result.size();i++){
ProfileErrorLocalizer::ResRecordTy record;
record.primeScore=(long long)(result[i].first*1000000);
record.secondScore=5;
record.loc=result[i].second;
record.pid="0";
ret.push_back(record);
}
return ret;
}
void SBFLLocalizer::printResult(const std::string &outfile,const std::string backupFile){
std::string cmd;
cmd="cp -rf "+outfile+" "+backupFile;
system(cmd.c_str());
std::ofstream file(outfile);
for (size_t i=0;i<result.size();i++){
file<<result[i].second.expFilename<<" "<<result[i].second.expLine<<" "<<result[i].second.expColumn<<" "
<<result[i].second.spellFilename <<" "<< result[i].second.spellLine <<" "<< result[i].second.spellColumn<<"\t\t"
<<(size_t)(result[i].first*1000000) << "\t\t" << 0 << "\t\t" << 0 << std::endl;
}
file.close();
}
void SBFLLocalizer::printResult(const std::string &outfile){
std::ofstream file(outfile);
for (size_t i=0;i<result.size();i++){
file<<result[i].second.expFilename<<" "<<result[i].second.expLine<<" "<<result[i].second.expColumn<<" "
<<result[i].second.spellFilename <<" "<< result[i].second.spellLine <<" "<< result[i].second.spellColumn<<"\t\t"
<<(size_t)(result[i].first*1000000) << "\t\t" << 0 << "\t\t" << 0 << std::endl;
}
file.close();
}