-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInstrumentationFilter.cpp
More file actions
283 lines (240 loc) · 7.08 KB
/
InstrumentationFilter.cpp
File metadata and controls
283 lines (240 loc) · 7.08 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
#include "InstrumentationFilter.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
using namespace json11;
using namespace std;
//#define INSFILT_DEBUG
void InstrumentationFilter::loadFilterDataEnv() {
const char * val = ::getenv("DIN_FILTERS");
if ((val == 0) || (strcmp(val,"") == 0)) {
cerr << "DIN_FILTERS not set, allowing all instrumentation" << endl;
return;
}
loadFilterData(val);
}
void InstrumentationFilter::loadFilterData(const char *filename) {
cerr << "DIN_FILTERS with " << filename << " file " << endl;
ifstream fin;
fin.open(filename);
if (!fin.is_open()) {
cerr << "Error: Couldn't open " << filename <<
", filters not loaded." << endl;
return;
}
stringstream ss;
ss << fin.rdbuf();
string err;
this->filters = Json::parse(ss.str(), err);
if (!this->filters.is_null()) {
loaded = true;
cerr << "DINAMITE filtering set up successfully!" << endl;
} else {
cerr << "Error parsing filters!" << endl;
}
}
string InstrumentationFilter::findBestFunctionMatch(string function_name) {
Json jsFunc = filters["whitelist"]["function_filters"][function_name];
if (!jsFunc.is_null()) {
return function_name;
}
jsFunc = filters["whitelist"]["function_filters"];
bool matched_star = false;
for (auto it : jsFunc.object_items()) {
if (it.first.compare("*") == 0) {
matched_star = true;
} else if (globMatch(it.first, function_name)) {
return it.first;
}
}
if (matched_star) {
return "*";
}
return "";
}
bool InstrumentationFilter::checkFunctionFilter(string function_name,
string event_type) {
if (!loaded)
return true; // No filters defined
if (checkFunctionBlacklist(function_name)) {
#ifdef INSFILT_DEBUG
cerr << "Function filter: Function " << function_name <<
" blacklisted" << endl;
#endif
return false;
}
if (function_name.compare("*") != 0) {
if (checkFunctionFilter("*", event_type)) {
return true;
}
}
if (filters["whitelist"]["function_filters"].object_items().size()
== 0) {
#ifdef INSFILT_DEBUG
cerr << "Function filter: Function " << function_name <<
" enabled, whitelist empty." << endl;
#endif
return true;
}
string match_name = findBestFunctionMatch(function_name);
Json eventarray =
filters["whitelist"]["function_filters"][match_name]["events"];
for (auto it : eventarray.array_items()) {
if (event_type.compare(it.string_value()) == 0) {
#ifdef INSFILT_DEBUG
cerr << "Function filter: Function " << function_name
<< " event: " << event_type << " enabled as "
<< match_name << endl;
#endif
return true;
}
}
return false;
}
bool InstrumentationFilter::checkFunctionArgFilter(string function_name, int arg) {
if (!loaded) return false; // don't print args if we haven't enabled it explicitly
string match_name = findBestFunctionMatch(function_name);
Json argfilter = filters["whitelist"]["function_filters"][match_name]["arguments"];
if (argfilter.is_array()) {
for (auto it : argfilter.array_items()) {
if (it.int_value() == arg) {
return true;
}
}
} else if (argfilter.is_string()) {
if (argfilter.string_value().compare("*") == 0) {
return true;
}
}
return false;
}
bool InstrumentationFilter::checkFileFilter(string file_name) {
if (!loaded)
return true;
if (checkFileBlacklist(file_name))
return false;
Json filearray = filters["whitelist"]["file_filters"];
if (filearray.is_null())
return true;
if (filearray.array_items().size() == 0)
return true;
for (auto it : filearray.array_items()) {
if (globMatch("*" + it.string_value(), file_name)) {
return true;
}
}
return false;
}
bool InstrumentationFilter::checkFunctionBlacklist(string function_name) {
if (!loaded)
return false;
Json functionarray = filters["blacklist"]["function_filters"];
if (functionarray.is_null())
return false;
for (auto it : functionarray.array_items()) {
if (globMatch("*" + it.string_value(), function_name))
return true;
}
return false;
}
bool InstrumentationFilter::checkFileBlacklist(string file_name) {
if (!loaded)
return false;
Json filearray = filters["blacklist"]["file_filters"];
if (filearray.is_null())
return false;
for (auto it : filearray.array_items()) {
if (globMatch("*" + it.string_value(), file_name))
return true;
}
return false;
}
bool InstrumentationFilter::loopCheckEnabled() {
if (filters["check_small_function_loops"].is_null())
return true;
#ifdef INSFILT_DEBUG
cerr << "check_small_function_loops is " <<
filters["check_small_function_loops"].bool_value() << endl;
#endif
return filters["check_small_function_loops"].bool_value();
}
bool InstrumentationFilter::checkFunctionSize(string function_name,
size_t size) {
if ((size > 0) && (functions.count(function_name) == 0)) {
/* Add to the list of encountered functions, mostly
* used for mangled stuff
*/
functions[function_name] = size;
}
if (filters["minimum_function_size"].is_null()) {
return true;
}
if (size >= (size_t) filters["minimum_function_size"].int_value()) {
#ifdef INSFILT_DEBUG
cerr << "Size filter: Function size " << function_name << " "
<< size << ", greater than minimum function size" << endl;
#endif
return true;
}
string bestMatch = findBestFunctionMatch(function_name);
if ((bestMatch.compare("*") != 0) && (bestMatch.compare("") != 0)) {
#ifdef INSFILT_DEBUG
cerr << "Size filter: Function " << function_name << " size: "
<< size << ", matched against " << bestMatch << endl;
#endif
return true;
}
#ifdef INSFILT_DEBUG
cerr << "Size filter: Function " << function_name << " size: " << size
<< " doesn't match whitelist filters based on size." << endl;
#endif
return false;
}
bool InstrumentationFilter::globMatch(string glob, string s) {
/* both done, matched */
if ((glob.length() == 0) && (s.length() == 0))
return true;
/* one is done, other has leftover characters */
if (glob.length() * s.length() == 0) {
if (glob.compare("*") == 0)
return true;
return false;
}
if (glob[0] == '*') { // recurse for both match cases
return
globMatch(glob.substr(1), s.substr(1)) ||
globMatch(glob, s.substr(1)) ||
globMatch(glob.substr(1), s);
}
if (glob[0] != s[0]) { // regular characters
return false;
} else {
return globMatch(glob.substr(1), s.substr(1));
}
}
fn_size_metrics InstrumentationFilter::getFunctionSizeMetric() {
if (!filters["function_size_metric"].is_null()) {
if (filters["function_size_metric"].string_value().compare("IR")
== 0) {
return FN_SIZE_IR;
}
if (filters["function_size_metric"].string_value().compare(
"LOC_PATH") == 0) {
return FN_SIZE_PATH;
}
}
return FN_SIZE_LOC;
}
// some simple glob tests to verify
#define GLOBTEST(x, y) cerr << "TEST " << x << " " << y << ": " << globMatch(x,y) << endl;
void InstrumentationFilter::testGlob() {
GLOBTEST("abc*", "abcd");
GLOBTEST("a*bc", "abcd");
GLOBTEST("*bc*", "abcd");
GLOBTEST("a*f", "abcd");
GLOBTEST("a**d", "abcd");
GLOBTEST("a*c*d", "abcdd");
GLOBTEST("*main*", "main");
exit(-1);
}