Skip to content

Commit

Permalink
Store recursion analysis results per policy.
Browse files Browse the repository at this point in the history
Protos of same type could get customized through different policies, which could lead to different recursive structure.

PiperOrigin-RevId: 726496710
  • Loading branch information
hadi88 authored and copybara-github committed Feb 13, 2025
1 parent 73356f7 commit eb4c69a
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions fuzztest/internal/domains/protobuf_domain_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ class ProtoPolicy {
public:
ProtoPolicy()
: optional_policies_({{/*filter=*/IncludeAll<FieldDescriptor>(),
/*value=*/OptionalPolicy::kWithNull}}) {}
/*value=*/OptionalPolicy::kWithNull}}) {
static int64_t next_id = 0;
id_ = next_id++;
}

void SetOptionalPolicy(OptionalPolicy optional_policy) {
SetOptionalPolicy(IncludeAll<FieldDescriptor>(), optional_policy);
Expand Down Expand Up @@ -314,7 +317,11 @@ class ProtoPolicy {
return max;
}

int64_t id() const { return id_; }

private:
int64_t id_;

template <typename T>
struct FilterToValue {
Filter filter;
Expand Down Expand Up @@ -1707,20 +1714,21 @@ 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>>
static absl::NoDestructor<
absl::flat_hash_map<std::pair<int64_t, const FieldDescriptor*>, bool>>
cache ABSL_GUARDED_BY(mutex);
bool can_use_cache = IsCustomizedRecursivelyOnly();
if (can_use_cache) {
absl::MutexLock l(&mutex);
auto it = cache->find(field);
auto it = cache->find({policy_.id(), 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 (can_use_cache) {
absl::MutexLock l(&mutex);
cache->insert({field, result});
cache->insert({{policy_.id(), field}, result});
}
return result;
}
Expand Down

0 comments on commit eb4c69a

Please sign in to comment.