Truly independent web browser
LibWeb: Mark style group payload comparisons as unsafe
Andreas Kling9df93bf1dc08
diff --git a/Libraries/LibWeb/Rust/src/css/computed_values.rs b/Libraries/LibWeb/Rust/src/css/computed_values.rsindex f09b2dcd34b..c0b461cafbf 100644--- a/Libraries/LibWeb/Rust/src/css/computed_values.rs+++ b/Libraries/LibWeb/Rust/src/css/computed_values.rs@@ -1381,7 +1381,10 @@ pub(crate) fn style_group_affects_layout(group_index: usize) -> bool {/// The style groups in `groups` whose payloads differ between two records, `[base, composed]` each: explicit/// inheritance reads both the base and the composition over it. `None` when a record holds fewer groups.-pub(crate) fn changed_style_groups(+///+/// # Safety+/// Every payload must be a live payload of the group at its index.+pub(crate) unsafe fn changed_style_groups(old: [&[crate::css::host_shared::SharedPayload]; 2],new: [&[crate::css::host_shared::SharedPayload]; 2],groups: std::ops::Range<usize>,@@ -1390,7 +1393,8 @@ pub(crate) fn changed_style_groups(for group in groups {for (before, after) in old.iter().zip(new.iter()) {let (&before, &after) = (before.get(group)?, after.get(group)?);- if before != after && !style_group_payloads_equal(group, before.as_ptr(), after.as_ptr()) {+ // SAFETY: The caller guarantees live payloads of each group.+ if before != after && !unsafe { style_group_payloads_equal(group, before.as_ptr(), after.as_ptr()) } {changed |= 1 << group;break;}@@ -1399,11 +1403,14 @@ pub(crate) fn changed_style_groups(Some(changed)}-pub(crate) fn style_group_payloads_equal(group_index: usize, a: *const c_void, b: *const c_void) -> bool {+/// Whether two payloads of the group at `group_index` hold the same values.+///+/// # Safety+/// `a` and `b` must be live payloads of the group at `group_index`.+pub(crate) unsafe fn style_group_payloads_equal(group_index: usize, a: *const c_void, b: *const c_void) -> bool {assert!(!a.is_null());assert!(!b.is_null());- // SAFETY: Published style-group payloads remain live for the call and both use the registered- // group type at `group_index`.+ // SAFETY: The caller guarantees live payloads of the group.unsafe { payloads_equal(style_group(group_index), a, b) }}@@ -1411,12 +1418,14 @@ pub(crate) fn style_group_payloads_equal(group_index: usize, a: *const c_void, b/// two payloads that compare equal always hash equal, whoever built them and wherever they live./// That is what lets a payload's identity be its content instead of its address. The group/// index is hashed with the content so payloads of different groups never share a bucket.-pub(crate) fn style_group_payloads_hash(group_index: usize, payload: *const c_void) -> u64 {+///+/// # Safety+/// `payload` must be a live payload of the group at `group_index`.+pub(crate) unsafe fn style_group_payloads_hash(group_index: usize, payload: *const c_void) -> u64 {assert!(!payload.is_null());let mut hasher = fast_hasher();hasher.write_usize(group_index);- // SAFETY: Published style-group payloads remain live for the call and use the registered- // group type at `group_index`.+ // SAFETY: The caller guarantees a live payload of the group.unsafe { payload_content_hash(style_group(group_index), payload, &mut hasher) };hasher.finish()}@@ -1547,10 +1556,12 @@ unsafe fn payload_holds_image_values(group: StyleGroup, payload: *const c_void)/// node loads and observes: the background and mask layers, the cursor, the border-image source and/// the list-style image. Nearly every style holds none, and a record answers that with one flag/// instead of materializing its layers to find out.-pub(crate) fn style_group_payloads_hold_image_values(payloads: &[*const c_void]) -> bool {+///+/// # Safety+/// Every non-null payload must be a live payload of the group at its index.+pub(crate) unsafe fn style_group_payloads_hold_image_values(payloads: &[*const c_void]) -> bool {payloads.iter().enumerate().any(|(group_index, &payload)| {- // SAFETY: Published style-group payloads remain live for the call and use the registered- // group type at `group_index`.+ // SAFETY: The caller guarantees live payloads of each group.!payload.is_null() && unsafe { payload_holds_image_values(style_group(group_index), payload) }})}@@ -1558,7 +1569,10 @@ pub(crate) fn style_group_payloads_hold_image_values(payloads: &[*const c_void])/// Whether the group payloads `old` and `new`, of two records, hold different `<image>` values in the properties whose/// images a layout node loads and observes, by identity. Where they hold the same ones, a box that moves from one/// record to the other keeps the images it loads and the facts it paints them with.-pub(crate) fn style_group_payloads_move_image_values(old: &[*const c_void], new: &[*const c_void]) -> bool {+///+/// # Safety+/// Every non-null payload must be a live payload of the group at its index.+pub(crate) unsafe fn style_group_payloads_move_image_values(old: &[*const c_void], new: &[*const c_void]) -> bool {old.iter().zip(new).enumerate()@@ -1933,7 +1947,8 @@ pub unsafe extern "C" fn rust_style_group_payloads_equal(a: *const c_void,b: *const c_void,) -> bool {- style_group_payloads_equal(group_index, a, b)+ // SAFETY: The caller guarantees live payloads of the group.+ unsafe { style_group_payloads_equal(group_index, a, b) }}/// One field of a style group the generic builder can populate or check: a pokeable simple field@@ -3930,10 +3945,13 @@ mod tests {let default = default_group_payload(group_index);// SAFETY: The default payload is a live payload of its group.let clone = unsafe { rust_style_group_clone(group_index, default) };- assert!(style_group_payloads_equal(group_index, default, clone));+ // SAFETY: The payloads are live defaults and clones of this group.+ assert!(unsafe { style_group_payloads_equal(group_index, default, clone) });assert_eq!(- style_group_payloads_hash(group_index, default),- style_group_payloads_hash(group_index, clone),+ // SAFETY: The payloads are live defaults and clones of this group.+ unsafe { style_group_payloads_hash(group_index, default) },+ // SAFETY: The payloads are live defaults and clones of this group.+ unsafe { style_group_payloads_hash(group_index, clone) },"a clone must hash like the payload it was copied from");refcount_of(clone, payload_align(style_group(group_index))).store(0, Ordering::Relaxed);@@ -3947,14 +3965,13 @@ mod tests {changed.border_spacing_horizontal = 17;let changed_payload = (&raw const changed).cast::<c_void>();let table_default = default_group_payload(group_index::INHERITED_TABLE);- assert!(!style_group_payloads_equal(- group_index::INHERITED_TABLE,- table_default,- changed_payload- ));+ // SAFETY: The payloads are live defaults and clones of this group.+ assert!(!unsafe { style_group_payloads_equal(group_index::INHERITED_TABLE, table_default, changed_payload) });assert_ne!(- style_group_payloads_hash(group_index::INHERITED_TABLE, table_default),- style_group_payloads_hash(group_index::INHERITED_TABLE, changed_payload)+ // SAFETY: The payloads are live defaults and clones of this group.+ unsafe { style_group_payloads_hash(group_index::INHERITED_TABLE, table_default) },+ // SAFETY: The payloads are live defaults and clones of this group.+ unsafe { style_group_payloads_hash(group_index::INHERITED_TABLE, changed_payload) });}}diff --git a/Libraries/LibWeb/Rust/src/css/style/bridge.rs b/Libraries/LibWeb/Rust/src/css/style/bridge.rsindex 41e23970b82..10143eb4fc4 100644--- a/Libraries/LibWeb/Rust/src/css/style/bridge.rs+++ b/Libraries/LibWeb/Rust/src/css/style/bridge.rs@@ -1962,10 +1962,12 @@ pub(crate) fn publish_computed_groups_from_inputs(inherited_group_swap_candidate && table.property_inheritance_is_standard() && !table.display_is_list_item()});let holds_image_values =- crate::css::computed_values::style_group_payloads_hold_image_values(HostShared::as_pointer_slice(payloads))- || crate::css::computed_values::style_group_payloads_hold_image_values(HostShared::as_pointer_slice(+ // SAFETY: Published style-group payloads stay live while their records do.+ unsafe { crate::css::computed_values::style_group_payloads_hold_image_values(HostShared::as_pointer_slice(payloads)) }+ // SAFETY: Published style-group payloads stay live while their records do.+ || unsafe { crate::css::computed_values::style_group_payloads_hold_image_values(HostShared::as_pointer_slice(animation_overlay_payloads,- ));+ )) };let dependency_flags = longhand_table.map_or(0, |table| table.publication_dependency_flags())| (u8::from(inherited_group_swap_eligible) * super::computed::INHERITED_GROUP_SWAP_ELIGIBLE)| (u8::from(holds_image_values) * super::computed::HOLDS_IMAGE_VALUES);@@ -2349,7 +2351,8 @@ pub unsafe extern "C" fn style_engine_changed_style_groups(let (Some(old), Some(new)) = (payloads(old), payloads(new)) else {return u32::MAX;};- crate::css::computed_values::changed_style_groups(old, new, first_group..end_group).unwrap_or(u32::MAX)+ // SAFETY: Published style-group payloads stay live while their records do.+ unsafe { crate::css::computed_values::changed_style_groups(old, new, first_group..end_group) }.unwrap_or(u32::MAX)}/// Returns a synchronous borrowed view of a base or live animation-overlay record.diff --git a/Libraries/LibWeb/Rust/src/css/style/child_reactions.rs b/Libraries/LibWeb/Rust/src/css/style/child_reactions.rsindex 7754223eddc..9f80afeedf2 100644--- a/Libraries/LibWeb/Rust/src/css/style/child_reactions.rs+++ b/Libraries/LibWeb/Rust/src/css/style/child_reactions.rs@@ -233,11 +233,14 @@ impl StyleEngine {) else {return u32::MAX;};- let Some(groups) = crate::css::computed_values::changed_style_groups(- [old.base_payloads, old.payloads],- [new.base_payloads, new.payloads],- INHERITED_STYLE_GROUP_COUNT..old.payloads.len(),- ) else {+ // SAFETY: Published style-group payloads stay live while their records do.+ let Some(groups) = (unsafe {+ crate::css::computed_values::changed_style_groups(+ [old.base_payloads, old.payloads],+ [new.base_payloads, new.payloads],+ INHERITED_STYLE_GROUP_COUNT..old.payloads.len(),+ )+ }) else {return u32::MAX;};let (Some(old_table), Some(new_table)) = (old.longhand_table, new.longhand_table) else {diff --git a/Libraries/LibWeb/Rust/src/css/style/computed.rs b/Libraries/LibWeb/Rust/src/css/style/computed.rsindex 3375ea1256e..b6b3007656c 100644--- a/Libraries/LibWeb/Rust/src/css/style/computed.rs+++ b/Libraries/LibWeb/Rust/src/css/style/computed.rs@@ -1133,11 +1133,14 @@ impl ComputedGroupSets {// A payload nobody has interned yet is asked for its content, which is what decides its// identity: an equal payload built by another record is the same group, however the two// builds were ordered.- let payload_content_hash = style_group_payloads_hash(index, payload.as_ptr());+ // SAFETY: Published style-group payloads stay live while their records do.+ let payload_content_hash = unsafe { style_group_payloads_hash(index, payload.as_ptr()) };let groups = &self.groups;if let Some(identity) = self.groups_by_content.find(payload_content_hash, |identity, ()| {let group = &groups[identity];- group.index == index && style_group_payloads_equal(index, group.payload.as_ptr(), payload.as_ptr())+ // SAFETY: Published style-group payloads stay live while their records do.+ group.index == index+ && unsafe { style_group_payloads_equal(index, group.payload.as_ptr(), payload.as_ptr()) }}) {return (identity, false);}@@ -1807,7 +1810,8 @@ impl ComputedGroupSets {// An equal payload keeps the old identity, as a C++ build adopts its parent's and// predecessor's identical payloads.let identity = if payload == old_payload- || style_group_payloads_equal(group, old_payload.as_ptr(), payload.as_ptr())+ // SAFETY: Published style-group payloads stay live while their records do.+ || unsafe { style_group_payloads_equal(group, old_payload.as_ptr(), payload.as_ptr()) }{canonicalized_groups += 1;release_group_payload(group, payload.as_ptr());@@ -1826,7 +1830,10 @@ impl ComputedGroupSets {let holds_image_values = self.sets.get_index(group_set.0 as usize)- .is_some_and(|set| style_group_payloads_hold_image_values(SharedPayload::as_pointer_slice(&set.payloads)));+ // SAFETY: Published style-group payloads stay live while their records do.+ .is_some_and(|set| unsafe {+ style_group_payloads_hold_image_values(SharedPayload::as_pointer_slice(&set.payloads))+ });let old_metadata = self.computed_fixed_metadata[old_record.fixed_metadata];let counter_style_environment_identity =counter_style_environment_identity.unwrap_or(old_metadata.counter_style_environment_identity);@@ -2034,7 +2041,8 @@ impl ComputedGroupSets {.get_index(base_style_record.index()).expect("base style-record is live");let mut dependency_flags = self.computed_fixed_metadata.get(base.fixed_metadata).dependency_flags;- if style_group_payloads_hold_image_values(SharedPayload::as_pointer_slice(payloads)) {+ // SAFETY: Published style-group payloads stay live while their records do.+ if unsafe { style_group_payloads_hold_image_values(SharedPayload::as_pointer_slice(payloads)) } {dependency_flags |= HOLDS_IMAGE_VALUES;}if let Some(in_display_none_subtree) = in_display_none_subtree {@@ -2392,7 +2400,8 @@ impl ComputedGroupSets {assert!(!payload.is_null(), "computed group payload is null");let previous_identity = previous_group_set.and_then(|set| self.sets[set].groups.get(index).copied());let previous_equal_identity = previous_identity.filter(|identity| {- style_group_payloads_equal(index, payload.as_ptr(), self.groups[*identity].payload.as_ptr())+ // SAFETY: Published style-group payloads stay live while their records do.+ unsafe { style_group_payloads_equal(index, payload.as_ptr(), self.groups[*identity].payload.as_ptr()) }});let identity = match previous_equal_identity {Some(identity) => {@@ -2776,7 +2785,9 @@ impl ComputedGroupSets {.zip(second_groups.iter()).enumerate().any(|(index, (&first, &second))| {- first != second && !style_group_payloads_equal(index, first.as_ptr(), second.as_ptr())+ // SAFETY: Published style-group payloads stay live while their records do.+ first != second+ && !unsafe { style_group_payloads_equal(index, first.as_ptr(), second.as_ptr()) }}){return false;@@ -2874,11 +2885,12 @@ impl ComputedGroupSets {let node_payload = node_groups[group];let parent_payload = parent_groups[group];node_payload == parent_payload- || crate::css::computed_values::style_group_payloads_equal(+ // SAFETY: Published style-group payloads stay live while their records do.+ || unsafe { crate::css::computed_values::style_group_payloads_equal(group,node_payload.as_ptr(),parent_payload.as_ptr(),- )+ ) }}))}@@ -2904,11 +2916,12 @@ impl ComputedGroupSets {(0..ENGINE_INHERITED_GROUP_COUNT).all(|group| {let sampled = overlay.payloads[group];sampled == base_groups[group]- || crate::css::computed_values::style_group_payloads_equal(+ // SAFETY: Published style-group payloads stay live while their records do.+ || unsafe { crate::css::computed_values::style_group_payloads_equal(group,sampled.as_ptr(),base_groups[group].as_ptr(),- )+ ) }})}@@ -2944,11 +2957,12 @@ impl ComputedGroupSets {let record_payload = record_groups[group];let node_payload = node_groups[group];record_payload == node_payload- || crate::css::computed_values::style_group_payloads_equal(+ // SAFETY: Published style-group payloads stay live while their records do.+ || unsafe { crate::css::computed_values::style_group_payloads_equal(group,record_payload.as_ptr(),node_payload.as_ptr(),- )+ ) }})}diff --git a/Libraries/LibWeb/Rust/src/css/style/hover_lane.rs b/Libraries/LibWeb/Rust/src/css/style/hover_lane.rsindex 456ed206f42..6edf2f8da4d 100644--- a/Libraries/LibWeb/Rust/src/css/style/hover_lane.rs+++ b/Libraries/LibWeb/Rust/src/css/style/hover_lane.rs@@ -726,10 +726,13 @@ impl StyleEngine {) else {return true;};- crate::css::computed_values::style_group_payloads_move_image_values(- SharedPayload::as_pointer_slice(old),- SharedPayload::as_pointer_slice(new),- )+ // SAFETY: Published style-group payloads stay live while their records do.+ unsafe {+ crate::css::computed_values::style_group_payloads_move_image_values(+ SharedPayload::as_pointer_slice(old),+ SharedPayload::as_pointer_slice(new),+ )+ }}/// Whether `row` owes its element no more than a moved inherited custom-property environment, which its style readsdiff --git a/Libraries/LibWeb/Rust/src/css/style/publication.rs b/Libraries/LibWeb/Rust/src/css/style/publication.rsindex 81373478dc9..6bacf21b4a0 100644--- a/Libraries/LibWeb/Rust/src/css/style/publication.rs+++ b/Libraries/LibWeb/Rust/src/css/style/publication.rs@@ -2351,9 +2351,11 @@ impl RetainedState {})}).collect::<Vec<_>>();- let holds_image_values = crate::css::computed_values::style_group_payloads_hold_image_values(- HostShared::as_pointer_slice(&payloads),- );+ // SAFETY: Each payload was just built for its group, and the drive holds the only reference to it until+ // ownership transfers to the catalog below.+ let holds_image_values = unsafe {+ crate::css::computed_values::style_group_payloads_hold_image_values(HostShared::as_pointer_slice(&payloads))+ };let dependency_flags = unsafe { &*table }.publication_dependency_flags()| (u8::from(swap_eligible) * computed::INHERITED_GROUP_SWAP_ELIGIBLE)| (u8::from(holds_image_values) * computed::HOLDS_IMAGE_VALUES);diff --git a/Libraries/LibWeb/Rust/src/css/style/style_invalidation.rs b/Libraries/LibWeb/Rust/src/css/style/style_invalidation.rsindex 986c0e91c61..1e75d41886a 100644--- a/Libraries/LibWeb/Rust/src/css/style/style_invalidation.rs+++ b/Libraries/LibWeb/Rust/src/css/style/style_invalidation.rs@@ -913,7 +913,10 @@ impl RetainedState {.iter().zip(new_record.payloads).enumerate()- .all(|(index, (&old, &new))| old == new || style_group_payloads_equal(index, old.as_ptr(), new.as_ptr()));+ // SAFETY: Published style-group payloads stay live while their records do.+ .all(|(index, (&old, &new))| {+ old == new || unsafe { style_group_payloads_equal(index, old.as_ptr(), new.as_ptr()) }+ });let can_skip = all_groups_equal&& std::ptr::eq(old_table, new_table)&& font_lists_equaldiff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rsindex 4f914910ca4..24d1f96b6f7 100644--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs@@ -782,11 +782,12 @@ fn style_payloads_equal_in_groups(a: StylePayloadsRef, b: StylePayloadsRef, comp(0..a.groups.len()).all(|group_index| {!compares(group_index)|| a.groups[group_index] == b.groups[group_index]- || crate::css::computed_values::style_group_payloads_equal(+ // SAFETY: Published style-group payloads stay live while their records do.+ || unsafe { crate::css::computed_values::style_group_payloads_equal(group_index,a.groups[group_index],b.groups[group_index],- )+ ) }})}