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
22 changes: 15 additions & 7 deletions verifier/src/main/java/dev/cel/verifier/CelAstToZ3Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.microsoft.z3.Expr;
import com.microsoft.z3.FuncDecl;
import com.microsoft.z3.Model;
import com.microsoft.z3.Quantifier;
import com.microsoft.z3.SeqExpr;
import com.microsoft.z3.Sort;
import dev.cel.common.CelAbstractSyntaxTree;
Expand Down Expand Up @@ -135,14 +134,21 @@ TranslatedValue translate(CelAbstractSyntaxTree ast) {
Set<Expr<?>> listRefs = new LinkedHashSet<>();
Set<Expr<?>> mapRefs = new LinkedHashSet<>();
Set<Expr<?>> msgRefs = new LinkedHashSet<>();
Set<Expr<?>> optRefs = new LinkedHashSet<>();

for (Expr<?> constraint : context.constraints()) {
collectReferences(constraint, visited, listRefs, mapRefs, msgRefs);
collectReferences(constraint, visited, listRefs, mapRefs, msgRefs, optRefs);
}
collectReferences(result.z3Expr(), visited, listRefs, mapRefs, msgRefs);
collectReferences(result.z3Expr(), visited, listRefs, mapRefs, msgRefs, optRefs);

CelZ3ExtensionalityAxioms.generateAxioms(ctx, typeSystem, listRefs, mapRefs, msgRefs)
.forEach(context::addConstraint);
// A present optional holds a CEL value, even when the optional is held by a dyn.
for (Expr<?> ref : optRefs) {
context.constrainValidValue(
ctx.mkAnd(typeSystem.isOptional(ref.getArgs()[0]), typeSystem.optHasValue(ref)),
typeSystem.getOptionalValue(ref));
}

return result;
}
Expand All @@ -152,7 +158,8 @@ private void collectReferences(
Set<Expr<?>> visited,
Set<Expr<?>> listRefs,
Set<Expr<?>> mapRefs,
Set<Expr<?>> msgRefs) {
Set<Expr<?>> msgRefs,
Set<Expr<?>> optRefs) {
if (!visited.add(expr)) {
return;
}
Expand All @@ -164,13 +171,14 @@ private void collectReferences(
} else if (sort.equals(typeSystem.messageRefSort())) {
msgRefs.add(expr);
}
if (expr.isApp() && typeSystem.isOptionalRefAccessor(expr.getFuncDecl())) {
optRefs.add(expr);
}

if (expr.isApp()) {
for (Expr<?> arg : expr.getArgs()) {
collectReferences(arg, visited, listRefs, mapRefs, msgRefs);
collectReferences(arg, visited, listRefs, mapRefs, msgRefs, optRefs);
}
} else if (expr.isQuantifier()) {
collectReferences(((Quantifier) expr).getBody(), visited, listRefs, mapRefs, msgRefs);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import org.jspecify.annotations.Nullable;

/** Translates CEL comprehensions to Z3, unrolling them up to the comprehension unroll limit. */
@SuppressWarnings({"unchecked", "rawtypes"}) // Z3 Java API uses raw types.
Expand Down Expand Up @@ -273,7 +274,7 @@ private TranslatedValue unrollAllAndExists(
BoolExpr inBounds = ctx.mkLt(idx, lengthExpr);
Expr<?> xVal = ctx.mkNth(seq, idx);

constrainIterationElement(idx, lengthExpr, xVal, inBounds, mapPresence);
constrainIterationElement(idx, lengthExpr, xVal, inBounds, mapPresence, iterRangeTv.z3Expr());

IterationElement iterElem =
getIterationElement(xVal, idx, iterRangeTv.z3Expr(), isMap, isTwoVar);
Expand Down Expand Up @@ -318,7 +319,7 @@ private TranslatedValue unrollMapAndFilter(
BoolExpr inBounds = ctx.mkLt(idx, lengthExpr);
Expr<?> xVal = ctx.mkNth(seq, idx);

constrainIterationElement(idx, lengthExpr, xVal, inBounds, mapPresence);
constrainIterationElement(idx, lengthExpr, xVal, inBounds, mapPresence, iterRangeTv.z3Expr());

Expr<?> currentAccu = chainedAccu;
IterationElement iterElem =
Expand Down Expand Up @@ -377,7 +378,12 @@ private TranslatedValue unrollMapAndFilter(
}

private void constrainIterationElement(
IntExpr idx, ArithExpr lengthExpr, Expr<?> xVal, BoolExpr inBounds, ArrayExpr mapPresence) {
IntExpr idx,
ArithExpr lengthExpr,
Expr<?> xVal,
BoolExpr inBounds,
@Nullable ArrayExpr mapPresence,
Expr<?> iterRange) {
// Constrain out-of-bounds elements to prevent MBQI from infinitely instantiating list/map
// axioms
BoolExpr outOfBounds = ctx.mkGe(idx, lengthExpr);
Expand All @@ -389,6 +395,13 @@ private void constrainIterationElement(
Expr<?> selectExpr = ctx.mkSelect(mapPresence, xVal);
context.addConstraint(ctx.mkImplies(inBounds, (BoolExpr) selectExpr));
}

// Elements of a list, and keys of a map, are CEL values.
context.constrainValidValue(
ctx.mkAnd(
mapPresence == null ? typeSystem.isList(iterRange) : typeSystem.isMap(iterRange),
inBounds),
xVal);
}

private TranslatedValue reduceAllOrExists(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,10 @@ private BoolExpr unrollListEquality(
IntExpr idx = ctx.mkInt(i);
Expr<?> elem0 = ctx.mkNth(seq0, idx);
Expr<?> elem1 = ctx.mkNth(seq1, idx);
context.constrainValidValue(
ctx.mkAnd(typeSystem.isList(listA.z3Expr()), ctx.mkLt(idx, ctx.mkLength(seq0))), elem0);
context.constrainValidValue(
ctx.mkAnd(typeSystem.isList(listB.z3Expr()), ctx.mkLt(idx, ctx.mkLength(seq1))), elem1);

if (i >= comprehensionUnrollLimit) {
constrainListElement(seq0, idx, elem0, type0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ void addConstraint(BoolExpr constraint) {
constraints.add(constraint);
}

/** Constrains {@code value} to be neither an error nor unknown whenever {@code guard} holds. */
/** Constrains {@code value} to be a CEL value whenever {@code guard} holds. */
void constrainValidValue(BoolExpr guard, Expr<?> value) {
addConstraint(ctx.mkImplies(guard, ctx.mkNot(typeSystem.isErrorOrUnknown(value))));
addConstraint(ctx.mkImplies(guard, typeSystem.isValidValue(value)));
}

void addInputVariable(Expr<?> variable, CelType declaredType) {
Expand Down
10 changes: 10 additions & 0 deletions verifier/src/main/java/dev/cel/verifier/CelZ3TypeSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,11 @@ public Expr<?> getOptionalRef(Expr<?> val) {
return ctx.mkApp(optionalCons.getAccessorDecls()[0], val);
}

/** Whether {@code func} is the accessor {@link #getOptionalRef} applies. */
boolean isOptionalRefAccessor(FuncDecl<?> func) {
return func.equals(optionalCons.getAccessorDecls()[0]);
}

/** Checks if the given optional reference contains a value. */
public BoolExpr optHasValue(Expr<?> optRef) {
return ctx.mkNot(ctx.mkEq(optRef, mkNoneOptionalRef()));
Expand Down Expand Up @@ -819,6 +824,11 @@ BoolExpr isValidScalar(Expr<?> val) {
ctx.mkImplies(isDuration(val), ctx.mkNot(checkDurationOverflow(getDuration(val)))));
}

/** Returns whether {@code val} is a CEL value: neither an error nor unknown, and in range. */
public BoolExpr isValidValue(Expr<?> val) {
return ctx.mkAnd(ctx.mkNot(isErrorOrUnknown(val)), isValidScalar(val));
}

/** Checks if the given arithmetic expression overflows a 64-bit unsigned integer. */
public BoolExpr checkUintOverflow(ArithExpr result) {
return ctx.mkOr(
Expand Down
10 changes: 8 additions & 2 deletions verifier/src/main/java/dev/cel/verifier/axioms/InAxiom.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,14 @@ public final class InAxiom {

BoolExpr containsIntOrUint =
ctx.mkOr(
ctx.mkContains((Expr) seq, (Expr) ctx.mkUnit(typeSystem.wrapInt(intVal))),
ctx.mkContains((Expr) seq, (Expr) ctx.mkUnit(typeSystem.wrapUint(intVal))));
ctx.mkAnd(
ctx.mkNot(typeSystem.checkIntOverflow(intVal)),
ctx.mkContains(
(Expr) seq, (Expr) ctx.mkUnit(typeSystem.wrapInt(intVal)))),
ctx.mkAnd(
ctx.mkNot(typeSystem.checkUintOverflow(intVal)),
ctx.mkContains(
(Expr) seq, (Expr) ctx.mkUnit(typeSystem.wrapUint(intVal)))));

BoolExpr intOrUintInList =
(BoolExpr) ctx.mkITE(isIntOrUintZero, containsAnyZero, containsIntOrUint);
Expand Down
68 changes: 33 additions & 35 deletions verifier/src/test/java/dev/cel/verifier/CelVerifierZ3ImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,23 +226,6 @@ private enum IsSatisfiableTestCase {
// An infinite double has no truncation, and the comparisons never need one.
INFINITE_DOUBLE_ABOVE_INT(
"x < d && d > 1.7976931348623157e308", "d = double\\(\"Infinity\"\\)"),
// Values reached by indexing into a dyn value aren't range constrained.
TIMESTAMP_ABOVE_RANGE_SATISFIABLE(
"type(dyn_var[0]) == google.protobuf.Timestamp"
+ " && dyn_var[0] > google.protobuf.Timestamp{seconds: 253402300799, nanos: 999999999}",
"dyn_var = .*timestamp\\(\\d+\\)"),
TIMESTAMP_BELOW_RANGE_SATISFIABLE(
"type(dyn_var[0]) == google.protobuf.Timestamp"
+ " && dyn_var[0] < google.protobuf.Timestamp{seconds: -62135596800}",
"dyn_var = .*timestamp\\(-\\d+\\)"),
DURATION_ABOVE_RANGE_SATISFIABLE(
"type(dyn_var[0]) == google.protobuf.Duration"
+ " && dyn_var[0] > google.protobuf.Duration{seconds: 315576000000, nanos: 999999999}",
"dyn_var = .*duration\\(\"\\d+s\"\\)"),
DURATION_BELOW_RANGE_SATISFIABLE(
"type(dyn_var[0]) == google.protobuf.Duration && dyn_var[0] <"
+ " google.protobuf.Duration{seconds: -315576000000, nanos: -999999999}",
"dyn_var = .*duration\\(\"-\\d+s\"\\)"),
;

private final String expr;
Expand Down Expand Up @@ -352,21 +335,6 @@ public void isSatisfiable_witnessValueSatisfiesExpressionAndDisplayRoundTrips(
}

private enum InexactWitnessTestCase {
INT_OUT_OF_RANGE("type(dyn_var[0]) == int && dyn_var[0] > 9223372036854775807", "dyn_var"),
UINT_OUT_OF_RANGE("type(dyn_var[0]) == uint && dyn_var[0] > 18446744073709551615u", "dyn_var"),
NEGATIVE_UINT("type(dyn_var[0]) == uint && dyn_var[0] < 0u", "dyn_var"),
TIMESTAMP_OUT_OF_RANGE(
"type(dyn_var[0]) == google.protobuf.Timestamp"
+ " && dyn_var[0] > google.protobuf.Timestamp{seconds: 253402300799, nanos: 999999999}",
"dyn_var"),
DURATION_ABOVE_MAX(
"type(dyn_var[0]) == google.protobuf.Duration"
+ " && dyn_var[0] > google.protobuf.Duration{seconds: 315576000000, nanos: 999999999}",
"dyn_var"),
DURATION_BELOW_MIN(
"type(dyn_var[0]) == google.protobuf.Duration && dyn_var[0] <"
+ " google.protobuf.Duration{seconds: -315576000000, nanos: -999999999}",
"dyn_var"),
SURROGATE_STRING(
"size(string_var) == 1 && string_var > '\\ud7ff' && string_var < '\\ue000'", "string_var"),
NON_ASCII_BYTES("by == b'\\xc3\\xa9'", "by"),
Expand Down Expand Up @@ -706,9 +674,6 @@ private enum IsSatisfiableInconclusiveTestCase {
APPROXIMATED_INT_TO_STRING("string(123) == '123'"),
APPROXIMATED_RANGE("int('123') > 100 && int('123') < 200"),
APPROXIMATED_BRANCHING("int('123') == 123 ? x > 5 : false"),
// An element indexed out of a dyn value may be out of range, unlike the literal list holding
// it.
OUT_OF_RANGE_NESTED_DYN_ELEMENT("type([dyn_var[0]][0]) == uint && [dyn_var[0]][0] < 0u"),
// The exact pass refines its candidate away; its assertions mustn't leak into the next pass.
APPROXIMATED_AFTER_REFINEMENT("(dyn(x) == d && d == 1.5) || int(string_var) == 5"),
APPROXIMATED_STRING_TO_INT_CONSTRAINED("string_var == 'abc' && int(string_var) == 5"),
Expand Down Expand Up @@ -886,6 +851,32 @@ private enum IsUnsatisfiableTestCase {
DYN_INT_OUT_OF_RANGE("type(dyn_var) == int && dyn_var > 9223372036854775807"),
DYN_UINT_OUT_OF_RANGE("type(dyn_var) == uint && dyn_var > 18446744073709551615u"),
DYN_NEGATIVE_UINT("type(dyn_var) == uint && dyn_var < 0u"),
DYN_INDEXED_NEGATIVE_UINT("type(dyn_var[0]) == uint && dyn_var[0] < 0u"),
DYN_INDEXED_INT_OUT_OF_RANGE("type(dyn_var['k']) == int && dyn_var['k'] > 9223372036854775807"),
DYN_OPTIONAL_VALUE_OUT_OF_RANGE("type(dyn_var.value()) == uint && dyn_var.value() < 0u"),
DYN_LIST_COMPREHENSION_ELEMENT_OUT_OF_RANGE(
"size(dyn_list) == 0 && size(dyn_var) == 1"
+ " && (dyn_list + dyn_var).exists(x, type(x) == uint && x < 0u)"),
DYN_LIST_FILTER_ELEMENT_OUT_OF_RANGE(
"size(dyn_list) == 0 && size(dyn_var) == 1"
+ " && size((dyn_list + dyn_var).filter(x, type(x) == uint && x < 0u)) == 1"),
DYN_OPTIONAL_LIST_COMPREHENSION_ELEMENT_OUT_OF_RANGE(
"[?dyn_var].exists(x, type(x) == uint && x < 0u)"),
DYN_MAP_COMPREHENSION_VALUE_OUT_OF_RANGE(
"{?'a': dyn_var}.exists(k, v, type(v) == uint && v < 0u)"),
DYN_OPTIONAL_LIST_ELEMENT_OUT_OF_INT_RANGE(
"d == -9223372036854777856.0 && [?dyn_var.?f] != [d] && ([?dyn_var.?f] + []) == [d]"),
DYN_LIST_EQUALITY_OUT_OF_RANGE_LEFT_ELEMENT(
"d == -9223372036854777856.0 && dyn_var == [d] && !(d in dyn_var)"),
DYN_LIST_EQUALITY_OUT_OF_RANGE_RIGHT_ELEMENT(
"d == -9223372036854777856.0 && [d] == dyn_var && !(d in dyn_var)"),
DYN_IN_LIST_OUT_OF_RANGE_CROSS_TYPE_CANDIDATE(
"size(dyn_var + []) == 1"
+ " && ((-1 in dyn_var && dyn_var != ([-1] + []))"
+ " || (18446744073709551615u in dyn_var"
+ " && dyn_var != ([18446744073709551615u] + [])))"),
DYN_OPTIONAL_OR_VALUE_OUT_OF_RANGE(
"type(dyn_var.orValue(1u)) == uint && dyn_var.orValue(1u) < 0u"),
ANY_NEGATIVE_UINT("type(test_all_types.single_any) == uint && test_all_types.single_any < 0u"),
DYN_TIMESTAMP_OUT_OF_RANGE(
"type(dyn_var) == google.protobuf.Timestamp"
Expand Down Expand Up @@ -2657,6 +2648,13 @@ private enum EquivalenceTestCase {
DOUBLE_NAN_ARITHMETIC("d != d ? d + 1.0 : 0.0", "d != d ? d : 0.0"),
DOUBLE_NAN_INPUT_AND_LITERAL("d != d ? d : 0.0", "d != d ? 0.0 / 0.0 : 0.0"),
DYN_NAN_INPUT_AND_LITERAL("dyn_var != dyn_var ? 0.0 / 0.0 : dyn_var", "dyn_var"),
DYN_INDEXED_NAN_AND_LITERAL("dyn_var[0] != dyn_var[0] ? 0.0 / 0.0 : dyn_var[0]", "dyn_var[0]"),
DYN_OPTIONAL_JSON_NAN_AND_LITERAL(
"google.protobuf.Value{?number_value: dyn_var} != google.protobuf.Value{?number_value:"
+ " dyn_var} ? 0.0 / 0.0 : google.protobuf.Value{?number_value: dyn_var}",
"google.protobuf.Value{?number_value: dyn_var}"),
DYN_OPTIONAL_NAN_AND_LITERAL(
"dyn_var.value() != dyn_var.value() ? 0.0 / 0.0 : dyn_var.value()", "dyn_var.value()"),
ANY_NAN_FIELD_AND_LITERAL(
"test_all_types.single_any != test_all_types.single_any"
+ " ? 0.0 / 0.0 : test_all_types.single_any",
Expand Down
Loading