Skip to content

Truly independent web browser

LibJS: Make creating a block of slices unsafe

Andreas Kling1adf113c123f
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
index ebe16c51b29..f869c8be6b2 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
@@ -1915,12 +1915,15 @@ impl Generator<'_> {
enclosing_code
};
self.seal_stream();
- let mut link_block = SliceBlock::builder(
- SliceBlock::room_for::<PendingSharedFunctionData>(nested_count)
- + SliceBlock::room_for::<PendingClassBlueprint>(self.class_blueprints.len())
- + SliceBlock::room_for::<ConstantValue>(self.context.constants.values.len())
- + self.context.names.room_for_link_tables(),
- );
+ // SAFETY: The slices and the block end up in an ExecutableData, which drops the slices first.
+ let mut link_block = unsafe {
+ SliceBlock::builder(
+ SliceBlock::room_for::<PendingSharedFunctionData>(nested_count)
+ + SliceBlock::room_for::<PendingClassBlueprint>(self.class_blueprints.len())
+ + SliceBlock::room_for::<ConstantValue>(self.context.constants.values.len())
+ + self.context.names.room_for_link_tables(),
+ )
+ };
let shared_function_data = link_block.slice((0..nested_count).map(|nested| {
let nested_function = &mut self.context.scope.nested_functions[nested];
let row = nested_function.row;
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
index 942d16e421d..2136fe3b703 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
@@ -3643,12 +3643,15 @@ impl Generator<'_> {
}
}
let source_entry_count = head.source_entries.len() + segment.source_entries.len();
- let mut block = SliceBlock::builder(
- SliceBlock::room_for::<u8>(head.bytes.len() + segment.bytes.len())
- + SliceBlock::room_for::<SourceOffsetEntry>(source_entry_count)
- + SliceBlock::room_for::<ExceptionHandler>(exception_handlers.len())
- + other_tables_size,
- );
+ // SAFETY: The slices and the block end up in an ExecutableData, which drops the slices first.
+ let mut block = unsafe {
+ SliceBlock::builder(
+ SliceBlock::room_for::<u8>(head.bytes.len() + segment.bytes.len())
+ + SliceBlock::room_for::<SourceOffsetEntry>(source_entry_count)
+ + SliceBlock::room_for::<ExceptionHandler>(exception_handlers.len())
+ + other_tables_size,
+ )
+ };
let mut bytecode = block.slice_from_parts(&head.bytes, &segment.bytes);
let head_positions = head.operand_positions.iter().map(|position| position.offset);
let positions = segment
diff --git a/Libraries/LibJS/Rust/src/bytecode/executable.rs b/Libraries/LibJS/Rust/src/bytecode/executable.rs
index 34b3929ff8a..bf446e2e799 100644
--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs
@@ -1395,7 +1395,8 @@ impl Executable {
source_code: Option<&Rc<SourceCode>>,
) -> Gc<Executable> {
let executable = DeferGc::scope(vm, |_| {
- let mut tables = SliceBlock::builder(Self::size_of_tables(&data));
+ // SAFETY: The slices and the block end up in the executable, which drops the slices first.
+ let mut tables = unsafe { SliceBlock::builder(Self::size_of_tables(&data)) };
let is_strict = data.is_strict;
let shared_function_data = match Arc::get_mut(&mut data) {
Some(code) => tables.slice(code.shared_function_data.take_elements().map(|pending| {
diff --git a/Libraries/LibJS/Rust/src/slice_block.rs b/Libraries/LibJS/Rust/src/slice_block.rs
index 5221b10c2b4..5620a042be6 100644
--- a/Libraries/LibJS/Rust/src/slice_block.rs
+++ b/Libraries/LibJS/Rust/src/slice_block.rs
@@ -46,7 +46,11 @@ impl SliceBlock {
}
/// A block with `size` bytes for slices to be put in.
- pub fn builder(size: usize) -> SliceBlockBuilder {
+ ///
+ /// # Safety
+ ///
+ /// Slices do not keep their block alive, so every slice put in this one has to go away before it does.
+ pub unsafe fn builder(size: usize) -> SliceBlockBuilder {
if size == 0 {
return SliceBlockBuilder {
block: Self::empty(),