difffuzz

package
v1.61.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package difffuzz is a differential fuzzer of ygo against yjs: a seeded scenario of several peers editing, undoing and exchanging updates is replayed through ygo's public API (Run) and through yjs by a Node worker (testdata/gen/difffuzz-oracle.mjs), and the two traces are compared step by step (Compare): every update event byte for byte, the bytes a sync, merge or diff sends, and each touched peer's state, state vector, pending queue, view and undo stacks.

The scenario is data, so both sides replay the same one: positions and lengths are hints reduced against the target's current length at run time, nested targets are paths resolved at run time, and an op whose path no longer resolves does nothing on either side.

Index

Constants

View Source
const (
	StepTxn     = "txn"     // Ops on peer P in one transaction
	StepUndo    = "undo"    // peer P's UndoManager
	StepRedo    = "redo"    //
	StepStop    = "stop"    // stopCapturing
	StepUClear  = "uclear"  // UndoManager clear
	StepDeliver = "deliver" // From's update events not yet delivered to To, by Order
	StepSync    = "sync"    // To applies From's state diffed against To's state vector
	StepMerge   = "merge"   // To applies the merge of From's undelivered events
	StepDiff    = "diff"    // To applies that merge diffed against To's state vector
	StepFinal   = "final"   // every peer syncs with every other until nothing changes
	StepUnwind  = "unwind"  // after final: each undo peer undoes until nothing changes, then redoes
)

Step kinds.

View Source
const (
	OrderIn   = "in"
	OrderRev  = "rev"
	OrderShuf = "shuf" // mulberry32(R) Fisher-Yates
	OrderDup  = "dup"  // each event twice, in order
	OrderPart = "part" // shuffled as shuf, then the first 1 + N % len
	OrderLate = "late" // all but the earliest, in order (it comes later)
)

Delivery orders.

View Source
const (
	WantText    = "text"
	WantArray   = "array"
	WantMap     = "map"
	WantXml     = "xml"   // an XML fragment or element (children)
	WantXmlEl   = "xmlel" // an XML element (attributes)
	WantXmlText = "xmltext"
)

Target kinds an op wants at the end of its path.

View Source
const (
	OpIns     = "ins"     // text: insert S (with Attrs if given); array: insert V's elements
	OpDel     = "del"     // text, array, xml: delete from Pos, Len
	OpFmt     = "fmt"     // text: format Pos, Len with Attrs
	OpEmb     = "emb"     // text: insert the embed V (with Attrs if given)
	OpDelta   = "delta"   // text: apply Delta
	OpPush    = "push"    // array: push V's elements
	OpInsMap  = "insmap"  // array: insert a nested map at Pos
	OpInsArr  = "insarr"  //
	OpInsText = "instext" // array: nested text; xml: an XmlText child
	OpSet     = "set"     // map: set Key to V
	OpMapDel  = "mdel"    // map: delete Key
	OpSetMap  = "setmap"  // map: nested map at Key
	OpSetArr  = "setarr"  //
	OpSetText = "settext" //
	OpClear   = "clear"   // map: clear
	OpInsEl   = "insel"   // xml: insert element S at Pos
	OpSetAttr = "setattr" // xml element: attribute Key = S
	OpRmAttr  = "rmattr"  // xml element: remove attribute Key
)

Op codes.

View Source
const Schema = 1

Schema is the trace format version both sides write.

Variables

View Source
var ErrNoOracle = errors.New("difffuzz: node or the yjs oracle is not available")

ErrNoOracle is returned by StartOracle when node, the worker script or its dependencies are not there; anything that goes wrong after that is a failure, not an absence.

Functions

func DecodeValue

func DecodeValue(raw json.RawMessage) (any, error)

DecodeValue builds the Go value of a tagged value: int64, float64, string, bool, nil, []any, map[string]any.

Types

type DeltaStep

type DeltaStep struct {
	R *int            `json:"r,omitempty"`
	D *int            `json:"d,omitempty"`
	I *string         `json:"i,omitempty"`
	E json.RawMessage `json:"e,omitempty"`
	A *[]KV           `json:"a,omitempty"`
}

