(* *********************************************************************)
(* *)
(* The Compcert verified compiler *)
(* *)
(* Xavier Leroy, INRIA Paris-Rocquencourt *)
(* *)
(* Copyright Institut National de Recherche en Informatique et en *)
(* Automatique. All rights reserved. This file is distributed *)
(* under the terms of the GNU General Public License as published by *)
(* the Free Software Foundation, either version 2 of the License, or *)
(* (at your option) any later version. This file is also distributed *)
(* under the terms of the INRIA Non-Commercial License Agreement. *)
(* *)
(* *********************************************************************)
(** Global environments are a component of the dynamic semantics of
all languages involved in the compiler. A global environment
maps symbol names (names of functions and of global variables)
to the corresponding memory addresses. It also maps memory addresses
of functions to the corresponding function descriptions.
Global environments, along with the initial memory state at the beginning
of program execution, are built from the program of interest, as follows:
- A distinct memory address is assigned to each function of the program.
These function addresses use negative numbers to distinguish them from
addresses of memory blocks. The associations of function name to function
address and function address to function description are recorded in
the global environment.
- For each global variable, a memory block is allocated and associated to
the name of the variable.
These operations reflect (at a high level of abstraction) what takes
place during program linking and program loading in a real operating
system. *)
Require Import Recdef. Require Import Zwf. Require Import Axioms Coqlib Errors Maps AST Linking. Require Import Integers Floats Values Memory. Notation "s #1" := (fst s) (at level 9, format "s '#1'") : pair_scope. Notation "s #2" := (snd s) (at level 9, format "s '#2'") : pair_scope. Local Open Scope pair_scope. Local Open Scope error_monad_scope. Set Implicit Arguments.
(* To avoid useless definitions of inductors in extracted code. *)
Local Unset Elimination Schemes. Local Unset Case Analysis Schemes. (** * Symbol environments *)
(** Symbol environments are a restricted view of global environments,
focusing on symbol names and their associated blocks. They do not
contain mappings from blocks to function or variable definitions. *)
Module Senv. Record t: Type := mksenv {
(** Operations *)
find_symbol: ident -> option block;
public_symbol: ident -> bool;
invert_symbol: block -> option ident;
block_is_volatile: block -> bool;
nextblock: block;
(** Properties *)
find_symbol_injective:
forall id1 id2 b, find_symbol id1 = Some b -> find_symbol id2 = Some b -> id1 = id2;
invert_find_symbol:
forall id b, invert_symbol b = Some id -> find_symbol id = Some b;
find_invert_symbol:
forall id b, find_symbol id = Some b -> invert_symbol b = Some id;
public_symbol_exists:
forall id, public_symbol id = true -> exists b, find_symbol id = Some b;
find_symbol_below:
forall id b, find_symbol id = Some b -> Plt b nextblock;
block_is_volatile_below:
forall b, block_is_volatile b = true -> Plt b nextblock
}. Definition symbol_address (ge: t) (id: ident) (ofs: ptrofs) : val :=
match find_symbol ge id with
| Some b => Vptr b ofs
| None => Vundef
end.
Proof. intros. unfold symbol_address. destruct (find_symbol ge id). { unfold Val.addl. rewrite H. auto. } { auto. } Qed.
Definition equiv (se1 se2: t) : Prop :=
(forall id, find_symbol se2 id = find_symbol se1 id)
/\ (forall id, public_symbol se2 id = public_symbol se1 id)
/\ (forall b, block_is_volatile se2 b = block_is_volatile se1 b). End Senv. Module Genv. (** * Global environments *)
Section GENV. Variable F: Type. (**r The type of function descriptions *)
Variable V: Type. (**r The type of information attached to variables *)
(** The type of global environments. *)
Record t: Type := mkgenv {
genv_public: list ident; (**r which symbol names are public *)
genv_symb: PTree.t block; (**r mapping symbol -> block *)
genv_defs: PTree.t (globdef F V); (**r mapping block -> definition *)
genv_next: block; (**r next symbol pointer *)
genv_symb_range: forall id b, PTree.get id genv_symb = Some b -> Plt b genv_next;
genv_defs_range: forall b g, PTree.get b genv_defs = Some g -> Plt b genv_next;
genv_vars_inj: forall id1 id2 b,
PTree.get id1 genv_symb = Some b -> PTree.get id2 genv_symb = Some b -> id1 = id2
}. (** ** Lookup functions *)
(** [find_symbol ge id] returns the block associated with the given name, if any *)
Definition find_symbol (ge: t) (id: ident) : option block :=
PTree.get id ge.(genv_symb). (** [symbol_address ge id ofs] returns a pointer into the block associated
with [id], at byte offset [ofs]. [Vundef] is returned if no block is associated
to [id]. *)
Definition symbol_address (ge: t) (id: ident) (ofs: ptrofs) : val :=
match find_symbol ge id with
| Some b => Vptr b ofs
| None => Vundef
end. (** [public_symbol ge id] says whether the name [id] is public and defined. *)
Definition public_symbol (ge: t) (id: ident) : bool :=
match find_symbol ge id with
| None => false
| Some _ => In_dec ident_eq id ge.(genv_public)
end. (** [find_def ge b] returns the global definition associated with the given address. *)
Definition find_def (ge: t) (b: block) : option (globdef F V) :=
PTree.get b ge.(genv_defs). (** [find_funct_ptr ge b] returns the function description associated with
the given address. *)
Definition find_funct_ptr (ge: t) (b: block) : option F :=
match find_def ge b with Some (Gfun f) => Some f | _ => None end. (** [find_funct] is similar to [find_funct_ptr], but the function address
is given as a value, which must be a pointer with offset 0. *)
Definition find_funct (ge: t) (v: val) : option F :=
match v with
| Vptr b ofs => if Ptrofs.eq_dec ofs Ptrofs.zero then find_funct_ptr ge b else None
| _ => None
end. (** [invert_symbol ge b] returns the name associated with the given block, if any *)
Definition invert_symbol (ge: t) (b: block) : option ident :=
PTree.fold
(fun res id b' => if eq_block b b' then Some id else res)
ge.(genv_symb) None. (** [find_var_info ge b] returns the information attached to the variable
at address [b]. *)
Definition find_var_info (ge: t) (b: block) : option (globvar V) :=
match find_def ge b with Some (Gvar v) => Some v | _ => None end. (** [block_is_volatile ge b] returns [true] if [b] points to a global variable
of volatile type, [false] otherwise. *)
Definition block_is_volatile (ge: t) (b: block) : bool :=
match find_var_info ge b with
| None => false
| Some gv => gv.(gvar_volatile)
end. (** ** Constructing the global environment *)
Program Definition add_global (ge: t) (idg: ident * globdef F V) : t :=
@mkgenv
ge.(genv_public)
(PTree.set idg#1 ge.(genv_next) ge.(genv_symb))
(PTree.set ge.(genv_next) idg#2 ge.(genv_defs))
(Pos.succ ge.(genv_next))
_ _ _.
Proof. Admitted.
destruct ge. simpl in *. rewrite PTree.gsspec in H. destruct (peq id i). { inv H. apply Plt_succ. } { apply Plt_trans_succ. eauto. } Qed.
Proof. Admitted.
destruct ge. simpl in *. rewrite PTree.gsspec in H. destruct (peq b genv_next0). { inv H. apply Plt_succ. } { apply Plt_trans_succ. eauto. } Qed.
Proof. intros. unfold find_var_info. destruct (find_def ge b) as [[f1|v1]|]. { intuition congruence. } { intuition congruence. } { intuition congruence. } Qed.
Proof. Admitted.
Proof. intros. set (P := fun m ge => m!id = Some g <-> exists b, find_symbol ge id = Some b /\ find_def ge b = Some g). assert (REC: forall l m ge,
P m ge ->
P (fold_left (fun m idg => PTree.set idg#1 idg#2 m) l m)
(add_globals ge l)). { induction l as [ | [id1 g1] l]. { intros. simpl. auto. } { intros. simpl. apply IHl. unfold P. unfold add_global. unfold find_symbol. unfold find_def. simpl. rewrite ! PTree.gsspec. destruct (peq id id1). { subst id1. split. { intros. inv H0. exists (genv_next ge). split. { auto. } { auto. apply PTree.gss. } } { intros. destruct H0 as (b & A & B). inv A. rewrite PTree.gss in B. auto. } } { red in H. rewrite H. split. { intros (b & A & B). exists b. split. { auto. } { auto. rewrite PTree.gso. { auto. } { auto. apply Plt_ne. eapply genv_symb_range. eauto. } } } { intros (b & A & B). rewrite PTree.gso in B. { exists b. auto. } { apply Plt_ne. eapply genv_symb_range. eauto. } } } } } { apply REC. unfold P. unfold find_symbol. unfold find_def. simpl. rewrite ! PTree.gempty. split. { congruence. } { intros (b & A & B). congruence. } } Qed.
Proof. unfold block_is_volatile. intros. destruct (find_var_info ge b) as [gv|] eqn:FV. { rewrite find_var_info_iff in FV. eapply genv_defs_range. eauto. } { discriminate. } Qed.
(** ** Coercing a global environment into a symbol environment *)
Definition to_senv (ge: t) : Senv.t :=
@Senv.mksenv
(find_symbol ge)
(public_symbol ge)
(invert_symbol ge)
(block_is_volatile ge)
ge.(genv_next)
ge.(genv_vars_inj)
(invert_find_symbol ge)
(find_invert_symbol ge)
(public_symbol_exists ge)
ge.(genv_symb_range)
(block_is_volatile_below ge). (** * Construction of the initial memory state *)
Section INITMEM. Variable ge: t. Definition store_init_data (m: mem) (b: block) (p: Z) (id: init_data) : option mem :=
match id with
| Init_int8 n => Mem.store Mint8unsigned m b p (Vint n)
| Init_int16 n => Mem.store Mint16unsigned m b p (Vint n)
| Init_int32 n => Mem.store Mint32 m b p (Vint n)
| Init_int64 n => Mem.store Mint64 m b p (Vlong n)
| Init_float32 n => Mem.store Mfloat32 m b p (Vsingle n)
| Init_float64 n => Mem.store Mfloat64 m b p (Vfloat n)
| Init_addrof symb ofs =>
match find_symbol ge symb with
| None => None
| Some b' => Mem.store Mptr m b p (Vptr b' ofs)
end
| Init_space n => Some m
end. Fixpoint store_init_data_list (m: mem) (b: block) (p: Z) (idl: list init_data)
{struct idl}: option mem :=
match idl with
| nil => Some m
| id :: idl' =>
match store_init_data m b p id with
| None => None
| Some m' => store_init_data_list m' b (p + init_data_size id) idl'
end
end. Definition perm_globvar (gv: globvar V) : permission :=
if gv.(gvar_volatile) then Nonempty
else if gv.(gvar_readonly) then Readable
else Writable. Definition alloc_global (m: mem) (idg: ident * globdef F V): option mem :=
match idg with
| (id, Gfun f) =>
let (m1, b) := Mem.alloc m 0 1 in
Mem.drop_perm m1 b 0 1 Nonempty
| (id, Gvar v) =>
let init := v.(gvar_init) in
let sz := init_data_list_size init in
let (m1, b) := Mem.alloc m 0 sz in
match store_zeros m1 b 0 sz with
| None => None
| Some m2 =>
match store_init_data_list m2 b 0 init with
| None => None
| Some m3 => Mem.drop_perm m3 b 0 sz (perm_globvar v)
end
end
end. Fixpoint alloc_globals (m: mem) (gl: list (ident * globdef F V))
{struct gl} : option mem :=
match gl with
| nil => Some m
| g :: gl' =>
match alloc_global m g with
| None => None
| Some m' => alloc_globals m' gl'
end
end.
Proof. induction gl1. { simpl. intros. inversion H. subst. auto. } { simpl. intros. destruct (alloc_global m a). { eauto. } { eauto. inversion H. } } Qed.
Proof. Admitted.
Proof. intros until n. functional induction (store_zeros m b p n). { intros. inv H. auto. } { intros. rewrite IHo. { eauto with mem. } { eauto with mem. } } { intros. congruence. } Qed.
Proof. Admitted.
Proof. induction idl. { simpl. intros until m'. intros. congruence. } { simpl. intros until m'. caseEq (store_init_data m b p a). { try congruence. intros. transitivity (Mem.nextblock m0). { eauto. } { destruct a. { simpl in H. try (eapply Mem.nextblock_store; eauto; fail). } { simpl in H. try (eapply Mem.nextblock_store; eauto; fail). } { simpl in H. try (eapply Mem.nextblock_store; eauto; fail). } { simpl in H. try (eapply Mem.nextblock_store; eauto; fail). } { simpl in H. try (eapply Mem.nextblock_store; eauto; fail). } { simpl in H. try (eapply Mem.nextblock_store; eauto; fail). } { simpl in H. try (eapply Mem.nextblock_store; eauto; fail). congruence. } { simpl in H. try (eapply Mem.nextblock_store; eauto; fail). destruct (find_symbol ge i). { try congruence. eapply Mem.nextblock_store. eauto. } { try congruence. } } } } { try congruence. } } Qed.
Proof. Admitted.
Proof. unfold alloc_global. intros. destruct g as [id [f|v]]. { (* function *)
destruct (Mem.alloc m 0 1) as [m1 b] eqn:?. erewrite Mem.nextblock_drop. { eauto. erewrite Mem.nextblock_alloc. { eauto. } { eauto. } } { eauto. } } { (* variable *)
set (init := gvar_init v) in *. set (sz := init_data_list_size init) in *. destruct (Mem.alloc m 0 sz) as [m1 b] eqn:?. destruct (store_zeros m1 b 0 sz) as [m2|] eqn:?. { try discriminate. destruct (store_init_data_list m2 b 0 init) as [m3|] eqn:?. { try discriminate. erewrite Mem.nextblock_drop. { eauto. erewrite store_init_data_list_nextblock. { eauto. erewrite store_zeros_nextblock. { eauto. erewrite Mem.nextblock_alloc. { eauto. } { eauto. } } { eauto. } } { eauto. } } { eauto. } } { try discriminate. } } { try discriminate. } } Qed.
Proof. intros until n. functional induction (store_zeros m b p n). { intros. inv H. tauto. } { intros. destruct (IHo _ H). intros. split. { eauto with mem. } { eauto with mem. } } { intros. congruence. } Qed.
Proof. Admitted.
Proof. intros. assert (forall chunk v,
Mem.store chunk m b p v = Some m' ->
(Mem.perm m b' q k prm <-> Mem.perm m' b' q k prm)). { intros. split. { eauto with mem. } { eauto with mem. } } { destruct i. { simpl in H. eauto. } { simpl in H. eauto. } { simpl in H. eauto. } { simpl in H. eauto. } { simpl in H. eauto. } { simpl in H. eauto. } { simpl in H. eauto. inv H. tauto. } { simpl in H. eauto. destruct (find_symbol ge i). { try discriminate. eauto. } { try discriminate. } } } Qed.
Proof. Admitted.
Proof. induction idl as [ | i1 idl]. { simpl. intros. inv H. tauto. } { simpl. intros. destruct (store_init_data m b p i1) as [m1|] eqn:S1. { try discriminate. transitivity (Mem.perm m1 b' q k prm). { eapply store_init_data_perm. eauto. } { eapply IHidl. eauto. } } { try discriminate. } } Qed.
Proof. Admitted.
Proof. intros. destruct idg as [id [f|v]]. { simpl in H. (* function *)
destruct (Mem.alloc m 0 1) as [m1 b] eqn:?. assert (b' <> b). { apply Mem.valid_not_valid_diff with m. { eauto with mem. } { eauto with mem. } } { split. { intros. eapply Mem.perm_drop_3. { eauto. } { eauto. } { eauto. eapply Mem.perm_alloc_1. { eauto. } { eauto. } } } { intros. eapply Mem.perm_alloc_4. { eauto. } { eauto. eapply Mem.perm_drop_4. { eauto. } { eauto. } } { eauto. } } } } { simpl in H. (* variable *)
set (init := gvar_init v) in *. set (sz := init_data_list_size init) in *. destruct (Mem.alloc m 0 sz) as [m1 b] eqn:?. destruct (store_zeros m1 b 0 sz) as [m2|] eqn:?. { try discriminate. destruct (store_init_data_list m2 b 0 init) as [m3|] eqn:?. { try discriminate. assert (b' <> b). { apply Mem.valid_not_valid_diff with m. { eauto with mem. } { eauto with mem. } } { split. { intros. eapply Mem.perm_drop_3. { eauto. } { eauto. } { eauto. erewrite <- store_init_data_list_perm. { idtac. erewrite <- store_zeros_perm. { idtac. eapply Mem.perm_alloc_1. { eauto. } { eauto. } } { eauto. } } { eauto. } } } { intros. eapply Mem.perm_alloc_4. { eauto. } { eauto. erewrite store_zeros_perm. { idtac. erewrite store_init_data_list_perm. { idtac. eapply Mem.perm_drop_4. { eauto. } { eauto. } } { eauto. } } { eauto. } } { eauto. } } } } { try discriminate. } } { try discriminate. } } Qed.
Proof. Admitted.
Proof. induction gl. simpl; intros. inv H. tauto. simpl; intros. destruct (alloc_global m a) as [m1|] eqn:?; try discriminate. erewrite alloc_global_perm; eauto. eapply IHgl; eauto. unfold Mem.valid_block in *. erewrite alloc_global_nextblock; eauto. apply Plt_trans_succ; auto. Qed.
Proof. Admitted.
Proof. intros until n. functional induction (store_zeros m b p n). { intros. inv H. apply Mem.unchanged_on_refl. } { intros. apply Mem.unchanged_on_trans with m'. { eapply Mem.store_unchanged_on. { eauto. } { eauto. simpl. intros. apply H0. omega. } } { apply IHo. { auto. } { auto. intros. apply H0. omega. } } } { intros. discriminate. } Qed.
Proof. Admitted.
Proof. intros. destruct i. { simpl in *. try (eapply Mem.store_unchanged_on; eauto; fail). } { simpl in *. try (eapply Mem.store_unchanged_on; eauto; fail). } { simpl in *. try (eapply Mem.store_unchanged_on; eauto; fail). } { simpl in *. try (eapply Mem.store_unchanged_on; eauto; fail). } { simpl in *. try (eapply Mem.store_unchanged_on; eauto; fail). } { simpl in *. try (eapply Mem.store_unchanged_on; eauto; fail). } { simpl in *. try (eapply Mem.store_unchanged_on; eauto; fail). inv H. apply Mem.unchanged_on_refl. } { simpl in *. try (eapply Mem.store_unchanged_on; eauto; fail). destruct (find_symbol ge i). { try congruence. eapply Mem.store_unchanged_on. { eauto. } { eauto. } } { try congruence. } } Qed.
Proof. Admitted.
Proof. induction il. { simpl. intros. inv H. apply Mem.unchanged_on_refl. } { simpl. intros. destruct (store_init_data m b p a) as [m1|] eqn:?. { try congruence. apply Mem.unchanged_on_trans with m1. { eapply store_init_data_unchanged. { eauto. } { eauto. intros. apply H0. tauto. } } { eapply IHil. { eauto. } { eauto. intros. apply H0. generalize (init_data_size_pos a). omega. } } } { try congruence. } } Qed.
(** Properties related to [loadbytes] *)
Definition readbytes_as_zero (m: mem) (b: block) (ofs len: Z) : Prop :=
forall p n,
ofs <= p -> p + Z.of_nat n <= ofs + len ->
Mem.loadbytes m b p (Z.of_nat n) = Some (list_repeat n (Byte Byte.zero)).
Fixpoint bytes_of_init_data_list (il: list init_data): list memval :=
match il with
| nil => nil
| i :: il => bytes_of_init_data i ++ bytes_of_init_data_list il
end.
Fixpoint load_store_init_data (m: mem) (b: block) (p: Z) (il: list init_data) {struct il} : Prop :=
match il with
| nil => True
| Init_int8 n :: il' =>
Mem.load Mint8unsigned m b p = Some(Vint(Int.zero_ext 8 n))
/\ load_store_init_data m b (p + 1) il'
| Init_int16 n :: il' =>
Mem.load Mint16unsigned m b p = Some(Vint(Int.zero_ext 16 n))
/\ load_store_init_data m b (p + 2) il'
| Init_int32 n :: il' =>
Mem.load Mint32 m b p = Some(Vint n)
/\ load_store_init_data m b (p + 4) il'
| Init_int64 n :: il' =>
Mem.load Mint64 m b p = Some(Vlong n)
/\ load_store_init_data m b (p + 8) il'
| Init_float32 n :: il' =>
Mem.load Mfloat32 m b p = Some(Vsingle n)
/\ load_store_init_data m b (p + 4) il'
| Init_float64 n :: il' =>
Mem.load Mfloat64 m b p = Some(Vfloat n)
/\ load_store_init_data m b (p + 8) il'
| Init_addrof symb ofs :: il' =>
(exists b', find_symbol ge symb = Some b' /\ Mem.load Mptr m b p = Some(Vptr b' ofs))
/\ load_store_init_data m b (p + size_chunk Mptr) il'
| Init_space n :: il' =>
read_as_zero m b p n
/\ load_store_init_data m b (p + Z.max n 0) il'
end.
Proof. Admitted.
Proof. assert (A: forall chunk v m b p m1 il m',
Mem.store chunk m b p v = Some m1 ->
store_init_data_list m1 b (p + size_chunk chunk) il = Some m' ->
Mem.load chunk m' b p = Some(Val.load_result chunk v)). { intros. eapply Mem.load_unchanged_on with (P := fun b' ofs' => ofs' < p + size_chunk chunk). { eapply store_init_data_list_unchanged. { eauto. } { eauto. intros. omega. } } { intros. tauto. } { eapply Mem.load_store_same. eauto. } } { induction il. { simpl. auto. } { simpl. intros. destruct (store_init_data m b p a) as [m1|] eqn:?. { try congruence. exploit IHil. { eauto. } { eauto. set (P := fun (b': block) ofs' => p + init_data_size a <= ofs'). apply read_as_zero_unchanged with (m := m) (P := P). { red. intros. apply H0. { auto. generalize (init_data_size_pos a). omega. } { auto. omega. } { auto. } } { eapply store_init_data_unchanged with (P := P). { eauto. } { eauto. intros. unfold P. omega. } } { intros. unfold P. omega. } } { eauto. intro D. destruct a. { simpl in Heqo. split. { auto. eapply (A Mint8unsigned (Vint i)). { eauto. } { eauto. } } { auto. } } { simpl in Heqo. split. { auto. eapply (A Mint16unsigned (Vint i)). { eauto. } { eauto. } } { auto. } } { simpl in Heqo. split. { auto. eapply (A Mint32 (Vint i)). { eauto. } { eauto. } } { auto. } } { simpl in Heqo. split. { auto. eapply (A Mint64 (Vlong i)). { eauto. } { eauto. } } { auto. } } { simpl in Heqo. split. { auto. eapply (A Mfloat32 (Vsingle f)). { eauto. } { eauto. } } { auto. } } { simpl in Heqo. split. { auto. eapply (A Mfloat64 (Vfloat f)). { eauto. } { eauto. } } { auto. } } { simpl in Heqo. split. { auto. set (P := fun (b': block) ofs' => ofs' < p + init_data_size (Init_space z)). inv Heqo. apply read_as_zero_unchanged with (m := m1) (P := P). { red. intros. apply H0. { auto. } { auto. simpl. generalize (init_data_list_size_pos il). xomega. } { auto. } } { eapply store_init_data_list_unchanged. { eauto. } { eauto. intros. unfold P. omega. } } { intros. unfold P. simpl. xomega. } } { auto. } } { simpl in Heqo. rewrite init_data_size_addrof in *. split. { auto. destruct (find_symbol ge i). { try congruence. exists b0. split. { auto. } { auto. transitivity (Some (Val.load_result Mptr (Vptr b0 i0))). { eapply (A Mptr (Vptr b0 i0)). { eauto. } { eauto. } } { unfold Val.load_result. unfold Mptr. destruct Archi.ptr64. { auto. } { auto. } } } } { try congruence. } } { auto. } } } } { try congruence. } } } Qed.
Proof. Admitted.
Proof. intros. destruct g as [f|v]. { simpl in H. (* function *)
destruct (Mem.alloc m 0 1) as [m1 b] eqn:?. set (Q := fun b' (ofs: Z) => b' <> b). apply Mem.unchanged_on_implies with Q. { apply Mem.unchanged_on_trans with m1. { eapply Mem.alloc_unchanged_on. eauto. } { eapply Mem.drop_perm_unchanged_on. { eauto. } { eauto. } } } { intros. red. apply Mem.valid_not_valid_diff with m. { eauto with mem. } { eauto with mem. } } } { simpl in H. (* variable *)
set (init := gvar_init v) in *. set (sz := init_data_list_size init) in *. destruct (Mem.alloc m 0 sz) as [m1 b] eqn:?. destruct (store_zeros m1 b 0 sz) as [m2|] eqn:?. { try discriminate. destruct (store_init_data_list m2 b 0 init) as [m3|] eqn:?. { try discriminate. set (Q := fun b' (ofs: Z) => b' <> b). apply Mem.unchanged_on_implies with Q. { apply Mem.unchanged_on_trans with m1. { eapply Mem.alloc_unchanged_on. eauto. } { apply Mem.unchanged_on_trans with m2. { eapply store_zeros_unchanged. { eauto. } { eauto. } } { apply Mem.unchanged_on_trans with m3. { eapply store_init_data_list_unchanged. { eauto. } { eauto. } } { eapply Mem.drop_perm_unchanged_on. { eauto. } { eauto. } } } } } { intros. red. apply Mem.valid_not_valid_diff with m. { eauto with mem. } { eauto with mem. } } } { try discriminate. } } { try discriminate. } } Qed.
Proof. Admitted.
Proof. induction gl. { simpl. intros. inv H. apply Mem.unchanged_on_refl. } { simpl. intros. destruct (alloc_global m a) as [m''|] eqn:?. { try discriminate. destruct a as [id g]. apply Mem.unchanged_on_trans with m''. { eapply alloc_global_unchanged. eauto. } { apply IHgl. auto. } } { try discriminate. } } Qed.
Proof. Admitted.
Proof. induction il. { intro p. simpl. auto. } { intro p. simpl. rewrite ! H. destruct a. { intuition. } { intuition. } { intuition. } { intuition. } { intuition. } { intuition. } { intuition. red. intros. rewrite H. auto. } { intuition. } } Qed.
Definition globals_initialized (g: t) (m: mem) :=
forall b gd,
find_def g b = Some gd ->
match gd with
| Gfun f =>
Mem.perm m b 0 Cur Nonempty
/\ (forall ofs k p, Mem.perm m b ofs k p -> ofs = 0 /\ p = Nonempty)
| Gvar v =>
Mem.range_perm m b 0 (init_data_list_size v.(gvar_init)) Cur (perm_globvar v)
/\ (forall ofs k p, Mem.perm m b ofs k p ->
0 <= ofs < init_data_list_size v.(gvar_init) /\ perm_order (perm_globvar v) p)
/\ (v.(gvar_volatile) = false -> load_store_init_data m b 0 v.(gvar_init))
/\ (v.(gvar_volatile) = false -> Mem.loadbytes m b 0 (init_data_list_size v.(gvar_init)) = Some (bytes_of_init_data_list v.(gvar_init)))
end.
Proof. intros. apply alloc_globals_initialized with Mem.empty. { auto. } { rewrite Mem.nextblock_empty. auto. } { red. intros. unfold find_def in H0. simpl in H0. rewrite PTree.gempty in H0. discriminate. } Qed.
Proof. Admitted.
Proof. intros. rewrite find_var_info_iff in H. exploit init_mem_characterization_gen. { eauto. } { eauto. } { eauto. } Qed.
Proof. Admitted.
Proof. intros. rewrite find_funct_ptr_iff in H. exploit init_mem_characterization_gen. { eauto. } { eauto. } { eauto. } Qed.
(** ** Compatibility with memory injections *)
Section INITMEM_INJ. Variable ge: t. Variable thr: block. Hypothesis symb_inject: forall id b, find_symbol ge id = Some b -> Plt b thr.
(** ** Sufficient and necessary conditions for the initial memory to exist. *)
(** Alignment properties *)
Definition init_data_alignment (i: init_data) : Z :=
match i with
| Init_int8 n => 1
| Init_int16 n => 2
| Init_int32 n => 4
| Init_int64 n => 8
| Init_float32 n => 4
| Init_float64 n => 4
| Init_addrof symb ofs => if Archi.ptr64 then 8 else 4
| Init_space n => 1
end. Fixpoint init_data_list_aligned (p: Z) (il: list init_data) {struct il} : Prop :=
match il with
| nil => True
| i1 :: il => (init_data_alignment i1 | p) /\ init_data_list_aligned (p + init_data_size i1) il
end. Section INITMEM_INVERSION. Variable ge: t.
Proof. Admitted.
Proof. intros. assert (DFL: forall chunk v,
Mem.store chunk m b p v = Some m' ->
align_chunk chunk = init_data_alignment i ->
(init_data_alignment i | p)). { intros. apply Mem.store_valid_access_3 in H0. destruct H0. congruence. } { destruct i. { simpl in H. eauto. } { simpl in H. eauto. } { simpl in H. eauto. } { simpl in H. eauto. } { simpl in H. eauto. } { simpl in H. eauto. } { simpl in H. eauto. simpl. apply Z.divide_1_l. } { simpl in H. eauto. destruct (find_symbol ge i). { try discriminate. eapply DFL. { eassumption. } { unfold Mptr. unfold init_data_alignment. destruct Archi.ptr64. { auto. } { auto. } } } { try discriminate. } } } Qed.
Proof. Admitted.
Proof. induction il as [ | i1 il]. { simpl. intros. auto. } { simpl. intros. destruct (store_init_data ge m b p i1) as [m1|] eqn:S1. { try discriminate. split. { eauto. eapply store_init_data_aligned. eauto. } { eauto. } } { try discriminate. } } Qed.
Proof. Admitted.
Proof. induction il as [ | i1 il]. { simpl. intros. contradiction. } { simpl. intros. destruct (store_init_data ge m b p i1) as [m1|] eqn:S1. { try discriminate. destruct H0. { subst i1. simpl in S1. destruct (find_symbol ge i) as [b'|]. { exists b'. auto. } { discriminate. } } { eapply IHil. { eauto. } { eauto. } } } { try discriminate. } } Qed.
End INITMEM_INVERSION.
Proof. Admitted.
Proof. intros until v. unfold init_mem. set (ge := globalenv p). revert m. generalize Mem.empty. generalize (prog_defs p). induction l as [ | idg1 defs ]. { simpl. intros m m'. intros. contradiction. } { simpl. intros m m'. intros. destruct (alloc_global ge m idg1) as [m''|] eqn:A. { try discriminate. destruct H0. { subst idg1. simpl in A. set (il := gvar_init v) in *. set (sz := init_data_list_size il) in *. destruct (Mem.alloc m 0 sz) as [m1 b]. destruct (store_zeros m1 b 0 sz) as [m2|]. { try discriminate. destruct (store_init_data_list ge m2 b 0 il) as [m3|] eqn:B. { try discriminate. split. { eapply store_init_data_list_aligned. eauto. } { intros. eapply store_init_data_list_free_idents. { eauto. } { eauto. } } } { try discriminate. } } { try discriminate. } } { eapply IHdefs. { eauto. } { eauto. } } } { try discriminate. } } Qed.
Section INITMEM_EXISTS. Variable ge: t.
Proof. Admitted.
Proof. intros until n. functional induction (store_zeros m b p n). { intros PERM. exists m. auto. } { intros PERM. apply IHo. red. intros. eapply Mem.perm_store_1. { eauto. } { eauto. apply PERM. omega. } } { intros PERM. destruct (Mem.valid_access_store m Mint8unsigned b p Vzero) as (m' & STORE). { split. { red. intros. apply Mem.perm_cur. apply PERM. simpl in H. omega. } { simpl. apply Z.divide_1_l. } } { congruence. } } Qed.
Proof. induction il as [ | i1 il ]. { simpl. intros. exists m. auto. } { simpl. intros. destruct H0. destruct (@store_init_data_exists m b p i1) as (m1 & S1). { eauto. red. intros. apply H. generalize (init_data_list_size_pos il). omega. } { eauto. } { eauto. } { eauto. rewrite S1. apply IHil. { eauto. red. intros. erewrite <- store_init_data_perm by eauto. apply H. generalize (init_data_size_pos i1). omega. } { eauto. } { eauto. } } } Qed.
Proof. Admitted.
Proof. intros m [id [f|v]]. { intros. simpl. destruct (Mem.alloc m 0 1) as [m1 b] eqn:ALLOC. destruct (Mem.range_perm_drop_2 m1 b 0 1 Nonempty) as [m2 DROP]. { red. intros. eapply Mem.perm_alloc_2. { eauto. } { eauto. } } { exists m2. auto. } } { intros. simpl. destruct H as [P Q]. set (sz := init_data_list_size (gvar_init v)). destruct (Mem.alloc m 0 sz) as [m1 b] eqn:ALLOC. assert (P1: Mem.range_perm m1 b 0 sz Cur Freeable). { red. intros. eapply Mem.perm_alloc_2. { eauto. } { eauto. } } { idtac. destruct (@store_zeros_exists m1 b 0 sz) as [m2 ZEROS]. { red. intros. apply Mem.perm_implies with Freeable. { auto with mem. } { auto with mem. } } { rewrite ZEROS. assert (P2: Mem.range_perm m2 b 0 sz Cur Freeable). { red. intros. erewrite <- store_zeros_perm by eauto. eauto. } { destruct (@store_init_data_list_exists b (gvar_init v) m2 0) as [m3 STORE]. { auto. red. intros. apply Mem.perm_implies with Freeable. { auto with mem. } { auto with mem. } } { auto. } { auto. } { auto. rewrite STORE. assert (P3: Mem.range_perm m3 b 0 sz Cur Freeable). { red. intros. erewrite <- store_init_data_list_perm by eauto. eauto. } { destruct (Mem.range_perm_drop_2 m3 b 0 sz (perm_globvar v)) as [m4 DROP]. { auto. } { auto. exists m4. auto. } } } } } } } Qed.
End INITMEM_EXISTS.
Proof. Admitted.
Proof. intros. set (ge := globalenv p) in *. unfold init_mem. revert H. generalize (prog_defs p) Mem.empty. induction l as [ | idg l]. { simpl. intros. exists m. auto. } { simpl. intros. destruct (@alloc_global_exists ge m idg) as [m1 A1]. { destruct idg as [id [f|v]]. { eauto. } { eauto. } } { fold ge. rewrite A1. eapply IHl. eauto. } } Qed.
End GENV. (** * Commutation with program transformations *)
Section MATCH_GENVS. Context {A B V W: Type} (R: globdef A V -> globdef B W -> Prop). Record match_genvs (ge1: t A V) (ge2: t B W): Prop := {
mge_next:
genv_next ge2 = genv_next ge1;
mge_symb:
forall id, PTree.get id (genv_symb ge2) = PTree.get id (genv_symb ge1);
mge_defs:
forall b, option_rel R (PTree.get b (genv_defs ge1)) (PTree.get b (genv_defs ge2))
}.
Proof. intros. generalize (find_def_match_2 b). rewrite H. intros R. inv R. exists y. auto. Qed.
Proof. Admitted.
Proof. intros. rewrite find_funct_ptr_iff in *. apply find_def_match in H. destruct H as (tg & P & Q). inv Q. exists ctx', f2. intuition auto. apply find_funct_ptr_iff. auto. Qed.
Proof. Admitted.
Proof. intros. exploit find_funct_inv. { eauto. } { eauto. intros [b EQ]. subst v. rewrite find_funct_find_funct_ptr in H. rewrite find_funct_find_funct_ptr. apply find_funct_ptr_match. auto. } Qed.
Proof. Admitted.
Proof. intros. rewrite find_var_info_iff in *. apply find_def_match in H. destruct H as (tg & P & Q). inv Q. exists v2. split. { auto. apply find_var_info_iff. auto. } { auto. } Qed.
End MATCH_PROGRAMS. (** Special case for partial transformations that do not depend on the compilation unit *)
Section TRANSFORM_PARTIAL. Context {A B V: Type} {LA: Linker A} {LV: Linker V}. Context {transf: A -> res B} {p: program A V} {tp: program B V}. Hypothesis progmatch: match_program (fun cu f tf => transf f = OK tf) eq p tp.
End TRANSFORM_PARTIAL. (** Special case for total transformations that do not depend on the compilation unit *)
Section TRANSFORM_TOTAL. Context {A B V: Type} {LA: Linker A} {LV: Linker V}. Context {transf: A -> B} {p: program A V} {tp: program B V}. Hypothesis progmatch: match_program (fun cu f tf => tf = transf f) eq p tp.