|
| 1 | +/* |
| 2 | + This header file contains all the functions that are needed for fuzz testing |
| 3 | +*/ |
| 4 | + |
| 5 | +#ifndef FUZZ_TESTING_H |
| 6 | +#define FUZZ_TESTING_H |
| 7 | +#include <fstream> |
| 8 | +#include <iostream> |
| 9 | +#include <sstream> |
| 10 | +#include <string> |
| 11 | +#include <unordered_map> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +using namespace std; |
| 15 | + |
| 16 | +#define YML ".yml" |
| 17 | +#define TAB " " |
| 18 | +#define TAB_LENGTH_FOR_VAR 4 |
| 19 | + |
| 20 | +#define FUZZ_TEST_ERROR "Fuzz test error: " |
| 21 | + |
| 22 | +// groupName-testname-varName => data |
| 23 | +inline unordered_map<string, string> data_map; |
| 24 | + |
| 25 | +// Basically find terminating '"' that concludes the variable value |
| 26 | +inline bool parseVariableValue(ifstream &inputFile, string &returnVal) { |
| 27 | + string line; |
| 28 | + while (getline(inputFile, line)) { |
| 29 | + returnVal = returnVal + "\n" + line; |
| 30 | + // Terminating '"' found |
| 31 | + if (line[line.length() - 1] == '"') { |
| 32 | + // Remove double quotes |
| 33 | + returnVal = returnVal.substr(1, returnVal.length() - 2); |
| 34 | + return true; |
| 35 | + } |
| 36 | + } |
| 37 | + // EOF |
| 38 | + return false; |
| 39 | +} |
| 40 | + |
| 41 | +// Generate a string that combines the groupName testName and varName (i.e. |
| 42 | +// groupName-testname-varName) |
| 43 | +inline string generate_key(const vector<string> &names) { |
| 44 | + if (names.size() != 3) { |
| 45 | + cout << FUZZ_TEST_ERROR << "Incorrect key size" << endl; |
| 46 | + exit(1); |
| 47 | + } |
| 48 | + return names[0] + "--" + names[1] + "--" + names[2]; |
| 49 | +} |
| 50 | + |
| 51 | +// A utility function that counts how many "TAB"s are in the current line |
| 52 | +// A line should only have at most 2 tabs in the current schema |
| 53 | +inline int countTabs(const string &line) { |
| 54 | + int count = 0; |
| 55 | + for (unsigned int i = 0; i < line.size(); i++) { |
| 56 | + if (line[i] == ' ') { |
| 57 | + count++; |
| 58 | + } else { |
| 59 | + if (count % 2 != 0) { |
| 60 | + cout << FUZZ_TEST_ERROR << "Yaml does not have correct indentation " |
| 61 | + << endl; |
| 62 | + exit(1); |
| 63 | + } |
| 64 | + int indent = count / 2; |
| 65 | + if (indent > 2) { |
| 66 | + cout << FUZZ_TEST_ERROR << "Yaml has too much indentation " << endl; |
| 67 | + exit(1); |
| 68 | + } |
| 69 | + return indent; |
| 70 | + } |
| 71 | + } |
| 72 | + // A line of spaces, yaml error |
| 73 | + cout << FUZZ_TEST_ERROR << "Yaml should not have an empty line " << endl; |
| 74 | + exit(1); |
| 75 | +} |
| 76 | + |
| 77 | +// A function that loads fuzzed data from a input yaml file to data_map |
| 78 | +inline void preload(ifstream &inputFile) { |
| 79 | + string line; |
| 80 | + vector<string> names; |
| 81 | + // 0 => group, 1 => test, 2 => variable |
| 82 | + int lastIndent = 0; |
| 83 | + bool first = true; |
| 84 | + |
| 85 | + while (getline(inputFile, line)) { |
| 86 | + // Parsing yaml file manually |
| 87 | + int currentIndent = countTabs(line); |
| 88 | + if (currentIndent - 1 > lastIndent) { |
| 89 | + cout << FUZZ_TEST_ERROR << "Yaml does not have correct indentation " |
| 90 | + << endl; |
| 91 | + exit(1); |
| 92 | + } |
| 93 | + int diff = lastIndent - currentIndent; |
| 94 | + // Delete leaf if exiting |
| 95 | + if (diff >= 0 && !first) { |
| 96 | + names.resize(names.size() - diff - 1); |
| 97 | + } |
| 98 | + stringstream ss_line(line); |
| 99 | + string curr_name; |
| 100 | + ss_line >> curr_name; |
| 101 | + // If current indent level is at group/test |
| 102 | + if (currentIndent <= 1) { |
| 103 | + // Remove : at the end and add them to names |
| 104 | + names.push_back(curr_name.substr(0, curr_name.length() - 1)); |
| 105 | + } else { |
| 106 | + // Else the current indent level must be at variable |
| 107 | + stringstream ss(line); |
| 108 | + string varName; |
| 109 | + string varValue; |
| 110 | + // Note: varName contains ':' |
| 111 | + ss >> varName; |
| 112 | + names.push_back(varName.substr(0, varName.length() - 1)); |
| 113 | + // Note that varValue contains "" |
| 114 | + getline(ss, varValue); |
| 115 | + // Bypass leading space |
| 116 | + varValue = varValue.substr(1, varValue.size() - 1); |
| 117 | + string key = generate_key(names); |
| 118 | + if (varValue[varValue.length() - 1] == '"') { |
| 119 | + // Filter out double quote characters |
| 120 | + data_map.insert(pair<string, string>( |
| 121 | + key, varValue.substr(1, varValue.length() - 2))); |
| 122 | + } else { |
| 123 | + // The variable value contains newline character |
| 124 | + // Needs to parse multiple lines until finding the terminating '"' |
| 125 | + if (parseVariableValue(inputFile, varValue)) { |
| 126 | + data_map.insert(pair<string, string>(key, varValue)); |
| 127 | + } |
| 128 | + // EOF reached but still couldn't find terminating " |
| 129 | + else { |
| 130 | + cout << FUZZ_TEST_ERROR |
| 131 | + << "EOF reached but still couldn't find terminating \"" << endl; |
| 132 | + exit(1); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + lastIndent = currentIndent; |
| 137 | + first = false; |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// A top level function that preloads all fuzzed data into the data_map |
| 142 | +inline void preload_data(string fileName) { |
| 143 | + cout << "\nPreloading fuzzed data starts: " << endl; |
| 144 | + string pathPrefix = "../mutated_inputs/"; |
| 145 | + string path = pathPrefix + fileName + YML; |
| 146 | + cout << "Opening input file: " << path << endl; |
| 147 | + ifstream inputFile(path.c_str()); |
| 148 | + string value; |
| 149 | + if (inputFile.is_open()) { |
| 150 | + preload(inputFile); |
| 151 | + cout << "Preloading fuzzed data ends " << endl; |
| 152 | + } else { |
| 153 | + cout << FUZZ_TEST_ERROR << "Unable to open fuzz test file! " << endl; |
| 154 | + cout << "Make sure you run the fuzz test in the test directory" << endl; |
| 155 | + exit(1); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +// Parameters: group name, test name, variable name |
| 160 | +inline string load_fuzzed_value(string groupName, string testName, |
| 161 | + string varName) { |
| 162 | + vector<string> names = {groupName, testName, varName}; |
| 163 | + string key = generate_key(names); |
| 164 | + if (data_map.find(key) == data_map.end()) { |
| 165 | + cout << FUZZ_TEST_ERROR << "Unable to find " << key << endl; |
| 166 | + exit(1); |
| 167 | + } |
| 168 | + return data_map[key]; |
| 169 | +} |
| 170 | + |
| 171 | +// Cast data to corresponding data types |
| 172 | +template <class castType> |
| 173 | +inline castType load_fuzzed_value_cast(string groupName, string testName, |
| 174 | + string varName) { |
| 175 | + try { |
| 176 | + // Convert to unsigned long long then convert to castType |
| 177 | + string value = load_fuzzed_value(groupName, testName, varName); |
| 178 | + int base = 10; |
| 179 | + if (value.size() > 2 && value.substr(0, 2) == "0x") { |
| 180 | + base = 16; |
| 181 | + } |
| 182 | + return (castType)stoull(value, 0, base); |
| 183 | + } catch (string e) { |
| 184 | + cout << FUZZ_TEST_ERROR << "Data type mismatch " << endl; |
| 185 | + cout << e << endl; |
| 186 | + exit(1); |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +// Utility functions |
| 191 | +inline bool check_condition(bool condition, bool &check) { |
| 192 | + check = condition; |
| 193 | + return check; |
| 194 | +} |
| 195 | +#endif |
0 commit comments