|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dev.cel.verifier; |
| 16 | + |
| 17 | +import com.google.common.base.Preconditions; |
| 18 | +import com.google.common.collect.ImmutableMap; |
| 19 | +import com.google.errorprone.annotations.Immutable; |
| 20 | +import dev.cel.bundle.Cel; |
| 21 | +import dev.cel.common.CelAbstractSyntaxTree; |
| 22 | +import dev.cel.runtime.CelEvaluationException; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Objects; |
| 26 | +import java.util.Optional; |
| 27 | + |
| 28 | +/** |
| 29 | + * Evaluates candidate counterexample models against the concrete CEL runtime to confirm or refute |
| 30 | + * potential violations (CEGAR refinement loop). |
| 31 | + */ |
| 32 | +@Immutable |
| 33 | +final class CegarRefiner { |
| 34 | + |
| 35 | + @Immutable |
| 36 | + static final class CegarOutcome { |
| 37 | + private final boolean isViolation; |
| 38 | + private final Optional<String> evaluationErrorMessage; |
| 39 | + |
| 40 | + static CegarOutcome violation() { |
| 41 | + return new CegarOutcome(true, Optional.empty()); |
| 42 | + } |
| 43 | + |
| 44 | + static CegarOutcome spurious() { |
| 45 | + return new CegarOutcome(false, Optional.empty()); |
| 46 | + } |
| 47 | + |
| 48 | + static CegarOutcome evaluationError(String errorMessage) { |
| 49 | + return new CegarOutcome(false, Optional.of(errorMessage)); |
| 50 | + } |
| 51 | + |
| 52 | + private CegarOutcome(boolean isViolation, Optional<String> evaluationErrorMessage) { |
| 53 | + this.isViolation = isViolation; |
| 54 | + this.evaluationErrorMessage = evaluationErrorMessage; |
| 55 | + } |
| 56 | + |
| 57 | + boolean isViolation() { |
| 58 | + return isViolation; |
| 59 | + } |
| 60 | + |
| 61 | + Optional<String> evaluationErrorMessage() { |
| 62 | + return evaluationErrorMessage; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private final Cel cel; |
| 67 | + |
| 68 | + CegarRefiner(Cel cel) { |
| 69 | + this.cel = Preconditions.checkNotNull(cel); |
| 70 | + } |
| 71 | + |
| 72 | + CegarOutcome refineEquivalence( |
| 73 | + CelAbstractSyntaxTree astA, CelAbstractSyntaxTree astB, CelCounterexample model) { |
| 74 | + if (model.isSatisfyingInput()) { |
| 75 | + return CegarOutcome.spurious(); |
| 76 | + } |
| 77 | + try { |
| 78 | + ImmutableMap<String, Object> evalContext = model.toEvaluationContext(); |
| 79 | + Object resA = cel.createProgram(astA).eval(evalContext); |
| 80 | + Object resB = cel.createProgram(astB).eval(evalContext); |
| 81 | + // If concrete evaluation produces identical results, the candidate SMT divergence was an |
| 82 | + // artifact of abstraction (spurious). Otherwise, concrete outputs diverge (violation). |
| 83 | + return Objects.equals(resA, resB) ? CegarOutcome.spurious() : CegarOutcome.violation(); |
| 84 | + } catch (CelEvaluationException e) { |
| 85 | + return CegarOutcome.evaluationError(e.getMessage()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + CegarOutcome refineSatisfiability( |
| 90 | + CelAbstractSyntaxTree ast, boolean searchForCounterexample, CelCounterexample model) { |
| 91 | + if (searchForCounterexample ? model.isSatisfyingInput() : !model.isSatisfyingInput()) { |
| 92 | + return CegarOutcome.spurious(); |
| 93 | + } |
| 94 | + try { |
| 95 | + ImmutableMap<String, Object> evalContext = model.toEvaluationContext(); |
| 96 | + Object res = cel.createProgram(ast).eval(evalContext); |
| 97 | + boolean isEvaluationTrue = Objects.equals(res, true); |
| 98 | + // For universal truth (searchForCounterexample=true), evaluating to true refutes the |
| 99 | + // candidate counterexample (spurious). For satisfiability search |
| 100 | + // (searchForCounterexample=false), |
| 101 | + // evaluating to true confirms the candidate satisfying model (violation). |
| 102 | + boolean isSpurious = searchForCounterexample == isEvaluationTrue; |
| 103 | + return isSpurious ? CegarOutcome.spurious() : CegarOutcome.violation(); |
| 104 | + } catch (CelEvaluationException e) { |
| 105 | + return CegarOutcome.evaluationError(e.getMessage()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + CegarOutcome refineImplication( |
| 110 | + CelAbstractSyntaxTree assumeAst, |
| 111 | + CelAbstractSyntaxTree assertAst, |
| 112 | + Map<String, CelAbstractSyntaxTree> boundSymbols, |
| 113 | + CelCounterexample model) { |
| 114 | + if (model.isSatisfyingInput()) { |
| 115 | + return CegarOutcome.spurious(); |
| 116 | + } |
| 117 | + try { |
| 118 | + Map<String, Object> evalContext = new HashMap<>(model.toEvaluationContext()); |
| 119 | + for (Map.Entry<String, CelAbstractSyntaxTree> entry : boundSymbols.entrySet()) { |
| 120 | + Object boundVal = cel.createProgram(entry.getValue()).eval(evalContext); |
| 121 | + evalContext.put(entry.getKey(), boundVal); |
| 122 | + } |
| 123 | + Object assumeVal = cel.createProgram(assumeAst).eval(evalContext); |
| 124 | + if (Objects.equals(assumeVal, true)) { |
| 125 | + Object assertVal = cel.createProgram(assertAst).eval(evalContext); |
| 126 | + // If the premise holds and the conclusion evaluates to true, the candidate counterexample |
| 127 | + // is refuted (spurious). If the conclusion fails under true premise, implication is |
| 128 | + // violated. |
| 129 | + return Objects.equals(assertVal, true) ? CegarOutcome.spurious() : CegarOutcome.violation(); |
| 130 | + } |
| 131 | + // The candidate input did not satisfy the premise, so it cannot serve as a counterexample. |
| 132 | + return CegarOutcome.spurious(); |
| 133 | + } catch (CelEvaluationException e) { |
| 134 | + return CegarOutcome.evaluationError(e.getMessage()); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments