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.
Implement check for
write_bytes
(model-checking#3108)
In the previous PR model-checking#3085, we did not support checks for `write_bytes` which is added in this PR. I am waiting for model-checking#3092 to add expected tests.
- Loading branch information
Showing
3 changed files
with
62 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,21 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// kani-flags: -Z valid-value-checks | ||
//! Check that Kani can identify UB when converting from a maybe uninit(). | ||
use std::mem::MaybeUninit; | ||
use std::num::NonZeroI64; | ||
|
||
#[kani::proof] | ||
pub fn check_valid_zeroed() { | ||
let maybe = MaybeUninit::zeroed(); | ||
let val: u128 = unsafe { maybe.assume_init() }; | ||
assert_eq!(val, 0); | ||
} | ||
|
||
#[kani::proof] | ||
#[kani::should_panic] | ||
pub fn check_invalid_zeroed() { | ||
let maybe = MaybeUninit::zeroed(); | ||
let _val: NonZeroI64 = unsafe { maybe.assume_init() }; | ||
} |
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,21 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// kani-flags: -Z valid-value-checks | ||
//! Check that Kani can identify UB when using write_bytes for initializing a variable. | ||
#![feature(core_intrinsics)] | ||
|
||
#[kani::proof] | ||
#[kani::should_panic] | ||
pub fn check_invalid_write() { | ||
let mut val = 'a'; | ||
let ptr = &mut val as *mut char; | ||
// Should fail given that we wrote invalid value to array of char. | ||
unsafe { std::intrinsics::write_bytes(ptr, kani::any(), 1) }; | ||
} | ||
|
||
#[kani::proof] | ||
pub fn check_valid_write() { | ||
let mut val = 10u128; | ||
let ptr = &mut val as *mut _; | ||
unsafe { std::intrinsics::write_bytes(ptr, kani::any(), 1) }; | ||
} |