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: 724449290
  • Loading branch information
hadi88 authored and copybara-github committed Feb 7, 2025
1 parent af84f4c commit 0617c13
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions fuzztest/internal/domains/protobuf_domain_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <cstddef>
#include <cstdint>
#include <functional>
#include <limits>
#include <memory>
#include <optional>
#include <string>
Expand Down Expand Up @@ -233,7 +234,10 @@ class ProtoPolicy {
public:
ProtoPolicy()
: optional_policies_({{/*filter=*/IncludeAll<FieldDescriptor>(),
/*value=*/OptionalPolicy::kWithNull}}) {}
/*value=*/OptionalPolicy::kWithNull}}) {
absl::BitGen gen;
id_ = absl::Uniform(gen, 0, std::numeric_limits<int64_t>::max());
}

void SetOptionalPolicy(OptionalPolicy optional_policy) {
SetOptionalPolicy(IncludeAll<FieldDescriptor>(), optional_policy);
Expand Down Expand Up @@ -314,7 +318,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 +1715,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 0617c13

Please sign in to comment.