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.
Move
volatile_store
and ensure alignment (model-checking#794)
* Move `volatile_store` and ensure alignment * Create util function for concurrency warnings * Add positive and negative test cases * Emit concurrency warning with macro
- Loading branch information
1 parent
30f8290
commit 46b14ce
Showing
5 changed files
with
111 additions
and
21 deletions.
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
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,27 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
// Check that `volatile_store` passes when it writes to an aligned value. | ||
// This example is similar to the one that appears in the documentation for | ||
// `write_unaligned`: | ||
// https://doc.rust-lang.org/std/ptr/fn.write_unaligned.html | ||
#![feature(core_intrinsics)] | ||
|
||
// In contrast to the `Packed` struct in `store_fail.rs`, this struct includes | ||
// padding so that each field is aligned. | ||
struct NonPacked { | ||
_padding: u8, | ||
unaligned: u32, | ||
} | ||
|
||
fn main() { | ||
let mut packed: NonPacked = unsafe { std::mem::zeroed() }; | ||
// Take the address of a 32-bit integer which is not aligned. | ||
// In contrast to `&packed.unaligned as *mut _`, this has no undefined behavior. | ||
let unaligned = std::ptr::addr_of_mut!(packed.unaligned); | ||
|
||
// Store the value with `volatile_store`. | ||
// This includes an alignment check for `unaligned` which should pass. | ||
unsafe { std::intrinsics::volatile_store(unaligned, 42) }; | ||
assert!(packed.unaligned == 42); | ||
} |
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,29 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// kani-verify-fail | ||
|
||
// Check that `volatile_store` fails when it writes to an unaligned value. | ||
// This example is similar to the one that appears in the documentation for | ||
// `write_unaligned`: | ||
// https://doc.rust-lang.org/std/ptr/fn.write_unaligned.html | ||
#![feature(core_intrinsics)] | ||
|
||
// `repr(packed)` forces the struct to be stripped of any padding and only align | ||
// its fields to a byte. | ||
#[repr(packed)] | ||
struct Packed { | ||
_padding: u8, | ||
unaligned: u32, | ||
} | ||
|
||
fn main() { | ||
let mut packed: Packed = unsafe { std::mem::zeroed() }; | ||
// Take the address of a 32-bit integer which is not aligned. | ||
// In contrast to `&packed.unaligned as *mut _`, this has no undefined behavior. | ||
let unaligned = std::ptr::addr_of_mut!(packed.unaligned); | ||
|
||
// Store the value with `volatile_store`. | ||
// This includes an alignment check for `unaligned` which should fail. | ||
unsafe { std::intrinsics::volatile_store(unaligned, 42) }; | ||
assert!(packed.unaligned == 42); | ||
} |