Skip to content

Truly independent web browser

LibJS: Declare the interpreter's dispatch tables as safe statics

Andreas Kling536b1a6d0322
diff --git a/Libraries/LibJS/Rust/src/interpreter/dispatch_tables.rs b/Libraries/LibJS/Rust/src/interpreter/dispatch_tables.rs
index 55ebd06986a..c515fccaf0b 100644
--- a/Libraries/LibJS/Rust/src/interpreter/dispatch_tables.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/dispatch_tables.rs
@@ -16,11 +16,12 @@ pub const DISPATCH_TABLE_SIZE: usize = 256;
pub type DispatchTable = [*const c_void; DISPATCH_TABLE_SIZE];
+// NB: The tables are immutable data in the assembled interpreter.
unsafe extern "C" {
/// The handlers of the interpreter, indexed by opcode.
- static js_interpreter_dispatch_table: DispatchTable;
+ safe static js_interpreter_dispatch_table: DispatchTable;
/// Every entry checks for breakpoints before running the instruction's handler.
- static js_interpreter_debug_dispatch_table: DispatchTable;
+ safe static js_interpreter_debug_dispatch_table: DispatchTable;
}
fn table_address(table: &'static DispatchTable) -> *const c_void {
@@ -29,8 +30,7 @@ fn table_address(table: &'static DispatchTable) -> *const c_void {
/// The handlers of the interpreter, indexed by opcode.
pub fn plain_handlers() -> &'static DispatchTable {
- // SAFETY: The table is immutable data in the assembled interpreter.
- unsafe { &js_interpreter_dispatch_table }
+ &js_interpreter_dispatch_table
}
/// The handlers of the interpreter.
@@ -40,8 +40,7 @@ pub fn plain_dispatch_table() -> *const c_void {
/// The handlers that check for breakpoints, which every executable runs with while the VM is debugging.
pub fn debug_dispatch_table() -> *const c_void {
- // SAFETY: The table is immutable data in the assembled interpreter.
- table_address(unsafe { &js_interpreter_debug_dispatch_table })
+ table_address(&js_interpreter_debug_dispatch_table)
}
impl Vm {
diff --git a/Libraries/LibJS/Rust/src/jit/dispatch_tables.rs b/Libraries/LibJS/Rust/src/jit/dispatch_tables.rs
index c78f01fcbe9..61137613d2f 100644
--- a/Libraries/LibJS/Rust/src/jit/dispatch_tables.rs
+++ b/Libraries/LibJS/Rust/src/jit/dispatch_tables.rs
@@ -13,15 +13,15 @@ use super::{InterpreterTier, JitState};
use crate::bytecode::instruction::{NUM_OPCODES, instruction_name_from_opcode};
use crate::interpreter::dispatch_tables::{DispatchTable, plain_dispatch_table, plain_handlers};
+// NB: The table is immutable data in the assembled interpreter.
unsafe extern "C" {
/// The handlers of the profiling build of the interpreter, which collect feedback for the optimizing JIT.
- static js_interpreter_dispatch_table_profiling: DispatchTable;
+ safe static js_interpreter_dispatch_table_profiling: DispatchTable;
}
/// The handlers of the profiling build of the interpreter.
fn profiling_dispatch_table() -> *const c_void {
- // SAFETY: The table is immutable data in the assembled interpreter.
- unsafe { js_interpreter_dispatch_table_profiling.as_ptr().cast() }
+ js_interpreter_dispatch_table_profiling.as_ptr().cast()
}
/// The plain handlers, with the profiling build's handlers for the instructions that count the tier-up budget
@@ -40,8 +40,7 @@ pub fn unprofiled_dispatch_table() -> Box<DispatchTable> {
/// The plain handlers, with the profiling build's handlers for the instructions whose names `use_profiling` selects.
fn mixed_dispatch_table(use_profiling: impl Fn(&str) -> bool) -> Box<DispatchTable> {
- // SAFETY: The table is immutable data in the assembled interpreter.
- let profiling = unsafe { &js_interpreter_dispatch_table_profiling };
+ let profiling = &js_interpreter_dispatch_table_profiling;
let mut table = Box::new(*plain_handlers());
for opcode in 0..NUM_OPCODES as usize {
if use_profiling(instruction_name_from_opcode(opcode as u8)) {
diff --git a/Libraries/LibJS/Rust/src/jit/executable_memory.rs b/Libraries/LibJS/Rust/src/jit/executable_memory.rs
index 6fc5cbab16f..160313dbbb1 100644
--- a/Libraries/LibJS/Rust/src/jit/executable_memory.rs
+++ b/Libraries/LibJS/Rust/src/jit/executable_memory.rs
@@ -42,7 +42,7 @@ mod protection_key {
unsafe extern "C" {
/// glibc's wrapper of WRPKRU for one key.
fn pkey_set(key: libc::c_int, rights: libc::c_uint) -> libc::c_int;
- fn pkey_alloc(flags: libc::c_uint, access_rights: libc::c_uint) -> libc::c_int;
+ safe fn pkey_alloc(flags: libc::c_uint, access_rights: libc::c_uint) -> libc::c_int;
pub fn pkey_mprotect(
address: *mut libc::c_void,
length: libc::size_t,
@@ -57,8 +57,7 @@ mod protection_key {
pub fn key() -> Option<libc::c_int> {
static KEY: OnceLock<Option<libc::c_int>> = OnceLock::new();
*KEY.get_or_init(|| {
- // SAFETY: pkey_alloc() has no preconditions.
- let key = unsafe { pkey_alloc(0, PKEY_DISABLE_WRITE) };
+ let key = pkey_alloc(0, PKEY_DISABLE_WRITE);
(key >= 0).then_some(key)
})
}