Skip to content

Truly independent web browser

LibGfx: Decode images straight into shared memory

Andreas Kling75eeb87a3057
diff --git a/Documentation/ProcessArchitecture.md b/Documentation/ProcessArchitecture.md
index 2665fc2d99e..04f8891418a 100644
--- a/Documentation/ProcessArchitecture.md
+++ b/Documentation/ProcessArchitecture.md
@@ -72,7 +72,7 @@ Each top-level site has a **RequestServer** of its own in each browsing session,
### Process: ImageDecoder
-This process decodes images (PNG, JPEG, BMP, ICO, PBM, etc.) into bitmaps. It is strongly sandboxed, and can do little more than receive encoded image data and return bitmaps to its clients.
+This process decodes images (PNG, JPEG, BMP, ICO, PBM, etc.) into bitmaps. It is strongly sandboxed, and can do little more than receive encoded image data and return bitmaps to its clients. It decodes into shared memory, which it hands to its clients as it is.
A decoder sees the images of every process it serves, so a malicious image that takes one over gets at those images. With site isolation, the processes of a site share an **ImageDecoder**, because they hold nothing of any other site's that they could not fetch for themselves. The site is that of the documents a process hosts, not their top-level site, so a frame never shares a decoder with the site embedding it, unless it has an opaque origin, which keeps it in the process of the document that made it. Private browsing is kept apart. Any other process, such as one with no site, gets a decoder of its own, and a process that comes to host a second site leaves the decoder of the first.
diff --git a/Libraries/LibGfx/BitmapSequence.cpp b/Libraries/LibGfx/BitmapSequence.cpp
index 3bbc8de668d..47b4c0cad12 100644
--- a/Libraries/LibGfx/BitmapSequence.cpp
+++ b/Libraries/LibGfx/BitmapSequence.cpp
@@ -4,7 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <AK/Checked.h>
#include <LibCore/AnonymousBuffer.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/BitmapSequence.h>
@@ -16,7 +15,7 @@ namespace Gfx {
static BitmapMetadata get_metadata(Bitmap const& bitmap)
{
- return BitmapMetadata { .format = bitmap.format(), .alpha_type = bitmap.alpha_type(), .size = bitmap.size(), .size_in_bytes = bitmap.size_in_bytes() };
+ return BitmapMetadata { .format = bitmap.format(), .alpha_type = bitmap.alpha_type(), .size = bitmap.size() };
}
}
@@ -28,7 +27,6 @@ ErrorOr<void> encode(Encoder& encoder, Gfx::BitmapMetadata const& metadata)
{
TRY(encoder.encode(static_cast<u32>(metadata.format)));
TRY(encoder.encode(static_cast<u32>(metadata.alpha_type)));
- TRY(encoder.encode(metadata.size_in_bytes));
TRY(encoder.encode(metadata.size));
return {};
@@ -47,12 +45,13 @@ ErrorOr<Gfx::BitmapMetadata> decode(Decoder& decoder)
return Error::from_string_literal("IPC: Invalid Gfx::BitmapSequence alpha type");
auto alpha_type = static_cast<Gfx::AlphaType>(raw_alpha_type);
- auto size_in_bytes = TRY(decoder.decode<size_t>());
auto size = TRY(decoder.decode<Gfx::IntSize>());
- return Gfx::BitmapMetadata { format, alpha_type, size, size_in_bytes };
+ return Gfx::BitmapMetadata { format, alpha_type, size };
}
+// Each bitmap travels in the shared memory that backs it, so the receiver maps the very pixels the sender wrote. A bitmap
+// in private memory is copied into shared memory first.
template<>
ErrorOr<void> encode(Encoder& encoder, Gfx::BitmapSequence const& bitmap_sequence)
{
@@ -60,40 +59,19 @@ ErrorOr<void> encode(Encoder& encoder, Gfx::BitmapSequence const& bitmap_sequenc
Vector<Optional<Gfx::BitmapMetadata>> metadata;
metadata.ensure_capacity(bitmaps.size());
-
- Checked<size_t> total_buffer_size = 0;
-
- for (auto const& bitmap_option : bitmaps) {
- Optional<Gfx::BitmapMetadata> data = {};
-
- if (bitmap_option) {
- data = get_metadata(*bitmap_option);
- total_buffer_size += data->size_in_bytes;
- }
-
+ for (auto const& bitmap : bitmaps) {
+ Optional<Gfx::BitmapMetadata> data;
+ if (bitmap)
+ data = get_metadata(*bitmap);
metadata.unchecked_append(data);
}
-
- VERIFY(!total_buffer_size.has_overflow());
-
TRY(encoder.encode(metadata));
- TRY(encoder.encode(total_buffer_size.value()));
-
- if (total_buffer_size.value() > 0) {
- // collate all of the bitmap data into one contiguous buffer
- auto collated_buffer = TRY(Core::AnonymousBuffer::create_with_size(total_buffer_size.value()));
-
- Bytes buffer_bytes = { collated_buffer.data<u8>(), collated_buffer.size() };
- size_t write_offset = 0;
- for (auto const& bitmap : bitmaps) {
- if (bitmap) {
- buffer_bytes.overwrite(write_offset, bitmap->scanline(0), bitmap->size_in_bytes());
- write_offset += bitmap->size_in_bytes();
- }
- }
-
- TRY(encoder.encode(collated_buffer));
+ for (auto const& bitmap : bitmaps) {
+ if (!bitmap)
+ continue;
+ auto shareable_bitmap = TRY(bitmap->to_bitmap_backed_by_anonymous_buffer());
+ TRY(encoder.encode(shareable_bitmap->anonymous_buffer()));
}
return {};
@@ -104,49 +82,26 @@ ErrorOr<Gfx::BitmapSequence> decode(Decoder& decoder)
{
auto metadata_list = TRY(decoder.decode<Vector<Optional<Gfx::BitmapMetadata>>>());
- auto total_buffer_size = TRY(decoder.decode<size_t>());
-
- Core::AnonymousBuffer collated_buffer;
- if (total_buffer_size > 0)
- collated_buffer = TRY(decoder.decode<Core::AnonymousBuffer>());
-
Gfx::BitmapSequence result = {};
auto& bitmaps = result.bitmaps;
TRY(bitmaps.try_ensure_capacity(metadata_list.size()));
- // Single-frame sequences can keep the transferred backing directly.
- if (metadata_list.size() == 1 && metadata_list[0].has_value()) {
- auto metadata = metadata_list[0].value();
- if (!collated_buffer.is_valid() || metadata.size_in_bytes != total_buffer_size || metadata.size_in_bytes != collated_buffer.size())
- return Error::from_string_literal("IPC: Invalid Gfx::BitmapSequence buffer data");
-
- RefPtr<Gfx::Bitmap> bitmap = TRY(Gfx::Bitmap::create_with_anonymous_buffer(metadata.format, metadata.alpha_type, move(collated_buffer), metadata.size));
- bitmaps.unchecked_append(move(bitmap));
- return result;
- }
-
- ReadonlyBytes bytes = ReadonlyBytes(collated_buffer.data<u8>(), collated_buffer.size());
- size_t bytes_read = 0;
-
- // sequentially read each valid bitmap's data from the collated buffer
- for (auto const& metadata_option : metadata_list) {
+ for (auto const& metadata : metadata_list) {
RefPtr<Gfx::Bitmap> bitmap;
-
- if (metadata_option.has_value()) {
- auto metadata = metadata_option.value();
- size_t size_in_bytes = metadata.size_in_bytes;
-
- Checked<size_t> size_check = bytes_read;
- size_check += size_in_bytes;
- if (size_check.has_overflow() || size_check.value() > bytes.size())
+ if (metadata.has_value()) {
+ auto buffer = TRY(decoder.decode<Core::AnonymousBuffer>());
+ if (!buffer.is_valid())
return Error::from_string_literal("IPC: Invalid Gfx::BitmapSequence buffer data");
-
- auto slice = bytes.slice(bytes_read, size_in_bytes);
- bytes_read += size_in_bytes;
- bitmap = TRY(Gfx::Bitmap::create_with_raw_data(metadata.format, metadata.alpha_type, slice, metadata.size));
+ // NB: A buffer that is too small is turned down below. One larger than a shareable bitmap of this size would
+ // have is turned down here, as the receiver would keep all of it mapped for as long as the bitmap lives.
+ if (Gfx::Bitmap::size_would_overflow(metadata->format, metadata->size))
+ return Error::from_string_literal("IPC: Invalid Gfx::BitmapSequence size");
+ auto data_size = Gfx::Bitmap::size_in_bytes(Gfx::Bitmap::minimum_pitch(metadata->size.width(), metadata->format), metadata->size.height());
+ if (buffer.size() > round_up_to_power_of_two(data_size, PAGE_SIZE))
+ return Error::from_string_literal("IPC: Oversized Gfx::BitmapSequence buffer");
+ bitmap = TRY(Gfx::Bitmap::create_with_anonymous_buffer(metadata->format, metadata->alpha_type, move(buffer), metadata->size));
}
-
- bitmaps.append(bitmap);
+ bitmaps.unchecked_append(move(bitmap));
}
return result;
diff --git a/Libraries/LibGfx/BitmapSequence.h b/Libraries/LibGfx/BitmapSequence.h
index 8a1d8fedd35..79b1a9e6b27 100644
--- a/Libraries/LibGfx/BitmapSequence.h
+++ b/Libraries/LibGfx/BitmapSequence.h
@@ -23,7 +23,6 @@ struct BitmapMetadata {
Gfx::BitmapFormat format;
Gfx::AlphaType alpha_type;
Gfx::IntSize size;
- size_t size_in_bytes;
};
}
diff --git a/Libraries/LibGfx/CMYKBitmap.cpp b/Libraries/LibGfx/CMYKBitmap.cpp
index 2ea6b798f55..95cdce9d557 100644
--- a/Libraries/LibGfx/CMYKBitmap.cpp
+++ b/Libraries/LibGfx/CMYKBitmap.cpp
@@ -24,7 +24,7 @@ ErrorOr<NonnullRefPtr<CMYKBitmap>> CMYKBitmap::create_with_size(IntSize const& s
ErrorOr<NonnullRefPtr<Bitmap>> CMYKBitmap::to_low_quality_rgb() const
{
if (!m_rgb_bitmap) {
- m_rgb_bitmap = TRY(Bitmap::create(BitmapFormat::BGRx8888, m_size));
+ m_rgb_bitmap = TRY(Bitmap::create_shareable(BitmapFormat::BGRx8888, AlphaType::Premultiplied, m_size));
for (int y = 0; y < m_size.height(); ++y) {
for (int x = 0; x < m_size.width(); ++x) {
diff --git a/Libraries/LibGfx/ImageFormats/AVIFLoader.cpp b/Libraries/LibGfx/ImageFormats/AVIFLoader.cpp
index 3dab695c878..dac93c7ab37 100644
--- a/Libraries/LibGfx/ImageFormats/AVIFLoader.cpp
+++ b/Libraries/LibGfx/ImageFormats/AVIFLoader.cpp
@@ -107,7 +107,7 @@ static ErrorOr<void> decode_avif_image(AVIFLoadingContext& context)
avifRGBImage rgb;
while (avifDecoderNextImage(context.decoder) == AVIF_RESULT_OK) {
auto bitmap_format = context.has_alpha ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888;
- auto bitmap = TRY(Bitmap::create(bitmap_format, AlphaType::Unpremultiplied, context.size.value()));
+ auto bitmap = TRY(Bitmap::create_shareable(bitmap_format, AlphaType::Unpremultiplied, context.size.value()));
avifRGBImageSetDefaults(&rgb, context.decoder->image);
rgb.pixels = bitmap->scanline_u8(0);
diff --git a/Libraries/LibGfx/ImageFormats/BMPLoader.cpp b/Libraries/LibGfx/ImageFormats/BMPLoader.cpp
index 52be8d005ae..7187d57c473 100644
--- a/Libraries/LibGfx/ImageFormats/BMPLoader.cpp
+++ b/Libraries/LibGfx/ImageFormats/BMPLoader.cpp
@@ -1318,7 +1318,7 @@ static ErrorOr<void> decode_bmp_pixel_data(BMPLoadingContext& context)
u32 const absolute_height = static_cast<u32>(abs(static_cast<i64>(context.dib.core.height)));
u32 const height = !context.is_included_in_ico ? absolute_height : (absolute_height / 2);
- context.bitmap = TRY(Bitmap::create(format, Gfx::AlphaType::Unpremultiplied, { static_cast<int>(width), static_cast<int>(height) }));
+ context.bitmap = TRY(Bitmap::create_shareable(format, Gfx::AlphaType::Unpremultiplied, { static_cast<int>(width), static_cast<int>(height) }));
ByteBuffer rle_buffer;
ReadonlyBytes bytes { context.file_bytes + context.data_offset, context.file_size - context.data_offset };
diff --git a/Libraries/LibGfx/ImageFormats/ExifOrientedBitmap.h b/Libraries/LibGfx/ImageFormats/ExifOrientedBitmap.h
index e5a0e4cb97f..5039a7d87d9 100644
--- a/Libraries/LibGfx/ImageFormats/ExifOrientedBitmap.h
+++ b/Libraries/LibGfx/ImageFormats/ExifOrientedBitmap.h
@@ -16,7 +16,7 @@ class ExifOrientedBitmap {
public:
static ErrorOr<ExifOrientedBitmap> create(TIFF::Orientation orientation, IntSize size, BitmapFormat format)
{
- auto bitmap = TRY(Bitmap::create(format, oriented_size(size, orientation)));
+ auto bitmap = TRY(Bitmap::create_shareable(format, AlphaType::Premultiplied, oriented_size(size, orientation)));
return ExifOrientedBitmap(move(bitmap), size, orientation);
}
diff --git a/Libraries/LibGfx/ImageFormats/GIFLoader.cpp b/Libraries/LibGfx/ImageFormats/GIFLoader.cpp
index dad8547a73a..461f80f42e0 100644
--- a/Libraries/LibGfx/ImageFormats/GIFLoader.cpp
+++ b/Libraries/LibGfx/ImageFormats/GIFLoader.cpp
@@ -293,7 +293,7 @@ ErrorOr<ImageFrameDescriptor> GIFImageDecoderPlugin::frame(size_t index, Optiona
while (context.frames_decoded <= index)
TRY(decode_next_frame(context));
- return ImageFrameDescriptor { TRY(context.canvas->clone()), context.frame_durations[index] };
+ return ImageFrameDescriptor { TRY(context.canvas->to_bitmap_backed_by_anonymous_buffer()), context.frame_durations[index] };
}
}
diff --git a/Libraries/LibGfx/ImageFormats/ImageDecoder.h b/Libraries/LibGfx/ImageFormats/ImageDecoder.h
index 5fde3346daa..2a7816d6da5 100644
--- a/Libraries/LibGfx/ImageFormats/ImageDecoder.h
+++ b/Libraries/LibGfx/ImageFormats/ImageDecoder.h
@@ -78,6 +78,8 @@ public:
virtual size_t frame_count() { return 1; }
virtual size_t first_animated_frame_index() { return 0; }
+ // Frames are decoded in the ImageDecoder and handed to the processes that show them, so a plugin decodes each frame
+ // it returns straight into a shareable bitmap. The pixels then never sit in the decoder's own heap.
virtual ErrorOr<ImageFrameDescriptor> frame(size_t index, Optional<IntSize> ideal_size = {}) = 0;
// Returns the duration of a frame in milliseconds without decoding pixel data.
diff --git a/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp b/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp
index 27771878c54..4ed80872ee8 100644
--- a/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp
+++ b/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp
@@ -95,7 +95,7 @@ ErrorOr<void> JPEGLoadingContext::decode()
bool could_read_all_scanlines = true;
if (cinfo.out_color_space == JCS_EXT_BGRX) {
- rgb_bitmap = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, { static_cast<int>(cinfo.output_width), static_cast<int>(cinfo.output_height) }));
+ rgb_bitmap = TRY(Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRx8888, Gfx::AlphaType::Premultiplied, { static_cast<int>(cinfo.output_width), static_cast<int>(cinfo.output_height) }));
while (cinfo.output_scanline < cinfo.output_height) {
auto* row_ptr = (u8*)rgb_bitmap->scanline(cinfo.output_scanline);
auto out_size = jpeg_read_scanlines(&cinfo, &row_ptr, 1);
diff --git a/Libraries/LibGfx/ImageFormats/JPEGXLLoader.cpp b/Libraries/LibGfx/ImageFormats/JPEGXLLoader.cpp
index 526f9b6dc31..b3054313ee1 100644
--- a/Libraries/LibGfx/ImageFormats/JPEGXLLoader.cpp
+++ b/Libraries/LibGfx/ImageFormats/JPEGXLLoader.cpp
@@ -76,7 +76,7 @@ private:
if (self.m_pending_bitmap)
return false;
- auto bitmap_or_error = Bitmap::create(Gfx::BitmapFormat::RGBA8888, self.m_alpha_type, self.m_size);
+ auto bitmap_or_error = Bitmap::create_shareable(Gfx::BitmapFormat::RGBA8888, self.m_alpha_type, self.m_size);
if (bitmap_or_error.is_error())
return false;
diff --git a/Libraries/LibGfx/ImageFormats/PNGLoader.cpp b/Libraries/LibGfx/ImageFormats/PNGLoader.cpp
index 49a8ce19f2d..24809ced8b0 100644
--- a/Libraries/LibGfx/ImageFormats/PNGLoader.cpp
+++ b/Libraries/LibGfx/ImageFormats/PNGLoader.cpp
@@ -81,7 +81,7 @@ ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> PNGImageDecoderPlugin::create(Readonl
// NOTE: If we didn't fail in initialize(), that means we have size information.
// We can create a single-frame bitmap with that size and return it.
// This is weird, but kinda matches the behavior of other browsers.
- auto bitmap = TRY(Bitmap::create(BitmapFormat::BGRA8888, AlphaType::Premultiplied, decoder->m_context->size));
+ auto bitmap = TRY(Bitmap::create_shareable(BitmapFormat::BGRA8888, AlphaType::Premultiplied, decoder->m_context->size));
decoder->m_context->frame_descriptors.append({ move(bitmap), 0 });
decoder->m_context->frame_count = 1;
return decoder;
@@ -334,10 +334,19 @@ static void clear_frame_pixels(Bitmap& output_buffer, IntRect rect)
ErrorOr<size_t> PNGLoadingContext::read_frames(png_structp png_ptr, png_infop info_ptr)
{
- auto decode_frame = [&](IntSize frame_size) -> ErrorOr<NonnullRefPtr<Bitmap>> {
+ // NB: Only a frame that is handed out as it was decoded needs to be shareable. Animation frames are composed into
+ // the output buffer first.
+ enum class DecodedFrameUse : u8 {
+ Output,
+ Composition,
+ };
+ auto decode_frame = [&](IntSize frame_size, DecodedFrameUse use) -> ErrorOr<NonnullRefPtr<Bitmap>> {
// Allocate into the in_flight_bitmap member — so the bitmap survives a potential libpng longjmp out of
// png_read_image() below.
- in_flight_bitmap = TRY(Bitmap::create(BitmapFormat::BGRA8888, AlphaType::Unpremultiplied, frame_size));
+ if (use == DecodedFrameUse::Output)
+ in_flight_bitmap = TRY(Bitmap::create_shareable(BitmapFormat::BGRA8888, AlphaType::Unpremultiplied, frame_size));
+ else
+ in_flight_bitmap = TRY(Bitmap::create(BitmapFormat::BGRA8888, AlphaType::Unpremultiplied, frame_size));
row_pointers.resize_and_keep_capacity(frame_size.height());
for (auto i = 0; i < frame_size.height(); ++i)
@@ -386,7 +395,7 @@ ErrorOr<size_t> PNGLoadingContext::read_frames(png_structp png_ptr, png_infop in
}
auto frame_rect = IntRect { static_cast<int>(x), static_cast<int>(y), static_cast<int>(width), static_cast<int>(height) };
- auto decoded_frame_bitmap = TRY(decode_frame({ width, height }));
+ auto decoded_frame_bitmap = TRY(decode_frame({ width, height }, DecodedFrameUse::Composition));
if (!has_fcTL)
continue;
@@ -409,7 +418,7 @@ ErrorOr<size_t> PNGLoadingContext::read_frames(png_structp png_ptr, png_infop in
}
animation_frame_count++;
- frame_descriptors.append({ TRY(output_buffer->clone()), duration_ms() });
+ frame_descriptors.append({ TRY(output_buffer->to_bitmap_backed_by_anonymous_buffer()), duration_ms() });
switch (dispose_op) {
case PNG_DISPOSE_OP_NONE:
@@ -432,7 +441,7 @@ ErrorOr<size_t> PNGLoadingContext::read_frames(png_structp png_ptr, png_infop in
// If we didn't find any valid animation frames with fcTL chunks, fall back to using the base IDAT data as a single frame.
if (frame_count == 0) {
- auto frame_bitmap = TRY(decode_frame(size));
+ auto frame_bitmap = TRY(decode_frame(size, DecodedFrameUse::Output));
frame_descriptors.append({ move(frame_bitmap), 0 });
frame_count = 1;
}
@@ -441,7 +450,7 @@ ErrorOr<size_t> PNGLoadingContext::read_frames(png_structp png_ptr, png_infop in
frame_count = 1;
loop_count = 0;
- auto decoded_frame_bitmap = TRY(decode_frame(size));
+ auto decoded_frame_bitmap = TRY(decode_frame(size, DecodedFrameUse::Output));
frame_descriptors.append({ move(decoded_frame_bitmap), 0 });
}
diff --git a/Libraries/LibGfx/ImageFormats/WebPLoader.cpp b/Libraries/LibGfx/ImageFormats/WebPLoader.cpp
index d49d13df5a4..78931a208fe 100644
--- a/Libraries/LibGfx/ImageFormats/WebPLoader.cpp
+++ b/Libraries/LibGfx/ImageFormats/WebPLoader.cpp
@@ -170,7 +170,7 @@ static ErrorOr<ImageFrameDescriptor> decode_next_webp_animation_frame(WebPLoadin
return Error::from_string_literal("Failed to decode animated frame");
auto bitmap_format = context.has_alpha ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888;
- auto bitmap = TRY(Bitmap::create(bitmap_format, Gfx::AlphaType::Unpremultiplied, context.size));
+ auto bitmap = TRY(Bitmap::create_shareable(bitmap_format, Gfx::AlphaType::Unpremultiplied, context.size));
memcpy(bitmap->scanline_u8(0), frame_data, context.size.width() * context.size.height() * 4);
auto duration = timestamp - context.anim_old_timestamp;
@@ -186,7 +186,7 @@ static ErrorOr<void> decode_webp_image(WebPLoadingContext& context)
VERIFY(!context.has_animation);
auto bitmap_format = context.has_alpha ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888;
- auto bitmap = TRY(Bitmap::create(bitmap_format, Gfx::AlphaType::Unpremultiplied, context.size));
+ auto bitmap = TRY(Bitmap::create_shareable(bitmap_format, Gfx::AlphaType::Unpremultiplied, context.size));
auto image_data = WebPDecodeBGRAInto(context.data.data(), context.data.size(), bitmap->scanline_u8(0), bitmap->data_size(), bitmap->pitch());
if (image_data == nullptr)
diff --git a/Tests/LibGfx/TestBitmapSequence.cpp b/Tests/LibGfx/TestBitmapSequence.cpp
index c2cb5e29529..b11470d7745 100644
--- a/Tests/LibGfx/TestBitmapSequence.cpp
+++ b/Tests/LibGfx/TestBitmapSequence.cpp
@@ -16,6 +16,27 @@
#include <LibIPC/Message.h>
#include <LibTest/TestCase.h>
+static ErrorOr<Gfx::BitmapSequence> round_trip(IPC::MessageBuffer& message_buffer)
+{
+ auto data = message_buffer.take_data();
+ FixedMemoryStream stream { data.span() };
+
+ Queue<IPC::Attachment> attachments;
+ for (auto& attachment : message_buffer.take_attachments())
+ attachments.enqueue(move(attachment));
+
+ IPC::Decoder decoder { stream, attachments };
+ return IPC::decode<Gfx::BitmapSequence>(decoder);
+}
+
+static ErrorOr<Gfx::BitmapSequence> round_trip(Gfx::BitmapSequence const& sequence)
+{
+ IPC::MessageBuffer message_buffer;
+ IPC::Encoder encoder { message_buffer };
+ TRY(encoder.encode(sequence));
+ return round_trip(message_buffer);
+}
+
TEST_CASE(ipc_decode_rejects_undersized_single_frame_backing)
{
// Issue #10036: A single-frame BitmapSequence whose metadata describes a 50000x10 (2,000,000-byte) bitmap but ships
@@ -27,7 +48,6 @@ TEST_CASE(ipc_decode_rejects_undersized_single_frame_backing)
.format = Gfx::BitmapFormat::BGRA8888,
.alpha_type = Gfx::AlphaType::Premultiplied,
.size = Gfx::IntSize { 50000, 10 },
- .size_in_bytes = backing_size,
};
Vector<Optional<Gfx::BitmapMetadata>> metadata_list;
@@ -38,19 +58,68 @@ TEST_CASE(ipc_decode_rejects_undersized_single_frame_backing)
IPC::MessageBuffer message_buffer;
IPC::Encoder encoder { message_buffer };
MUST(encoder.encode(metadata_list));
- MUST(encoder.encode(static_cast<size_t>(backing_size)));
MUST(encoder.encode(buffer));
- auto data = message_buffer.take_data();
- FixedMemoryStream stream { data.span() };
+ EXPECT(round_trip(message_buffer).is_error());
+}
- Queue<IPC::Attachment> attachments;
- for (auto& attachment : message_buffer.take_attachments())
- attachments.enqueue(move(attachment));
+TEST_CASE(ipc_decode_rejects_oversized_backing)
+{
+ // NB: The receiver keeps a bitmap's buffer mapped for as long as the bitmap lives, so a sender must not be able to make
+ // it keep far more than the bitmap needs.
+ auto buffer = MUST(Core::AnonymousBuffer::create_with_size(64 * MiB));
- IPC::Decoder decoder { stream, attachments };
- auto result = IPC::decode<Gfx::BitmapSequence>(decoder);
- EXPECT(result.is_error());
+ Vector<Optional<Gfx::BitmapMetadata>> metadata_list;
+ metadata_list.append(Gfx::BitmapMetadata {
+ .format = Gfx::BitmapFormat::BGRA8888,
+ .alpha_type = Gfx::AlphaType::Premultiplied,
+ .size = Gfx::IntSize { 1, 1 },
+ });
+
+ IPC::MessageBuffer message_buffer;
+ IPC::Encoder encoder { message_buffer };
+ MUST(encoder.encode(metadata_list));
+ MUST(encoder.encode(buffer));
+
+ EXPECT(round_trip(message_buffer).is_error());
+}
+
+TEST_CASE(ipc_round_trip_maps_the_pixels_of_shareable_bitmaps)
+{
+ auto first = MUST(Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, { 4, 4 }));
+ auto second = MUST(Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRx8888, Gfx::AlphaType::Premultiplied, { 3, 2 }));
+
+ Gfx::BitmapSequence sequence;
+ sequence.bitmaps.append(first);
+ sequence.bitmaps.append(nullptr);
+ sequence.bitmaps.append(second);
+ auto received = MUST(round_trip(sequence));
+
+ EXPECT_EQ(received.bitmaps.size(), 3u);
+ EXPECT(!received.bitmaps[1]);
+ EXPECT_EQ(received.bitmaps[0]->size(), first->size());
+ EXPECT_EQ(received.bitmaps[2]->size(), second->size());
+ EXPECT_EQ(received.bitmaps[2]->format(), Gfx::BitmapFormat::BGRx8888);
+
+ // NB: Writing through the sender's bitmaps after the fact shows that nothing was copied.
+ first->set_pixel(1, 2, Gfx::Color::Red);
+ second->set_pixel(2, 1, Gfx::Color::Blue);
+ EXPECT_EQ(received.bitmaps[0]->get_pixel(1, 2), Gfx::Color::Red);
+ EXPECT_EQ(received.bitmaps[2]->get_pixel(2, 1), Gfx::Color::Blue);
+}
+
+TEST_CASE(ipc_round_trip_copies_private_bitmaps)
+{
+ auto bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, { 4, 4 }));
+ bitmap->set_pixel(3, 0, Gfx::Color::Green);
+
+ Gfx::BitmapSequence sequence;
+ sequence.bitmaps.append(bitmap);
+ auto received = MUST(round_trip(sequence));
+
+ EXPECT_EQ(received.bitmaps.size(), 1u);
+ EXPECT(received.bitmaps[0]->anonymous_buffer().is_valid());
+ EXPECT_EQ(received.bitmaps[0]->get_pixel(3, 0), Gfx::Color::Green);
}
TEST_CASE(create_with_anonymous_buffer_rejects_undersized_buffer)
diff --git a/Tests/LibGfx/TestImageDecoder.cpp b/Tests/LibGfx/TestImageDecoder.cpp
index a5d68bf9d46..a9914ab0434 100644
--- a/Tests/LibGfx/TestImageDecoder.cpp
+++ b/Tests/LibGfx/TestImageDecoder.cpp
@@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Array.h>
#include <AK/ByteString.h>
#include <LibCore/MappedFile.h>
#include <LibGfx/ImageFormats/AVIFLoader.h>
@@ -85,6 +86,38 @@ static ErrorOr<ByteBuffer> make_webp_with_declared_but_missing_iccp_chunk(Readon
return extended_webp;
}
+TEST_CASE(test_frames_are_decoded_into_shareable_bitmaps)
+{
+ Array paths {
+ TEST_INPUT("avif/simple-lossy.avif"sv),
+ TEST_INPUT("bmp/bitmap.bmp"sv),
+ TEST_INPUT("gif/download-animation.gif"sv),
+ TEST_INPUT("ico/serenity.ico"sv),
+ TEST_INPUT("jpg/rgb24.jpg"sv),
+ TEST_INPUT("jpg/cmyk-no-adobe-marker.jpg"sv),
+ TEST_INPUT("jpg/ycck-1111.jpg"sv),
+ TEST_INPUT("jxl/modular_simple_tree_upsample2_10bits_rct.jxl"sv),
+ TEST_INPUT("png/buggie.png"sv),
+ TEST_INPUT("png/apng-blend.png"sv),
+ TEST_INPUT("png/corrupt-idat.png"sv),
+ TEST_INPUT("png/exif-orientation-5.png"sv),
+ TEST_INPUT("webp/simple-vp8.webp"sv),
+ TEST_INPUT("webp/extended-lossless-animated.webp"sv),
+ };
+ for (auto path : paths) {
+ auto file = TRY_OR_FAIL(Core::MappedFile::map(path));
+ auto decoder = TRY_OR_FAIL(Gfx::ImageDecoder::try_create_for_raw_bytes(file->bytes()));
+ EXPECT(decoder);
+ if (!decoder)
+ continue;
+ for (size_t index = 0; index < decoder->frame_count(); ++index) {
+ auto frame = TRY_OR_FAIL(decoder->frame(index));
+ if (!frame.image->anonymous_buffer().is_valid())
+ FAIL(ByteString::formatted("Frame {} of {} is not shareable", index, path));
+ }
+ }
+}
+
TEST_CASE(test_bmp)
{
auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("bmp/rgba32-1.bmp"sv)));