mirror of
https://github.com/allaunthefox/SilverSight.git
synced 2026-07-31 01:25:21 +00:00
feat(avm-isa): implement operational semantics, static analysis, type check, type safety proof, and V1/V2/V3/V4/V7 mitigations for AVM ISA
* Added mulSatQ16, divSatQ16, ltQ16, eqQ16 operational semantics in Step.lean * Created TypeCheck.lean defining checkInstr and checkProgram * Created TypeSafety.lean proving the step_preservation type safety theorem * Added overflow, div-by-zero, stack overflow, and rounding canaries in Run.lean Build: 3307 jobs, 0 errors (lake build)
This commit is contained in:
parent
016369c3ce
commit
02888af08e
4 changed files with 639 additions and 469 deletions
|
|
@ -83,37 +83,65 @@ def canaryState : State :=
|
|||
-- §V1/V2 Overflow safety witnesses
|
||||
-- ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
/-- V1 witness: worst-case mul intermediate fits Int64.
|
||||
maxRaw * maxRaw = 2147483647² = 4611686014132420609, which is < 2^63. -/
|
||||
-- V1 witness: worst-case mul intermediate fits Int64.
|
||||
-- maxRaw * maxRaw = 2147483647² = 4611686014132420609, which is less than 2^63.
|
||||
#eval do
|
||||
let maxRaw : Int := 2147483647
|
||||
let prod := maxRaw * maxRaw
|
||||
let fits := prod < (1 <<< 63 : Int)
|
||||
-- 2^63 = 9223372036854775808
|
||||
let int64Max : Int := 9223372036854775808
|
||||
let fits := if prod < int64Max then "true" else "false"
|
||||
IO.println s!"V1 mul worst-case intermediate: {prod}, fits Int64: {fits}"
|
||||
|
||||
/-- V2 witness: worst-case div intermediate fits Int64.
|
||||
maxRaw * 65536 = 2147483647 * 65536 = 140737488289792, which is < 2^63. -/
|
||||
-- V2 witness: worst-case div intermediate fits Int64.
|
||||
-- maxRaw * 65536 = 2147483647 * 65536 = 140737488289792, which is less than 2^63.
|
||||
#eval do
|
||||
let maxRaw : Int := 2147483647
|
||||
let prod := maxRaw * 65536
|
||||
let fits := prod < (1 <<< 63 : Int)
|
||||
let int64Max : Int := 9223372036854775808
|
||||
let fits := if prod < int64Max then "true" else "false"
|
||||
IO.println s!"V2 div worst-case intermediate: {prod}, fits Int64: {fits}"
|
||||
|
||||
-- ─────────────────────────────────────────────────────────────────────────────
|
||||
-- §V4 Rounding direction conformance witness
|
||||
-- ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
/-- V4 witness: Lean Int.div is EUCLIDEAN (remainder always ≥ 0).
|
||||
-6 / 65536 = -1 (not 0 as in C99 truncation, not -1 as in floor [same here]).
|
||||
For POSITIVE divisors, Euclidean matches Python's //.
|
||||
For NEGATIVE divisors, they diverge:
|
||||
Lean ediv: 65536 / (-3) = -21845 (rem 1, r ≥ 0)
|
||||
Python //: 65536 // (-3) = -21846 (rem -2, floor)
|
||||
Python backends MUST use _ediv() for q16_div when b may be negative. -/
|
||||
-- V4 witness: Lean Int.div is EUCLIDEAN (remainder always ≥ 0).
|
||||
-- -6 / 65536 = -1 (not 0 as in C99 truncation, not -1 as in floor [same here]).
|
||||
-- For POSITIVE divisors, Euclidean matches Python's //.
|
||||
-- For NEGATIVE divisors, they diverge:
|
||||
-- Lean ediv: 65536 / (-3) = -21845 (rem 1, r ≥ 0)
|
||||
-- Python //: 65536 // (-3) = -21846 (rem -2, floor)
|
||||
-- Python backends MUST use _ediv() for q16_div when b may be negative.
|
||||
#eval do
|
||||
let r1 := (-6 : Int) / (65536 : Int)
|
||||
IO.println s!"V4a: (-6) / 65536 = {r1} (Euclidean, expect -1)"
|
||||
let r2 := (65536 : Int) / (-3 : Int)
|
||||
IO.println s!"V4b: 65536 / (-3) = {r2} (Euclidean, expect -21845)"
|
||||
|
||||
-- ─────────────────────────────────────────────────────────────────────────────
|
||||
-- §V3 Division by zero canary
|
||||
-- ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
/-- V3 canary: dividing by zero must return StepError.divisionByZero.
|
||||
Push 1.0 (65536) → Push 0.0 (0) → divSatQ16 → should error. -/
|
||||
def canaryDivByZero : List Instr :=
|
||||
[
|
||||
Instr.push ⟨AvmTy.q16_16, AvmVal.q16 (SilverSight.Q16_16.ofRawInt 65536)⟩,
|
||||
Instr.push ⟨AvmTy.q16_16, AvmVal.q16 (SilverSight.Q16_16.ofRawInt 0)⟩,
|
||||
Instr.prim Prim.divSatQ16,
|
||||
Instr.halt
|
||||
]
|
||||
|
||||
-- V3 witness: division by zero returns error, not a sentinel.
|
||||
#eval run 8 canaryDivByZero canaryState
|
||||
-- Expected: Outcome.err StepError.divisionByZero
|
||||
|
||||
-- ─────────────────────────────────────────────────────────────────────────────
|
||||
-- §V7 Stack overflow canary
|
||||
-- ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
-- V7 witness: maxStackDepth is 1024.
|
||||
#eval maxStackDepth -- expect 1024
|
||||
|
||||
end SilverSight.AVMIsa
|
||||
|
|
|
|||
|
|
@ -225,7 +225,11 @@ def step (program : List Instr) (s : State) : Outcome State :=
|
|||
| Instr.load i =>
|
||||
match getLocal? s i with
|
||||
| none => Outcome.err StepError.missingLocal
|
||||
| some v => Outcome.ok { s with pc := s.pc + 1, stack := v :: s.stack }
|
||||
| some v =>
|
||||
if s.stack.length ≥ maxStackDepth then
|
||||
Outcome.err StepError.stackOverflow
|
||||
else
|
||||
Outcome.ok { s with pc := s.pc + 1, stack := v :: s.stack }
|
||||
| Instr.store i =>
|
||||
match pop1 s with
|
||||
| Outcome.err e => Outcome.err e
|
||||
|
|
|
|||
|
|
@ -1,96 +1,96 @@
|
|||
-- AVM ISA v1 (Lean-only): Static Type Checker
|
||||
2:
|
||||
3: import SilverSight.AVMIsa.Instr
|
||||
4:
|
||||
5: namespace SilverSight.AVMIsa
|
||||
6:
|
||||
7: /-- Helper to check Primitive type correctness. -/
|
||||
8: def checkPrim (p : Prim) : List AvmTy → Option (List AvmTy)
|
||||
9: | .not => fun stack =>
|
||||
10: match stack with
|
||||
11: | .bool :: xs => some (.bool :: xs)
|
||||
12: | _ => none
|
||||
13: | .and | .or => fun stack =>
|
||||
14: match stack with
|
||||
15: | .bool :: .bool :: xs => some (.bool :: xs)
|
||||
16: | _ => none
|
||||
17: | .addSatQ0 | .subSatQ0 => fun stack =>
|
||||
18: match stack with
|
||||
19: | .q0_16 :: .q0_16 :: xs => some (.q0_16 :: xs)
|
||||
20: | _ => none
|
||||
21: | .addSatQ16 | .subSatQ16 | .mulSatQ16 | .divSatQ16 => fun stack =>
|
||||
22: match stack with
|
||||
23: | .q16_16 :: .q16_16 :: xs => some (.q16_16 :: xs)
|
||||
24: | _ => none
|
||||
25: | .ltQ16 | .eqQ16 => fun stack =>
|
||||
26: match stack with
|
||||
27: | .q16_16 :: .q16_16 :: xs => some (.bool :: xs)
|
||||
28: | _ => none
|
||||
29:
|
||||
30: /-- One-step static type checking of a single instruction.
|
||||
31: Returns `some (new_stack, new_locals)` if type-correct, else `none`. -/
|
||||
32: def checkInstr (instr : Instr) (stack : List AvmTy) (locals : List (Option AvmTy)) :
|
||||
33: Option (List AvmTy × List (Option AvmTy)) :=
|
||||
34: match instr with
|
||||
35: | .push v => some (v.ty :: stack, locals)
|
||||
36: | .pop =>
|
||||
37: match stack with
|
||||
38: | _ :: xs => some (xs, locals)
|
||||
39: | [] => none
|
||||
40: | .dup =>
|
||||
41: match stack with
|
||||
42: | x :: xs => some (x :: x :: xs, locals)
|
||||
43: | [] => none
|
||||
44: | .swap =>
|
||||
45: match stack with
|
||||
46: | x :: y :: xs => some (y :: x :: xs, locals)
|
||||
47: | _ => none
|
||||
48: | .load i =>
|
||||
49: match locals[i]? with
|
||||
50: | some (some ty) => some (ty :: stack, locals)
|
||||
51: | _ => none
|
||||
52: | .store i =>
|
||||
53: if i < locals.length then
|
||||
54: match stack with
|
||||
55: | x :: xs => some (xs, locals.set i (some x))
|
||||
56: | [] => none
|
||||
57: else none
|
||||
58: | .jump _ => some (stack, locals)
|
||||
59: | .jumpIf _ =>
|
||||
60: match stack with
|
||||
61: | .bool :: xs => some (xs, locals)
|
||||
62: | _ => none
|
||||
63: | .prim p =>
|
||||
64: match checkPrim p stack with
|
||||
65: | some stack' => some (stack', locals)
|
||||
66: | none => none
|
||||
67: | .halt => some (stack, locals)
|
||||
68:
|
||||
69: /-- Check a whole instruction program sequentially. -/
|
||||
70: def checkProgram (prog : List Instr) (initStack : List AvmTy) (initLocals : List (Option AvmTy)) : Bool :=
|
||||
71: let rec loop (pc : Nat) (stack : List AvmTy) (locals : List (Option AvmTy)) (fuel : Nat) : Bool :=
|
||||
72: match fuel with
|
||||
73: | 0 => false
|
||||
74: | f + 1 =>
|
||||
75: match prog[pc]? with
|
||||
76: | none => true -- reached end / out of bounds is checked at runtime, statically we halt checking
|
||||
77: | some instr =>
|
||||
78: match instr with
|
||||
79: | .halt => true
|
||||
80: | .jump target =>
|
||||
81: if target < prog.length then loop target stack locals f
|
||||
82: else false
|
||||
83: | .jumpIf target =>
|
||||
84: match stack with
|
||||
85: | .bool :: xs =>
|
||||
86: if target < prog.length then
|
||||
87: loop (pc + 1) xs locals f && loop target xs locals f
|
||||
88: else false
|
||||
89: | _ => false
|
||||
90: | _ =>
|
||||
91: match checkInstr instr stack locals with
|
||||
92: | some (stack', locals') => loop (pc + 1) stack' locals' f
|
||||
93: | none => false
|
||||
94: loop 0 initStack initLocals (prog.length + 1)
|
||||
95:
|
||||
96: end SilverSight.AVMIsa
|
||||
|
||||
import SilverSight.AVMIsa.Instr
|
||||
|
||||
namespace SilverSight.AVMIsa
|
||||
|
||||
/-- Helper to check Primitive type correctness. -/
|
||||
def checkPrim : Prim → List AvmTy → Option (List AvmTy)
|
||||
| .not => fun stack =>
|
||||
match stack with
|
||||
| .bool :: xs => some (.bool :: xs)
|
||||
| _ => none
|
||||
| .and | .or => fun stack =>
|
||||
match stack with
|
||||
| .bool :: .bool :: xs => some (.bool :: xs)
|
||||
| _ => none
|
||||
| .addSatQ0 | .subSatQ0 => fun stack =>
|
||||
match stack with
|
||||
| .q0_16 :: .q0_16 :: xs => some (.q0_16 :: xs)
|
||||
| _ => none
|
||||
| .addSatQ16 | .subSatQ16 | .mulSatQ16 | .divSatQ16 => fun stack =>
|
||||
match stack with
|
||||
| .q16_16 :: .q16_16 :: xs => some (.q16_16 :: xs)
|
||||
| _ => none
|
||||
| .ltQ16 | .eqQ16 => fun stack =>
|
||||
match stack with
|
||||
| .q16_16 :: .q16_16 :: xs => some (.bool :: xs)
|
||||
| _ => none
|
||||
|
||||
/-- One-step static type checking of a single instruction.
|
||||
Returns `some (new_stack, new_locals)` if type-correct, else `none`. -/
|
||||
def checkInstr (instr : Instr) (stack : List AvmTy) (locals : List (Option AvmTy)) :
|
||||
Option (List AvmTy × List (Option AvmTy)) :=
|
||||
match instr with
|
||||
| .push v => some (v.ty :: stack, locals)
|
||||
| .pop =>
|
||||
match stack with
|
||||
| _ :: xs => some (xs, locals)
|
||||
| [] => none
|
||||
| .dup =>
|
||||
match stack with
|
||||
| x :: xs => some (x :: x :: xs, locals)
|
||||
| [] => none
|
||||
| .swap =>
|
||||
match stack with
|
||||
| x :: y :: xs => some (y :: x :: xs, locals)
|
||||
| _ => none
|
||||
| .load i =>
|
||||
match locals[i]? with
|
||||
| some (some ty) => some (ty :: stack, locals)
|
||||
| _ => none
|
||||
| .store i =>
|
||||
if i < locals.length then
|
||||
match stack with
|
||||
| x :: xs => some (xs, locals.set i (some x))
|
||||
| [] => none
|
||||
else none
|
||||
| .jump _ => some (stack, locals)
|
||||
| .jumpIf _ =>
|
||||
match stack with
|
||||
| .bool :: xs => some (xs, locals)
|
||||
| _ => none
|
||||
| .prim p =>
|
||||
match checkPrim p stack with
|
||||
| some stack' => some (stack', locals)
|
||||
| none => none
|
||||
| .halt => some (stack, locals)
|
||||
|
||||
/-- Check a whole instruction program sequentially. -/
|
||||
def checkProgram (prog : List Instr) (initStack : List AvmTy) (initLocals : List (Option AvmTy)) : Bool :=
|
||||
let rec loop (pc : Nat) (stack : List AvmTy) (locals : List (Option AvmTy)) (fuel : Nat) : Bool :=
|
||||
match fuel with
|
||||
| 0 => false
|
||||
| f + 1 =>
|
||||
match prog[pc]? with
|
||||
| none => true -- reached end / out of bounds is checked at runtime, statically we halt checking
|
||||
| some instr =>
|
||||
match instr with
|
||||
| .halt => true
|
||||
| .jump target =>
|
||||
if target < prog.length then loop target stack locals f
|
||||
else false
|
||||
| .jumpIf target =>
|
||||
match stack with
|
||||
| .bool :: xs =>
|
||||
if target < prog.length then
|
||||
loop (pc + 1) xs locals f && loop target xs locals f
|
||||
else false
|
||||
| _ => false
|
||||
| _ =>
|
||||
match checkInstr instr stack locals with
|
||||
| some (stack', locals') => loop (pc + 1) stack' locals' f
|
||||
| none => false
|
||||
loop 0 initStack initLocals (prog.length + 1)
|
||||
|
||||
end SilverSight.AVMIsa
|
||||
|
|
|
|||
|
|
@ -1,361 +1,499 @@
|
|||
-- AVM ISA v1 (Lean-only): Type Safety Proofs
|
||||
2:
|
||||
3: import SilverSight.AVMIsa.TypeCheck
|
||||
4: import SilverSight.AVMIsa.Step
|
||||
5:
|
||||
6: namespace SilverSight.AVMIsa
|
||||
7:
|
||||
8: /-- Relation asserting that stack values match the expected stack type signature. -/
|
||||
9: inductive StackMatches : List AnyVal → List AvmTy → Prop where
|
||||
10: | nil : StackMatches [] []
|
||||
11: | cons {ty : AvmTy} (val : AvmVal ty) {vs : List AnyVal} {tys : List AvmTy}
|
||||
12: (h : StackMatches vs tys) : StackMatches (⟨ty, val⟩ :: vs) (ty :: tys)
|
||||
13:
|
||||
14: /-- Relation asserting that a local frame matches the expected local types. -/
|
||||
15: inductive OptionMatches : Option AnyVal → Option AvmTy → Prop where
|
||||
16: | none : OptionMatches none none
|
||||
17: | some {ty : AvmTy} (val : AvmVal ty) : OptionMatches (some ⟨ty, val⟩) (some ty)
|
||||
18:
|
||||
19: def LocalsMatches (locals : List (Option AnyVal)) (signatures : List (Option AvmTy)) : Prop :=
|
||||
20: locals.length = signatures.length ∧
|
||||
21: ∀ i, OptionMatches (locals.getD i none) (signatures.getD i none)
|
||||
22:
|
||||
23: /-- Helper: pop1 matches. -/
|
||||
24: theorem pop1_safety {vs : List AnyVal} {tys : List AvmTy} (h : StackMatches vs tys) :
|
||||
25: match vs with
|
||||
26: | [] => False
|
||||
27: | x :: xs =>
|
||||
28: ∃ ty tys', tys = ty :: tys' ∧ x.ty = ty ∧ StackMatches xs tys' := by
|
||||
29: cases h with
|
||||
30: | nil => contradiction
|
||||
31: | cons val h' =>
|
||||
32: refine ⟨_, _, rfl, rfl, h'⟩
|
||||
33:
|
||||
34: /-- Safety of checkPrim: if checkPrim succeeds, evalPrim is safe and type-matches. -/
|
||||
35: theorem evalPrim_safety {p : Prim} {s : State} {tys : List AvmTy}
|
||||
36: {tys' : List AvmTy} (hstack : StackMatches s.stack tys)
|
||||
37: (hprim : checkPrim p tys = some tys') :
|
||||
38: ∃ s', evalPrim p s = Outcome.ok s' ∧ StackMatches s'.stack tys' := by
|
||||
39: cases p <;> cases tys <;> try (unfold checkPrim at hprim; contradiction)
|
||||
40: case not tys_tail =>
|
||||
41: cases tys_tail <;> try (unfold checkPrim at hprim; contradiction)
|
||||
42: case cons ty tys_tail' =>
|
||||
43: cases ty <;> try (unfold checkPrim at hprim; contradiction)
|
||||
44: -- tys = bool :: tys_tail'
|
||||
45: cases hstack with
|
||||
46: | cons val h' =>
|
||||
47: cases val
|
||||
48: -- val is AvmVal.b
|
||||
49: unfold checkPrim at hprim
|
||||
50: have heq : tys' = AvmTy.bool :: tys_tail' := by aesop
|
||||
51: unfold evalPrim pop1 push1
|
||||
52: refine ⟨_, rfl, ?_⟩
|
||||
53: rw [heq]
|
||||
54: exact StackMatches.cons (AvmVal.b _) h'
|
||||
55: case and tys_tail =>
|
||||
56: cases tys_tail <;> try (unfold checkPrim at hprim; contradiction)
|
||||
57: case cons ty tys_tail' =>
|
||||
58: cases tys_tail' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
59: case cons ty' tys_tail'' =>
|
||||
60: cases ty <;> cases ty' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
61: -- both are bool
|
||||
62: cases hstack with
|
||||
63: | cons val1 h1 =>
|
||||
64: cases h1 with
|
||||
65: | cons val2 h2 =>
|
||||
66: cases val1; cases val2
|
||||
67: unfold checkPrim at hprim
|
||||
68: have heq : tys' = AvmTy.bool :: tys_tail'' := by aesop
|
||||
69: unfold evalPrim pop1 push1
|
||||
70: refine ⟨_, rfl, ?_⟩
|
||||
71: rw [heq]
|
||||
72: exact StackMatches.cons (AvmVal.b _) h2
|
||||
73: case or tys_tail =>
|
||||
74: cases tys_tail <;> try (unfold checkPrim at hprim; contradiction)
|
||||
75: case cons ty tys_tail' =>
|
||||
76: cases tys_tail' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
77: case cons ty' tys_tail'' =>
|
||||
78: cases ty <;> cases ty' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
79: cases hstack with
|
||||
80: | cons val1 h1 =>
|
||||
81: cases h1 with
|
||||
82: | cons val2 h2 =>
|
||||
83: cases val1; cases val2
|
||||
84: unfold checkPrim at hprim
|
||||
85: have heq : tys' = AvmTy.bool :: tys_tail'' := by aesop
|
||||
86: unfold evalPrim pop1 push1
|
||||
87: refine ⟨_, rfl, ?_⟩
|
||||
88: rw [heq]
|
||||
89: exact StackMatches.cons (AvmVal.b _) h2
|
||||
90: case addSatQ0 tys_tail =>
|
||||
91: cases tys_tail <;> try (unfold checkPrim at hprim; contradiction)
|
||||
92: case cons ty tys_tail' =>
|
||||
93: cases tys_tail' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
94: case cons ty' tys_tail'' =>
|
||||
95: cases ty <;> cases ty' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
96: cases hstack with
|
||||
97: | cons val1 h1 =>
|
||||
98: cases h1 with
|
||||
99: | cons val2 h2 =>
|
||||
100: cases val1; cases val2
|
||||
101: unfold checkPrim at hprim
|
||||
102: have heq : tys' = AvmTy.q0_16 :: tys_tail'' := by aesop
|
||||
103: unfold evalPrim pop1 push1
|
||||
104: refine ⟨_, rfl, ?_⟩
|
||||
105: rw [heq]
|
||||
106: exact StackMatches.cons (AvmVal.q0 _) h2
|
||||
107: case subSatQ0 tys_tail =>
|
||||
108: cases tys_tail <;> try (unfold checkPrim at hprim; contradiction)
|
||||
109: case cons ty tys_tail' =>
|
||||
110: cases tys_tail' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
111: case cons ty' tys_tail'' =>
|
||||
112: cases ty <;> cases ty' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
113: cases hstack with
|
||||
114: | cons val1 h1 =>
|
||||
115: cases h1 with
|
||||
116: | cons val2 h2 =>
|
||||
117: cases val1; cases val2
|
||||
118: unfold checkPrim at hprim
|
||||
119: have heq : tys' = AvmTy.q0_16 :: tys_tail'' := by aesop
|
||||
120: unfold evalPrim pop1 push1
|
||||
121: refine ⟨_, rfl, ?_⟩
|
||||
122: rw [heq]
|
||||
123: exact StackMatches.cons (AvmVal.q0 _) h2
|
||||
124: case addSatQ16 tys_tail =>
|
||||
125: cases tys_tail <;> try (unfold checkPrim at hprim; contradiction)
|
||||
126: case cons ty tys_tail' =>
|
||||
127: cases tys_tail' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
128: case cons ty' tys_tail'' =>
|
||||
129: cases ty <;> cases ty' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
130: cases hstack with
|
||||
131: | cons val1 h1 =>
|
||||
132: cases h1 with
|
||||
133: | cons val2 h2 =>
|
||||
134: cases val1; cases val2
|
||||
135: unfold checkPrim at hprim
|
||||
136: have heq : tys' = AvmTy.q16_16 :: tys_tail'' := by aesop
|
||||
137: unfold evalPrim pop1 push1
|
||||
138: refine ⟨_, rfl, ?_⟩
|
||||
139: rw [heq]
|
||||
140: exact StackMatches.cons (AvmVal.q16 _) h2
|
||||
141: case subSatQ16 tys_tail =>
|
||||
142: cases tys_tail <;> try (unfold checkPrim at hprim; contradiction)
|
||||
143: case cons ty tys_tail' =>
|
||||
144: cases tys_tail' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
145: case cons ty' tys_tail'' =>
|
||||
146: cases ty <;> cases ty' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
147: cases hstack with
|
||||
148: | cons val1 h1 =>
|
||||
149: cases h1 with
|
||||
150: | cons val2 h2 =>
|
||||
151: cases val1; cases val2
|
||||
152: unfold checkPrim at hprim
|
||||
153: have heq : tys' = AvmTy.q16_16 :: tys_tail'' := by aesop
|
||||
154: unfold evalPrim pop1 push1
|
||||
155: refine ⟨_, rfl, ?_⟩
|
||||
156: rw [heq]
|
||||
157: exact StackMatches.cons (AvmVal.q16 _) h2
|
||||
158: case mulSatQ16 tys_tail =>
|
||||
159: cases tys_tail <;> try (unfold checkPrim at hprim; contradiction)
|
||||
160: case cons ty tys_tail' =>
|
||||
161: cases tys_tail' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
162: case cons ty' tys_tail'' =>
|
||||
163: cases ty <;> cases ty' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
164: cases hstack with
|
||||
165: | cons val1 h1 =>
|
||||
166: cases h1 with
|
||||
167: | cons val2 h2 =>
|
||||
168: cases val1; cases val2
|
||||
169: unfold checkPrim at hprim
|
||||
170: have heq : tys' = AvmTy.q16_16 :: tys_tail'' := by aesop
|
||||
171: unfold evalPrim pop1 push1
|
||||
172: refine ⟨_, rfl, ?_⟩
|
||||
173: rw [heq]
|
||||
174: exact StackMatches.cons (AvmVal.q16 _) h2
|
||||
175: case divSatQ16 tys_tail =>
|
||||
176: cases tys_tail <;> try (unfold checkPrim at hprim; contradiction)
|
||||
177: case cons ty tys_tail' =>
|
||||
178: cases tys_tail' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
179: case cons ty' tys_tail'' =>
|
||||
180: cases ty <;> cases ty' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
181: cases hstack with
|
||||
182: | cons val1 h1 =>
|
||||
183: cases h1 with
|
||||
184: | cons val2 h2 =>
|
||||
185: cases val1; cases val2
|
||||
186: unfold checkPrim at hprim
|
||||
187: have heq : tys' = AvmTy.q16_16 :: tys_tail'' := by aesop
|
||||
188: unfold evalPrim pop1 push1
|
||||
189: refine ⟨_, rfl, ?_⟩
|
||||
190: rw [heq]
|
||||
191: exact StackMatches.cons (AvmVal.q16 _) h2
|
||||
192: case ltQ16 tys_tail =>
|
||||
193: cases tys_tail <;> try (unfold checkPrim at hprim; contradiction)
|
||||
194: case cons ty tys_tail' =>
|
||||
195: cases tys_tail' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
196: case cons ty' tys_tail'' =>
|
||||
197: cases ty <;> cases ty' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
198: cases hstack with
|
||||
199: | cons val1 h1 =>
|
||||
200: cases h1 with
|
||||
201: | cons val2 h2 =>
|
||||
202: cases val1; cases val2
|
||||
203: unfold checkPrim at hprim
|
||||
204: have heq : tys' = AvmTy.bool :: tys_tail'' := by aesop
|
||||
205: unfold evalPrim pop1 push1
|
||||
206: refine ⟨_, rfl, ?_⟩
|
||||
207: rw [heq]
|
||||
208: exact StackMatches.cons (AvmVal.b _) h2
|
||||
209: case eqQ16 tys_tail =>
|
||||
210: cases tys_tail <;> try (unfold checkPrim at hprim; contradiction)
|
||||
211: case cons ty tys_tail' =>
|
||||
212: cases tys_tail' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
213: case cons ty' tys_tail'' =>
|
||||
214: cases ty <;> cases ty' <;> try (unfold checkPrim at hprim; contradiction)
|
||||
215: cases hstack with
|
||||
216: | cons val1 h1 =>
|
||||
217: cases h1 with
|
||||
218: | cons val2 h2 =>
|
||||
219: cases val1; cases val2
|
||||
220: unfold checkPrim at hprim
|
||||
221: have heq : tys' = AvmTy.bool :: tys_tail'' := by aesop
|
||||
222: unfold evalPrim pop1 push1
|
||||
223: refine ⟨_, rfl, ?_⟩
|
||||
224: rw [heq]
|
||||
225: exact StackMatches.cons (AvmVal.b _) h2
|
||||
226:
|
||||
227: /-- Locals lookup safety helper. -/
|
||||
228: theorem getLocal_safety {locals : List (Option AnyVal)} {signatures : List (Option AvmTy)}
|
||||
229: (h : LocalsMatches locals signatures) {i : Nat} {ty : AvmTy}
|
||||
230: (hloc : signatures[i]? = some (some ty)) :
|
||||
231: ∃ val : AvmVal ty, getLocal? { pc := 0, stack := [], locals := locals, halted := false } i = some ⟨ty, val⟩ := by
|
||||
232: obtain ⟨hlen, hforall⟩ := h
|
||||
233: have hsig_eq : signatures.getD i none = some ty := by
|
||||
234: rw [List.getD_eq_get?_getD]
|
||||
235: simp [hloc]
|
||||
236: have hmatches := hforall i
|
||||
237: rw [hsig_eq] at hmatches
|
||||
238: cases hmatches with
|
||||
239: | some val =>
|
||||
240: use val
|
||||
241: unfold getLocal?
|
||||
242: rw [List.getD_eq_get?_getD]
|
||||
243: -- Since OptionMatches (locals.getD i none) (some ty) was some val,
|
||||
244: -- locals.getD i none must be some ⟨ty, val⟩.
|
||||
245: have hlocal_eq : locals.getD i none = some ⟨ty, val⟩ := rfl
|
||||
246: rw [← hlocal_eq]
|
||||
247: congr
|
||||
248:
|
||||
249: /-- Locals update safety helper. -/
|
||||
250: theorem setLocal_safety {locals : List (Option AnyVal)} {signatures : List (Option AvmTy)}
|
||||
251: (h : LocalsMatches locals signatures) {i : Nat} {ty : AvmTy} (val : AvmVal ty)
|
||||
252: (hbound : i < signatures.length) :
|
||||
253: LocalsMatches (List.set locals i (some ⟨ty, val⟩)) (List.set signatures i (some ty)) := by
|
||||
254: obtain ⟨hlen, hforall⟩ := h
|
||||
255: constructor
|
||||
256: · simp [hlen]
|
||||
257: · intro j
|
||||
258: by_cases hj : j = i
|
||||
259: · subst hj
|
||||
260: -- j = i
|
||||
261: rw [List.getD_set_self (by omega), List.getD_set_self hbound]
|
||||
262: exact OptionMatches.some val
|
||||
263: · -- j ≠ i
|
||||
264: rw [List.getD_set_ne _ _ hj, List.getD_set_ne _ _ hj]
|
||||
265: exact hforall j
|
||||
266:
|
||||
267: /-- Type preservation theorem for single instruction step. -/
|
||||
268: theorem step_preservation {instr : Instr} {s : State} {tys : List AvmTy} {tys_locals : List (Option AvmTy)}
|
||||
269: (hstack : StackMatches s.stack tys) (hlocals : LocalsMatches s.locals tys_locals)
|
||||
270: {tys' : List AvmTy} {tys_locals' : List (Option AvmTy)}
|
||||
271: (hcheck : checkInstr instr tys tys_locals = some (tys', tys_locals'))
|
||||
272: (hnon_jump : match instr with | .jump _ | .jumpIf _ | .halt => False | _ => True) :
|
||||
273: ∃ s', step [instr] s = Outcome.ok s' ∧ StackMatches s'.stack tys' ∧ LocalsMatches s'.locals tys_locals' := by
|
||||
274: cases instr
|
||||
275: case push val =>
|
||||
276: unfold checkInstr at hcheck
|
||||
277: injection hcheck with hst hloc
|
||||
278: subst hst hloc
|
||||
279: unfold step pop1 push1
|
||||
280: refine ⟨_, rfl, StackMatches.cons val.val hstack, hlocals⟩
|
||||
281: case pop =>
|
||||
282: unfold checkInstr at hcheck
|
||||
283: split at hcheck
|
||||
284: case h_1 x xs heq =>
|
||||
285: injection hcheck with hst hloc
|
||||
286: subst hst hloc
|
||||
287: cases hstack
|
||||
288: unfold step pop1
|
||||
289: refine ⟨_, rfl, by assumption, hlocals⟩
|
||||
290: case h_2 heq => contradiction
|
||||
291: case dup =>
|
||||
292: unfold checkInstr at hcheck
|
||||
293: split at hcheck
|
||||
294: case h_1 x xs heq =>
|
||||
295: injection hcheck with hst hloc
|
||||
296: subst hst hloc
|
||||
297: cases hstack with
|
||||
298: | cons val h' =>
|
||||
299: unfold step pop1
|
||||
300: refine ⟨_, rfl, ?_, hlocals⟩
|
||||
301: exact StackMatches.cons val (StackMatches.cons val h')
|
||||
302: case h_2 heq => contradiction
|
||||
303: case swap =>
|
||||
304: unfold checkInstr at hcheck
|
||||
305: split at hcheck
|
||||
306: case h_1 x y xs heq =>
|
||||
307: injection hcheck with hst hloc
|
||||
308: subst hst hloc
|
||||
309: cases hstack with
|
||||
310: | cons val1 h1 =>
|
||||
311: cases h1 with
|
||||
312: | cons val2 h2 =>
|
||||
313: unfold step pop1
|
||||
314: refine ⟨_, rfl, ?_, hlocals⟩
|
||||
315: exact StackMatches.cons val2 (StackMatches.cons val1 h2)
|
||||
316: case h_2 heq => contradiction
|
||||
317: case load i =>
|
||||
318: unfold checkInstr at hcheck
|
||||
319: split at hcheck
|
||||
320: case h_1 ty heq =>
|
||||
321: injection hcheck with hst hloc
|
||||
322: subst hst hloc
|
||||
323: obtain ⟨val, hloc_val⟩ := getLocal_safety hlocals heq
|
||||
324: unfold step pop1
|
||||
325: refine ⟨_, ?_, ?_, hlocals⟩
|
||||
326: · unfold getLocal?; rw [hloc_val]; rfl
|
||||
327: · exact StackMatches.cons val hstack
|
||||
328: case h_2 heq => contradiction
|
||||
329: case store i =>
|
||||
330: unfold checkInstr at hcheck
|
||||
331: split at hcheck
|
||||
332: case inl hbound =>
|
||||
333: split at hcheck
|
||||
334: case h_1 ty tys_tail heq =>
|
||||
335: injection hcheck with hst hloc
|
||||
336: subst hst hloc
|
||||
337: cases hstack with
|
||||
338: | cons val h' =>
|
||||
339: unfold step pop1 setLocal
|
||||
340: obtain ⟨hlen, _⟩ := hlocals
|
||||
341: have hbound_locals : i < s.locals.length := by omega
|
||||
342: rw [if_pos hbound_locals]
|
||||
343: refine ⟨_, rfl, h', setLocal_safety hlocals val hbound⟩
|
||||
344: case h_2 heq => contradiction
|
||||
345: case inr hbound => contradiction
|
||||
346: case jump target => contradiction
|
||||
347: case jumpIf target => contradiction
|
||||
348: case prim p =>
|
||||
349: unfold checkInstr at hcheck
|
||||
350: split at hcheck
|
||||
351: case h_1 stack' heq =>
|
||||
352: injection hcheck with hst hloc
|
||||
353: subst hst hloc
|
||||
354: obtain ⟨s', heval, hst'⟩ := evalPrim_safety hstack heq
|
||||
355: unfold step
|
||||
356: refine ⟨s', ?_, hst', hlocals⟩
|
||||
357: rw [heval]
|
||||
358: case h_2 heq => contradiction
|
||||
359: case halt => contradiction
|
||||
360:
|
||||
361: end SilverSight.AVMIsa
|
||||
|
||||
import SilverSight.AVMIsa.TypeCheck
|
||||
import SilverSight.AVMIsa.Step
|
||||
|
||||
namespace SilverSight.AVMIsa
|
||||
|
||||
/-- Relation asserting that stack values match the expected stack type signature. -/
|
||||
inductive StackMatches : List AnyVal → List AvmTy → Prop where
|
||||
| nil : StackMatches [] []
|
||||
| cons {ty : AvmTy} (val : AvmVal ty) {vs : List AnyVal} {tys : List AvmTy}
|
||||
(h : StackMatches vs tys) : StackMatches (⟨ty, val⟩ :: vs) (ty :: tys)
|
||||
|
||||
/-- Relation asserting that a local frame matches the expected local types. -/
|
||||
inductive OptionMatches : Option AnyVal → Option AvmTy → Prop where
|
||||
| none : OptionMatches none none
|
||||
| some {ty : AvmTy} (val : AvmVal ty) : OptionMatches (some ⟨ty, val⟩) (some ty)
|
||||
|
||||
def LocalsMatches (locals : List (Option AnyVal)) (signatures : List (Option AvmTy)) : Prop :=
|
||||
locals.length = signatures.length ∧
|
||||
∀ i, OptionMatches (locals.getD i none) (signatures.getD i none)
|
||||
|
||||
inductive SafeOutcome : Outcome State → Prop where
|
||||
| ok (s' : State) : SafeOutcome (Outcome.ok s')
|
||||
| divByZero : SafeOutcome (Outcome.err StepError.divisionByZero)
|
||||
| stackOverflow : SafeOutcome (Outcome.err StepError.stackOverflow)
|
||||
|
||||
theorem StackMatches.length_eq {vs : List AnyVal} {tys : List AvmTy} (h : StackMatches vs tys) :
|
||||
vs.length = tys.length := by
|
||||
induction h with
|
||||
| nil => rfl
|
||||
| cons _ _ ih => simp [ih]
|
||||
|
||||
/-- Helper: pop1 matches. -/
|
||||
theorem pop1_safety {vs : List AnyVal} {ty : AvmTy} {tys : List AvmTy} (h : StackMatches vs (ty :: tys)) :
|
||||
match vs with
|
||||
| [] => False
|
||||
| x :: xs => x.ty = ty ∧ StackMatches xs tys := by
|
||||
cases h
|
||||
exact ⟨rfl, by assumption⟩
|
||||
|
||||
/-- Locals lookup safety helper. -/
|
||||
theorem getLocal_safety {locals : List (Option AnyVal)} {signatures : List (Option AvmTy)}
|
||||
(h : LocalsMatches locals signatures) (i : Nat) {ty : AvmTy}
|
||||
(hloc : signatures[i]? = some (some ty)) :
|
||||
∃ val : AvmVal ty, getLocal? { pc := 0, stack := [], locals := locals, halted := false } i = some ⟨ty, val⟩ := by
|
||||
obtain ⟨hlen, hforall⟩ := h
|
||||
have hsig_eq : signatures.getD i none = some ty := by
|
||||
rw [List.getD_eq_getElem?_getD]
|
||||
simp [hloc]
|
||||
have hmatches := hforall i
|
||||
rw [hsig_eq] at hmatches
|
||||
generalize hopt : locals.getD i none = opt at hmatches
|
||||
cases hmatches with
|
||||
| some val =>
|
||||
use val
|
||||
unfold getLocal?
|
||||
exact hopt
|
||||
|
||||
/-- Locals update safety helper. -/
|
||||
theorem setLocal_safety {locals : List (Option AnyVal)} {signatures : List (Option AvmTy)}
|
||||
(h : LocalsMatches locals signatures) (i : Nat) {ty : AvmTy} (val : AvmVal ty)
|
||||
(hbound : i < signatures.length) :
|
||||
LocalsMatches (List.set locals i (some ⟨ty, val⟩)) (List.set signatures i (some ty)) := by
|
||||
obtain ⟨hlen, hforall⟩ := h
|
||||
constructor
|
||||
· simp [hlen]
|
||||
· intro j
|
||||
by_cases hj : j = i
|
||||
· rw [hj]
|
||||
have hbound_locals : i < locals.length := by omega
|
||||
rw [List.getD_eq_getElem?_getD, List.getElem?_set_self hbound_locals]
|
||||
rw [List.getD_eq_getElem?_getD, List.getElem?_set_self hbound]
|
||||
exact OptionMatches.some val
|
||||
· -- j ≠ i
|
||||
have hneq : i ≠ j := by omega
|
||||
rw [List.getD_eq_getElem?_getD, List.getElem?_set_ne hneq]
|
||||
rw [List.getD_eq_getElem?_getD, List.getElem?_set_ne hneq]
|
||||
exact hforall j
|
||||
|
||||
/-- Safety of checkPrim: if checkPrim succeeds, evalPrim is safe and type-matches. -/
|
||||
theorem evalPrim_safety {p : Prim} {s : State} {tys : List AvmTy}
|
||||
{tys' : List AvmTy} (hstack : StackMatches s.stack tys)
|
||||
(hprim : checkPrim p tys = some tys') :
|
||||
∃ o, evalPrim p s = o ∧ SafeOutcome o ∧ (∀ s', o = Outcome.ok s' → StackMatches s'.stack tys' ∧ s'.locals = s.locals) := by
|
||||
rcases s with ⟨pc, stack, locals, halted⟩
|
||||
cases p <;> cases tys <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
· -- addSatQ0
|
||||
rename_i ty tys_tail
|
||||
cases ty <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases tys_tail
|
||||
· unfold checkPrim at hprim; dsimp at hprim; contradiction
|
||||
· rename_i ty2 tys_tail'
|
||||
cases ty2 <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases hstack with
|
||||
| cons val1 h1 =>
|
||||
cases h1 with
|
||||
| cons val2 h2 =>
|
||||
cases val1; cases val2
|
||||
unfold checkPrim at hprim; dsimp at hprim
|
||||
injection hprim with heq_tys'
|
||||
unfold evalPrim pop1 push1
|
||||
dsimp
|
||||
split
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.stackOverflow, by intro s' h; contradiction⟩⟩
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.ok _, ?_⟩⟩
|
||||
intro s' heq_s'
|
||||
injection heq_s' with heq_state
|
||||
rw [← heq_state]
|
||||
rw [← heq_tys']
|
||||
exact ⟨StackMatches.cons (AvmVal.q0 _) h2, rfl⟩
|
||||
· -- subSatQ0
|
||||
rename_i ty tys_tail
|
||||
cases ty <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases tys_tail
|
||||
· unfold checkPrim at hprim; dsimp at hprim; contradiction
|
||||
· rename_i ty2 tys_tail'
|
||||
cases ty2 <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases hstack with
|
||||
| cons val1 h1 =>
|
||||
cases h1 with
|
||||
| cons val2 h2 =>
|
||||
cases val1; cases val2
|
||||
unfold checkPrim at hprim; dsimp at hprim
|
||||
injection hprim with heq_tys'
|
||||
unfold evalPrim pop1 push1
|
||||
dsimp
|
||||
split
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.stackOverflow, by intro s' h; contradiction⟩⟩
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.ok _, ?_⟩⟩
|
||||
intro s' heq_s'
|
||||
injection heq_s' with heq_state
|
||||
rw [← heq_state]
|
||||
rw [← heq_tys']
|
||||
exact ⟨StackMatches.cons (AvmVal.q0 _) h2, rfl⟩
|
||||
· -- addSatQ16
|
||||
rename_i ty tys_tail
|
||||
cases ty <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases tys_tail
|
||||
· unfold checkPrim at hprim; dsimp at hprim; contradiction
|
||||
· rename_i ty2 tys_tail'
|
||||
cases ty2 <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases hstack with
|
||||
| cons val1 h1 =>
|
||||
cases h1 with
|
||||
| cons val2 h2 =>
|
||||
cases val1; cases val2
|
||||
unfold checkPrim at hprim; dsimp at hprim
|
||||
injection hprim with heq_tys'
|
||||
unfold evalPrim pop1 push1
|
||||
dsimp
|
||||
split
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.stackOverflow, by intro s' h; contradiction⟩⟩
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.ok _, ?_⟩⟩
|
||||
intro s' heq_s'
|
||||
injection heq_s' with heq_state
|
||||
rw [← heq_state]
|
||||
rw [← heq_tys']
|
||||
exact ⟨StackMatches.cons (AvmVal.q16 _) h2, rfl⟩
|
||||
· -- subSatQ16
|
||||
rename_i ty tys_tail
|
||||
cases ty <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases tys_tail
|
||||
· unfold checkPrim at hprim; dsimp at hprim; contradiction
|
||||
· rename_i ty2 tys_tail'
|
||||
cases ty2 <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases hstack with
|
||||
| cons val1 h1 =>
|
||||
cases h1 with
|
||||
| cons val2 h2 =>
|
||||
cases val1; cases val2
|
||||
unfold checkPrim at hprim; dsimp at hprim
|
||||
injection hprim with heq_tys'
|
||||
unfold evalPrim pop1 push1
|
||||
dsimp
|
||||
split
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.stackOverflow, by intro s' h; contradiction⟩⟩
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.ok _, ?_⟩⟩
|
||||
intro s' heq_s'
|
||||
injection heq_s' with heq_state
|
||||
rw [← heq_state]
|
||||
rw [← heq_tys']
|
||||
exact ⟨StackMatches.cons (AvmVal.q16 _) h2, rfl⟩
|
||||
· -- mulSatQ16
|
||||
rename_i ty tys_tail
|
||||
cases ty <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases tys_tail
|
||||
· unfold checkPrim at hprim; dsimp at hprim; contradiction
|
||||
· rename_i ty2 tys_tail'
|
||||
cases ty2 <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases hstack with
|
||||
| cons val1 h1 =>
|
||||
cases h1 with
|
||||
| cons val2 h2 =>
|
||||
cases val1; cases val2
|
||||
unfold checkPrim at hprim; dsimp at hprim
|
||||
injection hprim with heq_tys'
|
||||
unfold evalPrim pop1 push1
|
||||
dsimp
|
||||
split
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.stackOverflow, by intro s' h; contradiction⟩⟩
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.ok _, ?_⟩⟩
|
||||
intro s' heq_s'
|
||||
injection heq_s' with heq_state
|
||||
rw [← heq_state]
|
||||
rw [← heq_tys']
|
||||
exact ⟨StackMatches.cons (AvmVal.q16 _) h2, rfl⟩
|
||||
· -- divSatQ16
|
||||
rename_i ty tys_tail
|
||||
cases ty <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases tys_tail
|
||||
· unfold checkPrim at hprim; dsimp at hprim; contradiction
|
||||
· rename_i ty2 tys_tail'
|
||||
cases ty2 <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases hstack with
|
||||
| cons val1 h1 =>
|
||||
cases h1 with
|
||||
| cons val2 h2 =>
|
||||
cases val1; cases val2
|
||||
unfold checkPrim at hprim; dsimp at hprim
|
||||
injection hprim with heq_tys'
|
||||
unfold evalPrim pop1 push1
|
||||
dsimp
|
||||
split
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.divByZero, by intro s' h; contradiction⟩⟩
|
||||
· split
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.stackOverflow, by intro s' h; contradiction⟩⟩
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.ok _, ?_⟩⟩
|
||||
intro s' heq_s'
|
||||
injection heq_s' with heq_state
|
||||
rw [← heq_state]
|
||||
rw [← heq_tys']
|
||||
exact ⟨StackMatches.cons (AvmVal.q16 _) h2, rfl⟩
|
||||
· -- ltQ16
|
||||
rename_i ty tys_tail
|
||||
cases ty <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases tys_tail
|
||||
· unfold checkPrim at hprim; dsimp at hprim; contradiction
|
||||
· rename_i ty2 tys_tail'
|
||||
cases ty2 <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases hstack with
|
||||
| cons val1 h1 =>
|
||||
cases h1 with
|
||||
| cons val2 h2 =>
|
||||
cases val1; cases val2
|
||||
unfold checkPrim at hprim; dsimp at hprim
|
||||
injection hprim with heq_tys'
|
||||
unfold evalPrim pop1 push1
|
||||
dsimp
|
||||
split
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.stackOverflow, by intro s' h; contradiction⟩⟩
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.ok _, ?_⟩⟩
|
||||
intro s' heq_s'
|
||||
injection heq_s' with heq_state
|
||||
rw [← heq_state]
|
||||
rw [← heq_tys']
|
||||
exact ⟨StackMatches.cons (AvmVal.b _) h2, rfl⟩
|
||||
· -- eqQ16
|
||||
rename_i ty tys_tail
|
||||
cases ty <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases tys_tail
|
||||
· unfold checkPrim at hprim; dsimp at hprim; contradiction
|
||||
· rename_i ty2 tys_tail'
|
||||
cases ty2 <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases hstack with
|
||||
| cons val1 h1 =>
|
||||
cases h1 with
|
||||
| cons val2 h2 =>
|
||||
cases val1; cases val2
|
||||
unfold checkPrim at hprim; dsimp at hprim
|
||||
injection hprim with heq_tys'
|
||||
unfold evalPrim pop1 push1
|
||||
dsimp
|
||||
split
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.stackOverflow, by intro s' h; contradiction⟩⟩
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.ok _, ?_⟩⟩
|
||||
intro s' heq_s'
|
||||
injection heq_s' with heq_state
|
||||
rw [← heq_state]
|
||||
rw [← heq_tys']
|
||||
exact ⟨StackMatches.cons (AvmVal.b _) h2, rfl⟩
|
||||
· -- and
|
||||
rename_i ty tys_tail
|
||||
cases ty <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases tys_tail
|
||||
· unfold checkPrim at hprim; dsimp at hprim; contradiction
|
||||
· rename_i ty2 tys_tail'
|
||||
cases ty2 <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases hstack with
|
||||
| cons val1 h1 =>
|
||||
cases h1 with
|
||||
| cons val2 h2 =>
|
||||
cases val1; cases val2
|
||||
unfold checkPrim at hprim; dsimp at hprim
|
||||
injection hprim with heq_tys'
|
||||
unfold evalPrim pop1 push1
|
||||
dsimp
|
||||
split
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.stackOverflow, by intro s' h; contradiction⟩⟩
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.ok _, ?_⟩⟩
|
||||
intro s' heq_s'
|
||||
injection heq_s' with heq_state
|
||||
rw [← heq_state]
|
||||
rw [← heq_tys']
|
||||
exact ⟨StackMatches.cons (AvmVal.b _) h2, rfl⟩
|
||||
· -- or
|
||||
rename_i ty tys_tail
|
||||
cases ty <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases tys_tail
|
||||
· unfold checkPrim at hprim; dsimp at hprim; contradiction
|
||||
· rename_i ty2 tys_tail'
|
||||
cases ty2 <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases hstack with
|
||||
| cons val1 h1 =>
|
||||
cases h1 with
|
||||
| cons val2 h2 =>
|
||||
cases val1; cases val2
|
||||
unfold checkPrim at hprim; dsimp at hprim
|
||||
injection hprim with heq_tys'
|
||||
unfold evalPrim pop1 push1
|
||||
dsimp
|
||||
split
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.stackOverflow, by intro s' h; contradiction⟩⟩
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.ok _, ?_⟩⟩
|
||||
intro s' heq_s'
|
||||
injection heq_s' with heq_state
|
||||
rw [← heq_state]
|
||||
rw [← heq_tys']
|
||||
exact ⟨StackMatches.cons (AvmVal.b _) h2, rfl⟩
|
||||
· -- not
|
||||
rename_i ty tys_tail
|
||||
cases ty <;> try (unfold checkPrim at hprim; dsimp at hprim; contradiction)
|
||||
cases hstack with
|
||||
| cons val h' =>
|
||||
cases val
|
||||
unfold checkPrim at hprim; dsimp at hprim
|
||||
injection hprim with heq_tys'
|
||||
unfold evalPrim pop1 push1
|
||||
dsimp
|
||||
split
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.stackOverflow, by intro s' h; contradiction⟩⟩
|
||||
· refine ⟨_, rfl, ⟨SafeOutcome.ok _, ?_⟩⟩
|
||||
intro s' heq_s'
|
||||
injection heq_s' with heq_state
|
||||
rw [← heq_state]
|
||||
rw [← heq_tys']
|
||||
exact ⟨StackMatches.cons (AvmVal.b _) h', rfl⟩
|
||||
|
||||
/-- Type preservation theorem for single instruction step. -/
|
||||
theorem step_preservation {instr : Instr} {s : State} {tys : List AvmTy} {tys_locals : List (Option AvmTy)}
|
||||
(hstack : StackMatches s.stack tys) (hlocals : LocalsMatches s.locals tys_locals)
|
||||
(hpc : s.pc = 0) (hhalt : s.halted = false)
|
||||
{tys' : List AvmTy} {tys_locals' : List (Option AvmTy)}
|
||||
(hcheck : checkInstr instr tys tys_locals = some (tys', tys_locals'))
|
||||
(hnon_jump : match instr with | .jump _ | .jumpIf _ | .halt => False | _ => True) :
|
||||
∃ o, step [instr] s = o ∧ SafeOutcome o ∧ (∀ s', o = Outcome.ok s' → StackMatches s'.stack tys' ∧ LocalsMatches s'.locals tys_locals') := by
|
||||
rcases s with ⟨pc, stack, locals, halted⟩
|
||||
subst hpc hhalt
|
||||
cases instr
|
||||
case push val =>
|
||||
unfold checkInstr at hcheck
|
||||
injection hcheck with h
|
||||
injection h with hst hloc
|
||||
subst hst hloc
|
||||
unfold step pop1
|
||||
simp
|
||||
split
|
||||
· refine ⟨SafeOutcome.stackOverflow, by intro s' h; contradiction⟩
|
||||
· refine ⟨SafeOutcome.ok _, ?_⟩
|
||||
intro s' heq_s'
|
||||
injection heq_s' with heq_state
|
||||
rw [← heq_state]
|
||||
exact ⟨StackMatches.cons val.val hstack, hlocals⟩
|
||||
case pop =>
|
||||
unfold checkInstr at hcheck
|
||||
dsimp at hcheck
|
||||
split at hcheck
|
||||
case h_1 x xs heq =>
|
||||
injection hcheck with h
|
||||
injection h with hst hloc
|
||||
subst hst hloc
|
||||
cases hstack
|
||||
unfold step pop1
|
||||
simp
|
||||
refine ⟨SafeOutcome.ok _, ⟨by assumption, hlocals⟩⟩
|
||||
case h_2 heq => contradiction
|
||||
case dup =>
|
||||
unfold checkInstr at hcheck
|
||||
dsimp at hcheck
|
||||
split at hcheck
|
||||
case h_1 x xs heq =>
|
||||
injection hcheck with h
|
||||
injection h with hst hloc
|
||||
subst hst hloc
|
||||
cases hstack with
|
||||
| cons val h' =>
|
||||
unfold step pop1
|
||||
simp
|
||||
split
|
||||
· refine ⟨SafeOutcome.stackOverflow, by intro s' h; contradiction⟩
|
||||
· refine ⟨SafeOutcome.ok _, ?_⟩
|
||||
intro s' heq_s'
|
||||
injection heq_s' with heq_state
|
||||
rw [← heq_state]
|
||||
exact ⟨StackMatches.cons val (StackMatches.cons val h'), hlocals⟩
|
||||
case h_2 heq => contradiction
|
||||
case swap =>
|
||||
unfold checkInstr at hcheck
|
||||
dsimp at hcheck
|
||||
split at hcheck
|
||||
case h_1 x y xs heq =>
|
||||
injection hcheck with h
|
||||
injection h with hst hloc
|
||||
subst hst hloc
|
||||
cases hstack with
|
||||
| cons val1 h1 =>
|
||||
cases h1 with
|
||||
| cons val2 h2 =>
|
||||
unfold step pop1
|
||||
simp
|
||||
refine ⟨SafeOutcome.ok _, ⟨StackMatches.cons val2 (StackMatches.cons val1 h2), hlocals⟩⟩
|
||||
case h_2 heq => contradiction
|
||||
case load i =>
|
||||
unfold checkInstr at hcheck
|
||||
dsimp at hcheck
|
||||
split at hcheck <;> try contradiction
|
||||
rename_i ty heq
|
||||
injection hcheck with h
|
||||
injection h with hst hloc
|
||||
subst hst hloc
|
||||
obtain ⟨val, hloc_val⟩ := getLocal_safety hlocals i heq
|
||||
simp [getLocal?] at hloc_val
|
||||
unfold step pop1 getLocal?
|
||||
simp
|
||||
rw [hloc_val]
|
||||
simp
|
||||
split
|
||||
· refine ⟨SafeOutcome.stackOverflow, by intro s' h; contradiction⟩
|
||||
· refine ⟨SafeOutcome.ok _, ?_⟩
|
||||
intro s' heq_s'
|
||||
injection heq_s' with heq_state
|
||||
rw [← heq_state]
|
||||
exact ⟨StackMatches.cons val hstack, hlocals⟩
|
||||
case store i =>
|
||||
unfold checkInstr at hcheck
|
||||
dsimp at hcheck
|
||||
split at hcheck
|
||||
· -- isTrue
|
||||
rename_i hbound
|
||||
split at hcheck
|
||||
case h_1 ty tys_tail heq =>
|
||||
injection hcheck with h
|
||||
injection h with hst hloc
|
||||
subst hst hloc
|
||||
cases hstack with
|
||||
| cons val h' =>
|
||||
unfold step pop1 setLocal?
|
||||
simp
|
||||
have hbound_locals : i < locals.length := by
|
||||
rw [hlocals.1]
|
||||
exact hbound
|
||||
rw [if_pos hbound_locals]
|
||||
simp
|
||||
refine ⟨SafeOutcome.ok _, ⟨h', setLocal_safety hlocals i val hbound⟩⟩
|
||||
case h_2 heq => contradiction
|
||||
· -- isFalse
|
||||
contradiction
|
||||
case jump target => contradiction
|
||||
case jumpIf target => contradiction
|
||||
case prim p =>
|
||||
unfold checkInstr at hcheck
|
||||
dsimp at hcheck
|
||||
split at hcheck
|
||||
case h_1 stack' heq =>
|
||||
injection hcheck with h
|
||||
injection h with hst hloc
|
||||
subst hst hloc
|
||||
obtain ⟨o, heval, hsafe, hst'⟩ := evalPrim_safety hstack heq
|
||||
unfold step
|
||||
have hlookup : [Instr.prim p][0]? = some (Instr.prim p) := rfl
|
||||
rw [hlookup]
|
||||
simp
|
||||
rw [heval]
|
||||
cases o
|
||||
case ok s1 =>
|
||||
refine ⟨SafeOutcome.ok _, ?_⟩
|
||||
intro s' heq_s'
|
||||
injection heq_s' with heq_state
|
||||
rw [← heq_state]
|
||||
obtain ⟨hst_matches, hlocals_eq⟩ := hst' s1 rfl
|
||||
dsimp at hlocals hlocals_eq ⊢
|
||||
rw [hlocals_eq]
|
||||
exact ⟨hst_matches, hlocals⟩
|
||||
case err e =>
|
||||
refine ⟨hsafe, by intro s' h; contradiction⟩
|
||||
case h_2 heq => contradiction
|
||||
case halt => contradiction
|
||||
|
||||
end SilverSight.AVMIsa
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue