Skip to content

Truly independent web browser

LibJS: Declare LibGC's precondition-free queries as safe

Andreas Kling140df93f98b5
diff --git a/Libraries/LibJS/Rust/src/gc/capi.rs b/Libraries/LibJS/Rust/src/gc/capi.rs
index 41cc1982aa6..beb38e39f30 100644
--- a/Libraries/LibJS/Rust/src/gc/capi.rs
+++ b/Libraries/LibJS/Rust/src/gc/capi.rs
@@ -81,8 +81,8 @@ pub type GCGatherRootsCallback = unsafe extern "C" fn(context: *mut c_void, root
unsafe extern "C" {
pub fn gc_get_layout(layout: *mut GCLayout);
- pub fn gc_heap_region_base() -> usize;
- pub fn gc_primitive_storage_cage_base() -> usize;
+ pub safe fn gc_heap_region_base() -> usize;
+ pub safe fn gc_primitive_storage_cage_base() -> usize;
pub fn gc_heap_create(
gather_roots: GCGatherRootsCallback,
@@ -173,11 +173,12 @@ unsafe extern "C" {
out_layout: *mut GCPrimitiveStorageLayout,
) -> bool;
pub fn gc_primitive_storage_free(handle: GCPrimitiveStorageHandle);
- pub fn gc_primitive_storage_is_valid(handle: GCPrimitiveStorageHandle) -> bool;
- pub fn gc_primitive_storage_offset(handle: GCPrimitiveStorageHandle) -> usize;
- pub fn gc_primitive_storage_size(handle: GCPrimitiveStorageHandle) -> usize;
- pub fn gc_primitive_storage_capacity(handle: GCPrimitiveStorageHandle) -> usize;
- pub fn gc_primitive_storage_committed_size(handle: GCPrimitiveStorageHandle) -> usize;
+ // NB: LibGC checks the handles it is queried with, so any handle may be queried.
+ pub safe fn gc_primitive_storage_is_valid(handle: GCPrimitiveStorageHandle) -> bool;
+ pub safe fn gc_primitive_storage_offset(handle: GCPrimitiveStorageHandle) -> usize;
+ pub safe fn gc_primitive_storage_size(handle: GCPrimitiveStorageHandle) -> usize;
+ pub safe fn gc_primitive_storage_capacity(handle: GCPrimitiveStorageHandle) -> usize;
+ pub safe fn gc_primitive_storage_committed_size(handle: GCPrimitiveStorageHandle) -> usize;
pub fn gc_primitive_storage_data(handle: GCPrimitiveStorageHandle) -> *mut u8;
pub fn gc_shared_memory_create(size: usize, out_fd: *mut c_int) -> bool;
diff --git a/Libraries/LibJS/Rust/src/gc/primitive_storage.rs b/Libraries/LibJS/Rust/src/gc/primitive_storage.rs
index ae9f3edec7d..1ffd0a670a0 100644
--- a/Libraries/LibJS/Rust/src/gc/primitive_storage.rs
+++ b/Libraries/LibJS/Rust/src/gc/primitive_storage.rs
@@ -42,8 +42,8 @@ impl ZeroFillNewBytes {
/// reserves the cage on the first call, which the VM makes when it is created.
pub fn cage_base() -> usize {
static CAGE_BASE: OnceLock<usize> = OnceLock::new();
- // SAFETY: The function has no preconditions; it reserves the cage once and then returns its address.
- *CAGE_BASE.get_or_init(|| unsafe { capi::gc_primitive_storage_cage_base() })
+ // NB: The function reserves the cage once and then returns its address.
+ *CAGE_BASE.get_or_init(|| capi::gc_primitive_storage_cage_base())
}
fn address_in_cage(offset: usize) -> *mut u8 {
@@ -257,23 +257,19 @@ impl ForeignPrimitiveStorage {
}
pub fn is_valid(self) -> bool {
- // SAFETY: Any handle may be queried.
- unsafe { capi::gc_primitive_storage_is_valid(self.handle) }
+ capi::gc_primitive_storage_is_valid(self.handle)
}
pub fn offset(self) -> usize {
- // SAFETY: Any handle may be queried.
- unsafe { capi::gc_primitive_storage_offset(self.handle) }
+ capi::gc_primitive_storage_offset(self.handle)
}
pub fn size(self) -> usize {
- // SAFETY: Any handle may be queried.
- unsafe { capi::gc_primitive_storage_size(self.handle) }
+ capi::gc_primitive_storage_size(self.handle)
}
pub fn capacity(self) -> usize {
- // SAFETY: Any handle may be queried.
- unsafe { capi::gc_primitive_storage_capacity(self.handle) }
+ capi::gc_primitive_storage_capacity(self.handle)
}
/// The first byte, or null if the handle names no storage.
diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rs
index 8d274c3b3aa..08089e2eca9 100644
--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs
@@ -624,8 +624,8 @@ impl Vm {
/// `storage` must be valid for writes of a Vm and aligned for one. The VM must stay there until it is dropped in
/// place, which only the thread that constructed it may do.
pub unsafe fn create_at(storage: *mut Vm, options: VmOptions) {
- // SAFETY: Initializes the region cell pointers are relative to.
- let heap_region_base = unsafe { capi::gc_heap_region_base() };
+ // NB: Initializes the region cell pointers are relative to.
+ let heap_region_base = capi::gc_heap_region_base();
let primitive_storage_cage_base = crate::runtime::array_buffer::primitive_storage_cage_base();
let interpreter_stack_memory = InterpreterStackMemory::allocate();
// SAFETY: An all-zero NativeFunctionTableEntry is valid: no function and the first function type.