A rule that must hold in every state an engine visits, the first included.
Model.always("never above three", |n| n <= 3)
:= []
Describe a system as a state machine, say what must be true of it, and check
that several ways. The engines are pure functions of ordinary Roc values, so
they run in a plain expect under roc test, and inside a fuzz target
unchanged.
A Machine is where the system starts, what can happen in
each state, and what happening does. Its rules say what must always be
true and, as goals, what some run must reach. Start with a search in an
expect:
counter : Model.Machine(U64, [Up, Down])
counter = {
init: 0,
actions: |n| if (n == 0) [Up] else if (n == 3) [Down] else [Up, Down],
step: |n, action| match action {
Up => n + 1
Down => n - 1
},
rules: [Model.always("never above three", |n| n <= 3), Model.sometimes("reaches three", |n| n == 3)],
}
expect Model.check(counter, { depth: 10 }) == { verdict: Holds(Exhausted), states: 4, depth: 3 }
The engines, weakest assumption first:
check visits every state within a number of steps of init, and explore is the same search with a full report.walk follows one path chosen by a list of bytes: the body of a fuzz target.replay follows one path given as a list of actions: a regression test. apply does the same without asking whether each action is possible.step_from starts at any state at all and takes each action once.walk_target, path_target and
step_target make a fuzz target from a machine. Each
checks every rule and reports a broken one as a crash, so the runner
saves, replays and minimises it like any other finding.
app [target] { fuzz: platform "PLATFORM_URL" }
import fuzz.Model
import Index
target = Model.path_target({ name: "index", machine: Index.machine, decode: Index.path, examples: [] })
Each target declares the machine's goals to the runner and marks the ones
an input reaches. The campaign report then lists every goal with the first
input that reached it, and the goals no input reached, and a campaign that
leaves one unreached is inconclusive: it passed every rule without going
where the model says a run must go. Replay a goal's witness with
TARGET replay, or keep it with TARGET retain and add the Fuzz.saved
declaration it prints to the target's examples, after which
TARGET check reaches that goal every time.
A rule is about a state or about one step. A rule about a step is handed the state before, the action and the state after, and goes in the same list:
Model.always_step("one at a time", |{ before, action: _, after }| after == before + 1 or before == after + 1)
Model.sometimes_step("comes down from three", |{ before, action, after: _ }| action == Down and before == 3)
Every engine checks every rule of the machine it is given, so a goal is
written once and a search, a walk, a replayed path and a single step all
grade it. See Transition.
A search keeps the states it has met and does not look at one twice. "The
same state" is decided by == and the hash (check) or by a key you
give (explore), and by default a state already met is dropped
before any rule sees it. So if two different states compare equal, the
second is never checked and nothing reachable only through it is ever
visited: a broken state can vanish without a word. Write a state's is_eq
by hand only if it compares everything step and the rules read. When in
doubt, search with verify: EveryStep, which checks the rules on every
state a step produces before asking whether it was met before.
Roc code cannot catch a crash, so a crash or an integer overflow inside
actions, step or a rule ends the engine with no verdict and no trace.
roc test runs each top-level expect on its own and reports the crashed
one as failed with the crash message, and that is all it knows. A fuzz
campaign does better: the runner saves the input that crashed, and the
show of a path_target prints that input's path
without running it.
A rule that must hold in every state an engine visits, the first included.
Model.always("never above three", |n| n <= 3)
A goal: a rule that some visited state must satisfy. It guards against a model that passes only because it never gets anywhere interesting. Only a search can say a goal was never reached; a path reports the ones it did reach, and a fuzz campaign reports the ones no input reached.
Model.sometimes("reaches three", |n| n == 3)
A rule for states where the machine has come to rest: no action is
possible, or every possible action leaves the state exactly as it was.
Such a state must satisfy allowed, otherwise the machine is stuck there
and the engines report a Deadlock. With no at_rest rule, resting
anywhere is fine.
Model.at_rest("a lift rests only at the ground floor", |floor| floor == 0)
always_step : Str, (Transition(state, action) -> Bool) -> Rule(state, action)
A rule that must hold of every step an engine takes. It is handed a
Transition.
Model.always_step("one at a time", |{ before, action: _, after }| after == before + 1 or before == after + 1)
sometimes_step : Str, (Transition(state, action) -> Bool) -> Rule(state, action)
A goal about a step: some step taken must satisfy it.
Model.sometimes_step("comes down from three", |{ before, action, after: _ }| action == Down and before == 3)
The names of a machine's goals, its sometimes and sometimes_step
rules, in the order they are written: the inventory a fuzz target
declares to the runner.
expect Model.goals(counter) == ["reaches three"]
check : Machine(state, action), { depth : U64 } -> Search(action) where [state.is_eq : state, state -> Bool, state.to_hash : state, Hasher -> Hasher]
Visit every state reachable from init in at most depth actions,
nearest first, remembering the states already seen.
The states are held in memory and compared by value, so this is for
small configurations: shrink the sizes until the search is Exhausted.
A state equal to one already seen is dropped before any rule sees it, so
the state's == must not call two different states equal. See "What a
search remembers" above, and explore for the way out.
A broken rule comes back with a shortest trace that breaks it:
expect {
strict = { ..counter, rules: [Model.always("never reaches two", |n| n < 2)] }
Model.check(strict, { depth: 10 }) == { verdict: Violated({ rule: "never reaches two", trace: [Up, Up] }), states: 3, depth: 2 }
}
The shortest trace the search found that meets the goal with this name.
Keep it as a regression with replay, or as a fuzz
target's example with walk_example.
expect Model.witness(Model.explore(counter, { depth: 10, key: |n| n }), "reaches three") == Ok([Up, Up, Up])
A report in plain words: the verdict, then what the search covered, and so what the verdict does and does not establish.
expect Model.explain(Model.explore(counter, { depth: 10, key: |n| n })) == "Holds for every reachable state. The search ran out of new states: 4 visited, the farthest 3 actions from the start, and every step from each of them checked (6 steps). Goals reached: 1 of 1."
walk : Machine(state, action), List(U8) -> Run(state, action) where [state.is_eq : state, state -> Bool]
Follow one path from init, letting each byte choose among the actions
possible at that moment (byte % count). Every byte list is therefore a
valid path and none is ever rejected. The path ends when the bytes run
out, an always rule fails, or nothing can happen.
This is the body of a fuzz target: pass it the fuzzer's bytes and crash
unless the verdict is Completed. The same bytes always give the same
path, and a shorter byte list gives a prefix of it, so the fuzzer's
minimiser shortens the trace. Only the first 256 possible actions in a
state can be chosen.
# At 0 only `Up` is possible; at 1 and 2, byte 0 picks `Up` and byte 1 `Down`.
expect Model.walk(counter, [0, 0, 1]) == { trace: [Up, Up, Down], state: 1, reached: [], verdict: Completed }
replay : Machine(state, action), List(action) -> Run(state, action) where [state.is_eq : state, state -> Bool, action.is_eq : action, action -> Bool]
Follow a path given as actions, checking that each one is possible when
its turn comes. Paste the trace from a failed check or a
fuzzer's finding here to keep it as an ordinary expect.
expect Model.replay(counter, [Up, Up, Up]).reached == ["reaches three"]
expect Model.replay(counter, [Up, Down, Down]).verdict == Impossible({ at: 2 })
apply : Machine(state, action), List(action) -> Applied(state, action) where [state.is_eq : state, state -> Bool, action.is_eq : action, action -> Bool]
Follow a path given as actions without asking whether each one is
possible: step is called with every action in turn, and the rules
are checked as it goes. Use it to grade a path made elsewhere against
a machine's rules and goals: the schedule of an existing test, or the
actions a fuzz target decodes from its input. The machine then needs
no actions wide enough to list every action such a path may use.
Because actions is not consulted first, step must accept whatever
the path holds.
# At 3 only `Down` is possible. The fourth `Up` is applied all the same.
expect Model.apply(counter, [Up, Up, Up, Up]) == {
trace: [Up, Up, Up, Up],
state: 4,
reached: ["reaches three"],
unreached: [],
outside: [3],
verdict: Violated({ rule: "never above three" }),
}
step_from : Machine(state, action), state -> Step(action) where [state.is_eq : state, state -> Bool]
Start from any state at all, reachable or not, and take each possible action once.
If the rules hold in init, and no rule-satisfying state can break them
in one step, then they hold in every reachable state, however far away.
So feed this states from a generator instead of walking to them: a state
one step short of a sequence-number wrap is as easy to generate as an
empty one.
A Broken or Stuck answer is not yet a bug. Either the state is
reachable and it is one, or it is not and the rules are too weak to rule
it out: add the missing always rule and run again. The rules are strong
enough when this only ever answers Outside or Preserved.
expect Model.step_from(counter, 2) == Preserved({ actions: 2, reached: ["reaches three"] })
expect Model.step_from(counter, 7) == Outside({ rule: "never above three" })
walk_target : { name : Str, machine : Machine(state, action), examples : List(Example(List(U8))) } -> Target where [state.is_eq : state, state -> Bool, action.is_eq : action, action -> Bool]
Walks from init as a fuzz target. The fuzzer's bytes are the input,
each one choosing among the possible actions (walk), so
no input is rejected and the minimiser shortens the trace by deleting
bytes. show prints the trace taken, the state it ended in and the
verdict. Bound the walk's length with the runner's --max-input-size.
target = Model.walk_target({
name: "counter",
machine: counter,
examples: [Model.walk_example(counter, { name: "up to three", revision: "1", trace: [Up, Up, Up] })],
})
examples are inputs, checked on every run and added to the corpus.
walk_example makes one from a trace, such as a
witness from explore, and the Fuzz.saved
declaration that the runner's retain prints goes in the same list.
When the code under test crashes on a walk, show crashes with it,
because it has to take the walk to know the trace. Use
path_target where that matters: its show
prints the path without running it.
walk_example : Machine(state, action), { name : Str, revision : Str, trace : List(action) } -> Example(List(U8)) where [action.is_eq : action, action -> Bool]
An example for a walk_target: the input that
makes walk take exactly this trace. The bytes are
worked out when the runner prepares its examples, and an action that is
not possible when its turn comes, or is not among the first 256 that
are, fails that preparation and names the action.
path_target : { name : Str, machine : Machine(state, action), decode : List(U8) -> List(action), examples : List(Example(List(U8))) } -> Target where [state.is_eq : state, state -> Bool, action.is_eq : action, action -> Bool]
A path from init made by decode from the fuzzer's bytes and applied
as it comes (apply), as a fuzz target: the machine's
actions are not asked whether each action is possible, so the actions
can be drawn from a space far too wide to list. decode must be total,
so that no input is rejected, and should turn a prefix of the bytes
into a prefix of the path, so that the minimiser shortens the trace.
examples are inputs: bytes that decode turns into the paths to
check on every run. A Fuzz.saved declaration that the runner's
retain prints goes here as it is.
target = Model.path_target({
name: "index",
machine: Index.machine,
decode: Index.path,
examples: [Fuzz.saved({ name: "fork-at-bit-32", revision: "1", bytes: [2, 0, 0, 0, 0, 32] })],
})
show prints the decoded path and does not run it. The path is in the
input, so when the code under test crashes on it, show still prints
the trace that led there: the last action of the minimised input is
the one that crashed. TARGET replay names the goals an input reaches.
step_target : { name : Str, machine : Machine(state, action), generator : Generator(state), examples : List(Example(state)) } -> Target where [state.is_eq : state, state -> Bool, state.Encodable, state.Parseable(errs)]
One step from a generated state as a fuzz target:
step_from on whatever generator makes. A state
that already breaks an always rule is rejected, so the report's
rejected count says how often the generator misses. show prints the
state and what one step from it found.
examples are states to start the search from. The platform encodes
each one as a corpus seed and mutates the encoded state directly, which
gives the fuzzer valid states to vary even when the generator almost
never makes one.
target = Model.step_target({
name: "counter steps",
machine: counter,
generator: Fuzz.u64_in(0, 3),
examples: [Fuzz.example({ name: "at the top", revision: "1", make: || 3 })],
})
It is worth its cost when a bug lies further from init than a walk
goes, and it asks two things of you: always rules strong enough that
no unreachable state satisfies them (see step_from),
and a generator that makes states which satisfy them often.
:= [
Always(Str, state -> Bool),
Sometimes(Str, state -> Bool),
AtRest(Str, state -> Bool),
AlwaysStep(Str, Transition(state, action) -> Bool),
SometimesStep(Str, Transition(state, action) -> Bool),
]
Something that must be true of a machine, listed in its rules. The
Str names the rule in every verdict, so write it as the sentence you
want to read when it fails.
A rule is about a state (always, sometimes,
at_rest) or about one step (always_step,
sometimes_step). Every engine checks both kinds.
Transition : { before : state, action : action, after : state }
One step: the state before, the action taken, and the state after.
A step rule reads all three, so a fact about what just happened ("a live key was removed", "the count never drops by more than one") needs no field in the state to remember it. That matters to a search: memory kept in the state multiplies the states.
A search checks a step rule on every step it takes, including a step
that leads back to a state already met. walk, replay
and apply check it on every step of their path, and
step_from on each step it takes from its state.
Machine : {
init : state,
actions : state -> List(action),
step : state, action -> state,
rules : List(Rule(state, action)),
}
The system: where it starts, what can happen in a state, what happening
does, and what must be true. actions must list only what is possible in
that state, because every engine but apply picks from that list
and nowhere else, and calls step only with an action it returned for
that state.
Extent : [Exhausted, Bounded]
How much a search covered.
Exhausted: the search ran out of new states. Every reachable state was visited and every step from every one of them was taken, so the answer is final for this machine, at this size.Bounded: the depth limit stopped it. Every state within depth actions of init was visited, and every step of a trace of at most depth actions was taken. The states exactly depth actions away were visited and not stepped from, so whether anything lies beyond them, or one of them is stuck, is not known.Verdict : [
Holds(Extent),
Violated({ rule : Str, trace : List(action) }),
Deadlock({ rule : Str, trace : List(action) }),
Unreached({ rule : Str, extent : Extent }),
]
What a search concluded.
Holds: no rule failed, as far as the extent says.Violated: an always rule failed. trace is a shortest list of actions from init to a state, or through a step, that breaks any always rule. The search stopped there.Deadlock: trace leads to a resting state its at_rest rule forbids. The search stopped there.Unreached: no always rule failed, and no state visited or step taken satisfied this sometimes rule. When the extent is Exhausted none ever will.A verdict with the size of the search behind it: the number of distinct
states visited, and how many actions the farthest of them is from init
(for Violated, the length of the trace).
:= {
depth : U64,
key : state -> key,
verify : [NewStates, EveryStep] ?? NewStates,
blame : [FirstRule, EveryRule] ?? FirstRule,
}
How to search. Only depth and key must be given.
depth: stop after this many actions from init.key: what the search remembers a state by. Two states with equal keys are one state to the search: the second is not stepped from, and a step from one to the other is no move at all to an at_rest rule. Pass |state| state to remember the whole state, or a part of it when the rest has no hash, such as a reference kept beside the real structure. A key that leaves something out is only sound when the rules pin the rest to it, and then only with verify: EveryStep.verify: NewStates checks the state rules once for each new key. EveryStep checks them on the state after every step, before asking whether its key was met, so a broken state can no longer hide behind an equal key. It costs one round of rules for each step instead of each state, and it does not step from the hidden state. Rules about steps are checked on every step either way.blame: with FirstRule, broken names the one rule the verdict names. With EveryRule it names every always rule the offending state and step break, so the order of the rules does not decide which is blamed. Every rule is then called on a state that already broke one, so they must not crash on such a state.Report : {
verdict : Verdict(action),
states : U64,
depth : U64,
steps : U64,
broken : List(Str),
reached : List({ rule : Str, trace : List(action) }),
unreached : List(Str),
}
Everything a search found.
verdict: as check gives it, naming the first problem.states: the distinct keys visited. depth: how many actions the farthest of them is from init (for Violated, the length of the trace). steps: how many steps were taken and checked.broken: the always or at_rest rules the offending state and step break, state rules first; see blame in Options. Empty unless the verdict is Violated or Deadlock.reached: every goal that was met, in the order the rules are written, each with a shortest trace that meets it.unreached: every goal that was not met, not only the first. After Violated or Deadlock the search stopped early, so these two lists say only how far it had got.Run : {
trace : List(action),
state : state,
reached : List(Str),
verdict : [Completed, Violated({ rule : Str }), Deadlock({ rule : Str }), Impossible({ at : U64 })],
}
What happened along one path.
trace holds the actions actually taken, state is where the path
ended, and reached names the goals met on the way, by a state or by
a step.
Completed: every choice was taken, or nothing more could happen, and no rule failed.Violated: an always rule failed in state, or an always_step rule on the step into it. The path stops there.Deadlock: the path ended at rest in a state its at_rest rule forbids.Impossible: only from replay. The action at index at was not among the possible actions in state.Applied : {
trace : List(action),
state : state,
reached : List(Str),
unreached : List(Str),
outside : List(U64),
verdict : [Completed, Violated({ rule : Str }), Deadlock({ rule : Str })],
}
What happened along a path of actions applied as given.
As Run, and besides: unreached names every goal the
path did not meet, in the order the rules are written, and outside
holds the index of each action that actions did not list when its
turn came. Those were applied all the same, so a rule that failed
after one may be the path's fault and not the machine's.
Step : [Outside({ rule : Str }), Preserved({ actions : U64, reached : List(Str) }), Broken({ rule : Str, action : action }), Stuck({ rule : Str })]
What one step from a given state showed.
Outside: the state itself breaks this always rule, so it says nothing. A fuzz target rejects it.Preserved: each of actions possible actions was a step that keeps every always_step rule, into a state that keeps every always rule. reached names the goals met by the state, by one of those steps or by a state one led to.Broken: after action, this always rule fails, or the step itself breaks this always_step rule.Stuck: the state satisfies every always rule but is at rest where its at_rest rule forbids.