Correctness proof for code linearization
Require Import FSets.
Require Import Coqlib.
Require Import Maps.
Require Import Ordered.
Require Import Lattice.
Require Import AST.
Require Import Integers.
Require Import Values.
Require Import Memory.
Require Import Events.
Require Import Globalenvs.
Require Import Errors.
Require Import Smallstep.
Require Import Op.
Require Import Locations.
Require Import LTL.
Require Import Linear.
Require Import Linearize.
Require Import Annotations.
Module NodesetFacts :=
FSetFacts.Facts(
Nodeset).
Section LINEARIZATION.
Variable prog:
LTL.program.
Variable tprog:
Linear.program.
Hypothesis TRANSF:
transf_program prog =
OK tprog.
Let ge :=
Genv.globalenv prog.
Let tge :=
Genv.globalenv tprog.
Lemma functions_translated:
forall v f,
Genv.find_funct ge v =
Some f ->
exists tf,
Genv.find_funct tge v =
Some tf /\
transf_fundef f =
OK tf.
Proof (
Genv.find_funct_transf_partial transf_fundef _ TRANSF).
Lemma function_ptr_translated:
forall v f,
Genv.find_funct_ptr ge v =
Some f ->
exists tf,
Genv.find_funct_ptr tge v =
Some tf /\
transf_fundef f =
OK tf.
Proof (
Genv.find_funct_ptr_transf_partial transf_fundef _ TRANSF).
Lemma symbols_preserved:
forall id,
Genv.find_symbol tge id =
Genv.find_symbol ge id.
Proof (
Genv.find_symbol_transf_partial transf_fundef _ TRANSF).
Lemma public_preserved:
forall id,
Genv.public_symbol tge id =
Genv.public_symbol ge id.
Proof (
Genv.public_symbol_transf_partial transf_fundef _ TRANSF).
Lemma varinfo_preserved:
forall b,
Genv.find_var_info tge b =
Genv.find_var_info ge b.
Proof (
Genv.find_var_info_transf_partial transf_fundef _ TRANSF).
Lemma sig_preserved:
forall f tf,
transf_fundef f =
OK tf ->
Linear.funsig tf =
LTL.funsig f.
Proof.
Lemma stacksize_preserved:
forall f tf,
transf_function f =
OK tf ->
Linear.fn_stacksize tf =
LTL.fn_stacksize f.
Proof.
intros. monadInv H. auto.
Qed.
Lemma find_function_translated:
forall ros ls f,
LTL.find_function ge ros ls =
Some f ->
exists tf,
find_function tge ros ls =
Some tf /\
transf_fundef f =
OK tf.
Proof.
Correctness of reachability analysis
The entry point of the function is reachable.
Lemma reachable_entrypoint:
forall f, (
reachable f)!!(
f.(
fn_entrypoint)) =
true.
Proof.
The successors of a reachable instruction are reachable.
Lemma reachable_successors:
forall f pc pc'
b,
f.(
LTL.fn_code)!
pc =
Some b ->
In pc' (
successors_block b) ->
(
reachable f)!!
pc =
true ->
(
reachable f)!!
pc' =
true.
Proof.
Properties of node enumeration
An enumeration of CFG nodes is correct if the following conditions hold:
-
All nodes for reachable basic blocks must be in the list.
-
The list is without repetition (so that no code duplication occurs).
We prove that the result of the
enumerate function satisfies both
conditions.
Lemma nodeset_of_list_correct:
forall l s s',
nodeset_of_list l s =
OK s' ->
list_norepet l
/\ (
forall pc,
Nodeset.In pc s' <->
Nodeset.In pc s \/
In pc l)
/\ (
forall pc,
In pc l -> ~
Nodeset.In pc s).
Proof.
Lemma check_reachable_correct:
forall f reach s pc i,
check_reachable f reach s =
true ->
f.(
LTL.fn_code)!
pc =
Some i ->
reach!!
pc =
true ->
Nodeset.In pc s.
Proof.
Lemma enumerate_complete:
forall f enum pc i,
enumerate f =
OK enum ->
f.(
LTL.fn_code)!
pc =
Some i ->
(
reachable f)!!
pc =
true ->
In pc enum.
Proof.
Lemma enumerate_norepet:
forall f enum,
enumerate f =
OK enum ->
list_norepet enum.
Proof.
Properties related to labels
If labels are globally unique and the Linear code c contains
a subsequence Llabel lbl :: c1, then find_label lbl c returns c1.
Fixpoint unique_labels (
c:
code) :
Prop :=
match c with
|
nil =>
True
|
Llabel lbl ::
c => ~(
In (
Llabel lbl)
c) /\
unique_labels c
|
i ::
c =>
unique_labels c
end.
Lemma find_label_unique:
forall lbl c1 c2 c3,
is_tail (
Llabel lbl ::
c1)
c2 ->
unique_labels c2 ->
find_label lbl c2 =
Some c3 ->
c1 =
c3.
Proof.
induction c2.
simpl;
intros;
discriminate.
intros c3 TAIL UNIQ.
simpl.
generalize (
is_label_correct lbl a).
case (
is_label lbl a);
intro ISLBL.
subst a.
intro.
inversion TAIL.
congruence.
elim UNIQ;
intros.
elim H4.
apply is_tail_in with c1;
auto.
inversion TAIL.
congruence.
apply IHc2.
auto.
destruct a;
simpl in UNIQ;
tauto.
Qed.
Correctness of the starts_with test.
Lemma starts_with_correct:
forall lbl c1 c2 c3 s f sp ls m,
is_tail c1 c2 ->
unique_labels c2 ->
starts_with lbl c1 =
true ->
find_label lbl c2 =
Some c3 ->
plus step tge (
State s f sp c1 ls m)
E0 (
State s f sp c3 ls m).
Proof.
induction c1.
simpl;
intros;
discriminate.
simpl starts_with.
destruct a;
try (
intros;
discriminate).
intros.
apply plus_left with E0 (
State s f sp c1 ls m)
E0.
simpl.
constructor.
destruct (
peq lbl l).
subst l.
replace c3 with c1.
constructor.
apply find_label_unique with lbl c2;
auto.
apply plus_star.
apply IHc1 with c2;
auto.
eapply is_tail_cons_left;
eauto.
traceEq.
Qed.
Lemma starts_with_correct':
forall lbl c1 c2 c3 s f sp ls m,
is_tail c1 c2 ->
unique_labels c2 ->
starts_with lbl c1 =
true ->
find_label lbl c2 =
Some c3 ->
plus step_safe tge (
State s f sp c1 ls m)
E0 (
State s f sp c3 ls m).
Proof.
induction c1.
simpl;
intros;
discriminate.
simpl starts_with.
destruct a;
try (
intros;
discriminate).
intros.
apply plus_left with E0 (
State s f sp c1 ls m)
E0.
constructor;
auto.
simpl.
constructor.
destruct (
peq lbl l).
subst l.
replace c3 with c1.
constructor;
auto.
apply find_label_unique with lbl c2;
auto.
apply plus_star.
apply IHc1 with c2;
auto.
eapply is_tail_cons_left;
eauto.
traceEq.
Qed.
Connection between find_label and linearization.
Lemma find_label_add_branch:
forall lbl k s,
find_label lbl (
add_branch s k) =
find_label lbl k.
Proof.
Lemma find_label_lin_block:
forall lbl k b,
find_label lbl (
linearize_block b k) =
find_label lbl k.
Proof.
Remark linearize_body_cons:
forall f pc enum,
linearize_body f (
pc ::
enum) =
match f.(
LTL.fn_code)!
pc with
|
None =>
linearize_body f enum
|
Some b =>
Llabel pc ::
linearize_block b (
linearize_body f enum)
end.
Proof.
Lemma find_label_lin_rec:
forall f enum pc b,
In pc enum ->
f.(
LTL.fn_code)!
pc =
Some b ->
exists k,
find_label pc (
linearize_body f enum) =
Some (
linearize_block b k).
Proof.
Lemma find_label_lin:
forall f tf pc b,
transf_function f =
OK tf ->
f.(
LTL.fn_code)!
pc =
Some b ->
(
reachable f)!!
pc =
true ->
exists k,
find_label pc (
fn_code tf) =
Some (
linearize_block b k).
Proof.
Lemma find_label_lin_inv:
forall f tf pc b k,
transf_function f =
OK tf ->
f.(
LTL.fn_code)!
pc =
Some b ->
(
reachable f)!!
pc =
true ->
find_label pc (
fn_code tf) =
Some k ->
exists k',
k =
linearize_block b k'.
Proof.
intros.
exploit find_label_lin;
eauto.
intros [
k'
FIND].
exists k'.
congruence.
Qed.
Unique label property for linearized code.
Lemma label_in_add_branch:
forall lbl s k,
In (
Llabel lbl) (
add_branch s k) ->
In (
Llabel lbl)
k.
Proof.
Lemma label_in_lin_block:
forall lbl k b,
In (
Llabel lbl) (
linearize_block b k) ->
In (
Llabel lbl)
k.
Proof.
Lemma label_in_lin_rec:
forall f lbl enum,
In (
Llabel lbl) (
linearize_body f enum) ->
In lbl enum.
Proof.
Lemma unique_labels_add_branch:
forall lbl k,
unique_labels k ->
unique_labels (
add_branch lbl k).
Proof.
Lemma unique_labels_lin_block:
forall k b,
unique_labels k ->
unique_labels (
linearize_block b k).
Proof.
Lemma unique_labels_lin_rec:
forall f enum,
list_norepet enum ->
unique_labels (
linearize_body f enum).
Proof.
Lemma unique_labels_transf_function:
forall f tf,
transf_function f =
OK tf ->
unique_labels (
fn_code tf).
Proof.
Correctness of add_branch.
Lemma is_tail_find_label:
forall lbl c2 c1,
find_label lbl c1 =
Some c2 ->
is_tail c2 c1.
Proof.
induction c1;
simpl.
intros;
discriminate.
case (
is_label lbl a).
intro.
injection H;
intro.
subst c2.
constructor.
constructor.
intro.
constructor.
auto.
Qed.
Lemma is_tail_add_branch:
forall lbl c1 c2,
is_tail (
add_branch lbl c1)
c2 ->
is_tail c1 c2.
Proof.
Lemma is_tail_lin_block:
forall b c1 c2,
is_tail (
linearize_block b c1)
c2 ->
is_tail c1 c2.
Proof.
Lemma add_branch_correct:
forall lbl c k s f tf sp ls m,
transf_function f =
OK tf ->
is_tail k tf.(
fn_code) ->
find_label lbl tf.(
fn_code) =
Some c ->
plus step tge (
State s tf sp (
add_branch lbl k)
ls m)
E0 (
State s tf sp c ls m).
Proof.
Lemma add_branch_correct':
forall lbl c k s f tf sp ls m,
transf_function f =
OK tf ->
is_tail k tf.(
fn_code) ->
find_label lbl tf.(
fn_code) =
Some c ->
plus step_safe tge (
State s tf sp (
add_branch lbl k)
ls m)
E0 (
State s tf sp c ls m).
Proof.
Correctness of linearization
The proof of semantic preservation is a simulation argument of the "star" kind:
st1 --------------- st2
| |
t| t| + or ( 0 \/ |st1'| < |st1| )
| |
v v
st1'--------------- st2'
The invariant (horizontal lines above) is the
match_states
predicate defined below. It captures the fact that the flow
of data is the same in the source and linearized codes.
Moreover, whenever the source state is at node
pc in its
control-flow graph, the transformed state is at a code
sequence
c that starts with the label
pc.
Inductive match_stackframes:
LTL.stackframe ->
Linear.stackframe ->
Prop :=
|
match_stackframe_intro:
forall f sp bb ls tf c,
transf_function f =
OK tf ->
(
forall pc,
In pc (
successors_block bb) -> (
reachable f)!!
pc =
true) ->
is_tail c tf.(
fn_code) ->
match_stackframes
(
LTL.Stackframe f sp ls bb)
(
Linear.Stackframe tf sp ls (
linearize_block bb c)).
Inductive match_states:
LTL.state ->
Linear.state ->
Prop :=
|
match_states_add_branch:
forall s f sp pc ls m tf ts c
(
STACKS:
list_forall2 match_stackframes s ts)
(
TRF:
transf_function f =
OK tf)
(
REACH: (
reachable f)!!
pc =
true)
(
TAIL:
is_tail c tf.(
fn_code)),
match_states (
LTL.State s f sp pc ls m)
(
Linear.State ts tf sp (
add_branch pc c)
ls m)
|
match_states_cond_taken:
forall s f sp pc ls m tf ts cond args c
(
STACKS:
list_forall2 match_stackframes s ts)
(
TRF:
transf_function f =
OK tf)
(
REACH: (
reachable f)!!
pc =
true)
(
JUMP:
eval_condition cond (
reglist ls args)
m =
Some true),
match_states (
LTL.State s f sp pc (
undef_regs (
destroyed_by_cond cond)
ls)
m)
(
Linear.State ts tf sp (
Lcond cond args pc ::
c)
ls m)
|
match_states_jumptable:
forall s f sp pc ls m tf ts arg tbl c n
(
STACKS:
list_forall2 match_stackframes s ts)
(
TRF:
transf_function f =
OK tf)
(
REACH: (
reachable f)!!
pc =
true)
(
ARG:
ls (
R arg) =
Vint n)
(
JUMP:
list_nth_z tbl (
Int.unsigned n) =
Some pc),
match_states (
LTL.State s f sp pc (
undef_regs destroyed_by_jumptable ls)
m)
(
Linear.State ts tf sp (
Ljumptable arg tbl ::
c)
ls m)
|
match_states_block:
forall s f sp bb ls m tf ts c
(
STACKS:
list_forall2 match_stackframes s ts)
(
TRF:
transf_function f =
OK tf)
(
REACH:
forall pc,
In pc (
successors_block bb) -> (
reachable f)!!
pc =
true)
(
TAIL:
is_tail c tf.(
fn_code)),
match_states (
LTL.Block s f sp bb ls m)
(
Linear.State ts tf sp (
linearize_block bb c)
ls m)
|
match_states_call:
forall s f ls m tf ts,
list_forall2 match_stackframes s ts ->
transf_fundef f =
OK tf ->
match_states (
LTL.Callstate s f ls m)
(
Linear.Callstate ts tf ls m)
|
match_states_return:
forall s ls m ts,
list_forall2 match_stackframes s ts ->
match_states (
LTL.Returnstate s ls m)
(
Linear.Returnstate ts ls m).
Definition measure (
S:
LTL.state) :
nat :=
match S with
|
LTL.State s f sp pc ls m => 0%
nat
|
LTL.Block s f sp bb ls m => 1%
nat
|
_ => 0%
nat
end.
Remark match_parent_locset:
forall s ts,
list_forall2 match_stackframes s ts ->
parent_locset ts =
LTL.parent_locset s.
Proof.
induction 1; simpl. auto. inv H; auto.
Qed.
Theorem transf_step_correct:
forall s1 t s2,
LTL.step ge s1 t s2 ->
forall s1' (
MS:
match_states s1 s1'),
(
exists s2',
plus Linear.step tge s1'
t s2' /\
match_states s2 s2')
\/ (
measure s2 <
measure s1 /\
t =
E0 /\
match_states s2 s1')%
nat.
Proof.
Lemma transf_initial_states:
forall st1,
LTL.initial_state prog st1 ->
exists st2,
Linear.initial_state tprog st2 /\
match_states st1 st2.
Proof.
Lemma transf_final_states:
forall st1 st2 r,
match_states st1 st2 ->
LTL.final_state st1 r ->
Linear.final_state st2 r.
Proof.
intros. inv H0. inv H. inv H6. econstructor; eauto.
Qed.
Theorem transf_program_correct:
forward_simulation (
LTL.semantics prog) (
Linear.semantics tprog).
Proof.
Lemma match_stackframes_same_sps:
forall s ts,
list_forall2 match_stackframes s ts ->
map (
fun s =>
match s with |
LTL.Stackframe _ sp _ _ =>
sp end)
s =
map (
fun s =>
match s with |
Linear.Stackframe _ sp _ _ =>
sp end)
ts.
Proof.
induction s; intros.
- inv H; reflexivity.
- inv H. inv H2. simpl; erewrite IHs; eauto.
Qed.
Theorem transf_step_correct':
forall s1 t s2,
LTL.step_safe ge s1 t s2 ->
forall s1' (
MS:
match_states s1 s1'),
(
exists s2',
plus Linear.step_safe tge s1'
t s2' /\
match_states s2 s2')
\/ (
measure s2 <
measure s1 /\
t =
E0 /\
match_states s2 s1')%
nat.
Proof.
Theorem transf_program_correct':
forward_simulation (
LTL.semantics_safe prog) (
Linear.semantics_safe tprog).
Proof.
End LINEARIZATION.