virtio-devices: replace as <pointer> casts with safer alternatives

`as` casts can change mutability, which quickly leads to undefined
behavior.

Signed-off-by: Julian Schindel <mail@arctic-alpaca.de>
This commit is contained in:
Julian Schindel
2026-04-29 21:55:06 +02:00
committed by Rob Bradford
parent 8b101fb890
commit ae7113e1d4
7 changed files with 9 additions and 12 deletions

View File

@@ -185,7 +185,7 @@ impl BalloonEpollHandler {
let res =
// SAFETY: FFI call with valid arguments, guaranteed by VolatileSlice
unsafe {
libc::madvise(slice.ptr_guard_mut().as_ptr() as *mut libc::c_void,
libc::madvise(slice.ptr_guard_mut().as_ptr().cast(),
range_len as libc::size_t, advice) };
if res != 0 {
return Err(Error::MadviseFail(io::Error::last_os_error()));

View File

@@ -605,13 +605,10 @@ Setting device status to 'NEEDS_RESET' and stopping processing queues until rese
// Schedule the thread to run on the expected CPU set
if let Some(cpuset) = cpuset.as_ref() {
let cpuset: *const libc::cpu_set_t = cpuset;
// SAFETY: FFI call with correct arguments
let ret = unsafe {
libc::sched_setaffinity(
0,
std::mem::size_of::<libc::cpu_set_t>(),
cpuset as *const libc::cpu_set_t,
)
libc::sched_setaffinity(0, std::mem::size_of::<libc::cpu_set_t>(), cpuset)
};
if ret != 0 {
@@ -1070,7 +1067,7 @@ impl VirtioDevice for Block {
fn write_config(&mut self, offset: u64, data: &[u8]) {
// The "writeback" field is the only mutable field
let writeback_offset =
(&self.config.writeback as *const _ as u64) - (&self.config as *const _ as u64);
(&raw const self.config.writeback as u64) - (&raw const self.config as u64);
if offset != writeback_offset || data.len() != std::mem::size_of_val(&self.config.writeback)
{
error!(

View File

@@ -1081,7 +1081,7 @@ impl VirtioDevice for Iommu {
fn write_config(&mut self, offset: u64, data: &[u8]) {
// The "bypass" field is the only mutable field
let bypass_offset =
(&self.config.bypass as *const _ as u64) - (&self.config as *const _ as u64);
(&raw const self.config.bypass as u64) - (&raw const self.config as u64);
if offset != bypass_offset || data.len() != std::mem::size_of_val(&self.config.bypass) {
error!(
"Attempt to write to read-only field: offset {:x} length {}",

View File

@@ -480,7 +480,7 @@ impl MemEpollHandler {
// alone is not past the end.
let res = unsafe {
libc::madvise(
self.region.as_ptr().offset(offset as isize) as *mut libc::c_void,
self.region.as_ptr().offset(offset as isize).cast(),
size as libc::size_t,
libc::MADV_DONTNEED,
)

View File

@@ -233,7 +233,7 @@ impl VirtioDevice for Blk {
fn write_config(&mut self, offset: u64, data: &[u8]) {
// The "writeback" field is the only mutable field
let writeback_offset =
(&self.config.writeback as *const _ as u64) - (&self.config as *const _ as u64);
(&raw const self.config.writeback as u64) - (&raw const self.config as u64);
if offset != writeback_offset || data.len() != std::mem::size_of_val(&self.config.writeback)
{
error!(

View File

@@ -687,7 +687,7 @@ impl VhostUserHandle {
// SAFETY: region is of size len
let bitmap: &[u64] = unsafe {
// Cast the pointer to u64
let ptr = region.as_ptr() as *const u64;
let ptr = region.as_ptr().cast();
std::slice::from_raw_parts(ptr, len)
};
Ok(MemoryRangeTable::from_dirty_bitmap(

View File

@@ -396,7 +396,7 @@ impl VsockPacket {
PacketBuffer::Borrowed { ptr, len } => {
// SAFETY: bound checks have already been performed when creating the packet
// from the virtq descriptor.
Some(unsafe { std::slice::from_raw_parts(*ptr as *const u8, *len) })
Some(unsafe { std::slice::from_raw_parts((*ptr).cast(), *len) })
}
}
}