file_name
stringlengths
5
52
name
stringlengths
4
95
original_source_type
stringlengths
0
23k
source_type
stringlengths
9
23k
source_definition
stringlengths
9
57.9k
source
dict
source_range
dict
file_context
stringlengths
0
721k
dependencies
dict
opens_and_abbrevs
listlengths
2
94
vconfig
dict
interleaved
bool
1 class
verbose_type
stringlengths
1
7.42k
effect
stringclasses
118 values
effect_flags
sequencelengths
0
2
mutual_with
sequencelengths
0
11
ideal_premises
sequencelengths
0
236
proof_features
sequencelengths
0
1
is_simple_lemma
bool
2 classes
is_div
bool
2 classes
is_proof
bool
2 classes
is_simply_typed
bool
2 classes
is_type
bool
2 classes
partial_definition
stringlengths
5
3.99k
completed_definiton
stringlengths
1
1.63M
isa_cross_project_example
bool
1 class
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.run2
val run2 (f1 f2: st unit) (s: machine_state) : machine_state
val run2 (f1 f2: st unit) (s: machine_state) : machine_state
let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 29, "end_line": 558, "start_col": 0, "start_line": 556 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above).
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f1: Vale.X64.Machine_Semantics_s.st Prims.unit -> f2: Vale.X64.Machine_Semantics_s.st Prims.unit -> s: Vale.X64.Machine_Semantics_s.machine_state -> Vale.X64.Machine_Semantics_s.machine_state
Prims.Tot
[ "total" ]
[]
[ "Vale.X64.Machine_Semantics_s.st", "Prims.unit", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.X64.Machine_Semantics_s.run", "Vale.X64.Machine_Semantics_s.op_let_Star", "Vale.X64.Machine_Semantics_s.return" ]
[]
false
false
false
true
false
let run2 (f1 f2: st unit) (s: machine_state) : machine_state =
let open Vale.X64.Machine_Semantics_s in run (let* _ = f1 in let* _ = f2 in return ()) s
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_instr_write_outputs_equiv_states
val lemma_instr_write_outputs_equiv_states (outs: list instr_out) (args: list instr_operand) (vs: instr_ret_t outs) (oprs: instr_operands_t outs args) (s_orig1 s1 s_orig2 s2: machine_state) : Lemma (requires ((equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ((equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2))))
val lemma_instr_write_outputs_equiv_states (outs: list instr_out) (args: list instr_operand) (vs: instr_ret_t outs) (oprs: instr_operands_t outs args) (s_orig1 s1 s_orig2 s2: machine_state) : Lemma (requires ((equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ((equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2))))
let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 323, "start_col": 0, "start_line": 289 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
outs: Prims.list Vale.X64.Instruction_s.instr_out -> args: Prims.list Vale.X64.Instruction_s.instr_operand -> vs: Vale.X64.Instruction_s.instr_ret_t outs -> oprs: Vale.X64.Instruction_s.instr_operands_t outs args -> s_orig1: Vale.X64.Machine_Semantics_s.machine_state -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s_orig2: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.InstructionReorder.equiv_states s_orig1 s_orig2 /\ Vale.Transformers.InstructionReorder.equiv_states s1 s2) (ensures Vale.Transformers.InstructionReorder.equiv_states (Vale.X64.Machine_Semantics_s.instr_write_outputs outs args vs oprs s_orig1 s1) (Vale.X64.Machine_Semantics_s.instr_write_outputs outs args vs oprs s_orig2 s2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.list", "Vale.X64.Instruction_s.instr_out", "Vale.X64.Instruction_s.instr_operand", "Vale.X64.Instruction_s.instr_ret_t", "Vale.X64.Instruction_s.instr_operands_t", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.X64.Instruction_s.instr_operand_inout", "Vale.X64.Instruction_s.instr_val_t", "Vale.X64.Instruction_s.instr_operand_explicit", "Vale.Transformers.InstructionReorder.lemma_instr_write_outputs_equiv_states", "FStar.Pervasives.Native.snd", "Vale.X64.Instruction_s.instr_operand_t", "Vale.X64.Machine_Semantics_s.instr_write_output_explicit", "FStar.Pervasives.Native.fst", "Prims.unit", "Vale.Transformers.InstructionReorder.lemma_instr_write_output_explicit_equiv_states", "FStar.Pervasives.Native.tuple2", "Vale.X64.Instruction_s.coerce", "Vale.X64.Instruction_s.instr_operand_implicit", "Vale.X64.Machine_Semantics_s.instr_write_output_implicit", "Vale.Transformers.InstructionReorder.lemma_instr_write_output_implicit_equiv_states", "FStar.Pervasives.Native.Mktuple2", "Prims.l_and", "Vale.Transformers.InstructionReorder.equiv_states", "Prims.squash", "Vale.X64.Machine_Semantics_s.instr_write_outputs", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_instr_write_outputs_equiv_states (outs: list instr_out) (args: list instr_operand) (vs: instr_ret_t outs) (oprs: instr_operands_t outs args) (s_orig1 s1 s_orig2 s2: machine_state) : Lemma (requires ((equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ((equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) =
match outs with | [] -> () | (_, i) :: outs -> (let (v: instr_val_t i), (vs: instr_ret_t outs) = match outs with | [] -> (vs, ()) | _ :: _ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2)
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.equiv_option_states
val equiv_option_states : s1: FStar.Pervasives.Native.option Vale.X64.Machine_Semantics_s.machine_state -> s2: FStar.Pervasives.Native.option Vale.X64.Machine_Semantics_s.machine_state -> Prims.logical
let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2))
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 77, "end_line": 186, "start_col": 0, "start_line": 184 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *)
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: FStar.Pervasives.Native.option Vale.X64.Machine_Semantics_s.machine_state -> s2: FStar.Pervasives.Native.option Vale.X64.Machine_Semantics_s.machine_state -> Prims.logical
Prims.Tot
[ "total" ]
[]
[ "FStar.Pervasives.Native.option", "Vale.X64.Machine_Semantics_s.machine_state", "Prims.l_and", "Prims.eq2", "Prims.bool", "Vale.Transformers.InstructionReorder.erroring_option_state", "Prims.l_imp", "Prims.b2t", "Prims.op_Negation", "Vale.Transformers.InstructionReorder.equiv_states", "FStar.Pervasives.Native.__proj__Some__item__v", "Prims.logical" ]
[]
false
false
false
true
true
let equiv_option_states (s1 s2: option machine_state) =
(erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2))
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_eval_ins_equiv_states
val lemma_eval_ins_equiv_states (i: ins) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures (equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2)))
val lemma_eval_ins_equiv_states (i: ins) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures (equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2)))
let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 48, "end_line": 414, "start_col": 0, "start_line": 407 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
i: Vale.X64.Machine_Semantics_s.ins -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.InstructionReorder.equiv_states s1 s2) (ensures Vale.Transformers.InstructionReorder.equiv_states (Vale.X64.Machine_Semantics_s.machine_eval_ins i s1) (Vale.X64.Machine_Semantics_s.machine_eval_ins i s2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.ins", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.Transformers.InstructionReorder.lemma_machine_eval_ins_st_equiv_states", "Prims.unit", "Vale.Transformers.InstructionReorder.equiv_states", "Prims.squash", "Vale.X64.Machine_Semantics_s.machine_eval_ins", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let lemma_eval_ins_equiv_states (i: ins) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures (equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) =
lemma_machine_eval_ins_st_equiv_states i s1 s2
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_instr_write_output_implicit_equiv_states
val lemma_instr_write_output_implicit_equiv_states (i: instr_operand_implicit) (v: instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2: machine_state) : Lemma (requires ((equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ((equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2))))
val lemma_instr_write_output_implicit_equiv_states (i: instr_operand_implicit) (v: instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2: machine_state) : Lemma (requires ((equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ((equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2))))
let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2)
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 39, "end_line": 269, "start_col": 0, "start_line": 255 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0"
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 10, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
i: Vale.X64.Instruction_s.instr_operand_implicit -> v: Vale.X64.Instruction_s.instr_val_t (Vale.X64.Instruction_s.IOpIm i) -> s_orig1: Vale.X64.Machine_Semantics_s.machine_state -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s_orig2: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.InstructionReorder.equiv_states s_orig1 s_orig2 /\ Vale.Transformers.InstructionReorder.equiv_states s1 s2) (ensures Vale.Transformers.InstructionReorder.equiv_states (Vale.X64.Machine_Semantics_s.instr_write_output_implicit i v s_orig1 s1) (Vale.X64.Machine_Semantics_s.instr_write_output_implicit i v s_orig2 s2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Instruction_s.instr_operand_implicit", "Vale.X64.Instruction_s.instr_val_t", "Vale.X64.Instruction_s.IOpIm", "Vale.X64.Machine_Semantics_s.machine_state", "Prims._assert", "Vale.Transformers.InstructionReorder.equiv_states_ext", "Prims.unit", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "Vale.X64.Machine_Semantics_s.instr_write_output_implicit", "Prims.l_and", "Vale.Transformers.InstructionReorder.equiv_states", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let lemma_instr_write_output_implicit_equiv_states (i: instr_operand_implicit) (v: instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2: machine_state) : Lemma (requires ((equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ((equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) =
let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2)
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_instr_write_output_explicit_equiv_states
val lemma_instr_write_output_explicit_equiv_states (i: instr_operand_explicit) (v: instr_val_t (IOpEx i)) (o: instr_operand_t i) (s_orig1 s1 s_orig2 s2: machine_state) : Lemma (requires ((equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ((equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2))))
val lemma_instr_write_output_explicit_equiv_states (i: instr_operand_explicit) (v: instr_val_t (IOpEx i)) (o: instr_operand_t i) (s_orig1 s1 s_orig2 s2: machine_state) : Lemma (requires ((equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ((equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2))))
let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2)
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 39, "end_line": 285, "start_col": 0, "start_line": 271 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *)
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 10, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
i: Vale.X64.Instruction_s.instr_operand_explicit -> v: Vale.X64.Instruction_s.instr_val_t (Vale.X64.Instruction_s.IOpEx i) -> o: Vale.X64.Instruction_s.instr_operand_t i -> s_orig1: Vale.X64.Machine_Semantics_s.machine_state -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s_orig2: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.InstructionReorder.equiv_states s_orig1 s_orig2 /\ Vale.Transformers.InstructionReorder.equiv_states s1 s2) (ensures Vale.Transformers.InstructionReorder.equiv_states (Vale.X64.Machine_Semantics_s.instr_write_output_explicit i v o s_orig1 s1) (Vale.X64.Machine_Semantics_s.instr_write_output_explicit i v o s_orig2 s2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Instruction_s.instr_operand_explicit", "Vale.X64.Instruction_s.instr_val_t", "Vale.X64.Instruction_s.IOpEx", "Vale.X64.Instruction_s.instr_operand_t", "Vale.X64.Machine_Semantics_s.machine_state", "Prims._assert", "Vale.Transformers.InstructionReorder.equiv_states_ext", "Prims.unit", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "Vale.X64.Machine_Semantics_s.instr_write_output_explicit", "Prims.l_and", "Vale.Transformers.InstructionReorder.equiv_states", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let lemma_instr_write_output_explicit_equiv_states (i: instr_operand_explicit) (v: instr_val_t (IOpEx i)) (o: instr_operand_t i) (s_orig1 s1 s_orig2 s2: machine_state) : Lemma (requires ((equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ((equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) =
let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2)
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.wrap_ss
val wrap_ss (f: (machine_state -> machine_state)) : st unit
val wrap_ss (f: (machine_state -> machine_state)) : st unit
let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s)
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 11, "end_line": 1030, "start_col": 0, "start_line": 1027 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) )
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: (_: Vale.X64.Machine_Semantics_s.machine_state -> Vale.X64.Machine_Semantics_s.machine_state) -> Vale.X64.Machine_Semantics_s.st Prims.unit
Prims.Tot
[ "total" ]
[]
[ "Vale.X64.Machine_Semantics_s.machine_state", "Vale.X64.Machine_Semantics_s.op_let_Star", "Prims.unit", "Vale.X64.Machine_Semantics_s.get", "Vale.X64.Machine_Semantics_s.set", "Vale.X64.Machine_Semantics_s.st" ]
[]
false
false
false
true
false
let wrap_ss (f: (machine_state -> machine_state)) : st unit =
let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s)
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_unchanged_except_append_symmetric
val lemma_unchanged_except_append_symmetric (a1 a2: list location) (s1 s2: machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2))
val lemma_unchanged_except_append_symmetric (a1 a2: list location) (s1 s2: machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2))
let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux)
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 66, "end_line": 607, "start_col": 0, "start_line": 596 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux)
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a1: Prims.list Vale.Transformers.Locations.location -> a2: Prims.list Vale.Transformers.Locations.location -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.BoundedInstructionEffects.unchanged_except (a1 @ a2) s1 s2) (ensures Vale.Transformers.BoundedInstructionEffects.unchanged_except (a2 @ a1) s1 s2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.list", "Vale.Transformers.Locations.location", "Vale.X64.Machine_Semantics_s.machine_state", "FStar.Classical.forall_intro", "Prims.l_imp", "Prims.l_or", "Prims.b2t", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.Locations.disjoint_location_from_locations", "FStar.List.Tot.Base.append", "Prims.eq2", "Vale.Transformers.Locations.location_val_t", "Vale.Transformers.Locations.eval_location", "FStar.Classical.move_requires", "Prims.unit", "Vale.Def.PossiblyMonad.uu___is_Ok", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern", "Vale.Transformers.InstructionReorder.lemma_disjoint_location_from_locations_append", "Vale.Transformers.BoundedInstructionEffects.unchanged_except" ]
[]
false
false
true
false
false
let lemma_unchanged_except_append_symmetric (a1 a2: list location) (s1 s2: machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) =
let aux a : Lemma (requires ((!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux)
false
Hacl.Test.ECDSA.fst
Hacl.Test.ECDSA.siggen_vector
val siggen_vector : Type0
let siggen_vector = vec8 & vec8 & vec8 & vec8 & vec8 & vec8 & vec8
{ "file_name": "code/tests/Hacl.Test.ECDSA.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 66, "end_line": 78, "start_col": 0, "start_line": 78 }
module Hacl.Test.ECDSA open FStar.HyperStack.ST open Test.Lowstarize open Lib.IntTypes open Hacl.P256 open Spec.ECDSA.Test.Vectors module L = Test.Lowstarize module B = LowStar.Buffer #set-options "--fuel 0 --ifuel 0 --z3rlimit 100" noextract let sigver_vectors256_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_256 noextract let sigver_vectors384_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_384 noextract let sigver_vectors512_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_512 noextract let siggen_vectors256_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_256 noextract let siggen_vectors384_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_384 noextract let siggen_vectors512_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_512 %splice[sigver_vectors256_low] (lowstarize_toplevel "sigver_vectors256_tmp" "sigver_vectors256_low") %splice[sigver_vectors384_low] (lowstarize_toplevel "sigver_vectors384_tmp" "sigver_vectors384_low") %splice[sigver_vectors512_low] (lowstarize_toplevel "sigver_vectors512_tmp" "sigver_vectors512_low") %splice[siggen_vectors256_low] (lowstarize_toplevel "siggen_vectors256_tmp" "siggen_vectors256_low") %splice[siggen_vectors384_low] (lowstarize_toplevel "siggen_vectors384_tmp" "siggen_vectors384_low") %splice[siggen_vectors512_low] (lowstarize_toplevel "siggen_vectors512_tmp" "siggen_vectors512_low") // Cheap alternative to friend Lib.IntTypes needed because Test.Lowstarize uses UInt8.t assume val declassify_uint8: squash (uint8 == UInt8.t) let vec8 = L.lbuffer UInt8.t let sigver_vector = vec8 & vec8 & vec8 & vec8 & vec8 & bool
{ "checked_file": "/", "dependencies": [ "Test.Lowstarize.fst.checked", "Spec.P256.fst.checked", "Spec.ECDSA.Test.Vectors.fst.checked", "prims.fst.checked", "LowStar.Printf.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "Lib.ByteBuffer.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.P256.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Int32.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "C.String.fsti.checked", "C.Loops.fst.checked", "C.fst.checked" ], "interface_file": false, "source_file": "Hacl.Test.ECDSA.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "Test.Lowstarize", "short_module": "L" }, { "abbrev": false, "full_module": "Spec.ECDSA.Test.Vectors", "short_module": null }, { "abbrev": false, "full_module": "Hacl.P256", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Test.Lowstarize", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Type0
Prims.Tot
[ "total" ]
[]
[ "FStar.Pervasives.Native.tuple7", "Hacl.Test.ECDSA.vec8" ]
[]
false
false
false
true
true
let siggen_vector =
vec8 & vec8 & vec8 & vec8 & vec8 & vec8 & vec8
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_instr_apply_eval_args_equiv_states
val lemma_instr_apply_eval_args_equiv_states (outs: list instr_out) (args: list instr_operand) (f: instr_args_t outs args) (oprs: instr_operands_t_args args) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ((instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2)) )
val lemma_instr_apply_eval_args_equiv_states (outs: list instr_out) (args: list instr_operand) (f: instr_args_t outs args) (oprs: instr_operands_t_args args) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ((instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2)) )
let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 73, "end_line": 218, "start_col": 0, "start_line": 197 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok }
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
outs: Prims.list Vale.X64.Instruction_s.instr_out -> args: Prims.list Vale.X64.Instruction_s.instr_operand -> f: Vale.X64.Instruction_s.instr_args_t outs args -> oprs: Vale.X64.Instruction_s.instr_operands_t_args args -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.InstructionReorder.equiv_states s1 s2) (ensures Vale.X64.Machine_Semantics_s.instr_apply_eval_args outs args f oprs s1 == Vale.X64.Machine_Semantics_s.instr_apply_eval_args outs args f oprs s2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.list", "Vale.X64.Instruction_s.instr_out", "Vale.X64.Instruction_s.instr_operand", "Vale.X64.Instruction_s.instr_args_t", "Vale.X64.Instruction_s.instr_operands_t_args", "Vale.X64.Machine_Semantics_s.machine_state", "FStar.Pervasives.Native.option", "Vale.X64.Instruction_s.instr_val_t", "Vale.Transformers.InstructionReorder.lemma_instr_apply_eval_args_equiv_states", "Prims.unit", "Vale.X64.Instruction_s.arrow", "Vale.X64.Instruction_s.coerce", "FStar.Pervasives.Native.tuple2", "Vale.X64.Instruction_s.instr_operand_explicit", "FStar.Pervasives.Native.Mktuple2", "Vale.X64.Machine_Semantics_s.instr_eval_operand_explicit", "FStar.Pervasives.Native.fst", "Vale.X64.Instruction_s.instr_operand_t", "FStar.Pervasives.Native.snd", "Vale.X64.Instruction_s.instr_operand_implicit", "Vale.X64.Machine_Semantics_s.instr_eval_operand_implicit", "Vale.Transformers.InstructionReorder.equiv_states", "Prims.squash", "Prims.eq2", "Vale.X64.Instruction_s.instr_ret_t", "Vale.X64.Machine_Semantics_s.instr_apply_eval_args", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_instr_apply_eval_args_equiv_states (outs: list instr_out) (args: list instr_operand) (f: instr_args_t outs args) (oprs: instr_operands_t_args args) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ((instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2)) ) =
match args with | [] -> () | i :: args -> let v, oprs:option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_disjoint_location_from_locations_append
val lemma_disjoint_location_from_locations_append (a: location) (as1 as2: list location) : Lemma ((!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2))))
val lemma_disjoint_location_from_locations_append (a: location) (as1 as2: list location) : Lemma ((!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2))))
let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 58, "end_line": 584, "start_col": 0, "start_line": 575 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Vale.Transformers.Locations.location -> as1: Prims.list Vale.Transformers.Locations.location -> as2: Prims.list Vale.Transformers.Locations.location -> FStar.Pervasives.Lemma (ensures !!(Vale.Transformers.Locations.disjoint_location_from_locations a as1) /\ !!(Vale.Transformers.Locations.disjoint_location_from_locations a as2) <==> !!(Vale.Transformers.Locations.disjoint_location_from_locations a (as1 @ as2)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Transformers.Locations.location", "Prims.list", "Vale.Transformers.InstructionReorder.lemma_disjoint_location_from_locations_append", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.l_iff", "Prims.l_and", "Prims.b2t", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.Locations.disjoint_location_from_locations", "FStar.List.Tot.Base.append", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_disjoint_location_from_locations_append (a: location) (as1 as2: list location) : Lemma ((!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) =
match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_eval_instr_equiv_states
val lemma_eval_instr_equiv_states (it: instr_t_record) (oprs: instr_operands_t it.outs it.args) (ann: instr_annotation it) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures (equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2)))
val lemma_eval_instr_equiv_states (it: instr_t_record) (oprs: instr_operands_t it.outs it.args) (ann: instr_annotation it) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures (equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2)))
let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 80, "end_line": 357, "start_col": 0, "start_line": 325 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 )
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
it: Vale.X64.Instruction_s.instr_t_record -> oprs: Vale.X64.Instruction_s.instr_operands_t (InstrTypeRecord?.outs it) (InstrTypeRecord?.args it) -> ann: Vale.X64.Machine_Semantics_s.instr_annotation it -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.InstructionReorder.equiv_states s1 s2) (ensures Vale.Transformers.InstructionReorder.equiv_ostates (Vale.X64.Machine_Semantics_s.eval_instr it oprs ann s1) (Vale.X64.Machine_Semantics_s.eval_instr it oprs ann s2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Instruction_s.instr_t_record", "Vale.X64.Instruction_s.instr_operands_t", "Vale.X64.Instruction_s.__proj__InstrTypeRecord__item__outs", "Vale.X64.Instruction_s.__proj__InstrTypeRecord__item__args", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.X64.Machine_Semantics_s.machine_state", "Prims.list", "Vale.X64.Instruction_s.instr_out", "Vale.X64.Instruction_s.instr_operand", "Vale.X64.Instruction_s.flag_havoc", "Vale.X64.Instruction_s.instr_t", "Vale.X64.Instruction_s.instr_ret_t", "Vale.Transformers.InstructionReorder.lemma_instr_write_outputs_equiv_states", "Prims.unit", "FStar.Pervasives.Native.option", "FStar.Option.mapTot", "Vale.X64.Machine_Semantics_s.instr_write_outputs", "Prims._assert", "Vale.Transformers.InstructionReorder.equiv_states", "Prims.eq2", "Vale.X64.Machine_Semantics_s.flag_val_t", "Vale.X64.Machine_Semantics_s.cf", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_flags", "Vale.X64.Machine_Semantics_s.overflow", "Vale.X64.Machine_Semantics_s.Mkmachine_state", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_regs", "Vale.X64.Machine_Semantics_s.havoc_flags", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_heap", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stack", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stackTaint", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_trace", "Vale.Transformers.InstructionReorder.lemma_instr_apply_eval_inouts_equiv_states", "Vale.X64.Instruction_s.instr_eval", "Vale.X64.Machine_Semantics_s.instr_apply_eval", "Prims.squash", "Vale.Transformers.InstructionReorder.equiv_ostates", "Vale.X64.Machine_Semantics_s.eval_instr", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let lemma_eval_instr_equiv_states (it: instr_t_record) (oprs: instr_operands_t it.outs it.args) (ann: instr_annotation it) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures (equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) =
let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> { s1 with ms_flags = havoc_flags } | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> { s2 with ms_flags = havoc_flags } | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.value_of_const_loc
val value_of_const_loc (lv: locations_with_values) (l: location_eq{L.mem l (locations_of_locations_with_values lv)}) : location_val_eqt l
val value_of_const_loc (lv: locations_with_values) (l: location_eq{L.mem l (locations_of_locations_with_values lv)}) : location_val_eqt l
let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 56, "end_line": 656, "start_col": 0, "start_line": 652 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
lv: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> l: Vale.Transformers.Locations.location_eq { FStar.List.Tot.Base.mem l (Vale.Transformers.InstructionReorder.locations_of_locations_with_values lv) } -> Vale.Transformers.Locations.location_val_eqt l
Prims.Tot
[ "total" ]
[]
[ "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "Vale.Transformers.Locations.location_eq", "Prims.b2t", "FStar.List.Tot.Base.mem", "Vale.Transformers.Locations.location", "Vale.Transformers.InstructionReorder.locations_of_locations_with_values", "Vale.Transformers.BoundedInstructionEffects.location_with_value", "Prims.list", "Prims.op_Equality", "FStar.Pervasives.dfst", "Vale.Transformers.Locations.location_val_eqt", "FStar.Pervasives.dsnd", "Prims.bool", "Vale.Transformers.InstructionReorder.value_of_const_loc" ]
[ "recursion" ]
false
false
false
false
false
let rec value_of_const_loc (lv: locations_with_values) (l: location_eq{L.mem l (locations_of_locations_with_values lv)}) : location_val_eqt l =
let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_disjoint_location_from_locations_mem
val lemma_disjoint_location_from_locations_mem (a1 a2: list location) (a: location) : Lemma (requires ((L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures (!!(disjoint_location_from_locations a a2)))
val lemma_disjoint_location_from_locations_mem (a1 a2: list location) (a: location) : Lemma (requires ((L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures (!!(disjoint_location_from_locations a a2)))
let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 54, "end_line": 622, "start_col": 0, "start_line": 610 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux)
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 2, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a1: Prims.list Vale.Transformers.Locations.location -> a2: Prims.list Vale.Transformers.Locations.location -> a: Vale.Transformers.Locations.location -> FStar.Pervasives.Lemma (requires FStar.List.Tot.Base.mem a a1 /\ !!(Vale.Transformers.Locations.disjoint_locations a1 a2)) (ensures !!(Vale.Transformers.Locations.disjoint_location_from_locations a a2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.list", "Vale.Transformers.Locations.location", "Prims.op_Equality", "Prims.bool", "Vale.Transformers.InstructionReorder.lemma_disjoint_location_from_locations_mem", "Prims.unit", "Prims.l_and", "Prims.b2t", "FStar.List.Tot.Base.mem", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.Locations.disjoint_locations", "Prims.squash", "Vale.Transformers.Locations.disjoint_location_from_locations", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_disjoint_location_from_locations_mem (a1 a2: list location) (a: location) : Lemma (requires ((L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures (!!(disjoint_location_from_locations a a2))) =
match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_unchanged_except_transitive
val lemma_unchanged_except_transitive (a12 a23: list location) (s1 s2 s3: machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3))
val lemma_unchanged_except_transitive (a12 a23: list location) (s1 s2 s3: machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3))
let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux)
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 66, "end_line": 594, "start_col": 0, "start_line": 586 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a12: Prims.list Vale.Transformers.Locations.location -> a23: Prims.list Vale.Transformers.Locations.location -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> s3: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.BoundedInstructionEffects.unchanged_except a12 s1 s2 /\ Vale.Transformers.BoundedInstructionEffects.unchanged_except a23 s2 s3) (ensures Vale.Transformers.BoundedInstructionEffects.unchanged_except (a12 @ a23) s1 s3)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.list", "Vale.Transformers.Locations.location", "Vale.X64.Machine_Semantics_s.machine_state", "FStar.Classical.forall_intro", "Prims.l_imp", "Prims.b2t", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.Locations.disjoint_location_from_locations", "FStar.List.Tot.Base.append", "Prims.eq2", "Vale.Transformers.Locations.location_val_t", "Vale.Transformers.Locations.eval_location", "FStar.Classical.move_requires", "Prims.unit", "Vale.Def.PossiblyMonad.uu___is_Ok", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern", "Vale.Transformers.InstructionReorder.lemma_disjoint_location_from_locations_append", "Prims.l_and", "Vale.Transformers.BoundedInstructionEffects.unchanged_except" ]
[]
false
false
true
false
false
let lemma_unchanged_except_transitive (a12 a23: list location) (s1 s2 s3: machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) =
let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux)
false
Hacl.Test.ECDSA.fst
Hacl.Test.ECDSA.sigver_vector
val sigver_vector : Type0
let sigver_vector = vec8 & vec8 & vec8 & vec8 & vec8 & bool
{ "file_name": "code/tests/Hacl.Test.ECDSA.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 59, "end_line": 76, "start_col": 0, "start_line": 76 }
module Hacl.Test.ECDSA open FStar.HyperStack.ST open Test.Lowstarize open Lib.IntTypes open Hacl.P256 open Spec.ECDSA.Test.Vectors module L = Test.Lowstarize module B = LowStar.Buffer #set-options "--fuel 0 --ifuel 0 --z3rlimit 100" noextract let sigver_vectors256_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_256 noextract let sigver_vectors384_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_384 noextract let sigver_vectors512_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_512 noextract let siggen_vectors256_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_256 noextract let siggen_vectors384_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_384 noextract let siggen_vectors512_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_512 %splice[sigver_vectors256_low] (lowstarize_toplevel "sigver_vectors256_tmp" "sigver_vectors256_low") %splice[sigver_vectors384_low] (lowstarize_toplevel "sigver_vectors384_tmp" "sigver_vectors384_low") %splice[sigver_vectors512_low] (lowstarize_toplevel "sigver_vectors512_tmp" "sigver_vectors512_low") %splice[siggen_vectors256_low] (lowstarize_toplevel "siggen_vectors256_tmp" "siggen_vectors256_low") %splice[siggen_vectors384_low] (lowstarize_toplevel "siggen_vectors384_tmp" "siggen_vectors384_low") %splice[siggen_vectors512_low] (lowstarize_toplevel "siggen_vectors512_tmp" "siggen_vectors512_low") // Cheap alternative to friend Lib.IntTypes needed because Test.Lowstarize uses UInt8.t assume val declassify_uint8: squash (uint8 == UInt8.t) let vec8 = L.lbuffer UInt8.t
{ "checked_file": "/", "dependencies": [ "Test.Lowstarize.fst.checked", "Spec.P256.fst.checked", "Spec.ECDSA.Test.Vectors.fst.checked", "prims.fst.checked", "LowStar.Printf.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "Lib.ByteBuffer.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.P256.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Int32.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "C.String.fsti.checked", "C.Loops.fst.checked", "C.fst.checked" ], "interface_file": false, "source_file": "Hacl.Test.ECDSA.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "Test.Lowstarize", "short_module": "L" }, { "abbrev": false, "full_module": "Spec.ECDSA.Test.Vectors", "short_module": null }, { "abbrev": false, "full_module": "Hacl.P256", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Test.Lowstarize", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
Type0
Prims.Tot
[ "total" ]
[]
[ "FStar.Pervasives.Native.tuple6", "Hacl.Test.ECDSA.vec8", "Prims.bool" ]
[]
false
false
false
true
true
let sigver_vector =
vec8 & vec8 & vec8 & vec8 & vec8 & bool
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_disjoint_implies_unchanged_at
val lemma_disjoint_implies_unchanged_at (reads changes: list location) (s1 s2: machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2))
val lemma_disjoint_implies_unchanged_at (reads changes: list location) (s1 s2: machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2))
let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 56, "end_line": 573, "start_col": 0, "start_line": 565 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
reads: Prims.list Vale.Transformers.Locations.location -> changes: Prims.list Vale.Transformers.Locations.location -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires !!(Vale.Transformers.Locations.disjoint_locations reads changes) /\ Vale.Transformers.BoundedInstructionEffects.unchanged_except changes s1 s2) (ensures Vale.Transformers.BoundedInstructionEffects.unchanged_at reads s1 s2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.list", "Vale.Transformers.Locations.location", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.Transformers.InstructionReorder.lemma_disjoint_implies_unchanged_at", "Prims.unit", "Prims.l_and", "Prims.b2t", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.Locations.disjoint_locations", "Vale.Transformers.BoundedInstructionEffects.unchanged_except", "Prims.squash", "Vale.Transformers.BoundedInstructionEffects.unchanged_at", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_disjoint_implies_unchanged_at (reads changes: list location) (s1 s2: machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) =
match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_disjoint_location_from_locations_mem1
val lemma_disjoint_location_from_locations_mem1 (a: location) (as0: locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0)))
val lemma_disjoint_location_from_locations_mem1 (a: location) (as0: locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0)))
let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 63, "end_line": 650, "start_col": 0, "start_line": 644 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Vale.Transformers.Locations.location -> as0: Vale.Transformers.Locations.locations -> FStar.Pervasives.Lemma (requires Prims.op_Negation (FStar.List.Tot.Base.mem a as0)) (ensures !!(Vale.Transformers.Locations.disjoint_location_from_locations a as0))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Transformers.Locations.location", "Vale.Transformers.Locations.locations", "Prims.list", "Vale.Transformers.InstructionReorder.lemma_disjoint_location_from_locations_mem1", "Prims.unit", "Prims.b2t", "Prims.op_Negation", "FStar.List.Tot.Base.mem", "Prims.squash", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.Locations.disjoint_location_from_locations", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_disjoint_location_from_locations_mem1 (a: location) (as0: locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) =
match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_write_same_constants_append
val lemma_write_same_constants_append (c1 c1' c2: locations_with_values) : Lemma (ensures (!!(write_same_constants (c1 `L.append` c1') c2) = (!!(write_same_constants c1 c2) && !!(write_same_constants c1' c2))))
val lemma_write_same_constants_append (c1 c1' c2: locations_with_values) : Lemma (ensures (!!(write_same_constants (c1 `L.append` c1') c2) = (!!(write_same_constants c1 c2) && !!(write_same_constants c1' c2))))
let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 58, "end_line": 666, "start_col": 0, "start_line": 658 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c1: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> c1': Vale.Transformers.BoundedInstructionEffects.locations_with_values -> c2: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> FStar.Pervasives.Lemma (ensures !!(Vale.Transformers.InstructionReorder.write_same_constants (c1 @ c1') c2) = (!!(Vale.Transformers.InstructionReorder.write_same_constants c1 c2) && !!(Vale.Transformers.InstructionReorder.write_same_constants c1' c2)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "Vale.Transformers.BoundedInstructionEffects.location_with_value", "Prims.list", "Vale.Transformers.InstructionReorder.lemma_write_same_constants_append", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.b2t", "Prims.op_Equality", "Prims.bool", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.InstructionReorder.write_same_constants", "FStar.List.Tot.Base.append", "Prims.op_AmpAmp", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_write_same_constants_append (c1 c1' c2: locations_with_values) : Lemma (ensures (!!(write_same_constants (c1 `L.append` c1') c2) = (!!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) =
match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_mem_not_disjoint
val lemma_mem_not_disjoint (a: location) (as1 as2: list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ((not !!(disjoint_locations as1 as2))))
val lemma_mem_not_disjoint (a: location) (as1 as2: list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ((not !!(disjoint_locations as1 as2))))
let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 836, "start_col": 0, "start_line": 816 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok []
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 2, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Vale.Transformers.Locations.location -> as1: Prims.list Vale.Transformers.Locations.location -> as2: Prims.list Vale.Transformers.Locations.location -> FStar.Pervasives.Lemma (requires FStar.List.Tot.Base.mem a as1 /\ FStar.List.Tot.Base.mem a as2) (ensures Prims.op_Negation !!(Vale.Transformers.Locations.disjoint_locations as1 as2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Transformers.Locations.location", "Prims.list", "FStar.Pervasives.Native.Mktuple2", "Prims.op_Equality", "Prims.bool", "Vale.Transformers.InstructionReorder.lemma_mem_not_disjoint", "Prims.unit", "Vale.Transformers.Locations.lemma_disjoint_locations_symmetric", "Prims.l_and", "Prims.b2t", "FStar.List.Tot.Base.mem", "Prims.squash", "Prims.op_Negation", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.Locations.disjoint_locations", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_mem_not_disjoint (a: location) (as1 as2: list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ((not !!(disjoint_locations as1 as2)))) =
match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else (lemma_mem_not_disjoint a as1 ys) | x :: xs, y :: ys -> if a = x then (if a = y then () else (lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys)) else (lemma_mem_not_disjoint a xs as2)
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_unchanged_at_mem
val lemma_unchanged_at_mem (as0: list location) (a: location) (s1 s2: machine_state) : Lemma (requires ((unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ((eval_location a s1 == eval_location a s2)))
val lemma_unchanged_at_mem (as0: list location) (a: location) (s1 s2: machine_state) : Lemma (requires ((unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ((eval_location a s1 == eval_location a s2)))
let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 37, "end_line": 708, "start_col": 0, "start_line": 697 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 2, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
as0: Prims.list Vale.Transformers.Locations.location -> a: Vale.Transformers.Locations.location -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.BoundedInstructionEffects.unchanged_at as0 s1 s2 /\ FStar.List.Tot.Base.mem a as0) (ensures Vale.Transformers.Locations.eval_location a s1 == Vale.Transformers.Locations.eval_location a s2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.list", "Vale.Transformers.Locations.location", "Vale.X64.Machine_Semantics_s.machine_state", "Prims.op_Equality", "Prims.bool", "Vale.Transformers.InstructionReorder.lemma_unchanged_at_mem", "Prims.unit", "Prims.l_and", "Vale.Transformers.BoundedInstructionEffects.unchanged_at", "Prims.b2t", "FStar.List.Tot.Base.mem", "Prims.squash", "Prims.eq2", "Vale.Transformers.Locations.location_val_t", "Vale.Transformers.Locations.eval_location", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_unchanged_at_mem (as0: list location) (a: location) (s1 s2: machine_state) : Lemma (requires ((unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ((eval_location a s1 == eval_location a s2))) =
match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_constant_on_execution_mem
val lemma_constant_on_execution_mem (locv: locations_with_values) (f: st unit) (s: machine_state) (l: location_eq) (v: location_val_eqt l) : Lemma (requires ((constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv)) ) (ensures ((eval_location l (run f s) == raise_location_val_eqt v)))
val lemma_constant_on_execution_mem (locv: locations_with_values) (f: st unit) (s: machine_state) (l: location_eq) (v: location_val_eqt l) : Lemma (requires ((constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv)) ) (ensures ((eval_location l (run f s) == raise_location_val_eqt v)))
let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 641, "start_col": 0, "start_line": 626 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 2, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
locv: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> f: Vale.X64.Machine_Semantics_s.st Prims.unit -> s: Vale.X64.Machine_Semantics_s.machine_state -> l: Vale.Transformers.Locations.location_eq -> v: Vale.Transformers.Locations.location_val_eqt l -> FStar.Pervasives.Lemma (requires Vale.Transformers.BoundedInstructionEffects.constant_on_execution locv f s /\ Mkmachine_state?.ms_ok (Vale.X64.Machine_Semantics_s.run f s) /\ FStar.List.Tot.Base.mem (| l, v |) locv) (ensures Vale.Transformers.Locations.eval_location l (Vale.X64.Machine_Semantics_s.run f s) == Vale.Transformers.Locations.raise_location_val_eqt v)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "Vale.X64.Machine_Semantics_s.st", "Prims.unit", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.Transformers.Locations.location_eq", "Vale.Transformers.Locations.location_val_eqt", "Vale.Transformers.BoundedInstructionEffects.location_with_value", "Prims.list", "Prims.op_Equality", "Prims.dtuple2", "Prims.Mkdtuple2", "Prims.bool", "Vale.Transformers.InstructionReorder.lemma_constant_on_execution_mem", "Prims.l_and", "Vale.Transformers.BoundedInstructionEffects.constant_on_execution", "Prims.b2t", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Vale.X64.Machine_Semantics_s.run", "FStar.List.Tot.Base.mem", "Prims.squash", "Prims.eq2", "Vale.Transformers.Locations.location_val_t", "Vale.Transformers.Locations.eval_location", "Vale.Transformers.Locations.raise_location_val_eqt", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_constant_on_execution_mem (locv: locations_with_values) (f: st unit) (s: machine_state) (l: location_eq) (v: location_val_eqt l) : Lemma (requires ((constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv)) ) (ensures ((eval_location l (run f s) == raise_location_val_eqt v))) =
match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else (lemma_constant_on_execution_mem xs f s l v)
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_equiv_states_when_except_none
val lemma_equiv_states_when_except_none (s1 s2: machine_state) (ok: bool) : Lemma (requires ((unchanged_except [] s1 s2))) (ensures ((equiv_states ({ s1 with ms_ok = ok }) ({ s2 with ms_ok = ok }))))
val lemma_equiv_states_when_except_none (s1 s2: machine_state) (ok: bool) : Lemma (requires ((unchanged_except [] s1 s2))) (ensures ((equiv_states ({ s1 with ms_ok = ok }) ({ s2 with ms_ok = ok }))))
let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok []
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 50, "end_line": 813, "start_col": 0, "start_line": 805 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> ok: Prims.bool -> FStar.Pervasives.Lemma (requires Vale.Transformers.BoundedInstructionEffects.unchanged_except [] s1 s2) (ensures Vale.Transformers.InstructionReorder.equiv_states (Vale.X64.Machine_Semantics_s.Mkmachine_state ok (Mkmachine_state?.ms_regs s1) (Mkmachine_state?.ms_flags s1) (Mkmachine_state?.ms_heap s1) (Mkmachine_state?.ms_stack s1) (Mkmachine_state?.ms_stackTaint s1) (Mkmachine_state?.ms_trace s1)) (Vale.X64.Machine_Semantics_s.Mkmachine_state ok (Mkmachine_state?.ms_regs s2) (Mkmachine_state?.ms_flags s2) (Mkmachine_state?.ms_heap s2) (Mkmachine_state?.ms_stack s2) (Mkmachine_state?.ms_stackTaint s2) (Mkmachine_state?.ms_trace s2)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.machine_state", "Prims.bool", "Vale.Transformers.Locations.lemma_locations_complete", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_flags", "Prims.Nil", "Vale.X64.Machine_s.observation", "Prims.unit", "FStar.Pervasives.assert_norm", "Prims.eq2", "Vale.X64.Machine_Semantics_s.flag_val_t", "Vale.X64.Machine_Semantics_s.overflow", "Vale.Transformers.Locations.filter_state", "Vale.X64.Machine_Semantics_s.cf", "Vale.Transformers.BoundedInstructionEffects.unchanged_except", "Vale.Transformers.Locations.location", "Prims.squash", "Vale.Transformers.InstructionReorder.equiv_states", "Vale.X64.Machine_Semantics_s.Mkmachine_state", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_regs", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_heap", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stack", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stackTaint", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_trace", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let lemma_equiv_states_when_except_none (s1 s2: machine_state) (ok: bool) : Lemma (requires ((unchanged_except [] s1 s2))) (ensures ((equiv_states ({ s1 with ms_ok = ok }) ({ s2 with ms_ok = ok })))) =
assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); lemma_locations_complete s1 s2 s1.ms_flags ok []
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_unchanged_at_and_except
val lemma_unchanged_at_and_except (as0: list location) (s1 s2: machine_state) : Lemma (requires ((unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ((unchanged_except [] s1 s2)))
val lemma_unchanged_at_and_except (as0: list location) (s1 s2: machine_state) : Lemma (requires ((unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ((unchanged_except [] s1 s2)))
let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 42, "end_line": 803, "start_col": 0, "start_line": 793 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = ()
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
as0: Prims.list Vale.Transformers.Locations.location -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.BoundedInstructionEffects.unchanged_at as0 s1 s2 /\ Vale.Transformers.BoundedInstructionEffects.unchanged_except as0 s1 s2) (ensures Vale.Transformers.BoundedInstructionEffects.unchanged_except [] s1 s2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.list", "Vale.Transformers.Locations.location", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.Transformers.InstructionReorder.lemma_unchanged_at_and_except", "Prims.unit", "Prims.l_and", "Vale.Transformers.BoundedInstructionEffects.unchanged_at", "Vale.Transformers.BoundedInstructionEffects.unchanged_except", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_unchanged_at_and_except (as0: list location) (s1 s2: machine_state) : Lemma (requires ((unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ((unchanged_except [] s1 s2))) =
match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_instr_apply_eval_inouts_equiv_states
val lemma_instr_apply_eval_inouts_equiv_states (outs inouts: list instr_out) (args: list instr_operand) (f: instr_inouts_t outs inouts args) (oprs: instr_operands_t inouts args) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ((instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2)))
val lemma_instr_apply_eval_inouts_equiv_states (outs inouts: list instr_out) (args: list instr_operand) (f: instr_inouts_t outs inouts args) (oprs: instr_operands_t inouts args) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ((instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2)))
let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 82, "end_line": 250, "start_col": 0, "start_line": 221 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 10, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
outs: Prims.list Vale.X64.Instruction_s.instr_out -> inouts: Prims.list Vale.X64.Instruction_s.instr_out -> args: Prims.list Vale.X64.Instruction_s.instr_operand -> f: Vale.X64.Instruction_s.instr_inouts_t outs inouts args -> oprs: Vale.X64.Instruction_s.instr_operands_t inouts args -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.InstructionReorder.equiv_states s1 s2) (ensures Vale.X64.Machine_Semantics_s.instr_apply_eval_inouts outs inouts args f oprs s1 == Vale.X64.Machine_Semantics_s.instr_apply_eval_inouts outs inouts args f oprs s2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.list", "Vale.X64.Instruction_s.instr_out", "Vale.X64.Instruction_s.instr_operand", "Vale.X64.Instruction_s.instr_inouts_t", "Vale.X64.Instruction_s.instr_operands_t", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.Transformers.InstructionReorder.lemma_instr_apply_eval_args_equiv_states", "Vale.Transformers.InstructionReorder.lemma_instr_apply_eval_inouts_equiv_states", "Vale.X64.Instruction_s.coerce", "Vale.X64.Instruction_s.instr_operand_explicit", "FStar.Pervasives.Native.snd", "Vale.X64.Instruction_s.instr_operand_t", "FStar.Pervasives.Native.tuple2", "Vale.X64.Instruction_s.instr_operand_implicit", "FStar.Pervasives.Native.option", "Vale.X64.Instruction_s.instr_val_t", "Prims.unit", "Vale.X64.Instruction_s.arrow", "FStar.Pervasives.Native.Mktuple2", "Vale.X64.Machine_Semantics_s.instr_eval_operand_explicit", "FStar.Pervasives.Native.fst", "Vale.X64.Machine_Semantics_s.instr_eval_operand_implicit", "Vale.Transformers.InstructionReorder.equiv_states", "Prims.squash", "Prims.eq2", "Vale.X64.Instruction_s.instr_ret_t", "Vale.X64.Machine_Semantics_s.instr_apply_eval_inouts", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts: list instr_out) (args: list instr_operand) (f: instr_inouts_t outs inouts args) (oprs: instr_operands_t inouts args) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ((instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) =
match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i) :: inouts -> let v, oprs:option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.wrap_sos
val wrap_sos (f: (machine_state -> option machine_state)) : st unit
val wrap_sos (f: (machine_state -> option machine_state)) : st unit
let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 1037, "start_col": 0, "start_line": 1032 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s)
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f: (_: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Native.option Vale.X64.Machine_Semantics_s.machine_state) -> Vale.X64.Machine_Semantics_s.st Prims.unit
Prims.Tot
[ "total" ]
[]
[ "Vale.X64.Machine_Semantics_s.machine_state", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.Mktuple2", "Prims.unit", "Vale.X64.Machine_Semantics_s.Mkmachine_state", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_regs", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_flags", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_heap", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stack", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stackTaint", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_trace", "FStar.Pervasives.Native.tuple2", "Vale.X64.Machine_Semantics_s.st" ]
[]
false
false
false
true
false
let wrap_sos (f: (machine_state -> option machine_state)) : st unit =
fun s -> (match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s')
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_write_same_constants_mem_both
val lemma_write_same_constants_mem_both (c1 c2: locations_with_values) (l: location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l))
val lemma_write_same_constants_mem_both (c1 c2: locations_with_values) (l: location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l))
let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 685, "start_col": 0, "start_line": 668 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c1: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> c2: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> l: Vale.Transformers.Locations.location_eq -> FStar.Pervasives.Lemma (requires !!(Vale.Transformers.InstructionReorder.write_same_constants c1 c2) /\ FStar.List.Tot.Base.mem l (Vale.Transformers.InstructionReorder.locations_of_locations_with_values c1) /\ FStar.List.Tot.Base.mem l (Vale.Transformers.InstructionReorder.locations_of_locations_with_values c2)) (ensures Vale.Transformers.InstructionReorder.value_of_const_loc c1 l = Vale.Transformers.InstructionReorder.value_of_const_loc c2 l)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "Vale.Transformers.Locations.location_eq", "Vale.Transformers.BoundedInstructionEffects.location_with_value", "Prims.list", "Prims.op_Equality", "FStar.Pervasives.dfst", "Vale.Transformers.Locations.location_val_eqt", "Prims.bool", "Vale.Transformers.InstructionReorder.lemma_write_same_constants_mem_both", "Prims.unit", "Vale.Transformers.InstructionReorder.lemma_write_same_constants_symmetric", "Prims.l_and", "Prims.b2t", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.InstructionReorder.write_same_constants", "FStar.List.Tot.Base.mem", "Vale.Transformers.Locations.location", "Vale.Transformers.InstructionReorder.locations_of_locations_with_values", "Prims.squash", "Vale.Transformers.InstructionReorder.value_of_const_loc", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_write_same_constants_mem_both (c1 c2: locations_with_values) (l: location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) =
let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then (if dfst y = l then () else (lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l)) else (lemma_write_same_constants_mem_both xs c2 l)
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_unchanged_at_combine
val lemma_unchanged_at_combine (a1 a2: locations) (c1 c2: locations_with_values) (sa1 sa2 sb1 sb2: machine_state) : Lemma (requires (!!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ((unchanged_at (a1 `L.append` a2) sb1 sb2)))
val lemma_unchanged_at_combine (a1 a2: locations) (c1 c2: locations_with_values) (sa1 sa2 sb1 sb2: machine_state) : Lemma (requires (!!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ((unchanged_at (a1 `L.append` a2) sb1 sb2)))
let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 17, "end_line": 783, "start_col": 0, "start_line": 711 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a1: Vale.Transformers.Locations.locations -> a2: Vale.Transformers.Locations.locations -> c1: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> c2: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> sa1: Vale.X64.Machine_Semantics_s.machine_state -> sa2: Vale.X64.Machine_Semantics_s.machine_state -> sb1: Vale.X64.Machine_Semantics_s.machine_state -> sb2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires !!(Vale.Transformers.InstructionReorder.write_exchange_allowed a1 a2 c1 c2) /\ Vale.Transformers.BoundedInstructionEffects.unchanged_at (Vale.Transformers.InstructionReorder.locations_of_locations_with_values c1) sb1 sb2 /\ Vale.Transformers.BoundedInstructionEffects.unchanged_at (Vale.Transformers.InstructionReorder.locations_of_locations_with_values c2) sb1 sb2 /\ Vale.Transformers.BoundedInstructionEffects.unchanged_at a1 sa1 sb2 /\ Vale.Transformers.BoundedInstructionEffects.unchanged_except a2 sa1 sb1 /\ Vale.Transformers.BoundedInstructionEffects.unchanged_at a2 sa2 sb1 /\ Vale.Transformers.BoundedInstructionEffects.unchanged_except a1 sa2 sb2) (ensures Vale.Transformers.BoundedInstructionEffects.unchanged_at (a1 @ a2) sb1 sb2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Transformers.Locations.locations", "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "Vale.X64.Machine_Semantics_s.machine_state", "Prims.Nil", "Vale.Transformers.Locations.location", "Prims.list", "Prims.unit", "Prims.l_and", "Prims.eq2", "FStar.List.Tot.Base.append", "Prims.squash", "Vale.Transformers.BoundedInstructionEffects.unchanged_at", "FStar.Pervasives.pattern", "Prims.Cons", "FStar.List.Tot.Properties.append_mem", "FStar.List.Tot.Properties.append_l_cons", "Prims.b2t", "FStar.List.Tot.Base.mem", "Vale.Transformers.Locations.location_val_t", "Vale.Transformers.Locations.eval_location", "Vale.Transformers.InstructionReorder.locations_of_locations_with_values", "Vale.Transformers.InstructionReorder.lemma_unchanged_at_mem", "Prims.bool", "Prims._assert", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.Locations.disjoint_location_from_locations", "Vale.Transformers.InstructionReorder.aux_write_exchange_allowed", "FStar.List.Tot.Properties.mem_memP", "Vale.Def.PossiblyMonad.lemma_for_all_elim", "Vale.Transformers.InstructionReorder.lemma_write_exchange_allowed_symmetric", "Prims.logical", "Vale.Transformers.InstructionReorder.write_exchange_allowed", "Vale.Transformers.BoundedInstructionEffects.unchanged_except" ]
[]
false
false
true
false
false
let lemma_unchanged_at_combine (a1 a2: locations) (c1 c2: locations_with_values) (sa1 sa2 sb1 sb2: machine_state) : Lemma (requires (!!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ((unchanged_at (a1 `L.append` a2) sb1 sb2))) =
let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then (lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2) else (lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then (lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2) else (lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1'';a2'']) = match a1'' with | [] -> (match a2'' with | [] -> () | y :: ys -> (L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys)) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_feq_bounded_effects
val lemma_feq_bounded_effects (rw: rw_set) (f1 f2: st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2))
val lemma_feq_bounded_effects (rw: rw_set) (f1 f2: st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2))
let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 1066, "start_col": 0, "start_line": 1039 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' )
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
rw: Vale.Transformers.BoundedInstructionEffects.rw_set -> f1: Vale.X64.Machine_Semantics_s.st Prims.unit -> f2: Vale.X64.Machine_Semantics_s.st Prims.unit -> FStar.Pervasives.Lemma (requires Vale.Transformers.BoundedInstructionEffects.bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2) (ensures Vale.Transformers.BoundedInstructionEffects.bounded_effects rw f2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Transformers.BoundedInstructionEffects.rw_set", "Vale.X64.Machine_Semantics_s.st", "Prims.unit", "Prims._assert", "Prims.l_Forall", "Vale.X64.Machine_Semantics_s.machine_state", "Prims.l_imp", "Prims.l_and", "Prims.b2t", "Prims.op_Equality", "Prims.bool", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Vale.Transformers.BoundedInstructionEffects.unchanged_at", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_reads", "Vale.X64.Machine_Semantics_s.run", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_writes", "Vale.Transformers.Locations.location", "Prims.eq2", "Vale.Transformers.Locations.location_val_t", "FStar.Universe.raise_t", "Vale.Transformers.Locations.location_val_eqt", "FStar.List.Tot.Base.mem", "Vale.Transformers.BoundedInstructionEffects.location_with_value", "Prims.Mkdtuple2", "Vale.Transformers.Locations.location_eq", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_constant_writes", "Vale.Transformers.BoundedInstructionEffects.constant_on_execution", "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "FStar.FunctionalExtensionality.feq", "FStar.Pervasives.Native.tuple2", "Prims.squash", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.Nil", "Prims.list", "Vale.Transformers.BoundedInstructionEffects.only_affects", "Vale.Transformers.BoundedInstructionEffects.bounded_effects" ]
[]
false
false
true
false
false
let lemma_feq_bounded_effects (rw: rw_set) (f1 f2: st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) =
let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (| l, v |) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (| l, v |) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert (forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ((s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> (((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)))))
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_machine_eval_ins_st_exchange
val lemma_machine_eval_ins_st_exchange (i1 i2: ins) (s: machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2)))
val lemma_machine_eval_ins_st_exchange (i1 i2: ins) (s: machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2)))
let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 75, "end_line": 1102, "start_col": 0, "start_line": 1092 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i))
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
i1: Vale.X64.Machine_Semantics_s.ins -> i2: Vale.X64.Machine_Semantics_s.ins -> s: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires !!(Vale.Transformers.InstructionReorder.ins_exchange_allowed i1 i2)) (ensures Vale.Transformers.InstructionReorder.commutes s (Vale.X64.Machine_Semantics_s.machine_eval_ins_st i1) (Vale.X64.Machine_Semantics_s.machine_eval_ins_st i2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.ins", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.Transformers.InstructionReorder.lemma_commute", "Vale.X64.Machine_Semantics_s.machine_eval_ins_st", "Vale.Transformers.BoundedInstructionEffects.rw_set", "Vale.Transformers.BoundedInstructionEffects.rw_set_of_ins", "Prims.unit", "Vale.Transformers.BoundedInstructionEffects.lemma_machine_eval_ins_st_bounded_effects", "Prims.b2t", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.InstructionReorder.ins_exchange_allowed", "Prims.squash", "Vale.Transformers.InstructionReorder.commutes", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let lemma_machine_eval_ins_st_exchange (i1 i2: ins) (s: machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) =
lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.code_exchange_allowed
val code_exchange_allowed (c1 c2: safely_bounded_code) : pbool
val code_exchange_allowed (c1 c2: safely_bounded_code) : pbool
let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc))
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 101, "end_line": 1451, "start_col": 0, "start_line": 1449 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s.
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c1: Vale.Transformers.InstructionReorder.safely_bounded_code -> c2: Vale.Transformers.InstructionReorder.safely_bounded_code -> Vale.Def.PossiblyMonad.pbool
Prims.Tot
[ "total" ]
[]
[ "Vale.Transformers.InstructionReorder.safely_bounded_code", "Vale.Def.PossiblyMonad.op_Slash_Plus_Greater", "Vale.Transformers.InstructionReorder.rw_exchange_allowed", "Vale.Transformers.InstructionReorder.rw_set_of_code", "Vale.X64.Instruction_s.normal", "Prims.string", "Prims.op_Hat", "FStar.Pervasives.Native.fst", "Prims.int", "Vale.X64.Print_s.print_code", "Vale.X64.Print_s.gcc", "Vale.Def.PossiblyMonad.pbool" ]
[]
false
false
false
true
false
let code_exchange_allowed (c1 c2: safely_bounded_code) : pbool =
rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc))
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_both_not_ok
val lemma_both_not_ok (f1 f2: st unit) (rw1 rw2: rw_set) (s: machine_state) : Lemma (requires ((bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ((run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok))
val lemma_both_not_ok (f1 f2: st unit) (rw1 rw2: rw_set) (s: machine_state) : Lemma (requires ((bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ((run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok))
let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else ()
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 11, "end_line": 864, "start_col": 0, "start_line": 850 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = ()
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f1: Vale.X64.Machine_Semantics_s.st Prims.unit -> f2: Vale.X64.Machine_Semantics_s.st Prims.unit -> rw1: Vale.Transformers.BoundedInstructionEffects.rw_set -> rw2: Vale.Transformers.BoundedInstructionEffects.rw_set -> s: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.BoundedInstructionEffects.bounded_effects rw1 f1 /\ Vale.Transformers.BoundedInstructionEffects.bounded_effects rw2 f2 /\ !!(Vale.Transformers.InstructionReorder.rw_exchange_allowed rw1 rw2)) (ensures Mkmachine_state?.ms_ok (Vale.Transformers.InstructionReorder.run2 f1 f2 s) = Mkmachine_state?.ms_ok (Vale.Transformers.InstructionReorder.run2 f2 f1 s))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.st", "Prims.unit", "Vale.Transformers.BoundedInstructionEffects.rw_set", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Vale.X64.Machine_Semantics_s.run", "Vale.Transformers.InstructionReorder.lemma_disjoint_implies_unchanged_at", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_reads", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_writes", "Prims.bool", "Prims.l_and", "Vale.Transformers.BoundedInstructionEffects.bounded_effects", "Prims.b2t", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.InstructionReorder.rw_exchange_allowed", "Prims.squash", "Prims.op_Equality", "Vale.Transformers.InstructionReorder.run2", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let lemma_both_not_ok (f1 f2: st unit) (rw1 rw2: rw_set) (s: machine_state) : Lemma (requires ((bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ((run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) =
if (run f1 s).ms_ok then (lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s)); if (run f2 s).ms_ok then (lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s))
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_value_of_const_loc_mem
val lemma_value_of_const_loc_mem (c: locations_with_values) (l: location_eq) (v: location_val_eqt l) : Lemma (requires (L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (| l, v |) c))
val lemma_value_of_const_loc_mem (c: locations_with_values) (l: location_eq) (v: location_val_eqt l) : Lemma (requires (L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (| l, v |) c))
let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 64, "end_line": 694, "start_col": 0, "start_line": 687 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l )
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> l: Vale.Transformers.Locations.location_eq -> v: Vale.Transformers.Locations.location_val_eqt l -> FStar.Pervasives.Lemma (requires FStar.List.Tot.Base.mem l (Vale.Transformers.InstructionReorder.locations_of_locations_with_values c) /\ Vale.Transformers.InstructionReorder.value_of_const_loc c l = v) (ensures FStar.List.Tot.Base.mem (| l, v |) c)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "Vale.Transformers.Locations.location_eq", "Vale.Transformers.Locations.location_val_eqt", "Vale.Transformers.BoundedInstructionEffects.location_with_value", "Prims.list", "Prims.op_Equality", "FStar.Pervasives.dfst", "Prims.bool", "Vale.Transformers.InstructionReorder.lemma_value_of_const_loc_mem", "Prims.unit", "Prims.l_and", "Prims.b2t", "FStar.List.Tot.Base.mem", "Vale.Transformers.Locations.location", "Vale.Transformers.InstructionReorder.locations_of_locations_with_values", "Vale.Transformers.InstructionReorder.value_of_const_loc", "Prims.squash", "Prims.Mkdtuple2", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_value_of_const_loc_mem (c: locations_with_values) (l: location_eq) (v: location_val_eqt l) : Lemma (requires (L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (| l, v |) c)) =
let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v
false
Hacl.Test.ECDSA.fst
Hacl.Test.ECDSA.test_many
val test_many : label: C.String.t -> f: (_: a -> FStar.HyperStack.ST.Stack Prims.unit) -> vec: Test.Lowstarize.lbuffer a -> FStar.HyperStack.ST.STATE Prims.unit
let test_many #a (label:C.String.t) (f:a -> Stack unit (fun _ -> True) (fun _ _ _ -> True)) (vec: L.lbuffer a) = C.String.print label; C.String.(print !$"\n"); let L.LB len vs = vec in let f (i:UInt32.t{0 <= v i /\ v i < v len}): Stack unit (requires fun h -> True) (ensures fun h0 _ h1 -> True) = let open LowStar.BufferOps in B.recall vs; LowStar.Printf.(printf "ECDSA Test %ul/%ul\n" (i +! 1ul) len done); f vs.(i) in C.Loops.for 0ul len (fun _ _ -> True) f
{ "file_name": "code/tests/Hacl.Test.ECDSA.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 41, "end_line": 479, "start_col": 0, "start_line": 464 }
module Hacl.Test.ECDSA open FStar.HyperStack.ST open Test.Lowstarize open Lib.IntTypes open Hacl.P256 open Spec.ECDSA.Test.Vectors module L = Test.Lowstarize module B = LowStar.Buffer #set-options "--fuel 0 --ifuel 0 --z3rlimit 100" noextract let sigver_vectors256_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_256 noextract let sigver_vectors384_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_384 noextract let sigver_vectors512_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_512 noextract let siggen_vectors256_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_256 noextract let siggen_vectors384_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_384 noextract let siggen_vectors512_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_512 %splice[sigver_vectors256_low] (lowstarize_toplevel "sigver_vectors256_tmp" "sigver_vectors256_low") %splice[sigver_vectors384_low] (lowstarize_toplevel "sigver_vectors384_tmp" "sigver_vectors384_low") %splice[sigver_vectors512_low] (lowstarize_toplevel "sigver_vectors512_tmp" "sigver_vectors512_low") %splice[siggen_vectors256_low] (lowstarize_toplevel "siggen_vectors256_tmp" "siggen_vectors256_low") %splice[siggen_vectors384_low] (lowstarize_toplevel "siggen_vectors384_tmp" "siggen_vectors384_low") %splice[siggen_vectors512_low] (lowstarize_toplevel "siggen_vectors512_tmp" "siggen_vectors512_low") // Cheap alternative to friend Lib.IntTypes needed because Test.Lowstarize uses UInt8.t assume val declassify_uint8: squash (uint8 == UInt8.t) let vec8 = L.lbuffer UInt8.t let sigver_vector = vec8 & vec8 & vec8 & vec8 & vec8 & bool let siggen_vector = vec8 & vec8 & vec8 & vec8 & vec8 & vec8 & vec8 // This could replace TestLib.compare_and_print val compare_and_print: b1:B.buffer UInt8.t -> b2:B.buffer UInt8.t -> len:UInt32.t -> Stack bool (requires fun h0 -> B.live h0 b1 /\ B.live h0 b2 /\ B.length b1 == v len /\ B.length b2 == v len) (ensures fun h0 _ h1 -> B.modifies B.loc_none h0 h1) let compare_and_print b1 b2 len = push_frame(); LowStar.Printf.(printf "Expected: %xuy\n" len b1 done); LowStar.Printf.(printf "Computed: %xuy\n" len b2 done); let b = Lib.ByteBuffer.lbytes_eq #len b1 b2 in if b then LowStar.Printf.(printf "PASS\n" done) else LowStar.Printf.(printf "FAIL\n" done); pop_frame(); b let test_sigver256 (vec:sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha2 msg_len msg qxy r s in if result' = result then () else begin LowStar.Printf.(printf "FAIL\n" done); C.exit 1l end; pop_frame() end let test_sigver384 (vec:sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha384 msg_len msg qxy r s in if result' = result then () else begin LowStar.Printf.(printf "FAIL\n" done); C.exit 1l end; pop_frame() end let test_sigver512 (vec:sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha512 msg_len msg qxy r s in if result' = result then () else begin LowStar.Printf.(printf "FAIL\n" done); C.exit 1l end; pop_frame() end #push-options "--fuel 1 --ifuel 1 --z3rlimit 200" val check_bound: b:Lib.Buffer.lbuffer uint8 32ul -> Stack bool (requires fun h -> Lib.Buffer.live h b) (ensures fun h0 r h1 -> h0 == h1 /\ r == ( (Lib.ByteSequence.nat_from_bytes_be (Lib.Buffer.as_seq h0 b) > 0) && (Lib.ByteSequence.nat_from_bytes_be (Lib.Buffer.as_seq h0 b) < Spec.P256.order))) let check_bound b = let open FStar.Mul in let open Lib.ByteSequence in let open Spec.P256 in [@inline_let] let q1 = normalize_term (order % pow2 64) in [@inline_let] let q2 = normalize_term ((order / pow2 64) % pow2 64) in [@inline_let] let q3 = normalize_term ((order / pow2 128) % pow2 64) in [@inline_let] let q4 = normalize_term (((order / pow2 128) / pow2 64) % pow2 64) in assert_norm (pow2 128 * pow2 64 == pow2 192); assert (order == q1 + pow2 64 * q2 + pow2 128 * q3 + pow2 192 * q4); let zero = mk_int #U64 #PUB 0 in let q1 = mk_int #U64 #PUB q1 in let q2 = mk_int #U64 #PUB q2 in let q3 = mk_int #U64 #PUB q3 in let q4 = mk_int #U64 #PUB q4 in let h0 = get () in let x1 = Lib.ByteBuffer.uint_from_bytes_be #U64 (Lib.Buffer.sub b 0ul 8ul) in let x2 = Lib.ByteBuffer.uint_from_bytes_be #U64 (Lib.Buffer.sub b 8ul 8ul) in let x3 = Lib.ByteBuffer.uint_from_bytes_be #U64 (Lib.Buffer.sub b 16ul 8ul) in let x4 = Lib.ByteBuffer.uint_from_bytes_be #U64 (Lib.Buffer.sub b 24ul 8ul) in nat_from_intseq_be_slice_lemma (Lib.Buffer.as_seq h0 b) 8; lemma_reveal_uint_to_bytes_be #U64 (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 0 8); nat_from_intseq_be_slice_lemma (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 8 32) 8; lemma_reveal_uint_to_bytes_be #U64 (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 8 16); nat_from_intseq_be_slice_lemma (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 16 32) 8; lemma_reveal_uint_to_bytes_be #U64 (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 16 24); nat_from_intseq_be_slice_lemma (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 24 32) 8; lemma_reveal_uint_to_bytes_be #U64 (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 24 32); let x1 = Lib.RawIntTypes.u64_to_UInt64 x1 in let x2 = Lib.RawIntTypes.u64_to_UInt64 x2 in let x3 = Lib.RawIntTypes.u64_to_UInt64 x3 in let x4 = Lib.RawIntTypes.u64_to_UInt64 x4 in let r = x1 <. q4 || (x1 =. q4 && (x2 <. q3 || (x2 =. q3 && (x3 <. q2 || (x3 =. q2 && x4 <. q1))))) in let r1 = x1 = zero && x2 = zero && x3 = zero && x4 = zero in r && not r1 #push-options " --ifuel 1 --fuel 1" let test_siggen_256 (vec:siggen_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB d_len d, LB qx_len qx, LB qy_len qy, LB k_len k, LB r_len r, LB s_len s = vec in B.recall msg; B.recall d; B.recall qx; B.recall qy; B.recall k; B.recall r; B.recall s; if not (k_len = 32ul && d_len = 32ul) then C.exit (-1l); let bound_k = check_bound k in let bound_d = check_bound d in // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (bound_k && bound_d && qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let rs = B.alloca (u8 0) 64ul in let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let flag = ecdsa_sign_p256_sha2 rs msg_len msg d k in if flag then begin let okr = compare_and_print (B.sub rs 0ul 32ul) r 32ul in let oks = compare_and_print (B.sub rs 32ul 32ul) s 32ul in if okr && oks then begin let result = ecdsa_verif_p256_sha2 msg_len msg qxy r s in if not result then begin LowStar.Printf.(printf "FAIL: verification\n" done); C.exit 1l end end else begin LowStar.Printf.(printf "FAIL: signing\n" done); C.exit 1l end end else begin LowStar.Printf.(printf "FAIL: signing\n" done); C.exit 1l end; pop_frame() end let test_siggen_384 (vec:siggen_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB d_len d, LB qx_len qx, LB qy_len qy, LB k_len k, LB r_len r, LB s_len s = vec in B.recall msg; B.recall d; B.recall qx; B.recall qy; B.recall k; B.recall r; B.recall s; if not (k_len = 32ul && d_len = 32ul) then C.exit (-1l); let bound_k = check_bound k in let bound_d = check_bound d in // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (bound_k && bound_d && qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let rs = B.alloca (u8 0) 64ul in let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let flag = ecdsa_sign_p256_sha384 rs msg_len msg d k in if flag then begin let okr = compare_and_print (B.sub rs 0ul 32ul) r 32ul in let oks = compare_and_print (B.sub rs 32ul 32ul) s 32ul in if okr && oks then begin let result = ecdsa_verif_p256_sha384 msg_len msg qxy r s in if not result then begin LowStar.Printf.(printf "FAIL: verification\n" done); C.exit 1l end end else begin LowStar.Printf.(printf "FAIL: signing\n" done); C.exit 1l end end else begin LowStar.Printf.(printf "FAIL: signing\n" done); C.exit 1l end; pop_frame() end let test_siggen_512 (vec:siggen_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB d_len d, LB qx_len qx, LB qy_len qy, LB k_len k, LB r_len r, LB s_len s = vec in B.recall msg; B.recall d; B.recall qx; B.recall qy; B.recall k; B.recall r; B.recall s; if not (k_len = 32ul && d_len = 32ul) then C.exit (-1l); let bound_k = check_bound k in let bound_d = check_bound d in // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (bound_k && bound_d && qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let rs = B.alloca (u8 0) 64ul in let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let flag = ecdsa_sign_p256_sha512 rs msg_len msg d k in if flag then begin let okr = compare_and_print (B.sub rs 0ul 32ul) r 32ul in let oks = compare_and_print (B.sub rs 32ul 32ul) s 32ul in if okr && oks then begin let result = ecdsa_verif_p256_sha512 msg_len msg qxy r s in if not result then begin LowStar.Printf.(printf "FAIL: verification\n" done); C.exit 1l end end else begin LowStar.Printf.(printf "FAIL: signing\n" done); C.exit 1l end end else begin LowStar.Printf.(printf "FAIL: signing\n" done); C.exit 1l end; pop_frame() end
{ "checked_file": "/", "dependencies": [ "Test.Lowstarize.fst.checked", "Spec.P256.fst.checked", "Spec.ECDSA.Test.Vectors.fst.checked", "prims.fst.checked", "LowStar.Printf.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "Lib.ByteBuffer.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.P256.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Int32.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "C.String.fsti.checked", "C.Loops.fst.checked", "C.fst.checked" ], "interface_file": false, "source_file": "Hacl.Test.ECDSA.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "Test.Lowstarize", "short_module": "L" }, { "abbrev": false, "full_module": "Spec.ECDSA.Test.Vectors", "short_module": null }, { "abbrev": false, "full_module": "Hacl.P256", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Test.Lowstarize", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 1, "initial_ifuel": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 200, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
label: C.String.t -> f: (_: a -> FStar.HyperStack.ST.Stack Prims.unit) -> vec: Test.Lowstarize.lbuffer a -> FStar.HyperStack.ST.STATE Prims.unit
FStar.HyperStack.ST.STATE
[]
[]
[ "C.String.t", "Prims.unit", "FStar.Monotonic.HyperStack.mem", "Prims.l_True", "Test.Lowstarize.lbuffer", "FStar.UInt32.t", "LowStar.Buffer.buffer", "Prims.l_and", "Prims.eq2", "LowStar.Monotonic.Buffer.len", "LowStar.Buffer.trivial_preorder", "LowStar.Monotonic.Buffer.recallable", "C.Loops.for", "FStar.UInt32.__uint_to_t", "Prims.nat", "Prims.b2t", "Prims.op_LessThanOrEqual", "Lib.IntTypes.v", "Lib.IntTypes.U32", "Lib.IntTypes.PUB", "Prims.op_LessThan", "LowStar.BufferOps.op_Array_Access", "LowStar.Printf.printf", "Lib.IntTypes.op_Plus_Bang", "LowStar.Printf.done", "LowStar.Monotonic.Buffer.recall", "C.String.print", "C.String.op_Bang_Dollar" ]
[]
false
true
false
false
false
let test_many #a (label: C.String.t) (f: (a -> Stack unit (fun _ -> True) (fun _ _ _ -> True))) (vec: L.lbuffer a) =
C.String.print label; (let open C.String in print !$"\n"); let L.LB len vs = vec in let f (i: UInt32.t{0 <= v i /\ v i < v len}) : Stack unit (requires fun h -> True) (ensures fun h0 _ h1 -> True) = let open LowStar.BufferOps in B.recall vs; (let open LowStar.Printf in printf "ECDSA Test %ul/%ul\n" (i +! 1ul) len done); f vs.(i) in C.Loops.for 0ul len (fun _ _ -> True) f
false
Hacl.Test.ECDSA.fst
Hacl.Test.ECDSA.compare_and_print
val compare_and_print: b1:B.buffer UInt8.t -> b2:B.buffer UInt8.t -> len:UInt32.t -> Stack bool (requires fun h0 -> B.live h0 b1 /\ B.live h0 b2 /\ B.length b1 == v len /\ B.length b2 == v len) (ensures fun h0 _ h1 -> B.modifies B.loc_none h0 h1)
val compare_and_print: b1:B.buffer UInt8.t -> b2:B.buffer UInt8.t -> len:UInt32.t -> Stack bool (requires fun h0 -> B.live h0 b1 /\ B.live h0 b2 /\ B.length b1 == v len /\ B.length b2 == v len) (ensures fun h0 _ h1 -> B.modifies B.loc_none h0 h1)
let compare_and_print b1 b2 len = push_frame(); LowStar.Printf.(printf "Expected: %xuy\n" len b1 done); LowStar.Printf.(printf "Computed: %xuy\n" len b2 done); let b = Lib.ByteBuffer.lbytes_eq #len b1 b2 in if b then LowStar.Printf.(printf "PASS\n" done) else LowStar.Printf.(printf "FAIL\n" done); pop_frame(); b
{ "file_name": "code/tests/Hacl.Test.ECDSA.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 95, "start_col": 0, "start_line": 85 }
module Hacl.Test.ECDSA open FStar.HyperStack.ST open Test.Lowstarize open Lib.IntTypes open Hacl.P256 open Spec.ECDSA.Test.Vectors module L = Test.Lowstarize module B = LowStar.Buffer #set-options "--fuel 0 --ifuel 0 --z3rlimit 100" noextract let sigver_vectors256_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_256 noextract let sigver_vectors384_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_384 noextract let sigver_vectors512_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_512 noextract let siggen_vectors256_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_256 noextract let siggen_vectors384_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_384 noextract let siggen_vectors512_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_512 %splice[sigver_vectors256_low] (lowstarize_toplevel "sigver_vectors256_tmp" "sigver_vectors256_low") %splice[sigver_vectors384_low] (lowstarize_toplevel "sigver_vectors384_tmp" "sigver_vectors384_low") %splice[sigver_vectors512_low] (lowstarize_toplevel "sigver_vectors512_tmp" "sigver_vectors512_low") %splice[siggen_vectors256_low] (lowstarize_toplevel "siggen_vectors256_tmp" "siggen_vectors256_low") %splice[siggen_vectors384_low] (lowstarize_toplevel "siggen_vectors384_tmp" "siggen_vectors384_low") %splice[siggen_vectors512_low] (lowstarize_toplevel "siggen_vectors512_tmp" "siggen_vectors512_low") // Cheap alternative to friend Lib.IntTypes needed because Test.Lowstarize uses UInt8.t assume val declassify_uint8: squash (uint8 == UInt8.t) let vec8 = L.lbuffer UInt8.t let sigver_vector = vec8 & vec8 & vec8 & vec8 & vec8 & bool let siggen_vector = vec8 & vec8 & vec8 & vec8 & vec8 & vec8 & vec8 // This could replace TestLib.compare_and_print val compare_and_print: b1:B.buffer UInt8.t -> b2:B.buffer UInt8.t -> len:UInt32.t -> Stack bool (requires fun h0 -> B.live h0 b1 /\ B.live h0 b2 /\ B.length b1 == v len /\ B.length b2 == v len)
{ "checked_file": "/", "dependencies": [ "Test.Lowstarize.fst.checked", "Spec.P256.fst.checked", "Spec.ECDSA.Test.Vectors.fst.checked", "prims.fst.checked", "LowStar.Printf.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "Lib.ByteBuffer.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.P256.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Int32.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "C.String.fsti.checked", "C.Loops.fst.checked", "C.fst.checked" ], "interface_file": false, "source_file": "Hacl.Test.ECDSA.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "Test.Lowstarize", "short_module": "L" }, { "abbrev": false, "full_module": "Spec.ECDSA.Test.Vectors", "short_module": null }, { "abbrev": false, "full_module": "Hacl.P256", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Test.Lowstarize", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b1: LowStar.Buffer.buffer FStar.UInt8.t -> b2: LowStar.Buffer.buffer FStar.UInt8.t -> len: FStar.UInt32.t -> FStar.HyperStack.ST.Stack Prims.bool
FStar.HyperStack.ST.Stack
[]
[]
[ "LowStar.Buffer.buffer", "FStar.UInt8.t", "FStar.UInt32.t", "Prims.bool", "Prims.unit", "FStar.HyperStack.ST.pop_frame", "LowStar.Printf.printf", "LowStar.Printf.done", "Lib.ByteBuffer.lbytes_eq", "LowStar.Buffer.trivial_preorder", "FStar.HyperStack.ST.push_frame" ]
[]
false
true
false
false
false
let compare_and_print b1 b2 len =
push_frame (); (let open LowStar.Printf in printf "Expected: %xuy\n" len b1 done); (let open LowStar.Printf in printf "Computed: %xuy\n" len b2 done); let b = Lib.ByteBuffer.lbytes_eq #len b1 b2 in if b then let open LowStar.Printf in printf "PASS\n" done else (let open LowStar.Printf in printf "FAIL\n" done); pop_frame (); b
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.safely_bounded_code_p
val safely_bounded_code_p (c: code) : bool
val safely_bounded_code_p (c: code) : bool
let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 29, "end_line": 1080, "start_col": 0, "start_line": 1068 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) )
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code -> Prims.bool
Prims.Tot
[ "total" ]
[ "safely_bounded_code_p", "safely_bounded_codes_p" ]
[ "Vale.X64.Machine_Semantics_s.code", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.Transformers.BoundedInstructionEffects.safely_bounded", "Prims.list", "Vale.X64.Machine_s.precode", "Vale.X64.Bytes_Code_s.ocmp", "Vale.Transformers.InstructionReorder.safely_bounded_codes_p", "Prims.bool" ]
[ "mutual recursion" ]
false
false
false
true
false
let rec safely_bounded_code_p (c: code) : bool =
match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false | While c b -> false
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_instruction_exchange'
val lemma_instruction_exchange' (i1 i2: ins) (s1 s2: machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ((let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2')))
val lemma_instruction_exchange' (i1 i2: ins) (s1 s2: machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ((let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2')))
let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2)
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 82, "end_line": 1116, "start_col": 0, "start_line": 1104 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
i1: Vale.X64.Machine_Semantics_s.ins -> i2: Vale.X64.Machine_Semantics_s.ins -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires !!(Vale.Transformers.InstructionReorder.ins_exchange_allowed i1 i2) /\ Vale.Transformers.InstructionReorder.equiv_states s1 s2) (ensures (let _ = Vale.X64.Machine_Semantics_s.machine_eval_ins i2 (Vale.X64.Machine_Semantics_s.machine_eval_ins i1 s1), Vale.X64.Machine_Semantics_s.machine_eval_ins i1 (Vale.X64.Machine_Semantics_s.machine_eval_ins i2 s2) in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s1' s2' = _ in Vale.Transformers.InstructionReorder.equiv_states_or_both_not_ok s1' s2') <: Type0))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.ins", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.Transformers.InstructionReorder.lemma_eval_ins_equiv_states", "Vale.X64.Machine_Semantics_s.machine_eval_ins", "Prims.unit", "Vale.Transformers.InstructionReorder.lemma_machine_eval_ins_st_exchange", "Prims.l_and", "Prims.b2t", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.InstructionReorder.ins_exchange_allowed", "Vale.Transformers.InstructionReorder.equiv_states", "Prims.squash", "Vale.Transformers.InstructionReorder.equiv_states_or_both_not_ok", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let lemma_instruction_exchange' (i1 i2: ins) (s1 s2: machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ((let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) =
lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2)
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_instruction_exchange
val lemma_instruction_exchange (i1 i2: ins) (s1 s2: machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ((let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2')))
val lemma_instruction_exchange (i1 i2: ins) (s1 s2: machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ((let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2')))
let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 41, "end_line": 1134, "start_col": 0, "start_line": 1118 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2)
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
i1: Vale.X64.Machine_Semantics_s.ins -> i2: Vale.X64.Machine_Semantics_s.ins -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires !!(Vale.Transformers.InstructionReorder.ins_exchange_allowed i1 i2) /\ Vale.Transformers.InstructionReorder.equiv_states s1 s2) (ensures (let _ = Vale.X64.Machine_Semantics_s.machine_eval_ins i2 (Vale.Transformers.InstructionReorder.filt_state (Vale.X64.Machine_Semantics_s.machine_eval_ins i1 (Vale.Transformers.InstructionReorder.filt_state s1))), Vale.X64.Machine_Semantics_s.machine_eval_ins i1 (Vale.Transformers.InstructionReorder.filt_state (Vale.X64.Machine_Semantics_s.machine_eval_ins i2 (Vale.Transformers.InstructionReorder.filt_state s2))) in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s1' s2' = _ in Vale.Transformers.InstructionReorder.equiv_states_or_both_not_ok s1' s2') <: Type0))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.ins", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.Transformers.InstructionReorder.lemma_instruction_exchange'", "Prims.unit", "Vale.Transformers.InstructionReorder.lemma_eval_ins_equiv_states", "Vale.X64.Machine_Semantics_s.machine_eval_ins", "Vale.Transformers.InstructionReorder.filt_state", "Prims.l_and", "Prims.b2t", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.InstructionReorder.ins_exchange_allowed", "Vale.Transformers.InstructionReorder.equiv_states", "Prims.squash", "Vale.Transformers.InstructionReorder.equiv_states_or_both_not_ok", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let lemma_instruction_exchange (i1 i2: ins) (s1 s2: machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ((let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) =
lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_machine_eval_ins_bounded_effects
val lemma_machine_eval_ins_bounded_effects (i: safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i))))
val lemma_machine_eval_ins_bounded_effects (i: safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i))))
let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i))
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 100, "end_line": 1090, "start_col": 0, "start_line": 1086 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c})
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
i: Vale.Transformers.InstructionReorder.safely_bounded_ins -> FStar.Pervasives.Lemma (ensures Vale.Transformers.BoundedInstructionEffects.bounded_effects (Vale.Transformers.BoundedInstructionEffects.rw_set_of_ins i) (Vale.Transformers.InstructionReorder.wrap_ss (Vale.X64.Machine_Semantics_s.machine_eval_ins i)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Transformers.InstructionReorder.safely_bounded_ins", "Vale.Transformers.InstructionReorder.lemma_feq_bounded_effects", "Vale.Transformers.BoundedInstructionEffects.rw_set_of_ins", "Vale.X64.Machine_Semantics_s.machine_eval_ins_st", "Vale.Transformers.InstructionReorder.wrap_ss", "Vale.X64.Machine_Semantics_s.machine_eval_ins", "Prims.unit", "Vale.Transformers.BoundedInstructionEffects.lemma_machine_eval_ins_st_bounded_effects", "Prims.l_True", "Prims.squash", "Vale.Transformers.BoundedInstructionEffects.bounded_effects", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
true
false
true
false
false
let lemma_machine_eval_ins_bounded_effects (i: safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) =
lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i))
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_commute
val lemma_commute (f1 f2: st unit) (rw1 rw2: rw_set) (s: machine_state) : Lemma (requires ((bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)))
val lemma_commute (f1 f2: st unit) (rw1 rw2: rw_set) (s: machine_state) : Lemma (requires ((bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)))
let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 1025, "start_col": 0, "start_line": 982 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f1: Vale.X64.Machine_Semantics_s.st Prims.unit -> f2: Vale.X64.Machine_Semantics_s.st Prims.unit -> rw1: Vale.Transformers.BoundedInstructionEffects.rw_set -> rw2: Vale.Transformers.BoundedInstructionEffects.rw_set -> s: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.BoundedInstructionEffects.bounded_effects rw1 f1 /\ Vale.Transformers.BoundedInstructionEffects.bounded_effects rw2 f2 /\ !!(Vale.Transformers.InstructionReorder.rw_exchange_allowed rw1 rw2)) (ensures Vale.Transformers.InstructionReorder.equiv_states_or_both_not_ok (Vale.Transformers.InstructionReorder.run2 f1 f2 s) (Vale.Transformers.InstructionReorder.run2 f2 f1 s))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.st", "Prims.unit", "Vale.Transformers.BoundedInstructionEffects.rw_set", "Vale.X64.Machine_Semantics_s.machine_state", "Prims.op_BarBar", "Prims.op_Negation", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Vale.Transformers.InstructionReorder.lemma_both_not_ok", "Prims.bool", "Prims.list", "Vale.Transformers.Locations.location", "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "Prims._assert", "Vale.Transformers.InstructionReorder.equiv_states", "Vale.Transformers.InstructionReorder.run2", "Vale.Transformers.InstructionReorder.lemma_equiv_states_when_except_none", "Prims.b2t", "Prims.op_Equality", "Vale.Transformers.BoundedInstructionEffects.unchanged_except", "Prims.Nil", "Vale.Transformers.InstructionReorder.lemma_unchanged_at_and_except", "FStar.List.Tot.Base.append", "Vale.Transformers.InstructionReorder.lemma_unchanged_at_combine", "Vale.Transformers.InstructionReorder.lemma_constant_on_execution_stays_constant", "Vale.Transformers.InstructionReorder.lemma_write_exchange_allowed_symmetric", "Vale.Transformers.InstructionReorder.lemma_unchanged_except_same_transitive", "Vale.Transformers.InstructionReorder.lemma_unchanged_except_append_symmetric", "Vale.Transformers.InstructionReorder.lemma_unchanged_except_transitive", "Vale.Transformers.BoundedInstructionEffects.unchanged_at", "Vale.Transformers.InstructionReorder.lemma_disjoint_implies_unchanged_at", "Prims.l_and", "Prims.eq2", "Vale.X64.Machine_Semantics_s.run", "FStar.Pervasives.Native.tuple3", "FStar.Pervasives.Native.Mktuple3", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_reads", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_writes", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_constant_writes", "Vale.Transformers.BoundedInstructionEffects.bounded_effects", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.InstructionReorder.rw_exchange_allowed", "Prims.squash", "Vale.Transformers.InstructionReorder.equiv_states_or_both_not_ok", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let lemma_commute (f1 f2: st unit) (rw1 rw2: rw_set) (s: machine_state) : Lemma (requires ((bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) =
let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then (lemma_both_not_ok f1 f2 rw1 rw2 s) else (let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)))
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.safely_bounded_codes_p
val safely_bounded_codes_p (l: codes) : bool
val safely_bounded_codes_p (l: codes) : bool
let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 29, "end_line": 1080, "start_col": 0, "start_line": 1068 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) )
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
l: Vale.X64.Machine_Semantics_s.codes -> Prims.bool
Prims.Tot
[ "total" ]
[ "safely_bounded_code_p", "safely_bounded_codes_p" ]
[ "Vale.X64.Machine_Semantics_s.codes", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "Prims.op_AmpAmp", "Vale.Transformers.InstructionReorder.safely_bounded_code_p", "Vale.Transformers.InstructionReorder.safely_bounded_codes_p", "Prims.bool" ]
[ "mutual recursion" ]
false
false
false
true
false
let rec safely_bounded_codes_p (l: codes) : bool =
match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_bounded_effects_code_codes_aux3
val lemma_bounded_effects_code_codes_aux3 (c: code) (cs: codes) (rw: rw_set) (fuel: nat) (s1 s2: _) : Lemma (requires (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (let* _ = f1 in f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (let* _ = f1 in f2) s1) .ms_ok = (run f12 s1).ms_ok))
val lemma_bounded_effects_code_codes_aux3 (c: code) (cs: codes) (rw: rw_set) (fuel: nat) (s1 s2: _) : Lemma (requires (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (let* _ = f1 in f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (let* _ = f1 in f2) s1) .ms_ok = (run f12 s1).ms_ok))
let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 59, "end_line": 1355, "start_col": 0, "start_line": 1330 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = ()
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code -> cs: Vale.X64.Machine_Semantics_s.codes -> rw: Vale.Transformers.BoundedInstructionEffects.rw_set -> fuel: Prims.nat -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires (let f1 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_code c fuel) in let f2 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_codes cs fuel) in Vale.Transformers.BoundedInstructionEffects.bounded_effects rw (( let* ) f1 (fun _ -> f2)) /\ Mkmachine_state?.ms_ok s1 = Mkmachine_state?.ms_ok s2 /\ Vale.Transformers.BoundedInstructionEffects.unchanged_at (Mkrw_set?.loc_reads rw) s1 s2)) (ensures (let f1 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_code c fuel) in let f2 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_codes cs fuel) in let f12 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_codes (c :: cs) fuel) in Mkmachine_state?.ms_ok (Vale.X64.Machine_Semantics_s.run f12 s1) = Mkmachine_state?.ms_ok (Vale.X64.Machine_Semantics_s.run f12 s2) /\ Mkmachine_state?.ms_ok (Vale.X64.Machine_Semantics_s.run (( let* ) f1 (fun _ -> f2)) s1) = Mkmachine_state?.ms_ok (Vale.X64.Machine_Semantics_s.run f12 s1)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.code", "Vale.X64.Machine_Semantics_s.codes", "Vale.Transformers.BoundedInstructionEffects.rw_set", "Prims.nat", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.Transformers.InstructionReorder.lemma_unchanged_at_reads_implies_both_ok_equal", "Prims.unit", "Prims._assert", "Prims.eq2", "Prims.bool", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Vale.X64.Machine_Semantics_s.run", "Vale.Transformers.InstructionReorder.lemma_equiv_code_codes", "Vale.Transformers.BoundedInstructionEffects.bounded_effects", "Vale.X64.Machine_Semantics_s.st", "Vale.Transformers.InstructionReorder.wrap_sos", "Vale.X64.Machine_Semantics_s.machine_eval_codes", "Prims.Cons", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.X64.Machine_Semantics_s.op_let_Star", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Prims.l_and", "Prims.b2t", "Prims.op_Equality", "Vale.Transformers.BoundedInstructionEffects.unchanged_at", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_reads", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let lemma_bounded_effects_code_codes_aux3 (c: code) (cs: codes) (rw: rw_set) (fuel: nat) s1 s2 : Lemma (requires (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (let* _ = f1 in f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (let* _ = f1 in f2) s1) .ms_ok = (run f12 s1).ms_ok)) =
let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (let* _ = f1 in f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.eq_ins
val eq_ins (i1 i2: ins) : bool
val eq_ins (i1 i2: ins) : bool
let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 37, "end_line": 1695, "start_col": 0, "start_line": 1694 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *)
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
i1: Vale.X64.Machine_Semantics_s.ins -> i2: Vale.X64.Machine_Semantics_s.ins -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "Vale.X64.Machine_Semantics_s.ins", "Prims.op_Equality", "Prims.string", "Vale.X64.Print_s.print_ins", "Vale.X64.Print_s.gcc", "Prims.bool" ]
[]
false
false
false
true
false
let eq_ins (i1 i2: ins) : bool =
print_ins i1 gcc = print_ins i2 gcc
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_bounded_effects_on_functional_extensionality
val lemma_bounded_effects_on_functional_extensionality (rw: rw_set) (f1 f2: st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2))
val lemma_bounded_effects_on_functional_extensionality (rw: rw_set) (f1 f2: st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2))
let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 36, "end_line": 1238, "start_col": 0, "start_line": 1215 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs)
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
rw: Vale.Transformers.BoundedInstructionEffects.rw_set -> f1: Vale.X64.Machine_Semantics_s.st Prims.unit -> f2: Vale.X64.Machine_Semantics_s.st Prims.unit -> FStar.Pervasives.Lemma (requires FStar.FunctionalExtensionality.feq f1 f2 /\ Vale.Transformers.BoundedInstructionEffects.bounded_effects rw f1) (ensures Vale.Transformers.BoundedInstructionEffects.bounded_effects rw f2)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Transformers.BoundedInstructionEffects.rw_set", "Vale.X64.Machine_Semantics_s.st", "Prims.unit", "FStar.Classical.forall_intro_2", "Vale.X64.Machine_Semantics_s.machine_state", "Prims.l_imp", "Prims.l_and", "Prims.b2t", "Prims.op_Equality", "Prims.bool", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Vale.Transformers.BoundedInstructionEffects.unchanged_at", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_reads", "FStar.Pervasives.Native.snd", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_writes", "Prims.l_True", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern", "FStar.Classical.move_requires", "Vale.X64.Machine_Semantics_s.run", "FStar.Classical.forall_intro", "Vale.Transformers.BoundedInstructionEffects.constant_on_execution", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_constant_writes", "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "Vale.Transformers.Locations.location_eq", "Vale.Transformers.Locations.location_val_eqt", "Prims.list", "Vale.Transformers.BoundedInstructionEffects.location_with_value", "Prims._assert", "Prims.l_iff", "Vale.Transformers.BoundedInstructionEffects.only_affects", "Prims.logical", "FStar.FunctionalExtensionality.feq", "FStar.Pervasives.Native.tuple2", "Vale.Transformers.BoundedInstructionEffects.bounded_effects" ]
[]
false
false
true
false
false
let lemma_bounded_effects_on_functional_extensionality (rw: rw_set) (f1 f2: st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) =
let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (| l , v |) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok) ) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.rw_set_of_code
val rw_set_of_code (c: safely_bounded_code) : rw_set
val rw_set_of_code (c: safely_bounded_code) : rw_set
let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs)
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 26, "end_line": 1213, "start_col": 0, "start_line": 1184 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes].
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.Transformers.InstructionReorder.safely_bounded_code -> Vale.Transformers.BoundedInstructionEffects.rw_set
Prims.Tot
[ "total" ]
[ "rw_set_of_code", "rw_set_of_codes" ]
[ "Vale.Transformers.InstructionReorder.safely_bounded_code", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.Transformers.BoundedInstructionEffects.rw_set_of_ins", "Prims.list", "Vale.X64.Machine_s.precode", "Vale.X64.Bytes_Code_s.ocmp", "Vale.Transformers.InstructionReorder.rw_set_of_codes", "Vale.Transformers.BoundedInstructionEffects.add_r_to_rw_set", "Vale.Transformers.BoundedInstructionEffects.locations_of_ocmp", "Vale.Transformers.BoundedInstructionEffects.rw_set_in_parallel", "Vale.Transformers.InstructionReorder.rw_set_of_code", "Vale.Transformers.BoundedInstructionEffects.Mkrw_set", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_reads", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_writes", "Prims.Nil", "Vale.Transformers.BoundedInstructionEffects.location_with_value", "Vale.Transformers.BoundedInstructionEffects.rw_set" ]
[ "mutual recursion" ]
false
false
false
true
false
let rec rw_set_of_code (c: safely_bounded_code) : rw_set =
match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] }
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_bounded_effects_code_codes_aux1
val lemma_bounded_effects_code_codes_aux1 (c: code) (cs: codes) (rw: rw_set) (fuel: nat) (s a: _) : Lemma (requires (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (let* _ = f1 in f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures (let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s)))
val lemma_bounded_effects_code_codes_aux1 (c: code) (cs: codes) (rw: rw_set) (fuel: nat) (s a: _) : Lemma (requires (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (let* _ = f1 in f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures (let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s)))
let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 58, "end_line": 1296, "start_col": 0, "start_line": 1274 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s )
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code -> cs: Vale.X64.Machine_Semantics_s.codes -> rw: Vale.Transformers.BoundedInstructionEffects.rw_set -> fuel: Prims.nat -> s: Vale.X64.Machine_Semantics_s.machine_state -> a: Vale.Transformers.Locations.location -> FStar.Pervasives.Lemma (requires (let f1 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_code c fuel) in let f2 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_codes cs fuel) in let f12 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_codes (c :: cs) fuel) in Vale.Transformers.BoundedInstructionEffects.bounded_effects rw (( let* ) f1 (fun _ -> f2)) /\ !!(Vale.Transformers.Locations.disjoint_location_from_locations a (Mkrw_set?.loc_writes rw)) /\ Mkmachine_state?.ms_ok (Vale.X64.Machine_Semantics_s.run f12 s))) (ensures (let f12 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_codes (c :: cs) fuel) in Vale.Transformers.Locations.eval_location a s == Vale.Transformers.Locations.eval_location a (Vale.X64.Machine_Semantics_s.run f12 s)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.code", "Vale.X64.Machine_Semantics_s.codes", "Vale.Transformers.BoundedInstructionEffects.rw_set", "Prims.nat", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.Transformers.Locations.location", "Vale.Transformers.InstructionReorder.lemma_only_affects_to_unchanged_except", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_writes", "Prims.unit", "Prims._assert", "Vale.Transformers.InstructionReorder.equiv_states_or_both_not_ok", "Vale.Transformers.InstructionReorder.lemma_equiv_code_codes", "Vale.X64.Machine_Semantics_s.run", "Vale.X64.Machine_Semantics_s.op_let_Star", "Vale.X64.Machine_Semantics_s.st", "Vale.Transformers.InstructionReorder.wrap_sos", "Vale.X64.Machine_Semantics_s.machine_eval_codes", "Prims.Cons", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Prims.l_and", "Vale.Transformers.BoundedInstructionEffects.bounded_effects", "Prims.b2t", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.Locations.disjoint_location_from_locations", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Prims.squash", "Prims.eq2", "Vale.Transformers.Locations.location_val_t", "Vale.Transformers.Locations.eval_location", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let lemma_bounded_effects_code_codes_aux1 (c: code) (cs: codes) (rw: rw_set) (fuel: nat) s a : Lemma (requires (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (let* _ = f1 in f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures (let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) =
let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (let* _ = f1 in f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (let* _ = f1 in f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_not_ok_propagate_codes
val lemma_not_ok_propagate_codes (l: codes) (fuel: nat) (s: machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel;l])
val lemma_not_ok_propagate_codes (l: codes) (fuel: nat) (s: machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel;l])
let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 1178, "start_col": 0, "start_line": 1139 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 2, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
l: Vale.X64.Machine_Semantics_s.codes -> fuel: Prims.nat -> s: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Prims.op_Negation (Mkmachine_state?.ms_ok s)) (ensures Vale.Transformers.InstructionReorder.erroring_option_state (Vale.X64.Machine_Semantics_s.machine_eval_codes l fuel s)) (decreases %[fuel;l])
FStar.Pervasives.Lemma
[ "lemma", "" ]
[ "lemma_not_ok_propagate_code", "lemma_not_ok_propagate_codes", "lemma_not_ok_propagate_while" ]
[ "Vale.X64.Machine_Semantics_s.codes", "Prims.nat", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Vale.Transformers.InstructionReorder.lemma_not_ok_propagate_codes", "Prims.unit", "Vale.Transformers.InstructionReorder.lemma_not_ok_propagate_code", "Prims.b2t", "Prims.op_Negation", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Prims.squash", "Vale.Transformers.InstructionReorder.erroring_option_state", "Vale.X64.Machine_Semantics_s.machine_eval_codes", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "mutual recursion" ]
false
false
true
false
false
let rec lemma_not_ok_propagate_codes (l: codes) (fuel: nat) (s: machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel;l]) =
match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_equiv_code_codes
val lemma_equiv_code_codes (c: code) (cs: codes) (fuel: nat) (s: machine_state) : Lemma (ensures (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (let* _ = f1 in f2) s) (run f12 s)))
val lemma_equiv_code_codes (c: code) (cs: codes) (fuel: nat) (s: machine_state) : Lemma (ensures (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (let* _ = f1 in f2) s) (run f12 s)))
let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 1272, "start_col": 0, "start_line": 1245 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = ()
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code -> cs: Vale.X64.Machine_Semantics_s.codes -> fuel: Prims.nat -> s: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (ensures (let f1 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_code c fuel) in let f2 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_codes cs fuel) in let f12 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_codes (c :: cs) fuel) in Vale.Transformers.InstructionReorder.equiv_states_or_both_not_ok (Vale.X64.Machine_Semantics_s.run (( let* ) f1 (fun _ -> f2)) s) (Vale.X64.Machine_Semantics_s.run f12 s)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.code", "Vale.X64.Machine_Semantics_s.codes", "Prims.nat", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Prims.bool", "Vale.Transformers.InstructionReorder.lemma_not_ok_propagate_codes", "Prims.unit", "Prims.Cons", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.Transformers.InstructionReorder.lemma_not_ok_propagate_code", "Prims._assert", "Prims.eq2", "Vale.X64.Machine_Semantics_s.Mkmachine_state", "Prims.op_AmpAmp", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_regs", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_flags", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_heap", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stack", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stackTaint", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_trace", "Vale.X64.Machine_Semantics_s.run", "Vale.X64.Machine_Semantics_s.op_let_Star", "Vale.X64.Machine_Semantics_s.st", "Vale.Transformers.InstructionReorder.wrap_sos", "Vale.X64.Machine_Semantics_s.machine_eval_codes", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Prims.l_True", "Prims.squash", "Vale.Transformers.InstructionReorder.equiv_states_or_both_not_ok", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let lemma_equiv_code_codes (c: code) (cs: codes) (fuel: nat) (s: machine_state) : Lemma (ensures (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (let* _ = f1 in f2) s) (run f12 s))) =
let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (let* _ = f1 in f2) s in let s12 = run f12 s in assert (s_12 == { s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok }); if s.ms_ok then (if s_1.ms_ok then () else (lemma_not_ok_propagate_codes cs fuel s_1)) else (lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s)
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.rw_set_of_codes
val rw_set_of_codes (c: safely_bounded_codes) : rw_set
val rw_set_of_codes (c: safely_bounded_codes) : rw_set
let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs)
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 26, "end_line": 1213, "start_col": 0, "start_line": 1184 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes].
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.Transformers.InstructionReorder.safely_bounded_codes -> Vale.Transformers.BoundedInstructionEffects.rw_set
Prims.Tot
[ "total" ]
[ "rw_set_of_code", "rw_set_of_codes" ]
[ "Vale.Transformers.InstructionReorder.safely_bounded_codes", "Vale.Transformers.BoundedInstructionEffects.Mkrw_set", "Prims.Nil", "Vale.Transformers.Locations.location", "Vale.Transformers.BoundedInstructionEffects.location_with_value", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "Vale.Transformers.BoundedInstructionEffects.rw_set_in_series", "Vale.Transformers.InstructionReorder.rw_set_of_code", "Vale.Transformers.InstructionReorder.rw_set_of_codes", "Vale.Transformers.BoundedInstructionEffects.rw_set" ]
[ "mutual recursion" ]
false
false
false
true
false
let rec rw_set_of_codes (c: safely_bounded_codes) : rw_set =
match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = [] } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs)
false
Hacl.Test.ECDSA.fst
Hacl.Test.ECDSA.main
val main: Prims.unit -> St C.exit_code
val main: Prims.unit -> St C.exit_code
let main () : St C.exit_code = test_many C.String.(!$"[ECDSA SigVer]") test_sigver256 sigver_vectors256_low; test_many C.String.(!$"[ECDSA SigGen]") test_siggen_256 siggen_vectors256_low; test_many C.String.(!$"[ECDSA SigVer - SHA384]") test_sigver384 sigver_vectors384_low; test_many C.String.(!$"[ECDSA SigGen - SHA384]") test_siggen_384 siggen_vectors384_low; test_many C.String.(!$"[ECDSA SigVer - SHA512]") test_sigver512 sigver_vectors512_low; test_many C.String.(!$"[ECDSA SigGen - SHA512]") test_siggen_512 siggen_vectors512_low; C.EXIT_SUCCESS
{ "file_name": "code/tests/Hacl.Test.ECDSA.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 16, "end_line": 492, "start_col": 0, "start_line": 482 }
module Hacl.Test.ECDSA open FStar.HyperStack.ST open Test.Lowstarize open Lib.IntTypes open Hacl.P256 open Spec.ECDSA.Test.Vectors module L = Test.Lowstarize module B = LowStar.Buffer #set-options "--fuel 0 --ifuel 0 --z3rlimit 100" noextract let sigver_vectors256_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_256 noextract let sigver_vectors384_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_384 noextract let sigver_vectors512_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_512 noextract let siggen_vectors256_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_256 noextract let siggen_vectors384_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_384 noextract let siggen_vectors512_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_512 %splice[sigver_vectors256_low] (lowstarize_toplevel "sigver_vectors256_tmp" "sigver_vectors256_low") %splice[sigver_vectors384_low] (lowstarize_toplevel "sigver_vectors384_tmp" "sigver_vectors384_low") %splice[sigver_vectors512_low] (lowstarize_toplevel "sigver_vectors512_tmp" "sigver_vectors512_low") %splice[siggen_vectors256_low] (lowstarize_toplevel "siggen_vectors256_tmp" "siggen_vectors256_low") %splice[siggen_vectors384_low] (lowstarize_toplevel "siggen_vectors384_tmp" "siggen_vectors384_low") %splice[siggen_vectors512_low] (lowstarize_toplevel "siggen_vectors512_tmp" "siggen_vectors512_low") // Cheap alternative to friend Lib.IntTypes needed because Test.Lowstarize uses UInt8.t assume val declassify_uint8: squash (uint8 == UInt8.t) let vec8 = L.lbuffer UInt8.t let sigver_vector = vec8 & vec8 & vec8 & vec8 & vec8 & bool let siggen_vector = vec8 & vec8 & vec8 & vec8 & vec8 & vec8 & vec8 // This could replace TestLib.compare_and_print val compare_and_print: b1:B.buffer UInt8.t -> b2:B.buffer UInt8.t -> len:UInt32.t -> Stack bool (requires fun h0 -> B.live h0 b1 /\ B.live h0 b2 /\ B.length b1 == v len /\ B.length b2 == v len) (ensures fun h0 _ h1 -> B.modifies B.loc_none h0 h1) let compare_and_print b1 b2 len = push_frame(); LowStar.Printf.(printf "Expected: %xuy\n" len b1 done); LowStar.Printf.(printf "Computed: %xuy\n" len b2 done); let b = Lib.ByteBuffer.lbytes_eq #len b1 b2 in if b then LowStar.Printf.(printf "PASS\n" done) else LowStar.Printf.(printf "FAIL\n" done); pop_frame(); b let test_sigver256 (vec:sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha2 msg_len msg qxy r s in if result' = result then () else begin LowStar.Printf.(printf "FAIL\n" done); C.exit 1l end; pop_frame() end let test_sigver384 (vec:sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha384 msg_len msg qxy r s in if result' = result then () else begin LowStar.Printf.(printf "FAIL\n" done); C.exit 1l end; pop_frame() end let test_sigver512 (vec:sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha512 msg_len msg qxy r s in if result' = result then () else begin LowStar.Printf.(printf "FAIL\n" done); C.exit 1l end; pop_frame() end #push-options "--fuel 1 --ifuel 1 --z3rlimit 200" val check_bound: b:Lib.Buffer.lbuffer uint8 32ul -> Stack bool (requires fun h -> Lib.Buffer.live h b) (ensures fun h0 r h1 -> h0 == h1 /\ r == ( (Lib.ByteSequence.nat_from_bytes_be (Lib.Buffer.as_seq h0 b) > 0) && (Lib.ByteSequence.nat_from_bytes_be (Lib.Buffer.as_seq h0 b) < Spec.P256.order))) let check_bound b = let open FStar.Mul in let open Lib.ByteSequence in let open Spec.P256 in [@inline_let] let q1 = normalize_term (order % pow2 64) in [@inline_let] let q2 = normalize_term ((order / pow2 64) % pow2 64) in [@inline_let] let q3 = normalize_term ((order / pow2 128) % pow2 64) in [@inline_let] let q4 = normalize_term (((order / pow2 128) / pow2 64) % pow2 64) in assert_norm (pow2 128 * pow2 64 == pow2 192); assert (order == q1 + pow2 64 * q2 + pow2 128 * q3 + pow2 192 * q4); let zero = mk_int #U64 #PUB 0 in let q1 = mk_int #U64 #PUB q1 in let q2 = mk_int #U64 #PUB q2 in let q3 = mk_int #U64 #PUB q3 in let q4 = mk_int #U64 #PUB q4 in let h0 = get () in let x1 = Lib.ByteBuffer.uint_from_bytes_be #U64 (Lib.Buffer.sub b 0ul 8ul) in let x2 = Lib.ByteBuffer.uint_from_bytes_be #U64 (Lib.Buffer.sub b 8ul 8ul) in let x3 = Lib.ByteBuffer.uint_from_bytes_be #U64 (Lib.Buffer.sub b 16ul 8ul) in let x4 = Lib.ByteBuffer.uint_from_bytes_be #U64 (Lib.Buffer.sub b 24ul 8ul) in nat_from_intseq_be_slice_lemma (Lib.Buffer.as_seq h0 b) 8; lemma_reveal_uint_to_bytes_be #U64 (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 0 8); nat_from_intseq_be_slice_lemma (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 8 32) 8; lemma_reveal_uint_to_bytes_be #U64 (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 8 16); nat_from_intseq_be_slice_lemma (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 16 32) 8; lemma_reveal_uint_to_bytes_be #U64 (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 16 24); nat_from_intseq_be_slice_lemma (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 24 32) 8; lemma_reveal_uint_to_bytes_be #U64 (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 24 32); let x1 = Lib.RawIntTypes.u64_to_UInt64 x1 in let x2 = Lib.RawIntTypes.u64_to_UInt64 x2 in let x3 = Lib.RawIntTypes.u64_to_UInt64 x3 in let x4 = Lib.RawIntTypes.u64_to_UInt64 x4 in let r = x1 <. q4 || (x1 =. q4 && (x2 <. q3 || (x2 =. q3 && (x3 <. q2 || (x3 =. q2 && x4 <. q1))))) in let r1 = x1 = zero && x2 = zero && x3 = zero && x4 = zero in r && not r1 #push-options " --ifuel 1 --fuel 1" let test_siggen_256 (vec:siggen_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB d_len d, LB qx_len qx, LB qy_len qy, LB k_len k, LB r_len r, LB s_len s = vec in B.recall msg; B.recall d; B.recall qx; B.recall qy; B.recall k; B.recall r; B.recall s; if not (k_len = 32ul && d_len = 32ul) then C.exit (-1l); let bound_k = check_bound k in let bound_d = check_bound d in // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (bound_k && bound_d && qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let rs = B.alloca (u8 0) 64ul in let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let flag = ecdsa_sign_p256_sha2 rs msg_len msg d k in if flag then begin let okr = compare_and_print (B.sub rs 0ul 32ul) r 32ul in let oks = compare_and_print (B.sub rs 32ul 32ul) s 32ul in if okr && oks then begin let result = ecdsa_verif_p256_sha2 msg_len msg qxy r s in if not result then begin LowStar.Printf.(printf "FAIL: verification\n" done); C.exit 1l end end else begin LowStar.Printf.(printf "FAIL: signing\n" done); C.exit 1l end end else begin LowStar.Printf.(printf "FAIL: signing\n" done); C.exit 1l end; pop_frame() end let test_siggen_384 (vec:siggen_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB d_len d, LB qx_len qx, LB qy_len qy, LB k_len k, LB r_len r, LB s_len s = vec in B.recall msg; B.recall d; B.recall qx; B.recall qy; B.recall k; B.recall r; B.recall s; if not (k_len = 32ul && d_len = 32ul) then C.exit (-1l); let bound_k = check_bound k in let bound_d = check_bound d in // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (bound_k && bound_d && qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let rs = B.alloca (u8 0) 64ul in let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let flag = ecdsa_sign_p256_sha384 rs msg_len msg d k in if flag then begin let okr = compare_and_print (B.sub rs 0ul 32ul) r 32ul in let oks = compare_and_print (B.sub rs 32ul 32ul) s 32ul in if okr && oks then begin let result = ecdsa_verif_p256_sha384 msg_len msg qxy r s in if not result then begin LowStar.Printf.(printf "FAIL: verification\n" done); C.exit 1l end end else begin LowStar.Printf.(printf "FAIL: signing\n" done); C.exit 1l end end else begin LowStar.Printf.(printf "FAIL: signing\n" done); C.exit 1l end; pop_frame() end let test_siggen_512 (vec:siggen_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB d_len d, LB qx_len qx, LB qy_len qy, LB k_len k, LB r_len r, LB s_len s = vec in B.recall msg; B.recall d; B.recall qx; B.recall qy; B.recall k; B.recall r; B.recall s; if not (k_len = 32ul && d_len = 32ul) then C.exit (-1l); let bound_k = check_bound k in let bound_d = check_bound d in // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (bound_k && bound_d && qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let rs = B.alloca (u8 0) 64ul in let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let flag = ecdsa_sign_p256_sha512 rs msg_len msg d k in if flag then begin let okr = compare_and_print (B.sub rs 0ul 32ul) r 32ul in let oks = compare_and_print (B.sub rs 32ul 32ul) s 32ul in if okr && oks then begin let result = ecdsa_verif_p256_sha512 msg_len msg qxy r s in if not result then begin LowStar.Printf.(printf "FAIL: verification\n" done); C.exit 1l end end else begin LowStar.Printf.(printf "FAIL: signing\n" done); C.exit 1l end end else begin LowStar.Printf.(printf "FAIL: signing\n" done); C.exit 1l end; pop_frame() end inline_for_extraction noextract let test_many #a (label:C.String.t) (f:a -> Stack unit (fun _ -> True) (fun _ _ _ -> True)) (vec: L.lbuffer a) = C.String.print label; C.String.(print !$"\n"); let L.LB len vs = vec in let f (i:UInt32.t{0 <= v i /\ v i < v len}): Stack unit (requires fun h -> True) (ensures fun h0 _ h1 -> True) = let open LowStar.BufferOps in B.recall vs; LowStar.Printf.(printf "ECDSA Test %ul/%ul\n" (i +! 1ul) len done); f vs.(i) in C.Loops.for 0ul len (fun _ _ -> True) f
{ "checked_file": "/", "dependencies": [ "Test.Lowstarize.fst.checked", "Spec.P256.fst.checked", "Spec.ECDSA.Test.Vectors.fst.checked", "prims.fst.checked", "LowStar.Printf.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "Lib.ByteBuffer.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.P256.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Int32.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "C.String.fsti.checked", "C.Loops.fst.checked", "C.fst.checked" ], "interface_file": false, "source_file": "Hacl.Test.ECDSA.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "Test.Lowstarize", "short_module": "L" }, { "abbrev": false, "full_module": "Spec.ECDSA.Test.Vectors", "short_module": null }, { "abbrev": false, "full_module": "Hacl.P256", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Test.Lowstarize", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 1, "initial_ifuel": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 200, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
_: Prims.unit -> FStar.HyperStack.ST.St C.exit_code
FStar.HyperStack.ST.St
[]
[]
[ "Prims.unit", "C.EXIT_SUCCESS", "C.exit_code", "Hacl.Test.ECDSA.test_many", "FStar.Pervasives.Native.tuple7", "Test.Lowstarize.lbuffer", "FStar.UInt8.t", "C.String.op_Bang_Dollar", "Hacl.Test.ECDSA.test_siggen_512", "Hacl.Test.ECDSA.siggen_vectors512_low", "FStar.Pervasives.Native.tuple6", "Prims.bool", "Hacl.Test.ECDSA.test_sigver512", "Hacl.Test.ECDSA.sigver_vectors512_low", "Hacl.Test.ECDSA.test_siggen_384", "Hacl.Test.ECDSA.siggen_vectors384_low", "Hacl.Test.ECDSA.test_sigver384", "Hacl.Test.ECDSA.sigver_vectors384_low", "Hacl.Test.ECDSA.test_siggen_256", "Hacl.Test.ECDSA.siggen_vectors256_low", "Hacl.Test.ECDSA.test_sigver256", "Hacl.Test.ECDSA.sigver_vectors256_low" ]
[]
false
true
false
false
false
let main () : St C.exit_code =
test_many C.String.(!$"[ECDSA SigVer]") test_sigver256 sigver_vectors256_low; test_many C.String.(!$"[ECDSA SigGen]") test_siggen_256 siggen_vectors256_low; test_many C.String.(!$"[ECDSA SigVer - SHA384]") test_sigver384 sigver_vectors384_low; test_many C.String.(!$"[ECDSA SigGen - SHA384]") test_siggen_384 siggen_vectors384_low; test_many C.String.(!$"[ECDSA SigVer - SHA512]") test_sigver512 sigver_vectors512_low; test_many C.String.(!$"[ECDSA SigGen - SHA512]") test_siggen_512 siggen_vectors512_low; C.EXIT_SUCCESS
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_constant_on_execution_stays_constant
val lemma_constant_on_execution_stays_constant (f1 f2: st unit) (rw1 rw2: rw_set) (s s1 s2: machine_state) : Lemma (requires (s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures (unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2)))
val lemma_constant_on_execution_stays_constant (f1 f2: st unit) (rw1 rw2: rw_set) (s s1 s2: machine_state) : Lemma (requires (s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures (unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2)))
let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 12, "end_line": 979, "start_col": 0, "start_line": 867 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else ()
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 2, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
f1: Vale.X64.Machine_Semantics_s.st Prims.unit -> f2: Vale.X64.Machine_Semantics_s.st Prims.unit -> rw1: Vale.Transformers.BoundedInstructionEffects.rw_set -> rw2: Vale.Transformers.BoundedInstructionEffects.rw_set -> s: Vale.X64.Machine_Semantics_s.machine_state -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Mkmachine_state?.ms_ok s1 /\ Mkmachine_state?.ms_ok s2 /\ Mkmachine_state?.ms_ok (Vale.X64.Machine_Semantics_s.run f1 s1) /\ Mkmachine_state?.ms_ok (Vale.X64.Machine_Semantics_s.run f2 s2) /\ Vale.Transformers.BoundedInstructionEffects.bounded_effects rw1 f1 /\ Vale.Transformers.BoundedInstructionEffects.bounded_effects rw2 f2 /\ s1 == Vale.X64.Machine_Semantics_s.run f2 s /\ s2 == Vale.X64.Machine_Semantics_s.run f1 s /\ !!(Vale.Transformers.InstructionReorder.write_exchange_allowed (Mkrw_set?.loc_writes rw1) (Mkrw_set?.loc_writes rw2) (Mkrw_set?.loc_constant_writes rw1) (Mkrw_set?.loc_constant_writes rw2))) (ensures Vale.Transformers.BoundedInstructionEffects.unchanged_at (Vale.Transformers.InstructionReorder.locations_of_locations_with_values (Mkrw_set?.loc_constant_writes rw1)) (Vale.X64.Machine_Semantics_s.run f1 s1) (Vale.X64.Machine_Semantics_s.run f2 s2) /\ Vale.Transformers.BoundedInstructionEffects.unchanged_at (Vale.Transformers.InstructionReorder.locations_of_locations_with_values (Mkrw_set?.loc_constant_writes rw2)) (Vale.X64.Machine_Semantics_s.run f1 s1) (Vale.X64.Machine_Semantics_s.run f2 s2))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.st", "Prims.unit", "Vale.Transformers.BoundedInstructionEffects.rw_set", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.Transformers.Locations.locations", "Prims.list", "Vale.Transformers.Locations.location", "Vale.Transformers.BoundedInstructionEffects.location_with_value", "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "Prims.Nil", "Prims.l_and", "Prims.eq2", "FStar.List.Tot.Base.append", "Prims.squash", "Vale.Transformers.BoundedInstructionEffects.unchanged_at", "Vale.Transformers.InstructionReorder.locations_of_locations_with_values", "Vale.X64.Machine_Semantics_s.run", "FStar.Pervasives.pattern", "Vale.Transformers.Locations.location_eq", "Vale.Transformers.Locations.location_val_eqt", "Prims.Cons", "FStar.List.Tot.Properties.append_l_cons", "FStar.List.Tot.Base.mem", "Vale.Transformers.InstructionReorder.lemma_constant_on_execution_mem", "Vale.Transformers.InstructionReorder.lemma_value_of_const_loc_mem", "Vale.Transformers.InstructionReorder.lemma_write_same_constants_mem_both", "Vale.Transformers.InstructionReorder.lemma_write_same_constants_append", "Prims._assert", "Prims.b2t", "Prims.op_Equality", "Vale.Transformers.InstructionReorder.value_of_const_loc", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.InstructionReorder.write_same_constants", "Prims.op_Negation", "Vale.Transformers.Locations.disjoint_location_from_locations", "Vale.Transformers.InstructionReorder.lemma_mem_not_disjoint", "Vale.Transformers.InstructionReorder.aux_write_exchange_allowed", "FStar.List.Tot.Properties.mem_memP", "Prims.bool", "Vale.Transformers.Locations.location_val_t", "Vale.Transformers.Locations.eval_location", "Vale.Transformers.Locations.raise_location_val_eqt", "Vale.Transformers.InstructionReorder.lemma_disjoint_location_from_locations_mem1", "Vale.Transformers.BoundedInstructionEffects.unchanged_except", "Vale.Transformers.BoundedInstructionEffects.constant_on_execution", "Vale.Def.PossiblyMonad.lemma_for_all_elim", "Vale.Transformers.InstructionReorder.lemma_write_exchange_allowed_symmetric", "FStar.List.Tot.Properties.append_mem", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_constant_writes", "FStar.Pervasives.Native.tuple3", "FStar.Pervasives.Native.Mktuple3", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_reads", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_writes", "Prims.logical", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Vale.Transformers.BoundedInstructionEffects.bounded_effects", "Vale.Transformers.InstructionReorder.write_exchange_allowed" ]
[]
false
false
true
false
false
let lemma_constant_on_execution_stays_constant (f1 f2: st unit) (rw1 rw2: rw_set) (s s1 s2: machine_state) : Lemma (requires (s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures (unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) =
let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ((precond) /\ lv `L.append` lv' == c1)) (ensures ((unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (| l , v |) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then (L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v) else (assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v)); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ((precond) /\ lv `L.append` lv' == c2)) (ensures ((unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (| l , v |) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then (L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v) else (assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v)); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_bounded_effects_code_codes_aux4
val lemma_bounded_effects_code_codes_aux4 (c: code) (cs: codes) (rw: rw_set) (fuel: nat) (s1 s2: _) : Lemma (requires (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (let* _ = f1 in f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (let* _ = f1 in f2) s1) .ms_ok)) (ensures (let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2)))
val lemma_bounded_effects_code_codes_aux4 (c: code) (cs: codes) (rw: rw_set) (fuel: nat) (s1 s2: _) : Lemma (requires (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (let* _ = f1 in f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (let* _ = f1 in f2) s1) .ms_ok)) (ensures (let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2)))
let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2)
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 33, "end_line": 1381, "start_col": 0, "start_line": 1357 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code -> cs: Vale.X64.Machine_Semantics_s.codes -> rw: Vale.Transformers.BoundedInstructionEffects.rw_set -> fuel: Prims.nat -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires (let f1 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_code c fuel) in let f2 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_codes cs fuel) in Vale.Transformers.BoundedInstructionEffects.bounded_effects rw (( let* ) f1 (fun _ -> f2)) /\ Mkmachine_state?.ms_ok s1 = Mkmachine_state?.ms_ok s2 /\ Vale.Transformers.BoundedInstructionEffects.unchanged_at (Mkrw_set?.loc_reads rw) s1 s2 /\ Mkmachine_state?.ms_ok (Vale.X64.Machine_Semantics_s.run (( let* ) f1 (fun _ -> f2)) s1))) (ensures (let f12 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_codes (c :: cs) fuel) in Vale.Transformers.BoundedInstructionEffects.unchanged_at (Mkrw_set?.loc_writes rw) (Vale.X64.Machine_Semantics_s.run f12 s1) (Vale.X64.Machine_Semantics_s.run f12 s2)))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.code", "Vale.X64.Machine_Semantics_s.codes", "Vale.Transformers.BoundedInstructionEffects.rw_set", "Prims.nat", "Vale.X64.Machine_Semantics_s.machine_state", "Prims._assert", "Prims.eq2", "Vale.X64.Machine_Semantics_s.run", "Prims.unit", "Vale.Transformers.BoundedInstructionEffects.unchanged_at", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_writes", "Prims.b2t", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Vale.Transformers.InstructionReorder.lemma_unchanged_at_reads_implies_both_ok_equal", "Vale.Transformers.InstructionReorder.lemma_equiv_code_codes", "Vale.Transformers.BoundedInstructionEffects.bounded_effects", "Vale.X64.Machine_Semantics_s.st", "Vale.Transformers.InstructionReorder.wrap_sos", "Vale.X64.Machine_Semantics_s.machine_eval_codes", "Prims.Cons", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.X64.Machine_Semantics_s.op_let_Star", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Prims.l_and", "Prims.op_Equality", "Prims.bool", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_reads", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let lemma_bounded_effects_code_codes_aux4 (c: code) (cs: codes) (rw: rw_set) (fuel: nat) s1 s2 : Lemma (requires (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (let* _ = f1 in f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (let* _ = f1 in f2) s1) .ms_ok)) (ensures (let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) =
let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (let* _ = f1 in f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2)
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_not_ok_propagate_code
val lemma_not_ok_propagate_code (c: code) (fuel: nat) (s: machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel;c;1])
val lemma_not_ok_propagate_code (c: code) (fuel: nat) (s: machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel;c;1])
let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 1178, "start_col": 0, "start_line": 1139 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 2, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code -> fuel: Prims.nat -> s: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Prims.op_Negation (Mkmachine_state?.ms_ok s)) (ensures Vale.Transformers.InstructionReorder.erroring_option_state (Vale.X64.Machine_Semantics_s.machine_eval_code c fuel s)) (decreases %[fuel;c;1])
FStar.Pervasives.Lemma
[ "lemma", "" ]
[ "lemma_not_ok_propagate_code", "lemma_not_ok_propagate_codes", "lemma_not_ok_propagate_while" ]
[ "Vale.X64.Machine_Semantics_s.code", "Prims.nat", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "FStar.Pervasives.reveal_opaque", "Vale.X64.Machine_Semantics_s.ins", "FStar.Pervasives.Native.option", "Vale.X64.Machine_Semantics_s.machine_eval_code_ins", "Prims.list", "Vale.X64.Machine_s.precode", "Vale.X64.Bytes_Code_s.ocmp", "Vale.Transformers.InstructionReorder.lemma_not_ok_propagate_codes", "Prims.bool", "Vale.Transformers.InstructionReorder.lemma_not_ok_propagate_code", "Prims.unit", "FStar.Pervasives.Native.tuple2", "Vale.X64.Machine_Semantics_s.machine_eval_ocmp", "Vale.Transformers.InstructionReorder.lemma_not_ok_propagate_while", "Prims.b2t", "Prims.op_Negation", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Prims.squash", "Vale.Transformers.InstructionReorder.erroring_option_state", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "mutual recursion" ]
false
false
true
false
false
let rec lemma_not_ok_propagate_code (c: code) (fuel: nat) (s: machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel;c;1]) =
match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let s', b = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_bounded_effects_code_codes
val lemma_bounded_effects_code_codes (c: code) (cs: codes) (rw: rw_set) (fuel: nat) : Lemma (requires (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (let* _ = f1 in f2)))) (ensures (let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12))
val lemma_bounded_effects_code_codes (c: code) (cs: codes) (rw: rw_set) (fuel: nat) : Lemma (requires (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (let* _ = f1 in f2)))) (ensures (let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12))
let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 36, "end_line": 1406, "start_col": 0, "start_line": 1383 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2)
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code -> cs: Vale.X64.Machine_Semantics_s.codes -> rw: Vale.Transformers.BoundedInstructionEffects.rw_set -> fuel: Prims.nat -> FStar.Pervasives.Lemma (requires (let f1 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_code c fuel) in let f2 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_codes cs fuel) in Vale.Transformers.BoundedInstructionEffects.bounded_effects rw (( let* ) f1 (fun _ -> f2)) )) (ensures (let f12 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_codes (c :: cs) fuel) in Vale.Transformers.BoundedInstructionEffects.bounded_effects rw f12))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.code", "Vale.X64.Machine_Semantics_s.codes", "Vale.Transformers.BoundedInstructionEffects.rw_set", "Prims.nat", "FStar.Classical.forall_intro_2", "Vale.X64.Machine_Semantics_s.machine_state", "Prims.l_imp", "Prims.l_and", "Vale.Transformers.BoundedInstructionEffects.bounded_effects", "Vale.Transformers.InstructionReorder.wrap_sos", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Prims.unit", "Vale.X64.Machine_Semantics_s.machine_eval_codes", "FStar.Pervasives.Native.Mktuple2", "Vale.X64.Machine_Semantics_s.Mkmachine_state", "Prims.op_AmpAmp", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_regs", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_flags", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_heap", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stack", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stackTaint", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_trace", "FStar.Pervasives.Native.tuple2", "Prims.b2t", "Prims.op_Equality", "Prims.bool", "Vale.Transformers.BoundedInstructionEffects.unchanged_at", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_reads", "FStar.Pervasives.Native.snd", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_writes", "Prims.Cons", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.l_True", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern", "FStar.Classical.move_requires", "Vale.X64.Machine_Semantics_s.op_let_Star", "Vale.X64.Machine_Semantics_s.st", "Vale.X64.Machine_Semantics_s.run", "Vale.Transformers.InstructionReorder.lemma_bounded_effects_code_codes_aux4", "Vale.Transformers.InstructionReorder.lemma_bounded_effects_code_codes_aux3", "FStar.Classical.forall_intro", "Vale.Transformers.BoundedInstructionEffects.constant_on_execution", "Vale.Transformers.BoundedInstructionEffects.__proj__Mkrw_set__item__loc_constant_writes", "Vale.Transformers.InstructionReorder.lemma_bounded_effects_code_codes_aux2", "Vale.Transformers.Locations.location", "Vale.Def.PossiblyMonad.uu___is_Ok", "Vale.Transformers.Locations.disjoint_location_from_locations", "Prims.eq2", "Vale.Transformers.Locations.location_val_t", "Vale.Transformers.Locations.eval_location", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.InstructionReorder.lemma_bounded_effects_code_codes_aux1" ]
[]
false
false
true
false
false
let lemma_bounded_effects_code_codes (c: code) (cs: codes) (rw: rw_set) (fuel: nat) : Lemma (requires (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (let* _ = f1 in f2)))) (ensures (let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) =
let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = let* _ = f1 in f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_not_ok_propagate_while
val lemma_not_ok_propagate_while (c: code{While? c}) (fuel: nat) (s: machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel;c;0])
val lemma_not_ok_propagate_while (c: code{While? c}) (fuel: nat) (s: machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel;c;0])
let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 1178, "start_col": 0, "start_line": 1139 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 2, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code{While? c} -> fuel: Prims.nat -> s: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Prims.op_Negation (Mkmachine_state?.ms_ok s)) (ensures Vale.Transformers.InstructionReorder.erroring_option_state (Vale.X64.Machine_Semantics_s.machine_eval_code c fuel s)) (decreases %[fuel;c;0])
FStar.Pervasives.Lemma
[ "lemma", "" ]
[ "lemma_not_ok_propagate_code", "lemma_not_ok_propagate_codes", "lemma_not_ok_propagate_while" ]
[ "Vale.X64.Machine_Semantics_s.code", "Prims.b2t", "Vale.X64.Machine_s.uu___is_While", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.X64.Bytes_Code_s.ocmp", "Prims.nat", "Vale.X64.Machine_Semantics_s.machine_state", "Prims.op_Equality", "Prims.int", "Prims.bool", "Vale.X64.Machine_s.precode", "Prims.op_Negation", "Vale.Transformers.InstructionReorder.lemma_not_ok_propagate_code", "Prims.op_Subtraction", "Prims.unit", "FStar.Pervasives.Native.tuple2", "Vale.X64.Machine_Semantics_s.machine_eval_ocmp", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Prims.squash", "Vale.Transformers.InstructionReorder.erroring_option_state", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "mutual recursion" ]
false
false
true
false
false
let rec lemma_not_ok_propagate_while (c: code{While? c}) (fuel: nat) (s: machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel;c;0]) =
if fuel = 0 then () else (let While cond body = c in let s, b = machine_eval_ocmp s cond in if not b then () else (lemma_not_ok_propagate_code body (fuel - 1) s))
false
Hacl.Test.ECDSA.fst
Hacl.Test.ECDSA.test_sigver256
val test_sigver256 (vec: sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True)
val test_sigver256 (vec: sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True)
let test_sigver256 (vec:sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha2 msg_len msg qxy r s in if result' = result then () else begin LowStar.Printf.(printf "FAIL\n" done); C.exit 1l end; pop_frame() end
{ "file_name": "code/tests/Hacl.Test.ECDSA.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 7, "end_line": 128, "start_col": 0, "start_line": 97 }
module Hacl.Test.ECDSA open FStar.HyperStack.ST open Test.Lowstarize open Lib.IntTypes open Hacl.P256 open Spec.ECDSA.Test.Vectors module L = Test.Lowstarize module B = LowStar.Buffer #set-options "--fuel 0 --ifuel 0 --z3rlimit 100" noextract let sigver_vectors256_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_256 noextract let sigver_vectors384_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_384 noextract let sigver_vectors512_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_512 noextract let siggen_vectors256_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_256 noextract let siggen_vectors384_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_384 noextract let siggen_vectors512_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_512 %splice[sigver_vectors256_low] (lowstarize_toplevel "sigver_vectors256_tmp" "sigver_vectors256_low") %splice[sigver_vectors384_low] (lowstarize_toplevel "sigver_vectors384_tmp" "sigver_vectors384_low") %splice[sigver_vectors512_low] (lowstarize_toplevel "sigver_vectors512_tmp" "sigver_vectors512_low") %splice[siggen_vectors256_low] (lowstarize_toplevel "siggen_vectors256_tmp" "siggen_vectors256_low") %splice[siggen_vectors384_low] (lowstarize_toplevel "siggen_vectors384_tmp" "siggen_vectors384_low") %splice[siggen_vectors512_low] (lowstarize_toplevel "siggen_vectors512_tmp" "siggen_vectors512_low") // Cheap alternative to friend Lib.IntTypes needed because Test.Lowstarize uses UInt8.t assume val declassify_uint8: squash (uint8 == UInt8.t) let vec8 = L.lbuffer UInt8.t let sigver_vector = vec8 & vec8 & vec8 & vec8 & vec8 & bool let siggen_vector = vec8 & vec8 & vec8 & vec8 & vec8 & vec8 & vec8 // This could replace TestLib.compare_and_print val compare_and_print: b1:B.buffer UInt8.t -> b2:B.buffer UInt8.t -> len:UInt32.t -> Stack bool (requires fun h0 -> B.live h0 b1 /\ B.live h0 b2 /\ B.length b1 == v len /\ B.length b2 == v len) (ensures fun h0 _ h1 -> B.modifies B.loc_none h0 h1) let compare_and_print b1 b2 len = push_frame(); LowStar.Printf.(printf "Expected: %xuy\n" len b1 done); LowStar.Printf.(printf "Computed: %xuy\n" len b2 done); let b = Lib.ByteBuffer.lbytes_eq #len b1 b2 in if b then LowStar.Printf.(printf "PASS\n" done) else LowStar.Printf.(printf "FAIL\n" done); pop_frame(); b
{ "checked_file": "/", "dependencies": [ "Test.Lowstarize.fst.checked", "Spec.P256.fst.checked", "Spec.ECDSA.Test.Vectors.fst.checked", "prims.fst.checked", "LowStar.Printf.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "Lib.ByteBuffer.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.P256.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Int32.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "C.String.fsti.checked", "C.Loops.fst.checked", "C.fst.checked" ], "interface_file": false, "source_file": "Hacl.Test.ECDSA.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "Test.Lowstarize", "short_module": "L" }, { "abbrev": false, "full_module": "Spec.ECDSA.Test.Vectors", "short_module": null }, { "abbrev": false, "full_module": "Hacl.P256", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Test.Lowstarize", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
vec: Hacl.Test.ECDSA.sigver_vector -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "Hacl.Test.ECDSA.sigver_vector", "FStar.UInt32.t", "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.l_and", "Prims.eq2", "LowStar.Monotonic.Buffer.len", "LowStar.Buffer.trivial_preorder", "LowStar.Monotonic.Buffer.recallable", "Prims.bool", "Prims.op_Negation", "Prims.op_AmpAmp", "Prims.op_Equality", "FStar.UInt32.__uint_to_t", "C.exit", "FStar.Int32.__int_to_t", "Prims.unit", "FStar.HyperStack.ST.pop_frame", "LowStar.Printf.printf", "LowStar.Printf.done", "Hacl.P256.ecdsa_verif_p256_sha2", "LowStar.Monotonic.Buffer.blit", "LowStar.Monotonic.Buffer.mbuffer", "Prims.nat", "LowStar.Monotonic.Buffer.length", "FStar.UInt32.v", "FStar.UInt32.uint_to_t", "Prims.b2t", "LowStar.Monotonic.Buffer.g_is_null", "LowStar.Buffer.alloca", "Lib.IntTypes.u8", "FStar.HyperStack.ST.push_frame", "LowStar.Monotonic.Buffer.recall", "Prims.int", "FStar.Monotonic.HyperStack.mem", "Prims.l_True" ]
[]
false
true
false
false
false
let test_sigver256 (vec: sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) =
let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else (push_frame (); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha2 msg_len msg qxy r s in if result' = result then () else ((let open LowStar.Printf in printf "FAIL\n" done); C.exit 1l); pop_frame ())
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_bounded_effects_code_codes_aux2
val lemma_bounded_effects_code_codes_aux2 (c: code) (cs: codes) (fuel: nat) (cw s: _) : Lemma (requires (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (let* _ = f1 in f2) s))) (ensures (let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s)))
val lemma_bounded_effects_code_codes_aux2 (c: code) (cs: codes) (fuel: nat) (cw s: _) : Lemma (requires (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (let* _ = f1 in f2) s))) (ensures (let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s)))
let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else ()
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 11, "end_line": 1320, "start_col": 0, "start_line": 1298 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code -> cs: Vale.X64.Machine_Semantics_s.codes -> fuel: Prims.nat -> cw: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> s: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires (let f1 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_code c fuel) in let f2 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_codes cs fuel) in Vale.Transformers.BoundedInstructionEffects.constant_on_execution cw (( let* ) f1 (fun _ -> f2)) s)) (ensures (let f12 = Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_codes (c :: cs) fuel) in Vale.Transformers.BoundedInstructionEffects.constant_on_execution cw f12 s))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.code", "Vale.X64.Machine_Semantics_s.codes", "Prims.nat", "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Vale.X64.Machine_Semantics_s.run", "Vale.Transformers.Locations.location_eq", "Vale.Transformers.Locations.location_val_eqt", "Prims.list", "Vale.Transformers.BoundedInstructionEffects.location_with_value", "Vale.Transformers.InstructionReorder.lemma_bounded_effects_code_codes_aux2", "Prims.unit", "Prims.bool", "Vale.Transformers.InstructionReorder.lemma_equiv_code_codes", "Vale.X64.Machine_Semantics_s.st", "Vale.Transformers.InstructionReorder.wrap_sos", "Vale.X64.Machine_Semantics_s.machine_eval_codes", "Prims.Cons", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.X64.Machine_Semantics_s.op_let_Star", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Vale.Transformers.BoundedInstructionEffects.constant_on_execution", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_bounded_effects_code_codes_aux2 (c: code) (cs: codes) (fuel: nat) cw s : Lemma (requires (let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (let* _ = f1 in f2) s))) (ensures (let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) =
let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (let* _ = f1 in f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then (match cw with | [] -> () | (| l , v |) :: xs -> (lemma_bounded_effects_code_codes_aux2 c cs fuel xs s))
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.num_blocks_in_codes
val num_blocks_in_codes (c: codes) : nat
val num_blocks_in_codes (c: codes) : nat
let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 35, "end_line": 1520, "start_col": 0, "start_line": 1516 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.codes -> Prims.nat
Prims.Tot
[ "total" ]
[]
[ "Vale.X64.Machine_Semantics_s.codes", "Prims.list", "Vale.X64.Machine_s.precode", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.X64.Bytes_Code_s.ocmp", "Vale.X64.Bytes_Code_s.code_t", "Prims.op_Addition", "Vale.Transformers.InstructionReorder.num_blocks_in_codes", "Prims.nat" ]
[ "recursion" ]
false
false
false
true
false
let rec num_blocks_in_codes (c: codes) : nat =
match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_num_blocks_in_codes_append
val lemma_num_blocks_in_codes_append (c1 c2: codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))]
val lemma_num_blocks_in_codes_append (c1 c2: codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))]
let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 53, "end_line": 1528, "start_col": 0, "start_line": 1522 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c1: Vale.X64.Machine_Semantics_s.codes -> c2: Vale.X64.Machine_Semantics_s.codes -> FStar.Pervasives.Lemma (ensures Vale.Transformers.InstructionReorder.num_blocks_in_codes (c1 @ c2) == Vale.Transformers.InstructionReorder.num_blocks_in_codes c1 + Vale.Transformers.InstructionReorder.num_blocks_in_codes c2) [SMTPat (Vale.Transformers.InstructionReorder.num_blocks_in_codes (c1 @ c2))]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.codes", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "Vale.Transformers.InstructionReorder.lemma_num_blocks_in_codes_append", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.eq2", "Prims.int", "Vale.Transformers.InstructionReorder.num_blocks_in_codes", "FStar.List.Tot.Base.append", "Prims.op_Addition", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.nat", "Prims.Nil" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_num_blocks_in_codes_append (c1 c2: codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] =
match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.split3
val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a)
val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a)
let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 9, "end_line": 1572, "start_col": 0, "start_line": 1568 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *)
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
l: Prims.list a -> i: Prims.nat{i < FStar.List.Tot.Base.length l} -> (Prims.list a * a) * Prims.list a
Prims.Tot
[ "total" ]
[]
[ "Prims.list", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "FStar.List.Tot.Base.length", "FStar.Pervasives.Native.Mktuple3", "FStar.Pervasives.Native.tuple3", "Prims.unit", "FStar.List.Tot.Base.lemma_splitAt_snd_length", "FStar.Pervasives.Native.tuple2", "FStar.List.Tot.Base.splitAt" ]
[]
false
false
false
false
false
let split3 #a l i =
let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.bubble_to_top
val bubble_to_top (cs: codes) (i: nat{i < L.length cs}) : possibly (cs': codes { let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 })
val bubble_to_top (cs: codes) (i: nat{i < L.length cs}) : possibly (cs': codes { let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 })
let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 1513, "start_col": 0, "start_line": 1486 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps.
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 2, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Vale.X64.Machine_Semantics_s.codes -> i: Prims.nat{i < FStar.List.Tot.Base.length cs} -> Vale.Def.PossiblyMonad.possibly (cs': Vale.X64.Machine_Semantics_s.codes { let _ = FStar.List.Tot.Base.split3 cs i in (let FStar.Pervasives.Native.Mktuple3 #_ #_ #_ a _ c = _ in cs' == a @ c /\ FStar.List.Tot.Base.length cs' = FStar.List.Tot.Base.length cs - 1) <: Type0 })
Prims.Tot
[ "total" ]
[]
[ "Vale.X64.Machine_Semantics_s.codes", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "FStar.List.Tot.Base.length", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.Def.PossiblyMonad.return", "FStar.List.Tot.Base.split3", "Prims.list", "Prims.l_and", "Prims.eq2", "FStar.List.Tot.Base.append", "Prims.op_Equality", "Prims.int", "Prims.op_Subtraction", "Prims.Nil", "Prims.bool", "Prims.op_Negation", "Vale.Transformers.InstructionReorder.safely_bounded_code_p", "Vale.Def.PossiblyMonad.Err", "Prims.op_Hat", "FStar.Pervasives.Native.fst", "Prims.string", "Vale.X64.Print_s.print_code", "Vale.X64.Print_s.gcc", "Vale.Transformers.InstructionReorder.bubble_to_top", "Vale.Transformers.InstructionReorder.code_exchange_allowed", "Prims.Cons", "Vale.Def.PossiblyMonad.possibly", "FStar.Pervasives.Native.tuple3", "FStar.List.Tot.Base.index" ]
[ "recursion" ]
false
false
false
false
false
let rec bubble_to_top (cs: codes) (i: nat{i < L.length cs}) : possibly (cs': codes { let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) =
match cs with | [_] -> return [] | h :: t -> if i = 0 then (return t) else (let x = L.index cs i in if not (safely_bounded_code_p x) then (Err ("Cannot safely move " ^ fst (print_code x 0 gcc))) else (if not (safely_bounded_code_p h) then (Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc))) else (match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res))))
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.aux_string_of_transformation_hints
val aux_string_of_transformation_hints (ts: transformation_hints) : Tot string (decreases %[ts;0])
val aux_string_of_transformation_hints (ts: transformation_hints) : Tot string (decreases %[ts;0])
let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]"
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 51, "end_line": 1557, "start_col": 0, "start_line": 1538 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ts: Vale.Transformers.InstructionReorder.transformation_hints -> Prims.Tot Prims.string
Prims.Tot
[ "total", "" ]
[ "string_of_transformation_hint", "aux_string_of_transformation_hints", "string_of_transformation_hints" ]
[ "Vale.Transformers.InstructionReorder.transformation_hints", "Vale.Transformers.InstructionReorder.transformation_hint", "Prims.list", "Prims.op_Hat", "Vale.Transformers.InstructionReorder.string_of_transformation_hint", "Vale.Transformers.InstructionReorder.aux_string_of_transformation_hints", "Prims.string" ]
[ "mutual recursion" ]
false
false
false
true
false
let rec aux_string_of_transformation_hints (ts: transformation_hints) : Tot string (decreases %[ts;0]) =
match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_bounded_code
val lemma_bounded_code (c: safely_bounded_code) (fuel: nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c])
val lemma_bounded_code (c: safely_bounded_code) (fuel: nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c])
let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 66, "end_line": 1444, "start_col": 0, "start_line": 1408 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.Transformers.InstructionReorder.safely_bounded_code -> fuel: Prims.nat -> FStar.Pervasives.Lemma (ensures Vale.Transformers.BoundedInstructionEffects.bounded_effects (Vale.Transformers.InstructionReorder.rw_set_of_code c) (Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_code c fuel))) (decreases c)
FStar.Pervasives.Lemma
[ "lemma", "" ]
[ "lemma_bounded_code", "lemma_bounded_codes" ]
[ "Vale.Transformers.InstructionReorder.safely_bounded_code", "Prims.nat", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.Transformers.InstructionReorder.lemma_bounded_effects_on_functional_extensionality", "Vale.Transformers.BoundedInstructionEffects.rw_set_of_ins", "Vale.X64.Machine_Semantics_s.machine_state", "FStar.Pervasives.Native.Mktuple2", "Prims.unit", "FStar.Pervasives.Native.__proj__Some__item__v", "Vale.X64.Machine_Semantics_s.machine_eval_code_ins_def", "FStar.Pervasives.Native.tuple2", "Vale.Transformers.InstructionReorder.wrap_sos", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Vale.Transformers.BoundedInstructionEffects.lemma_machine_eval_code_Ins_bounded_effects", "FStar.Pervasives.reveal_opaque", "Vale.X64.Machine_Semantics_s.ins", "FStar.Pervasives.Native.option", "Vale.X64.Machine_Semantics_s.machine_eval_code_ins", "Prims.list", "Vale.X64.Machine_s.precode", "Vale.X64.Bytes_Code_s.ocmp", "Vale.Transformers.InstructionReorder.rw_set_of_codes", "Vale.X64.Machine_Semantics_s.machine_eval_codes", "Vale.X64.Machine_s.Block", "Vale.Transformers.InstructionReorder.lemma_bounded_codes", "Prims.l_True", "Prims.squash", "Vale.Transformers.BoundedInstructionEffects.bounded_effects", "Vale.Transformers.InstructionReorder.rw_set_of_code", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "mutual recursion" ]
false
false
true
false
false
let rec lemma_bounded_code (c: safely_bounded_code) (fuel: nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) =
match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> ()
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.wrap_diveinat
val wrap_diveinat (p: nat) (l: transformation_hints) : transformation_hints
val wrap_diveinat (p: nat) (l: transformation_hints) : transformation_hints
let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 38, "end_line": 1563, "start_col": 0, "start_line": 1559 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]"
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
p: Prims.nat -> l: Vale.Transformers.InstructionReorder.transformation_hints -> Vale.Transformers.InstructionReorder.transformation_hints
Prims.Tot
[ "total" ]
[]
[ "Prims.nat", "Vale.Transformers.InstructionReorder.transformation_hints", "Prims.Nil", "Vale.Transformers.InstructionReorder.transformation_hint", "Prims.list", "Prims.Cons", "Vale.Transformers.InstructionReorder.DiveInAt", "Vale.Transformers.InstructionReorder.wrap_diveinat" ]
[ "recursion" ]
false
false
false
true
false
let rec wrap_diveinat (p: nat) (l: transformation_hints) : transformation_hints =
match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.string_of_transformation_hints
val string_of_transformation_hints (ts: transformation_hints) : Tot string (decreases %[ts;1])
val string_of_transformation_hints (ts: transformation_hints) : Tot string (decreases %[ts;1])
let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]"
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 51, "end_line": 1557, "start_col": 0, "start_line": 1538 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ts: Vale.Transformers.InstructionReorder.transformation_hints -> Prims.Tot Prims.string
Prims.Tot
[ "total", "" ]
[ "string_of_transformation_hint", "aux_string_of_transformation_hints", "string_of_transformation_hints" ]
[ "Vale.Transformers.InstructionReorder.transformation_hints", "Prims.op_Hat", "Vale.Transformers.InstructionReorder.aux_string_of_transformation_hints", "Prims.string" ]
[ "mutual recursion" ]
false
false
false
true
false
let rec string_of_transformation_hints (ts: transformation_hints) : Tot string (decreases %[ts;1]) =
"[" ^ aux_string_of_transformation_hints ts ^ "]"
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.is_empty_codes
val is_empty_codes (c: codes) : bool
val is_empty_codes (c: codes) : bool
let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 51, "end_line": 1584, "start_col": 0, "start_line": 1574 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.codes -> Prims.bool
Prims.Tot
[ "total" ]
[ "is_empty_code", "is_empty_codes" ]
[ "Vale.X64.Machine_Semantics_s.codes", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "Prims.op_AmpAmp", "Vale.Transformers.InstructionReorder.is_empty_code", "Vale.Transformers.InstructionReorder.is_empty_codes", "Prims.bool" ]
[ "mutual recursion" ]
false
false
false
true
false
let rec is_empty_codes (c: codes) : bool =
match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.fully_unblocked_code
val fully_unblocked_code (c: code) : codes
val fully_unblocked_code (c: code) : codes
let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 62, "end_line": 1724, "start_col": 0, "start_line": 1713 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code -> Vale.X64.Machine_Semantics_s.codes
Prims.Tot
[ "total" ]
[ "fully_unblocked_code", "fully_unblocked_codes" ]
[ "Vale.X64.Machine_Semantics_s.code", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.Cons", "Vale.X64.Bytes_Code_s.code_t", "Prims.Nil", "Prims.list", "Vale.X64.Machine_s.precode", "Vale.X64.Bytes_Code_s.ocmp", "Vale.Transformers.InstructionReorder.fully_unblocked_codes", "Vale.X64.Machine_s.IfElse", "Vale.X64.Machine_s.Block", "Vale.Transformers.InstructionReorder.fully_unblocked_code", "Vale.X64.Machine_s.While", "Vale.X64.Machine_Semantics_s.codes" ]
[ "mutual recursion" ]
false
false
false
true
false
let rec fully_unblocked_code (c: code) : codes =
match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))]
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_metric_for_codes_append
val lemma_metric_for_codes_append (c1 c2: codes) : Lemma (ensures (metric_for_codes (c1 `L.append` c2) == metric_for_codes c1 + metric_for_codes c2)) [SMTPat (metric_for_codes (c1 `L.append` c2))]
val lemma_metric_for_codes_append (c1 c2: codes) : Lemma (ensures (metric_for_codes (c1 `L.append` c2) == metric_for_codes c1 + metric_for_codes c2)) [SMTPat (metric_for_codes (c1 `L.append` c2))]
let rec lemma_metric_for_codes_append (c1 c2:codes) : Lemma (ensures (metric_for_codes (c1 `L.append` c2) == metric_for_codes c1 + metric_for_codes c2)) [SMTPat (metric_for_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_metric_for_codes_append xs c2
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 50, "end_line": 1788, "start_col": 0, "start_line": 1782 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs let increment_hint (th:transformation_hint) : transformation_hint = match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th let rec find_deep_code_transform (c:code) (cs:codes) : possibly transformation_hint = match cs with | [] -> Err ("Not found (during find_deep_code_transform): " ^ fst (print_code c 0 gcc)) | x :: xs -> (* let _ = IO.debug_print_string ( "---------------------------------\n" ^ " c : \n" ^ fst (print_code c 0 gcc) ^ "\n" ^ " x : \n" ^ fst (print_code x 0 gcc) ^ "\n" ^ " xs : \n" ^ fst (print_code (Block xs) 0 gcc) ^ "\n" ^ "---------------------------------\n" ^ "") in *) if is_empty_code x then find_deep_code_transform c xs else ( if eq_codes (fully_unblocked_code x) (fully_unblocked_code c) then ( return (MoveUpFrom 0) ) else ( match x with | Block l -> ( match find_deep_code_transform c l with | Ok t -> return (DiveInAt 0 t) | Err reason -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) | _ -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) ) let rec metric_for_code (c:code) : GTot nat = 1 + ( match c with | Ins _ -> 0 | Block l -> metric_for_codes l | IfElse _ t f -> metric_for_code t + metric_for_code f | While _ b -> metric_for_code b ) and metric_for_codes (c:codes) : GTot nat = match c with | [] -> 0 | x :: xs -> 1 + metric_for_code x + metric_for_codes xs
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c1: Vale.X64.Machine_Semantics_s.codes -> c2: Vale.X64.Machine_Semantics_s.codes -> FStar.Pervasives.Lemma (ensures Vale.Transformers.InstructionReorder.metric_for_codes (c1 @ c2) == Vale.Transformers.InstructionReorder.metric_for_codes c1 + Vale.Transformers.InstructionReorder.metric_for_codes c2) [SMTPat (Vale.Transformers.InstructionReorder.metric_for_codes (c1 @ c2))]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.codes", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "Vale.Transformers.InstructionReorder.lemma_metric_for_codes_append", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.eq2", "Prims.int", "Vale.Transformers.InstructionReorder.metric_for_codes", "FStar.List.Tot.Base.append", "Prims.op_Addition", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.nat", "Prims.Nil" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_metric_for_codes_append (c1 c2: codes) : Lemma (ensures (metric_for_codes (c1 `L.append` c2) == metric_for_codes c1 + metric_for_codes c2)) [SMTPat (metric_for_codes (c1 `L.append` c2))] =
match c1 with | [] -> () | x :: xs -> lemma_metric_for_codes_append xs c2
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_code_exchange_allowed
val lemma_code_exchange_allowed (c1 c2: safely_bounded_code) (fuel: nat) (s: machine_state) : Lemma (requires (!!(code_exchange_allowed c1 c2))) (ensures (equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s)))
val lemma_code_exchange_allowed (c1 c2: safely_bounded_code) (fuel: nat) (s: machine_state) : Lemma (requires (!!(code_exchange_allowed c1 c2))) (ensures (equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s)))
let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 77, "end_line": 1478, "start_col": 0, "start_line": 1454 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc))
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 3, "initial_ifuel": 0, "max_fuel": 3, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c1: Vale.Transformers.InstructionReorder.safely_bounded_code -> c2: Vale.Transformers.InstructionReorder.safely_bounded_code -> fuel: Prims.nat -> s: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires !!(Vale.Transformers.InstructionReorder.code_exchange_allowed c1 c2)) (ensures Vale.Transformers.InstructionReorder.equiv_option_states (Vale.X64.Machine_Semantics_s.machine_eval_codes [c1; c2] fuel s) (Vale.X64.Machine_Semantics_s.machine_eval_codes [c2; c1] fuel s))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.Transformers.InstructionReorder.safely_bounded_code", "Prims.nat", "Vale.X64.Machine_Semantics_s.machine_state", "FStar.Classical.move_requires", "Prims.b2t", "Prims.op_Negation", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Vale.Transformers.InstructionReorder.erroring_option_state", "Vale.X64.Machine_Semantics_s.machine_eval_codes", "Prims.Cons", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.Nil", "Vale.Transformers.InstructionReorder.lemma_not_ok_propagate_codes", "Prims.unit", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Vale.Transformers.InstructionReorder.lemma_not_ok_propagate_code", "FStar.Pervasives.allow_inversion", "FStar.Pervasives.Native.option", "Vale.X64.Machine_Semantics_s.run", "Prims._assert", "Vale.Transformers.InstructionReorder.equiv_states_or_both_not_ok", "Vale.Transformers.InstructionReorder.run2", "Vale.Transformers.InstructionReorder.lemma_commute", "Vale.Transformers.InstructionReorder.rw_set_of_code", "Vale.X64.Machine_Semantics_s.st", "Vale.Transformers.InstructionReorder.wrap_sos", "Vale.Transformers.InstructionReorder.lemma_bounded_code", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.InstructionReorder.code_exchange_allowed", "Prims.squash", "Vale.Transformers.InstructionReorder.equiv_option_states", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let lemma_code_exchange_allowed (c1 c2: safely_bounded_code) (fuel: nat) (s: machine_state) : Lemma (requires (!!(code_exchange_allowed c1 c2))) (ensures (equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) =
lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1; c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2; c1] fuel) s
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.perform_reordering_with_hint
val perform_reordering_with_hint (t: transformation_hint) (c: codes) : possibly codes
val perform_reordering_with_hint (t: transformation_hint) (c: codes) : possibly codes
let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 7, "end_line": 1677, "start_col": 0, "start_line": 1586 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
t: Vale.Transformers.InstructionReorder.transformation_hint -> c: Vale.X64.Machine_Semantics_s.codes -> Vale.Def.PossiblyMonad.possibly Vale.X64.Machine_Semantics_s.codes
Prims.Tot
[ "total" ]
[ "perform_reordering_with_hint", "perform_reordering_with_hints" ]
[ "Vale.Transformers.InstructionReorder.transformation_hint", "Vale.X64.Machine_Semantics_s.codes", "Vale.Def.PossiblyMonad.Err", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "Vale.Transformers.InstructionReorder.is_empty_codes", "Prims.Cons", "Prims.Nil", "Vale.Transformers.InstructionReorder.perform_reordering_with_hint", "Prims.bool", "Prims.nat", "Prims.op_LessThan", "FStar.List.Tot.Base.length", "Vale.Def.PossiblyMonad.op_let_Plus", "FStar.List.Tot.Base.split3", "Prims.l_and", "Prims.eq2", "FStar.List.Tot.Base.append", "Prims.b2t", "Prims.op_Equality", "Prims.int", "Prims.op_Subtraction", "Vale.Transformers.InstructionReorder.bubble_to_top", "Vale.Def.PossiblyMonad.return", "FStar.List.Tot.Base.index", "Vale.Def.PossiblyMonad.possibly", "Prims.op_Hat", "Vale.Transformers.InstructionReorder.string_of_transformation_hint", "Vale.X64.Machine_s.precode", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Bytes_Code_s.ocmp", "Vale.X64.Machine_s.Block", "Prims.unit", "FStar.List.Tot.Properties.append_length", "FStar.Pervasives.Native.fst", "Prims.string", "Vale.X64.Print_s.print_code", "Vale.X64.Print_s.gcc", "FStar.Pervasives.Native.tuple3", "Vale.Transformers.InstructionReorder.split3", "FStar.List.Pure.Properties.lemma_split3_length", "Vale.Transformers.InstructionReorder.perform_reordering_with_hints", "Vale.X64.Machine_s.IfElse", "Vale.X64.Machine_s.While" ]
[ "mutual recursion" ]
false
false
false
true
false
let rec perform_reordering_with_hint (t: transformation_hint) (c: codes) : possibly codes =
match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else (match t with | MoveUpFrom i -> (if i < L.length c then (let+ c' = bubble_to_top c i in return (L.index c i :: c')) else (Err ("invalid hint : " ^ string_of_transformation_hint t))) | DiveInAt i t' -> if i < L.length c then (FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in (match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right)))) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc))) else (Err ("invalid hint : " ^ string_of_transformation_hint t)) | InPlaceIfElse tht thf -> (match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc))) | InPlaceWhile thb -> (match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc))))
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.increment_hint
val increment_hint (th: transformation_hint) : transformation_hint
val increment_hint (th: transformation_hint) : transformation_hint
let increment_hint (th:transformation_hint) : transformation_hint = match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 11, "end_line": 1730, "start_col": 0, "start_line": 1726 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
th: Vale.Transformers.InstructionReorder.transformation_hint -> Vale.Transformers.InstructionReorder.transformation_hint
Prims.Tot
[ "total" ]
[]
[ "Vale.Transformers.InstructionReorder.transformation_hint", "Prims.nat", "Vale.Transformers.InstructionReorder.MoveUpFrom", "Prims.op_Addition", "Vale.Transformers.InstructionReorder.DiveInAt" ]
[]
false
false
false
true
false
let increment_hint (th: transformation_hint) : transformation_hint =
match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.eq_codes
val eq_codes (c1 c2: codes) : bool
val eq_codes (c1 c2: codes) : bool
let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 18, "end_line": 1711, "start_col": 0, "start_line": 1697 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c1: Vale.X64.Machine_Semantics_s.codes -> c2: Vale.X64.Machine_Semantics_s.codes -> Prims.bool
Prims.Tot
[ "total" ]
[ "eq_code", "eq_codes" ]
[ "Vale.X64.Machine_Semantics_s.codes", "FStar.Pervasives.Native.Mktuple2", "Prims.list", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.op_AmpAmp", "Vale.Transformers.InstructionReorder.eq_code", "Vale.Transformers.InstructionReorder.eq_codes", "Prims.bool" ]
[ "mutual recursion" ]
false
false
false
true
false
let rec eq_codes (c1 c2: codes) : bool =
match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.perform_reordering_with_hints
val perform_reordering_with_hints (ts: transformation_hints) (c: codes) : possibly codes
val perform_reordering_with_hints (ts: transformation_hints) (c: codes) : possibly codes
let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 7, "end_line": 1677, "start_col": 0, "start_line": 1586 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ts: Vale.Transformers.InstructionReorder.transformation_hints -> c: Vale.X64.Machine_Semantics_s.codes -> Vale.Def.PossiblyMonad.possibly Vale.X64.Machine_Semantics_s.codes
Prims.Tot
[ "total" ]
[ "perform_reordering_with_hint", "perform_reordering_with_hints" ]
[ "Vale.Transformers.InstructionReorder.transformation_hints", "Vale.X64.Machine_Semantics_s.codes", "Vale.Transformers.InstructionReorder.is_empty_codes", "Vale.Def.PossiblyMonad.return", "Prims.Nil", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.bool", "Vale.Def.PossiblyMonad.Err", "Prims.op_Hat", "FStar.Pervasives.Native.fst", "Prims.string", "Prims.int", "Vale.X64.Print_s.print_code", "Vale.X64.Machine_s.Block", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Bytes_Code_s.ocmp", "Vale.X64.Print_s.gcc", "Vale.Def.PossiblyMonad.possibly", "Vale.Transformers.InstructionReorder.transformation_hint", "Prims.list", "Vale.Def.PossiblyMonad.op_let_Plus", "Vale.Transformers.InstructionReorder.perform_reordering_with_hint", "Prims.Cons", "Vale.Transformers.InstructionReorder.perform_reordering_with_hints" ]
[ "mutual recursion" ]
false
false
false
true
false
let rec perform_reordering_with_hints (ts: transformation_hints) (c: codes) : possibly codes =
match ts with | [] -> (if is_empty_codes c then (return []) else (Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)))) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then (Err "Trying to move 'empty' code.") else (let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs'))
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.is_empty_code
val is_empty_code (c: code) : bool
val is_empty_code (c: code) : bool
let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 51, "end_line": 1584, "start_col": 0, "start_line": 1574 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code -> Prims.bool
Prims.Tot
[ "total" ]
[ "is_empty_code", "is_empty_codes" ]
[ "Vale.X64.Machine_Semantics_s.code", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "Vale.X64.Machine_s.precode", "Vale.X64.Bytes_Code_s.ocmp", "Vale.Transformers.InstructionReorder.is_empty_codes", "Prims.bool" ]
[ "mutual recursion" ]
false
false
false
true
false
let rec is_empty_code (c: code) : bool =
match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.eq_code
val eq_code (c1 c2: code) : bool
val eq_code (c1 c2: code) : bool
let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 18, "end_line": 1711, "start_col": 0, "start_line": 1697 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c1: Vale.X64.Machine_Semantics_s.code -> c2: Vale.X64.Machine_Semantics_s.code -> Prims.bool
Prims.Tot
[ "total" ]
[ "eq_code", "eq_codes" ]
[ "Vale.X64.Machine_Semantics_s.code", "FStar.Pervasives.Native.Mktuple2", "Vale.X64.Machine_s.precode", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.X64.Bytes_Code_s.ocmp", "Vale.Transformers.InstructionReorder.eq_ins", "Prims.list", "Vale.Transformers.InstructionReorder.eq_codes", "Prims.op_AmpAmp", "Prims.op_Equality", "Vale.Transformers.InstructionReorder.eq_code", "Prims.bool" ]
[ "mutual recursion" ]
false
false
false
true
false
let rec eq_code (c1 c2: code) : bool =
match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_is_empty_codes
val lemma_is_empty_codes (cs: codes) (fuel: nat) (s: machine_state) : Lemma (requires (is_empty_codes cs)) (ensures ((machine_eval_codes cs fuel s) == (machine_eval_codes [] fuel s)))
val lemma_is_empty_codes (cs: codes) (fuel: nat) (s: machine_state) : Lemma (requires (is_empty_codes cs)) (ensures ((machine_eval_codes cs fuel s) == (machine_eval_codes [] fuel s)))
let rec lemma_is_empty_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (is_empty_code c)) (ensures ((machine_eval_code c fuel s) == (machine_eval_codes [] fuel s))) = match c with | Ins _ -> () | Block l -> lemma_is_empty_codes l fuel s | IfElse _ t f -> () | While _ c -> () and lemma_is_empty_codes (cs:codes) (fuel:nat) (s:machine_state) : Lemma (requires (is_empty_codes cs)) (ensures ((machine_eval_codes cs fuel s) == (machine_eval_codes [] fuel s))) = match cs with | [] -> () | x :: xs -> lemma_is_empty_code x fuel s; lemma_is_empty_codes xs fuel s
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 34, "end_line": 1963, "start_col": 0, "start_line": 1946 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs let increment_hint (th:transformation_hint) : transformation_hint = match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th let rec find_deep_code_transform (c:code) (cs:codes) : possibly transformation_hint = match cs with | [] -> Err ("Not found (during find_deep_code_transform): " ^ fst (print_code c 0 gcc)) | x :: xs -> (* let _ = IO.debug_print_string ( "---------------------------------\n" ^ " c : \n" ^ fst (print_code c 0 gcc) ^ "\n" ^ " x : \n" ^ fst (print_code x 0 gcc) ^ "\n" ^ " xs : \n" ^ fst (print_code (Block xs) 0 gcc) ^ "\n" ^ "---------------------------------\n" ^ "") in *) if is_empty_code x then find_deep_code_transform c xs else ( if eq_codes (fully_unblocked_code x) (fully_unblocked_code c) then ( return (MoveUpFrom 0) ) else ( match x with | Block l -> ( match find_deep_code_transform c l with | Ok t -> return (DiveInAt 0 t) | Err reason -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) | _ -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) ) let rec metric_for_code (c:code) : GTot nat = 1 + ( match c with | Ins _ -> 0 | Block l -> metric_for_codes l | IfElse _ t f -> metric_for_code t + metric_for_code f | While _ b -> metric_for_code b ) and metric_for_codes (c:codes) : GTot nat = match c with | [] -> 0 | x :: xs -> 1 + metric_for_code x + metric_for_codes xs let rec lemma_metric_for_codes_append (c1 c2:codes) : Lemma (ensures (metric_for_codes (c1 `L.append` c2) == metric_for_codes c1 + metric_for_codes c2)) [SMTPat (metric_for_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_metric_for_codes_append xs c2 irreducible (* Our proofs do not depend on how the hints are found. As long as some hints are provided, we validate the hints to perform the transformation and use it. Thus, we make this function [irreducible] to explicitly prevent any of the proofs from reasoning about it. *) let rec find_transformation_hints (c1 c2:codes) : Tot (possibly transformation_hints) (decreases %[metric_for_codes c2; metric_for_codes c1]) = let e1, e2 = is_empty_codes c1, is_empty_codes c2 in if e1 && e2 then ( return [] ) else if e2 then ( Err ("non empty first code: " ^ fst (print_code (Block c1) 0 gcc)) ) else if e1 then ( Err ("non empty second code: " ^ fst (print_code (Block c2) 0 gcc)) ) else ( let h1 :: t1 = c1 in let h2 :: t2 = c2 in assert (metric_for_codes c2 >= metric_for_code h2); (* OBSERVE *) if is_empty_code h1 then ( find_transformation_hints t1 c2 ) else if is_empty_code h2 then ( find_transformation_hints c1 t2 ) else ( match find_deep_code_transform h2 c1 with | Ok th -> ( match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Unable to find valid movement for : " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | Err reason -> ( let h1 :: t1 = c1 in match h1, h2 with | Block l1, Block l2 -> ( match ( let+ t_hints1 = find_transformation_hints l1 l2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (wrap_diveinat 0 t_hints1 `L.append` t_hints2) ) with | Ok ths -> return ths | Err reason -> find_transformation_hints c1 (l2 `L.append` t2) ) | IfElse co1 (Block tr1) (Block fa1), IfElse co2 (Block tr2) (Block fa2) -> (co1 = co2) /- ("Non-same conditions for IfElse: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block tr2)); (* OBSERVE *) assert (metric_for_code h2 > metric_for_code (Block fa2)); (* OBSERVE *) let+ tr_hints = find_transformation_hints tr1 tr2 in let+ fa_hints = find_transformation_hints fa1 fa2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceIfElse tr_hints fa_hints :: t_hints2) | While co1 (Block bo1), While co2 (Block bo2) -> (co1 = co2) /- ("Non-same conditions for While: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block bo2)); (* OBSERVE *) let+ bo_hints = find_transformation_hints bo1 bo2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceWhile bo_hints :: t_hints2) | Block l1, IfElse _ _ _ | Block l1, While _ _ -> assert (metric_for_codes (l1 `L.append` t1) == metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) assert_norm (metric_for_codes c1 == 2 + metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) let+ t_hints1 = find_transformation_hints (l1 `L.append` t1) c2 in ( match t_hints1 with | [] -> Err "Impossible" | th :: _ -> let th = DiveInAt 0 th in match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Failed during left-unblock for " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | _, Block l2 -> find_transformation_hints c1 (l2 `L.append` t2) | IfElse _ _ _, IfElse _ _ _ | While _ _, While _ _ -> Err ("Found weird non-standard code: " ^ fst (print_code h1 0 gcc)) | _ -> Err ("Find deep code failure. Reason: " ^ reason) ) ) ) /// If a transformation can be performed, then the result behaves /// identically as per the [equiv_states] relation. #push-options "--z3rlimit 10 --initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_bubble_to_top (cs : codes) (i:nat{i < L.length cs}) (fuel:nat) (s s' : machine_state) : Lemma (requires ( (s'.ms_ok) /\ (Some s' == machine_eval_codes cs fuel s) /\ (Ok? (bubble_to_top cs i)))) (ensures ( let x = L.index cs i in let Ok xs = bubble_to_top cs i in let s1' = machine_eval_code x fuel s in (Some? s1') /\ ( let Some s1 = s1' in let s2' = machine_eval_codes xs fuel s1 in (Some? s2') /\ ( let Some s2 = s2' in equiv_states s' s2)))) = match cs with | [_] -> () | h :: t -> let x = L.index cs i in let Ok xs = bubble_to_top cs i in if i = 0 then () else ( let Some s_h = machine_eval_code h fuel s in lemma_bubble_to_top (L.tl cs) (i-1) fuel s_h s'; let Some s_h_x = machine_eval_code x fuel s_h in let Some s_hx = machine_eval_codes [h;x] fuel s in assert (s_h_x == s_hx); lemma_code_exchange_allowed x h fuel s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes (L.tl xs) fuel) s_hx; assert (s_hx.ms_ok); let Some s_xh = machine_eval_codes [x;h] fuel s in lemma_eval_codes_equiv_states (L.tl xs) fuel s_hx s_xh ) #pop-options #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_machine_eval_codes_block_to_append (c1 c2 : codes) (fuel:nat) (s:machine_state) : Lemma (ensures (machine_eval_codes (c1 `L.append` c2) fuel s == machine_eval_codes (Block c1 :: c2) fuel s)) = match c1 with | [] -> () | x :: xs -> match machine_eval_code x fuel s with | None -> () | Some s1 -> lemma_machine_eval_codes_block_to_append xs c2 fuel s1 #pop-options let rec lemma_append_single (xs:list 'a) (y:'a) (i:nat) : Lemma (requires (i == L.length xs)) (ensures ( L.length (xs `L.append` [y]) = L.length xs + 1 /\ L.index (xs `L.append` [y]) i == y)) = match xs with | [] -> () | x :: xs -> lemma_append_single xs y (i - 1)
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 3, "initial_ifuel": 1, "max_fuel": 3, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Vale.X64.Machine_Semantics_s.codes -> fuel: Prims.nat -> s: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.InstructionReorder.is_empty_codes cs) (ensures Vale.X64.Machine_Semantics_s.machine_eval_codes cs fuel s == Vale.X64.Machine_Semantics_s.machine_eval_codes [] fuel s)
FStar.Pervasives.Lemma
[ "lemma" ]
[ "lemma_is_empty_code", "lemma_is_empty_codes" ]
[ "Vale.X64.Machine_Semantics_s.codes", "Prims.nat", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "Vale.Transformers.InstructionReorder.lemma_is_empty_codes", "Prims.unit", "Vale.Transformers.InstructionReorder.lemma_is_empty_code", "Prims.b2t", "Vale.Transformers.InstructionReorder.is_empty_codes", "Prims.squash", "Prims.eq2", "FStar.Pervasives.Native.option", "Vale.X64.Machine_Semantics_s.machine_eval_codes", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "mutual recursion" ]
false
false
true
false
false
let rec lemma_is_empty_codes (cs: codes) (fuel: nat) (s: machine_state) : Lemma (requires (is_empty_codes cs)) (ensures ((machine_eval_codes cs fuel s) == (machine_eval_codes [] fuel s))) =
match cs with | [] -> () | x :: xs -> lemma_is_empty_code x fuel s; lemma_is_empty_codes xs fuel s
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.string_of_transformation_hint
val string_of_transformation_hint (th: transformation_hint) : Tot string (decreases %[th])
val string_of_transformation_hint (th: transformation_hint) : Tot string (decreases %[th])
let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]"
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 51, "end_line": 1557, "start_col": 0, "start_line": 1538 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
th: Vale.Transformers.InstructionReorder.transformation_hint -> Prims.Tot Prims.string
Prims.Tot
[ "total", "" ]
[ "string_of_transformation_hint", "aux_string_of_transformation_hints", "string_of_transformation_hints" ]
[ "Vale.Transformers.InstructionReorder.transformation_hint", "Prims.nat", "Prims.op_Hat", "Prims.string_of_int", "Vale.Transformers.InstructionReorder.string_of_transformation_hint", "Prims.list", "Vale.Transformers.InstructionReorder.string_of_transformation_hints", "Prims.string" ]
[ "mutual recursion" ]
false
false
false
true
false
let rec string_of_transformation_hint (th: transformation_hint) : Tot string (decreases %[th]) =
match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")"
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_is_empty_code
val lemma_is_empty_code (c: code) (fuel: nat) (s: machine_state) : Lemma (requires (is_empty_code c)) (ensures ((machine_eval_code c fuel s) == (machine_eval_codes [] fuel s)))
val lemma_is_empty_code (c: code) (fuel: nat) (s: machine_state) : Lemma (requires (is_empty_code c)) (ensures ((machine_eval_code c fuel s) == (machine_eval_codes [] fuel s)))
let rec lemma_is_empty_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (is_empty_code c)) (ensures ((machine_eval_code c fuel s) == (machine_eval_codes [] fuel s))) = match c with | Ins _ -> () | Block l -> lemma_is_empty_codes l fuel s | IfElse _ t f -> () | While _ c -> () and lemma_is_empty_codes (cs:codes) (fuel:nat) (s:machine_state) : Lemma (requires (is_empty_codes cs)) (ensures ((machine_eval_codes cs fuel s) == (machine_eval_codes [] fuel s))) = match cs with | [] -> () | x :: xs -> lemma_is_empty_code x fuel s; lemma_is_empty_codes xs fuel s
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 34, "end_line": 1963, "start_col": 0, "start_line": 1946 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs let increment_hint (th:transformation_hint) : transformation_hint = match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th let rec find_deep_code_transform (c:code) (cs:codes) : possibly transformation_hint = match cs with | [] -> Err ("Not found (during find_deep_code_transform): " ^ fst (print_code c 0 gcc)) | x :: xs -> (* let _ = IO.debug_print_string ( "---------------------------------\n" ^ " c : \n" ^ fst (print_code c 0 gcc) ^ "\n" ^ " x : \n" ^ fst (print_code x 0 gcc) ^ "\n" ^ " xs : \n" ^ fst (print_code (Block xs) 0 gcc) ^ "\n" ^ "---------------------------------\n" ^ "") in *) if is_empty_code x then find_deep_code_transform c xs else ( if eq_codes (fully_unblocked_code x) (fully_unblocked_code c) then ( return (MoveUpFrom 0) ) else ( match x with | Block l -> ( match find_deep_code_transform c l with | Ok t -> return (DiveInAt 0 t) | Err reason -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) | _ -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) ) let rec metric_for_code (c:code) : GTot nat = 1 + ( match c with | Ins _ -> 0 | Block l -> metric_for_codes l | IfElse _ t f -> metric_for_code t + metric_for_code f | While _ b -> metric_for_code b ) and metric_for_codes (c:codes) : GTot nat = match c with | [] -> 0 | x :: xs -> 1 + metric_for_code x + metric_for_codes xs let rec lemma_metric_for_codes_append (c1 c2:codes) : Lemma (ensures (metric_for_codes (c1 `L.append` c2) == metric_for_codes c1 + metric_for_codes c2)) [SMTPat (metric_for_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_metric_for_codes_append xs c2 irreducible (* Our proofs do not depend on how the hints are found. As long as some hints are provided, we validate the hints to perform the transformation and use it. Thus, we make this function [irreducible] to explicitly prevent any of the proofs from reasoning about it. *) let rec find_transformation_hints (c1 c2:codes) : Tot (possibly transformation_hints) (decreases %[metric_for_codes c2; metric_for_codes c1]) = let e1, e2 = is_empty_codes c1, is_empty_codes c2 in if e1 && e2 then ( return [] ) else if e2 then ( Err ("non empty first code: " ^ fst (print_code (Block c1) 0 gcc)) ) else if e1 then ( Err ("non empty second code: " ^ fst (print_code (Block c2) 0 gcc)) ) else ( let h1 :: t1 = c1 in let h2 :: t2 = c2 in assert (metric_for_codes c2 >= metric_for_code h2); (* OBSERVE *) if is_empty_code h1 then ( find_transformation_hints t1 c2 ) else if is_empty_code h2 then ( find_transformation_hints c1 t2 ) else ( match find_deep_code_transform h2 c1 with | Ok th -> ( match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Unable to find valid movement for : " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | Err reason -> ( let h1 :: t1 = c1 in match h1, h2 with | Block l1, Block l2 -> ( match ( let+ t_hints1 = find_transformation_hints l1 l2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (wrap_diveinat 0 t_hints1 `L.append` t_hints2) ) with | Ok ths -> return ths | Err reason -> find_transformation_hints c1 (l2 `L.append` t2) ) | IfElse co1 (Block tr1) (Block fa1), IfElse co2 (Block tr2) (Block fa2) -> (co1 = co2) /- ("Non-same conditions for IfElse: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block tr2)); (* OBSERVE *) assert (metric_for_code h2 > metric_for_code (Block fa2)); (* OBSERVE *) let+ tr_hints = find_transformation_hints tr1 tr2 in let+ fa_hints = find_transformation_hints fa1 fa2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceIfElse tr_hints fa_hints :: t_hints2) | While co1 (Block bo1), While co2 (Block bo2) -> (co1 = co2) /- ("Non-same conditions for While: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block bo2)); (* OBSERVE *) let+ bo_hints = find_transformation_hints bo1 bo2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceWhile bo_hints :: t_hints2) | Block l1, IfElse _ _ _ | Block l1, While _ _ -> assert (metric_for_codes (l1 `L.append` t1) == metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) assert_norm (metric_for_codes c1 == 2 + metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) let+ t_hints1 = find_transformation_hints (l1 `L.append` t1) c2 in ( match t_hints1 with | [] -> Err "Impossible" | th :: _ -> let th = DiveInAt 0 th in match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Failed during left-unblock for " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | _, Block l2 -> find_transformation_hints c1 (l2 `L.append` t2) | IfElse _ _ _, IfElse _ _ _ | While _ _, While _ _ -> Err ("Found weird non-standard code: " ^ fst (print_code h1 0 gcc)) | _ -> Err ("Find deep code failure. Reason: " ^ reason) ) ) ) /// If a transformation can be performed, then the result behaves /// identically as per the [equiv_states] relation. #push-options "--z3rlimit 10 --initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_bubble_to_top (cs : codes) (i:nat{i < L.length cs}) (fuel:nat) (s s' : machine_state) : Lemma (requires ( (s'.ms_ok) /\ (Some s' == machine_eval_codes cs fuel s) /\ (Ok? (bubble_to_top cs i)))) (ensures ( let x = L.index cs i in let Ok xs = bubble_to_top cs i in let s1' = machine_eval_code x fuel s in (Some? s1') /\ ( let Some s1 = s1' in let s2' = machine_eval_codes xs fuel s1 in (Some? s2') /\ ( let Some s2 = s2' in equiv_states s' s2)))) = match cs with | [_] -> () | h :: t -> let x = L.index cs i in let Ok xs = bubble_to_top cs i in if i = 0 then () else ( let Some s_h = machine_eval_code h fuel s in lemma_bubble_to_top (L.tl cs) (i-1) fuel s_h s'; let Some s_h_x = machine_eval_code x fuel s_h in let Some s_hx = machine_eval_codes [h;x] fuel s in assert (s_h_x == s_hx); lemma_code_exchange_allowed x h fuel s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes (L.tl xs) fuel) s_hx; assert (s_hx.ms_ok); let Some s_xh = machine_eval_codes [x;h] fuel s in lemma_eval_codes_equiv_states (L.tl xs) fuel s_hx s_xh ) #pop-options #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_machine_eval_codes_block_to_append (c1 c2 : codes) (fuel:nat) (s:machine_state) : Lemma (ensures (machine_eval_codes (c1 `L.append` c2) fuel s == machine_eval_codes (Block c1 :: c2) fuel s)) = match c1 with | [] -> () | x :: xs -> match machine_eval_code x fuel s with | None -> () | Some s1 -> lemma_machine_eval_codes_block_to_append xs c2 fuel s1 #pop-options let rec lemma_append_single (xs:list 'a) (y:'a) (i:nat) : Lemma (requires (i == L.length xs)) (ensures ( L.length (xs `L.append` [y]) = L.length xs + 1 /\ L.index (xs `L.append` [y]) i == y)) = match xs with | [] -> () | x :: xs -> lemma_append_single xs y (i - 1)
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 3, "initial_ifuel": 1, "max_fuel": 3, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code -> fuel: Prims.nat -> s: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.InstructionReorder.is_empty_code c) (ensures Vale.X64.Machine_Semantics_s.machine_eval_code c fuel s == Vale.X64.Machine_Semantics_s.machine_eval_codes [] fuel s)
FStar.Pervasives.Lemma
[ "lemma" ]
[ "lemma_is_empty_code", "lemma_is_empty_codes" ]
[ "Vale.X64.Machine_Semantics_s.code", "Prims.nat", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "Vale.X64.Machine_s.precode", "Vale.X64.Bytes_Code_s.ocmp", "Vale.Transformers.InstructionReorder.lemma_is_empty_codes", "Prims.unit", "Prims.b2t", "Vale.Transformers.InstructionReorder.is_empty_code", "Prims.squash", "Prims.eq2", "FStar.Pervasives.Native.option", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Vale.X64.Machine_Semantics_s.machine_eval_codes", "Prims.Nil", "Vale.X64.Bytes_Code_s.code_t", "FStar.Pervasives.pattern" ]
[ "mutual recursion" ]
false
false
true
false
false
let rec lemma_is_empty_code (c: code) (fuel: nat) (s: machine_state) : Lemma (requires (is_empty_code c)) (ensures ((machine_eval_code c fuel s) == (machine_eval_codes [] fuel s))) =
match c with | Ins _ -> () | Block l -> lemma_is_empty_codes l fuel s | IfElse _ t f -> () | While _ c -> ()
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_bounded_codes
val lemma_bounded_codes (c: safely_bounded_codes) (fuel: nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c])
val lemma_bounded_codes (c: safely_bounded_codes) (fuel: nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c])
let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 66, "end_line": 1444, "start_col": 0, "start_line": 1408 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.Transformers.InstructionReorder.safely_bounded_codes -> fuel: Prims.nat -> FStar.Pervasives.Lemma (ensures Vale.Transformers.BoundedInstructionEffects.bounded_effects (Vale.Transformers.InstructionReorder.rw_set_of_codes c) (Vale.Transformers.InstructionReorder.wrap_sos (Vale.X64.Machine_Semantics_s.machine_eval_codes c fuel))) (decreases c)
FStar.Pervasives.Lemma
[ "lemma", "" ]
[ "lemma_bounded_code", "lemma_bounded_codes" ]
[ "Vale.Transformers.InstructionReorder.safely_bounded_codes", "Prims.nat", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "Vale.Transformers.InstructionReorder.lemma_bounded_effects_code_codes", "Vale.Transformers.InstructionReorder.rw_set_of_codes", "Prims.unit", "Vale.Transformers.BoundedInstructionEffects.lemma_bounded_effects_series", "Vale.Transformers.InstructionReorder.rw_set_of_code", "Vale.Transformers.InstructionReorder.wrap_sos", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Vale.X64.Machine_Semantics_s.machine_eval_codes", "Vale.Transformers.InstructionReorder.lemma_bounded_codes", "Vale.Transformers.InstructionReorder.lemma_bounded_code", "Prims.l_True", "Prims.squash", "Vale.Transformers.BoundedInstructionEffects.bounded_effects", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "mutual recursion" ]
false
false
true
false
false
let rec lemma_bounded_codes (c: safely_bounded_codes) (fuel: nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) =
let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_machine_eval_codes_block_to_append
val lemma_machine_eval_codes_block_to_append (c1 c2: codes) (fuel: nat) (s: machine_state) : Lemma (ensures (machine_eval_codes (c1 `L.append` c2) fuel s == machine_eval_codes (Block c1 :: c2) fuel s))
val lemma_machine_eval_codes_block_to_append (c1 c2: codes) (fuel: nat) (s: machine_state) : Lemma (ensures (machine_eval_codes (c1 `L.append` c2) fuel s == machine_eval_codes (Block c1 :: c2) fuel s))
let rec lemma_machine_eval_codes_block_to_append (c1 c2 : codes) (fuel:nat) (s:machine_state) : Lemma (ensures (machine_eval_codes (c1 `L.append` c2) fuel s == machine_eval_codes (Block c1 :: c2) fuel s)) = match c1 with | [] -> () | x :: xs -> match machine_eval_code x fuel s with | None -> () | Some s1 -> lemma_machine_eval_codes_block_to_append xs c2 fuel s1
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 60, "end_line": 1932, "start_col": 0, "start_line": 1923 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs let increment_hint (th:transformation_hint) : transformation_hint = match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th let rec find_deep_code_transform (c:code) (cs:codes) : possibly transformation_hint = match cs with | [] -> Err ("Not found (during find_deep_code_transform): " ^ fst (print_code c 0 gcc)) | x :: xs -> (* let _ = IO.debug_print_string ( "---------------------------------\n" ^ " c : \n" ^ fst (print_code c 0 gcc) ^ "\n" ^ " x : \n" ^ fst (print_code x 0 gcc) ^ "\n" ^ " xs : \n" ^ fst (print_code (Block xs) 0 gcc) ^ "\n" ^ "---------------------------------\n" ^ "") in *) if is_empty_code x then find_deep_code_transform c xs else ( if eq_codes (fully_unblocked_code x) (fully_unblocked_code c) then ( return (MoveUpFrom 0) ) else ( match x with | Block l -> ( match find_deep_code_transform c l with | Ok t -> return (DiveInAt 0 t) | Err reason -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) | _ -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) ) let rec metric_for_code (c:code) : GTot nat = 1 + ( match c with | Ins _ -> 0 | Block l -> metric_for_codes l | IfElse _ t f -> metric_for_code t + metric_for_code f | While _ b -> metric_for_code b ) and metric_for_codes (c:codes) : GTot nat = match c with | [] -> 0 | x :: xs -> 1 + metric_for_code x + metric_for_codes xs let rec lemma_metric_for_codes_append (c1 c2:codes) : Lemma (ensures (metric_for_codes (c1 `L.append` c2) == metric_for_codes c1 + metric_for_codes c2)) [SMTPat (metric_for_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_metric_for_codes_append xs c2 irreducible (* Our proofs do not depend on how the hints are found. As long as some hints are provided, we validate the hints to perform the transformation and use it. Thus, we make this function [irreducible] to explicitly prevent any of the proofs from reasoning about it. *) let rec find_transformation_hints (c1 c2:codes) : Tot (possibly transformation_hints) (decreases %[metric_for_codes c2; metric_for_codes c1]) = let e1, e2 = is_empty_codes c1, is_empty_codes c2 in if e1 && e2 then ( return [] ) else if e2 then ( Err ("non empty first code: " ^ fst (print_code (Block c1) 0 gcc)) ) else if e1 then ( Err ("non empty second code: " ^ fst (print_code (Block c2) 0 gcc)) ) else ( let h1 :: t1 = c1 in let h2 :: t2 = c2 in assert (metric_for_codes c2 >= metric_for_code h2); (* OBSERVE *) if is_empty_code h1 then ( find_transformation_hints t1 c2 ) else if is_empty_code h2 then ( find_transformation_hints c1 t2 ) else ( match find_deep_code_transform h2 c1 with | Ok th -> ( match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Unable to find valid movement for : " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | Err reason -> ( let h1 :: t1 = c1 in match h1, h2 with | Block l1, Block l2 -> ( match ( let+ t_hints1 = find_transformation_hints l1 l2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (wrap_diveinat 0 t_hints1 `L.append` t_hints2) ) with | Ok ths -> return ths | Err reason -> find_transformation_hints c1 (l2 `L.append` t2) ) | IfElse co1 (Block tr1) (Block fa1), IfElse co2 (Block tr2) (Block fa2) -> (co1 = co2) /- ("Non-same conditions for IfElse: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block tr2)); (* OBSERVE *) assert (metric_for_code h2 > metric_for_code (Block fa2)); (* OBSERVE *) let+ tr_hints = find_transformation_hints tr1 tr2 in let+ fa_hints = find_transformation_hints fa1 fa2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceIfElse tr_hints fa_hints :: t_hints2) | While co1 (Block bo1), While co2 (Block bo2) -> (co1 = co2) /- ("Non-same conditions for While: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block bo2)); (* OBSERVE *) let+ bo_hints = find_transformation_hints bo1 bo2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceWhile bo_hints :: t_hints2) | Block l1, IfElse _ _ _ | Block l1, While _ _ -> assert (metric_for_codes (l1 `L.append` t1) == metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) assert_norm (metric_for_codes c1 == 2 + metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) let+ t_hints1 = find_transformation_hints (l1 `L.append` t1) c2 in ( match t_hints1 with | [] -> Err "Impossible" | th :: _ -> let th = DiveInAt 0 th in match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Failed during left-unblock for " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | _, Block l2 -> find_transformation_hints c1 (l2 `L.append` t2) | IfElse _ _ _, IfElse _ _ _ | While _ _, While _ _ -> Err ("Found weird non-standard code: " ^ fst (print_code h1 0 gcc)) | _ -> Err ("Find deep code failure. Reason: " ^ reason) ) ) ) /// If a transformation can be performed, then the result behaves /// identically as per the [equiv_states] relation. #push-options "--z3rlimit 10 --initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_bubble_to_top (cs : codes) (i:nat{i < L.length cs}) (fuel:nat) (s s' : machine_state) : Lemma (requires ( (s'.ms_ok) /\ (Some s' == machine_eval_codes cs fuel s) /\ (Ok? (bubble_to_top cs i)))) (ensures ( let x = L.index cs i in let Ok xs = bubble_to_top cs i in let s1' = machine_eval_code x fuel s in (Some? s1') /\ ( let Some s1 = s1' in let s2' = machine_eval_codes xs fuel s1 in (Some? s2') /\ ( let Some s2 = s2' in equiv_states s' s2)))) = match cs with | [_] -> () | h :: t -> let x = L.index cs i in let Ok xs = bubble_to_top cs i in if i = 0 then () else ( let Some s_h = machine_eval_code h fuel s in lemma_bubble_to_top (L.tl cs) (i-1) fuel s_h s'; let Some s_h_x = machine_eval_code x fuel s_h in let Some s_hx = machine_eval_codes [h;x] fuel s in assert (s_h_x == s_hx); lemma_code_exchange_allowed x h fuel s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes (L.tl xs) fuel) s_hx; assert (s_hx.ms_ok); let Some s_xh = machine_eval_codes [x;h] fuel s in lemma_eval_codes_equiv_states (L.tl xs) fuel s_hx s_xh ) #pop-options
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 3, "initial_ifuel": 1, "max_fuel": 3, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c1: Vale.X64.Machine_Semantics_s.codes -> c2: Vale.X64.Machine_Semantics_s.codes -> fuel: Prims.nat -> s: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (ensures Vale.X64.Machine_Semantics_s.machine_eval_codes (c1 @ c2) fuel s == Vale.X64.Machine_Semantics_s.machine_eval_codes (Vale.X64.Machine_s.Block c1 :: c2) fuel s)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.codes", "Prims.nat", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Vale.Transformers.InstructionReorder.lemma_machine_eval_codes_block_to_append", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.eq2", "FStar.Pervasives.Native.option", "Vale.X64.Machine_Semantics_s.machine_eval_codes", "FStar.List.Tot.Base.append", "Prims.Cons", "Vale.X64.Machine_s.Block", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Bytes_Code_s.ocmp", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_machine_eval_codes_block_to_append (c1 c2: codes) (fuel: nat) (s: machine_state) : Lemma (ensures (machine_eval_codes (c1 `L.append` c2) fuel s == machine_eval_codes (Block c1 :: c2) fuel s)) =
match c1 with | [] -> () | x :: xs -> match machine_eval_code x fuel s with | None -> () | Some s1 -> lemma_machine_eval_codes_block_to_append xs c2 fuel s1
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.metric_for_codes
val metric_for_codes (c: codes) : GTot nat
val metric_for_codes (c: codes) : GTot nat
let rec metric_for_code (c:code) : GTot nat = 1 + ( match c with | Ins _ -> 0 | Block l -> metric_for_codes l | IfElse _ t f -> metric_for_code t + metric_for_code f | While _ b -> metric_for_code b ) and metric_for_codes (c:codes) : GTot nat = match c with | [] -> 0 | x :: xs -> 1 + metric_for_code x + metric_for_codes xs
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 58, "end_line": 1780, "start_col": 0, "start_line": 1768 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs let increment_hint (th:transformation_hint) : transformation_hint = match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th let rec find_deep_code_transform (c:code) (cs:codes) : possibly transformation_hint = match cs with | [] -> Err ("Not found (during find_deep_code_transform): " ^ fst (print_code c 0 gcc)) | x :: xs -> (* let _ = IO.debug_print_string ( "---------------------------------\n" ^ " c : \n" ^ fst (print_code c 0 gcc) ^ "\n" ^ " x : \n" ^ fst (print_code x 0 gcc) ^ "\n" ^ " xs : \n" ^ fst (print_code (Block xs) 0 gcc) ^ "\n" ^ "---------------------------------\n" ^ "") in *) if is_empty_code x then find_deep_code_transform c xs else ( if eq_codes (fully_unblocked_code x) (fully_unblocked_code c) then ( return (MoveUpFrom 0) ) else ( match x with | Block l -> ( match find_deep_code_transform c l with | Ok t -> return (DiveInAt 0 t) | Err reason -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) | _ -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) )
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.codes -> Prims.GTot Prims.nat
Prims.GTot
[ "sometrivial" ]
[ "metric_for_code", "metric_for_codes" ]
[ "Vale.X64.Machine_Semantics_s.codes", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "Prims.op_Addition", "Vale.Transformers.InstructionReorder.metric_for_code", "Vale.Transformers.InstructionReorder.metric_for_codes", "Prims.nat" ]
[ "mutual recursion" ]
false
false
false
false
false
let rec metric_for_codes (c: codes) : GTot nat =
match c with | [] -> 0 | x :: xs -> 1 + metric_for_code x + metric_for_codes xs
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.purge_empty_codes
val purge_empty_codes (cs: codes) : codes
val purge_empty_codes (cs: codes) : codes
let rec purge_empty_code (c:code) : code = match c with | Block l -> Block (purge_empty_codes l) | IfElse c t f -> IfElse c (purge_empty_code t) (purge_empty_code f) | While c b -> While c (purge_empty_code b) | _ -> c and purge_empty_codes (cs:codes) : codes = match cs with | [] -> [] | x :: xs -> if is_empty_code x then ( purge_empty_codes xs ) else ( purge_empty_code x :: purge_empty_codes xs )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 2151, "start_col": 0, "start_line": 2132 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs let increment_hint (th:transformation_hint) : transformation_hint = match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th let rec find_deep_code_transform (c:code) (cs:codes) : possibly transformation_hint = match cs with | [] -> Err ("Not found (during find_deep_code_transform): " ^ fst (print_code c 0 gcc)) | x :: xs -> (* let _ = IO.debug_print_string ( "---------------------------------\n" ^ " c : \n" ^ fst (print_code c 0 gcc) ^ "\n" ^ " x : \n" ^ fst (print_code x 0 gcc) ^ "\n" ^ " xs : \n" ^ fst (print_code (Block xs) 0 gcc) ^ "\n" ^ "---------------------------------\n" ^ "") in *) if is_empty_code x then find_deep_code_transform c xs else ( if eq_codes (fully_unblocked_code x) (fully_unblocked_code c) then ( return (MoveUpFrom 0) ) else ( match x with | Block l -> ( match find_deep_code_transform c l with | Ok t -> return (DiveInAt 0 t) | Err reason -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) | _ -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) ) let rec metric_for_code (c:code) : GTot nat = 1 + ( match c with | Ins _ -> 0 | Block l -> metric_for_codes l | IfElse _ t f -> metric_for_code t + metric_for_code f | While _ b -> metric_for_code b ) and metric_for_codes (c:codes) : GTot nat = match c with | [] -> 0 | x :: xs -> 1 + metric_for_code x + metric_for_codes xs let rec lemma_metric_for_codes_append (c1 c2:codes) : Lemma (ensures (metric_for_codes (c1 `L.append` c2) == metric_for_codes c1 + metric_for_codes c2)) [SMTPat (metric_for_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_metric_for_codes_append xs c2 irreducible (* Our proofs do not depend on how the hints are found. As long as some hints are provided, we validate the hints to perform the transformation and use it. Thus, we make this function [irreducible] to explicitly prevent any of the proofs from reasoning about it. *) let rec find_transformation_hints (c1 c2:codes) : Tot (possibly transformation_hints) (decreases %[metric_for_codes c2; metric_for_codes c1]) = let e1, e2 = is_empty_codes c1, is_empty_codes c2 in if e1 && e2 then ( return [] ) else if e2 then ( Err ("non empty first code: " ^ fst (print_code (Block c1) 0 gcc)) ) else if e1 then ( Err ("non empty second code: " ^ fst (print_code (Block c2) 0 gcc)) ) else ( let h1 :: t1 = c1 in let h2 :: t2 = c2 in assert (metric_for_codes c2 >= metric_for_code h2); (* OBSERVE *) if is_empty_code h1 then ( find_transformation_hints t1 c2 ) else if is_empty_code h2 then ( find_transformation_hints c1 t2 ) else ( match find_deep_code_transform h2 c1 with | Ok th -> ( match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Unable to find valid movement for : " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | Err reason -> ( let h1 :: t1 = c1 in match h1, h2 with | Block l1, Block l2 -> ( match ( let+ t_hints1 = find_transformation_hints l1 l2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (wrap_diveinat 0 t_hints1 `L.append` t_hints2) ) with | Ok ths -> return ths | Err reason -> find_transformation_hints c1 (l2 `L.append` t2) ) | IfElse co1 (Block tr1) (Block fa1), IfElse co2 (Block tr2) (Block fa2) -> (co1 = co2) /- ("Non-same conditions for IfElse: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block tr2)); (* OBSERVE *) assert (metric_for_code h2 > metric_for_code (Block fa2)); (* OBSERVE *) let+ tr_hints = find_transformation_hints tr1 tr2 in let+ fa_hints = find_transformation_hints fa1 fa2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceIfElse tr_hints fa_hints :: t_hints2) | While co1 (Block bo1), While co2 (Block bo2) -> (co1 = co2) /- ("Non-same conditions for While: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block bo2)); (* OBSERVE *) let+ bo_hints = find_transformation_hints bo1 bo2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceWhile bo_hints :: t_hints2) | Block l1, IfElse _ _ _ | Block l1, While _ _ -> assert (metric_for_codes (l1 `L.append` t1) == metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) assert_norm (metric_for_codes c1 == 2 + metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) let+ t_hints1 = find_transformation_hints (l1 `L.append` t1) c2 in ( match t_hints1 with | [] -> Err "Impossible" | th :: _ -> let th = DiveInAt 0 th in match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Failed during left-unblock for " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | _, Block l2 -> find_transformation_hints c1 (l2 `L.append` t2) | IfElse _ _ _, IfElse _ _ _ | While _ _, While _ _ -> Err ("Found weird non-standard code: " ^ fst (print_code h1 0 gcc)) | _ -> Err ("Find deep code failure. Reason: " ^ reason) ) ) ) /// If a transformation can be performed, then the result behaves /// identically as per the [equiv_states] relation. #push-options "--z3rlimit 10 --initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_bubble_to_top (cs : codes) (i:nat{i < L.length cs}) (fuel:nat) (s s' : machine_state) : Lemma (requires ( (s'.ms_ok) /\ (Some s' == machine_eval_codes cs fuel s) /\ (Ok? (bubble_to_top cs i)))) (ensures ( let x = L.index cs i in let Ok xs = bubble_to_top cs i in let s1' = machine_eval_code x fuel s in (Some? s1') /\ ( let Some s1 = s1' in let s2' = machine_eval_codes xs fuel s1 in (Some? s2') /\ ( let Some s2 = s2' in equiv_states s' s2)))) = match cs with | [_] -> () | h :: t -> let x = L.index cs i in let Ok xs = bubble_to_top cs i in if i = 0 then () else ( let Some s_h = machine_eval_code h fuel s in lemma_bubble_to_top (L.tl cs) (i-1) fuel s_h s'; let Some s_h_x = machine_eval_code x fuel s_h in let Some s_hx = machine_eval_codes [h;x] fuel s in assert (s_h_x == s_hx); lemma_code_exchange_allowed x h fuel s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes (L.tl xs) fuel) s_hx; assert (s_hx.ms_ok); let Some s_xh = machine_eval_codes [x;h] fuel s in lemma_eval_codes_equiv_states (L.tl xs) fuel s_hx s_xh ) #pop-options #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_machine_eval_codes_block_to_append (c1 c2 : codes) (fuel:nat) (s:machine_state) : Lemma (ensures (machine_eval_codes (c1 `L.append` c2) fuel s == machine_eval_codes (Block c1 :: c2) fuel s)) = match c1 with | [] -> () | x :: xs -> match machine_eval_code x fuel s with | None -> () | Some s1 -> lemma_machine_eval_codes_block_to_append xs c2 fuel s1 #pop-options let rec lemma_append_single (xs:list 'a) (y:'a) (i:nat) : Lemma (requires (i == L.length xs)) (ensures ( L.length (xs `L.append` [y]) = L.length xs + 1 /\ L.index (xs `L.append` [y]) i == y)) = match xs with | [] -> () | x :: xs -> lemma_append_single xs y (i - 1) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_is_empty_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (is_empty_code c)) (ensures ((machine_eval_code c fuel s) == (machine_eval_codes [] fuel s))) = match c with | Ins _ -> () | Block l -> lemma_is_empty_codes l fuel s | IfElse _ t f -> () | While _ c -> () and lemma_is_empty_codes (cs:codes) (fuel:nat) (s:machine_state) : Lemma (requires (is_empty_codes cs)) (ensures ((machine_eval_codes cs fuel s) == (machine_eval_codes [] fuel s))) = match cs with | [] -> () | x :: xs -> lemma_is_empty_code x fuel s; lemma_is_empty_codes xs fuel s #pop-options #restart-solver #push-options "--z3rlimit 100 --initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_perform_reordering_with_hint (t:transformation_hint) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (requires ( (Ok? (perform_reordering_with_hint t cs)) /\ (Some? (machine_eval_codes cs fuel s)) /\ (Some?.v (machine_eval_codes cs fuel s)).ms_ok)) (ensures ( let Ok cs' = perform_reordering_with_hint t cs in equiv_ostates (machine_eval_codes cs fuel s) (machine_eval_codes cs' fuel s))) (decreases %[t; fuel; cs]) = let c = cs in let Ok cs' = perform_reordering_with_hint t cs in let Some s' = machine_eval_codes cs fuel s in let x :: xs = cs in if is_empty_codes [x] then ( lemma_is_empty_codes [x] fuel s; match machine_eval_code x fuel s with | None -> () | Some s' -> lemma_eval_codes_equiv_states xs fuel s s'; lemma_perform_reordering_with_hint t xs fuel s ) else ( match t with | MoveUpFrom i -> ( let Ok c' = bubble_to_top c i in lemma_bubble_to_top c i fuel s s' ) | DiveInAt i t' -> ( FStar.List.Pure.lemma_split3_append c i; FStar.List.Pure.lemma_split3_length c i; let left, mid, right = L.split3 c i in let Block l = mid in let Ok (y :: ys) = perform_reordering_with_hint t' l in L.append_length left [y]; let Ok left' = bubble_to_top (left `L.append` [y]) i in // assert (cs' == y :: (left' `L.append` (Block ys :: right))); assert (left `L.append` (mid :: right) == c); L.append_l_cons mid right left; assert ((left `L.append` [mid]) `L.append` right == c); lemma_machine_eval_codes_block_to_append (left `L.append` [mid]) right fuel s; let Some s_1 = machine_eval_code (Block (left `L.append` [mid])) fuel s in assert (Some s_1 == machine_eval_codes (left `L.append` [mid]) fuel s); lemma_machine_eval_codes_block_to_append left [mid] fuel s; let Some s_2 = machine_eval_code (Block left) fuel s in assert (Some s_2 == machine_eval_codes left fuel s); // assert (Some s_1 == machine_eval_codes [mid] fuel s_2); assert (Some s_1 == machine_eval_code (Block l) fuel s_2); assert (Some s_1 == machine_eval_codes l fuel s_2); assert (Some s' == machine_eval_codes right fuel s_1); if s_1.ms_ok then () else lemma_not_ok_propagate_codes right fuel s_1; lemma_perform_reordering_with_hint t' l fuel s_2; // let Some s_11 = machine_eval_codes (y :: ys) fuel s_2 in let Some s_12 = machine_eval_code y fuel s_2 in if s_12.ms_ok then () else lemma_not_ok_propagate_codes ys fuel s_12; assert (Some s_2 == machine_eval_codes left fuel s); assert (Some s_2 == machine_eval_code (Block left) fuel s); assert (Some s_12 == machine_eval_codes (Block left :: [y]) fuel s); lemma_machine_eval_codes_block_to_append left [y] fuel s; assert (Some s_12 == machine_eval_codes (left `L.append` [y]) fuel s); lemma_bubble_to_top (left `L.append` [y]) i fuel s s_12; // lemma_append_single left y i; assert (L.index (left `L.append` [y]) i == y); // let Some s_3 = machine_eval_codes (y :: left') fuel s in assert (equiv_states s_3 s_12); lemma_eval_codes_equiv_states right fuel s_1 s_11; let Some s_0 = machine_eval_codes right fuel s_11 in lemma_eval_codes_equiv_states (Block ys :: right) fuel s_12 s_3; let Some s_00 = machine_eval_codes (Block ys :: right) fuel s_3 in // assert (equiv_states s_00 s'); assert (Some s_3 == machine_eval_code (Block (y :: left')) fuel s); assert (Some s_00 == machine_eval_codes (Block ys :: right) fuel s_3); lemma_machine_eval_codes_block_to_append (y :: left') (Block ys :: right) fuel s ) | InPlaceIfElse tht thf -> ( let IfElse cond c_ift c_iff :: xs = cs in let Block cs_ift, Block cs_iff = c_ift, c_iff in let (s1, b) = machine_eval_ocmp s cond in if b then ( assert (Some s' == machine_eval_codes (c_ift :: xs) fuel s1); let Some s'' = machine_eval_code c_ift fuel s1 in if not s''.ms_ok then (lemma_not_ok_propagate_codes xs fuel s'') else (); lemma_perform_reordering_with_hints tht cs_ift fuel s1; let Some s''' = machine_eval_code (IfElse cond c_ift c_iff) fuel s in let x' :: _ = cs' in let Some s'''' = machine_eval_code x' fuel s in assert (equiv_states s''' s''''); lemma_eval_codes_equiv_states xs fuel s''' s'''' ) else ( let Some s'' = machine_eval_code c_iff fuel s1 in if not s''.ms_ok then (lemma_not_ok_propagate_codes xs fuel s'') else (); lemma_perform_reordering_with_hints thf cs_iff fuel s1; let Some s''' = machine_eval_code (IfElse cond c_ift c_iff) fuel s in let x' :: _ = cs' in let Some s'''' = machine_eval_code x' fuel s in lemma_eval_codes_equiv_states xs fuel s''' s'''' ) ) | InPlaceWhile thb -> ( assert (fuel <> 0); let While cond body :: xs = cs in let Block cs_body = body in let (s0, b) = machine_eval_ocmp s cond in if not b then () else ( let Some s1 = machine_eval_code body (fuel - 1) s0 in if s1.ms_ok then () else lemma_not_ok_propagate_codes xs fuel s1; lemma_perform_reordering_with_hints thb cs_body (fuel - 1) s0; let x' :: xs' = cs' in assert (xs' == xs); let While cond' body' = x' in let cs_body' = body' in let Some s11 = machine_eval_code (While cond body) (fuel - 1) s1 in if s11.ms_ok then () else lemma_not_ok_propagate_codes xs fuel s11; assert (Some s' == machine_eval_codes xs fuel s11); lemma_perform_reordering_with_hint t [While cond body] (fuel - 1) s1; let Some s11' = machine_eval_code x' (fuel - 1) s1 in lemma_eval_codes_equiv_states xs fuel s11 s11'; let Some s'' = machine_eval_codes xs fuel s11' in assert (machine_eval_codes cs fuel s == Some s'); assert (equiv_states s' s''); let Some s1' = machine_eval_code body' (fuel - 1) s0 in lemma_eval_code_equiv_states x' (fuel-1) s1 s1'; let Some s11'' = machine_eval_code x' (fuel-1) s1' in assert (machine_eval_codes cs' fuel s == machine_eval_codes xs fuel s11''); lemma_eval_codes_equiv_states xs fuel s11' s11'' ) ) ) and lemma_perform_reordering_with_hints (ts:transformation_hints) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (requires ( (Ok? (perform_reordering_with_hints ts cs)) /\ (Some? (machine_eval_codes cs fuel s)) /\ (Some?.v (machine_eval_codes cs fuel s)).ms_ok)) (ensures ( let Ok cs' = perform_reordering_with_hints ts cs in equiv_ostates (machine_eval_codes cs fuel s) (machine_eval_codes cs' fuel s))) (decreases %[ts; fuel; cs]) = let c = cs in let Ok cs' = perform_reordering_with_hints ts cs in let Some s' = machine_eval_codes cs fuel s in match ts with | [] -> lemma_is_empty_codes cs fuel s | t :: ts' -> let Ok (x :: xs) = perform_reordering_with_hint t c in lemma_perform_reordering_with_hint t c fuel s; let Ok xs' = perform_reordering_with_hints ts' xs in let Some s1 = machine_eval_code x fuel s in lemma_perform_reordering_with_hints ts' xs fuel s1 #pop-options /// Some convenience functions to be run before the pass, to ensure /// that we don't have miscounting due to empty code.
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Vale.X64.Machine_Semantics_s.codes -> Vale.X64.Machine_Semantics_s.codes
Prims.Tot
[ "total" ]
[ "purge_empty_code", "purge_empty_codes" ]
[ "Vale.X64.Machine_Semantics_s.codes", "Prims.Nil", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "Vale.Transformers.InstructionReorder.is_empty_code", "Vale.Transformers.InstructionReorder.purge_empty_codes", "Prims.bool", "Prims.Cons", "Vale.Transformers.InstructionReorder.purge_empty_code" ]
[ "mutual recursion" ]
false
false
false
true
false
let rec purge_empty_codes (cs: codes) : codes =
match cs with | [] -> [] | x :: xs -> if is_empty_code x then (purge_empty_codes xs) else (purge_empty_code x :: purge_empty_codes xs)
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.metric_for_code
val metric_for_code (c: code) : GTot nat
val metric_for_code (c: code) : GTot nat
let rec metric_for_code (c:code) : GTot nat = 1 + ( match c with | Ins _ -> 0 | Block l -> metric_for_codes l | IfElse _ t f -> metric_for_code t + metric_for_code f | While _ b -> metric_for_code b ) and metric_for_codes (c:codes) : GTot nat = match c with | [] -> 0 | x :: xs -> 1 + metric_for_code x + metric_for_codes xs
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 58, "end_line": 1780, "start_col": 0, "start_line": 1768 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs let increment_hint (th:transformation_hint) : transformation_hint = match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th let rec find_deep_code_transform (c:code) (cs:codes) : possibly transformation_hint = match cs with | [] -> Err ("Not found (during find_deep_code_transform): " ^ fst (print_code c 0 gcc)) | x :: xs -> (* let _ = IO.debug_print_string ( "---------------------------------\n" ^ " c : \n" ^ fst (print_code c 0 gcc) ^ "\n" ^ " x : \n" ^ fst (print_code x 0 gcc) ^ "\n" ^ " xs : \n" ^ fst (print_code (Block xs) 0 gcc) ^ "\n" ^ "---------------------------------\n" ^ "") in *) if is_empty_code x then find_deep_code_transform c xs else ( if eq_codes (fully_unblocked_code x) (fully_unblocked_code c) then ( return (MoveUpFrom 0) ) else ( match x with | Block l -> ( match find_deep_code_transform c l with | Ok t -> return (DiveInAt 0 t) | Err reason -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) | _ -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) )
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code -> Prims.GTot Prims.nat
Prims.GTot
[ "sometrivial" ]
[ "metric_for_code", "metric_for_codes" ]
[ "Vale.X64.Machine_Semantics_s.code", "Prims.op_Addition", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "Vale.X64.Machine_s.precode", "Vale.X64.Bytes_Code_s.ocmp", "Vale.Transformers.InstructionReorder.metric_for_codes", "Vale.Transformers.InstructionReorder.metric_for_code", "Prims.int", "Prims.nat" ]
[ "mutual recursion" ]
false
false
false
false
false
let rec metric_for_code (c: code) : GTot nat =
1 + (match c with | Ins _ -> 0 | Block l -> metric_for_codes l | IfElse _ t f -> metric_for_code t + metric_for_code f | While _ b -> metric_for_code b)
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.fully_unblocked_codes
val fully_unblocked_codes (c: codes) : codes
val fully_unblocked_codes (c: codes) : codes
let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 62, "end_line": 1724, "start_col": 0, "start_line": 1713 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.codes -> Vale.X64.Machine_Semantics_s.codes
Prims.Tot
[ "total" ]
[ "fully_unblocked_code", "fully_unblocked_codes" ]
[ "Vale.X64.Machine_Semantics_s.codes", "Prims.Nil", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "FStar.List.Tot.Base.append", "Vale.Transformers.InstructionReorder.fully_unblocked_code", "Vale.Transformers.InstructionReorder.fully_unblocked_codes" ]
[ "mutual recursion" ]
false
false
false
true
false
let rec fully_unblocked_codes (c: codes) : codes =
match c with | [] -> [] | x :: xs -> (fully_unblocked_code x) `L.append` (fully_unblocked_codes xs)
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.find_deep_code_transform
val find_deep_code_transform (c: code) (cs: codes) : possibly transformation_hint
val find_deep_code_transform (c: code) (cs: codes) : possibly transformation_hint
let rec find_deep_code_transform (c:code) (cs:codes) : possibly transformation_hint = match cs with | [] -> Err ("Not found (during find_deep_code_transform): " ^ fst (print_code c 0 gcc)) | x :: xs -> (* let _ = IO.debug_print_string ( "---------------------------------\n" ^ " c : \n" ^ fst (print_code c 0 gcc) ^ "\n" ^ " x : \n" ^ fst (print_code x 0 gcc) ^ "\n" ^ " xs : \n" ^ fst (print_code (Block xs) 0 gcc) ^ "\n" ^ "---------------------------------\n" ^ "") in *) if is_empty_code x then find_deep_code_transform c xs else ( if eq_codes (fully_unblocked_code x) (fully_unblocked_code c) then ( return (MoveUpFrom 0) ) else ( match x with | Block l -> ( match find_deep_code_transform c l with | Ok t -> return (DiveInAt 0 t) | Err reason -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) | _ -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 1766, "start_col": 0, "start_line": 1732 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs let increment_hint (th:transformation_hint) : transformation_hint = match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code -> cs: Vale.X64.Machine_Semantics_s.codes -> Vale.Def.PossiblyMonad.possibly Vale.Transformers.InstructionReorder.transformation_hint
Prims.Tot
[ "total" ]
[]
[ "Vale.X64.Machine_Semantics_s.code", "Vale.X64.Machine_Semantics_s.codes", "Vale.Def.PossiblyMonad.Err", "Vale.Transformers.InstructionReorder.transformation_hint", "Prims.op_Hat", "FStar.Pervasives.Native.fst", "Prims.string", "Prims.int", "Vale.X64.Print_s.print_code", "Vale.X64.Print_s.gcc", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "Vale.Transformers.InstructionReorder.is_empty_code", "Vale.Transformers.InstructionReorder.find_deep_code_transform", "Prims.bool", "Vale.Transformers.InstructionReorder.eq_codes", "Vale.Transformers.InstructionReorder.fully_unblocked_code", "Vale.Def.PossiblyMonad.return", "Vale.Transformers.InstructionReorder.MoveUpFrom", "Vale.X64.Machine_s.precode", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Bytes_Code_s.ocmp", "Vale.Transformers.InstructionReorder.DiveInAt", "Vale.Def.PossiblyMonad.op_let_Plus", "Vale.Transformers.InstructionReorder.increment_hint", "Vale.Def.PossiblyMonad.possibly" ]
[ "recursion" ]
false
false
false
true
false
let rec find_deep_code_transform (c: code) (cs: codes) : possibly transformation_hint =
match cs with | [] -> Err ("Not found (during find_deep_code_transform): " ^ fst (print_code c 0 gcc)) | x :: xs -> if is_empty_code x then find_deep_code_transform c xs else (if eq_codes (fully_unblocked_code x) (fully_unblocked_code c) then (return (MoveUpFrom 0)) else (match x with | Block l -> (match find_deep_code_transform c l with | Ok t -> return (DiveInAt 0 t) | Err reason -> let+ th = find_deep_code_transform c xs in return (increment_hint th)) | _ -> let+ th = find_deep_code_transform c xs in return (increment_hint th)))
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.purge_empty_code
val purge_empty_code (c: code) : code
val purge_empty_code (c: code) : code
let rec purge_empty_code (c:code) : code = match c with | Block l -> Block (purge_empty_codes l) | IfElse c t f -> IfElse c (purge_empty_code t) (purge_empty_code f) | While c b -> While c (purge_empty_code b) | _ -> c and purge_empty_codes (cs:codes) : codes = match cs with | [] -> [] | x :: xs -> if is_empty_code x then ( purge_empty_codes xs ) else ( purge_empty_code x :: purge_empty_codes xs )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 2151, "start_col": 0, "start_line": 2132 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs let increment_hint (th:transformation_hint) : transformation_hint = match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th let rec find_deep_code_transform (c:code) (cs:codes) : possibly transformation_hint = match cs with | [] -> Err ("Not found (during find_deep_code_transform): " ^ fst (print_code c 0 gcc)) | x :: xs -> (* let _ = IO.debug_print_string ( "---------------------------------\n" ^ " c : \n" ^ fst (print_code c 0 gcc) ^ "\n" ^ " x : \n" ^ fst (print_code x 0 gcc) ^ "\n" ^ " xs : \n" ^ fst (print_code (Block xs) 0 gcc) ^ "\n" ^ "---------------------------------\n" ^ "") in *) if is_empty_code x then find_deep_code_transform c xs else ( if eq_codes (fully_unblocked_code x) (fully_unblocked_code c) then ( return (MoveUpFrom 0) ) else ( match x with | Block l -> ( match find_deep_code_transform c l with | Ok t -> return (DiveInAt 0 t) | Err reason -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) | _ -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) ) let rec metric_for_code (c:code) : GTot nat = 1 + ( match c with | Ins _ -> 0 | Block l -> metric_for_codes l | IfElse _ t f -> metric_for_code t + metric_for_code f | While _ b -> metric_for_code b ) and metric_for_codes (c:codes) : GTot nat = match c with | [] -> 0 | x :: xs -> 1 + metric_for_code x + metric_for_codes xs let rec lemma_metric_for_codes_append (c1 c2:codes) : Lemma (ensures (metric_for_codes (c1 `L.append` c2) == metric_for_codes c1 + metric_for_codes c2)) [SMTPat (metric_for_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_metric_for_codes_append xs c2 irreducible (* Our proofs do not depend on how the hints are found. As long as some hints are provided, we validate the hints to perform the transformation and use it. Thus, we make this function [irreducible] to explicitly prevent any of the proofs from reasoning about it. *) let rec find_transformation_hints (c1 c2:codes) : Tot (possibly transformation_hints) (decreases %[metric_for_codes c2; metric_for_codes c1]) = let e1, e2 = is_empty_codes c1, is_empty_codes c2 in if e1 && e2 then ( return [] ) else if e2 then ( Err ("non empty first code: " ^ fst (print_code (Block c1) 0 gcc)) ) else if e1 then ( Err ("non empty second code: " ^ fst (print_code (Block c2) 0 gcc)) ) else ( let h1 :: t1 = c1 in let h2 :: t2 = c2 in assert (metric_for_codes c2 >= metric_for_code h2); (* OBSERVE *) if is_empty_code h1 then ( find_transformation_hints t1 c2 ) else if is_empty_code h2 then ( find_transformation_hints c1 t2 ) else ( match find_deep_code_transform h2 c1 with | Ok th -> ( match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Unable to find valid movement for : " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | Err reason -> ( let h1 :: t1 = c1 in match h1, h2 with | Block l1, Block l2 -> ( match ( let+ t_hints1 = find_transformation_hints l1 l2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (wrap_diveinat 0 t_hints1 `L.append` t_hints2) ) with | Ok ths -> return ths | Err reason -> find_transformation_hints c1 (l2 `L.append` t2) ) | IfElse co1 (Block tr1) (Block fa1), IfElse co2 (Block tr2) (Block fa2) -> (co1 = co2) /- ("Non-same conditions for IfElse: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block tr2)); (* OBSERVE *) assert (metric_for_code h2 > metric_for_code (Block fa2)); (* OBSERVE *) let+ tr_hints = find_transformation_hints tr1 tr2 in let+ fa_hints = find_transformation_hints fa1 fa2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceIfElse tr_hints fa_hints :: t_hints2) | While co1 (Block bo1), While co2 (Block bo2) -> (co1 = co2) /- ("Non-same conditions for While: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block bo2)); (* OBSERVE *) let+ bo_hints = find_transformation_hints bo1 bo2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceWhile bo_hints :: t_hints2) | Block l1, IfElse _ _ _ | Block l1, While _ _ -> assert (metric_for_codes (l1 `L.append` t1) == metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) assert_norm (metric_for_codes c1 == 2 + metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) let+ t_hints1 = find_transformation_hints (l1 `L.append` t1) c2 in ( match t_hints1 with | [] -> Err "Impossible" | th :: _ -> let th = DiveInAt 0 th in match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Failed during left-unblock for " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | _, Block l2 -> find_transformation_hints c1 (l2 `L.append` t2) | IfElse _ _ _, IfElse _ _ _ | While _ _, While _ _ -> Err ("Found weird non-standard code: " ^ fst (print_code h1 0 gcc)) | _ -> Err ("Find deep code failure. Reason: " ^ reason) ) ) ) /// If a transformation can be performed, then the result behaves /// identically as per the [equiv_states] relation. #push-options "--z3rlimit 10 --initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_bubble_to_top (cs : codes) (i:nat{i < L.length cs}) (fuel:nat) (s s' : machine_state) : Lemma (requires ( (s'.ms_ok) /\ (Some s' == machine_eval_codes cs fuel s) /\ (Ok? (bubble_to_top cs i)))) (ensures ( let x = L.index cs i in let Ok xs = bubble_to_top cs i in let s1' = machine_eval_code x fuel s in (Some? s1') /\ ( let Some s1 = s1' in let s2' = machine_eval_codes xs fuel s1 in (Some? s2') /\ ( let Some s2 = s2' in equiv_states s' s2)))) = match cs with | [_] -> () | h :: t -> let x = L.index cs i in let Ok xs = bubble_to_top cs i in if i = 0 then () else ( let Some s_h = machine_eval_code h fuel s in lemma_bubble_to_top (L.tl cs) (i-1) fuel s_h s'; let Some s_h_x = machine_eval_code x fuel s_h in let Some s_hx = machine_eval_codes [h;x] fuel s in assert (s_h_x == s_hx); lemma_code_exchange_allowed x h fuel s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes (L.tl xs) fuel) s_hx; assert (s_hx.ms_ok); let Some s_xh = machine_eval_codes [x;h] fuel s in lemma_eval_codes_equiv_states (L.tl xs) fuel s_hx s_xh ) #pop-options #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_machine_eval_codes_block_to_append (c1 c2 : codes) (fuel:nat) (s:machine_state) : Lemma (ensures (machine_eval_codes (c1 `L.append` c2) fuel s == machine_eval_codes (Block c1 :: c2) fuel s)) = match c1 with | [] -> () | x :: xs -> match machine_eval_code x fuel s with | None -> () | Some s1 -> lemma_machine_eval_codes_block_to_append xs c2 fuel s1 #pop-options let rec lemma_append_single (xs:list 'a) (y:'a) (i:nat) : Lemma (requires (i == L.length xs)) (ensures ( L.length (xs `L.append` [y]) = L.length xs + 1 /\ L.index (xs `L.append` [y]) i == y)) = match xs with | [] -> () | x :: xs -> lemma_append_single xs y (i - 1) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_is_empty_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (is_empty_code c)) (ensures ((machine_eval_code c fuel s) == (machine_eval_codes [] fuel s))) = match c with | Ins _ -> () | Block l -> lemma_is_empty_codes l fuel s | IfElse _ t f -> () | While _ c -> () and lemma_is_empty_codes (cs:codes) (fuel:nat) (s:machine_state) : Lemma (requires (is_empty_codes cs)) (ensures ((machine_eval_codes cs fuel s) == (machine_eval_codes [] fuel s))) = match cs with | [] -> () | x :: xs -> lemma_is_empty_code x fuel s; lemma_is_empty_codes xs fuel s #pop-options #restart-solver #push-options "--z3rlimit 100 --initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_perform_reordering_with_hint (t:transformation_hint) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (requires ( (Ok? (perform_reordering_with_hint t cs)) /\ (Some? (machine_eval_codes cs fuel s)) /\ (Some?.v (machine_eval_codes cs fuel s)).ms_ok)) (ensures ( let Ok cs' = perform_reordering_with_hint t cs in equiv_ostates (machine_eval_codes cs fuel s) (machine_eval_codes cs' fuel s))) (decreases %[t; fuel; cs]) = let c = cs in let Ok cs' = perform_reordering_with_hint t cs in let Some s' = machine_eval_codes cs fuel s in let x :: xs = cs in if is_empty_codes [x] then ( lemma_is_empty_codes [x] fuel s; match machine_eval_code x fuel s with | None -> () | Some s' -> lemma_eval_codes_equiv_states xs fuel s s'; lemma_perform_reordering_with_hint t xs fuel s ) else ( match t with | MoveUpFrom i -> ( let Ok c' = bubble_to_top c i in lemma_bubble_to_top c i fuel s s' ) | DiveInAt i t' -> ( FStar.List.Pure.lemma_split3_append c i; FStar.List.Pure.lemma_split3_length c i; let left, mid, right = L.split3 c i in let Block l = mid in let Ok (y :: ys) = perform_reordering_with_hint t' l in L.append_length left [y]; let Ok left' = bubble_to_top (left `L.append` [y]) i in // assert (cs' == y :: (left' `L.append` (Block ys :: right))); assert (left `L.append` (mid :: right) == c); L.append_l_cons mid right left; assert ((left `L.append` [mid]) `L.append` right == c); lemma_machine_eval_codes_block_to_append (left `L.append` [mid]) right fuel s; let Some s_1 = machine_eval_code (Block (left `L.append` [mid])) fuel s in assert (Some s_1 == machine_eval_codes (left `L.append` [mid]) fuel s); lemma_machine_eval_codes_block_to_append left [mid] fuel s; let Some s_2 = machine_eval_code (Block left) fuel s in assert (Some s_2 == machine_eval_codes left fuel s); // assert (Some s_1 == machine_eval_codes [mid] fuel s_2); assert (Some s_1 == machine_eval_code (Block l) fuel s_2); assert (Some s_1 == machine_eval_codes l fuel s_2); assert (Some s' == machine_eval_codes right fuel s_1); if s_1.ms_ok then () else lemma_not_ok_propagate_codes right fuel s_1; lemma_perform_reordering_with_hint t' l fuel s_2; // let Some s_11 = machine_eval_codes (y :: ys) fuel s_2 in let Some s_12 = machine_eval_code y fuel s_2 in if s_12.ms_ok then () else lemma_not_ok_propagate_codes ys fuel s_12; assert (Some s_2 == machine_eval_codes left fuel s); assert (Some s_2 == machine_eval_code (Block left) fuel s); assert (Some s_12 == machine_eval_codes (Block left :: [y]) fuel s); lemma_machine_eval_codes_block_to_append left [y] fuel s; assert (Some s_12 == machine_eval_codes (left `L.append` [y]) fuel s); lemma_bubble_to_top (left `L.append` [y]) i fuel s s_12; // lemma_append_single left y i; assert (L.index (left `L.append` [y]) i == y); // let Some s_3 = machine_eval_codes (y :: left') fuel s in assert (equiv_states s_3 s_12); lemma_eval_codes_equiv_states right fuel s_1 s_11; let Some s_0 = machine_eval_codes right fuel s_11 in lemma_eval_codes_equiv_states (Block ys :: right) fuel s_12 s_3; let Some s_00 = machine_eval_codes (Block ys :: right) fuel s_3 in // assert (equiv_states s_00 s'); assert (Some s_3 == machine_eval_code (Block (y :: left')) fuel s); assert (Some s_00 == machine_eval_codes (Block ys :: right) fuel s_3); lemma_machine_eval_codes_block_to_append (y :: left') (Block ys :: right) fuel s ) | InPlaceIfElse tht thf -> ( let IfElse cond c_ift c_iff :: xs = cs in let Block cs_ift, Block cs_iff = c_ift, c_iff in let (s1, b) = machine_eval_ocmp s cond in if b then ( assert (Some s' == machine_eval_codes (c_ift :: xs) fuel s1); let Some s'' = machine_eval_code c_ift fuel s1 in if not s''.ms_ok then (lemma_not_ok_propagate_codes xs fuel s'') else (); lemma_perform_reordering_with_hints tht cs_ift fuel s1; let Some s''' = machine_eval_code (IfElse cond c_ift c_iff) fuel s in let x' :: _ = cs' in let Some s'''' = machine_eval_code x' fuel s in assert (equiv_states s''' s''''); lemma_eval_codes_equiv_states xs fuel s''' s'''' ) else ( let Some s'' = machine_eval_code c_iff fuel s1 in if not s''.ms_ok then (lemma_not_ok_propagate_codes xs fuel s'') else (); lemma_perform_reordering_with_hints thf cs_iff fuel s1; let Some s''' = machine_eval_code (IfElse cond c_ift c_iff) fuel s in let x' :: _ = cs' in let Some s'''' = machine_eval_code x' fuel s in lemma_eval_codes_equiv_states xs fuel s''' s'''' ) ) | InPlaceWhile thb -> ( assert (fuel <> 0); let While cond body :: xs = cs in let Block cs_body = body in let (s0, b) = machine_eval_ocmp s cond in if not b then () else ( let Some s1 = machine_eval_code body (fuel - 1) s0 in if s1.ms_ok then () else lemma_not_ok_propagate_codes xs fuel s1; lemma_perform_reordering_with_hints thb cs_body (fuel - 1) s0; let x' :: xs' = cs' in assert (xs' == xs); let While cond' body' = x' in let cs_body' = body' in let Some s11 = machine_eval_code (While cond body) (fuel - 1) s1 in if s11.ms_ok then () else lemma_not_ok_propagate_codes xs fuel s11; assert (Some s' == machine_eval_codes xs fuel s11); lemma_perform_reordering_with_hint t [While cond body] (fuel - 1) s1; let Some s11' = machine_eval_code x' (fuel - 1) s1 in lemma_eval_codes_equiv_states xs fuel s11 s11'; let Some s'' = machine_eval_codes xs fuel s11' in assert (machine_eval_codes cs fuel s == Some s'); assert (equiv_states s' s''); let Some s1' = machine_eval_code body' (fuel - 1) s0 in lemma_eval_code_equiv_states x' (fuel-1) s1 s1'; let Some s11'' = machine_eval_code x' (fuel-1) s1' in assert (machine_eval_codes cs' fuel s == machine_eval_codes xs fuel s11''); lemma_eval_codes_equiv_states xs fuel s11' s11'' ) ) ) and lemma_perform_reordering_with_hints (ts:transformation_hints) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (requires ( (Ok? (perform_reordering_with_hints ts cs)) /\ (Some? (machine_eval_codes cs fuel s)) /\ (Some?.v (machine_eval_codes cs fuel s)).ms_ok)) (ensures ( let Ok cs' = perform_reordering_with_hints ts cs in equiv_ostates (machine_eval_codes cs fuel s) (machine_eval_codes cs' fuel s))) (decreases %[ts; fuel; cs]) = let c = cs in let Ok cs' = perform_reordering_with_hints ts cs in let Some s' = machine_eval_codes cs fuel s in match ts with | [] -> lemma_is_empty_codes cs fuel s | t :: ts' -> let Ok (x :: xs) = perform_reordering_with_hint t c in lemma_perform_reordering_with_hint t c fuel s; let Ok xs' = perform_reordering_with_hints ts' xs in let Some s1 = machine_eval_code x fuel s in lemma_perform_reordering_with_hints ts' xs fuel s1 #pop-options /// Some convenience functions to be run before the pass, to ensure /// that we don't have miscounting due to empty code.
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code -> Vale.X64.Machine_Semantics_s.code
Prims.Tot
[ "total" ]
[ "purge_empty_code", "purge_empty_codes" ]
[ "Vale.X64.Machine_Semantics_s.code", "Prims.list", "Vale.X64.Machine_s.precode", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.X64.Bytes_Code_s.ocmp", "Vale.X64.Machine_s.Block", "Vale.Transformers.InstructionReorder.purge_empty_codes", "Vale.X64.Machine_s.IfElse", "Vale.Transformers.InstructionReorder.purge_empty_code", "Vale.X64.Machine_s.While" ]
[ "mutual recursion" ]
false
false
false
true
false
let rec purge_empty_code (c: code) : code =
match c with | Block l -> Block (purge_empty_codes l) | IfElse c t f -> IfElse c (purge_empty_code t) (purge_empty_code f) | While c b -> While c (purge_empty_code b) | _ -> c
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_append_single
val lemma_append_single (xs: list 'a) (y: 'a) (i: nat) : Lemma (requires (i == L.length xs)) (ensures (L.length (xs `L.append` [y]) = L.length xs + 1 /\ L.index (xs `L.append` [y]) i == y))
val lemma_append_single (xs: list 'a) (y: 'a) (i: nat) : Lemma (requires (i == L.length xs)) (ensures (L.length (xs `L.append` [y]) = L.length xs + 1 /\ L.index (xs `L.append` [y]) i == y))
let rec lemma_append_single (xs:list 'a) (y:'a) (i:nat) : Lemma (requires (i == L.length xs)) (ensures ( L.length (xs `L.append` [y]) = L.length xs + 1 /\ L.index (xs `L.append` [y]) i == y)) = match xs with | [] -> () | x :: xs -> lemma_append_single xs y (i - 1)
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 47, "end_line": 1943, "start_col": 0, "start_line": 1935 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs let increment_hint (th:transformation_hint) : transformation_hint = match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th let rec find_deep_code_transform (c:code) (cs:codes) : possibly transformation_hint = match cs with | [] -> Err ("Not found (during find_deep_code_transform): " ^ fst (print_code c 0 gcc)) | x :: xs -> (* let _ = IO.debug_print_string ( "---------------------------------\n" ^ " c : \n" ^ fst (print_code c 0 gcc) ^ "\n" ^ " x : \n" ^ fst (print_code x 0 gcc) ^ "\n" ^ " xs : \n" ^ fst (print_code (Block xs) 0 gcc) ^ "\n" ^ "---------------------------------\n" ^ "") in *) if is_empty_code x then find_deep_code_transform c xs else ( if eq_codes (fully_unblocked_code x) (fully_unblocked_code c) then ( return (MoveUpFrom 0) ) else ( match x with | Block l -> ( match find_deep_code_transform c l with | Ok t -> return (DiveInAt 0 t) | Err reason -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) | _ -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) ) let rec metric_for_code (c:code) : GTot nat = 1 + ( match c with | Ins _ -> 0 | Block l -> metric_for_codes l | IfElse _ t f -> metric_for_code t + metric_for_code f | While _ b -> metric_for_code b ) and metric_for_codes (c:codes) : GTot nat = match c with | [] -> 0 | x :: xs -> 1 + metric_for_code x + metric_for_codes xs let rec lemma_metric_for_codes_append (c1 c2:codes) : Lemma (ensures (metric_for_codes (c1 `L.append` c2) == metric_for_codes c1 + metric_for_codes c2)) [SMTPat (metric_for_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_metric_for_codes_append xs c2 irreducible (* Our proofs do not depend on how the hints are found. As long as some hints are provided, we validate the hints to perform the transformation and use it. Thus, we make this function [irreducible] to explicitly prevent any of the proofs from reasoning about it. *) let rec find_transformation_hints (c1 c2:codes) : Tot (possibly transformation_hints) (decreases %[metric_for_codes c2; metric_for_codes c1]) = let e1, e2 = is_empty_codes c1, is_empty_codes c2 in if e1 && e2 then ( return [] ) else if e2 then ( Err ("non empty first code: " ^ fst (print_code (Block c1) 0 gcc)) ) else if e1 then ( Err ("non empty second code: " ^ fst (print_code (Block c2) 0 gcc)) ) else ( let h1 :: t1 = c1 in let h2 :: t2 = c2 in assert (metric_for_codes c2 >= metric_for_code h2); (* OBSERVE *) if is_empty_code h1 then ( find_transformation_hints t1 c2 ) else if is_empty_code h2 then ( find_transformation_hints c1 t2 ) else ( match find_deep_code_transform h2 c1 with | Ok th -> ( match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Unable to find valid movement for : " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | Err reason -> ( let h1 :: t1 = c1 in match h1, h2 with | Block l1, Block l2 -> ( match ( let+ t_hints1 = find_transformation_hints l1 l2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (wrap_diveinat 0 t_hints1 `L.append` t_hints2) ) with | Ok ths -> return ths | Err reason -> find_transformation_hints c1 (l2 `L.append` t2) ) | IfElse co1 (Block tr1) (Block fa1), IfElse co2 (Block tr2) (Block fa2) -> (co1 = co2) /- ("Non-same conditions for IfElse: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block tr2)); (* OBSERVE *) assert (metric_for_code h2 > metric_for_code (Block fa2)); (* OBSERVE *) let+ tr_hints = find_transformation_hints tr1 tr2 in let+ fa_hints = find_transformation_hints fa1 fa2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceIfElse tr_hints fa_hints :: t_hints2) | While co1 (Block bo1), While co2 (Block bo2) -> (co1 = co2) /- ("Non-same conditions for While: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block bo2)); (* OBSERVE *) let+ bo_hints = find_transformation_hints bo1 bo2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceWhile bo_hints :: t_hints2) | Block l1, IfElse _ _ _ | Block l1, While _ _ -> assert (metric_for_codes (l1 `L.append` t1) == metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) assert_norm (metric_for_codes c1 == 2 + metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) let+ t_hints1 = find_transformation_hints (l1 `L.append` t1) c2 in ( match t_hints1 with | [] -> Err "Impossible" | th :: _ -> let th = DiveInAt 0 th in match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Failed during left-unblock for " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | _, Block l2 -> find_transformation_hints c1 (l2 `L.append` t2) | IfElse _ _ _, IfElse _ _ _ | While _ _, While _ _ -> Err ("Found weird non-standard code: " ^ fst (print_code h1 0 gcc)) | _ -> Err ("Find deep code failure. Reason: " ^ reason) ) ) ) /// If a transformation can be performed, then the result behaves /// identically as per the [equiv_states] relation. #push-options "--z3rlimit 10 --initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_bubble_to_top (cs : codes) (i:nat{i < L.length cs}) (fuel:nat) (s s' : machine_state) : Lemma (requires ( (s'.ms_ok) /\ (Some s' == machine_eval_codes cs fuel s) /\ (Ok? (bubble_to_top cs i)))) (ensures ( let x = L.index cs i in let Ok xs = bubble_to_top cs i in let s1' = machine_eval_code x fuel s in (Some? s1') /\ ( let Some s1 = s1' in let s2' = machine_eval_codes xs fuel s1 in (Some? s2') /\ ( let Some s2 = s2' in equiv_states s' s2)))) = match cs with | [_] -> () | h :: t -> let x = L.index cs i in let Ok xs = bubble_to_top cs i in if i = 0 then () else ( let Some s_h = machine_eval_code h fuel s in lemma_bubble_to_top (L.tl cs) (i-1) fuel s_h s'; let Some s_h_x = machine_eval_code x fuel s_h in let Some s_hx = machine_eval_codes [h;x] fuel s in assert (s_h_x == s_hx); lemma_code_exchange_allowed x h fuel s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes (L.tl xs) fuel) s_hx; assert (s_hx.ms_ok); let Some s_xh = machine_eval_codes [x;h] fuel s in lemma_eval_codes_equiv_states (L.tl xs) fuel s_hx s_xh ) #pop-options #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_machine_eval_codes_block_to_append (c1 c2 : codes) (fuel:nat) (s:machine_state) : Lemma (ensures (machine_eval_codes (c1 `L.append` c2) fuel s == machine_eval_codes (Block c1 :: c2) fuel s)) = match c1 with | [] -> () | x :: xs -> match machine_eval_code x fuel s with | None -> () | Some s1 -> lemma_machine_eval_codes_block_to_append xs c2 fuel s1 #pop-options
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
xs: Prims.list 'a -> y: 'a -> i: Prims.nat -> FStar.Pervasives.Lemma (requires i == FStar.List.Tot.Base.length xs) (ensures FStar.List.Tot.Base.length (xs @ [y]) = FStar.List.Tot.Base.length xs + 1 /\ FStar.List.Tot.Base.index (xs @ [y]) i == y)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.list", "Prims.nat", "Vale.Transformers.InstructionReorder.lemma_append_single", "Prims.op_Subtraction", "Prims.unit", "Prims.eq2", "FStar.List.Tot.Base.length", "Prims.squash", "Prims.l_and", "Prims.b2t", "Prims.op_Equality", "Prims.int", "FStar.List.Tot.Base.append", "Prims.Cons", "Prims.Nil", "Prims.op_Addition", "FStar.List.Tot.Base.index", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_append_single (xs: list 'a) (y: 'a) (i: nat) : Lemma (requires (i == L.length xs)) (ensures (L.length (xs `L.append` [y]) = L.length xs + 1 /\ L.index (xs `L.append` [y]) i == y)) =
match xs with | [] -> () | x :: xs -> lemma_append_single xs y (i - 1)
false
Hacl.Test.ECDSA.fst
Hacl.Test.ECDSA.test_sigver384
val test_sigver384 (vec: sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True)
val test_sigver384 (vec: sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True)
let test_sigver384 (vec:sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha384 msg_len msg qxy r s in if result' = result then () else begin LowStar.Printf.(printf "FAIL\n" done); C.exit 1l end; pop_frame() end
{ "file_name": "code/tests/Hacl.Test.ECDSA.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 7, "end_line": 162, "start_col": 0, "start_line": 131 }
module Hacl.Test.ECDSA open FStar.HyperStack.ST open Test.Lowstarize open Lib.IntTypes open Hacl.P256 open Spec.ECDSA.Test.Vectors module L = Test.Lowstarize module B = LowStar.Buffer #set-options "--fuel 0 --ifuel 0 --z3rlimit 100" noextract let sigver_vectors256_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_256 noextract let sigver_vectors384_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_384 noextract let sigver_vectors512_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_512 noextract let siggen_vectors256_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_256 noextract let siggen_vectors384_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_384 noextract let siggen_vectors512_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_512 %splice[sigver_vectors256_low] (lowstarize_toplevel "sigver_vectors256_tmp" "sigver_vectors256_low") %splice[sigver_vectors384_low] (lowstarize_toplevel "sigver_vectors384_tmp" "sigver_vectors384_low") %splice[sigver_vectors512_low] (lowstarize_toplevel "sigver_vectors512_tmp" "sigver_vectors512_low") %splice[siggen_vectors256_low] (lowstarize_toplevel "siggen_vectors256_tmp" "siggen_vectors256_low") %splice[siggen_vectors384_low] (lowstarize_toplevel "siggen_vectors384_tmp" "siggen_vectors384_low") %splice[siggen_vectors512_low] (lowstarize_toplevel "siggen_vectors512_tmp" "siggen_vectors512_low") // Cheap alternative to friend Lib.IntTypes needed because Test.Lowstarize uses UInt8.t assume val declassify_uint8: squash (uint8 == UInt8.t) let vec8 = L.lbuffer UInt8.t let sigver_vector = vec8 & vec8 & vec8 & vec8 & vec8 & bool let siggen_vector = vec8 & vec8 & vec8 & vec8 & vec8 & vec8 & vec8 // This could replace TestLib.compare_and_print val compare_and_print: b1:B.buffer UInt8.t -> b2:B.buffer UInt8.t -> len:UInt32.t -> Stack bool (requires fun h0 -> B.live h0 b1 /\ B.live h0 b2 /\ B.length b1 == v len /\ B.length b2 == v len) (ensures fun h0 _ h1 -> B.modifies B.loc_none h0 h1) let compare_and_print b1 b2 len = push_frame(); LowStar.Printf.(printf "Expected: %xuy\n" len b1 done); LowStar.Printf.(printf "Computed: %xuy\n" len b2 done); let b = Lib.ByteBuffer.lbytes_eq #len b1 b2 in if b then LowStar.Printf.(printf "PASS\n" done) else LowStar.Printf.(printf "FAIL\n" done); pop_frame(); b let test_sigver256 (vec:sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha2 msg_len msg qxy r s in if result' = result then () else begin LowStar.Printf.(printf "FAIL\n" done); C.exit 1l end; pop_frame() end
{ "checked_file": "/", "dependencies": [ "Test.Lowstarize.fst.checked", "Spec.P256.fst.checked", "Spec.ECDSA.Test.Vectors.fst.checked", "prims.fst.checked", "LowStar.Printf.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "Lib.ByteBuffer.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.P256.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Int32.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "C.String.fsti.checked", "C.Loops.fst.checked", "C.fst.checked" ], "interface_file": false, "source_file": "Hacl.Test.ECDSA.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "Test.Lowstarize", "short_module": "L" }, { "abbrev": false, "full_module": "Spec.ECDSA.Test.Vectors", "short_module": null }, { "abbrev": false, "full_module": "Hacl.P256", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Test.Lowstarize", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
vec: Hacl.Test.ECDSA.sigver_vector -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "Hacl.Test.ECDSA.sigver_vector", "FStar.UInt32.t", "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.l_and", "Prims.eq2", "LowStar.Monotonic.Buffer.len", "LowStar.Buffer.trivial_preorder", "LowStar.Monotonic.Buffer.recallable", "Prims.bool", "Prims.op_Negation", "Prims.op_AmpAmp", "Prims.op_Equality", "FStar.UInt32.__uint_to_t", "C.exit", "FStar.Int32.__int_to_t", "Prims.unit", "FStar.HyperStack.ST.pop_frame", "LowStar.Printf.printf", "LowStar.Printf.done", "Hacl.P256.ecdsa_verif_p256_sha384", "LowStar.Monotonic.Buffer.blit", "LowStar.Monotonic.Buffer.mbuffer", "Prims.nat", "LowStar.Monotonic.Buffer.length", "FStar.UInt32.v", "FStar.UInt32.uint_to_t", "Prims.b2t", "LowStar.Monotonic.Buffer.g_is_null", "LowStar.Buffer.alloca", "Lib.IntTypes.u8", "FStar.HyperStack.ST.push_frame", "LowStar.Monotonic.Buffer.recall", "Prims.int", "FStar.Monotonic.HyperStack.mem", "Prims.l_True" ]
[]
false
true
false
false
false
let test_sigver384 (vec: sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) =
let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else (push_frame (); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha384 msg_len msg qxy r s in if result' = result then () else ((let open LowStar.Printf in printf "FAIL\n" done); C.exit 1l); pop_frame ())
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_purge_empty_code
val lemma_purge_empty_code (c: code) (fuel: nat) (s: machine_state) : Lemma (ensures (machine_eval_code c fuel s == machine_eval_code (purge_empty_code c) fuel s)) (decreases %[fuel;c;1])
val lemma_purge_empty_code (c: code) (fuel: nat) (s: machine_state) : Lemma (ensures (machine_eval_code c fuel s == machine_eval_code (purge_empty_code c) fuel s)) (decreases %[fuel;c;1])
let rec lemma_purge_empty_code (c:code) (fuel:nat) (s:machine_state) : Lemma (ensures (machine_eval_code c fuel s == machine_eval_code (purge_empty_code c) fuel s)) (decreases %[fuel; c; 1]) = match c with | Block l -> lemma_purge_empty_codes l fuel s | IfElse c t f -> let (s', b) = machine_eval_ocmp s c in if b then lemma_purge_empty_code t fuel s' else lemma_purge_empty_code f fuel s' | While _ _ -> lemma_purge_empty_while c fuel s | _ -> () and lemma_purge_empty_codes (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures (machine_eval_codes cs fuel s == machine_eval_codes (purge_empty_codes cs) fuel s)) (decreases %[fuel; cs]) = match cs with | [] -> () | x :: xs -> if is_empty_code x then ( lemma_is_empty_code x fuel s; lemma_purge_empty_codes xs fuel s ) else ( lemma_purge_empty_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s' -> lemma_purge_empty_codes xs fuel s' ) and lemma_purge_empty_while (c:code{While? c}) (fuel:nat) (s0:machine_state) : Lemma (ensures (machine_eval_code c fuel s0 == machine_eval_code (purge_empty_code c) fuel s0)) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s0, b) = machine_eval_ocmp s0 cond in if not b then () else let s_opt = machine_eval_code body (fuel - 1) s0 in lemma_purge_empty_code body (fuel - 1) s0; match s_opt with | None -> () | Some s1 -> if s1.ms_ok then lemma_purge_empty_while c (fuel - 1) s1 else () )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 2201, "start_col": 0, "start_line": 2154 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs let increment_hint (th:transformation_hint) : transformation_hint = match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th let rec find_deep_code_transform (c:code) (cs:codes) : possibly transformation_hint = match cs with | [] -> Err ("Not found (during find_deep_code_transform): " ^ fst (print_code c 0 gcc)) | x :: xs -> (* let _ = IO.debug_print_string ( "---------------------------------\n" ^ " c : \n" ^ fst (print_code c 0 gcc) ^ "\n" ^ " x : \n" ^ fst (print_code x 0 gcc) ^ "\n" ^ " xs : \n" ^ fst (print_code (Block xs) 0 gcc) ^ "\n" ^ "---------------------------------\n" ^ "") in *) if is_empty_code x then find_deep_code_transform c xs else ( if eq_codes (fully_unblocked_code x) (fully_unblocked_code c) then ( return (MoveUpFrom 0) ) else ( match x with | Block l -> ( match find_deep_code_transform c l with | Ok t -> return (DiveInAt 0 t) | Err reason -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) | _ -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) ) let rec metric_for_code (c:code) : GTot nat = 1 + ( match c with | Ins _ -> 0 | Block l -> metric_for_codes l | IfElse _ t f -> metric_for_code t + metric_for_code f | While _ b -> metric_for_code b ) and metric_for_codes (c:codes) : GTot nat = match c with | [] -> 0 | x :: xs -> 1 + metric_for_code x + metric_for_codes xs let rec lemma_metric_for_codes_append (c1 c2:codes) : Lemma (ensures (metric_for_codes (c1 `L.append` c2) == metric_for_codes c1 + metric_for_codes c2)) [SMTPat (metric_for_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_metric_for_codes_append xs c2 irreducible (* Our proofs do not depend on how the hints are found. As long as some hints are provided, we validate the hints to perform the transformation and use it. Thus, we make this function [irreducible] to explicitly prevent any of the proofs from reasoning about it. *) let rec find_transformation_hints (c1 c2:codes) : Tot (possibly transformation_hints) (decreases %[metric_for_codes c2; metric_for_codes c1]) = let e1, e2 = is_empty_codes c1, is_empty_codes c2 in if e1 && e2 then ( return [] ) else if e2 then ( Err ("non empty first code: " ^ fst (print_code (Block c1) 0 gcc)) ) else if e1 then ( Err ("non empty second code: " ^ fst (print_code (Block c2) 0 gcc)) ) else ( let h1 :: t1 = c1 in let h2 :: t2 = c2 in assert (metric_for_codes c2 >= metric_for_code h2); (* OBSERVE *) if is_empty_code h1 then ( find_transformation_hints t1 c2 ) else if is_empty_code h2 then ( find_transformation_hints c1 t2 ) else ( match find_deep_code_transform h2 c1 with | Ok th -> ( match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Unable to find valid movement for : " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | Err reason -> ( let h1 :: t1 = c1 in match h1, h2 with | Block l1, Block l2 -> ( match ( let+ t_hints1 = find_transformation_hints l1 l2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (wrap_diveinat 0 t_hints1 `L.append` t_hints2) ) with | Ok ths -> return ths | Err reason -> find_transformation_hints c1 (l2 `L.append` t2) ) | IfElse co1 (Block tr1) (Block fa1), IfElse co2 (Block tr2) (Block fa2) -> (co1 = co2) /- ("Non-same conditions for IfElse: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block tr2)); (* OBSERVE *) assert (metric_for_code h2 > metric_for_code (Block fa2)); (* OBSERVE *) let+ tr_hints = find_transformation_hints tr1 tr2 in let+ fa_hints = find_transformation_hints fa1 fa2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceIfElse tr_hints fa_hints :: t_hints2) | While co1 (Block bo1), While co2 (Block bo2) -> (co1 = co2) /- ("Non-same conditions for While: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block bo2)); (* OBSERVE *) let+ bo_hints = find_transformation_hints bo1 bo2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceWhile bo_hints :: t_hints2) | Block l1, IfElse _ _ _ | Block l1, While _ _ -> assert (metric_for_codes (l1 `L.append` t1) == metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) assert_norm (metric_for_codes c1 == 2 + metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) let+ t_hints1 = find_transformation_hints (l1 `L.append` t1) c2 in ( match t_hints1 with | [] -> Err "Impossible" | th :: _ -> let th = DiveInAt 0 th in match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Failed during left-unblock for " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | _, Block l2 -> find_transformation_hints c1 (l2 `L.append` t2) | IfElse _ _ _, IfElse _ _ _ | While _ _, While _ _ -> Err ("Found weird non-standard code: " ^ fst (print_code h1 0 gcc)) | _ -> Err ("Find deep code failure. Reason: " ^ reason) ) ) ) /// If a transformation can be performed, then the result behaves /// identically as per the [equiv_states] relation. #push-options "--z3rlimit 10 --initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_bubble_to_top (cs : codes) (i:nat{i < L.length cs}) (fuel:nat) (s s' : machine_state) : Lemma (requires ( (s'.ms_ok) /\ (Some s' == machine_eval_codes cs fuel s) /\ (Ok? (bubble_to_top cs i)))) (ensures ( let x = L.index cs i in let Ok xs = bubble_to_top cs i in let s1' = machine_eval_code x fuel s in (Some? s1') /\ ( let Some s1 = s1' in let s2' = machine_eval_codes xs fuel s1 in (Some? s2') /\ ( let Some s2 = s2' in equiv_states s' s2)))) = match cs with | [_] -> () | h :: t -> let x = L.index cs i in let Ok xs = bubble_to_top cs i in if i = 0 then () else ( let Some s_h = machine_eval_code h fuel s in lemma_bubble_to_top (L.tl cs) (i-1) fuel s_h s'; let Some s_h_x = machine_eval_code x fuel s_h in let Some s_hx = machine_eval_codes [h;x] fuel s in assert (s_h_x == s_hx); lemma_code_exchange_allowed x h fuel s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes (L.tl xs) fuel) s_hx; assert (s_hx.ms_ok); let Some s_xh = machine_eval_codes [x;h] fuel s in lemma_eval_codes_equiv_states (L.tl xs) fuel s_hx s_xh ) #pop-options #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_machine_eval_codes_block_to_append (c1 c2 : codes) (fuel:nat) (s:machine_state) : Lemma (ensures (machine_eval_codes (c1 `L.append` c2) fuel s == machine_eval_codes (Block c1 :: c2) fuel s)) = match c1 with | [] -> () | x :: xs -> match machine_eval_code x fuel s with | None -> () | Some s1 -> lemma_machine_eval_codes_block_to_append xs c2 fuel s1 #pop-options let rec lemma_append_single (xs:list 'a) (y:'a) (i:nat) : Lemma (requires (i == L.length xs)) (ensures ( L.length (xs `L.append` [y]) = L.length xs + 1 /\ L.index (xs `L.append` [y]) i == y)) = match xs with | [] -> () | x :: xs -> lemma_append_single xs y (i - 1) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_is_empty_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (is_empty_code c)) (ensures ((machine_eval_code c fuel s) == (machine_eval_codes [] fuel s))) = match c with | Ins _ -> () | Block l -> lemma_is_empty_codes l fuel s | IfElse _ t f -> () | While _ c -> () and lemma_is_empty_codes (cs:codes) (fuel:nat) (s:machine_state) : Lemma (requires (is_empty_codes cs)) (ensures ((machine_eval_codes cs fuel s) == (machine_eval_codes [] fuel s))) = match cs with | [] -> () | x :: xs -> lemma_is_empty_code x fuel s; lemma_is_empty_codes xs fuel s #pop-options #restart-solver #push-options "--z3rlimit 100 --initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_perform_reordering_with_hint (t:transformation_hint) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (requires ( (Ok? (perform_reordering_with_hint t cs)) /\ (Some? (machine_eval_codes cs fuel s)) /\ (Some?.v (machine_eval_codes cs fuel s)).ms_ok)) (ensures ( let Ok cs' = perform_reordering_with_hint t cs in equiv_ostates (machine_eval_codes cs fuel s) (machine_eval_codes cs' fuel s))) (decreases %[t; fuel; cs]) = let c = cs in let Ok cs' = perform_reordering_with_hint t cs in let Some s' = machine_eval_codes cs fuel s in let x :: xs = cs in if is_empty_codes [x] then ( lemma_is_empty_codes [x] fuel s; match machine_eval_code x fuel s with | None -> () | Some s' -> lemma_eval_codes_equiv_states xs fuel s s'; lemma_perform_reordering_with_hint t xs fuel s ) else ( match t with | MoveUpFrom i -> ( let Ok c' = bubble_to_top c i in lemma_bubble_to_top c i fuel s s' ) | DiveInAt i t' -> ( FStar.List.Pure.lemma_split3_append c i; FStar.List.Pure.lemma_split3_length c i; let left, mid, right = L.split3 c i in let Block l = mid in let Ok (y :: ys) = perform_reordering_with_hint t' l in L.append_length left [y]; let Ok left' = bubble_to_top (left `L.append` [y]) i in // assert (cs' == y :: (left' `L.append` (Block ys :: right))); assert (left `L.append` (mid :: right) == c); L.append_l_cons mid right left; assert ((left `L.append` [mid]) `L.append` right == c); lemma_machine_eval_codes_block_to_append (left `L.append` [mid]) right fuel s; let Some s_1 = machine_eval_code (Block (left `L.append` [mid])) fuel s in assert (Some s_1 == machine_eval_codes (left `L.append` [mid]) fuel s); lemma_machine_eval_codes_block_to_append left [mid] fuel s; let Some s_2 = machine_eval_code (Block left) fuel s in assert (Some s_2 == machine_eval_codes left fuel s); // assert (Some s_1 == machine_eval_codes [mid] fuel s_2); assert (Some s_1 == machine_eval_code (Block l) fuel s_2); assert (Some s_1 == machine_eval_codes l fuel s_2); assert (Some s' == machine_eval_codes right fuel s_1); if s_1.ms_ok then () else lemma_not_ok_propagate_codes right fuel s_1; lemma_perform_reordering_with_hint t' l fuel s_2; // let Some s_11 = machine_eval_codes (y :: ys) fuel s_2 in let Some s_12 = machine_eval_code y fuel s_2 in if s_12.ms_ok then () else lemma_not_ok_propagate_codes ys fuel s_12; assert (Some s_2 == machine_eval_codes left fuel s); assert (Some s_2 == machine_eval_code (Block left) fuel s); assert (Some s_12 == machine_eval_codes (Block left :: [y]) fuel s); lemma_machine_eval_codes_block_to_append left [y] fuel s; assert (Some s_12 == machine_eval_codes (left `L.append` [y]) fuel s); lemma_bubble_to_top (left `L.append` [y]) i fuel s s_12; // lemma_append_single left y i; assert (L.index (left `L.append` [y]) i == y); // let Some s_3 = machine_eval_codes (y :: left') fuel s in assert (equiv_states s_3 s_12); lemma_eval_codes_equiv_states right fuel s_1 s_11; let Some s_0 = machine_eval_codes right fuel s_11 in lemma_eval_codes_equiv_states (Block ys :: right) fuel s_12 s_3; let Some s_00 = machine_eval_codes (Block ys :: right) fuel s_3 in // assert (equiv_states s_00 s'); assert (Some s_3 == machine_eval_code (Block (y :: left')) fuel s); assert (Some s_00 == machine_eval_codes (Block ys :: right) fuel s_3); lemma_machine_eval_codes_block_to_append (y :: left') (Block ys :: right) fuel s ) | InPlaceIfElse tht thf -> ( let IfElse cond c_ift c_iff :: xs = cs in let Block cs_ift, Block cs_iff = c_ift, c_iff in let (s1, b) = machine_eval_ocmp s cond in if b then ( assert (Some s' == machine_eval_codes (c_ift :: xs) fuel s1); let Some s'' = machine_eval_code c_ift fuel s1 in if not s''.ms_ok then (lemma_not_ok_propagate_codes xs fuel s'') else (); lemma_perform_reordering_with_hints tht cs_ift fuel s1; let Some s''' = machine_eval_code (IfElse cond c_ift c_iff) fuel s in let x' :: _ = cs' in let Some s'''' = machine_eval_code x' fuel s in assert (equiv_states s''' s''''); lemma_eval_codes_equiv_states xs fuel s''' s'''' ) else ( let Some s'' = machine_eval_code c_iff fuel s1 in if not s''.ms_ok then (lemma_not_ok_propagate_codes xs fuel s'') else (); lemma_perform_reordering_with_hints thf cs_iff fuel s1; let Some s''' = machine_eval_code (IfElse cond c_ift c_iff) fuel s in let x' :: _ = cs' in let Some s'''' = machine_eval_code x' fuel s in lemma_eval_codes_equiv_states xs fuel s''' s'''' ) ) | InPlaceWhile thb -> ( assert (fuel <> 0); let While cond body :: xs = cs in let Block cs_body = body in let (s0, b) = machine_eval_ocmp s cond in if not b then () else ( let Some s1 = machine_eval_code body (fuel - 1) s0 in if s1.ms_ok then () else lemma_not_ok_propagate_codes xs fuel s1; lemma_perform_reordering_with_hints thb cs_body (fuel - 1) s0; let x' :: xs' = cs' in assert (xs' == xs); let While cond' body' = x' in let cs_body' = body' in let Some s11 = machine_eval_code (While cond body) (fuel - 1) s1 in if s11.ms_ok then () else lemma_not_ok_propagate_codes xs fuel s11; assert (Some s' == machine_eval_codes xs fuel s11); lemma_perform_reordering_with_hint t [While cond body] (fuel - 1) s1; let Some s11' = machine_eval_code x' (fuel - 1) s1 in lemma_eval_codes_equiv_states xs fuel s11 s11'; let Some s'' = machine_eval_codes xs fuel s11' in assert (machine_eval_codes cs fuel s == Some s'); assert (equiv_states s' s''); let Some s1' = machine_eval_code body' (fuel - 1) s0 in lemma_eval_code_equiv_states x' (fuel-1) s1 s1'; let Some s11'' = machine_eval_code x' (fuel-1) s1' in assert (machine_eval_codes cs' fuel s == machine_eval_codes xs fuel s11''); lemma_eval_codes_equiv_states xs fuel s11' s11'' ) ) ) and lemma_perform_reordering_with_hints (ts:transformation_hints) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (requires ( (Ok? (perform_reordering_with_hints ts cs)) /\ (Some? (machine_eval_codes cs fuel s)) /\ (Some?.v (machine_eval_codes cs fuel s)).ms_ok)) (ensures ( let Ok cs' = perform_reordering_with_hints ts cs in equiv_ostates (machine_eval_codes cs fuel s) (machine_eval_codes cs' fuel s))) (decreases %[ts; fuel; cs]) = let c = cs in let Ok cs' = perform_reordering_with_hints ts cs in let Some s' = machine_eval_codes cs fuel s in match ts with | [] -> lemma_is_empty_codes cs fuel s | t :: ts' -> let Ok (x :: xs) = perform_reordering_with_hint t c in lemma_perform_reordering_with_hint t c fuel s; let Ok xs' = perform_reordering_with_hints ts' xs in let Some s1 = machine_eval_code x fuel s in lemma_perform_reordering_with_hints ts' xs fuel s1 #pop-options /// Some convenience functions to be run before the pass, to ensure /// that we don't have miscounting due to empty code. let rec purge_empty_code (c:code) : code = match c with | Block l -> Block (purge_empty_codes l) | IfElse c t f -> IfElse c (purge_empty_code t) (purge_empty_code f) | While c b -> While c (purge_empty_code b) | _ -> c and purge_empty_codes (cs:codes) : codes = match cs with | [] -> [] | x :: xs -> if is_empty_code x then ( purge_empty_codes xs ) else ( purge_empty_code x :: purge_empty_codes xs )
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 2, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code -> fuel: Prims.nat -> s: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (ensures Vale.X64.Machine_Semantics_s.machine_eval_code c fuel s == Vale.X64.Machine_Semantics_s.machine_eval_code (Vale.Transformers.InstructionReorder.purge_empty_code c) fuel s) (decreases %[fuel;c;1])
FStar.Pervasives.Lemma
[ "lemma", "" ]
[ "lemma_purge_empty_code", "lemma_purge_empty_codes", "lemma_purge_empty_while" ]
[ "Vale.X64.Machine_Semantics_s.code", "Prims.nat", "Vale.X64.Machine_Semantics_s.machine_state", "Prims.list", "Vale.X64.Machine_s.precode", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.X64.Bytes_Code_s.ocmp", "Vale.Transformers.InstructionReorder.lemma_purge_empty_codes", "Prims.bool", "Vale.Transformers.InstructionReorder.lemma_purge_empty_code", "Prims.unit", "FStar.Pervasives.Native.tuple2", "Vale.X64.Machine_Semantics_s.machine_eval_ocmp", "Vale.Transformers.InstructionReorder.lemma_purge_empty_while", "Prims.l_True", "Prims.squash", "Prims.eq2", "FStar.Pervasives.Native.option", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Vale.Transformers.InstructionReorder.purge_empty_code", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "mutual recursion" ]
false
false
true
false
false
let rec lemma_purge_empty_code (c: code) (fuel: nat) (s: machine_state) : Lemma (ensures (machine_eval_code c fuel s == machine_eval_code (purge_empty_code c) fuel s)) (decreases %[fuel;c;1]) =
match c with | Block l -> lemma_purge_empty_codes l fuel s | IfElse c t f -> let s', b = machine_eval_ocmp s c in if b then lemma_purge_empty_code t fuel s' else lemma_purge_empty_code f fuel s' | While _ _ -> lemma_purge_empty_while c fuel s | _ -> ()
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_purge_empty_while
val lemma_purge_empty_while (c: code{While? c}) (fuel: nat) (s0: machine_state) : Lemma (ensures (machine_eval_code c fuel s0 == machine_eval_code (purge_empty_code c) fuel s0)) (decreases %[fuel;c;0])
val lemma_purge_empty_while (c: code{While? c}) (fuel: nat) (s0: machine_state) : Lemma (ensures (machine_eval_code c fuel s0 == machine_eval_code (purge_empty_code c) fuel s0)) (decreases %[fuel;c;0])
let rec lemma_purge_empty_code (c:code) (fuel:nat) (s:machine_state) : Lemma (ensures (machine_eval_code c fuel s == machine_eval_code (purge_empty_code c) fuel s)) (decreases %[fuel; c; 1]) = match c with | Block l -> lemma_purge_empty_codes l fuel s | IfElse c t f -> let (s', b) = machine_eval_ocmp s c in if b then lemma_purge_empty_code t fuel s' else lemma_purge_empty_code f fuel s' | While _ _ -> lemma_purge_empty_while c fuel s | _ -> () and lemma_purge_empty_codes (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures (machine_eval_codes cs fuel s == machine_eval_codes (purge_empty_codes cs) fuel s)) (decreases %[fuel; cs]) = match cs with | [] -> () | x :: xs -> if is_empty_code x then ( lemma_is_empty_code x fuel s; lemma_purge_empty_codes xs fuel s ) else ( lemma_purge_empty_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s' -> lemma_purge_empty_codes xs fuel s' ) and lemma_purge_empty_while (c:code{While? c}) (fuel:nat) (s0:machine_state) : Lemma (ensures (machine_eval_code c fuel s0 == machine_eval_code (purge_empty_code c) fuel s0)) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s0, b) = machine_eval_ocmp s0 cond in if not b then () else let s_opt = machine_eval_code body (fuel - 1) s0 in lemma_purge_empty_code body (fuel - 1) s0; match s_opt with | None -> () | Some s1 -> if s1.ms_ok then lemma_purge_empty_while c (fuel - 1) s1 else () )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 2201, "start_col": 0, "start_line": 2154 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs let increment_hint (th:transformation_hint) : transformation_hint = match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th let rec find_deep_code_transform (c:code) (cs:codes) : possibly transformation_hint = match cs with | [] -> Err ("Not found (during find_deep_code_transform): " ^ fst (print_code c 0 gcc)) | x :: xs -> (* let _ = IO.debug_print_string ( "---------------------------------\n" ^ " c : \n" ^ fst (print_code c 0 gcc) ^ "\n" ^ " x : \n" ^ fst (print_code x 0 gcc) ^ "\n" ^ " xs : \n" ^ fst (print_code (Block xs) 0 gcc) ^ "\n" ^ "---------------------------------\n" ^ "") in *) if is_empty_code x then find_deep_code_transform c xs else ( if eq_codes (fully_unblocked_code x) (fully_unblocked_code c) then ( return (MoveUpFrom 0) ) else ( match x with | Block l -> ( match find_deep_code_transform c l with | Ok t -> return (DiveInAt 0 t) | Err reason -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) | _ -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) ) let rec metric_for_code (c:code) : GTot nat = 1 + ( match c with | Ins _ -> 0 | Block l -> metric_for_codes l | IfElse _ t f -> metric_for_code t + metric_for_code f | While _ b -> metric_for_code b ) and metric_for_codes (c:codes) : GTot nat = match c with | [] -> 0 | x :: xs -> 1 + metric_for_code x + metric_for_codes xs let rec lemma_metric_for_codes_append (c1 c2:codes) : Lemma (ensures (metric_for_codes (c1 `L.append` c2) == metric_for_codes c1 + metric_for_codes c2)) [SMTPat (metric_for_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_metric_for_codes_append xs c2 irreducible (* Our proofs do not depend on how the hints are found. As long as some hints are provided, we validate the hints to perform the transformation and use it. Thus, we make this function [irreducible] to explicitly prevent any of the proofs from reasoning about it. *) let rec find_transformation_hints (c1 c2:codes) : Tot (possibly transformation_hints) (decreases %[metric_for_codes c2; metric_for_codes c1]) = let e1, e2 = is_empty_codes c1, is_empty_codes c2 in if e1 && e2 then ( return [] ) else if e2 then ( Err ("non empty first code: " ^ fst (print_code (Block c1) 0 gcc)) ) else if e1 then ( Err ("non empty second code: " ^ fst (print_code (Block c2) 0 gcc)) ) else ( let h1 :: t1 = c1 in let h2 :: t2 = c2 in assert (metric_for_codes c2 >= metric_for_code h2); (* OBSERVE *) if is_empty_code h1 then ( find_transformation_hints t1 c2 ) else if is_empty_code h2 then ( find_transformation_hints c1 t2 ) else ( match find_deep_code_transform h2 c1 with | Ok th -> ( match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Unable to find valid movement for : " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | Err reason -> ( let h1 :: t1 = c1 in match h1, h2 with | Block l1, Block l2 -> ( match ( let+ t_hints1 = find_transformation_hints l1 l2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (wrap_diveinat 0 t_hints1 `L.append` t_hints2) ) with | Ok ths -> return ths | Err reason -> find_transformation_hints c1 (l2 `L.append` t2) ) | IfElse co1 (Block tr1) (Block fa1), IfElse co2 (Block tr2) (Block fa2) -> (co1 = co2) /- ("Non-same conditions for IfElse: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block tr2)); (* OBSERVE *) assert (metric_for_code h2 > metric_for_code (Block fa2)); (* OBSERVE *) let+ tr_hints = find_transformation_hints tr1 tr2 in let+ fa_hints = find_transformation_hints fa1 fa2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceIfElse tr_hints fa_hints :: t_hints2) | While co1 (Block bo1), While co2 (Block bo2) -> (co1 = co2) /- ("Non-same conditions for While: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block bo2)); (* OBSERVE *) let+ bo_hints = find_transformation_hints bo1 bo2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceWhile bo_hints :: t_hints2) | Block l1, IfElse _ _ _ | Block l1, While _ _ -> assert (metric_for_codes (l1 `L.append` t1) == metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) assert_norm (metric_for_codes c1 == 2 + metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) let+ t_hints1 = find_transformation_hints (l1 `L.append` t1) c2 in ( match t_hints1 with | [] -> Err "Impossible" | th :: _ -> let th = DiveInAt 0 th in match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Failed during left-unblock for " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | _, Block l2 -> find_transformation_hints c1 (l2 `L.append` t2) | IfElse _ _ _, IfElse _ _ _ | While _ _, While _ _ -> Err ("Found weird non-standard code: " ^ fst (print_code h1 0 gcc)) | _ -> Err ("Find deep code failure. Reason: " ^ reason) ) ) ) /// If a transformation can be performed, then the result behaves /// identically as per the [equiv_states] relation. #push-options "--z3rlimit 10 --initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_bubble_to_top (cs : codes) (i:nat{i < L.length cs}) (fuel:nat) (s s' : machine_state) : Lemma (requires ( (s'.ms_ok) /\ (Some s' == machine_eval_codes cs fuel s) /\ (Ok? (bubble_to_top cs i)))) (ensures ( let x = L.index cs i in let Ok xs = bubble_to_top cs i in let s1' = machine_eval_code x fuel s in (Some? s1') /\ ( let Some s1 = s1' in let s2' = machine_eval_codes xs fuel s1 in (Some? s2') /\ ( let Some s2 = s2' in equiv_states s' s2)))) = match cs with | [_] -> () | h :: t -> let x = L.index cs i in let Ok xs = bubble_to_top cs i in if i = 0 then () else ( let Some s_h = machine_eval_code h fuel s in lemma_bubble_to_top (L.tl cs) (i-1) fuel s_h s'; let Some s_h_x = machine_eval_code x fuel s_h in let Some s_hx = machine_eval_codes [h;x] fuel s in assert (s_h_x == s_hx); lemma_code_exchange_allowed x h fuel s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes (L.tl xs) fuel) s_hx; assert (s_hx.ms_ok); let Some s_xh = machine_eval_codes [x;h] fuel s in lemma_eval_codes_equiv_states (L.tl xs) fuel s_hx s_xh ) #pop-options #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_machine_eval_codes_block_to_append (c1 c2 : codes) (fuel:nat) (s:machine_state) : Lemma (ensures (machine_eval_codes (c1 `L.append` c2) fuel s == machine_eval_codes (Block c1 :: c2) fuel s)) = match c1 with | [] -> () | x :: xs -> match machine_eval_code x fuel s with | None -> () | Some s1 -> lemma_machine_eval_codes_block_to_append xs c2 fuel s1 #pop-options let rec lemma_append_single (xs:list 'a) (y:'a) (i:nat) : Lemma (requires (i == L.length xs)) (ensures ( L.length (xs `L.append` [y]) = L.length xs + 1 /\ L.index (xs `L.append` [y]) i == y)) = match xs with | [] -> () | x :: xs -> lemma_append_single xs y (i - 1) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_is_empty_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (is_empty_code c)) (ensures ((machine_eval_code c fuel s) == (machine_eval_codes [] fuel s))) = match c with | Ins _ -> () | Block l -> lemma_is_empty_codes l fuel s | IfElse _ t f -> () | While _ c -> () and lemma_is_empty_codes (cs:codes) (fuel:nat) (s:machine_state) : Lemma (requires (is_empty_codes cs)) (ensures ((machine_eval_codes cs fuel s) == (machine_eval_codes [] fuel s))) = match cs with | [] -> () | x :: xs -> lemma_is_empty_code x fuel s; lemma_is_empty_codes xs fuel s #pop-options #restart-solver #push-options "--z3rlimit 100 --initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_perform_reordering_with_hint (t:transformation_hint) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (requires ( (Ok? (perform_reordering_with_hint t cs)) /\ (Some? (machine_eval_codes cs fuel s)) /\ (Some?.v (machine_eval_codes cs fuel s)).ms_ok)) (ensures ( let Ok cs' = perform_reordering_with_hint t cs in equiv_ostates (machine_eval_codes cs fuel s) (machine_eval_codes cs' fuel s))) (decreases %[t; fuel; cs]) = let c = cs in let Ok cs' = perform_reordering_with_hint t cs in let Some s' = machine_eval_codes cs fuel s in let x :: xs = cs in if is_empty_codes [x] then ( lemma_is_empty_codes [x] fuel s; match machine_eval_code x fuel s with | None -> () | Some s' -> lemma_eval_codes_equiv_states xs fuel s s'; lemma_perform_reordering_with_hint t xs fuel s ) else ( match t with | MoveUpFrom i -> ( let Ok c' = bubble_to_top c i in lemma_bubble_to_top c i fuel s s' ) | DiveInAt i t' -> ( FStar.List.Pure.lemma_split3_append c i; FStar.List.Pure.lemma_split3_length c i; let left, mid, right = L.split3 c i in let Block l = mid in let Ok (y :: ys) = perform_reordering_with_hint t' l in L.append_length left [y]; let Ok left' = bubble_to_top (left `L.append` [y]) i in // assert (cs' == y :: (left' `L.append` (Block ys :: right))); assert (left `L.append` (mid :: right) == c); L.append_l_cons mid right left; assert ((left `L.append` [mid]) `L.append` right == c); lemma_machine_eval_codes_block_to_append (left `L.append` [mid]) right fuel s; let Some s_1 = machine_eval_code (Block (left `L.append` [mid])) fuel s in assert (Some s_1 == machine_eval_codes (left `L.append` [mid]) fuel s); lemma_machine_eval_codes_block_to_append left [mid] fuel s; let Some s_2 = machine_eval_code (Block left) fuel s in assert (Some s_2 == machine_eval_codes left fuel s); // assert (Some s_1 == machine_eval_codes [mid] fuel s_2); assert (Some s_1 == machine_eval_code (Block l) fuel s_2); assert (Some s_1 == machine_eval_codes l fuel s_2); assert (Some s' == machine_eval_codes right fuel s_1); if s_1.ms_ok then () else lemma_not_ok_propagate_codes right fuel s_1; lemma_perform_reordering_with_hint t' l fuel s_2; // let Some s_11 = machine_eval_codes (y :: ys) fuel s_2 in let Some s_12 = machine_eval_code y fuel s_2 in if s_12.ms_ok then () else lemma_not_ok_propagate_codes ys fuel s_12; assert (Some s_2 == machine_eval_codes left fuel s); assert (Some s_2 == machine_eval_code (Block left) fuel s); assert (Some s_12 == machine_eval_codes (Block left :: [y]) fuel s); lemma_machine_eval_codes_block_to_append left [y] fuel s; assert (Some s_12 == machine_eval_codes (left `L.append` [y]) fuel s); lemma_bubble_to_top (left `L.append` [y]) i fuel s s_12; // lemma_append_single left y i; assert (L.index (left `L.append` [y]) i == y); // let Some s_3 = machine_eval_codes (y :: left') fuel s in assert (equiv_states s_3 s_12); lemma_eval_codes_equiv_states right fuel s_1 s_11; let Some s_0 = machine_eval_codes right fuel s_11 in lemma_eval_codes_equiv_states (Block ys :: right) fuel s_12 s_3; let Some s_00 = machine_eval_codes (Block ys :: right) fuel s_3 in // assert (equiv_states s_00 s'); assert (Some s_3 == machine_eval_code (Block (y :: left')) fuel s); assert (Some s_00 == machine_eval_codes (Block ys :: right) fuel s_3); lemma_machine_eval_codes_block_to_append (y :: left') (Block ys :: right) fuel s ) | InPlaceIfElse tht thf -> ( let IfElse cond c_ift c_iff :: xs = cs in let Block cs_ift, Block cs_iff = c_ift, c_iff in let (s1, b) = machine_eval_ocmp s cond in if b then ( assert (Some s' == machine_eval_codes (c_ift :: xs) fuel s1); let Some s'' = machine_eval_code c_ift fuel s1 in if not s''.ms_ok then (lemma_not_ok_propagate_codes xs fuel s'') else (); lemma_perform_reordering_with_hints tht cs_ift fuel s1; let Some s''' = machine_eval_code (IfElse cond c_ift c_iff) fuel s in let x' :: _ = cs' in let Some s'''' = machine_eval_code x' fuel s in assert (equiv_states s''' s''''); lemma_eval_codes_equiv_states xs fuel s''' s'''' ) else ( let Some s'' = machine_eval_code c_iff fuel s1 in if not s''.ms_ok then (lemma_not_ok_propagate_codes xs fuel s'') else (); lemma_perform_reordering_with_hints thf cs_iff fuel s1; let Some s''' = machine_eval_code (IfElse cond c_ift c_iff) fuel s in let x' :: _ = cs' in let Some s'''' = machine_eval_code x' fuel s in lemma_eval_codes_equiv_states xs fuel s''' s'''' ) ) | InPlaceWhile thb -> ( assert (fuel <> 0); let While cond body :: xs = cs in let Block cs_body = body in let (s0, b) = machine_eval_ocmp s cond in if not b then () else ( let Some s1 = machine_eval_code body (fuel - 1) s0 in if s1.ms_ok then () else lemma_not_ok_propagate_codes xs fuel s1; lemma_perform_reordering_with_hints thb cs_body (fuel - 1) s0; let x' :: xs' = cs' in assert (xs' == xs); let While cond' body' = x' in let cs_body' = body' in let Some s11 = machine_eval_code (While cond body) (fuel - 1) s1 in if s11.ms_ok then () else lemma_not_ok_propagate_codes xs fuel s11; assert (Some s' == machine_eval_codes xs fuel s11); lemma_perform_reordering_with_hint t [While cond body] (fuel - 1) s1; let Some s11' = machine_eval_code x' (fuel - 1) s1 in lemma_eval_codes_equiv_states xs fuel s11 s11'; let Some s'' = machine_eval_codes xs fuel s11' in assert (machine_eval_codes cs fuel s == Some s'); assert (equiv_states s' s''); let Some s1' = machine_eval_code body' (fuel - 1) s0 in lemma_eval_code_equiv_states x' (fuel-1) s1 s1'; let Some s11'' = machine_eval_code x' (fuel-1) s1' in assert (machine_eval_codes cs' fuel s == machine_eval_codes xs fuel s11''); lemma_eval_codes_equiv_states xs fuel s11' s11'' ) ) ) and lemma_perform_reordering_with_hints (ts:transformation_hints) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (requires ( (Ok? (perform_reordering_with_hints ts cs)) /\ (Some? (machine_eval_codes cs fuel s)) /\ (Some?.v (machine_eval_codes cs fuel s)).ms_ok)) (ensures ( let Ok cs' = perform_reordering_with_hints ts cs in equiv_ostates (machine_eval_codes cs fuel s) (machine_eval_codes cs' fuel s))) (decreases %[ts; fuel; cs]) = let c = cs in let Ok cs' = perform_reordering_with_hints ts cs in let Some s' = machine_eval_codes cs fuel s in match ts with | [] -> lemma_is_empty_codes cs fuel s | t :: ts' -> let Ok (x :: xs) = perform_reordering_with_hint t c in lemma_perform_reordering_with_hint t c fuel s; let Ok xs' = perform_reordering_with_hints ts' xs in let Some s1 = machine_eval_code x fuel s in lemma_perform_reordering_with_hints ts' xs fuel s1 #pop-options /// Some convenience functions to be run before the pass, to ensure /// that we don't have miscounting due to empty code. let rec purge_empty_code (c:code) : code = match c with | Block l -> Block (purge_empty_codes l) | IfElse c t f -> IfElse c (purge_empty_code t) (purge_empty_code f) | While c b -> While c (purge_empty_code b) | _ -> c and purge_empty_codes (cs:codes) : codes = match cs with | [] -> [] | x :: xs -> if is_empty_code x then ( purge_empty_codes xs ) else ( purge_empty_code x :: purge_empty_codes xs )
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 2, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code{While? c} -> fuel: Prims.nat -> s0: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (ensures Vale.X64.Machine_Semantics_s.machine_eval_code c fuel s0 == Vale.X64.Machine_Semantics_s.machine_eval_code (Vale.Transformers.InstructionReorder.purge_empty_code c) fuel s0) (decreases %[fuel;c;0])
FStar.Pervasives.Lemma
[ "lemma", "" ]
[ "lemma_purge_empty_code", "lemma_purge_empty_codes", "lemma_purge_empty_while" ]
[ "Vale.X64.Machine_Semantics_s.code", "Prims.b2t", "Vale.X64.Machine_s.uu___is_While", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.X64.Bytes_Code_s.ocmp", "Prims.nat", "Vale.X64.Machine_Semantics_s.machine_state", "Prims.op_Equality", "Prims.int", "Prims.bool", "Vale.X64.Machine_s.precode", "Prims.op_Negation", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Vale.Transformers.InstructionReorder.lemma_purge_empty_while", "Prims.op_Subtraction", "Prims.unit", "Vale.Transformers.InstructionReorder.lemma_purge_empty_code", "FStar.Pervasives.Native.option", "Vale.X64.Machine_Semantics_s.machine_eval_code", "FStar.Pervasives.Native.tuple2", "Vale.X64.Machine_Semantics_s.machine_eval_ocmp", "Prims.l_True", "Prims.squash", "Prims.eq2", "Vale.Transformers.InstructionReorder.purge_empty_code", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "mutual recursion" ]
false
false
true
false
false
let rec lemma_purge_empty_while (c: code{While? c}) (fuel: nat) (s0: machine_state) : Lemma (ensures (machine_eval_code c fuel s0 == machine_eval_code (purge_empty_code c) fuel s0)) (decreases %[fuel;c;0]) =
if fuel = 0 then () else (let While cond body = c in let s0, b = machine_eval_ocmp s0 cond in if not b then () else let s_opt = machine_eval_code body (fuel - 1) s0 in lemma_purge_empty_code body (fuel - 1) s0; match s_opt with | None -> () | Some s1 -> if s1.ms_ok then lemma_purge_empty_while c (fuel - 1) s1)
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_bubble_to_top
val lemma_bubble_to_top (cs: codes) (i: nat{i < L.length cs}) (fuel: nat) (s s': machine_state) : Lemma (requires ((s'.ms_ok) /\ (Some s' == machine_eval_codes cs fuel s) /\ (Ok? (bubble_to_top cs i)))) (ensures (let x = L.index cs i in let Ok xs = bubble_to_top cs i in let s1' = machine_eval_code x fuel s in (Some? s1') /\ (let Some s1 = s1' in let s2' = machine_eval_codes xs fuel s1 in (Some? s2') /\ (let Some s2 = s2' in equiv_states s' s2))))
val lemma_bubble_to_top (cs: codes) (i: nat{i < L.length cs}) (fuel: nat) (s s': machine_state) : Lemma (requires ((s'.ms_ok) /\ (Some s' == machine_eval_codes cs fuel s) /\ (Ok? (bubble_to_top cs i)))) (ensures (let x = L.index cs i in let Ok xs = bubble_to_top cs i in let s1' = machine_eval_code x fuel s in (Some? s1') /\ (let Some s1 = s1' in let s2' = machine_eval_codes xs fuel s1 in (Some? s2') /\ (let Some s2 = s2' in equiv_states s' s2))))
let rec lemma_bubble_to_top (cs : codes) (i:nat{i < L.length cs}) (fuel:nat) (s s' : machine_state) : Lemma (requires ( (s'.ms_ok) /\ (Some s' == machine_eval_codes cs fuel s) /\ (Ok? (bubble_to_top cs i)))) (ensures ( let x = L.index cs i in let Ok xs = bubble_to_top cs i in let s1' = machine_eval_code x fuel s in (Some? s1') /\ ( let Some s1 = s1' in let s2' = machine_eval_codes xs fuel s1 in (Some? s2') /\ ( let Some s2 = s2' in equiv_states s' s2)))) = match cs with | [_] -> () | h :: t -> let x = L.index cs i in let Ok xs = bubble_to_top cs i in if i = 0 then () else ( let Some s_h = machine_eval_code h fuel s in lemma_bubble_to_top (L.tl cs) (i-1) fuel s_h s'; let Some s_h_x = machine_eval_code x fuel s_h in let Some s_hx = machine_eval_codes [h;x] fuel s in assert (s_h_x == s_hx); lemma_code_exchange_allowed x h fuel s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes (L.tl xs) fuel) s_hx; assert (s_hx.ms_ok); let Some s_xh = machine_eval_codes [x;h] fuel s in lemma_eval_codes_equiv_states (L.tl xs) fuel s_hx s_xh )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 5, "end_line": 1919, "start_col": 0, "start_line": 1887 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs let increment_hint (th:transformation_hint) : transformation_hint = match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th let rec find_deep_code_transform (c:code) (cs:codes) : possibly transformation_hint = match cs with | [] -> Err ("Not found (during find_deep_code_transform): " ^ fst (print_code c 0 gcc)) | x :: xs -> (* let _ = IO.debug_print_string ( "---------------------------------\n" ^ " c : \n" ^ fst (print_code c 0 gcc) ^ "\n" ^ " x : \n" ^ fst (print_code x 0 gcc) ^ "\n" ^ " xs : \n" ^ fst (print_code (Block xs) 0 gcc) ^ "\n" ^ "---------------------------------\n" ^ "") in *) if is_empty_code x then find_deep_code_transform c xs else ( if eq_codes (fully_unblocked_code x) (fully_unblocked_code c) then ( return (MoveUpFrom 0) ) else ( match x with | Block l -> ( match find_deep_code_transform c l with | Ok t -> return (DiveInAt 0 t) | Err reason -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) | _ -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) ) let rec metric_for_code (c:code) : GTot nat = 1 + ( match c with | Ins _ -> 0 | Block l -> metric_for_codes l | IfElse _ t f -> metric_for_code t + metric_for_code f | While _ b -> metric_for_code b ) and metric_for_codes (c:codes) : GTot nat = match c with | [] -> 0 | x :: xs -> 1 + metric_for_code x + metric_for_codes xs let rec lemma_metric_for_codes_append (c1 c2:codes) : Lemma (ensures (metric_for_codes (c1 `L.append` c2) == metric_for_codes c1 + metric_for_codes c2)) [SMTPat (metric_for_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_metric_for_codes_append xs c2 irreducible (* Our proofs do not depend on how the hints are found. As long as some hints are provided, we validate the hints to perform the transformation and use it. Thus, we make this function [irreducible] to explicitly prevent any of the proofs from reasoning about it. *) let rec find_transformation_hints (c1 c2:codes) : Tot (possibly transformation_hints) (decreases %[metric_for_codes c2; metric_for_codes c1]) = let e1, e2 = is_empty_codes c1, is_empty_codes c2 in if e1 && e2 then ( return [] ) else if e2 then ( Err ("non empty first code: " ^ fst (print_code (Block c1) 0 gcc)) ) else if e1 then ( Err ("non empty second code: " ^ fst (print_code (Block c2) 0 gcc)) ) else ( let h1 :: t1 = c1 in let h2 :: t2 = c2 in assert (metric_for_codes c2 >= metric_for_code h2); (* OBSERVE *) if is_empty_code h1 then ( find_transformation_hints t1 c2 ) else if is_empty_code h2 then ( find_transformation_hints c1 t2 ) else ( match find_deep_code_transform h2 c1 with | Ok th -> ( match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Unable to find valid movement for : " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | Err reason -> ( let h1 :: t1 = c1 in match h1, h2 with | Block l1, Block l2 -> ( match ( let+ t_hints1 = find_transformation_hints l1 l2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (wrap_diveinat 0 t_hints1 `L.append` t_hints2) ) with | Ok ths -> return ths | Err reason -> find_transformation_hints c1 (l2 `L.append` t2) ) | IfElse co1 (Block tr1) (Block fa1), IfElse co2 (Block tr2) (Block fa2) -> (co1 = co2) /- ("Non-same conditions for IfElse: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block tr2)); (* OBSERVE *) assert (metric_for_code h2 > metric_for_code (Block fa2)); (* OBSERVE *) let+ tr_hints = find_transformation_hints tr1 tr2 in let+ fa_hints = find_transformation_hints fa1 fa2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceIfElse tr_hints fa_hints :: t_hints2) | While co1 (Block bo1), While co2 (Block bo2) -> (co1 = co2) /- ("Non-same conditions for While: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block bo2)); (* OBSERVE *) let+ bo_hints = find_transformation_hints bo1 bo2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceWhile bo_hints :: t_hints2) | Block l1, IfElse _ _ _ | Block l1, While _ _ -> assert (metric_for_codes (l1 `L.append` t1) == metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) assert_norm (metric_for_codes c1 == 2 + metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) let+ t_hints1 = find_transformation_hints (l1 `L.append` t1) c2 in ( match t_hints1 with | [] -> Err "Impossible" | th :: _ -> let th = DiveInAt 0 th in match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Failed during left-unblock for " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | _, Block l2 -> find_transformation_hints c1 (l2 `L.append` t2) | IfElse _ _ _, IfElse _ _ _ | While _ _, While _ _ -> Err ("Found weird non-standard code: " ^ fst (print_code h1 0 gcc)) | _ -> Err ("Find deep code failure. Reason: " ^ reason) ) ) ) /// If a transformation can be performed, then the result behaves /// identically as per the [equiv_states] relation.
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 3, "initial_ifuel": 1, "max_fuel": 3, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 10, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Vale.X64.Machine_Semantics_s.codes -> i: Prims.nat{i < FStar.List.Tot.Base.length cs} -> fuel: Prims.nat -> s: Vale.X64.Machine_Semantics_s.machine_state -> s': Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Mkmachine_state?.ms_ok s' /\ FStar.Pervasives.Native.Some s' == Vale.X64.Machine_Semantics_s.machine_eval_codes cs fuel s /\ Ok? (Vale.Transformers.InstructionReorder.bubble_to_top cs i)) (ensures (let x = FStar.List.Tot.Base.index cs i in let _ = Vale.Transformers.InstructionReorder.bubble_to_top cs i in (let Vale.Def.PossiblyMonad.Ok #_ xs = _ in let s1' = Vale.X64.Machine_Semantics_s.machine_eval_code x fuel s in Some? s1' /\ (let _ = s1' in (let FStar.Pervasives.Native.Some #_ s1 = _ in let s2' = Vale.X64.Machine_Semantics_s.machine_eval_codes xs fuel s1 in Some? s2' /\ (let _ = s2' in (let FStar.Pervasives.Native.Some #_ s2 = _ in Vale.Transformers.InstructionReorder.equiv_states s' s2) <: Prims.logical)) <: Prims.logical)) <: Type0))
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Vale.X64.Machine_Semantics_s.codes", "Prims.nat", "Prims.b2t", "Prims.op_LessThan", "FStar.List.Tot.Base.length", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.X64.Machine_Semantics_s.machine_state", "Prims.list", "FStar.List.Tot.Base.split3", "Prims.l_and", "Prims.eq2", "FStar.List.Tot.Base.append", "Prims.op_Equality", "Prims.int", "Prims.op_Subtraction", "Prims.bool", "Vale.Transformers.InstructionReorder.lemma_eval_codes_equiv_states", "FStar.List.Tot.Base.tl", "Prims.unit", "FStar.Pervasives.Native.option", "Vale.X64.Machine_Semantics_s.machine_eval_codes", "Prims.Cons", "Prims.Nil", "Prims._assert", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "FStar.Classical.move_requires", "Prims.op_Negation", "Vale.Transformers.InstructionReorder.erroring_option_state", "Vale.Transformers.InstructionReorder.lemma_not_ok_propagate_codes", "Vale.Transformers.InstructionReorder.lemma_code_exchange_allowed", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Vale.Transformers.InstructionReorder.lemma_bubble_to_top", "Vale.Def.PossiblyMonad.possibly", "Vale.Transformers.InstructionReorder.bubble_to_top", "FStar.List.Tot.Base.index", "FStar.Pervasives.Native.Some", "Vale.Def.PossiblyMonad.uu___is_Ok", "Prims.squash", "FStar.Pervasives.Native.uu___is_Some", "Vale.Transformers.InstructionReorder.equiv_states", "Prims.logical", "FStar.Pervasives.pattern" ]
[ "recursion" ]
false
false
true
false
false
let rec lemma_bubble_to_top (cs: codes) (i: nat{i < L.length cs}) (fuel: nat) (s s': machine_state) : Lemma (requires ((s'.ms_ok) /\ (Some s' == machine_eval_codes cs fuel s) /\ (Ok? (bubble_to_top cs i)))) (ensures (let x = L.index cs i in let Ok xs = bubble_to_top cs i in let s1' = machine_eval_code x fuel s in (Some? s1') /\ (let Some s1 = s1' in let s2' = machine_eval_codes xs fuel s1 in (Some? s2') /\ (let Some s2 = s2' in equiv_states s' s2)))) =
match cs with | [_] -> () | h :: t -> let x = L.index cs i in let Ok xs = bubble_to_top cs i in if i = 0 then () else (let Some s_h = machine_eval_code h fuel s in lemma_bubble_to_top (L.tl cs) (i - 1) fuel s_h s'; let Some s_h_x = machine_eval_code x fuel s_h in let Some s_hx = machine_eval_codes [h; x] fuel s in assert (s_h_x == s_hx); lemma_code_exchange_allowed x h fuel s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes (L.tl xs) fuel) s_hx; assert (s_hx.ms_ok); let Some s_xh = machine_eval_codes [x; h] fuel s in lemma_eval_codes_equiv_states (L.tl xs) fuel s_hx s_xh)
false
MiniParse.Spec.Int.Aux.fst
MiniParse.Spec.Int.Aux.encode_u16
val encode_u16 (x: U16.t) : Tot (U8.t * U8.t)
val encode_u16 (x: U16.t) : Tot (U8.t * U8.t)
let encode_u16 (x: U16.t) : Tot (U8.t * U8.t) = let lo = Cast.uint16_to_uint8 x in let hi = Cast.uint16_to_uint8 (x `U16.div` 256us) in (lo, hi)
{ "file_name": "examples/miniparse/MiniParse.Spec.Int.Aux.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 10, "end_line": 31, "start_col": 0, "start_line": 28 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module MiniParse.Spec.Int.Aux module U8 = FStar.UInt8 module U16 = FStar.UInt16 module Cast = FStar.Int.Cast inline_for_extraction let decode_u16 (lohi: U8.t * U8.t) : Tot U16.t = let (lo, hi) = lohi in Cast.uint8_to_uint16 lo `U16.add` (256us `U16.mul` Cast.uint8_to_uint16 hi)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Int.Cast.fst.checked" ], "interface_file": false, "source_file": "MiniParse.Spec.Int.Aux.fst" }
[ { "abbrev": true, "full_module": "FStar.Int.Cast", "short_module": "Cast" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "MiniParse.Spec.Int", "short_module": null }, { "abbrev": false, "full_module": "MiniParse.Spec.Int", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
x: FStar.UInt16.t -> FStar.UInt8.t * FStar.UInt8.t
Prims.Tot
[ "total" ]
[]
[ "FStar.UInt16.t", "FStar.Pervasives.Native.Mktuple2", "FStar.UInt8.t", "Prims.b2t", "Prims.op_Equality", "Prims.int", "FStar.UInt8.v", "Prims.op_Modulus", "FStar.UInt16.v", "FStar.UInt16.div", "FStar.UInt16.uint_to_t", "Prims.pow2", "FStar.Int.Cast.uint16_to_uint8", "FStar.UInt16.__uint_to_t", "FStar.Pervasives.Native.tuple2" ]
[]
false
false
false
true
false
let encode_u16 (x: U16.t) : Tot (U8.t * U8.t) =
let lo = Cast.uint16_to_uint8 x in let hi = Cast.uint16_to_uint8 (x `U16.div` 256us) in (lo, hi)
false
MiniParse.Spec.Int.Aux.fst
MiniParse.Spec.Int.Aux.decode_u16
val decode_u16 (lohi: (U8.t * U8.t)) : Tot U16.t
val decode_u16 (lohi: (U8.t * U8.t)) : Tot U16.t
let decode_u16 (lohi: U8.t * U8.t) : Tot U16.t = let (lo, hi) = lohi in Cast.uint8_to_uint16 lo `U16.add` (256us `U16.mul` Cast.uint8_to_uint16 hi)
{ "file_name": "examples/miniparse/MiniParse.Spec.Int.Aux.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 77, "end_line": 25, "start_col": 0, "start_line": 23 }
(* Copyright 2008-2018 Microsoft Research Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) module MiniParse.Spec.Int.Aux module U8 = FStar.UInt8 module U16 = FStar.UInt16 module Cast = FStar.Int.Cast
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt8.fsti.checked", "FStar.UInt16.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Int.Cast.fst.checked" ], "interface_file": false, "source_file": "MiniParse.Spec.Int.Aux.fst" }
[ { "abbrev": true, "full_module": "FStar.Int.Cast", "short_module": "Cast" }, { "abbrev": true, "full_module": "FStar.UInt16", "short_module": "U16" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": false, "full_module": "MiniParse.Spec.Int", "short_module": null }, { "abbrev": false, "full_module": "MiniParse.Spec.Int", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 8, "max_ifuel": 2, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": true, "z3cliopt": [], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
lohi: (FStar.UInt8.t * FStar.UInt8.t) -> FStar.UInt16.t
Prims.Tot
[ "total" ]
[]
[ "FStar.Pervasives.Native.tuple2", "FStar.UInt8.t", "FStar.UInt16.add", "FStar.Int.Cast.uint8_to_uint16", "FStar.UInt16.mul", "FStar.UInt16.__uint_to_t", "FStar.UInt16.t" ]
[]
false
false
false
true
false
let decode_u16 (lohi: U8.t * U8.t) : Tot U16.t =
let lo, hi = lohi in (Cast.uint8_to_uint16 lo) `U16.add` (256us `U16.mul` (Cast.uint8_to_uint16 hi))
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.find_transformation_hints
val find_transformation_hints (c1 c2: codes) : Tot (possibly transformation_hints) (decreases %[metric_for_codes c2;metric_for_codes c1])
val find_transformation_hints (c1 c2: codes) : Tot (possibly transformation_hints) (decreases %[metric_for_codes c2;metric_for_codes c1])
let rec find_transformation_hints (c1 c2:codes) : Tot (possibly transformation_hints) (decreases %[metric_for_codes c2; metric_for_codes c1]) = let e1, e2 = is_empty_codes c1, is_empty_codes c2 in if e1 && e2 then ( return [] ) else if e2 then ( Err ("non empty first code: " ^ fst (print_code (Block c1) 0 gcc)) ) else if e1 then ( Err ("non empty second code: " ^ fst (print_code (Block c2) 0 gcc)) ) else ( let h1 :: t1 = c1 in let h2 :: t2 = c2 in assert (metric_for_codes c2 >= metric_for_code h2); (* OBSERVE *) if is_empty_code h1 then ( find_transformation_hints t1 c2 ) else if is_empty_code h2 then ( find_transformation_hints c1 t2 ) else ( match find_deep_code_transform h2 c1 with | Ok th -> ( match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Unable to find valid movement for : " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | Err reason -> ( let h1 :: t1 = c1 in match h1, h2 with | Block l1, Block l2 -> ( match ( let+ t_hints1 = find_transformation_hints l1 l2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (wrap_diveinat 0 t_hints1 `L.append` t_hints2) ) with | Ok ths -> return ths | Err reason -> find_transformation_hints c1 (l2 `L.append` t2) ) | IfElse co1 (Block tr1) (Block fa1), IfElse co2 (Block tr2) (Block fa2) -> (co1 = co2) /- ("Non-same conditions for IfElse: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block tr2)); (* OBSERVE *) assert (metric_for_code h2 > metric_for_code (Block fa2)); (* OBSERVE *) let+ tr_hints = find_transformation_hints tr1 tr2 in let+ fa_hints = find_transformation_hints fa1 fa2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceIfElse tr_hints fa_hints :: t_hints2) | While co1 (Block bo1), While co2 (Block bo2) -> (co1 = co2) /- ("Non-same conditions for While: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block bo2)); (* OBSERVE *) let+ bo_hints = find_transformation_hints bo1 bo2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceWhile bo_hints :: t_hints2) | Block l1, IfElse _ _ _ | Block l1, While _ _ -> assert (metric_for_codes (l1 `L.append` t1) == metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) assert_norm (metric_for_codes c1 == 2 + metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) let+ t_hints1 = find_transformation_hints (l1 `L.append` t1) c2 in ( match t_hints1 with | [] -> Err "Impossible" | th :: _ -> let th = DiveInAt 0 th in match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Failed during left-unblock for " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | _, Block l2 -> find_transformation_hints c1 (l2 `L.append` t2) | IfElse _ _ _, IfElse _ _ _ | While _ _, While _ _ -> Err ("Found weird non-standard code: " ^ fst (print_code h1 0 gcc)) | _ -> Err ("Find deep code failure. Reason: " ^ reason) ) ) )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 1881, "start_col": 0, "start_line": 1796 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs let increment_hint (th:transformation_hint) : transformation_hint = match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th let rec find_deep_code_transform (c:code) (cs:codes) : possibly transformation_hint = match cs with | [] -> Err ("Not found (during find_deep_code_transform): " ^ fst (print_code c 0 gcc)) | x :: xs -> (* let _ = IO.debug_print_string ( "---------------------------------\n" ^ " c : \n" ^ fst (print_code c 0 gcc) ^ "\n" ^ " x : \n" ^ fst (print_code x 0 gcc) ^ "\n" ^ " xs : \n" ^ fst (print_code (Block xs) 0 gcc) ^ "\n" ^ "---------------------------------\n" ^ "") in *) if is_empty_code x then find_deep_code_transform c xs else ( if eq_codes (fully_unblocked_code x) (fully_unblocked_code c) then ( return (MoveUpFrom 0) ) else ( match x with | Block l -> ( match find_deep_code_transform c l with | Ok t -> return (DiveInAt 0 t) | Err reason -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) | _ -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) ) let rec metric_for_code (c:code) : GTot nat = 1 + ( match c with | Ins _ -> 0 | Block l -> metric_for_codes l | IfElse _ t f -> metric_for_code t + metric_for_code f | While _ b -> metric_for_code b ) and metric_for_codes (c:codes) : GTot nat = match c with | [] -> 0 | x :: xs -> 1 + metric_for_code x + metric_for_codes xs let rec lemma_metric_for_codes_append (c1 c2:codes) : Lemma (ensures (metric_for_codes (c1 `L.append` c2) == metric_for_codes c1 + metric_for_codes c2)) [SMTPat (metric_for_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_metric_for_codes_append xs c2 irreducible (* Our proofs do not depend on how the hints are found. As long as some hints are provided, we validate the hints to perform the transformation and use it. Thus, we make this function [irreducible] to explicitly prevent any of the proofs from
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c1: Vale.X64.Machine_Semantics_s.codes -> c2: Vale.X64.Machine_Semantics_s.codes -> Prims.Tot (Vale.Def.PossiblyMonad.possibly Vale.Transformers.InstructionReorder.transformation_hints)
Prims.Tot
[ "total", "" ]
[]
[ "Vale.X64.Machine_Semantics_s.codes", "Prims.bool", "Prims.op_AmpAmp", "Vale.Def.PossiblyMonad.return", "Vale.Transformers.InstructionReorder.transformation_hints", "Prims.Nil", "Vale.Transformers.InstructionReorder.transformation_hint", "Vale.Def.PossiblyMonad.Err", "Prims.op_Hat", "FStar.Pervasives.Native.fst", "Prims.string", "Prims.int", "Vale.X64.Print_s.print_code", "Vale.X64.Machine_s.Block", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.X64.Bytes_Code_s.ocmp", "Vale.X64.Print_s.gcc", "Vale.X64.Bytes_Code_s.code_t", "Prims.list", "Vale.Transformers.InstructionReorder.is_empty_code", "Vale.Transformers.InstructionReorder.find_transformation_hints", "Vale.Transformers.InstructionReorder.find_deep_code_transform", "Vale.Transformers.InstructionReorder.perform_reordering_with_hint", "Vale.Def.PossiblyMonad.op_let_Plus", "Prims.Cons", "Vale.Def.PossiblyMonad.possibly", "FStar.Pervasives.Native.Mktuple2", "Vale.X64.Machine_s.precode", "FStar.List.Tot.Base.append", "Vale.Transformers.InstructionReorder.wrap_diveinat", "Prims.unit", "Vale.Def.PossiblyMonad.op_Slash_Subtraction", "Prims.op_Equality", "Vale.X64.Print_s.print_cmp", "Vale.Transformers.InstructionReorder.InPlaceIfElse", "Prims._assert", "Prims.b2t", "Prims.op_GreaterThan", "Vale.Transformers.InstructionReorder.metric_for_code", "Vale.Transformers.InstructionReorder.InPlaceWhile", "Vale.Transformers.InstructionReorder.DiveInAt", "FStar.Pervasives.assert_norm", "Prims.eq2", "Vale.Transformers.InstructionReorder.metric_for_codes", "Prims.op_Addition", "FStar.Pervasives.Native.tuple2", "Prims.op_GreaterThanOrEqual", "Vale.Transformers.InstructionReorder.is_empty_codes" ]
[ "recursion" ]
false
false
false
true
false
let rec find_transformation_hints (c1 c2: codes) : Tot (possibly transformation_hints) (decreases %[metric_for_codes c2;metric_for_codes c1]) =
let e1, e2 = is_empty_codes c1, is_empty_codes c2 in if e1 && e2 then (return []) else if e2 then (Err ("non empty first code: " ^ fst (print_code (Block c1) 0 gcc))) else if e1 then (Err ("non empty second code: " ^ fst (print_code (Block c2) 0 gcc))) else (let h1 :: t1 = c1 in let h2 :: t2 = c2 in assert (metric_for_codes c2 >= metric_for_code h2); if is_empty_code h1 then (find_transformation_hints t1 c2) else if is_empty_code h2 then (find_transformation_hints c1 t2) else (match find_deep_code_transform h2 c1 with | Ok th -> (match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Unable to find valid movement for : " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason)) | Err reason -> (let h1 :: t1 = c1 in match h1, h2 with | Block l1, Block l2 -> (match (let+ t_hints1 = find_transformation_hints l1 l2 in let+ t_hints2 = find_transformation_hints t1 t2 in return ((wrap_diveinat 0 t_hints1) `L.append` t_hints2)) with | Ok ths -> return ths | Err reason -> find_transformation_hints c1 (l2 `L.append` t2)) | IfElse co1 (Block tr1) (Block fa1), IfElse co2 (Block tr2) (Block fa2) -> let+ _ = (co1 = co2) /- ("Non-same conditions for IfElse: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")") in assert (metric_for_code h2 > metric_for_code (Block tr2)); assert (metric_for_code h2 > metric_for_code (Block fa2)); let+ tr_hints = find_transformation_hints tr1 tr2 in let+ fa_hints = find_transformation_hints fa1 fa2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceIfElse tr_hints fa_hints :: t_hints2) | While co1 (Block bo1), While co2 (Block bo2) -> let+ _ = (co1 = co2) /- ("Non-same conditions for While: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")") in assert (metric_for_code h2 > metric_for_code (Block bo2)); let+ bo_hints = find_transformation_hints bo1 bo2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceWhile bo_hints :: t_hints2) | Block l1, IfElse _ _ _ | Block l1, While _ _ -> assert (metric_for_codes (l1 `L.append` t1) == metric_for_codes l1 + metric_for_codes t1); assert_norm (metric_for_codes c1 == 2 + metric_for_codes l1 + metric_for_codes t1); let+ t_hints1 = find_transformation_hints (l1 `L.append` t1) c2 in (match t_hints1 with | [] -> Err "Impossible" | th :: _ -> let th = DiveInAt 0 th in match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Failed during left-unblock for " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason)) | _, Block l2 -> find_transformation_hints c1 (l2 `L.append` t2) | IfElse _ _ _, IfElse _ _ _ | While _ _, While _ _ -> Err ("Found weird non-standard code: " ^ fst (print_code h1 0 gcc)) | _ -> Err ("Find deep code failure. Reason: " ^ reason))))
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_eval_code_equiv_states
val lemma_eval_code_equiv_states (c: code) (fuel: nat) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures (let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel;c])
val lemma_eval_code_equiv_states (c: code) (fuel: nat) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures (let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel;c])
let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 504, "start_col": 0, "start_line": 423 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1"
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 10, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
c: Vale.X64.Machine_Semantics_s.code -> fuel: Prims.nat -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.InstructionReorder.equiv_states s1 s2) (ensures (let _ = Vale.X64.Machine_Semantics_s.machine_eval_code c fuel s1, Vale.X64.Machine_Semantics_s.machine_eval_code c fuel s2 in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s1'' s2'' = _ in Vale.Transformers.InstructionReorder.equiv_ostates s1'' s2'') <: Type0)) (decreases %[fuel;c])
FStar.Pervasives.Lemma
[ "lemma", "" ]
[ "lemma_eval_code_equiv_states", "lemma_eval_codes_equiv_states", "lemma_eval_while_equiv_states" ]
[ "Vale.X64.Machine_Semantics_s.code", "Prims.nat", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Vale.Transformers.InstructionReorder.lemma_eval_ins_equiv_states", "Vale.Transformers.InstructionReorder.filt_state", "Prims.unit", "FStar.Pervasives.reveal_opaque", "Vale.X64.Machine_Semantics_s.ins", "FStar.Pervasives.Native.option", "Vale.X64.Machine_Semantics_s.machine_eval_code_ins", "Prims.list", "Vale.X64.Machine_s.precode", "Vale.X64.Bytes_Code_s.ocmp", "Vale.Transformers.InstructionReorder.lemma_eval_codes_equiv_states", "Prims.bool", "Vale.Transformers.InstructionReorder.lemma_eval_code_equiv_states", "Prims._assert", "Vale.Transformers.InstructionReorder.equiv_states", "Prims.eq2", "FStar.Pervasives.Native.tuple2", "Vale.X64.Machine_Semantics_s.machine_eval_ocmp", "Vale.X64.Machine_Semantics_s.ocmp", "Vale.X64.Machine_Semantics_s.eval_ocmp_opaque", "Vale.X64.Machine_Semantics_s.valid_ocmp_opaque", "Vale.Transformers.InstructionReorder.lemma_eval_while_equiv_states", "Prims.squash", "Vale.Transformers.InstructionReorder.equiv_ostates", "FStar.Pervasives.Native.Mktuple2", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "mutual recursion" ]
false
false
true
false
false
let rec lemma_eval_code_equiv_states (c: code) (fuel: nat) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures (let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel;c]) =
match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let s1', b1 = machine_eval_ocmp s1 ifCond in let s2', b2 = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then (lemma_eval_code_equiv_states ifTrue fuel s1' s2') else (lemma_eval_code_equiv_states ifFalse fuel s1' s2') | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_eval_codes_equiv_states
val lemma_eval_codes_equiv_states (cs: codes) (fuel: nat) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures (let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel;cs])
val lemma_eval_codes_equiv_states (cs: codes) (fuel: nat) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures (let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel;cs])
let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 504, "start_col": 0, "start_line": 423 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1"
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 10, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Vale.X64.Machine_Semantics_s.codes -> fuel: Prims.nat -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.InstructionReorder.equiv_states s1 s2) (ensures (let _ = Vale.X64.Machine_Semantics_s.machine_eval_codes cs fuel s1, Vale.X64.Machine_Semantics_s.machine_eval_codes cs fuel s2 in (let FStar.Pervasives.Native.Mktuple2 #_ #_ s1'' s2'' = _ in Vale.Transformers.InstructionReorder.equiv_ostates s1'' s2'') <: Type0)) (decreases %[fuel;cs])
FStar.Pervasives.Lemma
[ "lemma", "" ]
[ "lemma_eval_code_equiv_states", "lemma_eval_codes_equiv_states", "lemma_eval_while_equiv_states" ]
[ "Vale.X64.Machine_Semantics_s.codes", "Prims.nat", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "FStar.Pervasives.Native.option", "Vale.Transformers.InstructionReorder.lemma_eval_codes_equiv_states", "Prims.unit", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Vale.Transformers.InstructionReorder.lemma_eval_code_equiv_states", "Vale.Transformers.InstructionReorder.equiv_states", "Prims.squash", "Vale.Transformers.InstructionReorder.equiv_ostates", "Vale.X64.Machine_Semantics_s.machine_eval_codes", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "mutual recursion" ]
false
false
true
false
false
let rec lemma_eval_codes_equiv_states (cs: codes) (fuel: nat) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures (let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel;cs]) =
match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_purge_empty_codes
val lemma_purge_empty_codes (cs: codes) (fuel: nat) (s: machine_state) : Lemma (ensures (machine_eval_codes cs fuel s == machine_eval_codes (purge_empty_codes cs) fuel s)) (decreases %[fuel;cs])
val lemma_purge_empty_codes (cs: codes) (fuel: nat) (s: machine_state) : Lemma (ensures (machine_eval_codes cs fuel s == machine_eval_codes (purge_empty_codes cs) fuel s)) (decreases %[fuel;cs])
let rec lemma_purge_empty_code (c:code) (fuel:nat) (s:machine_state) : Lemma (ensures (machine_eval_code c fuel s == machine_eval_code (purge_empty_code c) fuel s)) (decreases %[fuel; c; 1]) = match c with | Block l -> lemma_purge_empty_codes l fuel s | IfElse c t f -> let (s', b) = machine_eval_ocmp s c in if b then lemma_purge_empty_code t fuel s' else lemma_purge_empty_code f fuel s' | While _ _ -> lemma_purge_empty_while c fuel s | _ -> () and lemma_purge_empty_codes (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures (machine_eval_codes cs fuel s == machine_eval_codes (purge_empty_codes cs) fuel s)) (decreases %[fuel; cs]) = match cs with | [] -> () | x :: xs -> if is_empty_code x then ( lemma_is_empty_code x fuel s; lemma_purge_empty_codes xs fuel s ) else ( lemma_purge_empty_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s' -> lemma_purge_empty_codes xs fuel s' ) and lemma_purge_empty_while (c:code{While? c}) (fuel:nat) (s0:machine_state) : Lemma (ensures (machine_eval_code c fuel s0 == machine_eval_code (purge_empty_code c) fuel s0)) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s0, b) = machine_eval_ocmp s0 cond in if not b then () else let s_opt = machine_eval_code body (fuel - 1) s0 in lemma_purge_empty_code body (fuel - 1) s0; match s_opt with | None -> () | Some s1 -> if s1.ms_ok then lemma_purge_empty_while c (fuel - 1) s1 else () )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 2201, "start_col": 0, "start_line": 2154 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1" let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) ) #pop-options /// If an exchange is allowed between two instructions based off of /// their read/write sets, then both orderings of the two instructions /// behave exactly the same, as per the previously defined /// [equiv_states] relation. /// /// Note that we require (for the overall proof) a notion of the /// following: /// /// s1 ===== s2 Key: /// | | /// . . + s1, s2, ... : machine_states /// . f1 . f2 + f1, f2 : some function from a /// . . machine_state to a /// | | machine_state /// V V + ===== : equiv_states /// s1' ===== s2' /// /// However, proving with the [equiv_states s1 s2] as part of the /// preconditions requires come complex wrangling and thinking about /// how different states [s1] and [s2] evolve. In particular, we'd /// need to show and write something similar _every_ step of the /// execution of [f1] and [f2]. Instead, we decompose the above /// diagram into the following: /// /// /// s1 ===== s2 /// / \ \ /// . . . /// . f1 . f2 . f2 /// . . . /// / \ \ /// V V V /// s1' ===== s2''===== s2' /// /// /// We now have the ability to decompose the left "triangular" portion /// which is similar to the rectangular diagram above, except the /// issue of having to manage both [s1] and [s2] is mitigated. Next, /// if we look at the right "parallelogram" portion of the diagram, we /// see that this is just the same as saying "running [f2] on /// [equiv_states] leads to [equiv_states]" which is something that is /// easier to prove. /// /// All the parallelogram proofs have already been completed by this /// point in the file, so only the triangular portions remain (and the /// one proof that links the two up into a single diagram as above). unfold let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s let commutes (s:machine_state) (f1 f2:st unit) : GTot Type0 = equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s) let rec lemma_disjoint_implies_unchanged_at (reads changes:list location) (s1 s2:machine_state) : Lemma (requires (!!(disjoint_locations reads changes) /\ unchanged_except changes s1 s2)) (ensures (unchanged_at reads s1 s2)) = match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2 let rec lemma_disjoint_location_from_locations_append (a:location) (as1 as2:list location) : Lemma ( (!!(disjoint_location_from_locations a as1) /\ !!(disjoint_location_from_locations a as2)) <==> (!!(disjoint_location_from_locations a (as1 `L.append` as2)))) = match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2 let lemma_unchanged_except_transitive (a12 a23:list location) (s1 s2 s3:machine_state) : Lemma (requires (unchanged_except a12 s1 s2 /\ unchanged_except a23 s2 s3)) (ensures (unchanged_except (a12 `L.append` a23) s1 s3)) = let aux a : Lemma (requires (!!(disjoint_location_from_locations a (a12 `L.append` a23)))) (ensures (eval_location a s1 == eval_location a s3)) = lemma_disjoint_location_from_locations_append a a12 a23 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) let lemma_unchanged_except_append_symmetric (a1 a2:list location) (s1 s2:machine_state) : Lemma (requires (unchanged_except (a1 `L.append` a2) s1 s2)) (ensures (unchanged_except (a2 `L.append` a1) s1 s2)) = let aux a : Lemma (requires ( (!!(disjoint_location_from_locations a (a1 `L.append` a2))) \/ (!!(disjoint_location_from_locations a (a2 `L.append` a1))))) (ensures (eval_location a s1 == eval_location a s2)) = lemma_disjoint_location_from_locations_append a a1 a2; lemma_disjoint_location_from_locations_append a a2 a1 in FStar.Classical.forall_intro (FStar.Classical.move_requires aux) #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_disjoint_location_from_locations_mem (a1 a2:list location) (a:location) : Lemma (requires ( (L.mem a a1) /\ !!(disjoint_locations a1 a2))) (ensures ( !!(disjoint_location_from_locations a a2))) = match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a #pop-options #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_constant_on_execution_mem (locv:locations_with_values) (f:st unit) (s:machine_state) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( (constant_on_execution locv f s) /\ ((run f s).ms_ok) /\ ((| l, v |) `L.mem` locv))) (ensures ( (eval_location l (run f s) == raise_location_val_eqt v))) = match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else ( lemma_constant_on_execution_mem xs f s l v ) #pop-options let rec lemma_disjoint_location_from_locations_mem1 (a:location) (as0:locations) : Lemma (requires (not (L.mem a as0))) (ensures (!!(disjoint_location_from_locations a as0))) = match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs let rec value_of_const_loc (lv:locations_with_values) (l:location_eq{ L.mem l (locations_of_locations_with_values lv) }) : location_val_eqt l = let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l let rec lemma_write_same_constants_append (c1 c1' c2:locations_with_values) : Lemma (ensures ( !!(write_same_constants (c1 `L.append` c1') c2) = ( !!(write_same_constants c1 c2) && !!(write_same_constants c1' c2)))) = match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2 let rec lemma_write_same_constants_mem_both (c1 c2:locations_with_values) (l:location_eq) : Lemma (requires (!!(write_same_constants c1 c2) /\ L.mem l (locations_of_locations_with_values c1) /\ L.mem l (locations_of_locations_with_values c2))) (ensures (value_of_const_loc c1 l = value_of_const_loc c2 l)) = let x :: xs = c1 in let y :: ys = c2 in if dfst x = l then ( if dfst y = l then () else ( lemma_write_same_constants_symmetric c1 c2; lemma_write_same_constants_symmetric ys c1; lemma_write_same_constants_mem_both c1 ys l ) ) else ( lemma_write_same_constants_mem_both xs c2 l ) let rec lemma_value_of_const_loc_mem (c:locations_with_values) (l:location_eq) (v:location_val_eqt l) : Lemma (requires ( L.mem l (locations_of_locations_with_values c) /\ value_of_const_loc c l = v)) (ensures (L.mem (|l,v|) c)) = let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_unchanged_at_mem (as0:list location) (a:location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (L.mem a as0))) (ensures ( (eval_location a s1 == eval_location a s2))) = match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2 #pop-options let lemma_unchanged_at_combine (a1 a2:locations) (c1 c2:locations_with_values) (sa1 sa2 sb1 sb2:machine_state) : Lemma (requires ( !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2))) (ensures ( (unchanged_at (a1 `L.append` a2) sb1 sb2))) = let precond = !!(write_exchange_allowed a1 a2 c1 c2) /\ (unchanged_at (locations_of_locations_with_values c1) sb1 sb2) /\ (unchanged_at (locations_of_locations_with_values c2) sb1 sb2) /\ (unchanged_at a1 sa1 sb2) /\ (unchanged_except a2 sa1 sb1) /\ (unchanged_at a2 sa2 sb1) /\ (unchanged_except a1 sa2 sb2) in let aux1 a : Lemma (requires (L.mem a a1 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c1) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c1) a sb1 sb2 ) else ( lemma_for_all_elim (aux_write_exchange_allowed a2 c1 c2) a1; L.mem_memP a a1; assert !!(aux_write_exchange_allowed a2 c1 c2 a); assert !!(disjoint_location_from_locations a a2); assert (eval_location a sb1 == eval_location a sa1); lemma_unchanged_at_mem a1 a sa1 sb2 ) in let aux2 a : Lemma (requires (L.mem a a2 /\ precond)) (ensures (eval_location a sb1 == eval_location a sb2)) = if L.mem a (locations_of_locations_with_values c2) then ( lemma_unchanged_at_mem (locations_of_locations_with_values c2) a sb1 sb2 ) else ( lemma_write_exchange_allowed_symmetric a1 a2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed a1 c2 c1) a2; L.mem_memP a a2; assert !!(aux_write_exchange_allowed a1 c2 c1 a); assert !!(disjoint_location_from_locations a a1); assert (eval_location a sb2 == eval_location a sa2); lemma_unchanged_at_mem a2 a sa2 sb1 ) in let rec aux a1' a1'' a2' a2'' : Lemma (requires (a1' `L.append` a1'' == a1 /\ a2' `L.append` a2'' == a2 /\ precond)) (ensures (unchanged_at (a1'' `L.append` a2'') sb1 sb2)) (decreases %[a1''; a2'']) = match a1'' with | [] -> ( match a2'' with | [] -> () | y :: ys -> ( L.append_l_cons y ys a2'; L.append_mem a2' a2'' y; aux2 y; aux a1' a1'' (a2' `L.append` [y]) ys ) ) | x :: xs -> L.append_l_cons x xs a1'; L.append_mem a1' a1'' x; aux1 x; aux (a1' `L.append` [x]) xs a2' a2'' in aux [] a1 [] a2 let lemma_unchanged_except_same_transitive (as0:list location) (s1 s2 s3:machine_state) : Lemma (requires ( (unchanged_except as0 s1 s2) /\ (unchanged_except as0 s2 s3))) (ensures ( (unchanged_except as0 s1 s3))) = () let rec lemma_unchanged_at_and_except (as0:list location) (s1 s2:machine_state) : Lemma (requires ( (unchanged_at as0 s1 s2) /\ (unchanged_except as0 s1 s2))) (ensures ( (unchanged_except [] s1 s2))) = match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2 let lemma_equiv_states_when_except_none (s1 s2:machine_state) (ok:bool) : Lemma (requires ( (unchanged_except [] s1 s2))) (ensures ( (equiv_states ({s1 with ms_ok=ok}) ({s2 with ms_ok=ok})))) = assert_norm (cf s2.ms_flags == cf (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) assert_norm (overflow s2.ms_flags == overflow (filter_state s2 s1.ms_flags ok []).ms_flags); (* OBSERVE *) lemma_locations_complete s1 s2 s1.ms_flags ok [] #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_mem_not_disjoint (a:location) (as1 as2:list location) : Lemma (requires (L.mem a as1 /\ L.mem a as2)) (ensures ( (not !!(disjoint_locations as1 as2)))) = match as1, as2 with | [_], [_] -> () | [_], y :: ys -> if a = y then () else ( lemma_mem_not_disjoint a as1 ys ) | x :: xs, y :: ys -> if a = x then ( if a = y then () else ( lemma_mem_not_disjoint a as1 ys; lemma_disjoint_locations_symmetric as1 as2; lemma_disjoint_locations_symmetric as1 ys ) ) else ( lemma_mem_not_disjoint a xs as2 ) #pop-options let lemma_bounded_effects_means_same_ok (rw:rw_set) (f:st unit) (s1 s2 s1' s2':machine_state) : Lemma (requires ( (bounded_effects rw f) /\ (s1.ms_ok = s2.ms_ok) /\ (unchanged_at rw.loc_reads s1 s2) /\ (s1' == run f s1) /\ (s2' == run f s2))) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok))) = () let lemma_both_not_ok (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( (run2 f1 f2 s).ms_ok = (run2 f2 f1 s).ms_ok)) = if (run f1 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw2.loc_reads rw1.loc_writes s (run f1 s) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else () #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let lemma_constant_on_execution_stays_constant (f1 f2:st unit) (rw1 rw2:rw_set) (s s1 s2:machine_state) : Lemma (requires ( s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes))) (ensures ( unchanged_at (locations_of_locations_with_values rw1.loc_constant_writes) (run f1 s1) (run f2 s2) /\ unchanged_at (locations_of_locations_with_values rw2.loc_constant_writes) (run f1 s1) (run f2 s2))) = let precond = s1.ms_ok /\ s2.ms_ok /\ (run f1 s1).ms_ok /\ (run f2 s2).ms_ok /\ (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ (s1 == run f2 s) /\ (s2 == run f1 s) /\ !!(write_exchange_allowed rw1.loc_writes rw2.loc_writes rw1.loc_constant_writes rw2.loc_constant_writes) in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in let cv1, cv2 = locations_of_locations_with_values rw1.loc_constant_writes, locations_of_locations_with_values rw2.loc_constant_writes in let rec aux1 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c1)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f1 s1 l v; lemma_for_all_elim (aux_write_exchange_allowed w2 c1 c2) w1; assert (eval_location l (run f1 s1) == raise_location_val_eqt v); if L.mem l w2 then ( L.mem_memP l w1; assert !!(aux_write_exchange_allowed w2 c1 c2 l); lemma_mem_not_disjoint l [l] w2; assert (not !!(disjoint_location_from_locations l w2)); //assert (L.mem (coerce l) cv2); assert !!(write_same_constants c1 c2); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c2; lemma_write_same_constants_mem_both lv' c2 l; lemma_value_of_const_loc_mem c2 l v; lemma_constant_on_execution_mem c2 f2 s2 l v ) else ( assert (constant_on_execution c1 f1 s); lemma_constant_on_execution_mem (lv `L.append` lv') f1 s l v; assert (eval_location l (run f1 s) == raise_location_val_eqt v); assert (unchanged_except w2 s2 (run f2 s2)); lemma_disjoint_location_from_locations_mem1 l w2; assert (!!(disjoint_location_from_locations l w2)); assert (eval_location l (run f2 s2) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux1 (lv `L.append` [x]) xs in let rec aux2 lv lv' : Lemma (requires ( (precond) /\ lv `L.append` lv' == c2)) (ensures ( (unchanged_at (locations_of_locations_with_values lv') (run f1 s1) (run f2 s2)))) (decreases %[lv']) = match lv' with | [] -> () | x :: xs -> let (|l,v|) = x in L.append_mem lv lv' x; lemma_constant_on_execution_mem (lv `L.append` lv') f2 s2 l v; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_for_all_elim (aux_write_exchange_allowed w1 c2 c1) w2; assert (eval_location l (run f2 s2) == raise_location_val_eqt v); if L.mem l w1 then ( L.mem_memP l w2; assert !!(aux_write_exchange_allowed w1 c2 c1 l); lemma_mem_not_disjoint l [l] w1; assert (not !!(disjoint_location_from_locations l w1)); //assert (L.mem (coerce l) cv1); assert !!(write_same_constants c2 c1); assert (value_of_const_loc lv' l = v); lemma_write_same_constants_append lv lv' c1; lemma_write_same_constants_mem_both lv' c1 l; lemma_value_of_const_loc_mem c1 l v; lemma_constant_on_execution_mem c1 f1 s1 l v ) else ( assert (constant_on_execution c2 f2 s); lemma_constant_on_execution_mem (lv `L.append` lv') f2 s l v; assert (eval_location l (run f2 s) == raise_location_val_eqt v); assert (unchanged_except w1 s1 (run f1 s1)); lemma_disjoint_location_from_locations_mem1 l w1; assert (!!(disjoint_location_from_locations l w1)); assert (eval_location l (run f1 s1) == raise_location_val_eqt v) ); L.append_l_cons x xs lv; aux2 (lv `L.append` [x]) xs in aux1 [] c1; aux2 [] c2 #pop-options let lemma_commute (f1 f2:st unit) (rw1 rw2:rw_set) (s:machine_state) : Lemma (requires ( (bounded_effects rw1 f1) /\ (bounded_effects rw2 f2) /\ !!(rw_exchange_allowed rw1 rw2))) (ensures ( equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s))) = let s12 = run2 f1 f2 s in let s21 = run2 f2 f1 s in if not s12.ms_ok || not s21.ms_ok then ( lemma_both_not_ok f1 f2 rw1 rw2 s ) else ( let s1 = run f1 s in let s2 = run f2 s in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in assert (s12 == run f2 s1 /\ s21 == run f1 s2); lemma_disjoint_implies_unchanged_at r1 w2 s s2; lemma_disjoint_implies_unchanged_at r2 w1 s s1; assert (unchanged_at w1 s1 s21); assert (unchanged_at w2 s2 s12); assert (unchanged_except w2 s s2); assert (unchanged_except w1 s s1); assert (unchanged_except w2 s1 s12); assert (unchanged_except w1 s2 s21); lemma_unchanged_except_transitive w1 w2 s s1 s12; assert (unchanged_except (w1 `L.append` w2) s s12); lemma_unchanged_except_transitive w2 w1 s s2 s21; assert (unchanged_except (w2 `L.append` w1) s s21); lemma_unchanged_except_append_symmetric w1 w2 s s12; lemma_unchanged_except_append_symmetric w2 w1 s s21; lemma_unchanged_except_same_transitive (w1 `L.append` w2) s s12 s21; lemma_write_exchange_allowed_symmetric w1 w2 c1 c2; lemma_constant_on_execution_stays_constant f2 f1 rw2 rw1 s s1 s2; lemma_unchanged_at_combine w1 w2 c1 c2 s1 s2 s12 s21; lemma_unchanged_at_and_except (w1 `L.append` w2) s12 s21; assert (unchanged_except [] s12 s21); assert (s21.ms_ok = s12.ms_ok); lemma_equiv_states_when_except_none s12 s21 s12.ms_ok; assert (equiv_states (run2 f1 f2 s) (run2 f2 f1 s)) ) let wrap_ss (f:machine_state -> machine_state) : st unit = let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s) let wrap_sos (f:machine_state -> option machine_state) : st unit = fun s -> ( match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s' ) let lemma_feq_bounded_effects (rw:rw_set) (f1 f2:st unit) : Lemma (requires (bounded_effects rw f1 /\ FStar.FunctionalExtensionality.feq f1 f2)) (ensures (bounded_effects rw f2)) = let open FStar.FunctionalExtensionality in assert (only_affects rw.loc_writes f2); let rec aux w s : Lemma (requires (feq f1 f2 /\ constant_on_execution w f1 s)) (ensures (constant_on_execution w f2 s)) [SMTPat (constant_on_execution w f2 s)] = match w with | [] -> () | x :: xs -> aux xs s in assert (forall s. {:pattern (constant_on_execution rw.loc_constant_writes f2 s)} constant_on_execution rw.loc_constant_writes f2 s); assert (forall l v. {:pattern (L.mem (|l,v|) rw.loc_constant_writes); (L.mem l rw.loc_writes)} L.mem (|l,v|) rw.loc_constant_writes ==> L.mem l rw.loc_writes); assert ( forall s1 s2. {:pattern (run f2 s1); (run f2 s2)} ( (s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2) ==> ( ((run f2 s1).ms_ok = (run f2 s2).ms_ok) /\ ((run f2 s1).ms_ok ==> unchanged_at rw.loc_writes (run f2 s1) (run f2 s2)) ) ) ) let rec safely_bounded_code_p (c:code) : bool = match c with | Ins i -> safely_bounded i | Block l -> safely_bounded_codes_p l | IfElse c t f -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p t && safely_bounded_code_p f *) | While c b -> false (* Temporarily disabled. TODO: Re-enable this. safely_bounded_code_p b *) and safely_bounded_codes_p (l:codes) : bool = match l with | [] -> true | x :: xs -> safely_bounded_code_p x && safely_bounded_codes_p xs type safely_bounded_ins = (i:ins{safely_bounded i}) type safely_bounded_code = (c:code{safely_bounded_code_p c}) type safely_bounded_codes = (c:codes{safely_bounded_codes_p c}) let lemma_machine_eval_ins_bounded_effects (i:safely_bounded_ins) : Lemma (ensures (bounded_effects (rw_set_of_ins i) (wrap_ss (machine_eval_ins i)))) = lemma_machine_eval_ins_st_bounded_effects i; lemma_feq_bounded_effects (rw_set_of_ins i) (machine_eval_ins_st i) (wrap_ss (machine_eval_ins i)) let lemma_machine_eval_ins_st_exchange (i1 i2 : ins) (s : machine_state) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (commutes s (machine_eval_ins_st i1) (machine_eval_ins_st i2))) = lemma_machine_eval_ins_st_bounded_effects i1; lemma_machine_eval_ins_st_bounded_effects i2; let rw1 = rw_set_of_ins i1 in let rw2 = rw_set_of_ins i2 in lemma_commute (machine_eval_ins_st i1) (machine_eval_ins_st i2) rw1 rw2 s let lemma_instruction_exchange' (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (machine_eval_ins i1 s1), machine_eval_ins i1 (machine_eval_ins i2 s2) in equiv_states_or_both_not_ok s1' s2'))) = lemma_machine_eval_ins_st_exchange i1 i2 s1; lemma_eval_ins_equiv_states i2 s1 s2; lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s1) (machine_eval_ins i2 s2) let lemma_instruction_exchange (i1 i2 : ins) (s1 s2 : machine_state) : Lemma (requires ( !!(ins_exchange_allowed i1 i2) /\ (equiv_states s1 s2))) (ensures ( (let s1', s2' = machine_eval_ins i2 (filt_state (machine_eval_ins i1 (filt_state s1))), machine_eval_ins i1 (filt_state (machine_eval_ins i2 (filt_state s2))) in equiv_states_or_both_not_ok s1' s2'))) = lemma_eval_ins_equiv_states i1 s1 (filt_state s1); lemma_eval_ins_equiv_states i2 s2 (filt_state s2); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 (filt_state s1)) (filt_state (machine_eval_ins i1 (filt_state s1))); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 (filt_state s2)) (filt_state (machine_eval_ins i2 (filt_state s2))); lemma_eval_ins_equiv_states i2 (machine_eval_ins i1 s1) (machine_eval_ins i1 (filt_state s1)); lemma_eval_ins_equiv_states i1 (machine_eval_ins i2 s2) (machine_eval_ins i2 (filt_state s2)); lemma_instruction_exchange' i1 i2 s1 s2 /// Not-ok states lead to erroring states upon execution #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec lemma_not_ok_propagate_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 1]) = match c with | Ins _ -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins | Block l -> lemma_not_ok_propagate_codes l fuel s | IfElse ifCond ifTrue ifFalse -> let (s', b) = machine_eval_ocmp s ifCond in if b then lemma_not_ok_propagate_code ifTrue fuel s' else lemma_not_ok_propagate_code ifFalse fuel s' | While _ _ -> lemma_not_ok_propagate_while c fuel s and lemma_not_ok_propagate_codes (l:codes) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_codes l fuel s))) (decreases %[fuel; l]) = match l with | [] -> () | x :: xs -> lemma_not_ok_propagate_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s -> lemma_not_ok_propagate_codes xs fuel s and lemma_not_ok_propagate_while (c:code{While? c}) (fuel:nat) (s:machine_state) : Lemma (requires (not s.ms_ok)) (ensures (erroring_option_state (machine_eval_code c fuel s))) (decreases %[fuel; c; 0]) = if fuel = 0 then () else ( let While cond body = c in let (s, b) = machine_eval_ocmp s cond in if not b then () else ( lemma_not_ok_propagate_code body (fuel - 1) s ) ) #pop-options /// Given that we have bounded instructions, we can compute bounds on /// [code] and [codes]. let rec rw_set_of_code (c:safely_bounded_code) : rw_set = match c with | Ins i -> rw_set_of_ins i | Block l -> rw_set_of_codes l | IfElse c t f -> add_r_to_rw_set (locations_of_ocmp c) (rw_set_in_parallel (rw_set_of_code t) (rw_set_of_code f)) | While c b -> { add_r_to_rw_set (locations_of_ocmp c) (rw_set_of_code b) with loc_constant_writes = [] (* Since the loop may not execute, we are not sure of any constant writes *) } and rw_set_of_codes (c:safely_bounded_codes) : rw_set = match c with | [] -> { loc_reads = []; loc_writes = []; loc_constant_writes = []; } | x :: xs -> rw_set_in_series (rw_set_of_code x) (rw_set_of_codes xs) let lemma_bounded_effects_on_functional_extensionality (rw:rw_set) (f1 f2:st unit) : Lemma (requires (FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1)) (ensures (bounded_effects rw f2)) = let pre = FStar.FunctionalExtensionality.feq f1 f2 /\ bounded_effects rw f1 in assert (only_affects rw.loc_writes f1 <==> only_affects rw.loc_writes f2); let rec aux c s : Lemma (requires (pre /\ constant_on_execution c f1 s)) (ensures (constant_on_execution c f2 s)) = match c with | [] -> () | (|l,v|) :: xs -> aux xs s in let aux = FStar.Classical.move_requires (aux rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 s2 : Lemma (requires (pre /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run f2 s1).ms_ok)) (ensures (unchanged_at rw.loc_writes (run f2 s1) (run f2 s2))) = () in let aux s1 = FStar.Classical.move_requires (aux s1) in FStar.Classical.forall_intro_2 aux let lemma_only_affects_to_unchanged_except locs f s : (* REVIEW: Why is this even needed?! *) Lemma (requires (only_affects locs f /\ (run f s).ms_ok)) (ensures (unchanged_except locs s (run f s))) = () let lemma_equiv_code_codes (c:code) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in equiv_states_or_both_not_ok (run (f1;* f2) s) (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_1 = run f1 s in let s_1_2 = run f2 s_1 in let s_12 = run (f1;* f2) s in let s12 = run f12 s in assert (s_12 == {s_1_2 with ms_ok = s.ms_ok && s_1.ms_ok && s_1_2.ms_ok}); if s.ms_ok then ( if s_1.ms_ok then () else ( lemma_not_ok_propagate_codes cs fuel s_1 ) ) else ( lemma_not_ok_propagate_code c fuel s; lemma_not_ok_propagate_codes cs fuel s_1; lemma_not_ok_propagate_codes (c :: cs) fuel s ) let lemma_bounded_effects_code_codes_aux1 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s a : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (bounded_effects rw (f1 ;* f2)) /\ !!(disjoint_location_from_locations a rw.loc_writes) /\ (run f12 s).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in eval_location a s == eval_location a (run f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let s_12 = run (f1;*f2) s in let s12 = run f12 s in lemma_equiv_code_codes c cs fuel s; assert (equiv_states_or_both_not_ok s_12 s12); lemma_only_affects_to_unchanged_except rw.loc_writes f s let rec lemma_bounded_effects_code_codes_aux2 (c:code) (cs:codes) (fuel:nat) cw s : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (constant_on_execution cw (f1;*f2) s))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in lemma_equiv_code_codes c cs fuel s; if (run f s).ms_ok then ( match cw with | [] -> () | (|l, v|) :: xs -> ( lemma_bounded_effects_code_codes_aux2 c cs fuel xs s ) ) else () let lemma_unchanged_at_reads_implies_both_ok_equal (rw:rw_set) (f:st unit) s1 s2 : (* REVIEW: Why is this necessary?! *) Lemma (requires (bounded_effects rw f /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( ((run f s1).ms_ok = (run f s2).ms_ok) /\ ((run f s1).ms_ok ==> unchanged_at rw.loc_writes (run f s1) (run f s2)))) = () let lemma_bounded_effects_code_codes_aux3 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2)) (ensures ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (run f12 s1).ms_ok = (run f12 s2).ms_ok /\ (run (f1 ;* f2) s1).ms_ok = (run f12 s1).ms_ok)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; assert ((run f s1).ms_ok == (run f12 s1).ms_ok); assert ((run f s2).ms_ok == (run f12 s2).ms_ok); lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2 let lemma_bounded_effects_code_codes_aux4 (c:code) (cs:codes) (rw:rw_set) (fuel:nat) s1 s2 : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)) /\ s1.ms_ok = s2.ms_ok /\ unchanged_at rw.loc_reads s1 s2 /\ (run (f1 ;* f2) s1).ms_ok)) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in unchanged_at rw.loc_writes (run f12 s1) (run f12 s2))) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = (f1;*f2) in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in lemma_equiv_code_codes c cs fuel s1; lemma_equiv_code_codes c cs fuel s2; lemma_unchanged_at_reads_implies_both_ok_equal rw f s1 s2; assert (run f12 s1).ms_ok; assert (run f12 s2).ms_ok; assert (unchanged_at rw.loc_writes (run f s1) (run f s2)); assert (run f s1 == run f12 s1); assert (run f s2 == run f12 s2) let lemma_bounded_effects_code_codes (c:code) (cs:codes) (rw:rw_set) (fuel:nat) : Lemma (requires ( let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in (bounded_effects rw (f1 ;* f2)))) (ensures ( let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) = let open Vale.X64.Machine_Semantics_s in let f1 = wrap_sos (machine_eval_code c fuel) in let f2 = wrap_sos (machine_eval_codes cs fuel) in let f = f1;*f2 in let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in let pre = bounded_effects rw f in let aux s = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux1 c cs rw fuel s) in FStar.Classical.forall_intro_2 aux; let aux = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux2 c cs fuel rw.loc_constant_writes) in FStar.Classical.forall_intro aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux3 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux; let aux s1 = FStar.Classical.move_requires (lemma_bounded_effects_code_codes_aux4 c cs rw fuel s1) in FStar.Classical.forall_intro_2 aux let rec lemma_bounded_code (c:safely_bounded_code) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_code c) (wrap_sos (machine_eval_code c fuel)))) (decreases %[c]) = match c with | Ins i -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_machine_eval_code_Ins_bounded_effects i fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_ins i) (fun s -> (), (Some?.v (machine_eval_code_ins_def i s))) (wrap_sos (machine_eval_code c fuel)) | Block l -> lemma_bounded_codes l fuel; lemma_bounded_effects_on_functional_extensionality (rw_set_of_codes l) (wrap_sos (machine_eval_codes l fuel)) (wrap_sos (machine_eval_code (Block l) fuel)) | IfElse c t f -> () | While c b -> () and lemma_bounded_codes (c:safely_bounded_codes) (fuel:nat) : Lemma (ensures (bounded_effects (rw_set_of_codes c) (wrap_sos (machine_eval_codes c fuel)))) (decreases %[c]) = let open Vale.X64.Machine_Semantics_s in match c with | [] -> () | x :: xs -> lemma_bounded_code x fuel; lemma_bounded_codes xs fuel; lemma_bounded_effects_series (rw_set_of_code x) (rw_set_of_codes xs) (wrap_sos (machine_eval_code x fuel)) (wrap_sos (machine_eval_codes xs fuel)); lemma_bounded_effects_code_codes x xs (rw_set_of_codes c) fuel /// Given that we can perform simple swaps between instructions, we /// can do swaps between [code]s. let code_exchange_allowed (c1 c2:safely_bounded_code) : pbool = rw_exchange_allowed (rw_set_of_code c1) (rw_set_of_code c2) /+> normal (" for instructions " ^ fst (print_code c1 0 gcc) ^ " and " ^ fst (print_code c2 0 gcc)) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 0 --max_ifuel 0" let lemma_code_exchange_allowed (c1 c2:safely_bounded_code) (fuel:nat) (s:machine_state) : Lemma (requires ( !!(code_exchange_allowed c1 c2))) (ensures ( equiv_option_states (machine_eval_codes [c1; c2] fuel s) (machine_eval_codes [c2; c1] fuel s))) = lemma_bounded_code c1 fuel; lemma_bounded_code c2 fuel; let f1 = wrap_sos (machine_eval_code c1 fuel) in let f2 = wrap_sos (machine_eval_code c2 fuel) in lemma_commute f1 f2 (rw_set_of_code c1) (rw_set_of_code c2) s; assert (equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)); let s1 = run f1 s in let s12 = run f2 s1 in let s2 = run f2 s in let s21 = run f1 s2 in allow_inversion (option machine_state); FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s1; FStar.Classical.move_requires (lemma_not_ok_propagate_code c2 fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_code c1 fuel) s2; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c1;c2] fuel) s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes [c2;c1] fuel) s #pop-options /// Given that we can perform simple swaps between [code]s, we can /// define a relation that tells us if some [codes] can be transformed /// into another using only allowed swaps. #push-options "--initial_fuel 2 --max_fuel 2 --initial_ifuel 1 --max_ifuel 1" let rec bubble_to_top (cs:codes) (i:nat{i < L.length cs}) : possibly (cs':codes{ let a, b, c = L.split3 cs i in cs' == L.append a c /\ L.length cs' = L.length cs - 1 }) = match cs with | [_] -> return [] | h :: t -> if i = 0 then ( return t ) else ( let x = L.index cs i in if not (safely_bounded_code_p x) then ( Err ("Cannot safely move " ^ fst (print_code x 0 gcc)) ) else ( if not (safely_bounded_code_p h) then ( Err ("Cannot safely move beyond " ^ fst (print_code h 0 gcc)) ) else ( match bubble_to_top t (i - 1) with | Err reason -> Err reason | Ok res -> match code_exchange_allowed x h with | Err reason -> Err reason | Ok () -> return (h :: res) ) ) ) #pop-options let rec num_blocks_in_codes (c:codes) : nat = match c with | [] -> 0 | Block l :: t -> 1 + num_blocks_in_codes l + num_blocks_in_codes t | _ :: t -> num_blocks_in_codes t let rec lemma_num_blocks_in_codes_append (c1 c2:codes) : Lemma (ensures (num_blocks_in_codes (c1 `L.append` c2) == num_blocks_in_codes c1 + num_blocks_in_codes c2)) [SMTPat (num_blocks_in_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2 type transformation_hint = | MoveUpFrom : p:nat -> transformation_hint | DiveInAt : p:nat -> q:transformation_hint -> transformation_hint | InPlaceIfElse : ifTrue:transformation_hints -> ifFalse:transformation_hints -> transformation_hint | InPlaceWhile : whileBody:transformation_hints -> transformation_hint and transformation_hints = list transformation_hint let rec string_of_transformation_hint (th:transformation_hint) : Tot string (decreases %[th]) = match th with | MoveUpFrom p -> "(MoveUpFrom " ^ string_of_int p ^ ")" | DiveInAt p q -> "(DiveInAt " ^ string_of_int p ^ " " ^ string_of_transformation_hint q ^ ")" | InPlaceIfElse tr fa -> "(InPlaceIfElse " ^ string_of_transformation_hints tr ^ " " ^ string_of_transformation_hints fa ^ ")" | InPlaceWhile bo -> "(InPlaceWhile " ^ string_of_transformation_hints bo ^ ")" and aux_string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 0]) = match ts with | [] -> "" | x :: xs -> string_of_transformation_hint x ^ "; " ^ aux_string_of_transformation_hints xs and string_of_transformation_hints (ts:transformation_hints) : Tot string (decreases %[ts; 1]) = "[" ^ aux_string_of_transformation_hints ts ^ "]" let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs (* XXX: Copied from List.Tot.Base because of an extraction issue. See https://github.com/FStarLang/FStar/pull/1822. *) val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i = let a, as0 = L.splitAt i l in L.lemma_splitAt_snd_length i l; let b :: c = as0 in a, b, c let rec is_empty_code (c:code) : bool = match c with | Ins _ -> false | Block l -> is_empty_codes l | IfElse _ t f -> false | While _ c -> false and is_empty_codes (c:codes) : bool = match c with | [] -> true | x :: xs -> is_empty_code x && is_empty_codes xs let rec perform_reordering_with_hint (t:transformation_hint) (c:codes) : possibly codes = match c with | [] -> Err "trying to transform empty code" | x :: xs -> if is_empty_codes [x] then perform_reordering_with_hint t xs else ( match t with | MoveUpFrom i -> ( if i < L.length c then ( let+ c'= bubble_to_top c i in return (L.index c i :: c') ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) ) | DiveInAt i t' -> if i < L.length c then ( FStar.List.Pure.lemma_split3_length c i; let left, mid, right = split3 c i in match mid with | Block l -> let+ l' = perform_reordering_with_hint t' l in ( match l' with | [] -> Err "impossible" | y :: ys -> L.append_length left [y]; let+ left' = bubble_to_top (left `L.append` [y]) i in return (y :: (left' `L.append` (Block ys :: right))) ) | _ -> Err ("trying to dive into a non-block : " ^ string_of_transformation_hint t ^ " " ^ fst (print_code (Block c) 0 gcc)) ) else ( Err ("invalid hint : " ^ string_of_transformation_hint t) ) | InPlaceIfElse tht thf -> ( match x with | IfElse c (Block t) (Block f) -> let+ tt = perform_reordering_with_hints tht t in let+ ff = perform_reordering_with_hints thf f in return (IfElse c (Block tt) (Block ff) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) | InPlaceWhile thb -> ( match x with | While c (Block b) -> let+ bb = perform_reordering_with_hints thb b in return (While c (Block bb) :: xs) | _ -> Err ("Invalid hint : " ^ string_of_transformation_hint t ^ " for codes " ^ fst (print_code (Block c) 0 gcc)) ) ) and perform_reordering_with_hints (ts:transformation_hints) (c:codes) : possibly codes = (* let _ = IO.debug_print_string ( "-----------------------------\n" ^ " th : " ^ string_of_transformation_hints ts ^ "\n" ^ " c :\n" ^ fst (print_code (Block c) 0 gcc) ^ "\n" ^ "-----------------------------\n" ^ "") in *) match ts with | [] -> ( if is_empty_codes c then ( return [] ) else ( (* let _ = IO.debug_print_string ( "failed here!!!\n" ^ "\n") in *) Err ("no more transformation hints for " ^ fst (print_code (Block c) 0 gcc)) ) ) | t :: ts' -> let+ c' = perform_reordering_with_hint t c in match c' with | [] -> Err "impossible" | x :: xs -> if is_empty_codes [x] then ( Err "Trying to move 'empty' code." ) else ( (* let _ = IO.debug_print_string ( "dragged up: \n" ^ fst (print_code x 0 gcc) ^ "\n") in *) let+ xs' = perform_reordering_with_hints ts' xs in return (x :: xs') ) (* NOTE: We assume this function since it is not yet exposed. Once exposed from the instructions module, we should be able to remove it from here. Also, note that we don't require any other properties from [eq_ins]. It is an uninterpreted function that simply gives us a "hint" to find equivalent instructions! For testing purposes, we have it set to an [irreducible] function that looks at the printed representation of the instructions. Since it is irreducible, no other function should be able to "look into" the definition of this function, but instead should be limited only to its signature. However, the OCaml extraction _should_ be able to peek inside, and be able to proceed. *) irreducible let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc let rec eq_code (c1 c2:code) : bool = match c1, c2 with | Ins i1, Ins i2 -> eq_ins i1 i2 | Block l1, Block l2 -> eq_codes l1 l2 | IfElse c1 t1 f1, IfElse c2 t2 f2 -> c1 = c2 && eq_code t1 t2 && eq_code f1 f2 | While c1 b1, While c2 b2 -> c1 = c2 && eq_code b1 b2 | _, _ -> false and eq_codes (c1 c2:codes) : bool = match c1, c2 with | [], [] -> true | _, [] | [], _ -> false | x :: xs, y :: ys -> eq_code x y && eq_codes xs ys let rec fully_unblocked_code (c:code) : codes = match c with | Ins i -> [c] | Block l -> fully_unblocked_codes l | IfElse c t f -> [IfElse c (Block (fully_unblocked_code t)) (Block (fully_unblocked_code f))] | While c b -> [While c (Block (fully_unblocked_code b))] and fully_unblocked_codes (c:codes) : codes = match c with | [] -> [] | x :: xs -> fully_unblocked_code x `L.append` fully_unblocked_codes xs let increment_hint (th:transformation_hint) : transformation_hint = match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th let rec find_deep_code_transform (c:code) (cs:codes) : possibly transformation_hint = match cs with | [] -> Err ("Not found (during find_deep_code_transform): " ^ fst (print_code c 0 gcc)) | x :: xs -> (* let _ = IO.debug_print_string ( "---------------------------------\n" ^ " c : \n" ^ fst (print_code c 0 gcc) ^ "\n" ^ " x : \n" ^ fst (print_code x 0 gcc) ^ "\n" ^ " xs : \n" ^ fst (print_code (Block xs) 0 gcc) ^ "\n" ^ "---------------------------------\n" ^ "") in *) if is_empty_code x then find_deep_code_transform c xs else ( if eq_codes (fully_unblocked_code x) (fully_unblocked_code c) then ( return (MoveUpFrom 0) ) else ( match x with | Block l -> ( match find_deep_code_transform c l with | Ok t -> return (DiveInAt 0 t) | Err reason -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) | _ -> let+ th = find_deep_code_transform c xs in return (increment_hint th) ) ) let rec metric_for_code (c:code) : GTot nat = 1 + ( match c with | Ins _ -> 0 | Block l -> metric_for_codes l | IfElse _ t f -> metric_for_code t + metric_for_code f | While _ b -> metric_for_code b ) and metric_for_codes (c:codes) : GTot nat = match c with | [] -> 0 | x :: xs -> 1 + metric_for_code x + metric_for_codes xs let rec lemma_metric_for_codes_append (c1 c2:codes) : Lemma (ensures (metric_for_codes (c1 `L.append` c2) == metric_for_codes c1 + metric_for_codes c2)) [SMTPat (metric_for_codes (c1 `L.append` c2))] = match c1 with | [] -> () | x :: xs -> lemma_metric_for_codes_append xs c2 irreducible (* Our proofs do not depend on how the hints are found. As long as some hints are provided, we validate the hints to perform the transformation and use it. Thus, we make this function [irreducible] to explicitly prevent any of the proofs from reasoning about it. *) let rec find_transformation_hints (c1 c2:codes) : Tot (possibly transformation_hints) (decreases %[metric_for_codes c2; metric_for_codes c1]) = let e1, e2 = is_empty_codes c1, is_empty_codes c2 in if e1 && e2 then ( return [] ) else if e2 then ( Err ("non empty first code: " ^ fst (print_code (Block c1) 0 gcc)) ) else if e1 then ( Err ("non empty second code: " ^ fst (print_code (Block c2) 0 gcc)) ) else ( let h1 :: t1 = c1 in let h2 :: t2 = c2 in assert (metric_for_codes c2 >= metric_for_code h2); (* OBSERVE *) if is_empty_code h1 then ( find_transformation_hints t1 c2 ) else if is_empty_code h2 then ( find_transformation_hints c1 t2 ) else ( match find_deep_code_transform h2 c1 with | Ok th -> ( match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Unable to find valid movement for : " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | Err reason -> ( let h1 :: t1 = c1 in match h1, h2 with | Block l1, Block l2 -> ( match ( let+ t_hints1 = find_transformation_hints l1 l2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (wrap_diveinat 0 t_hints1 `L.append` t_hints2) ) with | Ok ths -> return ths | Err reason -> find_transformation_hints c1 (l2 `L.append` t2) ) | IfElse co1 (Block tr1) (Block fa1), IfElse co2 (Block tr2) (Block fa2) -> (co1 = co2) /- ("Non-same conditions for IfElse: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block tr2)); (* OBSERVE *) assert (metric_for_code h2 > metric_for_code (Block fa2)); (* OBSERVE *) let+ tr_hints = find_transformation_hints tr1 tr2 in let+ fa_hints = find_transformation_hints fa1 fa2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceIfElse tr_hints fa_hints :: t_hints2) | While co1 (Block bo1), While co2 (Block bo2) -> (co1 = co2) /- ("Non-same conditions for While: (" ^ print_cmp co1 0 gcc ^ ") and (" ^ print_cmp co2 0 gcc ^ ")");+ assert (metric_for_code h2 > metric_for_code (Block bo2)); (* OBSERVE *) let+ bo_hints = find_transformation_hints bo1 bo2 in let+ t_hints2 = find_transformation_hints t1 t2 in return (InPlaceWhile bo_hints :: t_hints2) | Block l1, IfElse _ _ _ | Block l1, While _ _ -> assert (metric_for_codes (l1 `L.append` t1) == metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) assert_norm (metric_for_codes c1 == 2 + metric_for_codes l1 + metric_for_codes t1); (* OBSERVE *) let+ t_hints1 = find_transformation_hints (l1 `L.append` t1) c2 in ( match t_hints1 with | [] -> Err "Impossible" | th :: _ -> let th = DiveInAt 0 th in match perform_reordering_with_hint th c1 with | Ok (h1 :: t1) -> let+ t_hints2 = find_transformation_hints t1 t2 in return (th :: t_hints2) | Ok [] -> Err "Impossible" | Err reason -> Err ("Failed during left-unblock for " ^ fst (print_code h2 0 gcc) ^ ". Reason: " ^ reason) ) | _, Block l2 -> find_transformation_hints c1 (l2 `L.append` t2) | IfElse _ _ _, IfElse _ _ _ | While _ _, While _ _ -> Err ("Found weird non-standard code: " ^ fst (print_code h1 0 gcc)) | _ -> Err ("Find deep code failure. Reason: " ^ reason) ) ) ) /// If a transformation can be performed, then the result behaves /// identically as per the [equiv_states] relation. #push-options "--z3rlimit 10 --initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_bubble_to_top (cs : codes) (i:nat{i < L.length cs}) (fuel:nat) (s s' : machine_state) : Lemma (requires ( (s'.ms_ok) /\ (Some s' == machine_eval_codes cs fuel s) /\ (Ok? (bubble_to_top cs i)))) (ensures ( let x = L.index cs i in let Ok xs = bubble_to_top cs i in let s1' = machine_eval_code x fuel s in (Some? s1') /\ ( let Some s1 = s1' in let s2' = machine_eval_codes xs fuel s1 in (Some? s2') /\ ( let Some s2 = s2' in equiv_states s' s2)))) = match cs with | [_] -> () | h :: t -> let x = L.index cs i in let Ok xs = bubble_to_top cs i in if i = 0 then () else ( let Some s_h = machine_eval_code h fuel s in lemma_bubble_to_top (L.tl cs) (i-1) fuel s_h s'; let Some s_h_x = machine_eval_code x fuel s_h in let Some s_hx = machine_eval_codes [h;x] fuel s in assert (s_h_x == s_hx); lemma_code_exchange_allowed x h fuel s; FStar.Classical.move_requires (lemma_not_ok_propagate_codes (L.tl xs) fuel) s_hx; assert (s_hx.ms_ok); let Some s_xh = machine_eval_codes [x;h] fuel s in lemma_eval_codes_equiv_states (L.tl xs) fuel s_hx s_xh ) #pop-options #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_machine_eval_codes_block_to_append (c1 c2 : codes) (fuel:nat) (s:machine_state) : Lemma (ensures (machine_eval_codes (c1 `L.append` c2) fuel s == machine_eval_codes (Block c1 :: c2) fuel s)) = match c1 with | [] -> () | x :: xs -> match machine_eval_code x fuel s with | None -> () | Some s1 -> lemma_machine_eval_codes_block_to_append xs c2 fuel s1 #pop-options let rec lemma_append_single (xs:list 'a) (y:'a) (i:nat) : Lemma (requires (i == L.length xs)) (ensures ( L.length (xs `L.append` [y]) = L.length xs + 1 /\ L.index (xs `L.append` [y]) i == y)) = match xs with | [] -> () | x :: xs -> lemma_append_single xs y (i - 1) #push-options "--initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_is_empty_code (c:code) (fuel:nat) (s:machine_state) : Lemma (requires (is_empty_code c)) (ensures ((machine_eval_code c fuel s) == (machine_eval_codes [] fuel s))) = match c with | Ins _ -> () | Block l -> lemma_is_empty_codes l fuel s | IfElse _ t f -> () | While _ c -> () and lemma_is_empty_codes (cs:codes) (fuel:nat) (s:machine_state) : Lemma (requires (is_empty_codes cs)) (ensures ((machine_eval_codes cs fuel s) == (machine_eval_codes [] fuel s))) = match cs with | [] -> () | x :: xs -> lemma_is_empty_code x fuel s; lemma_is_empty_codes xs fuel s #pop-options #restart-solver #push-options "--z3rlimit 100 --initial_fuel 3 --max_fuel 3 --initial_ifuel 1 --max_ifuel 1" let rec lemma_perform_reordering_with_hint (t:transformation_hint) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (requires ( (Ok? (perform_reordering_with_hint t cs)) /\ (Some? (machine_eval_codes cs fuel s)) /\ (Some?.v (machine_eval_codes cs fuel s)).ms_ok)) (ensures ( let Ok cs' = perform_reordering_with_hint t cs in equiv_ostates (machine_eval_codes cs fuel s) (machine_eval_codes cs' fuel s))) (decreases %[t; fuel; cs]) = let c = cs in let Ok cs' = perform_reordering_with_hint t cs in let Some s' = machine_eval_codes cs fuel s in let x :: xs = cs in if is_empty_codes [x] then ( lemma_is_empty_codes [x] fuel s; match machine_eval_code x fuel s with | None -> () | Some s' -> lemma_eval_codes_equiv_states xs fuel s s'; lemma_perform_reordering_with_hint t xs fuel s ) else ( match t with | MoveUpFrom i -> ( let Ok c' = bubble_to_top c i in lemma_bubble_to_top c i fuel s s' ) | DiveInAt i t' -> ( FStar.List.Pure.lemma_split3_append c i; FStar.List.Pure.lemma_split3_length c i; let left, mid, right = L.split3 c i in let Block l = mid in let Ok (y :: ys) = perform_reordering_with_hint t' l in L.append_length left [y]; let Ok left' = bubble_to_top (left `L.append` [y]) i in // assert (cs' == y :: (left' `L.append` (Block ys :: right))); assert (left `L.append` (mid :: right) == c); L.append_l_cons mid right left; assert ((left `L.append` [mid]) `L.append` right == c); lemma_machine_eval_codes_block_to_append (left `L.append` [mid]) right fuel s; let Some s_1 = machine_eval_code (Block (left `L.append` [mid])) fuel s in assert (Some s_1 == machine_eval_codes (left `L.append` [mid]) fuel s); lemma_machine_eval_codes_block_to_append left [mid] fuel s; let Some s_2 = machine_eval_code (Block left) fuel s in assert (Some s_2 == machine_eval_codes left fuel s); // assert (Some s_1 == machine_eval_codes [mid] fuel s_2); assert (Some s_1 == machine_eval_code (Block l) fuel s_2); assert (Some s_1 == machine_eval_codes l fuel s_2); assert (Some s' == machine_eval_codes right fuel s_1); if s_1.ms_ok then () else lemma_not_ok_propagate_codes right fuel s_1; lemma_perform_reordering_with_hint t' l fuel s_2; // let Some s_11 = machine_eval_codes (y :: ys) fuel s_2 in let Some s_12 = machine_eval_code y fuel s_2 in if s_12.ms_ok then () else lemma_not_ok_propagate_codes ys fuel s_12; assert (Some s_2 == machine_eval_codes left fuel s); assert (Some s_2 == machine_eval_code (Block left) fuel s); assert (Some s_12 == machine_eval_codes (Block left :: [y]) fuel s); lemma_machine_eval_codes_block_to_append left [y] fuel s; assert (Some s_12 == machine_eval_codes (left `L.append` [y]) fuel s); lemma_bubble_to_top (left `L.append` [y]) i fuel s s_12; // lemma_append_single left y i; assert (L.index (left `L.append` [y]) i == y); // let Some s_3 = machine_eval_codes (y :: left') fuel s in assert (equiv_states s_3 s_12); lemma_eval_codes_equiv_states right fuel s_1 s_11; let Some s_0 = machine_eval_codes right fuel s_11 in lemma_eval_codes_equiv_states (Block ys :: right) fuel s_12 s_3; let Some s_00 = machine_eval_codes (Block ys :: right) fuel s_3 in // assert (equiv_states s_00 s'); assert (Some s_3 == machine_eval_code (Block (y :: left')) fuel s); assert (Some s_00 == machine_eval_codes (Block ys :: right) fuel s_3); lemma_machine_eval_codes_block_to_append (y :: left') (Block ys :: right) fuel s ) | InPlaceIfElse tht thf -> ( let IfElse cond c_ift c_iff :: xs = cs in let Block cs_ift, Block cs_iff = c_ift, c_iff in let (s1, b) = machine_eval_ocmp s cond in if b then ( assert (Some s' == machine_eval_codes (c_ift :: xs) fuel s1); let Some s'' = machine_eval_code c_ift fuel s1 in if not s''.ms_ok then (lemma_not_ok_propagate_codes xs fuel s'') else (); lemma_perform_reordering_with_hints tht cs_ift fuel s1; let Some s''' = machine_eval_code (IfElse cond c_ift c_iff) fuel s in let x' :: _ = cs' in let Some s'''' = machine_eval_code x' fuel s in assert (equiv_states s''' s''''); lemma_eval_codes_equiv_states xs fuel s''' s'''' ) else ( let Some s'' = machine_eval_code c_iff fuel s1 in if not s''.ms_ok then (lemma_not_ok_propagate_codes xs fuel s'') else (); lemma_perform_reordering_with_hints thf cs_iff fuel s1; let Some s''' = machine_eval_code (IfElse cond c_ift c_iff) fuel s in let x' :: _ = cs' in let Some s'''' = machine_eval_code x' fuel s in lemma_eval_codes_equiv_states xs fuel s''' s'''' ) ) | InPlaceWhile thb -> ( assert (fuel <> 0); let While cond body :: xs = cs in let Block cs_body = body in let (s0, b) = machine_eval_ocmp s cond in if not b then () else ( let Some s1 = machine_eval_code body (fuel - 1) s0 in if s1.ms_ok then () else lemma_not_ok_propagate_codes xs fuel s1; lemma_perform_reordering_with_hints thb cs_body (fuel - 1) s0; let x' :: xs' = cs' in assert (xs' == xs); let While cond' body' = x' in let cs_body' = body' in let Some s11 = machine_eval_code (While cond body) (fuel - 1) s1 in if s11.ms_ok then () else lemma_not_ok_propagate_codes xs fuel s11; assert (Some s' == machine_eval_codes xs fuel s11); lemma_perform_reordering_with_hint t [While cond body] (fuel - 1) s1; let Some s11' = machine_eval_code x' (fuel - 1) s1 in lemma_eval_codes_equiv_states xs fuel s11 s11'; let Some s'' = machine_eval_codes xs fuel s11' in assert (machine_eval_codes cs fuel s == Some s'); assert (equiv_states s' s''); let Some s1' = machine_eval_code body' (fuel - 1) s0 in lemma_eval_code_equiv_states x' (fuel-1) s1 s1'; let Some s11'' = machine_eval_code x' (fuel-1) s1' in assert (machine_eval_codes cs' fuel s == machine_eval_codes xs fuel s11''); lemma_eval_codes_equiv_states xs fuel s11' s11'' ) ) ) and lemma_perform_reordering_with_hints (ts:transformation_hints) (cs:codes) (fuel:nat) (s:machine_state) : Lemma (requires ( (Ok? (perform_reordering_with_hints ts cs)) /\ (Some? (machine_eval_codes cs fuel s)) /\ (Some?.v (machine_eval_codes cs fuel s)).ms_ok)) (ensures ( let Ok cs' = perform_reordering_with_hints ts cs in equiv_ostates (machine_eval_codes cs fuel s) (machine_eval_codes cs' fuel s))) (decreases %[ts; fuel; cs]) = let c = cs in let Ok cs' = perform_reordering_with_hints ts cs in let Some s' = machine_eval_codes cs fuel s in match ts with | [] -> lemma_is_empty_codes cs fuel s | t :: ts' -> let Ok (x :: xs) = perform_reordering_with_hint t c in lemma_perform_reordering_with_hint t c fuel s; let Ok xs' = perform_reordering_with_hints ts' xs in let Some s1 = machine_eval_code x fuel s in lemma_perform_reordering_with_hints ts' xs fuel s1 #pop-options /// Some convenience functions to be run before the pass, to ensure /// that we don't have miscounting due to empty code. let rec purge_empty_code (c:code) : code = match c with | Block l -> Block (purge_empty_codes l) | IfElse c t f -> IfElse c (purge_empty_code t) (purge_empty_code f) | While c b -> While c (purge_empty_code b) | _ -> c and purge_empty_codes (cs:codes) : codes = match cs with | [] -> [] | x :: xs -> if is_empty_code x then ( purge_empty_codes xs ) else ( purge_empty_code x :: purge_empty_codes xs )
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 2, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cs: Vale.X64.Machine_Semantics_s.codes -> fuel: Prims.nat -> s: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (ensures Vale.X64.Machine_Semantics_s.machine_eval_codes cs fuel s == Vale.X64.Machine_Semantics_s.machine_eval_codes (Vale.Transformers.InstructionReorder.purge_empty_codes cs) fuel s) (decreases %[fuel;cs])
FStar.Pervasives.Lemma
[ "lemma", "" ]
[ "lemma_purge_empty_code", "lemma_purge_empty_codes", "lemma_purge_empty_while" ]
[ "Vale.X64.Machine_Semantics_s.codes", "Prims.nat", "Vale.X64.Machine_Semantics_s.machine_state", "Vale.X64.Bytes_Code_s.code_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "Prims.list", "Vale.Transformers.InstructionReorder.is_empty_code", "Vale.Transformers.InstructionReorder.lemma_purge_empty_codes", "Prims.unit", "Vale.Transformers.InstructionReorder.lemma_is_empty_code", "Prims.bool", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Vale.Transformers.InstructionReorder.lemma_purge_empty_code", "Prims.l_True", "Prims.squash", "Prims.eq2", "FStar.Pervasives.Native.option", "Vale.X64.Machine_Semantics_s.machine_eval_codes", "Vale.Transformers.InstructionReorder.purge_empty_codes", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "mutual recursion" ]
false
false
true
false
false
let rec lemma_purge_empty_codes (cs: codes) (fuel: nat) (s: machine_state) : Lemma (ensures (machine_eval_codes cs fuel s == machine_eval_codes (purge_empty_codes cs) fuel s)) (decreases %[fuel;cs]) =
match cs with | [] -> () | x :: xs -> if is_empty_code x then (lemma_is_empty_code x fuel s; lemma_purge_empty_codes xs fuel s) else (lemma_purge_empty_code x fuel s; match machine_eval_code x fuel s with | None -> () | Some s' -> lemma_purge_empty_codes xs fuel s')
false
Hacl.Test.ECDSA.fst
Hacl.Test.ECDSA.test_sigver512
val test_sigver512 (vec: sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True)
val test_sigver512 (vec: sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True)
let test_sigver512 (vec:sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha512 msg_len msg qxy r s in if result' = result then () else begin LowStar.Printf.(printf "FAIL\n" done); C.exit 1l end; pop_frame() end
{ "file_name": "code/tests/Hacl.Test.ECDSA.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 7, "end_line": 196, "start_col": 0, "start_line": 165 }
module Hacl.Test.ECDSA open FStar.HyperStack.ST open Test.Lowstarize open Lib.IntTypes open Hacl.P256 open Spec.ECDSA.Test.Vectors module L = Test.Lowstarize module B = LowStar.Buffer #set-options "--fuel 0 --ifuel 0 --z3rlimit 100" noextract let sigver_vectors256_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_256 noextract let sigver_vectors384_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_384 noextract let sigver_vectors512_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_512 noextract let siggen_vectors256_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_256 noextract let siggen_vectors384_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_384 noextract let siggen_vectors512_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_512 %splice[sigver_vectors256_low] (lowstarize_toplevel "sigver_vectors256_tmp" "sigver_vectors256_low") %splice[sigver_vectors384_low] (lowstarize_toplevel "sigver_vectors384_tmp" "sigver_vectors384_low") %splice[sigver_vectors512_low] (lowstarize_toplevel "sigver_vectors512_tmp" "sigver_vectors512_low") %splice[siggen_vectors256_low] (lowstarize_toplevel "siggen_vectors256_tmp" "siggen_vectors256_low") %splice[siggen_vectors384_low] (lowstarize_toplevel "siggen_vectors384_tmp" "siggen_vectors384_low") %splice[siggen_vectors512_low] (lowstarize_toplevel "siggen_vectors512_tmp" "siggen_vectors512_low") // Cheap alternative to friend Lib.IntTypes needed because Test.Lowstarize uses UInt8.t assume val declassify_uint8: squash (uint8 == UInt8.t) let vec8 = L.lbuffer UInt8.t let sigver_vector = vec8 & vec8 & vec8 & vec8 & vec8 & bool let siggen_vector = vec8 & vec8 & vec8 & vec8 & vec8 & vec8 & vec8 // This could replace TestLib.compare_and_print val compare_and_print: b1:B.buffer UInt8.t -> b2:B.buffer UInt8.t -> len:UInt32.t -> Stack bool (requires fun h0 -> B.live h0 b1 /\ B.live h0 b2 /\ B.length b1 == v len /\ B.length b2 == v len) (ensures fun h0 _ h1 -> B.modifies B.loc_none h0 h1) let compare_and_print b1 b2 len = push_frame(); LowStar.Printf.(printf "Expected: %xuy\n" len b1 done); LowStar.Printf.(printf "Computed: %xuy\n" len b2 done); let b = Lib.ByteBuffer.lbytes_eq #len b1 b2 in if b then LowStar.Printf.(printf "PASS\n" done) else LowStar.Printf.(printf "FAIL\n" done); pop_frame(); b let test_sigver256 (vec:sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha2 msg_len msg qxy r s in if result' = result then () else begin LowStar.Printf.(printf "FAIL\n" done); C.exit 1l end; pop_frame() end let test_sigver384 (vec:sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha384 msg_len msg qxy r s in if result' = result then () else begin LowStar.Printf.(printf "FAIL\n" done); C.exit 1l end; pop_frame() end
{ "checked_file": "/", "dependencies": [ "Test.Lowstarize.fst.checked", "Spec.P256.fst.checked", "Spec.ECDSA.Test.Vectors.fst.checked", "prims.fst.checked", "LowStar.Printf.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "Lib.ByteBuffer.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.P256.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Int32.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "C.String.fsti.checked", "C.Loops.fst.checked", "C.fst.checked" ], "interface_file": false, "source_file": "Hacl.Test.ECDSA.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "Test.Lowstarize", "short_module": "L" }, { "abbrev": false, "full_module": "Spec.ECDSA.Test.Vectors", "short_module": null }, { "abbrev": false, "full_module": "Hacl.P256", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Test.Lowstarize", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 0, "initial_ifuel": 0, "max_fuel": 0, "max_ifuel": 0, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 100, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
vec: Hacl.Test.ECDSA.sigver_vector -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "Hacl.Test.ECDSA.sigver_vector", "FStar.UInt32.t", "LowStar.Buffer.buffer", "FStar.UInt8.t", "Prims.l_and", "Prims.eq2", "LowStar.Monotonic.Buffer.len", "LowStar.Buffer.trivial_preorder", "LowStar.Monotonic.Buffer.recallable", "Prims.bool", "Prims.op_Negation", "Prims.op_AmpAmp", "Prims.op_Equality", "FStar.UInt32.__uint_to_t", "C.exit", "FStar.Int32.__int_to_t", "Prims.unit", "FStar.HyperStack.ST.pop_frame", "LowStar.Printf.printf", "LowStar.Printf.done", "Hacl.P256.ecdsa_verif_p256_sha512", "LowStar.Monotonic.Buffer.blit", "LowStar.Monotonic.Buffer.mbuffer", "Prims.nat", "LowStar.Monotonic.Buffer.length", "FStar.UInt32.v", "FStar.UInt32.uint_to_t", "Prims.b2t", "LowStar.Monotonic.Buffer.g_is_null", "LowStar.Buffer.alloca", "Lib.IntTypes.u8", "FStar.HyperStack.ST.push_frame", "LowStar.Monotonic.Buffer.recall", "Prims.int", "FStar.Monotonic.HyperStack.mem", "Prims.l_True" ]
[]
false
true
false
false
false
let test_sigver512 (vec: sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) =
let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else (push_frame (); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha512 msg_len msg qxy r s in if result' = result then () else ((let open LowStar.Printf in printf "FAIL\n" done); C.exit 1l); pop_frame ())
false
Vale.Transformers.InstructionReorder.fst
Vale.Transformers.InstructionReorder.lemma_eval_while_equiv_states
val lemma_eval_while_equiv_states (cond: ocmp) (body: code) (fuel: nat) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures (equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2) )) (decreases %[fuel;body])
val lemma_eval_while_equiv_states (cond: ocmp) (body: code) (fuel: nat) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures (equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2) )) (decreases %[fuel;body])
let rec lemma_eval_code_equiv_states (c : code) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; c]) = match c with | Ins ins -> reveal_opaque (`%machine_eval_code_ins) machine_eval_code_ins; lemma_eval_ins_equiv_states ins (filt_state s1) (filt_state s2) | Block l -> lemma_eval_codes_equiv_states l fuel s1 s2 | IfElse ifCond ifTrue ifFalse -> reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1', b1) = machine_eval_ocmp s1 ifCond in let (s2', b2) = machine_eval_ocmp s2 ifCond in assert (b1 == b2); assert (equiv_states s1' s2'); if b1 then ( lemma_eval_code_equiv_states ifTrue fuel s1' s2' ) else ( lemma_eval_code_equiv_states ifFalse fuel s1' s2' ) | While cond body -> lemma_eval_while_equiv_states cond body fuel s1 s2 and lemma_eval_codes_equiv_states (cs : codes) (fuel:nat) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( let s1'', s2'' = machine_eval_codes cs fuel s1, machine_eval_codes cs fuel s2 in equiv_ostates s1'' s2'')) (decreases %[fuel; cs]) = match cs with | [] -> () | c :: cs -> lemma_eval_code_equiv_states c fuel s1 s2; let s1'', s2'' = machine_eval_code c fuel s1, machine_eval_code c fuel s2 in match s1'' with | None -> () | _ -> let Some s1, Some s2 = s1'', s2'' in lemma_eval_codes_equiv_states cs fuel s1 s2 and lemma_eval_while_equiv_states (cond:ocmp) (body:code) (fuel:nat) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2))) (decreases %[fuel; body]) = if fuel = 0 then () else ( reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let (s1, b1) = machine_eval_ocmp s1 cond in let (s2, b2) = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else ( assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then ( lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2 ) else () ) )
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 3, "end_line": 504, "start_col": 0, "start_line": 423 }
(** This module defines a transformer that performs safe instruction reordering. Example: The following set of instructions can be reordered in any order without any observable change in behavior: mov rax, 10 mov rbx, 3 Usage: Actual vale-tool or user-facing code should probably use the even nicer interface provided by the [Vale.Transformers.Transform] module. To use this module, you need to generate a [transformation_hints] object (a nice default is provided in this module via [find_transformation_hints], but users of this module can write their own, without needing to change any proofs), that can then be applied to a [codes] object (say [c1]) via [perform_reordering_with_hints] which tells you if this is a safe reordering, and if so, it produces the transformed [codes] object. If it is not considered to be safe, then the transformer gives a (human-readable) reason for why it doesn't consider it a safe reordering. If the transformation is safe and was indeed performed, then you can use [lemma_perform_reordering_with_hints] to reason about the reordered code having semantically equivalent behavior as the untransformed code. *) module Vale.Transformers.InstructionReorder /// Open all the relevant modules open Vale.X64.Bytes_Code_s open Vale.X64.Instruction_s open Vale.X64.Instructions_s open Vale.X64.Machine_Semantics_s open Vale.X64.Machine_s open Vale.X64.Print_s open Vale.Def.PossiblyMonad open Vale.Transformers.Locations open Vale.Transformers.BoundedInstructionEffects module L = FStar.List.Tot /// Some convenience functions let rec locations_of_locations_with_values (lv:locations_with_values) : locations = match lv with | [] -> [] | (|l,v|) :: lv -> l :: locations_of_locations_with_values lv /// Given two read/write sets corresponding to two neighboring /// instructions, we can say whether exchanging those two instructions /// should be allowed. let write_same_constants (c1 c2:locations_with_values) : pbool = for_all (fun (x1:location_with_value) -> for_all (fun (x2:location_with_value) -> let (| l1, v1 |) = x1 in let (| l2, v2 |) = x2 in (if l1 = l2 then v1 = v2 else true) /- "not writing same constants" ) c2 ) c1 let aux_write_exchange_allowed (w2:locations) (c1 c2:locations_with_values) (x:location) : pbool = let cv1, cv2 = locations_of_locations_with_values c1, locations_of_locations_with_values c2 in (disjoint_location_from_locations x w2) ||. ((x `L.mem` cv1 && x `L.mem` cv2) /- "non constant write") let write_exchange_allowed (w1 w2:locations) (c1 c2:locations_with_values) : pbool = write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. (* REVIEW: Just to make the symmetry proof easier, we write the other way around too. However, this makes things not as fast as they _could_ be. *) for_all (aux_write_exchange_allowed w1 c2 c1) w2 let rw_exchange_allowed (rw1 rw2 : rw_set) : pbool = let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in (disjoint_locations r1 w2 /+< "read set of 1st not disjoint from write set of 2nd because ") &&. (disjoint_locations r2 w1 /+< "read set of 2nd not disjoint from write set of 1st because ") &&. (write_exchange_allowed w1 w2 c1 c2 /+< "write sets not disjoint because ") let ins_exchange_allowed (i1 i2 : ins) : pbool = ( match i1, i2 with | Instr _ _ _, Instr _ _ _ -> (rw_exchange_allowed (rw_set_of_ins i1) (rw_set_of_ins i2)) | _, _ -> ffalse "non-generic instructions: conservatively disallowed exchange" ) /+> normal (" for instructions " ^ print_ins i1 gcc ^ " and " ^ print_ins i2 gcc) let rec lemma_write_same_constants_symmetric (c1 c2:locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) = match c1, c2 with | [], [] -> () | x :: xs, [] -> lemma_write_same_constants_symmetric xs [] | [], y :: ys -> lemma_write_same_constants_symmetric [] ys | x :: xs, y :: ys -> lemma_write_same_constants_symmetric c1 ys; lemma_write_same_constants_symmetric xs c2; lemma_write_same_constants_symmetric xs ys let lemma_write_exchange_allowed_symmetric (w1 w2:locations) (c1 c2:locations_with_values) : Lemma (ensures (!!(write_exchange_allowed w1 w2 c1 c2) = !!(write_exchange_allowed w2 w1 c2 c1))) = lemma_write_same_constants_symmetric c1 c2 let lemma_ins_exchange_allowed_symmetric (i1 i2 : ins) : Lemma (requires ( !!(ins_exchange_allowed i1 i2))) (ensures ( !!(ins_exchange_allowed i2 i1))) = let rw1, rw2 = rw_set_of_ins i1, rw_set_of_ins i2 in let r1, w1, c1 = rw1.loc_reads, rw1.loc_writes, rw1.loc_constant_writes in let r2, w2, c2 = rw2.loc_reads, rw2.loc_writes, rw2.loc_constant_writes in lemma_write_exchange_allowed_symmetric w1 w2 c1 c2 /// First, we must define what it means for two states to be /// equivalent. Here, we basically say they must be exactly the same. let equiv_states (s1 s2 : machine_state) : GTot Type0 = (s1.ms_ok == s2.ms_ok) /\ (s1.ms_regs == s2.ms_regs) /\ (cf s1.ms_flags = cf s2.ms_flags) /\ (overflow s1.ms_flags = overflow s2.ms_flags) /\ (s1.ms_heap == s2.ms_heap) /\ (s1.ms_stack == s2.ms_stack) /\ (s1.ms_stackTaint == s2.ms_stackTaint) (** Same as [equiv_states] but uses extensionality to "think harder"; useful at lower-level details of the proof. *) let equiv_states_ext (s1 s2 : machine_state) : GTot Type0 = let open FStar.FunctionalExtensionality in (feq s1.ms_regs s2.ms_regs) /\ (s1.ms_heap == s2.ms_heap) /\ (Map.equal s1.ms_stack.stack_mem s2.ms_stack.stack_mem) /\ (Map.equal s1.ms_stackTaint s2.ms_stackTaint) /\ (equiv_states s1 s2) (** A weaker version of [equiv_states] that makes all non-ok states equivalent. Since non-ok states indicate something "gone-wrong" in execution, we can safely say that the rest of the state is irrelevant. *) let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok)) (** Convenience wrapper around [equiv_states] *) unfold let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2))) (** An [option state] is said to be erroring if it is either [None] or if it is [Some] but is not ok. *) unfold let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok) (** [equiv_option_states s1 s2] means that [s1] and [s2] are equivalent [option machine_state]s iff both have same erroring behavior and if they are non-erroring, they are [equiv_states]. *) unfold let equiv_option_states (s1 s2:option machine_state) = (erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2)) /// If evaluation starts from a set of equivalent states, and the /// exact same thing is evaluated, then the final states are still /// equivalent. unfold let proof_run (s:machine_state) (f:st unit) : machine_state = let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok } let rec lemma_instr_apply_eval_args_equiv_states (outs:list instr_out) (args:list instr_operand) (f:instr_args_t outs args) (oprs:instr_operands_t_args args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_args outs args f oprs s1) == (instr_apply_eval_args outs args f oprs s2))) = match args with | [] -> () | i :: args -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_args_t outs args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_args_equiv_states outs args (f v) oprs s1 s2 #push-options "--z3rlimit 10" let rec lemma_instr_apply_eval_inouts_equiv_states (outs inouts:list instr_out) (args:list instr_operand) (f:instr_inouts_t outs inouts args) (oprs:instr_operands_t inouts args) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( (instr_apply_eval_inouts outs inouts args f oprs s1) == (instr_apply_eval_inouts outs inouts args f oprs s2))) = match inouts with | [] -> lemma_instr_apply_eval_args_equiv_states outs args f oprs s1 s2 | (Out, i) :: inouts -> let oprs = match i with | IOpEx i -> snd #(instr_operand_t i) (coerce oprs) | IOpIm i -> coerce oprs in lemma_instr_apply_eval_inouts_equiv_states outs inouts args (coerce f) oprs s1 s2 | (InOut, i)::inouts -> let (v, oprs) : option (instr_val_t i) & _ = match i with | IOpEx i -> let oprs = coerce oprs in (instr_eval_operand_explicit i (fst oprs) s1, snd oprs) | IOpIm i -> (instr_eval_operand_implicit i s1, coerce oprs) in let f:arrow (instr_val_t i) (instr_inouts_t outs inouts args) = coerce f in match v with | None -> () | Some v -> lemma_instr_apply_eval_inouts_equiv_states outs inouts args (f v) oprs s1 s2 #pop-options #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 0" let lemma_instr_write_output_implicit_equiv_states (i:instr_operand_implicit) (v:instr_val_t (IOpIm i)) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_implicit i v s_orig1 s1) (instr_write_output_implicit i v s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_implicit i v s_orig1 s1), (instr_write_output_implicit i v s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) let lemma_instr_write_output_explicit_equiv_states (i:instr_operand_explicit) (v:instr_val_t (IOpEx i)) (o:instr_operand_t i) (s_orig1 s1 s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_output_explicit i v o s_orig1 s1) (instr_write_output_explicit i v o s_orig2 s2)))) = let snew1, snew2 = (instr_write_output_explicit i v o s_orig1 s1), (instr_write_output_explicit i v o s_orig2 s2) in assert (equiv_states_ext snew1 snew2) (* OBSERVE *) #pop-options let rec lemma_instr_write_outputs_equiv_states (outs:list instr_out) (args:list instr_operand) (vs:instr_ret_t outs) (oprs:instr_operands_t outs args) (s_orig1 s1:machine_state) (s_orig2 s2:machine_state) : Lemma (requires ( (equiv_states s_orig1 s_orig2) /\ (equiv_states s1 s2))) (ensures ( (equiv_states (instr_write_outputs outs args vs oprs s_orig1 s1) (instr_write_outputs outs args vs oprs s_orig2 s2)))) = match outs with | [] -> () | (_, i)::outs -> ( let ((v:instr_val_t i), (vs:instr_ret_t outs)) = match outs with | [] -> (vs, ()) | _::_ -> let vs = coerce vs in (fst vs, snd vs) in match i with | IOpEx i -> let oprs = coerce oprs in lemma_instr_write_output_explicit_equiv_states i v (fst oprs) s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_explicit i v (fst oprs) s_orig1 s1 in let s2 = instr_write_output_explicit i v (fst oprs) s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (snd oprs) s_orig1 s1 s_orig2 s2 | IOpIm i -> lemma_instr_write_output_implicit_equiv_states i v s_orig1 s1 s_orig2 s2; let s1 = instr_write_output_implicit i v s_orig1 s1 in let s2 = instr_write_output_implicit i v s_orig2 s2 in lemma_instr_write_outputs_equiv_states outs args vs (coerce oprs) s_orig1 s1 s_orig2 s2 ) let lemma_eval_instr_equiv_states (it:instr_t_record) (oprs:instr_operands_t it.outs it.args) (ann:instr_annotation it) (s1 s2:machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_ostates (eval_instr it oprs ann s1) (eval_instr it oprs ann s2))) = let InstrTypeRecord #outs #args #havoc_flags' i = it in let vs1 = instr_apply_eval outs args (instr_eval i) oprs s1 in let vs2 = instr_apply_eval outs args (instr_eval i) oprs s2 in lemma_instr_apply_eval_inouts_equiv_states outs outs args (instr_eval i) oprs s1 s2; assert (vs1 == vs2); let s1_new = match havoc_flags' with | HavocFlags -> {s1 with ms_flags = havoc_flags} | PreserveFlags -> s1 in let s2_new = match havoc_flags' with | HavocFlags -> {s2 with ms_flags = havoc_flags} | PreserveFlags -> s2 in assert (overflow s1_new.ms_flags == overflow s2_new.ms_flags); assert (cf s1_new.ms_flags == cf s2_new.ms_flags); assert (equiv_states s1_new s2_new); let os1 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s1 s1_new) vs1 in let os2 = FStar.Option.mapTot (fun vs -> instr_write_outputs outs args vs oprs s2 s2_new) vs2 in match vs1 with | None -> () | Some vs -> lemma_instr_write_outputs_equiv_states outs args vs oprs s1 s1_new s2 s2_new #push-options "--z3rlimit 20 --max_fuel 0 --max_ifuel 1" (* REVIEW: This proof is INSANELY annoying to deal with due to the [Pop]. TODO: Figure out why it is slowing down so much. It practically brings F* to a standstill even when editing, and it acts worse during an interactive proof. *) let lemma_machine_eval_ins_st_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (run (machine_eval_ins_st i) s1) (run (machine_eval_ins_st i) s2))) = let s1_orig, s2_orig = s1, s2 in let s1_final = run (machine_eval_ins_st i) s1 in let s2_final = run (machine_eval_ins_st i) s2 in match i with | Instr it oprs ann -> lemma_eval_instr_equiv_states it oprs ann s1 s2 | Push _ _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Pop dst t -> let stack_op = OStack (MReg (Reg 0 rRsp) 0, t) in let s1 = proof_run s1 (check (valid_src_operand64_and_taint stack_op)) in let s2 = proof_run s2 (check (valid_src_operand64_and_taint stack_op)) in // assert (equiv_states s1 s2); let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in // assert (new_dst1 == new_dst2); let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in // assert (new_rsp1 == new_rsp2); let s1 = proof_run s1 (update_operand64_preserve_flags dst new_dst1) in let s2 = proof_run s2 (update_operand64_preserve_flags dst new_dst2) in assert (equiv_states_ext s1 s2); let s1 = proof_run s1 (free_stack (new_rsp1 - 8) new_rsp1) in let s2 = proof_run s2 (free_stack (new_rsp2 - 8) new_rsp2) in // assert (equiv_states s1 s2); let s1 = proof_run s1 (update_rsp new_rsp1) in let s2 = proof_run s2 (update_rsp new_rsp2) in assert (equiv_states_ext s1 s2); assert_spinoff (equiv_states s1_final s2_final) | Alloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) | Dealloc _ -> assert_spinoff (equiv_states_ext s1_final s2_final) #pop-options let lemma_eval_ins_equiv_states (i : ins) (s1 s2 : machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures ( equiv_states (machine_eval_ins i s1) (machine_eval_ins i s2))) = lemma_machine_eval_ins_st_equiv_states i s1 s2 (** Filter out observation related stuff from the state. *) let filt_state (s:machine_state) = { s with ms_trace = [] } #push-options "--z3rlimit 10 --max_fuel 1 --max_ifuel 1"
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instructions_s.fsti.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Transformers.Locations.fsti.checked", "Vale.Transformers.BoundedInstructionEffects.fsti.checked", "Vale.Def.PossiblyMonad.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Option.fst.checked", "FStar.Map.fsti.checked", "FStar.List.Tot.fst.checked", "FStar.List.Pure.fst.checked", "FStar.FunctionalExtensionality.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Vale.Transformers.InstructionReorder.fst" }
[ { "abbrev": true, "full_module": "FStar.List.Tot", "short_module": "L" }, { "abbrev": false, "full_module": "Vale.Transformers.BoundedInstructionEffects", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers.Locations", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.PossiblyMonad", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Print_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instructions_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "Vale.Transformers", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 10, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
cond: Vale.X64.Machine_Semantics_s.ocmp -> body: Vale.X64.Machine_Semantics_s.code -> fuel: Prims.nat -> s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> FStar.Pervasives.Lemma (requires Vale.Transformers.InstructionReorder.equiv_states s1 s2) (ensures Vale.Transformers.InstructionReorder.equiv_ostates (Vale.X64.Machine_Semantics_s.machine_eval_while cond body fuel s1) (Vale.X64.Machine_Semantics_s.machine_eval_while cond body fuel s2)) (decreases %[fuel;body])
FStar.Pervasives.Lemma
[ "lemma", "" ]
[ "lemma_eval_code_equiv_states", "lemma_eval_codes_equiv_states", "lemma_eval_while_equiv_states" ]
[ "Vale.X64.Machine_Semantics_s.ocmp", "Vale.X64.Machine_Semantics_s.code", "Prims.nat", "Vale.X64.Machine_Semantics_s.machine_state", "Prims.op_Equality", "Prims.int", "Prims.bool", "Prims.op_Negation", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Vale.Transformers.InstructionReorder.lemma_eval_while_equiv_states", "Prims.op_Subtraction", "Prims.unit", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.option", "FStar.Pervasives.Native.Mktuple2", "Prims._assert", "Vale.Transformers.InstructionReorder.equiv_ostates", "Vale.Transformers.InstructionReorder.lemma_eval_code_equiv_states", "Vale.X64.Machine_Semantics_s.machine_eval_code", "Vale.Transformers.InstructionReorder.equiv_states", "Prims.eq2", "Vale.X64.Machine_Semantics_s.machine_eval_ocmp", "FStar.Pervasives.reveal_opaque", "Vale.X64.Machine_Semantics_s.eval_ocmp_opaque", "Vale.X64.Machine_Semantics_s.valid_ocmp_opaque", "Prims.squash", "Vale.X64.Machine_Semantics_s.machine_eval_while", "Prims.Nil", "FStar.Pervasives.pattern" ]
[ "mutual recursion" ]
false
false
true
false
false
let rec lemma_eval_while_equiv_states (cond: ocmp) (body: code) (fuel: nat) (s1 s2: machine_state) : Lemma (requires (equiv_states s1 s2)) (ensures (equiv_ostates (machine_eval_while cond body fuel s1) (machine_eval_while cond body fuel s2) )) (decreases %[fuel;body]) =
if fuel = 0 then () else (reveal_opaque (`%valid_ocmp_opaque) valid_ocmp_opaque; reveal_opaque (`%eval_ocmp_opaque) eval_ocmp_opaque; let s1, b1 = machine_eval_ocmp s1 cond in let s2, b2 = machine_eval_ocmp s2 cond in assert (equiv_states s1 s2); assert (b1 == b2); if not b1 then () else (assert (equiv_states s1 s2); let s_opt1 = machine_eval_code body (fuel - 1) s1 in let s_opt2 = machine_eval_code body (fuel - 1) s2 in lemma_eval_code_equiv_states body (fuel - 1) s1 s2; assert (equiv_ostates s_opt1 s_opt2); match s_opt1 with | None -> () | Some _ -> let Some s1, Some s2 = s_opt1, s_opt2 in if s1.ms_ok then (lemma_eval_while_equiv_states cond body (fuel - 1) s1 s2)))
false
Hacl.Test.ECDSA.fst
Hacl.Test.ECDSA.check_bound
val check_bound: b:Lib.Buffer.lbuffer uint8 32ul -> Stack bool (requires fun h -> Lib.Buffer.live h b) (ensures fun h0 r h1 -> h0 == h1 /\ r == ( (Lib.ByteSequence.nat_from_bytes_be (Lib.Buffer.as_seq h0 b) > 0) && (Lib.ByteSequence.nat_from_bytes_be (Lib.Buffer.as_seq h0 b) < Spec.P256.order)))
val check_bound: b:Lib.Buffer.lbuffer uint8 32ul -> Stack bool (requires fun h -> Lib.Buffer.live h b) (ensures fun h0 r h1 -> h0 == h1 /\ r == ( (Lib.ByteSequence.nat_from_bytes_be (Lib.Buffer.as_seq h0 b) > 0) && (Lib.ByteSequence.nat_from_bytes_be (Lib.Buffer.as_seq h0 b) < Spec.P256.order)))
let check_bound b = let open FStar.Mul in let open Lib.ByteSequence in let open Spec.P256 in [@inline_let] let q1 = normalize_term (order % pow2 64) in [@inline_let] let q2 = normalize_term ((order / pow2 64) % pow2 64) in [@inline_let] let q3 = normalize_term ((order / pow2 128) % pow2 64) in [@inline_let] let q4 = normalize_term (((order / pow2 128) / pow2 64) % pow2 64) in assert_norm (pow2 128 * pow2 64 == pow2 192); assert (order == q1 + pow2 64 * q2 + pow2 128 * q3 + pow2 192 * q4); let zero = mk_int #U64 #PUB 0 in let q1 = mk_int #U64 #PUB q1 in let q2 = mk_int #U64 #PUB q2 in let q3 = mk_int #U64 #PUB q3 in let q4 = mk_int #U64 #PUB q4 in let h0 = get () in let x1 = Lib.ByteBuffer.uint_from_bytes_be #U64 (Lib.Buffer.sub b 0ul 8ul) in let x2 = Lib.ByteBuffer.uint_from_bytes_be #U64 (Lib.Buffer.sub b 8ul 8ul) in let x3 = Lib.ByteBuffer.uint_from_bytes_be #U64 (Lib.Buffer.sub b 16ul 8ul) in let x4 = Lib.ByteBuffer.uint_from_bytes_be #U64 (Lib.Buffer.sub b 24ul 8ul) in nat_from_intseq_be_slice_lemma (Lib.Buffer.as_seq h0 b) 8; lemma_reveal_uint_to_bytes_be #U64 (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 0 8); nat_from_intseq_be_slice_lemma (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 8 32) 8; lemma_reveal_uint_to_bytes_be #U64 (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 8 16); nat_from_intseq_be_slice_lemma (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 16 32) 8; lemma_reveal_uint_to_bytes_be #U64 (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 16 24); nat_from_intseq_be_slice_lemma (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 24 32) 8; lemma_reveal_uint_to_bytes_be #U64 (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 24 32); let x1 = Lib.RawIntTypes.u64_to_UInt64 x1 in let x2 = Lib.RawIntTypes.u64_to_UInt64 x2 in let x3 = Lib.RawIntTypes.u64_to_UInt64 x3 in let x4 = Lib.RawIntTypes.u64_to_UInt64 x4 in let r = x1 <. q4 || (x1 =. q4 && (x2 <. q3 || (x2 =. q3 && (x3 <. q2 || (x3 =. q2 && x4 <. q1))))) in let r1 = x1 = zero && x2 = zero && x3 = zero && x4 = zero in r && not r1
{ "file_name": "code/tests/Hacl.Test.ECDSA.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 13, "end_line": 262, "start_col": 0, "start_line": 212 }
module Hacl.Test.ECDSA open FStar.HyperStack.ST open Test.Lowstarize open Lib.IntTypes open Hacl.P256 open Spec.ECDSA.Test.Vectors module L = Test.Lowstarize module B = LowStar.Buffer #set-options "--fuel 0 --ifuel 0 --z3rlimit 100" noextract let sigver_vectors256_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_256 noextract let sigver_vectors384_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_384 noextract let sigver_vectors512_tmp = List.Tot.map (fun x -> h x.msg, h x.qx, h x.qy, h x.r, h x.s, x.result) sigver_vectors_sha2_512 noextract let siggen_vectors256_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_256 noextract let siggen_vectors384_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_384 noextract let siggen_vectors512_tmp = List.Tot.map (fun x -> h x.msg', h x.d, h x.qx', h x.qy', h x.k, h x.r', h x.s') siggen_vectors_sha2_512 %splice[sigver_vectors256_low] (lowstarize_toplevel "sigver_vectors256_tmp" "sigver_vectors256_low") %splice[sigver_vectors384_low] (lowstarize_toplevel "sigver_vectors384_tmp" "sigver_vectors384_low") %splice[sigver_vectors512_low] (lowstarize_toplevel "sigver_vectors512_tmp" "sigver_vectors512_low") %splice[siggen_vectors256_low] (lowstarize_toplevel "siggen_vectors256_tmp" "siggen_vectors256_low") %splice[siggen_vectors384_low] (lowstarize_toplevel "siggen_vectors384_tmp" "siggen_vectors384_low") %splice[siggen_vectors512_low] (lowstarize_toplevel "siggen_vectors512_tmp" "siggen_vectors512_low") // Cheap alternative to friend Lib.IntTypes needed because Test.Lowstarize uses UInt8.t assume val declassify_uint8: squash (uint8 == UInt8.t) let vec8 = L.lbuffer UInt8.t let sigver_vector = vec8 & vec8 & vec8 & vec8 & vec8 & bool let siggen_vector = vec8 & vec8 & vec8 & vec8 & vec8 & vec8 & vec8 // This could replace TestLib.compare_and_print val compare_and_print: b1:B.buffer UInt8.t -> b2:B.buffer UInt8.t -> len:UInt32.t -> Stack bool (requires fun h0 -> B.live h0 b1 /\ B.live h0 b2 /\ B.length b1 == v len /\ B.length b2 == v len) (ensures fun h0 _ h1 -> B.modifies B.loc_none h0 h1) let compare_and_print b1 b2 len = push_frame(); LowStar.Printf.(printf "Expected: %xuy\n" len b1 done); LowStar.Printf.(printf "Computed: %xuy\n" len b2 done); let b = Lib.ByteBuffer.lbytes_eq #len b1 b2 in if b then LowStar.Printf.(printf "PASS\n" done) else LowStar.Printf.(printf "FAIL\n" done); pop_frame(); b let test_sigver256 (vec:sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha2 msg_len msg qxy r s in if result' = result then () else begin LowStar.Printf.(printf "FAIL\n" done); C.exit 1l end; pop_frame() end let test_sigver384 (vec:sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha384 msg_len msg qxy r s in if result' = result then () else begin LowStar.Printf.(printf "FAIL\n" done); C.exit 1l end; pop_frame() end let test_sigver512 (vec:sigver_vector) : Stack unit (requires fun _ -> True) (ensures fun _ _ _ -> True) = let max_msg_len = 0 in let LB msg_len msg, LB qx_len qx, LB qy_len qy, LB r_len r, LB s_len s, result = vec in B.recall msg; B.recall qx; B.recall qy; B.recall r; B.recall s; // We need to check this at runtime because Low*-ized vectors don't carry any refinements if not (qx_len = 32ul && qy_len = 32ul && r_len = 32ul && s_len = 32ul) then C.exit (-1l) else begin push_frame(); let qxy = B.alloca (u8 0) 64ul in B.blit qx 0ul qxy 0ul 32ul; B.blit qy 0ul qxy 32ul 32ul; let result' = ecdsa_verif_p256_sha512 msg_len msg qxy r s in if result' = result then () else begin LowStar.Printf.(printf "FAIL\n" done); C.exit 1l end; pop_frame() end #push-options "--fuel 1 --ifuel 1 --z3rlimit 200" val check_bound: b:Lib.Buffer.lbuffer uint8 32ul -> Stack bool (requires fun h -> Lib.Buffer.live h b) (ensures fun h0 r h1 -> h0 == h1 /\ r == ( (Lib.ByteSequence.nat_from_bytes_be (Lib.Buffer.as_seq h0 b) > 0) && (Lib.ByteSequence.nat_from_bytes_be (Lib.Buffer.as_seq h0 b) < Spec.P256.order)))
{ "checked_file": "/", "dependencies": [ "Test.Lowstarize.fst.checked", "Spec.P256.fst.checked", "Spec.ECDSA.Test.Vectors.fst.checked", "prims.fst.checked", "LowStar.Printf.fst.checked", "LowStar.BufferOps.fst.checked", "LowStar.Buffer.fst.checked", "Lib.Sequence.fsti.checked", "Lib.RawIntTypes.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "Lib.ByteBuffer.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.P256.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.Int32.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "C.String.fsti.checked", "C.Loops.fst.checked", "C.fst.checked" ], "interface_file": false, "source_file": "Hacl.Test.ECDSA.fst" }
[ { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "Test.Lowstarize", "short_module": "L" }, { "abbrev": false, "full_module": "Spec.ECDSA.Test.Vectors", "short_module": null }, { "abbrev": false, "full_module": "Hacl.P256", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "Test.Lowstarize", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Test", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Prims", "short_module": null }, { "abbrev": false, "full_module": "FStar", "short_module": null } ]
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 1, "initial_ifuel": 1, "max_fuel": 1, "max_ifuel": 1, "no_plugins": false, "no_smt": false, "no_tactics": false, "quake_hi": 1, "quake_keep": false, "quake_lo": 1, "retry": false, "reuse_hint_for": null, "smtencoding_elim_box": false, "smtencoding_l_arith_repr": "boxwrap", "smtencoding_nl_arith_repr": "boxwrap", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 200, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
b: Lib.Buffer.lbuffer Lib.IntTypes.uint8 32ul -> FStar.HyperStack.ST.Stack Prims.bool
FStar.HyperStack.ST.Stack
[]
[]
[ "Lib.Buffer.lbuffer", "Lib.IntTypes.uint8", "FStar.UInt32.__uint_to_t", "Prims.op_AmpAmp", "Prims.op_Negation", "Prims.bool", "Prims.op_Equality", "FStar.UInt64.t", "Prims.l_or", "Prims.b2t", "Prims.int", "Lib.IntTypes.range", "Lib.IntTypes.U64", "FStar.UInt.size", "FStar.UInt64.n", "Lib.IntTypes.uint_v", "Lib.IntTypes.SEC", "FStar.UInt64.v", "Prims.eq2", "Lib.IntTypes.range_t", "Lib.IntTypes.v", "Lib.IntTypes.PUB", "Prims.op_BarBar", "Lib.IntTypes.op_Less_Dot", "Lib.IntTypes.op_Equals_Dot", "Lib.RawIntTypes.u64_to_UInt64", "Prims.unit", "Lib.ByteSequence.lemma_reveal_uint_to_bytes_be", "Lib.Sequence.slice", "Lib.IntTypes.U32", "Lib.Buffer.as_seq", "Lib.Buffer.MUT", "Lib.ByteSequence.nat_from_intseq_be_slice_lemma", "Lib.IntTypes.U8", "Lib.IntTypes.int_t", "Lib.ByteBuffer.uint_from_bytes_be", "Lib.IntTypes.uint_t", "Lib.Buffer.lbuffer_t", "Lib.IntTypes.mk_int", "Lib.Buffer.sub", "FStar.Monotonic.HyperStack.mem", "FStar.HyperStack.ST.get", "Prims._assert", "Spec.P256.PointOps.order", "Prims.op_Addition", "FStar.Mul.op_Star", "Prims.pow2", "FStar.Pervasives.assert_norm", "FStar.Pervasives.normalize_term", "Prims.op_Modulus", "Prims.op_Division" ]
[]
false
true
false
false
false
let check_bound b =
let open FStar.Mul in let open Lib.ByteSequence in let open Spec.P256 in [@@ inline_let ]let q1 = normalize_term (order % pow2 64) in [@@ inline_let ]let q2 = normalize_term ((order / pow2 64) % pow2 64) in [@@ inline_let ]let q3 = normalize_term ((order / pow2 128) % pow2 64) in [@@ inline_let ]let q4 = normalize_term (((order / pow2 128) / pow2 64) % pow2 64) in assert_norm (pow2 128 * pow2 64 == pow2 192); assert (order == q1 + pow2 64 * q2 + pow2 128 * q3 + pow2 192 * q4); let zero = mk_int #U64 #PUB 0 in let q1 = mk_int #U64 #PUB q1 in let q2 = mk_int #U64 #PUB q2 in let q3 = mk_int #U64 #PUB q3 in let q4 = mk_int #U64 #PUB q4 in let h0 = get () in let x1 = Lib.ByteBuffer.uint_from_bytes_be #U64 (Lib.Buffer.sub b 0ul 8ul) in let x2 = Lib.ByteBuffer.uint_from_bytes_be #U64 (Lib.Buffer.sub b 8ul 8ul) in let x3 = Lib.ByteBuffer.uint_from_bytes_be #U64 (Lib.Buffer.sub b 16ul 8ul) in let x4 = Lib.ByteBuffer.uint_from_bytes_be #U64 (Lib.Buffer.sub b 24ul 8ul) in nat_from_intseq_be_slice_lemma (Lib.Buffer.as_seq h0 b) 8; lemma_reveal_uint_to_bytes_be #U64 (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 0 8); nat_from_intseq_be_slice_lemma (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 8 32) 8; lemma_reveal_uint_to_bytes_be #U64 (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 8 16); nat_from_intseq_be_slice_lemma (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 16 32) 8; lemma_reveal_uint_to_bytes_be #U64 (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 16 24); nat_from_intseq_be_slice_lemma (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 24 32) 8; lemma_reveal_uint_to_bytes_be #U64 (Lib.Sequence.slice (Lib.Buffer.as_seq h0 b) 24 32); let x1 = Lib.RawIntTypes.u64_to_UInt64 x1 in let x2 = Lib.RawIntTypes.u64_to_UInt64 x2 in let x3 = Lib.RawIntTypes.u64_to_UInt64 x3 in let x4 = Lib.RawIntTypes.u64_to_UInt64 x4 in let r = x1 <. q4 || (x1 =. q4 && (x2 <. q3 || (x2 =. q3 && (x3 <. q2 || (x3 =. q2 && x4 <. q1))))) in let r1 = x1 = zero && x2 = zero && x3 = zero && x4 = zero in r && not r1
false