Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial cmov crate #741

Merged
merged 3 commits into from
Feb 27, 2022
Merged

Initial cmov crate #741

merged 3 commits into from
Feb 27, 2022

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

cmov/README.md Outdated Show resolved Hide resolved
cmov/README.md Outdated Show resolved Hide resolved
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.
cmov/src/lib.rs Outdated Show resolved Hide resolved
@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.

None yet

1 participant