Skip to content

atomic compare_exchange failure #4537

Closed

Description

@bagnalla

A comment in the Kani backend claims the following about compare-and-exchange operations:

In a sequential context, the update is always sucessful so we assume the second value to be true.

Is this true? Consider the program cas.rs:

use std::sync::atomic::{AtomicU8, Ordering};

fn main() {
    let x = AtomicU8::new(0);
    let r = x.compare_exchange(1, 2, Ordering::Relaxed, Ordering::Relaxed);
    println!("{}", r.is_err());
}

#[cfg(kani)]
#[kani::proof]
fn check_is_err() {
    let x = AtomicU8::new(0);
    let r = x.compare_exchange(1, 2, Ordering::Relaxed, Ordering::Relaxed);
    assert!(r.is_err());
}
$ rustc cas.rs && ./cas
true
$ kani cas.rs --harness check_is_err
...

SUMMARY:
 ** 1 of 101 failed (1 unreachable)
Failed Checks: assertion failed: r.is_err()
 File: "cas.rs", line 17, in check_is_err

VERIFICATION:- FAILED

It seems like the claim is true assuming the sequential code is correct. But assuming that the code is correct kind of defeats the purpose of using Kani.

This issue also affects the compare_exchange_weak method.

Activity

  1. added
    [C] BugThis is a bug. Something isn't working.
    on Feb 10, 2026
  2. tautschnig commented on Feb 10, 2026

    @tautschnig
    Member

    Thank you for the detailed report. We'll take a look at this (and also the others that you filed) shortly.

  3. added a commit that references this issue on Feb 11, 2026
    5ca135e
  4. added a commit that references this issue on Mar 4, 2026
    edfa537
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

    [C] BugThis is a bug. Something isn't working.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions