Truly independent web browser
LibWeb: Take the WebGL context cell as a typed reference
Andreas Klingd59213d11d7c
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/canvas.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/canvas.rsindex 2627d595a04..9d80bce1e04 100644--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/canvas.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/canvas.rs@@ -39,15 +39,15 @@ unsafe extern "C" {fn web_webgl_canvas_bitmap_size(canvas_host: *mut c_void, out_width: *mut i32, out_height: *mut i32);fn web_webgl_canvas_wrapper(canvas: *mut c_void, canvas_is_offscreen: bool, realm: *mut c_void) -> *mut c_void;fn web_webgl_canvas_relevant_realm(canvas_host: *mut c_void) -> *mut c_void;- fn web_webgl_expose_experimental_interfaces() -> bool;- fn web_webgl_is_valid_2d_pixel_unpack_state(width: i32, unpack_state: *const PixelUnpackState) -> bool;- fn web_webgl_required_2d_texture_data_size(+ safe fn web_webgl_expose_experimental_interfaces() -> bool;+ safe fn web_webgl_is_valid_2d_pixel_unpack_state(width: i32, unpack_state: &PixelUnpackState) -> bool;+ safe fn web_webgl_required_2d_texture_data_size(width: i32,height: i32,format: u32,r#type: u32,- unpack_state: *const PixelUnpackState,- out_size: *mut usize,+ unpack_state: &PixelUnpackState,+ out_size: &mut usize,) -> bool;fn web_webgl_read_tex_image_source(source_wrapper: *mut c_void,@@ -96,13 +96,11 @@ impl Canvas {}pub(crate) fn expose_experimental_interfaces() -> bool {- // SAFETY: The function has no preconditions.- unsafe { web_webgl_expose_experimental_interfaces() }+ web_webgl_expose_experimental_interfaces()}pub(crate) fn is_valid_2d_pixel_unpack_state(width: i32, unpack_state: &PixelUnpackState) -> bool {- // SAFETY: The state is live for the call.- unsafe { web_webgl_is_valid_2d_pixel_unpack_state(width, unpack_state) }+ web_webgl_is_valid_2d_pixel_unpack_state(width, unpack_state)}pub(crate) fn required_2d_texture_data_size(@@ -113,9 +111,7 @@ pub(crate) fn required_2d_texture_data_size(unpack_state: &PixelUnpackState,) -> Option<usize> {let mut size = 0;- // SAFETY: The state is live for the call, and the size is stored when the function returns true.- unsafe { web_webgl_required_2d_texture_data_size(width, height, format, r#type, unpack_state, &raw mut size) }- .then_some(size)+ web_webgl_required_2d_texture_data_size(width, height, format, r#type, unpack_state, &mut size).then_some(size)}pub(crate) enum TexImageSourceFrame {diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/exports.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/exports.rsindex 0a77f7a93be..578518cba17 100644--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/exports.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/exports.rs@@ -8,6 +8,7 @@//! context of either kind.use core::ffi::c_void;+use core::marker::PhantomData;use core::ptr::NonNull;use super::canvas::Canvas;@@ -124,12 +125,19 @@ pub struct ContextCreationArguments {supported_extension_count: usize,}-/// # Safety-///-/// `context` is the live cell of a WebGLRenderingContext or of a WebGL2RenderingContext.-unsafe fn with_context<R>(context: *mut c_void, function: impl FnOnce(&ContextState) -> R) -> R {- let cell = NonNull::new(context).expect("C++ passes the cell of a context");- // SAFETY: The caller passes a live cell of one of the two classes, which the downcasts tell apart.+/// The cell of a WebGLRenderingContext or of a WebGL2RenderingContext, which only C++ lends out, for as long as the+/// context is live.+#[repr(C)]+pub struct ContextCell {+ // NB: The cell is mutable behind a shared reference, and bigger than Rust sees, so a reference to one must not tell+ // the compiler that the memory behind it is read only or unaliased.+ _opaque: core::cell::UnsafeCell<[u8; 0]>,+ _not_send_sync_or_unpin: PhantomData<(*const (), core::marker::PhantomPinned)>,+}++fn with_context<R>(context: &ContextCell, function: impl FnOnce(&ContextState) -> R) -> R {+ let cell = NonNull::from(context).cast();+ // SAFETY: A ContextCell is the live cell of one of the two classes, which the downcasts tell apart.unsafe {if let Some(context) = RenderingContext::<WebGL1>::CELL_CLASS.downcast(cell) {return function(&context);@@ -190,116 +198,112 @@ pub unsafe extern "C" fn web_webgl_context_create(}}-// SAFETY (of every function below): The caller passes the live cell of a context, and live pointers to what a-// function stores.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_webgl_context_canvas_host(context: *mut c_void) -> *mut c_void {- unsafe { with_context(context, |context| context.canvas().host.as_ptr()) }+pub extern "C" fn web_webgl_context_canvas_host(context: &ContextCell) -> *mut c_void {+ with_context(context, |context| context.canvas().host.as_ptr())}#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_webgl_context_realm(context: *mut c_void) -> *mut c_void {- unsafe { with_context(context, |context| context.realm().as_ptr().cast()) }+pub extern "C" fn web_webgl_context_realm(context: &ContextCell) -> *mut c_void {+ with_context(context, |context| context.realm().as_ptr().cast())}#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_webgl_context_is_webgl2(context: *mut c_void) -> bool {- unsafe { with_context(context, ContextState::is_webgl2) }+pub extern "C" fn web_webgl_context_is_webgl2(context: &ContextCell) -> bool {+ with_context(context, ContextState::is_webgl2)}#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_webgl_context_is_lost(context: *mut c_void) -> bool {- unsafe { with_context(context, ContextState::is_context_lost) }+pub extern "C" fn web_webgl_context_is_lost(context: &ContextCell) -> bool {+ with_context(context, ContextState::is_context_lost)}#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_webgl_context_canvas_id(context: *mut c_void, out_canvas_id: *mut u64) -> bool {- let Some(canvas_id) = (unsafe { with_context(context, |context| context.proxy.canvas_id()) }) else {+pub extern "C" fn web_webgl_context_canvas_id(context: &ContextCell, out_canvas_id: &mut u64) -> bool {+ let Some(canvas_id) = with_context(context, |context| context.proxy.canvas_id()) else {return false;};- unsafe { out_canvas_id.write(canvas_id) };+ *out_canvas_id = canvas_id;true}#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_webgl_context_set_size(context: *mut c_void, width: i32, height: i32) {- unsafe {- with_context(context, |context| {- context.proxy.set_size(- width.clamp(1, MAX_WEBGL_DRAWING_BUFFER_DIMENSION),- height.clamp(1, MAX_WEBGL_DRAWING_BUFFER_DIMENSION),- );- });- }+pub extern "C" fn web_webgl_context_set_size(context: &ContextCell, width: i32, height: i32) {+ with_context(context, |context| {+ context.proxy.set_size(+ width.clamp(1, MAX_WEBGL_DRAWING_BUFFER_DIMENSION),+ height.clamp(1, MAX_WEBGL_DRAWING_BUFFER_DIMENSION),+ );+ });}#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_webgl_context_present_for_compositing(context: *mut c_void) {- unsafe {- with_context(context, |context| {- let preserve_drawing_buffer = context.context_creation_parameters.preserve_drawing_buffer;- context.proxy.present_canvas_for_compositing(preserve_drawing_buffer);- });- }+pub extern "C" fn web_webgl_context_present_for_compositing(context: &ContextCell) {+ with_context(context, |context| {+ let preserve_drawing_buffer = context.context_creation_parameters.preserve_drawing_buffer;+ context.proxy.present_canvas_for_compositing(preserve_drawing_buffer);+ });}#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_webgl_context_clear_drawing_buffer(context: *mut c_void) {- unsafe { with_context(context, |context| context.proxy.clear_drawing_buffer()) };+pub extern "C" fn web_webgl_context_clear_drawing_buffer(context: &ContextCell) {+ with_context(context, |context| context.proxy.clear_drawing_buffer());}/// A Gfx::Bitmap with a reference that the caller takes over, or null.#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_webgl_context_read_back_drawing_buffer(- context: *mut c_void,+pub extern "C" fn web_webgl_context_read_back_drawing_buffer(+ context: &ContextCell,x: i32,y: i32,width: i32,height: i32,) -> *mut c_void {- let bitmap = unsafe {- with_context(context, |context| {- context.proxy.read_back_drawing_buffer(x, y, width, height)- })- };+ let bitmap = with_context(context, |context| {+ context.proxy.read_back_drawing_buffer(x, y, width, height)+ });bitmap.map_or(core::ptr::null_mut(), super::host::Bitmap::into_raw)}/// False when the context was lost already.#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_webgl_context_mark_lost(context: *mut c_void) -> bool {- unsafe { with_context(context, ContextState::mark_lost) }+pub extern "C" fn web_webgl_context_mark_lost(context: &ContextCell) -> bool {+ with_context(context, ContextState::mark_lost)}#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_webgl_context_set_restore_requested(context: *mut c_void, requested: bool) {- unsafe { with_context(context, |context| context.set_context_restore_requested(requested)) };+pub extern "C" fn web_webgl_context_set_restore_requested(context: &ContextCell, requested: bool) {+ with_context(context, |context| context.set_context_restore_requested(requested));}#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_webgl_context_wants_restoring(context: *mut c_void) -> bool {- unsafe { with_context(context, ContextState::wants_restoring) }+pub extern "C" fn web_webgl_context_wants_restoring(context: &ContextCell) -> bool {+ with_context(context, ContextState::wants_restoring)}#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_webgl_context_actual_context_parameters(- context: *mut c_void,- out_context_attributes: *mut ContextAttributes,+pub extern "C" fn web_webgl_context_actual_context_parameters(+ context: &ContextCell,+ out_context_attributes: &mut ContextAttributes,) {- let context_attributes = unsafe { with_context(context, |context| context.actual_context_parameters) };- unsafe { out_context_attributes.write(context_attributes.into()) };+ let context_attributes = with_context(context, |context| context.actual_context_parameters);+ *out_context_attributes = context_attributes.into();}/// The context takes over the reference to `transport`.+///+/// # Safety+///+/// `transport` is a RemoteWebGLTransport that has created its context, and the spans are live.#[unsafe(no_mangle)]pub unsafe extern "C" fn web_webgl_context_restore(- context: *mut c_void,+ context: &ContextCell,transport: *mut c_void,supported_extensions: *const ByteSpan,supported_extension_count: usize,) {let transport = NonNull::new(transport).expect("C++ passes the transport");+ // SAFETY: The caller passes the transport with a reference, and live names.unsafe {let supported_opengl_extensions = extension_names(supported_extensions, supported_extension_count);with_context(context, |context| {@@ -309,11 +313,11 @@ pub unsafe extern "C" fn web_webgl_context_restore(}#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_webgl_context_xr_compatible(context: *mut c_void) -> bool {- unsafe { with_context(context, ContextState::xr_compatible) }+pub extern "C" fn web_webgl_context_xr_compatible(context: &ContextCell) -> bool {+ with_context(context, ContextState::xr_compatible)}#[unsafe(no_mangle)]-pub unsafe extern "C" fn web_webgl_context_set_xr_compatible(context: *mut c_void, xr_compatible: bool) {- unsafe { with_context(context, |context| context.set_xr_compatible(xr_compatible)) };+pub extern "C" fn web_webgl_context_set_xr_compatible(context: &ContextCell, xr_compatible: bool) {+ with_context(context, |context| context.set_xr_compatible(xr_compatible));}diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/fingerprint.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/fingerprint.rsindex 1fd0d3ff2a1..a7dc75c60fc 100644--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/fingerprint.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/fingerprint.rs@@ -31,15 +31,14 @@ struct ConstantsRestatedInRust {unsafe extern "C" {fn compositing_webgl_wire_struct_layouts(out_word_count: *mut usize) -> *const u32;- fn web_webgl_constants_restated_in_rust(out_constants: *mut ConstantsRestatedInRust);+ safe fn web_webgl_constants_restated_in_rust(out_constants: &mut ConstantsRestatedInRust);}pub(crate) fn verify_that_rust_and_cpp_agree_on_layouts_and_constants() {static VERIFIED: Once = Once::new();VERIFIED.call_once(|| {let mut constants_of_the_cpp_side = ConstantsRestatedInRust::default();- // SAFETY: The function fills in the struct, which has the layout of its C++ counterpart.- unsafe { web_webgl_constants_restated_in_rust(&raw mut constants_of_the_cpp_side) };+ web_webgl_constants_restated_in_rust(&mut constants_of_the_cpp_side);let constants_of_the_rust_side = ConstantsRestatedInRust {record_alignment: RECORD_ALIGNMENT,header_size: HEADER_SIZE,diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/host.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/host.rsindex 925ca2a1b1e..582e5ae0146 100644--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/host.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/host.rs@@ -68,8 +68,8 @@ unsafe extern "C" {width: i32,height: i32,) -> *mut c_void;- fn web_webgl_max_frames_per_message() -> usize;- fn web_webgl_anonymous_buffer_create(size: usize, out_data: *mut *mut u8) -> *mut c_void;+ safe fn web_webgl_max_frames_per_message() -> usize;+ safe fn web_webgl_anonymous_buffer_create(size: usize, out_data: &mut *mut u8) -> *mut c_void;fn web_webgl_anonymous_buffer_destroy(anonymous_buffer: *mut c_void);fn web_webgl_frame_destroy(frame: *mut c_void);fn web_webgl_bitmap_unref(bitmap: *mut c_void);@@ -84,8 +84,7 @@ pub(crate) struct AnonymousBuffer {impl AnonymousBuffer {fn create(size: usize) -> Option<Self> {let mut data = ptr::null_mut();- // SAFETY: The function has no preconditions, and stores where the memory is mapped when it returns a buffer.- let buffer = NonNull::new(unsafe { web_webgl_anonymous_buffer_create(size, &raw mut data) })?;+ let buffer = NonNull::new(web_webgl_anonymous_buffer_create(size, &mut data))?;Some(Self { buffer, data, size })}}@@ -184,7 +183,7 @@ impl Transport for CppTransport {type Bitmap = Bitmap;fn max_frames_per_message(&self) -> usize {- unsafe { web_webgl_max_frames_per_message() }+ web_webgl_max_frames_per_message()}fn create_shared_memory(&self, size: usize) -> Option<AnonymousBuffer> {diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_gl/parameters.rs b/Libraries/LibWeb/Bindings/Rust/src/web_gl/parameters.rsindex 38ab6945aa7..652e6e04a21 100644--- a/Libraries/LibWeb/Bindings/Rust/src/web_gl/parameters.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/web_gl/parameters.rs@@ -70,7 +70,7 @@ use super::web_gl_shader::WebGLShader;use super::web_gl_uniform_location::WebGLUniformLocation;unsafe extern "C" {- fn web_navigator_compatibility_mode() -> u8;+ safe fn web_navigator_compatibility_mode() -> u8;}/// The navigator compatibility mode, numbered as Web::NavigatorCompatibilityMode is.@@ -79,8 +79,7 @@ const MODE_GECKO: u8 = 1;const MODE_WEBKIT: u8 = 2;fn navigator_compatibility_mode() -> u8 {- // SAFETY: The function reads a process-wide setting and takes nothing.- unsafe { web_navigator_compatibility_mode() }+ web_navigator_compatibility_mode()}/// VENDOR, RENDERER, VERSION, and SHADING_LANGUAGE_VERSION name the browser's WebGL implementation rather than the