Coverage Report

Created: 2026-11-12 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/runtime/activation.h
Line
Count
Source
1
// Copyright 2023 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#ifndef THIRD_PARTY_CEL_CPP_RUNTIME_ACTIVATION_H_
16
#define THIRD_PARTY_CEL_CPP_RUNTIME_ACTIVATION_H_
17
18
#include <memory>
19
#include <string>
20
#include <utility>
21
#include <vector>
22
23
#include "absl/base/nullability.h"
24
#include "absl/container/flat_hash_map.h"
25
#include "absl/functional/any_invocable.h"
26
#include "absl/status/status.h"
27
#include "absl/status/statusor.h"
28
#include "absl/strings/string_view.h"
29
#include "absl/synchronization/mutex.h"
30
#include "absl/types/optional.h"
31
#include "absl/types/span.h"
32
#include "base/attribute.h"
33
#include "base/attribute_matcher.h"
34
#include "common/function_descriptor.h"
35
#include "common/value.h"
36
#include "internal/status_macros.h"
37
#include "runtime/activation_interface.h"
38
#include "runtime/function.h"
39
#include "runtime/function_overload_reference.h"
40
#include "google/protobuf/arena.h"
41
#include "google/protobuf/descriptor.h"
42
#include "google/protobuf/message.h"
43
44
namespace cel {
45
46
namespace runtime_internal {
47
class ActivationAttributeMatcherAccess;
48
}
49
50
// Thread-compatible implementation of a CEL Activation.
51
//
52
// Values can either be provided eagerly or via a provider.
53
class Activation final : public ActivationInterface {
54
 public:
55
  // Definition for value providers.
56
  using ValueProvider =
57
      absl::AnyInvocable<absl::StatusOr<absl::optional<Value>>(
58
          absl::string_view, const google::protobuf::DescriptorPool* absl_nonnull,
59
          google::protobuf::MessageFactory* absl_nonnull, google::protobuf::Arena* absl_nonnull)>;
60
61
0
  Activation() = default;
62
63
  // Move only.
64
  Activation(Activation&& other);
65
66
  Activation& operator=(Activation&& other);
67
68
  // Implements ActivationInterface.
69
  absl::StatusOr<bool> FindVariable(
70
      absl::string_view name,
71
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
72
      google::protobuf::MessageFactory* absl_nonnull message_factory,
73
      google::protobuf::Arena* absl_nonnull arena,
74
      Value* absl_nonnull result) const override;
75
  using ActivationInterface::FindVariable;
76
77
  std::vector<FunctionOverloadReference> FindFunctionOverloads(
78
      absl::string_view name) const override;
79
80
  // Bind a value to a named variable.
81
  //
82
  // Returns false if the entry for name was overwritten.
83
  bool InsertOrAssignValue(absl::string_view name, Value value);
84
85
  // Bind a provider to a named variable. The result of the provider may be
86
  // memoized by the activation.
87
  //
88
  // Returns false if the entry for name was overwritten.
89
  bool InsertOrAssignValueProvider(absl::string_view name,
90
                                   ValueProvider provider);
91
92
0
  absl::Status AddUnknownPattern(const cel::AttributePattern& pattern) {
93
0
    // This function used to return void, thus they could not fail. We use
94
0
    // upsert because it just works most of the time, especially if the caller
95
0
    // provided overlapped attribute patterns. Ideally we would use insert.
96
0
    return unknown_attributes_.UpsertAttribute(pattern);
97
0
  }
98
99
  absl::Status SetUnknownPatterns(
100
0
      absl::Span<const cel::AttributePattern> patterns) {
101
0
    AttributeMatcher matcher;
102
0
    for (const auto& pattern : patterns) {
103
0
      // This function used to return void, thus they could not fail. We use
104
0
      // upsert because it just works most of the time, especially if the caller
105
0
      // provided overlapped attribute patterns. Ideally we would use insert.
106
0
      CEL_RETURN_IF_ERROR(matcher.UpsertAttribute(pattern));
107
0
    }
108
0
    unknown_attributes_ = std::move(matcher);
109
0
    return absl::OkStatus();
110
0
  }
111
112
0
  absl::Status AddMissingPattern(const cel::AttributePattern& pattern) {
113
0
    // This function used to return void, thus they could not fail. We use
114
0
    // upsert because it just works most of the time, especially if the caller
115
0
    // provided overlapped attribute patterns. Ideally we would use insert.
116
0
    return missing_attributes_.UpsertAttribute(pattern);
117
0
  }
118
119
  absl::Status SetMissingPatterns(
120
0
      absl::Span<const cel::AttributePattern> patterns) {
121
0
    AttributeMatcher matcher;
122
0
    for (const auto& pattern : patterns) {
123
0
      // This function used to return void, thus they could not fail. We use
124
0
      // upsert because it just works most of the time, especially if the caller
125
0
      // provided overlapped attribute patterns. Ideally we would use insert.
126
0
      CEL_RETURN_IF_ERROR(matcher.UpsertAttribute(pattern));
127
0
    }
128
0
    missing_attributes_ = std::move(matcher);
129
0
    return absl::OkStatus();
130
0
  }
131
132
0
  absl::Status AddKnownPattern(const cel::AttributePattern& pattern) {
133
0
    // This function used to return void, thus they could not fail. We use
134
0
    // upsert because it just works most of the time, especially if the caller
135
0
    // provided overlapped attribute patterns. Ideally we would use insert.
136
0
    return known_attributes_.UpsertAttribute(pattern);
137
0
  }
138
139
  absl::Status SetKnownPatterns(
140
0
      absl::Span<const cel::AttributePattern> patterns) {
141
0
    AttributeMatcher matcher;
142
0
    for (const auto& pattern : patterns) {
143
0
      // This function used to return void, thus they could not fail. We use
144
0
      // upsert because it just works most of the time, especially if the caller
145
0
      // provided overlapped attribute patterns. Ideally we would use insert.
146
0
      CEL_RETURN_IF_ERROR(matcher.UpsertAttribute(pattern));
147
0
    }
148
0
    known_attributes_ = std::move(matcher);
149
0
    return absl::OkStatus();
150
0
  }
151
152
  // Returns true if the function was inserted (no other registered function has
153
  // a matching descriptor).
154
  bool InsertFunction(const cel::FunctionDescriptor& descriptor,
155
                      std::unique_ptr<cel::Function> impl);
156
157
0
  const AttributeMatcher& GetUnknownAttributeMatcher() const final {
158
0
    return unknown_attributes_;
159
0
  }
160
161
0
  const AttributeMatcher& GetKnownAttributeMatcher() const final {
162
0
    return known_attributes_;
163
0
  }
164
165
0
  const AttributeMatcher& GetMissingAttributeMatcher() const final {
166
0
    return missing_attributes_;
167
0
  }
168
169
 private:
170
  struct ValueEntry {
171
    // If provider is present, then access must be synchronized to maintain
172
    // thread-compatible semantics for the lazily provided value.
173
    absl::optional<Value> value;
174
    absl::optional<ValueProvider> provider;
175
  };
176
177
  struct FunctionEntry {
178
    std::unique_ptr<cel::FunctionDescriptor> descriptor;
179
    std::unique_ptr<cel::Function> implementation;
180
  };
181
182
0
  friend void swap(Activation& a, Activation& b) {
183
0
    using std::swap;
184
0
    swap(a.values_, b.values_);
185
0
    swap(a.functions_, b.functions_);
186
0
    swap(a.unknown_attributes_, b.unknown_attributes_);
187
0
    swap(a.known_attributes_, b.known_attributes_);
188
0
    swap(a.missing_attributes_, b.missing_attributes_);
189
0
  }
190
191
  // Internal getter for provided values.
192
  // Assumes entry for name is present and is a provided value.
193
  // Handles synchronization for caching the provided value.
194
  absl::StatusOr<bool> ProvideValue(
195
      absl::string_view name,
196
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
197
      google::protobuf::MessageFactory* absl_nonnull message_factory,
198
      google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const;
199
200
  // mutex_ used for safe caching of provided variables
201
  mutable absl::Mutex mutex_;
202
  mutable absl::flat_hash_map<std::string, ValueEntry> values_;
203
204
  AttributeMatcher unknown_attributes_;
205
  AttributeMatcher known_attributes_;
206
  AttributeMatcher missing_attributes_;
207
208
  absl::flat_hash_map<std::string, std::vector<FunctionEntry>> functions_;
209
};
210
211
}  // namespace cel
212
213
#endif  // THIRD_PARTY_CEL_CPP_RUNTIME_ACTIVATION_H_