forked from model-checking/kani
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetch functions of `atomic_ptr` calls atomic intrinsic functions with pointer-type arguments (`invalid_mut`), which will cause typecheck failures. The change in this commit add support of pointer-type arguments into `codegen_atomic_binop` to fix the issue. The new `codegen_atomic_binop` will cast pointer arguments to `size_t`, apply op on them, and then cast the op result back to the pointer type. The new test include all fetch functions of `atomic_ptr` except for `fetch_ptr_add` and `fetch_ptr_sub`, which do not call intrinsic functions. Resolves model-checking#3042. --------- Co-authored-by: Adrian Palacios <[email protected]> Co-authored-by: Zyad Hassan <[email protected]>
- Loading branch information
1 parent
9b9094d
commit 73b4c47
Showing
2 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// | ||
// Test atomic intrinsics through the stable interface of atomic_ptr. | ||
// Specifically, it checks that Kani correctly handles atomic_ptr's fetch methods, in which the second argument is a pointer type. | ||
// These methods were not correctly handled as explained in https://github.com/model-checking/kani/issues/3042. | ||
|
||
#![feature(strict_provenance_atomic_ptr, strict_provenance)] | ||
use std::sync::atomic::{AtomicPtr, Ordering}; | ||
|
||
#[kani::proof] | ||
fn check_fetch_byte_add() { | ||
let atom = AtomicPtr::<i64>::new(core::ptr::null_mut()); | ||
assert_eq!(atom.fetch_byte_add(1, Ordering::Relaxed).addr(), 0); | ||
// Note: in units of bytes, not `size_of::<i64>()`. | ||
assert_eq!(atom.load(Ordering::Relaxed).addr(), 1); | ||
} | ||
|
||
#[kani::proof] | ||
fn check_fetch_byte_sub() { | ||
let atom = AtomicPtr::<i64>::new(core::ptr::invalid_mut(1)); | ||
assert_eq!(atom.fetch_byte_sub(1, Ordering::Relaxed).addr(), 1); | ||
assert_eq!(atom.load(Ordering::Relaxed).addr(), 0); | ||
} | ||
|
||
#[kani::proof] | ||
fn check_fetch_and() { | ||
let pointer = &mut 3i64 as *mut i64; | ||
// A tagged pointer | ||
let atom = AtomicPtr::<i64>::new(pointer.map_addr(|a| a | 1)); | ||
assert_eq!(atom.fetch_or(1, Ordering::Relaxed).addr() & 1, 1); | ||
// Untag, and extract the previously tagged pointer. | ||
let untagged = atom.fetch_and(!1, Ordering::Relaxed).map_addr(|a| a & !1); | ||
assert_eq!(untagged, pointer); | ||
} | ||
|
||
#[kani::proof] | ||
fn check_fetch_or() { | ||
let pointer = &mut 3i64 as *mut i64; | ||
|
||
let atom = AtomicPtr::<i64>::new(pointer); | ||
// Tag the bottom bit of the pointer. | ||
assert_eq!(atom.fetch_or(1, Ordering::Relaxed).addr() & 1, 0); | ||
// Extract and untag. | ||
let tagged = atom.load(Ordering::Relaxed); | ||
assert_eq!(tagged.addr() & 1, 1); | ||
assert_eq!(tagged.map_addr(|p| p & !1), pointer); | ||
} | ||
|
||
#[kani::proof] | ||
fn check_fetch_update() { | ||
let ptr: *mut _ = &mut 5; | ||
let some_ptr = AtomicPtr::new(ptr); | ||
|
||
let new: *mut _ = &mut 10; | ||
assert_eq!(some_ptr.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(ptr)); | ||
let result = some_ptr.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| { | ||
if x == ptr { Some(new) } else { None } | ||
}); | ||
assert_eq!(result, Ok(ptr)); | ||
assert_eq!(some_ptr.load(Ordering::SeqCst), new); | ||
} | ||
|
||
#[kani::proof] | ||
fn check_fetch_xor() { | ||
let pointer = &mut 3i64 as *mut i64; | ||
let atom = AtomicPtr::<i64>::new(pointer); | ||
|
||
// Toggle a tag bit on the pointer. | ||
atom.fetch_xor(1, Ordering::Relaxed); | ||
assert_eq!(atom.load(Ordering::Relaxed).addr() & 1, 1); | ||
} |