Truly independent web browser
LibWeb: Stop marking FFI entry points unsafe when they need nothing
Andreas Klingeaa78d652c03
diff --git a/Libraries/LibWeb/Rust/src/css/animation.rs b/Libraries/LibWeb/Rust/src/css/animation.rsindex fbd8448c5fe..51621fbaf16 100644--- a/Libraries/LibWeb/Rust/src/css/animation.rs+++ b/Libraries/LibWeb/Rust/src/css/animation.rs@@ -7292,12 +7292,8 @@ pub(crate) fn evaluate_animations(batch: ComputedAnimationBatch<'_>) -> Vec<(u32}/// Attempt Rust-owned style value interpolation without consulting C++ or the DOM.-///-/// # Safety-/// `context` must be null or point at a live `FfiAnimationContext`. `from` and `to` must point at live-/// `StyleValueData` allocations.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_interpolate_scalar_style_value(+pub extern "C" fn rust_interpolate_scalar_style_value(context: Option<&FfiAnimationContext>,property_id: u16,from: &StyleValueData,@@ -7309,11 +7305,8 @@ pub unsafe extern "C" fn rust_interpolate_scalar_style_value(/// Test-only bridge for exercising Rust-owned style value composition without constructing an/// animation batch. Production animation evaluation uses `evaluate_animations`.-///-/// # Safety-/// `underlying` and `animated` must point at live `StyleValueData` allocations.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_test_composite_style_value(+pub extern "C" fn rust_test_composite_style_value(underlying: &StyleValueData,animated: &StyleValueData,operation: FfiCompositeOperation,diff --git a/Libraries/LibWeb/Rust/src/css/calc.rs b/Libraries/LibWeb/Rust/src/css/calc.rsindex e4a8d5ed1ed..6023eede345 100644--- a/Libraries/LibWeb/Rust/src/css/calc.rs+++ b/Libraries/LibWeb/Rust/src/css/calc.rs@@ -1170,11 +1170,8 @@ pub unsafe extern "C" fn rust_calc_node_release(node: *const CalcNode) {/// Determines the type of the calculation rooted at `node` under the given/// resolve-as target. The result's valid flag is false when the tree does not/// type-check.-///-/// # Safety-/// `node` must be a valid calculation node pointer.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_calc_node_determine_type(+pub extern "C" fn rust_calc_node_determine_type(node: &CalcNode,has_percentages_resolve_as: bool,resolve_as_is_number: bool,diff --git a/Libraries/LibWeb/Rust/src/css/counter_style.rs b/Libraries/LibWeb/Rust/src/css/counter_style.rsindex 43e6bf0c6cb..57162287894 100644--- a/Libraries/LibWeb/Rust/src/css/counter_style.rs+++ b/Libraries/LibWeb/Rust/src/css/counter_style.rs@@ -71,7 +71,7 @@ pub unsafe extern "C" fn rust_counter_style_name(rule: &FfiCounterStyle) -> FfiU}#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_counter_style_set_name(rule: &FfiCounterStyle, name: FfiUtf16View) -> bool {+pub extern "C" fn rust_counter_style_set_name(rule: &FfiCounterStyle, name: FfiUtf16View) -> bool {let Some(mut name) = name.to_utf16() else {return false;};diff --git a/Libraries/LibWeb/Rust/src/css/declaration_block.rs b/Libraries/LibWeb/Rust/src/css/declaration_block.rsindex 4cc799fe1f8..42442af8fc3 100644--- a/Libraries/LibWeb/Rust/src/css/declaration_block.rs+++ b/Libraries/LibWeb/Rust/src/css/declaration_block.rs@@ -598,10 +598,7 @@ pub extern "C" fn rust_declaration_block_remove(block: &mut DeclarationBlock, pr}#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_declaration_block_remove_custom(- block: &mut DeclarationBlock,- name: FfiUtf16View,-) -> bool {+pub extern "C" fn rust_declaration_block_remove_custom(block: &mut DeclarationBlock, name: FfiUtf16View) -> bool {let name = name.to_utf16().unwrap();let Some(index) = block.data()@@ -657,9 +654,10 @@ mod tests {view.important = true;assert!(unsafe { rust_declaration_block_set(&mut block, &view) });let name = block.data().custom_properties[0].name.clone();- assert!(unsafe {- rust_declaration_block_remove_custom(&mut block, FfiUtf16View::from_utf16(name.units()))- });+ assert!(rust_declaration_block_remove_custom(+ &mut block,+ FfiUtf16View::from_utf16(name.units())+ ));assert!(block.data().properties[0].important);assert!(block.data().custom_properties.is_empty());assert_eq!(crate::css::ffi_stats::CPP_CALLBACK_COUNT.get(), 0);@@ -688,7 +686,10 @@ mod tests {});let name: Vec<_> = "--幅".encode_utf16().collect();- assert!(unsafe { rust_declaration_block_remove_custom(&mut retained, FfiUtf16View::from_utf16(&name),) });+ assert!(rust_declaration_block_remove_custom(+ &mut retained,+ FfiUtf16View::from_utf16(&name),+ ));assert_eq!(rust_declaration_block_dependencies(&fork),FfiDeclarationBlockDependencies::default()diff --git a/Libraries/LibWeb/Rust/src/css/descriptor_block.rs b/Libraries/LibWeb/Rust/src/css/descriptor_block.rsindex fdf9d8c2e0b..a7ae440ebf8 100644--- a/Libraries/LibWeb/Rust/src/css/descriptor_block.rs+++ b/Libraries/LibWeb/Rust/src/css/descriptor_block.rs@@ -165,11 +165,7 @@ pub extern "C" fn rust_descriptor_block_length(block: &FfiDescriptorBlock) -> us// The block owns the returned value until its next mutation.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_descriptor_block_get(- block: &FfiDescriptorBlock,- id: u8,- name: FfiUtf16View,-) -> *const c_void {+pub extern "C" fn rust_descriptor_block_get(block: &FfiDescriptorBlock, id: u8, name: FfiUtf16View) -> *const c_void {let name = name.units().unwrap();block.data()@@ -244,11 +240,7 @@ pub unsafe extern "C" fn rust_descriptor_block_set(block: &mut FfiDescriptorBloc}#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_descriptor_block_remove(- block: &mut FfiDescriptorBlock,- id: u8,- name: FfiUtf16View,-) -> bool {+pub extern "C" fn rust_descriptor_block_remove(block: &mut FfiDescriptorBlock, id: u8, name: FfiUtf16View) -> bool {let name = name.to_utf16().unwrap();let Some(index) =block.data().descriptors.iter().position(|descriptor| {@@ -301,7 +293,7 @@ mod tests {first.value = Arc::as_ptr(&data.descriptors[1].value).cast();assert!(unsafe { rust_descriptor_block_set(&mut block, &first) });let second = data.descriptors[1].view();- assert!(unsafe { rust_descriptor_block_remove(&mut block, second.id, second.name) });+ assert!(rust_descriptor_block_remove(&mut block, second.id, second.name));assert_eq!(block.data().descriptors.len(), 1);assert!(Arc::ptr_eq(&block.data().descriptors[0].value,@@ -336,7 +328,7 @@ mod tests {assert_eq!(rust_descriptor_block_length(block), 2);let allocations = DESCRIPTOR_OWNER_ALLOCATIONS.get();let descriptor = snapshot.descriptors[0].view();- let value = unsafe { rust_descriptor_block_get(block, descriptor.id, descriptor.name) };+ let value = rust_descriptor_block_get(block, descriptor.id, descriptor.name);assert_eq!(value, descriptor.value);assert_eq!(serialize_descriptors(&block.data()).into_utf16(),@@ -348,7 +340,11 @@ mod tests {assert!(matches!(*block.state.borrow(), DescriptorBlockState::Immutable(_)));assert!(rust_descriptor_block_external_memory_size(block) > 0);let mut fork = unsafe { Box::from_raw(rust_descriptor_block_share(block)) };- assert!(!unsafe { rust_descriptor_block_remove(&mut fork, CUSTOM_DESCRIPTOR_ID, FfiUtf16View::default()) });+ assert!(!rust_descriptor_block_remove(+ &mut fork,+ CUSTOM_DESCRIPTOR_ID,+ FfiUtf16View::default()+ ));assert!(matches!(*block.state.borrow(), DescriptorBlockState::Immutable(_)));assert!(matches!(*fork.state.borrow(), DescriptorBlockState::Immutable(_)));assert!(Arc::ptr_eq(&snapshot, &fork.data()));@@ -356,7 +352,11 @@ mod tests {let mut retained = unsafe { Box::from_raw(rust_descriptor_block_retain(block)) };assert!(Rc::ptr_eq(&block.owner(), &retained.owner()));let descriptor = snapshot.descriptors[0].view();- assert!(unsafe { rust_descriptor_block_remove(&mut retained, descriptor.id, descriptor.name) });+ assert!(rust_descriptor_block_remove(+ &mut retained,+ descriptor.id,+ descriptor.name+ ));assert_eq!(rust_descriptor_block_length(block), 1);// The pinned snapshot still owns the removed descriptor.assert_eq!(@@ -369,7 +369,11 @@ mod tests {assert!(matches!(*fork.state.borrow(), DescriptorBlockState::Immutable(_)));drop((rule, rules));let first = &snapshot.descriptors[0];- assert!(unsafe { rust_descriptor_block_remove(&mut fork, first.id, FfiUtf16View::default()) });+ assert!(rust_descriptor_block_remove(+ &mut fork,+ first.id,+ FfiUtf16View::default()+ ));assert!(matches!(*fork.state.borrow(), DescriptorBlockState::Mutable(_)));assert_eq!(fork.data().descriptors.len(), 1);assert_eq!(retained.data().descriptors.len(), 2);diff --git a/Libraries/LibWeb/Rust/src/css/font_feature_values.rs b/Libraries/LibWeb/Rust/src/css/font_feature_values.rsindex 731fa5e4402..5cfc3d0758c 100644--- a/Libraries/LibWeb/Rust/src/css/font_feature_values.rs+++ b/Libraries/LibWeb/Rust/src/css/font_feature_values.rs@@ -167,7 +167,7 @@ pub extern "C" fn rust_font_feature_values_count(}#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_font_feature_values_find(+pub extern "C" fn rust_font_feature_values_find(owner: &FontFeatureValuesRule,kind: FontFeatureValuesRuleKind,name: FfiUtf16View,@@ -215,7 +215,7 @@ pub unsafe extern "C" fn rust_font_feature_values_set(}#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_font_feature_values_remove(+pub extern "C" fn rust_font_feature_values_remove(owner: &FontFeatureValuesRule,kind: FontFeatureValuesRuleKind,name: FfiUtf16View,diff --git a/Libraries/LibWeb/Rust/src/css/media_list.rs b/Libraries/LibWeb/Rust/src/css/media_list.rsindex 228ffcad05b..ce3b3d9d674 100644--- a/Libraries/LibWeb/Rust/src/css/media_list.rs+++ b/Libraries/LibWeb/Rust/src/css/media_list.rs@@ -126,12 +126,12 @@ pub extern "C" fn rust_media_list_length(list: &MediaList) -> usize {}#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_media_list_set_text(list: &MediaList, source: FfiUtf16View) {+pub extern "C" fn rust_media_list_set_text(list: &MediaList, source: FfiUtf16View) {list.set_text(source.units().unwrap());}#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_media_list_append(list: &MediaList, source: FfiUtf16View) -> bool {+pub extern "C" fn rust_media_list_append(list: &MediaList, source: FfiUtf16View) -> bool {// https://drafts.csswg.org/cssom/#dom-medialist-appendmedium// 1. Let m be the result of parsing the given value.// 2. If m is null, then return.@@ -158,7 +158,7 @@ pub enum MediaListDeleteResult {}#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_media_list_delete(list: &MediaList, source: FfiUtf16View) -> MediaListDeleteResult {+pub extern "C" fn rust_media_list_delete(list: &MediaList, source: FfiUtf16View) -> MediaListDeleteResult {// https://drafts.csswg.org/cssom/#dom-medialist-deletemedium// 1. Let m be the result of parsing the given value.// 2. If m is null, then return.diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax.rsindex 211e1316d22..82e58db0b84 100644--- a/Libraries/LibWeb/Rust/src/css/parser/syntax.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax.rs@@ -430,11 +430,8 @@ pub(crate) fn parse_with_syntax(context: &ParseContext, source: &[u16], syntax:}/// Parses a complete CSS syntax definition and returns an opaque syntax tree.-///-/// # Safety-/// `source` must remain readable for `source_length` UTF-16 code units during this call.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_parse_syntax(source: FfiUtf16View, limit_ident_to_custom_ident: bool) -> *const c_void {+pub extern "C" fn rust_parse_syntax(source: FfiUtf16View, limit_ident_to_custom_ident: bool) -> *const c_void {let Some(source) = source.to_utf16() else {return std::ptr::null();};diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rsindex 2bacc773c51..297b17e1965 100644--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs@@ -3673,11 +3673,8 @@ pub(crate) fn parse_keyframe_selector_list(source: TokenizerInput<'_>) -> Option}/// Parses an immutable CSS page selector list.-///-/// # Safety-/// `source` must remain readable for this call.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_parse_page_selector_list(source: FfiUtf16View) -> *const PageSelectorList {+pub extern "C" fn rust_parse_page_selector_list(source: FfiUtf16View) -> *const PageSelectorList {let Some(source) = source.units() else {return std::ptr::null();};diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/ownership_tests.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/ownership_tests.rsindex 5ba8804c085..2fe075bda98 100644--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/ownership_tests.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/ownership_tests.rs@@ -1075,10 +1075,8 @@ fn declaration_mutation_does_not_change_cached_or_nested_shared_blocks() {let mut changed = DeclarationBlock::new(original.clone());let independent = DeclarationBlock::new(original.clone());assert_eq!(changed.data().properties.len(), 4);- unsafe {- assert!(rust_declaration_block_remove(&mut changed, property_id::MARGIN_TOP));- assert!(rust_declaration_block_remove_custom(&mut changed, view(&utf16("--色"))));- }+ assert!(rust_declaration_block_remove(&mut changed, property_id::MARGIN_TOP));+ assert!(rust_declaration_block_remove_custom(&mut changed, view(&utf16("--色"))));assert_eq!(independent.data().properties.len(), 4);assert_eq!(independent.data().custom_properties.len(), 1);let third = parse(&source, &context);diff --git a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rsindex e38977bb786..366b401bf5e 100644--- a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs@@ -6258,11 +6258,8 @@ pub struct FfiSimpleColor {}/// Parses the allocation-free subset used by the canvas color cache.-///-/// # Safety-/// The source view must contain exactly one valid pointer when non-empty.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_parse_simple_color(source: FfiUtf16View) -> FfiSimpleColor {+pub extern "C" fn rust_parse_simple_color(source: FfiUtf16View) -> FfiSimpleColor {let Some(source) = source.units() else {return FfiSimpleColor::default();};@@ -6278,10 +6275,8 @@ pub unsafe extern "C" fn rust_parse_simple_color(source: FfiUtf16View) -> FfiSim}}-/// # Safety-/// The source view must contain exactly one valid pointer when non-empty.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_is_valid_animation_name_custom_ident(source: FfiUtf16View) -> bool {+pub extern "C" fn rust_is_valid_animation_name_custom_ident(source: FfiUtf16View) -> bool {let Some(source) = source.units() else {return false;};diff --git a/Libraries/LibWeb/Rust/src/css/rule.rs b/Libraries/LibWeb/Rust/src/css/rule.rsindex 55731022597..82b55e645cc 100644--- a/Libraries/LibWeb/Rust/src/css/rule.rs+++ b/Libraries/LibWeb/Rust/src/css/rule.rs@@ -730,20 +730,16 @@ pub unsafe extern "C" fn rust_namespace_context_release(context: *const NativeNa}}-/// # Safety-/// The name must be readable for the duration of the call.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_keyframes_set_name(rule: &NativeRule, name: FfiUtf16View) {+pub extern "C" fn rust_keyframes_set_name(rule: &NativeRule, name: FfiUtf16View) {let RulePayload::Keyframes(current) = &rule.payload else {unreachable!()};*current.borrow_mut() = CssString::from_utf16(&name.to_utf16().unwrap());}-/// # Safety-/// The selector must be readable for the duration of the call.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_keyframes_find_rule(rule: &NativeRule, selector: FfiUtf16View) -> usize {+pub extern "C" fn rust_keyframes_find_rule(rule: &NativeRule, selector: FfiUtf16View) -> usize {let Some(selectors) = selector.units().and_then(crate::css::parser::syntax_parser::parse_keyframe_selector_list)diff --git a/Libraries/LibWeb/Rust/src/css/selector_parser.rs b/Libraries/LibWeb/Rust/src/css/selector_parser.rsindex affd177535d..61a96fd54a1 100644--- a/Libraries/LibWeb/Rust/src/css/selector_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/selector_parser.rs@@ -1337,11 +1337,8 @@ pub struct FfiParsedPseudoElement {}/// Parses the restricted pseudo-element syntax accepted by `getComputedStyle()` and animation APIs.-///-/// # Safety-/// `input` must contain readable storage for the duration of this call.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_selector_parse_pseudo_element(input: FfiUtf16View) -> FfiParsedPseudoElement {+pub extern "C" fn rust_selector_parse_pseudo_element(input: FfiUtf16View) -> FfiParsedPseudoElement {let Some(input) = input.units() else {return FfiParsedPseudoElement {selector: std::ptr::null_mut(),diff --git a/Libraries/LibWeb/Rust/src/css/style/host_atoms.rs b/Libraries/LibWeb/Rust/src/css/style/host_atoms.rsindex b3a57509f0c..c48d82f4801 100644--- a/Libraries/LibWeb/Rust/src/css/style/host_atoms.rs+++ b/Libraries/LibWeb/Rust/src/css/style/host_atoms.rs@@ -123,16 +123,8 @@ pub extern "C" fn document_host_atom_generation(host: &DocumentHost) -> u64 {/// Records the language the element `node` resolves to, or none for no element. The first time the host names a/// language, the engine is also given its tag: a range is not a name, so `:lang()` compares against the tag itself.-///-/// # Safety-/// `host` must be a live document host, on its document's thread, and `text` must name readable code units.#[unsafe(no_mangle)]-pub unsafe extern "C" fn style_engine_set_element_language(- host: &DocumentHost,- node: u32,- language: u32,- text: FfiUtf16View,-) {+pub extern "C" fn style_engine_set_element_language(host: &DocumentHost, node: u32, language: u32, text: FfiUtf16View) {let tag_is_new = language != 0&& !text.is_empty()&& hostdiff --git a/Libraries/LibWeb/Rust/src/layout/box_removal.rs b/Libraries/LibWeb/Rust/src/layout/box_removal.rsindex 39eb4d952a0..631ecef5824 100644--- a/Libraries/LibWeb/Rust/src/layout/box_removal.rs+++ b/Libraries/LibWeb/Rust/src/layout/box_removal.rs@@ -269,17 +269,8 @@ impl BoxRemoval {/// far: the render state detaches the node's box from the parent's box in place, or marks the parent for the next/// layout tree build to rebuild, as it finds the boxes. What the drop owes the host is paid once the job that applies/// it is done.-///-/// # Safety-///-/// `host` must be a live document host, on its document's thread.#[unsafe(no_mangle)]-pub unsafe extern "C" fn render_state_remove_box(- host: &DocumentHost,- parent: u32,- child: u32,- facts: FfiDetachedBoxFacts,-) {+pub extern "C" fn render_state_remove_box(host: &DocumentHost, parent: u32, child: u32, facts: FfiDetachedBoxFacts) {let (Some(parent), Some(child)) = (StyleNodeID::from_raw(parent), StyleNodeID::from_raw(child)) else {panic!("a removed box and its parent are named by their identities");};@@ -291,12 +282,8 @@ pub unsafe extern "C" fn render_state_remove_box(/// Whether the box of the element `child` can leave the box of its parent `parent` in place, as a removal would find/// it, spending `read`.-///-/// # Safety-///-/// `host` must be a live document host, on its document's thread.#[unsafe(no_mangle)]-pub unsafe extern "C" fn render_state_can_detach_box_in_place(+pub extern "C" fn render_state_can_detach_box_in_place(host: &DocumentHost,read: &crate::render_state::BegunRead,parent: u32,diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena/main_thread_entries.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena/main_thread_entries.rsindex 71ed0c4704c..ed358a0f50a 100644--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena/main_thread_entries.rs+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena/main_thread_entries.rs@@ -56,19 +56,17 @@ mod tests {is_direct_without_box: true,};// SAFETY: As above. This queues the removal while the host still holds the child's layout node.- unsafe {- render_state_remove_box(- host,- parent_node.raw(),- child_node.raw(),- FfiDetachedBoxFacts {- parent_is_body: false,- previous_sibling: sibling,- next_sibling: sibling,- level: FfiDetachedBoxLevel::Block,- },- );- }+ render_state_remove_box(+ host,+ parent_node.raw(),+ child_node.raw(),+ FfiDetachedBoxFacts {+ parent_is_body: false,+ previous_sibling: sibling,+ next_sibling: sibling,+ level: FfiDetachedBoxLevel::Block,+ },+ );// A recording can still name the removed image. Resolving it must apply the removal before answering.let resolved = render_state_node_shell_if_live(host, read, child);let shell_was_destroyed = !host.host_tables().shells.borrow().contains_key(&child);