Skip to content
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 copy intrinsic #1199

Merged
merged 3 commits into from
May 16, 2022

Conversation

adpaco-aws
Copy link
Contributor

Description of changes:

Completes the audit for the copy intrinsic, which was disabled in #1081:

  • Adds alignment checks for both src and dst.
  • Adds overflow check for the count in bytes (this is not mentioned in the documentation).
  • Refactors the code into a function because macros are harder to debug.
  • Adds tests for all possible cases.

Initially, the changes in this PR were for both copy and copy_nonoverlapping, but as it has been noted in #1176 the copy_nonoverlapping case is handled somewhere else.

Resolved issues:

Creates #1198
Part of #1163

Call-outs:

Testing:

  • How is this change tested? Existing regression and 7 new/restored tests.

  • Is this a refactor change? No.

Checklist

  • Each commit message has a non-empty body, explaining why the change was made
  • Methods or procedures are documented
  • Regression or unit tests are included, or existing tests cover the modified code
  • My PR is restricted to a single feature or bugfix

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@adpaco-aws adpaco-aws requested a review from a team as a code owner May 13, 2022 21:51
@adpaco-aws adpaco-aws force-pushed the audit-copy-intrinsics branch from 2369fe3 to 7a968ec Compare May 13, 2022 22:24
Copy link
Contributor

@celinval celinval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one question about whether we should check if length is zero before adding the checks.

/// size_of::<T>()` bytes (done by calls to `memmove`)
/// In addition, we check that computing `count` in bytes (i.e., the third
/// argument for the `memmove` call) would not overflow.
fn codegen_copy_intrinsic(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functions are so much better!


// Generate alignment checks for both pointers
let src_align = self.is_ptr_aligned(farg_types[0], src.clone());
let src_align_check = self.codegen_assert(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these checks relevant if length is zero?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please see the last sentence in "Safety" here: https://doc.rust-lang.org/std/intrinsics/fn.copy.html

Note that even if the effectively copied size (count * size_of::<T>()) is 0, the pointers must be non-null and properly aligned.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect! thanks

Copy link
Contributor

@celinval celinval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great test coverage! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants