Skip to content

Warn on clippy::cast_possible_truncation#3557

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

Conversation

JBYoshi
Copy link
Contributor

@JBYoshiJBYoshi commented Mar 24, 2023

Adds checks for clippy::cast_possible_truncation as per #3197.

Changes

I added -Dclippy::cast_possible_truncation to the Rust configuration and wrapped most unchecked casts with try_from(...).unwrap(). In a few cases like tests or where the truncation behavior was intentional, I updated the types or added explicit allow markers.

Reason

Fixes#3197.

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

  • If a specific issue led to this PR, this PR closes the issue.
  • The description of changes is clear and encompassing.
  • Any required documentation changes (code and docs) are included in this PR.
  • API changes follow the Runbook for Firecracker API changes.
  • User-facing changes are mentioned in CHANGELOG.md.
  • All added/changed functionality is tested.
  • New TODOs link to an issue.
  • Commits meet contribution quality standards.

  • This functionality cannot be added in rust-vmm.
@JBYoshiJBYoshiforce-pushed the clippy-cast-possible-truncation branch from ffcd821 to 0dfbf3aCompareMarch 24, 2023 23:23
@JonathanWoollett-LightJonathanWoollett-Light added Priority: Low Indicates that an issue or pull request should be resolved behind issues or pull requests labelled ` Type: Fix Indicates a fix to existing code labels Mar 28, 2023
@JonathanWoollett-LightJonathanWoollett-Light changed the title Warn on clippy::cast_possible_truncation.Warn on clippy::cast_possible_truncationMar 28, 2023
@JBYoshiJBYoshiforce-pushed the clippy-cast-possible-truncation branch 2 times, most recently from 794d0d6 to fa3a8cbCompareApril 3, 2023 18:49
@JBYoshi
Copy link
ContributorAuthor

I've updated to the latest main commit. All tests are passing on my end, though the coverage for the case without io_uring has decreased due to the increased line count in io_uring. My dev machine uses an Intel chip so I updated the coverage for that; I don't have an AMD or ARM machine to test on, so those coverage numbers may still need to be fixed.

@JBYoshiJBYoshiforce-pushed the clippy-cast-possible-truncation branch 2 times, most recently from e47a81d to e85a4aaCompareApril 12, 2023 20:03
@JBYoshi
Copy link
ContributorAuthor

Updated to fix AMD test coverage and ARM compile errors.

@JonathanWoollett-Light
Copy link
Contributor

Please squash these changes into 1 commit.

@JBYoshiJBYoshiforce-pushed the clippy-cast-possible-truncation branch 2 times, most recently from c72f1e3 to 6bec8a2CompareApril 13, 2023 16:27
@JBYoshi
Copy link
ContributorAuthor

Squashed, and I fixed a few more bugs in the build.

@JBYoshiJBYoshiforce-pushed the clippy-cast-possible-truncation branch from 6bec8a2 to 2523fe9CompareApril 14, 2023 17:28
@JBYoshi
Copy link
ContributorAuthor

Fixed a few more styling points and updated coverage numbers.

I'm noticing that the Git commit message lint is failing because my sign-off line is longer than the maximum line length. Is there a way to work around this?

@JBYoshiJBYoshiforce-pushed the clippy-cast-possible-truncation branch from b5d62a0 to fb21477CompareApril 27, 2023 02:47
@JBYoshi
Copy link
ContributorAuthor

Updated to add comments to the other "allow" markers.

@JonathanWoollett-Light
Copy link
Contributor

@JBYoshi Can you please resolve merge conflicts and rebase.

@JBYoshiJBYoshiforce-pushed the clippy-cast-possible-truncation branch from fb21477 to 99f2ec8CompareApril 27, 2023 15:19
@JBYoshi
Copy link
ContributorAuthor

@JBYoshiJBYoshiforce-pushed the clippy-cast-possible-truncation branch from 99f2ec8 to 1eb79ecCompareApril 27, 2023 16:55
@JBYoshi
Copy link
ContributorAuthor

Fixed Clippy error from merge.

@JonathanWoollett-Light
Copy link
Contributor

JonathanWoollett-Light commented May 16, 2023

@JBYoshi Could you please rebase.

@JBYoshiJBYoshiforce-pushed the clippy-cast-possible-truncation branch from 41d8ccd to 407bc6dCompareMay 17, 2023 02:10
@JBYoshi
Copy link
ContributorAuthor

@JonathanWoollett-Light I've rebased to the latest changes as far as I can test. The coverage numbers may still need to be updated though.

@JBYoshiJBYoshiforce-pushed the clippy-cast-possible-truncation branch 2 times, most recently from 0138434 to 690abb3CompareJune 1, 2023 02:51
In vmm/builder.rs, the CPU count can be fetched from the VM configuration without having to do a type conversion. In the serial device and the networking code, I added explicit checks to ensure that values fit in bounds. In the vCPU code, the existing code uses floats for tolerance values. We can replace these all with integer multiplications and divisions. Signed-off-by: Jonathan Browne <12983479+JBYoshi@users.noreply.github.com>
We can use usize in place of u64 here to reduce type conversion warnings. On 64-bit systems the types are equivalent, but Clippy allows automatic conversions in the one direction but not in the other. Signed-off-by: Jonathan Browne <12983479+JBYoshi@users.noreply.github.com>
Some places in the code don't allow us to re-type values to let the compiler verify that those conversions are safe, particularly with length values. All of these are marked with comments justifying why they are safe. Signed-off-by: Jonathan Browne <12983479+JBYoshi@users.noreply.github.com>
Some places in the code don't allow us to re-type values to let the compiler verify that those conversions are safe, particularly with length values. Since these are for test code, I don't expect overflows to happen, so I've just inserted unwraps here. Signed-off-by: Jonathan Browne <12983479+JBYoshi@users.noreply.github.com>
In this commit, I changed several places that refer to memory addresses that we generate and that are bounded by limitations of the system to use explicit unwraps. Signed-off-by: Jonathan Browne <12983479+JBYoshi@users.noreply.github.com>
Some of Rust's time measurements use u128 for outputs but u64 for inputs. I don't expect we'll overflow a u64 - 2^64 nanoseconds is over 500 years - so I've added try_from()/unwrap() calls to those conversions. Signed-off-by: Jonathan Browne <12983479+JBYoshi@users.noreply.github.com>
This code shoud already be safe because of debug-mode assertions and formal proofs done ahead of time. Signed-off-by: Jonathan Browne <12983479+JBYoshi@users.noreply.github.com>
These values can be changed to smaller types without causing problems. I've done this in a separate commit because my dev setup doesn't use ARM. In case something ARM-specific breaks in CI, I'd like to be able to keep those changes in their own commit so I can debug more easily. Signed-off-by: Jonathan Browne <12983479+JBYoshi@users.noreply.github.com>
For equalities and inequalities, we can change the types on each side to always convert to the larger type, which ensures all conversions are safe. I've done this in a separate commit because my dev setup doesn't use ARM. In case something ARM-specific breaks in CI, I'd like to be able to keep those changes in their own commit so I can debug more easily. Signed-off-by: Jonathan Browne <12983479+JBYoshi@users.noreply.github.com>
These values can be changed to smaller types without causing problems. I've done this in a separate commit because my dev setup doesn't use ARM. In case something ARM-specific breaks in CI, I'd like to be able to keep those changes in their own commit so I can debug more easily. Signed-off-by: Jonathan Browne <12983479+JBYoshi@users.noreply.github.com>
In these cases, values are intentionally truncated, either to split them into multiple values or to ensure that user-supplied data does not cause Firecracker to panic with a try_from/unwrap failure. Clippy will allow us to use "as" when we use bit masks to limit the size of an explicitly-sized value (u16, u32, u64). It doesn't work on usize values or on some more complicated expressions. I've done this in a separate commit because my dev setup doesn't use ARM. In case something ARM-specific breaks in CI, I'd like to be able to keep those changes in their own commits so I can debug more easily. Signed-off-by: Jonathan Browne <12983479+JBYoshi@users.noreply.github.com>
Some places in the code don't allow us to re-type values to let the compiler verify that those conversions are safe, particularly with length values. All of these are marked with comments justifying why they are safe. I've done this in a separate commit because my dev setup doesn't use ARM. In case something ARM-specific breaks in CI, I'd like to be able to keep those changes in their own commits so I can debug more easily. Signed-off-by: Jonathan Browne <12983479+JBYoshi@users.noreply.github.com>
Some places in the code don't allow us to re-type values to let the compiler verify that those conversions are safe, particularly with length values. Since these are for test code, I don't expect overflows to happen, so I've just inserted unwraps here. I've done this in a separate commit because my dev setup doesn't use ARM. In case something ARM-specific breaks in CI, I'd like to be able to keep those changes in their own commits so I can debug more easily. Signed-off-by: Jonathan Browne <12983479+JBYoshi@users.noreply.github.com>
Errors here appear to just emit warnings, so I added a check here to ensure the offset is in bounds. I've done this in a separate commit because my dev setup doesn't use ARM. In case something ARM-specific breaks in CI, I'd like to be able to keep those changes in their own commits so I can debug more easily. Signed-off-by: Jonathan Browne <12983479+JBYoshi@users.noreply.github.com>
Interrupt statuses are stored as 32-bit values in the system, but as 64-bit values in the snapshots. This commit changes the snapshot type to a u32 to remove those conversions. Signed-off-by: Jonathan Browne <12983479+JBYoshi@users.noreply.github.com>
This commit enables Clippy lint checks for truncating conversions. All code changes to conform to this are included in earlier commits. Signed-off-by: Jonathan Browne <12983479+JBYoshi@users.noreply.github.com>
@JBYoshiJBYoshiforce-pushed the clippy-cast-possible-truncation branch 2 times, most recently from 4d55f2d to b6bf058CompareOctober 8, 2023 23:19
@JBYoshi
Copy link
ContributorAuthor

I found the problem with the performance tests (I had mixed up process_startup_time_us and process_startup_time_cpu_us in some of the metrics) - should be fixed now. I also fixed the THC tolerance tests that were originally always rounding to 0.

In these tests, the tolerance constants all originally rounded to 0, which meant they weren't testing the correct behavior (everything was always under the tolerance level). This fixes the tests to properly put everything over the tolerance threshold. Signed-off-by: Jonathan Browne <12983479+JBYoshi@users.noreply.github.com>
@JBYoshiJBYoshiforce-pushed the clippy-cast-possible-truncation branch from b6bf058 to d0deb48CompareOctober 9, 2023 03:40
Copy link
Contributor

@roypatroypat left a comment

Choose a reason for hiding this comment

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

Great work, thank you!

@JonathanWoollett-Light
Copy link
Contributor

Great work, this turned into a surprisingly herculean effort, thank you for all your work.

@JonathanWoollett-LightJonathanWoollett-Light merged commit 5a6f28d into firecracker-microvm:mainOct 9, 2023
@JBYoshiJBYoshi deleted the clippy-cast-possible-truncation branch October 9, 2023 23:21
roypat added a commit to roypat/firecracker that referenced this pull request Oct 12, 2023
It seems a rebase gone awry caused the version bump from firecracker-microvm#3557 to be considered as part of 1.5, even though it will only land with 1.6. Swap the two lines to unblock CI. Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
roypat added a commit that referenced this pull request Oct 12, 2023
It seems a rebase gone awry caused the version bump from #3557 to be considered as part of 1.5, even though it will only land with 1.6. Swap the two lines to unblock CI. Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: LowIndicates that an issue or pull request should be resolved behind issues or pull requests labelled `Type: FixIndicates a fix to existing code
5 participants
@JBYoshi@JonathanWoollett-Light@roypat@kalyazin@bchalios
close