#20591·aptos-core

[prover] loop havoc misses &mut arguments passed through a function value

Author: GotenJBZCreated Sep 22, 2026Updated Sep 22, 2026
Labelsbug

Bug

A loop must forget any value that the loop body can change. The prover asks each instruction what it changes. For Operation::Invoke the answer only lists the explicit results. A &mut argument is left out. So the value behind that reference is not forgotten at the top of the loop. The prover keeps the value it had before the loop.

apply_in_loop writes 0 through x. Then it calls set_one through a function value inside a loop. That call writes 1 through x. So the function returns 1. The prover accepts ensures result == 0.

To reproduce

  1. Save the snippet as third_party/move/move-prover/tests/sources/regression/unsoundness_showcase/03_loop_invoke_mutref.move.
  2. Create an empty .exp file next to it with the same base name.
  3. Run cargo test -p move-prover --test testsuite 03_loop_invoke_mutref -- --test-threads=1.

The test passes. That means the prover found nothing for either false postcondition.

Code snippet to reproduce

move
module 0x42::loops_invoke_mutref {
    fun set_one(x: &mut u64) {
        *x = 1;
    }

    fun set_two_and_return(x: &mut u64): u64 {
        *x = 2;
        7
    }

    public fun apply_in_loop(x: &mut u64): u64 {
        *x = 0;
        let f: |&mut u64| has copy + drop = set_one;
        let i = 0;
        while (i < 1) {
            f(x);
            i += 1;
        };
        *x
    }

    spec apply_in_loop {
        ensures result == 0;
    }

    public fun invoke_with_destination_in_loop(x: &mut u64): u64 {
        *x = 0;
        let f: |&mut u64| u64 has copy + drop = set_two_and_return;
        let i = 0;
        while (i < 1) {
            let _call_result = f(x);
            i += 1;
        };
        *x
    }

    spec invoke_with_destination_in_loop {
        ensures result == 0;
    }

    public fun invoke_with_destination_control(x: &mut u64): u64 {
        let f: |&mut u64| u64 has copy + drop = set_two_and_return;
        f(x)
    }

    spec invoke_with_destination_control {
        ensures result == 7;
    }

    #[test]
    fun test_apply_in_loop() {
        let x = 0;
        let result = apply_in_loop(&mut x);
        assert_eq!(result, x);
        assert_eq!(x, 1);

        let result_with_destination = invoke_with_destination_in_loop(&mut x);
        assert_eq!(result_with_destination, x);
        assert_eq!(x, 2);

        let call_result = invoke_with_destination_control(&mut x);
        assert_eq!(call_result, 7);
        assert_eq!(x, 2);
    }
}

Expected Behavior

The prover should reject both loop postconditions:

error: post-condition does not hold
  03_loop_invoke_mutref.move
        ensures result == 0;

error: post-condition does not hold
  03_loop_invoke_mutref.move
        ensures result == 0;

invoke_with_destination_control is a good case. It must still pass. It shows that the return value of a call through a function value is still handled right.

You do not need to pick any input. The function writes 0 to x. The loop test 0 < 1 is true. f is set_one. The call writes 1 to x. The counter goes to 1. The loop stops. The function returns 1. The Move unit test in the snippet shows the same numbers.

invoke_with_destination_in_loop is the same problem when the call also returns a value. It writes 2 through x, so it cannot return 0.

Additional context

f(x) becomes Operation::Invoke. x is a source operand. There is no explicit result. See third_party/move/move-model/bytecode/src/stackless_bytecode_generator.rs:1828-1850.

The backend does apply the write through the &mut source. See third_party/move/move-prover/boogie-backend/src/bytecode_translator.rs:7133-7157.

The loop code calls Bytecode::modifies on each instruction. See third_party/move/move-model/bytecode/src/fat_loop.rs:477-515.

Bytecode::modifies lists &mut sources for normal calls. The fallback arm used for Invoke looks at explicit results only. So x is missing from the list for the loop. See third_party/move/move-model/bytecode/src/stackless_bytecode.rs:979-1002.

The loop pass uses that list when it adds the forget step at the top of the loop. See third_party/move/move-prover/bytecode-pipeline/src/loop_analysis.rs:564-605.