Definition vis mu := fun b => locBlocksSrc mu b || frgnBlocksSrc mu b.
Definition visTgt mu := fun b => locBlocksTgt mu b || frgnBlocksTgt mu b.
Inductive reach (m:mem) (B:block -> Prop): list (block * Z) -> block -> Prop :=
reach_nil: forall b, B b -> reach m B nil b
| reach_cons: forall b L b' z off n q,
reach m B L b' ->
Mem.perm m b' z Cur Readable ->
ZMap.get z (PMap.get b' (Mem.mem_contents m)) =
Fragment (Vptr b off) q n ->
reach m B ((b',z)::L) b.
Fixpoint reach' (m:mem) (B:block -> Prop) (L:list (block * Z)): block -> Prop:=
match L with
nil => B
| l::L => match l with
(b',z) => match ZMap.get z (PMap.get b' (Mem.mem_contents m))
with Fragment (Vptr b off) q  n => fun bb => bb = b /\
Mem.perm m b' z Cur Readable /\
reach' m B L b'
| _ => fun bb => False
end
end
end.
Fixpoint reach'' (m:mem) (B:block -> bool) (L:list (block * Z)): block -> bool:=
match L with
nil => B
| l::L => match l with
(b',z) => match ZMap.get z (PMap.get b' (Mem.mem_contents m))
with Fragment (Vptr b off) q  n => fun bb => eq_block bb b &&
Mem.perm_dec m b' z Cur Readable  &&
reach'' m B L b'
| _ => fun bb => false
end
end
end.
Parameter REACH : mem -> (block -> bool) -> block -> bool.
Definition replace_locals (mu:SM_Injection) pSrc' pTgt': SM_Injection :=
match mu with
Build_SM_Injection locBSrc locBTgt pSrc pTgt local extBSrc extBTgt fSrc fTgt extern =>
Build_SM_Injection locBSrc locBTgt pSrc' pTgt' local extBSrc extBTgt fSrc fTgt extern
end.
(*typically, we have forall b, pSrc b -> pSrc' b and forall b, pTgt b -> pTgt' b, i.e. only reclassify private entries as public*)
Definition replace_externs (mu:SM_Injection) fSrc' fTgt': SM_Injection :=
match mu with
Build_SM_Injection locBSrc locBTgt pSrc pTgt local extBSrc extBTgt fSrc fTgt extern =>
Build_SM_Injection locBSrc locBTgt pSrc pTgt local extBSrc extBTgt fSrc' fTgt' extern
end.
(*typically, we have forall b, fSrc b -> fSrc' b and forall b, fTgt b -> fTgt' b, i.e. only reclassify unknown entries as foreign*)
Definition getBlocks (V:list val) (b: block): bool :=
in_dec eq_block b
(fold_right (fun v L => match v with Vptr b' z => b'::L | _ => L end) nil V).
Definition REACH_closed m (X: Values.block -> bool) : Prop :=
(forall b, REACH m X b = true -> X b = true).
Definition mapped (j:meminj) b : bool :=
match j b with None => false | Some _ => true end.

Lemma restrict_val_inject: forall j val1 val2
(Inj : val_inject j val1 val2)
X (HR: forall b, getBlocks (val1::nil) b = true -> X b = true),
val_inject (restrict j X) val1 val2.