From 05b964ed58ed52bbd7d8898d10b6f0adff20c19e Mon Sep 17 00:00:00 2001 From: Pierre-Yves Strub Date: Fri, 9 Oct 2026 10:18:15 +0200 Subject: [PATCH] fix: `call /fc` must not read the caller's locals in the callee `call /fc` (concave ehoare call) typed its specification (`call /fc (_ : P ==> Q)`) and its invariant (`call /fc (: I)`) in the memory of the goal, i.e. of the caller, whereas the premise is a judgement on the called procedure. A local variable of the caller was therefore read as the callee's variable of the same name. With proc g() = { var y; y <- 0; } proc main() : int = { var y; y <- 1; g(); return y; } `call /(fun x => x) (_ : 0%xr ==> (y = 1)%xr)` proves `ehoare [M.main : 0%xr ==> (res = 1)%xr]`, hence (with `byehoare` and `hoare [M.main : true ==> res = 1]`) `false`; an invariant mentioning a local of the caller gives `false` the same way. The specification is now typed in the memories of the called procedure (`LDecl.hoareF`) and the invariant in an abstract memory, as `call` already does: the locals of the caller are no longer in scope. Fixes #1196. Origin: 99ec9f4b (2023-10-11, "ehoare"), first released in r2024.01. Test: tests/ehoare-call-concave-locals.ec (the previous build accepts the `fail` lines). unit (124 files), stdlib (128) and examples (49) pass. --- src/phl/ecPhlCall.ml | 33 ++++++++++++++--------- tests/ehoare-call-concave-locals.ec | 41 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 tests/ehoare-call-concave-locals.ec diff --git a/src/phl/ecPhlCall.ml b/src/phl/ecPhlCall.ml index 7fb727bab..5352f522c 100644 --- a/src/phl/ecPhlCall.ml +++ b/src/phl/ecPhlCall.ml @@ -750,21 +750,32 @@ let process_call_concave (fc, info) tc = | _ -> tc_error !!tc "the conclusion is not a ehoare" in - let process_spec tc = - let _, concl = FApi.tc1_flat tc in + (* As for [call] (see [process_call]), the specification is typed in the + memories of the called procedure, and the invariant in an abstract + memory: the local variables of the caller are not in scope (they + would be read as the callee's variables of the same name). *) + let process_spec tc pre post = + let hyps, concl = FApi.tc1_flat tc in match concl.f_node with | FeHoareS hs -> let (_,f,_) = fst (tc1_last_call tc hs.ehs_s) in - (txreal, fun pre post -> f_eHoareF pre f post) + let m = EcIdent.create "&hr" in + let penv, qenv = LDecl.hoareF m f hyps in + let pre = TTC.pf_process_form !!tc penv txreal pre in + let post = TTC.pf_process_form !!tc qenv txreal post in + f_eHoareF {m; inv = pre} f {m; inv = post} | _ -> tc_error !!tc "the conclusion is not a ehoare" in - let process_inv tc = - let _, concl = FApi.tc1_flat tc in + let process_inv tc inv = + let hyps, concl = FApi.tc1_flat tc in match concl.f_node with | FeHoareS hs -> + let m = fst hs.ehs_m in let (_,f,_) = fst (tc1_last_call tc hs.ehs_s) in - (txreal, fun inv -> f_eHoareF inv f inv) + let hyps = LDecl.push_active_ss (EcMemory.abstract m) hyps in + let inv = {m; inv = TTC.pf_process_form !!tc hyps txreal inv} in + (f_eHoareF inv f inv, inv) | _ -> tc_error !!tc "the conclusion is not a ehoare" in @@ -773,17 +784,13 @@ let process_call_concave (fc, info) tc = let process_cut tc info = match info with | CI_spec (pre, epost) -> - let ty,fmake = process_spec tc in - let _, pre = TTC.tc1_process_Xhl_form tc ty pre in - let _, post = TTC.tc1_process_Xhl_form tc ty epost.pnormal in - fmake pre post + process_spec tc pre epost.pnormal | CI_inv inv -> - let ty, fmake = process_inv tc in - let _, inv = TTC.tc1_process_Xhl_form tc ty inv in + let spec, inv = process_inv tc inv in subtactic := (fun tc -> FApi.t_firsts t_trivial 2 (EcPhlFun.t_fun (Inv_ss inv) tc)); - fmake inv + spec | _ -> tc_error !!tc "cannot supply additional information for call /" diff --git a/tests/ehoare-call-concave-locals.ec b/tests/ehoare-call-concave-locals.ec new file mode 100644 index 000000000..907323f9f --- /dev/null +++ b/tests/ehoare-call-concave-locals.ec @@ -0,0 +1,41 @@ +(* [call /fc] on an ehoare goal: as for [call], the specification is typed + in the memories of the called procedure and the invariant in an + abstract memory, so that the local variables of the caller are not in + scope (they used to be read as the callee's variables of the same + name, which proves [false]). *) +require import AllCore Xreal. + +module G = { var g : int }. + +module M = { + proc g() = { var y; y <- 0; } + + proc h() = { var y; G.g <- G.g + 1; y <- G.g; } + + proc main() : int = { var y; y <- 1; g(); return y; } + + proc main2() : bool = { var y; G.g <- 0; y <- 0; h(); return y <> G.g; } +}. + +(* the specification mentions [y], a local of the caller *) +lemma bad : ehoare [M.main : 0%xr ==> (res = 1)%xr]. +proof. +proc. +fail call /(fun x => x) (_ : 0%xr ==> (y = 1)%xr). +abort. + +(* the invariant mentions [y], a local of the caller *) +lemma bad_inv : ehoare [M.main2 : 0%xr ==> res%xr]. +proof. +proc. +fail call /(fun x => x) (: (y <> G.g)%xr). +abort. + +(* globals only: accepted *) +lemma ok : ehoare [M.main2 : 1%xr ==> (G.g = 1)%xr]. +proof. +proc. +call /(fun x => x) (_ : (G.g = 0)%xr ==> (G.g = 1)%xr). ++ by proc; auto => &hr /=; have -> : (G.g{hr} + 1 = 1) = (G.g{hr} = 0) by smt(). +by auto. +qed.