-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCompilationMutators.cpp
More file actions
370 lines (307 loc) · 12.6 KB
/
CompilationMutators.cpp
File metadata and controls
370 lines (307 loc) · 12.6 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
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <limits.h>
#include <cstring>
#include <unistd.h>
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <filesystem>
#include <random>
#include <algorithm>
using namespace std;
struct Config
{
string flags_list_path;
string used_flags_path;
string src_code_file;
string output_path;
};
string getEnvVar( string const & key )
{
char * val = getenv( key.c_str() );
return val == NULL ? string("") : string(val);
}
vector<string> splitStringByComma(const string& str) {
vector<string> result;
stringstream iss(str);
string token;
while (getline(iss, token, ',')) {
result.push_back(token);
}
return result;
}
// Mutate bytes representing each compilation optimization flag
void bytes_mutation(uint8_t *bytes_buf, size_t bytes_size) {
for (size_t i=1; i<bytes_size; i++) {
random_device rd_bytes;
mt19937 gen(rd_bytes());
uniform_int_distribution<> dis(0, 6);
int randNum = dis(gen);
switch (randNum) {
case 0:{ // flip bit
random_device rd_bitflip;
mt19937 gen_bitflip(rd_bitflip());
uniform_int_distribution<> dis(0, 1);
uint8_t bit = dis(gen_bitflip);
bytes_buf[i] ^= 1 << bit;
break;
}
case 1:{ // flip byte
random_device rd_byteflip;
mt19937 gen_byteflip(rd_byteflip());
uniform_int_distribution<> distr(0, 255);
uint8_t byte = distr(gen_byteflip);
bytes_buf[i] ^= byte;
break;
}
case 2:{ // add 1
bytes_buf[i] += 1;
break;
}
case 3:{ // sub 1
if (bytes_buf[i] != 0){
bytes_buf[i] -= 1;
}
break;
}
case 4:{ // set to 0
bytes_buf[i] += 0;
break;
}
case 5:{ // set to 1
bytes_buf[i] = 1;
break;
}
default:{
break;
}
}
}
}
bool endsWith(const string& str, char c) {
if (!str.empty()) {
return str[str.length() - 1] == c;
}
return false;
}
string getLeftPart(const string& str) {
size_t pos = str.find_last_of('=');
if (pos != string::npos) {
return str.substr(0, pos);
}
return str;
}
string flags_mutation(uint8_t *buf, size_t buf_size, string path, string compiler){
vector<string> optimFlags;
string comp_flags("");
fstream file;
file.open(path, ios::in);
// read all the flags from FLAGS_LIST_PATH
if(file.is_open()){
string line;
while(getline(file, line)){
optimFlags.push_back(line);
}
}
else{
cout << "Error opening flags list text file" << endl;
exit(1);
}
bytes_mutation(buf, optimFlags.size());
for (unsigned int i = 2; i < buf_size; i++) {
unsigned int intVal = buf[i];
if (i < optimFlags.size()) {
string flag(optimFlags[i]);
// only select a flag when the corresponding byte is odd
if (comp_flags.find(flag) == string::npos && (intVal % 2 == 1)) {
if (compiler.find("MSVC") != std::string::npos || compiler.find("msvc") != std::string::npos) {
if (flag.find("=") != std::string::npos){
string left_part = getLeftPart(flag);
if (comp_flags.find(left_part) == string::npos){
if (endsWith(flag, '=-')) {
flag.erase(remove(flag.begin(), flag.end(), '='), flag.end());
} else if (endsWith(flag, '=+')) {
flag.erase(remove(flag.begin(), flag.end(), '='), flag.end());
flag.erase(remove(flag.begin(), flag.end(), '+'), flag.end());
} else if (endsWith(flag, '=')) {
flag.erase(remove(flag.begin(), flag.end(), '='), flag.end());
flag += to_string( ((intVal - 1) / 2) );
} else {
flag.erase(remove(flag.begin(), flag.end(), '='), flag.end());
}
comp_flags += flag;
comp_flags += " ";
}
} else{
comp_flags += flag;
comp_flags += " ";
}
}
else {
if (flag.find("=") != std::string::npos){
string left_part = getLeftPart(flag);
if (comp_flags.find(left_part) == string::npos){
if (endsWith(flag, '=')) {
flag += to_string( ((intVal - 1) / 2) );
}
if ((compiler == "clang" or compiler == "o64-clang" or compiler == "icx") && flag != "") {
flag = "-mllvm=" + flag;
}
comp_flags += flag;
comp_flags += " ";
}
} else{
if ((compiler == "clang" or compiler == "o64-clang" or compiler == "icx") && flag != "") {
flag = "-mllvm=" + flag;
}
comp_flags += flag;
comp_flags += " ";
}
}
}
}
}
return comp_flags;
}
extern "C"{
void compilation_mutation(uint8_t *buf, size_t buf_size, uint8_t *pointer){
Config* config = new Config;
config->used_flags_path = getEnvVar("USED_FLAGS_PATH");
config->src_code_file = getEnvVar("ORIGINAL_CODE_PATH");
config->output_path = getEnvVar("ORIGINAL_EXEC_PATH");
string faulty_flags_path = getEnvVar("FAULTY_FLAGS_PATH");
if (filesystem::exists(config->output_path)) {
filesystem::remove(config->output_path);
}
if (filesystem::exists(config->used_flags_path)) {
filesystem::remove(config->used_flags_path);
}
string command("");
string comp_env = getEnvVar("COMPILER");
string compiler_chosen;
vector<string> compilers = splitStringByComma(comp_env);
random_device rd_mutate;
mt19937 gen_mutate(rd_mutate());
uniform_int_distribution<> dis(0, compilers.size()-1);
int c_byte = dis(gen_mutate);
compiler_chosen = compilers[c_byte];
buf[0] = c_byte;
string flags_chosen = "";
string msvc_inc = getEnvVar("MSVCINC");
string msvc_lib = getEnvVar("MSVCLIB");
string win2lin = getEnvVar("WIN2LIN");
string tmp_folder = getEnvVar("TMP_FOLDER");
string orig_filename = ((filesystem::path)config->src_code_file).filename().string();
int compile_status = 0;
string exec_postfix = "_exec";
string defaultOutputName = config->output_path;
if(compiler_chosen == "msvc"){
compiler_chosen = getEnvVar("MSVC");
config->flags_list_path = getEnvVar("MSVC_FLAGS");
// get optimization flags based on mapped bytes and compiler chosen
flags_chosen = flags_mutation(buf, buf_size, config->flags_list_path, compiler_chosen);
char cwd[1024];
getcwd(cwd, sizeof(cwd));
chdir(tmp_folder.c_str());
command = "(timeout 30s " + win2lin + " " + compiler_chosen + " " + flags_chosen + " " + orig_filename + " " + msvc_inc + " " + msvc_lib + ") > /dev/null 2>&1";
compile_status = system(command.c_str());
chdir(cwd);
config->output_path = config->output_path.replace(config->output_path.find(exec_postfix), exec_postfix.length(), ".exe");
}
else{
if(compiler_chosen == "clang"){
config->flags_list_path = getEnvVar("CLANG_FLAGS");
}
else if(compiler_chosen == "gcc"){
config->flags_list_path = getEnvVar("GCC_FLAGS");
}
else if(compiler_chosen == "tcc"){
config->flags_list_path = getEnvVar("TCC_FLAGS");
}
else if(compiler_chosen == "icx"){
config->flags_list_path = getEnvVar("ICX_FLAGS");
}
else if(compiler_chosen == "o64-clang"){
config->flags_list_path = getEnvVar("CLANG_FLAGS");
}
// get optimization flags based on mapped bytes and compiler chosen
flags_chosen = flags_mutation(buf, buf_size, config->flags_list_path, compiler_chosen);
command = "(timeout 15s " + compiler_chosen + " " + flags_chosen + config->src_code_file + " -o " + config->output_path + " ) > /dev/null 2>&1";
compile_status = system(command.c_str());
}
// if the executable does not exist after compilation command, mark compilation failed
if (!filesystem::exists(config->output_path)) {
compile_status = 256;
string faulty_flags = compiler_chosen + " " + flags_chosen;
// save used flags into a file
ofstream ffFile(faulty_flags_path);
if (ffFile.is_open()) {
ffFile << faulty_flags;
ffFile.close();
} else {
}
}
if (compile_status != 0) {
// if compilation failed, compile with default command
string defaultCommand = "";
if(compiler_chosen == getEnvVar("MSVC")){
random_device rd;
mt19937 eng(rd());
uniform_int_distribution<> distr(1, 2);
int randomNumber = distr(eng);
char cwd[1024];
getcwd(cwd, sizeof(cwd));
chdir(tmp_folder.c_str());
flags_chosen = "/O" + to_string(randomNumber);
string newCommand = "(timeout 30s " + win2lin + " " + compiler_chosen + " " + flags_chosen + " " + orig_filename + " " + msvc_inc + " " + msvc_lib + ") > /dev/null 2>&1";
system(newCommand.c_str());
chdir(cwd);
buf[1] = randomNumber;
if (!filesystem::exists(config->output_path)) {
char cwd[1024];
getcwd(cwd, sizeof(cwd));
chdir(tmp_folder.c_str());
defaultCommand = "(timeout 30s " + win2lin + " " + compiler_chosen + " /Od " + orig_filename + " " + msvc_inc + " " + msvc_lib + ") > /dev/null 2>&1";
flags_chosen = "/Od";
system(defaultCommand.c_str());
chdir(cwd);
buf[1] = 0;
}
}
else{
random_device rd;
mt19937 eng(rd());
uniform_int_distribution<> distr(1, 3);
int randomNumber = distr(eng);
flags_chosen = "-O" + to_string(randomNumber);
string new_command = "(timeout 15s " + compiler_chosen + " " + flags_chosen + " " + config->src_code_file+" -o "+config->output_path + ") > /dev/null 2>&1";
system(new_command.c_str());
buf[1] = randomNumber;
if (!filesystem::exists(config->output_path)) {
defaultCommand = "(timeout 15s " + compiler_chosen + " -O0 " + config->src_code_file+" -o "+config->output_path + ") > /dev/null 2>&1";
flags_chosen = "-O0";
system(defaultCommand.c_str());
buf[1] = 0;
}
}
for (size_t i=2; i< buf_size; i++){
buf[i] = 0;
}
}
if(compiler_chosen == getEnvVar("MSVC")){
rename(config->output_path.c_str(), defaultOutputName.c_str());
}
string used_flags = compiler_chosen + " " + flags_chosen;
// save used flags into a file
ofstream outputFile(config->used_flags_path);
if (outputFile.is_open()) {
outputFile << used_flags;
outputFile.close();
} else {
}
memcpy(pointer, buf, buf_size);
}
}