Skip to content

Conversation

@tarcieri
Copy link
Member

@tarcieri tarcieri commented Dec 17, 2025

The core predication function of the portable implementation is called is_non_zero and it returns a 1-bit mask value which is subsequently used to "select" between two values using bitwise masking.

In some previous real-world cases in the Rust compiler, it has been able to deduce, when using this sort of pattern in a loop, that the entire loop can be skipped when e.g. the condition is zero. RUSTSEC-2024-0344 is one such example.

Adding a black_box in this position should hopefully help hint to the compiler that we do not want such an "optimization" performed.

Note that the only thing we're actually relying on from black_box for program correctness is that it behaves as an identity function, so this will not have deleterious effects. Whether it actually helps avoid problems like RUSTSEC-2024-0344 is something that can only be seen in practice, unfortunately, as we don't have any way to get actual guarantees out of the Rust compiler in cases where we can't use asm!.

The core predication function of the portable implementation is called
`is_non_zero` and it returns a bitmask which is subsequently used to
"select" between two values using bitwise masking.

In some previous real-world cases in the Rust compiler, it has been able
to deduce, when using this sort of pattern in a loop, that the entire
loop can be skipped when e.g. the condition is zero. RUSTSEC-2024-0344
is one such example.

Adding a `black_box` in this position should hopefully help hint to the
compiler that we do not want such an "optimization" performed.

Note that the only thing we're actually relying on from `black_box` for
program correctness is that it behaves as an identity function, so this
will not have deleterious effects. Whether it actually helps avoid
problems like RUSTSEC-2024-0344 is something that can only be seen in
practice, unfortunately, as we don't have any way to get actual
guarantees out of the Rust compiler in cases where we can't use `asm!`.
@tarcieri tarcieri force-pushed the cmov/use-black_box-for-portable-impl branch from 29cb3be to 9365992 Compare December 17, 2025 22:58
@tarcieri tarcieri merged commit e24cfe8 into master Dec 17, 2025
16 checks passed
@tarcieri tarcieri deleted the cmov/use-black_box-for-portable-impl branch December 17, 2025 23:00
@tarcieri tarcieri mentioned this pull request Dec 20, 2025
tarcieri added a commit that referenced this pull request Dec 20, 2025
### Changed
- Use `black_box` in portable impl ([#1255])

[#1255]: #1255
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants