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
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private TranslatedValue translateIdent(CelExpr celExpr, CelAbstractSyntaxTree as
}
String name = ident.name();
CelType type = ast.getTypeOrThrow(exprId);
if (type instanceof TypeType) {
if (type instanceof TypeType && !context.lookupSymbol(name).isPresent()) {
TypeType typeType = (TypeType) type;
if (typeType.type().kind() != CelKind.DYN) {
return TranslatedValue.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ final class CelZ3ComprehensionTranslator {
TranslatedValue translateComprehension(CelExpr celExpr, CelAbstractSyntaxTree ast) {
CelComprehension comp = celExpr.comprehension();
CelExpr iterRangeExpr = comp.iterRange();
if (iterRangeExpr.exprKind().getKind() == ExprKind.Kind.IDENT) {
iterRangeExpr =
context
.lookupSymbol(iterRangeExpr.ident().name())
.flatMap(TranslatedValue::celExpr)
.orElse(iterRangeExpr);
}
List<IterationElement> iterationElements = new ArrayList<>();
List<BoolExpr> taints = new ArrayList<>();
List<Expr<?>> allRangeElems = new ArrayList<>();
Expand Down Expand Up @@ -86,6 +79,7 @@ TranslatedValue translateComprehension(CelExpr celExpr, CelAbstractSyntaxTree as
allRangeElems.add(key);
allRangeElems.add(value);
}
allRangeElems.add(translateExpr(iterRangeExpr, ast).z3Expr());
} else {
return translateDynamicComprehension(celExpr, ast);
}
Expand Down Expand Up @@ -182,18 +176,36 @@ private TranslatedValue translateDynamicComprehension(
isMap ? (ArrayExpr) typeSystem.getMapPresence(typeSystem.getMapRef(iterRange)) : null;

BoolExpr isValidRange = isMap ? typeSystem.isMap(iterRange) : typeSystem.isList(iterRange);
int unrollLimit = unrollLimit(comp.iterRange());
BoolExpr isTruncated =
context.recordUnrollLimitApproximation(
ctx.mkAnd(isValidRange, ctx.mkGt(lengthExpr, ctx.mkInt(comprehensionUnrollLimit))));
ctx.mkAnd(isValidRange, ctx.mkGt(lengthExpr, ctx.mkInt(unrollLimit))));
context.addUnmodeledResult(isTruncated);

if (isAllMacro(comp) || isExistsMacro(comp)) {
return unrollAllAndExists(
celExpr, ast, seq, lengthExpr, mapPresence, isTruncated, iterRangeTv);
celExpr, ast, seq, lengthExpr, mapPresence, isTruncated, iterRangeTv, unrollLimit);
} else {
return unrollMapAndFilter(
celExpr, ast, seq, lengthExpr, mapPresence, isTruncated, iterRangeTv);
celExpr, ast, seq, lengthExpr, mapPresence, isTruncated, iterRangeTv, unrollLimit);
}
}

/** The size of the literal bound to {@code iterRange}, else the unroll limit. */
private int unrollLimit(CelExpr iterRange) {
if (iterRange.exprKind().getKind() != ExprKind.Kind.IDENT) {
return comprehensionUnrollLimit;
}
return context
.lookupSymbol(iterRange.ident().name())
.flatMap(TranslatedValue::celExpr)
.map(e ->
e.exprKind().getKind() == ExprKind.Kind.LIST
? e.list().elements().size()
: e.exprKind().getKind() == ExprKind.Kind.MAP
? e.map().entries().size()
: comprehensionUnrollLimit)
.orElse(comprehensionUnrollLimit);
}

private <T> T withIterationScope(
Expand Down Expand Up @@ -260,7 +272,8 @@ private TranslatedValue unrollAllAndExists(
ArithExpr lengthExpr,
ArrayExpr mapPresence,
BoolExpr isTruncated,
TranslatedValue iterRangeTv) {
TranslatedValue iterRangeTv,
int unrollLimit) {
CelComprehension comp = celExpr.comprehension();
List<BoundedIteration> iterations = new ArrayList<>();
TranslatedValue accuInitTv = translateExpr(comp.accuInit(), ast);
Expand All @@ -269,7 +282,7 @@ private TranslatedValue unrollAllAndExists(
boolean isMap = mapPresence != null;
boolean isTwoVar = !comp.iterVar2().isEmpty();

for (int i = 0; i < comprehensionUnrollLimit; i++) {
for (int i = 0; i < unrollLimit; i++) {
IntExpr idx = ctx.mkInt(i);
BoolExpr inBounds = ctx.mkLt(idx, lengthExpr);
Expr<?> xVal = ctx.mkNth(seq, idx);
Expand Down Expand Up @@ -301,7 +314,8 @@ private TranslatedValue unrollMapAndFilter(
ArithExpr lengthExpr,
ArrayExpr mapPresence,
BoolExpr isTruncated,
TranslatedValue iterRangeTv) {
TranslatedValue iterRangeTv,
int unrollLimit) {
CelComprehension comp = celExpr.comprehension();
// For macros like map and filter, we must sequentially thread the accumulator through the loop
TranslatedValue chainedAccuTv = translateExpr(comp.accuInit(), ast);
Expand All @@ -314,7 +328,7 @@ private TranslatedValue unrollMapAndFilter(
taints.add(chainedAccuTv.isApproximate());
taints.add(iterRangeTv.isApproximate());

for (int i = 0; i < comprehensionUnrollLimit; i++) {
for (int i = 0; i < unrollLimit; i++) {
IntExpr idx = ctx.mkInt(i);
BoolExpr inBounds = ctx.mkLt(idx, lengthExpr);
Expr<?> xVal = ctx.mkNth(seq, idx);
Expand Down
113 changes: 66 additions & 47 deletions verifier/src/main/java/dev/cel/verifier/CelZ3OperatorTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ final class CelZ3OperatorTranslator {

Optional<TranslatedValue> translateFunctionCall(
String functionName, List<TranslatedValue> args, long exprId, CelAbstractSyntaxTree ast) {
ImmutableList<String> overloadIds =
ast.getReference(exprId).map(CelReference::overloadIds).orElse(ImmutableList.of());
if (overloadIds.equals(ImmutableList.of("optional_or_optional"))) {
return Optional.of(translateOptionalOr(functionName, args, ast, /* unwrap= */ false));
}
if (overloadIds.equals(ImmutableList.of("optional_orValue_value"))) {
return Optional.of(translateOptionalOr(functionName, args, ast, /* unwrap= */ true));
}
Optional<Operator> opOpt = Operator.findReverse(functionName);
ImmutableList<Expr<?>> z3Args =
args.stream().map(TranslatedValue::z3Expr).collect(toImmutableList());
Expand Down Expand Up @@ -238,6 +246,43 @@ private BoolExpr constrainInWitnessTypes(
mayObserveTruncatedTypeConstraint(ast, lhsTv, rhsTv)));
}

/**
* Translates {@code receiver.or(alternative)}, or {@code orValue} if {@code unwrap}, which only
* evaluates the alternative if the receiver is empty.
*/
private TranslatedValue translateOptionalOr(
String functionName, List<TranslatedValue> args, CelAbstractSyntaxTree ast, boolean unwrap) {
TranslatedValue receiver = args.get(0);
TranslatedValue alternative = args.get(1);
Expr<?> optRef = typeSystem.getOptionalRef(receiver.z3Expr());
TranslatedValue hasValue =
TranslatedValue.propagateStrict(
ctx,
typeSystem,
functionName,
typeSystem.withRuntimeError(
typeSystem.wrapBool(typeSystem.optHasValue(optRef)),
ctx.mkNot(typeSystem.isOptional(receiver.z3Expr()))),
receiver);
TranslatedValue present =
unwrap
? TranslatedValue.create(typeSystem.getOptionalValue(optRef), typeSystem, ctx.mkFalse())
: receiver;
Expr<?> alt = alternative.z3Expr();
// `isUnknown` is untested: a model can't be forced to make an unknown input unknown.
TranslatedValue absent =
unwrap
? alternative
: TranslatedValue.create(
ctx.mkITE(
ctx.mkOr(typeSystem.isOptional(alt), typeSystem.isUnknown(alt)),
alt,
typeSystem.mkError()),
typeSystem,
alternative.isApproximate());
return translateConditional(ImmutableList.of(hasValue, present, absent), ast);
}

private BoolExpr mkTypeGuard(Expr<?> arg, CelType expectedType) {
switch (expectedType.kind()) {
case LIST:
Expand Down Expand Up @@ -864,9 +909,6 @@ private BoolExpr listMembership(
// Whether lhs may equal an element of elemType: a number only across numeric types, and a list
// only another list (anything else conservatively).
private BoolExpr mayEqualElement(Expr<?> lhs, CelType elemType) {
if (elemType instanceof NullableType) {
return ctx.mkTrue();
}
if (isNumericType(elemType)) {
return isNumeric(lhs);
}
Expand Down Expand Up @@ -989,8 +1031,6 @@ private TranslatedValue translateEquality(
structuralEq = (BoolExpr) ctx.mkITE(bothMaps, mapEquality, structuralEq);
} else if (!isStaticallyKnown(type0) && !isStaticallyKnown(type1)) {
// Performance only: a side with a known type isn't a list or map here.
// TODO: Unless the type checker narrowed it, e.g. `(int_list +
// dyn_var)[0]`.
structuralEq =
approximateEqualityWhere(
context.recordApproximation(
Expand Down Expand Up @@ -1027,13 +1067,11 @@ private static final class IndexAccess {
private final BoolExpr valid;
private final BoolExpr found;
private final Expr<?> value;
private final BoolExpr isApproximate;

private IndexAccess(BoolExpr valid, BoolExpr found, Expr<?> value, BoolExpr isApproximate) {
private IndexAccess(BoolExpr valid, BoolExpr found, Expr<?> value) {
this.valid = valid;
this.found = found;
this.value = value;
this.isApproximate = isApproximate;
}
}

Expand All @@ -1043,54 +1081,40 @@ private IndexAccess listAccess(Expr<?> lhsTrans, Expr<?> rhsTrans) {
return new IndexAccess(
ctx.mkAnd(typeSystem.isList(lhsTrans), typeSystem.isInt(rhsTrans)),
ctx.mkAnd(ctx.mkGe(index, ctx.mkInt(0)), ctx.mkLt(index, ctx.mkLength(seq))),
ctx.mkNth(seq, index),
/* isApproximate= */ ctx.mkFalse());
ctx.mkNth(seq, index));
}

private IndexAccess mapAccess(Expr<?> lhsTrans, Expr<?> rhsTrans) {
ImmutableList<CelZ3TypeSystem.EqualKey> equalKeys = typeSystem.equalKeys(rhsTrans);
return new IndexAccess(
typeSystem.isMap(lhsTrans),
typeSystem.hasEqualKey(lhsTrans, equalKeys),
typeSystem.valueAtEqualKey(lhsTrans, equalKeys),
/* isApproximate= */ ctx.mkFalse());
typeSystem.valueAtEqualKey(lhsTrans, equalKeys));
}

/**
* Looks up {@code rhsTrans} in the list or map {@code lhsTrans}. The value is approximate if the
* static element type isn't guaranteed and it may not conform.
* Looks up {@code rhsTrans} in the list or map {@code lhsTrans} of type {@code containerType}.
*/
private IndexAccess indexAccess(
Expr<?> lhsTrans, Expr<?> rhsTrans, CelType containerType, boolean isLhsTypeGuaranteed) {
private IndexAccess indexAccess(Expr<?> lhsTrans, Expr<?> rhsTrans, CelType containerType) {
IndexAccess list = listAccess(lhsTrans, rhsTrans);
IndexAccess map = mapAccess(lhsTrans, rhsTrans);
IndexAccess access =
new IndexAccess(
ctx.mkOr(list.valid, map.valid),
(BoolExpr) ctx.mkITE(list.valid, list.found, map.found),
ctx.mkITE(list.valid, list.value, map.value),
/* isApproximate= */ ctx.mkFalse());
ctx.mkITE(list.valid, list.value, map.value));
BoolExpr isRead = ctx.mkAnd(access.valid, access.found);
context.constrainValidValue(isRead, access.value);
if (containerType.kind() != CelKind.MAP && containerType.kind() != CelKind.LIST) {
return access;
}
CelType elemType =
containerType.kind() == CelKind.MAP
? ((MapType) containerType).valueType()
: ((ListType) containerType).elemType();
BoolExpr conforms = typeConstraintGenerator.createTypeConstraintForType(access.value, elemType);
if (isLhsTypeGuaranteed) {
context.addConstraint(ctx.mkImplies(isRead, conforms));
return access;
if (containerType instanceof MapType || containerType instanceof ListType) {
CelType elemType =
containerType instanceof MapType
? ((MapType) containerType).valueType()
: ((ListType) containerType).elemType();
context.addConstraint(
ctx.mkImplies(
isRead, typeConstraintGenerator.createTypeConstraintForType(access.value, elemType)));
}
// The type checker may narrow other static types (e.g. `(['a'] + dyn_var)[1]` is typed
// string), so the element may not conform.
return new IndexAccess(
access.valid,
access.found,
access.value,
CelZ3Exprs.mkAndFlattened(ctx, isRead, CelZ3Exprs.mkNotFlattened(ctx, conforms)));
return access;
}

/**
Expand All @@ -1102,7 +1126,6 @@ private TranslatedValue translateIndex(
TranslatedValue lhs = args.get(0);
TranslatedValue rhs = args.get(1);
CelType lhsType = extractAstTypeOrDefault(lhs, ast);
boolean isLhsTypeGuaranteed = lhsType.equals(guaranteedType(lhs, ast));
CelType containerType = lhsType instanceof OptionalType ? lhsType.parameters().get(0) : lhsType;

Expr<?> lhsTrans = lhs.z3Expr();
Expand All @@ -1112,7 +1135,7 @@ private TranslatedValue translateIndex(
BoolExpr lhsHasValue = typeSystem.optHasValue(optRef);
lhsTrans = ctx.mkITE(isOptionalLhs, typeSystem.getOptionalValue(optRef), lhsTrans);
// An empty optional's value can't be read, so constraining it is harmless.
IndexAccess access = indexAccess(lhsTrans, rhsTrans, containerType, isLhsTypeGuaranteed);
IndexAccess access = indexAccess(lhsTrans, rhsTrans, containerType);
Expr<?> element =
ctx.mkITE(ctx.mkAnd(access.valid, access.found), access.value, typeSystem.mkError());
Expr<?> optionalElement =
Expand All @@ -1139,8 +1162,7 @@ private TranslatedValue translateIndex(
BoolExpr isMapIndexTruncated = context.isTruncatedMap(lhsTrans);
return TranslatedValue.propagateStrict(ctx, typeSystem, op.getFunction(), actualValue, args)
.withApproximation(
CelZ3Exprs.mkOrFlattened(
ctx, isListIndexTruncated, isMapIndexTruncated, access.isApproximate));
CelZ3Exprs.mkOrFlattened(ctx, isListIndexTruncated, isMapIndexTruncated));
}

private TranslatedValue translateConditional(
Expand Down Expand Up @@ -1197,12 +1219,7 @@ private static CelType extractAstTypeOrDefault(TranslatedValue val, CelAbstractS
return val.celExpr().map(node -> ast.getTypeOrThrow(node.id())).orElse(SimpleType.DYN);
}

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

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,24 @@ BoolExpr hasUnmodeledResult() {
}

void bindSymbol(String name, TranslatedValue value) {
localScope.put(name, value);
symbolTable.put(name, value);
}

Optional<TranslatedValue> lookupSymbol(String name) {
return Optional.ofNullable(localScope.get(name));
TranslatedValue local = localScope.get(name);
return Optional.ofNullable(local != null ? local : symbolTable.get(globalName(name)));
}

TranslatedValue computeSymbolIfAbsent(
String rawName, Function<String, TranslatedValue> mappingFunction) {
if (!rawName.startsWith(".") && localScope.containsKey(rawName)) {
if (localScope.containsKey(rawName)) {
return localScope.get(rawName);
}
return symbolTable.computeIfAbsent(
rawName.startsWith(".") ? rawName.substring(1) : rawName, mappingFunction);
return symbolTable.computeIfAbsent(globalName(rawName), mappingFunction);
}

private static String globalName(String name) {
return name.startsWith(".") ? name.substring(1) : name;
}

/** Binds {@code varName} to {@code value} while running {@code action}, then restores it. */
Expand Down
Loading
Loading