From 2595e76a2f9c9dfb9a262380524578371661fb51 Mon Sep 17 00:00:00 2001 From: Sean Huh Date: Wed, 4 Nov 2026 16:42:34 -0800 Subject: [PATCH] Give the verifier's double values a single NaN PiperOrigin-RevId: 1014238489 --- .../dev/cel/verifier/CelZ3TypeConstraintGenerator.java | 7 ++++++- .../src/main/java/dev/cel/verifier/CelZ3TypeSystem.java | 5 ++++- .../test/java/dev/cel/verifier/CelVerifierZ3ImplTest.java | 4 ++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/verifier/src/main/java/dev/cel/verifier/CelZ3TypeConstraintGenerator.java b/verifier/src/main/java/dev/cel/verifier/CelZ3TypeConstraintGenerator.java index 1f6e86475..8826f8a43 100644 --- a/verifier/src/main/java/dev/cel/verifier/CelZ3TypeConstraintGenerator.java +++ b/verifier/src/main/java/dev/cel/verifier/CelZ3TypeConstraintGenerator.java @@ -80,7 +80,12 @@ BoolExpr createTypeConstraintForType(Expr val, CelType type) { return ctx.mkAnd(typeSystem.isUint(val), ctx.mkNot(typeSystem.checkUintOverflow(unwrapped))); } if (type.equals(SimpleType.DOUBLE)) { - return typeSystem.isDouble(val); + // Inputs share wrapDouble's canonical NaN. + return ctx.mkAnd( + typeSystem.isDouble(val), + ctx.mkImplies( + ctx.mkFPIsNaN(typeSystem.getDouble(val)), + ctx.mkEq(val, typeSystem.wrapDouble(typeSystem.getDouble(val))))); } if (type.equals(SimpleType.STRING)) { return typeSystem.isString(val); diff --git a/verifier/src/main/java/dev/cel/verifier/CelZ3TypeSystem.java b/verifier/src/main/java/dev/cel/verifier/CelZ3TypeSystem.java index 1e92aed35..74a05cf5b 100644 --- a/verifier/src/main/java/dev/cel/verifier/CelZ3TypeSystem.java +++ b/verifier/src/main/java/dev/cel/verifier/CelZ3TypeSystem.java @@ -295,7 +295,10 @@ public Expr wrapUint(IntExpr expr) { /** Wraps a Z3 double (floating-point) expression into a CelValue. */ public Expr wrapDouble(FPExpr expr) { - return ctx.mkApp(doubleCons.ConstructorDecl(), expr); + // CEL has a single NaN, but Z3 can model distinct NaN terms as distinct values. + return ctx.mkApp( + doubleCons.ConstructorDecl(), + ctx.mkITE(ctx.mkFPIsNaN(expr), ctx.mkFPNaN(ctx.mkFPSortDouble()), expr)); } /** Wraps a Z3 bytes sequence expression into a CelValue. */ diff --git a/verifier/src/test/java/dev/cel/verifier/CelVerifierZ3ImplTest.java b/verifier/src/test/java/dev/cel/verifier/CelVerifierZ3ImplTest.java index 722f808f6..8c84a915a 100644 --- a/verifier/src/test/java/dev/cel/verifier/CelVerifierZ3ImplTest.java +++ b/verifier/src/test/java/dev/cel/verifier/CelVerifierZ3ImplTest.java @@ -2449,6 +2449,10 @@ private enum EquivalenceTestCase { "google.protobuf.FloatValue{value: 0.1}", "0.10000000149011612"), WRAPPER_FLOAT_PRECISION_LOSS_NOT_EQUAL_ORIGINAL( "google.protobuf.FloatValue{value: 0.1} == 0.1", "false"), + WRAPPER_FLOAT_NAN("d != d ? google.protobuf.FloatValue{value: d} : 0.0", "d != d ? d : 0.0"), + DOUBLE_NAN_ARITHMETIC("d != d ? d + 1.0 : 0.0", "d != d ? d : 0.0"), + DOUBLE_NAN_INPUT_AND_LITERAL("d != d ? d : 0.0", "d != d ? 0.0 / 0.0 : 0.0"), + DOUBLE_NAN_IN_LIST("d != d ? [d] : []", "d != d ? [-d] : []"), WRAPPER_FLOAT_IDEMPOTENT_LITERAL( "google.protobuf.FloatValue{value: google.protobuf.FloatValue{value: 0.1}}", "google.protobuf.FloatValue{value: 0.1}"),