Registry Mirror
Skip to content

Unsound: the paths of the core theories can be taken by user theories #1249

Backlog

Description

@strub

The built-in reduction of integer, real and map operators and the translation to Why3 recognise these operators by their path: op_kind (src/ecFol.ml) and core_theories (src/ecSmt.ml) look up Top.CoreInt.lt, Top.CoreInt.add, etc. But the prelude only loads Pervasive, Tactics and Logic, so nothing prevents a user from defining a theory at one of these paths with other definitions. The reduction and the SMT translation then give these operators the meaning of the core ones.

Minimal working example

No require, no axiom, no SMT:

theory CoreInt.
  op lt (x y : int) : bool = true.
end CoreInt.

(* No SMT: the built-in reduction of integer comparisons is keyed by
   the path Top.CoreInt.lt as well. *)
lemma l2 : !CoreInt.lt 1 0.
proof. by simplify. qed.

lemma l1 : CoreInt.lt 1 0.
proof. by delta. qed.

lemma bad : false.
proof. by have := l2; rewrite l1. qed.

print axiom bad.

The same holds with smt() in place of simplify for l2. The other core theories (CoreReal, CoreMap, ...) are presumably affected as well.

Possible fix

Reserve the names of the core theories (reject theory and require ... as with these names outside the system library), or bind the core theories in the prelude so that defining them again raises DuplicatedBinding.

Activity

  1. oskgo commented on Nov 29, 2026

    @oskgo
    Contributor

    There might be similar problems with other tactics that hardcode objects from outside the prelude. Rewrite Pr is one example, but I'm sure there are others.

  2. oskgo commented on Nov 30, 2026

    @oskgo
    Contributor

    Yup:

    module M = {proc main() = {}}.
    
    theory CoreReal.
    op le (r1 r2: real) = false.
    end CoreReal.
    
    abbrev (<=) = CoreReal.le.
    
    lemma L_stdlib &m: Pr[M.main() @ &m : true] <= Pr[M.main() @ &m : true].
    proof. by rewrite Pr[mu_sub]. qed.
    
    lemma L_user &m: !Pr[M.main() @ &m : true] <= Pr[M.main() @ &m : true] by done.
    
    lemma bad: false.
    suff: forall &m, false by done.
    move => &m.
    apply/(L_user &m)/(L_stdlib &m).
    qed.
    
  3. oskgo commented on Dec 1, 2026

    @oskgo
    Contributor

    It's not just core theories either. There are for example places where we hardcode StdBigop.Bigreal.BRA.big. One instance is in rewrite Pr (mu_has_le), but that one isn't easily exploitable due to an internal typing error. I found an exploitable instance using the fel tactic:

    Click to expand code
    require import Int Real List.
    
    (* user provided "Core" theories *)
    theory FelTactic.
    end FelTactic.
    
    theory StdBigop.
      theory Bigreal.
        theory BRA.
          op big ['a] (P: 'a -> bool) (F: 'a -> real) (r: 'a list): real = 0%r.
        end BRA.
      end Bigreal.
    end StdBigop.
    
    (* setting up example *)
    module type OT = {proc incr(): unit}.
    
    module type Adv(O: OT) = {proc a(): unit}.
    
    module Exp(A:Adv) = {
      var c: int
    
      module O = {
        proc incr() = {c <- c + 1;}
      }
    
      proc main(): unit = {
        c <- 0;
        A(O).a();
      }
    }.
    
    module (Once: Adv) (O: OT) = {
      proc a() ={
        O.incr();
      }
    }.
    
    lemma L_real &m:
      Pr[Exp(Once).main() @ &m : Exp.c = 1]
      = 1%r.
    proof.
      byphoare => //.
      proc.
      inline.
      auto.
    qed.
    
    (* bug starts here *)
    lemma L_bad &m:
      Pr[Exp(Once).main() @ &m : Exp.c = 1]
      <= 0%r.
    proof.
      fel 1 (Exp.c) (fun x, 1%r) 1 (Exp.c = 1) []=> //.
      - inline; auto.
      move => c.
      proc; auto => /> /#.
    qed.
    
    lemma bad: false.
    proof.
      suff: forall &m, false by done.
      move => &m.
      have L_bad:=L_bad &m.
      by rewrite (L_real &m) in L_bad.
    qed.
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions