-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
Miscompilation in Botan's SHA3 with optimization -O2 and -O3 #51299
Comments
Can we get a bisect on this? |
The essential items to reproduce are that the target is x86_64, and -mavx is enabled. I did a bisect and found that the assertions are fixed by (or as a side-effect of) 0d74fd3 ("[SLP][COST][X86]Improve cost model for masked gather"). I had to slightly modify the reproducer to make it compile, adding : // clang++ -std=gnu++17 -O3 -mavx pr51957.cpp -o pr51957 && ./pr51957 #include template<size_t ROT, typename T> attribute((noinline)) // the calculation of C2 fails for -O3 or -O2 with clang 12 const uint64_t C3 = A[3] ^ A[8] ^ A[13] ^ A[18] ^ A[23]; const uint64_t D0 = rotl<1>(C0) ^ C3; const uint64_t B00 = A[ 0] ^ D1; const uint64_t B05 = rotl<28>(A[ 3] ^ D4); // --- instructions starting from here can be removed const uint64_t B10 = rotl< 1>(A[ 1] ^ D2); const uint64_t B15 = rotl<27>(A[ 4] ^ D0); const uint64_t B20 = rotl<62>(A[ 2] ^ D3); int main()
} But obviously this could be reduced much further. I'll also have a look at where this started going wrong, as e.g. clang 10.x didn't result in an assertion failure. |
I have a suspicion that https://reviews.llvm.org/D106613 will fix this |
Bisecting from llvm 10 onwards, it turns out that the problem got introduced (or only exposed :) in fcad8d3 ("[SLP] Make SLPVectorizer to use llvm.masked.gather intrinsic"). |
Seems quite similar. The code that failed for me was for xoshiro256, which looks like they do similar operations. Probably only got exposed by that commit, since the bad ExternalUser calls look like they were already present in that diff. |
LLVM 13 was really released without fix for this important security issue? |
Should we backport this to the release branch? |
I think it should be backported, since it fixes a possible out-of-bounds store. Even if the test case problem from botan is papered over by a seemingly unrelated commit (e.g. "Improve cost model"), it could still occur in other situations. I hope Simon and Jameson agree. :) |
+1 for rGe27a6db5298f6ba3c1dbc8bab25c769cfa761b2a to be merged to 13.x |
This should be definitely backported to 13.0.1 since "Improve cost model..." commit unfortunately veiled issue only partially. For instance, the bug is still reproduced on botan for clang-13.0.0 on skylake without avx512: https://godbolt.org/z/fWTEEs5e7 |
The fix does not apply cleanly, could someone backport this and push a branch to their local github fork? |
Merged: 32bb956 |
mentioned in issue #51489 |
Extended Description
Clang 12 and Apple Clang 13 seem to cause a miscompilation for optimization levels -O2 and -O3 in the C++ SHA3 implementation of the Botan library.
GitHub issue with detailed investigation steps and a minimal reproducer that doesn't depend on Botan:
randombit/botan#2802 (comment)
GitHub pull request with a workaround:
randombit/botan#2803
In the reproducing program, wrong results seem to surface in line 3 of SHA3_round():
const uint64_t C2 = A[2] ^ A[7] ^ A[12] ^ A[17] ^ A[22];
for the given input values, the result in C2 would be too large to fit into a 64-bit signed integer. FWIW.
The text was updated successfully, but these errors were encountered: