| -- Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| -- Use of this source code is governed by a BSD-style license that can be | |
| -- found in the LICENSE file. | |
| import Extracted | |
| import Assumptions | |
| import Spec | |
| import Proof.ApiConverterLinkerExec | |
| import Proof.LinkerCorrespondence | |
| namespace StarnixEbpf.Proof | |
| set_option linter.auxLemma false | |
| open Aeneas.Std | |
| open Aeneas.Std.Result | |
| open StarnixEbpf.Assumptions | |
| open StarnixEbpf.Spec | |
| open StarnixEbpf.ApiConverterLinkerExec | |
| open StarnixEbpf.LinkerCorrespondence | |
| -- ============================================================================ | |
| -- Concrete Satisfiability / Non-Vacuity Witness for `BackendSimulatesProgram` | |
| -- ============================================================================ | |
| private theorem usize_max_ge_31 : 2 ^ 31 ≤ Usize.max := by | |
| rcases System.Platform.numBits_eq with hnb | hnb <;> | |
| simp [Usize.max, Usize.numBits, UScalarTy.Usize_numBits_eq, hnb] | |
| private def mkUsizeSmall (n : Nat) : Usize := | |
| ⟨BitVec.ofNat System.Platform.numBits n⟩ | |
| private theorem mkUsizeSmall_val (n : Nat) (h : n < 2 ^ 31) : | |
| (mkUsizeSmall n).val = n := by | |
| change (BitVec.ofNat System.Platform.numBits n).toNat = n | |
| rw [BitVec.toNat_ofNat] | |
| rcases System.Platform.numBits_eq with hnb | hnb <;> simp [hnb] <;> omega | |
| private theorem usize_eq_of_val {a b : Usize} (h : a.val = b.val) : a = b := by | |
| cases a; cases b | |
| simp only [UScalar.val] at h | |
| congr 1 | |
| exact BitVec.eq_of_toNat_eq h | |
| private theorem usize_add_small (a b : Usize) (ka kb : Nat) (hk : ka + kb < 100) | |
| (ha : a.val = ka) (hb : b.val = kb) : | |
| (a + b : Result Usize) = Result.ok (mkUsizeSmall (ka + kb)) := by | |
| change UScalar.add a b = _ | |
| unfold UScalar.add UScalar.tryMk | |
| have hopt := UScalar.tryMkOpt_eq .Usize (a.val + b.val) | |
| have hin : UScalar.inBounds .Usize (a.val + b.val) := by | |
| simp only [UScalar.inBounds, ha, hb] | |
| rcases System.Platform.numBits_eq with hnb | hnb <;> | |
| simp [UScalarTy.Usize_numBits_eq, hnb] <;> omega | |
| cases h : UScalar.tryMkOpt .Usize (a.val + b.val) with | |
| | none => | |
| rw [h] at hopt | |
| exact absurd hin hopt | |
| | some z => | |
| rw [h] at hopt | |
| simp only [Result.ofOption] | |
| congr 1 | |
| apply usize_eq_of_val | |
| rw [hopt.1, ha, hb, mkUsizeSmall_val (ka + kb) (by omega)] | |
| private theorem usize_add_one_small (a : Usize) (k : Nat) (hk : k < 99) (ha : a.val = k) : | |
| (a + 1#usize : Result Usize) = Result.ok (mkUsizeSmall (k + 1)) := | |
| usize_add_small a 1#usize k 1 (by omega) ha (by | |
| rcases System.Platform.numBits_eq with hnb | hnb <;> | |
| simp [UScalar.val, UScalarTy.Usize_numBits_eq, hnb]) | |
| private theorem usize_add_two_small (a : Usize) (k : Nat) (hk : k < 98) (ha : a.val = k) : | |
| (a + 2#usize : Result Usize) = Result.ok (mkUsizeSmall (k + 2)) := | |
| usize_add_small a 2#usize k 2 (by omega) ha (by | |
| rcases System.Platform.numBits_eq with hnb | hnb <;> | |
| simp [UScalar.val, UScalarTy.Usize_numBits_eq, hnb]) | |
| private theorem u64_shl_i32_ok (x : U64) (s : I32) | |
| (hge : s.val ≥ 0) (hlt : s.toNat < 64) : | |
| (x <<< s : Result U64) = .ok ⟨x.bv <<< s.toNat⟩ := by | |
| change UScalar.shiftLeft_IScalar x s = _ | |
| simp only [UScalar.shiftLeft_IScalar, hge, ↓reduceIte, UScalar.shiftLeft, | |
| UScalarTy.numBits, hlt] | |
| rfl | |
| private theorem u64_shr_i32_ok (x : U64) (s : I32) | |
| (hge : s.val ≥ 0) (hlt : s.toNat < 64) : | |
| (x >>> s : Result U64) = .ok ⟨x.bv >>> s.toNat⟩ := by | |
| change UScalar.shiftRight_IScalar x s = _ | |
| simp only [UScalar.shiftRight_IScalar, hge, ↓reduceIte, UScalar.shiftRight, | |
| UScalarTy.numBits, hlt] | |
| rfl | |
| private theorem bpf_cls_mask_val : ebpf_core.api.BPF_CLS_MASK = 7#u8 := by | |
| unfold ebpf_core.api.BPF_CLS_MASK ebpf_core.api.BPF_LD ebpf_core.api.BPF_LDX | |
| ebpf_core.api.BPF_ST ebpf_core.api.BPF_STX ebpf_core.api.BPF_ALU | |
| ebpf_core.api.BPF_JMP ebpf_core.api.BPF_JMP32 ebpf_core.api.BPF_ALU64; decide | |
| private theorem bpf_lddw_val : ebpf_core.api.BPF_LDDW = ⟨24#8⟩ := by | |
| unfold ebpf_core.api.BPF_LDDW ebpf_core.api.BPF_LD ebpf_core.api.BPF_DW; decide | |
| private theorem bpf_jmp_call_val : | |
| (ebpf_core.api.BPF_JMP ||| ebpf_core.api.BPF_CALL) = ⟨0x85#8⟩ := by | |
| unfold ebpf_core.api.BPF_JMP ebpf_core.api.BPF_CALL; decide | |
| -- `EbpfInstruction` accessors built on `u64_shr_i32_ok`, `u64_shl_i32_ok`, and `ebpf_offset_spec` | |
| -- from `Proof/LinkerCorrespondence.lean`. | |
| private theorem ei_code_ok (w : U64) : | |
| ebpf_core.api.EbpfInstruction.code w = Result.ok (UScalar.cast .U8 w) := rfl | |
| private theorem ei_dst_ok (w : U64) : | |
| ebpf_core.api.EbpfInstruction.dst_reg w = | |
| Result.ok (UScalar.cast .U8 (⟨(w.bv &&& 3840#64) >>> 8⟩ : U64)) := by | |
| unfold ebpf_core.api.EbpfInstruction.dst_reg | |
| simp only [lift, Aeneas.Std.bind_ok] | |
| rw [u64_shr_i32_ok _ 8#i32 (by decide) (by decide), Aeneas.Std.bind_ok] | |
| rfl | |
| private theorem ei_src_ok (w : U64) : | |
| ebpf_core.api.EbpfInstruction.src_reg w = | |
| Result.ok (UScalar.cast .U8 (⟨(w.bv &&& 61440#64) >>> 12⟩ : U64)) := by | |
| unfold ebpf_core.api.EbpfInstruction.src_reg | |
| simp only [lift, Aeneas.Std.bind_ok] | |
| rw [u64_shr_i32_ok _ 12#i32 (by decide) (by decide), Aeneas.Std.bind_ok] | |
| rfl | |
| private theorem ei_imm_ok (w : U64) : | |
| ebpf_core.api.EbpfInstruction.imm w = | |
| Result.ok (UScalar.hcast .I32 (UScalar.cast .U32 (⟨w.bv >>> 32⟩ : U64))) := by | |
| unfold ebpf_core.api.EbpfInstruction.imm | |
| rw [u64_shr_i32_ok w 32#i32 (by decide) (by decide), Aeneas.Std.bind_ok] | |
| simp only [lift, Aeneas.Std.bind_ok] | |
| rfl | |
| private theorem ei_set_src0_ok (w : U64) : | |
| ebpf_core.api.EbpfInstruction.set_src_reg w 0#u8 = | |
| Result.ok ((w &&& ~~~61440#u64) ||| ⟨(UScalar.cast .U64 0#u8).bv <<< 12⟩) := by | |
| unfold ebpf_core.api.EbpfInstruction.set_src_reg | |
| have hlt : ((0#u8 : U8) < 16#u8) := by decide | |
| simp only [massert, hlt, ↓reduceIte, lift, Aeneas.Std.bind_ok] | |
| rw [u64_shl_i32_ok _ 12#i32 (by decide) (by decide), Aeneas.Std.bind_ok] | |
| have h12 : I32.toNat 12#i32 = 12 := by decide | |
| rw [h12] | |
| private theorem ei_set_imm_ok (w : U64) (imm : I32) : | |
| ebpf_core.api.EbpfInstruction.set_imm w imm = | |
| Result.ok ((w &&& 4294967295#u64) ||| ⟨(UScalar.cast .U64 (IScalar.hcast .U32 imm)).bv <<< 32⟩) := by | |
| unfold ebpf_core.api.EbpfInstruction.set_imm | |
| simp only [lift, Aeneas.Std.bind_ok] | |
| rw [u64_shl_i32_ok _ 32#i32 (by decide) (by decide), Aeneas.Std.bind_ok] | |
| have h32 : I32.toNat 32#i32 = 32 := by decide | |
| rw [h32] | |
| private theorem u8_and15_small (k : Nat) (hk : k < 16) : | |
| (⟨BitVec.ofNat 8 k⟩ : U8) &&& 15#u8 = ⟨BitVec.ofNat 8 k⟩ := by | |
| apply UScalar.eq_of_val_eq | |
| change (BitVec.ofNat 8 k &&& 15#8).toNat = (BitVec.ofNat 8 k).toNat | |
| match k with | |
| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 => decide | |
| | n + 16 => omega | |
| private theorem usize_cast_u8_val (k : Nat) (hk : k < 16) : | |
| (UScalar.cast .Usize (⟨BitVec.ofNat 8 k⟩ : U8)).val = k := by | |
| change (BitVec.setWidth System.Platform.numBits (BitVec.ofNat 8 k)).toNat = k | |
| rw [BitVec.toNat_setWidth, BitVec.toNat_ofNat] | |
| rcases System.Platform.numBits_eq with hnb | hnb <;> simp [hnb] <;> omega | |
| private theorem array16_len (a : Array U64 16#usize) : a.val.length = 16 := by | |
| have h := a.property | |
| have h16 : (16#usize : Usize).val = 16 := by | |
| rcases System.Platform.numBits_eq with hnb | hnb <;> | |
| simp [UScalar.val, UScalarTy.Usize_numBits_eq, hnb] | |
| omega | |
| private theorem reg_interp_small (istate : ebpf_core.executor.InterpreterState) | |
| (k : Nat) (hk : k < 16) : | |
| ebpf_core.executor.InterpreterState.reg istate ⟨BitVec.ofNat 8 k⟩ = | |
| Result.ok (istate.registers.val[k]'(by have := array16_len istate.registers; omega)) := by | |
| unfold ebpf_core.executor.InterpreterState.reg | |
| simp only [lift, Aeneas.Std.bind_ok] | |
| rw [u8_and15_small k hk] | |
| unfold Array.index_usize | |
| have hval := usize_cast_u8_val k hk | |
| have hlen := array16_len istate.registers | |
| have hget : istate.registers[UScalar.cast .Usize (⟨BitVec.ofNat 8 k⟩ : U8)]? = | |
| some (istate.registers.val[k]'(by omega)) := by | |
| change istate.registers.val[(UScalar.cast .Usize (⟨BitVec.ofNat 8 k⟩ : U8)).val]? = _ | |
| rw [hval, List.getElem?_eq_getElem (by omega)] | |
| rw [hget] | |
| private theorem set_reg_interp_small (istate : ebpf_core.executor.InterpreterState) | |
| (k : Nat) (hk : k < 16) (v : U64) : | |
| ∃ regs' : Array U64 16#usize, | |
| ebpf_core.executor.InterpreterState.set_reg istate ⟨BitVec.ofNat 8 k⟩ v = | |
| Result.ok { istate with registers := regs' } ∧ | |
| regs'.val = istate.registers.val.set k v := by | |
| unfold ebpf_core.executor.InterpreterState.set_reg | |
| simp only [lift, Aeneas.Std.bind_ok] | |
| rw [u8_and15_small k hk] | |
| have hval := usize_cast_u8_val k hk | |
| have hlen := array16_len istate.registers | |
| have hget : istate.registers[UScalar.cast .Usize (⟨BitVec.ofNat 8 k⟩ : U8)]? = | |
| some (istate.registers.val[k]'(by omega)) := by | |
| change istate.registers.val[(UScalar.cast .Usize (⟨BitVec.ofNat 8 k⟩ : U8)).val]? = _ | |
| rw [hval, List.getElem?_eq_getElem (by omega)] | |
| have hupd : ∃ regs' : Array U64 16#usize, | |
| Array.update istate.registers (UScalar.cast .Usize (⟨BitVec.ofNat 8 k⟩ : U8)) v = Result.ok regs' ∧ | |
| regs'.val = istate.registers.val.set k v := by | |
| unfold Array.update | |
| rw [hget] | |
| exact ⟨_, rfl, by simp [hval]⟩ | |
| rcases hupd with ⟨regs', hupd, hregs'_val⟩ | |
| rw [hupd, Aeneas.Std.bind_ok] | |
| exact ⟨regs', rfl, hregs'_val⟩ | |
| private theorem reg_interp_u8 (istate : ebpf_core.executor.InterpreterState) | |
| (r : U8) (k : Nat) (hk : k < 16) (hr : r = ⟨BitVec.ofNat 8 k⟩) : | |
| ebpf_core.executor.InterpreterState.reg istate r = | |
| Result.ok (istate.registers.val[k]'(by have := array16_len istate.registers; omega)) := by | |
| subst hr; exact reg_interp_small istate k hk | |
| private theorem set_reg_interp_u8 (istate : ebpf_core.executor.InterpreterState) | |
| (r : U8) (k : Nat) (hk : k < 16) (hr : r = ⟨BitVec.ofNat 8 k⟩) (v : U64) : | |
| ∃ regs' : Array U64 16#usize, | |
| ebpf_core.executor.InterpreterState.set_reg istate r v = | |
| Result.ok { istate with registers := regs' } ∧ | |
| regs'.val = istate.registers.val.set k v := by | |
| subst hr; exact set_reg_interp_small istate k hk v | |
| private theorem advance_pc_one_small (istate : ebpf_core.executor.InterpreterState) | |
| (k : Nat) (hk : k < 100) (hpc : istate.pc.val = k) : | |
| ebpf_core.executor.InterpreterState.advance_pc istate 1#i16 = | |
| Result.ok { istate with pc := mkUsizeSmall (k + 1) } := by | |
| unfold ebpf_core.executor.InterpreterState.advance_pc | |
| simp only [lift, Aeneas.Std.bind_ok] | |
| congr 2 | |
| apply usize_eq_of_val | |
| rw [mkUsizeSmall_val (k + 1) (by omega)] | |
| have hpc_bv : istate.pc.bv = BitVec.ofNat System.Platform.numBits k := by | |
| apply BitVec.eq_of_toNat_eq | |
| have htv : istate.pc.bv.toNat = k := hpc | |
| rcases System.Platform.numBits_eq with hnb | hnb <;> | |
| simp only [hnb, BitVec.toNat_ofNat] <;> omega | |
| change (BitVec.signExtend System.Platform.numBits | |
| (BitVec.setWidth 64 istate.pc.bv + BitVec.signExtend 64 (1#16))).toNat = k + 1 | |
| rw [hpc_bv] | |
| rcases System.Platform.numBits_eq with hnb | hnb | |
| · rw [hnb] | |
| have h1 : BitVec.setWidth 64 (BitVec.ofNat 32 k) = BitVec.ofNat 64 k := by | |
| apply BitVec.eq_of_toNat_eq; simp [BitVec.toNat_setWidth]; omega | |
| rw [h1] | |
| have h2 : BitVec.ofNat 64 k + BitVec.signExtend 64 1#16 = BitVec.ofNat 64 (k + 1) := by | |
| apply BitVec.eq_of_toNat_eq | |
| change (k % 2 ^ 64 + 1) % 2 ^ 64 = (k + 1) % 2 ^ 64 | |
| omega | |
| rw [h2, BitVec.signExtend] | |
| have hti : (BitVec.ofNat 64 (k + 1)).toInt = Int.ofNat (k + 1) := by | |
| rw [BitVec.toInt_eq_toNat_cond, ite_eq_left_of_eq_true _ _ (eq_true (by simp; omega))] | |
| simp; omega | |
| rw [hti] | |
| change (BitVec.ofNat 32 (k + 1)).toNat = k + 1 | |
| rw [BitVec.toNat_ofNat]; omega | |
| · rw [hnb] | |
| have h1 : BitVec.setWidth 64 (BitVec.ofNat 64 k) = BitVec.ofNat 64 k := by | |
| apply BitVec.eq_of_toNat_eq; simp | |
| rw [h1] | |
| have h2 : BitVec.ofNat 64 k + BitVec.signExtend 64 1#16 = BitVec.ofNat 64 (k + 1) := by | |
| apply BitVec.eq_of_toNat_eq | |
| change (k % 2 ^ 64 + 1) % 2 ^ 64 = (k + 1) % 2 ^ 64 | |
| omega | |
| rw [h2, BitVec.signExtend_eq] | |
| simp; omega | |
| private theorem loop_unfold_sat {α β : Type} | |
| (body : α → Result (ControlFlow α β)) (x : α) : | |
| loop body x = (do | |
| let r ← body x | |
| match r with | |
| | .cont x' => loop body x' | |
| | .done y => Result.ok y) := by | |
| rw [loop.eq_def] | |
| rfl | |
| -- Forward `Slice`/`Vec` indexing helpers using `Slice.getElem?_Usize_eq` and `Slice.set_val_eq` | |
| private theorem slice_index_at {α : Type} (s : Slice α) (k : Nat) (hk : k < 2 ^ 31) | |
| (hlt : k < s.val.length) : | |
| Slice.index_usize s (mkUsizeSmall k) = Result.ok (s.val[k]'hlt) := by | |
| unfold Slice.index_usize | |
| rw [Slice.getElem?_Usize_eq, mkUsizeSmall_val k hk, List.getElem?_eq_getElem hlt] | |
| private theorem vec_index_at {α : Type} (v : alloc.vec.Vec α) (k : Nat) (hk : k < 2 ^ 31) | |
| (hlt : k < v.val.length) : | |
| alloc.vec.Vec.index (core.slice.index.SliceIndexUsizeSlice α) v (mkUsizeSmall k) = | |
| Result.ok (v.val[k]'hlt) := by | |
| unfold alloc.vec.Vec.index core.slice.index.SliceIndexUsizeSlice core.slice.index.Usize.index | |
| exact slice_index_at v.slice k hk hlt | |
| private theorem vec_index_mut_at {α : Type} (v : alloc.vec.Vec α) (k : Nat) (hk : k < 2 ^ 31) | |
| (hlt : k < v.val.length) : | |
| ∃ back : α → alloc.vec.Vec α, | |
| alloc.vec.Vec.index_mut (core.slice.index.SliceIndexUsizeSlice α) v (mkUsizeSmall k) = | |
| Result.ok (v.val[k]'hlt, back) ∧ | |
| ∀ u, (back u).val = v.val.set k u := by | |
| unfold alloc.vec.Vec.index_mut core.slice.index.SliceIndexUsizeSlice | |
| core.slice.index.Usize.index_mut Slice.index_mut_usize | |
| dsimp only | |
| rw [slice_index_at v.slice k hk hlt] | |
| refine ⟨fun o' => { slice := v.slice.set (mkUsizeSmall k) o' }, ?_, ?_⟩ | |
| · simp only [Bind.bind, Pure.pure, Aeneas.Std.bind_ok]; rfl | |
| · intro u | |
| simp [alloc.vec.Vec.val, Slice.set_val_eq, mkUsizeSmall_val k hk] | |
| -- Concrete witness objects | |
| private def witnessArrId : MemoryId := .mk .Hardcoded ⟨1#64⟩ none | |
| private def witnessCtxId : MemoryId := .mk .Hardcoded ⟨2#64⟩ none | |
| private def witnessLayout : HostLayout where | |
| stackBase := 0x0000_0001_0000_0000#64 | |
| bufBase _ := 0x0000_0002_0000_0000#64 | |
| mapDataAddr _ := 0x0000_0003_0000_0000#64 | |
| structBase _ := 0x0000_0004_0000_0000#64 | |
| arrayBase _ ep := if ep = 0 then 0x0000_0005_0000_0000#64 else 0x0000_0006_0000_0000#64 | |
| mapAddr _ := 0x0000_0007_0000_0000#64 | |
| private theorem witnessLayout_valid : ValidHostLayout witnessLayout := by | |
| have h1 : 0#64 < 0x0000_0001_0000_0000#64 ∧ 0x0000_0001_0000_0000#64 ≤ CANONICAL_USER_MAX := by decide | |
| have h2 : 0#64 < 0x0000_0002_0000_0000#64 ∧ 0x0000_0002_0000_0000#64 ≤ CANONICAL_USER_MAX := by decide | |
| have h3 : 0#64 < 0x0000_0003_0000_0000#64 ∧ 0x0000_0003_0000_0000#64 ≤ CANONICAL_USER_MAX := by decide | |
| have h4 : 0#64 < 0x0000_0004_0000_0000#64 ∧ 0x0000_0004_0000_0000#64 ≤ CANONICAL_USER_MAX := by decide | |
| have h5a : 0#64 < 0x0000_0005_0000_0000#64 ∧ 0x0000_0005_0000_0000#64 ≤ CANONICAL_USER_MAX := by decide | |
| have h5b : 0#64 < 0x0000_0006_0000_0000#64 ∧ 0x0000_0006_0000_0000#64 ≤ CANONICAL_USER_MAX := by decide | |
| have h6 : 0x0000_0007_0000_0000#64 ≠ 0#64 := by decide | |
| refine ⟨h1, fun _ => h2, fun _ => h3, fun _ => h4, ?_, fun _ => h6⟩ | |
| intro _ ep | |
| cases ep | |
| · exact h5a | |
| · exact h5b | |
| private def witnessSecret : SecretInit where | |
| stackBits _ := 0#64 | |
| regBits _ _ := 0#64 | |
| preserveCallScratch := true | |
| private def witnessMapSchemaCore : ebpf_core.maps.MapSchema where | |
| map_type := ebpf_core.api.BPF_MAP_TYPE_ARRAY | |
| key_size := 4#u32 | |
| value_size := 8#u32 | |
| max_entries := 1#u32 | |
| flags := 0#u32 | |
| private def witnessMapSpec : MapSpec := MapSpec.ofCore witnessMapSchemaCore | |
| private def witnessHelperSpec : HelperSpec where | |
| args := [] | |
| ret := .scalar false | |
| invalidateArrayBounds := true | |
| private def witnessEnv : ExecEnv where | |
| maps i := if i = 0 then some witnessMapSpec else none | |
| structFields _ := [] | |
| entryContextArgs _ := none | |
| packetCtx := some (.structCtx witnessCtxId) | |
| allowPtrLeaks := false | |
| helpers i := if i = 1 then some witnessHelperSpec else none | |
| helperScalarOracle _ _ := 0#64 | |
| helperNullOracle _ _ := false | |
| helperBufWriteOracle _ _ _ := 0#8 | |
| helperArrayLenOracle _ _ := 10#64 | |
| helperArrayBytesOracle _ _ _ := 0#8 | |
| helperMapBufOracle _ _ := .mapValue 0 | |
| packetOracle _ _ _ := some 100#64 | |
| private def witnessVerifiedRawList : List ebpf_core.api.EbpfInstruction := | |
| [ 0x0000_0000_0000_5118#u64, | |
| 0x0000_0000_0000_0000#u64, | |
| 0x0000_0000_0000_6218#u64, | |
| 0x0000_0000_0000_0000#u64, | |
| 0x0000_0000_0000_6318#u64, | |
| 0x0000_0000_0000_0000#u64, | |
| 0x0000_002A_0000_04B7#u64, | |
| 0x0000_0000_0000_427B#u64, | |
| 0x0000_0000_0000_3879#u64, | |
| 0x0000_0001_0000_0085#u64, | |
| 0x0000_0000_0000_0020#u64, | |
| 0x0000_0000_0000_800F#u64, | |
| 0x0000_0000_0000_0095#u64 ] | |
| private def witnessLinkedRawList : List ebpf_core.api.EbpfInstruction := | |
| [ 0x0000_0000_0000_0118#u64, | |
| 0x0000_0007_0000_0000#u64, | |
| 0x0000_0000_0000_0218#u64, | |
| 0x0000_0003_0000_0000#u64, | |
| 0x0000_0000_0000_0318#u64, | |
| 0x0000_0003_0000_0000#u64, | |
| 0x0000_002A_0000_04B7#u64, | |
| 0x0000_0000_0000_427B#u64, | |
| 0x0000_0000_0000_3879#u64, | |
| 0x0000_0001_0000_0085#u64, | |
| 0x0000_0000_0000_0020#u64, | |
| 0x0000_0000_0000_800F#u64, | |
| 0x0000_0000_0000_0095#u64 ] | |
| private def mkVecSmall {α : Type} (l : List α) (h : l.length < 2 ^ 31) : alloc.vec.Vec α := | |
| alloc.vec.Vec.from l (by have := usize_max_ge_31; omega) | |
| private theorem mkVecSmall_val {α : Type} (l : List α) (h : l.length < 2 ^ 31) : | |
| (mkVecSmall l h).val = l := | |
| alloc.vec.Vec.from_val l _ | |
| private def witnessVerified : ebpf_core.verifier.VerifiedEbpfProgram where | |
| code := mkVecSmall witnessVerifiedRawList (by decide) | |
| args := mkVecSmall [] (by decide) | |
| struct_access_instructions := mkVecSmall [] (by decide) | |
| maps := mkVecSmall [witnessMapSchemaCore] (by decide) | |
| private def witnessLinkage : ebpf_core.program.ProgramLinkageInfo where | |
| struct_mappings := mkVecSmall [] (by decide) | |
| helpers := mkVecSmall [{ id := 1#u32, fn_addr := 0x0000_0008_0000_0000#u64 }] (by decide) | |
| maps := mkVecSmall [{ map_addr := 0x0000_0007_0000_0000#u64, | |
| data_addr := some 0x0000_0003_0000_0000#u64 }] (by decide) | |
| packet_load_fn_addr := some 0x0000_0009_0000_0000#u64 | |
| private def witnessLinkedCode : alloc.vec.Vec ebpf_core.api.EbpfInstruction := | |
| mkVecSmall witnessLinkedRawList (by decide) | |
| private theorem witnessLinkage_conforms : LinkageConformsLayout witnessLinkage witnessLayout := by | |
| intro idx hlt | |
| have h0 : idx = 0 := by simp [witnessLinkage, mkVecSmall_val] at hlt; omega | |
| subst h0 | |
| refine ⟨by simp [witnessLinkage, mkVecSmall_val, witnessLayout], ?_⟩ | |
| intro addr hda | |
| simp only [witnessLinkage, mkVecSmall_val, List.getElem_cons_zero] at hda | |
| cases Option.some.inj hda | |
| rfl | |
| private def witnessProgList : List Insn := | |
| [ .lddwMap 1 0, | |
| .lddwCont, | |
| .lddwMapValue 2 0 0#64, | |
| .lddwCont, | |
| .lddwMapValue 3 0 0#64, | |
| .lddwCont, | |
| .alu64 .mov 4 (.imm 42#64), | |
| .store .u64 2 0#16 (.reg 4), | |
| .load .u64 8 3 0#16, | |
| .callExternal 1, | |
| .loadPacket .u32 0#32 none, | |
| .alu64 .add 0 (.reg 8), | |
| .exit ] | |
| private def witnessProg : Program := witnessProgList.toArray | |
| private theorem witness_decode_ok : | |
| decodeVerifiedCode witnessVerified.code.val = some witnessProg := by | |
| unfold decodeVerifiedCode witnessVerified witnessProg witnessProgList | |
| rw [mkVecSmall_val] | |
| decide | |
| private theorem witness_sa_wf : | |
| StructAccessesWellFormed witnessVerified.struct_access_instructions.val witnessProg := by | |
| simp [StructAccessesWellFormed, witnessVerified, mkVecSmall_val] | |
| -- Linking proof for `witnessVerified` and `witnessLinkage` | |
| -- Reuses `link_instructions_loop0_eq` and `ebpf_offset_spec` from `Proof/ApiConverterLinkerExec.lean`. | |
| private theorem link_loop0_terminates (src : alloc.vec.Vec ebpf_core.api.EbpfInstruction) | |
| (hlen : src.val.length < 100) : | |
| ∀ n (acc : alloc.vec.Vec ebpf_core.api.EbpfInstruction) (i : Usize), | |
| src.val.length - i.val = n → | |
| i.val ≤ src.val.length → | |
| acc.val.length ≤ i.val → | |
| ∃ code', ebpf_core.program.link_instructions_loop0 src acc i = Result.ok code' := by | |
| intro n | |
| induction n with | |
| | zero => | |
| intro acc i hsub hle _ | |
| unfold ebpf_core.program.link_instructions_loop0 | |
| rw [loop_unfold_sat] | |
| unfold ebpf_core.program.link_instructions_loop0.body | |
| have hnotlt : ¬ (i < alloc.vec.Vec.len src) := by | |
| show ¬ (i.val < src.val.length); omega | |
| simp only [hnotlt, ↓reduceIte, Aeneas.Std.bind_ok] | |
| exact ⟨acc, rfl⟩ | |
| | succ k ih => | |
| intro acc i hsub hle hacc | |
| have hlt : i.val < src.val.length := by omega | |
| unfold ebpf_core.program.link_instructions_loop0 | |
| rw [loop_unfold_sat] | |
| unfold ebpf_core.program.link_instructions_loop0.body | |
| have hlt' : i < alloc.vec.Vec.len src := hlt | |
| simp only [hlt', ↓reduceIte] | |
| have hi_eq : i = mkUsizeSmall i.val := | |
| usize_eq_of_val (by rw [mkUsizeSmall_val i.val (by omega)]) | |
| have hidx : alloc.vec.Vec.index | |
| (core.slice.index.SliceIndexUsizeSlice ebpf_core.api.EbpfInstruction) src i = | |
| Result.ok (src.val[i.val]'hlt) := by | |
| have h0 := vec_index_at src i.val (by omega) hlt | |
| rwa [← hi_eq] at h0 | |
| rw [hidx, Aeneas.Std.bind_ok] | |
| have hpush : ∃ acc', | |
| alloc.vec.Vec.push acc (src.val[i.val]'hlt) = Result.ok acc' ∧ | |
| acc'.val.length = acc.val.length + 1 := by | |
| unfold alloc.vec.Vec.push; dsimp only | |
| have hpush_ok : (decide (acc.val.length + 1 ≤ U32.max) || | |
| decide (acc.val.length + 1 ≤ Usize.max)) = true := by | |
| have : acc.val.length + 1 ≤ Usize.max := by | |
| have := usize_max_ge_31; omega | |
| simp [this] | |
| simp only [hpush_ok, ↓reduceDIte] | |
| exact ⟨_, rfl, by simp [alloc.vec.Vec.from, alloc.vec.Vec.val]⟩ | |
| obtain ⟨acc', hpush_eq, hacc'_len⟩ := hpush | |
| rw [hpush_eq, Aeneas.Std.bind_ok] | |
| rw [usize_add_one_small i i.val (by omega) rfl, Aeneas.Std.bind_ok] | |
| simp only [Aeneas.Std.bind_ok] | |
| change ∃ code', ebpf_core.program.link_instructions_loop0 src acc' (mkUsizeSmall (i.val + 1)) = | |
| Result.ok code' | |
| exact ih acc' (mkUsizeSmall (i.val + 1)) | |
| (by rw [mkUsizeSmall_val (i.val + 1) (by omega)]; omega) | |
| (by rw [mkUsizeSmall_val (i.val + 1) (by omega)]; omega) | |
| (by rw [mkUsizeSmall_val (i.val + 1) (by omega)]; omega) | |
| private theorem link_loop0_copy (src : alloc.vec.Vec ebpf_core.api.EbpfInstruction) | |
| (hlen : src.val.length < 100) : | |
| ebpf_core.program.link_instructions_loop0 src | |
| (alloc.vec.Vec.new ebpf_core.api.EbpfInstruction) 0#usize = Result.ok src := by | |
| have h0val : (0#usize : Usize).val = 0 := by | |
| rcases System.Platform.numBits_eq with hnb | hnb <;> | |
| simp [UScalar.val, UScalarTy.Usize_numBits_eq, hnb] | |
| obtain ⟨code', hloop0⟩ := link_loop0_terminates src hlen src.val.length | |
| (alloc.vec.Vec.new ebpf_core.api.EbpfInstruction) 0#usize | |
| (by simp [h0val]) (Nat.zero_le _) (by simp [alloc.vec.Vec.new, h0val]) | |
| have hval := link_instructions_loop0_eq src (alloc.vec.Vec.new _) code' 0#usize | |
| (by simp [alloc.vec.Vec.new, h0val]) (Nat.zero_le _) hloop0 | |
| rwa [alloc.vec.Vec.ext code' src hval] at hloop0 | |
| private theorem witness_has_helper_1 : | |
| ebpf_core.program.has_linked_helper (alloc.vec.Vec.deref witnessLinkage.helpers) 1#u32 = | |
| Result.ok true := by | |
| unfold ebpf_core.program.has_linked_helper ebpf_core.program.has_linked_helper_loop | |
| rw [loop_unfold_sat] | |
| unfold ebpf_core.program.has_linked_helper_loop.body | |
| have h0 : (0#usize : Usize).val = 0 := by | |
| rcases System.Platform.numBits_eq with hnb | hnb <;> | |
| simp [UScalar.val, UScalarTy.Usize_numBits_eq, hnb] | |
| have h0eq : (0#usize : Usize) = mkUsizeSmall 0 := | |
| usize_eq_of_val (by rw [h0, mkUsizeSmall_val 0 (by omega)]) | |
| have hlt : (0#usize : Usize) < Slice.len (alloc.vec.Vec.deref witnessLinkage.helpers) := by | |
| show (0#usize : Usize).val < (alloc.vec.Vec.deref witnessLinkage.helpers).val.length | |
| simp [witnessLinkage, alloc.vec.Vec.deref, mkVecSmall_val, h0] | |
| simp only [hlt, ↓reduceIte] | |
| have hidx := slice_index_at (alloc.vec.Vec.deref witnessLinkage.helpers) 0 (by omega) | |
| (by simp [witnessLinkage, alloc.vec.Vec.deref, mkVecSmall_val]) | |
| rw [← h0eq] at hidx | |
| have hidx' : Slice.index_usize (alloc.vec.Vec.deref witnessLinkage.helpers) 0#usize = | |
| Result.ok { id := 1#u32, fn_addr := 0x0000_0008_0000_0000#u64 } := by | |
| simpa [witnessLinkage, alloc.vec.Vec.deref, mkVecSmall_val] using hidx | |
| rw [hidx', Aeneas.Std.bind_ok] | |
| have hne : (0x0000_0008_0000_0000#u64 != 0#u64) = true := by decide | |
| simp [hne, Aeneas.Std.bind_ok] | |
| private theorem link_lddw_vec_update {E : Type} | |
| (code code' : alloc.vec.Vec ebpf_core.api.EbpfInstruction) (pc : Nat) (hpc : pc < 12) | |
| (hlt_len : code.val.length = 13) (w0' w1' : ebpf_core.api.EbpfInstruction) | |
| (hcode' : code'.val = (code.val.set pc w0').set (pc + 1) w1') : | |
| Aeneas.Std.bind | |
| (alloc.vec.Vec.index_mut (core.slice.index.SliceIndexUsizeSlice ebpf_core.api.EbpfInstruction) | |
| code (mkUsizeSmall pc)) | |
| (fun p => Aeneas.Std.bind | |
| (alloc.vec.Vec.index_mut (core.slice.index.SliceIndexUsizeSlice ebpf_core.api.EbpfInstruction) | |
| (p.2 w0') (mkUsizeSmall (pc + 1))) | |
| (fun p1 => Result.ok (@core.result.Result.Ok Usize E 2#usize, p1.2 w1'))) = | |
| Result.ok (@core.result.Result.Ok Usize E 2#usize, code') := by | |
| obtain ⟨back1, hmut1, hback1⟩ := vec_index_mut_at code pc (by omega) (by omega) | |
| rw [hmut1, Aeneas.Std.bind_ok] | |
| have hlen1 : (back1 w0').val.length = 13 := by rw [hback1, List.length_set, hlt_len] | |
| obtain ⟨back2, hmut2, hback2⟩ := vec_index_mut_at (back1 w0') (pc + 1) (by omega) (by omega) | |
| rw [hmut2, Aeneas.Std.bind_ok] | |
| congr 2; apply alloc.vec.Vec.ext; rw [hback2, hback1, hcode'] | |
| private theorem link_single_witness_lddw | |
| (code code' : alloc.vec.Vec ebpf_core.api.EbpfInstruction) (pc : Nat) (hpc : pc < 12) | |
| (hlt_len : code.val.length = 13) | |
| (w0 w0' w1' : ebpf_core.api.EbpfInstruction) (srcReg : U8) | |
| (hcode' : code'.val = (code.val.set pc w0').set (pc + 1) w1') | |
| (hw0 : code.val[pc]'(by omega) = w0) | |
| (hw1 : code.val[pc + 1]'(by omega) = 0#u64) | |
| (hcode : ebpf_core.api.EbpfInstruction.code w0 = Result.ok ⟨24#8⟩) | |
| (hsrc : ebpf_core.api.EbpfInstruction.src_reg w0 = Result.ok srcReg) | |
| (himm : ebpf_core.api.EbpfInstruction.imm w0 = Result.ok 0#i32) | |
| (hsrcCase : | |
| (srcReg = 5#u8 ∧ w1' = 0x0000_0007_0000_0000#u64 ∧ | |
| ebpf_core.api.EbpfInstruction.set_imm w0' | |
| (UScalar.hcast .I32 0x0000_0007_0000_0000#u64) = Result.ok w0') ∨ | |
| (srcReg = 6#u8 ∧ w1' = 0x0000_0003_0000_0000#u64 ∧ | |
| ebpf_core.api.EbpfInstruction.set_imm w0' | |
| (UScalar.hcast .I32 0x0000_0003_0000_0000#u64) = Result.ok w0')) | |
| (hset_sr : ebpf_core.api.EbpfInstruction.set_src_reg w0 0#u8 = Result.ok w0') : | |
| ebpf_core.program.link_single_instruction (mkUsizeSmall pc) | |
| witnessVerified witnessLinkage true code = | |
| Result.ok (.Ok 2#usize, code') := by | |
| unfold ebpf_core.program.link_single_instruction | |
| rw [vec_index_at code pc (by omega) (by omega), hw0, Aeneas.Std.bind_ok] | |
| rw [hcode, Aeneas.Std.bind_ok, lift, Aeneas.Std.bind_ok] | |
| have hncall : ¬ ((⟨24#8⟩ : U8) = (ebpf_core.api.BPF_JMP ||| ebpf_core.api.BPF_CALL)) := by | |
| rw [bpf_jmp_call_val]; decide | |
| have hcls : ((⟨24#8⟩ : U8) &&& ebpf_core.api.BPF_CLS_MASK) = ebpf_core.api.BPF_LD := by | |
| rw [bpf_cls_mask_val]; unfold ebpf_core.api.BPF_LD; decide | |
| have hnabs : ¬ (((⟨24#8⟩ : U8) &&& 224#u8) = ebpf_core.api.BPF_ABS) := by | |
| unfold ebpf_core.api.BPF_ABS; decide | |
| have hnind : ¬ (((⟨24#8⟩ : U8) &&& 224#u8) = ebpf_core.api.BPF_IND) := by | |
| unfold ebpf_core.api.BPF_IND; decide | |
| have hlddw : (⟨24#8⟩ : U8) = ebpf_core.api.BPF_LDDW := bpf_lddw_val.symm | |
| rw [ite_eq_right_of_eq_false _ _ (eq_false hncall), lift, Aeneas.Std.bind_ok] | |
| rw [ite_eq_left_of_eq_true _ _ (eq_true hcls), lift, Aeneas.Std.bind_ok] | |
| rw [ite_eq_right_of_eq_false _ _ (eq_false hnabs), Aeneas.Std.bind_ok] | |
| rw [ite_eq_right_of_eq_false _ _ (eq_false hnind)] | |
| rw [ite_eq_left_of_eq_true _ _ (eq_true hlddw)] | |
| rw [usize_add_one_small _ pc (by omega) (mkUsizeSmall_val pc (by omega)), Aeneas.Std.bind_ok] | |
| have hnge : ¬ (mkUsizeSmall (pc + 1) ≥ alloc.vec.Vec.len code) := by | |
| show ¬ ((mkUsizeSmall (pc + 1)).val ≥ code.val.length) | |
| rw [mkUsizeSmall_val (pc + 1) (by omega), hlt_len]; omega | |
| rw [ite_eq_right_of_eq_false _ _ (eq_false hnge)] | |
| rw [vec_index_at code (pc + 1) (by omega) (by omega), hw1, Aeneas.Std.bind_ok] | |
| have hc0 : ebpf_core.api.EbpfInstruction.code 0#u64 = Result.ok 0#u8 := by rw [ei_code_ok]; congr 1 | |
| have ho0 : ebpf_core.api.EbpfInstruction.offset 0#u64 = Result.ok 0#i16 := by | |
| rw [ebpf_offset_spec]; congr 1 | |
| have hs0 : ebpf_core.api.EbpfInstruction.src_reg 0#u64 = Result.ok 0#u8 := by rw [ei_src_ok]; congr 1 | |
| have hd0 : ebpf_core.api.EbpfInstruction.dst_reg 0#u64 = Result.ok 0#u8 := by rw [ei_dst_ok]; congr 1 | |
| have hi0 : ebpf_core.api.EbpfInstruction.imm 0#u64 = Result.ok 0#i32 := by rw [ei_imm_ok]; congr 1 | |
| rw [hc0, Aeneas.Std.bind_ok] | |
| simp only [show ((0#u8 : U8) != 0#u8) = false by decide, Bool.false_eq_true, ↓reduceIte] | |
| rw [ho0, Aeneas.Std.bind_ok] | |
| simp only [show ((0#i16 : I16) != 0#i16) = false by decide, Bool.false_eq_true, ↓reduceIte] | |
| rw [hs0, Aeneas.Std.bind_ok] | |
| simp only [show ((0#u8 : U8) != 0#u8) = false by decide, Bool.false_eq_true, ↓reduceIte] | |
| rw [hd0, Aeneas.Std.bind_ok] | |
| simp only [show ((0#u8 : U8) != 0#u8) = false by decide, Bool.false_eq_true, ↓reduceIte] | |
| rw [hsrc, Aeneas.Std.bind_ok, himm, Aeneas.Std.bind_ok] | |
| simp only [show ¬ ((0#i32 : I32) < 0#i32) by decide, ↓reduceIte, lift, Aeneas.Std.bind_ok] | |
| have hcast0 : IScalar.hcast .Usize 0#i32 = mkUsizeSmall 0 := by | |
| apply usize_eq_of_val; rw [mkUsizeSmall_val 0 (by omega)] | |
| change (BitVec.signExtend System.Platform.numBits 0#32).toNat = 0 | |
| rcases System.Platform.numBits_eq with hnb | hnb <;> (rw [hnb]; decide) | |
| rw [hcast0] | |
| have hnge_m : ¬ (mkUsizeSmall 0 ≥ alloc.vec.Vec.len witnessVerified.maps) := by | |
| show ¬ ((mkUsizeSmall 0).val ≥ witnessVerified.maps.val.length) | |
| rw [mkUsizeSmall_val 0 (by omega)]; simp [witnessVerified, mkVecSmall_val] | |
| have hnge_lm : ¬ (mkUsizeSmall 0 ≥ alloc.vec.Vec.len witnessLinkage.maps) := by | |
| show ¬ ((mkUsizeSmall 0).val ≥ witnessLinkage.maps.val.length) | |
| rw [mkUsizeSmall_val 0 (by omega)]; simp [witnessLinkage, mkVecSmall_val] | |
| rw [ite_eq_right_of_eq_false _ _ (eq_false hnge_m)] | |
| rw [ite_eq_right_of_eq_false _ _ (eq_false hnge_lm)] | |
| rw [vec_index_at witnessVerified.maps 0 (by omega) (by simp [witnessVerified, mkVecSmall_val]), | |
| Aeneas.Std.bind_ok] | |
| rw [vec_index_at witnessLinkage.maps 0 (by omega) (by simp [witnessLinkage, mkVecSmall_val]), | |
| Aeneas.Std.bind_ok] | |
| rcases hsrcCase with ⟨rfl, rfl, hset_i1⟩ | ⟨rfl, rfl, hset_i1⟩ | |
| · have h5eq : (5#u8 : U8) = ebpf_core.api.BPF_PSEUDO_MAP_IDX := by | |
| unfold ebpf_core.api.BPF_PSEUDO_MAP_IDX; decide | |
| rw [ite_eq_left_of_eq_true _ _ (eq_true h5eq)] | |
| have hsh : ((0x0000_0007_0000_0000#u64 : U64) >>> 32#i32 : Result U64) = Result.ok 7#u64 := by | |
| rw [u64_shr_i32_ok _ 32#i32 (by decide) (by decide)]; congr 1 | |
| have hset_i2 : ebpf_core.api.EbpfInstruction.set_imm 0#u64 (UScalar.hcast .I32 7#u64) = | |
| Result.ok 0x0000_0007_0000_0000#u64 := by | |
| rw [ei_set_imm_ok]; congr 1 | |
| simp only [witnessLinkage, mkVecSmall_val, List.getElem_cons_zero] | |
| rw [hsh, Aeneas.Std.bind_ok, hset_sr, Aeneas.Std.bind_ok, hset_i1, Aeneas.Std.bind_ok, | |
| hset_i2, Aeneas.Std.bind_ok] | |
| exact link_lddw_vec_update code code' pc hpc hlt_len w0' 0x0000_0007_0000_0000#u64 hcode' | |
| · have h6ne : ¬ ((6#u8 : U8) = ebpf_core.api.BPF_PSEUDO_MAP_IDX) := by | |
| unfold ebpf_core.api.BPF_PSEUDO_MAP_IDX; decide | |
| rw [ite_eq_right_of_eq_false _ _ (eq_false h6ne)] | |
| simp only [witnessVerified, witnessLinkage, witnessMapSchemaCore, mkVecSmall_val, List.getElem_cons_zero] | |
| simp only [show (ebpf_core.api.BPF_MAP_TYPE_ARRAY != ebpf_core.api.BPF_MAP_TYPE_ARRAY) = false by | |
| unfold ebpf_core.api.BPF_MAP_TYPE_ARRAY; decide, Bool.false_eq_true, ↓reduceIte] | |
| simp only [show ¬ ((1#u32 : U32) = 0#u32) by decide, ↓reduceIte] | |
| rw [hi0, Aeneas.Std.bind_ok] | |
| simp only [show ¬ ((0#i32 : I32) < 0#i32) by decide, ↓reduceIte] | |
| simp only [show ¬ (IScalar.hcast .U32 0#i32 ≥ 8#u32) by decide, ↓reduceIte] | |
| have hchk : U64.checked_add 0x0000_0003_0000_0000#u64 | |
| (UScalar.cast .U64 (IScalar.hcast .U32 0#i32)) = some 0x0000_0003_0000_0000#u64 := by decide | |
| rw [hchk]; dsimp only | |
| have hsh : ((0x0000_0003_0000_0000#u64 : U64) >>> 32#i32 : Result U64) = Result.ok 3#u64 := by | |
| rw [u64_shr_i32_ok _ 32#i32 (by decide) (by decide)]; congr 1 | |
| have hset_i2 : ebpf_core.api.EbpfInstruction.set_imm 0#u64 (UScalar.hcast .I32 3#u64) = | |
| Result.ok 0x0000_0003_0000_0000#u64 := by | |
| rw [ei_set_imm_ok]; congr 1 | |
| rw [hsh, Aeneas.Std.bind_ok, hset_sr, Aeneas.Std.bind_ok, hset_i1, Aeneas.Std.bind_ok, | |
| hset_i2, Aeneas.Std.bind_ok] | |
| exact link_lddw_vec_update code code' pc hpc hlt_len w0' 0x0000_0003_0000_0000#u64 hcode' | |
| private theorem link_single_witness_plain | |
| (code : alloc.vec.Vec ebpf_core.api.EbpfInstruction) (pc : Nat) (hpc : pc < 13) | |
| (hlt_len : code.val.length = 13) (op : U8) | |
| (hcode : ebpf_core.api.EbpfInstruction.code (code.val[pc]'(by omega)) = Result.ok op) | |
| (hncall : ¬ (op = (ebpf_core.api.BPF_JMP ||| ebpf_core.api.BPF_CALL))) | |
| (hncls : ¬ ((op &&& ebpf_core.api.BPF_CLS_MASK) = ebpf_core.api.BPF_LD)) | |
| (hnlddw : ¬ (op = ebpf_core.api.BPF_LDDW)) : | |
| ebpf_core.program.link_single_instruction (mkUsizeSmall pc) | |
| witnessVerified witnessLinkage true code = Result.ok (.Ok 1#usize, code) := by | |
| unfold ebpf_core.program.link_single_instruction | |
| rw [vec_index_at code pc (by omega) (by omega), Aeneas.Std.bind_ok] | |
| rw [hcode, Aeneas.Std.bind_ok, lift, Aeneas.Std.bind_ok] | |
| rw [ite_eq_right_of_eq_false _ _ (eq_false hncall), lift, Aeneas.Std.bind_ok] | |
| rw [ite_eq_right_of_eq_false _ _ (eq_false hncls)] | |
| rw [ite_eq_right_of_eq_false _ _ (eq_false hnlddw)] | |
| private theorem witness_link_ok : | |
| ebpf_core.program.link_instructions witnessVerified witnessLinkage = | |
| Result.ok (.Ok witnessLinkedCode) := by | |
| unfold ebpf_core.program.link_instructions | |
| try dsimp (config := { zetaHave := true }) only | |
| have hlen_eq : (alloc.vec.Vec.len witnessLinkage.maps != alloc.vec.Vec.len witnessVerified.maps) = false := by | |
| have h1 : alloc.vec.Vec.len witnessLinkage.maps = mkUsizeSmall 1 := | |
| usize_eq_of_val (by simp [witnessLinkage, mkVecSmall_val, alloc.vec.Vec.len, mkUsizeSmall_val 1 (by omega)]) | |
| have h2 : alloc.vec.Vec.len witnessVerified.maps = mkUsizeSmall 1 := | |
| usize_eq_of_val (by simp [witnessVerified, mkVecSmall_val, alloc.vec.Vec.len, mkUsizeSmall_val 1 (by omega)]) | |
| rw [h1, h2]; simp | |
| rw [hlen_eq] | |
| simp only [Bool.false_eq_true, ↓reduceIte] | |
| have hpkt : (0x0000_0009_0000_0000#u64 != 0#u64) = true := by decide | |
| have h0val : (0#usize : Usize).val = 0 := by | |
| rcases System.Platform.numBits_eq with hnb | hnb <;> | |
| simp [UScalar.val, UScalarTy.Usize_numBits_eq, hnb] | |
| have h0eq : (0#usize : Usize) = mkUsizeSmall 0 := | |
| usize_eq_of_val (by rw [h0val, mkUsizeSmall_val 0 (by omega)]) | |
| have hpkt_val : witnessLinkage.packet_load_fn_addr = some 0x0000_0009_0000_0000#u64 := rfl | |
| rw [hpkt_val] | |
| simp only [hpkt, Aeneas.Std.bind_ok] | |
| have hvc_len : witnessVerified.code.val.length = 13 := by | |
| simp [witnessVerified, mkVecSmall_val, witnessVerifiedRawList] | |
| have hloop0 := link_loop0_copy witnessVerified.code (by omega) | |
| change Aeneas.Std.bind | |
| (ebpf_core.program.link_instructions_loop0 witnessVerified.code | |
| (alloc.vec.Vec.new ebpf_core.api.EbpfInstruction) 0#usize) | |
| (fun code => Aeneas.Std.bind | |
| (ebpf_core.program.link_instructions_loop1 witnessVerified.struct_access_instructions | |
| witnessLinkage.struct_mappings code none 0#usize) | |
| (fun (code1, loop_err) => | |
| match loop_err with | |
| | none => Aeneas.Std.bind | |
| (ebpf_core.program.link_instructions_loop2 witnessVerified.code witnessVerified.args | |
| witnessVerified.struct_access_instructions witnessVerified.maps witnessLinkage.struct_mappings | |
| witnessLinkage.helpers witnessLinkage.maps (some 0x0000_0009_0000_0000#u64) true code1 none 0#usize) | |
| (fun (code2, loop_err1) => | |
| match loop_err1 with | |
| | none => Result.ok (core.result.Result.Ok code2) | |
| | some err => Result.ok (core.result.Result.Err err)) | |
| | some err => Result.ok (core.result.Result.Err err))) = _ | |
| rw [hloop0, Aeneas.Std.bind_ok] | |
| have hloop1 : ebpf_core.program.link_instructions_loop1 | |
| witnessVerified.struct_access_instructions witnessLinkage.struct_mappings | |
| witnessVerified.code none 0#usize = Result.ok (witnessVerified.code, none) := by | |
| unfold ebpf_core.program.link_instructions_loop1 | |
| rw [loop_unfold_sat] | |
| unfold ebpf_core.program.link_instructions_loop1.body | |
| have hnotlt : ¬ ((0#usize : Usize) < alloc.vec.Vec.len witnessVerified.struct_access_instructions) := by | |
| show ¬ ((0#usize : Usize).val < witnessVerified.struct_access_instructions.val.length) | |
| simp [witnessVerified, mkVecSmall_val] | |
| simp [hnotlt, Aeneas.Std.bind_ok] | |
| rw [hloop1, Aeneas.Std.bind_ok] | |
| change Aeneas.Std.bind | |
| (ebpf_core.program.link_instructions_loop2 witnessVerified.code witnessVerified.args | |
| witnessVerified.struct_access_instructions witnessVerified.maps witnessLinkage.struct_mappings | |
| witnessLinkage.helpers witnessLinkage.maps (some 0x0000_0009_0000_0000#u64) true witnessVerified.code none 0#usize) | |
| (fun p => match p.2 with | |
| | none => Result.ok (core.result.Result.Ok p.1) | |
| | some err => Result.ok (core.result.Result.Err err)) = _ | |
| have hstep2 : ∀ (code code' : alloc.vec.Vec ebpf_core.api.EbpfInstruction) | |
| (pc step : Nat) (hpc : pc < 13) (hstep : step = 1 ∨ step = 2), | |
| code.val.length = 13 → | |
| ebpf_core.program.link_single_instruction (mkUsizeSmall pc) | |
| witnessVerified witnessLinkage true code = | |
| Result.ok (.Ok (if step = 1 then 1#usize else 2#usize), code') → | |
| ebpf_core.program.link_instructions_loop2 | |
| witnessVerified.code witnessVerified.args witnessVerified.struct_access_instructions | |
| witnessVerified.maps witnessLinkage.struct_mappings witnessLinkage.helpers | |
| witnessLinkage.maps (some 0x0000_0009_0000_0000#u64) true code none | |
| (mkUsizeSmall pc) = | |
| ebpf_core.program.link_instructions_loop2 | |
| witnessVerified.code witnessVerified.args witnessVerified.struct_access_instructions | |
| witnessVerified.maps witnessLinkage.struct_mappings witnessLinkage.helpers | |
| witnessLinkage.maps (some 0x0000_0009_0000_0000#u64) true code' none | |
| (mkUsizeSmall (pc + step)) := by | |
| intro code code' pc step hpc hs hlen hsingle | |
| have hlt : mkUsizeSmall pc < alloc.vec.Vec.len code := by | |
| show (mkUsizeSmall pc).val < code.val.length | |
| rw [mkUsizeSmall_val pc (by omega), hlen]; exact hpc | |
| have hsingle' : ebpf_core.program.link_single_instruction (mkUsizeSmall pc) | |
| { code := witnessVerified.code, args := witnessVerified.args, | |
| struct_access_instructions := witnessVerified.struct_access_instructions, | |
| maps := witnessVerified.maps } | |
| { struct_mappings := witnessLinkage.struct_mappings, | |
| helpers := witnessLinkage.helpers, | |
| maps := witnessLinkage.maps, | |
| packet_load_fn_addr := some 0x0000_0009_0000_0000#u64 } | |
| true code = Result.ok (.Ok (if step = 1 then 1#usize else 2#usize), code') := hsingle | |
| have hbody : ebpf_core.program.link_instructions_loop2.body | |
| witnessVerified.code witnessVerified.args witnessVerified.struct_access_instructions | |
| witnessVerified.maps witnessLinkage.struct_mappings witnessLinkage.helpers | |
| witnessLinkage.maps (some 0x0000_0009_0000_0000#u64) true code none (mkUsizeSmall pc) = | |
| Result.ok (ControlFlow.cont (code', none, mkUsizeSmall (pc + step))) := by | |
| unfold ebpf_core.program.link_instructions_loop2.body | |
| simp only [hlt, core.option.Option.is_none, Option.isNone_none, ↓reduceIte] | |
| rw [hsingle', Aeneas.Std.bind_ok] | |
| rcases hs with rfl | rfl | |
| · change Aeneas.Std.bind (mkUsizeSmall pc + 1#usize) | |
| (fun pc1 => Result.ok (ControlFlow.cont (code', none, pc1))) = _ | |
| rw [usize_add_one_small _ pc (by omega) (mkUsizeSmall_val pc (by omega)), Aeneas.Std.bind_ok] | |
| · change Aeneas.Std.bind (mkUsizeSmall pc + 2#usize) | |
| (fun pc1 => Result.ok (ControlFlow.cont (code', none, pc1))) = _ | |
| rw [usize_add_two_small _ pc (by omega) (mkUsizeSmall_val pc (by omega)), Aeneas.Std.bind_ok] | |
| have hloop_step : ∀ {α β : Type} (f : α → Result (ControlFlow α β)) (x x' : α), | |
| f x = Result.ok (ControlFlow.cont x') → loop f x = loop f x' := by | |
| intro α β f x x' hf | |
| conv_lhs => rw [loop_unfold_sat] | |
| rw [hf, Aeneas.Std.bind_ok] | |
| exact hloop_step _ _ _ hbody | |
| rw [h0eq] | |
| let c1 : alloc.vec.Vec ebpf_core.api.EbpfInstruction := | |
| mkVecSmall ((witnessVerified.code.val.set 0 0x0000_0000_0000_0118#u64).set 1 | |
| 0x0000_0007_0000_0000#u64) (by rw [List.length_set, List.length_set, hvc_len]; omega) | |
| let c2 : alloc.vec.Vec ebpf_core.api.EbpfInstruction := | |
| mkVecSmall ((c1.val.set 2 0x0000_0000_0000_0218#u64).set 3 | |
| 0x0000_0003_0000_0000#u64) (by | |
| have hc1 : c1.val.length = 13 := by simp [c1, mkVecSmall_val, hvc_len] | |
| rw [List.length_set, List.length_set, hc1]; omega) | |
| -- pc = 0: lddwMap r1, 0 -> step 2 | |
| have hs0 := link_single_witness_lddw witnessVerified.code c1 0 (by omega) hvc_len | |
| 0x0000_0000_0000_5118#u64 0x0000_0000_0000_0118#u64 0x0000_0007_0000_0000#u64 5#u8 | |
| (by simp [c1, mkVecSmall_val]) | |
| (by simp [witnessVerified, mkVecSmall_val, witnessVerifiedRawList]) | |
| (by simp [witnessVerified, mkVecSmall_val, witnessVerifiedRawList]) | |
| (by rw [ei_code_ok]; congr 1) (by rw [ei_src_ok]; congr 1) (by rw [ei_imm_ok]; congr 1) | |
| (Or.inl ⟨rfl, rfl, by rw [ei_set_imm_ok]; congr 1⟩) | |
| (by rw [ei_set_src0_ok]; congr 1) | |
| rw [hstep2 witnessVerified.code c1 0 2 (by omega) (Or.inr rfl) hvc_len hs0] | |
| -- pc = 2: lddwMapValue r2, 0, 0 -> step 2 | |
| have hlen_c1 : c1.val.length = 13 := by | |
| simp [c1, mkVecSmall_val, witnessVerified, witnessVerifiedRawList] | |
| have hs2 := link_single_witness_lddw c1 c2 2 (by omega) hlen_c1 | |
| 0x0000_0000_0000_6218#u64 0x0000_0000_0000_0218#u64 0x0000_0003_0000_0000#u64 6#u8 | |
| (by simp [c2, mkVecSmall_val]) | |
| (by simp [c1, witnessVerified, mkVecSmall_val, witnessVerifiedRawList]) | |
| (by simp [c1, witnessVerified, mkVecSmall_val, witnessVerifiedRawList]) | |
| (by rw [ei_code_ok]; congr 1) (by rw [ei_src_ok]; congr 1) (by rw [ei_imm_ok]; congr 1) | |
| (Or.inr ⟨rfl, rfl, by rw [ei_set_imm_ok]; congr 1⟩) | |
| (by rw [ei_set_src0_ok]; congr 1) | |
| rw [hstep2 c1 c2 2 2 (by omega) (Or.inr rfl) hlen_c1 hs2] | |
| -- pc = 4: lddwMapValue r3, 0, 0 -> step 2 | |
| have hlen_c2 : c2.val.length = 13 := by | |
| simp [c2, c1, mkVecSmall_val, witnessVerified, witnessVerifiedRawList] | |
| have hs4 := link_single_witness_lddw c2 witnessLinkedCode 4 (by omega) hlen_c2 | |
| 0x0000_0000_0000_6318#u64 0x0000_0000_0000_0318#u64 0x0000_0003_0000_0000#u64 6#u8 | |
| (by simp only [c2, c1, witnessLinkedCode, witnessVerified, mkVecSmall_val]; decide) | |
| (by simp [c2, c1, witnessVerified, mkVecSmall_val, witnessVerifiedRawList]) | |
| (by simp [c2, c1, witnessVerified, mkVecSmall_val, witnessVerifiedRawList]) | |
| (by rw [ei_code_ok]; congr 1) (by rw [ei_src_ok]; congr 1) (by rw [ei_imm_ok]; congr 1) | |
| (Or.inr ⟨rfl, rfl, by rw [ei_set_imm_ok]; congr 1⟩) | |
| (by rw [ei_set_src0_ok]; congr 1) | |
| rw [hstep2 c2 witnessLinkedCode 4 2 (by omega) (Or.inr rfl) hlen_c2 hs4] | |
| have hlc_len : witnessLinkedCode.val.length = 13 := by | |
| simp [witnessLinkedCode, mkVecSmall_val, witnessLinkedRawList] | |
| have plain_helper : ∀ (pc : Nat) (hpc : pc < 13) (op : U8), | |
| ebpf_core.api.EbpfInstruction.code (witnessLinkedCode.val[pc]'(by omega)) = Result.ok op → | |
| ¬ (op = ⟨0x85#8⟩) → | |
| ¬ ((op &&& 7#u8) = ebpf_core.api.BPF_LD) → | |
| ¬ (op = ⟨24#8⟩) → | |
| ebpf_core.program.link_single_instruction (mkUsizeSmall pc) | |
| witnessVerified witnessLinkage true witnessLinkedCode = | |
| Result.ok (.Ok 1#usize, witnessLinkedCode) := | |
| fun pc hpc op h1 h2 h3 h4 => link_single_witness_plain witnessLinkedCode pc hpc hlc_len op h1 | |
| (bpf_jmp_call_val ▸ h2) (bpf_cls_mask_val ▸ h3) (bpf_lddw_val ▸ h4) | |
| -- pc = 6, 7, 8: mov64, stxdw, ldxdw | |
| rw [hstep2 witnessLinkedCode witnessLinkedCode 6 1 (by omega) (Or.inl rfl) hlc_len | |
| (plain_helper 6 (by omega) ⟨0xB7#8⟩ | |
| (by simp only [witnessLinkedCode, mkVecSmall_val, witnessLinkedRawList, List.getElem_cons_succ, List.getElem_cons_zero, ei_code_ok]; congr 1) | |
| (by decide) (by unfold ebpf_core.api.BPF_LD; decide) (by decide))] | |
| rw [hstep2 witnessLinkedCode witnessLinkedCode 7 1 (by omega) (Or.inl rfl) hlc_len | |
| (plain_helper 7 (by omega) ⟨0x7B#8⟩ | |
| (by simp only [witnessLinkedCode, mkVecSmall_val, witnessLinkedRawList, List.getElem_cons_succ, List.getElem_cons_zero, ei_code_ok]; congr 1) | |
| (by decide) (by unfold ebpf_core.api.BPF_LD; decide) (by decide))] | |
| rw [hstep2 witnessLinkedCode witnessLinkedCode 8 1 (by omega) (Or.inl rfl) hlc_len | |
| (plain_helper 8 (by omega) ⟨0x79#8⟩ | |
| (by simp only [witnessLinkedCode, mkVecSmall_val, witnessLinkedRawList, List.getElem_cons_succ, List.getElem_cons_zero, ei_code_ok]; congr 1) | |
| (by decide) (by unfold ebpf_core.api.BPF_LD; decide) (by decide))] | |
| -- pc = 9: call 1 | |
| have hsingle9 : ebpf_core.program.link_single_instruction (mkUsizeSmall 9) | |
| witnessVerified witnessLinkage true witnessLinkedCode = Result.ok (.Ok 1#usize, witnessLinkedCode) := by | |
| unfold ebpf_core.program.link_single_instruction | |
| rw [vec_index_at witnessLinkedCode 9 (by omega) (by omega), Aeneas.Std.bind_ok] | |
| have hw9 : witnessLinkedCode.val[9]'(by omega) = 0x0000_0001_0000_0085#u64 := by | |
| simp [witnessLinkedCode, mkVecSmall_val, witnessLinkedRawList] | |
| rw [hw9] | |
| have hc9 : ebpf_core.api.EbpfInstruction.code 0x0000_0001_0000_0085#u64 = Result.ok ⟨0x85#8⟩ := by | |
| rw [ei_code_ok]; congr 1 | |
| have hsr9 : ebpf_core.api.EbpfInstruction.src_reg 0x0000_0001_0000_0085#u64 = Result.ok 0#u8 := by | |
| rw [ei_src_ok]; congr 1 | |
| have himm9 : ebpf_core.api.EbpfInstruction.imm 0x0000_0001_0000_0085#u64 = Result.ok 1#i32 := by | |
| rw [ei_imm_ok]; congr 1 | |
| rw [hc9, Aeneas.Std.bind_ok, lift, Aeneas.Std.bind_ok] | |
| rw [ite_eq_left_of_eq_true _ _ (eq_true bpf_jmp_call_val.symm)] | |
| rw [hsr9, Aeneas.Std.bind_ok] | |
| simp only [show ((0#u8 : U8) != 0#u8) = false by decide, Bool.false_eq_true, ↓reduceIte] | |
| rw [himm9, Aeneas.Std.bind_ok, lift, Aeneas.Std.bind_ok] | |
| have hh1 : IScalar.hcast .U32 1#i32 = 1#u32 := by decide | |
| rw [hh1, witness_has_helper_1, Aeneas.Std.bind_ok] | |
| simp | |
| rw [hstep2 witnessLinkedCode witnessLinkedCode 9 1 (by omega) (Or.inl rfl) hlc_len hsingle9] | |
| -- pc = 10: ldabsw [0] | |
| have hsingle10 : ebpf_core.program.link_single_instruction (mkUsizeSmall 10) | |
| witnessVerified witnessLinkage true witnessLinkedCode = Result.ok (.Ok 1#usize, witnessLinkedCode) := by | |
| unfold ebpf_core.program.link_single_instruction | |
| rw [vec_index_at witnessLinkedCode 10 (by omega) (by omega), Aeneas.Std.bind_ok] | |
| have hw10 : witnessLinkedCode.val[10]'(by omega) = 0x0000_0000_0000_0020#u64 := by | |
| simp [witnessLinkedCode, mkVecSmall_val, witnessLinkedRawList] | |
| rw [hw10] | |
| have hc10 : ebpf_core.api.EbpfInstruction.code 0x0000_0000_0000_0020#u64 = Result.ok ⟨0x20#8⟩ := by | |
| rw [ei_code_ok]; congr 1 | |
| have hncall : ¬ ((⟨0x20#8⟩ : U8) = (ebpf_core.api.BPF_JMP ||| ebpf_core.api.BPF_CALL)) := by | |
| rw [bpf_jmp_call_val]; decide | |
| have hcls : ((⟨0x20#8⟩ : U8) &&& ebpf_core.api.BPF_CLS_MASK) = ebpf_core.api.BPF_LD := by | |
| rw [bpf_cls_mask_val]; unfold ebpf_core.api.BPF_LD; decide | |
| have habs : ((⟨0x20#8⟩ : U8) &&& 224#u8) = ebpf_core.api.BPF_ABS := by | |
| unfold ebpf_core.api.BPF_ABS; decide | |
| have hnlddw : ¬ ((⟨0x20#8⟩ : U8) = ebpf_core.api.BPF_LDDW) := by | |
| rw [bpf_lddw_val]; decide | |
| rw [hc10, Aeneas.Std.bind_ok, lift, Aeneas.Std.bind_ok] | |
| rw [ite_eq_right_of_eq_false _ _ (eq_false hncall), lift, Aeneas.Std.bind_ok] | |
| rw [ite_eq_left_of_eq_true _ _ (eq_true hcls), lift, Aeneas.Std.bind_ok] | |
| rw [ite_eq_left_of_eq_true _ _ (eq_true habs)] | |
| simp only [↓reduceIte] | |
| rw [ite_eq_right_of_eq_false _ _ (eq_false hnlddw)] | |
| rw [hstep2 witnessLinkedCode witnessLinkedCode 10 1 (by omega) (Or.inl rfl) hlc_len hsingle10] | |
| -- pc = 11, 12: add64 r0, r8; exit | |
| rw [hstep2 witnessLinkedCode witnessLinkedCode 11 1 (by omega) (Or.inl rfl) hlc_len | |
| (plain_helper 11 (by omega) ⟨0x0F#8⟩ | |
| (by simp only [witnessLinkedCode, mkVecSmall_val, witnessLinkedRawList, List.getElem_cons_succ, List.getElem_cons_zero, ei_code_ok]; congr 1) | |
| (by decide) (by unfold ebpf_core.api.BPF_LD; decide) (by decide))] | |
| rw [hstep2 witnessLinkedCode witnessLinkedCode 12 1 (by omega) (Or.inl rfl) hlc_len | |
| (plain_helper 12 (by omega) ⟨0x95#8⟩ | |
| (by simp only [witnessLinkedCode, mkVecSmall_val, witnessLinkedRawList, List.getElem_cons_succ, List.getElem_cons_zero, ei_code_ok]; congr 1) | |
| (by decide) (by unfold ebpf_core.api.BPF_LD; decide) (by decide))] | |
| unfold ebpf_core.program.link_instructions_loop2 | |
| rw [loop_unfold_sat] | |
| unfold ebpf_core.program.link_instructions_loop2.body | |
| have hnotlt : ¬ (mkUsizeSmall 13 < alloc.vec.Vec.len witnessLinkedCode) := by | |
| show ¬ ((mkUsizeSmall 13).val < witnessLinkedCode.val.length) | |
| rw [mkUsizeSmall_val 13 (by omega), hlc_len]; omega | |
| simp [hnotlt, Aeneas.Std.bind_ok] | |
| -- Concrete `EbpfMachineBackend` | |
| private structure WitnessBackend where | |
| mapSlot0 : BitVec 64 | |
| pktEpoch : Nat | |
| pktLen : BitVec 64 | |
| deriving DecidableEq | |
| private def witnessBackendInst : ebpf_core.executor.EbpfMachineBackend WitnessBackend where | |
| stack_top _ := Result.ok 0x0000_0001_0000_0200#u64 | |
| load_memory b addr w := | |
| match w with | |
| | .U64 => if addr = 0x0000_0003_0000_0000#u64 then Result.ok ⟨b.mapSlot0⟩ else Result.ok 0#u64 | |
| | _ => Result.ok 0#u64 | |
| store_memory b addr val w := | |
| match w with | |
| | .U64 => if addr = 0x0000_0003_0000_0000#u64 then Result.ok { b with mapSlot0 := val.bv } else Result.ok b | |
| | _ => Result.ok b | |
| atomic_rmw32 b _ _ _ := Result.ok (0#u32, b) | |
| atomic_rmw64 b _ _ _ := Result.ok (0#u64, b) | |
| atomic_cmpxchg32 b _ _ _ := Result.ok (0#u32, b) | |
| atomic_cmpxchg64 b _ _ _ := Result.ok (0#u64, b) | |
| call_helper b idx _ _ _ _ _ := | |
| if idx = 1#u32 then Result.ok (0#u64, { b with pktEpoch := b.pktEpoch + 1, pktLen := 10#64 }) | |
| else Result.ok (0#u64, b) | |
| load_from_packet b _ _ _ := Result.ok (some 100#u64, b) | |
| -- Explicit concrete states along the 10-step execution (`r0` starts at `77#64` before `callExternal 1` | |
| -- so the void helper overwrite to `0#64` is genuinely exercised). | |
| private def witnessStack0 : StackMem := fun _ => .bytes 0#64 0#64 | |
| private def buf0 : BufId → BitVec 64 → BitVec 8 := fun _ _ => 0#8 | |
| private def buf42 : BufId → BitVec 64 → BitVec 8 := | |
| writeBufWord buf0 (.mapValue 0) 0#64 .u64 42#64 | |
| private def len0 : MemoryId → Nat → BitVec 64 := fun _ ep => if ep = 0 then 100#64 else 0#64 | |
| private def len1 : MemoryId → Nat → BitVec 64 := | |
| fun _ ep => if ep = 1 then 10#64 else if ep = 0 then 100#64 else 0#64 | |
| private def mkWorld (bufs : BufId → BitVec 64 → BitVec 8) (epoch : Nat) | |
| (lenFn : MemoryId → Nat → BitVec 64) (steps : Nat) : WorldState where | |
| bufBytes := bufs | |
| structBytes _ _ := 0#8 | |
| structFieldNull _ := false | |
| arrayEpoch _ := epoch | |
| arrayLen := lenFn | |
| stepCount := steps | |
| liveResources := [] | |
| nextAllocId := 0 | |
| private def mkRegs (r0 r1 r2 r3 r4 r8 : RuntimeVal) : Nat → RuntimeVal := fun r => | |
| match r with | |
| | 0 => r0 | |
| | 1 => r1 | |
| | 2 => r2 | |
| | 3 => r3 | |
| | 4 => r4 | |
| | 6 => .structPtr witnessCtxId none 0#64 | |
| | 7 => .endArrayPtr witnessArrId 0 | |
| | 8 => r8 | |
| | 10 => .stackPtr 512#64 | |
| | _ => .scalar 0#64 | |
| private def mapValPtr0 : RuntimeVal := .memPtr (.mapValue 0) .mapWritable none 8#64 0#64 | |
| private def st0 : MachineState := | |
| { pc := 0, regs := mkRegs (.scalar 77#64) (.scalar 0#64) (.scalar 0#64) (.scalar 0#64) (.scalar 0#64) (.scalar 0#64), | |
| stack := witnessStack0, world := mkWorld buf0 0 len0 0 } | |
| private def st1 : MachineState := | |
| { pc := 2, regs := mkRegs (.scalar 77#64) (.mapPtr 0) (.scalar 0#64) (.scalar 0#64) (.scalar 0#64) (.scalar 0#64), | |
| stack := witnessStack0, world := mkWorld buf0 0 len0 1 } | |
| private def st2 : MachineState := | |
| { pc := 4, regs := mkRegs (.scalar 77#64) (.mapPtr 0) mapValPtr0 (.scalar 0#64) (.scalar 0#64) (.scalar 0#64), | |
| stack := witnessStack0, world := mkWorld buf0 0 len0 2 } | |
| private def st3 : MachineState := | |
| { pc := 6, regs := mkRegs (.scalar 77#64) (.mapPtr 0) mapValPtr0 mapValPtr0 (.scalar 0#64) (.scalar 0#64), | |
| stack := witnessStack0, world := mkWorld buf0 0 len0 3 } | |
| private def st4 : MachineState := | |
| { pc := 7, regs := mkRegs (.scalar 77#64) (.mapPtr 0) mapValPtr0 mapValPtr0 (.scalar 42#64) (.scalar 0#64), | |
| stack := witnessStack0, world := mkWorld buf0 0 len0 4 } | |
| private def st5 : MachineState := | |
| { pc := 8, regs := mkRegs (.scalar 77#64) (.mapPtr 0) mapValPtr0 mapValPtr0 (.scalar 42#64) (.scalar 0#64), | |
| stack := witnessStack0, world := mkWorld buf42 0 len0 5 } | |
| private def st6 : MachineState := | |
| { pc := 9, regs := mkRegs (.scalar 77#64) (.mapPtr 0) mapValPtr0 mapValPtr0 (.scalar 42#64) (.scalar 42#64), | |
| stack := witnessStack0, world := mkWorld buf42 0 len0 6 } | |
| private def st7 : MachineState := | |
| { pc := 10, regs := mkRegs (.scalar 0#64) (.scalar 0x0000_0007_0000_0000#64) | |
| (.scalar 0x0000_0003_0000_0000#64) (.scalar 0x0000_0003_0000_0000#64) (.scalar 42#64) (.scalar 42#64), | |
| stack := witnessStack0, world := mkWorld buf42 1 len1 7 } | |
| private def st8 : MachineState := | |
| { pc := 11, regs := mkRegs (.scalar 100#64) (.scalar 0x0000_0007_0000_0000#64) | |
| (.scalar 0x0000_0003_0000_0000#64) (.scalar 0x0000_0003_0000_0000#64) (.scalar 42#64) (.scalar 42#64), | |
| stack := witnessStack0, world := mkWorld buf42 1 len1 8 } | |
| private def st9 : MachineState := | |
| { pc := 12, regs := mkRegs (.scalar 142#64) (.scalar 0x0000_0007_0000_0000#64) | |
| (.scalar 0x0000_0003_0000_0000#64) (.scalar 0x0000_0003_0000_0000#64) (.scalar 42#64) (.scalar 42#64), | |
| stack := witnessStack0, world := mkWorld buf42 1 len1 9 } | |
| private def witnessFinalWorld : WorldState := bumpWorldStep st9.world | |
| private def witnessB0 : WitnessBackend := { mapSlot0 := 0#64, pktEpoch := 0, pktLen := 100#64 } | |
| private def bAfterStore : WitnessBackend := { witnessB0 with mapSlot0 := 42#64 } | |
| private def bAfterCall : WitnessBackend := { bAfterStore with pktEpoch := 1, pktLen := 10#64 } | |
| private def witnessIState0 : ebpf_core.executor.InterpreterState where | |
| registers := Array.make 16#usize | |
| [ 77#u64, 0#u64, 0#u64, 0#u64, 0#u64, 0#u64, | |
| 0x0000_0004_0000_0000#u64, | |
| 0x0000_0005_0000_0064#u64, | |
| 0#u64, 0#u64, | |
| 0x0000_0001_0000_0200#u64, | |
| 0#u64, 0#u64, 0#u64, 0#u64, 0#u64 ] | |
| (by rcases System.Platform.numBits_eq with hnb | hnb <;> | |
| simp [UScalar.val, UScalarTy.Usize_numBits_eq, hnb]) | |
| pc := 0#usize | |
| next_imm := 0#i32 | |
| result := none | |
| private theorem witnessIState0_regs_match : | |
| InterpreterRegsMatch witnessLayout st0 witnessIState0.registers := by | |
| intro r hr h16 | |
| match r with | |
| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 => | |
| simp only [witnessIState0, Array.make_val, List.getElem_cons_succ, List.getElem_cons_zero, | |
| st0, mkRegs, mkWorld, len0, witnessLayout, RuntimeVal.toBits] | |
| decide | |
| | n + 11 => simp [REGISTER_COUNT] at hr | |
| -- Indexed `Fin 9` state table + compact 2-constructor simulation relation | |
| private def witnessStepTable : Fin 9 → WitnessBackend × MachineState | |
| | ⟨0, _⟩ => (witnessB0, st0) | |
| | ⟨1, _⟩ => (witnessB0, st1) | |
| | ⟨2, _⟩ => (witnessB0, st2) | |
| | ⟨3, _⟩ => (witnessB0, st3) | |
| | ⟨4, _⟩ => (witnessB0, st4) | |
| | ⟨5, _⟩ => (bAfterStore, st5) | |
| | ⟨6, _⟩ => (bAfterStore, st6) | |
| | ⟨7, _⟩ => (bAfterCall, st7) | |
| | ⟨8, _⟩ => (bAfterCall, st8) | |
| private inductive WitnessRel : WitnessBackend → MachineState → Prop where | |
| | step (i : Fin 9) : | |
| WitnessRel (witnessStepTable i).1 (witnessStepTable i).2 | |
| | halt (w : WorldState) (hlive : w.liveResources = []) : | |
| WitnessRel bAfterCall { st9 with world := w } | |
| -- Abstract `step` proofs | |
| private theorem step_st0 : step witnessProg witnessEnv witnessLayout witnessSecret st0 = .next st1 := by | |
| unfold step | |
| simp only [st0, st1, witnessProg, witnessProgList, witnessEnv, bumpWorldStep, | |
| List.size_toArray, List.length_cons, List.length_nil, | |
| show 0 < 13 by decide, show 0 + 1 < 13 by decide, show 0 + 2 < 13 by decide, ↓reduceDIte, | |
| List.getElem_toArray, List.getElem_cons_zero, List.getElem_cons_succ, | |
| show (1 : Nat) < 10 by decide, | |
| show (Insn.lddwMapValue 2 0 0#64 == Insn.lddwCont) = false by decide, | |
| beq_self_eq_true, Bool.false_eq_true, ↓reduceIte] | |
| congr 1; congr 1 | |
| funext r; match r with | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | _ + 11 => rfl | |
| private theorem step_st1 : step witnessProg witnessEnv witnessLayout witnessSecret st1 = .next st2 := by | |
| unfold step | |
| have hcond : (U32.bv 2#u32 != 2#32 || U32.bv 1#u32 == 0#32 || | |
| decide (0#64 ≥ 8#64)) = false := by decide | |
| have hro : (U32.bv 0#u32 &&& 128#32 != 0#32) = false := by decide | |
| have hze8 : BitVec.zeroExtend 64 (U32.bv 8#u32) = 8#64 := by decide | |
| simp only [st1, st2, witnessProg, witnessProgList, witnessEnv, witnessMapSpec, witnessMapSchemaCore, | |
| MapSpec.ofCore, ebpf_core.api.BPF_MAP_TYPE_ARRAY, bumpWorldStep, | |
| List.size_toArray, List.length_cons, List.length_nil, | |
| show 2 < 13 by decide, show 2 + 1 < 13 by decide, show 2 + 2 < 13 by decide, ↓reduceDIte, | |
| List.getElem_toArray, List.getElem_cons_zero, List.getElem_cons_succ, | |
| show (2 : Nat) < 10 by decide, | |
| show (Insn.lddwMapValue 3 0 0#64 == Insn.lddwCont) = false by decide, | |
| hze8, hcond, hro, beq_self_eq_true, Bool.false_eq_true, ↓reduceIte, mapValPtr0] | |
| congr 1; congr 1 | |
| funext r | |
| match r with | |
| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | _ + 11 => | |
| simp [updateReg, mkRegs] | |
| private theorem step_st2 : step witnessProg witnessEnv witnessLayout witnessSecret st2 = .next st3 := by | |
| unfold step | |
| have hcond : (U32.bv 2#u32 != 2#32 || U32.bv 1#u32 == 0#32 || | |
| decide (0#64 ≥ 8#64)) = false := by decide | |
| have hro : (U32.bv 0#u32 &&& 128#32 != 0#32) = false := by decide | |
| have hze8 : BitVec.zeroExtend 64 (U32.bv 8#u32) = 8#64 := by decide | |
| simp only [st2, st3, witnessProg, witnessProgList, witnessEnv, witnessMapSpec, witnessMapSchemaCore, | |
| MapSpec.ofCore, ebpf_core.api.BPF_MAP_TYPE_ARRAY, bumpWorldStep, | |
| List.size_toArray, List.length_cons, List.length_nil, | |
| show 4 < 13 by decide, show 4 + 1 < 13 by decide, show 4 + 2 < 13 by decide, ↓reduceDIte, | |
| List.getElem_toArray, List.getElem_cons_zero, List.getElem_cons_succ, | |
| show (3 : Nat) < 10 by decide, | |
| show (Insn.alu64 AluOp.mov 4 (Operand.imm 42#64) == Insn.lddwCont) = false by decide, | |
| hze8, hcond, hro, beq_self_eq_true, Bool.false_eq_true, ↓reduceIte, mapValPtr0] | |
| congr 1; congr 1 | |
| funext r | |
| match r with | |
| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | _ + 11 => | |
| simp [updateReg, mkRegs] | |
| private theorem step_st3 : step witnessProg witnessEnv witnessLayout witnessSecret st3 = .next st4 := by | |
| unfold step | |
| simp only [st3, st4, witnessProg, witnessProgList, bumpWorldStep, | |
| List.size_toArray, List.length_cons, List.length_nil, | |
| show 6 < 13 by decide, show 6 + 1 < 13 by decide, ↓reduceDIte, | |
| List.getElem_toArray, List.getElem_cons_zero, List.getElem_cons_succ, | |
| show (4 : Nat) < 10 by decide, | |
| show (Insn.store DataWidth.u64 2 0#16 (Operand.reg 4) == Insn.lddwCont) = false by decide, | |
| beq_self_eq_true, Bool.false_eq_true, ↓reduceIte] | |
| congr 1; congr 1 | |
| funext r; match r with | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | _ + 11 => rfl | |
| private theorem step_st4 : step witnessProg witnessEnv witnessLayout witnessSecret st4 = .next st5 := by | |
| unfold step | |
| have hdisp : 0#64 + sextOffset16 0#16 = 0#64 := by decide | |
| have hib : inBoundsRange 0#64 .u64 8#64 = true := by decide | |
| simp only [st4, st5, witnessProg, witnessProgList, bumpWorldStep, | |
| List.size_toArray, List.length_cons, List.length_nil, show 7 < 13 by decide, | |
| show 7 + 1 < 13 by decide, ↓reduceDIte, | |
| List.getElem_toArray, List.getElem_cons_zero, List.getElem_cons_succ, | |
| mkRegs, mapValPtr0, MemoryOrigin.isWritable, checkAlive, RuntimeVal.resourceId?, mkWorld, | |
| show ¬ (2 ≥ REGISTER_COUNT) by decide, show (4 : Nat) < REGISTER_COUNT by decide, | |
| show (Insn.load DataWidth.u64 8 3 0#16 == Insn.lddwCont) = false by decide, | |
| hdisp, hib, Bool.not_true, Bool.false_eq_true, ↓reduceIte, buf42] | |
| private theorem step_st5 : step witnessProg witnessEnv witnessLayout witnessSecret st5 = .next st6 := by | |
| unfold step | |
| have hdisp : 0#64 + sextOffset16 0#16 = 0#64 := by decide | |
| have hib : inBoundsRange 0#64 .u64 8#64 = true := by decide | |
| have hread : readBufWord buf42 (.mapValue 0) 0#64 .u64 = 42#64 := by | |
| unfold buf42 buf0 readBufWord writeBufWord; decide | |
| simp only [st5, st6, witnessProg, witnessProgList, bumpWorldStep, | |
| List.size_toArray, List.length_cons, List.length_nil, show 8 < 13 by decide, | |
| show 8 + 1 < 13 by decide, ↓reduceDIte, | |
| List.getElem_toArray, List.getElem_cons_zero, List.getElem_cons_succ, | |
| mkRegs, mapValPtr0, checkAlive, RuntimeVal.resourceId?, mkWorld, | |
| show (8 : Nat) < 10 by decide, show ¬ (3 ≥ REGISTER_COUNT) by decide, | |
| show (Insn.callExternal 1 == Insn.lddwCont) = false by decide, | |
| hdisp, hib, hread, Bool.not_true, Bool.false_eq_true, ↓reduceIte] | |
| congr 1; congr 1 | |
| funext r | |
| match r with | |
| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | _ + 11 => rfl | |
| private theorem step_st6 : step witnessProg witnessEnv witnessLayout witnessSecret st6 = .next st7 := by | |
| unfold step | |
| simp only [st6, st7, witnessProg, witnessProgList, witnessEnv, witnessHelperSpec, witnessLayout, | |
| witnessSecret, bumpWorldStep, checkAndApplyHelperArgs, applyArrayBoundsInvalidation, | |
| evalHelperReturn, List.size_toArray, List.length_cons, List.length_nil, | |
| show 9 < 13 by decide, show 9 + 1 < 13 by decide, ↓reduceDIte, | |
| List.getElem_toArray, List.getElem_cons_zero, List.getElem_cons_succ, | |
| show (Insn.loadPacket DataWidth.u32 0#32 none == Insn.lddwCont) = false by decide, | |
| Bool.false_eq_true, ↓reduceIte] | |
| congr 1; simp only [mkWorld] | |
| congr 1 | |
| · funext r | |
| match r with | |
| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | _ + 11 => | |
| simp [mkRegs, mapValPtr0, RuntimeVal.toBits, HostLayout.bufAddr] | |
| · congr 1 | |
| · funext bid off | |
| cases bid with | |
| | mem _ => rfl | |
| | mapValue _ => rfl | |
| | array _ ep => | |
| simp only [buf42, writeBufWord] | |
| split_ifs <;> simp [writeBufByte, buf0] | |
| · funext id ep | |
| simp only [len0, len1] | |
| by_cases h1 : ep = 1 | |
| · subst h1; rfl | |
| · simp [h1] | |
| private theorem step_st7 : step witnessProg witnessEnv witnessLayout witnessSecret st7 = .next st8 := by | |
| unfold step | |
| simp only [st7, st8, witnessProg, witnessProgList, witnessEnv, witnessLayout, witnessSecret, | |
| bumpWorldStep, ContextObjSpec.matchesVal, | |
| List.size_toArray, List.length_cons, List.length_nil, show 10 < 13 by decide, | |
| show 10 + 1 < 13 by decide, ↓reduceDIte, | |
| List.getElem_toArray, List.getElem_cons_zero, List.getElem_cons_succ, mkRegs, | |
| show (Insn.alu64 AluOp.add 0 (Operand.reg 8) == Insn.lddwCont) = false by decide, | |
| beq_self_eq_true, Bool.not_true, Bool.false_eq_true, ↓reduceIte, | |
| show BitVec.saddOverflow 0#32 0#32 = false by decide, DataWidth.valMask64] | |
| congr 1; congr 1 | |
| funext r | |
| match r with | |
| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | _ + 11 => | |
| simp [mkRegs, RuntimeVal.toBits] | |
| private theorem step_st8 : step witnessProg witnessEnv witnessLayout witnessSecret st8 = .next st9 := by | |
| unfold step | |
| simp only [st8, st9, witnessProg, witnessProgList, bumpWorldStep, | |
| List.size_toArray, List.length_cons, List.length_nil, show 11 < 13 by decide, | |
| show 11 + 1 < 13 by decide, ↓reduceDIte, | |
| List.getElem_toArray, List.getElem_cons_zero, List.getElem_cons_succ, | |
| show (0 : Nat) < 10 by decide, show (8 : Nat) < REGISTER_COUNT by decide, | |
| show (AluOp.add == AluOp.mov) = false by decide, | |
| show (Insn.exit == Insn.lddwCont) = false by decide, | |
| Bool.false_eq_true, ↓reduceIte, mkRegs, evalAlu64Val, evalAlu64, RuntimeVal.toBits, | |
| show (100#64 + 42#64 : BitVec 64) = 142#64 by decide] | |
| congr 1; congr 1 | |
| funext r | |
| match r with | |
| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | _ + 11 => | |
| simp [updateReg, mkRegs] | |
| private theorem step_st9_any (w : WorldState) (hlive : w.liveResources = []) : | |
| step witnessProg witnessEnv witnessLayout witnessSecret { st9 with world := w } = | |
| .halted 142#64 (bumpWorldStep w) := by | |
| unfold step | |
| simp only [st9, witnessProg, witnessProgList, List.size_toArray, List.length_cons, List.length_nil, | |
| show 12 < 13 by decide, ↓reduceDIte, List.getElem_toArray, List.getElem_cons_succ, | |
| List.getElem_cons_zero, hlive, List.isEmpty_nil, Bool.not_true, Bool.false_eq_true, ↓reduceIte, | |
| mkRegs] | |
| private theorem witness_runSteps_eq : | |
| runSteps witnessProg witnessEnv witnessLayout witnessSecret 10 st0 = | |
| some (.inl (142#64, witnessFinalWorld)) := by | |
| simp only [runSteps, step_st0, step_st1, step_st2, step_st3, step_st4, step_st5, | |
| step_st6, step_st7, step_st8, step_st9_any st9.world rfl, witnessFinalWorld] | |
| -- Concrete interpreter simulation helpers | |
| private theorem slice_index_witnessLinked (k : Nat) (hk : k < 13) (w : U64) | |
| (hw : witnessLinkedRawList[k]? = some w) : | |
| Slice.index_usize (alloc.vec.Vec.deref witnessLinkedCode) (mkUsizeSmall k) = | |
| Result.ok w := by | |
| have hlt : k < (alloc.vec.Vec.deref witnessLinkedCode).val.length := by | |
| simp [witnessLinkedCode, alloc.vec.Vec.deref, mkVecSmall_val, witnessLinkedRawList]; omega | |
| have hidx := slice_index_at (alloc.vec.Vec.deref witnessLinkedCode) k (by omega) hlt | |
| have heq : (alloc.vec.Vec.deref witnessLinkedCode).val[k]'hlt = w := by | |
| have hget : (alloc.vec.Vec.deref witnessLinkedCode).val[k]? = some w := hw | |
| rw [List.getElem?_eq_getElem hlt] at hget | |
| exact Option.some.inj hget | |
| rwa [heq] at hidx | |
| private theorem regs_match_set (st st' : MachineState) | |
| (regs regs' : Array U64 16#usize) (dst : Nat) (hdst : dst < 11) (v : U64) | |
| (hregs' : regs'.val = regs.val.set dst v) | |
| (hmatch : InterpreterRegsMatch witnessLayout st regs) | |
| (hdst_eq : v.bv = (st'.regs dst).toBits witnessLayout st'.world.arrayLen) | |
| (hother : ∀ r, r < 11 → r ≠ dst → | |
| (st'.regs r).toBits witnessLayout st'.world.arrayLen = | |
| (st.regs r).toBits witnessLayout st.world.arrayLen) : | |
| InterpreterRegsMatch witnessLayout st' regs' := by | |
| intro r hr h16 | |
| have hr11 : r < 11 := by simp [REGISTER_COUNT] at hr; exact hr | |
| have h16' : r < regs.val.length := by have := array16_len regs; omega | |
| have hget : regs'.val[r]'h16 = (regs.val.set dst v)[r]'(by rw [List.length_set]; exact h16') := by | |
| simp [hregs'] | |
| rw [hget] | |
| by_cases hre : r = dst | |
| · subst hre | |
| rw [List.getElem_set_self] | |
| exact hdst_eq | |
| · rw [List.getElem_set_ne (Ne.symm hre)] | |
| rw [hmatch r hr h16', hother r hr11 hre] | |
| private theorem exec_body_lddw_step | |
| (pc dst : Nat) (hpc : pc < 12) (hdst : dst < 11) (imm64 : BitVec 64) | |
| (w0 w1 : ebpf_core.api.EbpfInstruction) | |
| (b : WitnessBackend) (istate : ebpf_core.executor.InterpreterState) | |
| (hres : istate.result = none) (hpc_eq : istate.pc.val = pc) | |
| (hw0 : witnessLinkedRawList[pc]? = some w0 := by rfl) | |
| (hw1 : witnessLinkedRawList[pc + 1]? = some w1 := by rfl) | |
| (hc0 : ebpf_core.api.EbpfInstruction.code w0 = Result.ok ⟨24#8⟩ := by rw [ei_code_ok]; congr 1) | |
| (hc1 : ebpf_core.api.EbpfInstruction.code w1 = Result.ok 0#u8 := by rw [ei_code_ok]; congr 1) | |
| (hd1 : ebpf_core.api.EbpfInstruction.dst_reg w1 = Result.ok 0#u8 := by rw [ei_dst_ok]; congr 1) | |
| (hs1 : ebpf_core.api.EbpfInstruction.src_reg w1 = Result.ok 0#u8 := by rw [ei_src_ok]; congr 1) | |
| (ho1 : ebpf_core.api.EbpfInstruction.offset w1 = Result.ok 0#i16 := by rw [ebpf_offset_spec]; congr 1) | |
| (hi1 : ebpf_core.api.EbpfInstruction.imm w1 = Result.ok ⟨(imm64 >>> 32).setWidth 32⟩ := by rw [ei_imm_ok]; congr 1) | |
| (hd0 : ebpf_core.api.EbpfInstruction.dst_reg w0 = Result.ok ⟨BitVec.ofNat 8 dst⟩ := by rw [ei_dst_ok]; congr 1) | |
| (hs0 : ebpf_core.api.EbpfInstruction.src_reg w0 = Result.ok 0#u8 := by rw [ei_src_ok]; congr 1) | |
| (hi0 : ebpf_core.api.EbpfInstruction.imm w0 = Result.ok ⟨imm64.setWidth 32⟩ := by rw [ei_imm_ok]; congr 1) : | |
| ∃ stateNext, | |
| ebpf_core.executor.execute_core_loop1.body witnessBackendInst | |
| (alloc.vec.Vec.deref witnessLinkedCode) b istate = Result.ok (.cont (b, stateNext)) ∧ | |
| stateNext.result = none ∧ | |
| stateNext.pc.val = pc + 2 ∧ | |
| stateNext.registers.val = istate.registers.val.set dst ⟨imm64⟩ := by | |
| unfold ebpf_core.executor.execute_core_loop1.body | |
| rw [hres] | |
| simp only [core.option.Option.is_none, Option.isNone_none, ↓reduceIte] | |
| have hpc_mk : istate.pc = mkUsizeSmall pc := | |
| usize_eq_of_val (by rw [hpc_eq, mkUsizeSmall_val pc (by omega)]) | |
| have hlen13 : (alloc.vec.Vec.deref witnessLinkedCode).val.length = 13 := by | |
| simp [witnessLinkedCode, alloc.vec.Vec.deref, mkVecSmall_val, witnessLinkedRawList] | |
| have hnge : ¬ (istate.pc ≥ Slice.len (alloc.vec.Vec.deref witnessLinkedCode)) := by | |
| show ¬ (istate.pc.val ≥ (alloc.vec.Vec.deref witnessLinkedCode).val.length); omega | |
| rw [ite_eq_right_of_eq_false _ _ (eq_false hnge)] | |
| rw [hpc_mk, slice_index_witnessLinked pc (by omega) w0 hw0, Aeneas.Std.bind_ok] | |
| rw [usize_add_one_small _ pc (by omega) (mkUsizeSmall_val pc (by omega)), Aeneas.Std.bind_ok] | |
| rw [hc0, Aeneas.Std.bind_ok] | |
| rw [ite_eq_left_of_eq_true _ _ (eq_true bpf_lddw_val.symm)] | |
| have hnge1 : ¬ (mkUsizeSmall (pc + 1) ≥ Slice.len (alloc.vec.Vec.deref witnessLinkedCode)) := by | |
| show ¬ ((mkUsizeSmall (pc + 1)).val ≥ (alloc.vec.Vec.deref witnessLinkedCode).val.length) | |
| rw [mkUsizeSmall_val (pc + 1) (by omega), hlen13]; omega | |
| rw [ite_eq_right_of_eq_false _ _ (eq_false hnge1)] | |
| rw [slice_index_witnessLinked (pc + 1) (by omega) w1 hw1, Aeneas.Std.bind_ok] | |
| rw [hc1, Aeneas.Std.bind_ok] | |
| simp only [show ((0#u8 : U8) != 0#u8) = false by decide, Bool.false_eq_true, ↓reduceIte] | |
| rw [hd1, Aeneas.Std.bind_ok] | |
| simp only [show ((0#u8 : U8) != 0#u8) = false by decide, Bool.false_eq_true, ↓reduceIte] | |
| rw [hs1, Aeneas.Std.bind_ok] | |
| simp only [show ((0#u8 : U8) != 0#u8) = false by decide, Bool.false_eq_true, ↓reduceIte] | |
| rw [ho1, Aeneas.Std.bind_ok] | |
| simp only [show ((0#i16 : I16) != 0#i16) = false by decide, Bool.false_eq_true, ↓reduceIte] | |
| rw [hi1, Aeneas.Std.bind_ok] | |
| delta ebpf_core.visitor.BpfVisitor.visit.default | |
| ebpf_core.visitor.BpfVisitor.visit.default.match_1 | |
| rw [hd0, Aeneas.Std.bind_ok, hi0, Aeneas.Std.bind_ok, lift, Aeneas.Std.bind_ok, | |
| hs0, Aeneas.Std.bind_ok, hc0, Aeneas.Std.bind_ok] | |
| simp +decide only [↓reduceDIte] | |
| unfold ebpf_core.executor.InterpreterState.Insts.Ebpf_coreVisitorBpfVisitor.load64 | |
| simp only [show ((0#u8 : U8) != 0#u8) = false by decide, Bool.false_eq_true, ↓reduceIte, | |
| lift, Aeneas.Std.bind_ok] | |
| rw [u64_shl_i32_ok _ 32#i32 (by decide) (by decide), Aeneas.Std.bind_ok] | |
| have h32 : I32.toNat 32#i32 = 32 := by decide | |
| rw [h32] | |
| have hval : (UScalar.cast .U64 (IScalar.hcast .U32 (⟨imm64.setWidth 32⟩ : I32)) : U64) ||| | |
| (⟨(UScalar.cast .U64 (IScalar.hcast .U32 (⟨(imm64 >>> 32).setWidth 32⟩ : I32))).bv <<< 32⟩ : U64) = | |
| ⟨imm64⟩ := by | |
| apply UScalar.eq_of_val_eq | |
| change (BitVec.zeroExtend 64 (BitVec.signExtend 32 (imm64.setWidth 32)) ||| | |
| BitVec.zeroExtend 64 (BitVec.signExtend 32 ((imm64 >>> 32).setWidth 32)) <<< 32).toNat = imm64.toNat | |
| rw [BitVec.signExtend_eq, BitVec.signExtend_eq] | |
| congr 1; bv_decide | |
| rw [hval] | |
| obtain ⟨regs', hset, hregs'_val⟩ := set_reg_interp_small | |
| { registers := istate.registers, pc := mkUsizeSmall (pc + 1), | |
| next_imm := ⟨(imm64 >>> 32).setWidth 32⟩, result := none } | |
| dst (by omega) ⟨imm64⟩ | |
| rw [hset, Aeneas.Std.bind_ok] | |
| rw [advance_pc_one_small _ (pc + 1) (by omega) (mkUsizeSmall_val (pc + 1) (by omega)), | |
| Aeneas.Std.bind_ok, Aeneas.Std.bind_ok] | |
| refine ⟨_, rfl, rfl, mkUsizeSmall_val (pc + 2) (by omega), hregs'_val⟩ | |
| -- Shared dispatch + body unfolding helper for all non-`lddw` instructions (cases 3..9). | |
| private inductive WitnessOp | |
| | mov64 | |
| | store | |
| | load | |
| | callExternal | |
| | loadPacket | |
| | add64 | |
| | exit | |
| private def WitnessOp.code : WitnessOp → U8 | |
| | .mov64 => ⟨0xB7#8⟩ | |
| | .store => ⟨0x7B#8⟩ | |
| | .load => ⟨0x79#8⟩ | |
| | .callExternal => ⟨0x85#8⟩ | |
| | .loadPacket => ⟨0x20#8⟩ | |
| | .add64 => ⟨0x0F#8⟩ | |
| | .exit => ⟨0x95#8⟩ | |
| private def witnessVisitBody (op : WitnessOp) (dst src : U8) (imm : I32) (off : I16) | |
| (s1 : ebpf_core.executor.InterpreterState) (b : WitnessBackend) : | |
| Result (core.result.Result Unit ebpf_core.error.VerifierErrorKind × | |
| ebpf_core.executor.InterpreterState × WitnessBackend) := | |
| match op with | |
| | .mov64 => | |
| ebpf_core.executor.InterpreterState.Insts.Ebpf_coreVisitorBpfVisitor.mov64 | |
| witnessBackendInst s1 b dst (.Value (IScalar.hcast .U64 imm)) | |
| | .store => | |
| ebpf_core.executor.InterpreterState.Insts.Ebpf_coreVisitorBpfVisitor.store | |
| witnessBackendInst s1 b dst off (.Reg src) .U64 | |
| | .load => | |
| ebpf_core.executor.InterpreterState.Insts.Ebpf_coreVisitorBpfVisitor.load | |
| witnessBackendInst s1 b dst off src .U64 | |
| | .callExternal => | |
| ebpf_core.executor.InterpreterState.Insts.Ebpf_coreVisitorBpfVisitor.call_external | |
| witnessBackendInst s1 b (IScalar.hcast .U32 imm) | |
| | .loadPacket => | |
| ebpf_core.executor.InterpreterState.Insts.Ebpf_coreVisitorBpfVisitor.load_from_packet | |
| witnessBackendInst s1 b 0#u8 6#u8 imm none .U32 | |
| | .add64 => | |
| ebpf_core.executor.InterpreterState.Insts.Ebpf_coreVisitorBpfVisitor.add64 | |
| witnessBackendInst s1 b dst (.Reg src) | |
| | .exit => | |
| ebpf_core.executor.InterpreterState.Insts.Ebpf_coreVisitorBpfVisitor.exit | |
| witnessBackendInst s1 b | |
| private theorem exec_body_non_lddw_step | |
| (pc : Nat) (hpc : pc < 13) (w : ebpf_core.api.EbpfInstruction) | |
| (wop : WitnessOp) (dst src : U8) (imm : I32) (off : I16) | |
| (b b' : WitnessBackend) (istate stateNext : ebpf_core.executor.InterpreterState) | |
| (hres : istate.result = none) (hpc_eq : istate.pc.val = pc) | |
| (hvisit : witnessVisitBody wop dst src imm off | |
| { registers := istate.registers, pc := mkUsizeSmall (pc + 1), | |
| next_imm := 0#i32, result := none } b = | |
| Result.ok (.Ok (), stateNext, b')) | |
| (hw : witnessLinkedRawList[pc]? = some w := by rfl) | |
| (hsrc0 : wop = .callExternal → src = 0#u8 := | |
| by first | (intro _; rfl) | (intro h; cases h)) | |
| (hc : ebpf_core.api.EbpfInstruction.code w = Result.ok wop.code := by rw [ei_code_ok]; congr 1) | |
| (hd : ebpf_core.api.EbpfInstruction.dst_reg w = Result.ok dst := by rw [ei_dst_ok]; congr 1) | |
| (hi : ebpf_core.api.EbpfInstruction.imm w = Result.ok imm := by rw [ei_imm_ok]; congr 1) | |
| (hs : ebpf_core.api.EbpfInstruction.src_reg w = Result.ok src := by rw [ei_src_ok]; congr 1) | |
| (ho : ebpf_core.api.EbpfInstruction.offset w = Result.ok off := by rw [ebpf_offset_spec]; congr 1) : | |
| ebpf_core.executor.execute_core_loop1.body witnessBackendInst | |
| (alloc.vec.Vec.deref witnessLinkedCode) b istate = | |
| Result.ok (.cont (b', stateNext)) := by | |
| have hnot18 : ¬ (wop.code = ebpf_core.api.BPF_LDDW) := by | |
| cases wop <;> (rw [bpf_lddw_val]; decide) | |
| have hpc_mk : istate.pc = mkUsizeSmall pc := | |
| usize_eq_of_val (by rw [hpc_eq, mkUsizeSmall_val pc (by omega)]) | |
| have hlen13 : (alloc.vec.Vec.deref witnessLinkedCode).val.length = 13 := by | |
| simp [witnessLinkedCode, alloc.vec.Vec.deref, mkVecSmall_val, witnessLinkedRawList] | |
| have hnge : ¬ (istate.pc ≥ Slice.len (alloc.vec.Vec.deref witnessLinkedCode)) := by | |
| show ¬ (istate.pc.val ≥ (alloc.vec.Vec.deref witnessLinkedCode).val.length); omega | |
| unfold ebpf_core.executor.execute_core_loop1.body | |
| rw [hres] | |
| simp only [core.option.Option.is_none, Option.isNone_none, ↓reduceIte] | |
| rw [ite_eq_right_of_eq_false _ _ (eq_false hnge), hpc_mk] | |
| rw [slice_index_witnessLinked pc hpc w hw, Aeneas.Std.bind_ok] | |
| rw [usize_add_one_small _ pc (by omega) (mkUsizeSmall_val pc (by omega)), Aeneas.Std.bind_ok] | |
| rw [hc, Aeneas.Std.bind_ok, ite_eq_right_of_eq_false _ _ (eq_false hnot18)] | |
| delta ebpf_core.visitor.BpfVisitor.visit.default | |
| ebpf_core.visitor.BpfVisitor.visit.default.match_1 | |
| rw [hd, Aeneas.Std.bind_ok, hi, Aeneas.Std.bind_ok, lift, Aeneas.Std.bind_ok, | |
| hs, Aeneas.Std.bind_ok, hc, Aeneas.Std.bind_ok] | |
| cases wop <;> ( | |
| dsimp only [WitnessOp.code] at * | |
| simp +decide only [↓reduceDIte] at * | |
| first | |
| | (rw [ho, Aeneas.Std.bind_ok]) | |
| | (have hsr : src = (0#8#uscalar : U8) := by rw [hsrc0 trivial]; decide | |
| subst hsr | |
| simp only [lift, Aeneas.Std.bind_ok]) | |
| | skip | |
| dsimp only [witnessVisitBody] at hvisit | |
| rw [hvisit, Aeneas.Std.bind_ok]; rfl) | |
| private theorem witness_simulates : | |
| BackendSimulatesProgram witnessBackendInst (alloc.vec.Vec.deref witnessLinkedCode) | |
| witnessProg witnessEnv witnessLayout witnessSecret | |
| witnessVerified.struct_access_instructions.val witnessLinkage.struct_mappings.val | |
| WitnessRel := by | |
| refine ⟨by have := usize_max_ge_31; simp [witnessProg, witnessProgList]; omega, | |
| link_instructions_matches_prog witnessVerified witnessLinkage witnessLinkedCode | |
| witnessProg witnessLayout witness_decode_ok witnessLinkage_conforms witness_sa_wf witness_link_ok, | |
| rfl, ?_⟩ | |
| intro b st hrel istate hres hpc hregs | |
| cases hrel with | |
| | step i => | |
| match i with | |
| | ⟨0, _⟩ => | |
| dsimp only [witnessStepTable] at * | |
| refine ⟨fun st' hst => ?_, fun _ _ hst => by rw [step_st0] at hst; cases hst⟩ | |
| rw [step_st0] at hst; cases hst | |
| obtain ⟨stateNext, hbody, hres', hpc', hregs'⟩ := exec_body_lddw_step 0 1 (by omega) (by omega) | |
| 0x0000_0007_0000_0000#64 0x0000_0000_0000_0118#u64 0x0000_0007_0000_0000#u64 | |
| witnessB0 istate hres hpc | |
| refine ⟨stateNext, witnessB0, hbody, hres', hpc', ?_, WitnessRel.step 1⟩ | |
| exact regs_match_set st0 st1 istate.registers stateNext.registers 1 (by omega) _ hregs' hregs | |
| (by decide) | |
| (fun r hr hne => by match r with | 0 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 => rfl | 1 => contradiction | _ + 11 => omega) | |
| | ⟨1, _⟩ => | |
| dsimp only [witnessStepTable] at * | |
| refine ⟨fun st' hst => ?_, fun _ _ hst => by rw [step_st1] at hst; cases hst⟩ | |
| rw [step_st1] at hst; cases hst | |
| obtain ⟨stateNext, hbody, hres', hpc', hregs'⟩ := exec_body_lddw_step 2 2 (by omega) (by omega) | |
| 0x0000_0003_0000_0000#64 0x0000_0000_0000_0218#u64 0x0000_0003_0000_0000#u64 | |
| witnessB0 istate hres hpc | |
| refine ⟨stateNext, witnessB0, hbody, hres', hpc', ?_, WitnessRel.step 2⟩ | |
| exact regs_match_set st1 st2 istate.registers stateNext.registers 2 (by omega) _ hregs' hregs | |
| (by decide) | |
| (fun r hr hne => by match r with | 0 | 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 => rfl | 2 => contradiction | _ + 11 => omega) | |
| | ⟨2, _⟩ => | |
| dsimp only [witnessStepTable] at * | |
| refine ⟨fun st' hst => ?_, fun _ _ hst => by rw [step_st2] at hst; cases hst⟩ | |
| rw [step_st2] at hst; cases hst | |
| obtain ⟨stateNext, hbody, hres', hpc', hregs'⟩ := exec_body_lddw_step 4 3 (by omega) (by omega) | |
| 0x0000_0003_0000_0000#64 0x0000_0000_0000_0318#u64 0x0000_0003_0000_0000#u64 | |
| witnessB0 istate hres hpc | |
| refine ⟨stateNext, witnessB0, hbody, hres', hpc', ?_, WitnessRel.step 3⟩ | |
| exact regs_match_set st2 st3 istate.registers stateNext.registers 3 (by omega) _ hregs' hregs | |
| (by decide) | |
| (fun r hr hne => by match r with | 0 | 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 => rfl | 3 => contradiction | _ + 11 => omega) | |
| | ⟨3, _⟩ => | |
| dsimp only [witnessStepTable] at * | |
| refine ⟨fun st' hst => ?_, fun _ _ hst => by rw [step_st3] at hst; cases hst⟩ | |
| rw [step_st3] at hst; cases hst | |
| obtain ⟨regs', hset, hregs'_val⟩ := set_reg_interp_small | |
| { registers := istate.registers, pc := mkUsizeSmall (6 + 1), next_imm := 0#i32, result := none } | |
| 4 (by omega) (IScalar.hcast .U64 42#i32) | |
| have hv : witnessVisitBody .mov64 ⟨4#8⟩ 0#u8 42#i32 0#i16 | |
| { registers := istate.registers, pc := mkUsizeSmall (6 + 1), next_imm := 0#i32, result := none } | |
| witnessB0 = | |
| Result.ok (.Ok (), { registers := regs', pc := mkUsizeSmall (6 + 1), next_imm := 0#i32, result := none }, | |
| witnessB0) := by | |
| unfold witnessVisitBody | |
| unfold ebpf_core.executor.InterpreterState.Insts.Ebpf_coreVisitorBpfVisitor.mov64 | |
| ebpf_core.executor.InterpreterState.compute_source | |
| rw [Aeneas.Std.bind_ok, hset, Aeneas.Std.bind_ok] | |
| have hbody := exec_body_non_lddw_step 6 (by omega) 0x0000_002A_0000_04B7#u64 | |
| .mov64 ⟨4#8⟩ 0#u8 42#i32 0#i16 witnessB0 witnessB0 istate _ hres hpc hv | |
| refine ⟨_, witnessB0, hbody, rfl, mkUsizeSmall_val 7 (by omega), ?_, WitnessRel.step 4⟩ | |
| exact regs_match_set st3 st4 istate.registers regs' 4 (by omega) _ hregs'_val hregs (by decide) | |
| (fun r hr hne => by match r with | 0 | 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9 | 10 => rfl | 4 => contradiction | _ + 11 => omega) | |
| | ⟨4, _⟩ => | |
| dsimp only [witnessStepTable] at * | |
| refine ⟨fun st' hst => ?_, fun _ _ hst => by rw [step_st4] at hst; cases hst⟩ | |
| rw [step_st4] at hst; cases hst | |
| have hlen16 := array16_len istate.registers | |
| have hr2_eq : istate.registers.val[2]'(by omega) = 0x0000_0003_0000_0000#u64 := by | |
| apply UScalar.eq_of_val_eq | |
| exact congrArg BitVec.toNat (hregs 2 (by decide) (by omega)) | |
| have hr4_eq : istate.registers.val[4]'(by omega) = 42#u64 := by | |
| apply UScalar.eq_of_val_eq | |
| exact congrArg BitVec.toNat (hregs 4 (by decide) (by omega)) | |
| have hv : witnessVisitBody .store ⟨2#8⟩ ⟨4#8⟩ 0#i32 0#i16 | |
| { registers := istate.registers, pc := mkUsizeSmall (7 + 1), next_imm := 0#i32, result := none } | |
| witnessB0 = | |
| Result.ok (.Ok (), { registers := istate.registers, pc := mkUsizeSmall (7 + 1), next_imm := 0#i32, result := none }, | |
| bAfterStore) := by | |
| unfold witnessVisitBody | |
| unfold ebpf_core.executor.InterpreterState.Insts.Ebpf_coreVisitorBpfVisitor.store | |
| ebpf_core.executor.InterpreterState.compute_source | |
| dsimp only | |
| rw [reg_interp_u8 _ ⟨4#8⟩ 4 (by omega) rfl, Aeneas.Std.bind_ok] | |
| rw [reg_interp_u8 _ ⟨2#8⟩ 2 (by omega) rfl, Aeneas.Std.bind_ok] | |
| simp only [hr2_eq, hr4_eq, lift, Aeneas.Std.bind_ok] | |
| have haddr : core.num.U64.wrapping_add 0x0000_0003_0000_0000#u64 | |
| (IScalar.hcast .U64 (IScalar.cast .I64 0#i16)) = 0x0000_0003_0000_0000#u64 := by decide | |
| rw [haddr] | |
| dsimp only [witnessBackendInst] | |
| simp only [↓reduceIte, Aeneas.Std.bind_ok] | |
| rfl | |
| have hbody := exec_body_non_lddw_step 7 (by omega) 0x0000_0000_0000_427B#u64 | |
| .store ⟨2#8⟩ ⟨4#8⟩ 0#i32 0#i16 witnessB0 bAfterStore istate _ hres hpc hv | |
| refine ⟨_, bAfterStore, hbody, rfl, mkUsizeSmall_val 8 (by omega), ?_, WitnessRel.step 5⟩ | |
| intro r hr h16; exact hregs r hr h16 | |
| | ⟨5, _⟩ => | |
| dsimp only [witnessStepTable] at * | |
| refine ⟨fun st' hst => ?_, fun _ _ hst => by rw [step_st5] at hst; cases hst⟩ | |
| rw [step_st5] at hst; cases hst | |
| have hlen16 := array16_len istate.registers | |
| have hr3_eq : istate.registers.val[3]'(by omega) = 0x0000_0003_0000_0000#u64 := by | |
| apply UScalar.eq_of_val_eq | |
| exact congrArg BitVec.toNat (hregs 3 (by decide) (by omega)) | |
| obtain ⟨regs', hset, hregs'_val⟩ := set_reg_interp_small | |
| { registers := istate.registers, pc := mkUsizeSmall (8 + 1), next_imm := 0#i32, result := none } | |
| 8 (by omega) ⟨42#64⟩ | |
| have hv : witnessVisitBody .load ⟨8#8⟩ ⟨3#8⟩ 0#i32 0#i16 | |
| { registers := istate.registers, pc := mkUsizeSmall (8 + 1), next_imm := 0#i32, result := none } | |
| bAfterStore = | |
| Result.ok (.Ok (), { registers := regs', pc := mkUsizeSmall (8 + 1), next_imm := 0#i32, result := none }, | |
| bAfterStore) := by | |
| unfold witnessVisitBody | |
| unfold ebpf_core.executor.InterpreterState.Insts.Ebpf_coreVisitorBpfVisitor.load | |
| rw [reg_interp_u8 _ ⟨3#8⟩ 3 (by omega) rfl, Aeneas.Std.bind_ok] | |
| simp only [hr3_eq, lift, Aeneas.Std.bind_ok] | |
| have haddr : core.num.U64.wrapping_add 0x0000_0003_0000_0000#u64 | |
| (IScalar.hcast .U64 (IScalar.cast .I64 0#i16)) = 0x0000_0003_0000_0000#u64 := by decide | |
| rw [haddr] | |
| dsimp only [witnessBackendInst, bAfterStore] | |
| simp only [↓reduceIte, Aeneas.Std.bind_ok] | |
| rw [hset, Aeneas.Std.bind_ok] | |
| have hbody := exec_body_non_lddw_step 8 (by omega) 0x0000_0000_0000_3879#u64 | |
| .load ⟨8#8⟩ ⟨3#8⟩ 0#i32 0#i16 bAfterStore bAfterStore istate _ hres hpc hv | |
| refine ⟨_, bAfterStore, hbody, rfl, mkUsizeSmall_val 9 (by omega), ?_, WitnessRel.step 6⟩ | |
| exact regs_match_set st5 st6 istate.registers regs' 8 (by omega) _ hregs'_val hregs rfl | |
| (fun r hr hne => by match r with | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | 10 => rfl | 8 => contradiction | _ + 11 => omega) | |
| | ⟨6, _⟩ => | |
| dsimp only [witnessStepTable] at * | |
| refine ⟨fun st' hst => ?_, fun _ _ hst => by rw [step_st6] at hst; cases hst⟩ | |
| rw [step_st6] at hst; cases hst | |
| obtain ⟨regs', hset, hregs'_val⟩ := set_reg_interp_u8 | |
| { registers := istate.registers, pc := mkUsizeSmall (9 + 1), next_imm := 0#i32, result := none } | |
| 0#u8 0 (by omega) (by decide) 0#u64 | |
| have hv : witnessVisitBody .callExternal 0#u8 ⟨0#8⟩ 1#i32 0#i16 | |
| { registers := istate.registers, pc := mkUsizeSmall (9 + 1), next_imm := 0#i32, result := none } | |
| bAfterStore = | |
| Result.ok (.Ok (), { registers := regs', pc := mkUsizeSmall (9 + 1), next_imm := 0#i32, result := none }, | |
| bAfterCall) := by | |
| unfold witnessVisitBody | |
| unfold ebpf_core.executor.InterpreterState.Insts.Ebpf_coreVisitorBpfVisitor.call_external | |
| rw [reg_interp_u8 _ 1#u8 1 (by omega) (by decide), Aeneas.Std.bind_ok] | |
| rw [reg_interp_u8 _ 2#u8 2 (by omega) (by decide), Aeneas.Std.bind_ok] | |
| rw [reg_interp_u8 _ 3#u8 3 (by omega) (by decide), Aeneas.Std.bind_ok] | |
| rw [reg_interp_u8 _ 4#u8 4 (by omega) (by decide), Aeneas.Std.bind_ok] | |
| rw [reg_interp_u8 _ 5#u8 5 (by omega) (by decide), Aeneas.Std.bind_ok] | |
| have hidx1 : IScalar.hcast .U32 1#i32 = 1#u32 := by decide | |
| rw [hidx1] | |
| dsimp only [witnessBackendInst, bAfterStore] | |
| simp only [↓reduceIte, Aeneas.Std.bind_ok] | |
| change Aeneas.Std.bind | |
| (ebpf_core.executor.InterpreterState.set_reg | |
| { registers := istate.registers, pc := mkUsizeSmall (9 + 1), next_imm := 0#i32, | |
| result := none } 0#u8 0#u64) | |
| (fun self1 => Result.ok (core.result.Result.Ok (), self1, bAfterCall)) = _ | |
| rw [hset, Aeneas.Std.bind_ok] | |
| have hbody := exec_body_non_lddw_step 9 (by omega) 0x0000_0001_0000_0085#u64 | |
| .callExternal 0#u8 ⟨0#8⟩ 1#i32 0#i16 bAfterStore bAfterCall istate _ hres hpc hv | |
| refine ⟨_, bAfterCall, hbody, rfl, mkUsizeSmall_val 10 (by omega), ?_, WitnessRel.step 7⟩ | |
| exact regs_match_set st6 st7 istate.registers regs' 0 (by omega) _ hregs'_val hregs rfl | |
| (fun r hr hne => by match r with | |
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 => | |
| simp [st6, st7, mkRegs, mapValPtr0, mkWorld, len0, len1, witnessLayout, RuntimeVal.toBits, HostLayout.bufAddr] | |
| | 0 => contradiction | |
| | _ + 11 => omega) | |
| | ⟨7, _⟩ => | |
| dsimp only [witnessStepTable] at * | |
| refine ⟨fun st' hst => ?_, fun _ _ hst => by rw [step_st7] at hst; cases hst⟩ | |
| rw [step_st7] at hst; cases hst | |
| obtain ⟨regs', hset, hregs'_val⟩ := set_reg_interp_u8 | |
| { registers := istate.registers, pc := mkUsizeSmall (10 + 1), next_imm := 0#i32, result := none } | |
| 0#u8 0 (by omega) (by decide) 100#u64 | |
| have hv : witnessVisitBody .loadPacket 0#u8 0#u8 0#i32 0#i16 | |
| { registers := istate.registers, pc := mkUsizeSmall (10 + 1), next_imm := 0#i32, result := none } | |
| bAfterCall = | |
| Result.ok (.Ok (), { registers := regs', pc := mkUsizeSmall (10 + 1), next_imm := 0#i32, result := none }, | |
| bAfterCall) := by | |
| unfold witnessVisitBody | |
| unfold ebpf_core.executor.InterpreterState.Insts.Ebpf_coreVisitorBpfVisitor.load_from_packet | |
| simp only [Aeneas.Std.bind_ok, lift] | |
| have hchk0 : I32.checked_add 0#i32 0#i32 = some 0#i32 := by decide | |
| rw [hchk0]; dsimp only | |
| rw [reg_interp_u8 _ 6#u8 6 (by omega) (by decide), Aeneas.Std.bind_ok] | |
| dsimp only [witnessBackendInst] | |
| simp only [Aeneas.Std.bind_ok] | |
| change Aeneas.Std.bind | |
| (ebpf_core.executor.InterpreterState.set_reg | |
| { registers := istate.registers, pc := mkUsizeSmall (10 + 1), next_imm := 0#i32, | |
| result := none } 0#u8 100#u64) | |
| (fun self1 => Result.ok (core.result.Result.Ok (), self1, bAfterCall)) = _ | |
| rw [hset, Aeneas.Std.bind_ok] | |
| have hbody := exec_body_non_lddw_step 10 (by omega) 0x0000_0000_0000_0020#u64 | |
| .loadPacket 0#u8 0#u8 0#i32 0#i16 bAfterCall bAfterCall istate _ hres hpc hv | |
| refine ⟨_, bAfterCall, hbody, rfl, mkUsizeSmall_val 11 (by omega), ?_, WitnessRel.step 8⟩ | |
| exact regs_match_set st7 st8 istate.registers regs' 0 (by omega) _ hregs'_val hregs rfl | |
| (fun r hr hne => by match r with | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 => rfl | 0 => contradiction | _ + 11 => omega) | |
| | ⟨8, _⟩ => | |
| dsimp only [witnessStepTable] at * | |
| refine ⟨fun st' hst => ?_, fun _ _ hst => by rw [step_st8] at hst; cases hst⟩ | |
| rw [step_st8] at hst; cases hst | |
| have hlen16 := array16_len istate.registers | |
| have hr0_eq : istate.registers.val[0]'(by omega) = 100#u64 := by | |
| apply UScalar.eq_of_val_eq; exact congrArg BitVec.toNat (hregs 0 (by decide) (by omega)) | |
| have hr8_eq : istate.registers.val[8]'(by omega) = 42#u64 := by | |
| apply UScalar.eq_of_val_eq; exact congrArg BitVec.toNat (hregs 8 (by decide) (by omega)) | |
| obtain ⟨regs', hset, hregs'_val⟩ := set_reg_interp_u8 | |
| { registers := istate.registers, pc := mkUsizeSmall (11 + 1), next_imm := 0#i32, result := none } | |
| ⟨0#8⟩ 0 (by omega) rfl 142#u64 | |
| have hv : witnessVisitBody .add64 ⟨0#8⟩ ⟨8#8⟩ 0#i32 0#i16 | |
| { registers := istate.registers, pc := mkUsizeSmall (11 + 1), next_imm := 0#i32, result := none } | |
| bAfterCall = | |
| Result.ok (.Ok (), { registers := regs', pc := mkUsizeSmall (11 + 1), next_imm := 0#i32, result := none }, | |
| bAfterCall) := by | |
| unfold witnessVisitBody | |
| unfold ebpf_core.executor.InterpreterState.Insts.Ebpf_coreVisitorBpfVisitor.add64 | |
| ebpf_core.executor.InterpreterState.compute_source | |
| dsimp only | |
| rw [reg_interp_u8 _ ⟨0#8⟩ 0 (by omega) rfl, Aeneas.Std.bind_ok] | |
| rw [reg_interp_u8 _ ⟨8#8⟩ 8 (by omega) rfl, Aeneas.Std.bind_ok] | |
| simp only [hr0_eq, hr8_eq, lift, Aeneas.Std.bind_ok] | |
| have hadd : core.num.U64.wrapping_add 100#u64 42#u64 = 142#u64 := by decide | |
| rw [hadd, hset, Aeneas.Std.bind_ok] | |
| have hbody := exec_body_non_lddw_step 11 (by omega) 0x0000_0000_0000_800F#u64 | |
| .add64 ⟨0#8⟩ ⟨8#8⟩ 0#i32 0#i16 bAfterCall bAfterCall istate _ hres hpc hv | |
| refine ⟨_, bAfterCall, hbody, rfl, mkUsizeSmall_val 12 (by omega), ?_, | |
| WitnessRel.halt st9.world rfl⟩ | |
| exact regs_match_set st8 st9 istate.registers regs' 0 (by omega) _ hregs'_val hregs rfl | |
| (fun r hr hne => by match r with | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 => rfl | 0 => contradiction | _ + 11 => omega) | |
| | halt w hlive => | |
| refine ⟨fun _ hst => (by rw [step_st9_any w hlive] at hst; cases hst), fun ret wrld' hst => ?_⟩ | |
| rw [step_st9_any w hlive] at hst | |
| cases hst | |
| have hlen16 := array16_len istate.registers | |
| have hr0_eq : istate.registers.val[0]'(by omega) = 142#u64 := by | |
| apply UScalar.eq_of_val_eq | |
| exact congrArg BitVec.toNat (hregs 0 (by decide) (by omega)) | |
| have hv : witnessVisitBody .exit 0#u8 0#u8 0#i32 0#i16 | |
| { registers := istate.registers, pc := mkUsizeSmall (12 + 1), next_imm := 0#i32, result := none } | |
| bAfterCall = | |
| Result.ok (.Ok (), | |
| { registers := istate.registers, pc := mkUsizeSmall (12 + 1), next_imm := 0#i32, | |
| result := some 142#u64 }, | |
| bAfterCall) := by | |
| unfold witnessVisitBody | |
| unfold ebpf_core.executor.InterpreterState.Insts.Ebpf_coreVisitorBpfVisitor.exit | |
| rw [reg_interp_u8 _ 0#u8 0 (by omega) (by decide), Aeneas.Std.bind_ok] | |
| simp only [hr0_eq] | |
| have hbody := exec_body_non_lddw_step 12 (by omega) 0x0000_0000_0000_0095#u64 | |
| .exit 0#u8 0#u8 0#i32 0#i16 bAfterCall bAfterCall istate _ hres (by simpa [st9] using hpc) hv | |
| exact ⟨_, bAfterCall, hbody, rfl, WitnessRel.halt (bumpWorldStep w) hlive⟩ | |
| /-- Concrete satisfiability / non-vacuity witness theorem proved in | |
| `StarnixEbpf.Proof` (matching the target statement for `StarnixEbpf.Contract`). -/ | |
| theorem backend_simulates_program_satisfiable : | |
| ∃ (B : Type) (inst : ebpf_core.executor.EbpfMachineBackend B) | |
| (verified : ebpf_core.verifier.VerifiedEbpfProgram) | |
| (linkage : ebpf_core.program.ProgramLinkageInfo) | |
| (linkedCode : alloc.vec.Vec ebpf_core.api.EbpfInstruction) | |
| (prog : Program) (env : ExecEnv) (layout : HostLayout) (secret : SecretInit) | |
| (R : B → MachineState → Prop) (b0 : B) (st0 : MachineState) | |
| (istate0 : ebpf_core.executor.InterpreterState) | |
| (arrId : MemoryId) (fuel : Nat) (finalWorld : WorldState), | |
| ValidHostLayout layout ∧ | |
| LinkageConformsLayout linkage layout ∧ | |
| StructAccessesWellFormed verified.struct_access_instructions.val prog ∧ | |
| decodeVerifiedCode verified.code.val = some prog ∧ | |
| ebpf_core.program.link_instructions verified linkage = .ok (.Ok linkedCode) ∧ | |
| (∃ d, Insn.lddwMap d 0 ∈ prog.toList) ∧ | |
| (∃ d₁ d₂, d₁ ≠ d₂ ∧ | |
| Insn.lddwMapValue d₁ 0 0#64 ∈ prog.toList ∧ | |
| Insn.lddwMapValue d₂ 0 0#64 ∈ prog.toList) ∧ | |
| (∃ hIdx hSpec, | |
| Insn.callExternal hIdx ∈ prog.toList ∧ | |
| env.helpers hIdx = some hSpec ∧ | |
| hSpec.ret = .scalar false ∧ | |
| hSpec.invalidateArrayBounds = true) ∧ | |
| (∃ w off ind, Insn.loadPacket w off ind ∈ prog.toList) ∧ | |
| (∃ v, v ≠ 0#64 ∧ st0.regs 0 = .scalar v) ∧ | |
| st0.pc = 0 ∧ | |
| st0.regs 7 = .endArrayPtr arrId 0 ∧ | |
| st0.world.arrayEpoch arrId = 0 ∧ | |
| layout.arrayBase arrId 0 ≠ layout.arrayBase arrId 1 ∧ | |
| (∀ (k : Nat) (hk : k < prog.size), | |
| match prog[k]'hk with | |
| | .jump _ | .condJump _ _ _ _ _ => False | |
| | .exit => k = prog.size - 1 | |
| | _ => True) ∧ | |
| BackendSimulatesProgram inst (alloc.vec.Vec.deref linkedCode) prog env layout secret | |
| verified.struct_access_instructions.val linkage.struct_mappings.val R ∧ | |
| R b0 st0 ∧ | |
| istate0.result = none ∧ | |
| istate0.pc.val = st0.pc ∧ | |
| InterpreterRegsMatch layout st0 istate0.registers ∧ | |
| runSteps prog env layout secret fuel st0 = some (.inl (142#64, finalWorld)) ∧ | |
| finalWorld.arrayEpoch arrId = 1 ∧ | |
| finalWorld.arrayLen arrId 1 < finalWorld.arrayLen arrId 0 ∧ | |
| readBufWord st0.world.bufBytes (.mapValue 0) 0#64 .u64 ≠ | |
| readBufWord finalWorld.bufBytes (.mapValue 0) 0#64 .u64 ∧ | |
| ∃ bFinal, | |
| ebpf_core.executor.execute_core_loop1 inst (alloc.vec.Vec.deref linkedCode) b0 istate0 = | |
| .ok (.Ok 142#u64, bFinal) := by | |
| have hpc0 : witnessIState0.pc.val = st0.pc := by | |
| show (0#usize : Usize).val = 0 | |
| rcases System.Platform.numBits_eq with hnb | hnb <;> | |
| simp [UScalar.val, UScalarTy.Usize_numBits_eq, hnb] | |
| refine ⟨WitnessBackend, witnessBackendInst, witnessVerified, witnessLinkage, witnessLinkedCode, | |
| witnessProg, witnessEnv, witnessLayout, witnessSecret, WitnessRel, witnessB0, st0, | |
| witnessIState0, witnessArrId, 10, witnessFinalWorld, | |
| witnessLayout_valid, witnessLinkage_conforms, witness_sa_wf, witness_decode_ok, witness_link_ok, | |
| ⟨1, by decide⟩, | |
| ⟨2, 3, by decide, by decide, by decide⟩, | |
| ⟨1, witnessHelperSpec, by decide, rfl, rfl, rfl⟩, | |
| ⟨.u32, 0#32, none, by decide⟩, | |
| ⟨77#64, by decide, rfl⟩, | |
| rfl, | |
| rfl, | |
| rfl, | |
| by decide, | |
| ?_, | |
| witness_simulates, | |
| WitnessRel.step 0, | |
| rfl, | |
| hpc0, | |
| witnessIState0_regs_match, | |
| witness_runSteps_eq, | |
| rfl, | |
| by decide, | |
| by decide, | |
| ?_⟩ | |
| · intro k hk | |
| match k with | |
| | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 => trivial | |
| | 12 => dsimp [witnessProg, witnessProgList]; rfl | |
| | n + 13 => | |
| exfalso | |
| have hsz : witnessProg.size = 13 := rfl | |
| omega | |
| · exact execute_core_simulates_run_steps witnessBackendInst | |
| (alloc.vec.Vec.deref witnessLinkedCode) witnessProg witnessEnv witnessLayout witnessSecret | |
| witnessVerified.struct_access_instructions.val witnessLinkage.struct_mappings.val | |
| WitnessRel 10 witnessB0 st0 witnessIState0 142#64 witnessFinalWorld | |
| witness_simulates (WitnessRel.step 0) rfl hpc0 witnessIState0_regs_match witness_runSteps_eq | |
| end StarnixEbpf.Proof |