Skip to content

Remove access to unaligned refs from bzImage tests #127

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

Merged
merged 1 commit into from
Jan 5, 2023

Conversation

AlexandruCihodaru
Copy link
Contributor

Summary of the PR

Replace access to unaligned reference in bzImage tests with std::ptr::addr_of macro and read_unaligned method.
Fixes: #123

Requirements

Before submitting your PR, please make sure you addressed the following
requirements:

  • All commits in this PR are signed (with git commit -s), and the commit
    message has max 60 characters for the summary and max 75 characters for each
    description line.
  • [] All added/changed functionality has a corresponding unit/integration
    test.
  • All added/changed public-facing functionality has entries in the "Upcoming
    Release" section of CHANGELOG.md (if no such section exists, please create one).
  • [] Any newly added unsafe code is properly documented.

Replace access to unaligned reference in bzImage tests with
std::ptr::addr_of macro and `read_unaligned` method.
Signed-off-by: Alexandru Cihodaru <[email protected]>
@andreeaflorescu andreeaflorescu merged commit 741965d into rust-vmm:main Jan 5, 2023
Copy link
Collaborator

@rbradford rbradford left a comment

Choose a reason for hiding this comment

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

Whoops, looks like I forgot to send this comment as it was part of a review.

assert_eq!(
// SAFETY:
// Reading the value from an unaligned address is not considered safe.
// but this is not an issue since this is a test.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This comment is a bit ambiguous. Rust allows you to read the contents of fields in an struct even if they are not aligned if that type is Copy. What you can't do is safely deference references to unaligned members.

Here is a little example:

#[repr(packed)]
struct Foo {
    a: u8,
    b: u64
}

// This won't compile due to unaligned access as the members are referenced
//fn bad(foo: &Foo) {
//    println!("{} {}", foo.a, foo.b)
//}

// This version is fine as we can copy the values from the struct
fn good(foo: &Foo) {
    let (a, b) = (foo.a, foo.b);
    println!("{} {}", a, b)
}

fn main() {
    let foo = Foo {a: 1, b: 2};
    good(&foo)
}

I would therefore advise against using the word "reading" here. Alternatively if the values setup_header are just copied to the stack then all this could be simplfied to:

        let setup_header = loader_result.setup_header.unwrap();
        let header = setup_header.header; 
        assert_eq!(header, 0x53726448);

I hope this is helpful as I went through the same journey to understand the details of this error.

Copy link
Member

Choose a reason for hiding this comment

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

@AlexandruCihodaru do you mind sending an update with the suggestion from @rbradford? This is a good catch!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing. Will create a new PR this week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix unaligned_references warnings instead of supressing them
4 participants