| 159 | } |
| 160 | |
| 161 | bool DataFlowTrace::Init(const std::string &DirPath, std::string *FocusFunction, |
| 162 | std::vector<SizedFile> &CorporaFiles, Random &Rand) { |
| 163 | if (DirPath.empty()) return false; |
| 164 | Printf("INFO: DataFlowTrace: reading from '%s'\n", DirPath.c_str()); |
| 165 | std::vector<SizedFile> Files; |
| 166 | GetSizedFilesFromDir(DirPath, &Files); |
| 167 | std::string L; |
| 168 | size_t FocusFuncIdx = SIZE_MAX; |
| 169 | std::vector<std::string> FunctionNames; |
| 170 | |
| 171 | // Collect the hashes of the corpus files. |
| 172 | for (auto &SF : CorporaFiles) |
| 173 | CorporaHashes.insert(Hash(FileToVector(SF.File))); |
| 174 | |
| 175 | // Read functions.txt |
| 176 | std::ifstream IF(DirPlusFile(DirPath, kFunctionsTxt)); |
| 177 | size_t NumFunctions = 0; |
| 178 | while (std::getline(IF, L, '\n')) { |
| 179 | FunctionNames.push_back(L); |
| 180 | NumFunctions++; |
| 181 | if (*FocusFunction == L) |
| 182 | FocusFuncIdx = NumFunctions - 1; |
| 183 | } |
| 184 | if (!NumFunctions) |
| 185 | return false; |
| 186 | |
| 187 | if (*FocusFunction == "auto") { |
| 188 | // AUTOFOCUS works like this: |
| 189 | // * reads the coverage data from the DFT files. |
| 190 | // * assigns weights to functions based on coverage. |
| 191 | // * chooses a random function according to the weights. |
| 192 | ReadCoverage(DirPath); |
| 193 | auto Weights = Coverage.FunctionWeights(NumFunctions); |
| 194 | std::vector<double> Intervals(NumFunctions + 1); |
| 195 | std::iota(Intervals.begin(), Intervals.end(), 0); |
| 196 | auto Distribution = std::piecewise_constant_distribution<double>( |
| 197 | Intervals.begin(), Intervals.end(), Weights.begin()); |
| 198 | FocusFuncIdx = static_cast<size_t>(Distribution(Rand)); |
| 199 | *FocusFunction = FunctionNames[FocusFuncIdx]; |
| 200 | assert(FocusFuncIdx < NumFunctions); |
| 201 | Printf("INFO: AUTOFOCUS: %zd %s\n", FocusFuncIdx, |
| 202 | FunctionNames[FocusFuncIdx].c_str()); |
| 203 | for (size_t i = 0; i < NumFunctions; i++) { |
| 204 | if (Weights[i] == 0.0) |
| 205 | continue; |
| 206 | Printf(" [%zd] W %g\tBB-tot %u\tBB-cov %u\tEntryFreq %u:\t%s\n", i, |
| 207 | Weights[i], Coverage.GetNumberOfBlocks(i), |
| 208 | Coverage.GetNumberOfCoveredBlocks(i), Coverage.GetCounter(i, 0), |
| 209 | FunctionNames[i].c_str()); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (!NumFunctions || FocusFuncIdx == SIZE_MAX || Files.size() <= 1) |
| 214 | return false; |
| 215 | |
| 216 | // Read traces. |
| 217 | size_t NumTraceFiles = 0; |
| 218 | size_t NumTracesWithFocusFunction = 0; |
no test coverage detected