Skip to content

Commit 28ecf79

Browse files
l46kokcopybara-github
authored andcommitted
Scope policy variables globally and trust checked element types in the verifier
PiperOrigin-RevId: 1021348550
1 parent bbbe626 commit 28ecf79

7 files changed

Lines changed: 183 additions & 90 deletions

File tree

‎verifier/src/main/java/dev/cel/verifier/CelAstToZ3Translator.java‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,9 @@ Set<Expr<?>> getInputVariables() {
109109
return context.inputVariables();
110110
}
111111

112-
/**
113-
* Binds an identifier name in the symbol table to an already-translated value. Used to bind
114-
* reserved policy symbols such as 'rule.result' to the composed policy graph.
115-
*/
116-
void bindSymbol(String varName, TranslatedValue value) {
117-
context.bindSymbol(varName, value);
112+
/** Binds the global symbol {@code name} to the result of {@code ast}. */
113+
void bindSymbol(String name, CelAbstractSyntaxTree ast) {
114+
context.bindSymbol(name, translate(ast), ast);
118115
}
119116

120117
TranslatedValue translate(CelAbstractSyntaxTree ast) {
@@ -243,7 +240,7 @@ private TranslatedValue translateIdent(CelExpr celExpr, CelAbstractSyntaxTree as
243240
}
244241
String name = ident.name();
245242
CelType type = ast.getTypeOrThrow(exprId);
246-
if (type instanceof TypeType) {
243+
if (type instanceof TypeType && !context.isBound(name)) {
247244
TypeType typeType = (TypeType) type;
248245
if (typeType.type().kind() != CelKind.DYN) {
249246
return TranslatedValue.create(

‎verifier/src/main/java/dev/cel/verifier/CelVerifierZ3Impl.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,7 @@ CelVerificationResult verifyImplication(
265265
try (Context ctx = new Context(ImmutableMap.of("model", "true"))) {
266266
CelAstToZ3Translator translator = newTranslator(ctx, /* parameterizeUnknowns= */ false);
267267

268-
for (Map.Entry<String, CelAbstractSyntaxTree> entry : boundSymbols.entrySet()) {
269-
TranslatedValue tv = translator.translate(entry.getValue());
270-
translator.bindSymbol(entry.getKey(), tv);
271-
}
268+
boundSymbols.forEach(translator::bindSymbol);
272269

273270
TranslatedValue assumeTv = translator.translate(assumeAst);
274271
TranslatedValue assertTv = translator.translate(assertAst);

‎verifier/src/main/java/dev/cel/verifier/CelZ3ComprehensionTranslator.java‎

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import dev.cel.common.types.SimpleType;
3535
import java.util.ArrayList;
3636
import java.util.List;
37+
import java.util.Optional;
3738
import java.util.function.BiFunction;
39+
import java.util.function.Function;
3840
import java.util.function.Supplier;
3941
import org.jspecify.annotations.Nullable;
4042

@@ -50,14 +52,16 @@ final class CelZ3ComprehensionTranslator {
5052

5153
TranslatedValue translateComprehension(CelExpr celExpr, CelAbstractSyntaxTree ast) {
5254
CelComprehension comp = celExpr.comprehension();
53-
CelExpr iterRangeExpr = comp.iterRange();
54-
if (iterRangeExpr.exprKind().getKind() == ExprKind.Kind.IDENT) {
55-
iterRangeExpr =
56-
context
57-
.lookupSymbol(iterRangeExpr.ident().name())
58-
.flatMap(TranslatedValue::celExpr)
59-
.orElse(iterRangeExpr);
60-
}
55+
CelExpr iterRange = comp.iterRange();
56+
Optional<CelAbstractSyntaxTree> symbolAst =
57+
iterRange.exprKind().getKind() == ExprKind.Kind.IDENT
58+
? context.symbolAst(iterRange.ident().name())
59+
: Optional.empty();
60+
CelExpr iterRangeExpr = symbolAst.map(CelAbstractSyntaxTree::getExpr).orElse(iterRange);
61+
Function<CelExpr, TranslatedValue> translateRangeExpr =
62+
symbolAst.isPresent()
63+
? e -> context.withoutLocals(() -> translateExpr(e, symbolAst.get()))
64+
: e -> translateExpr(e, ast);
6165
List<IterationElement> iterationElements = new ArrayList<>();
6266
List<BoolExpr> taints = new ArrayList<>();
6367
List<Expr<?>> allRangeElems = new ArrayList<>();
@@ -67,7 +71,7 @@ TranslatedValue translateComprehension(CelExpr celExpr, CelAbstractSyntaxTree as
6771
&& iterRangeExpr.list().optionalIndices().isEmpty()) {
6872
ImmutableList<CelExpr> elements = iterRangeExpr.list().elements();
6973
for (int i = 0; i < elements.size(); i++) {
70-
TranslatedValue valueTv = translateExpr(elements.get(i), ast);
74+
TranslatedValue valueTv = translateRangeExpr.apply(elements.get(i));
7175
Expr<?> value = valueTv.z3Expr();
7276
taints.add(valueTv.isApproximate());
7377
iterationElements.add(new IterationElement(typeSystem.mkInt(i), value));
@@ -76,10 +80,10 @@ TranslatedValue translateComprehension(CelExpr celExpr, CelAbstractSyntaxTree as
7680
} else if (iterRangeExpr.exprKind().getKind() == ExprKind.Kind.MAP
7781
&& iterRangeExpr.map().entries().stream().noneMatch(CelExpr.CelMap.Entry::optionalEntry)) {
7882
for (CelExpr.CelMap.Entry entry : iterRangeExpr.map().entries()) {
79-
TranslatedValue keyTv = translateExpr(entry.key(), ast);
83+
TranslatedValue keyTv = translateRangeExpr.apply(entry.key());
8084
Expr<?> key = keyTv.z3Expr();
8185
taints.add(keyTv.isApproximate());
82-
TranslatedValue valueTv = translateExpr(entry.value(), ast);
86+
TranslatedValue valueTv = translateRangeExpr.apply(entry.value());
8387
Expr<?> value = valueTv.z3Expr();
8488
taints.add(valueTv.isApproximate());
8589
iterationElements.add(new IterationElement(key, value));
@@ -182,18 +186,34 @@ private TranslatedValue translateDynamicComprehension(
182186
isMap ? (ArrayExpr) typeSystem.getMapPresence(typeSystem.getMapRef(iterRange)) : null;
183187

184188
BoolExpr isValidRange = isMap ? typeSystem.isMap(iterRange) : typeSystem.isList(iterRange);
189+
int unrollLimit = Math.max(comprehensionUnrollLimit, boundLiteralSize(comp.iterRange()));
185190
BoolExpr isTruncated =
186191
context.recordUnrollLimitApproximation(
187-
ctx.mkAnd(isValidRange, ctx.mkGt(lengthExpr, ctx.mkInt(comprehensionUnrollLimit))));
192+
ctx.mkAnd(isValidRange, ctx.mkGt(lengthExpr, ctx.mkInt(unrollLimit))));
188193
context.addUnmodeledResult(isTruncated);
189194

190195
if (isAllMacro(comp) || isExistsMacro(comp)) {
191196
return unrollAllAndExists(
192-
celExpr, ast, seq, lengthExpr, mapPresence, isTruncated, iterRangeTv);
197+
celExpr, ast, seq, lengthExpr, mapPresence, isTruncated, iterRangeTv, unrollLimit);
193198
} else {
194199
return unrollMapAndFilter(
195-
celExpr, ast, seq, lengthExpr, mapPresence, isTruncated, iterRangeTv);
200+
celExpr, ast, seq, lengthExpr, mapPresence, isTruncated, iterRangeTv, unrollLimit);
201+
}
202+
}
203+
204+
/** The size of the literal a local such as a {@code cel.@block} index is bound to, or 0. */
205+
private int boundLiteralSize(CelExpr iterRange) {
206+
if (iterRange.exprKind().getKind() != ExprKind.Kind.IDENT) {
207+
return 0;
196208
}
209+
return context
210+
.local(iterRange.ident().name())
211+
.flatMap(TranslatedValue::celExpr)
212+
.map(e ->
213+
e.exprKind().getKind() == ExprKind.Kind.LIST
214+
? e.list().elements().size()
215+
: e.exprKind().getKind() == ExprKind.Kind.MAP ? e.map().entries().size() : 0)
216+
.orElse(0);
197217
}
198218

199219
private <T> T withIterationScope(
@@ -260,7 +280,8 @@ private TranslatedValue unrollAllAndExists(
260280
ArithExpr lengthExpr,
261281
ArrayExpr mapPresence,
262282
BoolExpr isTruncated,
263-
TranslatedValue iterRangeTv) {
283+
TranslatedValue iterRangeTv,
284+
int unrollLimit) {
264285
CelComprehension comp = celExpr.comprehension();
265286
List<BoundedIteration> iterations = new ArrayList<>();
266287
TranslatedValue accuInitTv = translateExpr(comp.accuInit(), ast);
@@ -269,7 +290,7 @@ private TranslatedValue unrollAllAndExists(
269290
boolean isMap = mapPresence != null;
270291
boolean isTwoVar = !comp.iterVar2().isEmpty();
271292

272-
for (int i = 0; i < comprehensionUnrollLimit; i++) {
293+
for (int i = 0; i < unrollLimit; i++) {
273294
IntExpr idx = ctx.mkInt(i);
274295
BoolExpr inBounds = ctx.mkLt(idx, lengthExpr);
275296
Expr<?> xVal = ctx.mkNth(seq, idx);
@@ -301,7 +322,8 @@ private TranslatedValue unrollMapAndFilter(
301322
ArithExpr lengthExpr,
302323
ArrayExpr mapPresence,
303324
BoolExpr isTruncated,
304-
TranslatedValue iterRangeTv) {
325+
TranslatedValue iterRangeTv,
326+
int unrollLimit) {
305327
CelComprehension comp = celExpr.comprehension();
306328
// For macros like map and filter, we must sequentially thread the accumulator through the loop
307329
TranslatedValue chainedAccuTv = translateExpr(comp.accuInit(), ast);
@@ -314,7 +336,7 @@ private TranslatedValue unrollMapAndFilter(
314336
taints.add(chainedAccuTv.isApproximate());
315337
taints.add(iterRangeTv.isApproximate());
316338

317-
for (int i = 0; i < comprehensionUnrollLimit; i++) {
339+
for (int i = 0; i < unrollLimit; i++) {
318340
IntExpr idx = ctx.mkInt(i);
319341
BoolExpr inBounds = ctx.mkLt(idx, lengthExpr);
320342
Expr<?> xVal = ctx.mkNth(seq, idx);

‎verifier/src/main/java/dev/cel/verifier/CelZ3OperatorTranslator.java‎

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -864,9 +864,6 @@ private BoolExpr listMembership(
864864
// Whether lhs may equal an element of elemType: a number only across numeric types, and a list
865865
// only another list (anything else conservatively).
866866
private BoolExpr mayEqualElement(Expr<?> lhs, CelType elemType) {
867-
if (elemType instanceof NullableType) {
868-
return ctx.mkTrue();
869-
}
870867
if (isNumericType(elemType)) {
871868
return isNumeric(lhs);
872869
}
@@ -989,8 +986,6 @@ private TranslatedValue translateEquality(
989986
structuralEq = (BoolExpr) ctx.mkITE(bothMaps, mapEquality, structuralEq);
990987
} else if (!isStaticallyKnown(type0) && !isStaticallyKnown(type1)) {
991988
// Performance only: a side with a known type isn't a list or map here.
992-
// TODO: Unless the type checker narrowed it, e.g. `(int_list +
993-
// dyn_var)[0]`.
994989
structuralEq =
995990
approximateEqualityWhere(
996991
context.recordApproximation(
@@ -1027,13 +1022,11 @@ private static final class IndexAccess {
10271022
private final BoolExpr valid;
10281023
private final BoolExpr found;
10291024
private final Expr<?> value;
1030-
private final BoolExpr isApproximate;
10311025

1032-
private IndexAccess(BoolExpr valid, BoolExpr found, Expr<?> value, BoolExpr isApproximate) {
1026+
private IndexAccess(BoolExpr valid, BoolExpr found, Expr<?> value) {
10331027
this.valid = valid;
10341028
this.found = found;
10351029
this.value = value;
1036-
this.isApproximate = isApproximate;
10371030
}
10381031
}
10391032

@@ -1043,54 +1036,40 @@ private IndexAccess listAccess(Expr<?> lhsTrans, Expr<?> rhsTrans) {
10431036
return new IndexAccess(
10441037
ctx.mkAnd(typeSystem.isList(lhsTrans), typeSystem.isInt(rhsTrans)),
10451038
ctx.mkAnd(ctx.mkGe(index, ctx.mkInt(0)), ctx.mkLt(index, ctx.mkLength(seq))),
1046-
ctx.mkNth(seq, index),
1047-
/* isApproximate= */ ctx.mkFalse());
1039+
ctx.mkNth(seq, index));
10481040
}
10491041

10501042
private IndexAccess mapAccess(Expr<?> lhsTrans, Expr<?> rhsTrans) {
10511043
ImmutableList<CelZ3TypeSystem.EqualKey> equalKeys = typeSystem.equalKeys(rhsTrans);
10521044
return new IndexAccess(
10531045
typeSystem.isMap(lhsTrans),
10541046
typeSystem.hasEqualKey(lhsTrans, equalKeys),
1055-
typeSystem.valueAtEqualKey(lhsTrans, equalKeys),
1056-
/* isApproximate= */ ctx.mkFalse());
1047+
typeSystem.valueAtEqualKey(lhsTrans, equalKeys));
10571048
}
10581049

10591050
/**
1060-
* Looks up {@code rhsTrans} in the list or map {@code lhsTrans}. The value is approximate if the
1061-
* static element type isn't guaranteed and it may not conform.
1051+
* Looks up {@code rhsTrans} in the list or map {@code lhsTrans} of type {@code containerType}.
10621052
*/
1063-
private IndexAccess indexAccess(
1064-
Expr<?> lhsTrans, Expr<?> rhsTrans, CelType containerType, boolean isLhsTypeGuaranteed) {
1053+
private IndexAccess indexAccess(Expr<?> lhsTrans, Expr<?> rhsTrans, CelType containerType) {
10651054
IndexAccess list = listAccess(lhsTrans, rhsTrans);
10661055
IndexAccess map = mapAccess(lhsTrans, rhsTrans);
10671056
IndexAccess access =
10681057
new IndexAccess(
10691058
ctx.mkOr(list.valid, map.valid),
10701059
(BoolExpr) ctx.mkITE(list.valid, list.found, map.found),
1071-
ctx.mkITE(list.valid, list.value, map.value),
1072-
/* isApproximate= */ ctx.mkFalse());
1060+
ctx.mkITE(list.valid, list.value, map.value));
10731061
BoolExpr isRead = ctx.mkAnd(access.valid, access.found);
10741062
context.constrainValidValue(isRead, access.value);
1075-
if (containerType.kind() != CelKind.MAP && containerType.kind() != CelKind.LIST) {
1076-
return access;
1077-
}
1078-
CelType elemType =
1079-
containerType.kind() == CelKind.MAP
1080-
? ((MapType) containerType).valueType()
1081-
: ((ListType) containerType).elemType();
1082-
BoolExpr conforms = typeConstraintGenerator.createTypeConstraintForType(access.value, elemType);
1083-
if (isLhsTypeGuaranteed) {
1084-
context.addConstraint(ctx.mkImplies(isRead, conforms));
1085-
return access;
1063+
if (containerType.kind() == CelKind.MAP || containerType.kind() == CelKind.LIST) {
1064+
CelType elemType =
1065+
containerType.kind() == CelKind.MAP
1066+
? ((MapType) containerType).valueType()
1067+
: ((ListType) containerType).elemType();
1068+
context.addConstraint(
1069+
ctx.mkImplies(
1070+
isRead, typeConstraintGenerator.createTypeConstraintForType(access.value, elemType)));
10861071
}
1087-
// The type checker may narrow other static types (e.g. `(['a'] + dyn_var)[1]` is typed
1088-
// string), so the element may not conform.
1089-
return new IndexAccess(
1090-
access.valid,
1091-
access.found,
1092-
access.value,
1093-
CelZ3Exprs.mkAndFlattened(ctx, isRead, CelZ3Exprs.mkNotFlattened(ctx, conforms)));
1072+
return access;
10941073
}
10951074

10961075
/**
@@ -1102,7 +1081,6 @@ private TranslatedValue translateIndex(
11021081
TranslatedValue lhs = args.get(0);
11031082
TranslatedValue rhs = args.get(1);
11041083
CelType lhsType = extractAstTypeOrDefault(lhs, ast);
1105-
boolean isLhsTypeGuaranteed = lhsType.equals(guaranteedType(lhs, ast));
11061084
CelType containerType = lhsType instanceof OptionalType ? lhsType.parameters().get(0) : lhsType;
11071085

11081086
Expr<?> lhsTrans = lhs.z3Expr();
@@ -1112,7 +1090,7 @@ private TranslatedValue translateIndex(
11121090
BoolExpr lhsHasValue = typeSystem.optHasValue(optRef);
11131091
lhsTrans = ctx.mkITE(isOptionalLhs, typeSystem.getOptionalValue(optRef), lhsTrans);
11141092
// An empty optional's value can't be read, so constraining it is harmless.
1115-
IndexAccess access = indexAccess(lhsTrans, rhsTrans, containerType, isLhsTypeGuaranteed);
1093+
IndexAccess access = indexAccess(lhsTrans, rhsTrans, containerType);
11161094
Expr<?> element =
11171095
ctx.mkITE(ctx.mkAnd(access.valid, access.found), access.value, typeSystem.mkError());
11181096
Expr<?> optionalElement =
@@ -1139,8 +1117,7 @@ private TranslatedValue translateIndex(
11391117
BoolExpr isMapIndexTruncated = context.isTruncatedMap(lhsTrans);
11401118
return TranslatedValue.propagateStrict(ctx, typeSystem, op.getFunction(), actualValue, args)
11411119
.withApproximation(
1142-
CelZ3Exprs.mkOrFlattened(
1143-
ctx, isListIndexTruncated, isMapIndexTruncated, access.isApproximate));
1120+
CelZ3Exprs.mkOrFlattened(ctx, isListIndexTruncated, isMapIndexTruncated));
11441121
}
11451122

11461123
private TranslatedValue translateConditional(
@@ -1197,12 +1174,7 @@ private static CelType extractAstTypeOrDefault(TranslatedValue val, CelAbstractS
11971174
return val.celExpr().map(node -> ast.getTypeOrThrow(node.id())).orElse(SimpleType.DYN);
11981175
}
11991176

1200-
/**
1201-
* Returns the type that {@code val} is guaranteed to conform to at runtime: a constant's, the
1202-
* declared type of an input variable, or else dyn. Global constraints may only be derived from
1203-
* guaranteed types: the type checker may narrow static types (e.g. {@code ['a'] + dyn_var} is
1204-
* typed list(string)), so constraints derived from those could exclude valid inputs.
1205-
*/
1177+
/** Returns a constant's type, or an input variable's declared type, or else dyn. */
12061178
private CelType guaranteedType(TranslatedValue val, CelAbstractSyntaxTree ast) {
12071179
return val.isLiteral(ExprKind.Kind.CONSTANT)
12081180
? extractAstTypeOrDefault(val, ast)
@@ -1254,9 +1226,11 @@ private static boolean hasStructuralEquality(CelType type) {
12541226
}
12551227
}
12561228

1229+
/** Excludes wrappers, whose kind is their value's but which may also be null. */
12571230
private static boolean isNumericType(CelType type) {
12581231
CelKind kind = type.kind();
1259-
return kind == CelKind.INT || kind == CelKind.UINT || kind == CelKind.DOUBLE;
1232+
return !(type instanceof NullableType)
1233+
&& (kind == CelKind.INT || kind == CelKind.UINT || kind == CelKind.DOUBLE);
12601234
}
12611235

12621236
/**

0 commit comments

Comments
 (0)