Truly independent web browser
LibWeb: Check scheduler durations before converting to counters
Andreas Kling77b168306027
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cppindex 8ded8d27e01..7bb99f92a58 100644--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp@@ -28,6 +28,7 @@#include <LibWeb/HTML/EventLoop/ClockPlan.h>#include <LibWeb/HTML/EventLoop/EventLoop.h>#include <LibWeb/HTML/EventLoop/FrameCompletion.h>+#include <LibWeb/HTML/EventLoop/RenderingSchedulerCounters.h>#include <LibWeb/HTML/FormAssociatedElement.h>#include <LibWeb/HTML/HTMLMediaElement.h>#include <LibWeb/HTML/ListOfAvailableImages.h>@@ -233,7 +234,7 @@ void EventLoop::process()if (oldest_task && oldest_task->source() != Task::Source::Rendering) {auto task_duration = task_end_time - task_start_time;- auto task_duration_microseconds = static_cast<u64>(task_duration * 1000.0);+ auto task_duration_microseconds = rendering_scheduler_duration_microseconds(task_duration);++m_rendering_scheduler_counters.tasks_between_updates;m_rendering_scheduler_counters.task_microseconds_between_updates += task_duration_microseconds;switch (oldest_task->source()) {@@ -590,7 +591,7 @@ struct EventLoop::RenderingUpdateInFlight {void EventLoop::finish_rendering_update(double update_start_time){auto update_end_time = HighResolutionTime::unsafe_shared_current_time();- m_rendering_scheduler_counters.update_microseconds += static_cast<u64>((update_end_time - update_start_time) * 1000.0);+ m_rendering_scheduler_counters.update_microseconds += rendering_scheduler_duration_microseconds(update_end_time - update_start_time);m_running_rendering_task = false;for (auto const& page : pages_of_local_roots())diff --git a/Libraries/LibWeb/HTML/EventLoop/RenderingSchedulerCounters.h b/Libraries/LibWeb/HTML/EventLoop/RenderingSchedulerCounters.hnew file mode 100644index 00000000000..a1d24993a96--- /dev/null+++ b/Libraries/LibWeb/HTML/EventLoop/RenderingSchedulerCounters.h@@ -0,0 +1,23 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Types.h>++namespace Web::HTML {++inline u64 rendering_scheduler_duration_microseconds(double duration_ms)+{+ auto microseconds = duration_ms * 1000.0;+ // NB: The exclusive upper bound is exactly representable, unlike u64's maximum value.+ // Ordered comparisons also reject NaN and infinities before the integer conversion.+ if (!(microseconds > 0 && microseconds < 0x1p64))+ return 0;+ return static_cast<u64>(microseconds);+}++}diff --git a/Tests/LibWeb/TestEventLoop.cpp b/Tests/LibWeb/TestEventLoop.cppindex ee9dbd899d6..894c37a6c08 100644--- a/Tests/LibWeb/TestEventLoop.cpp+++ b/Tests/LibWeb/TestEventLoop.cpp@@ -4,11 +4,14 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/Math.h>+#include <AK/NumericLimits.h>#include <LibTest/TestCase.h>#include <LibWeb/Bindings/MainThreadVM.h>#include <LibWeb/Bindings/PrincipalHostDefined.h>#include <LibWeb/DOM/Document.h>#include <LibWeb/HTML/EventLoop/EventLoop.h>+#include <LibWeb/HTML/EventLoop/RenderingSchedulerCounters.h>#include <LibWeb/HTML/Scripting/Environments.h>#include <LibWeb/HTML/Window.h>#include <LibWeb/Platform/FontPlugin.h>@@ -41,3 +44,27 @@ TEST_CASE(nested_pause_handles_keep_the_event_loop_paused)EXPECT(!event_loop.execution_paused());}++TEST_CASE(rendering_scheduler_duration_preserves_truncation)+{+ for (auto duration : { 0.0, 0.0005, 0.0015, 1.2349, 2.5, 1000.1234 })+ EXPECT_EQ(Web::HTML::rendering_scheduler_duration_microseconds(duration), static_cast<u64>(duration * 1000.0));+ EXPECT_EQ(Web::HTML::rendering_scheduler_duration_microseconds(0.0005), 0u);+ EXPECT_EQ(Web::HTML::rendering_scheduler_duration_microseconds(0.0015), 1u);+ EXPECT_EQ(Web::HTML::rendering_scheduler_duration_microseconds(1.2349), 1234u);+}++TEST_CASE(rendering_scheduler_duration_rejects_invalid_intervals)+{+ for (auto duration : { -0.0, -0.0005, -1.0, AK::NaN<double>, AK::Infinity<double>, -AK::Infinity<double>, NumericLimits<double>::max() })+ EXPECT_EQ(Web::HTML::rendering_scheduler_duration_microseconds(duration), 0u);+}++TEST_CASE(rendering_scheduler_duration_checks_the_unsigned_boundary)+{+ constexpr double boundary_ms = 0x1p64 / 1000.0;+ EXPECT_EQ(Web::HTML::rendering_scheduler_duration_microseconds(boundary_ms), 0u);+ EXPECT_EQ(Web::HTML::rendering_scheduler_duration_microseconds(nextafter(boundary_ms, AK::Infinity<double>)), 0u);+ EXPECT_EQ(Web::HTML::rendering_scheduler_duration_microseconds(nextafter(boundary_ms, 0.0)), 0xfffffffffffff000ULL);+ EXPECT_EQ(Web::HTML::rendering_scheduler_duration_microseconds(0x1p63 / 1000.0), 0x8000000000000000ULL);+}