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
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,6 @@ private TranslatedValue reduceAllOrExists(
List<BoolExpr> hasUnknownList = new ArrayList<>();

List<BoolExpr> hasSafeMatchList = new ArrayList<>();
List<BoolExpr> hasSafeErrorList = new ArrayList<>();
List<BoolExpr> hasSafeUnknownList = new ArrayList<>();
List<BoolExpr> activeTaints = new ArrayList<>();

for (BoundedIteration iter : iterations) {
Expand All @@ -430,12 +428,6 @@ private TranslatedValue reduceAllOrExists(
hasSafeMatchList.add(
CelZ3Exprs.mkAndFlattened(
ctx, isActive, isMatch, CelZ3Exprs.mkNotFlattened(ctx, stepTv.isApproximate())));
hasSafeErrorList.add(
CelZ3Exprs.mkAndFlattened(
ctx, isActive, isE, CelZ3Exprs.mkNotFlattened(ctx, stepTv.isApproximate())));
hasSafeUnknownList.add(
CelZ3Exprs.mkAndFlattened(
ctx, isActive, isU, CelZ3Exprs.mkNotFlattened(ctx, stepTv.isApproximate())));
activeTaints.add(CelZ3Exprs.mkAndFlattened(ctx, isActive, stepTv.isApproximate()));
}

Expand All @@ -446,8 +438,6 @@ private TranslatedValue reduceAllOrExists(
BoolExpr rangeIsUnknown = typeSystem.isUnknown(iterRangeTv.z3Expr());

BoolExpr hasSafeMatch = CelZ3Exprs.mkOrFlattened(ctx, hasSafeMatchList);
BoolExpr hasSafeError = CelZ3Exprs.mkOrFlattened(ctx, hasSafeErrorList);
BoolExpr hasSafeUnknown = CelZ3Exprs.mkOrFlattened(ctx, hasSafeUnknownList);
BoolExpr anyActiveTaint = CelZ3Exprs.mkOrFlattened(ctx, activeTaints);

Expr<?> comprehensionUnknown = mkParameterizedUnknown(compExpr, ast);
Expand All @@ -460,19 +450,19 @@ private TranslatedValue reduceAllOrExists(
.addCase(hasError, typeSystem.mkError())
.build(typeSystem.mkBool(isAll));

BoolExpr baseTaint = CelZ3Exprs.mkOrFlattened(ctx, anyActiveTaint, iterRangeTv.isApproximate());

// Taint identically shadows the value flow's short-circuit control structure
// Only an exact match short-circuits; without one, any approximate step may match at runtime.
// The cases mirror the result's to keep the formula easy for Z3 (some versions give up on maps
// with fewer, equivalent cases).
BoolExpr resultTaint =
(BoolExpr)
CelZ3Exprs.SwitchBuilder.newBuilder(ctx)
.addCase(ctx.mkOr(rangeIsError, rangeIsUnknown), iterRangeTv.isApproximate())
.addCase(hasMatch, ctx.mkOr(iterRangeTv.isApproximate(), ctx.mkNot(hasSafeMatch)))
.addCase(
ctx.mkOr(hasUnknown, isTruncated),
ctx.mkOr(iterRangeTv.isApproximate(), isTruncated, ctx.mkNot(hasSafeUnknown)))
.addCase(hasError, ctx.mkOr(iterRangeTv.isApproximate(), ctx.mkNot(hasSafeError)))
.build(baseTaint);
ctx.mkOr(iterRangeTv.isApproximate(), isTruncated, anyActiveTaint))
.addCase(hasError, ctx.mkOr(iterRangeTv.isApproximate(), anyActiveTaint))
.build(CelZ3Exprs.mkOrFlattened(ctx, anyActiveTaint, iterRangeTv.isApproximate()));

return TranslatedValue.create(result, compExpr, typeSystem, resultTaint);
}
Expand Down
31 changes: 26 additions & 5 deletions verifier/src/test/java/dev/cel/verifier/CelVerifierZ3ImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1416,18 +1416,36 @@ public void isAlwaysTrue_withUnknownIdentifier_evaluatesToUnknown(
assertThat(result.witness().get().variables().get("x").value()).isEmpty();
}

@Test
public void isAlwaysTrue_approximateOperandWithUnknown_inconclusive() throws Exception {
private enum ApproximateBesideUnknownTestCase {
// The truncated `all` may really be false, which absorbs the unknown, so this is always true.
CelAbstractSyntaxTree ast =
CEL.compile("!(int_list.all(x, x > 0) && unknown_var) || int_list.all(x, x > 0)").getAst();
APPROXIMATE_OPERAND(
"!(int_list.all(x, x > 0) && unknown_var) || int_list.all(x, x > 0)",
"lists and maps longer than the unroll limit (5)"),
// int('5') == 5 may really be true, which absorbs the unknown in `exists`.
APPROXIMATE_MATCH(
"int_list == [1, 0] ? int_list.exists(x, x == 1 ? int('5') == 5 : unknown_var == 1) : true",
"calls to `int`");

private final String expr;
private final String expectedSources;

ApproximateBesideUnknownTestCase(String expr, String expectedSources) {
this.expr = expr;
this.expectedSources = expectedSources;
}
}

@Test
public void isAlwaysTrue_approximateBesideUnknown_inconclusive(
@TestParameter ApproximateBesideUnknownTestCase testCase) throws Exception {
CelAbstractSyntaxTree ast = CEL.compile(testCase.expr).getAst();
CelVerifier verifier = withRuntimeOracle(CEL, b -> b.addUnknownIdentifier("unknown_var"));

CelVerificationResult result = verifier.isAlwaysTrue(ast);

assertThat(result.status()).isEqualTo(VerificationStatus.INCONCLUSIVE);
assertThat(result.message())
.contains("doesn't model exactly: lists and maps longer than the unroll limit (5).");
.contains("doesn't model exactly: " + testCase.expectedSources + ".");
}

@Test
Expand Down Expand Up @@ -2090,6 +2108,9 @@ public void isAlwaysTrue_violation_returnsFalse(
}

private enum IsInconclusiveTestCase {
// int('5') == 5 is approximate and may really be true, which absorbs the 1 / 0 error.
APPROXIMATE_MATCH_BEFORE_ERROR(
"int_list == [1, 0] ? int_list.exists(x, x == 1 ? int('5') == 5 : 1 / x == 1) : true"),
// nested_list[5] is past the unroll limit, so it may not be a list.
APPROXIMATE_DYN_RANGE("size(nested_list) == 6 ? dyn(nested_list)[5].all(e, e == e) : true"),
// TODO: Implement RFC 3339 spec in conversion
Expand Down
Loading