Truly independent web browser
LibWeb: Declare host imports that take plain values as safe
Andreas Kling74d6d1b6bf17
diff --git a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs b/Libraries/LibWeb/Rust/src/layout/text_chunker.rsindex c1f249cbd3c..82b46e819cb 100644--- a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs+++ b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs@@ -20,9 +20,9 @@ unsafe extern "C" {start: *mut usize,end: *mut usize,);- fn unicode_layout_bidirectional_class(code_point: u32) -> u8;- fn unicode_layout_line_break_class(code_point: u32) -> u8;- fn unicode_layout_code_point_has_emoji_property(code_point: u32) -> bool;+ safe fn unicode_layout_bidirectional_class(code_point: u32) -> u8;+ safe fn unicode_layout_line_break_class(code_point: u32) -> u8;+ safe fn unicode_layout_code_point_has_emoji_property(code_point: u32) -> bool;}// LibUnicode's LineBreakClass, in declaration order.@@ -36,8 +36,7 @@ mod line_break_class {}fn line_break_class(code_point: u32) -> u8 {- // SAFETY: Pure Unicode table lookup.- unsafe { unicode_layout_line_break_class(code_point) }+ unicode_layout_line_break_class(code_point)}// Classifies a code point for direction-run splitting: strong LTR/RTL, direction-neutral Common, or ContextDependent@@ -72,8 +71,7 @@ fn text_type_for_code_point(code_point: u32) -> u8 {return text_type;}- // SAFETY: Pure Unicode table lookup.- match unsafe { unicode_layout_bidirectional_class(code_point) } {+ match unicode_layout_bidirectional_class(code_point) {// LibUnicode's BidiClass, in declaration order: AN, B, CS, NSM, EN, ES, ET, S, WS.0 | 1 | 3..=7 | 21 | 22 => X,// BN, FSI, ON, PDF, PDI.@@ -609,14 +607,12 @@ impl<'text> TextChunker<'text> {let has_emoji_property = if code_point < 0x300 {matches!(code_point, 0x23 | 0x2a | 0x30..=0x39 | 0xa9 | 0xae)} else {- // SAFETY: Pure Unicode table lookup.- unsafe { unicode_layout_code_point_has_emoji_property(code_point) }+ unicode_layout_code_point_has_emoji_property(code_point)};debug_assert!(code_point >= 0x300 || {let presentation = emoji_presentation_for_code_point(code_point, next_code_point);- // SAFETY: Pure Unicode table lookup.- let has_property = unsafe { unicode_layout_code_point_has_emoji_property(code_point) };+ let has_property = unicode_layout_code_point_has_emoji_property(code_point);presentation.is_emoji == default_presentation.is_emoji&& presentation.forced == default_presentation.forced&& has_property == has_emoji_propertydiff --git a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs b/Libraries/LibWeb/Rust/src/layout/tree_builder.rsindex 1b8c024eb4b..75445018284 100644--- a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs+++ b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs@@ -3492,7 +3492,7 @@ pub struct FfiCodePointCategoryFacts {}unsafe extern "C" {- fn ladybird_layout_code_point_category_facts(code_point: u32) -> FfiCodePointCategoryFacts;+ safe fn ladybird_layout_code_point_category_facts(code_point: u32) -> FfiCodePointCategoryFacts;}#[derive(Clone, Copy, Debug, PartialEq, Eq)]@@ -4482,10 +4482,7 @@ fn find_first_letter_in_layout_text(host: &TreeBuilderHost<'_>, node: LayoutNode&text,preserves_segment_breaks,|index| segmenter.next_boundary(index, false).unwrap_or(text.len()),- |code_point| {- // SAFETY: This service classifies a scalar value without accessing layout.- unsafe { ladybird_layout_code_point_category_facts(code_point) }- },+ |code_point| ladybird_layout_code_point_category_facts(code_point),);if target.found {target.text_layout_node = node;diff --git a/Libraries/LibWeb/Rust/src/render_state.rs b/Libraries/LibWeb/Rust/src/render_state.rsindex 564e37158b3..ec97c1dd0ad 100644--- a/Libraries/LibWeb/Rust/src/render_state.rs+++ b/Libraries/LibWeb/Rust/src/render_state.rs@@ -785,15 +785,14 @@ fn stack_has_room_for_render_job(tree_depth: u32) -> bool {/// The stack a job on a render state takes beside its recursion through the tree, with room to spare.const STACK_BESIDE_RECURSION: usize = 1024 * 1024;unsafe extern "C" {- fn web_current_thread_stack_base() -> usize;+ safe fn web_current_thread_stack_base() -> usize;}thread_local! {static STACK_BASE: Cell<usize> = const { Cell::new(0) };}let mut base = STACK_BASE.get();if base == 0 {- // SAFETY: The function only reads the calling thread's stack bounds.- base = unsafe { web_current_thread_stack_base() };+ base = web_current_thread_stack_base();STACK_BASE.set(base);}let here = 0u8;diff --git a/Libraries/LibWeb/Rust/src/render_state/clock/hover.rs b/Libraries/LibWeb/Rust/src/render_state/clock/hover.rsindex 2bce1e7fd45..1ddd8e2742d 100644--- a/Libraries/LibWeb/Rust/src/render_state/clock/hover.rs+++ b/Libraries/LibWeb/Rust/src/render_state/clock/hover.rs@@ -50,8 +50,8 @@ unsafe extern "C" {/// Hands the render clock a pointer move over the compositor context of a same-process iframe's document, as the/// compositor hands it those over the page's: the context, the position in the context's device pixels, or none/// where the pointer left it, the buttons held, whether the compositor scrolled since the frame, and the input- /// event id. Any thread may call it.- fn web_render_clock_hand_pointer_move(+ /// event id. Any thread may call it, and it ignores a context it has no lane for.+ safe fn web_render_clock_hand_pointer_move(context: u64,has_position: bool,x: f32,@@ -238,18 +238,15 @@ impl HoverPlan {/// pixels, or out of it.fn hand_pointer_to_context(context: u64, position: Option<FloatPoint>, pointer: PendingPointer) {let position_or_origin = position.unwrap_or(FloatPoint { x: 0.0, y: 0.0 });- // SAFETY: The render clock takes these arguments from any thread.- unsafe {- web_render_clock_hand_pointer_move(- context,- position.is_some(),- position_or_origin.x,- position_or_origin.y,- pointer.buttons,- pointer.scrolled_since_frame,- pointer.input_event_id,- );- }+ web_render_clock_hand_pointer_move(+ context,+ position.is_some(),+ position_or_origin.x,+ position_or_origin.y,+ pointer.buttons,+ pointer.scrolled_since_frame,+ pointer.input_event_id,+ );}/// Where the render clock heard the pointer went.@@ -492,7 +489,7 @@ fn monotonic_milliseconds() -> f64 {nanoseconds: i64,}unsafe extern "C" {- fn clock_gettime(clock: i32, time: *mut Timespec) -> i32;+ safe fn clock_gettime(clock: i32, time: &mut Timespec) -> i32;}#[cfg(target_os = "macos")]const CLOCK_MONOTONIC: i32 = 6;@@ -502,8 +499,7 @@ fn monotonic_milliseconds() -> f64 {seconds: 0,nanoseconds: 0,};- // SAFETY: `time` is valid for writes.- unsafe { clock_gettime(CLOCK_MONOTONIC, &raw mut time) };+ clock_gettime(CLOCK_MONOTONIC, &mut time);time.seconds as f64 * 1000.0 + time.nanoseconds as f64 / 1_000_000.0}diff --git a/Libraries/LibWeb/Rust/src/stage_thread.rs b/Libraries/LibWeb/Rust/src/stage_thread.rsindex f8d58244831..6a5b2323d21 100644--- a/Libraries/LibWeb/Rust/src/stage_thread.rs+++ b/Libraries/LibWeb/Rust/src/stage_thread.rs@@ -35,18 +35,17 @@ use std::thread::Thread;#[cfg(feature = "thread-sanitizer")]pub(crate) mod tsan {unsafe extern "C" {- fn __tsan_acquire(address: *mut std::ffi::c_void);- fn __tsan_release(address: *mut std::ffi::c_void);+ // NB: The TSan runtime only uses the address as a synchronization key, and never reads or writes through it.+ safe fn __tsan_acquire(address: *mut std::ffi::c_void);+ safe fn __tsan_release(address: *mut std::ffi::c_void);}pub(crate) fn release(key: &std::sync::atomic::AtomicBool) {- // SAFETY: The TSan runtime only uses the address as a synchronization key.- unsafe { __tsan_release(std::ptr::from_ref(key).cast_mut().cast()) }+ __tsan_release(std::ptr::from_ref(key).cast_mut().cast());}pub(crate) fn acquire(key: &std::sync::atomic::AtomicBool) {- // SAFETY: The TSan runtime only uses the address as a synchronization key.- unsafe { __tsan_acquire(std::ptr::from_ref(key).cast_mut().cast()) }+ __tsan_acquire(std::ptr::from_ref(key).cast_mut().cast());}}