5. Checks and data types
A check
Section titled “A check”So far the fuzzer only finds crashes. A check also finds wrong results. Pick a property that
holds for every store: loading the output of dump gives the same entries.
@stitch.endpointdef roundtrip(st: Borrow[Store]): again = kvlib.load(st.dump()) stitch.check(dict(again.items()) == dict(st.items()), "dump/load roundtrip")roundtrip does not list ValueError in bails_on: load must accept what dump writes, so an
exception here is a bug too.
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);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");});roundtrip has no bailsOn: load must accept what dump writes, so an exception here is a bug
too.
@Endpointpublic static void roundtrip(Store st) { Store again = Store.load(st.dump()); Stitch.check(again.size() == st.size(), "dump/load roundtrip");}roundtrip has no bailsOn: load must accept what dump writes, so an exception here is a bug
too.
stitch.Endpoint("roundtrip", roundtrip, "st") // in init()
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")}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"); }}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")endroundtrip has no bails_on: load must accept what dump writes, so an exception here is a bug
too.
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")}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");}A data type for load
Section titled “A data type for load”load gets random text, so it rejects much of it. A data type tells the fuzzer what a string
parameter should look like. Tag the parameter with it:
KvText = stitch.datatype("KvText")
@stitch.endpoint(bails_on=(ValueError,))def load(text: Str[256, KvText]) -> New[Store]: return kvlib.load(text)STITCH_DATATYPE(KvText);
New<Store> load(Str<256, KvText> text) { ... }const KvText = stitch.datatype("KvText");
stitch.endpoint("load", { text: Str(256, KvText) }, New(Store), ({ text }) => kv.load(text), { bailsOn: [RangeError] });@Endpoint(bailsOn = {IllegalArgumentException.class})public static Store load(@Str(max = 256, type = "KvText") String text) { return Store.load(text);}stitch.Endpoint("load", kvgo.Load, "text:max=256,type=KvText")datatype!(KvText);
endpoint! { fn load(text: Str<256, KvText>) -> Result<New<Store>, String> { Ok(New::new(kvrs::load(&text)?)) }}KvText = Stitch.datatype("KvText")
Stitch.endpoint :load, { text: Str[256, KvText] }, New[Store], bails_on: [ArgumentError] do |text| Kvrb.load(text)endlet load = endpoint("load") { (text: Str<256, KvText>) throws -> New<Store> in New(try KVSwift.load(text.value))}In Swift, the data type is the KvText enum with a decoder, below.
STITCH_ENDPOINT(New(Store), load, Str(256, KvText) text) { ... }Then give the data type something to start from. Create harness/data/KvText/ with a sample
input and a dictionary:
harness/data/KvText/ seeds/s1.txt a=1 b=2 kv.dict tmp "#!"Seeds are sample inputs, one per file. Copy them from the library’s tests when it has some. The dictionary holds tokens the fuzzer mixes into inputs, one per line.
A decoder
Section titled “A decoder”A decoder builds a value from a stream of fuzzer choices. Mutations change the choices, so
every value it returns has the right shape: here, key=value lines.
@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_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;});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");});@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();}stitch.Decoder("KvText", kvText) // in init()
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}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});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")endenum 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") }}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"];}s.more()decides how many lines there are. The fuzzer can add and remove lines.s.token()draws a token from the dictionary, so keys liketmpshow up often.s.str(8)draws text of at most 8 bytes.
s.more()decides how many lines there are. The fuzzer can add and remove lines.s.token()draws a token from the dictionary, so keys liketmpshow up often.s.str(8)draws text of at most 8 bytes.
s.more()decides how many lines there are. The fuzzer can add and remove lines.s.token()draws a token from the dictionary, so keys liketmpshow up often.s.str(8)draws text of at most 8 bytes.
s.more()decides how many lines there are. The fuzzer can add and remove lines.s.token()draws a token from the dictionary, so keys liketmpshow up often.s.str(8)draws text of at most 8 bytes.
s.More()decides how many lines there are. The fuzzer can add and remove lines.s.Token()draws a token from the dictionary, so keys liketmpshow up often.s.Str(8)draws text of at most 8 bytes.
s.more()decides how many lines there are. The fuzzer can add and remove lines.s.token()draws a token from the dictionary, so keys liketmpshow up often.s.str(8)draws text of at most 8 bytes.
s.moredecides how many lines there are. The fuzzer can add and remove lines.s.tokendraws a token from the dictionary, so keys liketmpshow up often.s.str(8)draws text of at most 8 bytes.
s.more()decides how many lines there are. The fuzzer can add and remove lines.s.token()draws a token from the dictionary, so keys liketmpshow up often.s.str(8)draws text of at most 8 bytes.
[s more]decides how many lines there are. The fuzzer can add and remove lines.[s token]draws a token from the dictionary, so keys liketmpshow up often.[s str:8]draws text of at most 8 bytes.
See what it makes:
$ stitch sample KvText -n 5"=V\n#!=\ufffd_a7q\u98cc\ufffd\ufffd\ufffdQ\n)+6P4R70PR4~9=#j{\ne^S/?P=]L3P C_(""""""erc*k:n7=TrH-v%?v{!.3""QTT=WNy+s'}QE{0;\njyKdP4n=kF<T=\nc\ufffd\ufffd\u0014T\ufffd\u0016e=h2!AkI[4E\ngxF%8ers=\ufffd e\f\n\ufffd\ufffd\ufffd\u0000\u0002h=\u0007=~T"$ stitch sample KvText -n 5"""6./Z= +jG<P<MZ\"m$\n""Z-PQG97d=\\KU'Arc,8]l0\ntmp=\ufffd\u052f\u0013\u44a8\ufffd\ufffd\u0001%\ufffd\n\ufffd8\ufffd=RA-X tFKd4d\nfq0YvKAu=VD\ntmp=J$^.te45Q@l7\n""""N:[@F4\"p=^x`<4,HZ~JK\n6R=\ufffd\bT_?Z\u00ccu\ufffd\ufffd-\n\ufffd\ufffdk3\ufffd\ufffd\\G=#!\ufffd[\u000b\ufffdx\n"$ stitch sample KvText -n 5"""""w@\\}s\"j8==+|4/jkDs;k>""\u007f\ufffd\ufffd\ufffd\ufffd-\ufffd\ufffd&\ufffd\u0014\ufffdw\ufffd\u007f\ufffd>\u0006\ufffd\ufffd\ufffd\u0013\ufffd!2\ufffdZ^\ufffd\ufffd\ufffd\ufffdWe\ufffdw\ufffd\ufffd\ufffd>\ufffd\ufffd_\ufffd\ufffdZD\ufffd\ufffd\ufffd\ufffd\ufffd\f\ufffd=\ufffdiuKLv ""\ufffdc\u0015\ufffd\u03b8\ufffd\ufffdc\u0005\u001a<\u0015V\ufffd\"=\ufffd+xE#x"$ stitch sample KvText -n 5"""""w@\\}s\"j8==+|4/jkDs;k>\n""\u007f\ufffd\ufffd\ufffd\ufffd-\ufffd\ufffd&\ufffd\u0014\ufffdw\ufffd\u007f\ufffd>\u0006\ufffd\ufffd\ufffd\u0013\ufffd!2\ufffdZ^\ufffd\ufffd\ufffd\ufffdWe\ufffdw\ufffd\ufffd\ufffd>\ufffd\ufffd_\ufffd\ufffdZD\ufffd\ufffd\ufffd\ufffd\ufffd\f\ufffd=\ufffdiuKLv \n""\ufffdc\u0015\ufffd\u03b8\ufffd\ufffdc\u0005\u001a<\u0015V\ufffd\"=\ufffd+xE#x\n"$ stitch sample KvText -n 5"9jLBi=\ufffd\ufffd\ufffd2\n\ufffdl\u0003\ufffd\ufffd!\ufffd\ufffd=E\nYb'1lAB_=uRMlD.\nKK\u02d9\ufffd'?O\ufffd-\ufffd^\ufffd\ufffd\ufffd\u001a\ufffd\u001f\ufffdgT\ufffd\ufffdtY=Qa!EVN?cwoZh\n'7u<::$6=e}>ci_p\n""yRU3\"\\R$=Kvu>u]1k0KRN\nhr{53JZ7=5v~U==\"Be[=\n\u001bo\ufffd\ufffd\u00f2=OmV96si,Sec\n4#3j'74Lw]X:0*RXR=K%?,TZ$fZmXK#n7S{/q5ZgKRFwn/5#E2=No65th=CAoOg\"bd9!\n\ufffd\u0015\ufffd=m9V\"L\n$?}k.$\u0005\u0017=Y\ufffd\u0012\u0013%b\ufffdxz8\ufffd=\n/\ufffd\ufffd\ufffdq\u0014\ufffd\u0002\ufffd\u001d\ufffd\ufffd\ufffdQ\ufffd=4YM4\\\n\ufffde\n\ufffd\ufffd\\%\ufffd=5\n\u0015\ufffd\u0015\ufffd\ufffd=FI3tszo,\n@4=\ufffd\n')T4\\UwY=%R!cSySnuBQY\n""""\u02f2\ufffd=-\ufffd\n_XO>o<Mu=8!az>-QQ3Q} \n_\ufffd\ufffdZD\ufffd\ufffd\ufffd=$X;7bmn7t`\n"""$ stitch sample KvText -n 5"tmp=ir,\nao_lg::=VktShDfoER5H\n""\ufffd\u007f=nlXLh\ufffdb\ufffd\u001b\ufffd\ufffd\ufffd\nvu>u]1k0=\ufffd\ufffdo\ufffdc\n=0p,5v~U==\"Be\n""""\u007f\ufffd\ufffd\ufffd\ufffd-\ufffd\ufffd&\ufffd\u0014\ufffdw\ufffd\u007f\ufffd>\u0006\ufffd\ufffd\ufffd\u0013\ufffd!2\ufffdZ^\ufffd\ufffd\ufffd\ufffdWe\ufffdw\ufffd\ufffd\ufffd>\ufffd\ufffd_\ufffd\ufffdZD\ufffd\ufffd\ufffd\ufffd\ufffd\f\ufffd=\ufffdiuKLv \n\ufffd\u001d\ufffd\u000f\ufffd\u0011\ufffd\ufffd=\\3kgC`^\n"""$ stitch sample KvText -n 5"=)\n#!=\ufffdt\ufffd\ufffd\ufffd\ufffd\\\ufffd\ufffdpF\ufffd\n8q1A ax!2Wp`%=<y\nEew!Wb#7=L[Zp>}-""""""GE=&n(Fsx6J/2b\\\n4>VH-J88=^0&C4Aq0\nad*ZDNdV=$LZQ,""LO,=*IQ0FOsywD+;\ny$(s-k7=kAF\"B\n=*B\ufffdIx~ =JU]d%I=HE\n{n-k[LJ<=@`\ufffd\ufffd\nc;\ufffd\bj\ufffdis=.O"$ stitch sample KvText -n 5"""6./Z= +jG<P<MZ\"m$""Z-PQG97d=\\KU'Arc,8]l0\ntmp=\ufffd\u052f\u0013\u44a8\ufffd\ufffd\u0001%\ufffd\n\ufffd8\ufffd=RA-X tFKd4d\nfq0YvKAu=VD\ntmp=J$^.te45Q@l7""""N:[@F4\"p=^x`<4,HZ~JK\n6R=\ufffd\bT_?Z\u00ccu\ufffd\ufffd-\n\ufffd\ufffdk3\ufffd\ufffd\\G=#!\ufffd[\u000b\ufffdx"$ stitch sample KvText -n 5"""6./Z= +jG<P<MZ\"m$""Z-PQG97d=\\KU'Arc,8]l0\ntmp=\ufffd\u052f\u0013\u44a8\ufffd\ufffd\u0001%\ufffd\n\ufffd8\ufffd=RA-X tFKd4d\nfq0YvKAu=VD\ntmp=J$^.te45Q@l7""""N:[@F4\"p=^x`<4,HZ~JK\n6R=\ufffd\bT_?Z\u00ccu\ufffd\ufffd-\n\ufffd\ufffdk3\ufffd\ufffd\\G=#!\ufffd[\u000b\ufffdx"The keys and values are arbitrary, but every line has an =. (stitch sample prints each value
as a JSON string.)
Probe again
Section titled “Probe again”$ stitch probe --secs 30[stitch] probing for 30s of fuzzing (plus the harness build and worker start-up)...probe: 30s, 215k execs (7.2k/s), 64 edges, corpus 20, calls bailing 11% (programs with a bail 52%), 25 crash/timeout programs savedcrashes found: run `stitch crashes probe`$ stitch probe --secs 30[stitch] probing for 30s of fuzzing (plus the harness build and worker start-up)...probe: 30s, 75.6k execs (2.5k/s), 180/801 edges, corpus 145, calls bailing 12% (programs with a bail 46%), 15 crash/timeout programs savedcrashes found: run `stitch crashes probe`$ stitch probe --secs 30[stitch] probing for 30s of fuzzing (plus the harness build and worker start-up)...probe: 30s, 103k execs (3.4k/s), 32 edges, corpus 40, calls bailing 15% (programs with a bail 53%), 25 crash/timeout programs savedcrashes found: run `stitch crashes probe`$ stitch probe --secs 30[stitch] probing for 30s of fuzzing (plus the harness build and worker start-up)...probe: 30s, 378k execs (12.6k/s), 57 edges, corpus 46, calls bailing 12% (programs with a bail 51%), 25 crash/timeout programs saveddepends on where an input came from (the stub that made or last used that object; the fuzzer learns these pairs. Act only when the low one should work: a different kind of object behind one type gets its own type): put(st): ok 43% when st last came from roundtrip, 98% when from loadcrashes found: run `stitch crashes probe`$ stitch probe --secs 30[stitch] probing for 30s of fuzzing (plus the harness build and worker start-up)...probe: 30s, 351k execs (11.7k/s), 43/83 edges, corpus 63, calls bailing 17% (programs with a bail 47%), 18 crash/timeout programs savedcrashes found: run `stitch crashes probe`$ stitch probe --secs 30[stitch] probing for 30s of fuzzing (plus the harness build and worker start-up)...probe: 30s, 92.9k execs (3.1k/s), 459/1948 edges, corpus 238, calls bailing 11% (programs with a bail 41%), 16 crash/timeout programs saveddepends on where an input came from (the stub that made or last used that object; the fuzzer learns these pairs. Act only when the low one should work: a different kind of object behind one type gets its own type): put(st): ok 38% when st last came from put, 96% when from loadcrashes found: run `stitch crashes probe`$ stitch probe --secs 30[stitch] probing for 30s of fuzzing (plus the harness build and worker start-up)...probe: 30s, 122k execs (4.1k/s), 31 edges, corpus 34, calls bailing 19% (programs with a bail 53%), 25 crash/timeout programs saveddepends on where an input came from (the stub that made or last used that object; the fuzzer learns these pairs. Act only when the low one should work: a different kind of object behind one type gets its own type): put(st): ok 32% when st last came from new_store, 96% when from roundtripcrashes found: run `stitch crashes probe`$ stitch probe --secs 30[stitch] probing for 30s of fuzzing (plus the harness build and worker start-up)...probe: 30s, 16.6k execs (553/s), 378/6457 edges, corpus 123, calls bailing 8% (programs with a bail 31%), 15 crash/timeout programs saveddepends on where an input came from (the stub that made or last used that object; the fuzzer learns these pairs. Act only when the low one should work: a different kind of object behind one type gets its own type): roundtrip(st): ok 41% when st last came from remove, 100% when from loadcrashes found: run `stitch crashes probe`$ stitch probe --secs 30[stitch] probing for 30s of fuzzing (plus the harness build and worker start-up)...probe: 30s, 78.0k execs (2.6k/s), 43/76 edges, corpus 63, calls bailing 13% (programs with a bail 46%), 20 crash/timeout programs saveddepends on where an input came from (the stub that made or last used that object; the fuzzer learns these pairs. Act only when the low one should work: a different kind of object behind one type gets its own type): put(st): ok 16% when st last came from roundtrip, 97% when from removecrashes found: run `stitch crashes probe`Compared with the first probe:
| first probe | second probe | |
|---|---|---|
| calls bailing | 18% | 11% |
load bail rate |
34% | 22% |
| edges | 43 | 64 |
| crash programs saved | 5 | 25 |
| first probe | second probe | |
|---|---|---|
| calls bailing | 19% | 12% |
load bail rate |
51% | 17% |
| edges | 135 | 180 |
| crash programs saved | 5 | 15 |
| first probe | second probe | |
|---|---|---|
| calls bailing | 14% | 15% |
load bail rate |
41% | 49% |
| edges | 24 | 32 |
| crash programs saved | 5 | 25 |
| first probe | second probe | |
|---|---|---|
| calls bailing | 26% | 12% |
load bail rate |
52% | 24% |
| edges | 36 | 57 |
| crash programs saved | 5 | 25 |
| first probe | second probe | |
|---|---|---|
| calls bailing | 30% | 17% |
load bail rate |
70% | 37% |
| edges | 28 | 43 |
| crash programs saved | 5 | 18 |
| first probe | second probe | |
|---|---|---|
| calls bailing | 10% | 11% |
load bail rate |
37% | 22% |
| edges | 406 | 459 |
| crash programs saved | 5 | 16 |
| first probe | second probe | |
|---|---|---|
| calls bailing | 22% | 19% |
load bail rate |
37% | 34% |
| edges | 22 | 31 |
| crash programs saved | 5 | 25 |
| first probe | second probe | |
|---|---|---|
| calls bailing | 22% | 8% |
load bail rate |
61% | 19% |
| edges | 248 | 378 |
| crash programs saved | 5 | 15 |
| first probe | second probe | |
|---|---|---|
| calls bailing | 20% | 13% |
load bail rate |
57% | 24% |
| edges | 26 | 43 |
| crash programs saved | 5 | 20 |
The second probe reaches more of the library (more edges): load now makes stores with entries
for the other stubs to work on. Next, run a longer campaign and look at the bugs it finds.