Skip to content

Conversation

tarcieri
Copy link
Member

Implements safe wrappers for the cmovz and cmovnz instructions which work on x86 and x86_64, implemented using inline assembly.

Using inline assembly allows us to provide a sort of black box which LLVM will not interfere with, which is otherwise problematic when using anything besides ASM.

It's otherwise not possible to correctly emit CMOV instructions on x86 platforms with LLVM because the x86-cmov-conversion pass which will potentially rewrite them as branches.

For more details, see:

https://dsprenkels.com/cmov-conversion.html

Implements safe wrappers for the `cmovz` and `cmovnz` instructions which
work on `x86` and `x86_64`, implemented using inline assembly.

Using inline assembly allows us to provide a sort of black box which
LLVM will not interfere with, which is otherwise problematic when using
anything besides ASM.

It's otherwise not possible to correctly emit CMOV instructions on x86
platforms with LLVM because the `x86-cmov-conversion` pass which will
potentially rewrite them as branches.

For more details, see:

https://dsprenkels.com/cmov-conversion.html
Adds a fallback implementation based on bitwise arithmetic, which allows
the crate to work on any target architecture.

Unfortunately, this implementation cannot guarantee constant-time
operation in the same way as dedicated instructions are able to.
@tarcieri
Copy link
Member Author

tarcieri commented Feb 26, 2022

Went ahead and added a portable fallback implementation for non-x86/x86_64 platforms in 5dfbdca with optimizations in 876653d which uses bitwise arithmetic but can't otherwise specifically guarantee constant-time operation.

I think it's nice to have from an ergonomics perspective, even if we can't make guarantees.

We could potentially add some tests that automatically inspect the generated assembly and make sure the generated instructions fall within a particular allow list. That'd provide a degree of due diligence that the resulting implementation is constant time, if not a guarantee.

@tarcieri tarcieri merged commit 92f3680 into master Feb 27, 2022
@tarcieri tarcieri deleted the cmov branch February 27, 2022 14:45
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.

1 participant