Skip to content

Commit

Permalink
fix warning: unaligned_references
Browse files Browse the repository at this point in the history
fix warning, when compiling with 1.53.0
```
warning: reference to packed field is unaligned
   --> src/vhost_user/message.rs:252:53
    |
252 |   unsafe { std::mem::transmute_copy::<u32, R>(&self.request) }
    |                                               ^^^^^^^^^^^^^
    |
    = note: `#[warn(unaligned_references)]` on by default
    = warning: this was previously accepted by the compiler but is being
      phased out; it will become a hard error in a future release!
    = note: for more information, see issue #82523
      <rust-lang/rust#82523>
    = note: fields of packed structs are not properly aligned, and
      creating a misaligned reference is undefined behavior (even if
      that reference is never dereferenced)
```

Signed-off-by: wanglei <wllenyj@linux.alibaba.com>
  • Loading branch information
wanglei01 authored and slp committed Jul 29, 2021
1 parent c1f77c7 commit 488b3ad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion coverage_config_x86_64.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"coverage_score": 82.2, "exclude_path": "src/vhost_kern/", "crate_features": "vhost-user-master,vhost-user-slave"}
{"coverage_score": 82.3, "exclude_path": "src/vhost_kern/", "crate_features": "vhost-user-master,vhost-user-slave"}
32 changes: 27 additions & 5 deletions src/vhost_user/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,37 @@ bitflags! {
/// Common message header for vhost-user requests and replies.
/// A vhost-user message consists of 3 header fields and an optional payload. All numbers are in the
/// machine native byte order.
#[allow(safe_packed_borrows)]
#[repr(packed)]
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Copy)]
pub(super) struct VhostUserMsgHeader<R: Req> {
request: u32,
flags: u32,
size: u32,
_r: PhantomData<R>,
}

impl<R: Req> Debug for VhostUserMsgHeader<R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Point")
.field("request", &{ self.request })
.field("flags", &{ self.flags })
.field("size", &{ self.size })
.finish()
}
}

impl<R: Req> Clone for VhostUserMsgHeader<R> {
fn clone(&self) -> VhostUserMsgHeader<R> {
*self
}
}

impl<R: Req> PartialEq for VhostUserMsgHeader<R> {
fn eq(&self, other: &Self) -> bool {
self.request == other.request && self.flags == other.flags && self.size == other.size
}
}

impl<R: Req> VhostUserMsgHeader<R> {
/// Create a new instance of `VhostUserMsgHeader`.
pub fn new(request: R, flags: u32, size: u32) -> Self {
Expand All @@ -249,7 +270,7 @@ impl<R: Req> VhostUserMsgHeader<R> {
/// Get message type.
pub fn get_code(&self) -> R {
// It's safe because R is marked as repr(u32).
unsafe { std::mem::transmute_copy::<u32, R>(&self.request) }
unsafe { std::mem::transmute_copy::<u32, R>(&{ self.request }) }
}

/// Set message type.
Expand Down Expand Up @@ -803,7 +824,6 @@ impl DescStateSplit {
}

/// Inflight I/O queue region for split virtqueues
#[allow(safe_packed_borrows)]
#[repr(packed)]
pub struct QueueRegionSplit {
/// Features flags of this region
Expand Down Expand Up @@ -868,7 +888,6 @@ impl DescStatePacked {
}

/// Inflight I/O queue region for packed virtqueues
#[allow(safe_packed_borrows)]
#[repr(packed)]
pub struct QueueRegionPacked {
/// Features flags of this region
Expand Down Expand Up @@ -994,7 +1013,10 @@ mod tests {
hdr.set_version(0x1);
assert!(hdr.is_valid());

// Test Debug, Clone, PartiaEq trait
assert_eq!(hdr, hdr.clone());
assert_eq!(hdr.clone().get_code(), hdr.get_code());
assert_eq!(format!("{:?}", hdr.clone()), format!("{:?}", hdr));
}

#[test]
Expand Down

0 comments on commit 488b3ad

Please sign in to comment.