[zircon][rust] Use zr::slice_from_raw_parts in kernel FFI Replace manual zero-length checks around `core::slice::from_raw_parts` in `io_buffer_dispatcher_ffi` and `ioapic` with `zr::slice_from_raw_parts`. Bug: 448152804 Change-Id: I25cf1322a4fb4eed798cb718b8caac0f431513bf Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/1835924 Reviewed-by: Corey Tabaka <eieio@google.com> Fuchsia-Auto-Submit: Adam Barth <abarth@google.com> Commit-Queue: Adam Barth <abarth@google.com>
diff --git a/src/lib/zr/src/ptr.rs b/src/lib/zr/src/ptr.rs index 2c60bee..580655ef 100644 --- a/src/lib/zr/src/ptr.rs +++ b/src/lib/zr/src/ptr.rs
@@ -158,7 +158,6 @@ if len == 0 { &[] } else { - debug_assert!(!data.is_null()); // SAFETY: `len > 0`, and the caller guarantees `data` is valid for `len` elements. unsafe { core::slice::from_raw_parts(data, len) } } @@ -179,7 +178,6 @@ if len == 0 { &mut [] } else { - debug_assert!(!data.is_null()); // SAFETY: `len > 0`, and the caller guarantees `data` is uniquely valid for `len` elements. unsafe { core::slice::from_raw_parts_mut(data, len) } }
diff --git a/zircon/kernel/arch/x86/src/ioapic.rs b/zircon/kernel/arch/x86/src/ioapic.rs index 0a8e1a2..622255e 100644 --- a/zircon/kernel/arch/x86/src/ioapic.rs +++ b/zircon/kernel/arch/x86/src/ioapic.rs
@@ -366,19 +366,12 @@ overrides: *const IoApicIsaOverride, num_overrides: usize, ) { - let io_apic_descs = if num_io_apic_descs > 0 { - // SAFETY: `io_apic_descs` points to a valid array of size `num_io_apic_descs` provided by - // early boot. - unsafe { core::slice::from_raw_parts(io_apic_descs, num_io_apic_descs) } - } else { - &[] - }; - let overrides = if num_overrides > 0 { - // SAFETY: `overrides` points to a valid array of size `num_overrides` provided by early boot. - unsafe { core::slice::from_raw_parts(overrides, num_overrides) } - } else { - &[] - }; + // SAFETY: The caller guarantees `io_apic_descs` points to `num_io_apic_descs` initialized + // descriptors whenever `num_io_apic_descs > 0`. + let io_apic_descs = unsafe { zr::slice_from_raw_parts(io_apic_descs, num_io_apic_descs) }; + // SAFETY: The caller guarantees `overrides` points to `num_overrides` initialized entries + // whenever `num_overrides > 0`. + let overrides = unsafe { zr::slice_from_raw_parts(overrides, num_overrides) }; apic_io_init_safe(io_apic_descs, overrides); }
diff --git a/zircon/kernel/object/io_buffer_dispatcher_ffi.rs b/zircon/kernel/object/io_buffer_dispatcher_ffi.rs index 7268310..596dab6 100644 --- a/zircon/kernel/object/io_buffer_dispatcher_ffi.rs +++ b/zircon/kernel/object/io_buffer_dispatcher_ffi.rs
@@ -53,8 +53,8 @@ name: *const c_char, len: usize, ) -> zx_status_t { - let name_bytes = - if len == 0 { &[] } else { unsafe { core::slice::from_raw_parts(name.cast(), len) } }; + // SAFETY: The caller guarantees `name` points to `len` initialized bytes whenever `len > 0`. + let name_bytes = unsafe { zr::slice_from_raw_parts(name.cast(), len) }; Status::result_into_raw(disp.set_name(name_bytes)) }