DeltaStep is a delta op by hints: R retains 1 + R % (what is left), D deletes 1 + D % (what is left), I inserts a string, E an embed; A are its attributes (nil: none given). A retain or delete with nothing left is dropped on both sides.

type Divergence

type Divergence struct {
	Step  int    `json:"step"`
	Kind  string `json:"kind"`  // the step's kind
	Field string `json:"field"` // e.g. "events", "peers.v1", "converge.go"
	Where string `json:"where"` // the peer or event index, for the report
	Go    string `json:"go"`
	JS    string `json:"js"`
}

Divergence is the first place two traces differ, or where one implementation's peers did not converge.

func Compare

func Compare(s Scenario, g, j Trace) *Divergence

Compare walks the traces of s step by step and returns the first difference, then checks that each side's peers converged at the end.

func Converged

func Converged(s Scenario, tr Trace) *Divergence

Converged checks that the peers of one trace of s were together after its last final step.

func (*Divergence) Class

func (d *Divergence) Class() string

Class is what a shrunk scenario must keep failing on.

func (*Divergence) String

func (d *Divergence) String() string

type Event

type Event struct {
	Peer int    `json:"peer"`
	V    string `json:"v"`
	Hex  string `json:"hex"`
}

Event is one update event: the peer, the format ("1" or "2"), the bytes.

type GenOptions

type GenOptions struct {
	// BMPOnly leaves characters outside the BMP out of strings. Two peers
	// that cut one surrogate pair in different places end apart in yjs
	// (each replaces the halves it cut with U+FFFD), so a check of
	// convergence without yjs to compare with needs strings without pairs.
	BMPOnly bool
}

GenOptions changes what Generate draws.

type KV

type KV struct {
	K string          `json:"k"`
	V json.RawMessage `json:"v"`
}

KV is an attribute: a key and a tagged value.

type ObsEvent

type ObsEvent struct {
	Batch  int    `json:"batch"`
	Peer   int    `json:"peer"`
	Root   string `json:"root"`
	Path   string `json:"path"`
	Kind   string `json:"kind"`
	Change string `json:"change"`
}

ObsEvent is one observer event: where it fired, the path of its target below the root, the target's kind and the change, as JSON.

type Op

type Op struct {
	Root  string          `json:"root"`
	Path  []string        `json:"path,omitempty"`
	Want  string          `json:"want"`
	O     string          `json:"o"`
	Pos   int             `json:"pos,omitempty"`
	Len   int             `json:"len,omitempty"`
	S     string          `json:"s,omitempty"`
	Key   string          `json:"key,omitempty"`
	V     json.RawMessage `json:"v,omitempty"`
	Attrs *[]KV           `json:"attrs,omitempty"`
	Delta []DeltaStep     `json:"delta,omitempty"`
}

Op is one change inside a transaction. Path segments are keys ("k0") for a map and index hints ("#3") for an array or an XML node; Attrs nil means "not given" (an insert then inherits the formatting around it), an empty list means "given, empty".

type Oracle

type Oracle struct {
	// contains filtered or unexported fields
}

Oracle is a running yjs worker (testdata/gen/difffuzz-oracle.mjs).

func StartOracle

func StartOracle(script string) (*Oracle, error)

StartOracle starts the worker script (its directory must hold node_modules with yjs and lib0).

func (*Oracle) Close

func (o *Oracle) Close() error

Close ends the worker; it must exit with status 0 and nothing on stderr.

func (*Oracle) Run

func (o *Oracle) Run(s Scenario, timeout time.Duration) (Trace, error)

Run replays s in yjs. Every reply must carry the request's id and the schema version, and come within timeout; after a failure the worker is killed and every later call fails.

type Peer

type Peer struct {
	Client uint64 `json:"client"`
	GC     bool   `json:"gc"`
	Undo   bool   `json:"undo"`
}

Peer is one document: its client ID, whether it collects garbage, and whether an UndoManager watches its roots.

type PeerState

