From 0cdccff51912359b7ec5afa57fbbd5bb69d8f3a2 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 9 Sep 2024 20:37:03 -0700 Subject: fuzzer: move web files into separate directory --- lib/fuzzer/index.html | 161 ----------------- lib/fuzzer/main.js | 249 --------------------------- lib/fuzzer/wasm/main.zig | 428 ---------------------------------------------- lib/fuzzer/web/index.html | 161 +++++++++++++++++ lib/fuzzer/web/main.js | 249 +++++++++++++++++++++++++++ lib/fuzzer/web/main.zig | 428 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 838 insertions(+), 838 deletions(-) delete mode 100644 lib/fuzzer/index.html delete mode 100644 lib/fuzzer/main.js delete mode 100644 lib/fuzzer/wasm/main.zig create mode 100644 lib/fuzzer/web/index.html create mode 100644 lib/fuzzer/web/main.js create mode 100644 lib/fuzzer/web/main.zig (limited to 'lib/fuzzer') diff --git a/lib/fuzzer/index.html b/lib/fuzzer/index.html deleted file mode 100644 index 16fa879913..0000000000 --- a/lib/fuzzer/index.html +++ /dev/null @@ -1,161 +0,0 @@ - - - - - Zig Build System Interface - - - -

Loading JavaScript...

