Skip to content

fuzz: prevent pcs slice panic and null-base memcpy; ensure capacity before first append#24869

Closed
ghost wants to merge 2 commits into
masterfrom
unknown repository
Closed

fuzz: prevent pcs slice panic and null-base memcpy; ensure capacity before first append#24869
ghost wants to merge 2 commits into
masterfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Aug 15, 2025

Copy link
Copy Markdown

Fixes two crash paths in the new fuzz runner:

std/Build.Fuzz.addEntryPoint: guard pcs.len == 0 (avoid pcs[1..] slice panic) and make logs bounds-safe.

lib/fuzzer.zig:

appendSliceAssumeCapacity: early-return on zero-length and copy into [old_len .. old_len + items.len] to avoid forming a slice off a null base.

start(): ensure capacity before the first append when the corpus is empty (Web UI/coverage path triggers this).

minimal reproduction using zig master: zig init

// src/main.zig
const std = @import("std");
test "fuzz minimal" {
const Ctx = struct { fn testOne(_: @this(), input: []const u8) !void { _ = input; } };
try std.testing.fuzz(Ctx{}, Ctx.testOne, .{});
}

Run: zig build test --fuzz --webui=[::1]:45891.

Before: either a pcs slice panic or a segfault at the fuzzer memcpy (null-base slice).
After: fuzzing runs indefinitely (Ctrl-C to stop).

Note: the ensureTotalCapacity(len) is defensive; a higher-level invariant likely intended the assume-capacity precondition to hold. A TODO comment is left to flag follow-up.

jwstolk added 2 commits August 15, 2025 22:44
…efore first append

- std/Build.Fuzz.addEntryPoint: guard pcs.len==0 and make logs bounds-safe.
- lib/fuzzer.zig:
  - appendSliceAssumeCapacity: early-return on zero-length; copy to [old_len .. old_len+len].
  - start(): ensureTotalCapacity before first append when corpus is empty (TODO: revisit invariant).
@ifreund

ifreund commented Aug 24, 2025

Copy link
Copy Markdown
Member

This isn't the correct fix or even in the correct direction, see #23423 for context.

@ifreund ifreund closed this Aug 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant