1. The library
This tutorial fuzzes a small key-value store. The store has the same API in every language. Pick your language above; the choice applies to every page of the tutorial.
The store can:
- create an empty store
puta key and a value (keys and values are single lines, keys are not empty)getandremovea keydumpthe store as text, onekey=valueline per entryloadthat text back into a store
It also has a few planted bugs. You don’t need to know where they are: finding them is the fuzzer’s job.
The API
Section titled “The API”import kvlib
st = kvlib.Store()st.put("a", "1") # ValueError for an empty key or a newlinest.get("a") # "1"st.remove("a")text = st.dump() # "a=1\nb=2"st = kvlib.load(text) # ValueError for a line without '='#include <kv.h>
kv_store *st = kv_new();kv_put(st, "a", "1"); // -1 for an empty key or a newlinekv_get(st, "a"); // "1"kv_remove(st, "a");char *text = kv_dump(st); // "a=1\nb=2\n"; the caller frees itkv_store *b = kv_load(text); // NULL for a line without '='kv_free(st);Source: examples/cpp-demo/lib/kv.c
const kv = require("kvjs");
const st = new kv.Store();st.put("a", "1"); // RangeError for an empty key or a newlinest.data.a; // "1"st.remove("a");const text = st.dump(); // "a=1\nb=2"const again = kv.load(text); // RangeError for a line without '='kv.setPath(st.data, "x.y", "1"); // nested assignmentimport demo.kv.Store;
Store st = new Store();st.put("a", "1"); // IllegalArgumentException for an empty key or a newlinest.remove("a");String text = st.dump(); // "a=1\nb=2\n"st = Store.load(text); // IllegalArgumentException for a line without '='st.size();import "example.com/kvgo"
st := kvgo.New()err := st.Put("a", "1") // an error for an empty key or a newlinest.Remove("a")text := st.Dump() // "a=1\nb=2\n"st, err = kvgo.Load(text) // an error for a line without '='st.Len()Source: examples/go-demo/lib/kv.go
let mut st = kvrs::Store::new();st.put("a", "1")?; // Err for an empty key or a newlinest.remove("a");let text = st.dump(); // "a=1\nb=2\n"let st = kvrs::load(&text)?; // Err for a line without '='st.len();st.byte_at("a", 0); // byte 0 of the value of "a"require "kvrb"
st = Kvrb::Store.newst.put("a", "1") # ArgumentError for an empty key or a newlinest.get("a") # "1"st.remove("a")text = st.dump # "a=1\nb=2"st = Kvrb.load(text) # ArgumentError for a line without '='import KVSwift
let st = Store()try st.put("a", "1") // throws for an empty key or a newlinest.get("a") // "1"st.remove("a")let text = st.dump() // "a=1\nb=2"let again = try load(text) // throws for a line without '='st.byteAt("a", 0) // byte 0 of the value of "a"#import "KVStore.h"
KVStore *st = [KVStore new];[st putValue:@"1" forKey:@"a" error:&err]; // NO for an empty key or a newline[st stringForKey:@"a"]; // @"1"[st removeKey:@"a"];NSString *text = [st dump]; // @"a=1\nb=2"st = [KVStore storeWithText:text error:&err]; // nil for a line without '='[st byteForKey:@"a" atIndex:0]; // byte 0 of the value of "a"Source: examples/objc-demo/lib/KVStore.m
Get the code
Section titled “Get the code”git clone https://github.com/hgarrereyn/STITCHpluspluscd STITCHplusplus/examplesEach language has a folder examples/<language>-demo/. Its lib/ folder holds the store, and its
harness/ folder holds the finished harness. This tutorial writes the harness again from scratch
in a new project, so you can compare your result with the finished one.