forked from rust-lang/rust
-
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.
Mitigate invalid
transmute
when checking memory initialization (rus…
…t-lang#3338) This PR addresses another aspect of rust-lang#3324, where delayed UB could be caused by transmuting a mutable pointer into the one of incompatible padding. It also adds a check to error whenever transmuting between two types of incompatible padding. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
- Loading branch information
1 parent
398729c
commit ff91762
Showing
6 changed files
with
85 additions
and
10 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
14 changes: 14 additions & 0 deletions
14
tests/expected/uninit/delayed-ub-transmute/delayed-ub-transmute.rs
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,14 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// kani-flags: -Z uninit-checks | ||
|
||
/// Checks that Kani rejects mutable pointer casts between types of different padding. | ||
#[kani::proof] | ||
fn invalid_value() { | ||
unsafe { | ||
let mut value: u128 = 0; | ||
let ptr: *mut (u8, u32, u64) = std::mem::transmute(&mut value as *mut _); | ||
*ptr = (4, 4, 4); // This assignment itself does not cause UB... | ||
let c: u128 = value; // ...but this reads a padding value! | ||
} | ||
} |
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,5 @@ | ||
Failed Checks: Kani does not support reasoning about memory initialization in presence of mutable raw pointer casts that could cause delayed UB. | ||
|
||
VERIFICATION:- FAILED | ||
|
||
Complete - 0 successfully verified harnesses, 1 failures, 1 total. |
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,5 @@ | ||
Failed Checks: Transmuting between types of incompatible layouts. | ||
|
||
VERIFICATION:- FAILED | ||
|
||
Complete - 0 successfully verified harnesses, 1 failures, 1 total. |
19 changes: 19 additions & 0 deletions
19
tests/expected/uninit/transmute-padding/transmute_padding.rs
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,19 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
// kani-flags: -Z uninit-checks | ||
|
||
// 5 bytes of data + 3 bytes of padding. | ||
#[repr(C)] | ||
#[derive(kani::Arbitrary)] | ||
struct S(u32, u8); | ||
|
||
/// Checks that Kani catches an attempt to access padding of a struct using transmute. | ||
#[kani::proof] | ||
fn check_uninit_padding() { | ||
let s = kani::any(); | ||
access_padding(s); | ||
} | ||
|
||
fn access_padding(s: S) { | ||
let _padding: u64 = unsafe { std::mem::transmute(s) }; // ~ERROR: padding bytes are uninitialized, so reading them is UB. | ||
} |