Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ final class CelZ3OperatorTranslator {

Optional<TranslatedValue> translateFunctionCall(
String functionName, List<TranslatedValue> args, long exprId, CelAbstractSyntaxTree ast) {
ImmutableList<String> overloadIds =
ast.getReference(exprId).map(CelReference::overloadIds).orElse(ImmutableList.of());
if (overloadIds.equals(ImmutableList.of("optional_or_optional"))) {
return Optional.of(translateOptionalOr(functionName, args, ast, /* unwrap= */ false));
}
if (overloadIds.equals(ImmutableList.of("optional_orValue_value"))) {
return Optional.of(translateOptionalOr(functionName, args, ast, /* unwrap= */ true));
}
Optional<Operator> opOpt = Operator.findReverse(functionName);
ImmutableList<Expr<?>> z3Args =
args.stream().map(TranslatedValue::z3Expr).collect(toImmutableList());
Expand Down Expand Up @@ -238,6 +246,43 @@ private BoolExpr constrainInWitnessTypes(
mayObserveTruncatedTypeConstraint(ast, lhsTv, rhsTv)));
}

/**
* Translates {@code receiver.or(alternative)}, or {@code orValue} if {@code unwrap}, which only
* evaluates the alternative if the receiver is empty.
*/
private TranslatedValue translateOptionalOr(
String functionName, List<TranslatedValue> args, CelAbstractSyntaxTree ast, boolean unwrap) {
TranslatedValue receiver = args.get(0);
TranslatedValue alternative = args.get(1);
Expr<?> optRef = typeSystem.getOptionalRef(receiver.z3Expr());
TranslatedValue hasValue =
TranslatedValue.propagateStrict(
ctx,
typeSystem,
functionName,
typeSystem.withRuntimeError(
typeSystem.wrapBool(typeSystem.optHasValue(optRef)),
ctx.mkNot(typeSystem.isOptional(receiver.z3Expr()))),
receiver);
TranslatedValue present =
unwrap
? TranslatedValue.create(typeSystem.getOptionalValue(optRef), typeSystem, ctx.mkFalse())
: receiver;
Expr<?> alt = alternative.z3Expr();
// `isUnknown` is untested: a model can't be forced to make an unknown input unknown.
TranslatedValue absent =
unwrap
? alternative
: TranslatedValue.create(
ctx.mkITE(
ctx.mkOr(typeSystem.isOptional(alt), typeSystem.isUnknown(alt)),
alt,
typeSystem.mkError()),
typeSystem,
alternative.isApproximate());
return translateConditional(ImmutableList.of(hasValue, present, absent), ast);
}

private BoolExpr mkTypeGuard(Expr<?> arg, CelType expectedType) {
switch (expectedType.kind()) {
case LIST:
Expand Down
38 changes: 7 additions & 31 deletions verifier/src/main/java/dev/cel/verifier/axioms/OptionalAxioms.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
import static dev.cel.extensions.CelOptionalLibrary.OptionalDeclaration.OPTIONAL_NONE;
import static dev.cel.extensions.CelOptionalLibrary.OptionalDeclaration.OPTIONAL_OF;
import static dev.cel.extensions.CelOptionalLibrary.OptionalDeclaration.OPTIONAL_OF_NON_ZERO_VALUE;
import static dev.cel.extensions.CelOptionalLibrary.OptionalDeclaration.OPTIONAL_OR;
import static dev.cel.extensions.CelOptionalLibrary.OptionalDeclaration.OPTIONAL_OR_VALUE;
import static dev.cel.extensions.CelOptionalLibrary.OptionalDeclaration.OPTIONAL_VALUE;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.microsoft.z3.BoolExpr;
import com.microsoft.z3.Context;
import com.microsoft.z3.Expr;
import com.microsoft.z3.FPExpr;
import com.microsoft.z3.SeqExpr;
import dev.cel.common.CelFunctionDecl;
import dev.cel.common.CelOverloadDecl;
Expand Down Expand Up @@ -72,19 +69,6 @@ final class OptionalAxioms {
Expr<?> optRef = ts.getOptionalRef(val);
return Optional.of(
ctx.mkITE(ts.optHasValue(optRef), ts.getOptionalValue(optRef), ts.mkError()));
}),
createBinaryAxiom(
OPTIONAL_OR_VALUE,
(ctx, ts, sink, val, other) -> {
Expr<?> optRef = ts.getOptionalRef(val);
return Optional.of(
ctx.mkITE(ts.optHasValue(optRef), ts.getOptionalValue(optRef), other));
}),
createBinaryAxiom(
OPTIONAL_OR,
(ctx, ts, sink, val, other) -> {
Expr<?> optRef = ts.getOptionalRef(val);
return Optional.of(ctx.mkITE(ts.optHasValue(optRef), val, other));
}));

