-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstraintResolver.cpp
181 lines (142 loc) · 6.54 KB
/
ConstraintResolver.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
//=--ConstraintResolver.cpp---------------------------------------*- C++-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// Implementation of methods in ConstraintResolver.h that help in fetching
// constraints for a given expression.
//===----------------------------------------------------------------------===//
#include "clang/CheckMate/ConstraintResolver.h"
#include "clang/CheckMate/CheckMateGlobalOptions.h"
#include <map>
using namespace llvm;
using namespace clang;
ConstraintResolver::~ConstraintResolver() {}
// Force all ConstraintVariables in this set to be WILD
std::string base_name(std::string const & path)
{
return path.substr(path.find_last_of("/\\") + 1);
}
inline std::string resolve_base_name(std::string const & path)
{
return base_name(path);
}
void ConstraintResolver::storeTaintMirroredVarDecl(VarDecl* VD) {
std::string TaintedDirPath = _CheckMateOpts.TaintedDefDir;
if(TaintedDirPath.find_last_not_of("/\\"))
TaintedDirPath += "/";
auto VdSourceFileName = resolve_base_name(VD->getASTContext().getSourceManager()
.getFilename(VD->getLocation()).str());
std::string FinalPathWithFileName = TaintedDirPath + "Tainted"
+ VdSourceFileName;
Info.TaintMirroredVarDecls[VD] = FinalPathWithFileName;
}
void ConstraintResolver::storeTaintMirroredStructVarDecl(RecordDecl* RD) {
std::string TaintedDirPath = _CheckMateOpts.TaintedDefDir;
if(TaintedDirPath.find_last_not_of("/\\"))
TaintedDirPath += "/";
auto VdSourceFileName = resolve_base_name(RD->getASTContext().getSourceManager()
.getFilename(RD->getLocation()).str());
std::string FinalPathWithFileName = TaintedDirPath + "Tainted"
+ VdSourceFileName;
Info.TaintMirroredVarStructDecls[RD] = FinalPathWithFileName;
}
void ConstraintResolver::storeTaintMirroredTypedefDecl(TypedefDecl* TD) {
std::string TaintedDirPath = _CheckMateOpts.TaintedDefDir;
if(TaintedDirPath.find_last_not_of("/\\"))
TaintedDirPath += "/";
auto VdSourceFileName = resolve_base_name(TD->getASTContext().getSourceManager()
.getFilename(TD->getLocation()).str());
std::string FinalPathWithFileName = TaintedDirPath + "Tainted"
+ VdSourceFileName;
Info.TaintMirroredTypedefDecls[TD] = FinalPathWithFileName;
}
void ConstraintResolver::storeTaintMirroredEnumDecl
(EnumDecl* ED) {
std::string TaintedDirPath = _CheckMateOpts.TaintedDefDir;
if(TaintedDirPath.find_last_not_of("/\\"))
TaintedDirPath += "/";
auto VdSourceFileName = resolve_base_name(ED->getASTContext().getSourceManager()
.getFilename(ED->getLocation()).str());
std::string FinalPathWithFileName = TaintedDirPath + "Tainted"
+ VdSourceFileName;
Info.TaintMirroredEnumDecls[ED] = FinalPathWithFileName;
}
void ConstraintResolver::storeIncludeStatement(SourceLocation SLC,
SourceManager &SM,
std::string IncludeStmt) {
std::string TaintedDirPath = _CheckMateOpts.TaintedDefDir;
if(TaintedDirPath.find_last_not_of("/\\"))
TaintedDirPath += "/";
auto SourceFileName = resolve_base_name(SM.getFilename(SLC).str());
std::string FinalPathWithFileName = TaintedDirPath + "Tainted"
+ SourceFileName;
/*
* Insert only if IncludeStmt has never been inserted before
*/
errs()<<"Inserting macro: " << IncludeStmt<<"\n";
if (Info.MapTaintedFile2IncludeStmt.find(IncludeStmt)
== Info.MapTaintedFile2IncludeStmt.end()) {
Info.MapTaintedFile2IncludeStmt.insert(std::pair<std::string, std::string>(
IncludeStmt, FinalPathWithFileName));
}
}
void ConstraintResolver::storeTaintedFunctionDecl(FunctionDecl* FD){
Info.storeTaintedDecl(FD);
/*
* Check if the map has an entry(filename) corresponding to this FD's source file
*/
std::string FdSourceFileName;
/*
* This FD belongs to a new file, hence a new file has to be created for
* this tainted decl
*/
//fetch the tainted directory path -->
std::string TaintedDirPath = _CheckMateOpts.TaintedDefDir;
if(TaintedDirPath.find_last_not_of("/\\"))
TaintedDirPath += "/";
FdSourceFileName = resolve_base_name(FD->getASTContext().getSourceManager()
.getFilename(FD->getLocation()).str());
std::string FinalPathWithFileName = TaintedDirPath + "Tainted"
+ FdSourceFileName;
if(std::find(Info.TaintedRewriteFileVector.begin(),
Info.TaintedRewriteFileVector.end(),
FinalPathWithFileName.c_str())
== Info.TaintedRewriteFileVector.end()){
Info.TaintedRewriteFileVector.push_back(FinalPathWithFileName.c_str());
}
Info.TaintedFuncStreamWriter.insert({FD, FinalPathWithFileName});
}
void ConstraintResolver::storeCallbackFunctionDecl(FunctionDecl* FD){
/*
* Check if the map has an entry(filename) corresponding to this FD's source file
*/
std::string FdSourceFileName;
/*
* This FD belongs to a new file, hence a new file has to be created for
* this tainted decl
*/
//fetch the tainted directory path -->
std::string TaintedDirPath = _CheckMateOpts.TaintedDefDir;
if(TaintedDirPath.find_last_not_of("/\\"))
TaintedDirPath += "/";
FdSourceFileName = resolve_base_name(FD->getASTContext().getSourceManager()
.getFilename(FD->getLocation()).str());
std::string FinalPathWithFileName = TaintedDirPath + "Tainted"
+ FdSourceFileName;
Info.CallbackFuncStreamWriter.insert({FD, FinalPathWithFileName});
}
static bool getSizeOfArg(Expr *Arg, QualType &ArgTy) {
Arg = Arg->IgnoreParenImpCasts();
if (auto *SizeOf = dyn_cast<UnaryExprOrTypeTraitExpr>(Arg))
if (SizeOf->getKind() == UETT_SizeOf) {
ArgTy = SizeOf->getTypeOfArgument();
return true;
}
return false;
}
// Returns a pair of set of ConstraintVariables and set of BoundsKey
// after evaluating the expression E. Will explore E recursively, but will
// ignore parts of it that do not contribute to the final result.