Truly independent web browser
LibMedia: Wait for every byte of a stream read before allocating it
Zaggy1024b50ee8022f1a
diff --git a/Libraries/LibMedia/IncrementallyPopulatedStream.cpp b/Libraries/LibMedia/IncrementallyPopulatedStream.cppindex 63a66c93b4e..f572b3e9e61 100644--- a/Libraries/LibMedia/IncrementallyPopulatedStream.cpp+++ b/Libraries/LibMedia/IncrementallyPopulatedStream.cpp@@ -18,8 +18,6 @@ static constexpr u64 PRECEDING_DATA_SIZE = 1 * KiB;static constexpr u64 FORWARD_REQUEST_THRESHOLD = 1 * MiB;static constexpr AK::Duration CURSOR_ACTIVE_TIME = AK::Duration::from_milliseconds(50);-static constexpr size_t UNKNOWN_SIZE_READ_CHUNK_SIZE = 64 * KiB;-NonnullRefPtr<IncrementallyPopulatedStream> IncrementallyPopulatedStream::create_empty(){return adopt_ref(*new IncrementallyPopulatedStream());@@ -419,24 +417,41 @@ DecoderErrorOr<size_t> IncrementallyPopulatedStream::read_at(Cursor& cursor, sizreturn read_from_chunks_while_locked(position, bytes);}-DecoderErrorOr<void> IncrementallyPopulatedStream::discard_at(Cursor& cursor, size_t position, size_t length)+DecoderErrorOr<void> IncrementallyPopulatedStream::wait_for_every_byte_while_locked(Cursor& cursor, size_t position, size_t length){- MutexLocker locker { m_mutex };auto extends_past_the_expected_size = [&] {return m_expected_size.has_value() && (position >= m_expected_size.value() || length > m_expected_size.value() - position);};if (extends_past_the_expected_size())- return DecoderError::with_description(DecoderErrorCategory::EndOfStream, "Discarded bytes extend past the end of the stream"sv);+ return DecoderError::with_description(DecoderErrorCategory::EndOfStream, "The bytes extend past the end of the stream"sv);TRY(wait_for_data_while_locked(cursor, position, length));if (extends_past_the_expected_size())- return DecoderError::with_description(DecoderErrorCategory::EndOfStream, "Discarded bytes extend past the end of the stream"sv);+ return DecoderError::with_description(DecoderErrorCategory::EndOfStream, "The bytes extend past the end of the stream"sv);if (!cursor.m_is_blocking && !check_if_data_is_available_or_begin_request_while_locked(cursor, position, length))- return DecoderError::with_description(DecoderErrorCategory::NeedsMoreInput, "The discarded bytes are not all present"sv);+ return DecoderError::with_description(DecoderErrorCategory::NeedsMoreInput, "The bytes are not all present"sv);return {};}+DecoderErrorOr<FixedArray<u8>> IncrementallyPopulatedStream::read_bytes_at(Cursor& cursor, size_t position, size_t length)+{+ MutexLocker locker { m_mutex };+ TRY(wait_for_every_byte_while_locked(cursor, position, length));++ auto buffer = DECODER_TRY_ALLOC(FixedArray<u8>::create(length));+ auto bytes = buffer.span();+ auto read_count = read_from_chunks_while_locked(position, bytes);+ VERIFY(read_count == length);+ return buffer;+}++DecoderErrorOr<void> IncrementallyPopulatedStream::discard_at(Cursor& cursor, size_t position, size_t length)+{+ MutexLocker locker { m_mutex };+ return wait_for_every_byte_while_locked(cursor, position, length);+}+NonnullRefPtr<MediaStreamCursor> IncrementallyPopulatedStream::create_cursor(){return adopt_ref(*new Cursor(NonnullRefPtr { *this }));@@ -518,25 +533,9 @@ DecoderErrorOr<FixedArray<u8>> IncrementallyPopulatedStream::Cursor::read_bytes(if (size == 0)return FixedArray<u8>();- auto expected_size = m_stream->expected_size();- if (expected_size.has_value()) {- if (m_position >= expected_size.value() || size > expected_size.value() - m_position)- return DecoderError::with_description(DecoderErrorCategory::EndOfStream, "Not enough data to read the requested bytes"sv);-- auto buffer = DECODER_TRY_ALLOC(FixedArray<u8>::create(size));- TRY(read_until_filled(buffer.span()));- return buffer;- }-- // If the size is unknown, grow a buffer in chunks before copying it out.- ByteBuffer buffer;- while (buffer.size() < size) {- auto filled_size = buffer.size();- auto chunk_size = min(size - filled_size, UNKNOWN_SIZE_READ_CHUNK_SIZE);- DECODER_TRY_ALLOC(buffer.try_resize(filled_size + chunk_size));- TRY(read_until_filled(buffer.span().slice(filled_size, chunk_size)));- }- return DECODER_TRY_ALLOC(FixedArray<u8>::create(buffer.span()));+ auto buffer = TRY(m_stream->read_bytes_at(*this, m_position, size));+ m_position += size;+ return buffer;}void IncrementallyPopulatedStream::Cursor::abort()diff --git a/Libraries/LibMedia/IncrementallyPopulatedStream.h b/Libraries/LibMedia/IncrementallyPopulatedStream.hindex 9e37fd80ec5..a7c8538fa66 100644--- a/Libraries/LibMedia/IncrementallyPopulatedStream.h+++ b/Libraries/LibMedia/IncrementallyPopulatedStream.h@@ -128,6 +128,8 @@ private:DecoderErrorOr<void> wait_for_data_while_locked(Cursor&, u64 position, u64 length);DecoderErrorOr<size_t> read_at(Cursor&, size_t position, Bytes&);+ DecoderErrorOr<void> wait_for_every_byte_while_locked(Cursor&, size_t position, size_t length);+ DecoderErrorOr<FixedArray<u8>> read_bytes_at(Cursor&, size_t position, size_t length);DecoderErrorOr<void> discard_at(Cursor&, size_t position, size_t length);bool a_cursor_is_blocked_while_locked() const;diff --git a/Tests/LibMedia/TestIncrementallyPopulatedStream.cpp b/Tests/LibMedia/TestIncrementallyPopulatedStream.cppindex c5d8d4ddd26..337670f4e8c 100644--- a/Tests/LibMedia/TestIncrementallyPopulatedStream.cpp+++ b/Tests/LibMedia/TestIncrementallyPopulatedStream.cpp@@ -1285,3 +1285,22 @@ TEST_CASE(blocking_read_and_discard_waits_for_every_byte)EXPECT(discard_succeeded.load());EXPECT_EQ(cursor->position(), 70u);}++TEST_CASE(read_bytes_allocates_only_once_every_byte_has_arrived)+{+ // A declared size that could never be allocated fails on the missing data instead of the allocation.+ static constexpr size_t declared_size = 1ull << 60;+ auto stream = Media::IncrementallyPopulatedStream::create_empty();+ stream->set_expected_size(declared_size + 100);+ auto data = make_test_data(100);+ stream->add_chunk_at(0, data.bytes());+ stream->make_missing_data_permanently_unavailable();++ auto cursor = stream->create_cursor();+ auto result = cursor->read_bytes(declared_size);+ EXPECT(result.is_error());+ EXPECT_EQ(result.error().category(), Media::DecoderErrorCategory::PermanentlyUnavailable);++ auto bytes = MUST(cursor->read_bytes(100));+ EXPECT_EQ(bytes.span(), data.bytes());+}