Skip to content

Conversation

satiscugcat
Copy link
Contributor

@satiscugcat satiscugcat commented Jun 3, 2025

This fixes the error mentioned in issue #1822

@rustbot
Copy link
Collaborator

rustbot commented Jun 3, 2025

r? @Amanieu

rustbot has assigned @Amanieu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@satiscugcat satiscugcat changed the title Fix in erroneous implementation of _mm256_bsrli_epi128, removal of redundant operation in _mm256_alignr_epi8 issue Fix in erroneous implementation of _mm256_bsrli_epi128, removal of redundant operation in _mm256_alignr_epi8 Jun 3, 2025
}

let r: i8x32 = match IMM8 % 16 {
let r: i8x32 = match IMM8 {
Copy link
Member

@bjorn3 bjorn3 Jun 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to also add a value to the default arm. It is currently unreachable_unchecked(), which makes IMM8 values > 15 UB.
Edit: Maybe you should change the IMM8 == 16 if above to be IMM8 >= 16? Also there is handling for IMM8 > 16 above already: (_mm256_setzero_si256(), a) which would become unreachable in that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, I was mistaken. I think specifically values between 17-31 (inclusive) are UB without the %, which makes it not redundant. My mistake!

@bjorn3
Copy link
Member

bjorn3 commented Jun 3, 2025

Did you try reverifying _mm256_alignr_epi8 with whatever tool you used to find the _mm256_bsrli_epi128 bug? And if so how did the unreachable_unchecked() bug slip past it?

@satiscugcat
Copy link
Contributor Author

Did you try reverifying _mm256_alignr_epi8 with whatever tool you used to find the _mm256_bsrli_epi128 bug? And if so how did the unreachable_unchecked() bug slip past it?

The _mm256_bsrli_epi128 bug was found manually upon noticing a discrepancy in the intel specifications and the rust implementations in that case, and then testing it as it is tested in issue #1822 . Thus the other bug slipped past me too. My bad!

@satiscugcat satiscugcat changed the title Fix in erroneous implementation of _mm256_bsrli_epi128, removal of redundant operation in _mm256_alignr_epi8 Fix in erroneous implementation of _mm256_bsrli_epi128 Jun 3, 2025
@sayantn
Copy link
Contributor

sayantn commented Jun 3, 2025

Could you check pls if this bug is also there for _mm256_bslli_epi128 and the _mm512 variants too. They might be affected as the impl is very similar

@satiscugcat
Copy link
Contributor Author

Could you check pls if this bug is also there for _mm256_bslli_epi128 and the _mm512 variants too. They might be affected as the impl is very similar

Sure! We'll work on testing the variants from tomorrow.

@sayantn
Copy link
Contributor

sayantn commented Jun 3, 2025

just checked, seems like the same bug is there for _mm512_bsrli_epi128, but not for any of the bslli variants (it is actually amusing how different the implementations are for bslli and bsrli, but anyway it seems like the bslli implementation is much more robust).

Actually, would you mind changing the impl of the bsrli variants to be like as the bslli variants? It would also fix this bug, and make it more consistent

@satiscugcat
Copy link
Contributor Author

just checked, seems like the same bug is there for _mm512_bsrli_epi128, but not for any of the bslli variants (it is actually amusing how different the implementations are for bslli and bsrli, but anyway it seems like the bslli implementation is much more robust).

Actually, would you mind changing the impl of the bsrli variants to be like as the bslli variants? It would also fix this bug, and make it more consistent

I've made the appropriate changes.

Copy link
Contributor

@sayantn sayantn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. Could you also see if this refactoring is also possible for _mm{256,512}_alignr_epi8. You could see the impl for _mm_alignr_epi8 for some reference (sorry for this many requests 😅)

…mm_alignr_epi8 in ssse3.rs, also removed the import of the unreachable unchecked hint as it was no longer necessary
@satiscugcat
Copy link
Contributor Author

I don't understnd why the CI failed, running ci/run-docker.sh went through fine on my end.

@satiscugcat
Copy link
Contributor Author

Ok, everything should be fine now. Let me know if there's anything else you think might need attention.

@Amanieu Amanieu added this pull request to the merge queue Jun 17, 2025
Merged via the queue into rust-lang:master with commit 99a1172 Jun 17, 2025
62 checks passed
github-merge-queue bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Sep 24, 2025
Solution to challenge 15, resolves #173.

This PR provides testable models for `core::arch` intrinsics, including
abstractions to streamline the process of implementing these intrinsics
and their tests.

Currently there are 384 x86 intrinsics modelled, and 181 aarch64
intrinsics modelled.

The methodology for writing the models is decribed in
`testable-simd-models/README.md`.
First, we model the SIMD types as bitvectors that can be converted to
and from arrays of machine integers.
Then, we model the raw operations on these types as functions over
bitvectors, while keeping as much code as possible unchanged from the
Rust code in `rust-lang/stdarch/crates/core_arch`.
Finally, we write tests (using a generic macro) to compare the behaviour
of our models with the corresponding intrinsic implementations in Rust
core.

Interestingly, in the process of modeling these intrinsivcs, we found
bugs in the implementation of two intrinsics, `_mm256_bsrli_epi128` and
`_mm512_bsrli_epi128`. These bugs were [fixed by our
PR](rust-lang/stdarch#1823) in the 2025-06-30
version of the library. In a small way, this shows off the impact of
writing testable models of the SIMD intrinsics.

The model of intrinsics defined here is also used as the basis of formal
proofs of Rust programs that use intrinsics. In particular, the
`libcrux` cryptographic library uses these models in its [proofs of the
post-quantum cryptographic
algorithms](https://github.com/cryspen/libcrux/tree/main/fstar-helpers/core-models).

As next steps, we intend to extend these testable models to a larger
subset of core (beyond SIMD) and then translate these Rust models to
models in F*, Rocq, and Lean, to enable proofs using our models in these
backends. This work is being done as part of the [hax
project](https://github.com/cryspen/hax).

The work in this PR was primarily done by Aniket Mishra, under the
supervision of Karthikeyan Bhargavan and Maxime Buyse at Cryspen.

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.

---------

Co-authored-by: maximebuyse <[email protected]>
Co-authored-by: Maxime Buyse <[email protected]>
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.

5 participants