question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bebop on Rust - "reference to packed field is unaligned" - ENTIRE CRATE BROKEN ON LATEST RUST

See original GitHub issue

Describe the bug Using a unaligned references to packed fields seems to be an issue in the Rust compiler. It generates warnings or even a compile error.

To Reproduce I used the following Bebop code:

struct IntVector {
    int32 x;
    int32 y;
}```
And it generated the following lines (among others):
#[allow(unaligned_references)]
fn _serialize_chained<W: ::std::io::Write>(&self, dest: &mut W) -> ::bebop::SeResult<usize> {
    Ok(self.x._serialize_chained(dest)? + self.y._serialize_chained(dest)?)
}```

This generates the following warnings: image

On the most recent nightly, the warnings turn into hard errors. This means using Bebop on Nightly is currently impossible.

Expected behavior No compile issues.

Bebop info:

  • Version: 2.4.9
  • Runtime: Rust

Desktop (please complete the following information):

  • OS: Arch Linux

Issue Analytics

  • State:closed
  • Created 7 months ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
mattiecnvrcommented, Mar 8, 2023

Looks like they finally removed that feature. There is a recommended solution that should work: https://github.com/rust-lang/rust/issues/82523

To fix this code, it needs to stop creating a reference to a packed field. The alternative is to either just copy the packed field by adding curly braces (the compiler knows how to do that despite lack of alignment), or to create a raw pointer:

let mut foo = Foo1 { bar: 1, baz: 2 };

let brw = std::ptr::addr_of!(foo.baz); // Create an immutable raw pointer to the packed field.
let val = unsafe { brw.read_unaligned() }; // Perform an unaligned read of that pointer.
let brw_mut = std::ptr::addr_of_mut!(foo.baz); // Create a mutable raw pointer to the packed field.
unsafe { brw_mut.write_unaligned(val+1); } // Perform an unaligned write to that pointer.

// For formatting, adding curly braces means that a copy of the field is made, stored
// in a (properly aligned) temporary, and a reference to that temporary is being formatted.
println!("{}", {foo.baz});
// This is equivalent to the more verbose
println!("{}", {let copy = foo.baz; copy});

I think we just need to generate the pointer and copy to a temporary, aligned place in memory. Basically every field after the first one in the struct needs to be copied so it can be read from aligned memory. If we want to be really fancy that only needs to be done for unaligned fields. But it would be a bit of work to code in only copying if needed and the compiler might already get rid of the extra copy for us.

0reactions
andrewmd5commented, Mar 20, 2023

Released. Give it a go @Yaron-Ha

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tracking Issue for future-incompatibility warning ...
Fields of packed structs are not necessarily properly aligned. Hence creating a reference to a field of a packed struct can cause UB,...
Read more >
My Rust project is broken all of the sudden
error[E0793 ]: reference to packed field is unaligned --> C:\Users\u\.cargo\registry\src\github.com-1eccb9ec823\ntapi-0.3.6\src\ntexapi.rs ...
Read more >
Pointer to reference alignment : r/rust
a structure whose repr(align) is modified to be more tolerant than its fields can have a reference to it created with a more...
Read more >
1240-repr-packed-unsafe-ref - The Rust RFC Book
Taking a reference into a struct marked repr(packed) should become unsafe , because it can lead to undefined behaviour. repr(packed) structs need to...
Read more >
E0793 - Error codes index
Creating a reference to an insufficiently aligned packed field is undefined behavior and therefore disallowed. Using an unsafe block does not change ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found