-
Notifications
You must be signed in to change notification settings - Fork 72
Specify alloc::Layout
#1972
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
Open
dianegolfouse
wants to merge
1
commit into
creusot-rs:master
Choose a base branch
from
dianegolfouse:layout
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+367
−0
Open
Specify alloc::Layout
#1972
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| //! Specifications for the `std` crate | ||
| pub mod alloc; | ||
| mod array; | ||
| mod borrow; | ||
| mod boxed; | ||
|
|
||
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,112 @@ | ||
| use crate::prelude::*; | ||
| #[cfg(creusot)] | ||
| use crate::std::mem::{align_of_logic, size_of_logic}; | ||
|
|
||
| use core::alloc::Layout; | ||
| #[cfg(creusot)] | ||
| use core::{ | ||
| alloc::LayoutError, | ||
| mem::{align_of, size_of}, | ||
| }; | ||
|
|
||
| pub trait LayoutExt { | ||
| /// Read [`Layout::size`] in logic. | ||
| #[logic] | ||
| fn size_log(self) -> usize; | ||
|
|
||
| /// Read [`Layout::align`] in logic. | ||
| #[logic] | ||
| fn align_log(self) -> usize; | ||
| } | ||
|
|
||
| impl LayoutExt for Layout { | ||
| #[logic(opaque)] | ||
| fn size_log(self) -> usize { | ||
| dead | ||
| } | ||
|
|
||
| #[logic(opaque)] | ||
| fn align_log(self) -> usize { | ||
| dead | ||
| } | ||
| } | ||
|
|
||
| /// Returns the rounding up of `n` to the nearest multiple of `of`. | ||
| #[logic] | ||
| #[requires(n >= 0 && of > 0)] | ||
| #[ensures(result >= n)] | ||
| #[ensures(exists<m: Int> m * of == result)] | ||
| #[ensures(result - of < n)] | ||
| pub fn round_nearest_multiple(n: Int, of: Int) -> Int { | ||
| pearlite! { | ||
| let m = crate::logic::such_that(|m: Int| m >= 0 && (m - 1) * of < n && n <= m * of); | ||
| m * of | ||
| } | ||
| } | ||
|
|
||
| /// Requirements for creating a [`Layout`]. | ||
| #[logic(open)] | ||
| pub fn layout_requirements(size: Int, align: Int) -> bool { | ||
| pearlite! { | ||
| align > 0 && | ||
| (exists<a: Int> a >= 0 && a.pow2() == align) && | ||
| align <= isize::MAX@ && | ||
| round_nearest_multiple(size, align) <= isize::MAX@ | ||
| } | ||
| } | ||
|
|
||
| extern_spec! { | ||
| impl Layout { | ||
| #[ensures(if layout_requirements(size@, align@) { | ||
| exists<res> result == Ok(res) && | ||
| res.size_log() == size && | ||
| res.align_log() == align | ||
| } else { | ||
| exists<err> result == Err(err) | ||
| })] | ||
| const fn from_size_align(size: usize, align: usize) -> Result<Layout, LayoutError>; | ||
|
|
||
| #[requires(layout_requirements(size@, align@))] | ||
| #[ensures(result.size_log() == size && result.align_log() == align)] | ||
| const unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Layout; | ||
|
|
||
| #[ensures(result == self.size_log())] | ||
| const fn size(&self) -> usize; | ||
|
|
||
| #[ensures(result == self.align_log())] | ||
| const fn align(&self) -> usize; | ||
|
|
||
| #[requires(layout_requirements(size_of_logic::<T>(), align_of_logic::<T>()@))] | ||
|
jhjourdan marked this conversation as resolved.
|
||
| #[ensures(result.size_log()@ == size_of_logic::<T>())] | ||
| #[ensures(result.align_log() == align_of_logic::<T>())] | ||
| #[ensures(exists<n: Int> result.size_log()@ == n * result.align_log()@)] | ||
| const fn new<T>() -> Layout { | ||
|
dianegolfouse marked this conversation as resolved.
|
||
| unsafe { Layout::from_size_align_unchecked(size_of::<T>(), align_of::<T>()) } | ||
| } | ||
|
|
||
| #[ensures(if layout_requirements(self.size_log()@, align@) { | ||
| exists<res> result == Ok(res) && | ||
| res.size_log() == self.size_log() && | ||
| res.align_log()@ == self.align_log()@.max(align@) | ||
| } else { | ||
| exists<err> result == Err(err) | ||
| })] | ||
| const fn align_to(&self, align: usize) -> Result<Layout, LayoutError> { | ||
| Layout::from_size_align(self.size(), align) | ||
| } | ||
|
|
||
| #[ensures(if n@ * size_of_logic::<T>() > isize::MAX@ { | ||
| exists<err> result == Err(err) | ||
| } else { | ||
| let s = crate::logic::such_that(|s: usize| s@ == n@ * size_of_logic::<T>()); | ||
| if layout_requirements(s@, align_of_logic::<T>()@) { | ||
| exists<res> result == Ok(res) && | ||
| res.size_log() == s && | ||
| res.align_log() == align_of_logic::<T>() | ||
| } else { | ||
| exists<err> result == Err(err) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this dead code? |
||
| } | ||
| })] | ||
| const fn array<T>(n: usize) -> Result<Layout, LayoutError>; | ||
| } | ||
| } | ||
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
Binary file not shown.
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.
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.
One possibility would be to require
sizeandalignto be positiveusizevalues that fit anisize. I think that would simplify quite a few specs.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.
Also, there is one missing requirement:
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.
Yeah, but the actual program values this specifies are
usize, so you would still need to require that they are in bounds forisizeThere 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.
I don't think your requirements implies this requirement, which is part of Rust's documentation.
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.
That's what I originally did, but this is annoying since
size_of_logic/align_of_logicreturnInts, so you need some shenanigans to pass them tolayout_requirements.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.
I see. We need a way to caset from
Intto machine integers.