mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
vmm: 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:
committed by
Rob Bradford
parent
cb09c37c55
commit
8b101fb890
@@ -1278,7 +1278,7 @@ mod tests {
|
||||
// safe to read. Casting to `u8` satisfies alignment requirements.
|
||||
let bytes = unsafe {
|
||||
std::slice::from_raw_parts(
|
||||
&gi as *const GenericInitiatorAffinity as *const u8,
|
||||
(&raw const gi).cast::<u8>(),
|
||||
std::mem::size_of::<GenericInitiatorAffinity>(),
|
||||
)
|
||||
};
|
||||
|
||||
@@ -87,7 +87,7 @@ fn modify_mode<F: FnOnce(&mut termios)>(
|
||||
// and we check the return result.
|
||||
let mut termios: termios = unsafe { zeroed() };
|
||||
// SAFETY: see above
|
||||
let ret = unsafe { tcgetattr(fd, &mut termios as *mut _) };
|
||||
let ret = unsafe { tcgetattr(fd, &raw mut termios) };
|
||||
if ret < 0 {
|
||||
return vmm_sys_util::errno::errno_result();
|
||||
}
|
||||
@@ -98,7 +98,7 @@ fn modify_mode<F: FnOnce(&mut termios)>(
|
||||
f(&mut termios);
|
||||
// SAFETY: Safe because the syscall will only read the extent of termios and we check
|
||||
// the return result.
|
||||
let ret = unsafe { tcsetattr(fd, TCSANOW, &termios as *const _) };
|
||||
let ret = unsafe { tcsetattr(fd, TCSANOW, &raw const termios) };
|
||||
if ret < 0 {
|
||||
return vmm_sys_util::errno::errno_result();
|
||||
}
|
||||
|
||||
@@ -303,7 +303,7 @@ fn core_scheduling_cookie() -> u64 {
|
||||
PR_SCHED_CORE_GET,
|
||||
0,
|
||||
PR_SCHED_CORE_SCOPE_THREAD,
|
||||
&mut cookie as *mut u64,
|
||||
&raw mut cookie,
|
||||
)
|
||||
};
|
||||
if ret == -1 {
|
||||
@@ -1187,12 +1187,13 @@ impl CpuManager {
|
||||
.spawn(move || {
|
||||
// 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,
|
||||
cpuset,
|
||||
)
|
||||
};
|
||||
|
||||
|
||||
@@ -4902,7 +4902,7 @@ impl DeviceManager {
|
||||
.remove_userspace_mapping(
|
||||
mapping.addr.raw_value(),
|
||||
mapping.mapping.size(),
|
||||
mapping.mapping.as_ptr() as _,
|
||||
mapping.mapping.as_ptr().cast(),
|
||||
mapping.mergeable,
|
||||
mapping.mem_slot,
|
||||
)
|
||||
|
||||
@@ -1188,7 +1188,7 @@ impl MemoryManager {
|
||||
let n = unsafe {
|
||||
libc::read(
|
||||
uffd_raw_fd,
|
||||
&mut msg as *mut uffd::UffdMsg as *mut libc::c_void,
|
||||
(&raw mut msg).cast(),
|
||||
std::mem::size_of::<uffd::UffdMsg>(),
|
||||
)
|
||||
};
|
||||
@@ -1824,7 +1824,7 @@ impl MemoryManager {
|
||||
let res = unsafe {
|
||||
libc::syscall(
|
||||
libc::SYS_mbind,
|
||||
addr as *mut libc::c_void,
|
||||
addr.cast::<libc::c_void>(),
|
||||
len,
|
||||
mode,
|
||||
nodemask.as_ptr(),
|
||||
@@ -2002,7 +2002,7 @@ impl MemoryManager {
|
||||
// SAFETY: FFI call with correct arguments
|
||||
let ret = unsafe {
|
||||
let addr = r.as_ptr().add(offset);
|
||||
libc::madvise(addr as _, pages * page_size, libc::MADV_POPULATE_WRITE)
|
||||
libc::madvise(addr.cast(), pages * page_size, libc::MADV_POPULATE_WRITE)
|
||||
};
|
||||
if ret != 0 {
|
||||
let e = io::Error::last_os_error();
|
||||
@@ -2021,7 +2021,7 @@ impl MemoryManager {
|
||||
|
||||
if thp && !hugepages {
|
||||
// SAFETY: FFI call with correct arguments
|
||||
let ret = unsafe { libc::madvise(region.as_ptr() as _, size, libc::MADV_HUGEPAGE) };
|
||||
let ret = unsafe { libc::madvise(region.as_ptr().cast(), size, libc::MADV_HUGEPAGE) };
|
||||
if ret != 0 {
|
||||
let e = io::Error::last_os_error();
|
||||
warn!("Failed to mark pages as THP eligible: {e}");
|
||||
@@ -2319,7 +2319,7 @@ impl MemoryManager {
|
||||
// mmap succeeded.
|
||||
let ret = unsafe {
|
||||
libc::madvise(
|
||||
userspace_addr as *mut libc::c_void,
|
||||
userspace_addr.cast(),
|
||||
memory_size as libc::size_t,
|
||||
libc::MADV_DONTDUMP,
|
||||
)
|
||||
@@ -2335,7 +2335,7 @@ impl MemoryManager {
|
||||
// mmap succeeded.
|
||||
let ret = unsafe {
|
||||
libc::madvise(
|
||||
userspace_addr as *mut libc::c_void,
|
||||
userspace_addr.cast(),
|
||||
memory_size as libc::size_t,
|
||||
libc::MADV_MERGEABLE,
|
||||
)
|
||||
@@ -2400,7 +2400,7 @@ impl MemoryManager {
|
||||
// previously advised.
|
||||
let ret = unsafe {
|
||||
libc::madvise(
|
||||
userspace_addr as *mut libc::c_void,
|
||||
userspace_addr.cast(),
|
||||
memory_size as libc::size_t,
|
||||
libc::MADV_UNMERGEABLE,
|
||||
)
|
||||
|
||||
@@ -4069,7 +4069,7 @@ pub fn test_vm() {
|
||||
index as u32,
|
||||
region.start_addr().raw_value(),
|
||||
region.len().try_into().unwrap(),
|
||||
region.as_ptr() as _,
|
||||
region.as_ptr().cast(),
|
||||
false,
|
||||
false,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user