generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 142
Basic tests for size_of_val and min_alig_of_val
#1101
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
adpaco-aws
merged 5 commits into
model-checking:main
from
adpaco-aws:size-align-val-tests
Apr 27, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a84b84f
Basic tests for `size_of_val` and `min_alig_of_val`
adpaco-aws cbee7b6
Fix format
adpaco-aws 26d979b
Add cases for `repr(C)` struct
adpaco-aws dd2f755
Add note
adpaco-aws 4229e8b
Merge branch 'main' into size-align-val-tests
adpaco-aws 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // Check that we get the expected results for the `min_align_of_val` intrinsic | ||
| // with common data types. Note that these tests assume an x86_64 architecture, | ||
| // which is the only architecture supported by Kani at the moment. | ||
| #![feature(core_intrinsics)] | ||
| use std::intrinsics::min_align_of_val; | ||
|
|
||
| struct MyStruct { | ||
| val: u32, | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| struct CStruct { | ||
| a: u8, | ||
| b: i32, | ||
| } | ||
|
|
||
| enum MyEnum { | ||
| Variant, | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn main() { | ||
| unsafe { | ||
| // Scalar types | ||
| assert!(min_align_of_val(&0i8) == 1); | ||
| assert!(min_align_of_val(&0i16) == 2); | ||
| assert!(min_align_of_val(&0i32) == 4); | ||
| assert!(min_align_of_val(&0i64) == 8); | ||
| assert!(min_align_of_val(&0i128) == 8); | ||
| assert!(min_align_of_val(&0isize) == 8); | ||
| assert!(min_align_of_val(&0u8) == 1); | ||
| assert!(min_align_of_val(&0u16) == 2); | ||
| assert!(min_align_of_val(&0u32) == 4); | ||
| assert!(min_align_of_val(&0u64) == 8); | ||
| assert!(min_align_of_val(&0u128) == 8); | ||
| assert!(min_align_of_val(&0usize) == 8); | ||
| assert!(min_align_of_val(&0f32) == 4); | ||
| assert!(min_align_of_val(&0f64) == 8); | ||
| assert!(min_align_of_val(&false) == 1); | ||
| assert!(min_align_of_val(&(0 as char)) == 4); | ||
| // Compound types (tuple and array) | ||
| assert!(min_align_of_val(&(0i32, 0i32)) == 4); | ||
| assert!(min_align_of_val(&[0i32; 5]) == 4); | ||
| // Custom data types (struct and enum) | ||
| assert!(min_align_of_val(&MyStruct { val: 0u32 }) == 4); | ||
| assert!(min_align_of_val(&MyEnum::Variant) == 1); | ||
| assert!(min_align_of_val(&CStruct { a: 0u8, b: 0i32 }) == 4); | ||
| } | ||
| } |
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,52 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // Check that we get the expected results for the `size_of_val` intrinsic with | ||
| // common data types. Note that these tests assume an x86_64 architecture, which | ||
| // is the only architecture supported by Kani at the moment. | ||
| #![feature(core_intrinsics)] | ||
| use std::intrinsics::size_of_val; | ||
|
|
||
| struct MyStruct { | ||
| val: u32, | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| struct CStruct { | ||
| a: u8, | ||
| b: i32, | ||
| } | ||
|
|
||
| enum MyEnum { | ||
| Variant, | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn main() { | ||
| unsafe { | ||
| // Scalar types | ||
| assert!(size_of_val(&0i8) == 1); | ||
| assert!(size_of_val(&0i16) == 2); | ||
| assert!(size_of_val(&0i32) == 4); | ||
| assert!(size_of_val(&0i64) == 8); | ||
| assert!(size_of_val(&0i128) == 16); | ||
| assert!(size_of_val(&0isize) == 8); | ||
| assert!(size_of_val(&0u8) == 1); | ||
| assert!(size_of_val(&0u16) == 2); | ||
| assert!(size_of_val(&0u32) == 4); | ||
| assert!(size_of_val(&0u64) == 8); | ||
| assert!(size_of_val(&0u128) == 16); | ||
| assert!(size_of_val(&0usize) == 8); | ||
| assert!(size_of_val(&0f32) == 4); | ||
| assert!(size_of_val(&0f64) == 8); | ||
| assert!(size_of_val(&false) == 1); | ||
| assert!(size_of_val(&(0 as char)) == 4); | ||
| // Compound types (tuple and array) | ||
| assert!(size_of_val(&(0i32, 0i32)) == 8); | ||
| assert!(size_of_val(&[0i32; 5]) == 20); | ||
| // Custom data types (struct and enum) | ||
| assert!(size_of_val(&MyStruct { val: 0u32 }) == 4); | ||
| assert!(size_of_val(&MyEnum::Variant) == 0); | ||
| assert!(size_of_val(&CStruct { a: 0u8, b: 0i32 }) == 8); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be interesting to add a
repr(C)struct which would require padding and make sure we get this rightThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a
repr(C)struct and assertions based on it. As far as I can tell, it's working as expected:But let me know if you want to test specific cases and I will add them.