[cr147] Updates device memory farbling limits.

The upstream memory farbling has been bumped to 2GB-32GB range via enabling
the blink::features::kUpdatedDeviceMemoryLimitsFor2026 flag.

Also, updates BraveDeviceMemoryFarblingBrowserTest.FarbleDeviceMemory
test correspondingly.

Chromium change:
https://source.chromium.org/chromium/chromium/src/+/7405c88bf77a7484c24ef82ed0404493239c24b7

commit 7405c88bf77a7484c24ef82ed0404493239c24b7
Author: Barry Pollard <barrypollard@google.com>
Date:   Mon Feb 23 06:17:54 2026 -0800

    Re-enable new device memory limits.

    This was enabled briefly but disabled because of a logon failure for X
    (see crbug.com/482915825) picked up during testing. However after this
    was fixed and reviewing with API owners we are now ready to re-enable
    this again.

    I2S: https://groups.google.com/a/chromium.org/g/blink-dev/c/vXtAmrVZeDk
    Bug: 454354290
This commit is contained in:
Max Karolinskiy
2026-03-26 19:25:23 -04:00
parent 502133225b
commit dff1c7cc0c
2 changed files with 31 additions and 13 deletions
@@ -52,6 +52,16 @@ class BraveDeviceMemoryFarblingBrowserTest : public InProcessBrowserTest {
~BraveDeviceMemoryFarblingBrowserTest() override = default;
void SetUpCommandLine(base::CommandLine* command_line) override {
InProcessBrowserTest::SetUpCommandLine(command_line);
// Use single process so that we can call
// blink::ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting to set
// the memory size for the renderers. Otherwise each renderer will
// initialize the approximate device memory based on the physical RAM size
// on the machine and we can't check for the expected results in the test.
command_line->AppendSwitch("single-process");
}
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
base::FilePath test_data_dir;
@@ -103,11 +113,13 @@ IN_PROC_BROWSER_TEST_F(BraveDeviceMemoryFarblingBrowserTest,
std::string domain2 = "d.test";
GURL url1 = https_server_.GetURL(domain1, "/simple.html");
GURL url2 = https_server_.GetURL(domain2, "/simple.html");
// set memory to 10GB
blink::ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(1024 * 10);
// Set memory to 64GB (greater than the ApproximatedDeviceMemory's kMaxMemory
// of 32GB).
blink::ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(1024 * 64);
int true_value =
blink::ApproximatedDeviceMemory::GetApproximatedDeviceMemory() * 1024;
EXPECT_EQ(true_value, 8192);
EXPECT_EQ(true_value, 32768);
// Farbling level: off
AllowFingerprinting(domain1);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url1));
@@ -119,18 +131,18 @@ IN_PROC_BROWSER_TEST_F(BraveDeviceMemoryFarblingBrowserTest,
// Farbling level: default
SetFingerprintingDefault(domain1);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url1));
EXPECT_EQ(4096, EvalJs(contents(), kDeviceMemoryScript));
EXPECT_EQ(8192, EvalJs(contents(), kDeviceMemoryScript));
SetFingerprintingDefault(domain2);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url2));
EXPECT_EQ(512, EvalJs(contents(), kDeviceMemoryScript));
EXPECT_EQ(32768, EvalJs(contents(), kDeviceMemoryScript));
// Farbling level: maximum
BlockFingerprinting(domain1);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url1));
EXPECT_EQ(512, EvalJs(contents(), kDeviceMemoryScript));
EXPECT_EQ(16384, EvalJs(contents(), kDeviceMemoryScript));
AllowFingerprinting(domain2);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url2));
EXPECT_EQ(8192, EvalJs(contents(), kDeviceMemoryScript));
EXPECT_EQ(32768, EvalJs(contents(), kDeviceMemoryScript));
// Farbling level: default, but webcompat exception enabled
SetFingerprintingDefault(domain1);
@@ -21,10 +21,14 @@ float FarbleDeviceMemory(blink::ExecutionContext* context) {
BraveFarblingLevel::OFF);
// If Brave Shields are down or anti-fingerprinting is off for this site,
// return the true value.
if (farbling_level == BraveFarblingLevel::OFF)
if (farbling_level == BraveFarblingLevel::OFF) {
return true_value;
}
// See kMinMemory and kMaxMemory values in
// ApproximatedDeviceMemory::CalculateAndSetApproximatedDeviceMemory
std::vector<float> valid_values = {2.0, 4.0, 8.0, 16.0, 32.0};
std::vector<float> valid_values = {0.25, 0.5, 1.0, 2.0, 4.0, 8.0};
size_t min_farbled_index;
size_t max_farbled_index;
if (farbling_level == BraveFarblingLevel::MAXIMUM) {
@@ -34,21 +38,23 @@ float FarbleDeviceMemory(blink::ExecutionContext* context) {
max_farbled_index = valid_values.size() - 1;
} else {
// If anti-fingerprinting is at default level, select a pseudo-random valid
// value between 0.5 and the true value (unless the true value is 0.25 in
// value between 4.0 and the true value (unless the true value is 2.0 in
// which case just return that).
auto true_it = std::ranges::find(valid_values, true_value);
size_t true_index;
// Get index into |valid_values| of the true value. If it's not found,
// assume the last index. (This should not happen, but it allows us to
// fail closed instead of failing open.)
if (true_it != valid_values.end())
if (true_it != valid_values.end()) {
true_index = std::distance(valid_values.begin(), true_it);
else
} else {
true_index = valid_values.size() - 1;
}
min_farbled_index = 1;
max_farbled_index = true_index;
if (max_farbled_index <= min_farbled_index)
if (max_farbled_index <= min_farbled_index) {
return valid_values[min_farbled_index];
}
}
FarblingPRNG prng =
BraveSessionCache::From(*context).MakePseudoRandomGenerator();