Skip to content

Truly independent web browser

LibWeb: Trace the targeted style update the layout bound failure makes

sideshowbarker7182b5810560
diff --git a/Libraries/LibWeb/Layout/UpdateLayout.cpp b/Libraries/LibWeb/Layout/UpdateLayout.cpp
index 11d14877cb6..4b84ebc9831 100644
--- a/Libraries/LibWeb/Layout/UpdateLayout.cpp
+++ b/Libraries/LibWeb/Layout/UpdateLayout.cpp
@@ -7,6 +7,7 @@
#include <AK/Array.h>
#include <AK/ScopeGuard.h>
#include <AK/StringBuilder.h>
+#include <AK/Vector.h>
#include <LibGfx/FontCascadeList.h>
#include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/CSS/StyleEngineBridge.h>
@@ -88,6 +89,12 @@ static void describe_node_for_bound_failure(Document& document, Layout::BegunRea
builder.append(",owes-input"sv);
if (facts.ancestor_owes_style_input)
builder.append(",ancestor-owes-input"sv);
+ // The record the engine holds the node on against the one the element holds, the environment the
+ // engine holds for it, whether its record has a cascade state, and the derived records the host
+ // has not taken.
+ builder.appendff(",rec:{}/{},env:{},cs:{}", facts.assigned_record, element->style_record_identity().value(), facts.environment, facts.has_cascade_state ? 1 : 0);
+ if (facts.pending_records)
+ builder.appendff(",pending:{}({}>{})", facts.pending_records, facts.pending_old_record, facts.pending_new_record);
auto* flat_tree_parent_element = element->flat_tree_parent_element();
u32 flat_tree_parent_style_node = flat_tree_parent_element ? flat_tree_parent_element->style_node_id().value() : 0;
if (facts.inheritance_parent != flat_tree_parent_style_node || facts.flat_tree_parent != flat_tree_parent_style_node)
@@ -101,8 +108,39 @@ static void describe_node_for_bound_failure(Document& document, Layout::BegunRea
if (element->has_attribute(name))
builder.appendff(" {}", name);
}
- // What one more targeted style update of the element answers now.
- builder.appendff(" targeted-update:{}", document.update_style_for_element(AbstractElement { *element }) ? "styled"sv : "unstyled"sv);
+ // What one more targeted style update of the element answers now, with the engine's trace of the
+ // update and the engine counters the update moved.
+ auto& style_engine = document.style_computer().style_engine();
+ Vector<u64> counters_before;
+ for (size_t index = 0;; ++index) {
+ StringView name;
+ u64 value = 0;
+ if (!style_engine.counter(read, index, name, value))
+ break;
+ counters_before.append(value);
+ }
+ CSS::StyleEngineFFI::style_engine_begin_bound_failure_trace(engine_host);
+ bool styled = document.update_style_for_element(AbstractElement { *element });
+ Array<u8, 512> trace_buffer;
+ auto trace_length = CSS::StyleEngineFFI::style_engine_end_bound_failure_trace(engine_host, trace_buffer.data(), trace_buffer.size());
+ builder.appendff(" targeted-update:{} trace:[{}] counters:[", styled ? "styled"sv : "unstyled"sv, StringView { reinterpret_cast<char const*>(trace_buffer.data()), trace_length });
+ bool first_counter = true;
+ for (size_t index = 0; index < counters_before.size(); ++index) {
+ StringView name;
+ u64 value = 0;
+ if (!style_engine.counter(read, index, name, value))
+ break;
+ if (value == counters_before[index])
+ continue;
+ // Only the counters about deriving records and answering demands: a targeted update moves dozens
+ // of others, which would crowd these out of the report.
+ if (!name.contains("engineComputedRecord"sv) && !name.contains("Demand"sv) && !name.contains("Bail"sv) && !name.contains("Abandon"sv))
+ continue;
+ auto delta = static_cast<i64>(value) - static_cast<i64>(counters_before[index]);
+ builder.appendff("{}{}{}{}", first_counter ? ""sv : ","sv, name, delta > 0 ? "+"sv : ""sv, delta);
+ first_counter = false;
+ }
+ builder.append(']');
}
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/bridge.rs b/Libraries/LibWeb/Rust/src/css/style/bridge.rs
index 59e722b97d6..ee3c066bd97 100644
--- a/Libraries/LibWeb/Rust/src/css/style/bridge.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/bridge.rs
@@ -2593,8 +2593,18 @@ pub(crate) fn answer_record_demand(
style_node: StyleNodeID,
demand: super::publication::RecordDemand,
) -> FfiRecordDemandAnswer {
- let read_only = demand.is_read_only();
- match engine.answer_record_demand(style_node, demand) {
+ let answer = engine.answer_record_demand(style_node, demand);
+ ffi_record_demand_answer(engine, style_node, demand.is_read_only(), answer)
+}
+
+/// The FFI form of `answer`, what a demand of `style_node`'s record answered.
+pub(in crate::css::style) fn ffi_record_demand_answer(
+ engine: &mut StyleEngine,
+ style_node: StyleNodeID,
+ read_only: bool,
+ answer: Result<super::publication::RecordDemandAnswer, super::publication::Unanswered>,
+) -> FfiRecordDemandAnswer {
+ match answer {
Ok(super::publication::RecordDemandAnswer::Record {
record,
uses_substitution,
diff --git a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
index 7f481d2ca62..e36b7d78009 100644
--- a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
@@ -824,6 +824,9 @@ pub(crate) struct EngineMemo {
/// Whether the transaction the host takes is one a style update applies, whose reactions close over the elements
/// they inherit through as the engine computes them.
takes_transaction_to_apply: std::cell::Cell<bool>,
+ /// The trace of the targeted style update the panic of a layout update that ran out of passes makes of the
+ /// element it kept reaching without a style, while the update is traced.
+ bound_failure_trace: std::cell::RefCell<Option<String>>,
}
impl EngineMemo {
@@ -919,6 +922,7 @@ impl Default for EngineMemo {
declarations_changed_during_apply: Default::default(),
applying_reactions: Default::default(),
takes_transaction_to_apply: Default::default(),
+ bound_failure_trace: Default::default(),
}
}
}
@@ -1230,6 +1234,16 @@ pub struct FfiBoundFailureNodeFacts {
/// How a read-only demand of the node's record answers: 0 for a record, 1 for absent, 2 for refused, 3 for
/// suspended, 4 for a parent that holds no record yet.
pub read_demand_answer: u8,
+ /// The records the engine derived for the element that the host has not taken: how many, and the records the
+ /// last of the element's own moves between, or 0.
+ pub pending_records: u8,
+ pub pending_old_record: u64,
+ pub pending_new_record: u64,
+ /// The record the engine holds the node on and the custom-property environment it holds, or 0, and whether a
+ /// cascade state is bound to the node's record.
+ pub assigned_record: u64,
+ pub environment: u64,
+ pub has_cascade_state: bool,
}
/// Answers what the engine knows of `node` for the panic of a layout update that ran out of passes reaching it without
@@ -1251,6 +1265,12 @@ pub extern "C" fn style_engine_bound_failure_node_facts(
owes_style_input: false,
ancestor_owes_style_input: false,
read_demand_answer: 2,
+ pending_records: 0,
+ pending_old_record: 0,
+ pending_new_record: 0,
+ assigned_record: 0,
+ environment: 0,
+ has_cascade_state: false,
};
};
let published_display = engine.element_published_box_facts(node).map_or(0, |facts| {
@@ -1267,6 +1287,8 @@ pub extern "C" fn style_engine_bound_failure_node_facts(
engine.retained.tree.inheritance_parent(ancestor)
})
.any(|ancestor| engine.host.owes_element_style_input(ancestor));
+ let (pending_records, pending_old_record, pending_new_record) =
+ engine.retained.bound_failure_pending_records(node);
let read_demand_answer =
match engine.answer_record_demand(node, RecordDemand::Element(FfiRecordDemand::ElementRead)) {
Ok(RecordDemandAnswer::Record { .. }) => 0,
@@ -1284,10 +1306,72 @@ pub extern "C" fn style_engine_bound_failure_node_facts(
owes_style_input: engine.host.owes_element_style_input(node),
ancestor_owes_style_input,
read_demand_answer,
+ pending_records,
+ pending_old_record,
+ pending_new_record,
+ assigned_record: engine
+ .retained
+ .computed_group_sets
+ .assigned_style_record(node)
+ .map_or(0, |record| record.raw()),
+ environment: engine
+ .retained
+ .computed_group_sets
+ .custom_property_environment_identity(node)
+ .unwrap_or(0),
+ has_cascade_state: engine
+ .retained
+ .computed_group_sets
+ .cascade_state(super::computed::ComputedStyleTarget::new(node, u8::MAX))
+ .is_some(),
}
})
}
+/// Appends what `line` says to the trace of a targeted style update of `host`'s document, where one is being
+/// traced.
+pub(crate) fn note_bound_failure_trace(host: &DocumentHost, line: impl FnOnce() -> String) {
+ if let Some(trace) = host.engine_memo().bound_failure_trace.borrow_mut().as_mut() {
+ if !trace.is_empty() {
+ trace.push(' ');
+ }
+ trace.push_str(&line());
+ }
+}
+
+/// Starts tracing the targeted style updates of `host`'s document, for the panic of a layout update that ran out of
+/// passes.
+#[unsafe(no_mangle)]
+pub extern "C" fn style_engine_begin_bound_failure_trace(host: &DocumentHost) {
+ *host.engine_memo().bound_failure_trace.borrow_mut() = Some(String::new());
+}
+
+/// Ends the trace and writes it to `buffer`, NUL-terminated and cut to fit `capacity`, answering its length.
+///
+/// # Safety
+///
+/// `buffer` must be writable for `capacity` bytes.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn style_engine_end_bound_failure_trace(
+ host: &DocumentHost,
+ buffer: *mut u8,
+ capacity: usize,
+) -> usize {
+ let trace = host
+ .engine_memo()
+ .bound_failure_trace
+ .borrow_mut()
+ .take()
+ .unwrap_or_default();
+ let length = trace.len().min(capacity.saturating_sub(1));
+ // SAFETY: Guaranteed by the caller.
+ unsafe {
+ std::ptr::copy_nonoverlapping(trace.as_ptr(), buffer, length);
+ buffer.add(length).write(0);
+ }
+ length
+}
+
#[unsafe(no_mangle)]
pub extern "C" fn style_engine_has_pending_transaction(host: &DocumentHost, read: &BegunRead) -> bool {
match host.known_engine_facts() {
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication.rs b/Libraries/LibWeb/Rust/src/css/style/publication.rs
index acc65fb1ee3..65a0bef47d8 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication.rs
@@ -1363,6 +1363,24 @@ impl RetainedState {
self.mark_published_answer_observed(node);
}
+ /// The records the engine derived for `node`'s element that the host has not taken, for the
+ /// panic of a layout update that ran out of passes: how many, and the records the last of the
+ /// element's own moves between.
+ pub(crate) fn bound_failure_pending_records(&self, node: StyleNodeID) -> (u8, u64, u64) {
+ let Some(pending_records) = self.engine_computed_records_pending.get(&node) else {
+ return (0, 0, 0);
+ };
+ let last = pending_records
+ .iter()
+ .rev()
+ .find(|pending| pending.pseudo_kind == u8::MAX);
+ (
+ u8::try_from(pending_records.len()).unwrap_or(u8::MAX),
+ last.map_or(0, |pending| pending.old_style_record.raw()),
+ last.map_or(0, |pending| pending.new_style_record.raw()),
+ )
+ }
+
/// The host computed the target's record itself: the record the engine derived for it is not
/// installed, and a composition the derivation detached gives way to what the host published.
pub(super) fn forget_engine_computed_record(&mut self, target: computed::ComputedStyleTarget) {
diff --git a/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs b/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs
index 537027aaa1e..0a0c9b1105f 100644
--- a/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs
@@ -18,7 +18,9 @@ use super::bridge::{
FfiStyleInvalidationField, PSEUDO_RECORD_SLOTS, close_style_reactions_over_inheritance,
sort_style_deltas_for_direct_application,
};
-use super::engine_calls::{absorb_element_style_input, has_deferred_element_style_input, with_engine};
+use super::engine_calls::{
+ absorb_element_style_input, has_deferred_element_style_input, note_bound_failure_trace, with_engine,
+};
use super::publication::RecordDemand;
use super::transaction::{
STYLE_REACTION_ANCESTOR_BECAME_VISIBLE, STYLE_REACTION_INHERITED_CUSTOM_PROPERTIES, STYLE_REACTION_INHERITED_STYLE,
@@ -277,20 +279,32 @@ impl Application<'_> {
/// resolved the record's environment over the parent's own; where the parent's inheritable environment differs,
/// it takes back what the demand derived.
fn answer_record_demand(self, element: &FfiReactionElement, node: StyleNodeID) -> Option<FfiEngineComputedRecord> {
+ let demand = RecordDemand::Element(FfiRecordDemand::TargetedElement);
+ let mut refusal = None;
let answer = with_engine(self.read, self.host, |engine| {
- super::bridge::answer_record_demand(engine, node, RecordDemand::Element(FfiRecordDemand::TargetedElement))
+ let answer = engine.answer_record_demand(node, demand);
+ if let Err(unanswered) = &answer {
+ refusal = Some(*unanswered);
+ }
+ super::bridge::ffi_record_demand_answer(engine, node, demand.is_read_only(), answer)
})
.record;
+ if let Some(unanswered) = refusal {
+ note_bound_failure_trace(self.host, || format!("unanswered:{unanswered:?}"));
+ }
if answer.style_record == 0 {
+ note_bound_failure_trace(self.host, || "demand:none".to_string());
return None;
}
if !self.record_environment_is_installable(element, node, answer.style_record) {
+ note_bound_failure_trace(self.host, || format!("demand:uninstallable:{}", answer.style_record));
self.host
.queue_change(ArenaChange::Style(StyleChange::AbandonDemandedRecords {
node: Some(node),
}));
return None;
}
+ note_bound_failure_trace(self.host, || format!("demand:record:{}", answer.style_record));
Some(answer)
}
@@ -305,6 +319,16 @@ impl Application<'_> {
})
.any(|ancestor| engine.has_deferred_element_style_input(ancestor))
});
+ note_bound_failure_trace(self.host, || {
+ format!(
+ "refuse:{}",
+ if ancestor_owes_style_input {
+ "ancestor-owes"
+ } else {
+ "consume"
+ }
+ )
+ });
if ancestor_owes_style_input {
// SAFETY: The host lends its application for the call that applies the batch.
unsafe {
@@ -819,6 +843,12 @@ impl Application<'_> {
embedding_document_layout_was_stale: bool,
) -> FfiTargetedStyleAnswer {
let scan = self.scan_inheritance_chain(first, true);
+ note_bound_failure_trace(self.host, || {
+ format!(
+ "scan:{:?}/{:?}/{:?}",
+ scan.topmost_requiring_style, scan.nearest_display_none, scan.topmost_display_none
+ )
+ });
if mode == FfiStyleUpdateMode::StopAtDisplayNone && scan.display_none_above_every_element_requiring_style() {
return FfiTargetedStyleAnswer::Unstyled;
}
@@ -842,8 +872,17 @@ impl Application<'_> {
.map(|element| element.style_node)
.collect();
let mut descendant_style_recompute_needed = false;
+ note_bound_failure_trace(self.host, || format!("chain:{}", chain.len()));
for &style_node in chain.iter().rev() {
let element = self.element(style_node);
+ note_bound_failure_trace(self.host, || {
+ format!(
+ "node:{}:{}{}",
+ style_node,
+ if element.has_style { "styled" } else { "unstyled" },
+ if element.element.is_null() { ",gone" } else { "" }
+ )
+ });
let Some(node) = StyleNodeID::from_raw(style_node).filter(|_| !element.element.is_null()) else {
return FfiTargetedStyleAnswer::HeldStyle;
};
@@ -867,6 +906,14 @@ impl Application<'_> {
// The engine can refuse a first style, which leaves the rest of the chain nothing to inherit from.
let element = self.element(style_node);
+ note_bound_failure_trace(self.host, || {
+ format!(
+ "after:{}{}:{}",
+ if element.has_style { "styled" } else { "unstyled" },
+ if element.display_none { ",none" } else { "" },
+ element.style_record
+ )
+ });
if !element.has_style {
return FfiTargetedStyleAnswer::HeldStyle;
}
diff --git a/Libraries/LibWeb/Rust/src/layout/update_layout.rs b/Libraries/LibWeb/Rust/src/layout/update_layout.rs
index ece76b6832f..2888a12a45f 100644
--- a/Libraries/LibWeb/Rust/src/layout/update_layout.rs
+++ b/Libraries/LibWeb/Rust/src/layout/update_layout.rs
@@ -153,7 +153,7 @@ pub struct FfiStabilizationBoundFailureHostFacts {
pub document_child_needs_layout_tree_update: bool,
/// The element the last unstyled-element report of the update named, with its flat tree ancestors, as the host
/// describes them: a NUL-terminated ASCII string.
- pub last_unstyled_element: [u8; 640],
+ pub last_unstyled_element: [u8; 2048],
/// As above, for the element the last rebuild request of the update named.
pub last_rebuild_request: [u8; 640],
}