Skip to content

fuzzing smith API plus feedback #25281

Closed
Closed

Description

@andrewrk

When writing a fuzz test, a common, effective strategy is to create a generator (also known as "smith"). Such logic converts fuzz input, which is a linear slice of N bytes, into concrete test input.

Part 1: Standard Library Smith API

For example, we might want to consume an appropriate number of bytes from the fuzz input for an enum, giving equal weight to each tag:

const Fish = enum { one, two, red, blue };
const fish = smith.takeEnum(Fish);

This logically consumes 4 bits from the fuzzing input, although it might consume a full byte from the fuzzing input (see "open question: conserve bits" below). The important thing is that fish ends up being only one of the valid enum values.

Another important consideration is that some values might be more common in real world inputs than others. By expressing these likelihoods, a smith can help the fuzzer find interesting inputs much faster:

const fish = smith.takeEnumProb(Fish, .{ .one = 30, .two = 30, .red = 30, .blue = 10 });

This function would accept a usize for each possible value, and their relative probabilities would be against all the values added together as the denominator, which also must fit into a usize.

In addition to the standard library enhancement to add such an API, which should support a wide range of types beyond enums, such as integers, structs, packed structs, unions, etc.

Finally, the API needs a way to handle end of input (see "open question: how to handle end of input" below).

Part 2: Feedback

Observation: The logic it takes to create a smith is effectively a cheat sheet for the fuzzing input genetic algorithm.

For example, the fuzzer could take advantage of the probabilities listed above by preferring highly probable inputs at first to explore a codebase. Similarly, it delineates the opaque byte array into semantically meaningful segments. This range here is a 32-bit unsigned integer; this range here is an enum with 4 tags values 0-3, this value here is a 16-bit length followed by a string of that length.

Finally, knowing that X amount of input bytes were actually observed is useful to know that input length - X were dead and ignored.

So, part 2 here is having this API push data back to the fuzzing algorithm where it can be properly exploited.

Open question: how to handle end of input?

One possibility is having smith methods possibly return error.EndOfInput which the programmer then must handle higher up the call stack. Another possibility is having it treat fuzzing input as all zeroes, while setting a flag, which can be checked: smith.atEnd(). I think the latter sounds more convenient, and with feedback as noted above, the fuzzer can still learn which bits were treated as all zeroes.

Open question: conserve bits?

In the enum example above we see only 4 bits used, with 4 bits remaining in a byte. The smith API could try to conserve bits, thus leaving fewer dead bits in the fuzzing input. Generally, this is slower than tossing them and byte aligning, which is what std.Random does. However, avoiding useless unit test runs has the potential to be worth it.

There are some tricks available to a fuzzer not available to compression algorithms: the bits don't need to be consecutive; "leftover" bits could be accumulated in a usize that is only used for small bit grabs. Also the smith API could byte-align while informing the fuzzer about which bit ranges are dead.

It's probably worth experimenting with both ways and seeing what works better in practice.

Activity

  1. added this to the urgent milestone on Sep 19, 2025
  2. added
    enhancement
    standard library
    on Sep 19, 2025
  3. gooncreeper commented on Sep 19, 2025

    @gooncreeper
    Contributor

    Related to feedback: I think it could be very powerful if the smith called into the fuzzer implementation to get each value, that way a value can always be generated when needed while getting feedback for free. This would also reduce the number of 'bad' inputs the fuzzer gives and effectively replace the bytes approach and need for minimization.

    The above approach would also answer the end of input question: you would just ask the fuzzer for a bool (there possibly could be an ad hoc function to indicate to the fuzzer that the requested bool is end of stream).

    On another note, I think conserving bits would not be good since it makes mutating inputs harder.

  4. andrewrk commented on Sep 19, 2025

    @andrewrk
    MemberAuthor

    Keep in mind that you don't want to lose the power of having a linear array of bytes as inputs. Such thing can be minimized, used as a corpus, and used to easily reproduce a bug - without depending on libfuzzer to deterministically reproduce the failure. So, there should still be a canonical mapping of array of bytes to input, which has no dependency on libfuzzer.

  5. ProkopRandacek commented on Sep 22, 2025

    @ProkopRandacek
    Contributor

    conserve bits

    Since the fuzzer mutations are going to be operating on bytes, it would make sense to have the input values byte aligned.

    I imagine that if some fuzz test value is going to be constructed form cherry picked bits here and there in the byte stream, it is going to be very hard for the evolution algorithm to mutate this value effectively, since the bits that make it up are spread far apart.

  6. kristoff-it commented on Sep 24, 2025

    @kristoff-it
    Member

    This would also reduce the number of 'bad' inputs the fuzzer gives and effectively replace the bytes approach and need for minimization.

    I think an attempt at minimization would still be welcome, the generator could easily generate unnecessary data.

    As a concrete example, this is my current generator https://github.com/kristoff-it/superhtml/blob/main/src/generator/html.zig and it can generate something like <div id id id id id id id id id crashy_attribute>.

    What this api could help with is make sure that the edit removes one full operation from the input and not attempt some (likely meaningless) in between.

  7. andrewrk commented on Feb 25, 2026

    @andrewrk
    MemberAuthor
  8. modified the milestones: urgent, 0.16.0 on Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementfuzzingstandard library

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions