From fbc5345c9a602a696b8903c3892148911c678bb4 Mon Sep 17 00:00:00 2001 From: Alexander McLin Date: Wed, 8 Apr 2026 19:39:05 -0400 Subject: [PATCH] Issue 3223: more `unsafe` scope refinements --- src/ffi.rs | 27 ++++++++------------------ src/machine/heap.rs | 46 +++++++++++++++++++++------------------------ 2 files changed, 29 insertions(+), 44 deletions(-) diff --git a/src/ffi.rs b/src/ffi.rs index 8cfc2979..125930eb 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -65,20 +65,16 @@ impl FunctionImpl { Integer: From, T: Copy + TryInto + MightNotFitInFixnum, { - unsafe { - let n = self.cif.call::(self.code_ptr, args); + let n = unsafe { self.cif.call::(self.code_ptr, args) }; Ok(Value::Number(fixnum!(Number, n, arena))) - } } unsafe fn call_float(&self, args: &[Arg], _: &mut Arena) -> Result where T: Into, { - unsafe { - let n = self.cif.call::(self.code_ptr, args); + let n = unsafe { self.cif.call::(self.code_ptr, args) }; Ok(Value::Number(Number::Float(OrderedFloat(n.into())))) - } } unsafe fn call_ptr(&self, args: &[Arg], arena: &mut Arena) -> Result { @@ -270,11 +266,9 @@ impl StructImpl { let (new_layout, offset) = layout .extend(Layout::new::()) .map_err(|_| FfiError::LayoutError)?; - *layout = new_layout; - unsafe { - let n = std::ptr::read::(ptr.byte_offset(offset as isize).cast()); + *layout = new_layout; + let n = unsafe { std::ptr::read::(ptr.byte_offset(offset as isize).cast()) }; Ok(n) - } } unsafe fn read_int( @@ -286,10 +280,9 @@ impl StructImpl { T: Copy + TryInto + MightNotFitInFixnum, Integer: From, { - unsafe { - let n = read_primitive::(ptr, layout)?; + + let n = unsafe { read_primitive::(ptr, layout)? }; Ok(Value::Number(fixnum!(Number, n, arena))) - } } unsafe fn read_float( @@ -299,10 +292,8 @@ impl StructImpl { where T: Into, { - unsafe { - let n = read_primitive::(ptr, layout)?; + let n = unsafe { read_primitive::(ptr, layout)? }; Ok(Value::Number(Number::Float(OrderedFloat(n.into())))) - } } let mut layout = Layout::from_size_align(0, 1).map_err(|_| FfiError::LayoutError)?; @@ -803,10 +794,8 @@ impl ForeignFunctionTable { T: Copy + TryInto + MightNotFitInFixnum, Integer: From, { - unsafe { - let n = ptr.cast::().read(); + let n = unsafe { ptr.cast::().read() }; Value::Number(fixnum!(Number, n, arena)) - } } let ptr = ptr.as_ptr()?; diff --git a/src/machine/heap.rs b/src/machine/heap.rs index a59bdb16..12a0e40a 100644 --- a/src/machine/heap.rs +++ b/src/machine/heap.rs @@ -71,14 +71,16 @@ impl InnerHeap { new_layout.size() <= isize::MAX as usize, "Allocation too large. We should probably GC (TODO)" ); - unsafe { - let new_ptr = if self.byte_cap == 0 { - alloc::alloc(new_layout) - } else { - let old_layout = - alloc::Layout::from_size_align(self.byte_cap, size_of::()) - .unwrap(); - alloc::realloc(self.ptr, old_layout, new_layout.size()) + + let new_ptr = unsafe { + if self.byte_cap == 0 { + alloc::alloc(new_layout) + } else { + let old_layout = + alloc::Layout::from_size_align(self.byte_cap, size_of::()) + .unwrap(); + alloc::realloc(self.ptr, old_layout, new_layout.size()) + } }; if !new_ptr.is_null() { @@ -89,7 +91,6 @@ impl InnerHeap { } else { false } - } } } @@ -109,8 +110,7 @@ unsafe fn scan_slice_to_str(heap_slice: &[u8]) -> HeapStringScan<'_> { .position(|b| *b == 0u8) .unwrap_or(heap_slice.len()); - unsafe { - let zero_byte_addr = heap_slice.as_ptr().add(string_len); + let zero_byte_addr = unsafe { heap_slice.as_ptr().add(string_len) }; let sentinel_len = pstr_sentinel_length(zero_byte_addr.addr()); let tail_idx = cell_index!( @@ -121,10 +121,9 @@ unsafe fn scan_slice_to_str(heap_slice: &[u8]) -> HeapStringScan<'_> { let str_slice = &heap_slice[..string_len]; HeapStringScan { - string: std::str::from_utf8_unchecked(str_slice), + string: unsafe { std::str::from_utf8_unchecked(str_slice) }, tail_idx, } - } } // Same as scan_slice_to_str but assumes that the slice is from the start of a string. @@ -143,12 +142,10 @@ unsafe fn scan_slice_to_str_from_start(heap_slice: &[u8]) -> HeapStringScan<'_> let str_slice = &heap_slice[..string_len]; - unsafe { HeapStringScan { - string: std::str::from_utf8_unchecked(str_slice), + string: unsafe { std::str::from_utf8_unchecked(str_slice) }, tail_idx, } - } } #[derive(Debug, Clone, Copy)] @@ -282,19 +279,21 @@ impl ReservedHeapSection { src.as_ptr(), self.heap_ptr.add(heap_index!(self.heap_cell_len)), str_byte_len, - ); + ) + }; let zero_region_idx = heap_index!(self.heap_cell_len) + str_byte_len; let align_offset = pstr_sentinel_length(zero_region_idx); - ptr::write_bytes(self.heap_ptr.add(zero_region_idx), 0u8, align_offset); + unsafe { ptr::write_bytes(self.heap_ptr.add(zero_region_idx), 0u8, align_offset) }; cells_written = if align_offset == 1 { - ptr::write_bytes( + unsafe { ptr::write_bytes( self.heap_ptr.add(zero_region_idx + 1), 0u8, size_of::(), - ); + ) + }; // ensure there are at least two bytes in the boundary // buffer separating the string data from the tail @@ -305,7 +304,6 @@ impl ReservedHeapSection { }; self.heap_cell_len += cells_written; - } cells_written } @@ -629,17 +627,15 @@ impl Heap { let len = heap_index_checked!(num_cells).ok_or(AllocError)?; loop { - unsafe { if self.free_space() >= len { section = ReservedHeapSection { heap_ptr: self.inner.ptr, heap_cell_len: self.cell_len(), }; break; - } else if !self.grow() { + } else if unsafe { !self.grow() } { return Err(AllocError); } - } } Ok(HeapWriter { @@ -868,8 +864,8 @@ impl Heap { let align_offset = pstr_sentinel_length(s_len); let copy_size = s_len + align_offset; - unsafe { loop { + unsafe { if self.free_space() >= copy_size { let slice = std::slice::from_raw_parts_mut(self.inner.ptr, self.inner.byte_len + s_len);