Skip to content

Commit bd1bf25

Browse files
l46kokcopybara-github
authored andcommitted
Select fields from and index optionals, and error on .? of undeclared fields
PiperOrigin-RevId: 1021151594
1 parent 26b44a4 commit bd1bf25

8 files changed

Lines changed: 325 additions & 217 deletions

File tree

‎verifier/src/main/java/dev/cel/verifier/BUILD.bazel‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ java_library(
195195
"//common/types:type_providers",
196196
"//common/values",
197197
"//common/values:cel_byte_string",
198+
"//extensions:optional_library",
198199
"//optimizer",
199200
"//optimizer:optimization_exception",
200201
"//optimizer:optimizer_builder",

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package dev.cel.verifier;
1616

17+
import static com.google.common.base.Preconditions.checkArgument;
18+
1719
import com.google.common.collect.ImmutableList;
1820
import com.google.common.collect.ImmutableSet;
1921
import com.google.common.collect.ImmutableSortedSet;
@@ -439,6 +441,15 @@ private TranslatedValue translateCall(CelExpr expr, CelAbstractSyntaxTree ast) {
439441
long exprId = expr.id();
440442
String functionName = call.function();
441443
ImmutableList<CelExpr> argsAst = call.args();
444+
if (functionName.equals(Operator.OPTIONAL_SELECT.getFunction())) {
445+
checkArgument(
446+
!call.target().isPresent()
447+
&& argsAst.size() == 2
448+
&& argsAst.get(1).constantOrDefault().getKind() == CelConstant.Kind.STRING_VALUE,
449+
"Malformed AST: operator %s expects an operand and a field name",
450+
Operator.OPTIONAL_SELECT);
451+
return structTranslator.translateSelect(expr, ast);
452+
}
442453

443454
List<TranslatedValue> args = new ArrayList<>();
444455

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

Lines changed: 135 additions & 137 deletions
Large diffs are not rendered by default.

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

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.microsoft.z3.SeqExpr;
2727
import dev.cel.common.CelAbstractSyntaxTree;
2828
import dev.cel.common.ast.CelExpr;
29+
import dev.cel.common.ast.CelExpr.ExprKind;
2930
import dev.cel.common.internal.ProtoTimeUtils;
3031
import dev.cel.common.types.CelKind;
3132
import dev.cel.common.types.CelType;
@@ -536,25 +537,49 @@ private FieldAccess getMsgAccess(Expr<?> operand, String field, BoolExpr typeGua
536537
return new FieldAccess(presence, value);
537538
}
538539

540+
/**
541+
* Translates {@code e.f}, {@code has(e.f)} or {@code e.?f} (an optional of {@code e.f} if {@code
542+
* has(e.f)}, else empty).
543+
*/
539544
TranslatedValue translateSelect(CelExpr celExpr, CelAbstractSyntaxTree ast) {
540-
CelExpr.CelSelect select = celExpr.select();
545+
boolean isOptional = celExpr.getKind() == ExprKind.Kind.CALL;
546+
CelExpr operandExpr = isOptional ? celExpr.call().args().get(0) : celExpr.select().operand();
547+
String field =
548+
isOptional
549+
? celExpr.call().args().get(1).constant().stringValue()
550+
: celExpr.select().field();
551+
boolean testOnly = !isOptional && celExpr.select().testOnly();
541552
long exprId = celExpr.id();
542-
TranslatedValue operandTv = translateExpr(select.operand(), ast);
553+
TranslatedValue operandTv = translateExpr(operandExpr, ast);
543554
Expr<?> operand = operandTv.z3Expr();
544-
String field = select.field();
545-
CelType operandType = extractAstTypeOrDefault(ast, select.operand().id());
555+
CelType operandType = extractAstTypeOrDefault(ast, operandExpr.id());
556+
// Selecting from an optional selects from its value optionally (e.g. `optional.of({}).a` is
557+
// `optional.none()`). Performance only: an operand of a known type other than optional isn't.
558+
boolean mayBeOptional =
559+
operandType instanceof OptionalType
560+
|| !CelZ3OperatorTranslator.isStaticallyKnown(operandType);
561+
BoolExpr isOptionalOperand = ctx.mkFalse();
562+
BoolExpr hasValue = ctx.mkFalse();
563+
BoolExpr shouldEvaluate = ctx.mkTrue();
564+
if (mayBeOptional) {
565+
Expr<?> optRef = typeSystem.getOptionalRef(operand);
566+
isOptionalOperand = typeSystem.isOptional(operand);
567+
hasValue = typeSystem.optHasValue(optRef);
568+
shouldEvaluate = (BoolExpr) ctx.mkITE(isOptionalOperand, hasValue, ctx.mkTrue());
569+
operand = ctx.mkITE(isOptionalOperand, typeSystem.getOptionalValue(optRef), operand);
570+
}
546571

547572
Expr<?> presenceResult;
548573
Expr<?> valueResult;
549574
BoolExpr unmodeledMessage = ctx.mkFalse();
550575
BoolExpr presenceError = ctx.mkFalse();
551576

552577
if (operandType instanceof MapType) {
553-
FieldAccess mapAcc = getMapAccess(operand, field, ctx.mkTrue());
578+
FieldAccess mapAcc = getMapAccess(operand, field, shouldEvaluate);
554579
presenceResult = mapAcc.presence;
555580
valueResult = ctx.mkITE(mapAcc.presence, mapAcc.value, typeSystem.mkError());
556581
} else if (operandType.kind() == CelKind.STRUCT) {
557-
FieldAccess msgAcc = getMsgAccess(operand, field, ctx.mkTrue());
582+
FieldAccess msgAcc = getMsgAccess(operand, field, shouldEvaluate);
558583
presenceResult = msgAcc.presence;
559584
Expr<?> defaultVal = getDefaultValueForType(extractAstTypeOrDefault(ast, exprId));
560585
valueResult = ctx.mkITE(msgAcc.presence, msgAcc.value, defaultVal);
@@ -563,8 +588,8 @@ TranslatedValue translateSelect(CelExpr celExpr, CelAbstractSyntaxTree ast) {
563588
BoolExpr isMap = typeSystem.isMap(operand);
564589
BoolExpr isMessage = typeSystem.isMessage(operand);
565590

566-
FieldAccess mapAcc = getMapAccess(operand, field, isMap);
567-
FieldAccess msgAcc = getMsgAccess(operand, field, isMessage);
591+
FieldAccess mapAcc = getMapAccess(operand, field, ctx.mkAnd(shouldEvaluate, isMap));
592+
FieldAccess msgAcc = getMsgAccess(operand, field, ctx.mkAnd(shouldEvaluate, isMessage));
568593

569594
presenceResult =
570595
CelZ3Exprs.SwitchBuilder.newBuilder(ctx)
@@ -590,13 +615,13 @@ TranslatedValue translateSelect(CelExpr celExpr, CelAbstractSyntaxTree ast) {
590615
msgRead.addCase(isType, typeSystem.mkError());
591616
continue;
592617
}
593-
if (select.testOnly()) {
618+
if (testOnly) {
594619
continue;
595620
}
596621
CelType fieldType = declared.get().type();
597622
context.addConstraint(
598623
ctx.mkImplies(
599-
ctx.mkAnd(isMessage, isType, msgAcc.presence),
624+
ctx.mkAnd(shouldEvaluate, isMessage, isType, msgAcc.presence),
600625
typeConstraintGenerator.createTypeConstraintForType(msgAcc.value, fieldType)));
601626
msgRead.addCase(
602627
isType, ctx.mkITE(msgAcc.presence, msgAcc.value, getDefaultValueForType(fieldType)));
@@ -605,7 +630,10 @@ TranslatedValue translateSelect(CelExpr celExpr, CelAbstractSyntaxTree ast) {
605630
unmodeledMessage =
606631
context.recordApproximation(
607632
"messages of types the type provider doesn't declare",
608-
ctx.mkAnd(isMessage, ctx.mkNot(CelZ3Exprs.mkOrFlattened(ctx, isModeledType))));
633+
ctx.mkAnd(
634+
shouldEvaluate,
635+
isMessage,
636+
ctx.mkNot(CelZ3Exprs.mkOrFlattened(ctx, isModeledType))));
609637
Expr<?> mapRead = ctx.mkITE(mapAcc.presence, mapAcc.value, typeSystem.mkError());
610638
// Testing an undeclared field, or a field of something other than a map or message, errors.
611639
presenceError =
@@ -622,10 +650,27 @@ TranslatedValue translateSelect(CelExpr celExpr, CelAbstractSyntaxTree ast) {
622650
.build(typeSystem.mkError());
623651
}
624652

653+
// has(e.f), else e.f, or for e.?f and an optional e, an optional of e.f if has(e.f).
625654
Expr<?> fieldAccess =
626-
select.testOnly()
655+
testOnly
627656
? typeSystem.withRuntimeError(typeSystem.wrapBool(presenceResult), presenceError)
628657
: valueResult;
658+
Expr<?> chained = fieldAccess;
659+
if (!testOnly && (isOptional || mayBeOptional)) {
660+
chained =
661+
ctx.mkITE(
662+
presenceError,
663+
typeSystem.mkError(),
664+
ctx.mkITE(
665+
(BoolExpr) presenceResult,
666+
context.mkOptionalOf(valueResult),
667+
typeSystem.mkOptionalNone()));
668+
fieldAccess = isOptional ? chained : valueResult;
669+
}
670+
if (mayBeOptional) {
671+
Expr<?> empty = testOnly ? typeSystem.mkBool(false) : typeSystem.mkOptionalNone();
672+
fieldAccess = ctx.mkITE(isOptionalOperand, ctx.mkITE(hasValue, chained, empty), fieldAccess);
673+
}
629674

630675
context.addConstraint(typeConstraintGenerator.createTypeConstraint(fieldAccess, exprId, ast));
631676

@@ -636,7 +681,8 @@ TranslatedValue translateSelect(CelExpr celExpr, CelAbstractSyntaxTree ast) {
636681
return operandType.kind() == CelKind.STRUCT
637682
? result
638683
: result.withApproximation(
639-
CelZ3Exprs.mkOrFlattened(ctx, context.isTruncatedMap(operand), unmodeledMessage));
684+
CelZ3Exprs.mkOrFlattened(
685+
ctx, ctx.mkAnd(shouldEvaluate, context.isTruncatedMap(operand)), unmodeledMessage));
640686
}
641687

642688
private static CelType extractAstTypeOrDefault(CelAbstractSyntaxTree ast, long id) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ void constrainValidValue(BoolExpr guard, Expr<?> value) {
8080
addConstraint(ctx.mkImplies(guard, typeSystem.isValidValue(value)));
8181
}
8282

83+
/** Returns an optional holding {@code value}. */
84+
Expr<?> mkOptionalOf(Expr<?> value) {
85+
Expr<?> optRef = ctx.mkApp(typeSystem.optionalOfRefFunc(), value);
86+
addConstraint(ctx.mkEq(typeSystem.getOptionalValue(optRef), value));
87+
addConstraint(typeSystem.optHasValue(optRef));
88+
return typeSystem.mkOptionalOf(optRef);
89+
}
90+
8391
void addInputVariable(Expr<?> variable, CelType declaredType) {
8492
inputVariableTypes.put(variable, declaredType);
8593
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ private static String extractOpName(CelExpr expr) {
199199
switch (expr.exprKind().getKind()) {
200200
case SELECT:
201201
return "select_" + expr.select().field() + "_" + expr.select().testOnly();
202+
case CALL: // e.?f
203+
return "optional_select_" + expr.call().args().get(1).constant().stringValue();
202204
case STRUCT:
203205
StringBuilder structSb = new StringBuilder("struct_").append(expr.struct().messageName());
204206
for (CelExpr.CelStruct.Entry entry : expr.struct().entries()) {

‎verifier/src/main/java/dev/cel/verifier/axioms/OptionalAxioms.java‎

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,17 @@
2020
import static dev.cel.extensions.CelOptionalLibrary.OptionalDeclaration.OPTIONAL_OF_NON_ZERO_VALUE;
2121
import static dev.cel.extensions.CelOptionalLibrary.OptionalDeclaration.OPTIONAL_OR;
2222
import static dev.cel.extensions.CelOptionalLibrary.OptionalDeclaration.OPTIONAL_OR_VALUE;
23-
import static dev.cel.extensions.CelOptionalLibrary.OptionalDeclaration.OPTIONAL_SELECT;
2423
import static dev.cel.extensions.CelOptionalLibrary.OptionalDeclaration.OPTIONAL_VALUE;
2524

2625
import com.google.common.base.Preconditions;
2726
import com.google.common.collect.ImmutableList;
28-
import com.microsoft.z3.ArrayExpr;
2927
import com.microsoft.z3.BoolExpr;
3028
import com.microsoft.z3.Context;
3129
import com.microsoft.z3.Expr;
3230
import com.microsoft.z3.FPExpr;
3331
import com.microsoft.z3.SeqExpr;
3432
import dev.cel.common.CelFunctionDecl;
3533
import dev.cel.common.CelOverloadDecl;
36-
import dev.cel.verifier.CelZ3Exprs;
3734
import dev.cel.verifier.CelZ3TypeSystem;
3835
import java.util.Optional;
3936

@@ -88,63 +85,6 @@ final class OptionalAxioms {
8885
(ctx, ts, sink, val, other) -> {
8986
Expr<?> optRef = ts.getOptionalRef(val);
9087
return Optional.of(ctx.mkITE(ts.optHasValue(optRef), val, other));
91-
}),
92-
createBinaryAxiom(
93-
OPTIONAL_SELECT,
94-
(ctx, ts, sink, operand, field) -> {
95-
Expr<?> optRef = ts.getOptionalRef(operand);
96-
BoolExpr isOpt = ts.isOptional(operand);
97-
BoolExpr hasValue = ts.optHasValue(optRef);
98-
Expr<?> actualOperand = ctx.mkITE(isOpt, ts.getOptionalValue(optRef), operand);
99-
100-
BoolExpr isMap = ts.isMap(actualOperand);
101-
BoolExpr isMsg = ts.isMessage(actualOperand);
102-
BoolExpr isValidTarget = ctx.mkOr(isMap, isMsg);
103-
104-
Expr<?> msgFieldZ3Str = ts.getString(field);
105-
Expr<?> mapFieldCelVal = field;
106-
107-
Expr<?> msgRef = ts.getMessageRef(actualOperand);
108-
Expr<?> mapRef = ts.getMapRef(actualOperand);
109-
110-
Expr<?> presence =
111-
CelZ3Exprs.SwitchBuilder.newBuilder(ctx)
112-
.addCase(
113-
isMsg,
114-
ctx.mkSelect((ArrayExpr) ts.getMsgPresence(msgRef), msgFieldZ3Str))
115-
.addCase(
116-
isMap,
117-
ctx.mkSelect((ArrayExpr) ts.getMapPresence(mapRef), mapFieldCelVal))
118-
.build(ctx.mkFalse());
119-
120-
Expr<?> value =
121-
CelZ3Exprs.SwitchBuilder.newBuilder(ctx)
122-
.addCase(
123-
isMsg, ctx.mkSelect((ArrayExpr) ts.getMsgValues(msgRef), msgFieldZ3Str))
124-
.addCase(
125-
isMap,
126-
ctx.mkSelect((ArrayExpr) ts.getMapValues(mapRef), mapFieldCelVal))
127-
.build(ts.mkError());
128-
129-
BoolExpr valNotError = ctx.mkNot(ctx.mkEq(value, ts.mkError()));
130-
BoolExpr shouldEvaluate = (BoolExpr) ctx.mkITE(isOpt, hasValue, ctx.mkTrue());
131-
sink.accept(
132-
ctx.mkImplies(
133-
CelZ3Exprs.mkAndFlattened(
134-
ctx, shouldEvaluate, isValidTarget, (BoolExpr) presence),
135-
valNotError));
136-
137-
Expr<?> resultOptRef = ctx.mkApp(ts.optionalOfRefFunc(), value);
138-
sink.accept(ctx.mkEq(ts.getOptionalValue(resultOptRef), value));
139-
sink.accept(ts.optHasValue(resultOptRef));
140-
141-
Expr<?> optionalResult =
142-
ctx.mkITE(
143-
(BoolExpr) presence, ts.mkOptionalOf(resultOptRef), ts.mkOptionalNone());
144-
145-
Expr<?> result = ctx.mkITE(isValidTarget, optionalResult, ts.mkError());
146-
return Optional.of(
147-
ctx.mkITE(ctx.mkAnd(isOpt, ctx.mkNot(hasValue)), ts.mkOptionalNone(), result));
14888
}));
14989

15090
private static BoolExpr isZeroValue(Context ctx, CelZ3TypeSystem ts, Expr<?> val) {

0 commit comments

Comments
 (0)