Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
628 changes: 628 additions & 0 deletions library/core/src/intrinsics/mod.rs

Large diffs are not rendered by default.

1,625 changes: 1,625 additions & 0 deletions library/core/src/sync/atomic.rs

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions verifast-proofs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ Specifically, it currently contains the following proofs:

- Partial proof of [LinkedList](alloc/collections/linked_list.rs/)
- Partial proof of [RawVec](alloc/raw_vec/mod.rs/)
- Challenge 7 [atomic storage, operations, and concurrent clients](core/sync/atomic.rs/)

See each proof's accompanying README for a tour of the proof and applicable caveats.

## Maintaining the proofs

If the VeriFast CI action fails because of a failing diff, please run `cd verifast-proofs; ./patch-verifast-proofs.sh` to fix the problem.
For a LinkedList or RawVec snapshot mismatch, run `bash verifast-proofs/patch-verifast-proofs.sh` from the repository root. For atomic sources, run `bash verifast-proofs/core/sync/atomic.rs/update.sh` and review the annotation diffs against the new sources.

## `-skip_specless_fns`

Expand All @@ -23,7 +24,7 @@ To verify a function in a file <code>library/<i>crate</i>/src/<i>mod</i>/<i>file
1. Copy that file to <code>verifast-proofs/<i>crate</i>/<i>mod</i>/<i>file</i>.rs/original/<i>file</i>.rs</code> as well as to <code>verifast-proofs/<i>crate</i>/<i>mod</i>/<i>file</i>.rs/verified/<i>file</i>.rs</code>.
2. Create a file <code>verifast-proofs/<i>crate</i>/<i>mod</i>/<i>file</i>.rs/original/lib.rs</code> to serve as crate root for verification, and include <code><b>mod</b> <i>file</i>;</code>. (See the existing proofs for examples.) Copy it to <code>verifast-proofs/<i>crate</i>/<i>mod</i>/<i>file</i>.rs/verified/lib.rs</code>.
2. Add a VeriFast specification to the function(s) you want to verify, and any other VeriFast annotations to make the proof go through, in <code>verifast-proofs/<i>crate</i>/<i>mod</i>/<i>file</i>.rs/verified/<i>file</i>.rs</code>. While doing so, you will need to change the code slightly so as to be able to insert ghost commands in the correct places.
3. Add commands for checking your proof to `verifast-proofs/check-verifast-proofs.mysh`. This includes the following:
3. Add commands for checking your proof to `verifast-proofs/check-verifast-proofs.sh`. This includes the following:
1. A `verifast` invocation for checking the VeriFast proof.
2. A `refinement-checker` invocation for checking that the code changes you made in the verified version do not change the meaning of the program. Specifically, this tool checks that the original code *refines* the verified code, i.e. that each behavior of a function in the original version is also a behavior of the corresponding function in the verified version. As a result, if the verified version has been verified to have no bad behaviors, the original version also has no bad behaviors.
3. A `diff` invocation for checking that the version in `original` is identical to the original version in `library`.
Expand Down Expand Up @@ -56,4 +57,4 @@ We then:

Separating steps 2) and 3) lets us distinguish between CI failures caused by incorrect modifications to the verified files (failure in step 2) versus a proof that was once correct, but is just out of date (failure in step 3). Without the copy of the original file, when the `library/` files change, there would be no easy way to tell whether the VeriFast proofs are wrong or just stale.

The [VeriFast](../.github/workflows/verifast.yml) GitHub action will run `verifast-proofs/check-verifast-proofs.mysh`. Check that file to see which version of VeriFast is used.
The [VeriFast](../.github/workflows/verifast.yml) GitHub action will run `verifast-proofs/check-verifast-proofs.sh`. Check that file to see which version of VeriFast is used.
2 changes: 2 additions & 0 deletions verifast-proofs/check-verifast-proofs-negative.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ cd alloc
cd ..
cd ..
cd ..

bash core/sync/atomic.rs-negative/verify.sh
2 changes: 2 additions & 0 deletions verifast-proofs/check-verifast-proofs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ cd alloc
cd ..
cd ..
cd ..

