|
17 | 17 | import com.google.auto.value.AutoValue; |
18 | 18 | import com.google.common.collect.ImmutableList; |
19 | 19 | import com.google.common.collect.ObjectArrays; |
| 20 | +import com.google.common.math.DoubleMath; |
20 | 21 | import com.google.common.primitives.UnsignedLongs; |
21 | 22 | import com.microsoft.z3.ArithExpr; |
22 | 23 | import com.microsoft.z3.ArrayExpr; |
| 24 | +import com.microsoft.z3.BitVecNum; |
23 | 25 | import com.microsoft.z3.BoolExpr; |
| 26 | +import com.microsoft.z3.BoolSort; |
24 | 27 | import com.microsoft.z3.Constructor; |
25 | 28 | import com.microsoft.z3.Context; |
26 | 29 | import com.microsoft.z3.DatatypeSort; |
27 | 30 | import com.microsoft.z3.Expr; |
28 | 31 | import com.microsoft.z3.FPExpr; |
| 32 | +import com.microsoft.z3.FPNum; |
29 | 33 | import com.microsoft.z3.FuncDecl; |
30 | 34 | import com.microsoft.z3.IntExpr; |
| 35 | +import com.microsoft.z3.IntNum; |
| 36 | +import com.microsoft.z3.IntSort; |
| 37 | +import com.microsoft.z3.Model; |
31 | 38 | import com.microsoft.z3.SeqExpr; |
32 | 39 | import com.microsoft.z3.SeqSort; |
33 | 40 | import com.microsoft.z3.Sort; |
34 | 41 | import dev.cel.common.internal.ProtoTimeUtils; |
35 | 42 | import java.math.BigInteger; |
| 43 | +import java.math.RoundingMode; |
36 | 44 | import java.util.Arrays; |
37 | 45 | import java.util.HashMap; |
| 46 | +import java.util.Iterator; |
| 47 | +import java.util.LinkedHashSet; |
38 | 48 | import java.util.List; |
39 | 49 | import java.util.Map; |
| 50 | +import java.util.Set; |
40 | 51 |
|
41 | 52 | /** |
42 | 53 | * Type system for mapping CEL values to Z3 expressions. |
@@ -141,6 +152,8 @@ public final class CelZ3TypeSystem { |
141 | 152 | private static final String FUNC_MSG_PRESENCE = "msg_presence"; |
142 | 153 | private static final String FUNC_MSG_TYPE_NAME = "msg_type_name"; |
143 | 154 | private static final String FUNC_HAS_TRUNCATED_TYPE_CONSTRAINT = "!has_truncated_type_constraint"; |
| 155 | + private static final String FUNC_TRUNCATE_DOUBLE = "!truncate_double"; |
| 156 | + private static final String FUNC_HAS_FRACTION = "!has_fraction"; |
144 | 157 |
|
145 | 158 | private final Context ctx; |
146 | 159 | private final Map<FuncDeclKey, FuncDecl<?>> funcDeclCache; |
@@ -180,6 +193,10 @@ public final class CelZ3TypeSystem { |
180 | 193 | private final FuncDecl msgPresenceFunc; |
181 | 194 | private final FuncDecl msgTypeNameFunc; |
182 | 195 | private final FuncDecl<?> hasTruncatedTypeConstraintFunc; |
| 196 | + private final FuncDecl<IntSort> truncateDoubleFunc; |
| 197 | + private final FuncDecl<BoolSort> hasFractionFunc; |
| 198 | + // Doubles whose truncation and fraction are still uninterpreted, in order of first use. |
| 199 | + private final Set<FPExpr> undefinedDoubles; |
183 | 200 |
|
184 | 201 | private final boolean parameterizeUnknowns; |
185 | 202 |
|
@@ -786,6 +803,120 @@ public BoolExpr checkUintOverflow(ArithExpr result) { |
786 | 803 | ctx.mkGt(result, ctx.mkInt(CelNumericBounds.MAX_UINT64)), ctx.mkLt(result, ctx.mkInt(0))); |
787 | 804 | } |
788 | 805 |
|
| 806 | + // Int/double comparisons are exact for |n| < 2^64. A double's truncation and fraction stay |
| 807 | + // uninterpreted until defineDoublesContradictedBy refutes a model that gets them wrong. |
| 808 | + |
| 809 | + /** Returns whether the integer {@code n} equals the double {@code d}. */ |
| 810 | + BoolExpr intEqDouble(IntExpr n, FPExpr d) { |
| 811 | + return ctx.mkAnd(isIntegral(d), ctx.mkEq(n, truncateDouble(d))); |
| 812 | + } |
| 813 | + |
| 814 | + /** Returns whether the integer {@code n} is less than the double {@code d}. */ |
| 815 | + public BoolExpr intLtDouble(IntExpr n, FPExpr d) { |
| 816 | + IntExpr t = truncateDouble(d); |
| 817 | + return ctx.mkOr( |
| 818 | + ctx.mkFPGEq(d, mkFpDouble(CelNumericBounds.TWO_TO_64)), |
| 819 | + ctx.mkAnd( |
| 820 | + isBelowTwoTo64(d), |
| 821 | + ctx.mkOr( |
| 822 | + ctx.mkLt(n, t), |
| 823 | + ctx.mkAnd(ctx.mkEq(n, t), hasFraction(d), ctx.mkNot(ctx.mkFPIsNegative(d)))))); |
| 824 | + } |
| 825 | + |
| 826 | + /** Returns whether the double {@code d} is less than the integer {@code n}. */ |
| 827 | + public BoolExpr doubleLtInt(FPExpr d, IntExpr n) { |
| 828 | + IntExpr t = truncateDouble(d); |
| 829 | + return ctx.mkOr( |
| 830 | + ctx.mkFPLEq(d, ctx.mkFPNeg(mkFpDouble(CelNumericBounds.TWO_TO_64))), |
| 831 | + ctx.mkAnd( |
| 832 | + isBelowTwoTo64(d), |
| 833 | + ctx.mkOr( |
| 834 | + ctx.mkLt(t, n), ctx.mkAnd(ctx.mkEq(n, t), hasFraction(d), ctx.mkFPIsNegative(d))))); |
| 835 | + } |
| 836 | + |
| 837 | + /** Returns whether the integer {@code n} is at most the double {@code d}. */ |
| 838 | + public BoolExpr intLeDouble(IntExpr n, FPExpr d) { |
| 839 | + return ctx.mkAnd(ctx.mkNot(ctx.mkFPIsNaN(d)), ctx.mkNot(doubleLtInt(d, n))); |
| 840 | + } |
| 841 | + |
| 842 | + /** Returns whether the double {@code d} is at most the integer {@code n}. */ |
| 843 | + public BoolExpr doubleLeInt(FPExpr d, IntExpr n) { |
| 844 | + return ctx.mkAnd(ctx.mkNot(ctx.mkFPIsNaN(d)), ctx.mkNot(intLtDouble(n, d))); |
| 845 | + } |
| 846 | + |
| 847 | + /** Returns whether {@code d} is an integer of magnitude below 2^64. */ |
| 848 | + BoolExpr isIntegral(FPExpr d) { |
| 849 | + return ctx.mkAnd(isBelowTwoTo64(d), ctx.mkNot(hasFraction(d))); |
| 850 | + } |
| 851 | + |
| 852 | + /** Returns {@code d} truncated toward zero, if its magnitude is below 2^64. */ |
| 853 | + IntExpr truncateDouble(FPExpr d) { |
| 854 | + return (IntExpr) |
| 855 | + ctx.mkITE(ctx.mkFPIsZero(d), ctx.mkInt(0), applyUndefined(truncateDoubleFunc, d)); |
| 856 | + } |
| 857 | + |
| 858 | + /** Returns exact definitions for the tracked doubles whose values {@code model} gets wrong. */ |
| 859 | + ImmutableList<BoolExpr> defineDoublesContradictedBy(Model model) { |
| 860 | + ImmutableList.Builder<BoolExpr> definitions = ImmutableList.builder(); |
| 861 | + for (Iterator<FPExpr> it = undefinedDoubles.iterator(); it.hasNext(); ) { |
| 862 | + FPExpr d = it.next(); |
| 863 | + double value = toJavaDouble((FPNum) model.evaluate(d, /* completion= */ true)); |
| 864 | + // The comparisons don't read the functions at NaN, zero, infinities, or beyond 2^64. |
| 865 | + if (Double.isNaN(value) || value == 0 || Math.abs(value) >= CelNumericBounds.TWO_TO_64) { |
| 866 | + continue; |
| 867 | + } |
| 868 | + BigInteger truncated = |
| 869 | + ((IntNum) model.evaluate(ctx.mkApp(truncateDoubleFunc, d), /* completion= */ true)) |
| 870 | + .getBigInteger(); |
| 871 | + boolean hasFraction = |
| 872 | + model.evaluate(ctx.mkApp(hasFractionFunc, d), /* completion= */ true).isTrue(); |
| 873 | + if (!truncated.equals(DoubleMath.roundToBigInteger(value, RoundingMode.DOWN)) |
| 874 | + || hasFraction == DoubleMath.isMathematicalInteger(value)) { |
| 875 | + it.remove(); |
| 876 | + definitions.add(defineDouble(d)); |
| 877 | + } |
| 878 | + } |
| 879 | + return definitions.build(); |
| 880 | + } |
| 881 | + |
| 882 | + /** Converts a Z3 double to the Java double with the same IEEE 754 bits (or NaN). */ |
| 883 | + double toJavaDouble(FPNum fpNum) { |
| 884 | + // Z3 doesn't simplify the bits of NaN to a numeral. |
| 885 | + return fpNum.isNaN() |
| 886 | + ? Double.NaN |
| 887 | + : Double.longBitsToDouble( |
| 888 | + ((BitVecNum) ctx.mkFPToIEEEBV(fpNum).simplify()).getBigInteger().longValue()); |
| 889 | + } |
| 890 | + |
| 891 | + private BoolExpr hasFraction(FPExpr d) { |
| 892 | + return ctx.mkAnd(ctx.mkNot(ctx.mkFPIsZero(d)), applyUndefined(hasFractionFunc, d)); |
| 893 | + } |
| 894 | + |
| 895 | + // Tracks d so that defineDoublesContradictedBy checks the model's value of func at d. |
| 896 | + private <R extends Sort> Expr<R> applyUndefined(FuncDecl<R> func, FPExpr d) { |
| 897 | + undefinedDoubles.add(d); |
| 898 | + return ctx.mkApp(func, d); |
| 899 | + } |
| 900 | + |
| 901 | + private BoolExpr defineDouble(FPExpr d) { |
| 902 | + // 65-bit two's complement holds every truncation below 2^64. |
| 903 | + IntExpr truncated = |
| 904 | + ctx.mkBV2Int( |
| 905 | + ctx.mkFPToBV(ctx.mkFPRoundTowardZero(), d, 65, /* signed= */ true), /* signed= */ true); |
| 906 | + BoolExpr hasFraction = |
| 907 | + ctx.mkNot(ctx.mkFPEq(d, ctx.mkFPRoundToIntegral(ctx.mkFPRoundTowardZero(), d))); |
| 908 | + return ctx.mkImplies( |
| 909 | + isBelowTwoTo64(d), |
| 910 | + ctx.mkAnd( |
| 911 | + ctx.mkEq(ctx.mkApp(truncateDoubleFunc, d), truncated), |
| 912 | + ctx.mkEq(ctx.mkApp(hasFractionFunc, d), hasFraction))); |
| 913 | + } |
| 914 | + |
| 915 | + // False for NaN and infinities. |
| 916 | + private BoolExpr isBelowTwoTo64(FPExpr d) { |
| 917 | + return ctx.mkFPLt(ctx.mkFPAbs(d), mkFpDouble(CelNumericBounds.TWO_TO_64)); |
| 918 | + } |
| 919 | + |
789 | 920 | /** Safely concatenates two Z3 sequences. */ |
790 | 921 | @SuppressWarnings("unchecked") // Callers verify arguments are SeqExprs. |
791 | 922 | public <R extends Sort> SeqExpr<R> mkConcatSafe(Expr<?> arg1, Expr<?> arg2) { |
@@ -956,6 +1087,11 @@ private static FuncDeclKey create(String name, Sort[] domain, Sort range) { |
956 | 1087 | this.hasTruncatedTypeConstraintFunc = |
957 | 1088 | ctx.mkFuncDecl( |
958 | 1089 | FUNC_HAS_TRUNCATED_TYPE_CONSTRAINT, new Sort[] {this.celValueSort}, ctx.getBoolSort()); |
| 1090 | + this.truncateDoubleFunc = |
| 1091 | + ctx.mkFuncDecl(FUNC_TRUNCATE_DOUBLE, ctx.mkFPSortDouble(), ctx.getIntSort()); |
| 1092 | + this.hasFractionFunc = |
| 1093 | + ctx.mkFuncDecl(FUNC_HAS_FRACTION, ctx.mkFPSortDouble(), ctx.getBoolSort()); |
| 1094 | + this.undefinedDoubles = new LinkedHashSet<>(); |
959 | 1095 | this.parameterizeUnknowns = parameterizeUnknowns; |
960 | 1096 | } |
961 | 1097 | } |
0 commit comments