/* Main-thread UI controller: owns the editor, spawns the worker, renders * progress. All the heavy lifting (Pyodide + wasm + the student loop) happens * in worker.js so the page never freezes. */ const $ = (id) => document.getElementById(id); const statusEl = $("status"); const runBtn = $("run"); const stopBtn = $("stop"); const resetBtn = $("reset"); const consoleEl = $("console"); const edgesEl = $("edges"); let editor; let worker = null; let workerReady = false; let starterCode = ""; function print(text, cls) { const span = document.createElement("span"); span.textContent = text + "\n"; if (cls) span.style.color = cls; consoleEl.appendChild(span); consoleEl.scrollTop = consoleEl.scrollHeight; } function renderEdges(numEdges, covered) { const set = new Set(covered || []); if (edgesEl.childElementCount !== numEdges) { edgesEl.innerHTML = ""; for (let i = 0; i < numEdges; i++) { const d = document.createElement("div"); d.className = "edge"; d.id = "edge-" + i; d.textContent = i; edgesEl.appendChild(d); } } for (let i = 0; i < numEdges; i++) { $("edge-" + i).classList.toggle("hit", set.has(i)); } } const CRASH_KINDS = ["bug1", "bug2"]; function resetDashboard() { $("s-execs").textContent = "0"; $("s-eps").textContent = "0"; $("s-corpus").textContent = "0"; $("s-cov").textContent = "0 / 0"; for (const kind of CRASH_KINDS) { const el = $("crash-" + kind); if (el) el.style.display = "none"; } consoleEl.innerHTML = ""; } // --- worker lifecycle ----------------------------------------------------- function spawnWorker() { worker = new Worker("worker.js"); workerReady = false; worker.onmessage = (e) => { const m = e.data; switch (m.type) { case "ready": workerReady = true; statusEl.textContent = "ready — edit fuzz.py and click Run"; runBtn.disabled = false; break; case "progress": $("s-execs").textContent = m.execs.toLocaleString(); $("s-eps").textContent = Math.round(m.execsPerSec).toLocaleString(); $("s-corpus").textContent = m.corpus.toLocaleString(); $("s-cov").textContent = `${m.covered.length} / ${m.numEdges}`; renderEdges(m.numEdges, m.covered); break; case "log": print(m.text, "#9fd0ff"); break; case "print": print(m.text); break; case "crash": { const kind = CRASH_KINDS.includes(m.which) ? m.which : "bug1"; const bytes = Uint8Array.from(m.bytes); const hex = Array.from(bytes) .map((b) => b.toString(16).padStart(2, "0")) .join(" "); $(`crash-${kind}-hex`).textContent = hex + ` (${bytes.length} bytes)`; const blob = new Blob([bytes], { type: "application/octet-stream" }); $(`crash-${kind}-dl`).href = URL.createObjectURL(blob); $(`crash-${kind}`).style.display = "block"; print(`💥 crash saved [${kind}] (` + hex + ")", "#ff9aa2"); break; } case "done": finishRun("finished"); break; case "error": print(m.text, "#ff5c6c"); finishRun("error"); break; } }; } function finishRun(why) { runBtn.disabled = false; stopBtn.disabled = true; statusEl.textContent = why === "error" ? "stopped on error" : "run complete"; } // --- buttons -------------------------------------------------------------- runBtn.onclick = () => { if (!workerReady) return; resetDashboard(); runBtn.disabled = true; stopBtn.disabled = false; statusEl.textContent = "fuzzing…"; worker.postMessage({ type: "run", code: editor.getValue() }); }; stopBtn.onclick = () => { if (worker) worker.terminate(); print("■ stopped by user", "#ffcf6b"); spawnWorker(); statusEl.textContent = "restarting runtime…"; finishRun("stopped"); runBtn.disabled = true; // until 'ready' }; resetBtn.onclick = () => { if (starterCode) editor.setValue(starterCode); }; // Hint: reveal only the coverage-retention step (TODO 2). Does not give away the // mutation operator (TODO 1), which students must choose and justify. const hintBtn = document.getElementById("hint-btn"); const hintBox = document.getElementById("hint"); hintBox.innerHTML = "Hint — the two bugs.\n" + " • Bug 1: an out-of-bounds READ (an unchecked index/offset).\n" + " • Bug 2: an INTEGER OVERFLOW (an arithmetic check that can wrap around).\n\n" + "Hint — TODO 1: the mutation operator.\n" + "The target checks individual bytes against fixed values. Think it through:\n" + " • If one byte is wrong, what is the simplest change that could make it right?\n" + " • A single bit-flip only nudges one bit. If the byte you need differs in\n" + " several bits, how many flips (and how much luck) would that take?\n" + " • What if, instead, you picked one position and dropped in a brand-new\n" + " random byte value? Which reaches an exact target byte faster?\n" + "That's a poor fit for Bug 2, which hinges on a NUMBER, not one exact byte.\n" + "Try whole 'interesting' values (0, 1, max, half-max, powers of two) instead\n" + "of a single random byte, then nudge a promising one with small +/- steps.\n\n" + "Hint — TODO 2: is_interesting(cov, covered).\n" + "`cov[k] > 0` means edge k was reached on THIS run. `covered` is the set of\n" + "edges reached on every previous run. Ask yourself:\n" + " • Which edges did THIS run reach?\n" + " • Is any of them missing from `covered` — something never seen before?\n" + " • If so, the input reached new code: should the function say True or False?\n" + "Return True exactly when the run reached at least one brand-new edge. Keeping\n" + "only those inputs is what lets the fuzzer climb the checks one at a time\n" + "instead of guessing them all at once."; hintBtn.onclick = () => { const show = hintBox.style.display !== "block"; hintBox.style.display = show ? "block" : "none"; hintBtn.textContent = show ? "💡 Hide hint" : "💡 Hint"; }; // --- boot ----------------------------------------------------------------- async function boot() { editor = CodeMirror.fromTextArea($("code"), { mode: "python", theme: "idea", lineNumbers: true, indentUnit: 4, }); try { starterCode = await (await fetch("starter.py")).text(); editor.setValue(starterCode); window.__editor = editor; // exposed for automated testing } catch { editor.setValue("# could not load starter.py — are you serving over http?\n"); } renderEdges(12, []); spawnWorker(); } boot();