type PeerState struct {
	Peer    int    `json:"peer"`
	V1      string `json:"v1"`
	V2      string `json:"v2"`
	SV      string `json:"sv"`
	Pending bool   `json:"pending"`
	View    string `json:"view"`
	CanUndo bool   `json:"canUndo,omitempty"`
	CanRedo bool   `json:"canRedo,omitempty"`
}

PeerState is a touched peer after a step.

type RunOptions

type RunOptions struct {
	// Plant changes the Go trace on purpose (the harness's own tests): it
	// gets the step index and the step's trace after the step ran.
	Plant func(i int, st *StepTrace)
	// Stats, when set, counts what the run did.
	Stats *Stats
}

RunOptions are test hooks; the zero value replays the scenario as is.

type Scenario

type Scenario struct {
	Seed    uint64 `json:"seed"`
	Peers   []Peer `json:"peers"`
	Observe bool   `json:"observe,omitempty"`
	Steps   []Step `json:"steps"`
}

Scenario is one fuzz run.

func Generate

func Generate(seed uint64) Scenario

Generate builds the scenario of a seed.

func GenerateWith

func GenerateWith(seed uint64, opts GenOptions) Scenario

GenerateWith is Generate with options.

func Shrink

func Shrink(s Scenario, class string, budget int, check func(Scenario) *Divergence) Scenario

Shrink makes s smaller while check keeps reporting a divergence of the same class as the original: chunks of steps first (halving the chunk), then single ops inside transactions. The final step stays. budget bounds the candidate runs; check may return nil for a candidate that passes.

type Stats

type Stats struct {
	Ops     map[string]int // op (want/op) that found its target
	NoOps   map[string]int // op whose path did not resolve
	Steps   map[string]int // step kinds that were not skipped
	Events  int
	Pending int // touched peers left with something pending
	UndoDid int // undo or redo that changed something
	Errors  int
	Nested  int // ops on a target below a root
}

Stats counts what runs did, to see what the generator reaches.

type Step

type Step struct {
	K     string `json:"k"`
	P     int    `json:"p,omitempty"`
	From  int    `json:"from,omitempty"`
	To    int    `json:"to,omitempty"`
	V2    bool   `json:"v2,omitempty"`
	Order string `json:"order,omitempty"`
	N     int    `json:"n,omitempty"`
	R     uint32 `json:"r,omitempty"`
	// Conv delivers each event converted from the other format
	// (ConvertUpdateFormatV1ToV2 for a V2 delivery, V2ToV1 for V1).
	Conv bool `json:"conv,omitempty"`
	Ops  []Op `json:"ops,omitempty"`
}

Step is one action; only the fields of its kind are set.

type StepTrace

type StepTrace struct {
	Skip   bool   `json:"skip,omitempty"`
	Err    string `json:"err,omitempty"`
	Result *bool  `json:"result,omitempty"`
	// Sent is the update a sync, merge or diff sends, or each converted
	// update of a deliver step, each followed by a comma.
	Sent string `json:"sent,omitempty"`
	// SentSV and SentMeta are EncodeStateVectorFromUpdate(V2) and
	// ParseUpdateMeta(V2) of the sent bytes (of each, as Sent).
	SentSV   string      `json:"sentSV,omitempty"`
	SentMeta string      `json:"sentMeta,omitempty"`
	Events   []Event     `json:"events,omitempty"`
	Peers    []PeerState `json:"peers,omitempty"`
	// Obs are the observer events of the step (Scenario.Observe), in the
	// order the callbacks ran; Batch numbers the callback each came in.
	Obs []ObsEvent `json:"obs,omitempty"`
	// Depths are, for an unwind step, each undo peer's count of undos that
	// changed something until none did, then of redos.
	Depths []int `json:"depths,omitempty"`
}

StepTrace records one step.

type Trace

type Trace struct {
	Schema int         `json:"schema"`
	Steps  []StepTrace `json:"steps"`
}

Trace is what one implementation did with a scenario.

func Run

func Run(s Scenario, opts RunOptions) Trace

Run replays s through ygo and returns its trace.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL