Spaces:
Running
Running
File size: 6,672 Bytes
443f404 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | /* 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 =
"<b>Hint — the two bugs.</b>\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" +
"<b>Hint — TODO 1: the mutation operator.</b>\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" +
"<b>Hint — TODO 2: is_interesting(cov, covered).</b>\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();
|