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
12 changes: 12 additions & 0 deletions verifier/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,18 @@ by the Z3 solver during its search phase.
If the solver times out, the verifier will throw a `CelVerificationException`
which should be explicitly caught and handled by the caller.

### Validating Counterexamples with the CEL Runtime

`enableCegar(true)` checks the inputs the solver finds with the CEL runtime of
the verifier's environment: a confirmed `INCONCLUSIVE` counterexample (or
satisfying input) becomes `VIOLATED` (or `VERIFIED`), and any refuted one becomes
`INCONCLUSIVE`. Only exact inputs are checked, when no unknown identifiers are
registered; when the runtime can't evaluate them (e.g. a function has no
binding), the message says why. The runtime must follow the CEL spec (e.g.
`CelOptions.DEFAULT`, which has no iteration limit), and a function with
bindings must have one for each overload, which is synchronous, deterministic
and doesn't throw.

### Comprehensions and Bounded Model Checking

Because CEL lists/maps can be dynamically sized, the verifier cannot
Expand Down
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 @@ -170,6 +170,7 @@ java_library(
"CelZ3StructTranslator.java",
"CelZ3TranslationContext.java",
"CelZ3TypeConstraintGenerator.java",
"RuntimeOracle.java",
"TranslatedValue.java",
],
compatible_with = [],
Expand All @@ -184,9 +185,11 @@ java_library(
"//bundle:cel",
"//common:cel_ast",
"//common:compiler_common",
"//common:error_codes",
"//common:operator",
"//common/ast",
"//common/ast:cel_block",
"//common/exceptions:runtime_exception",
"//common/internal:cel_descriptor_pools",
"//common/internal:proto_time_utils",
"//common/internal:well_known_proto",
Expand All @@ -198,6 +201,10 @@ java_library(
"//optimizer",
"//optimizer:optimization_exception",
"//optimizer:optimizer_builder",
"//runtime",
"//runtime:function_resolver",
"//runtime:resolved_overload",
"//runtime:unknown_attributes",
"//verifier/axioms",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
Expand Down
86 changes: 48 additions & 38 deletions verifier/src/main/java/dev/cel/verifier/CelAstToZ3Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package dev.cel.verifier;

import static com.google.common.base.Preconditions.checkArgument;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
Expand Down Expand Up @@ -111,8 +113,8 @@ Set<Expr<?>> getInputVariables() {
* Binds an identifier name in the symbol table to an already-translated value. Used to bind
* reserved policy symbols such as 'rule.result' to the composed policy graph.
*/
void bindSymbol(String varName, TranslatedValue value) {
context.bindSymbol(varName, value);
void bindSymbol(String varName, TranslatedValue value, CelAbstractSyntaxTree ast) {
context.bindSymbol(varName, value, ast);
}

TranslatedValue translate(CelAbstractSyntaxTree ast) {
Expand Down Expand Up @@ -439,6 +441,15 @@ private TranslatedValue translateCall(CelExpr expr, CelAbstractSyntaxTree ast) {
long exprId = expr.id();
String functionName = call.function();
ImmutableList<CelExpr> argsAst = call.args();
if (functionName.equals(Operator.OPTIONAL_SELECT.getFunction())) {
checkArgument(
!call.target().isPresent()
&& argsAst.size() == 2
&& argsAst.get(1).constantOrDefault().getKind() == CelConstant.Kind.STRING_VALUE,
"Malformed AST: operator %s expects an operand and a field name",
Operator.OPTIONAL_SELECT);
return structTranslator.translateSelect(expr, ast);
}

List<TranslatedValue> args = new ArrayList<>();

Expand All @@ -453,42 +464,41 @@ private TranslatedValue translateCall(CelExpr expr, CelAbstractSyntaxTree ast) {

return operatorTranslator
.translateFunctionCall(functionName, args, exprId, ast)
.map(
tv -> {
if (mayEmbedArgs(functionName, ast.getTypeOrThrow(exprId))) {
propagateTruncatedTypeConstraint(
tv.z3Expr(), Iterables.transform(args, TranslatedValue::z3Expr));
}
return TranslatedValue.create(tv.z3Expr(), expr, typeSystem, tv.isApproximate());
})
.orElseGet(
() -> {
// Uninterpreted function
Sort[] argSorts = new Sort[args.size()];
Arrays.fill(argSorts, 0, args.size(), typeSystem.celValueSort());
FuncDecl<?> funcDecl =
typeSystem.internFuncDecl(
uninterpretedFunctionSymbol(exprId, ast),
argSorts,
typeSystem.celValueSort());

Expr<?>[] exprArgs = args.stream().map(TranslatedValue::z3Expr).toArray(Expr[]::new);
Expr<?> callRes = ctx.mkApp(funcDecl, exprArgs);
context.addConstraint(
typeConstraintGenerator.createTypeConstraint(callRes, exprId, ast));
context.addConstraint(ctx.mkNot(typeSystem.isUnknown(callRes)));
context.addConstraint(ctx.mkNot(typeSystem.isError(callRes)));

// The solver picks the result freely, so a model depending on it is never exact.
return TranslatedValue.propagateStrict(
ctx,
typeSystem,
functionName,
callRes,
Optional.of(expr),
/* baseTaint= */ context.recordCallApproximation(functionName, ctx.mkTrue()),
args);
});
.map(tv -> {
if (mayEmbedArgs(functionName, ast.getTypeOrThrow(exprId))) {
propagateTruncatedTypeConstraint(
tv.z3Expr(), Iterables.transform(args, TranslatedValue::z3Expr));
}
return TranslatedValue.create(tv.z3Expr(), expr, typeSystem, tv.isApproximate());
})
.orElseGet(() -> {
// Uninterpreted function
Sort[] argSorts = new Sort[args.size()];
Arrays.fill(argSorts, 0, args.size(), typeSystem.celValueSort());
FuncDecl<?> funcDecl =
typeSystem.internFuncDecl(
uninterpretedFunctionSymbol(exprId, ast), argSorts, typeSystem.celValueSort());

Expr<?>[] exprArgs = args.stream().map(TranslatedValue::z3Expr).toArray(Expr[]::new);
Expr<?> callRes = ctx.mkApp(funcDecl, exprArgs);
// A narrowed result's static type doesn't constrain it.
if (!context.mayBeNarrowed(expr, ast)) {
context.addConstraint(
typeConstraintGenerator.createTypeConstraint(callRes, exprId, ast));
}
context.addConstraint(ctx.mkNot(typeSystem.isUnknown(callRes)));
context.addConstraint(ctx.mkNot(typeSystem.isError(callRes)));

// The solver picks the result freely, so a model depending on it is never exact.
return TranslatedValue.propagateStrict(
ctx,
typeSystem,
functionName,
callRes,
Optional.of(expr),
/* baseTaint= */ context.recordCallApproximation(functionName, ctx.mkTrue()),
args);
});
}

/**
Expand Down
17 changes: 17 additions & 0 deletions verifier/src/main/java/dev/cel/verifier/CelVerifierBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ public interface CelVerifierBuilder {
@CanIgnoreReturnValue
CelVerifierBuilder setComprehensionUnrollLimit(int unrollLimit);

/**
* Checks the inputs the solver finds by evaluating them with the CEL runtime of the verifier's
* environment (off by default). For now this only validates them, without refining the solver
* (counterexample-guided abstraction refinement, CEGAR). An {@code INCONCLUSIVE} counterexample
* or satisfying input that the runtime confirms becomes {@code VIOLATED} (or {@code VERIFIED} for
* satisfiability), and any counterexample or satisfying input it disagrees with becomes {@code
* INCONCLUSIVE}.
*
* <p>Inputs are checked only when all their values are exact and no unknown identifiers are
* registered. When the runtime can't evaluate them, e.g. a function has no binding, the message
* says why. The runtime must follow the CEL spec (e.g. {@code CelOptions.DEFAULT}, which has no
* iteration limit), and a function with bindings must have one for each overload, which is
* synchronous, deterministic and doesn't throw.
*/
@CanIgnoreReturnValue
CelVerifierBuilder enableCegar(boolean value);

/** Builds the {@link CelVerifier} instance. */
CelVerifier build();
}
Expand Down
Loading
Loading