Skip to content

Truly independent web browser

LibWeb: Take FFI out-parameters as references and AK spans

Andreas Kling7fc02b085f3a
diff --git a/Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.cpp b/Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.cpp
index 779ace7ed92..67d6fae016d 100644
--- a/Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.cpp
+++ b/Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.cpp
@@ -55,7 +55,7 @@ GC::Ptr<CSSTransformComponent> TransformationStyleValue::reify_a_transform_funct
case TransformFunction::Matrix3d: {
Array<float, 16> transform_as_matrix;
bool is_2d_matrix = true;
- if (!ComputedValuesFFI::rust_transform_list_to_abstract_matrix(rust_style_value_data(), transform_as_matrix.data(), &is_2d_matrix))
+ if (!ComputedValuesFFI::rust_transform_list_to_abstract_matrix(rust_style_value_data(), transform_as_matrix.span(), &is_2d_matrix))
return nullptr;
// NB: The matrix is row by row, as Gfx::Matrix4x4 takes it.
diff --git a/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.cpp b/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.cpp
index c82d2f66334..79c9d5a75b2 100644
--- a/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.cpp
+++ b/Libraries/LibWeb/Geometry/DOMMatrixReadOnly.cpp
@@ -55,7 +55,7 @@ bool web_dom_matrix_parse_transform_list(FlatPtr transform_list_raw, Array<doubl
// NB: A <length> without an absolute unit is a failure as well, which Rust reports when it cannot resolve a function.
Array<float, 16> matrix;
bool is_2d_transform = true;
- if (!CSS::ComputedValuesFFI::rust_transform_list_to_abstract_matrix(transform_style_value->rust_style_value_data(), matrix.data(), &is_2d_transform))
+ if (!CSS::ComputedValuesFFI::rust_transform_list_to_abstract_matrix(transform_style_value->rust_style_value_data(), matrix.span(), &is_2d_transform))
return false;
// 7. Return matrix and 2dTransform.
diff --git a/Libraries/LibWeb/Layout/UpdateLayout.cpp b/Libraries/LibWeb/Layout/UpdateLayout.cpp
index 4d1e786ae21..67c9a96c627 100644
--- a/Libraries/LibWeb/Layout/UpdateLayout.cpp
+++ b/Libraries/LibWeb/Layout/UpdateLayout.cpp
@@ -123,7 +123,7 @@ static void describe_node_for_bound_failure(Document& document, Layout::BegunRea
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());
+ auto trace_length = CSS::StyleEngineFFI::style_engine_end_bound_failure_trace(engine_host, trace_buffer.span());
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) {
diff --git a/Libraries/LibWeb/Painting/PaintingRustBridge.cpp b/Libraries/LibWeb/Painting/PaintingRustBridge.cpp
index ebd41e891cf..2b5f87b096a 100644
--- a/Libraries/LibWeb/Painting/PaintingRustBridge.cpp
+++ b/Libraries/LibWeb/Painting/PaintingRustBridge.cpp
@@ -224,7 +224,7 @@ Vector<u32> rust_owned_visual_context_node_indices(Layout::Node const& layout_no
auto slot = committed_row_slot(layout_node);
indices.resize(Layout::RustFFI::render_state_paintable_visual_context_node_count(host, slot, list));
if (!indices.is_empty())
- Layout::RustFFI::render_state_paintable_visual_context_copy_node_indices(host, slot, list, indices.data(), indices.size());
+ Layout::RustFFI::render_state_paintable_visual_context_copy_node_indices(host, slot, list, indices.span());
return indices;
}
diff --git a/Libraries/LibWeb/Rust/src/css/computed_longhand_table.rs b/Libraries/LibWeb/Rust/src/css/computed_longhand_table.rs
index fc9b13f4a27..5c127f96519 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_longhand_table.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_longhand_table.rs
@@ -1580,15 +1580,12 @@ pub extern "C" fn rust_computed_longhand_table_metadata(
/// The recorded inheritance-dependent specified values as a borrowed span.
/// The span stays valid while the caller's table reference is live and no
/// further add or remove mutates the table.
-///
-/// # Safety
-/// `table` must be a valid table and `out_count` writable.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_computed_longhand_table_inheritance_dependent_values(
+pub extern "C" fn rust_computed_longhand_table_inheritance_dependent_values(
table: &ComputedLonghandTable,
- out_count: *mut usize,
+ out_count: &mut usize,
) -> *const FfiTableInheritanceDependentValue {
- unsafe { *out_count = table.inheritance_dependent_view.len() };
+ *out_count = table.inheritance_dependent_view.len();
table.inheritance_dependent_view.as_ptr()
}
diff --git a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
index 1b712ccdecc..188a2791fde 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs
@@ -6303,32 +6303,20 @@ pub(crate) enum FontDescriptorKind {
/// on success. A null return is disambiguated by `out_status`.
///
/// # Safety
-/// All non-null pointers must remain readable for their accompanying lengths
-/// for the duration of this call. `out_status` must be a valid writable
-/// pointer.
+/// The pointers `context` holds must remain readable for their accompanying lengths during this call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rust_parse_css_value(
context: Option<&ParseContext>,
property_id: u16,
source: FfiUtf16View,
- out_status: *mut FfiParseStatus,
+ out_status: Option<&mut FfiParseStatus>,
) -> *const c_void {
- let invalid_ffi_result = || {
- if !out_status.is_null() {
- unsafe { *out_status = FfiParseStatus::NotHandled };
- }
- std::ptr::null()
- };
-
- let Some(context) = context else {
- return invalid_ffi_result();
+ let Some(out_status) = out_status else {
+ return std::ptr::null();
};
-
- if out_status.is_null() {
- return invalid_ffi_result();
- }
- let Some(source) = source.units() else {
- return invalid_ffi_result();
+ let (Some(context), Some(source)) = (context, source.units()) else {
+ *out_status = FfiParseStatus::NotHandled;
+ return std::ptr::null();
};
let mut source_utf16 = Vec::with_capacity(source.len());
source.append_to(&mut source_utf16);
@@ -6348,41 +6336,24 @@ pub unsafe extern "C" fn rust_parse_css_value(
}
Err(()) => ParseOutcome::NotHandled,
};
- match outcome {
- ParseOutcome::Parsed(value) => {
- unsafe {
- *out_status = FfiParseStatus::Parsed;
- }
- Arc::into_raw(value).cast()
- }
- ParseOutcome::Invalid => {
- unsafe {
- *out_status = FfiParseStatus::Invalid;
- }
- std::ptr::null()
- }
- ParseOutcome::NotHandled => {
- unsafe {
- *out_status = FfiParseStatus::NotHandled;
- }
- std::ptr::null()
- }
- }
+ let (status, value) = match outcome {
+ ParseOutcome::Parsed(value) => (FfiParseStatus::Parsed, Arc::into_raw(value).cast()),
+ ParseOutcome::Invalid => (FfiParseStatus::Invalid, std::ptr::null()),
+ ParseOutcome::NotHandled => (FfiParseStatus::NotHandled, std::ptr::null()),
+ };
+ *out_status = status;
+ value
}
/// Returns a substitution-function presence bitmap for CSS source.
-///
-/// # Safety
-/// The source pointer must be valid for its accompanying length and
-/// `out_presence` must be writable.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_collect_arbitrary_substitution_function_presence_from_source(
+pub extern "C" fn rust_collect_arbitrary_substitution_function_presence_from_source(
source: FfiUtf16View,
- out_presence: *mut u8,
+ out_presence: Option<&mut u8>,
) -> bool {
- if out_presence.is_null() {
+ let Some(out_presence) = out_presence else {
return false;
- }
+ };
let Some(source) = source.units() else {
return false;
};
@@ -6392,7 +6363,7 @@ pub unsafe extern "C" fn rust_collect_arbitrary_substitution_function_presence_f
let Some(presence) = substitution_function_presence_bits(&values) else {
return false;
};
- unsafe { *out_presence = presence };
+ *out_presence = presence;
true
}
diff --git a/Libraries/LibWeb/Rust/src/css/property_metadata.rs b/Libraries/LibWeb/Rust/src/css/property_metadata.rs
index 8489f0c39fe..89ef9f39e66 100644
--- a/Libraries/LibWeb/Rust/src/css/property_metadata.rs
+++ b/Libraries/LibWeb/Rust/src/css/property_metadata.rs
@@ -233,33 +233,27 @@ pub extern "C" fn rust_property_metadata_is_inherited(property_id: u16) -> bool
property_is_inherited(property_id)
}
-/// # Safety
-/// `out_length` must be a valid pointer.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_property_metadata_numeric_ranges(
+pub extern "C" fn rust_property_metadata_numeric_ranges(
property_id: u16,
- out_length: *mut usize,
+ out_length: &mut usize,
) -> *const FfiPropertyNumericRange {
let ranges = property_numeric_ranges(property_id);
- unsafe { *out_length = ranges.len() };
+ *out_length = ranges.len();
ranges.as_ptr()
}
-/// # Safety
-/// All out-pointers must be valid.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_property_metadata_bounds(
- out_first_longhand: *mut u16,
- out_last_longhand: *mut u16,
- out_first_inherited: *mut u16,
- out_last_inherited: *mut u16,
+pub extern "C" fn rust_property_metadata_bounds(
+ out_first_longhand: &mut u16,
+ out_last_longhand: &mut u16,
+ out_first_inherited: &mut u16,
+ out_last_inherited: &mut u16,
) {
- unsafe {
- *out_first_longhand = FIRST_LONGHAND_PROPERTY_ID;
- *out_last_longhand = LAST_LONGHAND_PROPERTY_ID;
- *out_first_inherited = FIRST_INHERITED_PROPERTY_ID;
- *out_last_inherited = LAST_INHERITED_PROPERTY_ID;
- }
+ *out_first_longhand = FIRST_LONGHAND_PROPERTY_ID;
+ *out_last_longhand = LAST_LONGHAND_PROPERTY_ID;
+ *out_first_inherited = FIRST_INHERITED_PROPERTY_ID;
+ *out_last_inherited = LAST_INHERITED_PROPERTY_ID;
}
pub fn property_is_shorthand(property_id: u16) -> bool {
@@ -512,14 +506,12 @@ pub extern "C" fn rust_property_metadata_is_shorthand(property_id: u16) -> bool
property_is_shorthand(property_id)
}
-/// # Safety
-/// `out_length` must be a valid pointer.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_property_metadata_longhands_for_shorthand(
+pub extern "C" fn rust_property_metadata_longhands_for_shorthand(
property_id: u16,
- out_length: *mut usize,
+ out_length: &mut usize,
) -> *const u16 {
let longhands = longhands_for_shorthand(property_id);
- unsafe { *out_length = longhands.len() };
+ *out_length = longhands.len();
longhands.as_ptr()
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
index 99e3f32d3bf..b7f687e02bd 100644
--- a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
@@ -1331,28 +1331,20 @@ 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.
+/// Ends the trace and writes it to `buffer`, NUL-terminated and cut to fit, answering its length.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn style_engine_end_bound_failure_trace(
- host: &DocumentHost,
- buffer: *mut u8,
- capacity: usize,
-) -> usize {
+pub extern "C" fn style_engine_end_bound_failure_trace(host: &DocumentHost, buffer: ak::SpanMut<'_, u8>) -> 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);
+ let buffer = buffer.into_slice();
+ let length = trace.len().min(buffer.len().saturating_sub(1));
+ buffer[..length].copy_from_slice(&trace.as_bytes()[..length]);
+ if let Some(terminator) = buffer.get_mut(length) {
+ *terminator = 0;
}
length
}
@@ -1569,17 +1561,13 @@ pub extern "C" fn style_engine_native_rule_id(
}
/// The name of the engine's counter at `index`, with its length and value written out, or null past the last one.
-///
-/// # Safety
-///
-/// `host` must be a live document host, on its document's thread, and the out pointers writable.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn style_engine_counter(
+pub extern "C" fn style_engine_counter(
host: &DocumentHost,
read: &crate::render_state::BegunRead,
index: usize,
- out_value: *mut u64,
- out_name_length: *mut usize,
+ out_value: &mut u64,
+ out_name_length: &mut usize,
) -> *const u8 {
let counter = with_engine(read, host, |engine| {
let retired = engine.computed_group_sets.retired_animation_overlay_records();
@@ -1591,11 +1579,8 @@ pub unsafe extern "C" fn style_engine_counter(
let Some((name, value)) = counter else {
return std::ptr::null();
};
- // SAFETY: Guaranteed by the caller.
- unsafe {
- out_value.write(value);
- out_name_length.write(name.len());
- }
+ *out_value = value;
+ *out_name_length = name.len();
name.as_ptr()
}
diff --git a/Libraries/LibWeb/Rust/src/css/table_group_builder.rs b/Libraries/LibWeb/Rust/src/css/table_group_builder.rs
index a14473e71e6..0edfe48a060 100644
--- a/Libraries/LibWeb/Rust/src/css/table_group_builder.rs
+++ b/Libraries/LibWeb/Rust/src/css/table_group_builder.rs
@@ -1035,19 +1035,15 @@ pub(crate) fn transformation_to_matrix(
/// two-dimensional. False when a function needs a box or a font to resolve: a relative length, a
/// percentage of a length, a calculation that does not simplify, or a tree-counting function.
/// https://drafts.fxtf.org/geometry/#parse-a-string-into-an-abstract-matrix
-///
-/// # Safety
-/// `value` must be a live style value, and `out_matrix` must point at 16 writable floats, row by
-/// row.
+/// The matrix is written to the 16 floats of `out_matrix`, row by row.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_transform_list_to_abstract_matrix(
- value: *const c_void,
- out_matrix: *mut f32,
- out_is_2d: *mut bool,
+pub extern "C" fn rust_transform_list_to_abstract_matrix(
+ value: &StyleValueData,
+ out_matrix: ak::SpanMut<'_, f32>,
+ out_is_2d: &mut bool,
) -> bool {
use crate::css::serialize::transform_function as functions;
- let value = unsafe { &*value.cast::<StyleValueData>() };
// The keyword none is no function at all.
let transform_list = match value {
StyleValueData::ValueList { values, .. } => values.as_slice(),
@@ -1098,11 +1094,8 @@ pub unsafe extern "C" fn rust_transform_list_to_abstract_matrix(
elements: std::array::from_fn(|row| std::array::from_fn(|column| function_matrix[row * 4 + column])),
});
}
- // SAFETY: The caller provides room for the 16 elements and the flag.
- unsafe {
- std::ptr::copy_nonoverlapping(matrix.elements.as_flattened().as_ptr(), out_matrix, 16);
- *out_is_2d = is_2d;
- }
+ out_matrix.into_slice().copy_from_slice(matrix.elements.as_flattened());
+ *out_is_2d = is_2d;
true
}
diff --git a/Libraries/LibWeb/Rust/src/painting/ffi.rs b/Libraries/LibWeb/Rust/src/painting/ffi.rs
index 5c721351ee1..a5a036e4852 100644
--- a/Libraries/LibWeb/Rust/src/painting/ffi.rs
+++ b/Libraries/LibWeb/Rust/src/painting/ffi.rs
@@ -352,16 +352,12 @@ pub extern "C" fn render_state_paintable_visual_context_node_count(
})
}
-/// # Safety
-///
-/// `host` must be a live document host, on its document's thread; `out` must have room for `capacity` indices.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn render_state_paintable_visual_context_copy_node_indices(
+pub extern "C" fn render_state_paintable_visual_context_copy_node_indices(
host: &DocumentHost,
slot: NodeSlotId,
list: crate::painting::host::FfiVisualContextBoxNodeList,
- out: *mut u32,
- capacity: usize,
+ out: ak::SpanMut<'_, u32>,
) {
let indices = {
read_arena(host, node_read(), (slot, list), |arena, (slot, list)| {
@@ -375,9 +371,7 @@ pub unsafe extern "C" fn render_state_paintable_visual_context_copy_node_indices
})
})
};
- assert!(indices.len() <= capacity);
- // SAFETY: the caller warrants `capacity` writable indices behind `out`.
- unsafe { std::ptr::copy_nonoverlapping(indices.as_ptr(), out, indices.len()) };
+ out.into_slice()[..indices.len()].copy_from_slice(&indices);
}
pub(super) fn apply_walk_assignments(