private static BoolExpr isZeroValue(Context ctx, CelZ3TypeSystem ts, Expr<?> val) {
Expand All @@ -93,7 +77,7 @@ private static BoolExpr isZeroValue(Context ctx, CelZ3TypeSystem ts, Expr<?> val
ctx.mkAnd(ts.isBool(val), ctx.mkEq(ts.unwrapBool(val), ctx.mkFalse())),
ctx.mkAnd(ts.isInt(val), ctx.mkEq(ts.getInt(val), ctx.mkInt(0))),
ctx.mkAnd(ts.isUint(val), ctx.mkEq(ts.getUint(val), ctx.mkInt(0))),
ctx.mkAnd(ts.isDouble(val), ctx.mkFPIsZero((FPExpr) ts.getDouble(val))),
ctx.mkAnd(ts.isDouble(val), ctx.mkFPIsZero(ts.getDouble(val))),
ctx.mkAnd(ts.isTimestamp(val), ctx.mkEq(ts.getTimestamp(val), ctx.mkInt(0))),
ctx.mkAnd(ts.isDuration(val), ctx.mkEq(ts.getDuration(val), ctx.mkInt(0))),
ctx.mkAnd(ts.isString(val), ctx.mkEq(ts.getString(val), ctx.mkString(""))),
Expand All @@ -113,25 +97,17 @@ private static BoolExpr isZeroValue(Context ctx, CelZ3TypeSystem ts, Expr<?> val
private static CelZ3FunctionAxiom createAxiom(
CelFunctionDecl.Declarer declarer, CelZ3OverloadTranslator translator) {
CelFunctionDecl functionDecl = declarer.functionDecl();
CelZ3FunctionAxiom.Builder builder = CelZ3FunctionAxiom.newBuilder(functionDecl);
builder.addOverloadTranslator(getSingleOverloadOrThrow(functionDecl), translator);
return builder.build();
return CelZ3FunctionAxiom.newBuilder(functionDecl)
.addOverloadTranslator(getSingleOverloadOrThrow(functionDecl), translator)
.build();
}

private static CelZ3FunctionAxiom createUnaryAxiom(
CelFunctionDecl.Declarer declarer, CelZ3FunctionAxiom.UnaryTranslator translator) {
CelFunctionDecl functionDecl = declarer.functionDecl();
CelZ3FunctionAxiom.Builder builder = CelZ3FunctionAxiom.newBuilder(functionDecl);
builder.addUnaryOverloadTranslator(getSingleOverloadOrThrow(functionDecl), translator);
return builder.build();
}

private static CelZ3FunctionAxiom createBinaryAxiom(
CelFunctionDecl.Declarer declarer, CelZ3FunctionAxiom.BinaryTranslator translator) {
CelFunctionDecl functionDecl = declarer.functionDecl();
CelZ3FunctionAxiom.Builder builder = CelZ3FunctionAxiom.newBuilder(functionDecl);
builder.addBinaryOverloadTranslator(getSingleOverloadOrThrow(functionDecl), translator);
return builder.build();
return CelZ3FunctionAxiom.newBuilder(functionDecl)
.addUnaryOverloadTranslator(getSingleOverloadOrThrow(functionDecl), translator)
.build();
}

private static CelOverloadDecl getSingleOverloadOrThrow(CelFunctionDecl functionDecl) {
Expand Down
30 changes: 30 additions & 0 deletions verifier/src/test/java/dev/cel/verifier/CelVerifierZ3ImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ public void setUp() {

private enum IsSatisfiableTestCase {
SATISFIABLE("x > 5", "x = (?:[6-9]|[1-9]\\d+)"),
OPTIONAL_OR_VALUE_SKIPS_APPROXIMATE_ALTERNATIVE(
"optional.of(x).orValue(int('123')) == 7", "x = 7"),
DYNAMIC_ARITHMETIC("request == 1 && request + 2 == 3", "request = 1"),
DYNAMIC_ARITHMETIC_UNARY("request == 1 && -request == -1", "request = 1"),
GREATER_DOUBLE("d > 1.5", "d = "),
Expand Down Expand Up @@ -689,6 +691,11 @@ public void isSatisfiable_approximate_returnsPotentialSatisfyingInput() throws E
}

private enum IsSatisfiableInconclusiveTestCase {
OPTIONAL_OR_VALUE_EMPTY_RECEIVER_APPROXIMATE_ALTERNATIVE(
"optional.none().orValue(int('123')) == 7"),
OPTIONAL_OR_VALUE_APPROXIMATE_RECEIVER("optional.of(int('123')).orValue(1) == 7"),
OPTIONAL_OR_EMPTY_RECEIVER_APPROXIMATE_ALTERNATIVE(
"optional.none().or(optional.of(int('123'))) == optional.of(7)"),
// A list of nullable ints may hold null past the unroll limit.
NULL_IN_NULLABLE_LIST_PAST_UNROLL_LIMIT(
"size(nullable_int_list) == 6 && nullable_int_list[0] != null && nullable_int_list[1] !="
Expand Down Expand Up @@ -834,6 +841,7 @@ public void isSatisfiable_comprehensionZeroUnrollLimit_inconclusive() throws Exc
}

private enum IsUnsatisfiableTestCase {
OPTIONAL_OR_NON_OPTIONAL_ALTERNATIVE("optional.none().or(dyn(x)) == dyn(x)"),
DYN_MESSAGE_PRESENT_UNDECLARED_FIELD("dyn(test_all_types).unknown_field == 42"),
REPEATED_FIELD_FROM_NON_LIST(
"dyn_var == 42 && TestAllTypes{repeated_int64: dyn_var} == TestAllTypes{}"),
Expand Down Expand Up @@ -1225,6 +1233,9 @@ private enum IsAlwaysTrueTestCase {
OPTIONAL_OF_EQ_OF_VAR("optional.of(x) == optional.of(x)"),
OPTIONAL_OF_NEQ_OF_VAR("x != y ? optional.of(x) != optional.of(y) : true"),
OPTIONAL_OR_VALUE_VAR("optional.of(x).orValue(y) == x"),
OPTIONAL_OR_VALUE_SKIPS_FAILING_ALTERNATIVE("optional.of(x).orValue(1 / 0) == x"),
OPTIONAL_OR_SKIPS_FAILING_ALTERNATIVE(
"optional.of(x).or(optional.of(1 / 0)) == optional.of(x)"),
// Indexing follows the value, whatever type the checker narrowed it to.
NARROWED_MAP_INDEXED_AS_LIST(
"dyn_var == optional.of([2]) ? dyn_var.orValue({0: 1})[0] == 2 : true"),
Expand Down Expand Up @@ -1778,6 +1789,19 @@ public void verifyEquivalence_withUnknownIdentifiers_inconclusive(
assertThat(result.status()).isEqualTo(VerificationStatus.INCONCLUSIVE);
}

@Test
public void verifyEquivalence_orValueOfNonOptionalWithUnknownAlternative_verified()
throws Exception {
CelVerifier verifier =
CelVerifierFactory.newVerifier(CEL).addUnknownIdentifier("unknown_var").build();
CelAbstractSyntaxTree astA = CEL.compile("dyn(1).orValue(unknown_var)").getAst();
CelAbstractSyntaxTree astB = CEL.compile("1 / 0").getAst();

CelVerificationResult result = verifier.verifyEquivalence(astA, astB);

assertThat(result.status()).isEqualTo(VerificationStatus.VERIFIED);
}

@Test
public void verifyEquivalence_freeVariableIndicesDeduplicated() throws Exception {
CelAbstractSyntaxTree astA =
Expand Down Expand Up @@ -2514,6 +2538,9 @@ public void isAlwaysTrue_inconclusive_containsPotentialCounterexample() throws E
}

private enum EquivalenceInconclusiveTestCase {
OPTIONAL_OR_VALUE_OF_TRUNCATED_RECEIVER(
"size(int_list) > 5 ? int_list.map(n, optional.none())[0].orValue(1) : 0",
"size(int_list) > 5 ? int_list.map(n, optional.none())[0].orValue(2) : 0"),
// A list or map typed dyn holding NaN (e.g. `[NaN]`) differs from itself, so these do diverge,
// but lists and maps typed dyn on both sides compare approximately.
DYN_NAN_INPUT_AND_LITERAL("dyn_var != dyn_var ? 0.0 / 0.0 : dyn_var", "dyn_var"),
Expand Down Expand Up @@ -2641,6 +2668,7 @@ public void verifyEquivalence_inconclusive(
}

private enum EquivalenceTestCase {
OPTIONAL_OR_VALUE_OF_NON_OPTIONAL("dyn(1).orValue(int('123'))", "1 / 0"),
STRUCT_UNSET_TIMESTAMP_DEFAULT("TestAllTypes{}.single_timestamp", "timestamp(0)"),
// Both are errors.
STRUCT_UNSET_ANY_DEFAULT("TestAllTypes{}.single_any == null", "1 / 0 == 1"),
Expand Down Expand Up @@ -3188,6 +3216,8 @@ private enum EquivalenceViolationTestCase {
CROSS_TYPE_SYMBOLIC_INEQUALITY_INT_UINT("dyn(x) == dyn(u)", "false"),
CROSS_TYPE_SYMBOLIC_INEQUALITY_UINT_INT("dyn(u) == dyn(x)", "false"),
OPTIONAL_OR_VALUE_VIOLATION("optional.of(x).orValue(y)", "y"),
OPTIONAL_OR_VALUE_FAILING_ALTERNATIVE(
"optional.of(x).orValue(1 / 0)", "optional.of(y).orValue(1 / 0)"),
OPTIONAL_VALUE_VIOLATION("optional.of(x).value()", "y"),
LIST_OPTIONAL_ELEMENTS_COLLISION("[1, ?opt_var]", "[1, opt_var]"),
CROSS_NUMERIC_EQUALITY_INT_DYN_VIOLATION("1 == request", "false"),
Expand Down
Loading