Truly independent web browser
LibWeb: Make small render state and painting helpers safe
Andreas Kling4fa2a5eb7939
diff --git a/Libraries/LibWeb/Rust/build.rs b/Libraries/LibWeb/Rust/build.rsindex be6b9c33a97..26515de3254 100644--- a/Libraries/LibWeb/Rust/build.rs+++ b/Libraries/LibWeb/Rust/build.rs@@ -2842,11 +2842,12 @@ fn main() -> Result<(), Box<dyn Error>> {layout_config.includes.push("LibWeb/Layout/TreeBuilderRustFFI.h".to_string());- // A compositor animation effect state is opaque to C++, which holds it by pointer.- layout_config- .export- .rename- .insert("CompositorAnimationEffectState".to_string(), "void".to_string());+ // A compositor animation effect state is opaque to C++, which holds it by pointer, and so is the paint server an+ // SVG paint resource callback fills.+ for name in ["CompositorAnimationEffectState", "PublishedSvgPaintServer"] {+ layout_config.export.rename.insert(name.to_string(), "void".to_string());+ layout_config.export.exclude.push(name.to_string());+ }generate_ffi_header(layout_config,&[diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rsindex 5732dfdbc3c..78ee20c841d 100644--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs@@ -3299,15 +3299,14 @@ impl LayoutNodeArena {if !self.has_style_engine() {return;}- // SAFETY: As with `with_style_store`, the engine outlives the arena's live nodes, and no- // reader keeps a borrow of the engine across the host call this publication arrives in.- let engine = unsafe { self.style_engine().get_mut() };- for atom in retained {- engine.retain_published_atom(crate::css::style::index::StyleAtomID(atom));- }- for atom in released {- engine.release_published_atom(crate::css::style::index::StyleAtomID(atom));- }+ self.with_style_engine(|engine| {+ for atom in retained {+ engine.retain_published_atom(crate::css::style::index::StyleAtomID(atom));+ }+ for atom in released {+ engine.release_published_atom(crate::css::style::index::StyleAtomID(atom));+ }+ });}pub(crate) fn with_counter_style_registry<T>(diff --git a/Libraries/LibWeb/Rust/src/painting/ffi.rs b/Libraries/LibWeb/Rust/src/painting/ffi.rsindex 1fcb207f6f8..ab6492ed846 100644--- a/Libraries/LibWeb/Rust/src/painting/ffi.rs+++ b/Libraries/LibWeb/Rust/src/painting/ffi.rs@@ -537,16 +537,13 @@ pub struct FfiImageMapArea {pub coords_count: u32,}-/// # Safety-///-/// `sink` must be the pointer handed to the callback, used synchronously.+/// Adds a color stop to `sink`, the paint server handed to the callback.#[unsafe(no_mangle)]-pub unsafe extern "C" fn layout_arena_paint_push_color_stop(- sink: *mut c_void,+pub extern "C" fn layout_arena_paint_push_color_stop(+ sink: &mut crate::painting::svg_paint_resources::PublishedSvgPaintServer,color: libgfx_rust::Color,position: f32,) {- let sink = unsafe { &mut *sink.cast::<crate::painting::svg_paint_resources::PublishedSvgPaintServer>() };if let crate::painting::svg_paint_resources::PublishedSvgPaintServer::Gradient(gradient) = sink {gradient.stops@@ -554,19 +551,15 @@ pub unsafe extern "C" fn layout_arena_paint_push_color_stop(}}-/// # Safety-///-/// `sink` must be the pointer handed to the callback, used synchronously, and `description`-/// must be readable.+/// Makes `sink`, the paint server handed to the callback, the gradient `description` describes.#[unsafe(no_mangle)]-pub unsafe extern "C" fn layout_arena_svg_paint_resources_push_gradient(- sink: *mut c_void,- description: *const crate::painting::host::FfiSvgGradientDescription,+pub extern "C" fn layout_arena_svg_paint_resources_push_gradient(+ sink: &mut crate::painting::svg_paint_resources::PublishedSvgPaintServer,+ description: &crate::painting::host::FfiSvgGradientDescription,) {- let sink = unsafe { &mut *sink.cast::<crate::painting::svg_paint_resources::PublishedSvgPaintServer>() };*sink = crate::painting::svg_paint_resources::PublishedSvgPaintServer::Gradient(crate::painting::svg_paint_resources::PublishedSvgGradient {- description: unsafe { *description },+ description: *description,stops: Vec::new(),},);@@ -574,20 +567,18 @@ pub unsafe extern "C" fn layout_arena_svg_paint_resources_push_gradient(/// # Safety///-/// `sink` must be the pointer handed to the callback, used synchronously, `description` must be-/// readable, and `css_transform_entries` must point at `css_transform_count` readable-/// `ComputedResolvedTransform` values of a live computed style.+/// `css_transform_entries` must point at `css_transform_count` readable `ComputedResolvedTransform` values of a live+/// computed style.#[unsafe(no_mangle)]pub unsafe extern "C" fn layout_arena_svg_paint_resources_push_pattern(- sink: *mut c_void,- description: *const crate::painting::host::FfiSvgPatternDescription,+ sink: &mut crate::painting::svg_paint_resources::PublishedSvgPaintServer,+ description: &crate::painting::host::FfiSvgPatternDescription,css_transform_entries: *const c_void,css_transform_count: usize,) {- let sink = unsafe { &mut *sink.cast::<crate::painting::svg_paint_resources::PublishedSvgPaintServer>() };*sink = crate::painting::svg_paint_resources::PublishedSvgPaintServer::Pattern(crate::painting::svg_paint_resources::PublishedSvgPattern {- description: unsafe { *description },+ description: *description,css_transform: unsafe {ak::slice_from_raw_parts(css_transform_entries.cast::<crate::css::computed_value_types::ComputedResolvedTransform>(),diff --git a/Libraries/LibWeb/Rust/src/painting/host/hit_test.rs b/Libraries/LibWeb/Rust/src/painting/host/hit_test.rsindex 785fa5a5f84..07881c22b21 100644--- a/Libraries/LibWeb/Rust/src/painting/host/hit_test.rs+++ b/Libraries/LibWeb/Rust/src/painting/host/hit_test.rs@@ -19,7 +19,7 @@ pub struct FfiHitTestQueryCallbacks {pub has_chrome_metrics: bool,pub chrome_metrics: crate::painting::ffi::FfiChromeMetrics,/// Private: reached only through the method below, which takes the main thread token.- contains: unsafe extern "C" fn(*mut c_void, FfiNodeIdentity, FfiNodeIdentity) -> bool,+ contains: extern "C" fn(*mut c_void, FfiNodeIdentity, FfiNodeIdentity) -> bool,}impl FfiHitTestQueryCallbacks {@@ -31,7 +31,7 @@ impl FfiHitTestQueryCallbacks {scroll_offsets: &[libgfx_rust::FloatPoint],chrome_metrics: crate::painting::ffi::FfiChromeMetrics,) -> Self {- unsafe extern "C" fn no_contains(_: *mut c_void, _: FfiNodeIdentity, _: FfiNodeIdentity) -> bool {+ extern "C" fn no_contains(_: *mut c_void, _: FfiNodeIdentity, _: FfiNodeIdentity) -> bool {panic!("a point query on the render owner asks the host nothing of the DOM");}Self {@@ -52,8 +52,7 @@ impl FfiHitTestQueryCallbacks {ancestor: FfiNodeIdentity,node: FfiNodeIdentity,) -> bool {- // SAFETY: The C++ host answers synchronously.- unsafe { (self.contains)(self.context, ancestor, node) }+ (self.contains)(self.context, ancestor, node)}pub(crate) fn scroll_offsets(&self) -> &[libgfx_rust::FloatPoint] {if self.scroll_offsets.is_null() {diff --git a/Libraries/LibWeb/Rust/src/painting/svg_paint_resources.rs b/Libraries/LibWeb/Rust/src/painting/svg_paint_resources.rsindex 522c113c37f..61e62f981ed 100644--- a/Libraries/LibWeb/Rust/src/painting/svg_paint_resources.rs+++ b/Libraries/LibWeb/Rust/src/painting/svg_paint_resources.rs@@ -50,7 +50,7 @@ pub(crate) struct PublishedSvgPattern {}#[derive(Clone, Debug, Default, PartialEq)]-pub(crate) enum PublishedSvgPaintServer {+pub enum PublishedSvgPaintServer {#[default]None,Gradient(PublishedSvgGradient),diff --git a/Libraries/LibWeb/Rust/src/render_state.rs b/Libraries/LibWeb/Rust/src/render_state.rsindex 3b6dd3a1a22..1a3a2186738 100644--- a/Libraries/LibWeb/Rust/src/render_state.rs+++ b/Libraries/LibWeb/Rust/src/render_state.rs@@ -146,8 +146,7 @@ impl RenderState {/// Applies `changes`, writes the host queued, in the order the host made them.fn apply(&mut self, changes: impl IntoIterator<Item = ArenaChange>) {for change in changes {- // SAFETY: The state is borrowed mutably, and so is the engine its arena links.- unsafe { change.apply(self.arena.arena_mut(), self.engine, &mut self.owed) };+ change.apply(self);}}@@ -364,15 +363,10 @@ impl ArenaChange {}impl ArenaChange {- /// # Safety- ///- /// `engine` must name the live style engine `arena` links, which nothing else borrows meanwhile.- unsafe fn apply(- self,- arena: &mut crate::layout::LayoutNodeArena,- engine: StyleEngineHandle,- owed: &mut Vec<crate::layout::tree_mutation::HostWorkDue>,- ) {+ /// Applies the change to `state`. A style change reaches the engine only through the state's own borrow of it.+ fn apply(self, state: &mut RenderState) {+ let arena = state.arena.arena_mut();+ let owed = &mut state.owed;match self {Self::Layout(change) => {let host_work = change.apply_owing(arena);@@ -397,12 +391,9 @@ impl ArenaChange {}Self::RemoveBox(removal) => removal.apply(arena, owed),Self::Paint(change) => change.apply(arena),- // SAFETY: Guaranteed by the caller. A style change reaches the engine only through this borrow.- Self::Style(change) => change.apply(unsafe { engine.get_mut() }),- // SAFETY: As above.- Self::Engine(write) => write.apply(unsafe { engine.get_mut() }),- // SAFETY: As above.- Self::Rule(write) => write.apply(unsafe { engine.get_mut() }),+ Self::Style(change) => change.apply(state.engine_mut()),+ Self::Engine(write) => write.apply(state.engine_mut()),+ Self::Rule(write) => write.apply(state.engine_mut()),}}diff --git a/Libraries/LibWeb/Rust/src/render_state/clock/hover.rs b/Libraries/LibWeb/Rust/src/render_state/clock/hover.rsindex 1ddd8e2742d..4895f9b047e 100644--- a/Libraries/LibWeb/Rust/src/render_state/clock/hover.rs+++ b/Libraries/LibWeb/Rust/src/render_state/clock/hover.rs@@ -1417,12 +1417,9 @@ fn cursor_for_hit(state: &mut RenderState, hit: CursorHit) -> Option<u8> {/// or out of the context, beside the mouse event with the input event id `input_event_id`, and answers what the lane/// wants next, as a `PointerAnswer`.///-/// # Safety-///-/// `ticks` must come from `document_host_lease_clock` and not be released yet.#[unsafe(no_mangle)]-pub unsafe extern "C" fn clock_ticks_pointer_moved(- ticks: *const super::ClockTicks,+pub extern "C" fn clock_ticks_pointer_moved(+ ticks: &super::ClockTicks,has_position: bool,x: f32,y: f32,@@ -1430,8 +1427,6 @@ pub unsafe extern "C" fn clock_ticks_pointer_moved(scrolled_since_frame: bool,input_event_id: u64,) -> u8 {- // SAFETY: Guaranteed by the caller, whose reference this borrows.- let ticks = std::mem::ManuallyDrop::new(unsafe { std::sync::Arc::from_raw(ticks) });ticks.pointer_moved(PendingPointer {position: has_position.then_some(FloatPoint { x, y }),buttons,