Skip to content

Commit 4aaf53a

Browse files
hadi88copybara-github
authored andcommitted
Remove synchronization from the protobuf domain mark the domain objects as thread-unsafe.
PiperOrigin-RevId: 792730547
1 parent ce6174e commit 4aaf53a

File tree

4 files changed

+11
-31
lines changed

4 files changed

+11
-31
lines changed

fuzztest/internal/domains/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ cc_library(
156156
hdrs = ["protobuf_domain_impl.h"],
157157
deps = [
158158
":core_domains_impl",
159-
"@abseil-cpp//absl/base",
160159
"@abseil-cpp//absl/base:core_headers",
161160
"@abseil-cpp//absl/base:no_destructor",
162161
"@abseil-cpp//absl/container:flat_hash_map",

fuzztest/internal/domains/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ fuzztest_cc_library(
171171
"protobuf_domain_impl.h"
172172
DEPS
173173
fuzztest::core_domains_impl
174-
absl::base
175174
absl::core_headers
176175
absl::no_destructor
177176
absl::flat_hash_map

fuzztest/internal/domains/domain.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ struct GenericPrinter {
6565
// `Domain<T>` is the type-erased domain interface.
6666
//
6767
// It can be constructed from any object derived from `DomainBase` that
68-
// implements the domain methods for the value type `T`.
68+
// implements the domain methods for the value type `T`. A Domain object is not
69+
// thread-safe. It's the domain object owner's responsibility to make sure the
70+
// domain object is not accessed concurrently by multiple threads.
6971
template <typename T>
7072
class Domain {
7173
public:

fuzztest/internal/domains/protobuf_domain_impl.h

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <vector>
2828

2929
#include "absl/base/attributes.h"
30-
#include "absl/base/call_once.h"
3130
#include "absl/base/const_init.h"
3231
#include "absl/base/no_destructor.h"
3332
#include "absl/base/thread_annotations.h"
@@ -408,12 +407,10 @@ class ProtoPolicy {
408407
class RecursiveFieldsCaches {
409408
public:
410409
void SetIsFieldFinitelyRecursive(const FieldDescriptor* field, bool value) {
411-
absl::MutexLock l(&field_to_is_finitely_recursive_mutex_);
412410
field_to_is_finitely_recursive_.insert({field, value});
413411
}
414412

415413
std::optional<bool> IsFieldFinitelyRecursive(const FieldDescriptor* field) {
416-
absl::ReaderMutexLock l(&field_to_is_finitely_recursive_mutex_);
417414
auto it = field_to_is_finitely_recursive_.find(field);
418415
return it != field_to_is_finitely_recursive_.end()
419416
? std::optional(it->second)
@@ -422,13 +419,11 @@ class ProtoPolicy {
422419

423420
void SetIsFieldInfinitelyRecursive(const FieldDescriptor* field,
424421
bool value) {
425-
absl::MutexLock l(&field_to_is_infinitely_recursive_mutex_);
426422
field_to_is_infinitely_recursive_.insert({field, value});
427423
}
428424

429425
std::optional<bool> IsFieldInfinitelyRecursive(
430426
const FieldDescriptor* field) {
431-
absl::ReaderMutexLock l(&field_to_is_infinitely_recursive_mutex_);
432427
auto it = field_to_is_infinitely_recursive_.find(field);
433428
return it != field_to_is_infinitely_recursive_.end()
434429
? std::optional(it->second)
@@ -437,34 +432,27 @@ class ProtoPolicy {
437432

438433
const std::vector<const FieldDescriptor*>* GetFields(
439434
const ProtoDescriptor* descriptor) {
440-
absl::ReaderMutexLock l(&proto_to_fields_mutex_);
441435
auto it = proto_to_fields_.find(descriptor);
442436
return it != proto_to_fields_.end() ? it->second.get() : nullptr;
443437
}
444438

445439
const std::vector<const FieldDescriptor*>& SetFields(
446440
const ProtoDescriptor* descriptor,
447441
std::vector<const FieldDescriptor*> fields) {
448-
absl::MutexLock l(&proto_to_fields_mutex_);
449442
auto [it, _] = proto_to_fields_.insert(
450443
{descriptor, std::make_unique<std::vector<const FieldDescriptor*>>(
451444
std::move(fields))});
452445
return *it->second;
453446
}
454447

455448
private:
456-
absl::Mutex field_to_is_finitely_recursive_mutex_;
457449
absl::flat_hash_map<const FieldDescriptor*, bool>
458-
field_to_is_finitely_recursive_
459-
ABSL_GUARDED_BY(field_to_is_finitely_recursive_mutex_);
460-
absl::Mutex field_to_is_infinitely_recursive_mutex_;
450+
field_to_is_finitely_recursive_;
461451
absl::flat_hash_map<const FieldDescriptor*, bool>
462-
field_to_is_infinitely_recursive_
463-
ABSL_GUARDED_BY(field_to_is_infinitely_recursive_mutex_);
464-
absl::Mutex proto_to_fields_mutex_;
452+
field_to_is_infinitely_recursive_;
465453
absl::flat_hash_map<const ProtoDescriptor*,
466454
std::unique_ptr<std::vector<const FieldDescriptor*>>>
467-
proto_to_fields_ ABSL_GUARDED_BY(proto_to_fields_mutex_);
455+
proto_to_fields_;
468456
};
469457

470458
std::shared_ptr<RecursiveFieldsCaches> caches_ = nullptr;
@@ -603,7 +591,6 @@ class ProtobufDomainUntypedImpl
603591
ProtobufDomainUntypedImpl(const ProtobufDomainUntypedImpl& other)
604592
: prototype_(other.prototype_),
605593
use_lazy_initialization_(other.use_lazy_initialization_) {
606-
absl::MutexLock l(&other.mutex_);
607594
domains_ = other.domains_;
608595
policy_ = other.policy_;
609596
customized_fields_ = other.customized_fields_;
@@ -1433,7 +1420,6 @@ class ProtobufDomainUntypedImpl
14331420
<< "` but the field needs a message of type `"
14341421
<< field->message_type()->full_name() << "`.";
14351422
}
1436-
absl::MutexLock l(&self.mutex_);
14371423
auto res = self.domains_.try_emplace(field->number(),
14381424
std::in_place_type<DomainT>,
14391425
std::forward<Inner>(domain));
@@ -1493,11 +1479,11 @@ class ProtobufDomainUntypedImpl
14931479
auto GetFieldCount() const { return GetProtobufFields().size(); }
14941480

14951481
const std::vector<const FieldDescriptor*>& GetProtobufFields() const {
1496-
absl::call_once(fields_cache_once_, [this] {
1482+
if (!fields_cache_.has_value()) {
14971483
fields_cache_ = ProtoPolicy<Message>::GetProtobufFields(
14981484
prototype_.Get()->GetDescriptor());
1499-
});
1500-
return fields_cache_;
1485+
};
1486+
return *fields_cache_;
15011487
}
15021488

15031489
static auto GetFieldName(const FieldDescriptor* field) {
@@ -1689,7 +1675,6 @@ class ProtobufDomainUntypedImpl
16891675
using DomainT = decltype(GetDefaultDomainForField<T, is_repeated>(field));
16901676
// Do the operation under a lock to prevent race conditions in `const`
16911677
// methods.
1692-
absl::MutexLock l(&mutex_);
16931678
auto it = domains_.find(field->number());
16941679
if (it == domains_.end()) {
16951680
it = domains_
@@ -2012,19 +1997,14 @@ class ProtobufDomainUntypedImpl
20121997
PrototypePtr<Message> prototype_;
20131998
bool use_lazy_initialization_;
20141999

2015-
mutable absl::Mutex mutex_;
2016-
mutable absl::flat_hash_map<int, CopyableAny> domains_
2017-
ABSL_GUARDED_BY(mutex_);
2000+
mutable absl::flat_hash_map<int, CopyableAny> domains_;
20182001

20192002
ProtoPolicy<Message> policy_;
20202003
absl::flat_hash_set<int> customized_fields_;
20212004
absl::flat_hash_set<int> always_set_oneofs_;
20222005
absl::flat_hash_set<int> uncustomizable_oneofs_;
20232006
absl::flat_hash_set<int> unset_oneof_fields_;
2024-
2025-
// Never access the field directly, always use `GetProtobufFields()`.
2026-
mutable std::vector<const FieldDescriptor*> fields_cache_;
2027-
mutable absl::once_flag fields_cache_once_;
2007+
mutable std::optional<std::vector<const FieldDescriptor*>> fields_cache_;
20282008
};
20292009

20302010
// Domain for `T` where `T` is a Protobuf message type.

0 commit comments

Comments
 (0)