Skip to content

Truly independent web browser

LibJS: Mark value storage header reads as unsafe

Andreas Kling2948696b7fe1
diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rs
index 39c667320f6..54e132df880 100644
--- a/Libraries/LibJS/Rust/src/runtime/object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/object.rs
@@ -345,7 +345,8 @@ unsafe impl Trace for Object {
}
if !named_properties.is_null()
&& !self.named_storage_is_inline()
- && let Some(storage) = value_storage::cell(named_properties)
+ // SAFETY: Out-of-line named storage is storage value_storage::allocate() returned.
+ && let Some(storage) = unsafe { value_storage::cell(named_properties) }
{
visitor.visit(storage);
}
@@ -364,7 +365,8 @@ unsafe impl Trace for Object {
self.indexed_storage_kind(),
IndexedStorageKind::Packed | IndexedStorageKind::Holey
) && !indexed_elements.is_null()
- && let Some(storage) = value_storage::cell(indexed_elements)
+ // SAFETY: Packed and holey indexed storage is storage value_storage::allocate() returned.
+ && let Some(storage) = unsafe { value_storage::cell(indexed_elements) }
{
visitor.visit(storage);
}
@@ -3611,7 +3613,8 @@ impl Object {
if self.named_properties.get().is_null() || self.named_storage_is_inline() {
return 0;
}
- value_storage::external_memory_size_of(self.named_properties.get())
+ // SAFETY: Out-of-line named storage is storage value_storage::allocate() returned.
+ unsafe { value_storage::external_memory_size_of(self.named_properties.get()) }
}
fn indexed_storage_external_memory_size(&self) -> usize {
@@ -3622,7 +3625,8 @@ impl Object {
if elements.is_null() {
return 0;
}
- value_storage::external_memory_size_of(elements)
+ // SAFETY: Packed and holey indexed storage is storage value_storage::allocate() returned.
+ unsafe { value_storage::external_memory_size_of(elements) }
}
IndexedStorageKind::Dictionary => {
size_of::<GenericIndexedPropertyStorage>() + self.indexed_dictionary().borrow().external_memory_size()
@@ -3636,7 +3640,8 @@ impl Object {
if self.named_storage_is_inline() {
return self.inline_named_capacity.get().into();
}
- value_storage::capacity(self.named_properties.get())
+ // SAFETY: Out-of-line named storage is storage value_storage::allocate() returned.
+ unsafe { value_storage::capacity(self.named_properties.get()) }
}
/// The VM whose heap the object lives in, for operations that allocate storage without being handed the VM.
@@ -3651,7 +3656,8 @@ impl Object {
let old_storage = storage.get();
let old_storage_is_malloc = self.has_flag(malloc_flag);
storage.set(new_storage);
- if value_storage::kind(new_storage) == ValueStorageKind::Malloc {
+ // SAFETY: Callers pass storage value_storage::allocate() returned.
+ if unsafe { value_storage::kind(new_storage) } == ValueStorageKind::Malloc {
self.set_flag(malloc_flag);
} else {
self.clear_flag(malloc_flag);
@@ -3723,7 +3729,8 @@ impl Object {
self.indexed_storage_kind(),
IndexedStorageKind::Packed | IndexedStorageKind::Holey
));
- value_storage::capacity(elements)
+ // SAFETY: Packed and holey indexed storage is storage value_storage::allocate() returned.
+ unsafe { value_storage::capacity(elements) }
}
/// Whether the packed or holey indexed storage holds an element (not a hole) at `index`.
diff --git a/Libraries/LibJS/Rust/src/runtime/value_storage.rs b/Libraries/LibJS/Rust/src/runtime/value_storage.rs
index e6f88bb51c0..14c5af00ca9 100644
--- a/Libraries/LibJS/Rust/src/runtime/value_storage.rs
+++ b/Libraries/LibJS/Rust/src/runtime/value_storage.rs
@@ -153,13 +153,19 @@ pub fn allocate(heap: &Heap, capacity: u32, fill: Value) -> *mut Value {
///
/// `values` must be storage that allocate() returned, and that nothing uses any more.
pub unsafe fn deallocate(values: *mut Value) {
- if kind(values) == ValueStorageKind::Malloc {
+ // SAFETY: Passed on from the caller.
+ if unsafe { kind(values) } == ValueStorageKind::Malloc {
// SAFETY: Passed on from the caller.
unsafe { malloc::deallocate(values) };
}
}
-pub fn capacity(values: *const Value) -> u32 {
+/// The number of values the storage has room for.
+///
+/// # Safety
+///
+/// `values` must be live storage that allocate() returned.
+pub unsafe fn capacity(values: *const Value) -> u32 {
// SAFETY: allocate() put the capacity in front of the first value.
unsafe {
values
@@ -170,7 +176,12 @@ pub fn capacity(values: *const Value) -> u32 {
}
}
-pub fn kind(values: *const Value) -> ValueStorageKind {
+/// Whether the storage is a cell or malloc storage.
+///
+/// # Safety
+///
+/// `values` must be live storage that allocate() returned.
+pub unsafe fn kind(values: *const Value) -> ValueStorageKind {
// SAFETY: allocate() put the kind in front of the first value, right after the capacity.
unsafe {
values
@@ -182,8 +193,13 @@ pub fn kind(values: *const Value) -> ValueStorageKind {
}
/// The cell holding the values, or None for malloc storage.
-pub fn cell(values: *mut Value) -> Option<Gc<ValueStorage>> {
- if kind(values) != ValueStorageKind::Cell {
+///
+/// # Safety
+///
+/// `values` must be live storage that allocate() returned.
+pub unsafe fn cell(values: *mut Value) -> Option<Gc<ValueStorage>> {
+ // SAFETY: Passed on from the caller.
+ if unsafe { kind(values) } != ValueStorageKind::Cell {
return None;
}
// SAFETY: Cell storage starts VALUES_OFFSET bytes in front of the values.
@@ -191,11 +207,17 @@ pub fn cell(values: *mut Value) -> Option<Gc<ValueStorage>> {
}
/// The memory the storage takes outside the GC heap.
-pub fn external_memory_size_of(values: *const Value) -> usize {
- if kind(values) != ValueStorageKind::Malloc {
+///
+/// # Safety
+///
+/// `values` must be live storage that allocate() returned.
+pub unsafe fn external_memory_size_of(values: *const Value) -> usize {
+ // SAFETY: Passed on from the caller.
+ if unsafe { kind(values) } != ValueStorageKind::Malloc {
return 0;
}
- malloc::allocation_size(capacity(values))
+ // SAFETY: As above.
+ malloc::allocation_size(unsafe { capacity(values) })
}
/// Storage beyond MAX_CELL_CAPACITY: [u32 capacity] [u32 kind] [Value 0] [Value 1] ...
@@ -261,7 +283,8 @@ mod malloc {
///
/// `values` must be malloc storage that allocate() returned.
pub unsafe fn deallocate(values: *mut Value) {
- let layout = layout(capacity(values));
+ // SAFETY: Passed on from the caller.
+ let layout = layout(unsafe { super::capacity(values) });
// SAFETY: The header is part of the same allocation, which the caller passes.
unsafe { raw::deallocate(values.cast::<u8>().sub(INDEXED_ELEMENTS_HEADER_SIZE), layout) };
}