-
Notifications
You must be signed in to change notification settings - Fork 97
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
Audit for write_bytes
#1102
Audit for write_bytes
#1102
Conversation
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.
Looks good, a couple comments
align, | ||
PropertyClass::DefaultAssertion, | ||
"`dst` is properly aligned", | ||
loc.clone(), |
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.
No need to clone locations
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, fixed!
); | ||
let layout = self.layout_of(ty); | ||
let sz = Expr::int_constant(layout.size.bytes(), Type::size_t()); | ||
let memset_call = BuiltinFn::Memset.call(vec![dst, val, count.mul(sz)], loc); |
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.
We have overflow checking on, right? So we'll see if this overflows?
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 checked it but we were not detecting overflows here. Updated to include an explicit overflow check for this computation. Added another test to check overflows.
@@ -1156,4 +1149,34 @@ impl<'tcx> GotocCtx<'tcx> { | |||
let expr = dst.dereference().assign(src, loc.clone()); | |||
Stmt::block(vec![align_check, expr], loc) | |||
} | |||
|
|||
/// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to `val` |
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.
Like the callout to the 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.
😄
// Check that computing `bytes` would not overflow | ||
let layout = self.layout_of(ty); | ||
let size = Expr::int_constant(layout.size.bytes(), Type::size_t()); | ||
let bytes = count.mul_overflow(size); | ||
let overflow_check = self.codegen_assert( | ||
bytes.overflowed.not(), | ||
PropertyClass::ArithmeticOverflow, | ||
format!("{}: attempt to compute `bytes` which would overflow", intrinsic).as_str(), | ||
loc, | ||
); |
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.
Not for this PR, but we've done this check > 1 times, which means a helper fn might be good
Description of changes:
Moves and updates the
write_bytes
intrinsic implementation with an alignment check that it was missing. Adds 3 tests cases (2 of them inexpected
).Resolved issues:
Part of #727
Call-outs:
Testing:
How is this change tested? Adds 3 tests.
Is this a refactor change? No.
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.