Skip to content

Commit 1fda06e

Browse files
authored
Merge pull request #37 from WasmCert/inst_typing_compute
Computable version of inst_typing, and some associated fixes for cl_typing
2 parents 7435b97 + 5085c39 commit 1fda06e

9 files changed

+302
-58
lines changed

theories/context_inference.v

+214
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
From mathcomp Require Import ssreflect ssrfun ssrnat ssrbool eqtype seq.
2+
From compcert Require lib.Floats.
3+
From Wasm Require Export common typing operations.
4+
From Coq Require Import BinNat.
5+
6+
Set Implicit Arguments.
7+
Unset Strict Implicit.
8+
Unset Printing Implicit Defensive.
9+
10+
Section Context_inference.
11+
12+
Variable host_function : eqType.
13+
14+
Let function_closure := function_closure host_function.
15+
Let store_record := store_record host_function.
16+
17+
Definition func_typing_inf (fs: list function_closure) (n: nat) : option function_type :=
18+
option_map cl_type (List.nth_error fs n).
19+
20+
Definition funcs_typing_inf (s: store_record) (inst: instance) : option (list function_type) :=
21+
those (map (fun i => func_typing_inf s.(s_funcs) i) inst.(inst_funcs)).
22+
23+
(* Choosing the most lenient bound *)
24+
Definition tab_typing_inf (ts: list tableinst) (n: nat) : option table_type :=
25+
match List.nth_error ts n with
26+
| Some _ => Some (Build_table_type (Build_limits 0%N None) ELT_funcref)
27+
| _ => None
28+
end.
29+
30+
Definition tabs_typing_inf (s: store_record) (inst: instance) : option (list table_type) :=
31+
those (map (fun i => tab_typing_inf s.(s_tables) i) inst.(inst_tab)).
32+
33+
(* Choosing the most lenient bound *)
34+
Definition mem_typing_inf (ms: list memory) (n: nat) : option memory_type :=
35+
match List.nth_error ms n with
36+
| Some _ => Some (Build_limits 0%N None)
37+
| _ => None
38+
end.
39+
40+
Definition mems_typing_inf (s: store_record) (inst: instance) : option (list memory_type) :=
41+
those (map (fun i => mem_typing_inf s.(s_mems) i) inst.(inst_memory)).
42+
43+
Definition global_typing_inf (gs: list global) (n: nat) : option global_type :=
44+
match List.nth_error gs n with
45+
| Some g => Some (Build_global_type g.(g_mut) (typeof g.(g_val)))
46+
| _ => None
47+
end.
48+
49+
Definition globals_typing_inf (s: store_record) (inst: instance) : option (list global_type) :=
50+
those (map (fun i => global_typing_inf s.(s_globals) i) inst.(inst_globs)).
51+
52+
Definition inst_typing_inf (s: store_record) (inst: instance) : option t_context :=
53+
match funcs_typing_inf s inst with
54+
| Some fts =>
55+
match tabs_typing_inf s inst with
56+
| Some tts =>
57+
match mems_typing_inf s inst with
58+
| Some mts =>
59+
match globals_typing_inf s inst with
60+
| Some gts =>
61+
Some (Build_t_context inst.(inst_types) fts gts tts mts nil nil None)
62+
| _ => None
63+
end
64+
| _ => None
65+
end
66+
| _ => None
67+
end
68+
| _ => None
69+
end.
70+
71+
Definition frame_typing_inf (s: store_record) (f: frame) : option t_context :=
72+
match inst_typing_inf s f.(f_inst) with
73+
| Some C => Some (upd_local C ((map typeof f.(f_locs)) ++ tc_local C))
74+
| None => None
75+
end.
76+
77+
Lemma func_typing_inf_agree: forall xs n x,
78+
(func_typing_inf xs n == Some x) =
79+
functions_agree xs n x.
80+
Proof.
81+
move => xs n x.
82+
unfold func_typing_inf, functions_agree.
83+
destruct (List.nth_error xs n) as [ x' | ] eqn:Hnth; rewrite Hnth => /=.
84+
- assert (n < length xs)%coq_nat as Hlen; first by apply List.nth_error_Some; rewrite Hnth.
85+
move/ltP in Hlen.
86+
by rewrite Hlen.
87+
- cbn.
88+
by lias.
89+
Qed.
90+
91+
Lemma tab_typing_inf_agree: forall xs n x,
92+
tab_typing_inf xs n = Some x ->
93+
tabi_agree xs n x.
94+
Proof.
95+
move => xs n x.
96+
unfold tab_typing_inf, tabi_agree, tab_typing, limit_match.
97+
destruct (List.nth_error xs n) as [ x' | ] eqn:Hnth => //=.
98+
assert (n < length xs)%coq_nat as Hlen; first by apply List.nth_error_Some; rewrite Hnth.
99+
move => Hinf.
100+
move/ltP in Hlen.
101+
rewrite Hlen => /=.
102+
injection Hinf as <-.
103+
cbn.
104+
rewrite Bool.andb_true_r.
105+
apply/N.leb_spec0.
106+
by lias.
107+
Qed.
108+
109+
Lemma mem_typing_inf_agree: forall xs n x,
110+
mem_typing_inf xs n = Some x ->
111+
memi_agree xs n x.
112+
Proof.
113+
move => xs n x.
114+
unfold mem_typing_inf, memi_agree, mem_typing, limit_match.
115+
destruct (List.nth_error xs n) as [ x' | ] eqn:Hnth => //=.
116+
assert (n < length xs)%coq_nat as Hlen; first by apply List.nth_error_Some; rewrite Hnth.
117+
move => Hinf.
118+
move/ltP in Hlen.
119+
rewrite Hlen => /=.
120+
injection Hinf as <-.
121+
cbn.
122+
rewrite Bool.andb_true_r.
123+
apply/N.leb_spec0.
124+
by lias.
125+
Qed.
126+
127+
Lemma global_typing_inf_agree: forall xs n x,
128+
(global_typing_inf xs n = Some x) ->
129+
globals_agree xs n x.
130+
Proof.
131+
move => xs n x.
132+
unfold global_typing_inf, globals_agree, global_agree.
133+
destruct (List.nth_error xs n) as [ x' | ] eqn:Hnth => //=.
134+
assert (n < length xs)%coq_nat as Hlen; first by apply List.nth_error_Some; rewrite Hnth.
135+
move/ltP in Hlen.
136+
rewrite Hlen => /=.
137+
move => [<-] => /=.
138+
apply/eqP; f_equal.
139+
by lias.
140+
Qed.
141+
142+
Lemma those_all2_impl {X Y Z: Type}: forall (xs: list X) (ns: list Y) (ts: list Z) (f: list X -> Y -> option Z) (g: list X -> Y -> Z -> bool),
143+
(forall xs n x, f xs n = Some x -> g xs n x) ->
144+
(those (map (f xs) ns) = Some ts) ->
145+
all2 (g xs) ns ts.
146+
Proof.
147+
setoid_rewrite <- those_those0.
148+
move => xs.
149+
elim => //=; first by intros; destruct ts.
150+
move => y ys IH ts f g Heq Himpl.
151+
remove_bools_options.
152+
apply Heq in Hoption.
153+
eapply IH in Hoption0; last by apply Heq.
154+
by rewrite Hoption Hoption0.
155+
Qed.
156+
157+
Lemma funcs_typing_inf_agree: forall s inst xts,
158+
(funcs_typing_inf s inst = Some xts) ->
159+
all2 (functions_agree s.(s_funcs)) inst.(inst_funcs) xts.
160+
Proof.
161+
move => f inst xts Hinf.
162+
unfold funcs_typing_inf in Hinf.
163+
eapply those_all2_impl; eauto.
164+
intros.
165+
rewrite <- func_typing_inf_agree.
166+
by apply/eqP.
167+
Qed.
168+
169+
Lemma tabs_typing_inf_agree: forall s inst xts,
170+
(tabs_typing_inf s inst = Some xts) ->
171+
all2 (tabi_agree s.(s_tables)) inst.(inst_tab) xts.
172+
Proof.
173+
move => f inst fts Hinf.
174+
eapply those_all2_impl; eauto.
175+
intros.
176+
by apply tab_typing_inf_agree.
177+
Qed.
178+
179+
Lemma mems_typing_inf_agree: forall s inst xts,
180+
(mems_typing_inf s inst = Some xts) ->
181+
all2 (memi_agree s.(s_mems)) inst.(inst_memory) xts.
182+
Proof.
183+
move => f inst fts Hinf.
184+
eapply those_all2_impl; eauto.
185+
intros.
186+
by apply mem_typing_inf_agree.
187+
Qed.
188+
189+
Lemma globals_typing_inf_agree: forall s inst xts,
190+
(globals_typing_inf s inst = Some xts) ->
191+
all2 (globals_agree s.(s_globals)) inst.(inst_globs) xts.
192+
Proof.
193+
move => f inst fts Hinf.
194+
eapply those_all2_impl; eauto.
195+
intros.
196+
by apply global_typing_inf_agree.
197+
Qed.
198+
199+
Lemma inst_typing_inf_impl: forall s inst C,
200+
inst_typing_inf s inst = Some C ->
201+
inst_typing s inst C.
202+
Proof.
203+
move => s inst C Hinf.
204+
unfold inst_typing_inf in Hinf.
205+
destruct inst.
206+
remove_bools_options => /=.
207+
apply/andP; split; last by apply mems_typing_inf_agree in Hoption1.
208+
apply/andP; split; last by apply tabs_typing_inf_agree in Hoption0.
209+
apply/andP; split; last by apply globals_typing_inf_agree in Hoption2.
210+
apply/andP; split; last by apply funcs_typing_inf_agree in Hoption.
211+
by apply/eqP.
212+
Defined.
213+
214+
End Context_inference.

theories/instantiation_sound.v

+18-17
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ Proof.
9292
by rewrite -> H.
9393
Qed.
9494

95-
9695
Let cl_type_check_single := @cl_type_check_single host_function.
9796

9897
Lemma cl_type_check_single_aux s_funcs s_tables s_mems s_globals func funcs tabs mems globs:
@@ -111,7 +110,7 @@ Proof.
111110
rename H4 into Hinst_typing. rename H8 into Hbe_typing.
112111
apply cl_typing_native
113112
with (C := C)
114-
(C' := upd_local_label_return C (tc_local C ++ t1s ++ ts) ([::t2s] ++ tc_label C) (Some t2s))
113+
(C' := upd_local_label_return C (t1s ++ ts) ([::t2s]) (Some t2s))
115114
=> //=.
116115
destruct C.
117116
rewrite /inst_typing. simpl.
@@ -630,10 +629,10 @@ Proof.
630629
this is no longer true in the future. *)
631630
exists (ET_tab {| tt_limits := {| lim_min := N.of_nat (tab_size tab); lim_max := table_max_opt tab |} ; tt_elem_type := ELT_funcref |}).
632631
econstructor; eauto.
633-
unfold tab_typing => /=.
634-
apply/andP; split => //.
635-
rewrite nat_bin.
636-
by lias.
632+
unfold tab_typing, limit_match => /=.
633+
apply/andP; split => //=; first by apply/N.leb_spec0; lias.
634+
destruct (table_max_opt tab) eqn:Hopt => //.
635+
by apply/N.leb_spec0; lias.
637636
Qed.
638637

639638
Lemma ext_typing_exists_mem addr s:
@@ -648,12 +647,12 @@ Proof.
648647
destruct Hnth as [mem Hnth].
649648

650649
(* Similar to tab_typing *)
651-
exists (ET_mem {| lim_min := N.of_nat (mem_size mem); lim_max := mem_max_opt mem |}).
650+
exists (ET_mem {| lim_min := mem_size mem; lim_max := mem_max_opt mem |}).
652651
econstructor; eauto.
653-
unfold mem_typing => /=.
654-
apply/andP; split => //.
655-
rewrite nat_bin N2Nat.id.
656-
by apply N.leb_refl.
652+
unfold mem_typing, limit_match => /=.
653+
apply/andP; split => //=; first by apply/N.leb_spec0; lias.
654+
destruct (mem_max_opt mem) eqn:Hopt => //.
655+
by apply/N.leb_spec0; lias.
657656
Qed.
658657

659658
Lemma ext_typing_exists_glob addr s:
@@ -667,8 +666,6 @@ Proof.
667666
apply nth_error_Some in Hnth'; by lias. }
668667
destruct Hnth as [glob Hnth].
669668

670-
(* Note that all tables can be tab_typed. This lemma needs more information if
671-
this is no longer true in the future. *)
672669
exists (ET_glob {| tg_mut := g_mut glob; tg_t := typeof (g_val glob) |}).
673670
econstructor; eauto.
674671
unfold global_agree => /=.
@@ -1045,9 +1042,13 @@ Proof.
10451042

10461043
destruct tt. destruct tt_limits. simpl.
10471044
rewrite /tab_typing. simpl.
1048-
apply/andP. split => //=.
1049-
rewrite /tab_size. simpl.
1050-
by rewrite repeat_length.
1045+
apply/andP. split => /=.
1046+
- rewrite /tab_size. simpl.
1047+
rewrite repeat_length.
1048+
apply/N.leb_spec0.
1049+
by rewrite nat_bin N2Nat.id; lias.
1050+
- destruct lim_max => //.
1051+
by apply/N.leb_spec0; lias.
10511052
}
10521053
-- (* memi_agree *)
10531054
rewrite <- Forall2_all2 => /=.
@@ -1090,7 +1091,7 @@ Proof.
10901091

10911092
destruct mt.
10921093
rewrite /mem_typing. simpl.
1093-
apply/andP. split => //=.
1094+
apply/andP. split => //=; last by destruct lim_max => //; apply/N.leb_spec0; lias.
10941095
rewrite /mem_size /operations.mem_length /memory_list.mem_length. simpl.
10951096
destruct lim_min => //.
10961097
rewrite /page_size. simpl.

theories/instantiation_spec.v

+2-2
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ Definition module_func_typing (c : t_context) (m : module_func) (tf : function_t
275275
tc_global := c.(tc_global);
276276
tc_table := c.(tc_table);
277277
tc_memory := c.(tc_memory);
278-
tc_local := c.(tc_local) ++ tn ++ t_locs;
279-
tc_label := tm :: c.(tc_label);
278+
tc_local := tn ++ t_locs;
279+
tc_label := [::tm];
280280
tc_return := Some tm;
281281
|} in
282282
typing.be_typing c' b_es (Tf [::] tm).

theories/interpreter_ctx.v

+2-2
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ Proof.
734734
inversion Hftype as [s' i tvs C f Hit Hfi Hlocs]; subst.
735735
destruct fc as [fvs fk ff fes]; simpl in *.
736736
apply inst_t_context_local_empty in Hit; rewrite -> Hit in *; simpl in *.
737-
rewrite length_is_size size_map -length_is_size in H3_getlocal; by lias.
737+
rewrite cats0 length_is_size size_map -length_is_size in H3_getlocal; by lias.
738738

739739
- (* AI_basic (BI_set_local j) *)
740740
get_cc ccs.
@@ -753,7 +753,7 @@ Proof.
753753
inversion Hftype as [s' i tvs C f Hit Hfi Hlocs]; subst.
754754
destruct fc as [fvs fk ff fes]; simpl in *.
755755
apply inst_t_context_local_empty in Hit; rewrite -> Hit in *; simpl in *.
756-
rewrite length_is_size size_map -length_is_size in H3_setlocal; by lias.
756+
rewrite cats0 length_is_size size_map -length_is_size in H3_setlocal; by lias.
757757

758758
- (* AI_basic (BI_tee_local j) *)
759759
destruct vs0 as [|v vs0].

theories/interpreter_func.v

+1
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,7 @@ Proof.
19171917
last by apply inst_t_context_local_empty in Hitype'.
19181918
replace (tc_label C''') with ([::] : seq (seq value_type)) in Hetype;
19191919
last by apply inst_t_context_label_empty in Hitype'.
1920+
rewrite cats0 in Hetype.
19201921
by apply Hetype.
19211922
Qed.
19221923

0 commit comments

Comments
 (0)