Skip to content

Commit 8d0ade3

Browse files
committed
fix undefined behavior in get_vring_base()
By using ioctl_with_ref() instead of ioctl_with_mut_ref(), we attempted to mutate through an immutable reference, so rustc was well within its rights to assume that `vring_state` does not change across the ioctl call, and hence optimize the return value of the function to simply be the value that `vring_state.num` was initialized to (which is 0). Signed-off-by: Patrick Roy <[email protected]>
1 parent 0eb5c21 commit 8d0ade3

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

vhost/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
### Deprecated
1313
### Fixed
1414
- [[#304]](https://github.com/rust-vmm/vhost/pull/304) Fix building docs.
15+
- [[#326]](https://github.com/rust-vmm/vhots/pull/326) Fix `get_vring_base()` returning 0 instead of
16+
the vring base for vhost-kern backends when compiling in release mode.
1517

1618
## v0.14.0
1719

vhost/src/vhost_kern/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,12 @@ impl<T: VhostKernBackend> VhostBackend for T {
235235

236236
/// Get a bitmask of supported virtio/vhost features.
237237
fn get_vring_base(&self, queue_index: usize) -> Result<u32> {
238-
let vring_state = vhost_vring_state {
238+
let mut vring_state = vhost_vring_state {
239239
index: queue_index as u32,
240240
num: 0,
241241
};
242242
// SAFETY: This ioctl is called on a valid vhost fd and has its return value checked.
243-
let ret = unsafe { ioctl_with_ref(self, VHOST_GET_VRING_BASE(), &vring_state) };
243+
let ret = unsafe { ioctl_with_mut_ref(self, VHOST_GET_VRING_BASE(), &mut vring_state) };
244244
ioctl_result(ret, vring_state.num)
245245
}
246246

0 commit comments

Comments
 (0)