Skip to content

Truly independent web browser

LibJS: Form addresses with wrapping pointer arithmetic

Andreas Kling5091c5ed15db
diff --git a/Libraries/LibJS/Rust/src/gc/heap.rs b/Libraries/LibJS/Rust/src/gc/heap.rs
index 5109f0b7ec9..00be6f299fe 100644
--- a/Libraries/LibJS/Rust/src/gc/heap.rs
+++ b/Libraries/LibJS/Rust/src/gc/heap.rs
@@ -146,8 +146,11 @@ impl Heap {
// SAFETY: The caller keeps the context alive for the heap's lifetime.
let raw = unsafe { capi::gc_heap_create(gather_roots, context, become_process_default) };
let raw = NonNull::new(raw).expect("LibGC creates the heap");
- // SAFETY: LibGC reports offsets of fields of its heap.
- let counter = |offset: u32| unsafe { raw.byte_add(offset as usize) }.cast::<usize>();
+ // NB: LibGC reports offsets of fields of its heap.
+ let counter = |offset: u32| {
+ NonNull::new(raw.as_ptr().wrapping_byte_add(offset as usize).cast::<usize>())
+ .expect("the fields of LibGC's heap are not at null")
+ };
Self {
raw,
allocators: [const { Cell::new(None) }; CLASS_COUNT],
diff --git a/Libraries/LibJS/Rust/src/interpreter/interpreter_stack.rs b/Libraries/LibJS/Rust/src/interpreter/interpreter_stack.rs
index a62d383ba6f..6aec0a7007c 100644
--- a/Libraries/LibJS/Rust/src/interpreter/interpreter_stack.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/interpreter_stack.rs
@@ -38,8 +38,7 @@ impl InterpreterStackMemory {
InterpreterStack {
base: Cell::new(base),
top: Cell::new(base),
- // SAFETY: The end of the allocation is in bounds.
- limit: Cell::new(unsafe { base.add(Self::SIZE) }),
+ limit: Cell::new(base.wrapping_add(Self::SIZE)),
next_frame_id: Cell::new(1),
}
}
diff --git a/Libraries/LibJS/Rust/src/jit/entry_table.rs b/Libraries/LibJS/Rust/src/jit/entry_table.rs
index 917f1c99eba..53440ff8924 100644
--- a/Libraries/LibJS/Rust/src/jit/entry_table.rs
+++ b/Libraries/LibJS/Rust/src/jit/entry_table.rs
@@ -88,8 +88,8 @@ impl JitEntryTable {
#[cfg(feature = "address-sanitizer")]
fn code_start(&self) -> *const c_void {
- // SAFETY: The offset is inside the mapping.
- unsafe { self.base.as_ptr().add(JIT_ENTRY_TABLE_CODE_OFFSET).cast() }
+ // NB: The offset is inside the mapping.
+ self.base.as_ptr().wrapping_add(JIT_ENTRY_TABLE_CODE_OFFSET).cast()
}
/// The address of the entries, which JIT code embeds.
diff --git a/Libraries/LibJS/Rust/src/jit/executable_memory.rs b/Libraries/LibJS/Rust/src/jit/executable_memory.rs
index 160313dbbb1..7644058a7c0 100644
--- a/Libraries/LibJS/Rust/src/jit/executable_memory.rs
+++ b/Libraries/LibJS/Rust/src/jit/executable_memory.rs
@@ -385,8 +385,8 @@ impl ExecutableMemory {
.map(|(offset, bytes)| {
assert!(*offset as usize + bytes.len() <= self.size);
CodeWrite {
- // SAFETY: The offset is within the memory, as asserted.
- destination: unsafe { self.address.add(*offset as usize) },
+ // NB: The offset is within the memory, as asserted.
+ destination: self.address.wrapping_add(*offset as usize),
code: bytes,
}
})
diff --git a/Libraries/LibJS/Rust/src/jit/translate.rs b/Libraries/LibJS/Rust/src/jit/translate.rs
index c05d0c71cd7..494f71bef4e 100644
--- a/Libraries/LibJS/Rust/src/jit/translate.rs
+++ b/Libraries/LibJS/Rust/src/jit/translate.rs
@@ -674,10 +674,13 @@ pub(super) fn materialize_call_site_frames(
let running = vm.head.running_execution_context.get();
let top = stack.top.get();
if let Some(callee) = callee {
- // SAFETY: Compiled code left this room below the callee's frame.
- stack
- .top
- .set(unsafe { callee.as_ptr().cast::<u8>().sub(site.inlined_frame_bytes as usize) });
+ // NB: Compiled code left this room below the callee's frame.
+ stack.top.set(
+ callee
+ .as_ptr()
+ .cast::<u8>()
+ .wrapping_sub(site.inlined_frame_bytes as usize),
+ );
}
vm.head.running_execution_context.set(root.as_ptr());
diff --git a/Libraries/LibJS/Rust/src/runtime/array_buffer.rs b/Libraries/LibJS/Rust/src/runtime/array_buffer.rs
index 3602bbd9154..572e6155fea 100644
--- a/Libraries/LibJS/Rust/src/runtime/array_buffer.rs
+++ b/Libraries/LibJS/Rust/src/runtime/array_buffer.rs
@@ -628,8 +628,8 @@ impl DataBlock {
if data.is_null() {
return data;
}
- // SAFETY: Callers only address bytes within the block.
- unsafe { data.add(byte_offset) }
+ // NB: This only forms the address, which callers that access the bytes check against the block.
+ data.wrapping_add(byte_offset)
}
pub fn set_size(&mut self, new_size: usize, zero_fill_new_bytes: ZeroFillNewBytes) {
diff --git a/Libraries/LibJS/Rust/src/runtime/value_storage.rs b/Libraries/LibJS/Rust/src/runtime/value_storage.rs
index 14c5af00ca9..5ae58d39832 100644
--- a/Libraries/LibJS/Rust/src/runtime/value_storage.rs
+++ b/Libraries/LibJS/Rust/src/runtime/value_storage.rs
@@ -143,8 +143,12 @@ pub fn allocate(heap: &Heap, capacity: u32, fill: Value) -> *mut Value {
},
fill,
);
- // SAFETY: The values follow the cell's header.
- unsafe { storage.as_ptr().cast::<u8>().add(VALUES_OFFSET).cast::<Value>() }
+ // NB: The values follow the cell's header.
+ storage
+ .as_ptr()
+ .cast::<u8>()
+ .wrapping_add(VALUES_OFFSET)
+ .cast::<Value>()
}
/// Frees malloc storage. Cells are collected instead.