-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Use u32 to describe vsock related buffer sizes #4637
Conversation
Move to u32 for vsock module. We can upsize from u32 to usize as needed. Signed-off-by: brandonpike <[email protected]>
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4637 +/- ##
=======================================
Coverage 82.08% 82.08%
=======================================
Files 255 255
Lines 31257 31262 +5
=======================================
+ Hits 25656 25661 +5
Misses 5601 5601
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
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.
Thanks!
if offset < self.len() as usize { | ||
let expected = buf.len(); | ||
if offset < self.len() { | ||
let expected = u32::try_from(buf.len()).unwrap(); |
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.
Here, we can translate the error from try_from
into some VolatileMemoryError
to indicate that its impossible to read more than u32::MAX bytes from a descriptor chain
let iov_len = u32::try_from(iov.iov_len).unwrap(); | ||
if offset >= iov_len { | ||
offset -= iov_len; |
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.
Mh, I think here its better to have a let mut offset = offset as usize
in line 142, so that we do not need any casts around this arithmetic and the VolatileSlice
constructor
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 agree, however, let's do let mut offset = u32::try_into(offset).unwrap
. We should also add a comment on why this is ok. Something like:
`iov.iov_len` is a `usize` but it gets assigned from `DescriptorChain::len` which is a `u32`, so the guest cannot pass to us something that is bigger than `u32`. As a result
offset = 0; | ||
|
||
if slice.len() > len { | ||
slice = slice.subslice(0, len)?; | ||
if u32::try_from(slice.len()).unwrap() > len { |
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.
if u32::try_from(slice.len()).unwrap() > len { | |
if slice.len() > len as usize { |
Or also just a let len = len as usize;
at the top of the method :). We mostly care that the API of these functions expresses the right thing, but casting up to usize inside of them is fine, since from there, the usize
won't "escape" again.
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.
again agree.
if offset < self.len() as usize { | ||
let expected = buf.len(); | ||
if offset < self.len() { | ||
let expected = u32::try_from(buf.len()).unwrap(); |
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.
see above
let mut total_bytes_read = 0; | ||
|
||
for iov in &self.vecs { | ||
if len == 0 { | ||
break; | ||
} | ||
|
||
if offset >= iov.iov_len { | ||
offset -= iov.iov_len; | ||
let iov_len = u32::try_from(iov.iov_len).unwrap(); |
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.
see above
offset = 0; | ||
|
||
if slice.len() > len { | ||
slice = slice.subslice(0, len)?; | ||
if u32::try_from(slice.len()).unwrap() > len { |
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.
see above
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.
Thanks for the PR Brandon!
I second Patrick's comments and I left a comment regarding the use of as
as opposed to into()
, try_into().unwrap()
.
expected: expected as usize, | ||
completed: bytes_read as usize, |
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.
Let's use expected.into()
instead of as
here.
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.
and everywhere else in this commit. We should always be able to use .into()
or .try_into().unwrap()
where appropriate.
let iov_len = u32::try_from(iov.iov_len).unwrap(); | ||
if offset >= iov_len { | ||
offset -= iov_len; |
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 agree, however, let's do let mut offset = u32::try_into(offset).unwrap
. We should also add a comment on why this is ok. Something like:
`iov.iov_len` is a `usize` but it gets assigned from `DescriptorChain::len` which is a `u32`, so the guest cannot pass to us something that is bigger than `u32`. As a result
offset = 0; | ||
|
||
if slice.len() > len { | ||
slice = slice.subslice(0, len)?; | ||
if u32::try_from(slice.len()).unwrap() > len { |
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.
again agree.
Subsumed by #4788 |
Move to u32 for vsock module. We can translate from u32 to usize as needed.
Changes
Closes #4627
Reason
Follow-up to #4556.
License Acceptance
By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache 2.0 license. For more information on following Developer
Certificate of Origin and signing off your commits, please check
CONTRIBUTING.md
.PR Checklist
PR.
CHANGELOG.md
.TODO
s link to an issue.contribution quality standards.
rust-vmm
.