Commit 6e6164f8a6

Andrew Kelley <andrew@ziglang.org>
2024-08-05 01:09:14
fuzzer web ui: add coverage stat
1 parent f56d113
Changed files (3)
lib/fuzzer/wasm/main.zig
@@ -110,6 +110,17 @@ export fn lowestStack() String {
     return String.init(string_result.items);
 }
 
+export fn totalSourceLocations() usize {
+    return coverage_source_locations.items.len;
+}
+
+export fn coveredSourceLocations() usize {
+    const covered_bits = recent_coverage_update.items[@sizeOf(abi.CoverageUpdateHeader)..];
+    var count: usize = 0;
+    for (covered_bits) |byte| count += @popCount(byte);
+    return count;
+}
+
 export fn totalRuns() u64 {
     const header: *abi.CoverageUpdateHeader = @ptrCast(recent_coverage_update.items[0..@sizeOf(abi.CoverageUpdateHeader)]);
     return header.n_runs;
lib/fuzzer/index.html
@@ -129,6 +129,7 @@
       <ul>
         <li>Total Runs: <span id="statTotalRuns"></span></li>
         <li>Unique Runs: <span id="statUniqueRuns"></span></li>
+        <li>Coverage: <span id="statCoverage"></span></li>
         <li>Lowest Stack: <span id="statLowestStack"></span></li>
       </ul>
     </div>
lib/fuzzer/main.js
@@ -5,6 +5,7 @@
   const domSourceText = document.getElementById("sourceText");
   const domStatTotalRuns = document.getElementById("statTotalRuns");
   const domStatUniqueRuns = document.getElementById("statUniqueRuns");
+  const domStatCoverage = document.getElementById("statCoverage");
   const domStatLowestStack = document.getElementById("statLowestStack");
 
   let wasm_promise = fetch("main.wasm");
@@ -112,8 +113,11 @@
   function renderStats() {
     const totalRuns = wasm_exports.totalRuns();
     const uniqueRuns = wasm_exports.uniqueRuns();
+    const totalSourceLocations = wasm_exports.totalSourceLocations();
+    const coveredSourceLocations = wasm_exports.coveredSourceLocations();
     domStatTotalRuns.innerText = totalRuns;
     domStatUniqueRuns.innerText = uniqueRuns + " (" + percent(uniqueRuns, totalRuns) + "%)";
+    domStatCoverage.innerText = coveredSourceLocations + " / " + totalSourceLocations + " (" + percent(coveredSourceLocations, totalSourceLocations) + "%)";
     domStatLowestStack.innerText = unwrapString(wasm_exports.lowestStack());
 
     domSectStats.classList.remove("hidden");