Skip to content

Truly independent web browser

Compositor: Don’t abort when a 2D canvas surface can’t be allocated

sideshowbarker9d5d401129e4
diff --git a/Services/Compositor/Canvas2DCommandPlayer.cpp b/Services/Compositor/Canvas2DCommandPlayer.cpp
index 0a06ab432b2..c335965a48b 100644
--- a/Services/Compositor/Canvas2DCommandPlayer.cpp
+++ b/Services/Compositor/Canvas2DCommandPlayer.cpp
@@ -25,9 +25,15 @@ namespace Compositor {
using namespace Compositing;
-Canvas2DCommandPlayer::Canvas2DCommandPlayer(RefPtr<Gfx::SkiaBackendContext> skia_backend_context, Gfx::IntSize size, Gfx::BitmapFormat format, Gfx::AlphaType alpha_type, Gfx::ColorSpace const& color_space, Resolvers resolvers)
+ErrorOr<NonnullOwnPtr<Canvas2DCommandPlayer>> Canvas2DCommandPlayer::create(RefPtr<Gfx::SkiaBackendContext> skia_backend_context, Gfx::IntSize size, Gfx::BitmapFormat format, Gfx::AlphaType alpha_type, Gfx::ColorSpace const& color_space, Resolvers resolvers)
+{
+ auto surface = TRY(Gfx::PaintingSurface::try_create_with_size(size, format, alpha_type, move(skia_backend_context), color_space));
+ return make<Canvas2DCommandPlayer>(move(surface), color_space, move(resolvers));
+}
+
+Canvas2DCommandPlayer::Canvas2DCommandPlayer(NonnullRefPtr<Gfx::PaintingSurface> surface, Gfx::ColorSpace const& color_space, Resolvers resolvers)
: m_color_space(color_space)
- , m_surface(Gfx::PaintingSurface::create_with_size(size, format, alpha_type, move(skia_backend_context), m_color_space))
+ , m_surface(move(surface))
, m_painter(make<Gfx::PainterSkia>(*m_surface))
, m_resolvers(move(resolvers))
{
diff --git a/Services/Compositor/Canvas2DCommandPlayer.h b/Services/Compositor/Canvas2DCommandPlayer.h
index 10fd97fcb72..8e968cc6731 100644
--- a/Services/Compositor/Canvas2DCommandPlayer.h
+++ b/Services/Compositor/Canvas2DCommandPlayer.h
@@ -48,8 +48,10 @@ public:
VideoFrameImageResolver video_frame_image;
};
- // The pixels of the surface, and the colors of the commands, are in the color space.
- Canvas2DCommandPlayer(RefPtr<Gfx::SkiaBackendContext>, Gfx::IntSize, Gfx::BitmapFormat, Gfx::AlphaType, Gfx::ColorSpace const&, Resolvers);
+ // The pixels of the surface, and the colors of the commands, are in the color space. Fails when a surface of the
+ // size can't be allocated.
+ static ErrorOr<NonnullOwnPtr<Canvas2DCommandPlayer>> create(RefPtr<Gfx::SkiaBackendContext>, Gfx::IntSize, Gfx::BitmapFormat, Gfx::AlphaType, Gfx::ColorSpace const&, Resolvers);
+ Canvas2DCommandPlayer(NonnullRefPtr<Gfx::PaintingSurface>, Gfx::ColorSpace const&, Resolvers);
~Canvas2DCommandPlayer();
NonnullRefPtr<Gfx::PaintingSurface> surface() const;
diff --git a/Services/Compositor/CanvasHost.cpp b/Services/Compositor/CanvasHost.cpp
index 37059444372..edbda19e4be 100644
--- a/Services/Compositor/CanvasHost.cpp
+++ b/Services/Compositor/CanvasHost.cpp
@@ -87,13 +87,19 @@ OwnPtr<Canvas2DCommandPlayer> CanvasHost::create_2d_command_player(Gfx::IntSize
auto video_frame_image_resolver = [this, skia_backend_context_of_canvas](u64 video_sink_handle, u64 generation) {
return held_video_frame_image(Media::VideoSinkHandle { video_sink_handle }, generation, skia_backend_context_of_canvas);
};
- auto player = make<Canvas2DCommandPlayer>(skia_backend_context_of_canvas, size, format, Gfx::AlphaType::Premultiplied, color_space,
+ auto player_or_error = Canvas2DCommandPlayer::create(skia_backend_context_of_canvas, size, format, Gfx::AlphaType::Premultiplied, color_space,
Canvas2DCommandPlayer::Resolvers {
.canvas_surface = move(canvas_surface_resolver),
.font = move(font_resolver),
.image_frame = move(image_frame_resolver),
.video_frame_image = move(video_frame_image_resolver),
});
+ // NB: Memory for a canvas of a size that passed the check above can still run out.
+ if (player_or_error.is_error()) {
+ dbgln("Unable to create a {}x{} 2D canvas context: {}", size.width(), size.height(), player_or_error.error());
+ return nullptr;
+ }
+ auto player = player_or_error.release_value();
// https://html.spec.whatwg.org/multipage/canvas.html#the-canvas-settings:concept-canvas-alpha
// "Thus, the bitmap of such a context starts off as opaque black instead of transparent black"
@@ -109,13 +115,13 @@ static void copy_surface_contents(Gfx::PaintingSurface& source, Gfx::PaintingSur
destination.copy_from_surface(source);
}
-static NonnullRefPtr<Gfx::PaintingSurface> create_presented_canvas_surface(Gfx::PaintingSurface& source, RefPtr<Gfx::SkiaBackendContext> skia_backend_context)
+static ErrorOr<NonnullRefPtr<Gfx::PaintingSurface>> create_presented_canvas_surface(Gfx::PaintingSurface& source, RefPtr<Gfx::SkiaBackendContext> skia_backend_context)
{
- auto surface = Gfx::PaintingSurface::create_with_size(
+ auto surface = TRY(Gfx::PaintingSurface::try_create_with_size(
source.size(),
Gfx::BitmapFormat::BGRA8888,
Gfx::AlphaType::Premultiplied,
- move(skia_backend_context));
+ move(skia_backend_context)));
copy_surface_contents(source, *surface);
return surface;
}
@@ -135,7 +141,12 @@ bool CanvasHost::create_2d_context(Compositing::CanvasId canvas_id, Gfx::IntSize
// NB: A canvas that draws in memory still presents onto the GPU, if there is one. A present then uploads it once,
// instead of every composited frame uploading a fresh snapshot of it.
- auto presented_surface = create_presented_canvas_surface(command_player->surface(), m_skia_backend_context);
+ auto presented_surface_or_error = create_presented_canvas_surface(command_player->surface(), m_skia_backend_context);
+ if (presented_surface_or_error.is_error()) {
+ dbgln("Unable to create the presented surface of a {}x{} 2D canvas context: {}", size.width(), size.height(), presented_surface_or_error.error());
+ return false;
+ }
+ auto presented_surface = presented_surface_or_error.release_value();
m_canvas_surface_registry.set_canvas_surface(m_canvas_surface_owner_id, canvas_id, presented_surface);
Canvas2DContext context {
.command_player = command_player.release_nonnull(),
diff --git a/Services/Compositor/PaintingSurface.cpp b/Services/Compositor/PaintingSurface.cpp
index 8a4ef680765..b6e5761a82c 100644
--- a/Services/Compositor/PaintingSurface.cpp
+++ b/Services/Compositor/PaintingSurface.cpp
@@ -139,6 +139,11 @@ static sk_sp<SkColorSpace> skia_color_space_of_pixels(ColorSpace const& color_sp
}
NonnullRefPtr<PaintingSurface> PaintingSurface::create_with_size(IntSize size, BitmapFormat color_type, AlphaType alpha_type, RefPtr<SkiaBackendContext> context, ColorSpace const& color_space)
+{
+ return MUST(try_create_with_size(size, color_type, alpha_type, move(context), color_space));
+}
+
+ErrorOr<NonnullRefPtr<PaintingSurface>> PaintingSurface::try_create_with_size(IntSize size, BitmapFormat color_type, AlphaType alpha_type, RefPtr<SkiaBackendContext> context, ColorSpace const& color_space)
{
auto sk_color_type = to_skia_color_type(color_type);
auto sk_alpha_type = to_skia_alpha_type(color_type, alpha_type);
@@ -155,9 +160,10 @@ NonnullRefPtr<PaintingSurface> PaintingSurface::create_with_size(IntSize size, B
context = nullptr;
}
- auto bitmap = Bitmap::create(color_type, alpha_type, size).value();
+ auto bitmap = TRY(Bitmap::create(color_type, alpha_type, size));
auto surface = SkSurfaces::WrapPixels(image_info, bitmap->begin(), bitmap->pitch());
- VERIFY(surface);
+ if (!surface)
+ return Error::from_string_literal("Unable to create a surface for the bitmap");
return adopt_ref(*new PaintingSurface(make<Impl>(context, size, surface, bitmap)));
}
diff --git a/Services/Compositor/PaintingSurface.h b/Services/Compositor/PaintingSurface.h
index 09b34ac6a77..a420ab8dc6c 100644
--- a/Services/Compositor/PaintingSurface.h
+++ b/Services/Compositor/PaintingSurface.h
@@ -55,6 +55,8 @@ public:
// A surface whose pixels are in the color space, which is sRGB unless one is given.
static NonnullRefPtr<PaintingSurface> create_with_size(IntSize size, BitmapFormat color_type, AlphaType alpha_type, RefPtr<SkiaBackendContext> = {}, ColorSpace const& = {});
+ // Fails when neither a GPU surface nor a bitmap of the size can be allocated.
+ static ErrorOr<NonnullRefPtr<PaintingSurface>> try_create_with_size(IntSize size, BitmapFormat color_type, AlphaType alpha_type, RefPtr<SkiaBackendContext> = {}, ColorSpace const& = {});
static NonnullRefPtr<PaintingSurface> wrap_bitmap(Bitmap&);
#ifdef AK_OS_MACOS
diff --git a/Tests/Compositor/TestPaintingSurface.cpp b/Tests/Compositor/TestPaintingSurface.cpp
index 34a2d1d4e34..c411e074019 100644
--- a/Tests/Compositor/TestPaintingSurface.cpp
+++ b/Tests/Compositor/TestPaintingSurface.cpp
@@ -143,3 +143,9 @@ TEST_CASE(flush_surface_skia_allocated)
surface->read_into_bitmap(*bitmap);
EXPECT_EQ(count_red_pixels(*bitmap, { {}, size }), size.width() * size.height());
}
+
+TEST_CASE(surface_too_wide_for_a_bitmap_is_an_error)
+{
+ auto surface = Gfx::PaintingSurface::try_create_with_size({ 70000, 1 }, Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied);
+ EXPECT(surface.is_error());
+}
diff --git a/Tests/LibWeb/Crash/HTML/canvas-2d-wider-than-a-bitmap-can-be.html b/Tests/LibWeb/Crash/HTML/canvas-2d-wider-than-a-bitmap-can-be.html
new file mode 100644
index 00000000000..dcb5f68daab
--- /dev/null
+++ b/Tests/LibWeb/Crash/HTML/canvas-2d-wider-than-a-bitmap-can-be.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<canvas id="canvas" width="70000" height="1"></canvas>
+<script>
+ // The canvas is within the area a 2D context allows, but wider than a bitmap can be.
+ const context = document.getElementById("canvas").getContext("2d");
+ context.fillStyle = "green";
+ context.fillRect(0, 0, 10, 1);
+ context.getImageData(0, 0, 1, 1);
+</script>