Skip to content

Truly independent web browser

LibCore: Let ImmutableBytes view a slice of its storage

Luke Wilde8bb2dd216859
diff --git a/Libraries/LibCore/ImmutableBytes.cpp b/Libraries/LibCore/ImmutableBytes.cpp
index b684370a7d0..3e102c34079 100644
--- a/Libraries/LibCore/ImmutableBytes.cpp
+++ b/Libraries/LibCore/ImmutableBytes.cpp
@@ -65,7 +65,7 @@ ReadonlyBytes ImmutableBytes::bytes() const
{
if (!m_impl)
return {};
- return m_impl->bytes();
+ return m_impl->bytes().slice(m_offset, m_length);
}
ErrorOr<ByteBuffer> ImmutableBytes::copy_to_byte_buffer() const
@@ -73,8 +73,18 @@ ErrorOr<ByteBuffer> ImmutableBytes::copy_to_byte_buffer() const
return ByteBuffer::copy(bytes());
}
+ImmutableBytes ImmutableBytes::slice(size_t offset, size_t length) const
+{
+ VERIFY(offset <= m_length && length <= m_length - offset);
+ auto slice = *this;
+ slice.m_offset += offset;
+ slice.m_length = length;
+ return slice;
+}
+
ImmutableBytes::ImmutableBytes(NonnullRefPtr<Impl> impl)
: m_impl(move(impl))
+ , m_length(m_impl->bytes().size())
{
}
diff --git a/Libraries/LibCore/ImmutableBytes.h b/Libraries/LibCore/ImmutableBytes.h
index 62a9b6cb6f5..8735ea4841a 100644
--- a/Libraries/LibCore/ImmutableBytes.h
+++ b/Libraries/LibCore/ImmutableBytes.h
@@ -37,10 +37,13 @@ public:
[[nodiscard]] bool is_file_backed() const;
[[nodiscard]] bool is_readonly_mapped() const;
- [[nodiscard]] size_t size() const { return bytes().size(); }
+ [[nodiscard]] size_t size() const { return m_length; }
[[nodiscard]] ReadonlyBytes bytes() const LIFETIME_BOUND;
[[nodiscard]] ErrorOr<ByteBuffer> copy_to_byte_buffer() const;
+ // A view of part of the same storage, which it keeps alive.
+ [[nodiscard]] ImmutableBytes slice(size_t offset, size_t length) const;
+
private:
class Impl final : public AtomicRefCounted<Impl> {
public:
@@ -80,6 +83,8 @@ private:
explicit ImmutableBytes(NonnullRefPtr<Impl>);
RefPtr<Impl> m_impl;
+ size_t m_offset { 0 };
+ size_t m_length { 0 };
};
}
diff --git a/Tests/LibCore/CMakeLists.txt b/Tests/LibCore/CMakeLists.txt
index f076cb77c61..6fbd5462eb7 100644
--- a/Tests/LibCore/CMakeLists.txt
+++ b/Tests/LibCore/CMakeLists.txt
@@ -31,6 +31,7 @@ foreach(source IN LISTS TEST_SOURCES)
endforeach()
target_link_libraries(TestLibCoreDirectory PRIVATE LibFileSystem)
+target_link_libraries(TestLibCoreImmutableBytes PRIVATE LibFileSystem)
target_link_libraries(TestLibCorePromise PRIVATE LibThreading)
target_link_libraries(TestLibCoreStream PRIVATE LibFileSystem LibThreading)
diff --git a/Tests/LibCore/TestLibCoreImmutableBytes.cpp b/Tests/LibCore/TestLibCoreImmutableBytes.cpp
index 27cf6e1a71a..0250f3aa235 100644
--- a/Tests/LibCore/TestLibCoreImmutableBytes.cpp
+++ b/Tests/LibCore/TestLibCoreImmutableBytes.cpp
@@ -4,8 +4,13 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/ByteString.h>
+#include <AK/Random.h>
#include <LibCore/AnonymousBuffer.h>
+#include <LibCore/File.h>
#include <LibCore/ImmutableBytes.h>
+#include <LibCore/StandardPaths.h>
+#include <LibFileSystem/FileSystem.h>
#include <LibTest/TestCase.h>
#include <string.h>
@@ -46,3 +51,62 @@ TEST_CASE(adopt_invalid_anonymous_buffer_is_empty)
EXPECT(bytes.is_empty());
EXPECT(bytes.bytes().is_empty());
}
+
+TEST_CASE(slice_views_the_same_storage)
+{
+ auto bytes = MUST(Core::ImmutableBytes::copy("0123456789"sv.bytes()));
+
+ auto slice = bytes.slice(2, 5);
+ EXPECT_EQ(slice.bytes(), "23456"sv.bytes());
+ EXPECT_EQ(slice.bytes().data(), bytes.bytes().data() + 2);
+
+ auto inner = slice.slice(1, 3);
+ EXPECT_EQ(inner.bytes(), "345"sv.bytes());
+ EXPECT_EQ(inner.bytes().data(), bytes.bytes().data() + 3);
+
+ EXPECT_EQ(bytes.slice(0, bytes.size()).bytes().data(), bytes.bytes().data());
+ EXPECT(bytes.slice(4, 0).is_empty());
+}
+
+TEST_CASE(a_slice_past_the_end_of_its_view_dies)
+{
+ auto bytes = MUST(Core::ImmutableBytes::copy("0123456789"sv.bytes()));
+ auto slice = bytes.slice(2, 5);
+
+ EXPECT_DEATH("a slice past the end of its view", (void)slice.slice(3, 3));
+ EXPECT_DEATH("a slice starting past the end", (void)bytes.slice(11, 0));
+}
+
+TEST_CASE(slice_keeps_the_storage_alive)
+{
+ Core::ImmutableBytes slice;
+ {
+ auto bytes = MUST(Core::ImmutableBytes::copy("outlives the handle it was cut from"sv.bytes()));
+ slice = bytes.slice(9, 10);
+ }
+ EXPECT_EQ(slice.bytes(), "the handle"sv.bytes());
+}
+
+TEST_CASE(slice_of_a_mapped_file_is_file_backed)
+{
+ auto const payload = "mapped from a file"sv;
+ auto const path = ByteString::formatted("{}/TestLibCoreImmutableBytes-{:016x}", Core::StandardPaths::tempfile_directory(), get_random<u64>());
+ {
+ auto file = MUST(Core::File::open(path, Core::File::OpenMode::Write | Core::File::OpenMode::MustBeNew));
+ MUST(file->write_until_depleted(payload.bytes()));
+ }
+
+ {
+ auto fd = MUST(Core::File::open(path, Core::File::OpenMode::Read))->leak_fd();
+ auto bytes = MUST(Core::ImmutableBytes::map_from_fd_range_and_close(fd, path, 0, payload.length()));
+ EXPECT(bytes.is_file_backed());
+
+ auto slice = bytes.slice(7, 6);
+ EXPECT(slice.is_file_backed());
+ EXPECT(!slice.is_readonly_mapped());
+ EXPECT_EQ(slice.bytes(), "from a"sv.bytes());
+ }
+
+ // A file that is still mapped cannot be removed on Windows.
+ MUST(FileSystem::remove(path, FileSystem::RecursionMode::Disallowed));
+}