Write small typed stubs defining your API. STITCH++ automatically fuzzes valid combinations of endpoints, generates arguments, and finds crashes, exceptions, and failed checks.
Are you an agent? Read this instead:https://hgarrereyn.github.io/STITCHplusplus/agents/overview.raw.md
A STITCH++ spec
A STITCH++ spec is written in the target language and consists of endpoint stubs and object definitions. Each stub describes what inputs are required to invoke library functions and what outputs they produce. Specs can also contain generators for structured datatypes, such as custom text formats.
import kvlibimport stitchfrom stitch import Borrow, New, Str, Take
Store = stitch.object("Store")Snapshot = stitch.object("Snapshot")KvText = stitch.datatype("KvText")
@stitch.decoder(KvText)def kv_text(s): lines = [] while s.more(): key = s.token() if s.coin(0.3) else s.str(8) lines.append(f"{key}={s.str(12)}") return "\n".join(lines)
@stitch.endpointdef new_store() -> New[Store]: return kvlib.Store()
@stitch.endpoint(bails_on=(ValueError,))def load(text: Str[256, KvText]) -> New[Store]: return kvlib.load(text)
@stitch.endpoint(bails_on=(ValueError,))def put(st: Borrow[Store], key: Str[16], value: Str[32]): st.put(key, value)
@stitch.endpointdef remove(st: Borrow[Store], key: Str[16]): st.remove(key)
@stitch.endpoint(bails_on=(ValueError,))def snapshot(st: Borrow[Store]) -> New[Snapshot]: return kvlib.load(st.dump())
@stitch.endpointdef merge(st: Borrow[Store], snap: Take[Snapshot]): st.merge(snap)
@stitch.endpointdef roundtrip(st: Borrow[Store]): again = kvlib.load(st.dump()) stitch.check(dict(again.items()) == dict(st.items()), "dump/load roundtrip")#include <stitch.h>extern "C" {#include <kv.h>}
STITCH_OBJECT(Store, kv_store*);STITCH_DROP(Store, [](kv_store* st) { kv_free(st); });STITCH_OBJECT(Snapshot, kv_store*);STITCH_DROP(Snapshot, [](kv_store* st) { kv_free(st); });STITCH_DATATYPE(KvText);
STITCH_DECODER(KvText, [](stitch::Source& s) { std::string out; while (s.more()) out += (s.coin(0.3) ? s.token() : s.str(8)) + "=" + s.str(12) + "\n"; return out;});
New<Store> new_store() { return kv_new(); }STITCH_ENDPOINT(new_store);
New<Store> load(Str<256, KvText> text) { kv_store* st = kv_load(text.c_str()); stitch::require(st, "invalid text"); return st;}STITCH_ENDPOINT(load);
void put(Borrow<Store> st, Str<16> key, Str<32> value) { stitch::require(kv_put(st, key.c_str(), value.c_str()) == 0, "invalid key or value");}STITCH_ENDPOINT(put);
void remove_key(Borrow<Store> st, Str<16> key) { kv_remove(st, key.c_str()); }STITCH_ENDPOINT(remove_key);
New<Snapshot> snapshot(Borrow<Store> st) { char* text = kv_dump(st); kv_store* copy = kv_load(text); free(text); stitch::require(copy, "dump did not load"); return copy;}STITCH_ENDPOINT(snapshot);
void merge(Borrow<Store> st, Take<Snapshot> snap) { kv_merge(st, snap); kv_free(snap);}STITCH_ENDPOINT(merge);
void roundtrip(Borrow<Store> st) { char* text = kv_dump(st); kv_store* again = kv_load(text); free(text); stitch::require(again, "dump did not load"); size_t n = kv_len(again); kv_free(again); stitch::check(n == kv_len(st), "dump/load roundtrip");}STITCH_ENDPOINT(roundtrip);const stitch = require("stitch");const { Borrow, New, Str, Take } = stitch;const kv = require("kvjs");
const Store = stitch.object("Store");const Snapshot = stitch.object("Snapshot");const KvText = stitch.datatype("KvText");
stitch.decoder(KvText, (s) => { const lines = []; while (s.more()) lines.push(`${s.coin(0.3) ? s.token() : s.str(8)}=${s.str(12)}`); return lines.join("\n");});
stitch.endpoint("new_store", {}, New(Store), () => new kv.Store());
stitch.endpoint("load", { text: Str(256, KvText) }, New(Store), ({ text }) => kv.load(text), { bailsOn: [RangeError] });
stitch.endpoint("put", { st: Borrow(Store), key: Str(16), value: Str(32) }, null, ({ st, key, value }) => { st.put(key, value);}, { bailsOn: [RangeError] });
stitch.endpoint("remove", { st: Borrow(Store), key: Str(16) }, null, ({ st, key }) => st.remove(key));
stitch.endpoint("snapshot", { st: Borrow(Store) }, New(Snapshot), ({ st }) => kv.load(st.dump()), { bailsOn: [RangeError] });
stitch.endpoint("merge", { st: Borrow(Store), snap: Take(Snapshot) }, null, ({ st, snap }) => st.merge(snap));
stitch.endpoint("roundtrip", { st: Borrow(Store) }, null, ({ st }) => { const again = kv.load(st.dump()); const keys = (o) => JSON.stringify([...o].sort()); stitch.check(keys(Object.keys(again.data)) === keys(st.order), "dump/load roundtrip");});import demo.kv.Store;import stitch.*;
public class Stubs { @Decoder("KvText") public static String kvText(Source s) { StringBuilder b = new StringBuilder(); while (s.more()) b.append(s.coin(0.3) ? s.token() : s.str(8)).append('=').append(s.str(12)).append('\n'); return b.toString(); }
@Endpoint public static Store newStore() { return new Store(); }
@Endpoint(bailsOn = {IllegalArgumentException.class}) public static Store load(@Str(max = 256, type = "KvText") String text) { return Store.load(text); }
@Endpoint(bailsOn = {IllegalArgumentException.class}) public static void put(Store st, @Str(max = 16) String key, @Str(max = 32) String value) { st.put(key, value); }
@Endpoint public static void remove(Store st, @Str(max = 16) String key) { st.remove(key); }
@Endpoint(bailsOn = {IllegalArgumentException.class}) @Obj("Snapshot") public static Store snapshot(Store st) { return Store.load(st.dump()); }
@Endpoint public static void merge(Store st, @Take @Obj("Snapshot") Store snap) { st.merge(snap); }
@Endpoint public static void roundtrip(Store st) { Store again = Store.load(st.dump()); Stitch.check(again.size() == st.size(), "dump/load roundtrip"); }}package harness
import ( "example.com/kvgo" "stitch")
type Snapshot struct{ *kvgo.Store }
func init() { stitch.Object[*kvgo.Store]("Store", nil) stitch.Object[*Snapshot]("Snapshot", nil) stitch.Decoder("KvText", kvText) stitch.Endpoint("new_store", newStore) stitch.Endpoint("load", load, "text:max=256,type=KvText") stitch.Endpoint("put", put, "st", "key:max=16", "value:max=32") stitch.Endpoint("remove", remove, "st", "key:max=16") stitch.Endpoint("snapshot", snapshot, "st") stitch.Endpoint("merge", merge, "st", "snap:take") stitch.Endpoint("roundtrip", roundtrip, "st")}
func kvText(s *stitch.Source) string { out := "" for s.More() { k := s.Str(8) if s.Coin(0.3) { k = s.Token() } out += k + "=" + s.Str(12) + "\n" } return out}
func newStore() *kvgo.Store { return kvgo.New() }
func load(text string) (*kvgo.Store, error) { return kvgo.Load(text) }
func put(st *kvgo.Store, key, value string) error { return st.Put(key, value) }
func remove(st *kvgo.Store, key string) { st.Remove(key) }
func snapshot(st *kvgo.Store) (*Snapshot, error) { copy, err := kvgo.Load(st.Dump()) if err != nil { return nil, err } return &Snapshot{copy}, nil}
func merge(st *kvgo.Store, snap *Snapshot) error { return st.Merge(snap.Store) }
func roundtrip(st *kvgo.Store) { again, err := kvgo.Load(st.Dump()) stitch.Require(err == nil, "dump did not load") stitch.Check(again.Len() == st.Len(), "dump/load roundtrip")}use stitch::*;
object!(Store, kvrs::Store);object!(Snapshot, kvrs::Store);datatype!(KvText);
decoder!(KvText, fn kv_text(s: &mut Source) -> String { let mut out = String::new(); while s.more() { let k = if s.coin(0.3) { s.token() } else { s.str(8) }; out += &format!("{k}={}\n", s.str(12)); } out});
endpoint! { fn new_store() -> New<Store> { New::new(kvrs::Store::new()) }}
endpoint! { fn load(text: Str<256, KvText>) -> Result<New<Store>, String> { Ok(New::new(kvrs::load(&text)?)) }}
endpoint! { fn put(st: Borrow<Store>, key: Str<16>, value: Str<32>) -> Result<(), String> { st.put(&key, &value) }}
endpoint! { fn remove(st: Borrow<Store>, key: Str<16>) { st.remove(&key); }}
endpoint! { fn snapshot(st: Borrow<Store>) -> Result<New<Snapshot>, String> { Ok(New::new(kvrs::load(&st.dump())?)) }}
endpoint! { fn merge(st: Borrow<Store>, snap: Take<Snapshot>) -> Result<(), String> { st.merge(&snap.into_inner()) }}
endpoint! { fn roundtrip(st: Borrow<Store>) { let again = kvrs::load(&st.dump()); require(again.is_ok(), "dump did not load"); check(again.unwrap().len() == st.len(), "dump/load roundtrip"); }}require "stitch"require "kvrb"include Stitch::DSL
Store = Stitch.object("Store")Snapshot = Stitch.object("Snapshot")KvText = Stitch.datatype("KvText")
Stitch.decoder(KvText) do |s| lines = [] while s.more key = s.coin(0.3) ? s.token : s.str(8) lines << "#{key}=#{s.str(12)}" end lines.join("\n")end
Stitch.endpoint :new_store, {}, New[Store] do Kvrb::Store.newend
Stitch.endpoint :load, { text: Str[256, KvText] }, New[Store], bails_on: [ArgumentError] do |text| Kvrb.load(text)end
Stitch.endpoint :put, { st: Borrow[Store], key: Str[16], value: Str[32] }, bails_on: [ArgumentError] do |st, key, value| st.put(key, value)end
Stitch.endpoint :remove, { st: Borrow[Store], key: Str[16] } do |st, key| st.remove(key)end
Stitch.endpoint :snapshot, { st: Borrow[Store] }, New[Snapshot], bails_on: [ArgumentError] do |st| Kvrb.load(st.dump)end
Stitch.endpoint :merge, { st: Borrow[Store], snap: Take[Snapshot] } do |st, snap| st.merge(snap)end
Stitch.endpoint :roundtrip, { st: Borrow[Store] } do |st| again = Kvrb.load(st.dump) Stitch.check(again.items.to_h == st.items.to_h, "dump/load roundtrip")endimport KVSwiftimport Stitch
enum Store: StitchObject { typealias Handle = KVSwift.Store }enum Snapshot: StitchObject { typealias Handle = KVSwift.Store }
enum KvText: Decoded { static func decode(_ s: Source) -> String { var lines: [String] = [] while s.more() { let key = s.coin(0.3) ? s.token() : s.str(8) lines.append("\(key)=\(s.str(12))") } return lines.joined(separator: "\n") }}
let newStore = endpoint("new_store") { () -> New<Store> in New(KVSwift.Store())}
let load = endpoint("load") { (text: Str<256, KvText>) throws -> New<Store> in New(try KVSwift.load(text.value))}
let put = endpoint("put") { (st: Borrow<Store>, key: Str<16, Plain>, value: Str<32, Plain>) throws in try st.value.put(key.value, value.value)}
let remove = endpoint("remove") { (st: Borrow<Store>, key: Str<16, Plain>) in st.value.remove(key.value)}
let snapshot = endpoint("snapshot") { (st: Borrow<Store>) throws -> New<Snapshot> in New(try KVSwift.load(st.value.dump()))}
let merge = endpoint("merge") { (st: Borrow<Store>, snap: Take<Snapshot>) throws in try st.value.merge(snap.value)}
let roundtrip = endpoint("roundtrip") { (st: Borrow<Store>) throws in let again = try KVSwift.load(st.value.dump()) try check(Dictionary(again.items(), uniquingKeysWith: { a, _ in a }) == Dictionary(st.value.items(), uniquingKeysWith: { a, _ in a }), "dump/load roundtrip")}#import <Stitch/Stitch.h>#import "KVStore.h"
STITCH_OBJECT(Store, KVStore *);STITCH_OBJECT(Snapshot, KVStore *);
STITCH_DECODER(KvText) { NSMutableArray *lines = [NSMutableArray array]; while ([s more]) { NSString *key = [s coin:0.3] ? [s token] : [s str:8]; [lines addObject:[NSString stringWithFormat:@"%@=%@", key, [s str:12]]]; } return [lines componentsJoinedByString:@"\n"];}
STITCH_ENDPOINT(New(Store), new_store) { return [KVStore new];}
STITCH_ENDPOINT(New(Store), load, Str(256, KvText) text) { KVStore *st = [KVStore storeWithText:text error:NULL]; STRequire(st != nil, @"invalid text"); return st;}
STITCH_ENDPOINT(void, put, Borrow(Store) st, Str(16) key, Str(32) value) { STRequire([st putValue:value forKey:key error:NULL], @"invalid key or value");}
STITCH_ENDPOINT(void, remove, Borrow(Store) st, Str(16) key) { [st removeKey:key];}
STITCH_ENDPOINT(New(Snapshot), snapshot, Borrow(Store) st) { KVStore *copy = [KVStore storeWithText:[st dump] error:NULL]; STRequire(copy != nil, @"dump did not load"); return copy;}
STITCH_ENDPOINT(void, merge, Borrow(Store) st, Take(Snapshot) snap) { STRequire([st mergeStore:snap error:NULL], @"invalid entry");}
STITCH_ENDPOINT(void, roundtrip, Borrow(Store) st) { KVStore *again = [KVStore storeWithText:[st dump] error:NULL]; STCheck(again != nil && [[again dictionary] isEqual:[st dictionary]], @"dump/load roundtrip");}Programs
The fuzzer stitches together (millions of) different combinations of endpoints into graphs (left) and executes them dynamically. Each program graph is equivalent to a specific sequential program (right).
Example crash
When the fuzzer finds a crashing testcase, it minimizes it and generates an external reproducer.
- st = load("tmp=")
- remove(st, "tmp")
- snap = snapshot(st)
KeyError: 'tmp'
Show the stack trace
- Store.items__init__.py:26library
- Store.dump__init__.py:34library
- snapshotstubs.py:41spec
- auto st = load("");
- put(st, "tmp", "");
- remove_key(st, "tmp");
- auto snap = snapshot(st);
heap-use-after-free: READ of size 8
Show the stack trace
Where it crashed
- kv_dumpkv.c:77library
- snapshotstubs.cpp:37spec
8 frames in the runtime and system libraries
- void stitch::detail::Endpoint<&snapshot(stitch::Borrow<Store>)>::call<0ul>(stitch::detail::Ctx&, std::integer_sequence<unsigned long, 0ul>)stitch.h:904
- stitch::run_program(std::vector<stitch::PNode, std::allocator<stitch::PNode>> const&, bool)runtime.cpp:1368
- stitch_execruntime.cpp:1545
- stitch_run_fileruntime.cpp:1565
- mainmain_repro.cpp:99
- ?libc.so.6
- __libc_start_mainlibc.so.6
- _startrepro
Where the memory was freed
- freerepro
- remove_keystubs.cpp:33spec
8 frames in the runtime and system libraries
- void stitch::detail::Endpoint<&remove_key(stitch::Borrow<Store>, stitch::Str<16ul, void>)>::call<0ul, 1ul>(stitch::detail::Ctx&, std::integer_sequence<unsigned long, 0ul, 1ul>)stitch.h:902
- stitch::run_program(std::vector<stitch::PNode, std::allocator<stitch::PNode>> const&, bool)runtime.cpp:1368
- stitch_execruntime.cpp:1545
- stitch_run_fileruntime.cpp:1565
- mainmain_repro.cpp:99
- ?libc.so.6
- __libc_start_mainlibc.so.6
- _startrepro
Where it was allocated
- mallocrepro
- kv_putkv.c:35library
- putstubs.cpp:29spec
8 frames in the runtime and system libraries
- void stitch::detail::Endpoint<&put(stitch::Borrow<Store>, stitch::Str<16ul, void>, stitch::Str<32ul, void>)>::call<0ul, 1ul, 2ul>(stitch::detail::Ctx&, std::integer_sequence<unsigned long, 0ul, 1ul, 2ul>)stitch.h:902
- stitch::run_program(std::vector<stitch::PNode, std::allocator<stitch::PNode>> const&, bool)runtime.cpp:1368
- stitch_execruntime.cpp:1545
- stitch_run_fileruntime.cpp:1565
- mainmain_repro.cpp:99
- ?libc.so.6
- __libc_start_mainlibc.so.6
- _startrepro
- const st = load("tmp=");
- remove(st, "tmp");
- const snap = snapshot(st);
TypeError: Cannot read properties of undefined (reading 'trim')
Show the stack trace
- anonymous@index.jsindex.js:24library
- Store.dumpindex.js:24library
- stitch.endpoint.bailsOn [as fn]stubs.js:26spec
- var st = load("tmp=\n");
- remove(st, "tmp");
- roundtrip(st);
NullPointerException: Cannot invoke "String.trim()" because the return value of "java.util.Map.get(Object)" is null
Show the stack trace
- Store.dumpStore.java:30library
- Stubs.roundtripStubs.java:45spec
- st, _ := load("")
- put(st, "tmp", "")
- remove(st, "tmp")
- snap, _ := snapshot(st)
nil pointer dereference: runtime error: invalid memory address or nil pointer dereference
Show the stack trace
- kvgo.Store.Dumpkv.go:58library
- stitchharness.snapshotstubs.go:44spec
- let st = load("tmp=\n")?;
- remove(&st, "tmp");
- let snap = snapshot(&st)?;
called `Option::unwrap()` on a `None` value
Show the stack trace
- kvrs::Store::dump::{{closure}}lib.rs:41library
- kvrs::Store::dumplib.rs:41library
- stitch_harness::stubs::snapshotstubs.rs:42spec
- st = load("tmp=")
- remove(st, "tmp")
- snap = snapshot(st)
KeyError: key not found: "tmp"
Show the stack trace
- Hash#fetchkvrb.rb:27library
- block in Kvrb::Store#itemskvrb.rb:27library
- Array#mapkvrb.rb:27library
- Kvrb::Store#itemskvrb.rb:27library
- Kvrb::Store#dumpkvrb.rb:31library
- block in <top (required)>stubs.rb:35spec
- <main>stitch-rb:8
- let st = try load("tmp=")
- remove(st, "tmp")
- try roundtrip(st)
Unexpectedly found nil while unwrapping an Optional value
Show the stack trace
- Swift runtime failure: Unexpectedly found nil while unwrapping an Optional value<compiler-generated>
- closure #1 in Store.items()KV.swift:29library
- specialized Collection.map<A, B>(_:)<compiler-generated>
- Store.items()KV.swift:29library
- Store.dump()KV.swift:36library
- closure #1 in variable initialization expression of roundtripstubs.swift:43spec
15 frames in the runtime and system libraries
- closure #1 in endpoint<A>(_:file:_:)Endpoints.swift:98
- partial apply for closure #1 in endpoint<A>(_:file:_:)<compiler-generated>
- thunk for @escaping @callee_guaranteed (@unowned Call) -> (@error @owned Error)<compiler-generated>
- partial apply for thunk for @escaping @callee_guaranteed (@unowned Call) -> (@error @owned Error)<compiler-generated>
- closure #1 in static Runtime.setup()Stitch.swift:527
- void (anonymous namespace)::invoke<6>(stitch::detail::Ctx&)bridge.cpp:47
- stitch::run_program(std::vector<stitch::PNode, std::allocator<stitch::PNode>> const&, bool)runtime.cpp:1368
- stitch_execruntime.cpp:1545
- stitch_run_fileruntime.cpp:1565
- stitch_repro_main(int, char**)main_repro.cpp:99
- stitchMain(argNames:_:)Stitch.swift:595
- worker_mainmain.swift:7
- ?libc.so.6
- __libc_start_mainlibc.so.6
- _startworker
- KVStore *st = load(@"tmp=");
- remove(st, @"tmp");
- roundtrip(st);
NSInvalidArgumentException: Tried to init array with nil object
Show the stack trace
6 frames in the runtime and system libraries
- objc_exception_throwstitch_objc_throw.c:30
- -[NSException raise]NSException.m:1611
- +[NSException raise:format:arguments:]NSException.m:1490
- +[NSException raise:format:]NSException.m:1475
- -[GSInlineArray initWithObjects:count:]GSArray.m:419
- +[NSArray arrayWithObjects:count:]NSArray.m:277
- -[KVStore items]KVStore.m:65library
- -[KVStore dump]KVStore.m:81library
- stub_roundtripstubs.m:45spec
Install
pip install stitch-fuzzstitch setup --lang pythonRequires Docker. The library, the harness and the fuzzer run in containers.
FAQ
How do I use this?
Point your coding agent at this repo and tell it to fuzz your library :)
What kinds of things can I fuzz?
The main use case is fuzzing libraries that define an API (both large and small).
How is STITCH++ different from ___?
vs libFuzzer, AFL++, honggfuzz, ...
Those are fuzzing engines which require a harness. Each of them makes different choices about how to collect coverage, mutate, keep track of inputs, etc...
STITCH++ introduces both a fuzzable specification (which replaces a traditional fuzz harness) and a new fuzzing engine designed to support this specification.
The STITCH++ fuzzing engine is built with LibAFL (same authors as AFL++) and can still make use of modern fuzzing features, for example sanitizer coverage, compare-logging, forkserver, etc...
vs FUDGE, FuzzGen, PromptFuzz, PromeFuzz, CKGFuzzer, OGHarn, oss-fuzz-gen, ...
Lots of prior work explores the idea of automatically generating fuzz harnesses (both with and without LLMs).
STITCH++ instead creates a fuzzable specification, a replacement of (many) traditional fuzz harnesses.
vs STITCH
Work on stitching started in early 2025 and the original tool (which we describe in our paper) was built in a LLM-pipeline world, not yet an agent-powered world.
Many of the design choices we made were necessary at the time for weaker models, and are no longer necessary.
STITCH++ is an agent-forward rebuilt version of STITCH, where we retain only the fuzzing core and we extend support to many more languages.
vs GraphFuzz
I'm also the author of GraphFuzz :)
In a sense, stitching is the LLM-powered successor. It makes possible what used to be very tedious to do by hand.
The key innovation was introducing extrinsic type-state allowing the spec to model open-ended constraints. There are also a lot of engineering improvements with STITCH++.
How expensive is it?
The code is free and open-source; the only cost is writing the specification (most likely using an agent), compute to run the fuzzer, and any costs associated with triaging bugs (also probably an agent).
Note that once the spec is created, the fuzzing itself is purely computational, no LLM usage.
Will it find false positives?
Yes, sort of, but we expect this to improve:
All the bug reports contain real runnable code, so there is no hallucination risk. The only risk is that a bug may be misusing the target API.
You could also use STITCH++ bug reports as input to some other kind of triage system.
The fuzzing engine accurately respects the specification. So if your specification perfectly defines the expected way to use the API, you will be fine.
In practice, for any non-trivial API, it's basically impossible to produce an accurate specification (let alone understand what the API should do in certain edge cases), even for an expert human.
In practice, what we observed with STITCH (the predecessor) is that most API-misuse bugs are "obvious in retrospect" — that is, while it may be hard to spot a slightly broken specification ahead of time, it is generally very obvious if a specific testcase is abusing the API in a way that is clearly unintended. Thus, after seeing such a testcase, one can go back and fix the specification to avoid it.
We expect specification development to undergo this sort of refinement loop via dynamically testing the specification. In practice, agents are already really good at this and we expect them to continue to get better.