From 3844146a2163a394d3f6126b4ebf6eb5e5ba2dc2 Mon Sep 17 00:00:00 2001 From: Jay Bosamiya Date: Tue, 21 Apr 2026 17:14:43 -0700 Subject: [PATCH] More unsafe removal --- litebox_shim_linux/src/syscalls/tests.rs | 63 +++++++----------------- 1 file changed, 18 insertions(+), 45 deletions(-) diff --git a/litebox_shim_linux/src/syscalls/tests.rs b/litebox_shim_linux/src/syscalls/tests.rs index cd58df3dc..0bf1843fd 100644 --- a/litebox_shim_linux/src/syscalls/tests.rs +++ b/litebox_shim_linux/src/syscalls/tests.rs @@ -5,6 +5,7 @@ use litebox::fs::{FileSystem as _, Mode, OFlags}; use litebox::platform::RawConstPointer as _; use litebox_common_linux::{AtFlags, EfdFlags, FcntlArg, FileDescriptorFlags, errno::Errno}; use litebox_platform_multiplex::{Platform, set_platform}; +use zerocopy::FromBytes as _; use crate::MutPtr; @@ -179,22 +180,8 @@ fn test_getdent64() { let mut found_entries = alloc::vec::Vec::new(); while offset < bytes_read { - assert!( - unsafe { buffer.as_ptr().add(offset) }.addr() - & (core::mem::align_of::() - 1) - == 0, - "Pointer at offset {} is not aligned for LinuxDirent64 (requires {}-byte alignment)", - offset, - core::mem::align_of::() - ); - let dirent = unsafe { - core::ptr::read_unaligned( - buffer - .as_ptr() - .add(offset) - .cast::(), - ) - }; + let (dirent, _) = + litebox_common_linux::LinuxDirent64::read_from_prefix(&buffer[offset..]).unwrap(); // Validate the entry length assert!(dirent.len > 0, "Directory entry length must be positive"); @@ -203,14 +190,11 @@ fn test_getdent64() { "Entry should not exceed buffer" ); - let name_ptr = unsafe { - buffer - .as_ptr() - .add(offset + core::mem::offset_of!(litebox_common_linux::LinuxDirent64, __name)) + let name_bytes = { + let start = offset + core::mem::offset_of!(litebox_common_linux::LinuxDirent64, __name); + let end = offset + dirent.len as usize; + &buffer[start..end] }; - let name_len = dirent.len as usize - - core::mem::offset_of!(litebox_common_linux::LinuxDirent64, __name); - let name_bytes = unsafe { core::slice::from_raw_parts(name_ptr, name_len) }; // Find the null terminator let null_pos = name_bytes @@ -281,13 +265,8 @@ fn test_getdent64() { assert!(bytes <= small_buffer.len(), "Should not exceed buffer size"); // If bytes > 0, verify the structure is valid if bytes > 0 { - let dirent = unsafe { - core::ptr::read_unaligned( - small_buffer - .as_ptr() - .cast::(), - ) - }; + let (dirent, _) = + litebox_common_linux::LinuxDirent64::read_from_prefix(&small_buffer[..bytes]).unwrap(); assert!( dirent.len as usize <= bytes, "First entry length should fit in returned bytes" @@ -358,14 +337,10 @@ fn test_getdent64() { // Parse entries from this chunk let mut offset = 0; while offset < bytes_read { - let dirent = unsafe { - core::ptr::read_unaligned( - chunk_buffer - .as_ptr() - .add(offset) - .cast::(), - ) - }; + let (dirent, _) = litebox_common_linux::LinuxDirent64::read_from_prefix( + &chunk_buffer[offset..bytes_read], + ) + .unwrap(); assert!(dirent.len > 0, "Entry length must be positive"); assert!( @@ -373,14 +348,12 @@ fn test_getdent64() { "Entry should fit in chunk" ); - let name_ptr = unsafe { - chunk_buffer.as_ptr().add( - offset + core::mem::offset_of!(litebox_common_linux::LinuxDirent64, __name), - ) + let name_bytes = { + let start = + offset + core::mem::offset_of!(litebox_common_linux::LinuxDirent64, __name); + let end = offset + dirent.len as usize; + &chunk_buffer[start..end] }; - let name_len = dirent.len as usize - - core::mem::offset_of!(litebox_common_linux::LinuxDirent64, __name); - let name_bytes = unsafe { core::slice::from_raw_parts(name_ptr, name_len) }; let null_pos = name_bytes .iter()