From 00b9bf9610900c0c327a4a17ca4efd793ca9b45b Mon Sep 17 00:00:00 2001 From: Zhang Junyang Date: Fri, 19 Jul 2024 13:22:23 +0000 Subject: [PATCH] Fix assertion failure constructing `VmReader`s from zero sized slices --- ostd/src/mm/io.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ostd/src/mm/io.rs b/ostd/src/mm/io.rs index d3dcd60d6..a4bdc385b 100644 --- a/ostd/src/mm/io.rs +++ b/ostd/src/mm/io.rs @@ -340,8 +340,10 @@ impl<'a> VmReader<'a, KernelSpace> { /// it should _not_ overlap with other `VmWriter`s. /// The user space memory is treated as untyped. pub unsafe fn from_kernel_space(ptr: *const u8, len: usize) -> Self { - debug_assert!(KERNEL_BASE_VADDR <= ptr as usize); - debug_assert!(ptr.add(len) as usize <= KERNEL_END_VADDR); + // If casting a zero sized slice to a pointer, the pointer may be null + // and does not reside in our kernel space range. + debug_assert!(len == 0 || KERNEL_BASE_VADDR <= ptr as usize); + debug_assert!(len == 0 || ptr.add(len) as usize <= KERNEL_END_VADDR); Self { cursor: ptr, @@ -516,8 +518,10 @@ impl<'a> VmWriter<'a, KernelSpace> { /// is typed, it should _not_ overlap with other `VmReader`s and `VmWriter`s. /// The user space memory is treated as untyped. pub unsafe fn from_kernel_space(ptr: *mut u8, len: usize) -> Self { - debug_assert!(KERNEL_BASE_VADDR <= ptr as usize); - debug_assert!(ptr.add(len) as usize <= KERNEL_END_VADDR); + // If casting a zero sized slice to a pointer, the pointer may be null + // and does not reside in our kernel space range. + debug_assert!(len == 0 || KERNEL_BASE_VADDR <= ptr as usize); + debug_assert!(len == 0 || ptr.add(len) as usize <= KERNEL_END_VADDR); Self { cursor: ptr,