-
-
Notifications
You must be signed in to change notification settings - Fork 15.1k
cmse: clear padding when crossing the secure boundary #157397
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
folkertdev
wants to merge
4
commits into
rust-lang:main
Choose a base branch
from
folkertdev:cmse-clear-padding
base: main
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.
+408
−55
Open
Changes from all commits
Commits
Show all changes
4 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
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
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,58 @@ | ||
| /// Represents a set of `Size` values as a sorted list of ranges. | ||
| // These are (offset, length) pairs, and they are sorted and mutually disjoint, | ||
| // and never adjacent (i.e. there's always a gap between two of them). | ||
| #[derive(Debug, Clone)] | ||
| pub struct RangeSet<T>(pub Vec<(T, T)>); | ||
|
|
||
| impl<T> RangeSet<T> | ||
| where | ||
| T: Copy + Ord + Default, | ||
| T: core::ops::Add<Output = T>, | ||
| T: core::ops::Sub<Output = T>, | ||
| { | ||
| pub fn new() -> Self { | ||
| Self(Vec::new()) | ||
| } | ||
|
|
||
| pub fn add_range(&mut self, offset: T, size: T) { | ||
| if size == T::default() { | ||
| // No need to track empty ranges. | ||
| return; | ||
| } | ||
| let v = &mut self.0; | ||
| // We scan for a partition point where the left partition is all the elements that end | ||
| // strictly before we start. Those are elements that are too "low" to merge with us. | ||
| let idx = | ||
| v.partition_point(|&(other_offset, other_size)| other_offset + other_size < offset); | ||
| // Now we want to either merge with the first element of the second partition, or insert ourselves before that. | ||
| if let Some(&(other_offset, other_size)) = v.get(idx) | ||
| && offset + size >= other_offset | ||
| { | ||
| // Their end is >= our start (otherwise it would not be in the 2nd partition) and | ||
| // our end is >= their start. This means we can merge the ranges. | ||
| let new_start = other_offset.min(offset); | ||
| let mut new_end = (other_offset + other_size).max(offset + size); | ||
| // We grew to the right, so merge with overlapping/adjacent elements. | ||
| // (We also may have grown to the left, but that can never make us adjacent with | ||
| // anything there since we selected the first such candidate via `partition_point`.) | ||
| let mut scan_right = 1; | ||
| while let Some(&(next_offset, next_size)) = v.get(idx + scan_right) | ||
| && new_end >= next_offset | ||
| { | ||
| // Increase our size to absorb the next element. | ||
| new_end = new_end.max(next_offset + next_size); | ||
| // Look at the next element. | ||
| scan_right += 1; | ||
| } | ||
| // Update the element we grew. | ||
| v[idx] = (new_start, new_end - new_start); | ||
| // Remove the elements we absorbed (if any). | ||
| if scan_right > 1 { | ||
| drop(v.drain((idx + 1)..(idx + scan_right))); | ||
| } | ||
| } else { | ||
| // Insert new element. | ||
| v.insert(idx, (offset, size)); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
How could you possibly zero uninit bytes? You have no way of knowing which bytes are uninit.
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.
Hmm, is "padding" the right word then? Basically I mean bytes that are not initialized by any valid value of the type.
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 not quite correct either. It's totally allowed for padding to be initialized.
What you are looking for is bytes that are always ignored by the representation relation of the type. I.e., for any sequence of bytes, if we reset the padding bytes to uninit, then these two sequences of bytes represent the same value (or they are both invalid).
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.
Exactly, but that's kind of a mouthful.
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.
Calling them "padding bytes" is a good shorthand I think. The long form can then go in a doc comment.