Commit f56d113503

Andrew Kelley <andrew@ziglang.org>
2024-08-05 00:45:16
fuzzer web ui: render stats
1 parent dec7e45
Changed files (3)
lib/fuzzer/wasm/main.zig
@@ -103,6 +103,23 @@ export fn decl_source_html(decl_index: Decl.Index) String {
     return String.init(string_result.items);
 }
 
+export fn lowestStack() String {
+    const header: *abi.CoverageUpdateHeader = @ptrCast(recent_coverage_update.items[0..@sizeOf(abi.CoverageUpdateHeader)]);
+    string_result.clearRetainingCapacity();
+    string_result.writer(gpa).print("0x{d}", .{header.lowest_stack}) catch @panic("OOM");
+    return String.init(string_result.items);
+}
+
+export fn totalRuns() u64 {
+    const header: *abi.CoverageUpdateHeader = @ptrCast(recent_coverage_update.items[0..@sizeOf(abi.CoverageUpdateHeader)]);
+    return header.n_runs;
+}
+
+export fn uniqueRuns() u64 {
+    const header: *abi.CoverageUpdateHeader = @ptrCast(recent_coverage_update.items[0..@sizeOf(abi.CoverageUpdateHeader)]);
+    return header.unique_runs;
+}
+
 const String = Slice(u8);
 
 fn Slice(T: type) type {
lib/fuzzer/index.html
@@ -125,6 +125,13 @@
   </head>
   <body>
     <p id="status">Loading JavaScript...</p>
+    <div id="sectStats" class="hidden">
+      <ul>
+        <li>Total Runs: <span id="statTotalRuns"></span></li>
+        <li>Unique Runs: <span id="statUniqueRuns"></span></li>
+        <li>Lowest Stack: <span id="statLowestStack"></span></li>
+      </ul>
+    </div>
     <div id="sectSource" class="hidden">
       <h2>Source Code</h2>
       <pre><code id="sourceText"></code></pre>
lib/fuzzer/main.js
@@ -1,7 +1,11 @@
 (function() {
   const domStatus = document.getElementById("status");
   const domSectSource = document.getElementById("sectSource");
+  const domSectStats = document.getElementById("sectStats");
   const domSourceText = document.getElementById("sourceText");
+  const domStatTotalRuns = document.getElementById("statTotalRuns");
+  const domStatUniqueRuns = document.getElementById("statUniqueRuns");
+  const domStatLowestStack = document.getElementById("statLowestStack");
 
   let wasm_promise = fetch("main.wasm");
   let sources_promise = fetch("sources.tar").then(function(response) {
@@ -94,7 +98,7 @@
   }
 
   function onCoverageUpdate() {
-    console.log("coverage update");
+    renderStats();
   }
 
   function render() {
@@ -105,6 +109,20 @@
     renderSource("/home/andy/dev/zig/lib/std/zig/tokenizer.zig");
   }
 
+  function renderStats() {
+    const totalRuns = wasm_exports.totalRuns();
+    const uniqueRuns = wasm_exports.uniqueRuns();
+    domStatTotalRuns.innerText = totalRuns;
+    domStatUniqueRuns.innerText = uniqueRuns + " (" + percent(uniqueRuns, totalRuns) + "%)";
+    domStatLowestStack.innerText = unwrapString(wasm_exports.lowestStack());
+
+    domSectStats.classList.remove("hidden");
+  }
+
+  function percent(a, b) {
+    return ((Number(a) / Number(b)) * 100).toFixed(1);
+  }
+
   function renderSource(path) {
     const decl_index = findFileRoot(path);
     if (decl_index == null) throw new Error("file not found: " + path);