Skip to content

Truly independent web browser

LibGC+LibJS+LibWasm: Check the size of each mirror of GCLayout

Andreas Kling5afe07533c47
diff --git a/Libraries/LibGC/CAPI.cpp b/Libraries/LibGC/CAPI.cpp
index cc0a581c219..915837f4692 100644
--- a/Libraries/LibGC/CAPI.cpp
+++ b/Libraries/LibGC/CAPI.cpp
@@ -210,8 +210,9 @@ void** gc_heap_identity_hashes(void* heap)
return reinterpret_cast<void**>(CAPI::identity_hashes(*static_cast<Heap*>(heap)));
}
-void gc_get_layout(GCLayout* layout)
+void gc_get_layout(GCLayout* layout, size_t layout_size)
{
+ VERIFY(layout_size == sizeof(GCLayout));
CAPI::get_layout(*layout);
}
diff --git a/Libraries/LibGC/CAPI.h b/Libraries/LibGC/CAPI.h
index 2e16a000e5a..5b1093ab907 100644
--- a/Libraries/LibGC/CAPI.h
+++ b/Libraries/LibGC/CAPI.h
@@ -107,7 +107,9 @@ typedef struct GCLayout {
uint8_t identity_hash_moved;
} GCLayout;
-GC_API void gc_get_layout(GCLayout*);
+// The caller passes the size of its GCLayout, which must match LibGC's, so that a mirror that drifted is caught before
+// anything is written to it.
+GC_API void gc_get_layout(GCLayout*, size_t layout_size);
// The base of the region all cells are allocated from, which NaN-boxed cell pointers are relative to. Reserves the
// region on first use.
diff --git a/Libraries/LibJS/Rust/src/gc/capi.rs b/Libraries/LibJS/Rust/src/gc/capi.rs
index c75ac7bbbed..5fe175ac313 100644
--- a/Libraries/LibJS/Rust/src/gc/capi.rs
+++ b/Libraries/LibJS/Rust/src/gc/capi.rs
@@ -91,7 +91,7 @@ pub type GCCallback = unsafe extern "C" fn(context: *mut c_void);
pub type GCGatherRootsCallback = unsafe extern "C" fn(context: *mut c_void, root_visitor: *mut GCVisitor);
unsafe extern "C" {
- pub safe fn gc_get_layout(layout: &mut GCLayout);
+ pub safe fn gc_get_layout(layout: &mut GCLayout, layout_size: usize);
pub safe fn gc_heap_region_base() -> usize;
pub safe fn gc_primitive_storage_cage_base() -> usize;
diff --git a/Libraries/LibJS/Rust/src/gc/heap.rs b/Libraries/LibJS/Rust/src/gc/heap.rs
index 3094788cad4..7074f373b8e 100644
--- a/Libraries/LibJS/Rust/src/gc/heap.rs
+++ b/Libraries/LibJS/Rust/src/gc/heap.rs
@@ -816,7 +816,7 @@ impl Drop for Heap {
/// Checks the layouts this crate mirrors against the LibGC it is linked with, and returns LibGC's layout.
fn check_layout() -> GCLayout {
let mut layout = GCLayout::default();
- capi::gc_get_layout(&mut layout);
+ capi::gc_get_layout(&mut layout, size_of::<GCLayout>());
let expect = |what: &str, actual: u64, expected: u64| {
assert!(
actual == expected,
diff --git a/Libraries/LibWasm/Engine/src/runtime/heap.rs b/Libraries/LibWasm/Engine/src/runtime/heap.rs
index a1cb83ca1a4..2e1a18cb2c8 100644
--- a/Libraries/LibWasm/Engine/src/runtime/heap.rs
+++ b/Libraries/LibWasm/Engine/src/runtime/heap.rs
@@ -55,6 +55,9 @@ pub struct GcLayout {
pub heap_region_offset_mask: u64,
pub primitive_storage_cage_offset_mask: u64,
pub visitor_marker_offset: u32,
+ pub identity_hash_state_offset: u32,
+ pub identity_hash_hashed: u8,
+ pub identity_hash_moved: u8,
}
/// Mirrors GCPrimitiveStorageLayout.
@@ -72,7 +75,7 @@ pub struct PrimitiveStorageLayout {
#[repr(C)]
pub struct GcInterface {
pub heap: *mut c_void,
- pub gc_get_layout: unsafe extern "C" fn(layout: *mut GcLayout),
+ pub gc_get_layout: unsafe extern "C" fn(layout: *mut GcLayout, layout_size: usize),
pub gc_allocator_create: unsafe extern "C" fn(
type_info: *const CellTypeInfo,
name: *const c_char,
@@ -346,7 +349,7 @@ impl Heap {
return Self::Standalone(core::cell::RefCell::new(Vec::new()));
};
let mut layout = GcLayout::default();
- unsafe { (interface.gc_get_layout)(&raw mut layout) };
+ unsafe { (interface.gc_get_layout)(&raw mut layout, size_of::<GcLayout>()) };
assert!(
layout.cell_state_offset as usize >= 8 && (layout.cell_kind_offset as usize) < CELL_HEADER_SIZE,
"LibGC keeps its cell header in the second word"
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/mod.rs
index 2ee759e5da3..75e47305017 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/mod.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/mod.rs
@@ -66,7 +66,7 @@ pub(crate) const PAGE_SIZE: u64 = 65536;
unsafe extern "C" {
// The functions of Libraries/LibGC/CAPI.h that the engine uses, with the types of its GcInterface.
- fn gc_get_layout(layout: *mut GcLayout);
+ fn gc_get_layout(layout: *mut GcLayout, layout_size: usize);
fn gc_allocator_create(
type_info: *const CellTypeInfo,
name: *const c_char,