#20484·aptos-core

[prover] Inferred ensures_of/result_of name the post-state in the callee's pre-state slot (old(..) stripped in eliminate_write_of)

Author: rahxephon89Created Sep 2, 2026Updated Sep 2, 2026

Rewritten 2026-09-02. Earlier revisions of this issue blamed #20470 and then called it a regression of a sound proof. Both were wrong, and the comments below record how. The root cause is pre-existing and is identified at the line level here; #20470 is correct and merely made the defect observable.

Summary

eliminate_write_of in move-prover/bytecode-pipeline/src/spec_inference.rs emits inferred ensures_of / result_of predicates whose pre-state slots are filled with old-stripped arguments. For a &mut parameter that means the slot names the post-state, so a contract that should relate old to new instead relates new to new.

For a callee dec(s: &mut S, k: u64) with ensures s.v == old(s.v) - k, the emitted obligation becomes

ensures_of<dec>(post, k, post)     // i.e.  post.v == post.v - k    ⟹  k == 0

which no real call satisfies.

The line

rust
let args_natural: Vec<Exp> = args.iter().map(strip_all_olds).collect();
// Sites are identified by (function, args, RANGE) ...
sites.push((fun_exp, args_natural, range.clone()));
...
canonical_args.push(fun_exp.clone());
canonical_args.extend(args_natural.iter().cloned());   // <-- stripped args reach the OUTPUT
canonical_args.extend(dests);
canonical_args.extend(post_state_slots);

Stripping old is correct for site identity — it lets the same call in different branches match as one site. It is not correct for the emitted predicate: the leading slots are the callee's pre-state, and a bare &mut temp there denotes the post-state.

Why it went unnoticed

Before #20470, the normal-return path carried assume aborts_of<callee>(..), which contradicts the path condition on that path, making it infeasible and every postcondition on it vacuously provable. That is #20460. #20470 replaced that assume with assert !aborts_of(..) — correctly — and with the contradiction gone the malformed postcondition is checked for the first time and fails.

The complete Boogie diff for the repro below, baseline vs #20470, is 4827 lines with 7 differing, and only one hunk is semantic:

base:  assume  $bp_aborts_of'dec'($t2, $t0);
head:  assert !$bp_aborts_of'dec'($t2, $t0);

The malformed postcondition $bp_ensures_of'dec'($t4, $t0, $t4) is byte-identical on both sides.

The defect is systematic

Five tests in tests/inference/ had already recorded it as expected output, each showing the collapsed-slot signature — the same expression in both the pre and post slot:

test recorded correct
guarded_call ensures_of<dec>(s, d, s) (old(s), d, s)
ref_result_pred ensures_of<peek>(self, result, self) (old(self), result, self)
shadowed_behavior_pred ensures_of<set_value>(s, v, s) (old(s), v, s)
spec_companion ensures_of<increment>(self, self) (old(self), self)
mutations ensures_of<write_to_ref>(p.x, val, p.x) (old(p).x, val, p.x)
mutations ensures_of<replace_ref>(r, 99, result, r) (old(r), 99, result, r)

mutations.exp.move is the clearest: it had

move
ensures [inferred] result == result_of<replace_ref>(old(r), 99);   // old(r) — correct
ensures [inferred] ensures_of<replace_ref>(r, 99, result, r);      // r      — wrong

two clauses about the same call disagreeing on the pre-state.

Repro

move
// flag: --check-inconsistency
module 0x42::bp_mut_return_copy_old_slot {
    struct S has copy, drop, store { v: u64 }

    fun dec(s: &mut S, k: u64) { s.v = s.v - k; }
    spec dec {
        aborts_if s.v < k;
        ensures s.v == old(s.v) - k;
    }

    /// Non-inline HOF taking a real function value, importing the closure's
    /// contract (the shape `big_ordered_map::iter_modify` uses).
    fun apply<R>(s: &mut S, f: |&mut S|R has drop): R { f(s) }
    spec apply {
        pragma opaque;
        pragma verify = false;
        aborts_if aborts_of<f>(s);
        ensures ensures_of<f>(old(s), result, s);
    }

    /// FAILS: closure returns a copy of the parameter it just mutated.
    fun returns_copy(s: &mut S, k: u64): S { apply(s, |x| { dec(x, k); *x }) }

    /// CONTROL, passes: returned value not derived from the mutated parameter.
    fun returns_const(s: &mut S, k: u64): u64 { apply(s, |x| { dec(x, k); k }) }
}

The closure must (a) return the whole mutated value and (b) call a callee with a functional ensures, so an exact post value is routed rather than a fresh symbol. Drop either and the derivation is correct.

Fix

Keep both forms of the arguments — old-stripped for site identity, as-written for emission:

rust
// stripped for identity, as-written for the emitted predicate
let mut sites: Vec<(Exp, Vec<Exp>, Vec<Exp>, MemoryRange)> = Vec::new();
...
sites.push((fun_exp, args_natural, args.to_vec(), range.clone()));

and use the as-written vector for canonical_args and for mk_result_of_at_with_state.

Resulting Boogie: $bp_ensures_of'dec'($t2, $t0, $t4) — pre-state $t2, post-state $t4, matching what the passing control produced all along.

Site identity is unchanged, so no site is merged, split, added or dropped; only the expressions filling the pre-state slots change, and the change can only strengthen a contract.

Measured with the fix: move-prover functional suite 380/380, inference suite 44/44 (the five baselines above regenerated as strict corrections), and a downstream package that previously failed one obligation returns to a full pass with its job count unchanged and its inconsistency check clean.

Version

Present well before 6fa1c24481 (#20470); observable from that commit onwards.