- - - - - - diff --git a/lib/fuzzer/main.js b/lib/fuzzer/main.js deleted file mode 100644 index ce02276f98..0000000000 --- a/lib/fuzzer/main.js +++ /dev/null @@ -1,249 +0,0 @@ -(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 domStatCoverage = document.getElementById("statCoverage"); - const domStatLowestStack = document.getElementById("statLowestStack"); - const domEntryPointsList = document.getElementById("entryPointsList"); - - let wasm_promise = fetch("main.wasm"); - let sources_promise = fetch("sources.tar").then(function(response) { - if (!response.ok) throw new Error("unable to download sources"); - return response.arrayBuffer(); - }); - var wasm_exports = null; - var curNavSearch = null; - var curNavLocation = null; - - const text_decoder = new TextDecoder(); - const text_encoder = new TextEncoder(); - - domStatus.textContent = "Loading WebAssembly..."; - WebAssembly.instantiateStreaming(wasm_promise, { - js: { - log: function(ptr, len) { - const msg = decodeString(ptr, len); - console.log(msg); - }, - panic: function (ptr, len) { - const msg = decodeString(ptr, len); - throw new Error("panic: " + msg); - }, - emitSourceIndexChange: onSourceIndexChange, - emitCoverageUpdate: onCoverageUpdate, - emitEntryPointsUpdate: renderStats, - }, - }).then(function(obj) { - wasm_exports = obj.instance.exports; - window.wasm = obj; // for debugging - domStatus.textContent = "Loading sources tarball..."; - - sources_promise.then(function(buffer) { - domStatus.textContent = "Parsing sources..."; - const js_array = new Uint8Array(buffer); - const ptr = wasm_exports.alloc(js_array.length); - const wasm_array = new Uint8Array(wasm_exports.memory.buffer, ptr, js_array.length); - wasm_array.set(js_array); - wasm_exports.unpack(ptr, js_array.length); - - window.addEventListener('popstate', onPopState, false); - onHashChange(null); - - domStatus.textContent = "Waiting for server to send source location metadata..."; - connectWebSocket(); - }); - }); - - function onPopState(ev) { - onHashChange(ev.state); - } - - function onHashChange(state) { - history.replaceState({}, ""); - navigate(location.hash); - if (state == null) window.scrollTo({top: 0}); - } - - function navigate(location_hash) { - domSectSource.classList.add("hidden"); - - curNavLocation = null; - curNavSearch = null; - - if (location_hash.length > 1 && location_hash[0] === '#') { - const query = location_hash.substring(1); - const qpos = query.indexOf("?"); - let nonSearchPart; - if (qpos === -1) { - nonSearchPart = query; - } else { - nonSearchPart = query.substring(0, qpos); - curNavSearch = decodeURIComponent(query.substring(qpos + 1)); - } - - if (nonSearchPart[0] == "l") { - curNavLocation = +nonSearchPart.substring(1); - renderSource(curNavLocation); - } - } - - render(); - } - - function connectWebSocket() { - const host = document.location.host; - const pathname = document.location.pathname; - const isHttps = document.location.protocol === 'https:'; - const match = host.match(/^(.+):(\d+)$/); - const defaultPort = isHttps ? 443 : 80; - const port = match ? parseInt(match[2], 10) : defaultPort; - const hostName = match ? match[1] : host; - const wsProto = isHttps ? "wss:" : "ws:"; - const wsUrl = wsProto + '//' + hostName + ':' + port + pathname; - ws = new WebSocket(wsUrl); - ws.binaryType = "arraybuffer"; - ws.addEventListener('message', onWebSocketMessage, false); - ws.addEventListener('error', timeoutThenCreateNew, false); - ws.addEventListener('close', timeoutThenCreateNew, false); - ws.addEventListener('open', onWebSocketOpen, false); - } - - function onWebSocketOpen() { - //console.log("web socket opened"); - } - - function onWebSocketMessage(ev) { - wasmOnMessage(ev.data); - } - - function timeoutThenCreateNew() { - ws.removeEventListener('message', onWebSocketMessage, false); - ws.removeEventListener('error', timeoutThenCreateNew, false); - ws.removeEventListener('close', timeoutThenCreateNew, false); - ws.removeEventListener('open', onWebSocketOpen, false); - ws = null; - setTimeout(connectWebSocket, 1000); - } - - function wasmOnMessage(data) { - const jsArray = new Uint8Array(data); - const ptr = wasm_exports.message_begin(jsArray.length); - const wasmArray = new Uint8Array(wasm_exports.memory.buffer, ptr, jsArray.length); - wasmArray.set(jsArray); - wasm_exports.message_end(); - } - - function onSourceIndexChange() { - render(); - if (curNavLocation != null) renderSource(curNavLocation); - } - - function onCoverageUpdate() { - renderStats(); - renderCoverage(); - } - - function render() { - domStatus.classList.add("hidden"); - } - - 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()); - - const entryPoints = unwrapInt32Array(wasm_exports.entryPoints()); - resizeDomList(domEntryPointsList, entryPoints.length, "
  • "); - for (let i = 0; i < entryPoints.length; i += 1) { - const liDom = domEntryPointsList.children[i]; - liDom.innerHTML = unwrapString(wasm_exports.sourceLocationLinkHtml(entryPoints[i])); - } - - - domSectStats.classList.remove("hidden"); - } - - function renderCoverage() { - if (curNavLocation == null) return; - const sourceLocationIndex = curNavLocation; - - for (let i = 0; i < domSourceText.children.length; i += 1) { - const childDom = domSourceText.children[i]; - if (childDom.id != null && childDom.id[0] == "l") { - childDom.classList.add("l"); - childDom.classList.remove("c"); - } - } - const coveredList = unwrapInt32Array(wasm_exports.sourceLocationFileCoveredList(sourceLocationIndex)); - for (let i = 0; i < coveredList.length; i += 1) { - document.getElementById("l" + coveredList[i]).classList.add("c"); - } - } - - function resizeDomList(listDom, desiredLen, templateHtml) { - for (let i = listDom.childElementCount; i < desiredLen; i += 1) { - listDom.insertAdjacentHTML('beforeend', templateHtml); - } - while (desiredLen < listDom.childElementCount) { - listDom.removeChild(listDom.lastChild); - } - } - - function percent(a, b) { - return ((Number(a) / Number(b)) * 100).toFixed(1); - } - - function renderSource(sourceLocationIndex) { - const pathName = unwrapString(wasm_exports.sourceLocationPath(sourceLocationIndex)); - if (pathName.length === 0) return; - - const h2 = domSectSource.children[0]; - h2.innerText = pathName; - domSourceText.innerHTML = unwrapString(wasm_exports.sourceLocationFileHtml(sourceLocationIndex)); - - domSectSource.classList.remove("hidden"); - - // Empirically, Firefox needs this requestAnimationFrame in order for the scrollIntoView to work. - requestAnimationFrame(function() { - const slDom = document.getElementById("l" + sourceLocationIndex); - if (slDom != null) slDom.scrollIntoView({ - behavior: "smooth", - block: "center", - }); - }); - } - - function decodeString(ptr, len) { - if (len === 0) return ""; - return text_decoder.decode(new Uint8Array(wasm_exports.memory.buffer, ptr, len)); - } - - function unwrapInt32Array(bigint) { - const ptr = Number(bigint & 0xffffffffn); - const len = Number(bigint >> 32n); - if (len === 0) return new Uint32Array(); - return new Uint32Array(wasm_exports.memory.buffer, ptr, len); - } - - function setInputString(s) { - const jsArray = text_encoder.encode(s); - const len = jsArray.length; - const ptr = wasm_exports.set_input_string(len); - const wasmArray = new Uint8Array(wasm_exports.memory.buffer, ptr, len); - wasmArray.set(jsArray); - } - - function unwrapString(bigint) { - const ptr = Number(bigint & 0xffffffffn); - const len = Number(bigint >> 32n); - return decodeString(ptr, len); - } -})(); diff --git a/lib/fuzzer/wasm/main.zig b/lib/fuzzer/wasm/main.zig deleted file mode 100644 index 342adc3b56..0000000000 --- a/lib/fuzzer/wasm/main.zig +++ /dev/null @@ -1,428 +0,0 @@ -const std = @import("std"); -const assert = std.debug.assert; -const abi = std.Build.Fuzz.abi; -const gpa = std.heap.wasm_allocator; -const log = std.log; -const Coverage = std.debug.Coverage; -const Allocator = std.mem.Allocator; - -const Walk = @import("Walk"); -const Decl = Walk.Decl; -const html_render = @import("html_render"); - -const js = struct { - extern "js" fn log(ptr: [*]const u8, len: usize) void; - extern "js" fn panic(ptr: [*]const u8, len: usize) noreturn; - extern "js" fn emitSourceIndexChange() void; - extern "js" fn emitCoverageUpdate() void; - extern "js" fn emitEntryPointsUpdate() void; -}; - -pub const std_options: std.Options = .{ - .logFn = logFn, -}; - -pub fn panic(msg: []const u8, st: ?*std.builtin.StackTrace, addr: ?usize) noreturn { - _ = st; - _ = addr; - log.err("panic: {s}", .{msg}); - @trap(); -} - -fn logFn( - comptime message_level: log.Level, - comptime scope: @TypeOf(.enum_literal), - comptime format: []const u8, - args: anytype, -) void { - const level_txt = comptime message_level.asText(); - const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): "; - var buf: [500]u8 = undefined; - const line = std.fmt.bufPrint(&buf, level_txt ++ prefix2 ++ format, args) catch l: { - buf[buf.len - 3 ..][0..3].* = "...".*; - break :l &buf; - }; - js.log(line.ptr, line.len); -} - -export fn alloc(n: usize) [*]u8 { - const slice = gpa.alloc(u8, n) catch @panic("OOM"); - return slice.ptr; -} - -var message_buffer: std.ArrayListAlignedUnmanaged(u8, @alignOf(u64)) = .{}; - -/// Resizes the message buffer to be the correct length; returns the pointer to -/// the query string. -export fn message_begin(len: usize) [*]u8 { - message_buffer.resize(gpa, len) catch @panic("OOM"); - return message_buffer.items.ptr; -} - -export fn message_end() void { - const msg_bytes = message_buffer.items; - - const tag: abi.ToClientTag = @enumFromInt(msg_bytes[0]); - switch (tag) { - .source_index => return sourceIndexMessage(msg_bytes) catch @panic("OOM"), - .coverage_update => return coverageUpdateMessage(msg_bytes) catch @panic("OOM"), - .entry_points => return entryPointsMessage(msg_bytes) catch @panic("OOM"), - _ => unreachable, - } -} - -export fn unpack(tar_ptr: [*]u8, tar_len: usize) void { - const tar_bytes = tar_ptr[0..tar_len]; - log.debug("received {d} bytes of tar file", .{tar_bytes.len}); - - unpackInner(tar_bytes) catch |err| { - fatal("unable to unpack tar: {s}", .{@errorName(err)}); - }; -} - -/// Set by `set_input_string`. -var input_string: std.ArrayListUnmanaged(u8) = .{}; -var string_result: std.ArrayListUnmanaged(u8) = .{}; - -export fn set_input_string(len: usize) [*]u8 { - input_string.resize(gpa, len) catch @panic("OOM"); - return input_string.items.ptr; -} - -/// Looks up the root struct decl corresponding to a file by path. -/// Uses `input_string`. -export fn find_file_root() Decl.Index { - const file: Walk.File.Index = @enumFromInt(Walk.files.getIndex(input_string.items) orelse return .none); - return file.findRootDecl(); -} - -export fn decl_source_html(decl_index: Decl.Index) String { - const decl = decl_index.get(); - - string_result.clearRetainingCapacity(); - html_render.fileSourceHtml(decl.file, &string_result, decl.ast_node, .{}) catch |err| { - fatal("unable to render source: {s}", .{@errorName(err)}); - }; - 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 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 = @alignCast(@ptrCast(recent_coverage_update.items[0..@sizeOf(abi.CoverageUpdateHeader)])); - return header.n_runs; -} - -export fn uniqueRuns() u64 { - const header: *abi.CoverageUpdateHeader = @alignCast(@ptrCast(recent_coverage_update.items[0..@sizeOf(abi.CoverageUpdateHeader)])); - return header.unique_runs; -} - -const String = Slice(u8); - -fn Slice(T: type) type { - return packed struct(u64) { - ptr: u32, - len: u32, - - fn init(s: []const T) @This() { - return .{ - .ptr = @intFromPtr(s.ptr), - .len = s.len, - }; - } - }; -} - -fn unpackInner(tar_bytes: []u8) !void { - var fbs = std.io.fixedBufferStream(tar_bytes); - var file_name_buffer: [1024]u8 = undefined; - var link_name_buffer: [1024]u8 = undefined; - var it = std.tar.iterator(fbs.reader(), .{ - .file_name_buffer = &file_name_buffer, - .link_name_buffer = &link_name_buffer, - }); - while (try it.next()) |tar_file| { - switch (tar_file.kind) { - .file => { - if (tar_file.size == 0 and tar_file.name.len == 0) break; - if (std.mem.endsWith(u8, tar_file.name, ".zig")) { - log.debug("found file: '{s}'", .{tar_file.name}); - const file_name = try gpa.dupe(u8, tar_file.name); - if (std.mem.indexOfScalar(u8, file_name, '/')) |pkg_name_end| { - const pkg_name = file_name[0..pkg_name_end]; - const gop = try Walk.modules.getOrPut(gpa, pkg_name); - const file: Walk.File.Index = @enumFromInt(Walk.files.entries.len); - if (!gop.found_existing or - std.mem.eql(u8, file_name[pkg_name_end..], "/root.zig") or - std.mem.eql(u8, file_name[pkg_name_end + 1 .. file_name.len - ".zig".len], pkg_name)) - { - gop.value_ptr.* = file; - } - const file_bytes = tar_bytes[fbs.pos..][0..@intCast(tar_file.size)]; - assert(file == try Walk.add_file(file_name, file_bytes)); - } - } else { - log.warn("skipping: '{s}' - the tar creation should have done that", .{tar_file.name}); - } - }, - else => continue, - } - } -} - -fn fatal(comptime format: []const u8, args: anytype) noreturn { - var buf: [500]u8 = undefined; - const line = std.fmt.bufPrint(&buf, format, args) catch l: { - buf[buf.len - 3 ..][0..3].* = "...".*; - break :l &buf; - }; - js.panic(line.ptr, line.len); -} - -fn sourceIndexMessage(msg_bytes: []u8) error{OutOfMemory}!void { - const Header = abi.SourceIndexHeader; - const header: Header = @bitCast(msg_bytes[0..@sizeOf(Header)].*); - - const directories_start = @sizeOf(Header); - const directories_end = directories_start + header.directories_len * @sizeOf(Coverage.String); - const files_start = directories_end; - const files_end = files_start + header.files_len * @sizeOf(Coverage.File); - const source_locations_start = files_end; - const source_locations_end = source_locations_start + header.source_locations_len * @sizeOf(Coverage.SourceLocation); - const string_bytes = msg_bytes[source_locations_end..][0..header.string_bytes_len]; - - const directories: []const Coverage.String = @alignCast(std.mem.bytesAsSlice(Coverage.String, msg_bytes[directories_start..directories_end])); - const files: []const Coverage.File = @alignCast(std.mem.bytesAsSlice(Coverage.File, msg_bytes[files_start..files_end])); - const source_locations: []const Coverage.SourceLocation = @alignCast(std.mem.bytesAsSlice(Coverage.SourceLocation, msg_bytes[source_locations_start..source_locations_end])); - - try updateCoverage(directories, files, source_locations, string_bytes); - js.emitSourceIndexChange(); -} - -fn coverageUpdateMessage(msg_bytes: []u8) error{OutOfMemory}!void { - recent_coverage_update.clearRetainingCapacity(); - recent_coverage_update.appendSlice(gpa, msg_bytes) catch @panic("OOM"); - js.emitCoverageUpdate(); -} - -var entry_points: std.ArrayListUnmanaged(u32) = .{}; - -fn entryPointsMessage(msg_bytes: []u8) error{OutOfMemory}!void { - const header: abi.EntryPointHeader = @bitCast(msg_bytes[0..@sizeOf(abi.EntryPointHeader)].*); - entry_points.resize(gpa, header.flags.locs_len) catch @panic("OOM"); - @memcpy(entry_points.items, std.mem.bytesAsSlice(u32, msg_bytes[@sizeOf(abi.EntryPointHeader)..])); - js.emitEntryPointsUpdate(); -} - -export fn entryPoints() Slice(u32) { - return Slice(u32).init(entry_points.items); -} - -/// Index into `coverage_source_locations`. -const SourceLocationIndex = enum(u32) { - _, - - fn haveCoverage(sli: SourceLocationIndex) bool { - return @intFromEnum(sli) < coverage_source_locations.items.len; - } - - fn ptr(sli: SourceLocationIndex) *Coverage.SourceLocation { - return &coverage_source_locations.items[@intFromEnum(sli)]; - } - - fn sourceLocationLinkHtml( - sli: SourceLocationIndex, - out: *std.ArrayListUnmanaged(u8), - ) Allocator.Error!void { - const sl = sli.ptr(); - try out.writer(gpa).print("", .{@intFromEnum(sli)}); - try sli.appendPath(out); - try out.writer(gpa).print(":{d}:{d}", .{ sl.line, sl.column }); - } - - fn appendPath(sli: SourceLocationIndex, out: *std.ArrayListUnmanaged(u8)) Allocator.Error!void { - const sl = sli.ptr(); - const file = coverage.fileAt(sl.file); - const file_name = coverage.stringAt(file.basename); - const dir_name = coverage.stringAt(coverage.directories.keys()[file.directory_index]); - try html_render.appendEscaped(out, dir_name); - try out.appendSlice(gpa, "/"); - try html_render.appendEscaped(out, file_name); - } - - fn toWalkFile(sli: SourceLocationIndex) ?Walk.File.Index { - var buf: std.ArrayListUnmanaged(u8) = .{}; - defer buf.deinit(gpa); - sli.appendPath(&buf) catch @panic("OOM"); - return @enumFromInt(Walk.files.getIndex(buf.items) orelse return null); - } - - fn fileHtml( - sli: SourceLocationIndex, - out: *std.ArrayListUnmanaged(u8), - ) error{ OutOfMemory, SourceUnavailable }!void { - const walk_file_index = sli.toWalkFile() orelse return error.SourceUnavailable; - const root_node = walk_file_index.findRootDecl().get().ast_node; - var annotations: std.ArrayListUnmanaged(html_render.Annotation) = .{}; - defer annotations.deinit(gpa); - try computeSourceAnnotations(sli.ptr().file, walk_file_index, &annotations, coverage_source_locations.items); - html_render.fileSourceHtml(walk_file_index, out, root_node, .{ - .source_location_annotations = annotations.items, - }) catch |err| { - fatal("unable to render source: {s}", .{@errorName(err)}); - }; - } -}; - -fn computeSourceAnnotations( - cov_file_index: Coverage.File.Index, - walk_file_index: Walk.File.Index, - annotations: *std.ArrayListUnmanaged(html_render.Annotation), - source_locations: []const Coverage.SourceLocation, -) !void { - // Collect all the source locations from only this file into this array - // first, then sort by line, col, so that we can collect annotations with - // O(N) time complexity. - var locs: std.ArrayListUnmanaged(SourceLocationIndex) = .{}; - defer locs.deinit(gpa); - - for (source_locations, 0..) |sl, sli_usize| { - if (sl.file != cov_file_index) continue; - const sli: SourceLocationIndex = @enumFromInt(sli_usize); - try locs.append(gpa, sli); - } - - std.mem.sortUnstable(SourceLocationIndex, locs.items, {}, struct { - pub fn lessThan(context: void, lhs: SourceLocationIndex, rhs: SourceLocationIndex) bool { - _ = context; - const lhs_ptr = lhs.ptr(); - const rhs_ptr = rhs.ptr(); - if (lhs_ptr.line < rhs_ptr.line) return true; - if (lhs_ptr.line > rhs_ptr.line) return false; - return lhs_ptr.column < rhs_ptr.column; - } - }.lessThan); - - const source = walk_file_index.get_ast().source; - var line: usize = 1; - var column: usize = 1; - var next_loc_index: usize = 0; - for (source, 0..) |byte, offset| { - if (byte == '\n') { - line += 1; - column = 1; - } else { - column += 1; - } - while (true) { - if (next_loc_index >= locs.items.len) return; - const next_sli = locs.items[next_loc_index]; - const next_sl = next_sli.ptr(); - if (next_sl.line > line or (next_sl.line == line and next_sl.column >= column)) break; - try annotations.append(gpa, .{ - .file_byte_offset = offset, - .dom_id = @intFromEnum(next_sli), - }); - next_loc_index += 1; - } - } -} - -var coverage = Coverage.init; -/// Index of type `SourceLocationIndex`. -var coverage_source_locations: std.ArrayListUnmanaged(Coverage.SourceLocation) = .{}; -/// Contains the most recent coverage update message, unmodified. -var recent_coverage_update: std.ArrayListAlignedUnmanaged(u8, @alignOf(u64)) = .{}; - -fn updateCoverage( - directories: []const Coverage.String, - files: []const Coverage.File, - source_locations: []const Coverage.SourceLocation, - string_bytes: []const u8, -) !void { - coverage.directories.clearRetainingCapacity(); - coverage.files.clearRetainingCapacity(); - coverage.string_bytes.clearRetainingCapacity(); - coverage_source_locations.clearRetainingCapacity(); - - try coverage_source_locations.appendSlice(gpa, source_locations); - try coverage.string_bytes.appendSlice(gpa, string_bytes); - - try coverage.files.entries.resize(gpa, files.len); - @memcpy(coverage.files.entries.items(.key), files); - try coverage.files.reIndexContext(gpa, .{ .string_bytes = coverage.string_bytes.items }); - - try coverage.directories.entries.resize(gpa, directories.len); - @memcpy(coverage.directories.entries.items(.key), directories); - try coverage.directories.reIndexContext(gpa, .{ .string_bytes = coverage.string_bytes.items }); -} - -export fn sourceLocationLinkHtml(index: SourceLocationIndex) String { - string_result.clearRetainingCapacity(); - index.sourceLocationLinkHtml(&string_result) catch @panic("OOM"); - return String.init(string_result.items); -} - -/// Returns empty string if coverage metadata is not available for this source location. -export fn sourceLocationPath(sli: SourceLocationIndex) String { - string_result.clearRetainingCapacity(); - if (sli.haveCoverage()) sli.appendPath(&string_result) catch @panic("OOM"); - return String.init(string_result.items); -} - -export fn sourceLocationFileHtml(sli: SourceLocationIndex) String { - string_result.clearRetainingCapacity(); - sli.fileHtml(&string_result) catch |err| switch (err) { - error.OutOfMemory => @panic("OOM"), - error.SourceUnavailable => {}, - }; - return String.init(string_result.items); -} - -export fn sourceLocationFileCoveredList(sli_file: SourceLocationIndex) Slice(SourceLocationIndex) { - const global = struct { - var result: std.ArrayListUnmanaged(SourceLocationIndex) = .{}; - fn add(i: u32, want_file: Coverage.File.Index) void { - const src_loc_index: SourceLocationIndex = @enumFromInt(i); - if (src_loc_index.ptr().file == want_file) result.appendAssumeCapacity(src_loc_index); - } - }; - const want_file = sli_file.ptr().file; - global.result.clearRetainingCapacity(); - - // This code assumes 64-bit elements, which is incorrect if the executable - // being fuzzed is not a 64-bit CPU. It also assumes little-endian which - // can also be incorrect. - comptime assert(abi.CoverageUpdateHeader.trailing[0] == .pc_bits_usize); - const n_bitset_elems = (coverage_source_locations.items.len + @bitSizeOf(u64) - 1) / @bitSizeOf(u64); - const covered_bits = std.mem.bytesAsSlice( - u64, - recent_coverage_update.items[@sizeOf(abi.CoverageUpdateHeader)..][0 .. n_bitset_elems * @sizeOf(u64)], - ); - var sli: u32 = 0; - for (covered_bits) |elem| { - global.result.ensureUnusedCapacity(gpa, 64) catch @panic("OOM"); - for (0..@bitSizeOf(u64)) |i| { - if ((elem & (@as(u64, 1) << @intCast(i))) != 0) global.add(sli, want_file); - sli += 1; - } - } - return Slice(SourceLocationIndex).init(global.result.items); -} diff --git a/lib/fuzzer/web/index.html b/lib/fuzzer/web/index.html new file mode 100644 index 0000000000..16fa879913 --- /dev/null +++ b/lib/fuzzer/web/index.html @@ -0,0 +1,161 @@ + + + + + Zig Build System Interface + + + +

    Loading JavaScript...

    + + + + + + diff --git a/lib/fuzzer/web/main.js b/lib/fuzzer/web/main.js new file mode 100644 index 0000000000..ce02276f98 --- /dev/null +++ b/lib/fuzzer/web/main.js @@ -0,0 +1,249 @@ +(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 domStatCoverage = document.getElementById("statCoverage"); + const domStatLowestStack = document.getElementById("statLowestStack"); + const domEntryPointsList = document.getElementById("entryPointsList"); + + let wasm_promise = fetch("main.wasm"); + let sources_promise = fetch("sources.tar").then(function(response) { + if (!response.ok) throw new Error("unable to download sources"); + return response.arrayBuffer(); + }); + var wasm_exports = null; + var curNavSearch = null; + var curNavLocation = null; + + const text_decoder = new TextDecoder(); + const text_encoder = new TextEncoder(); + + domStatus.textContent = "Loading WebAssembly..."; + WebAssembly.instantiateStreaming(wasm_promise, { + js: { + log: function(ptr, len) { + const msg = decodeString(ptr, len); + console.log(msg); + }, + panic: function (ptr, len) { + const msg = decodeString(ptr, len); + throw new Error("panic: " + msg); + }, + emitSourceIndexChange: onSourceIndexChange, + emitCoverageUpdate: onCoverageUpdate, + emitEntryPointsUpdate: renderStats, + }, + }).then(function(obj) { + wasm_exports = obj.instance.exports; + window.wasm = obj; // for debugging + domStatus.textContent = "Loading sources tarball..."; + + sources_promise.then(function(buffer) { + domStatus.textContent = "Parsing sources..."; + const js_array = new Uint8Array(buffer); + const ptr = wasm_exports.alloc(js_array.length); + const wasm_array = new Uint8Array(wasm_exports.memory.buffer, ptr, js_array.length); + wasm_array.set(js_array); + wasm_exports.unpack(ptr, js_array.length); + + window.addEventListener('popstate', onPopState, false); + onHashChange(null); + + domStatus.textContent = "Waiting for server to send source location metadata..."; + connectWebSocket(); + }); + }); + + function onPopState(ev) { + onHashChange(ev.state); + } + + function onHashChange(state) { + history.replaceState({}, ""); + navigate(location.hash); + if (state == null) window.scrollTo({top: 0}); + } + + function navigate(location_hash) { + domSectSource.classList.add("hidden"); + + curNavLocation = null; + curNavSearch = null; + + if (location_hash.length > 1 && location_hash[0] === '#') { + const query = location_hash.substring(1); + const qpos = query.indexOf("?"); + let nonSearchPart; + if (qpos === -1) { + nonSearchPart = query; + } else { + nonSearchPart = query.substring(0, qpos); + curNavSearch = decodeURIComponent(query.substring(qpos + 1)); + } + + if (nonSearchPart[0] == "l") { + curNavLocation = +nonSearchPart.substring(1); + renderSource(curNavLocation); + } + } + + render(); + } + + function connectWebSocket() { + const host = document.location.host; + const pathname = document.location.pathname; + const isHttps = document.location.protocol === 'https:'; + const match = host.match(/^(.+):(\d+)$/); + const defaultPort = isHttps ? 443 : 80; + const port = match ? parseInt(match[2], 10) : defaultPort; + const hostName = match ? match[1] : host; + const wsProto = isHttps ? "wss:" : "ws:"; + const wsUrl = wsProto + '//' + hostName + ':' + port + pathname; + ws = new WebSocket(wsUrl); + ws.binaryType = "arraybuffer"; + ws.addEventListener('message', onWebSocketMessage, false); + ws.addEventListener('error', timeoutThenCreateNew, false); + ws.addEventListener('close', timeoutThenCreateNew, false); + ws.addEventListener('open', onWebSocketOpen, false); + } + + function onWebSocketOpen() { + //console.log("web socket opened"); + } + + function onWebSocketMessage(ev) { + wasmOnMessage(ev.data); + } + + function timeoutThenCreateNew() { + ws.removeEventListener('message', onWebSocketMessage, false); + ws.removeEventListener('error', timeoutThenCreateNew, false); + ws.removeEventListener('close', timeoutThenCreateNew, false); + ws.removeEventListener('open', onWebSocketOpen, false); + ws = null; + setTimeout(connectWebSocket, 1000); + } + + function wasmOnMessage(data) { + const jsArray = new Uint8Array(data); + const ptr = wasm_exports.message_begin(jsArray.length); + const wasmArray = new Uint8Array(wasm_exports.memory.buffer, ptr, jsArray.length); + wasmArray.set(jsArray); + wasm_exports.message_end(); + } + + function onSourceIndexChange() { + render(); + if (curNavLocation != null) renderSource(curNavLocation); + } + + function onCoverageUpdate() { + renderStats(); + renderCoverage(); + } + + function render() { + domStatus.classList.add("hidden"); + } + + 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()); + + const entryPoints = unwrapInt32Array(wasm_exports.entryPoints()); + resizeDomList(domEntryPointsList, entryPoints.length, "
  • "); + for (let i = 0; i < entryPoints.length; i += 1) { + const liDom = domEntryPointsList.children[i]; + liDom.innerHTML = unwrapString(wasm_exports.sourceLocationLinkHtml(entryPoints[i])); + } + + + domSectStats.classList.remove("hidden"); + } + + function renderCoverage() { + if (curNavLocation == null) return; + const sourceLocationIndex = curNavLocation; + + for (let i = 0; i < domSourceText.children.length; i += 1) { + const childDom = domSourceText.children[i]; + if (childDom.id != null && childDom.id[0] == "l") { + childDom.classList.add("l"); + childDom.classList.remove("c"); + } + } + const coveredList = unwrapInt32Array(wasm_exports.sourceLocationFileCoveredList(sourceLocationIndex)); + for (let i = 0; i < coveredList.length; i += 1) { + document.getElementById("l" + coveredList[i]).classList.add("c"); + } + } + + function resizeDomList(listDom, desiredLen, templateHtml) { + for (let i = listDom.childElementCount; i < desiredLen; i += 1) { + listDom.insertAdjacentHTML('beforeend', templateHtml); + } + while (desiredLen < listDom.childElementCount) { + listDom.removeChild(listDom.lastChild); + } + } + + function percent(a, b) { + return ((Number(a) / Number(b)) * 100).toFixed(1); + } + + function renderSource(sourceLocationIndex) { + const pathName = unwrapString(wasm_exports.sourceLocationPath(sourceLocationIndex)); + if (pathName.length === 0) return; + + const h2 = domSectSource.children[0]; + h2.innerText = pathName; + domSourceText.innerHTML = unwrapString(wasm_exports.sourceLocationFileHtml(sourceLocationIndex)); + + domSectSource.classList.remove("hidden"); + + // Empirically, Firefox needs this requestAnimationFrame in order for the scrollIntoView to work. + requestAnimationFrame(function() { + const slDom = document.getElementById("l" + sourceLocationIndex); + if (slDom != null) slDom.scrollIntoView({ + behavior: "smooth", + block: "center", + }); + }); + } + + function decodeString(ptr, len) { + if (len === 0) return ""; + return text_decoder.decode(new Uint8Array(wasm_exports.memory.buffer, ptr, len)); + } + + function unwrapInt32Array(bigint) { + const ptr = Number(bigint & 0xffffffffn); + const len = Number(bigint >> 32n); + if (len === 0) return new Uint32Array(); + return new Uint32Array(wasm_exports.memory.buffer, ptr, len); + } + + function setInputString(s) { + const jsArray = text_encoder.encode(s); + const len = jsArray.length; + const ptr = wasm_exports.set_input_string(len); + const wasmArray = new Uint8Array(wasm_exports.memory.buffer, ptr, len); + wasmArray.set(jsArray); + } + + function unwrapString(bigint) { + const ptr = Number(bigint & 0xffffffffn); + const len = Number(bigint >> 32n); + return decodeString(ptr, len); + } +})(); diff --git a/lib/fuzzer/web/main.zig b/lib/fuzzer/web/main.zig new file mode 100644 index 0000000000..342adc3b56 --- /dev/null +++ b/lib/fuzzer/web/main.zig @@ -0,0 +1,428 @@ +const std = @import("std"); +const assert = std.debug.assert; +const abi = std.Build.Fuzz.abi; +const gpa = std.heap.wasm_allocator; +const log = std.log; +const Coverage = std.debug.Coverage; +const Allocator = std.mem.Allocator; + +const Walk = @import("Walk"); +const Decl = Walk.Decl; +const html_render = @import("html_render"); + +const js = struct { + extern "js" fn log(ptr: [*]const u8, len: usize) void; + extern "js" fn panic(ptr: [*]const u8, len: usize) noreturn; + extern "js" fn emitSourceIndexChange() void; + extern "js" fn emitCoverageUpdate() void; + extern "js" fn emitEntryPointsUpdate() void; +}; + +pub const std_options: std.Options = .{ + .logFn = logFn, +}; + +pub fn panic(msg: []const u8, st: ?*std.builtin.StackTrace, addr: ?usize) noreturn { + _ = st; + _ = addr; + log.err("panic: {s}", .{msg}); + @trap(); +} + +fn logFn( + comptime message_level: log.Level, + comptime scope: @TypeOf(.enum_literal), + comptime format: []const u8, + args: anytype, +) void { + const level_txt = comptime message_level.asText(); + const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): "; + var buf: [500]u8 = undefined; + const line = std.fmt.bufPrint(&buf, level_txt ++ prefix2 ++ format, args) catch l: { + buf[buf.len - 3 ..][0..3].* = "...".*; + break :l &buf; + }; + js.log(line.ptr, line.len); +} + +export fn alloc(n: usize) [*]u8 { + const slice = gpa.alloc(u8, n) catch @panic("OOM"); + return slice.ptr; +} + +var message_buffer: std.ArrayListAlignedUnmanaged(u8, @alignOf(u64)) = .{}; + +/// Resizes the message buffer to be the correct length; returns the pointer to +/// the query string. +export fn message_begin(len: usize) [*]u8 { + message_buffer.resize(gpa, len) catch @panic("OOM"); + return message_buffer.items.ptr; +} + +export fn message_end() void { + const msg_bytes = message_buffer.items; + + const tag: abi.ToClientTag = @enumFromInt(msg_bytes[0]); + switch (tag) { + .source_index => return sourceIndexMessage(msg_bytes) catch @panic("OOM"), + .coverage_update => return coverageUpdateMessage(msg_bytes) catch @panic("OOM"), + .entry_points => return entryPointsMessage(msg_bytes) catch @panic("OOM"), + _ => unreachable, + } +} + +export fn unpack(tar_ptr: [*]u8, tar_len: usize) void { + const tar_bytes = tar_ptr[0..tar_len]; + log.debug("received {d} bytes of tar file", .{tar_bytes.len}); + + unpackInner(tar_bytes) catch |err| { + fatal("unable to unpack tar: {s}", .{@errorName(err)}); + }; +} + +/// Set by `set_input_string`. +var input_string: std.ArrayListUnmanaged(u8) = .{}; +var string_result: std.ArrayListUnmanaged(u8) = .{}; + +export fn set_input_string(len: usize) [*]u8 { + input_string.resize(gpa, len) catch @panic("OOM"); + return input_string.items.ptr; +} + +/// Looks up the root struct decl corresponding to a file by path. +/// Uses `input_string`. +export fn find_file_root() Decl.Index { + const file: Walk.File.Index = @enumFromInt(Walk.files.getIndex(input_string.items) orelse return .none); + return file.findRootDecl(); +} + +export fn decl_source_html(decl_index: Decl.Index) String { + const decl = decl_index.get(); + + string_result.clearRetainingCapacity(); + html_render.fileSourceHtml(decl.file, &string_result, decl.ast_node, .{}) catch |err| { + fatal("unable to render source: {s}", .{@errorName(err)}); + }; + 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 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 = @alignCast(@ptrCast(recent_coverage_update.items[0..@sizeOf(abi.CoverageUpdateHeader)])); + return header.n_runs; +} + +export fn uniqueRuns() u64 { + const header: *abi.CoverageUpdateHeader = @alignCast(@ptrCast(recent_coverage_update.items[0..@sizeOf(abi.CoverageUpdateHeader)])); + return header.unique_runs; +} + +const String = Slice(u8); + +fn Slice(T: type) type { + return packed struct(u64) { + ptr: u32, + len: u32, + + fn init(s: []const T) @This() { + return .{ + .ptr = @intFromPtr(s.ptr), + .len = s.len, + }; + } + }; +} + +fn unpackInner(tar_bytes: []u8) !void { + var fbs = std.io.fixedBufferStream(tar_bytes); + var file_name_buffer: [1024]u8 = undefined; + var link_name_buffer: [1024]u8 = undefined; + var it = std.tar.iterator(fbs.reader(), .{ + .file_name_buffer = &file_name_buffer, + .link_name_buffer = &link_name_buffer, + }); + while (try it.next()) |tar_file| { + switch (tar_file.kind) { + .file => { + if (tar_file.size == 0 and tar_file.name.len == 0) break; + if (std.mem.endsWith(u8, tar_file.name, ".zig")) { + log.debug("found file: '{s}'", .{tar_file.name}); + const file_name = try gpa.dupe(u8, tar_file.name); + if (std.mem.indexOfScalar(u8, file_name, '/')) |pkg_name_end| { + const pkg_name = file_name[0..pkg_name_end]; + const gop = try Walk.modules.getOrPut(gpa, pkg_name); + const file: Walk.File.Index = @enumFromInt(Walk.files.entries.len); + if (!gop.found_existing or + std.mem.eql(u8, file_name[pkg_name_end..], "/root.zig") or + std.mem.eql(u8, file_name[pkg_name_end + 1 .. file_name.len - ".zig".len], pkg_name)) + { + gop.value_ptr.* = file; + } + const file_bytes = tar_bytes[fbs.pos..][0..@intCast(tar_file.size)]; + assert(file == try Walk.add_file(file_name, file_bytes)); + } + } else { + log.warn("skipping: '{s}' - the tar creation should have done that", .{tar_file.name}); + } + }, + else => continue, + } + } +} + +fn fatal(comptime format: []const u8, args: anytype) noreturn { + var buf: [500]u8 = undefined; + const line = std.fmt.bufPrint(&buf, format, args) catch l: { + buf[buf.len - 3 ..][0..3].* = "...".*; + break :l &buf; + }; + js.panic(line.ptr, line.len); +} + +fn sourceIndexMessage(msg_bytes: []u8) error{OutOfMemory}!void { + const Header = abi.SourceIndexHeader; + const header: Header = @bitCast(msg_bytes[0..@sizeOf(Header)].*); + + const directories_start = @sizeOf(Header); + const directories_end = directories_start + header.directories_len * @sizeOf(Coverage.String); + const files_start = directories_end; + const files_end = files_start + header.files_len * @sizeOf(Coverage.File); + const source_locations_start = files_end; + const source_locations_end = source_locations_start + header.source_locations_len * @sizeOf(Coverage.SourceLocation); + const string_bytes = msg_bytes[source_locations_end..][0..header.string_bytes_len]; + + const directories: []const Coverage.String = @alignCast(std.mem.bytesAsSlice(Coverage.String, msg_bytes[directories_start..directories_end])); + const files: []const Coverage.File = @alignCast(std.mem.bytesAsSlice(Coverage.File, msg_bytes[files_start..files_end])); + const source_locations: []const Coverage.SourceLocation = @alignCast(std.mem.bytesAsSlice(Coverage.SourceLocation, msg_bytes[source_locations_start..source_locations_end])); + + try updateCoverage(directories, files, source_locations, string_bytes); + js.emitSourceIndexChange(); +} + +fn coverageUpdateMessage(msg_bytes: []u8) error{OutOfMemory}!void { + recent_coverage_update.clearRetainingCapacity(); + recent_coverage_update.appendSlice(gpa, msg_bytes) catch @panic("OOM"); + js.emitCoverageUpdate(); +} + +var entry_points: std.ArrayListUnmanaged(u32) = .{}; + +fn entryPointsMessage(msg_bytes: []u8) error{OutOfMemory}!void { + const header: abi.EntryPointHeader = @bitCast(msg_bytes[0..@sizeOf(abi.EntryPointHeader)].*); + entry_points.resize(gpa, header.flags.locs_len) catch @panic("OOM"); + @memcpy(entry_points.items, std.mem.bytesAsSlice(u32, msg_bytes[@sizeOf(abi.EntryPointHeader)..])); + js.emitEntryPointsUpdate(); +} + +export fn entryPoints() Slice(u32) { + return Slice(u32).init(entry_points.items); +} + +/// Index into `coverage_source_locations`. +const SourceLocationIndex = enum(u32) { + _, + + fn haveCoverage(sli: SourceLocationIndex) bool { + return @intFromEnum(sli) < coverage_source_locations.items.len; + } + + fn ptr(sli: SourceLocationIndex) *Coverage.SourceLocation { + return &coverage_source_locations.items[@intFromEnum(sli)]; + } + + fn sourceLocationLinkHtml( + sli: SourceLocationIndex, + out: *std.ArrayListUnmanaged(u8), + ) Allocator.Error!void { + const sl = sli.ptr(); + try out.writer(gpa).print("", .{@intFromEnum(sli)}); + try sli.appendPath(out); + try out.writer(gpa).print(":{d}:{d}", .{ sl.line, sl.column }); + } + + fn appendPath(sli: SourceLocationIndex, out: *std.ArrayListUnmanaged(u8)) Allocator.Error!void { + const sl = sli.ptr(); + const file = coverage.fileAt(sl.file); + const file_name = coverage.stringAt(file.basename); + const dir_name = coverage.stringAt(coverage.directories.keys()[file.directory_index]); + try html_render.appendEscaped(out, dir_name); + try out.appendSlice(gpa, "/"); + try html_render.appendEscaped(out, file_name); + } + + fn toWalkFile(sli: SourceLocationIndex) ?Walk.File.Index { + var buf: std.ArrayListUnmanaged(u8) = .{}; + defer buf.deinit(gpa); + sli.appendPath(&buf) catch @panic("OOM"); + return @enumFromInt(Walk.files.getIndex(buf.items) orelse return null); + } + + fn fileHtml( + sli: SourceLocationIndex, + out: *std.ArrayListUnmanaged(u8), + ) error{ OutOfMemory, SourceUnavailable }!void { + const walk_file_index = sli.toWalkFile() orelse return error.SourceUnavailable; + const root_node = walk_file_index.findRootDecl().get().ast_node; + var annotations: std.ArrayListUnmanaged(html_render.Annotation) = .{}; + defer annotations.deinit(gpa); + try computeSourceAnnotations(sli.ptr().file, walk_file_index, &annotations, coverage_source_locations.items); + html_render.fileSourceHtml(walk_file_index, out, root_node, .{ + .source_location_annotations = annotations.items, + }) catch |err| { + fatal("unable to render source: {s}", .{@errorName(err)}); + }; + } +}; + +fn computeSourceAnnotations( + cov_file_index: Coverage.File.Index, + walk_file_index: Walk.File.Index, + annotations: *std.ArrayListUnmanaged(html_render.Annotation), + source_locations: []const Coverage.SourceLocation, +) !void { + // Collect all the source locations from only this file into this array + // first, then sort by line, col, so that we can collect annotations with + // O(N) time complexity. + var locs: std.ArrayListUnmanaged(SourceLocationIndex) = .{}; + defer locs.deinit(gpa); + + for (source_locations, 0..) |sl, sli_usize| { + if (sl.file != cov_file_index) continue; + const sli: SourceLocationIndex = @enumFromInt(sli_usize); + try locs.append(gpa, sli); + } + + std.mem.sortUnstable(SourceLocationIndex, locs.items, {}, struct { + pub fn lessThan(context: void, lhs: SourceLocationIndex, rhs: SourceLocationIndex) bool { + _ = context; + const lhs_ptr = lhs.ptr(); + const rhs_ptr = rhs.ptr(); + if (lhs_ptr.line < rhs_ptr.line) return true; + if (lhs_ptr.line > rhs_ptr.line) return false; + return lhs_ptr.column < rhs_ptr.column; + } + }.lessThan); + + const source = walk_file_index.get_ast().source; + var line: usize = 1; + var column: usize = 1; + var next_loc_index: usize = 0; + for (source, 0..) |byte, offset| { + if (byte == '\n') { + line += 1; + column = 1; + } else { + column += 1; + } + while (true) { + if (next_loc_index >= locs.items.len) return; + const next_sli = locs.items[next_loc_index]; + const next_sl = next_sli.ptr(); + if (next_sl.line > line or (next_sl.line == line and next_sl.column >= column)) break; + try annotations.append(gpa, .{ + .file_byte_offset = offset, + .dom_id = @intFromEnum(next_sli), + }); + next_loc_index += 1; + } + } +} + +var coverage = Coverage.init; +/// Index of type `SourceLocationIndex`. +var coverage_source_locations: std.ArrayListUnmanaged(Coverage.SourceLocation) = .{}; +/// Contains the most recent coverage update message, unmodified. +var recent_coverage_update: std.ArrayListAlignedUnmanaged(u8, @alignOf(u64)) = .{}; + +fn updateCoverage( + directories: []const Coverage.String, + files: []const Coverage.File, + source_locations: []const Coverage.SourceLocation, + string_bytes: []const u8, +) !void { + coverage.directories.clearRetainingCapacity(); + coverage.files.clearRetainingCapacity(); + coverage.string_bytes.clearRetainingCapacity(); + coverage_source_locations.clearRetainingCapacity(); + + try coverage_source_locations.appendSlice(gpa, source_locations); + try coverage.string_bytes.appendSlice(gpa, string_bytes); + + try coverage.files.entries.resize(gpa, files.len); + @memcpy(coverage.files.entries.items(.key), files); + try coverage.files.reIndexContext(gpa, .{ .string_bytes = coverage.string_bytes.items }); + + try coverage.directories.entries.resize(gpa, directories.len); + @memcpy(coverage.directories.entries.items(.key), directories); + try coverage.directories.reIndexContext(gpa, .{ .string_bytes = coverage.string_bytes.items }); +} + +export fn sourceLocationLinkHtml(index: SourceLocationIndex) String { + string_result.clearRetainingCapacity(); + index.sourceLocationLinkHtml(&string_result) catch @panic("OOM"); + return String.init(string_result.items); +} + +/// Returns empty string if coverage metadata is not available for this source location. +export fn sourceLocationPath(sli: SourceLocationIndex) String { + string_result.clearRetainingCapacity(); + if (sli.haveCoverage()) sli.appendPath(&string_result) catch @panic("OOM"); + return String.init(string_result.items); +} + +export fn sourceLocationFileHtml(sli: SourceLocationIndex) String { + string_result.clearRetainingCapacity(); + sli.fileHtml(&string_result) catch |err| switch (err) { + error.OutOfMemory => @panic("OOM"), + error.SourceUnavailable => {}, + }; + return String.init(string_result.items); +} + +export fn sourceLocationFileCoveredList(sli_file: SourceLocationIndex) Slice(SourceLocationIndex) { + const global = struct { + var result: std.ArrayListUnmanaged(SourceLocationIndex) = .{}; + fn add(i: u32, want_file: Coverage.File.Index) void { + const src_loc_index: SourceLocationIndex = @enumFromInt(i); + if (src_loc_index.ptr().file == want_file) result.appendAssumeCapacity(src_loc_index); + } + }; + const want_file = sli_file.ptr().file; + global.result.clearRetainingCapacity(); + + // This code assumes 64-bit elements, which is incorrect if the executable + // being fuzzed is not a 64-bit CPU. It also assumes little-endian which + // can also be incorrect. + comptime assert(abi.CoverageUpdateHeader.trailing[0] == .pc_bits_usize); + const n_bitset_elems = (coverage_source_locations.items.len + @bitSizeOf(u64) - 1) / @bitSizeOf(u64); + const covered_bits = std.mem.bytesAsSlice( + u64, + recent_coverage_update.items[@sizeOf(abi.CoverageUpdateHeader)..][0 .. n_bitset_elems * @sizeOf(u64)], + ); + var sli: u32 = 0; + for (covered_bits) |elem| { + global.result.ensureUnusedCapacity(gpa, 64) catch @panic("OOM"); + for (0..@bitSizeOf(u64)) |i| { + if ((elem & (@as(u64, 1) << @intCast(i))) != 0) global.add(sli, want_file); + sli += 1; + } + } + return Slice(SourceLocationIndex).init(global.result.items); +} -- cgit v1.2.3