bash core/sync/atomic.rs/verify.sh
1,588 changes: 1,588 additions & 0 deletions verifast-proofs/core/sync/atomic.rs-negative/verify.sh

Large diffs are not rendered by default.

173 changes: 173 additions & 0 deletions verifast-proofs/core/sync/atomic.rs/README.md

Large diffs are not rendered by default.

115 changes: 115 additions & 0 deletions verifast-proofs/core/sync/atomic.rs/TRUSTED-BASE.md

Large diffs are not rendered by default.

122 changes: 122 additions & 0 deletions verifast-proofs/core/sync/atomic.rs/build-linux-tools.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/usr/bin/env bash
# Print the tool directory on stdout. Build messages go to stderr.
set -euo pipefail
here=$(cd "$(dirname "$0")" && pwd)
tc=nightly-2026-11-21
base=5b7d6f529e673f0f59aa9c11d7e95a70a6fa6b50
decoder=2d6606d9b59cd0c88a66729f3f076c10c0c8e0b2
deps=/tmp/vfdeps-adf88dc
test "$(uname -sm)" = 'Linux x86_64'
valid_tools() {
local tool
for tool in verifast refinement-checker vf-rust-mir-exporter; do
[[ -x $1/bin/$tool ]] || return 1
done
[[ -f $1/bin/rust/std/lib.rsspec && -f $1/tests/rust/unverified/platform/spec/lib.rsspec ]]
}

if ! rustup run "$tc" rustc --version >/dev/null 2>&1; then
rustup toolchain install "$tc" --profile minimal >&2
fi
installed=$(rustup component list --toolchain "$tc" --installed)
for component in rustc-dev rust-src llvm-tools-preview; do
if ! grep -Eq "^${component%-preview}(-x86_64-unknown-linux-gnu)?$" <<< "$installed"; then
rustup component add --toolchain "$tc" "$component" >&2
fi
done
if ! rustup target list --toolchain "$tc" --installed | grep -Fxq aarch64-unknown-linux-gnu; then
rustup target add --toolchain "$tc" aarch64-unknown-linux-gnu >&2
fi
key=$(cd "$here" && sha256sum verifast-core.patch build-linux-tools.sh platform.Cargo.lock | sha256sum | cut -d' ' -f1)
if [[ -n ${ATOMIC_VERIFAST_DIR:-} ]]; then
if [[ ! -f $ATOMIC_VERIFAST_DIR/complete || $(<"$ATOMIC_VERIFAST_DIR/complete") != "$key" ]] || ! valid_tools "$ATOMIC_VERIFAST_DIR"; then
echo "Incomplete or stale ATOMIC_VERIFAST_DIR (expected build key $key): $ATOMIC_VERIFAST_DIR" >&2
exit 1
fi
realpath "$ATOMIC_VERIFAST_DIR"
exit
fi

