Skip to content

Truly independent web browser

LibWebCommon: Let agents on several threads match site rules

Andreas Kling3f96a7b430d0
diff --git a/Libraries/LibWebCommon/Loader/LoaderConfigSnapshot.h b/Libraries/LibWebCommon/Loader/LoaderConfigSnapshot.h
index c00e7f9f031..879f645d5f4 100644
--- a/Libraries/LibWebCommon/Loader/LoaderConfigSnapshot.h
+++ b/Libraries/LibWebCommon/Loader/LoaderConfigSnapshot.h
@@ -16,7 +16,7 @@
namespace Web {
-// Site compatibility data, whose URL patterns may only be matched on one thread.
+// Site compatibility data, which the agents of a process share across their threads.
struct WEBCOMMON_API SharedSiteCompatibilityData final : public AtomicRefCounted<SharedSiteCompatibilityData> {
explicit SharedSiteCompatibilityData(SiteCompatibilityData data)
: data(move(data))
diff --git a/Libraries/LibWebCommon/Loader/SiteCompatibility.cpp b/Libraries/LibWebCommon/Loader/SiteCompatibility.cpp
index 781a410cf31..fbaf62af68c 100644
--- a/Libraries/LibWebCommon/Loader/SiteCompatibility.cpp
+++ b/Libraries/LibWebCommon/Loader/SiteCompatibility.cpp
@@ -6,6 +6,8 @@
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
+#include <AK/Mutex.h>
+#include <AK/NeverDestroyed.h>
#include <AK/StringBuilder.h>
#include <LibWebCommon/Loader/SiteCompatibility.h>
@@ -122,6 +124,12 @@ bool SiteCompatibilityRule::exposes_experimental_interface(StringView name) cons
bool SiteCompatibilityRule::matches(URL::URL const& url) const
{
auto serialized_url = url.serialize();
+
+ // NB: Agents on different threads share these rules, and a compiled pattern keeps non-atomic state: each regex has
+ // a reference count and the scratch space of its matcher. A match is short, and a fetch makes few of them.
+ static NeverDestroyed<Mutex> s_match_mutex;
+ MutexLocker locker(*s_match_mutex);
+
for (auto const& pattern : m_compiled_patterns) {
auto result = pattern.match(serialized_url, {});
if (!result.is_error() && result.value().has_value())
diff --git a/Tests/LibWebCommon/CMakeLists.txt b/Tests/LibWebCommon/CMakeLists.txt
index 3637caf4c77..fed9ae48830 100644
--- a/Tests/LibWebCommon/CMakeLists.txt
+++ b/Tests/LibWebCommon/CMakeLists.txt
@@ -19,4 +19,4 @@ set(TEST_SOURCES
)
target_sources(TestLibWebCommon PRIVATE ${TEST_SOURCES})
-target_link_libraries(TestLibWebCommon PRIVATE LibHTTP LibIPC LibURL LibWebCommon)
+target_link_libraries(TestLibWebCommon PRIVATE LibHTTP LibIPC LibThreading LibURL LibWebCommon)
diff --git a/Tests/LibWebCommon/TestSiteCompatibility.cpp b/Tests/LibWebCommon/TestSiteCompatibility.cpp
index db7072740f7..818fa606fba 100644
--- a/Tests/LibWebCommon/TestSiteCompatibility.cpp
+++ b/Tests/LibWebCommon/TestSiteCompatibility.cpp
@@ -4,8 +4,11 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Atomic.h>
#include <AK/JsonValue.h>
+#include <AK/Vector.h>
#include <LibTest/TestCase.h>
+#include <LibThreading/Thread.h>
#include <LibURL/Parser.h>
#include <LibWebCommon/Loader/SiteCompatibility.h>
@@ -47,6 +50,31 @@ TEST_CASE(rule_matches_apex_and_subdomain_patterns)
EXPECT(!rule.matches(url("https://notexample.com/article"sv)));
}
+// The agents of a process share these rules, each matching on its own thread.
+TEST_CASE(rule_matches_on_several_threads_at_once)
+{
+ IGNORE_USE_IN_ESCAPING_LAMBDA auto rule = MUST(Web::SiteCompatibilityRule::create({ "https://*.example.com/*"_string }, {}));
+ IGNORE_USE_IN_ESCAPING_LAMBDA auto matching_url = url("https://www.example.com/page"sv);
+ IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<size_t> wrong_answers { 0 };
+
+ Vector<NonnullRefPtr<Threading::Thread>> threads;
+ for (size_t i = 0; i < 4; ++i) {
+ threads.append(Threading::Thread::construct("Matcher"sv, [&] -> intptr_t {
+ for (size_t round = 0; round < 20'000; ++round) {
+ if (!rule.matches(matching_url))
+ wrong_answers.fetch_add(1);
+ }
+ return 0;
+ }));
+ }
+ for (auto& thread : threads)
+ thread->start();
+ for (auto& thread : threads)
+ (void)thread->join();
+
+ EXPECT_EQ(wrong_answers.load(), 0u);
+}
+
TEST_CASE(rule_transforms_user_agent)
{
auto rule = MUST(Web::SiteCompatibilityRule::create(