Skip to content

Commit

Permalink
Use atomics to access g_partition_by_default_locked
Browse files Browse the repository at this point in the history
This value is a debugging aid, to ensure that `g_partition_by_default`
is not read before it is written. Since it is potentially written and
read from different threads, it must be handled atomically, but because
it is a debugging aid relaxed memory semantics are sufficient.

Bug: 1495572
Change-Id: Ic1947871305115642493057eb218f6396ae4a7c8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5006043
Reviewed-by: Brianna Goldstein <brgoldstein@google.com>
Commit-Queue: Dustin Mitchell <djmitche@chromium.org>
Reviewed-by: Matthew Denton <mpdenton@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1220928}
  • Loading branch information
djmitche authored and Chromium LUCI CQ committed Nov 7, 2023
1 parent cb10119 commit f75cd68
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions net/base/network_anonymization_key.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "net/base/network_anonymization_key.h"

#include <atomic>
#include <optional>

#include "base/feature_list.h"
Expand All @@ -24,7 +25,7 @@ bool g_partition_by_default = false;

// True if NAK::IsPartitioningEnabled has been called, and the value of
// `g_partition_by_default` cannot be changed.
bool g_partition_by_default_locked = false;
ABSL_CONST_INIT std::atomic<bool> g_partition_by_default_locked = false;

} // namespace

Expand Down Expand Up @@ -188,7 +189,7 @@ std::optional<std::string> NetworkAnonymizationKey::SerializeSiteWithNonce(

// static
bool NetworkAnonymizationKey::IsPartitioningEnabled() {
g_partition_by_default_locked = true;
g_partition_by_default_locked.store(true, std::memory_order_relaxed);
return g_partition_by_default ||
base::FeatureList::IsEnabled(
features::kSplitHostCacheByNetworkIsolationKey) ||
Expand All @@ -204,7 +205,7 @@ bool NetworkAnonymizationKey::IsPartitioningEnabled() {

// static
void NetworkAnonymizationKey::PartitionByDefault() {
DCHECK(!g_partition_by_default_locked);
DCHECK(!g_partition_by_default_locked.load(std::memory_order_relaxed));
// Only set the global if none of the relevant features are overridden.
if (!base::FeatureList::GetInstance()->IsFeatureOverridden(
"SplitHostCacheByNetworkIsolationKey") &&
Expand All @@ -223,7 +224,7 @@ void NetworkAnonymizationKey::PartitionByDefault() {
// static
void NetworkAnonymizationKey::ClearGlobalsForTesting() {
g_partition_by_default = false;
g_partition_by_default_locked = false;
g_partition_by_default_locked.store(false);
}

} // namespace net

0 comments on commit f75cd68

Please sign in to comment.