cache=${ATOMIC_TOOL_CACHE:-${RUNNER_TEMP:-$HOME/.verifast}/atomic-verifast-5b7d6f52}
mkdir -p "$cache"
cache=$(realpath "$cache")
dest=$cache/$key
exec 9>"$cache/build.lock"
flock 9
if [[ -f $dest/complete && $(<"$dest/complete") == "$key" ]] && valid_tools "$dest"; then
echo "$dest"
exit
fi
stage=$(mktemp -d "$cache/.build.XXXXXXXX")
deps_tmp=
cleanup() {
rm -rf -- "$stage"
if [[ -n $deps_tmp ]]; then rm -rf -- "$deps_tmp"; fi
}
trap cleanup EXIT
fetch() {
curl --fail --location --silent --show-error --retry 3 --proto '=https' "$1" -o "$stage/download"
printf '%s %s\n' "$2" "$stage/download" | sha256sum -c >&2
}
mkdir "$stage/vf" "$stage/decoder" "$stage/commands"
fetch "https://codeload.github.com/verifast/verifast/tar.gz/$base" 8995b3cb008c9110c14284702f99cec91426c7547067c7c08fbab5f9b93eb0b1
tar xf "$stage/download" --strip-components=1 -C "$stage/vf"
fetch "https://codeload.github.com/btj/capnpc-ocaml-decoder/tar.gz/$decoder" 29c8025b7fbf96213db5a07fef707e214a4f7547b84a89faf9816cd0ad117b59
tar xf "$stage/download" --strip-components=1 -C "$stage/decoder"
# The prebuilt OCaml tools use this absolute prefix internally.
deps_sha=8d022c93d51a1d13ec1e782d767c60462405f6865d5ee416f82d6234e93ee580
# The prefix is shared even by builds using different ATOMIC_TOOL_CACHE values.
# Check payload names, modes, symlinks and bytes against the verified release.
tree_sha=71c349ae1e2d471c404cce9880de25b73766bec3742e2de033a5c402402c6d10
marker=.atomic-vfdeps-sha256
verify_deps() {
local digest
[[ -d $1 && ! -L $1 ]] || return 1
digest=$(LC_ALL=C tar --sort=name --mtime=@0 --owner=0 --group=0 --numeric-owner \
--format=gnu --exclude="./$marker" -cf - -C "$1" . | sha256sum)
[[ ${digest%% *} == "$tree_sha" ]]
}
exec 8>>"$deps.lock"
flock 8
if [[ ! -e $deps && ! -L $deps ]]; then
fetch https://github.com/verifast/vfdeps/releases/download/25.01/vfdeps-adf88dc-linux.txz "$deps_sha"
deps_tmp=$(mktemp -d /tmp/atomic-vfdeps.XXXXXXXX)
tar xpf "$stage/download" -C "$deps_tmp"
extracted=$deps_tmp/vfdeps-adf88dc
verify_deps "$extracted" || { echo 'Invalid extracted vfdeps payload' >&2; exit 1; }
printf '%s\n' "$deps_sha" > "$extracted/$marker"
mv -T "$extracted" "$deps"
else
verify_deps "$deps" || { echo "Invalid vfdeps payload in $deps" >&2; exit 1; }
if [[ ! -e $deps/$marker && ! -L $deps/$marker ]]; then
# Adopt an existing verified installation without replacing it.
printf '%s\n' "$deps_sha" > "$deps/$marker"
fi
[[ -f $deps/$marker && ! -L $deps/$marker && $(<"$deps/$marker") == "$deps_sha" ]] ||
{ echo "Invalid vfdeps marker in $deps" >&2; exit 1; }
fi
flock -u 8
export GIT_CEILING_DIRECTORIES="$stage"
git -C "$stage/vf" apply --check "$here/verifast-core.patch"
git -C "$stage/vf" apply --whitespace=nowarn "$here/verifast-core.patch"
export PATH="$stage/commands:$deps/bin:$PATH" CAPNP_INC_DIR="$deps/include"
export LD_LIBRARY_PATH="$deps/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
export CARGO_BUILD_JOBS=1 CARGO_INCREMENTAL=0 DUNE_CACHE=disabled
export RAYON_NUM_THREADS=1 MALLOC_ARENA_MAX=2
ulimit -S -v 8388608
printf '#!/bin/sh\nexec %s/bin/dune "$@" -j 1\n' "$deps" > "$stage/commands/dune"
chmod +x "$stage/commands/dune"
cargo "+$tc" build --release --locked -j1 --manifest-path "$stage/decoder/Cargo.toml" >&2
export PATH="$stage/decoder/target/release:$PATH"
cargo "+$tc" build --release --locked -j1 --manifest-path "$stage/vf/src/rust_frontend/vf_mir_exporter/Cargo.toml" --target-dir "$stage/exporter" >&2
cp "$stage/exporter/release/vf_mir_exporter" "$stage/vf/bin/vf-rust-mir-exporter"
make -C "$stage/vf/src" -j1 VFVERSION="atomic-${base:0:8}" WITHOUT_LABLGTK=1 ../bin/verifast ../bin/refinement-checker >&2
mkdir -p "$stage/install/tests/rust/unverified"
cp -a "$stage/vf/bin" "$stage/install/bin"
cp -a "$stage/vf/tests/rust/unverified/platform" "$stage/install/tests/rust/unverified/"
cp "$here/platform.Cargo.lock" "$stage/install/tests/rust/unverified/platform/Cargo.lock"
valid_tools "$stage/install"
echo "$key" > "$stage/install/complete"
if [[ -e $dest ]]; then mv -T "$dest" "$stage/incomplete"; fi
mv -T "$stage/install" "$dest"
echo "$dest"
11 changes: 11 additions & 0 deletions verifast-proofs/core/sync/atomic.rs/check-sources.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
src=../../../../library/core/src
for path in lib.rs sync/atomic.rs sync/atomic_load_volatile.md sync/atomic_store_volatile.md intrinsics/mod.rs; do
snapshot=original/${path#sync/}
if ! cmp "$snapshot" "$src/$path"; then
echo "::error title=VeriFast source snapshot is out of date::Run verifast-proofs/core/sync/atomic.rs/update.sh and review the proof annotations." >&2
exit 1
fi
done
67 changes: 67 additions & 0 deletions verifast-proofs/core/sync/atomic.rs/checks/bool-access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// This module also contains the checked ordinary_bool and prepare_bool lemmas.
unsafe fn borrowed_bool_access(initial: bool, replacement: bool)
//@ req true;
//@ ens true;
//@ on_unwind_ens false;
{
use crate::sync::atomic::Ordering;
let mut value = initial;
let slot = &raw mut value;
//@ to_u8s_(slot);
//@ from_u8s_(slot);
//@ let k = begin_lifetime();
//@ close ordinary_bool(slot)();
//@ borrow(k, ordinary_bool(slot));
//@ prepare_bool(slot, k);
{
//@ let_lft 'a = k;
let view = unsafe { AtomicBool::from_ptr/*@::<'a>@*/(slot) };
//@ close sync::atomic::Ordering_own(currentThread, sync::atomic::Ordering::Relaxed);
view.store(replacement, Ordering::Relaxed);
//@ close sync::atomic::Ordering_own(currentThread, sync::atomic::Ordering::Acquire);
let _ = view.load(Ordering::Acquire);
}
//@ end_lifetime(k);
//@ borrow_end(k, ordinary_bool(slot));
//@ open ordinary_bool(slot)();
let recovered = unsafe { *slot };
//@ points_to_bool_to_u8(slot);
let byte = unsafe { *slot.cast::<u8>() };
//@ if recovered {} else {}
//@ assert byte == if recovered { 1 } else { 0 };
//@ points_to_u8_to_bool(slot as *u8);
unsafe { *slot = replacement };
}
unsafe fn shared_bool_view<'a>(ptr: *mut bool) -> &'a AtomicBool
//@ req [?q]lifetime_token('a) &*& [_]frac_borrow('a, sync::atomic::bool_slot_content(ptr)) &*& ref_origin(ptr) == ptr;
//@ ens [q]lifetime_token('a) &*& [_]frac_borrow('a, sync::atomic::bool_slot_content(ref_origin(result))) &*& [_]frac_borrow('a, ref_initialized_(result)) &*& ref_origin(result) as pointer == ptr as pointer;
//@ on_unwind_ens false;
{
let result = unsafe { AtomicBool::from_ptr/*@::<'a>@*/(ptr) };
//@ open sync::atomic::Atomic_share::<bool>('a, currentThread, result);
result
}

unsafe fn shared_bool_store(receiver: &AtomicBool, val: bool, order: crate::sync::atomic::Ordering)
//@ req [?q]lifetime_token(?k) &*& [_]frac_borrow(k, sync::atomic::bool_slot_content(ref_origin(receiver))) &*& [_]frac_borrow(k, ref_initialized_(receiver)) &*& order != sync::atomic::Ordering::Acquire &*& order != sync::atomic::Ordering::AcqRel;
//@ ens [q]lifetime_token(k);
//@ on_unwind_ens false;
{
//@ close exists(false);
//@ close sync::atomic::Atomic_share::<bool>(k, currentThread, receiver);
//@ leak sync::atomic::Atomic_share::<bool>(k, currentThread, receiver);
//@ close sync::atomic::Ordering_own(currentThread, order);
receiver.store(val, order)
}

unsafe fn shared_bool_load(receiver: &AtomicBool, order: crate::sync::atomic::Ordering) -> bool
//@ req [?q]lifetime_token(?k) &*& [_]frac_borrow(k, sync::atomic::bool_slot_content(ref_origin(receiver))) &*& [_]frac_borrow(k, ref_initialized_(receiver)) &*& order != sync::atomic::Ordering::Release &*& order != sync::atomic::Ordering::AcqRel;
//@ ens [q]lifetime_token(k);
//@ on_unwind_ens false;
{
//@ close exists(false);
//@ close sync::atomic::Atomic_share::<bool>(k, currentThread, receiver);
//@ leak sync::atomic::Atomic_share::<bool>(k, currentThread, receiver);
//@ close sync::atomic::Ordering_own(currentThread, order);
receiver.load(order)
}
89 changes: 89 additions & 0 deletions verifast-proofs/core/sync/atomic.rs/checks/bool-rmw.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// The enclosing proof module supplies ordinary_bool, prepare_bool and AtomicBool.
unsafe fn borrowed_bool_rmw(initial: bool, operand: bool, order: crate::sync::atomic::Ordering) -> (bool, u8)
//@ req true;
//@ ens result.1 == if result.0 { 1 } else { 0 };
//@ on_unwind_ens false;
{
let mut value = initial;
let slot = &raw mut value;
let byte = slot.cast::<u8>();
//@ to_u8s_(slot);
//@ from_u8s_(slot);
//@ let k = begin_lifetime();
//@ close ordinary_bool(slot)();
//@ borrow(k, ordinary_bool(slot));
//@ prepare_bool(slot, k);
{
//@ let_lft 'a = k;
let view = unsafe { AtomicBool::from_ptr/*@::<'a>@*/(slot) };
//@ close sync::atomic::Ordering_own(currentThread, order);
let old = view.swap(operand, order);
//@ close sync::atomic::Ordering_own(currentThread, order);
let previous = view.fetch_and(old, order);
//@ close sync::atomic::Ordering_own(currentThread, order);
let previous = view.fetch_or(previous, order);
//@ close sync::atomic::Ordering_own(currentThread, order);
let previous = view.fetch_xor(previous, order);
//@ close sync::atomic::Ordering_own(currentThread, order);
let _ = view.fetch_nand(previous, order);
//@ close sync::atomic::Ordering_own(currentThread, order);
let _ = view.fetch_not(order);
}
//@ end_lifetime(k);
//@ borrow_end(k, ordinary_bool(slot));
//@ open ordinary_bool(slot)();
let recovered = unsafe { *slot };
//@ points_to_bool_to_u8(slot);
let encoded = unsafe { *byte };
//@ if recovered {} else {}
//@ points_to_u8_to_bool(byte);
(recovered, encoded)
}

macro_rules! byte_row {
($old:expr, $arg:expr) => {{
const AND: u8 = $old & $arg;
const OR: u8 = $old | $arg;
const XOR: u8 = $old ^ $arg;
const NAND: u8 = !($old & $arg);
(AND, OR, XOR, NAND)
}};
}

// Each value comes from the fixed Rust compiler's byte arithmetic.
unsafe fn byte_results(old: u8, arg: u8) -> (u8, u8, u8, u8)
//@ req sync::atomic::valid_bool_byte(old) == true &*& sync::atomic::valid_bool_byte(arg) == true;
//@ ens result.0 == sync::atomic::bool_byte_after(sync::atomic::byte_and, old, arg) &*& result.1 == sync::atomic::bool_byte_after(sync::atomic::byte_or, old, arg) &*& result.2 == sync::atomic::bool_byte_after(sync::atomic::byte_xor, old, arg) &*& result.3 == sync::atomic::bool_byte_after(sync::atomic::byte_nand, old, arg);
//@ on_unwind_ens false;
{
if old == 0 {
if arg == 0 { byte_row!(0u8, 0u8) } else { byte_row!(0u8, 1u8) }
} else if arg == 0 {
byte_row!(1u8, 0u8)
} else {
byte_row!(1u8, 1u8)
}
}
unsafe fn shared_bool_nand(receiver: &AtomicBool, val: bool, order: crate::sync::atomic::Ordering) -> bool
//@ req [?q]lifetime_token(?k) &*& [_]frac_borrow(k, sync::atomic::bool_slot_content(ref_origin(receiver))) &*& [_]frac_borrow(k, ref_initialized_(receiver));
//@ ens [q]lifetime_token(k);
//@ on_unwind_ens false;
{
//@ close exists(false);
//@ close sync::atomic::Atomic_share::<bool>(k, currentThread, receiver);
//@ leak sync::atomic::Atomic_share::<bool>(k, currentThread, receiver);
//@ close sync::atomic::Ordering_own(currentThread, order);
receiver.fetch_nand(val, order)
}

unsafe fn shared_bool_and(receiver: &AtomicBool, val: bool, order: crate::sync::atomic::Ordering) -> bool
//@ req [?q]lifetime_token(?k) &*& [_]frac_borrow(k, sync::atomic::bool_slot_content(ref_origin(receiver))) &*& [_]frac_borrow(k, ref_initialized_(receiver));
//@ ens [q]lifetime_token(k);
//@ on_unwind_ens false;
{
//@ close exists(false);
//@ close sync::atomic::Atomic_share::<bool>(k, currentThread, receiver);
//@ leak sync::atomic::Atomic_share::<bool>(k, currentThread, receiver);
//@ close sync::atomic::Ordering_own(currentThread, order);
receiver.fetch_and(val, order)
}
45 changes: 45 additions & 0 deletions verifast-proofs/core/sync/atomic.rs/checks/bool-storage.gh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*@
pred_ctor ordinary_bool(slot: *bool)(;) = *slot |-> ?value;

lem prepare_bool(slot: *bool, k: lifetime_t)
nonghost_callers_only
req [?q]lifetime_token(k) &*& full_borrow(k, ordinary_bool(slot));
ens [q]lifetime_token(k) &*& [_]frac_borrow(k, sync::atomic::bool_slot_content(slot));
{
let long = open_full_borrow_strong(k, ordinary_bool(slot), q);
open ordinary_bool(slot)();
{
pred Pre() = *slot |-> ?value;
pred Post() = sync::atomic::bool_slot_content(slot)();
close Pre();
produce_lem_ptr_chunk atomic_block(Pre, Post)() {
open Pre();
assert *slot |-> ?value;
points_to_limits(slot);
points_to_bool_to_u8(slot);
if value {} else {}
std::intrinsics::atomic_align_of_u8();
div_rem_nonneg(slot as usize, 1);
std::intrinsics::close_atomic_points_to_m(slot as *u8, sync::atomic::valid_bool_byte);
close sync::atomic::bool_slot_content(slot)();
close Post();
} { perform_atomically(); }
open Post();
}
{
pred Ctx() = true;
produce_lem_ptr_chunk full_borrow_convert_strong(Ctx, sync::atomic::bool_slot_content(slot), long, ordinary_bool(slot))() {
open Ctx();
open sync::atomic::bool_slot_content(slot)();
std::intrinsics::open_atomic_points_to(slot as *u8);
points_to_u8_to_bool(slot as *u8);
close ordinary_bool(slot)();
} {
close Ctx();
close_full_borrow_strong(long, ordinary_bool(slot), sync::atomic::bool_slot_content(slot));
full_borrow_mono(long, k, sync::atomic::bool_slot_content(slot));
}
}
full_borrow_into_frac(k, sync::atomic::bool_slot_content(slot));
}
@*/
Loading
Loading