Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make recursion analysis cache thread safe. #1535

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions fuzztest/internal/domains/protobuf_domain_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <utility>
#include <vector>

#include "absl/base/attributes.h"
#include "absl/base/const_init.h"
#include "absl/base/no_destructor.h"
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
Expand Down Expand Up @@ -1703,17 +1705,22 @@ class ProtobufDomainUntypedImpl

bool IsFieldFinitelyRecursive(const FieldDescriptor* field) {
if (!field->message_type()) return false;
ABSL_CONST_INIT static absl::Mutex mutex(absl::kConstInit);
static absl::NoDestructor<absl::flat_hash_map<const FieldDescriptor*, bool>>
cache;
auto it = cache->end();
if (IsCustomizedRecursivelyOnly()) {
it = cache->find(field);
cache ABSL_GUARDED_BY(mutex);
bool can_use_cache = IsCustomizedRecursivelyOnly();
if (can_use_cache) {
absl::MutexLock l(&mutex);
auto it = cache->find(field);
if (it != cache->end()) return it->second;
}
absl::flat_hash_set<decltype(field->message_type())> parents;
bool result = IsProtoRecursive(field->message_type(), parents,
RecursionType::kFinitelyRecursive);
if (IsCustomizedRecursivelyOnly()) cache->insert(it, {field, result});
if (can_use_cache) {
absl::MutexLock l(&mutex);
cache->insert({field, result});
}
return result;
}

Expand Down Expand Up @@ -1831,7 +1838,7 @@ class ProtobufDomainUntypedImpl
// policy) and no individual field is customized at the top level. This check
// would be useful in recursion analysis. In particular, recursion analysis
// is only meaningful when all customizations are also recursive.
bool IsCustomizedRecursivelyOnly() {
bool IsCustomizedRecursivelyOnly() const {
return customized_fields_.empty() && always_set_oneofs_.empty() &&
uncustomizable_oneofs_.empty() && unset_oneof_fields_.empty();
}
Expand Down
Loading