[GH-ISSUE #2716] Change the AVX2 detection in x86.S and x86-64.S to check for features we actually need. #1748

Closed
opened 2026-10-15 04:23:59 +07:00 by bsakaguchi · 4 comments
Owner

Originally created by @magnumripper on GitHub (Sep 2, 2017).
Original GitHub issue: https://github.com/openwall/john/issues/2716

#2384
https://github.com/magnumripper/JohnTheRipper/issues/2674#issuecomment-326740705

We should check for actual AVX2 support (and whatever else we need, if anything) and not features that merely suggest we have AVX2.

Originally created by @magnumripper on GitHub (Sep 2, 2017). Original GitHub issue: https://github.com/openwall/john/issues/2716 #2384 https://github.com/magnumripper/JohnTheRipper/issues/2674#issuecomment-326740705 We should check for actual AVX2 support (and whatever else we need, if anything) and not features that merely suggest we have AVX2.
bsakaguchi 2026-10-15 04:23:59 +07:00
  • closed this issue
  • added the
    invalid
    label
Author
Owner

@magnumripper commented on GitHub (Sep 2, 2017):

@solardiz I recall I was actually hoping for you to add such test to John proper so I could just merge that 😉

<!-- gh-comment-id:326750002 --> @magnumripper commented on GitHub (Sep 2, 2017): @solardiz I recall I was actually hoping for you to add such test to John proper so I could just merge that 😉
Author
Owner

@magnumripper commented on GitHub (Sep 8, 2017):

@kholia from https://github.com/magnumripper/JohnTheRipper/issues/2674:

From x86-64.S,

#define CF_AVX                  $0x1C000000 /* AVX + XSAVE + OSXSAVE */
#define CF_AVX2                 $0x1C401000 /* the above + MOVBE + FMA3 */

From commit f9338ccaee, "The AVX2 check is very quick and dirty, not using leaf 7 nor extended flags but it should be sufficient for a starter".

You fooled me there. That's not the latest commit to this file. The below is also there (but I forgot I already did this!):

#define C7_AVX2                 $0x00000128 /* BMI2 + AVX2 + BMI1 */
#define C7_AVX512F              $0x00010000
#define C7_AVX512BW             $0x40010000 /* AVX512BW + AVX512F */

Those are leaf 7 tests. Actually we do test everything that we should - and nothing else.

@solardiz from https://github.com/magnumripper/JohnTheRipper/issues/2674:

per Dhiru's comments above we actually know our check for AVX2 is wrong - it doesn't check for AVX2, but rather checks for MOVBE and FMA3, neither of which we use.

Dhiru's comment was incorrect, but the last part of what you say puzzles me. Of course we have to check for MOVBE and FMA3. How would we be sure some compiler doesn't emit them?! The tests I do are everything Intel says we have to check for and nothing else.

From Intel spec:

Generally, compilers and libraries generating code for 4th generation Intel Core processors are expected and allowed to use all the instructions listed above, with the exception of Intel TSX. Below is the complete list of CPUID flags that generally must be checked:

CPUID.(EAX=01H, ECX=0H):ECX.FMA[bit 12]==1 &&
CPUID.(EAX=07H, ECX=0H):EBX.AVX2[bit 5]==1 &&
CPUID.(EAX=07H, ECX=0H):EBX.BMI1[bit 3]==1 &&
CPUID.(EAX=07H, ECX=0H):EBX.BMI2[bit 8]==1 &&
CPUID.(EAX=80000001H):ECX.LZCNT[bit 5]==1 &&
CPUID.(EAX=01H, ECX=0H):ECX.MOVBE[bit 22]==1

For 32-bit builds though, the AVX2 test is still quick'n'dirty. I don't intend to fix that. If someone else does (or John Proper gets it) it would be nice of course.

<!-- gh-comment-id:328170645 --> @magnumripper commented on GitHub (Sep 8, 2017): @kholia from https://github.com/magnumripper/JohnTheRipper/issues/2674: > From x86-64.S, > > ``` > #define CF_AVX $0x1C000000 /* AVX + XSAVE + OSXSAVE */ > #define CF_AVX2 $0x1C401000 /* the above + MOVBE + FMA3 */ > ``` > > From commit f9338ccaeec94949a2e7f4a7cb54da9a1919750d, "The AVX2 check is very quick and dirty, not using leaf 7 nor extended flags but it should be sufficient for a starter". You fooled me there. That's not the latest commit to this file. The below is also there (but I forgot I already did this!): ``` #define C7_AVX2 $0x00000128 /* BMI2 + AVX2 + BMI1 */ #define C7_AVX512F $0x00010000 #define C7_AVX512BW $0x40010000 /* AVX512BW + AVX512F */ ``` Those are leaf 7 tests. Actually we do test **everything** that we should - and nothing else. @solardiz from https://github.com/magnumripper/JohnTheRipper/issues/2674: > per Dhiru's comments above we actually know our check for AVX2 is wrong - it doesn't check for AVX2, but rather checks for MOVBE and FMA3, neither of which we use. Dhiru's comment was incorrect, but the last part of what you say puzzles me. Of course we have to check for MOVBE and FMA3. How would we be sure some compiler doesn't emit them?! The tests I do are everything Intel says we have to check for and nothing else. From [Intel spec](https://software.intel.com/en-us/articles/how-to-detect-new-instruction-support-in-the-4th-generation-intel-core-processor-family): > Generally, compilers and libraries generating code for 4th generation Intel Core processors are expected and allowed to use all the instructions listed above, with the exception of Intel TSX. Below is the complete list of CPUID flags that generally must be checked: > > CPUID.(EAX=01H, ECX=0H):ECX.FMA[bit 12]==1 && > CPUID.(EAX=07H, ECX=0H):EBX.AVX2[bit 5]==1 && > CPUID.(EAX=07H, ECX=0H):EBX.BMI1[bit 3]==1 && > CPUID.(EAX=07H, ECX=0H):EBX.BMI2[bit 8]==1 && > CPUID.(EAX=80000001H):ECX.LZCNT[bit 5]==1 && > CPUID.(EAX=01H, ECX=0H):ECX.MOVBE[bit 22]==1 For 32-bit builds though, the AVX2 test is still quick'n'dirty. I don't intend to fix that. If someone else does (or John Proper gets it) it would be nice of course.
Author
Owner

@solardiz commented on GitHub (Sep 8, 2017):

Of course we have to check for MOVBE and FMA3. How would we be sure some compiler doesn't emit them?!

That's a good point. They're allowed to when we build with -march=native, and MOVBE is likely to actually be used where we use __builtin_bswap32 and __builtin_bswap64, which we do. We could avoid FMA3 but using -mno-fma, but that option is available only in recent versions of gcc, so we'd have to check whether it's supported first. Overall, it's probably OK to leave the CPU check as-is for now. Thanks!

<!-- gh-comment-id:328211198 --> @solardiz commented on GitHub (Sep 8, 2017): > Of course we have to check for MOVBE and FMA3. How would we be sure some compiler doesn't emit them?! That's a good point. They're allowed to when we build with `-march=native`, and MOVBE is likely to actually be used where we use `__builtin_bswap32` and `__builtin_bswap64`, which we do. We could avoid FMA3 but using `-mno-fma`, but that option is available only in recent versions of gcc, so we'd have to check whether it's supported first. Overall, it's probably OK to leave the CPU check as-is for now. Thanks!
Author
Owner

@magnumripper commented on GitHub (Sep 8, 2017):

They're allowed to when we build with -march=native

I parsed the Intel document as that they're allowed to as soon as we build with -mavx2. Anyway, the 64-bit code is fine. We should fix the 32-bit stuff some day. In fact, you should merge all AVX2 support into John Proper and do it for us - a proper x86.S is the only thing missing ;-)

<!-- gh-comment-id:328224975 --> @magnumripper commented on GitHub (Sep 8, 2017): > They're allowed to when we build with `-march=native` I parsed the Intel document as that they're allowed to as soon as we build with `-mavx2`. Anyway, the 64-bit code is fine. We should fix the 32-bit stuff some day. In fact, you should merge all AVX2 support into John Proper and do it for us - a proper `x86.S` is the only thing missing ;-)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openwall/john#1748
No description provided.