@@ -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
203250int 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
0 commit comments