Skip to content

Commit cb5a8f8

Browse files
authored
Merge pull request #87 from hbgit/fix/testcomp27-engine-evidence
fix(testcomp): the smoke test requires evidence that each engine ran
2 parents 4186227 + 2ee2a0d commit cb5a8f8

3 files changed

Lines changed: 88 additions & 4 deletions

File tree

‎modules/frontend/caller.cpp‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,53 @@ long long kleeCoveredInstructions(const std::string &statsPath) {
198198
return -1;
199199
#endif
200200
}
201+
202+
/** One field of afl-fuzz's fuzzer_stats ("name : value" lines), which it
203+
* rewrites while fuzzing and once more on exit; -1 when the file or the field
204+
* is missing, as when afl-fuzz exited before fuzzing anything. */
205+
long long aflFuzzerStat(const std::string &statsPath, const std::string &name) {
206+
std::ifstream stats(statsPath);
207+
std::string line;
208+
while (std::getline(stats, line)) {
209+
const size_t colon = line.find(':');
210+
if (colon == std::string::npos) continue;
211+
std::string field = line.substr(0, colon);
212+
field.erase(field.find_last_not_of(' ') + 1);
213+
if (field != name) continue;
214+
try {
215+
return std::stoll(line.substr(colon + 1));
216+
} catch (const std::exception &) {
217+
return -1;
218+
}
219+
}
220+
return -1;
221+
}
222+
223+
/** The .ktest files KLEE wrote, one per path it terminated. */
224+
size_t kleeTestCount(const std::string &outputDir) {
225+
size_t count = 0;
226+
std::error_code error;
227+
for (const auto &entry :
228+
std::filesystem::directory_iterator(outputDir, error)) {
229+
if (entry.path().extension() == ".ktest") ++count;
230+
}
231+
return count;
232+
}
233+
234+
/** The last `count` lines of a file. For the log: the scratch directory that
235+
* holds an engine's own output is removed at the end of the run. */
236+
std::string lastLines(const std::string &path, size_t count) {
237+
std::ifstream file(path);
238+
std::vector<std::string> lines;
239+
std::string line;
240+
while (std::getline(file, line)) {
241+
lines.push_back(line);
242+
if (lines.size() > count) lines.erase(lines.begin());
243+
}
244+
std::string tail;
245+
for (const std::string &kept : lines) tail += " " + kept + "\n";
246+
return tail.empty() ? " (no output)\n" : tail;
247+
}
201248
} // namespace
202249

203250
int Caller::runKleeWatched(const std::string &command) {
@@ -1446,6 +1493,16 @@ void Caller::executeAnalysis(std::string solvername) {
14461493
exportKleeVectorsAsSeeds();
14471494
}
14481495
Map2Check::Log::Warning("Exited klee with " + std::to_string(result));
1496+
// "Executing Klee" is printed before KLEE starts, so it does not say
1497+
// that KLEE ran. The tests it wrote do, and this line is what the
1498+
// archive's smoke test requires.
1499+
const size_t kleeTests = kleeTestCount(Map2Check::kleeOutputDir);
1500+
if (kleeTests > 0) {
1501+
Map2Check::Log::Info("KLEE ran: " + std::to_string(kleeTests) +
1502+
" tests generated");
1503+
} else {
1504+
Map2Check::Log::Warning("KLEE generated no tests");
1505+
}
14491506
// KLEE found nothing: its vectors, run on natively. Not for
14501507
// Cover-Branches, whose goal is no violation.
14511508
if (map2checkMode != Map2CheckMode::COVER_BRANCHES_MODE &&
@@ -1577,6 +1634,26 @@ void Caller::executeAnalysis(std::string solvername) {
15771634
// its findings live under afl-out/default/, not afl-out/.
15781635
const std::string aflFindings = "afl-out/default";
15791636

1637+
// As for KLEE, "Executing AFL++" precedes the launch. The execution
1638+
// count afl-fuzz recorded is the evidence that it fuzzed, and this line
1639+
// is what the archive's smoke test requires. When there is none, what
1640+
// afl-fuzz said goes to the log: fuzzer.output is removed with the
1641+
// scratch directory.
1642+
const long long aflExecs =
1643+
aflFuzzerStat(aflFindings + "/fuzzer_stats", "execs_done");
1644+
if (aflExecs > 0) {
1645+
const long long corpus =
1646+
aflFuzzerStat(aflFindings + "/fuzzer_stats", "corpus_count");
1647+
Map2Check::Log::Info("AFL++ ran: " + std::to_string(aflExecs) +
1648+
" executions, " +
1649+
std::to_string(std::max(0LL, corpus)) +
1650+
" inputs in the corpus");
1651+
} else {
1652+
Map2Check::Log::Warning(
1653+
"AFL++ recorded no executions; its output ended with:\n" +
1654+
lastLines("fuzzer.output", 15));
1655+
}
1656+
15801657
// Replay crashes with the witness binary to confirm a real violation.
15811658
// Standalone, the persistent binary reads its input from stdin (see
15821659
// NonDetGeneratorAFL.c), so the crash file is redirected in; the names

‎utils/testcomp-archive/README.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ runs until the budget ends or KLEE has explored every path, and writes at most
4545
50 test cases: up to half from the fuzzer's corpus (the inputs that found new
4646
edges), the rest one per path KLEE explored.
4747

48+
The log records what each engine did, from its own output after the run:
49+
"AFL++ ran: N executions, M inputs in the corpus" and "KLEE ran: N tests
50+
generated". ./smoketest.sh requires both lines.
51+
4852
Bundled components
4953
------------------
5054
Map2Check GPL-2.0 LICENSE

‎utils/testcomp-archive/smoketest.sh‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@ fail() { # <work> <message>
4444
exit 1
4545
}
4646

47-
# The run log must show that <engine> ran: map2check prints this line only
48-
# after the engine's binary (AFL++) or bitcode (KLEE) was built.
47+
# The run log must show that <engine> ran. Not the "Executing ..." banners:
48+
# map2check prints those before launching the engine, so they appear when it
49+
# fails to start too, and the other engine can still produce the suite (review
50+
# of #86). These lines are printed after the run, from what the engine itself
51+
# wrote: afl-fuzz's execution count in fuzzer_stats, KLEE's .ktest files.
4952
ran() { # <work> <program> <engine>
5053
case "$3" in
51-
AFL++) line="Executing AFL++ with map2check" ;;
52-
KLEE) line="Executing Klee with map2check" ;;
54+
AFL++) line="AFL++ ran: [1-9][0-9]* executions" ;;
55+
KLEE) line="KLEE ran: [1-9][0-9]* tests generated" ;;
5356
esac
5457
grep -q "$line" "$1/run.log" || fail "$1" "$2: $3 did not run"
5558
}

0 commit comments

Comments
 (0)