effect
stringclasses
48 values
original_source_type
stringlengths
0
23k
opens_and_abbrevs
listlengths
2
92
isa_cross_project_example
bool
1 class
source_definition
stringlengths
9
57.9k
partial_definition
stringlengths
7
23.3k
is_div
bool
2 classes
is_type
null
is_proof
bool
2 classes
completed_definiton
stringlengths
1
250k
dependencies
dict
effect_flags
sequencelengths
0
2
ideal_premises
sequencelengths
0
236
mutual_with
sequencelengths
0
11
file_context
stringlengths
0
407k
interleaved
bool
1 class
is_simply_typed
bool
2 classes
file_name
stringlengths
5
48
vconfig
dict
is_simple_lemma
null
source_type
stringlengths
10
23k
proof_features
sequencelengths
0
1
name
stringlengths
8
95
source
dict
verbose_type
stringlengths
1
7.42k
source_range
dict
FStar.HyperStack.ST.Stack
val fmul_rn: #s:field_spec -> out:felem s -> f1:felem s -> precomp:precomp_r s -> Stack unit (requires fun h -> live h out /\ live h f1 /\ live h precomp /\ (let rn = gsub precomp 10ul 5ul in let rn_5 = gsub precomp 15ul 5ul in felem_fits h f1 (3, 3, 3, 3, 3) /\ felem_fits h rn (2, 2, 2, 2, 2) /\ felem_fits h rn_5 (10, 10, 10, 10, 10) /\ F32xN.as_tup5 #(width s) h rn_5 == F32xN.precomp_r5 (F32xN.as_tup5 h rn))) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ felem_fits h1 out (1, 2, 1, 1, 2) /\ feval h1 out == Vec.fmul (feval h0 f1) (feval h0 (gsub precomp 10ul 5ul)))
[ { "abbrev": true, "full_module": "Hacl.Impl.Poly1305.Field32xN", "short_module": "F32xN" }, { "abbrev": true, "full_module": "Hacl.Spec.Poly1305.Vec", "short_module": "Vec" }, { "abbrev": true, "full_module": "Spec.Poly1305", "short_module": "S" }, { "abbrev": true, "full_module": "Lib.ByteSequence", "short_module": "BSeq" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305.Field32xN_256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305.Field32xN_128", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305.Field32xN_32", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305", "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 } ]
false
let fmul_rn #s out f1 precomp = match s with | M32 -> F32xN.fmul_rn #1 out f1 precomp | M128 -> F32xN.fmul_rn #2 out f1 precomp | M256 -> F32xN.fmul_rn #4 out f1 precomp
val fmul_rn: #s:field_spec -> out:felem s -> f1:felem s -> precomp:precomp_r s -> Stack unit (requires fun h -> live h out /\ live h f1 /\ live h precomp /\ (let rn = gsub precomp 10ul 5ul in let rn_5 = gsub precomp 15ul 5ul in felem_fits h f1 (3, 3, 3, 3, 3) /\ felem_fits h rn (2, 2, 2, 2, 2) /\ felem_fits h rn_5 (10, 10, 10, 10, 10) /\ F32xN.as_tup5 #(width s) h rn_5 == F32xN.precomp_r5 (F32xN.as_tup5 h rn))) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ felem_fits h1 out (1, 2, 1, 1, 2) /\ feval h1 out == Vec.fmul (feval h0 f1) (feval h0 (gsub precomp 10ul 5ul))) let fmul_rn #s out f1 precomp =
true
null
false
match s with | M32 -> F32xN.fmul_rn #1 out f1 precomp | M128 -> F32xN.fmul_rn #2 out f1 precomp | M256 -> F32xN.fmul_rn #4 out f1 precomp
{ "checked_file": "Hacl.Impl.Poly1305.Fields.fst.checked", "dependencies": [ "Spec.Poly1305.fst.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.Poly1305.Vec.fst.checked", "Hacl.Impl.Poly1305.Field32xN_32.fst.checked", "Hacl.Impl.Poly1305.Field32xN_256.fst.checked", "Hacl.Impl.Poly1305.Field32xN_128.fst.checked", "Hacl.Impl.Poly1305.Field32xN.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "Hacl.Impl.Poly1305.Fields.fst" }
[]
[ "Hacl.Impl.Poly1305.Fields.field_spec", "Hacl.Impl.Poly1305.Fields.felem", "Hacl.Impl.Poly1305.Fields.precomp_r", "Hacl.Impl.Poly1305.Field32xN.fmul_rn", "Prims.unit" ]
[]
module Hacl.Impl.Poly1305.Fields open FStar.HyperStack open FStar.HyperStack.All open FStar.Mul open Lib.IntTypes open Lib.Buffer open Hacl.Impl.Poly1305.Field32xN_32 open Hacl.Impl.Poly1305.Field32xN_128 open Hacl.Impl.Poly1305.Field32xN_256 open Hacl.Impl.Poly1305.Field32xN module ST = FStar.HyperStack.ST module LSeq = Lib.Sequence module BSeq = Lib.ByteSequence module S = Spec.Poly1305 module Vec = Hacl.Spec.Poly1305.Vec module F32xN = Hacl.Impl.Poly1305.Field32xN #reset-options "--z3rlimit 50 --max_fuel 0 --max_fuel 0" noextract type field_spec = | M32 | M128 | M256 unfold noextract let width (s:field_spec) : Vec.lanes = match s with | M32 -> 1 | M128 -> 2 | M256 -> 4 unfold noextract let limb (s:field_spec) = match s with | M32 -> F32xN.uint64xN 1 | M128 -> F32xN.uint64xN 2 | M256 -> F32xN.uint64xN 4 unfold noextract let limb_zero (s:field_spec) : limb s= match s with | M32 -> F32xN.zero 1 | M128 -> F32xN.zero 2 | M256 -> F32xN.zero 4 unfold noextract let wide (s:field_spec) = match s with | M32 -> F32xN.uint64xN 1 | M128 -> F32xN.uint64xN 2 | M256 -> F32xN.uint64xN 4 unfold noextract let nlimb (s:field_spec) : size_t = match s with | M32 -> 5ul | M128 -> 5ul | M256 -> 5ul unfold noextract let blocklen (s:field_spec) : r:size_t{0 < v r /\ v r == width s * S.size_block} = match s with | M32 -> 16ul | M128 -> 32ul | M256 -> 64ul unfold noextract let nelem (s:field_spec) : size_t = match s with | M32 -> 1ul | M128 -> 2ul | M256 -> 4ul unfold noextract let precomplen (s:field_spec) : size_t = match s with | M32 -> 20ul | M128 -> 20ul | M256 -> 20ul inline_for_extraction noextract type felem (s:field_spec) = lbuffer (limb s) (nlimb s) type felem_wide (s:field_spec) = lbuffer (wide s) (nlimb s) inline_for_extraction noextract type precomp_r (s:field_spec) = lbuffer (limb s) (precomplen s) noextract val felem_fits: #s:field_spec -> h:mem -> f:felem s -> m:F32xN.scale32_5 -> Type0 let felem_fits #s h f m = match s with | M32 -> F32xN.felem_fits #1 h f m | M128 -> F32xN.felem_fits #2 h f m | M256 -> F32xN.felem_fits #4 h f m noextract val fas_nat: #s:field_spec -> h:mem -> e:felem s -> GTot (LSeq.lseq nat (width s)) let fas_nat #s h e = match s with | M32 -> F32xN.fas_nat #1 h e | M128 -> F32xN.fas_nat #2 h e | M256 -> F32xN.fas_nat #4 h e noextract val feval: #s:field_spec -> h:mem -> e:felem s -> GTot (LSeq.lseq S.felem (width s)) let feval #s h e = match s with | M32 -> F32xN.feval #1 h e | M128 -> F32xN.feval #2 h e | M256 -> F32xN.feval #4 h e unfold noextract let op_String_Access #a #len = LSeq.index #a #len val lemma_feval_is_fas_nat: #s:field_spec -> h:mem -> f:felem s -> Lemma (requires F32xN.felem_less #(width s) h f (pow2 128)) (ensures (forall (i:nat). i < width s ==> (feval h f).[i] == (fas_nat h f).[i])) let lemma_feval_is_fas_nat #s h f = F32xN.lemma_feval_is_fas_nat #(width s) h f inline_for_extraction noextract val create_felem: s:field_spec -> StackInline (felem s) (requires fun h -> True) (ensures fun h0 f h1 -> stack_allocated f h0 h1 (LSeq.create 5 (limb_zero s)) /\ feval h1 f == LSeq.create (width s) 0) let create_felem s = match s with | M32 -> (F32xN.create_felem 1) <: felem s | M128 -> (F32xN.create_felem 2) <: felem s | M256 -> (F32xN.create_felem 4) <: felem s inline_for_extraction noextract val load_felem_le: #s:field_spec -> f:felem s -> b:lbuffer uint8 16ul -> Stack unit (requires fun h -> live h f /\ live h b) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (1, 1, 1, 1, 1) /\ F32xN.felem_less #(width s) h1 f (pow2 128) /\ feval h1 f == LSeq.create (width s) (BSeq.nat_from_bytes_le (as_seq h0 b))) let load_felem_le #s f b = match s with | M32 -> F32xN.load_felem_le #1 f b | M128 -> F32xN.load_felem_le #2 f b | M256 -> F32xN.load_felem_le #4 f b inline_for_extraction noextract val load_felems_le: #s:field_spec -> f:felem s -> b:lbuffer uint8 (blocklen s) -> Stack unit (requires fun h -> live h f /\ live h b) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (1, 1, 1, 1, 1) /\ F32xN.felem_less #(width s) h1 f (pow2 128) /\ feval h1 f == Vec.load_elem #(width s) (as_seq h0 b)) let load_felems_le #s f b = match s with | M32 -> F32xN.load_felems_le #1 f b | M128 -> F32xN.load_felems_le #2 f b | M256 -> F32xN.load_felems_le #4 f b inline_for_extraction noextract val load_acc: #s:field_spec -> acc:felem s -> b:lbuffer uint8 (blocklen s) -> Stack unit (requires fun h -> live h acc /\ live h b /\ disjoint acc b /\ felem_fits h acc (2, 2, 2, 2, 2)) (ensures fun h0 _ h1 -> modifies (loc acc) h0 h1 /\ felem_fits h1 acc (3, 3, 3, 3, 3) /\ feval h1 acc == Vec.load_acc #(width s) (as_seq h0 b) (feval h0 acc).[0]) let load_acc #s acc b = match s with | M32 -> Field32xN_32.load_acc1 acc b | M128 -> Field32xN_128.load_acc2 acc b | M256 -> Field32xN_256.load_acc4 acc b inline_for_extraction noextract val set_bit: #s:field_spec -> f:felem s -> i:size_t{size_v i <= 128} -> Stack unit (requires fun h -> live h f /\ felem_fits h f (1, 1, 1, 1, 1) /\ F32xN.felem_less #(width s) h f (pow2 (v i))) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (1, 1, 1, 1, 1) /\ (Math.Lemmas.pow2_le_compat 128 (v i); feval h1 f == LSeq.map (Vec.pfadd (pow2 (v i))) (feval h0 f))) let set_bit #s f i = match s with | M32 -> F32xN.set_bit #1 f i | M128 -> F32xN.set_bit #2 f i | M256 -> F32xN.set_bit #4 f i inline_for_extraction noextract val set_bit128: #s:field_spec -> f:felem s -> Stack unit (requires fun h -> live h f /\ felem_fits h f (1, 1, 1, 1, 1) /\ F32xN.felem_less #(width s) h f (pow2 128)) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (1, 1, 1, 1, 1) /\ feval h1 f == LSeq.map (Vec.pfadd (pow2 128)) (feval h0 f)) let set_bit128 #s f = match s with | M32 -> F32xN.set_bit128 #1 f | M128 -> F32xN.set_bit128 #2 f | M256 -> F32xN.set_bit128 #4 f inline_for_extraction noextract val set_zero: #s:field_spec -> f:felem s -> Stack unit (requires fun h -> live h f) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (0, 0, 0, 0, 0) /\ feval h1 f == LSeq.create (width s) 0) let set_zero #s f = match s with | M32 -> F32xN.set_zero #1 f | M128 -> F32xN.set_zero #2 f | M256 -> F32xN.set_zero #4 f inline_for_extraction noextract val reduce_felem: #s:field_spec -> f:felem s -> Stack unit (requires fun h -> live h f /\ felem_fits h f (2, 2, 2, 2, 2)) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (1, 1, 1, 1, 1) /\ (fas_nat h1 f).[0] == (feval h0 f).[0]) let reduce_felem #s f = match s with | M32 -> F32xN.reduce_felem #1 f | M128 -> F32xN.reduce_felem #2 f | M256 -> F32xN.reduce_felem #4 f inline_for_extraction noextract val load_precompute_r: #s:field_spec -> p:precomp_r s -> r0:uint64 -> r1:uint64 -> Stack unit (requires fun h -> live h p) (ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\ F32xN.load_precompute_r_post #(width s) h1 p /\ (assert (uint_v r1 * pow2 64 + uint_v r0 < pow2 128); feval h1 (gsub p 0ul 5ul) == LSeq.create (width s) (uint_v r1 * pow2 64 + uint_v r0))) let load_precompute_r #s p r0 r1 = match s with | M32 -> F32xN.load_precompute_r #1 p r0 r1 | M128 -> F32xN.load_precompute_r #2 p r0 r1 | M256 -> F32xN.load_precompute_r #4 p r0 r1 inline_for_extraction noextract val fadd_mul_r: #s:field_spec -> out:felem s -> f1:felem s -> precomp:precomp_r s -> Stack unit (requires fun h -> live h out /\ live h f1 /\ live h precomp /\ F32xN.fmul_precomp_r_pre #(width s) h precomp /\ felem_fits h out (2, 2, 2, 2, 2) /\ felem_fits h f1 (1, 1, 1, 1, 1)) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ felem_fits h1 out (1, 2, 1, 1, 2) /\ feval h1 out == Vec.fmul (Vec.fadd (feval h0 out) (feval h0 f1)) (feval h0 (gsub precomp 0ul 5ul))) let fadd_mul_r #s out f1 precomp = match s with | M32 -> F32xN.fadd_mul_r #1 out f1 precomp | M128 -> F32xN.fadd_mul_r #2 out f1 precomp | M256 -> F32xN.fadd_mul_r #4 out f1 precomp inline_for_extraction noextract val fmul_rn: #s:field_spec -> out:felem s -> f1:felem s -> precomp:precomp_r s -> Stack unit (requires fun h -> live h out /\ live h f1 /\ live h precomp /\ (let rn = gsub precomp 10ul 5ul in let rn_5 = gsub precomp 15ul 5ul in felem_fits h f1 (3, 3, 3, 3, 3) /\ felem_fits h rn (2, 2, 2, 2, 2) /\ felem_fits h rn_5 (10, 10, 10, 10, 10) /\ F32xN.as_tup5 #(width s) h rn_5 == F32xN.precomp_r5 (F32xN.as_tup5 h rn))) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ felem_fits h1 out (1, 2, 1, 1, 2) /\ feval h1 out == Vec.fmul (feval h0 f1) (feval h0 (gsub precomp 10ul 5ul)))
false
false
Hacl.Impl.Poly1305.Fields.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val fmul_rn: #s:field_spec -> out:felem s -> f1:felem s -> precomp:precomp_r s -> Stack unit (requires fun h -> live h out /\ live h f1 /\ live h precomp /\ (let rn = gsub precomp 10ul 5ul in let rn_5 = gsub precomp 15ul 5ul in felem_fits h f1 (3, 3, 3, 3, 3) /\ felem_fits h rn (2, 2, 2, 2, 2) /\ felem_fits h rn_5 (10, 10, 10, 10, 10) /\ F32xN.as_tup5 #(width s) h rn_5 == F32xN.precomp_r5 (F32xN.as_tup5 h rn))) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ felem_fits h1 out (1, 2, 1, 1, 2) /\ feval h1 out == Vec.fmul (feval h0 f1) (feval h0 (gsub precomp 10ul 5ul)))
[]
Hacl.Impl.Poly1305.Fields.fmul_rn
{ "file_name": "code/poly1305/Hacl.Impl.Poly1305.Fields.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
out: Hacl.Impl.Poly1305.Fields.felem s -> f1: Hacl.Impl.Poly1305.Fields.felem s -> precomp: Hacl.Impl.Poly1305.Fields.precomp_r s -> FStar.HyperStack.ST.Stack Prims.unit
{ "end_col": 43, "end_line": 356, "start_col": 2, "start_line": 353 }
FStar.HyperStack.ST.Stack
val fadd_mul_r: #s:field_spec -> out:felem s -> f1:felem s -> precomp:precomp_r s -> Stack unit (requires fun h -> live h out /\ live h f1 /\ live h precomp /\ F32xN.fmul_precomp_r_pre #(width s) h precomp /\ felem_fits h out (2, 2, 2, 2, 2) /\ felem_fits h f1 (1, 1, 1, 1, 1)) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ felem_fits h1 out (1, 2, 1, 1, 2) /\ feval h1 out == Vec.fmul (Vec.fadd (feval h0 out) (feval h0 f1)) (feval h0 (gsub precomp 0ul 5ul)))
[ { "abbrev": true, "full_module": "Hacl.Impl.Poly1305.Field32xN", "short_module": "F32xN" }, { "abbrev": true, "full_module": "Hacl.Spec.Poly1305.Vec", "short_module": "Vec" }, { "abbrev": true, "full_module": "Spec.Poly1305", "short_module": "S" }, { "abbrev": true, "full_module": "Lib.ByteSequence", "short_module": "BSeq" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305.Field32xN_256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305.Field32xN_128", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305.Field32xN_32", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305", "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 } ]
false
let fadd_mul_r #s out f1 precomp = match s with | M32 -> F32xN.fadd_mul_r #1 out f1 precomp | M128 -> F32xN.fadd_mul_r #2 out f1 precomp | M256 -> F32xN.fadd_mul_r #4 out f1 precomp
val fadd_mul_r: #s:field_spec -> out:felem s -> f1:felem s -> precomp:precomp_r s -> Stack unit (requires fun h -> live h out /\ live h f1 /\ live h precomp /\ F32xN.fmul_precomp_r_pre #(width s) h precomp /\ felem_fits h out (2, 2, 2, 2, 2) /\ felem_fits h f1 (1, 1, 1, 1, 1)) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ felem_fits h1 out (1, 2, 1, 1, 2) /\ feval h1 out == Vec.fmul (Vec.fadd (feval h0 out) (feval h0 f1)) (feval h0 (gsub precomp 0ul 5ul))) let fadd_mul_r #s out f1 precomp =
true
null
false
match s with | M32 -> F32xN.fadd_mul_r #1 out f1 precomp | M128 -> F32xN.fadd_mul_r #2 out f1 precomp | M256 -> F32xN.fadd_mul_r #4 out f1 precomp
{ "checked_file": "Hacl.Impl.Poly1305.Fields.fst.checked", "dependencies": [ "Spec.Poly1305.fst.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.Poly1305.Vec.fst.checked", "Hacl.Impl.Poly1305.Field32xN_32.fst.checked", "Hacl.Impl.Poly1305.Field32xN_256.fst.checked", "Hacl.Impl.Poly1305.Field32xN_128.fst.checked", "Hacl.Impl.Poly1305.Field32xN.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "Hacl.Impl.Poly1305.Fields.fst" }
[]
[ "Hacl.Impl.Poly1305.Fields.field_spec", "Hacl.Impl.Poly1305.Fields.felem", "Hacl.Impl.Poly1305.Fields.precomp_r", "Hacl.Impl.Poly1305.Field32xN.fadd_mul_r", "Prims.unit" ]
[]
module Hacl.Impl.Poly1305.Fields open FStar.HyperStack open FStar.HyperStack.All open FStar.Mul open Lib.IntTypes open Lib.Buffer open Hacl.Impl.Poly1305.Field32xN_32 open Hacl.Impl.Poly1305.Field32xN_128 open Hacl.Impl.Poly1305.Field32xN_256 open Hacl.Impl.Poly1305.Field32xN module ST = FStar.HyperStack.ST module LSeq = Lib.Sequence module BSeq = Lib.ByteSequence module S = Spec.Poly1305 module Vec = Hacl.Spec.Poly1305.Vec module F32xN = Hacl.Impl.Poly1305.Field32xN #reset-options "--z3rlimit 50 --max_fuel 0 --max_fuel 0" noextract type field_spec = | M32 | M128 | M256 unfold noextract let width (s:field_spec) : Vec.lanes = match s with | M32 -> 1 | M128 -> 2 | M256 -> 4 unfold noextract let limb (s:field_spec) = match s with | M32 -> F32xN.uint64xN 1 | M128 -> F32xN.uint64xN 2 | M256 -> F32xN.uint64xN 4 unfold noextract let limb_zero (s:field_spec) : limb s= match s with | M32 -> F32xN.zero 1 | M128 -> F32xN.zero 2 | M256 -> F32xN.zero 4 unfold noextract let wide (s:field_spec) = match s with | M32 -> F32xN.uint64xN 1 | M128 -> F32xN.uint64xN 2 | M256 -> F32xN.uint64xN 4 unfold noextract let nlimb (s:field_spec) : size_t = match s with | M32 -> 5ul | M128 -> 5ul | M256 -> 5ul unfold noextract let blocklen (s:field_spec) : r:size_t{0 < v r /\ v r == width s * S.size_block} = match s with | M32 -> 16ul | M128 -> 32ul | M256 -> 64ul unfold noextract let nelem (s:field_spec) : size_t = match s with | M32 -> 1ul | M128 -> 2ul | M256 -> 4ul unfold noextract let precomplen (s:field_spec) : size_t = match s with | M32 -> 20ul | M128 -> 20ul | M256 -> 20ul inline_for_extraction noextract type felem (s:field_spec) = lbuffer (limb s) (nlimb s) type felem_wide (s:field_spec) = lbuffer (wide s) (nlimb s) inline_for_extraction noextract type precomp_r (s:field_spec) = lbuffer (limb s) (precomplen s) noextract val felem_fits: #s:field_spec -> h:mem -> f:felem s -> m:F32xN.scale32_5 -> Type0 let felem_fits #s h f m = match s with | M32 -> F32xN.felem_fits #1 h f m | M128 -> F32xN.felem_fits #2 h f m | M256 -> F32xN.felem_fits #4 h f m noextract val fas_nat: #s:field_spec -> h:mem -> e:felem s -> GTot (LSeq.lseq nat (width s)) let fas_nat #s h e = match s with | M32 -> F32xN.fas_nat #1 h e | M128 -> F32xN.fas_nat #2 h e | M256 -> F32xN.fas_nat #4 h e noextract val feval: #s:field_spec -> h:mem -> e:felem s -> GTot (LSeq.lseq S.felem (width s)) let feval #s h e = match s with | M32 -> F32xN.feval #1 h e | M128 -> F32xN.feval #2 h e | M256 -> F32xN.feval #4 h e unfold noextract let op_String_Access #a #len = LSeq.index #a #len val lemma_feval_is_fas_nat: #s:field_spec -> h:mem -> f:felem s -> Lemma (requires F32xN.felem_less #(width s) h f (pow2 128)) (ensures (forall (i:nat). i < width s ==> (feval h f).[i] == (fas_nat h f).[i])) let lemma_feval_is_fas_nat #s h f = F32xN.lemma_feval_is_fas_nat #(width s) h f inline_for_extraction noextract val create_felem: s:field_spec -> StackInline (felem s) (requires fun h -> True) (ensures fun h0 f h1 -> stack_allocated f h0 h1 (LSeq.create 5 (limb_zero s)) /\ feval h1 f == LSeq.create (width s) 0) let create_felem s = match s with | M32 -> (F32xN.create_felem 1) <: felem s | M128 -> (F32xN.create_felem 2) <: felem s | M256 -> (F32xN.create_felem 4) <: felem s inline_for_extraction noextract val load_felem_le: #s:field_spec -> f:felem s -> b:lbuffer uint8 16ul -> Stack unit (requires fun h -> live h f /\ live h b) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (1, 1, 1, 1, 1) /\ F32xN.felem_less #(width s) h1 f (pow2 128) /\ feval h1 f == LSeq.create (width s) (BSeq.nat_from_bytes_le (as_seq h0 b))) let load_felem_le #s f b = match s with | M32 -> F32xN.load_felem_le #1 f b | M128 -> F32xN.load_felem_le #2 f b | M256 -> F32xN.load_felem_le #4 f b inline_for_extraction noextract val load_felems_le: #s:field_spec -> f:felem s -> b:lbuffer uint8 (blocklen s) -> Stack unit (requires fun h -> live h f /\ live h b) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (1, 1, 1, 1, 1) /\ F32xN.felem_less #(width s) h1 f (pow2 128) /\ feval h1 f == Vec.load_elem #(width s) (as_seq h0 b)) let load_felems_le #s f b = match s with | M32 -> F32xN.load_felems_le #1 f b | M128 -> F32xN.load_felems_le #2 f b | M256 -> F32xN.load_felems_le #4 f b inline_for_extraction noextract val load_acc: #s:field_spec -> acc:felem s -> b:lbuffer uint8 (blocklen s) -> Stack unit (requires fun h -> live h acc /\ live h b /\ disjoint acc b /\ felem_fits h acc (2, 2, 2, 2, 2)) (ensures fun h0 _ h1 -> modifies (loc acc) h0 h1 /\ felem_fits h1 acc (3, 3, 3, 3, 3) /\ feval h1 acc == Vec.load_acc #(width s) (as_seq h0 b) (feval h0 acc).[0]) let load_acc #s acc b = match s with | M32 -> Field32xN_32.load_acc1 acc b | M128 -> Field32xN_128.load_acc2 acc b | M256 -> Field32xN_256.load_acc4 acc b inline_for_extraction noextract val set_bit: #s:field_spec -> f:felem s -> i:size_t{size_v i <= 128} -> Stack unit (requires fun h -> live h f /\ felem_fits h f (1, 1, 1, 1, 1) /\ F32xN.felem_less #(width s) h f (pow2 (v i))) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (1, 1, 1, 1, 1) /\ (Math.Lemmas.pow2_le_compat 128 (v i); feval h1 f == LSeq.map (Vec.pfadd (pow2 (v i))) (feval h0 f))) let set_bit #s f i = match s with | M32 -> F32xN.set_bit #1 f i | M128 -> F32xN.set_bit #2 f i | M256 -> F32xN.set_bit #4 f i inline_for_extraction noextract val set_bit128: #s:field_spec -> f:felem s -> Stack unit (requires fun h -> live h f /\ felem_fits h f (1, 1, 1, 1, 1) /\ F32xN.felem_less #(width s) h f (pow2 128)) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (1, 1, 1, 1, 1) /\ feval h1 f == LSeq.map (Vec.pfadd (pow2 128)) (feval h0 f)) let set_bit128 #s f = match s with | M32 -> F32xN.set_bit128 #1 f | M128 -> F32xN.set_bit128 #2 f | M256 -> F32xN.set_bit128 #4 f inline_for_extraction noextract val set_zero: #s:field_spec -> f:felem s -> Stack unit (requires fun h -> live h f) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (0, 0, 0, 0, 0) /\ feval h1 f == LSeq.create (width s) 0) let set_zero #s f = match s with | M32 -> F32xN.set_zero #1 f | M128 -> F32xN.set_zero #2 f | M256 -> F32xN.set_zero #4 f inline_for_extraction noextract val reduce_felem: #s:field_spec -> f:felem s -> Stack unit (requires fun h -> live h f /\ felem_fits h f (2, 2, 2, 2, 2)) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (1, 1, 1, 1, 1) /\ (fas_nat h1 f).[0] == (feval h0 f).[0]) let reduce_felem #s f = match s with | M32 -> F32xN.reduce_felem #1 f | M128 -> F32xN.reduce_felem #2 f | M256 -> F32xN.reduce_felem #4 f inline_for_extraction noextract val load_precompute_r: #s:field_spec -> p:precomp_r s -> r0:uint64 -> r1:uint64 -> Stack unit (requires fun h -> live h p) (ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\ F32xN.load_precompute_r_post #(width s) h1 p /\ (assert (uint_v r1 * pow2 64 + uint_v r0 < pow2 128); feval h1 (gsub p 0ul 5ul) == LSeq.create (width s) (uint_v r1 * pow2 64 + uint_v r0))) let load_precompute_r #s p r0 r1 = match s with | M32 -> F32xN.load_precompute_r #1 p r0 r1 | M128 -> F32xN.load_precompute_r #2 p r0 r1 | M256 -> F32xN.load_precompute_r #4 p r0 r1 inline_for_extraction noextract val fadd_mul_r: #s:field_spec -> out:felem s -> f1:felem s -> precomp:precomp_r s -> Stack unit (requires fun h -> live h out /\ live h f1 /\ live h precomp /\ F32xN.fmul_precomp_r_pre #(width s) h precomp /\ felem_fits h out (2, 2, 2, 2, 2) /\ felem_fits h f1 (1, 1, 1, 1, 1)) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ felem_fits h1 out (1, 2, 1, 1, 2) /\ feval h1 out == Vec.fmul (Vec.fadd (feval h0 out) (feval h0 f1)) (feval h0 (gsub precomp 0ul 5ul)))
false
false
Hacl.Impl.Poly1305.Fields.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val fadd_mul_r: #s:field_spec -> out:felem s -> f1:felem s -> precomp:precomp_r s -> Stack unit (requires fun h -> live h out /\ live h f1 /\ live h precomp /\ F32xN.fmul_precomp_r_pre #(width s) h precomp /\ felem_fits h out (2, 2, 2, 2, 2) /\ felem_fits h f1 (1, 1, 1, 1, 1)) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ felem_fits h1 out (1, 2, 1, 1, 2) /\ feval h1 out == Vec.fmul (Vec.fadd (feval h0 out) (feval h0 f1)) (feval h0 (gsub precomp 0ul 5ul)))
[]
Hacl.Impl.Poly1305.Fields.fadd_mul_r
{ "file_name": "code/poly1305/Hacl.Impl.Poly1305.Fields.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
out: Hacl.Impl.Poly1305.Fields.felem s -> f1: Hacl.Impl.Poly1305.Fields.felem s -> precomp: Hacl.Impl.Poly1305.Fields.precomp_r s -> FStar.HyperStack.ST.Stack Prims.unit
{ "end_col": 46, "end_line": 329, "start_col": 2, "start_line": 326 }
FStar.HyperStack.ST.Stack
val fmul_rn_normalize: #s:field_spec -> out:felem s -> precomp:precomp_r s -> Stack unit (requires fun h -> live h out /\ live h precomp /\ felem_fits h out (3, 3, 3, 3, 3) /\ F32xN.load_precompute_r_post #(width s) h precomp) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ felem_fits h1 out (2, 2, 2, 2, 2) /\ (feval h1 out).[0] == Vec.normalize_n #(width s) (feval h0 (gsub precomp 0ul 5ul)).[0] (feval h0 out))
[ { "abbrev": true, "full_module": "Hacl.Impl.Poly1305.Field32xN", "short_module": "F32xN" }, { "abbrev": true, "full_module": "Hacl.Spec.Poly1305.Vec", "short_module": "Vec" }, { "abbrev": true, "full_module": "Spec.Poly1305", "short_module": "S" }, { "abbrev": true, "full_module": "Lib.ByteSequence", "short_module": "BSeq" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305.Field32xN", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305.Field32xN_256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305.Field32xN_128", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305.Field32xN_32", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.All", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Poly1305", "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 } ]
false
let fmul_rn_normalize #s out precomp = match s with | M32 -> Field32xN_32.fmul_r1_normalize out precomp | M128 -> Field32xN_128.fmul_r2_normalize out precomp | M256 -> Field32xN_256.fmul_r4_normalize out precomp
val fmul_rn_normalize: #s:field_spec -> out:felem s -> precomp:precomp_r s -> Stack unit (requires fun h -> live h out /\ live h precomp /\ felem_fits h out (3, 3, 3, 3, 3) /\ F32xN.load_precompute_r_post #(width s) h precomp) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ felem_fits h1 out (2, 2, 2, 2, 2) /\ (feval h1 out).[0] == Vec.normalize_n #(width s) (feval h0 (gsub precomp 0ul 5ul)).[0] (feval h0 out)) let fmul_rn_normalize #s out precomp =
true
null
false
match s with | M32 -> Field32xN_32.fmul_r1_normalize out precomp | M128 -> Field32xN_128.fmul_r2_normalize out precomp | M256 -> Field32xN_256.fmul_r4_normalize out precomp
{ "checked_file": "Hacl.Impl.Poly1305.Fields.fst.checked", "dependencies": [ "Spec.Poly1305.fst.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.Poly1305.Vec.fst.checked", "Hacl.Impl.Poly1305.Field32xN_32.fst.checked", "Hacl.Impl.Poly1305.Field32xN_256.fst.checked", "Hacl.Impl.Poly1305.Field32xN_128.fst.checked", "Hacl.Impl.Poly1305.Field32xN.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.Math.Lemmas.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.All.fst.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "Hacl.Impl.Poly1305.Fields.fst" }
[]
[ "Hacl.Impl.Poly1305.Fields.field_spec", "Hacl.Impl.Poly1305.Fields.felem", "Hacl.Impl.Poly1305.Fields.precomp_r", "Hacl.Impl.Poly1305.Field32xN_32.fmul_r1_normalize", "Prims.unit", "Hacl.Impl.Poly1305.Field32xN_128.fmul_r2_normalize", "Hacl.Impl.Poly1305.Field32xN_256.fmul_r4_normalize" ]
[]
module Hacl.Impl.Poly1305.Fields open FStar.HyperStack open FStar.HyperStack.All open FStar.Mul open Lib.IntTypes open Lib.Buffer open Hacl.Impl.Poly1305.Field32xN_32 open Hacl.Impl.Poly1305.Field32xN_128 open Hacl.Impl.Poly1305.Field32xN_256 open Hacl.Impl.Poly1305.Field32xN module ST = FStar.HyperStack.ST module LSeq = Lib.Sequence module BSeq = Lib.ByteSequence module S = Spec.Poly1305 module Vec = Hacl.Spec.Poly1305.Vec module F32xN = Hacl.Impl.Poly1305.Field32xN #reset-options "--z3rlimit 50 --max_fuel 0 --max_fuel 0" noextract type field_spec = | M32 | M128 | M256 unfold noextract let width (s:field_spec) : Vec.lanes = match s with | M32 -> 1 | M128 -> 2 | M256 -> 4 unfold noextract let limb (s:field_spec) = match s with | M32 -> F32xN.uint64xN 1 | M128 -> F32xN.uint64xN 2 | M256 -> F32xN.uint64xN 4 unfold noextract let limb_zero (s:field_spec) : limb s= match s with | M32 -> F32xN.zero 1 | M128 -> F32xN.zero 2 | M256 -> F32xN.zero 4 unfold noextract let wide (s:field_spec) = match s with | M32 -> F32xN.uint64xN 1 | M128 -> F32xN.uint64xN 2 | M256 -> F32xN.uint64xN 4 unfold noextract let nlimb (s:field_spec) : size_t = match s with | M32 -> 5ul | M128 -> 5ul | M256 -> 5ul unfold noextract let blocklen (s:field_spec) : r:size_t{0 < v r /\ v r == width s * S.size_block} = match s with | M32 -> 16ul | M128 -> 32ul | M256 -> 64ul unfold noextract let nelem (s:field_spec) : size_t = match s with | M32 -> 1ul | M128 -> 2ul | M256 -> 4ul unfold noextract let precomplen (s:field_spec) : size_t = match s with | M32 -> 20ul | M128 -> 20ul | M256 -> 20ul inline_for_extraction noextract type felem (s:field_spec) = lbuffer (limb s) (nlimb s) type felem_wide (s:field_spec) = lbuffer (wide s) (nlimb s) inline_for_extraction noextract type precomp_r (s:field_spec) = lbuffer (limb s) (precomplen s) noextract val felem_fits: #s:field_spec -> h:mem -> f:felem s -> m:F32xN.scale32_5 -> Type0 let felem_fits #s h f m = match s with | M32 -> F32xN.felem_fits #1 h f m | M128 -> F32xN.felem_fits #2 h f m | M256 -> F32xN.felem_fits #4 h f m noextract val fas_nat: #s:field_spec -> h:mem -> e:felem s -> GTot (LSeq.lseq nat (width s)) let fas_nat #s h e = match s with | M32 -> F32xN.fas_nat #1 h e | M128 -> F32xN.fas_nat #2 h e | M256 -> F32xN.fas_nat #4 h e noextract val feval: #s:field_spec -> h:mem -> e:felem s -> GTot (LSeq.lseq S.felem (width s)) let feval #s h e = match s with | M32 -> F32xN.feval #1 h e | M128 -> F32xN.feval #2 h e | M256 -> F32xN.feval #4 h e unfold noextract let op_String_Access #a #len = LSeq.index #a #len val lemma_feval_is_fas_nat: #s:field_spec -> h:mem -> f:felem s -> Lemma (requires F32xN.felem_less #(width s) h f (pow2 128)) (ensures (forall (i:nat). i < width s ==> (feval h f).[i] == (fas_nat h f).[i])) let lemma_feval_is_fas_nat #s h f = F32xN.lemma_feval_is_fas_nat #(width s) h f inline_for_extraction noextract val create_felem: s:field_spec -> StackInline (felem s) (requires fun h -> True) (ensures fun h0 f h1 -> stack_allocated f h0 h1 (LSeq.create 5 (limb_zero s)) /\ feval h1 f == LSeq.create (width s) 0) let create_felem s = match s with | M32 -> (F32xN.create_felem 1) <: felem s | M128 -> (F32xN.create_felem 2) <: felem s | M256 -> (F32xN.create_felem 4) <: felem s inline_for_extraction noextract val load_felem_le: #s:field_spec -> f:felem s -> b:lbuffer uint8 16ul -> Stack unit (requires fun h -> live h f /\ live h b) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (1, 1, 1, 1, 1) /\ F32xN.felem_less #(width s) h1 f (pow2 128) /\ feval h1 f == LSeq.create (width s) (BSeq.nat_from_bytes_le (as_seq h0 b))) let load_felem_le #s f b = match s with | M32 -> F32xN.load_felem_le #1 f b | M128 -> F32xN.load_felem_le #2 f b | M256 -> F32xN.load_felem_le #4 f b inline_for_extraction noextract val load_felems_le: #s:field_spec -> f:felem s -> b:lbuffer uint8 (blocklen s) -> Stack unit (requires fun h -> live h f /\ live h b) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (1, 1, 1, 1, 1) /\ F32xN.felem_less #(width s) h1 f (pow2 128) /\ feval h1 f == Vec.load_elem #(width s) (as_seq h0 b)) let load_felems_le #s f b = match s with | M32 -> F32xN.load_felems_le #1 f b | M128 -> F32xN.load_felems_le #2 f b | M256 -> F32xN.load_felems_le #4 f b inline_for_extraction noextract val load_acc: #s:field_spec -> acc:felem s -> b:lbuffer uint8 (blocklen s) -> Stack unit (requires fun h -> live h acc /\ live h b /\ disjoint acc b /\ felem_fits h acc (2, 2, 2, 2, 2)) (ensures fun h0 _ h1 -> modifies (loc acc) h0 h1 /\ felem_fits h1 acc (3, 3, 3, 3, 3) /\ feval h1 acc == Vec.load_acc #(width s) (as_seq h0 b) (feval h0 acc).[0]) let load_acc #s acc b = match s with | M32 -> Field32xN_32.load_acc1 acc b | M128 -> Field32xN_128.load_acc2 acc b | M256 -> Field32xN_256.load_acc4 acc b inline_for_extraction noextract val set_bit: #s:field_spec -> f:felem s -> i:size_t{size_v i <= 128} -> Stack unit (requires fun h -> live h f /\ felem_fits h f (1, 1, 1, 1, 1) /\ F32xN.felem_less #(width s) h f (pow2 (v i))) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (1, 1, 1, 1, 1) /\ (Math.Lemmas.pow2_le_compat 128 (v i); feval h1 f == LSeq.map (Vec.pfadd (pow2 (v i))) (feval h0 f))) let set_bit #s f i = match s with | M32 -> F32xN.set_bit #1 f i | M128 -> F32xN.set_bit #2 f i | M256 -> F32xN.set_bit #4 f i inline_for_extraction noextract val set_bit128: #s:field_spec -> f:felem s -> Stack unit (requires fun h -> live h f /\ felem_fits h f (1, 1, 1, 1, 1) /\ F32xN.felem_less #(width s) h f (pow2 128)) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (1, 1, 1, 1, 1) /\ feval h1 f == LSeq.map (Vec.pfadd (pow2 128)) (feval h0 f)) let set_bit128 #s f = match s with | M32 -> F32xN.set_bit128 #1 f | M128 -> F32xN.set_bit128 #2 f | M256 -> F32xN.set_bit128 #4 f inline_for_extraction noextract val set_zero: #s:field_spec -> f:felem s -> Stack unit (requires fun h -> live h f) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (0, 0, 0, 0, 0) /\ feval h1 f == LSeq.create (width s) 0) let set_zero #s f = match s with | M32 -> F32xN.set_zero #1 f | M128 -> F32xN.set_zero #2 f | M256 -> F32xN.set_zero #4 f inline_for_extraction noextract val reduce_felem: #s:field_spec -> f:felem s -> Stack unit (requires fun h -> live h f /\ felem_fits h f (2, 2, 2, 2, 2)) (ensures fun h0 _ h1 -> modifies (loc f) h0 h1 /\ felem_fits h1 f (1, 1, 1, 1, 1) /\ (fas_nat h1 f).[0] == (feval h0 f).[0]) let reduce_felem #s f = match s with | M32 -> F32xN.reduce_felem #1 f | M128 -> F32xN.reduce_felem #2 f | M256 -> F32xN.reduce_felem #4 f inline_for_extraction noextract val load_precompute_r: #s:field_spec -> p:precomp_r s -> r0:uint64 -> r1:uint64 -> Stack unit (requires fun h -> live h p) (ensures fun h0 _ h1 -> modifies (loc p) h0 h1 /\ F32xN.load_precompute_r_post #(width s) h1 p /\ (assert (uint_v r1 * pow2 64 + uint_v r0 < pow2 128); feval h1 (gsub p 0ul 5ul) == LSeq.create (width s) (uint_v r1 * pow2 64 + uint_v r0))) let load_precompute_r #s p r0 r1 = match s with | M32 -> F32xN.load_precompute_r #1 p r0 r1 | M128 -> F32xN.load_precompute_r #2 p r0 r1 | M256 -> F32xN.load_precompute_r #4 p r0 r1 inline_for_extraction noextract val fadd_mul_r: #s:field_spec -> out:felem s -> f1:felem s -> precomp:precomp_r s -> Stack unit (requires fun h -> live h out /\ live h f1 /\ live h precomp /\ F32xN.fmul_precomp_r_pre #(width s) h precomp /\ felem_fits h out (2, 2, 2, 2, 2) /\ felem_fits h f1 (1, 1, 1, 1, 1)) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ felem_fits h1 out (1, 2, 1, 1, 2) /\ feval h1 out == Vec.fmul (Vec.fadd (feval h0 out) (feval h0 f1)) (feval h0 (gsub precomp 0ul 5ul))) let fadd_mul_r #s out f1 precomp = match s with | M32 -> F32xN.fadd_mul_r #1 out f1 precomp | M128 -> F32xN.fadd_mul_r #2 out f1 precomp | M256 -> F32xN.fadd_mul_r #4 out f1 precomp inline_for_extraction noextract val fmul_rn: #s:field_spec -> out:felem s -> f1:felem s -> precomp:precomp_r s -> Stack unit (requires fun h -> live h out /\ live h f1 /\ live h precomp /\ (let rn = gsub precomp 10ul 5ul in let rn_5 = gsub precomp 15ul 5ul in felem_fits h f1 (3, 3, 3, 3, 3) /\ felem_fits h rn (2, 2, 2, 2, 2) /\ felem_fits h rn_5 (10, 10, 10, 10, 10) /\ F32xN.as_tup5 #(width s) h rn_5 == F32xN.precomp_r5 (F32xN.as_tup5 h rn))) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ felem_fits h1 out (1, 2, 1, 1, 2) /\ feval h1 out == Vec.fmul (feval h0 f1) (feval h0 (gsub precomp 10ul 5ul))) let fmul_rn #s out f1 precomp = match s with | M32 -> F32xN.fmul_rn #1 out f1 precomp | M128 -> F32xN.fmul_rn #2 out f1 precomp | M256 -> F32xN.fmul_rn #4 out f1 precomp inline_for_extraction noextract val fmul_rn_normalize: #s:field_spec -> out:felem s -> precomp:precomp_r s -> Stack unit (requires fun h -> live h out /\ live h precomp /\ felem_fits h out (3, 3, 3, 3, 3) /\ F32xN.load_precompute_r_post #(width s) h precomp) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ felem_fits h1 out (2, 2, 2, 2, 2) /\ (feval h1 out).[0] == Vec.normalize_n #(width s) (feval h0 (gsub precomp 0ul 5ul)).[0] (feval h0 out))
false
false
Hacl.Impl.Poly1305.Fields.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 1, "max_fuel": 0, "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": false, "z3cliopt": [], "z3refresh": false, "z3rlimit": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val fmul_rn_normalize: #s:field_spec -> out:felem s -> precomp:precomp_r s -> Stack unit (requires fun h -> live h out /\ live h precomp /\ felem_fits h out (3, 3, 3, 3, 3) /\ F32xN.load_precompute_r_post #(width s) h precomp) (ensures fun h0 _ h1 -> modifies (loc out) h0 h1 /\ felem_fits h1 out (2, 2, 2, 2, 2) /\ (feval h1 out).[0] == Vec.normalize_n #(width s) (feval h0 (gsub precomp 0ul 5ul)).[0] (feval h0 out))
[]
Hacl.Impl.Poly1305.Fields.fmul_rn_normalize
{ "file_name": "code/poly1305/Hacl.Impl.Poly1305.Fields.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
out: Hacl.Impl.Poly1305.Fields.felem s -> precomp: Hacl.Impl.Poly1305.Fields.precomp_r s -> FStar.HyperStack.ST.Stack Prims.unit
{ "end_col": 55, "end_line": 379, "start_col": 2, "start_line": 376 }
Prims.Tot
[ { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "EverParse3d.Prelude", "short_module": null }, { "abbrev": true, "full_module": "FStar.Int.Cast", "short_module": "Cast" }, { "abbrev": false, "full_module": "EverParse3d.Actions", "short_module": null }, { "abbrev": false, "full_module": "EverParse3d.Actions", "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 } ]
false
let is_range_okay = EverParse3d.ErrorCode.is_range_okay
let is_range_okay =
false
null
false
EverParse3d.ErrorCode.is_range_okay
{ "checked_file": "EverParse3d.Actions.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Int.Cast.fst.checked", "EverParse3d.Prelude.fsti.checked", "EverParse3d.ErrorCode.fst.checked" ], "interface_file": false, "source_file": "EverParse3d.Actions.Base.fsti" }
[ "total" ]
[ "EverParse3d.ErrorCode.is_range_okay" ]
[]
(* Copyright 2019 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 EverParse3d.Actions.Base module Cast = FStar.Int.Cast open EverParse3d.Prelude module U32 = FStar.UInt32 module U64 = FStar.UInt64 // inline_for_extraction // let ___PUINT8 = LPL.puint8 inline_for_extraction
false
true
EverParse3d.Actions.Base.fsti
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 2, "max_fuel": 0, "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": [ "smt.qi.eager_threshold=10" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val is_range_okay : size: FStar.UInt32.t -> offset: FStar.UInt32.t -> access_size: FStar.UInt32.t -> Prims.bool
[]
EverParse3d.Actions.Base.is_range_okay
{ "file_name": "src/3d/prelude/EverParse3d.Actions.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
size: FStar.UInt32.t -> offset: FStar.UInt32.t -> access_size: FStar.UInt32.t -> Prims.bool
{ "end_col": 55, "end_line": 27, "start_col": 20, "start_line": 27 }
Prims.Tot
[ { "abbrev": true, "full_module": "FStar.UInt64", "short_module": "U64" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": false, "full_module": "EverParse3d.Prelude", "short_module": null }, { "abbrev": true, "full_module": "FStar.Int.Cast", "short_module": "Cast" }, { "abbrev": false, "full_module": "EverParse3d.Actions", "short_module": null }, { "abbrev": false, "full_module": "EverParse3d.Actions", "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 } ]
false
let validator #nz #wk (#k:parser_kind nz wk) (#t:Type) (p:parser k t) = validate_with_action_t p true_inv eloc_none true
let validator #nz #wk (#k: parser_kind nz wk) (#t: Type) (p: parser k t) =
false
null
false
validate_with_action_t p true_inv eloc_none true
{ "checked_file": "EverParse3d.Actions.Base.fsti.checked", "dependencies": [ "prims.fst.checked", "FStar.UInt64.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Int.Cast.fst.checked", "EverParse3d.Prelude.fsti.checked", "EverParse3d.ErrorCode.fst.checked" ], "interface_file": false, "source_file": "EverParse3d.Actions.Base.fsti" }
[ "total" ]
[ "Prims.bool", "EverParse3d.Kinds.weak_kind", "EverParse3d.Kinds.parser_kind", "EverParse3d.Prelude.parser", "EverParse3d.Actions.Base.validate_with_action_t", "EverParse3d.Actions.Base.true_inv", "EverParse3d.Actions.Base.eloc_none" ]
[]
(* Copyright 2019 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 EverParse3d.Actions.Base module Cast = FStar.Int.Cast open EverParse3d.Prelude module U32 = FStar.UInt32 module U64 = FStar.UInt64 // inline_for_extraction // let ___PUINT8 = LPL.puint8 inline_for_extraction noextract let is_range_okay = EverParse3d.ErrorCode.is_range_okay [@@erasable] val slice_inv : Type u#1 val inv_implies (inv0 inv1: slice_inv) : Tot prop val true_inv : slice_inv val conj_inv (i0 i1: slice_inv) : Tot slice_inv [@@erasable] val eloc : Type0 val eloc_union (l1 l2: eloc) : Tot eloc val eloc_none : eloc val eloc_includes (l1 l2: eloc) : Tot prop val inv_implies_refl (inv: slice_inv) : Tot (squash (inv `inv_implies` inv)) val inv_implies_true (inv0:slice_inv) : Tot (squash (inv0 `inv_implies` true_inv)) val inv_implies_conj (inv0 inv1 inv2: slice_inv) (h01: squash (inv0 `inv_implies` inv1)) (h02: squash (inv0 `inv_implies` inv2)) : Tot (squash (inv0 `inv_implies` (inv1 `conj_inv` inv2))) val eloc_includes_none (l1:eloc) : Tot (squash (l1 `eloc_includes` eloc_none)) val eloc_includes_union (l0: eloc) (l1 l2: eloc) (h01: squash (l0 `eloc_includes` l1)) (h02: squash (l0 `eloc_includes` l2)) : Tot (squash (l0 `eloc_includes` (l1 `eloc_union` l2))) val eloc_includes_refl (l: eloc) : Tot (squash (l `eloc_includes` l)) inline_for_extraction noextract val bpointer (a: Type0) : Tot Type0 val ptr_loc (#a: _) (x: bpointer a) : Tot eloc val ptr_inv (#a: _) (x: bpointer a) : Tot slice_inv inline_for_extraction noextract val action (#nz:bool) (#wk: _) (#k:parser_kind nz wk) (#t:Type) (p:parser k t) (inv:slice_inv) (l:eloc) (on_success:bool) (a:Type) : Type0 inline_for_extraction noextract val validate_with_action_t (#nz:bool) (#wk: _) (#k:parser_kind nz wk) (#t:Type) (p:parser k t) (inv:slice_inv) (l:eloc) (allow_reading:bool) : Type0 inline_for_extraction noextract val validate_eta (#nz:bool) (#wk: _) (#k:parser_kind nz wk) (#[@@@erasable] t:Type) (#[@@@erasable] p:parser k t) (#[@@@erasable] inv:slice_inv) (#[@@@erasable] l:eloc) (#allow_reading:bool) (v: validate_with_action_t p inv l allow_reading) : Tot (validate_with_action_t p inv l allow_reading) inline_for_extraction noextract val act_with_comment (s: string) (#nz:bool) (#wk: _) (#k:parser_kind nz wk) (#[@@@erasable] t:Type) (#[@@@erasable] p:parser k t) (#[@@@erasable] inv:slice_inv) (#[@@@erasable] l:eloc) (#b:_) (res:Type) (a: action p inv l b res) : Tot (action p inv l b res) inline_for_extraction noextract val leaf_reader (#nz:bool) (#k: parser_kind nz WeakKindStrongPrefix) (#t: Type) (p: parser k t) : Type u#0 inline_for_extraction noextract val validate_with_success_action (name: string) (#nz:bool) (#wk: _) (#k1:parser_kind nz wk) (#[@@@erasable] t1:Type) (#[@@@erasable] p1:parser k1 t1) (#[@@@erasable] inv1:slice_inv) (#[@@@erasable] l1:eloc) (#allow_reading:bool) (v1:validate_with_action_t p1 inv1 l1 allow_reading) (#[@@@erasable] inv2:slice_inv) (#[@@@erasable] l2:eloc) (#b:bool) (a:action p1 inv2 l2 b bool) : validate_with_action_t p1 (conj_inv inv1 inv2) (l1 `eloc_union` l2) false inline_for_extraction noextract val validate_with_error_handler (typename: string) (fieldname: string) (#nz: _) (#wk: _) (#k1:parser_kind nz wk) (#[@@@erasable] t1: Type) (#[@@@erasable] p1:parser k1 t1) (#[@@@erasable] inv1:slice_inv) (#[@@@erasable] l1:eloc) (#ar:_) (v1:validate_with_action_t p1 inv1 l1 ar) : validate_with_action_t p1 inv1 l1 ar inline_for_extraction noextract val validate_ret : validate_with_action_t (parse_ret ()) true_inv eloc_none true inline_for_extraction noextract val validate_pair (name1: string) (#nz1:_) (#k1:parser_kind nz1 WeakKindStrongPrefix) (#[@@@erasable] t1:Type) (#[@@@erasable] p1:parser k1 t1) (#[@@@erasable] inv1:slice_inv) (#[@@@erasable] l1:eloc) (#allow_reading1:bool) (v1:validate_with_action_t p1 inv1 l1 allow_reading1) (#nz2:_) (#wk2: _) (#k2:parser_kind nz2 wk2) (#[@@@erasable] t2:Type) (#[@@@erasable] p2:parser k2 t2) (#[@@@erasable] inv2:slice_inv) (#[@@@erasable] l2:eloc) (#allow_reading2:bool) (v2:validate_with_action_t p2 inv2 l2 allow_reading2) : validate_with_action_t (p1 `parse_pair` p2) (conj_inv inv1 inv2) (l1 `eloc_union` l2) false inline_for_extraction noextract val validate_dep_pair (name1: string) (#nz1:_) (#k1:parser_kind nz1 WeakKindStrongPrefix) (#t1:Type) (#[@@@erasable] p1:parser k1 t1) (#[@@@erasable] inv1:slice_inv) (#[@@@erasable] l1:eloc) (v1:validate_with_action_t p1 inv1 l1 true) (r1: leaf_reader p1) (#nz2:_) (#wk2: _) (#k2:parser_kind nz2 wk2) (#[@@@erasable] t2:t1 -> Type) (#[@@@erasable] p2:(x:t1 -> parser k2 (t2 x))) (#[@@@erasable] inv2:slice_inv) (#[@@@erasable] l2:eloc) (#allow_reading2:bool) (v2:(x:t1 -> validate_with_action_t (p2 x) inv2 l2 allow_reading2)) : validate_with_action_t (p1 `parse_dep_pair` p2) (conj_inv inv1 inv2) (l1 `eloc_union` l2) false inline_for_extraction noextract val validate_dep_pair_with_refinement_and_action (p1_is_constant_size_without_actions: bool) (name1: string) (#nz1:_) (#k1:parser_kind nz1 WeakKindStrongPrefix) (#t1:Type) (#[@@@erasable] p1:parser k1 t1) (#[@@@erasable] inv1:slice_inv) (#[@@@erasable] l1:eloc) (v1:validate_with_action_t p1 inv1 l1 true) (r1: leaf_reader p1) (f: t1 -> bool) (#[@@@erasable] inv1':slice_inv) (#[@@@erasable] l1':eloc) (#b:_) (a:t1 -> action p1 inv1' l1' b bool) (#nz2:_) (#wk2: _) (#k2:parser_kind nz2 wk2) (#[@@@erasable] t2:refine _ f -> Type) (#[@@@erasable] p2:(x:refine _ f -> parser k2 (t2 x))) (#[@@@erasable] inv2:slice_inv) (#[@@@erasable] l2:eloc) (#allow_reading2:bool) (v2:(x:refine _ f -> validate_with_action_t (p2 x) inv2 l2 allow_reading2)) : validate_with_action_t ((p1 `parse_filter` f) `parse_dep_pair` p2) (conj_inv inv1 (conj_inv inv1' inv2)) (l1 `eloc_union` (l1' `eloc_union` l2)) false inline_for_extraction noextract val validate_dep_pair_with_action (#nz1:_) (#k1:parser_kind nz1 WeakKindStrongPrefix) (#t1:Type) (#[@@@erasable] p1:parser k1 t1) (#[@@@erasable] inv1:slice_inv) (#[@@@erasable] l1:eloc) (v1:validate_with_action_t p1 inv1 l1 true) (r1: leaf_reader p1) (#[@@@erasable] inv1':slice_inv) (#[@@@erasable] l1':eloc) (#b:_) (a:t1 -> action p1 inv1' l1' b bool) (#nz2:_) (#wk2: _) (#k2:parser_kind nz2 wk2) (#[@@@erasable] t2:t1 -> Type) (#[@@@erasable] p2:(x:t1 -> parser k2 (t2 x))) (#[@@@erasable] inv2:slice_inv) (#[@@@erasable] l2:eloc) (#allow_reading2:_) (v2:(x:t1 -> validate_with_action_t (p2 x) inv2 l2 allow_reading2)) : validate_with_action_t (p1 `(parse_dep_pair #nz1)` p2) (conj_inv inv1 (conj_inv inv1' inv2)) (l1 `eloc_union` (l1' `eloc_union` l2)) false inline_for_extraction noextract val validate_dep_pair_with_refinement (p1_is_constant_size_without_actions: bool) (name1: string) (#nz1:_) (#k1:parser_kind nz1 WeakKindStrongPrefix) (#t1:Type) (#[@@@erasable] p1:parser k1 t1) (#[@@@erasable] inv1:slice_inv) (#[@@@erasable] l1:eloc) (v1:validate_with_action_t p1 inv1 l1 true) (r1: leaf_reader p1) (f: t1 -> bool) (#nz2:_) (#wk2: _) (#k2:parser_kind nz2 wk2) (#[@@@erasable] t2:refine _ f -> Type) (#[@@@erasable] p2:(x:refine _ f -> parser k2 (t2 x))) (#[@@@erasable] inv2:slice_inv) (#[@@@erasable] l2:eloc) (#allow_reading2:bool) (v2:(x:refine _ f -> validate_with_action_t (p2 x) inv2 l2 allow_reading2)) : validate_with_action_t ((p1 `parse_filter` f) `parse_dep_pair` p2) (conj_inv inv1 inv2) (l1 `eloc_union` l2) false inline_for_extraction noextract val validate_filter (name: string) (#nz:_) (#k:parser_kind nz WeakKindStrongPrefix) (#t:Type) (#[@@@erasable] p:parser k t) (#[@@@erasable] inv:slice_inv) (#[@@@erasable] l:eloc) (v:validate_with_action_t p inv l true) (r:leaf_reader p) (f:t -> bool) (cr:string) (cf:string) : validate_with_action_t (p `parse_filter` f) inv l false inline_for_extraction noextract val validate_filter_with_action (name: string) (#nz:_) (#k:parser_kind nz WeakKindStrongPrefix) (#t:Type) (#[@@@erasable] p:parser k t) (#[@@@erasable] inv:slice_inv) (#[@@@erasable] l:eloc) (v:validate_with_action_t p inv l true) (r:leaf_reader p) (f:t -> bool) (cr:string) (cf:string) (#b:bool) (#[@@@erasable] inva:slice_inv) (#[@@@erasable] la:eloc) (a: t -> action (p `parse_filter` f) inva la b bool) : validate_with_action_t #nz (p `parse_filter` f) (conj_inv inv inva) (eloc_union l la) false inline_for_extraction noextract val validate_with_dep_action (name: string) (#nz:_) (#k:parser_kind nz WeakKindStrongPrefix) (#t:Type) (#[@@@erasable] p:parser k t) (#[@@@erasable] inv:slice_inv) (#[@@@erasable] l:eloc) (v:validate_with_action_t p inv l true) (r:leaf_reader p) (#b:bool) (#[@@@erasable] inva:slice_inv) (#[@@@erasable] la:eloc) (a: t -> action p inva la b bool) : validate_with_action_t #nz p (conj_inv inv inva) (eloc_union l la) false inline_for_extraction noextract val validate_weaken_left (#nz:_) (#wk: _) (#k:parser_kind nz wk) (#[@@@erasable] t:Type) (#[@@@erasable] p:parser k t) (#[@@@erasable] inv:slice_inv) (#[@@@erasable] l:eloc) (#allow_reading:bool) (v:validate_with_action_t p inv l allow_reading) (#nz':_) (#wk': _) (k':parser_kind nz' wk') : validate_with_action_t (parse_weaken_left p k') inv l allow_reading inline_for_extraction noextract val validate_weaken_right (#nz:_) (#wk: _) (#k:parser_kind nz wk) (#[@@@erasable] t:Type) (#[@@@erasable] p:parser k t) (#[@@@erasable] inv:slice_inv) (#[@@@erasable] l:eloc) (#allow_reading:bool) (v:validate_with_action_t p inv l allow_reading) (#nz':_) (#wk': _) (k':parser_kind nz' wk') : validate_with_action_t (parse_weaken_right p k') inv l allow_reading inline_for_extraction noextract val validate_impos (_:unit) : validate_with_action_t (parse_impos ()) true_inv eloc_none true noextract inline_for_extraction val validate_ite (#nz:_) (#wk: _) (#k:parser_kind nz wk) (e:bool) (#[@@@erasable] a:squash e -> Type) (#[@@@erasable] b:squash (not e) -> Type) (#[@@@erasable] inv1:slice_inv) (#[@@@erasable] l1:eloc) (#ar1:_) (#[@@@erasable] inv2:slice_inv) (#[@@@erasable] l2:eloc) (#ar2:_) ([@@@erasable] p1:squash e -> parser k (a())) (v1:(squash e -> validate_with_action_t (p1()) inv1 l1 ar1)) ([@@@erasable] p2:squash (not e) -> parser k (b())) (v2:(squash (not e) -> validate_with_action_t (p2()) inv2 l2 ar2)) : validate_with_action_t (parse_ite e p1 p2) (conj_inv inv1 inv2) (l1 `eloc_union` l2) false noextract inline_for_extraction val validate_nlist (n:U32.t) (#wk: _) (#k:parser_kind true wk) (#[@@@erasable] t:Type) (#[@@@erasable] p:parser k t) (#[@@@erasable] inv:slice_inv) (#[@@@erasable] l:eloc) (#allow_reading:bool) (v: validate_with_action_t p inv l allow_reading) : validate_with_action_t (parse_nlist n p) inv l false noextract inline_for_extraction val validate_nlist_constant_size_without_actions (n_is_const: bool) (n:U32.t) (#wk: _) (#k:parser_kind true wk) (#[@@@erasable] t:Type) (#[@@@erasable] p:parser k t) (#[@@@erasable] inv:slice_inv) (#[@@@erasable] l:eloc) (#allow_reading:bool) (v: validate_with_action_t p inv l allow_reading) : Tot (validate_with_action_t (parse_nlist n p) inv l false) noextract inline_for_extraction val validate_t_at_most (n:U32.t) (#nz: _) (#wk: _) (#k:parser_kind nz wk) (#[@@@erasable] t:Type) (#[@@@erasable] p:parser k t) (#[@@@erasable] inv:slice_inv) (#[@@@erasable] l:eloc) (#ar:_) (v:validate_with_action_t p inv l ar) : Tot (validate_with_action_t (parse_t_at_most n p) inv l false) noextract inline_for_extraction val validate_t_exact (n:U32.t) (#nz:bool) (#wk: _) (#k:parser_kind nz wk) (#[@@@erasable] t:Type) (#[@@@erasable] p:parser k t) (#[@@@erasable] inv:slice_inv) (#[@@@erasable] l:eloc) (#ar:_) (v:validate_with_action_t p inv l ar) : Tot (validate_with_action_t (parse_t_exact n p) inv l false) inline_for_extraction noextract val validate_with_comment (c:string) (#nz:_) (#wk: _) (#k:parser_kind nz wk) (#[@@@erasable] t:Type) (#[@@@erasable] p:parser k t) (#[@@@erasable] inv:slice_inv) (#[@@@erasable] l:eloc) (#allow_reading:bool) (v:validate_with_action_t p inv l allow_reading) : validate_with_action_t p inv l allow_reading inline_for_extraction noextract val validate_weaken_inv_loc (#nz:_) (#wk: _) (#k:parser_kind nz wk) (#[@@@erasable] t:Type) (#[@@@erasable] p:parser k t) (#[@@@erasable] inv:slice_inv) (#[@@@erasable] l:eloc) (#allow_reading:bool) ([@@@erasable] inv':slice_inv{inv' `inv_implies` inv}) ([@@@erasable] l':eloc{l' `eloc_includes` l}) (v:validate_with_action_t p inv l allow_reading) : Tot (validate_with_action_t p inv' l' allow_reading) inline_for_extraction noextract val read_filter (#nz:_) (#k: parser_kind nz WeakKindStrongPrefix) (#t: Type) (#[@@@erasable] p: parser k t) (p32: leaf_reader p) (f: (t -> bool)) : leaf_reader (parse_filter p f) inline_for_extraction noextract val read_impos : leaf_reader (parse_impos()) inline_for_extraction
false
false
EverParse3d.Actions.Base.fsti
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 2, "max_fuel": 0, "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": [ "smt.qi.eager_threshold=10" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val validator : p: EverParse3d.Prelude.parser k t -> Type0
[]
EverParse3d.Actions.Base.validator
{ "file_name": "src/3d/prelude/EverParse3d.Actions.Base.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
p: EverParse3d.Prelude.parser k t -> Type0
{ "end_col": 52, "end_line": 502, "start_col": 4, "start_line": 502 }
Prims.Tot
val pad (a: md_alg) (total_len: nat{total_len `less_than_max_input_length` a}) : Tot (b: bytes{(S.length b + total_len) % block_length a = 0})
[ { "abbrev": false, "full_module": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "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 } ]
false
let pad (a:md_alg) (total_len:nat{total_len `less_than_max_input_length` a}): Tot (b:bytes{(S.length b + total_len) % block_length a = 0}) = let open FStar.Mul in let firstbyte = S.create 1 (u8 0x80) in let zeros = S.create (pad0_length a total_len) (u8 0) in let total_len_bits = total_len * 8 in // Saves the need for high fuel + makes hint replayable. max_input_size_len a; let encodedlen : lbytes (len_length a) = match a with | MD5 -> Lib.ByteSequence.uint_to_bytes_le (secret (nat_to_len a (total_len * 8))) | _ -> Lib.ByteSequence.uint_to_bytes_be (secret (nat_to_len a (total_len * 8))) in S.(firstbyte @| zeros @| encodedlen)
val pad (a: md_alg) (total_len: nat{total_len `less_than_max_input_length` a}) : Tot (b: bytes{(S.length b + total_len) % block_length a = 0}) let pad (a: md_alg) (total_len: nat{total_len `less_than_max_input_length` a}) : Tot (b: bytes{(S.length b + total_len) % block_length a = 0}) =
false
null
false
let open FStar.Mul in let firstbyte = S.create 1 (u8 0x80) in let zeros = S.create (pad0_length a total_len) (u8 0) in let total_len_bits = total_len * 8 in max_input_size_len a; let encodedlen:lbytes (len_length a) = match a with | MD5 -> Lib.ByteSequence.uint_to_bytes_le (secret (nat_to_len a (total_len * 8))) | _ -> Lib.ByteSequence.uint_to_bytes_be (secret (nat_to_len a (total_len * 8))) in let open S in firstbyte @| zeros @| encodedlen
{ "checked_file": "Spec.Hash.MD.fst.checked", "dependencies": [ "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Spec.Hash.MD.fst" }
[ "total" ]
[ "Spec.Hash.Definitions.md_alg", "Prims.nat", "Prims.b2t", "Spec.Hash.Definitions.less_than_max_input_length", "FStar.Seq.Base.op_At_Bar", "Lib.IntTypes.int_t", "Lib.IntTypes.U8", "Lib.IntTypes.SEC", "Lib.Sequence.lseq", "Spec.Hash.Definitions.len_length", "Lib.ByteSequence.uint_to_bytes_le", "Spec.Hash.Definitions.len_int_type", "Lib.IntTypes.secret", "Spec.Hash.Definitions.nat_to_len", "FStar.Mul.op_Star", "Spec.Hash.Definitions.hash_alg", "Lib.ByteSequence.uint_to_bytes_be", "Prims.unit", "Spec.Hash.MD.max_input_size_len", "Prims.int", "FStar.Seq.Base.seq", "FStar.Seq.Base.create", "Spec.Hash.Definitions.pad0_length", "Lib.IntTypes.u8", "Spec.Hash.Definitions.bytes", "Prims.op_Equality", "Prims.op_Modulus", "Prims.op_Addition", "FStar.Seq.Base.length", "Lib.IntTypes.uint8", "Spec.Hash.Definitions.block_length" ]
[]
module Spec.Hash.MD module S = FStar.Seq open Lib.IntTypes open Lib.ByteSequence open Spec.Hash.Definitions (** This module contains a Merkle-Damgard padding scheme for the MD hashes ONLY (md5, sha1, sha2) In Spec.Agile.Hash, the one-shot hash for MD hashes is defined pad, update_multi, finish. *) #push-options "--fuel 2 --ifuel 0" (* A useful lemma for all the operations that involve going from bytes to bits. *) let max_input_size_len (a: hash_alg{is_md a}): Lemma (ensures FStar.Mul.(Some ?.v (max_input_length a) * 8 + 8 = pow2 (len_length a * 8))) = let open FStar.Mul in assert_norm (Some?.v (max_input_length a) * 8 + 8 = pow2 (len_length a * 8)) #pop-options (** Padding *) #set-options "--fuel 0 --ifuel 0 --z3rlimit 10" let pad (a:md_alg) (total_len:nat{total_len `less_than_max_input_length` a}):
false
false
Spec.Hash.MD.fst
{ "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": 10, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val pad (a: md_alg) (total_len: nat{total_len `less_than_max_input_length` a}) : Tot (b: bytes{(S.length b + total_len) % block_length a = 0})
[]
Spec.Hash.MD.pad
{ "file_name": "specs/Spec.Hash.MD.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
a: Spec.Hash.Definitions.md_alg -> total_len: Prims.nat{Spec.Hash.Definitions.less_than_max_input_length total_len a} -> b: Spec.Hash.Definitions.bytes {(FStar.Seq.Base.length b + total_len) % Spec.Hash.Definitions.block_length a = 0}
{ "end_col": 40, "end_line": 43, "start_col": 4, "start_line": 32 }
FStar.Pervasives.Lemma
val max_input_size_len (a: hash_alg{is_md a}) : Lemma (ensures FStar.Mul.(Some?.v (max_input_length a) * 8 + 8 = pow2 (len_length a * 8)))
[ { "abbrev": false, "full_module": "Spec.Hash.Definitions", "short_module": null }, { "abbrev": false, "full_module": "Lib.ByteSequence", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": true, "full_module": "FStar.Seq", "short_module": "S" }, { "abbrev": false, "full_module": "Spec.Hash", "short_module": null }, { "abbrev": false, "full_module": "Spec.Hash", "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 } ]
false
let max_input_size_len (a: hash_alg{is_md a}): Lemma (ensures FStar.Mul.(Some ?.v (max_input_length a) * 8 + 8 = pow2 (len_length a * 8))) = let open FStar.Mul in assert_norm (Some?.v (max_input_length a) * 8 + 8 = pow2 (len_length a * 8))
val max_input_size_len (a: hash_alg{is_md a}) : Lemma (ensures FStar.Mul.(Some?.v (max_input_length a) * 8 + 8 = pow2 (len_length a * 8))) let max_input_size_len (a: hash_alg{is_md a}) : Lemma (ensures FStar.Mul.(Some?.v (max_input_length a) * 8 + 8 = pow2 (len_length a * 8))) =
false
null
true
let open FStar.Mul in assert_norm (Some?.v (max_input_length a) * 8 + 8 = pow2 (len_length a * 8))
{ "checked_file": "Spec.Hash.MD.fst.checked", "dependencies": [ "Spec.Hash.Definitions.fst.checked", "prims.fst.checked", "Lib.IntTypes.fsti.checked", "Lib.ByteSequence.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "Spec.Hash.MD.fst" }
[ "lemma" ]
[ "Spec.Hash.Definitions.hash_alg", "Prims.b2t", "Spec.Hash.Definitions.is_md", "FStar.Pervasives.assert_norm", "Prims.op_Equality", "Prims.int", "Prims.op_Addition", "FStar.Mul.op_Star", "FStar.Pervasives.Native.__proj__Some__item__v", "Prims.pos", "Spec.Hash.Definitions.max_input_length", "Prims.pow2", "Spec.Hash.Definitions.len_length", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module Spec.Hash.MD module S = FStar.Seq open Lib.IntTypes open Lib.ByteSequence open Spec.Hash.Definitions (** This module contains a Merkle-Damgard padding scheme for the MD hashes ONLY (md5, sha1, sha2) In Spec.Agile.Hash, the one-shot hash for MD hashes is defined pad, update_multi, finish. *) #push-options "--fuel 2 --ifuel 0" (* A useful lemma for all the operations that involve going from bytes to bits. *) let max_input_size_len (a: hash_alg{is_md a}): Lemma (ensures FStar.Mul.(Some ?.v (max_input_length a) * 8 + 8 = pow2 (len_length a * 8)))
false
false
Spec.Hash.MD.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 2, "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": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val max_input_size_len (a: hash_alg{is_md a}) : Lemma (ensures FStar.Mul.(Some?.v (max_input_length a) * 8 + 8 = pow2 (len_length a * 8)))
[]
Spec.Hash.MD.max_input_size_len
{ "file_name": "specs/Spec.Hash.MD.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
a: Spec.Hash.Definitions.hash_alg{Spec.Hash.Definitions.is_md a} -> FStar.Pervasives.Lemma (ensures Some?.v (Spec.Hash.Definitions.max_input_length a) * 8 + 8 = Prims.pow2 (Spec.Hash.Definitions.len_length a * 8))
{ "end_col": 78, "end_line": 22, "start_col": 2, "start_line": 21 }
FStar.HyperStack.ST.Stack
val clear_words_u8: #len:size_t -> b:lbytes len -> Stack unit (requires fun h -> live h b) (ensures fun h0 _ h1 -> modifies (loc b) h0 h1)
[ { "abbrev": false, "full_module": "Hacl.Impl.Matrix", "short_module": null }, { "abbrev": false, "full_module": "Lib.Memzero0", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Buffer", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Frodo", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Frodo", "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 } ]
false
let clear_words_u8 #len b = memzero #uint8 b len
val clear_words_u8: #len:size_t -> b:lbytes len -> Stack unit (requires fun h -> live h b) (ensures fun h0 _ h1 -> modifies (loc b) h0 h1) let clear_words_u8 #len b =
true
null
false
memzero #uint8 b len
{ "checked_file": "Hacl.Impl.Frodo.KEM.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Impl.Matrix.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "Hacl.Impl.Frodo.KEM.fst" }
[]
[ "Lib.IntTypes.size_t", "Hacl.Impl.Matrix.lbytes", "Lib.Memzero0.memzero", "Lib.IntTypes.uint8", "Prims.unit" ]
[]
module Hacl.Impl.Frodo.KEM open FStar.HyperStack open FStar.HyperStack.ST open FStar.Mul open LowStar.Buffer open Lib.IntTypes open Lib.Buffer open Lib.Memzero0 open Hacl.Impl.Matrix #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" inline_for_extraction noextract val clear_matrix: #n1:size_t -> #n2:size_t{v n1 * v n2 <= max_size_t} -> m:matrix_t n1 n2 -> Stack unit (requires fun h -> live h m) (ensures fun h0 _ h1 -> modifies1 m h0 h1) let clear_matrix #n1 #n2 m = memzero #uint16 m (n1 *! n2) inline_for_extraction noextract val clear_words_u8: #len:size_t -> b:lbytes len -> Stack unit (requires fun h -> live h b) (ensures fun h0 _ h1 -> modifies (loc b) h0 h1)
false
false
Hacl.Impl.Frodo.KEM.fst
{ "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": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val clear_words_u8: #len:size_t -> b:lbytes len -> Stack unit (requires fun h -> live h b) (ensures fun h0 _ h1 -> modifies (loc b) h0 h1)
[]
Hacl.Impl.Frodo.KEM.clear_words_u8
{ "file_name": "code/frodo/Hacl.Impl.Frodo.KEM.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
b: Hacl.Impl.Matrix.lbytes len -> FStar.HyperStack.ST.Stack Prims.unit
{ "end_col": 22, "end_line": 39, "start_col": 2, "start_line": 39 }
FStar.HyperStack.ST.Stack
val clear_matrix: #n1:size_t -> #n2:size_t{v n1 * v n2 <= max_size_t} -> m:matrix_t n1 n2 -> Stack unit (requires fun h -> live h m) (ensures fun h0 _ h1 -> modifies1 m h0 h1)
[ { "abbrev": false, "full_module": "Hacl.Impl.Matrix", "short_module": null }, { "abbrev": false, "full_module": "Lib.Memzero0", "short_module": null }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "LowStar.Buffer", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Frodo", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.Frodo", "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 } ]
false
let clear_matrix #n1 #n2 m = memzero #uint16 m (n1 *! n2)
val clear_matrix: #n1:size_t -> #n2:size_t{v n1 * v n2 <= max_size_t} -> m:matrix_t n1 n2 -> Stack unit (requires fun h -> live h m) (ensures fun h0 _ h1 -> modifies1 m h0 h1) let clear_matrix #n1 #n2 m =
true
null
false
memzero #uint16 m (n1 *! n2)
{ "checked_file": "Hacl.Impl.Frodo.KEM.fst.checked", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "Lib.Memzero0.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Impl.Matrix.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked" ], "interface_file": false, "source_file": "Hacl.Impl.Frodo.KEM.fst" }
[]
[ "Lib.IntTypes.size_t", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.Mul.op_Star", "Lib.IntTypes.v", "Lib.IntTypes.U32", "Lib.IntTypes.PUB", "Lib.IntTypes.max_size_t", "Hacl.Impl.Matrix.matrix_t", "Lib.Memzero0.memzero", "Lib.IntTypes.uint16", "Lib.IntTypes.op_Star_Bang", "Prims.unit" ]
[]
module Hacl.Impl.Frodo.KEM open FStar.HyperStack open FStar.HyperStack.ST open FStar.Mul open LowStar.Buffer open Lib.IntTypes open Lib.Buffer open Lib.Memzero0 open Hacl.Impl.Matrix #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" inline_for_extraction noextract val clear_matrix: #n1:size_t -> #n2:size_t{v n1 * v n2 <= max_size_t} -> m:matrix_t n1 n2 -> Stack unit (requires fun h -> live h m) (ensures fun h0 _ h1 -> modifies1 m h0 h1)
false
false
Hacl.Impl.Frodo.KEM.fst
{ "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": 50, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val clear_matrix: #n1:size_t -> #n2:size_t{v n1 * v n2 <= max_size_t} -> m:matrix_t n1 n2 -> Stack unit (requires fun h -> live h m) (ensures fun h0 _ h1 -> modifies1 m h0 h1)
[]
Hacl.Impl.Frodo.KEM.clear_matrix
{ "file_name": "code/frodo/Hacl.Impl.Frodo.KEM.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
m: Hacl.Impl.Matrix.matrix_t n1 n2 -> FStar.HyperStack.ST.Stack Prims.unit
{ "end_col": 30, "end_line": 27, "start_col": 2, "start_line": 27 }
Prims.Tot
val write_exchange_allowed (w1 w2: locations) (c1 c2: locations_with_values) : pbool
[ { "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 } ]
false
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
val write_exchange_allowed (w1 w2: locations) (c1 c2: locations_with_values) : pbool let write_exchange_allowed (w1 w2: locations) (c1 c2: locations_with_values) : pbool =
false
null
false
write_same_constants c1 c2 &&. for_all (aux_write_exchange_allowed w2 c1 c2) w1 &&. for_all (aux_write_exchange_allowed w1 c2 c1) w2
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "total" ]
[ "Vale.Transformers.Locations.locations", "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "Vale.Def.PossiblyMonad.op_Amp_Amp_Dot", "Vale.Transformers.InstructionReorder.write_same_constants", "Vale.Def.PossiblyMonad.for_all", "Vale.Transformers.Locations.location", "Vale.Transformers.InstructionReorder.aux_write_exchange_allowed", "Vale.Def.PossiblyMonad.pbool" ]
[]
(** 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")
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val write_exchange_allowed (w1 w2: locations) (c1 c2: locations_with_values) : pbool
[]
Vale.Transformers.InstructionReorder.write_exchange_allowed
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
w1: Vale.Transformers.Locations.locations -> w2: Vale.Transformers.Locations.locations -> c1: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> c2: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> Vale.Def.PossiblyMonad.pbool
{ "end_col": 50, "end_line": 87, "start_col": 2, "start_line": 82 }
Prims.GTot
val equiv_states_ext (s1 s2: machine_state) : GTot Type0
[ { "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 } ]
false
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)
val equiv_states_ext (s1 s2: machine_state) : GTot Type0 let equiv_states_ext (s1 s2: machine_state) : GTot Type0 =
false
null
false
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)
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "sometrivial" ]
[ "Vale.X64.Machine_Semantics_s.machine_state", "Prims.l_and", "FStar.FunctionalExtensionality.feq", "Vale.X64.Machine_s.reg", "Vale.X64.Machine_s.t_reg", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_regs", "Prims.eq2", "Vale.Arch.Heap.heap_impl", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_heap", "FStar.Map.equal", "Prims.int", "Vale.Def.Types_s.nat8", "Vale.X64.Machine_Semantics_s.__proj__Machine_stack__item__stack_mem", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stack", "Vale.Arch.HeapTypes_s.taint", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stackTaint", "Vale.Transformers.InstructionReorder.equiv_states" ]
[]
(** 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. *)
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val equiv_states_ext (s1 s2: machine_state) : GTot Type0
[]
Vale.Transformers.InstructionReorder.equiv_states_ext
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> Prims.GTot Type0
{ "end_col": 22, "end_line": 155, "start_col": 2, "start_line": 150 }
Prims.Tot
[ { "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 } ]
false
let filt_state (s:machine_state) = { s with ms_trace = [] }
let filt_state (s: machine_state) =
false
null
false
{ s with ms_trace = [] }
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "total" ]
[ "Vale.X64.Machine_Semantics_s.machine_state", "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.__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", "Prims.Nil", "Vale.X64.Machine_s.observation" ]
[]
(** 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. *)
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val filt_state : s: Vale.X64.Machine_Semantics_s.machine_state -> Vale.X64.Machine_Semantics_s.machine_state
[]
Vale.Transformers.InstructionReorder.filt_state
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
s: Vale.X64.Machine_Semantics_s.machine_state -> Vale.X64.Machine_Semantics_s.machine_state
{ "end_col": 17, "end_line": 419, "start_col": 4, "start_line": 418 }
Prims.Tot
val proof_run (s: machine_state) (f: st unit) : machine_state
[ { "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 } ]
false
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 }
val proof_run (s: machine_state) (f: st unit) : machine_state let proof_run (s: machine_state) (f: st unit) : machine_state =
false
null
false
let (), s1 = f s in { s1 with ms_ok = s1.ms_ok && s.ms_ok }
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "total" ]
[ "Vale.X64.Machine_Semantics_s.machine_state", "Vale.X64.Machine_Semantics_s.st", "Prims.unit", "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" ]
[]
(** 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.
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val proof_run (s: machine_state) (f: st unit) : machine_state
[]
Vale.Transformers.InstructionReorder.proof_run
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
s: Vale.X64.Machine_Semantics_s.machine_state -> f: Vale.X64.Machine_Semantics_s.st Prims.unit -> Vale.X64.Machine_Semantics_s.machine_state
{ "end_col": 41, "end_line": 195, "start_col": 61, "start_line": 193 }
Prims.Tot
val run2 (f1 f2: st unit) (s: machine_state) : machine_state
[ { "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 } ]
false
let run2 (f1 f2:st unit) (s:machine_state) : machine_state = let open Vale.X64.Machine_Semantics_s in run (f1;* f2;* return ()) s
val run2 (f1 f2: st unit) (s: machine_state) : machine_state let run2 (f1 f2: st unit) (s: machine_state) : machine_state =
false
null
false
let open Vale.X64.Machine_Semantics_s in run (let* _ = f1 in let* _ = f2 in return ()) s
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val run2 (f1 f2: st unit) (s: machine_state) : machine_state
[]
Vale.Transformers.InstructionReorder.run2
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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
{ "end_col": 29, "end_line": 558, "start_col": 2, "start_line": 557 }
Prims.Tot
val aux_write_exchange_allowed (w2: locations) (c1 c2: locations_with_values) (x: location) : pbool
[ { "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 } ]
false
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")
val aux_write_exchange_allowed (w2: locations) (c1 c2: locations_with_values) (x: location) : pbool let aux_write_exchange_allowed (w2: locations) (c1 c2: locations_with_values) (x: location) : pbool =
false
null
false
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")
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "total" ]
[ "Vale.Transformers.Locations.locations", "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "Vale.Transformers.Locations.location", "Prims.list", "Vale.Def.PossiblyMonad.op_Bar_Bar_Dot", "Vale.Transformers.Locations.disjoint_location_from_locations", "Vale.Def.PossiblyMonad.op_Slash_Subtraction", "Prims.op_AmpAmp", "FStar.List.Tot.Base.mem", "Vale.Def.PossiblyMonad.pbool", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "Vale.Transformers.InstructionReorder.locations_of_locations_with_values" ]
[]
(** 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
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val aux_write_exchange_allowed (w2: locations) (c1 c2: locations_with_values) (x: location) : pbool
[]
Vale.Transformers.InstructionReorder.aux_write_exchange_allowed
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
w2: Vale.Transformers.Locations.locations -> c1: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> c2: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> x: Vale.Transformers.Locations.location -> Vale.Def.PossiblyMonad.pbool
{ "end_col": 60, "end_line": 79, "start_col": 98, "start_line": 74 }
Prims.Tot
val rw_exchange_allowed (rw1 rw2: rw_set) : pbool
[ { "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 } ]
false
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 ")
val rw_exchange_allowed (rw1 rw2: rw_set) : pbool let rw_exchange_allowed (rw1 rw2: rw_set) : pbool =
false
null
false
let r1, w1, c1 = rw1.loc_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 ")
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "total" ]
[ "Vale.Transformers.BoundedInstructionEffects.rw_set", "Vale.Transformers.Locations.locations", "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "Vale.Def.PossiblyMonad.op_Amp_Amp_Dot", "Vale.Def.PossiblyMonad.op_Slash_Plus_Less", "Vale.Transformers.Locations.disjoint_locations", "Vale.Transformers.InstructionReorder.write_exchange_allowed", "Vale.Def.PossiblyMonad.pbool", "FStar.Pervasives.Native.tuple3", "Prims.list", "Vale.Transformers.Locations.location", "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" ]
[]
(** 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
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val rw_exchange_allowed (rw1 rw2: rw_set) : pbool
[]
Vale.Transformers.InstructionReorder.rw_exchange_allowed
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
rw1: Vale.Transformers.BoundedInstructionEffects.rw_set -> rw2: Vale.Transformers.BoundedInstructionEffects.rw_set -> Vale.Def.PossiblyMonad.pbool
{ "end_col": 77, "end_line": 94, "start_col": 52, "start_line": 89 }
Prims.Tot
[ { "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 } ]
false
let equiv_states_or_both_not_ok (s1 s2:machine_state) = (equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok))
let equiv_states_or_both_not_ok (s1 s2: machine_state) =
false
null
false
(equiv_states s1 s2) \/ ((not s1.ms_ok) /\ (not s2.ms_ok))
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "total" ]
[ "Vale.X64.Machine_Semantics_s.machine_state", "Prims.l_or", "Vale.Transformers.InstructionReorder.equiv_states", "Prims.l_and", "Prims.b2t", "Prims.op_Negation", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Prims.logical" ]
[]
(** 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. *)
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val equiv_states_or_both_not_ok : s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> Prims.logical
[]
Vale.Transformers.InstructionReorder.equiv_states_or_both_not_ok
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> Prims.logical
{ "end_col": 36, "end_line": 163, "start_col": 2, "start_line": 162 }
Prims.Tot
val ins_exchange_allowed (i1 i2: ins) : pbool
[ { "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 } ]
false
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)
val ins_exchange_allowed (i1 i2: ins) : pbool let ins_exchange_allowed (i1 i2: ins) : pbool =
false
null
false
(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)
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "total" ]
[ "Vale.X64.Machine_Semantics_s.ins", "Vale.Def.PossiblyMonad.op_Slash_Plus_Greater", "FStar.Pervasives.Native.Mktuple2", "Vale.X64.Bytes_Code_s.instruction_t", "Vale.X64.Machine_Semantics_s.instr_annotation", "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.Transformers.InstructionReorder.rw_exchange_allowed", "Vale.Transformers.BoundedInstructionEffects.rw_set_of_ins", "Vale.Def.PossiblyMonad.ffalse", "Vale.Def.PossiblyMonad.pbool", "Vale.X64.Instruction_s.normal", "Prims.string", "Prims.op_Hat", "Vale.X64.Print_s.print_ins", "Vale.X64.Print_s.gcc" ]
[]
(** 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 ")
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val ins_exchange_allowed (i1 i2: ins) : pbool
[]
Vale.Transformers.InstructionReorder.ins_exchange_allowed
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
i1: Vale.X64.Machine_Semantics_s.ins -> i2: Vale.X64.Machine_Semantics_s.ins -> Vale.Def.PossiblyMonad.pbool
{ "end_col": 85, "end_line": 103, "start_col": 2, "start_line": 97 }
Prims.GTot
val commutes (s: machine_state) (f1 f2: st unit) : GTot Type0
[ { "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 } ]
false
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)
val commutes (s: machine_state) (f1 f2: st unit) : GTot Type0 let commutes (s: machine_state) (f1 f2: st unit) : GTot Type0 =
false
null
false
equiv_states_or_both_not_ok (run2 f1 f2 s) (run2 f2 f1 s)
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "sometrivial" ]
[ "Vale.X64.Machine_Semantics_s.machine_state", "Vale.X64.Machine_Semantics_s.st", "Prims.unit", "Vale.Transformers.InstructionReorder.equiv_states_or_both_not_ok", "Vale.Transformers.InstructionReorder.run2" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val commutes (s: machine_state) (f1 f2: st unit) : GTot Type0
[]
Vale.Transformers.InstructionReorder.commutes
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
s: Vale.X64.Machine_Semantics_s.machine_state -> f1: Vale.X64.Machine_Semantics_s.st Prims.unit -> f2: Vale.X64.Machine_Semantics_s.st Prims.unit -> Prims.GTot Type0
{ "end_col": 18, "end_line": 563, "start_col": 2, "start_line": 561 }
Prims.GTot
val equiv_ostates (s1 s2: option machine_state) : GTot Type0
[ { "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 } ]
false
let equiv_ostates (s1 s2 : option machine_state) : GTot Type0 = (Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2)))
val equiv_ostates (s1 s2: option machine_state) : GTot Type0 let equiv_ostates (s1 s2: option machine_state) : GTot Type0 =
false
null
false
(Some? s1 = Some? s2) /\ (Some? s1 ==> (equiv_states (Some?.v s1) (Some?.v s2)))
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "sometrivial" ]
[ "FStar.Pervasives.Native.option", "Vale.X64.Machine_Semantics_s.machine_state", "Prims.l_and", "Prims.b2t", "Prims.op_Equality", "Prims.bool", "FStar.Pervasives.Native.uu___is_Some", "Prims.l_imp", "Vale.Transformers.InstructionReorder.equiv_states", "FStar.Pervasives.Native.__proj__Some__item__v" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val equiv_ostates (s1 s2: option machine_state) : GTot Type0
[]
Vale.Transformers.InstructionReorder.equiv_ostates
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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.GTot Type0
{ "end_col": 44, "end_line": 170, "start_col": 2, "start_line": 168 }
Prims.GTot
val equiv_states (s1 s2: machine_state) : GTot Type0
[ { "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 } ]
false
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)
val equiv_states (s1 s2: machine_state) : GTot Type0 let equiv_states (s1 s2: machine_state) : GTot Type0 =
false
null
false
(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)
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "sometrivial" ]
[ "Vale.X64.Machine_Semantics_s.machine_state", "Prims.l_and", "Prims.eq2", "Prims.bool", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Vale.X64.Machine_Semantics_s.regs_t", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_regs", "Prims.b2t", "Prims.op_Equality", "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.Arch.Heap.heap_impl", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_heap", "Vale.X64.Machine_Semantics_s.machine_stack", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stack", "Vale.Arch.HeapTypes_s.memTaint_t", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_stackTaint" ]
[]
(** 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.
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val equiv_states (s1 s2: machine_state) : GTot Type0
[]
Vale.Transformers.InstructionReorder.equiv_states
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
s1: Vale.X64.Machine_Semantics_s.machine_state -> s2: Vale.X64.Machine_Semantics_s.machine_state -> Prims.GTot Type0
{ "end_col": 40, "end_line": 145, "start_col": 2, "start_line": 139 }
Prims.Tot
[ { "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 } ]
false
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))
let equiv_option_states (s1 s2: option machine_state) =
false
null
false
(erroring_option_state s1 == erroring_option_state s2) /\ (not (erroring_option_state s1) ==> equiv_states (Some?.v s1) (Some?.v s2))
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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
[]
Vale.Transformers.InstructionReorder.equiv_option_states
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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
{ "end_col": 77, "end_line": 186, "start_col": 2, "start_line": 185 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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
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))) =
false
null
true
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": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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)
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)))
[]
Vale.Transformers.InstructionReorder.lemma_machine_eval_ins_st_exchange
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 75, "end_line": 1102, "start_col": 2, "start_line": 1098 }
FStar.Pervasives.Lemma
val lemma_ins_exchange_allowed_symmetric (i1 i2: ins) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (!!(ins_exchange_allowed i2 i1)))
[ { "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 } ]
false
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
val lemma_ins_exchange_allowed_symmetric (i1 i2: ins) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (!!(ins_exchange_allowed i2 i1))) let lemma_ins_exchange_allowed_symmetric (i1 i2: ins) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (!!(ins_exchange_allowed i2 i1))) =
false
null
true
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
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "lemma" ]
[ "Vale.X64.Machine_Semantics_s.ins", "Vale.Transformers.BoundedInstructionEffects.rw_set", "Vale.Transformers.Locations.locations", "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "Vale.Transformers.InstructionReorder.lemma_write_exchange_allowed_symmetric", "Prims.unit", "FStar.Pervasives.Native.tuple3", "Prims.list", "Vale.Transformers.Locations.location", "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", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "Vale.Transformers.BoundedInstructionEffects.rw_set_of_ins", "Prims.b2t", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.InstructionReorder.ins_exchange_allowed", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
(** 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)))
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val lemma_ins_exchange_allowed_symmetric (i1 i2: ins) : Lemma (requires (!!(ins_exchange_allowed i1 i2))) (ensures (!!(ins_exchange_allowed i2 i1)))
[]
Vale.Transformers.InstructionReorder.lemma_ins_exchange_allowed_symmetric
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
i1: Vale.X64.Machine_Semantics_s.ins -> i2: Vale.X64.Machine_Semantics_s.ins -> FStar.Pervasives.Lemma (requires !!(Vale.Transformers.InstructionReorder.ins_exchange_allowed i1 i2)) (ensures !!(Vale.Transformers.InstructionReorder.ins_exchange_allowed i2 i1))
{ "end_col": 52, "end_line": 133, "start_col": 42, "start_line": 129 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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
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))) =
false
null
true
lemma_machine_eval_ins_st_equiv_states i s1 s2
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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)
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)))
[]
Vale.Transformers.InstructionReorder.lemma_eval_ins_equiv_states
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 48, "end_line": 414, "start_col": 2, "start_line": 414 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
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)
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)) =
false
null
true
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": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))
[]
Vale.Transformers.InstructionReorder.lemma_unchanged_except_transitive
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 66, "end_line": 594, "start_col": 61, "start_line": 589 }
Prims.Tot
val wrap_sos (f: (machine_state -> option machine_state)) : st unit
[ { "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 } ]
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' )
val wrap_sos (f: (machine_state -> option machine_state)) : st unit let wrap_sos (f: (machine_state -> option machine_state)) : st unit =
false
null
false
fun s -> (match f s with | None -> (), { s with ms_ok = false } | Some s' -> (), s')
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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)
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val wrap_sos (f: (machine_state -> option machine_state)) : st unit
[]
Vale.Transformers.InstructionReorder.wrap_sos
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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
{ "end_col": 5, "end_line": 1037, "start_col": 2, "start_line": 1033 }
Prims.Tot
val locations_of_locations_with_values (lv: locations_with_values) : locations
[ { "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 } ]
false
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
val locations_of_locations_with_values (lv: locations_with_values) : locations let rec locations_of_locations_with_values (lv: locations_with_values) : locations =
false
null
false
match lv with | [] -> [] | (| l , v |) :: lv -> l :: locations_of_locations_with_values lv
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "total" ]
[ "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "Prims.Nil", "Vale.Transformers.Locations.location", "Vale.Transformers.Locations.location_eq", "Vale.Transformers.Locations.location_val_eqt", "Prims.list", "Vale.Transformers.BoundedInstructionEffects.location_with_value", "Prims.Cons", "Vale.Transformers.InstructionReorder.locations_of_locations_with_values", "Vale.Transformers.Locations.locations" ]
[]
(** 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
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val locations_of_locations_with_values (lv: locations_with_values) : locations
[ "recursion" ]
Vale.Transformers.InstructionReorder.locations_of_locations_with_values
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
lv: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> Vale.Transformers.Locations.locations
{ "end_col": 46, "end_line": 59, "start_col": 2, "start_line": 56 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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 (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 )
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 (let* _ = f1 in f2) s) (run f12 s))) =
false
null
true
let open Vale.X64.Machine_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)
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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)
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)))
[]
Vale.Transformers.InstructionReorder.lemma_equiv_code_codes
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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 (( op_let_Star* ) f1 (fun _ -> f2)) s) (Vale.X64.Machine_Semantics_s.run f12 s)))
{ "end_col": 3, "end_line": 1272, "start_col": 2, "start_line": 1255 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
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 (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
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 (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)) =
false
null
true
let open Vale.X64.Machine_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
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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 /\
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))
[]
Vale.Transformers.InstructionReorder.lemma_bounded_effects_code_codes_aux3
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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 (( op_let_Star* ) 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 (( op_let_Star* ) f1 (fun _ -> f2)) s1) = Mkmachine_state?.ms_ok (Vale.X64.Machine_Semantics_s.run f12 s1)))
{ "end_col": 59, "end_line": 1355, "start_col": 2, "start_line": 1345 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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 (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
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 (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))) =
false
null
true
let open Vale.X64.Machine_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
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)))
[]
Vale.Transformers.InstructionReorder.lemma_bounded_effects_code_codes_aux1
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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 (( op_let_Star* ) 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)))
{ "end_col": 58, "end_line": 1296, "start_col": 2, "start_line": 1287 }
FStar.Pervasives.Lemma
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))))
[ { "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 } ]
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
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)))) =
false
null
true
match c1 with | [] -> () | x :: xs -> lemma_write_same_constants_append xs c1' c2
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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) &&
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_write_same_constants_append
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)))
{ "end_col": 58, "end_line": 666, "start_col": 2, "start_line": 664 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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
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))) =
false
null
true
match as0 with | [] -> () | x :: xs -> lemma_unchanged_at_and_except xs s1 s2
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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 (
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_unchanged_at_and_except
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 42, "end_line": 803, "start_col": 2, "start_line": 800 }
FStar.Pervasives.Lemma
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))))
[ { "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 } ]
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
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)))) =
false
null
true
match as1 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_append a xs as2
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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)) <==>
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_disjoint_location_from_locations_append
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)))
{ "end_col": 58, "end_line": 584, "start_col": 2, "start_line": 581 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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 (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)
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 (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))) =
false
null
true
let open Vale.X64.Machine_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)
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)))
[]
Vale.Transformers.InstructionReorder.lemma_bounded_effects_code_codes_aux4
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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 (( op_let_Star* ) 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 (( op_let_Star* ) 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)))
{ "end_col": 33, "end_line": 1381, "start_col": 2, "start_line": 1368 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
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 (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
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 (let* _ = f1 in f2)))) (ensures (let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in bounded_effects rw f12)) =
false
null
true
let open Vale.X64.Machine_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
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))
[]
Vale.Transformers.InstructionReorder.lemma_bounded_effects_code_codes
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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 (( op_let_Star* ) 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))
{ "end_col": 36, "end_line": 1406, "start_col": 2, "start_line": 1393 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
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)) ) ) )
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)) =
false
null
true
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": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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))
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))
[]
Vale.Transformers.InstructionReorder.lemma_feq_bounded_effects
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 3, "end_line": 1066, "start_col": 2, "start_line": 1043 }
FStar.Pervasives.Lemma
val lemma_write_same_constants_symmetric (c1 c2: locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1)))
[ { "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 } ]
false
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
val lemma_write_same_constants_symmetric (c1 c2: locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) let rec lemma_write_same_constants_symmetric (c1 c2: locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1))) =
false
null
true
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
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "lemma" ]
[ "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "FStar.Pervasives.Native.Mktuple2", "Prims.list", "Vale.Transformers.BoundedInstructionEffects.location_with_value", "Vale.Transformers.InstructionReorder.lemma_write_same_constants_symmetric", "Prims.Nil", "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.Pervasives.pattern" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val lemma_write_same_constants_symmetric (c1 c2: locations_with_values) : Lemma (ensures (!!(write_same_constants c1 c2) = !!(write_same_constants c2 c1)))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_write_same_constants_symmetric
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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 c2) = !!(Vale.Transformers.InstructionReorder.write_same_constants c2 c1))
{ "end_col": 46, "end_line": 117, "start_col": 2, "start_line": 108 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
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
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)) =
false
null
true
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
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))
[]
Vale.Transformers.InstructionReorder.lemma_bounded_effects_on_functional_extensionality
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 36, "end_line": 1238, "start_col": 39, "start_line": 1218 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
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) ) else (); if (run f2 s).ms_ok then ( lemma_disjoint_implies_unchanged_at rw1.loc_reads rw2.loc_writes s (run f2 s) ) else ()
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)) =
false
null
true
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))
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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 =
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))
[]
Vale.Transformers.InstructionReorder.lemma_both_not_ok
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 11, "end_line": 864, "start_col": 2, "start_line": 859 }
FStar.Pervasives.Lemma
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))))
[ { "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 } ]
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: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 )
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 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)))) =
false
null
true
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": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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)
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_instr_write_outputs_equiv_states
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 5, "end_line": 323, "start_col": 2, "start_line": 302 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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 (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 ()
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 (let* _ = f1 in f2) s))) (ensures (let f12 = wrap_sos (machine_eval_codes (c :: cs) fuel) in (constant_on_execution cw f12 s))) =
false
null
true
let open Vale.X64.Machine_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))
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_bounded_effects_code_codes_aux2
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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 (( op_let_Star* ) 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))
{ "end_col": 11, "end_line": 1320, "start_col": 2, "start_line": 1308 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
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
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)) =
false
null
true
let x :: xs = c in if dfst x = l then () else lemma_value_of_const_loc_mem xs l v
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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) /\
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_value_of_const_loc_mem
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 64, "end_line": 694, "start_col": 33, "start_line": 692 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
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 )
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)) =
false
null
true
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": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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) /\
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_write_same_constants_mem_both
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 3, "end_line": 685, "start_col": 67, "start_line": 674 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
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)
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)) =
false
null
true
match xs with | [] -> () | x :: xs -> lemma_append_single xs y (i - 1)
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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 /\
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_append_single
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 47, "end_line": 1943, "start_col": 2, "start_line": 1941 }
Prims.Tot
val write_same_constants (c1 c2: locations_with_values) : pbool
[ { "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 } ]
false
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
val write_same_constants (c1 c2: locations_with_values) : pbool let write_same_constants (c1 c2: locations_with_values) : pbool =
false
null
false
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
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "total" ]
[ "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "Vale.Def.PossiblyMonad.for_all", "Vale.Transformers.BoundedInstructionEffects.location_with_value", "Vale.Transformers.Locations.location_eq", "Vale.Transformers.Locations.location_val_eqt", "Vale.Def.PossiblyMonad.op_Slash_Subtraction", "Prims.op_Equality", "Prims.bool", "Vale.Def.PossiblyMonad.pbool" ]
[]
(** 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.
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val write_same_constants (c1 c2: locations_with_values) : pbool
[]
Vale.Transformers.InstructionReorder.write_same_constants
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
c1: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> c2: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> Vale.Def.PossiblyMonad.pbool
{ "end_col": 8, "end_line": 72, "start_col": 2, "start_line": 66 }
FStar.Pervasives.Lemma
val 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)))
[ { "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 } ]
false
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
val 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))) 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))) =
false
null
true
lemma_write_same_constants_symmetric c1 c2
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "lemma" ]
[ "Vale.Transformers.Locations.locations", "Vale.Transformers.BoundedInstructionEffects.locations_with_values", "Vale.Transformers.InstructionReorder.lemma_write_same_constants_symmetric", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.b2t", "Prims.op_Equality", "Prims.bool", "Vale.Def.PossiblyMonad.op_Bang_Bang", "Vale.Transformers.InstructionReorder.write_exchange_allowed", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val 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)))
[]
Vale.Transformers.InstructionReorder.lemma_write_exchange_allowed_symmetric
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
w1: Vale.Transformers.Locations.locations -> w2: Vale.Transformers.Locations.locations -> c1: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> c2: Vale.Transformers.BoundedInstructionEffects.locations_with_values -> FStar.Pervasives.Lemma (ensures !!(Vale.Transformers.InstructionReorder.write_exchange_allowed w1 w2 c1 c2) = !!(Vale.Transformers.InstructionReorder.write_exchange_allowed w2 w1 c2 c1))
{ "end_col": 44, "end_line": 122, "start_col": 2, "start_line": 122 }
Prims.Tot
[ { "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 } ]
false
let erroring_option_state (s:option machine_state) = match s with | None -> true | Some s -> not (s.ms_ok)
let erroring_option_state (s: option machine_state) =
false
null
false
match s with | None -> true | Some s -> not (s.ms_ok)
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "total" ]
[ "FStar.Pervasives.Native.option", "Vale.X64.Machine_Semantics_s.machine_state", "Prims.op_Negation", "Vale.X64.Machine_Semantics_s.__proj__Mkmachine_state__item__ms_ok", "Prims.bool" ]
[]
(** 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
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val erroring_option_state : s: FStar.Pervasives.Native.option Vale.X64.Machine_Semantics_s.machine_state -> Prims.bool
[]
Vale.Transformers.InstructionReorder.erroring_option_state
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
s: FStar.Pervasives.Native.option Vale.X64.Machine_Semantics_s.machine_state -> Prims.bool
{ "end_col": 27, "end_line": 178, "start_col": 2, "start_line": 176 }
Prims.Tot
val wrap_ss (f: (machine_state -> machine_state)) : st unit
[ { "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 } ]
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)
val wrap_ss (f: (machine_state -> machine_state)) : st unit let wrap_ss (f: (machine_state -> machine_state)) : st unit =
false
null
false
let open Vale.X64.Machine_Semantics_s in let* s = get in set (f s)
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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)) )
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val wrap_ss (f: (machine_state -> machine_state)) : st unit
[]
Vale.Transformers.InstructionReorder.wrap_ss
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
f: (_: Vale.X64.Machine_Semantics_s.machine_state -> Vale.X64.Machine_Semantics_s.machine_state) -> Vale.X64.Machine_Semantics_s.st Prims.unit
{ "end_col": 11, "end_line": 1030, "start_col": 2, "start_line": 1028 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
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)
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)) =
false
null
true
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": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))
[]
Vale.Transformers.InstructionReorder.lemma_unchanged_except_append_symmetric
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 66, "end_line": 607, "start_col": 59, "start_line": 599 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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
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))) =
false
null
true
match a1 with | [_] -> () | x :: xs -> if a = x then () else lemma_disjoint_location_from_locations_mem xs a2 a
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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 (
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_disjoint_location_from_locations_mem
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 54, "end_line": 622, "start_col": 2, "start_line": 618 }
FStar.Pervasives.Lemma
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))))
[ { "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 } ]
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)
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)))) =
false
null
true
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)
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))))
[]
Vale.Transformers.InstructionReorder.lemma_instr_write_output_explicit_equiv_states
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 39, "end_line": 285, "start_col": 62, "start_line": 281 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
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
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)) =
false
null
true
match reads with | [] -> () | x :: xs -> lemma_disjoint_implies_unchanged_at xs changes s1 s2
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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))
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_disjoint_implies_unchanged_at
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 56, "end_line": 573, "start_col": 2, "start_line": 570 }
Prims.Tot
val code_exchange_allowed (c1 c2: safely_bounded_code) : pbool
[ { "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 } ]
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))
val code_exchange_allowed (c1 c2: safely_bounded_code) : pbool let code_exchange_allowed (c1 c2: safely_bounded_code) : pbool =
false
null
false
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": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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.
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val code_exchange_allowed (c1 c2: safely_bounded_code) : pbool
[]
Vale.Transformers.InstructionReorder.code_exchange_allowed
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
c1: Vale.Transformers.InstructionReorder.safely_bounded_code -> c2: Vale.Transformers.InstructionReorder.safely_bounded_code -> Vale.Def.PossiblyMonad.pbool
{ "end_col": 101, "end_line": 1451, "start_col": 2, "start_line": 1450 }
Prims.Tot
val find_transformation_hints (c1 c2: codes) : Tot (possibly transformation_hints) (decreases %[metric_for_codes c2;metric_for_codes c1])
[ { "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 } ]
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); (* 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) ) ) )
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]) =
false
null
false
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))))
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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) :
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val find_transformation_hints (c1 c2: codes) : Tot (possibly transformation_hints) (decreases %[metric_for_codes c2;metric_for_codes c1])
[ "recursion" ]
Vale.Transformers.InstructionReorder.find_transformation_hints
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 3, "end_line": 1881, "start_col": 61, "start_line": 1798 }
FStar.Pervasives.Lemma
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)) )
[ { "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 } ]
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
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)) ) =
false
null
true
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": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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) ==
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)) )
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_instr_apply_eval_args_equiv_states
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 73, "end_line": 218, "start_col": 2, "start_line": 206 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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
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))) =
false
null
true
match as0 with | [] -> () | x :: xs -> lemma_disjoint_location_from_locations_mem1 a xs
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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)))
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_disjoint_location_from_locations_mem1
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 63, "end_line": 650, "start_col": 2, "start_line": 648 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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 )
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))) =
false
null
true
match locv with | [_] -> () | x :: xs -> if x = (| l, v |) then () else (lemma_constant_on_execution_mem xs f s l v)
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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 (
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_constant_on_execution_mem
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 5, "end_line": 641, "start_col": 2, "start_line": 636 }
FStar.Pervasives.Lemma
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))))
[ { "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 } ]
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)
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)))) =
false
null
true
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)
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))))
[]
Vale.Transformers.InstructionReorder.lemma_instr_write_output_implicit_equiv_states
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 39, "end_line": 269, "start_col": 60, "start_line": 265 }
Prims.Tot
val eq_ins (i1 i2: ins) : bool
[ { "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 } ]
false
let eq_ins (i1 i2:ins) : bool = print_ins i1 gcc = print_ins i2 gcc
val eq_ins (i1 i2: ins) : bool let eq_ins (i1 i2: ins) : bool =
false
null
false
print_ins i1 gcc = print_ins i2 gcc
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val eq_ins (i1 i2: ins) : bool
[]
Vale.Transformers.InstructionReorder.eq_ins
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
i1: Vale.X64.Machine_Semantics_s.ins -> i2: Vale.X64.Machine_Semantics_s.ins -> Prims.bool
{ "end_col": 37, "end_line": 1695, "start_col": 2, "start_line": 1695 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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
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))) =
false
null
true
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
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)))
[]
Vale.Transformers.InstructionReorder.lemma_eval_instr_equiv_states
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 80, "end_line": 357, "start_col": 41, "start_line": 333 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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
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))) =
false
null
true
match as0 with | [_] -> () | x :: xs -> if a = x then () else lemma_unchanged_at_mem xs a s1 s2
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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 (
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_unchanged_at_mem
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 37, "end_line": 708, "start_col": 2, "start_line": 704 }
FStar.Pervasives.Lemma
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 }))))
[ { "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 } ]
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); (* 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 []
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 })))) =
false
null
true
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 []
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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 (
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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 }))))
[]
Vale.Transformers.InstructionReorder.lemma_equiv_states_when_except_none
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)))
{ "end_col": 50, "end_line": 813, "start_col": 2, "start_line": 811 }
FStar.Pervasives.Lemma
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))))
[ { "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 } ]
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 )
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)))) =
false
null
true
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)
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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 (
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_mem_not_disjoint
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 5, "end_line": 836, "start_col": 2, "start_line": 821 }
Prims.Tot
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
[ { "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 } ]
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
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 =
false
null
false
let x :: xs = lv in if dfst x = l then dsnd x else value_of_const_loc xs l
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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{
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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
[ "recursion" ]
Vale.Transformers.InstructionReorder.value_of_const_loc
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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
{ "end_col": 56, "end_line": 656, "start_col": 27, "start_line": 654 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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
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))) =
false
null
true
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
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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)))
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)))
[]
Vale.Transformers.InstructionReorder.lemma_unchanged_at_combine
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 17, "end_line": 783, "start_col": 53, "start_line": 722 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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
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))) =
false
null
true
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
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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) ==
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_instr_apply_eval_inouts_equiv_states
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 82, "end_line": 250, "start_col": 2, "start_line": 230 }
FStar.Pervasives.Lemma
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')))
[ { "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 } ]
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)
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'))) =
false
null
true
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": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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')))
[]
Vale.Transformers.InstructionReorder.lemma_instruction_exchange'
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 82, "end_line": 1116, "start_col": 2, "start_line": 1114 }
FStar.Pervasives.Lemma
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')))
[ { "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 } ]
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
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'))) =
false
null
true
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
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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')))
[]
Vale.Transformers.InstructionReorder.lemma_instruction_exchange
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 41, "end_line": 1134, "start_col": 2, "start_line": 1128 }
FStar.Pervasives.Lemma
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))))
[ { "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 } ]
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))
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)))) =
false
null
true
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": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))))
[]
Vale.Transformers.InstructionReorder.lemma_machine_eval_ins_bounded_effects
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)))
{ "end_col": 100, "end_line": 1090, "start_col": 2, "start_line": 1089 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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)) )
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))) =
false
null
true
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": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)))
[]
Vale.Transformers.InstructionReorder.lemma_commute
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 3, "end_line": 1025, "start_col": 28, "start_line": 991 }
Prims.Tot
val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a)
[ { "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 } ]
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
val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a) let split3 #a l i =
false
null
false
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": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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. *)
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val split3: #a:Type -> l:list a -> i:nat{i < L.length l} -> Tot (list a * a * list a)
[]
Vale.Transformers.InstructionReorder.split3
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
l: Prims.list a -> i: Prims.nat{i < FStar.List.Tot.Base.length l} -> (Prims.list a * a) * Prims.list a
{ "end_col": 9, "end_line": 1572, "start_col": 19, "start_line": 1568 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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 (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
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))) =
false
null
true
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
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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)
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)))
[]
Vale.Transformers.InstructionReorder.lemma_constant_on_execution_stays_constant
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 12, "end_line": 979, "start_col": 25, "start_line": 883 }
Prims.Tot
val num_blocks_in_codes (c: codes) : nat
[ { "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 } ]
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
val num_blocks_in_codes (c: codes) : nat let rec num_blocks_in_codes (c: codes) : nat =
false
null
false
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": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val num_blocks_in_codes (c: codes) : nat
[ "recursion" ]
Vale.Transformers.InstructionReorder.num_blocks_in_codes
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
c: Vale.X64.Machine_Semantics_s.codes -> Prims.nat
{ "end_col": 35, "end_line": 1520, "start_col": 2, "start_line": 1517 }
Prims.Tot
val wrap_diveinat (p: nat) (l: transformation_hints) : transformation_hints
[ { "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 } ]
false
let rec wrap_diveinat (p:nat) (l:transformation_hints) : transformation_hints = match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs
val wrap_diveinat (p: nat) (l: transformation_hints) : transformation_hints let rec wrap_diveinat (p: nat) (l: transformation_hints) : transformation_hints =
false
null
false
match l with | [] -> [] | x :: xs -> DiveInAt p x :: wrap_diveinat p xs
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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 ^ "]"
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val wrap_diveinat (p: nat) (l: transformation_hints) : transformation_hints
[ "recursion" ]
Vale.Transformers.InstructionReorder.wrap_diveinat
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
p: Prims.nat -> l: Vale.Transformers.InstructionReorder.transformation_hints -> Vale.Transformers.InstructionReorder.transformation_hints
{ "end_col": 38, "end_line": 1563, "start_col": 2, "start_line": 1560 }
FStar.Pervasives.Lemma
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))]
[ { "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 } ]
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
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))] =
false
null
true
match c1 with | [] -> () | x :: xs -> lemma_num_blocks_in_codes_append xs c2
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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))
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))]
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_num_blocks_in_codes_append
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))]
{ "end_col": 53, "end_line": 1528, "start_col": 2, "start_line": 1526 }
Prims.Tot
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 })
[ { "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 } ]
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) ) ) )
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 }) =
false
null
false
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))))
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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 })
[ "recursion" ]
Vale.Transformers.InstructionReorder.bubble_to_top
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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 })
{ "end_col": 5, "end_line": 1513, "start_col": 2, "start_line": 1491 }
Prims.Tot
val increment_hint (th: transformation_hint) : transformation_hint
[ { "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 } ]
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
val increment_hint (th: transformation_hint) : transformation_hint let increment_hint (th: transformation_hint) : transformation_hint =
false
null
false
match th with | MoveUpFrom p -> MoveUpFrom (p + 1) | DiveInAt p q -> DiveInAt (p + 1) q | _ -> th
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "total" ]
[ "Vale.Transformers.InstructionReorder.transformation_hint", "Prims.nat", "Vale.Transformers.InstructionReorder.MoveUpFrom", "Prims.op_Addition", "Vale.Transformers.InstructionReorder.DiveInAt" ]
[]
(** 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
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val increment_hint (th: transformation_hint) : transformation_hint
[]
Vale.Transformers.InstructionReorder.increment_hint
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
th: Vale.Transformers.InstructionReorder.transformation_hint -> Vale.Transformers.InstructionReorder.transformation_hint
{ "end_col": 11, "end_line": 1730, "start_col": 2, "start_line": 1727 }
FStar.Pervasives.Lemma
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)))
[ { "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 } ]
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
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))) =
false
null
true
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
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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)
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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)))
[]
Vale.Transformers.InstructionReorder.lemma_code_exchange_allowed
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 77, "end_line": 1478, "start_col": 2, "start_line": 1462 }
Prims.Tot
val find_deep_code_transform (c: code) (cs: codes) : possibly transformation_hint
[ { "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 } ]
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 -> (* 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) ) )
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 =
false
null
false
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)))
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
true
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
val find_deep_code_transform (c: code) (cs: codes) : possibly transformation_hint
[ "recursion" ]
Vale.Transformers.InstructionReorder.find_deep_code_transform
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
c: Vale.X64.Machine_Semantics_s.code -> cs: Vale.X64.Machine_Semantics_s.codes -> Vale.Def.PossiblyMonad.possibly Vale.Transformers.InstructionReorder.transformation_hint
{ "end_col": 5, "end_line": 1766, "start_col": 2, "start_line": 1733 }
FStar.Pervasives.Lemma
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))]
[ { "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 } ]
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
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))] =
false
null
true
match c1 with | [] -> () | x :: xs -> lemma_metric_for_codes_append xs c2
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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))
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))]
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_metric_for_codes_append
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))]
{ "end_col": 50, "end_line": 1788, "start_col": 2, "start_line": 1786 }
FStar.Pervasives.Lemma
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))
[ { "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 } ]
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
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)) =
false
null
true
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
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_machine_eval_codes_block_to_append
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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)
{ "end_col": 60, "end_line": 1932, "start_col": 2, "start_line": 1926 }
FStar.Pervasives.Lemma
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))))
[ { "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 } ]
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 )
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)))) =
false
null
true
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)
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "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" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "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" }
null
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))))
[ "recursion" ]
Vale.Transformers.InstructionReorder.lemma_bubble_to_top
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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))
{ "end_col": 5, "end_line": 1919, "start_col": 2, "start_line": 1903 }
FStar.Pervasives.Lemma
val 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)))
[ { "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 } ]
false
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)
val 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 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))) =
false
null
true
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 let new_dst1 = eval_operand stack_op s1 in let new_dst2 = eval_operand stack_op s2 in let new_rsp1 = (eval_reg_64 rRsp s1 + 8) % pow2_64 in let new_rsp2 = (eval_reg_64 rRsp s2 + 8) % pow2_64 in 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 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)
{ "checked_file": "Vale.Transformers.InstructionReorder.fst.checked", "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" }
[ "lemma" ]
[ "Vale.X64.Machine_Semantics_s.ins", "Vale.X64.Machine_Semantics_s.machine_state", "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.Transformers.InstructionReorder.lemma_eval_instr_equiv_states", "Vale.X64.Machine_s.operand64", "Vale.Arch.HeapTypes_s.taint", "FStar.Pervasives.assert_spinoff", "Vale.Transformers.InstructionReorder.equiv_states_ext", "Vale.Transformers.InstructionReorder.equiv_states", "Prims.unit", "Prims._assert", "Vale.Transformers.InstructionReorder.proof_run", "Vale.X64.Machine_Semantics_s.update_rsp", "Vale.X64.Machine_Semantics_s.free_stack", "Prims.op_Subtraction", "Vale.X64.Machine_Semantics_s.update_operand64_preserve_flags", "Prims.int", "Prims.op_Modulus", "Prims.op_Addition", "Vale.X64.Machine_Semantics_s.eval_reg_64", "Vale.X64.Machine_s.rRsp", "Vale.X64.Machine_s.pow2_64", "Vale.Def.Words_s.nat64", "Vale.X64.Machine_Semantics_s.eval_operand", "Vale.X64.Machine_Semantics_s.check", "Vale.X64.Machine_Semantics_s.valid_src_operand64_and_taint", "Vale.X64.Machine_s.operand", "Vale.X64.Machine_s.reg_64", "Vale.X64.Machine_s.OStack", "Vale.X64.Machine_s.nat64", "FStar.Pervasives.Native.Mktuple2", "Vale.X64.Machine_s.maddr", "Vale.X64.Machine_s.MReg", "Vale.X64.Machine_s.Reg", "Vale.X64.Machine_Semantics_s.run", "Vale.X64.Machine_Semantics_s.machine_eval_ins_st", "FStar.Pervasives.Native.tuple2", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
(** 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
false
false
Vale.Transformers.InstructionReorder.fst
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 0, "max_fuel": 0, "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": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val 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)))
[]
Vale.Transformers.InstructionReorder.lemma_machine_eval_ins_st_equiv_states
{ "file_name": "vale/code/lib/transformers/Vale.Transformers.InstructionReorder.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
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.run (Vale.X64.Machine_Semantics_s.machine_eval_ins_st i) s1) (Vale.X64.Machine_Semantics_s.run (Vale.X64.Machine_Semantics_s.machine_eval_ins_st i) s2) )
{ "end_col": 55, "end_line": 404, "start_col": 46, "start_line": 371 }
Prims.Tot
val to_fun (m: t) : regs_fun
[ { "abbrev": false, "full_module": "Vale.Lib.Map16", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Prop_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "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 } ]
false
let to_fun (m:t) : regs_fun = FStar.FunctionalExtensionality.on_dom reg (fun (r:reg) -> sel r m)
val to_fun (m: t) : regs_fun let to_fun (m: t) : regs_fun =
false
null
false
FStar.FunctionalExtensionality.on_dom reg (fun (r: reg) -> sel r m)
{ "checked_file": "Vale.X64.Regs.fsti.checked", "dependencies": [ "Vale.X64.Machine_s.fst.checked", "Vale.Lib.Map16.fsti.checked", "Vale.Def.Prop_s.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.FunctionalExtensionality.fsti.checked" ], "interface_file": false, "source_file": "Vale.X64.Regs.fsti" }
[ "total" ]
[ "Vale.X64.Regs.t", "FStar.FunctionalExtensionality.on_dom", "Vale.X64.Machine_s.reg", "Vale.X64.Machine_s.t_reg", "Vale.X64.Regs.sel", "Vale.X64.Regs.regs_fun" ]
[]
module Vale.X64.Regs open FStar.Mul // This interface should not refer to Machine_Semantics_s open Vale.Def.Prop_s open Vale.X64.Machine_s open Vale.Lib.Map16 type regs_fun = FStar.FunctionalExtensionality.restricted_t reg t_reg type regs_def = map16 nat64 & map16 quad32 [@"opaque_to_smt"] type t = regs_def [@va_qattr "opaque_to_smt"] let sel (r:reg) (m:t) : t_reg r = match m with (m0, m1) -> match r with Reg rf i -> match rf with | 0 -> sel16 m0 i | 1 -> sel16 m1 i [@va_qattr "opaque_to_smt"] let upd (r:reg) (v:t_reg r) (m:t) : t = match m with (m0, m1) -> match r with Reg rf i -> match rf with | 0 -> (upd16 m0 i v, m1) | 1 -> (m0, upd16 m1 i v) // Used in eta-expansion; we ensure that it stops normalization by not marking it va_qattr [@"opaque_to_smt"] let eta_sel (r:reg) (m:t) : v:(t_reg r){v == sel r m} = sel r m // Eta-expand into a map where normalization gracefully terminates to eta_sel applications, // so that we don't accidentally normalize past type abstractions [@va_qattr "opaque_to_smt"] let eta (m:t) : t = let m0_3 = ((eta_sel (Reg 0 0) m, eta_sel (Reg 0 1) m), (eta_sel (Reg 0 2) m, eta_sel (Reg 0 3) m)) in let m4_7 = ((eta_sel (Reg 0 4) m, eta_sel (Reg 0 5) m), (eta_sel (Reg 0 6) m, eta_sel (Reg 0 7) m)) in let m8_11 = ((eta_sel (Reg 0 8) m, eta_sel (Reg 0 9) m), (eta_sel (Reg 0 10) m, eta_sel (Reg 0 11) m)) in let m12_15 = ((eta_sel (Reg 0 12) m, eta_sel (Reg 0 13) m), (eta_sel (Reg 0 14) m, eta_sel (Reg 0 15) m)) in let m0 = ((m0_3, m4_7), (m8_11, m12_15)) in let m0_3 = ((eta_sel (Reg 1 0) m, eta_sel (Reg 1 1) m), (eta_sel (Reg 1 2) m, eta_sel (Reg 1 3) m)) in let m4_7 = ((eta_sel (Reg 1 4) m, eta_sel (Reg 1 5) m), (eta_sel (Reg 1 6) m, eta_sel (Reg 1 7) m)) in let m8_11 = ((eta_sel (Reg 1 8) m, eta_sel (Reg 1 9) m), (eta_sel (Reg 1 10) m, eta_sel (Reg 1 11) m)) in let m12_15 = ((eta_sel (Reg 1 12) m, eta_sel (Reg 1 13) m), (eta_sel (Reg 1 14) m, eta_sel (Reg 1 15) m)) in let m1 = ((m0_3, m4_7), (m8_11, m12_15)) in (m0, m1)
false
true
Vale.X64.Regs.fsti
{ "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" }
null
val to_fun (m: t) : regs_fun
[]
Vale.X64.Regs.to_fun
{ "file_name": "vale/code/arch/x64/Vale.X64.Regs.fsti", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
m: Vale.X64.Regs.t -> Vale.X64.Regs.regs_fun
{ "end_col": 68, "end_line": 53, "start_col": 2, "start_line": 53 }
Prims.Tot
val eta_sel (r: reg) (m: t) : v: (t_reg r){v == sel r m}
[ { "abbrev": false, "full_module": "Vale.Lib.Map16", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Prop_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "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 } ]
false
let eta_sel (r:reg) (m:t) : v:(t_reg r){v == sel r m} = sel r m
val eta_sel (r: reg) (m: t) : v: (t_reg r){v == sel r m} let eta_sel (r: reg) (m: t) : v: (t_reg r){v == sel r m} =
false
null
false
sel r m
{ "checked_file": "Vale.X64.Regs.fsti.checked", "dependencies": [ "Vale.X64.Machine_s.fst.checked", "Vale.Lib.Map16.fsti.checked", "Vale.Def.Prop_s.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.FunctionalExtensionality.fsti.checked" ], "interface_file": false, "source_file": "Vale.X64.Regs.fsti" }
[ "total" ]
[ "Vale.X64.Machine_s.reg", "Vale.X64.Regs.t", "Vale.X64.Regs.sel", "Vale.X64.Machine_s.t_reg", "Prims.eq2" ]
[]
module Vale.X64.Regs open FStar.Mul // This interface should not refer to Machine_Semantics_s open Vale.Def.Prop_s open Vale.X64.Machine_s open Vale.Lib.Map16 type regs_fun = FStar.FunctionalExtensionality.restricted_t reg t_reg type regs_def = map16 nat64 & map16 quad32 [@"opaque_to_smt"] type t = regs_def [@va_qattr "opaque_to_smt"] let sel (r:reg) (m:t) : t_reg r = match m with (m0, m1) -> match r with Reg rf i -> match rf with | 0 -> sel16 m0 i | 1 -> sel16 m1 i [@va_qattr "opaque_to_smt"] let upd (r:reg) (v:t_reg r) (m:t) : t = match m with (m0, m1) -> match r with Reg rf i -> match rf with | 0 -> (upd16 m0 i v, m1) | 1 -> (m0, upd16 m1 i v) // Used in eta-expansion; we ensure that it stops normalization by not marking it va_qattr [@"opaque_to_smt"]
false
false
Vale.X64.Regs.fsti
{ "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" }
null
val eta_sel (r: reg) (m: t) : v: (t_reg r){v == sel r m}
[]
Vale.X64.Regs.eta_sel
{ "file_name": "vale/code/arch/x64/Vale.X64.Regs.fsti", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
r: Vale.X64.Machine_s.reg -> m: Vale.X64.Regs.t -> v: Vale.X64.Machine_s.t_reg r {v == Vale.X64.Regs.sel r m}
{ "end_col": 9, "end_line": 34, "start_col": 2, "start_line": 34 }
Prims.Tot
val sel (r: reg) (m: t) : t_reg r
[ { "abbrev": false, "full_module": "Vale.Lib.Map16", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Prop_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "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 } ]
false
let sel (r:reg) (m:t) : t_reg r = match m with (m0, m1) -> match r with Reg rf i -> match rf with | 0 -> sel16 m0 i | 1 -> sel16 m1 i
val sel (r: reg) (m: t) : t_reg r let sel (r: reg) (m: t) : t_reg r =
false
null
false
match m with | m0, m1 -> match r with | Reg rf i -> match rf with | 0 -> sel16 m0 i | 1 -> sel16 m1 i
{ "checked_file": "Vale.X64.Regs.fsti.checked", "dependencies": [ "Vale.X64.Machine_s.fst.checked", "Vale.Lib.Map16.fsti.checked", "Vale.Def.Prop_s.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.FunctionalExtensionality.fsti.checked" ], "interface_file": false, "source_file": "Vale.X64.Regs.fsti" }
[ "total" ]
[ "Vale.X64.Machine_s.reg", "Vale.X64.Regs.t", "Vale.Lib.Map16.map16", "Vale.X64.Machine_s.nat64", "Vale.X64.Machine_s.quad32", "Vale.X64.Machine_s.reg_file_id", "Vale.X64.Machine_s.reg_id", "Vale.Lib.Map16.sel16", "Vale.X64.Machine_s.t_reg" ]
[]
module Vale.X64.Regs open FStar.Mul // This interface should not refer to Machine_Semantics_s open Vale.Def.Prop_s open Vale.X64.Machine_s open Vale.Lib.Map16 type regs_fun = FStar.FunctionalExtensionality.restricted_t reg t_reg type regs_def = map16 nat64 & map16 quad32 [@"opaque_to_smt"] type t = regs_def [@va_qattr "opaque_to_smt"]
false
false
Vale.X64.Regs.fsti
{ "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" }
null
val sel (r: reg) (m: t) : t_reg r
[]
Vale.X64.Regs.sel
{ "file_name": "vale/code/arch/x64/Vale.X64.Regs.fsti", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
r: Vale.X64.Machine_s.reg -> m: Vale.X64.Regs.t -> Vale.X64.Machine_s.t_reg r
{ "end_col": 19, "end_line": 21, "start_col": 2, "start_line": 17 }
Prims.Tot
val upd (r: reg) (v: t_reg r) (m: t) : t
[ { "abbrev": false, "full_module": "Vale.Lib.Map16", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Prop_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "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 } ]
false
let upd (r:reg) (v:t_reg r) (m:t) : t = match m with (m0, m1) -> match r with Reg rf i -> match rf with | 0 -> (upd16 m0 i v, m1) | 1 -> (m0, upd16 m1 i v)
val upd (r: reg) (v: t_reg r) (m: t) : t let upd (r: reg) (v: t_reg r) (m: t) : t =
false
null
false
match m with | m0, m1 -> match r with | Reg rf i -> match rf with | 0 -> (upd16 m0 i v, m1) | 1 -> (m0, upd16 m1 i v)
{ "checked_file": "Vale.X64.Regs.fsti.checked", "dependencies": [ "Vale.X64.Machine_s.fst.checked", "Vale.Lib.Map16.fsti.checked", "Vale.Def.Prop_s.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.FunctionalExtensionality.fsti.checked" ], "interface_file": false, "source_file": "Vale.X64.Regs.fsti" }
[ "total" ]
[ "Vale.X64.Machine_s.reg", "Vale.X64.Machine_s.t_reg", "Vale.X64.Regs.t", "Vale.Lib.Map16.map16", "Vale.X64.Machine_s.nat64", "Vale.X64.Machine_s.quad32", "Vale.X64.Machine_s.reg_file_id", "Vale.X64.Machine_s.reg_id", "FStar.Pervasives.Native.Mktuple2", "Vale.Lib.Map16.upd16" ]
[]
module Vale.X64.Regs open FStar.Mul // This interface should not refer to Machine_Semantics_s open Vale.Def.Prop_s open Vale.X64.Machine_s open Vale.Lib.Map16 type regs_fun = FStar.FunctionalExtensionality.restricted_t reg t_reg type regs_def = map16 nat64 & map16 quad32 [@"opaque_to_smt"] type t = regs_def [@va_qattr "opaque_to_smt"] let sel (r:reg) (m:t) : t_reg r = match m with (m0, m1) -> match r with Reg rf i -> match rf with | 0 -> sel16 m0 i | 1 -> sel16 m1 i [@va_qattr "opaque_to_smt"]
false
false
Vale.X64.Regs.fsti
{ "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" }
null
val upd (r: reg) (v: t_reg r) (m: t) : t
[]
Vale.X64.Regs.upd
{ "file_name": "vale/code/arch/x64/Vale.X64.Regs.fsti", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
r: Vale.X64.Machine_s.reg -> v: Vale.X64.Machine_s.t_reg r -> m: Vale.X64.Regs.t -> Vale.X64.Regs.t
{ "end_col": 27, "end_line": 29, "start_col": 2, "start_line": 25 }
Prims.Tot
val eta (m: t) : t
[ { "abbrev": false, "full_module": "Vale.Lib.Map16", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Prop_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "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 } ]
false
let eta (m:t) : t = let m0_3 = ((eta_sel (Reg 0 0) m, eta_sel (Reg 0 1) m), (eta_sel (Reg 0 2) m, eta_sel (Reg 0 3) m)) in let m4_7 = ((eta_sel (Reg 0 4) m, eta_sel (Reg 0 5) m), (eta_sel (Reg 0 6) m, eta_sel (Reg 0 7) m)) in let m8_11 = ((eta_sel (Reg 0 8) m, eta_sel (Reg 0 9) m), (eta_sel (Reg 0 10) m, eta_sel (Reg 0 11) m)) in let m12_15 = ((eta_sel (Reg 0 12) m, eta_sel (Reg 0 13) m), (eta_sel (Reg 0 14) m, eta_sel (Reg 0 15) m)) in let m0 = ((m0_3, m4_7), (m8_11, m12_15)) in let m0_3 = ((eta_sel (Reg 1 0) m, eta_sel (Reg 1 1) m), (eta_sel (Reg 1 2) m, eta_sel (Reg 1 3) m)) in let m4_7 = ((eta_sel (Reg 1 4) m, eta_sel (Reg 1 5) m), (eta_sel (Reg 1 6) m, eta_sel (Reg 1 7) m)) in let m8_11 = ((eta_sel (Reg 1 8) m, eta_sel (Reg 1 9) m), (eta_sel (Reg 1 10) m, eta_sel (Reg 1 11) m)) in let m12_15 = ((eta_sel (Reg 1 12) m, eta_sel (Reg 1 13) m), (eta_sel (Reg 1 14) m, eta_sel (Reg 1 15) m)) in let m1 = ((m0_3, m4_7), (m8_11, m12_15)) in (m0, m1)
val eta (m: t) : t let eta (m: t) : t =
false
null
false
let m0_3 = ((eta_sel (Reg 0 0) m, eta_sel (Reg 0 1) m), (eta_sel (Reg 0 2) m, eta_sel (Reg 0 3) m)) in let m4_7 = ((eta_sel (Reg 0 4) m, eta_sel (Reg 0 5) m), (eta_sel (Reg 0 6) m, eta_sel (Reg 0 7) m)) in let m8_11 = ((eta_sel (Reg 0 8) m, eta_sel (Reg 0 9) m), (eta_sel (Reg 0 10) m, eta_sel (Reg 0 11) m)) in let m12_15 = ((eta_sel (Reg 0 12) m, eta_sel (Reg 0 13) m), (eta_sel (Reg 0 14) m, eta_sel (Reg 0 15) m)) in let m0 = ((m0_3, m4_7), (m8_11, m12_15)) in let m0_3 = ((eta_sel (Reg 1 0) m, eta_sel (Reg 1 1) m), (eta_sel (Reg 1 2) m, eta_sel (Reg 1 3) m)) in let m4_7 = ((eta_sel (Reg 1 4) m, eta_sel (Reg 1 5) m), (eta_sel (Reg 1 6) m, eta_sel (Reg 1 7) m)) in let m8_11 = ((eta_sel (Reg 1 8) m, eta_sel (Reg 1 9) m), (eta_sel (Reg 1 10) m, eta_sel (Reg 1 11) m)) in let m12_15 = ((eta_sel (Reg 1 12) m, eta_sel (Reg 1 13) m), (eta_sel (Reg 1 14) m, eta_sel (Reg 1 15) m)) in let m1 = ((m0_3, m4_7), (m8_11, m12_15)) in (m0, m1)
{ "checked_file": "Vale.X64.Regs.fsti.checked", "dependencies": [ "Vale.X64.Machine_s.fst.checked", "Vale.Lib.Map16.fsti.checked", "Vale.Def.Prop_s.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.FunctionalExtensionality.fsti.checked" ], "interface_file": false, "source_file": "Vale.X64.Regs.fsti" }
[ "total" ]
[ "Vale.X64.Regs.t", "FStar.Pervasives.Native.Mktuple2", "Vale.Lib.Map16.map16", "Vale.X64.Machine_s.nat64", "Vale.X64.Machine_s.quad32", "FStar.Pervasives.Native.tuple2", "Vale.Lib.Map16.map8", "Vale.Def.Types_s.quad32", "Vale.Lib.Map16.map4", "Vale.Lib.Map16.map2", "Vale.X64.Regs.eta_sel", "Vale.X64.Machine_s.Reg", "Vale.Def.Words_s.nat64" ]
[]
module Vale.X64.Regs open FStar.Mul // This interface should not refer to Machine_Semantics_s open Vale.Def.Prop_s open Vale.X64.Machine_s open Vale.Lib.Map16 type regs_fun = FStar.FunctionalExtensionality.restricted_t reg t_reg type regs_def = map16 nat64 & map16 quad32 [@"opaque_to_smt"] type t = regs_def [@va_qattr "opaque_to_smt"] let sel (r:reg) (m:t) : t_reg r = match m with (m0, m1) -> match r with Reg rf i -> match rf with | 0 -> sel16 m0 i | 1 -> sel16 m1 i [@va_qattr "opaque_to_smt"] let upd (r:reg) (v:t_reg r) (m:t) : t = match m with (m0, m1) -> match r with Reg rf i -> match rf with | 0 -> (upd16 m0 i v, m1) | 1 -> (m0, upd16 m1 i v) // Used in eta-expansion; we ensure that it stops normalization by not marking it va_qattr [@"opaque_to_smt"] let eta_sel (r:reg) (m:t) : v:(t_reg r){v == sel r m} = sel r m // Eta-expand into a map where normalization gracefully terminates to eta_sel applications, // so that we don't accidentally normalize past type abstractions
false
true
Vale.X64.Regs.fsti
{ "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" }
null
val eta (m: t) : t
[]
Vale.X64.Regs.eta
{ "file_name": "vale/code/arch/x64/Vale.X64.Regs.fsti", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
m: Vale.X64.Regs.t -> Vale.X64.Regs.t
{ "end_col": 10, "end_line": 50, "start_col": 19, "start_line": 39 }
Prims.Tot
val shift_gf128_key_1 (h: poly) : poly
[ { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.AES.GF128", "short_module": null }, { "abbrev": false, "full_module": "Vale.AES.GF128_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2.Bits_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.AES", "short_module": null }, { "abbrev": false, "full_module": "Vale.AES", "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 } ]
false
let shift_gf128_key_1 (h:poly) : poly = shift_key_1 128 gf128_modulus_low_terms h
val shift_gf128_key_1 (h: poly) : poly let shift_gf128_key_1 (h: poly) : poly =
false
null
false
shift_key_1 128 gf128_modulus_low_terms h
{ "checked_file": "Vale.AES.OptPublic_BE.fst.checked", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Bits_s.fsti.checked", "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Def.Prop_s.fst.checked", "Vale.AES.GF128_s.fsti.checked", "Vale.AES.GF128.fsti.checked", "prims.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.AES.OptPublic_BE.fst" }
[ "total" ]
[ "Vale.Math.Poly2_s.poly", "Vale.AES.GF128.shift_key_1", "Vale.AES.GF128_s.gf128_modulus_low_terms" ]
[]
module Vale.AES.OptPublic_BE open FStar.Mul open FStar.Seq open Vale.Def.Types_s open Vale.Math.Poly2_s open Vale.Math.Poly2.Bits_s open Vale.AES.GF128_s open Vale.AES.GF128 open Vale.Def.Words_s
false
true
Vale.AES.OptPublic_BE.fst
{ "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" }
null
val shift_gf128_key_1 (h: poly) : poly
[]
Vale.AES.OptPublic_BE.shift_gf128_key_1
{ "file_name": "vale/code/crypto/aes/Vale.AES.OptPublic_BE.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
h: Vale.Math.Poly2_s.poly -> Vale.Math.Poly2_s.poly
{ "end_col": 43, "end_line": 13, "start_col": 2, "start_line": 13 }
Prims.Tot
val gf128_power (h: poly) (n: nat) : poly
[ { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.AES.GF128", "short_module": null }, { "abbrev": false, "full_module": "Vale.AES.GF128_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2.Bits_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.AES", "short_module": null }, { "abbrev": false, "full_module": "Vale.AES", "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 } ]
false
let gf128_power (h:poly) (n:nat) : poly = shift_gf128_key_1 (g_power h n)
val gf128_power (h: poly) (n: nat) : poly let gf128_power (h: poly) (n: nat) : poly =
false
null
false
shift_gf128_key_1 (g_power h n)
{ "checked_file": "Vale.AES.OptPublic_BE.fst.checked", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Bits_s.fsti.checked", "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Def.Prop_s.fst.checked", "Vale.AES.GF128_s.fsti.checked", "Vale.AES.GF128.fsti.checked", "prims.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.AES.OptPublic_BE.fst" }
[ "total" ]
[ "Vale.Math.Poly2_s.poly", "Prims.nat", "Vale.AES.OptPublic_BE.shift_gf128_key_1", "Vale.AES.OptPublic_BE.g_power" ]
[]
module Vale.AES.OptPublic_BE open FStar.Mul open FStar.Seq open Vale.Def.Types_s open Vale.Math.Poly2_s open Vale.Math.Poly2.Bits_s open Vale.AES.GF128_s open Vale.AES.GF128 open Vale.Def.Words_s let shift_gf128_key_1 (h:poly) : poly = shift_key_1 128 gf128_modulus_low_terms h let rec g_power (a:poly) (n:nat) : poly = if n = 0 then zero else // arbitrary value for n = 0 if n = 1 then a else a *~ g_power a (n - 1)
false
true
Vale.AES.OptPublic_BE.fst
{ "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" }
null
val gf128_power (h: poly) (n: nat) : poly
[]
Vale.AES.OptPublic_BE.gf128_power
{ "file_name": "vale/code/crypto/aes/Vale.AES.OptPublic_BE.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
h: Vale.Math.Poly2_s.poly -> n: Prims.nat -> Vale.Math.Poly2_s.poly
{ "end_col": 73, "end_line": 20, "start_col": 42, "start_line": 20 }
Prims.Tot
val g_power (a: poly) (n: nat) : poly
[ { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.AES.GF128", "short_module": null }, { "abbrev": false, "full_module": "Vale.AES.GF128_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2.Bits_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.AES", "short_module": null }, { "abbrev": false, "full_module": "Vale.AES", "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 } ]
false
let rec g_power (a:poly) (n:nat) : poly = if n = 0 then zero else // arbitrary value for n = 0 if n = 1 then a else a *~ g_power a (n - 1)
val g_power (a: poly) (n: nat) : poly let rec g_power (a: poly) (n: nat) : poly =
false
null
false
if n = 0 then zero else if n = 1 then a else a *~ g_power a (n - 1)
{ "checked_file": "Vale.AES.OptPublic_BE.fst.checked", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Bits_s.fsti.checked", "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Def.Prop_s.fst.checked", "Vale.AES.GF128_s.fsti.checked", "Vale.AES.GF128.fsti.checked", "prims.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.AES.OptPublic_BE.fst" }
[ "total" ]
[ "Vale.Math.Poly2_s.poly", "Prims.nat", "Prims.op_Equality", "Prims.int", "Vale.Math.Poly2_s.zero", "Prims.bool", "Vale.AES.GF128.op_Star_Tilde", "Vale.AES.OptPublic_BE.g_power", "Prims.op_Subtraction" ]
[]
module Vale.AES.OptPublic_BE open FStar.Mul open FStar.Seq open Vale.Def.Types_s open Vale.Math.Poly2_s open Vale.Math.Poly2.Bits_s open Vale.AES.GF128_s open Vale.AES.GF128 open Vale.Def.Words_s let shift_gf128_key_1 (h:poly) : poly = shift_key_1 128 gf128_modulus_low_terms h
false
true
Vale.AES.OptPublic_BE.fst
{ "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" }
null
val g_power (a: poly) (n: nat) : poly
[ "recursion" ]
Vale.AES.OptPublic_BE.g_power
{ "file_name": "vale/code/crypto/aes/Vale.AES.OptPublic_BE.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
a: Vale.Math.Poly2_s.poly -> n: Prims.nat -> Vale.Math.Poly2_s.poly
{ "end_col": 24, "end_line": 18, "start_col": 2, "start_line": 16 }
Prims.Tot
val hkeys_reqs_pub (hkeys:FStar.Seq.seq quad32) (h_BE:quad32) : Vale.Def.Prop_s.prop0
[ { "abbrev": false, "full_module": "Vale.Def.Words_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.AES.GF128", "short_module": null }, { "abbrev": false, "full_module": "Vale.AES.GF128_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2.Bits_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.Math.Poly2_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Seq", "short_module": null }, { "abbrev": false, "full_module": "Vale.Def.Types_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.AES", "short_module": null }, { "abbrev": false, "full_module": "Vale.AES", "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 } ]
false
let hkeys_reqs_pub (hkeys:seq quad32) (h_BE:quad32) : Vale.Def.Prop_s.prop0 = let h = of_quad32 h_BE in length hkeys >= 3 /\ of_quad32 (index hkeys 0) == gf128_power h 1 /\ of_quad32 (index hkeys 1) == gf128_power h 2 /\ index hkeys 2 == h_BE
val hkeys_reqs_pub (hkeys:FStar.Seq.seq quad32) (h_BE:quad32) : Vale.Def.Prop_s.prop0 let hkeys_reqs_pub (hkeys: seq quad32) (h_BE: quad32) : Vale.Def.Prop_s.prop0 =
false
null
false
let h = of_quad32 h_BE in length hkeys >= 3 /\ of_quad32 (index hkeys 0) == gf128_power h 1 /\ of_quad32 (index hkeys 1) == gf128_power h 2 /\ index hkeys 2 == h_BE
{ "checked_file": "Vale.AES.OptPublic_BE.fst.checked", "dependencies": [ "Vale.Math.Poly2_s.fsti.checked", "Vale.Math.Poly2.Bits_s.fsti.checked", "Vale.Def.Words_s.fsti.checked", "Vale.Def.Types_s.fst.checked", "Vale.Def.Prop_s.fst.checked", "Vale.AES.GF128_s.fsti.checked", "Vale.AES.GF128.fsti.checked", "prims.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked" ], "interface_file": true, "source_file": "Vale.AES.OptPublic_BE.fst" }
[ "total" ]
[ "FStar.Seq.Base.seq", "Vale.Def.Types_s.quad32", "Prims.l_and", "Prims.b2t", "Prims.op_GreaterThanOrEqual", "FStar.Seq.Base.length", "Prims.eq2", "Vale.Math.Poly2_s.poly", "Vale.Math.Poly2.Bits_s.of_quad32", "FStar.Seq.Base.index", "Vale.AES.OptPublic_BE.gf128_power", "Vale.Def.Prop_s.prop0" ]
[]
module Vale.AES.OptPublic_BE open FStar.Mul open FStar.Seq open Vale.Def.Types_s open Vale.Math.Poly2_s open Vale.Math.Poly2.Bits_s open Vale.AES.GF128_s open Vale.AES.GF128 open Vale.Def.Words_s let shift_gf128_key_1 (h:poly) : poly = shift_key_1 128 gf128_modulus_low_terms h let rec g_power (a:poly) (n:nat) : poly = if n = 0 then zero else // arbitrary value for n = 0 if n = 1 then a else a *~ g_power a (n - 1) let gf128_power (h:poly) (n:nat) : poly = shift_gf128_key_1 (g_power h n)
false
true
Vale.AES.OptPublic_BE.fst
{ "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" }
null
val hkeys_reqs_pub (hkeys:FStar.Seq.seq quad32) (h_BE:quad32) : Vale.Def.Prop_s.prop0
[]
Vale.AES.OptPublic_BE.hkeys_reqs_pub
{ "file_name": "vale/code/crypto/aes/Vale.AES.OptPublic_BE.fst", "git_rev": "12c5e9539c7e3c366c26409d3b86493548c4483e", "git_url": "https://github.com/hacl-star/hacl-star.git", "project_name": "hacl-star" }
hkeys: FStar.Seq.Base.seq Vale.Def.Types_s.quad32 -> h_BE: Vale.Def.Types_s.quad32 -> Vale.Def.Prop_s.prop0
{ "end_col": 23, "end_line": 28, "start_col": 3, "start_line": 23 }
Prims.Tot
[ { "abbrev": true, "full_module": "LowParse.Low.Base", "short_module": "LPL" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "EverParse3d", "short_module": null }, { "abbrev": false, "full_module": "EverParse3d", "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 } ]
false
let perm (#t: Type) (b: B.buffer t) = (x: perm0 t { perm_prop x b })
let perm (#t: Type) (b: B.buffer t) =
false
null
false
(x: perm0 t {perm_prop x b})
{ "checked_file": "EverParse3d.Readable.fsti.checked", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParse.Low.Base.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "EverParse3d.Readable.fsti" }
[ "total" ]
[ "LowStar.Buffer.buffer", "EverParse3d.Readable.perm0", "EverParse3d.Readable.perm_prop" ]
[]
module EverParse3d.Readable module B = LowStar.Buffer module HS = FStar.HyperStack module HST = FStar.HyperStack.ST module U32 = FStar.UInt32 module LPL = LowParse.Low.Base // not erasable because Low* buffers of erased elements are not erasable inline_for_extraction noextract val perm0 (t: Type) : Tot Type0 val perm_prop (#t: Type) (x: perm0 t) (b: B.buffer t) : Tot prop
false
false
EverParse3d.Readable.fsti
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 2, "max_fuel": 0, "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": [ "smt.qi.eager_threshold=100" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 8, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val perm : b: LowStar.Buffer.buffer t -> Type0
[]
EverParse3d.Readable.perm
{ "file_name": "src/3d/prelude/buffer/EverParse3d.Readable.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
b: LowStar.Buffer.buffer t -> Type0
{ "end_col": 68, "end_line": 16, "start_col": 38, "start_line": 16 }
FStar.Pervasives.Lemma
val readable_frame (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t{U32.v from <= U32.v to /\ U32.v to <= B.length b}) (l: B.loc) (h': HS.mem) : Lemma (requires (readable h p from to /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b)) (ensures (readable h' p from to)) [ SMTPatOr [ [SMTPat (B.modifies l h h'); SMTPat (readable h p from to)]; [SMTPat (B.modifies l h h'); SMTPat (readable h' p from to)] ] ]
[ { "abbrev": true, "full_module": "LowParse.Low.Base", "short_module": "LPL" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "EverParse3d", "short_module": null }, { "abbrev": false, "full_module": "EverParse3d", "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 } ]
false
let readable_frame (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (l: B.loc) (h' : HS.mem) : Lemma (requires ( readable h p from to /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b // because nothing prevents b from being deallocated )) (ensures ( readable h' p from to )) [SMTPatOr [ [ SMTPat (B.modifies l h h'); SMTPat (readable h p from to) ] ; [ SMTPat (B.modifies l h h'); SMTPat (readable h' p from to) ] ; ]] = valid_perm_frame h p l h' ; preserved_split p 0ul from (B.len b) h h' ; preserved_split p from to (B.len b) h h' ; readable_frame0 h p from to h'
val readable_frame (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t{U32.v from <= U32.v to /\ U32.v to <= B.length b}) (l: B.loc) (h': HS.mem) : Lemma (requires (readable h p from to /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b)) (ensures (readable h' p from to)) [ SMTPatOr [ [SMTPat (B.modifies l h h'); SMTPat (readable h p from to)]; [SMTPat (B.modifies l h h'); SMTPat (readable h' p from to)] ] ] let readable_frame (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t{U32.v from <= U32.v to /\ U32.v to <= B.length b}) (l: B.loc) (h': HS.mem) : Lemma (requires (readable h p from to /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b)) (ensures (readable h' p from to)) [ SMTPatOr [ [SMTPat (B.modifies l h h'); SMTPat (readable h p from to)]; [SMTPat (B.modifies l h h'); SMTPat (readable h' p from to)] ] ] =
false
null
true
valid_perm_frame h p l h'; preserved_split p 0ul from (B.len b) h h'; preserved_split p from to (B.len b) h h'; readable_frame0 h p from to h'
{ "checked_file": "EverParse3d.Readable.fsti.checked", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParse.Low.Base.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "EverParse3d.Readable.fsti" }
[ "lemma" ]
[ "FStar.Monotonic.HyperStack.mem", "LowStar.Buffer.buffer", "EverParse3d.Readable.perm", "FStar.UInt32.t", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "LowStar.Monotonic.Buffer.loc", "EverParse3d.Readable.readable_frame0", "Prims.unit", "EverParse3d.Readable.preserved_split", "LowStar.Monotonic.Buffer.len", "FStar.UInt32.__uint_to_t", "EverParse3d.Readable.valid_perm_frame", "EverParse3d.Readable.readable", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_disjoint", "EverParse3d.Readable.loc_perm", "LowStar.Monotonic.Buffer.live", "Prims.squash", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat_or", "Prims.list", "FStar.Pervasives.smt_pat", "Prims.Nil" ]
[]
module EverParse3d.Readable module B = LowStar.Buffer module HS = FStar.HyperStack module HST = FStar.HyperStack.ST module U32 = FStar.UInt32 module LPL = LowParse.Low.Base // not erasable because Low* buffers of erased elements are not erasable inline_for_extraction noextract val perm0 (t: Type) : Tot Type0 val perm_prop (#t: Type) (x: perm0 t) (b: B.buffer t) : Tot prop inline_for_extraction noextract let perm (#t: Type) (b: B.buffer t) = (x: perm0 t { perm_prop x b }) val loc_perm (#t: _) (#b: B.buffer t) (p: perm b) : GTot B.loc val loc_perm_prop (#t: _) (#b: B.buffer t) (p: perm b) : Lemma (B.address_liveness_insensitive_locs `B.loc_includes` loc_perm p) [SMTPat (loc_perm p)] val valid_perm (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) : GTot Type0 val valid_perm_prop (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) : Lemma (requires (valid_perm h p)) (ensures ( B.live h b /\ B.loc_buffer b `B.loc_disjoint` loc_perm p /\ loc_perm p `B.loc_in` h )) [SMTPat (valid_perm h p)] val preserved (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h h' : HS.mem) : Tot prop val valid_perm_frame (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (l: B.loc) (h' : HS.mem) : Lemma (requires ( valid_perm h p /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b // because nothing prevents b from being deallocated )) (ensures ( valid_perm h' p /\ preserved p 0ul (B.len b) h h' )) let valid_perm_frame_pat (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (l: B.loc) (h' : HS.mem) : Lemma (requires ( valid_perm h p /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b // because nothing prevents b from being deallocated )) (ensures ( valid_perm h' p /\ preserved p 0ul (B.len b) h h' )) [SMTPatOr [ [ SMTPat (B.modifies l h h'); SMTPat (valid_perm h p) ] ; [ SMTPat (B.modifies l h h'); SMTPat (valid_perm h' p) ] ; ]] = valid_perm_frame h p l h' val preserved_refl (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h: HS.mem) : Lemma (preserved p from to h h) val preserved_trans (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h1 h2 h3: HS.mem) : Lemma (requires ( preserved p from to h1 h2 /\ preserved p from to h2 h3 )) (ensures ( preserved p from to h1 h3 )) val preserved_split (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) (h h' : HS.mem) : Lemma (preserved p from to h h' <==> (preserved p from mid h h' /\ preserved p mid to h h')) val readable (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) : GTot Type0 val readable_prop (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires (readable h p from to)) (ensures (valid_perm h p)) [SMTPat (readable h p from to)] val readable_split (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (readable h p from to <==> (readable h p from mid /\ readable h p mid to)) let readable_split' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires (readable h p from to)) (ensures (readable h p from mid /\ readable h p mid to)) = readable_split h p from mid to let readable_merge' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires ( readable h p from mid /\ readable h p mid to )) (ensures (readable h p from to)) = readable_split h p from mid to val readable_frame0 (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h' : HS.mem) : Lemma (requires ( readable h p from to /\ preserved p from to h h' )) (ensures ( readable h' p from to )) let readable_frame (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (l: B.loc) (h' : HS.mem) : Lemma (requires ( readable h p from to /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b // because nothing prevents b from being deallocated )) (ensures ( readable h' p from to )) [SMTPatOr [ [ SMTPat (B.modifies l h h'); SMTPat (readable h p from to) ] ; [ SMTPat (B.modifies l h h'); SMTPat (readable h' p from to) ] ; ]]
false
false
EverParse3d.Readable.fsti
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 2, "max_fuel": 0, "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": [ "smt.qi.eager_threshold=100" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 8, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val readable_frame (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t{U32.v from <= U32.v to /\ U32.v to <= B.length b}) (l: B.loc) (h': HS.mem) : Lemma (requires (readable h p from to /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b)) (ensures (readable h' p from to)) [ SMTPatOr [ [SMTPat (B.modifies l h h'); SMTPat (readable h p from to)]; [SMTPat (B.modifies l h h'); SMTPat (readable h' p from to)] ] ]
[]
EverParse3d.Readable.readable_frame
{ "file_name": "src/3d/prelude/buffer/EverParse3d.Readable.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
h: FStar.Monotonic.HyperStack.mem -> p: EverParse3d.Readable.perm b -> from: FStar.UInt32.t -> to: FStar.UInt32.t { FStar.UInt32.v from <= FStar.UInt32.v to /\ FStar.UInt32.v to <= LowStar.Monotonic.Buffer.length b } -> l: LowStar.Monotonic.Buffer.loc -> h': FStar.Monotonic.HyperStack.mem -> FStar.Pervasives.Lemma (requires EverParse3d.Readable.readable h p from to /\ LowStar.Monotonic.Buffer.modifies l h h' /\ LowStar.Monotonic.Buffer.loc_disjoint (EverParse3d.Readable.loc_perm p) l /\ LowStar.Monotonic.Buffer.live h' b) (ensures EverParse3d.Readable.readable h' p from to) [ SMTPatOr [ [ SMTPat (LowStar.Monotonic.Buffer.modifies l h h'); SMTPat (EverParse3d.Readable.readable h p from to) ]; [ SMTPat (LowStar.Monotonic.Buffer.modifies l h h'); SMTPat (EverParse3d.Readable.readable h' p from to) ] ] ]
{ "end_col": 32, "end_line": 204, "start_col": 2, "start_line": 201 }
FStar.Pervasives.Lemma
val unreadable_split' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from mid: U32.t) (to: U32.t{U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b}) : Lemma (requires (unreadable h p from to)) (ensures (unreadable h p from mid /\ unreadable h p mid to))
[ { "abbrev": true, "full_module": "LowParse.Low.Base", "short_module": "LPL" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "EverParse3d", "short_module": null }, { "abbrev": false, "full_module": "EverParse3d", "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 } ]
false
let unreadable_split' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires (unreadable h p from to)) (ensures (unreadable h p from mid /\ unreadable h p mid to)) = unreadable_split h p from mid to
val unreadable_split' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from mid: U32.t) (to: U32.t{U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b}) : Lemma (requires (unreadable h p from to)) (ensures (unreadable h p from mid /\ unreadable h p mid to)) let unreadable_split' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from mid: U32.t) (to: U32.t{U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b}) : Lemma (requires (unreadable h p from to)) (ensures (unreadable h p from mid /\ unreadable h p mid to)) =
false
null
true
unreadable_split h p from mid to
{ "checked_file": "EverParse3d.Readable.fsti.checked", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParse.Low.Base.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "EverParse3d.Readable.fsti" }
[ "lemma" ]
[ "FStar.Monotonic.HyperStack.mem", "LowStar.Buffer.buffer", "EverParse3d.Readable.perm", "FStar.UInt32.t", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "EverParse3d.Readable.unreadable_split", "Prims.unit", "EverParse3d.Readable.unreadable", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module EverParse3d.Readable module B = LowStar.Buffer module HS = FStar.HyperStack module HST = FStar.HyperStack.ST module U32 = FStar.UInt32 module LPL = LowParse.Low.Base // not erasable because Low* buffers of erased elements are not erasable inline_for_extraction noextract val perm0 (t: Type) : Tot Type0 val perm_prop (#t: Type) (x: perm0 t) (b: B.buffer t) : Tot prop inline_for_extraction noextract let perm (#t: Type) (b: B.buffer t) = (x: perm0 t { perm_prop x b }) val loc_perm (#t: _) (#b: B.buffer t) (p: perm b) : GTot B.loc val loc_perm_prop (#t: _) (#b: B.buffer t) (p: perm b) : Lemma (B.address_liveness_insensitive_locs `B.loc_includes` loc_perm p) [SMTPat (loc_perm p)] val valid_perm (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) : GTot Type0 val valid_perm_prop (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) : Lemma (requires (valid_perm h p)) (ensures ( B.live h b /\ B.loc_buffer b `B.loc_disjoint` loc_perm p /\ loc_perm p `B.loc_in` h )) [SMTPat (valid_perm h p)] val preserved (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h h' : HS.mem) : Tot prop val valid_perm_frame (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (l: B.loc) (h' : HS.mem) : Lemma (requires ( valid_perm h p /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b // because nothing prevents b from being deallocated )) (ensures ( valid_perm h' p /\ preserved p 0ul (B.len b) h h' )) let valid_perm_frame_pat (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (l: B.loc) (h' : HS.mem) : Lemma (requires ( valid_perm h p /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b // because nothing prevents b from being deallocated )) (ensures ( valid_perm h' p /\ preserved p 0ul (B.len b) h h' )) [SMTPatOr [ [ SMTPat (B.modifies l h h'); SMTPat (valid_perm h p) ] ; [ SMTPat (B.modifies l h h'); SMTPat (valid_perm h' p) ] ; ]] = valid_perm_frame h p l h' val preserved_refl (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h: HS.mem) : Lemma (preserved p from to h h) val preserved_trans (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h1 h2 h3: HS.mem) : Lemma (requires ( preserved p from to h1 h2 /\ preserved p from to h2 h3 )) (ensures ( preserved p from to h1 h3 )) val preserved_split (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) (h h' : HS.mem) : Lemma (preserved p from to h h' <==> (preserved p from mid h h' /\ preserved p mid to h h')) val readable (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) : GTot Type0 val readable_prop (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires (readable h p from to)) (ensures (valid_perm h p)) [SMTPat (readable h p from to)] val readable_split (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (readable h p from to <==> (readable h p from mid /\ readable h p mid to)) let readable_split' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires (readable h p from to)) (ensures (readable h p from mid /\ readable h p mid to)) = readable_split h p from mid to let readable_merge' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires ( readable h p from mid /\ readable h p mid to )) (ensures (readable h p from to)) = readable_split h p from mid to val readable_frame0 (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h' : HS.mem) : Lemma (requires ( readable h p from to /\ preserved p from to h h' )) (ensures ( readable h' p from to )) let readable_frame (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (l: B.loc) (h' : HS.mem) : Lemma (requires ( readable h p from to /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b // because nothing prevents b from being deallocated )) (ensures ( readable h' p from to )) [SMTPatOr [ [ SMTPat (B.modifies l h h'); SMTPat (readable h p from to) ] ; [ SMTPat (B.modifies l h h'); SMTPat (readable h' p from to) ] ; ]] = valid_perm_frame h p l h' ; preserved_split p 0ul from (B.len b) h h' ; preserved_split p from to (B.len b) h h' ; readable_frame0 h p from to h' val unreadable (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) : GTot Type0 val unreadable_prop (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires (~ (valid_perm h p))) (ensures (unreadable h p from to)) [SMTPat (readable h p from to)] val readable_not_unreadable (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from < (* important: not equal *) U32.v to /\ U32.v to <= B.length b }) : Lemma (~ (readable h p from to /\ unreadable h p from to)) val unreadable_split (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (unreadable h p from to <==> (unreadable h p from mid /\ unreadable h p mid to)) let unreadable_split' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires (unreadable h p from to))
false
false
EverParse3d.Readable.fsti
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 2, "max_fuel": 0, "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": [ "smt.qi.eager_threshold=100" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 8, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val unreadable_split' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from mid: U32.t) (to: U32.t{U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b}) : Lemma (requires (unreadable h p from to)) (ensures (unreadable h p from mid /\ unreadable h p mid to))
[]
EverParse3d.Readable.unreadable_split'
{ "file_name": "src/3d/prelude/buffer/EverParse3d.Readable.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
h: FStar.Monotonic.HyperStack.mem -> p: EverParse3d.Readable.perm b -> from: FStar.UInt32.t -> mid: FStar.UInt32.t -> to: FStar.UInt32.t { FStar.UInt32.v from <= FStar.UInt32.v mid /\ FStar.UInt32.v mid <= FStar.UInt32.v to /\ FStar.UInt32.v to <= LowStar.Monotonic.Buffer.length b } -> FStar.Pervasives.Lemma (requires EverParse3d.Readable.unreadable h p from to) (ensures EverParse3d.Readable.unreadable h p from mid /\ EverParse3d.Readable.unreadable h p mid to)
{ "end_col": 34, "end_line": 249, "start_col": 2, "start_line": 249 }
FStar.Pervasives.Lemma
val readable_split' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from mid: U32.t) (to: U32.t{U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b}) : Lemma (requires (readable h p from to)) (ensures (readable h p from mid /\ readable h p mid to))
[ { "abbrev": true, "full_module": "LowParse.Low.Base", "short_module": "LPL" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "EverParse3d", "short_module": null }, { "abbrev": false, "full_module": "EverParse3d", "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 } ]
false
let readable_split' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires (readable h p from to)) (ensures (readable h p from mid /\ readable h p mid to)) = readable_split h p from mid to
val readable_split' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from mid: U32.t) (to: U32.t{U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b}) : Lemma (requires (readable h p from to)) (ensures (readable h p from mid /\ readable h p mid to)) let readable_split' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from mid: U32.t) (to: U32.t{U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b}) : Lemma (requires (readable h p from to)) (ensures (readable h p from mid /\ readable h p mid to)) =
false
null
true
readable_split h p from mid to
{ "checked_file": "EverParse3d.Readable.fsti.checked", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParse.Low.Base.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "EverParse3d.Readable.fsti" }
[ "lemma" ]
[ "FStar.Monotonic.HyperStack.mem", "LowStar.Buffer.buffer", "EverParse3d.Readable.perm", "FStar.UInt32.t", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "EverParse3d.Readable.readable_split", "Prims.unit", "EverParse3d.Readable.readable", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module EverParse3d.Readable module B = LowStar.Buffer module HS = FStar.HyperStack module HST = FStar.HyperStack.ST module U32 = FStar.UInt32 module LPL = LowParse.Low.Base // not erasable because Low* buffers of erased elements are not erasable inline_for_extraction noextract val perm0 (t: Type) : Tot Type0 val perm_prop (#t: Type) (x: perm0 t) (b: B.buffer t) : Tot prop inline_for_extraction noextract let perm (#t: Type) (b: B.buffer t) = (x: perm0 t { perm_prop x b }) val loc_perm (#t: _) (#b: B.buffer t) (p: perm b) : GTot B.loc val loc_perm_prop (#t: _) (#b: B.buffer t) (p: perm b) : Lemma (B.address_liveness_insensitive_locs `B.loc_includes` loc_perm p) [SMTPat (loc_perm p)] val valid_perm (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) : GTot Type0 val valid_perm_prop (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) : Lemma (requires (valid_perm h p)) (ensures ( B.live h b /\ B.loc_buffer b `B.loc_disjoint` loc_perm p /\ loc_perm p `B.loc_in` h )) [SMTPat (valid_perm h p)] val preserved (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h h' : HS.mem) : Tot prop val valid_perm_frame (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (l: B.loc) (h' : HS.mem) : Lemma (requires ( valid_perm h p /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b // because nothing prevents b from being deallocated )) (ensures ( valid_perm h' p /\ preserved p 0ul (B.len b) h h' )) let valid_perm_frame_pat (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (l: B.loc) (h' : HS.mem) : Lemma (requires ( valid_perm h p /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b // because nothing prevents b from being deallocated )) (ensures ( valid_perm h' p /\ preserved p 0ul (B.len b) h h' )) [SMTPatOr [ [ SMTPat (B.modifies l h h'); SMTPat (valid_perm h p) ] ; [ SMTPat (B.modifies l h h'); SMTPat (valid_perm h' p) ] ; ]] = valid_perm_frame h p l h' val preserved_refl (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h: HS.mem) : Lemma (preserved p from to h h) val preserved_trans (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h1 h2 h3: HS.mem) : Lemma (requires ( preserved p from to h1 h2 /\ preserved p from to h2 h3 )) (ensures ( preserved p from to h1 h3 )) val preserved_split (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) (h h' : HS.mem) : Lemma (preserved p from to h h' <==> (preserved p from mid h h' /\ preserved p mid to h h')) val readable (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) : GTot Type0 val readable_prop (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires (readable h p from to)) (ensures (valid_perm h p)) [SMTPat (readable h p from to)] val readable_split (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (readable h p from to <==> (readable h p from mid /\ readable h p mid to)) let readable_split' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires (readable h p from to))
false
false
EverParse3d.Readable.fsti
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 2, "max_fuel": 0, "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": [ "smt.qi.eager_threshold=100" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 8, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val readable_split' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from mid: U32.t) (to: U32.t{U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b}) : Lemma (requires (readable h p from to)) (ensures (readable h p from mid /\ readable h p mid to))
[]
EverParse3d.Readable.readable_split'
{ "file_name": "src/3d/prelude/buffer/EverParse3d.Readable.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
h: FStar.Monotonic.HyperStack.mem -> p: EverParse3d.Readable.perm b -> from: FStar.UInt32.t -> mid: FStar.UInt32.t -> to: FStar.UInt32.t { FStar.UInt32.v from <= FStar.UInt32.v mid /\ FStar.UInt32.v mid <= FStar.UInt32.v to /\ FStar.UInt32.v to <= LowStar.Monotonic.Buffer.length b } -> FStar.Pervasives.Lemma (requires EverParse3d.Readable.readable h p from to) (ensures EverParse3d.Readable.readable h p from mid /\ EverParse3d.Readable.readable h p mid to)
{ "end_col": 32, "end_line": 148, "start_col": 2, "start_line": 148 }
FStar.Pervasives.Lemma
val readable_merge' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from mid: U32.t) (to: U32.t{U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b}) : Lemma (requires (readable h p from mid /\ readable h p mid to)) (ensures (readable h p from to))
[ { "abbrev": true, "full_module": "LowParse.Low.Base", "short_module": "LPL" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "EverParse3d", "short_module": null }, { "abbrev": false, "full_module": "EverParse3d", "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 } ]
false
let readable_merge' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires ( readable h p from mid /\ readable h p mid to )) (ensures (readable h p from to)) = readable_split h p from mid to
val readable_merge' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from mid: U32.t) (to: U32.t{U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b}) : Lemma (requires (readable h p from mid /\ readable h p mid to)) (ensures (readable h p from to)) let readable_merge' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from mid: U32.t) (to: U32.t{U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b}) : Lemma (requires (readable h p from mid /\ readable h p mid to)) (ensures (readable h p from to)) =
false
null
true
readable_split h p from mid to
{ "checked_file": "EverParse3d.Readable.fsti.checked", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParse.Low.Base.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "EverParse3d.Readable.fsti" }
[ "lemma" ]
[ "FStar.Monotonic.HyperStack.mem", "LowStar.Buffer.buffer", "EverParse3d.Readable.perm", "FStar.UInt32.t", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "EverParse3d.Readable.readable_split", "Prims.unit", "EverParse3d.Readable.readable", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module EverParse3d.Readable module B = LowStar.Buffer module HS = FStar.HyperStack module HST = FStar.HyperStack.ST module U32 = FStar.UInt32 module LPL = LowParse.Low.Base // not erasable because Low* buffers of erased elements are not erasable inline_for_extraction noextract val perm0 (t: Type) : Tot Type0 val perm_prop (#t: Type) (x: perm0 t) (b: B.buffer t) : Tot prop inline_for_extraction noextract let perm (#t: Type) (b: B.buffer t) = (x: perm0 t { perm_prop x b }) val loc_perm (#t: _) (#b: B.buffer t) (p: perm b) : GTot B.loc val loc_perm_prop (#t: _) (#b: B.buffer t) (p: perm b) : Lemma (B.address_liveness_insensitive_locs `B.loc_includes` loc_perm p) [SMTPat (loc_perm p)] val valid_perm (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) : GTot Type0 val valid_perm_prop (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) : Lemma (requires (valid_perm h p)) (ensures ( B.live h b /\ B.loc_buffer b `B.loc_disjoint` loc_perm p /\ loc_perm p `B.loc_in` h )) [SMTPat (valid_perm h p)] val preserved (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h h' : HS.mem) : Tot prop val valid_perm_frame (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (l: B.loc) (h' : HS.mem) : Lemma (requires ( valid_perm h p /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b // because nothing prevents b from being deallocated )) (ensures ( valid_perm h' p /\ preserved p 0ul (B.len b) h h' )) let valid_perm_frame_pat (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (l: B.loc) (h' : HS.mem) : Lemma (requires ( valid_perm h p /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b // because nothing prevents b from being deallocated )) (ensures ( valid_perm h' p /\ preserved p 0ul (B.len b) h h' )) [SMTPatOr [ [ SMTPat (B.modifies l h h'); SMTPat (valid_perm h p) ] ; [ SMTPat (B.modifies l h h'); SMTPat (valid_perm h' p) ] ; ]] = valid_perm_frame h p l h' val preserved_refl (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h: HS.mem) : Lemma (preserved p from to h h) val preserved_trans (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h1 h2 h3: HS.mem) : Lemma (requires ( preserved p from to h1 h2 /\ preserved p from to h2 h3 )) (ensures ( preserved p from to h1 h3 )) val preserved_split (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) (h h' : HS.mem) : Lemma (preserved p from to h h' <==> (preserved p from mid h h' /\ preserved p mid to h h')) val readable (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) : GTot Type0 val readable_prop (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires (readable h p from to)) (ensures (valid_perm h p)) [SMTPat (readable h p from to)] val readable_split (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (readable h p from to <==> (readable h p from mid /\ readable h p mid to)) let readable_split' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires (readable h p from to)) (ensures (readable h p from mid /\ readable h p mid to)) = readable_split h p from mid to let readable_merge' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires ( readable h p from mid /\ readable h p mid to ))
false
false
EverParse3d.Readable.fsti
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 2, "max_fuel": 0, "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": [ "smt.qi.eager_threshold=100" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 8, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val readable_merge' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from mid: U32.t) (to: U32.t{U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b}) : Lemma (requires (readable h p from mid /\ readable h p mid to)) (ensures (readable h p from to))
[]
EverParse3d.Readable.readable_merge'
{ "file_name": "src/3d/prelude/buffer/EverParse3d.Readable.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
h: FStar.Monotonic.HyperStack.mem -> p: EverParse3d.Readable.perm b -> from: FStar.UInt32.t -> mid: FStar.UInt32.t -> to: FStar.UInt32.t { FStar.UInt32.v from <= FStar.UInt32.v mid /\ FStar.UInt32.v mid <= FStar.UInt32.v to /\ FStar.UInt32.v to <= LowStar.Monotonic.Buffer.length b } -> FStar.Pervasives.Lemma (requires EverParse3d.Readable.readable h p from mid /\ EverParse3d.Readable.readable h p mid to) (ensures EverParse3d.Readable.readable h p from to)
{ "end_col": 32, "end_line": 162, "start_col": 2, "start_line": 162 }
FStar.Pervasives.Lemma
val valid_perm_frame_pat (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (l: B.loc) (h': HS.mem) : Lemma (requires (valid_perm h p /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b)) (ensures (valid_perm h' p /\ preserved p 0ul (B.len b) h h')) [ SMTPatOr [ [SMTPat (B.modifies l h h'); SMTPat (valid_perm h p)]; [SMTPat (B.modifies l h h'); SMTPat (valid_perm h' p)] ] ]
[ { "abbrev": true, "full_module": "LowParse.Low.Base", "short_module": "LPL" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "EverParse3d", "short_module": null }, { "abbrev": false, "full_module": "EverParse3d", "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 } ]
false
let valid_perm_frame_pat (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (l: B.loc) (h' : HS.mem) : Lemma (requires ( valid_perm h p /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b // because nothing prevents b from being deallocated )) (ensures ( valid_perm h' p /\ preserved p 0ul (B.len b) h h' )) [SMTPatOr [ [ SMTPat (B.modifies l h h'); SMTPat (valid_perm h p) ] ; [ SMTPat (B.modifies l h h'); SMTPat (valid_perm h' p) ] ; ]] = valid_perm_frame h p l h'
val valid_perm_frame_pat (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (l: B.loc) (h': HS.mem) : Lemma (requires (valid_perm h p /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b)) (ensures (valid_perm h' p /\ preserved p 0ul (B.len b) h h')) [ SMTPatOr [ [SMTPat (B.modifies l h h'); SMTPat (valid_perm h p)]; [SMTPat (B.modifies l h h'); SMTPat (valid_perm h' p)] ] ] let valid_perm_frame_pat (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (l: B.loc) (h': HS.mem) : Lemma (requires (valid_perm h p /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b)) (ensures (valid_perm h' p /\ preserved p 0ul (B.len b) h h')) [ SMTPatOr [ [SMTPat (B.modifies l h h'); SMTPat (valid_perm h p)]; [SMTPat (B.modifies l h h'); SMTPat (valid_perm h' p)] ] ] =
false
null
true
valid_perm_frame h p l h'
{ "checked_file": "EverParse3d.Readable.fsti.checked", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParse.Low.Base.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "EverParse3d.Readable.fsti" }
[ "lemma" ]
[ "FStar.Monotonic.HyperStack.mem", "LowStar.Buffer.buffer", "EverParse3d.Readable.perm", "LowStar.Monotonic.Buffer.loc", "EverParse3d.Readable.valid_perm_frame", "Prims.unit", "Prims.l_and", "EverParse3d.Readable.valid_perm", "LowStar.Monotonic.Buffer.modifies", "LowStar.Monotonic.Buffer.loc_disjoint", "EverParse3d.Readable.loc_perm", "LowStar.Monotonic.Buffer.live", "LowStar.Buffer.trivial_preorder", "Prims.squash", "EverParse3d.Readable.preserved", "FStar.UInt32.__uint_to_t", "LowStar.Monotonic.Buffer.len", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat_or", "Prims.list", "FStar.Pervasives.smt_pat", "Prims.Nil" ]
[]
module EverParse3d.Readable module B = LowStar.Buffer module HS = FStar.HyperStack module HST = FStar.HyperStack.ST module U32 = FStar.UInt32 module LPL = LowParse.Low.Base // not erasable because Low* buffers of erased elements are not erasable inline_for_extraction noextract val perm0 (t: Type) : Tot Type0 val perm_prop (#t: Type) (x: perm0 t) (b: B.buffer t) : Tot prop inline_for_extraction noextract let perm (#t: Type) (b: B.buffer t) = (x: perm0 t { perm_prop x b }) val loc_perm (#t: _) (#b: B.buffer t) (p: perm b) : GTot B.loc val loc_perm_prop (#t: _) (#b: B.buffer t) (p: perm b) : Lemma (B.address_liveness_insensitive_locs `B.loc_includes` loc_perm p) [SMTPat (loc_perm p)] val valid_perm (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) : GTot Type0 val valid_perm_prop (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) : Lemma (requires (valid_perm h p)) (ensures ( B.live h b /\ B.loc_buffer b `B.loc_disjoint` loc_perm p /\ loc_perm p `B.loc_in` h )) [SMTPat (valid_perm h p)] val preserved (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h h' : HS.mem) : Tot prop val valid_perm_frame (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (l: B.loc) (h' : HS.mem) : Lemma (requires ( valid_perm h p /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b // because nothing prevents b from being deallocated )) (ensures ( valid_perm h' p /\ preserved p 0ul (B.len b) h h' )) let valid_perm_frame_pat (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (l: B.loc) (h' : HS.mem) : Lemma (requires ( valid_perm h p /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b // because nothing prevents b from being deallocated )) (ensures ( valid_perm h' p /\ preserved p 0ul (B.len b) h h' )) [SMTPatOr [ [ SMTPat (B.modifies l h h'); SMTPat (valid_perm h p) ] ; [ SMTPat (B.modifies l h h'); SMTPat (valid_perm h' p) ] ;
false
false
EverParse3d.Readable.fsti
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 2, "max_fuel": 0, "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": [ "smt.qi.eager_threshold=100" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 8, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val valid_perm_frame_pat (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (l: B.loc) (h': HS.mem) : Lemma (requires (valid_perm h p /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b)) (ensures (valid_perm h' p /\ preserved p 0ul (B.len b) h h')) [ SMTPatOr [ [SMTPat (B.modifies l h h'); SMTPat (valid_perm h p)]; [SMTPat (B.modifies l h h'); SMTPat (valid_perm h' p)] ] ]
[]
EverParse3d.Readable.valid_perm_frame_pat
{ "file_name": "src/3d/prelude/buffer/EverParse3d.Readable.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
h: FStar.Monotonic.HyperStack.mem -> p: EverParse3d.Readable.perm b -> l: LowStar.Monotonic.Buffer.loc -> h': FStar.Monotonic.HyperStack.mem -> FStar.Pervasives.Lemma (requires EverParse3d.Readable.valid_perm h p /\ LowStar.Monotonic.Buffer.modifies l h h' /\ LowStar.Monotonic.Buffer.loc_disjoint (EverParse3d.Readable.loc_perm p) l /\ LowStar.Monotonic.Buffer.live h' b) (ensures EverParse3d.Readable.valid_perm h' p /\ EverParse3d.Readable.preserved p 0ul (LowStar.Monotonic.Buffer.len b) h h') [ SMTPatOr [ [ SMTPat (LowStar.Monotonic.Buffer.modifies l h h'); SMTPat (EverParse3d.Readable.valid_perm h p) ]; [ SMTPat (LowStar.Monotonic.Buffer.modifies l h h'); SMTPat (EverParse3d.Readable.valid_perm h' p) ] ] ]
{ "end_col": 27, "end_line": 80, "start_col": 2, "start_line": 80 }
FStar.Pervasives.Lemma
val unreadable_merge' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from mid: U32.t) (to: U32.t{U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b}) : Lemma (requires (unreadable h p from mid /\ unreadable h p mid to)) (ensures (unreadable h p from to))
[ { "abbrev": true, "full_module": "LowParse.Low.Base", "short_module": "LPL" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": false, "full_module": "EverParse3d", "short_module": null }, { "abbrev": false, "full_module": "EverParse3d", "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 } ]
false
let unreadable_merge' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires ( unreadable h p from mid /\ unreadable h p mid to )) (ensures (unreadable h p from to)) = unreadable_split h p from mid to
val unreadable_merge' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from mid: U32.t) (to: U32.t{U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b}) : Lemma (requires (unreadable h p from mid /\ unreadable h p mid to)) (ensures (unreadable h p from to)) let unreadable_merge' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from mid: U32.t) (to: U32.t{U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b}) : Lemma (requires (unreadable h p from mid /\ unreadable h p mid to)) (ensures (unreadable h p from to)) =
false
null
true
unreadable_split h p from mid to
{ "checked_file": "EverParse3d.Readable.fsti.checked", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParse.Low.Base.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "EverParse3d.Readable.fsti" }
[ "lemma" ]
[ "FStar.Monotonic.HyperStack.mem", "LowStar.Buffer.buffer", "EverParse3d.Readable.perm", "FStar.UInt32.t", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.length", "LowStar.Buffer.trivial_preorder", "EverParse3d.Readable.unreadable_split", "Prims.unit", "EverParse3d.Readable.unreadable", "Prims.squash", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
module EverParse3d.Readable module B = LowStar.Buffer module HS = FStar.HyperStack module HST = FStar.HyperStack.ST module U32 = FStar.UInt32 module LPL = LowParse.Low.Base // not erasable because Low* buffers of erased elements are not erasable inline_for_extraction noextract val perm0 (t: Type) : Tot Type0 val perm_prop (#t: Type) (x: perm0 t) (b: B.buffer t) : Tot prop inline_for_extraction noextract let perm (#t: Type) (b: B.buffer t) = (x: perm0 t { perm_prop x b }) val loc_perm (#t: _) (#b: B.buffer t) (p: perm b) : GTot B.loc val loc_perm_prop (#t: _) (#b: B.buffer t) (p: perm b) : Lemma (B.address_liveness_insensitive_locs `B.loc_includes` loc_perm p) [SMTPat (loc_perm p)] val valid_perm (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) : GTot Type0 val valid_perm_prop (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) : Lemma (requires (valid_perm h p)) (ensures ( B.live h b /\ B.loc_buffer b `B.loc_disjoint` loc_perm p /\ loc_perm p `B.loc_in` h )) [SMTPat (valid_perm h p)] val preserved (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h h' : HS.mem) : Tot prop val valid_perm_frame (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (l: B.loc) (h' : HS.mem) : Lemma (requires ( valid_perm h p /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b // because nothing prevents b from being deallocated )) (ensures ( valid_perm h' p /\ preserved p 0ul (B.len b) h h' )) let valid_perm_frame_pat (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (l: B.loc) (h' : HS.mem) : Lemma (requires ( valid_perm h p /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b // because nothing prevents b from being deallocated )) (ensures ( valid_perm h' p /\ preserved p 0ul (B.len b) h h' )) [SMTPatOr [ [ SMTPat (B.modifies l h h'); SMTPat (valid_perm h p) ] ; [ SMTPat (B.modifies l h h'); SMTPat (valid_perm h' p) ] ; ]] = valid_perm_frame h p l h' val preserved_refl (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h: HS.mem) : Lemma (preserved p from to h h) val preserved_trans (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h1 h2 h3: HS.mem) : Lemma (requires ( preserved p from to h1 h2 /\ preserved p from to h2 h3 )) (ensures ( preserved p from to h1 h3 )) val preserved_split (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) (h h' : HS.mem) : Lemma (preserved p from to h h' <==> (preserved p from mid h h' /\ preserved p mid to h h')) val readable (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) : GTot Type0 val readable_prop (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires (readable h p from to)) (ensures (valid_perm h p)) [SMTPat (readable h p from to)] val readable_split (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (readable h p from to <==> (readable h p from mid /\ readable h p mid to)) let readable_split' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires (readable h p from to)) (ensures (readable h p from mid /\ readable h p mid to)) = readable_split h p from mid to let readable_merge' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires ( readable h p from mid /\ readable h p mid to )) (ensures (readable h p from to)) = readable_split h p from mid to val readable_frame0 (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (h' : HS.mem) : Lemma (requires ( readable h p from to /\ preserved p from to h h' )) (ensures ( readable h' p from to )) let readable_frame (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) (l: B.loc) (h' : HS.mem) : Lemma (requires ( readable h p from to /\ B.modifies l h h' /\ B.loc_disjoint (loc_perm p) l /\ B.live h' b // because nothing prevents b from being deallocated )) (ensures ( readable h' p from to )) [SMTPatOr [ [ SMTPat (B.modifies l h h'); SMTPat (readable h p from to) ] ; [ SMTPat (B.modifies l h h'); SMTPat (readable h' p from to) ] ; ]] = valid_perm_frame h p l h' ; preserved_split p 0ul from (B.len b) h h' ; preserved_split p from to (B.len b) h h' ; readable_frame0 h p from to h' val unreadable (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) : GTot Type0 val unreadable_prop (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires (~ (valid_perm h p))) (ensures (unreadable h p from to)) [SMTPat (readable h p from to)] val readable_not_unreadable (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (to: U32.t { U32.v from < (* important: not equal *) U32.v to /\ U32.v to <= B.length b }) : Lemma (~ (readable h p from to /\ unreadable h p from to)) val unreadable_split (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (unreadable h p from to <==> (unreadable h p from mid /\ unreadable h p mid to)) let unreadable_split' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires (unreadable h p from to)) (ensures (unreadable h p from mid /\ unreadable h p mid to)) = unreadable_split h p from mid to let unreadable_merge' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from: U32.t) (mid: U32.t) (to: U32.t { U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b }) : Lemma (requires ( unreadable h p from mid /\ unreadable h p mid to ))
false
false
EverParse3d.Readable.fsti
{ "detail_errors": false, "detail_hint_replay": false, "initial_fuel": 2, "initial_ifuel": 2, "max_fuel": 0, "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": [ "smt.qi.eager_threshold=100" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 8, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
null
val unreadable_merge' (h: HS.mem) (#t: _) (#b: B.buffer t) (p: perm b) (from mid: U32.t) (to: U32.t{U32.v from <= U32.v mid /\ U32.v mid <= U32.v to /\ U32.v to <= B.length b}) : Lemma (requires (unreadable h p from mid /\ unreadable h p mid to)) (ensures (unreadable h p from to))
[]
EverParse3d.Readable.unreadable_merge'
{ "file_name": "src/3d/prelude/buffer/EverParse3d.Readable.fsti", "git_rev": "446a08ce38df905547cf20f28c43776b22b8087a", "git_url": "https://github.com/project-everest/everparse.git", "project_name": "everparse" }
h: FStar.Monotonic.HyperStack.mem -> p: EverParse3d.Readable.perm b -> from: FStar.UInt32.t -> mid: FStar.UInt32.t -> to: FStar.UInt32.t { FStar.UInt32.v from <= FStar.UInt32.v mid /\ FStar.UInt32.v mid <= FStar.UInt32.v to /\ FStar.UInt32.v to <= LowStar.Monotonic.Buffer.length b } -> FStar.Pervasives.Lemma (requires EverParse3d.Readable.unreadable h p from mid /\ EverParse3d.Readable.unreadable h p mid to) (ensures EverParse3d.Readable.unreadable h p from to)
{ "end_col": 34, "end_line": 263, "start_col": 2, "start_line": 263 }