Skip to content

Truly independent web browser

LibCore: Let ImmutableBytes view an AnonymousBuffer

Luke Wilde0429075ac53a
diff --git a/Libraries/LibCore/ImmutableBytes.cpp b/Libraries/LibCore/ImmutableBytes.cpp
index 8fd6e5a5d28..b684370a7d0 100644
--- a/Libraries/LibCore/ImmutableBytes.cpp
+++ b/Libraries/LibCore/ImmutableBytes.cpp
@@ -41,6 +41,11 @@ ImmutableBytes ImmutableBytes::adopt_mapped_file(NonnullOwnPtr<MappedFile> mappe
return ImmutableBytes { adopt_ref(*new Impl(move(mapped_file))) };
}
+ImmutableBytes ImmutableBytes::adopt_anonymous_buffer(AnonymousBuffer buffer)
+{
+ return ImmutableBytes { adopt_ref(*new Impl(move(buffer))) };
+}
+
ErrorOr<ImmutableBytes> ImmutableBytes::map_from_fd_range_and_close(int fd, StringView path, off_t offset, size_t size)
{
return adopt_mapped_file(TRY(MappedFile::map_from_fd_range_and_close(fd, path, offset, size)));
@@ -88,6 +93,11 @@ ImmutableBytes::Impl::Impl(ReadonlyMapping mapping)
{
}
+ImmutableBytes::Impl::Impl(AnonymousBuffer buffer)
+ : m_storage(move(buffer))
+{
+}
+
ImmutableBytes::Impl::ReadonlyMapping::~ReadonlyMapping()
{
if (data)
@@ -115,6 +125,9 @@ ReadonlyBytes ImmutableBytes::Impl::bytes() const
},
[](ReadonlyMapping const& mapping) -> ReadonlyBytes {
return { mapping.data, mapping.size };
+ },
+ [](AnonymousBuffer const& buffer) -> ReadonlyBytes {
+ return buffer.bytes();
});
}
diff --git a/Libraries/LibCore/ImmutableBytes.h b/Libraries/LibCore/ImmutableBytes.h
index 997b4055a09..62a9b6cb6f5 100644
--- a/Libraries/LibCore/ImmutableBytes.h
+++ b/Libraries/LibCore/ImmutableBytes.h
@@ -13,6 +13,7 @@
#include <AK/RefPtr.h>
#include <AK/StdLibExtras.h>
#include <AK/Variant.h>
+#include <LibCore/AnonymousBuffer.h>
#include <LibCore/Export.h>
#include <LibCore/MappedFile.h>
@@ -26,6 +27,7 @@ public:
static ErrorOr<ImmutableBytes> copy_to_readonly_mapping(ReadonlyBytes);
static ImmutableBytes adopt(ByteBuffer);
static ImmutableBytes adopt_mapped_file(NonnullOwnPtr<MappedFile>);
+ static ImmutableBytes adopt_anonymous_buffer(AnonymousBuffer);
static ErrorOr<ImmutableBytes> map_from_fd_range_and_close(int fd, StringView path, off_t offset, size_t size);
ImmutableBytes() = default;
@@ -65,13 +67,14 @@ private:
explicit Impl(ByteBuffer);
explicit Impl(NonnullOwnPtr<MappedFile>);
explicit Impl(ReadonlyMapping);
+ explicit Impl(AnonymousBuffer);
[[nodiscard]] bool is_file_backed() const;
[[nodiscard]] bool is_readonly_mapped() const;
[[nodiscard]] ReadonlyBytes bytes() const LIFETIME_BOUND;
private:
- Variant<ByteBuffer, NonnullOwnPtr<MappedFile>, ReadonlyMapping> m_storage;
+ Variant<ByteBuffer, NonnullOwnPtr<MappedFile>, ReadonlyMapping, AnonymousBuffer> m_storage;
};
explicit ImmutableBytes(NonnullRefPtr<Impl>);
diff --git a/Tests/LibCore/CMakeLists.txt b/Tests/LibCore/CMakeLists.txt
index e4c438120ec..f076cb77c61 100644
--- a/Tests/LibCore/CMakeLists.txt
+++ b/Tests/LibCore/CMakeLists.txt
@@ -4,6 +4,7 @@ set(TEST_SOURCES
TestLibCoreDeferredInvoke.cpp
TestLibCoreDirectory.cpp
TestLibCoreEventLoop.cpp
+ TestLibCoreImmutableBytes.cpp
TestLibCoreMappedFile.cpp
TestLibCoreMimeType.cpp
TestLibCorePromise.cpp
diff --git a/Tests/LibCore/TestLibCoreImmutableBytes.cpp b/Tests/LibCore/TestLibCoreImmutableBytes.cpp
new file mode 100644
index 00000000000..27cf6e1a71a
--- /dev/null
+++ b/Tests/LibCore/TestLibCoreImmutableBytes.cpp
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibCore/AnonymousBuffer.h>
+#include <LibCore/ImmutableBytes.h>
+#include <LibTest/TestCase.h>
+#include <string.h>
+
+TEST_CASE(adopt_anonymous_buffer_views_its_bytes)
+{
+ auto buffer = MUST(Core::AnonymousBuffer::create_with_size(64));
+ auto const payload = "viewed through ImmutableBytes"sv;
+ memcpy(buffer.data<void>(), payload.characters_without_null_termination(), payload.length());
+
+ auto bytes = Core::ImmutableBytes::adopt_anonymous_buffer(buffer);
+ EXPECT(bytes.is_valid());
+ EXPECT(!bytes.is_file_backed());
+ EXPECT(!bytes.is_readonly_mapped());
+ EXPECT_EQ(bytes.size(), buffer.size());
+ EXPECT_EQ(bytes.bytes().data(), buffer.bytes().data());
+ EXPECT_EQ(bytes.bytes().slice(0, payload.length()), payload.bytes());
+}
+
+TEST_CASE(adopt_anonymous_buffer_keeps_the_mapping_alive)
+{
+ auto const payload = "outlives the handle it came from"sv;
+ Core::ImmutableBytes bytes;
+ {
+ auto buffer = MUST(Core::AnonymousBuffer::create_with_size(64));
+ memcpy(buffer.data<void>(), payload.characters_without_null_termination(), payload.length());
+ bytes = Core::ImmutableBytes::adopt_anonymous_buffer(move(buffer));
+ }
+ EXPECT_EQ(bytes.bytes().slice(0, payload.length()), payload.bytes());
+
+ auto copy = MUST(bytes.copy_to_byte_buffer());
+ EXPECT_EQ(copy.bytes().slice(0, payload.length()), payload.bytes());
+}
+
+TEST_CASE(adopt_invalid_anonymous_buffer_is_empty)
+{
+ auto bytes = Core::ImmutableBytes::adopt_anonymous_buffer(Core::AnonymousBuffer {});
+ EXPECT(bytes.is_valid());
+ EXPECT(bytes.is_empty());
+ EXPECT(bytes.bytes().is_empty());
+}