Truly independent web browser
LibWeb: Pay layout rounds through safe functions
Andreas Kling24eaa5f6382d
diff --git a/Libraries/LibWeb/Rust/src/layout/commit.rs b/Libraries/LibWeb/Rust/src/layout/commit.rsindex 2c630c0dccf..1f54ea608e6 100644--- a/Libraries/LibWeb/Rust/src/layout/commit.rs+++ b/Libraries/LibWeb/Rust/src/layout/commit.rs@@ -71,11 +71,9 @@ impl CommitNotifications {.filter_map(|message| StyleNodeID::from_raw(message.style_node))}- /// # Safety- ///- /// The host must keep the document and node shells alive until these synchronous- /// notifications return. No mutable arena borrow may be active.- pub(crate) unsafe fn notify_host(+ /// Tells the host of the resets and messages of the commit, which it answers synchronously, while no mutable arena+ /// borrow is active.+ pub(crate) fn notify_host(self,main_thread: &crate::stage::MainThread,read: &crate::render_state::BegunRead,diff --git a/Libraries/LibWeb/Rust/src/layout/update_layout.rs b/Libraries/LibWeb/Rust/src/layout/update_layout.rsindex 2888a12a45f..72751afe9ae 100644--- a/Libraries/LibWeb/Rust/src/layout/update_layout.rs+++ b/Libraries/LibWeb/Rust/src/layout/update_layout.rs@@ -644,13 +644,8 @@ pub(crate) struct FlownRound {impl FlownRound {/// Pays what the round owes `host` in `read`, where no layout update goes on from it.- ///- /// # Safety- ///- /// The host's layout update callbacks must answer synchronously from its live document.- pub(crate) unsafe fn pay(mut self, main_thread: &MainThread, host: &DocumentHost, read: &BegunRead) {- // SAFETY: Guaranteed by the caller.- unsafe { self.answer.pay(main_thread, host, read) };+ pub(crate) fn pay(mut self, main_thread: &MainThread, host: &DocumentHost, read: &BegunRead) {+ self.answer.pay(main_thread, host, read);}}@@ -971,11 +966,7 @@ impl LayoutRoundAnswer {/// gained and lost and the host calls the round owes come first, then a build's commit messages/// and styled scroll containers, after which the document retires the tree the build replaced;/// then what the layout stages owe, and the scroll offsets the round's preparation clamped.- ///- /// # Safety- ///- /// The host's callbacks must answer synchronously from its live document.- pub(crate) unsafe fn pay(&mut self, main_thread: &MainThread, document_host: &DocumentHost, read: &BegunRead) {+ pub(crate) fn pay(&mut self, main_thread: &MainThread, document_host: &DocumentHost, read: &BegunRead) {let host = layout_update_host(document_host);let layout_host = FfiLayoutHostCallbacks::of(main_thread);document_host@@ -983,7 +974,6 @@ impl LayoutRoundAnswer {.owed_images.borrow_mut().append(&mut self.owed_images);- // SAFETY (for every call below): Guaranteed by the caller.std::mem::take(&mut self.work).pay(main_thread);if let Some((replaced_viewport, built)) = self.build.take() {layout_host.deliver_commit_messages(main_thread, read, &built.reports);@@ -994,7 +984,7 @@ impl LayoutRoundAnswer {return;}for commit in self.commits.drain(..) {- unsafe { commit.notify_host(main_thread, read, &layout_host) };+ commit.notify_host(main_thread, read, &layout_host);}super::trace::name_layout_trace_owners(main_thread, read);if let Some(geometry_host) = document_host.host_tables().geometry_host.get() {@@ -1125,11 +1115,7 @@ fn fly_first_round(/// Takes in the layout round that flew in the frame of `document_host`'s document, waiting for it to land, and pays it as/// a layout update pays its last round, where the round left the document laid out as of the frame. Answers whether it/// did: no other round runs, so what the host wrote since the frame flew stays the next layout update's.-///-/// # Safety-///-/// As for [`update_layout`].-unsafe fn take_flown_layout_in(main_thread: &MainThread, document_host: &DocumentHost, read: &BegunRead) -> bool {+fn take_flown_layout_in(main_thread: &MainThread, document_host: &DocumentHost, read: &BegunRead) -> bool {let host = layout_update_host(document_host);document_host.take_frame_in_with(read);let Some(FlownRound {@@ -1139,8 +1125,7 @@ unsafe fn take_flown_layout_in(main_thread: &MainThread, document_host: &Documenelse {return false;};- // SAFETY (for every pay below): Guaranteed by the caller.- unsafe { answer.pay(main_thread, document_host, read) };+ answer.pay(main_thread, document_host, read);if rebuilds_tree && host.reconcile_stale_list_item_counters_after_tree_build(main_thread, &answer.rebuilt_roots) {return false;}@@ -1158,10 +1143,7 @@ unsafe fn take_flown_layout_in(main_thread: &MainThread, document_host: &Documen!host.container_query_evaluation_is_pending(main_thread, read)}-/// # Safety-///-/// The layout update host's callbacks must answer synchronously from the live document.-unsafe fn update_layout(+fn update_layout(main_thread: &MainThread,document_host: &DocumentHost,read: &BegunRead,@@ -1231,7 +1213,7 @@ unsafe fn update_layout(NextRound::Job(job) => run_layout_round_job(document_host, read, job),};trace.note_build_reports(&answer);- unsafe { answer.pay(main_thread, document_host, read) };+ answer.pay(main_thread, document_host, read);if matches!(answer.end, LayoutRoundEnd::NeedsDocumentStyle) {let facts = host.document_facts(main_thread, read);next = NextRound::Job(next_round(main_thread, document_host, read, &host, facts, true).job);diff --git a/Libraries/LibWeb/Rust/src/layout/update_layout/main_thread_entries.rs b/Libraries/LibWeb/Rust/src/layout/update_layout/main_thread_entries.rsindex 639201be21e..e38ee12fe3c 100644--- a/Libraries/LibWeb/Rust/src/layout/update_layout/main_thread_entries.rs+++ b/Libraries/LibWeb/Rust/src/layout/update_layout/main_thread_entries.rs@@ -20,19 +20,17 @@ crate::stage::main_thread_ffi_entries!();/// # Safety////// `host` must be a live document host with registered layout and layout update hosts, on its document's thread-/// between `document_host_begin_update_layout` and its end, and `inputs` must remain valid for the call.+/// between `document_host_begin_update_layout` and its end.#[unsafe(no_mangle)]pub unsafe extern "C" fn render_state_update_layout(host: &DocumentHost,read: &BegunRead,- inputs: *const FfiLayoutUpdateInputs,+ inputs: &FfiLayoutUpdateInputs,) {- assert!(!inputs.is_null());// SAFETY: Guaranteed by the entry point's contract.let main_thread = unsafe { main_thread(host) };abort_on_panic(|| {- // SAFETY: Guaranteed by the entry point's contract.- unsafe { update_layout(&main_thread, host, read, &*inputs) };+ update_layout(&main_thread, host, read, inputs);attach_owed_image_resources(&main_thread, host, read);});}@@ -59,8 +57,7 @@ pub unsafe extern "C" fn render_state_pay_flown_round(host: &DocumentHost, read:abort_on_panic(|| {host.take_frame_in_with(read);if let Some(round) = host.take_flown_round() {- // SAFETY: Guaranteed by the entry point's contract.- unsafe { round.pay(&main_thread, host, read) };+ round.pay(&main_thread, host, read);}});}@@ -139,13 +136,11 @@ pub unsafe extern "C" fn render_state_seal_clock_plan(pub unsafe extern "C" fn render_state_seal_first_layout_round(host: &DocumentHost,read: &BegunRead,- inputs: *const FfiLayoutUpdateInputs,+ inputs: &FfiLayoutUpdateInputs,) {- assert!(!inputs.is_null());// SAFETY: Guaranteed by the entry point's contract.let main_thread = unsafe { main_thread(host) };- // SAFETY: Guaranteed by the entry point's contract.- abort_on_panic(|| unsafe { seal_first_round(&main_thread, host, read, &*inputs) });+ abort_on_panic(|| seal_first_round(&main_thread, host, read, inputs));}/// Lets the first round of the layout of a rendering update whose style is up to date fly beside the host, in `read`,@@ -157,22 +152,17 @@ pub unsafe extern "C" fn render_state_seal_first_layout_round(/// As for [`render_state_update_layout`], with no layout update running and no frame of the document in flight.#[unsafe(no_mangle)]pub unsafe extern "C" fn render_state_let_first_layout_round_fly(- host: *const DocumentHost,+ host: &DocumentHost,read: &BegunRead,- inputs: *const FfiLayoutUpdateInputs,+ inputs: &FfiLayoutUpdateInputs,blocker: crate::painting::ffi::FfiFlightBlocker,) -> bool {- assert!(!host.is_null(), "document host is null");- assert!(!inputs.is_null());- // SAFETY: Guaranteed by the caller.- let host = unsafe { &*host };// SAFETY: Guaranteed by the entry point's contract.let main_thread = unsafe { main_thread(host) };let Some(license) = FlightLicense::for_blocker(blocker) else {return false;};- // SAFETY: Guaranteed by the entry point's contract.- abort_on_panic(|| unsafe { fly_first_round(&main_thread, host, read, &*inputs, &license) })+ abort_on_panic(|| fly_first_round(&main_thread, host, read, inputs, &license))}/// Takes in the layout round that flew in the frame of `host`'s document, in `read`, and pays it, where it left the@@ -183,15 +173,11 @@ pub unsafe extern "C" fn render_state_let_first_layout_round_fly(////// As for [`render_state_update_layout`].#[unsafe(no_mangle)]-pub unsafe extern "C" fn render_state_take_flown_layout_in(host: *const DocumentHost, read: &BegunRead) -> bool {- assert!(!host.is_null(), "document host is null");- // SAFETY: Guaranteed by the caller.- let host = unsafe { &*host };+pub unsafe extern "C" fn render_state_take_flown_layout_in(host: &DocumentHost, read: &BegunRead) -> bool {// SAFETY: Guaranteed by the entry point's contract.let main_thread = unsafe { main_thread(host) };abort_on_panic(|| {- // SAFETY: Guaranteed by the entry point's contract.- let laid_out = unsafe { take_flown_layout_in(&main_thread, host, read) };+ let laid_out = take_flown_layout_in(&main_thread, host, read);attach_owed_image_resources(&main_thread, host, read);laid_out})