fix: mask left shifts in numeric fast path - #1096
Open
He-Pin wants to merge 2 commits into
Open
Conversation
Motivation: Jsonnet interprets shift counts modulo 64, but the numeric fast path validated left shifts using the unmasked count. Expressions routed through outer arithmetic could therefore fail even when the generic evaluator succeeded. Modification: Normalize non-negative left-shift counts modulo 64 before both overflow validation and execution, and add regressions for counts 64, 65, and 128. Result: Numeric fast-path behavior now matches the generic evaluator, go-jsonnet, jrsonnet, and the Jsonnet specification without changing valid count-65 behavior. References: - databricks#1096 - https://jsonnet.org/ref/spec.html
He-Pin
force-pushed
the
fix/left-shift-fast-path-mask
branch
from
July 29, 2026 08:27
6920423 to
34e2b63
Compare
Motivation: The original tests only covered zero and positive left operands. The overflow check uses math.abs(ll), so negative values exercise a distinct code path that should be regression-protected. Modification: Add assertions for (-1)<<64, (-2)<<65, (-3)<<128 through the numeric fast path (via * 1). Result: Negative operand behavior is now locked in.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Jsonnet specifies shift counts are modulo 64. The numeric fast path (
visitBinaryOpAsDouble) checked overflow with the unmasked count while the JVM masked the actual shift, making results path-dependent:(1 << 64) * 1incorrectly errored instead of returning1.Modification
rr % 64before the overflow check in the numeric fast path.Result
(1 << 64) * 1111(1 << 65) * 12222(1 << 128) * 1111(0 << 128) * 1000((1 << 64) + 0) * 1111Full matrix (JVM/JS/Wasm/Native) +
checkFormatpassed.References
builtinShiftLmodulo-64