diff --git a/src/ecEnv.ml b/src/ecEnv.ml index af52385a0..747733e94 100644 --- a/src/ecEnv.ml +++ b/src/ecEnv.ml @@ -2091,7 +2091,7 @@ module Mod = struct let me_oinfos = let keep = List.map (EcPath.mident -| fst) me_params in let keep = Sm.of_list keep in - Msym.map (OI.filter (fun f -> Sm.mem (f.x_top) keep)) me.me_oinfos in + Msym.map (OI.filter (fun f -> Sm.mem (m_functor f.x_top) keep)) me.me_oinfos in { me with me_params; me_oinfos; } in @@ -2352,7 +2352,7 @@ module ModTy = struct let keep = List.map (EcPath.mident -| fst) params in let keep = Sm.of_list keep in - let ois = Msym.map (OI.filter (fun f -> Sm.mem (f.x_top) keep)) ois in + let ois = Msym.map (OI.filter (fun f -> Sm.mem (m_functor f.x_top) keep)) ois in { mis_params = params; mis_body = items; diff --git a/src/ecTyping.ml b/src/ecTyping.ml index 313fff050..b136189c6 100644 --- a/src/ecTyping.ml +++ b/src/ecTyping.ml @@ -2166,13 +2166,28 @@ and transmod_body ~attop (env : EcEnv.env) x params (me:pmodule_expr) = let extraparams = me.me_params in let allparams = stparams @ extraparams in + (* A procedure of the alias is a call to the same procedure of the + target, applied to the remaining parameters. Its oracle infos + are computed from this call, as for a structure: the oracle + infos of the target do not see the calls that reach the + parameters of the alias through the arguments of the target. *) + let me_oinfos = + let env = EcEnv.Mod.bind_params extraparams env in + let mp = EcPath.m_apply mp (List.map (mident -| fst) extraparams) in + let entries = + List.map + (fun (Tys_function fs) -> + (fs.fs_name, [EcPath.xpath mp fs.fs_name])) + me.me_sig_body in + get_oi_calls_of_entries env allparams entries in + let me = { me_name = x.pl_desc; me_body = ME_Alias (arity,mp); me_params = allparams; me_sig_body = me.me_sig_body; me_comps = me.me_comps; - me_oinfos = me.me_oinfos; + me_oinfos; } in me | Pm_struct ps -> @@ -2458,40 +2473,49 @@ and transmod_body ~attop (env : EcEnv.env) x params (me:pmodule_expr) = (* -------------------------------------------------------------------- *) (* Module parameters must have been added to the environment *) and get_oi_calls env (params, items) = + let entries = List.filter_map (function + | MI_Module _ | MI_Variable _ -> None + | MI_Function f -> + let calls = + match f.f_def with + | FBalias f -> [f] + | FBdef def -> def.f_uses.us_calls + | FBabs oi -> OI.allowed oi in + Some (f.f_name, calls)) items in + + get_oi_calls_of_entries env params entries + +(* -------------------------------------------------------------------- *) +(* The oracle infos of procedures that make the calls [entries]: all *) +(* the procedures that these calls reach, transitively, whose functor *) +(* is one of [params]. Module parameters must have been added to the *) +(* environment. *) +and get_oi_calls_of_entries env params entries = let mparams = let mparams = List.map (mident -| fst) params in Sm.of_list mparams in - let comp_oi oi it = match it with - | MI_Module _ | MI_Variable _ -> oi - | MI_Function f -> - let rec f_call c f = - let f = NormMp.norm_xfun env f in - if EcPath.Sx.mem f c then c - else - let c = EcPath.Sx.add f c in - let fun_ = EcEnv.Fun.by_xpath f env in - match fun_.f_def with - | FBalias _ -> assert false - | FBdef def -> List.fold_left f_call c def.f_uses.us_calls - | FBabs oi -> - List.fold_left f_call c (OI.allowed oi) in - - let all_calls = - match f.f_def with - | FBalias f -> f_call EcPath.Sx.empty f - | FBdef def -> - List.fold_left f_call EcPath.Sx.empty def.f_uses.us_calls - | FBabs oi -> - List.fold_left f_call EcPath.Sx.empty (OI.allowed oi) in - let filter f = - let ftop = EcPath.m_functor f.EcPath.x_top in - Sm.mem ftop mparams in + let rec f_call c f = + let f = NormMp.norm_xfun env f in + if EcPath.Sx.mem f c then c + else + let c = EcPath.Sx.add f c in + let fun_ = EcEnv.Fun.by_xpath f env in + match fun_.f_def with + | FBalias _ -> assert false + | FBdef def -> List.fold_left f_call c def.f_uses.us_calls + | FBabs oi -> + List.fold_left f_call c (OI.allowed oi) in + + let filter f = + let ftop = EcPath.m_functor f.EcPath.x_top in + Sm.mem ftop mparams in + + List.fold_left (fun oi (name, calls) -> + let all_calls = List.fold_left f_call EcPath.Sx.empty calls in let calls = List.filter filter (EcPath.Sx.elements all_calls) in - - Msym.add f.f_name (OI.mk calls) oi in - - List.fold_left comp_oi Msym.empty items + Msym.add name (OI.mk calls) oi) + Msym.empty entries (* -------------------------------------------------------------------- *) and transstruct diff --git a/tests/partial-app-oracle-calls.ec b/tests/partial-app-oracle-calls.ec new file mode 100644 index 000000000..cfc44fea0 --- /dev/null +++ b/tests/partial-app-oracle-calls.ec @@ -0,0 +1,119 @@ +(* Regression for #1175: partially applying a functor must keep the + calls to its remaining parameters, including calls to an applied + parameter such as `S(M).kg`. Dropping them let `B (D : T) (S : Sch)` + be cast to (or instantiate) an adversary type forbidding those calls. *) +require import AllCore. + +module type T = { }. +module type Sch (X : T) = { proc kg() : unit }. +module type Adv (S : Sch) = { proc run() : unit }. + +module M : T = { }. + +module St (X : T) = { + var c : int + proc kg() : unit = { c <- c + 1; } +}. + +(* Without a leading parameter: rejected. *) +fail module (B' : Adv) (S : Sch) = { + proc run() : unit = { S(M).kg(); } +}. + +(* With a leading parameter: must be rejected as well. *) +fail module (B (D : T) : Adv) (S : Sch) = { + proc run() : unit = { S(M).kg(); } +}. + +(* A leading parameter alone does not make the cast fail. *) +module (C (D : T) : Adv) (S : Sch) = { + proc run() : unit = { } +}. + +(* Without the cast, `B(M)` must not instantiate an `Adv`. *) +module B (D : T) (S : Sch) = { + proc run() : unit = { S(M).kg(); } +}. + +lemma frame (A <: Adv {-St}) n : + hoare [A(St).run : St.c = n ==> St.c = n]. +proof. by proc (St.c = n). qed. + +lemma ok : hoare [C(M, St).run : St.c = 0 ==> St.c = 0]. +proof. exact (frame (C(M)) 0). qed. + +lemma bad : hoare [B(M, St).run : St.c = 0 ==> St.c = 0]. +proof. fail exact (frame (B(M)) 0). abort. + +(* Regression for #1234: the oracle infos of a module alias are the + calls that the procedures of its target reach, and some of these may + be calls to parameters of the alias, directly or through the + arguments of the target. *) +module type U = { proc p() : bool }. +module type V (O : U) = { proc p() : bool {} }. +module type W (O : U) = { proc p() : bool {O.p} }. + +module E (X : U) = { + proc p() : bool = { var r; r <@ X.p(); return r; } +}. + +module Id (X : U) = X. + +module E2 (X : U) (Y : U) = { + proc p() : bool = { var r; r <@ X.p(); return r; } +}. + +module Ap (A : V) (X : U) = { + proc p() : bool = { var r; r <@ A(X).p(); return r; } +}. + +module True = { proc p() = { return true; } }. +module False = { proc p() = { return false; } }. + +(* The alias calls its parameter: rejected. *) +fail module (C1 : V) (O : U) = O. + +(* The target calls the parameter of the alias: rejected... *) +fail module (C2 : V) (O : U) = E(O). + +(* ... also through nested applications, other aliases, functor + parameters or partially applied functors. *) +fail module (C3 : V) (O : U) = E(E(O)). +fail module (C4 : V) (O : U) = E(Id(O)). +fail module (C5 (A : W) : V) (O : U) = Id(A(O)). +fail module (C6 (A : W) : V) (O : U) = A(E(O)). +fail module (C7 : V) (O : U) = Ap(E2(O), True). + +(* Accepted when the type allows the call, or when there is none. *) +module (A1 : W) (O : U) = O. +module (A2 : W) (O : U) = E(O). +module (A3 : W) (O : U) = E(E(O)). +module (A4 (A : W) : W) (O : U) = Id(A(O)). +module (A5 : V) (O : U) = E(True). +module (A6 : W) = E. + +(* Without the cast, the aliases must not instantiate a `V`. *) +module C0 (O : U) = O. +module C8 (O : U) = E(E(O)). +module C9 (A : W) (O : U) = Id(A(O)). + +lemma L1 (F <: V) : equiv [F(True).p ~ F(False).p : ={glob F} ==> ={res}]. +proof. by proc*; call (: true); auto. qed. + +lemma bad0 : equiv [C0(True).p ~ C0(False).p : true ==> ={res}]. +proof. fail exact (L1 C0). abort. + +lemma bad8 : equiv [C8(True).p ~ C8(False).p : true ==> ={res}]. +proof. fail exact (L1 C8). abort. + +lemma bad9 : equiv [C9(Id, True).p ~ C9(Id, False).p : true ==> ={res}]. +proof. fail exact (L1 (C9(Id))). abort. + +section. +declare module A <: W. + +local module C10 (O : U) = E(A(O)). + +lemma bad10 : equiv [C10(True).p ~ C10(False).p : ={glob A} ==> ={res}]. +proof. fail exact (L1 C10). abort. +end section.