Skip to content

Lightpanda: the headless browser designed for AI and automation

mem: limit v8 arraybuffer allocation to 4GB

Karl Seguin1108c75be6b3
diff --git a/src/browser/js/Env.zig b/src/browser/js/Env.zig
index b111a52f9..357fd2859 100644
--- a/src/browser/js/Env.zig
+++ b/src/browser/js/Env.zig
@@ -153,7 +153,7 @@ pub fn init(app: *App, opts: InitOpts) !Env {
v8.v8__Isolate__CreateParams__CONSTRUCT(params);
params.snapshot_blob = @ptrCast(&snapshot.startup_data);
- params.array_buffer_allocator = v8.v8__ArrayBuffer__Allocator__NewDefaultAllocator(4 * 1024 * 1024 * 1024).?;
+ params.array_buffer_allocator = v8.v8__ArrayBuffer__Allocator__NewDefaultAllocator(js.ArrayBuffer.MAX_LENGTH).?;
errdefer v8.v8__ArrayBuffer__Allocator__DELETE(params.array_buffer_allocator.?);
params.external_references = &snapshot.external_references;
diff --git a/src/browser/js/js.zig b/src/browser/js/js.zig
index 3e4ac4bdd..69ee8ba4b 100644
--- a/src/browser/js/js.zig
+++ b/src/browser/js/js.zig
@@ -167,6 +167,10 @@ pub fn TypedArray(comptime T: type) type {
pub const ArrayBuffer = struct {
values: []const u8,
+ // Larger lengths throw a RangeError. Nothing real needs more, and an
+ // overcommitted buffer that size can take the whole process down.
+ pub const MAX_LENGTH = 4 * 1024 * 1024 * 1024;
+
pub fn dupe(self: ArrayBuffer, allocator: Allocator) !ArrayBuffer {
return .{ .values = try allocator.dupe(u8, self.values) };
}
diff --git a/src/browser/tests/window/window.html b/src/browser/tests/window/window.html
index 8347c24be..7c7cc5162 100644
--- a/src/browser/tests/window/window.html
+++ b/src/browser/tests/window/window.html
@@ -368,6 +368,12 @@
}
</script>
+<script id=array_buffer_max_length>
+ // Past the cap; at 32GB+, V8 used to abort the process on a CHECK.
+ testing.expectError('RangeError', () => new ArrayBuffer(4 * 1024 ** 3 + 1));
+ testing.expectError('RangeError', () => new ArrayBuffer(8, { maxByteLength: 4 * 1024 ** 3 + 1 }));
+</script>
+
<script id=cached_getter_wrong_this>
// Test that extracting a cached property getter and calling it with wrong `this`
// doesn't crash (V8 internal field out of bounds). V8's Signature validation