Skip to content
Merged
Show file tree
Hide file tree
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
20 changes: 9 additions & 11 deletions eval/public/extension_func_registrar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,12 @@ CelValue GetTimeOfDayUTC(Arena* arena, absl::Time time_stamp) {
return GetTimeOfDayTz(arena, time_stamp, utc);
}

int ToSeconds(const google::type::TimeOfDay* time_of_day) {
int seconds = 0;

seconds += time_of_day->hours() * 60 * 60;
seconds += time_of_day->minutes() * 60;
seconds += time_of_day->seconds();

return seconds;
// Fields are widened to int64_t so that out-of-range TimeOfDay values cannot
// overflow (and wrap into the valid range) in 32-bit arithmetic.
int64_t ToSeconds(const google::type::TimeOfDay* time_of_day) {
return static_cast<int64_t>(time_of_day->hours()) * 60 * 60 +
static_cast<int64_t>(time_of_day->minutes()) * 60 +
static_cast<int64_t>(time_of_day->seconds());
}

CelValue BetweenToD(Arena* arena, const google::protobuf::Message* time_of_day,
Expand All @@ -135,9 +133,9 @@ CelValue BetweenToD(Arena* arena, const google::protobuf::Message* time_of_day,
absl::StatusCode::kInvalidArgument);
}
// resolution for TimeOfDay in this function is 1 second
int start_time = ToSeconds(start_tod);
int stop_time = ToSeconds(stop_tod);
int tod_time = ToSeconds(time_of_day_tod);
int64_t start_time = ToSeconds(start_tod);
int64_t stop_time = ToSeconds(stop_tod);
int64_t tod_time = ToSeconds(time_of_day_tod);

is_between = (tod_time >= start_time) && (tod_time < stop_time);
return CelValue::CreateBool(is_between);
Expand Down
37 changes: 37 additions & 0 deletions eval/public/extension_func_test.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <cstdint>
#include <limits>
#include <string>
#include <vector>

Expand Down Expand Up @@ -544,6 +546,41 @@ TEST_F(ExtensionTest, TestBetweenToD) {
ASSERT_EQ(result.BoolOrDie(), false);
}

TEST_F(ExtensionTest, TestBetweenToDOutOfRangeHoursDoNotWrap) {
Arena arena;
CelValue result;
google::type::TimeOfDay* time_of_day =
Arena::Create<google::type::TimeOfDay>(&arena);
google::type::TimeOfDay* start =
Arena::Create<google::type::TimeOfDay>(&arena);
google::type::TimeOfDay* stop =
Arena::Create<google::type::TimeOfDay>(&arena);

start->set_hours(0);
stop->set_hours(1);
// 1193047 hours is 4294969200 seconds, which wraps to 1904 seconds (00:31:44)
// in 32-bit arithmetic and would wrongly land inside [00:00, 01:00).
time_of_day->set_hours(1193047);

PerformBetweenToDTest(&arena, time_of_day, start, stop, &result);
ASSERT_TRUE(result.IsBool());
EXPECT_FALSE(result.BoolOrDie());

time_of_day->set_hours(std::numeric_limits<int32_t>::max());
time_of_day->set_minutes(std::numeric_limits<int32_t>::max());
time_of_day->set_seconds(std::numeric_limits<int32_t>::max());
PerformBetweenToDTest(&arena, time_of_day, start, stop, &result);
ASSERT_TRUE(result.IsBool());
EXPECT_FALSE(result.BoolOrDie());

time_of_day->set_hours(std::numeric_limits<int32_t>::min());
time_of_day->set_minutes(std::numeric_limits<int32_t>::min());
time_of_day->set_seconds(std::numeric_limits<int32_t>::min());
PerformBetweenToDTest(&arena, time_of_day, start, stop, &result);
ASSERT_TRUE(result.IsBool());
EXPECT_FALSE(result.BoolOrDie());
}

TEST_F(ExtensionTest, TestBetweenTodStr) {
Arena arena;
CelValue result;
Expand Down
8 changes: 8 additions & 0 deletions runtime/standard/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,18 @@ cc_test(
"//base:builtins",
"//common:function_descriptor",
"//common:kind",
"//common:value",
"//common:value_testing",
"//internal:testing",
"//internal:testing_descriptor_pool",
"//internal:testing_message_factory",
"//runtime:function",
"//runtime:function_overload_reference",
"//runtime:function_registry",
"//runtime:runtime_options",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/strings",
"@com_google_protobuf//:protobuf",
],
)

Expand Down
4 changes: 2 additions & 2 deletions runtime/standard/container_membership_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ absl::Status RegisterMapMembershipFunctions(FunctionRegistry& registry,
CEL_RETURN_IF_ERROR(map_value.Has(IntValue(number.AsInt()),
descriptor_pool, message_factory,
arena, &has_alt));
if (has.IsTrue()) {
return has;
if (has_alt.IsTrue()) {
return has_alt;
}
}
return BoolValue(false);
Expand Down
56 changes: 56 additions & 0 deletions runtime/standard/container_membership_functions_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,33 @@
#include "runtime/standard/container_membership_functions.h"

#include <array>
#include <cstdint>
#include <limits>
#include <utility>
#include <vector>

#include "absl/status/status_matchers.h"
#include "absl/strings/string_view.h"
#include "base/builtins.h"
#include "common/function_descriptor.h"
#include "common/kind.h"
#include "common/value.h"
#include "common/value_testing.h"
#include "internal/testing.h"
#include "internal/testing_descriptor_pool.h"
#include "internal/testing_message_factory.h"
#include "runtime/function.h"
#include "runtime/function_overload_reference.h"
#include "runtime/function_registry.h"
#include "runtime/runtime_options.h"
#include "google/protobuf/arena.h"

namespace cel {
namespace {

using ::absl_testing::IsOk;
using ::cel::test::BoolValueIs;
using ::testing::SizeIs;
using ::testing::UnorderedElementsAre;

MATCHER_P3(MatchesDescriptor, name, receiver, expected_kinds, "") {
Expand Down Expand Up @@ -131,6 +145,48 @@ TEST(RegisterContainerMembershipFunctions, RegistersInOperatorListsDisabled) {
}
}

// When enable_fast_builtins is true, the planner routes heterogeneous `in`
// through a dedicated step; invoke the registered overload directly to cover
// the uint -> int key fallback used when enable_fast_builtins is false.
TEST(RegisterContainerMembershipFunctions, HeterogeneousUintKeyMatchesIntKey) {
FunctionRegistry registry;
RuntimeOptions options;
options.enable_heterogeneous_equality = true;
ASSERT_THAT(RegisterContainerMembershipFunctions(registry, options), IsOk());

google::protobuf::Arena arena;
auto builder = NewMapValueBuilder(&arena);
ASSERT_THAT(builder->Put(IntValue(1), IntValue(2)), IsOk());
ASSERT_THAT(builder->Put(IntValue(-1), IntValue(3)), IsOk());
MapValue map_value = std::move(*builder).Build();

for (absl::string_view operator_name : kInOperators) {
std::vector<FunctionOverloadReference> overloads =
registry.FindStaticOverloads(operator_name, /*receiver_style=*/false,
{Kind::kUint, Kind::kMap});
ASSERT_THAT(overloads, SizeIs(1));
const Function::InvokeContext context(internal::GetTestingDescriptorPool(),
internal::GetTestingMessageFactory(),
&arena);

ASSERT_OK_AND_ASSIGN(Value found, overloads[0].implementation.Invoke(
{UintValue(1), map_value}, context));
EXPECT_THAT(found, BoolValueIs(true));

ASSERT_OK_AND_ASSIGN(
Value missing,
overloads[0].implementation.Invoke({UintValue(2), map_value}, context));
EXPECT_THAT(missing, BoolValueIs(false));

ASSERT_OK_AND_ASSIGN(
Value unconvertible,
overloads[0].implementation.Invoke(
{UintValue(std::numeric_limits<uint64_t>::max()), map_value},
context));
EXPECT_THAT(unconvertible, BoolValueIs(false));
}
}

// TODO(uncreated-issue/41): move functional parsed expr tests when modern APIs for
// evaluator available.

Expand Down
27 changes: 27 additions & 0 deletions runtime/standard_runtime_builder_factory_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,33 @@ INSTANTIATE_TEST_SUITE_P(
{"list_in", "'a' in ['a', 'b', 'c', 'd']", true},
{"list_in_numeric", "3u in [1.1, 2.3, 3.0, 4.4]", true}}));

TEST(StandardRuntimeTest, HeterogeneousInMapWithoutFastBuiltins) {
// Without fast builtins the planner dispatches `in` to the registered map
// overloads, so heterogeneous numeric key fallbacks must be honored there.
RuntimeOptions options;
options.enable_fast_builtins = false;

ASSERT_OK_AND_ASSIGN(auto builder,
CreateStandardRuntimeBuilder(
google::protobuf::DescriptorPool::generated_pool(), options));
ASSERT_OK_AND_ASSIGN(auto runtime, std::move(builder).Build());

for (const auto& test_case : std::vector<std::pair<std::string, bool>>{
{"1u in {1: 1, 2: 2}", true},
{"3u in {1: 1, 2: 2}", false},
{"1 in {1u: 1, 2u: 2}", true},
{"1.0 in {1: 1, 2: 2}", true},
}) {
ASSERT_OK_AND_ASSIGN(ParsedExpr expr, ParseWithTestMacros(test_case.first));
ASSERT_OK_AND_ASSIGN(std::unique_ptr<Program> program,
ProtobufRuntimeAdapter::CreateProgram(*runtime, expr));
google::protobuf::Arena arena;
Activation activation;
ASSERT_OK_AND_ASSIGN(Value result, program->Evaluate(&arena, activation));
EXPECT_THAT(result, BoolValueIs(test_case.second)) << test_case.first;
}
}

TEST(StandardRuntimeTest, RuntimeIssueSupport) {
RuntimeOptions options;
options.fail_on_warnings = false;
Expand Down
Loading