[prover] global &mut references alias across different resource types
Bug
Two &mut references into global storage look like the same reference, even
when they point into two different resource types. The prover names the root of
a global borrow $Global(addr). That name keeps the address. It drops the
resource type. A field borrow only adds the field number. So A[addr].x and
B[addr].x both get the name (Global(addr), [0]). A write through one goes
into the other one too.
Move keeps resources apart by type and by address. So the field that
write_zero_and_return_other returns was never written. The prover still
accepts ensures result == 0.
To reproduce
- Save the snippet as
third_party/move/move-prover/tests/sources/regression/unsoundness_showcase/04_global_ref_type_alias.move. - Create an empty
.expfile next to it with the same base name. - Run
cargo test -p move-prover --test testsuite 04_global_ref_type_alias -- --test-threads=1.
The test passes. That means the prover found nothing.
Code snippet to reproduce
module 0x42::RefGlobalTypeAlias {
use std::signer;
struct A has key {
x: u64
}
struct B has key {
x: u64
}
public fun set_x(user: &signer, x: u64, y: u64) {
let addr = signer::address_of(user);
if (!exists<A>(addr)) {
move_to(user, A { x: 0 });
}
if (!exists<B>(addr)) {
move_to(user, B { x: 0 });
}
A[addr].x = x;
B[addr].x = y;
}
public fun write_zero_and_return_other(choose_a: bool, addr: address): u64 {
let a = &mut A[addr].x;
let b = &mut B[addr].x;
let selected = if (choose_a) a else b;
*selected = 0;
if (choose_a) B[addr].x else A[addr].x
}
spec write_zero_and_return_other {
requires exists<A>(addr) && exists<B>(addr);
ensures result == 0;
}
#[test(user = @0x42)]
public fun test_write_zero_and_return_other(user: &signer) {
set_x(user, 1, 2);
assert_eq!(write_zero_and_return_other(true, signer::address_of(user)), 2);
}
}Expected Behavior
The prover should reject the postcondition:
error: post-condition does not hold
04_global_ref_type_alias.move
ensures result == 0;A write to a field of A cannot change a field of B at the same address.
Put A and B at addr, with A.x = 1 and B.x = 2. If choose_a is true,
the function writes 0 into A.x and returns B.x. So it returns 2. If
choose_a is false, it writes 0 into B.x and returns A.x. So it returns
1. Both runs break result == 0. The requires makes both borrows work.
Additional context
BorrowGlobal makes a reference whose root is $Global(addr). See
third_party/move/move-prover/boogie-backend/src/bytecode_translator.rs:7377-7394.
$Global keeps the address and not the resource type. See
third_party/move/move-prover/boogie-backend/src/prelude/prelude.bpl:466-469.
$ChildMutation adds the field number only. $IsSameMutation compares the
place and the field path only. See
third_party/move/move-prover/boogie-backend/src/prelude/prelude.bpl:568-580.
When the write through selected is copied back, the prover updates both
memories, one per type. So the other field also looks like 0.
Source: aptos-labs/aptos-core