Repository navigation
Expand file tree
/
Copy pathSpillDiff.lean
More file actions
144 lines (131 loc) · 6.88 KB
/
Copy pathSpillDiff.lean
File metadata and controls
144 lines (131 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import Fe.Sonatina.FrameSafe
import Fe.Sonatina.Diff
/-!
# `FrameSafe`, tested
`FrameSafe s` says the machine IR with its spills (`Sidecar.spilledModel` under
`Sidecar.spillPlan`) ends as the machine IR does. Where no proof of it exists, running the two
models on the same inputs is the evidence: `frameSafeCalldata` runs both on call data and
counts the runs, the instrumented runs that panic with `spillClobbered` (a word changed while
the bytes still reload it: the plan or the protection rule is wrong, or the bytes are), and the
other disagreements (the bytes, if they match the instrumented model, would differ from the
machine IR: a miscompile). `Sidecar.pairedModel` runs both inside the differential harnesses
of `Fe/EVM/Diff.lean`, where a disagreement shows as a revert with a payload that names it.
`scripts/frame_safe_diff.sh` runs them.
-/
namespace Fe.Sonatina
open Ethereum Ethereum.EVM Fe Fe.EVM
/-- `EndsAs`, decided, but for the recorded calls, which have no equality to decide: `none`
when the first run ends as the second, else how they differ. -/
def endsAsDiff (a b : Option (Except (Exit World) (ByteArray × World))) : Option String :=
let same (out out' : ByteArray) (w w' : World) : Option String :=
if out != out' then some "return data"
else if w.accounts != w'.accounts then some "accounts"
else if w.logs != w'.logs then some "logs"
else if w.calls.length != w'.calls.length then some "calls"
else none
match a, b with
| none, none => none
| some (.ok (o, w)), some (.ok (o', w')) | some (.ok (o, w)), some (.error (.stop o' w'))
| some (.error (.stop o w)), some (.ok (o', w'))
| some (.error (.stop o w)), some (.error (.stop o' w')) => same o o' w w'
| some (.error (.revert d)), some (.error (.revert d')) =>
if d == d' then none else some "revert data"
| some (.error (.panic c)), some (.error (.panic c')) => if c == c' then none else some "panic"
| some (.error (.panic c)), some (.error (.revert d)) =>
if panicData c == d then none else some "panic"
| some (.error .static), some (.error .static) | some (.error .trap), some (.error .trap)
| some (.error .oob), some (.error .oob) => none
| _, _ => some "outcome"
/-- How a run ends, in a word. -/
def describeEnd : Option (Except (Exit World) (ByteArray × World)) → String
| none => "does not terminate"
| some (.ok (o, _)) => s!"returns {Ethereum.toHex o}"
| some (.error e) => Diff.describeExit e
/-- What the two models do on one run: agree, the instrumented model panics with
`spillClobbered`, or they differ. -/
inductive PairVerdict where
| agree
| clobber
| differ (how : String)
def pairVerdict (machine spilled : Option (Except (Exit World) (ByteArray × World))) :
PairVerdict :=
match spilled with
| some (.error (.panic c)) => if c == spillClobbered then .clobber else
match endsAsDiff machine spilled with
| none => .agree
| some how => .differ s!"{how}: machine {describeEnd machine}, spilled {describeEnd spilled}"
| _ =>
match endsAsDiff machine spilled with
| none => .agree
| some how => .differ s!"{how}: machine {describeEnd machine}, spilled {describeEnd spilled}"
/-- Both models run beside each other: the machine IR's run where the instrumented model ends
as it does, else a revert whose payload says how they differ (`SPILL CLOBBER` or
`SPILL DIFFER …`). -/
def Sidecar.pairedModel (s : Sidecar) : ContractModel :=
let init := s.spillPlan "init"
let runtime := s.spillPlan "runtime"
let spilled := s.spilledModel fun n => if n == "init" then init else runtime
let pair (m x : FeM ByteArray) : FeM ByteArray := fun I w =>
match pairVerdict (m.exec I w) (x.exec I w) with
| .agree => m I w
| .clobber => ExceptT.mk (some (.error (.revert "SPILL CLOBBER".toUTF8)))
| .differ how => ExceptT.mk (some (.error (.revert s!"SPILL DIFFER {how}".toUTF8)))
{ init := fun args => pair ((s.model .machine).init args) (spilled.init args)
runtime := pair (s.model .machine).runtime spilled.runtime }
/-- `targetsOf` with the paired model of each sidecar for its target. -/
def spillTargetsOf (file : String) (sidecars : List (String × Diff.Interface)) :
IO (List Diff.Target) :=
sidecars.filterMapM fun (path, interface) => do
match Parse.parseSidecar (← IO.FS.readFile path) with
| .ok s => return some { s.target interface .machine with model := s.pairedModel }
| .error e =>
IO.println s!"UNSUPPORTED {file}::{path}: {e}"
return none
/-- The plan of each section of the sidecar, a line per function: its spilled values, their
reloads, and whether the reload-liveness sets satisfy their equations. -/
def printPlan (name : String) (s : Sidecar) : IO Unit := do
for (sec, _) in Check.entries s do
let some code := s.section? sec | continue
let some low := code.lowering | continue
for (fn, p) in s.spillPlan sec do
let some f := (low.machine.instrument (s.spillPlan sec)).func? fn | continue
let reloads := p.foldl (· + ·.reloads.length) 0
let unreloaded := (p.filter (·.reloads.isEmpty)).length
IO.println s!"PLAN {name} {sec} {fn} spills={p.length} reloads={reloads} \
unreloaded={unreloaded} closed={f.liveClosed p (f.reloadLive p)}"
/-- Run the machine IR and the instrumented model of the sidecar's runtime on each call data,
at an account with no state in an Osaka block, and print a line per run that does not agree
and a total. -/
def frameSafeCalldata (name path : String) (inputs : List (String × ByteArray))
(prefix_ : ByteArray := .empty) : IO Unit := do
let s ← IO.ofExcept ((Parse.parseSidecar (← IO.FS.readFile path)).mapError (s!"{path}: " ++ ·))
printPlan name s
-- The plan, found once: the model finds it on each run otherwise.
let plan := s.spillPlan "runtime"
let spilled := s.spilledModel fun n => if n == "runtime" then plan else s.spillPlan n
let mut agree := 0
let mut clobber := 0
let mut differ := 0
-- Each line also goes to `FRAMESAFE_LOG`, if set, as it is found: `#eval` prints at the end.
let log ← IO.getEnv "FRAMESAFE_LOG"
let say (line : String) : IO Unit := do
IO.println line
if let some path := log then
IO.FS.withFile path .append fun h => h.putStrLn line
for (kind, d) in inputs do
let I : ExecutionEnv := { Diff.env with calldata := prefix_ ++ d }
let w := World.init ∅ default
let m := (s.model .machine).runtime.exec I w
match pairVerdict m (spilled.runtime.exec I w) with
| .agree => agree := agree + 1
| .clobber =>
clobber := clobber + 1
say s!"CLOBBER {name} {kind} {Ethereum.toHex d}: machine {describeEnd m}"
| .differ how =>
differ := differ + 1
say s!"DIFFER {name} {kind} {Ethereum.toHex d}: {how}"
if let some path := log then
IO.FS.withFile path .append fun h => h.putStrLn s!"RUN {name} {kind}"
say s!"FRAMESAFE {name} runs={inputs.length} agree={agree} clobber={clobber} \
differ={differ}"
end Fe.Sonatina