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
7 changes: 7 additions & 0 deletions verifier/src/main/java/dev/cel/verifier/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,20 @@ java_library(
"CelVerifierZ3Impl.java",
"CelZ3ComprehensionTranslator.java",
"CelZ3CounterexampleGenerator.java",
"CelZ3Equality.java",
"CelZ3ExtensionalityAxioms.java",
"CelZ3FunctionRegistry.java",
"CelZ3OperatorTranslator.java",
"CelZ3StructTranslator.java",
"CelZ3TranslationContext.java",
"CelZ3TypeConstraintGenerator.java",
"ListValueModel.java",
"MapValueModel.java",
"NumericValueModel.java",
"OptionalValueModel.java",
"RuntimeOracle.java",
"TranslatedValue.java",
"ValueModel.java",
],
compatible_with = [],
tags = [
Expand All @@ -193,6 +199,7 @@ java_library(
"//common/internal:proto_time_utils",
"//common/types",
"//common/types:cel_types",
"//common/types:default_type_provider",
"//common/types:type_providers",
"//common/values",
"//common/values:cel_byte_string",
Expand Down
45 changes: 37 additions & 8 deletions verifier/src/main/java/dev/cel/verifier/CelAstToZ3Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package dev.cel.verifier;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableSet.toImmutableSet;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
Expand All @@ -39,6 +40,9 @@
import dev.cel.common.types.CelKind;
import dev.cel.common.types.CelType;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.common.types.CelTypes;
import dev.cel.common.types.DefaultTypeProvider;
import dev.cel.common.types.StructType;
import dev.cel.common.types.TypeType;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -49,6 +53,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

/**
* Internal translator from CEL AST to Z3 SMT expressions using a 4-valued logic tagged union.
Expand Down Expand Up @@ -139,11 +144,16 @@ TranslatedValue translate(CelAbstractSyntaxTree ast) {

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.
// A present optional holds a CEL value, even when the optional is held by a dyn, and is the
// optional of that value.
for (Expr<?> ref : optRefs) {
context.constrainValidValue(
ctx.mkAnd(typeSystem.isOptional(ref.getArgs()[0]), typeSystem.optHasValue(ref)),
typeSystem.getOptionalValue(ref));
BoolExpr isPresent =
ctx.mkAnd(typeSystem.isOptional(ref.getArgs()[0]), typeSystem.optHasValue(ref));
Expr<?> value = typeSystem.getOptionalValue(ref);
context.constrainValidValue(isPresent, value);
context.addConstraint(
ctx.mkImplies(
isPresent, ctx.mkEq(ctx.mkApp(typeSystem.optionalOfRefFunc(), value), ref)));
}

return result;
Expand Down Expand Up @@ -250,12 +260,21 @@ private TranslatedValue translateIdent(CelExpr celExpr, CelAbstractSyntaxTree as
// Nothing binds `n`, so if it's typed type(T), it names T (e.g. `int`).
if (type instanceof TypeType) {
TypeType typeType = (TypeType) type;
if (typeType.type().kind() != CelKind.DYN) {
if (typeType.type().kind() != CelKind.DYN || n.equals("type")) {
return TranslatedValue.create(
typeSystem.mkString(typeType.containingTypeName()),
typeSystem.mkType(typeType.containingTypeName()),
typeSystem,
/* isApproximate= */ ctx.mkFalse());
}
// `dyn` and variables holding a type check as type(dyn).
if (n.equals("dyn")) {
// The spec says `dyn` has no value at runtime.
return TranslatedValue.create(
ctx.mkConst("!dyn", typeSystem.celValueSort()),
typeSystem,
context.recordApproximation(
"the `dyn` type as a value", /* condition= */ ctx.mkTrue()));
}
}
Expr<?> v = ctx.mkConst(n, typeSystem.celValueSort());
context.addInputVariable(v, type);
Expand Down Expand Up @@ -524,7 +543,7 @@ private static String uninterpretedFunctionSymbol(long exprId, CelAbstractSyntax
*/
private static boolean mayEmbedArgs(String functionName, CelType resultType) {
return (!CelZ3OperatorTranslator.isStaticallyKnown(resultType)
|| !resultType.parameters().isEmpty())
|| (!resultType.parameters().isEmpty() && resultType.kind() != CelKind.TYPE))
&& !functionName.equals(Operator.INDEX.getFunction())
&& !functionName.equals(Operator.CONDITIONAL.getFunction());
}
Expand All @@ -537,7 +556,17 @@ private static boolean mayEmbedArgs(String functionName, CelType resultType) {
CelTypeProvider typeProvider,
boolean parameterizeUnknowns) {
this.ctx = ctx;
this.typeSystem = new CelZ3TypeSystem(ctx, parameterizeUnknowns);
this.typeSystem =
new CelZ3TypeSystem(
ctx,
parameterizeUnknowns,
Stream.concat(
DefaultTypeProvider.getInstance().types().stream(),
typeProvider.types().stream()
.filter(
t -> t instanceof StructType && !CelTypes.isWellKnownType(t.name())))
.map(CelType::name)
.collect(toImmutableSet()));
this.context = new CelZ3TranslationContext(ctx, typeSystem, comprehensionUnrollLimit);
this.typeConstraintGenerator = context.typeConstraintGenerator();
this.operatorTranslator = new CelZ3OperatorTranslator(context, functionRegistry);
Expand Down
33 changes: 23 additions & 10 deletions verifier/src/main/java/dev/cel/verifier/CelVerifierZ3Impl.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import dev.cel.verifier.axioms.CelZ3FunctionAxiom;
import dev.cel.verifier.axioms.CelZ3StandardAxioms;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -436,19 +437,31 @@ private static Status checkSat(Solver solver, CelZ3TypeSystem typeSystem, BoolEx
solver.push();
solver.add(assertions);
Status status = solver.check();
// Each round defines at least one more double, so this terminates. Each round gets the full
// timeout; the rounds are bounded by the doubles in int/double comparisons.
List<BoolExpr> preferences = new ArrayList<>();
// Each round defines at least one more double or prefers one more type value's name, so this
// terminates. Each round gets the full timeout; the rounds are bounded by the doubles in
// int/double comparisons and the values that may be types.
while (status == Status.SATISFIABLE) {
ImmutableList<BoolExpr> definitions =
typeSystem.defineDoublesContradictedBy(solver.getModel());
Model model = solver.getModel();
ImmutableList<BoolExpr> definitions = typeSystem.defineDoublesContradictedBy(model);
if (definitions.isEmpty()) {
break;
ImmutableList<BoolExpr> moreKnownNames = typeSystem.preferKnownTypeNamesOver(model);
if (moreKnownNames.isEmpty()) {
break;
}
preferences.addAll(moreKnownNames);
} else {
solver.pop();
solver.add(definitions.toArray(new BoolExpr[0]));
solver.push();
solver.add(assertions);
}
status = solver.check(preferences.toArray(new BoolExpr[0]));
if (status != Status.SATISFIABLE && !preferences.isEmpty()) {
// No model avoids the unknown names, so they may well be real.
preferences.clear();
status = solver.check();
}
solver.pop();
solver.add(definitions.toArray(new BoolExpr[0]));
solver.push();
solver.add(assertions);
status = solver.check();
}
if (status == Status.UNSATISFIABLE) {
solver.pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.UnsignedLong;
Expand All @@ -30,6 +31,7 @@
import dev.cel.common.internal.ProtoTimeUtils;
import dev.cel.common.values.CelByteString;
import dev.cel.common.values.NullValue;
import dev.cel.runtime.CelAttribute;
import java.math.BigInteger;
import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -126,6 +128,12 @@ private Value extractValue(Expr<?> expr) {
return extractString(args[0], /* isBytes= */ false);
} else if (decl.equals(typeSystem.bytesCons().ConstructorDecl())) {
return extractString(args[0], /* isBytes= */ true);
} else if (decl.equals(typeSystem.typeCons().ConstructorDecl())) {
String name = args[0].getString();
return Value.inexact(
Splitter.on('.').splitToStream(name).allMatch(CelAttribute.Qualifier::isLegalIdentifier)
? name
: "<type " + quote(decodeZ3String(name), /* isBytes= */ false) + ">");
} else if (decl.equals(typeSystem.timestampCons().ConstructorDecl())) {
BigInteger nanos = ((IntNum) args[0]).getBigInteger();
BigInteger nanoAdjustment = nanos.mod(NANOS_PER_SECOND);
Expand Down
Loading
Loading