[24.0] Backport Cranelift: x64: fix incorrect load-sinking in copysign operator. (#12437)#13213
Merged
cfallin merged 2 commits intobytecodealliance:release-24.0.0from Apr 28, 2026
Conversation
alexcrichton
approved these changes
Apr 27, 2026
…gn` operator. (bytecodealliance#12437) The implementation of the `fcopysign` operator uses vector bitwise AND instructions on the floating-point/vector registers containing the inputs to the operator. This is a reasonable implementation as the instruction set does not have scalar (single-lane) bitwise operators. However, when load-sinking automatically kicks in for an operand to an `andps`, it can turn a 64-bit load (`f64.load`) into a 128-bit load incorrectly. This load-widening can cause out-of-bounds accesses where they were not expected. When dynamic bounds checks are enabled, we compile assuming the correct load-operator width is codegen'd; a too-wide load could read beyond the checked bound, either into unmapped memory (crashing the process) or, worse, valid data outside the sandbox. In the case of `fcopysign` the result of that read is not directly available, because it will go into the high (unused) lane, but the out-of-bounds read itself is a problem. Thanks to louismerlin for reporting!
1d50846 to
cf3ab46
Compare
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.
The implementation of the
fcopysignoperator uses vector bitwise AND instructions on the floating-point/vector registers containing the inputs to the operator. This is a reasonable implementation as the instruction set does not have scalar (single-lane) bitwise operators. However, when load-sinking automatically kicks in for an operand to anandps, it can turn a 64-bit load (f64.load) into a 128-bit load incorrectly.This load-widening can cause out-of-bounds accesses where they were not expected. When dynamic bounds checks are enabled, we compile assuming the correct load-operator width is codegen'd; a too-wide load could read beyond the checked bound, either into unmapped memory (crashing the process) or, worse, valid data outside the sandbox. In the case of
fcopysignthe result of that read is not directly available, because it will go into the high (unused) lane, but the out-of-bounds read itself is a problem.Thanks to louismerlin for reporting!