Skip to content

Commit 7a66fef

Browse files
CEL Dev Teamcopybara-github
authored andcommitted
Fix crash in coverage collection of bool nodes which return an error.
PiperOrigin-RevId: 819772476
1 parent 5eb221b commit 7a66fef

File tree

3 files changed

+124
-1
lines changed

3 files changed

+124
-1
lines changed

testing/testrunner/BUILD

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,33 @@ cc_library(
177177
"@com_google_cel_spec//proto/cel/expr:syntax_cc_proto",
178178
],
179179
)
180+
181+
cc_test(
182+
name = "coverage_index_test",
183+
srcs = ["coverage_index_test.cc"],
184+
deps = [
185+
":coverage_index",
186+
"//checker:type_checker_builder",
187+
"//checker:validation_result",
188+
"//common:ast",
189+
"//common:ast_proto",
190+
"//common:decl",
191+
"//common:type",
192+
"//common:value",
193+
"//compiler",
194+
"//compiler:compiler_factory",
195+
"//compiler:standard_library",
196+
"//internal:status_macros",
197+
"//internal:testing",
198+
"//internal:testing_descriptor_pool",
199+
"//runtime",
200+
"//runtime:activation",
201+
"//runtime:runtime_builder",
202+
"//runtime:standard_runtime_builder_factory",
203+
"@com_google_absl//absl/status:status_matchers",
204+
"@com_google_absl//absl/status:statusor",
205+
"@com_google_absl//absl/strings:string_view",
206+
"@com_google_cel_spec//proto/cel/expr:syntax_cc_proto",
207+
"@com_google_protobuf//:protobuf",
208+
],
209+
)

testing/testrunner/coverage_index.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ void TraverseAndCalculateCoverage(
175175
void CoverageIndex::RecordCoverage(int64_t node_id, const cel::Value& value) {
176176
NodeCoverageStats& stats = node_coverage_stats_[node_id];
177177
stats.covered = true;
178-
if (node_coverage_stats_[node_id].is_boolean_node) {
178+
if (node_coverage_stats_[node_id].is_boolean_node && value.IsBool()) {
179179
if (value.AsBool()->NativeValue()) {
180180
stats.has_true_branch = true;
181181
} else {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright 2025 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+
#include "testing/testrunner/coverage_index.h"
15+
16+
#include <memory>
17+
#include <utility>
18+
19+
#include "cel/expr/syntax.pb.h"
20+
#include "absl/status/status_matchers.h"
21+
#include "absl/status/statusor.h"
22+
#include "absl/strings/string_view.h"
23+
#include "checker/type_checker_builder.h"
24+
#include "checker/validation_result.h"
25+
#include "common/ast.h"
26+
#include "common/ast_proto.h"
27+
#include "common/decl.h"
28+
#include "common/type.h"
29+
#include "common/value.h"
30+
#include "compiler/compiler.h"
31+
#include "compiler/compiler_factory.h"
32+
#include "compiler/standard_library.h"
33+
#include "internal/status_macros.h"
34+
#include "internal/testing.h"
35+
#include "internal/testing_descriptor_pool.h"
36+
#include "runtime/activation.h"
37+
#include "runtime/runtime.h"
38+
#include "runtime/runtime_builder.h"
39+
#include "runtime/standard_runtime_builder_factory.h"
40+
#include "google/protobuf/arena.h"
41+
42+
namespace cel::test {
43+
namespace {
44+
45+
using ::absl_testing::IsOk;
46+
using ::cel::expr::CheckedExpr;
47+
48+
absl::StatusOr<std::unique_ptr<const cel::Runtime>> CreateTestRuntime() {
49+
CEL_ASSIGN_OR_RETURN(cel::RuntimeBuilder standard_runtime_builder,
50+
cel::CreateStandardRuntimeBuilder(
51+
cel::internal::GetTestingDescriptorPool(), {}));
52+
return std::move(standard_runtime_builder).Build();
53+
}
54+
55+
TEST(CoverageIndexTest, RecordCoverageWithErrorDoesNotCrash) {
56+
ASSERT_OK_AND_ASSIGN(
57+
std::unique_ptr<cel::CompilerBuilder> compiler_builder,
58+
cel::NewCompilerBuilder(cel::internal::GetTestingDescriptorPool()));
59+
ASSERT_THAT(compiler_builder->AddLibrary(cel::StandardCompilerLibrary()),
60+
IsOk());
61+
ASSERT_THAT(compiler_builder->GetCheckerBuilder().AddVariable(
62+
cel::MakeVariableDecl("x", cel::IntType())),
63+
IsOk());
64+
ASSERT_OK_AND_ASSIGN(std::unique_ptr<cel::Compiler> compiler,
65+
std::move(compiler_builder)->Build());
66+
ASSERT_OK_AND_ASSIGN(cel::ValidationResult validation_result,
67+
compiler->Compile("1/x > 1"));
68+
CheckedExpr checked_expr;
69+
ASSERT_THAT(cel::AstToCheckedExpr(*validation_result.GetAst(), &checked_expr),
70+
IsOk());
71+
72+
CoverageIndex coverage_index;
73+
coverage_index.Init(checked_expr);
74+
75+
ASSERT_OK_AND_ASSIGN(std::unique_ptr<const cel::Runtime> runtime,
76+
CreateTestRuntime());
77+
ASSERT_THAT(EnableCoverageInRuntime(*const_cast<cel::Runtime*>(runtime.get()),
78+
coverage_index),
79+
IsOk());
80+
ASSERT_OK_AND_ASSIGN(std::unique_ptr<cel::Ast> ast,
81+
cel::CreateAstFromCheckedExpr(checked_expr));
82+
ASSERT_OK_AND_ASSIGN(auto program, runtime->CreateProgram(std::move(ast)));
83+
84+
cel::Activation activation;
85+
activation.InsertOrAssignValue("x", cel::IntValue(0));
86+
google::protobuf::Arena arena;
87+
ASSERT_OK_AND_ASSIGN(cel::Value result,
88+
program->Evaluate(&arena, activation));
89+
EXPECT_TRUE(result.IsError());
90+
}
91+
92+
} // namespace
93+
} // namespace cel::test

0 commit comments

Comments
 (0)