generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 129
Fix transmute codegen when sizes are different #3861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,2 @@ | ||
| error[E0512]: cannot transmute between types of different sizes, or dependently-sized types | ||
| error: aborting due to 3 previous errors |
This file contains hidden or 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,33 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| //! Checks that compilation fails when trying to transmute with different src and target sizes. | ||
|
|
||
| #![feature(core_intrinsics)] | ||
| use std::intrinsics::transmute; | ||
|
|
||
| /// This should fail due to UB detection. | ||
| #[kani::proof] | ||
| pub fn transmute_diff_size() { | ||
| let a: u32 = kani::any(); | ||
| if kani::any() { | ||
| let smaller: u16 = unsafe { transmute(a) }; | ||
| std::hint::black_box(smaller); | ||
| } else { | ||
| let bigger: (u64, isize) = unsafe { transmute(a) }; | ||
| std::hint::black_box(bigger); | ||
| } | ||
| } | ||
|
|
||
| /// Generic transmute wrapper. | ||
| pub unsafe fn generic_transmute<S, D>(src: S) -> D { | ||
| transmute(src) | ||
| } | ||
|
|
||
| /// This should also fail due to UB detection. | ||
| #[kani::proof] | ||
| pub fn transmute_wrapper_diff_size() { | ||
| let a: (u32, char) = kani::any(); | ||
| let b: u128 = unsafe { generic_transmute(a) }; | ||
| std::hint::black_box(b); | ||
| } |
24 changes: 24 additions & 0 deletions
24
tests/expected/intrinsics/transmute_unchecked_size.expected
This file contains hidden or 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,24 @@ | ||
| Checking harness transmute_wrapper_diff_size... | ||
| Status: UNREACHABLE\ | ||
| Description: ""Unreachable expected"" | ||
|
|
||
| Failed Checks: Cannot transmute between types of different sizes. Transmuting from `8` to `16` bytes | ||
|
|
||
| VERIFICATION:- FAILED | ||
|
|
||
| Checking harness transmute_diff_size... | ||
|
|
||
| Status: UNREACHABLE\ | ||
| Description: ""This should never be reached"" | ||
|
|
||
| Status: UNREACHABLE\ | ||
| Description: ""Neither this one"" | ||
|
|
||
| Failed Checks: Cannot transmute between types of different sizes. Transmuting from `4` to `2` bytes | ||
| Failed Checks: Cannot transmute between types of different sizes. Transmuting from `4` to `16` bytes | ||
|
|
||
| VERIFICATION:- FAILED | ||
|
|
||
| Verification failed for - transmute_wrapper_diff_size | ||
| Verification failed for - transmute_diff_size | ||
| 0 successfully verified harnesses, 2 failures, 2 total |
This file contains hidden or 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,44 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| //! Checks that Kani correctly identify UB when invoking `transmute_unchecked` with different sizes. | ||
| //! See <https://github.com/model-checking/kani/issues/3839> for more details. | ||
|
|
||
| #![feature(core_intrinsics)] | ||
| use std::intrinsics::transmute_unchecked; | ||
|
|
||
| /// Reachability doesn't seem to work for unreachable statements. | ||
celinval marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| macro_rules! unreachable { | ||
| ($msg:literal) => { | ||
| assert!(false, $msg) | ||
| }; | ||
| } | ||
|
|
||
| /// This should fail due to UB detection. | ||
| #[kani::proof] | ||
| pub fn transmute_diff_size() { | ||
| let a: u32 = kani::any(); | ||
| if kani::any() { | ||
| let smaller: u16 = unsafe { transmute_unchecked(a) }; | ||
| std::hint::black_box(smaller); | ||
| unreachable!("This should never be reached"); | ||
| } else { | ||
| let bigger: (u64, isize) = unsafe { transmute_unchecked(a) }; | ||
| std::hint::black_box(bigger); | ||
| unreachable!("Neither this one"); | ||
| } | ||
| } | ||
|
|
||
| /// Generic transmute wrapper. | ||
| pub unsafe fn generic_transmute<S, D>(src: S) -> D { | ||
| transmute_unchecked(src) | ||
| } | ||
|
|
||
| /// This should also fail due to UB detection. | ||
| #[kani::proof] | ||
| pub fn transmute_wrapper_diff_size() { | ||
| let a: (u32, char) = kani::any(); | ||
| let b: u128 = unsafe { generic_transmute(a) }; | ||
| std::hint::black_box(b); | ||
| unreachable!("Unreachable expected"); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.