1414
1515package dev .cel .verifier ;
1616
17+ import static com .google .common .collect .ImmutableList .toImmutableList ;
18+
19+ import com .google .common .collect .ImmutableList ;
1720import com .microsoft .z3 .BoolExpr ;
1821import com .microsoft .z3 .Context ;
1922import com .microsoft .z3 .Expr ;
23+ import com .microsoft .z3 .Model ;
2024import dev .cel .common .types .CelType ;
2125import dev .cel .common .types .SimpleType ;
2226import java .util .ArrayList ;
27+ import java .util .Arrays ;
2328import java .util .Collections ;
2429import java .util .HashMap ;
2530import java .util .LinkedHashMap ;
@@ -41,6 +46,7 @@ final class CelZ3TranslationContext {
4146 private final Set <BoolExpr > constraints ;
4247 private final Map <Expr <?>, CelType > inputVariableTypes ;
4348 private final List <BoolExpr > truncationConditions ;
49+ private final Map <String , List <BoolExpr >> approximationTerms ;
4450 private final Map <String , TranslatedValue > symbolTable ;
4551 private final Map <String , TranslatedValue > localScope ;
4652
@@ -93,11 +99,65 @@ CelType guaranteedType(Expr<?> value) {
9399 * not linked to its keys.
94100 */
95101 BoolExpr isTruncatedMap (Expr <?> val ) {
96- return ctx .mkAnd (
97- typeSystem .isMap (val ),
98- ctx .mkGt (
99- ctx .mkLength (typeSystem .getMapKeys (typeSystem .getMapRef (val ))),
100- ctx .mkInt (comprehensionUnrollLimit )));
102+ return recordUnrollLimitApproximation (
103+ ctx .mkAnd (
104+ typeSystem .isMap (val ),
105+ ctx .mkGt (
106+ ctx .mkLength (typeSystem .getMapKeys (typeSystem .getMapRef (val ))),
107+ ctx .mkInt (comprehensionUnrollLimit ))));
108+ }
109+
110+ /**
111+ * Records {@code description} as a reason results are approximate where {@code condition} holds,
112+ * and returns the term to use for {@code condition}, so that it can be told apart in a taint.
113+ */
114+ BoolExpr recordApproximation (String description , BoolExpr condition ) {
115+ BoolExpr term = condition ;
116+ if (condition .isTrue ()) {
117+ // A literal `true` is shared by every other unconditional approximation.
118+ term = (BoolExpr ) ctx .mkFreshConst ("!approx" , ctx .getBoolSort ());
119+ constraints .add (term );
120+ }
121+ approximationTerms .computeIfAbsent (description , k -> new ArrayList <>()).add (term );
122+ return term ;
123+ }
124+
125+ BoolExpr recordUnrollLimitApproximation (BoolExpr condition ) {
126+ return recordApproximation (
127+ "lists and maps longer than the unroll limit (" + comprehensionUnrollLimit + ")" ,
128+ condition );
129+ }
130+
131+ BoolExpr recordCallApproximation (String functionName , BoolExpr condition ) {
132+ return recordApproximation ("calls to `" + functionName + "`" , condition );
133+ }
134+
135+ /**
136+ * Returns the descriptions of the approximations that are enough on their own to make {@code
137+ * taint} hold in {@code model}, which leaves out those in short-circuited operands or untaken
138+ * branches.
139+ */
140+ ImmutableList <String > approximationSources (Model model , BoolExpr taint ) {
141+ return approximationTerms .keySet ().stream ()
142+ .filter (description -> holds (model , withoutApproximation (taint , termsExcept (description ))))
143+ .collect (toImmutableList ());
144+ }
145+
146+ private ImmutableList <BoolExpr > termsExcept (String description ) {
147+ return approximationTerms .entrySet ().stream ()
148+ .filter (source -> !source .getKey ().equals (description ))
149+ .flatMap (source -> source .getValue ().stream ())
150+ .collect (toImmutableList ());
151+ }
152+
153+ BoolExpr withoutApproximation (BoolExpr expr , List <BoolExpr > terms ) {
154+ BoolExpr [] falses = new BoolExpr [terms .size ()];
155+ Arrays .fill (falses , ctx .mkFalse ());
156+ return (BoolExpr ) expr .substitute (terms .toArray (new BoolExpr [0 ]), falses );
157+ }
158+
159+ private static boolean holds (Model model , BoolExpr condition ) {
160+ return model .evaluate (condition , /* completion= */ true ).isTrue ();
101161 }
102162
103163 void addTruncationCondition (BoolExpr condition ) {
@@ -148,6 +208,7 @@ <T> T withScope(String varName, TranslatedValue value, Supplier<T> action) {
148208 this .constraints = new LinkedHashSet <>();
149209 this .inputVariableTypes = new LinkedHashMap <>();
150210 this .truncationConditions = new ArrayList <>();
211+ this .approximationTerms = new LinkedHashMap <>();
151212 this .symbolTable = new HashMap <>();
152213 this .localScope = new HashMap <>();
153214 }
0 commit comments