Skip to content

Commit 62200ff

Browse files
committed
Add cpuid checks for XSAVE, OSXSAVE and AVX
AVX2 and AVX512F depend on AVX, XSAVE and OSXSAVE being present. If they are not, AVX2/AVX512F instructions may be blocked even though cpuid() reports them to be available. Spotted when the AVX512 rANS codec crashed with illegal instructions on a virtual host that claimed to have AVX512F, but did not have OSXSAVE.
1 parent de42211 commit 62200ff

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

htscodecs/rANS_static4x16pr.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,8 @@ static int is_amd UNUSED = 0;
862862
#define HAVE_HTSCODECS_TLS_CPU_INIT
863863
static void htscodecs_tls_cpu_init(void) {
864864
unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
865+
unsigned int have_xsave UNUSED = 0;
866+
unsigned int have_avx UNUSED = 0;
865867
// These may be unused, depending on HAVE_* config.h macros
866868

867869
int level = __get_cpuid_max(0, NULL);
@@ -877,9 +879,16 @@ static void htscodecs_tls_cpu_init(void) {
877879
#endif
878880
#if defined(bit_SSE4_1)
879881
have_sse4_1 = ecx & bit_SSE4_1;
882+
#endif
883+
#if defined(bit_XSAVE) && defined(bit_OSXSAVE)
884+
have_xsave = (ecx & bit_XSAVE) && (ecx & bit_OSXSAVE);
885+
#endif
886+
#if defined(bit_AVX)
887+
have_avx = ecx & bit_AVX;
880888
#endif
881889
}
882-
if (level >= 7) {
890+
// AVX2 and AVX512F depend on XSAVE, OSXSAVE and AVX
891+
if (level >= 7 && have_xsave && have_avx) {
883892
__cpuid_count(7, 0, eax, ebx, ecx, edx);
884893
#if defined(bit_AVX2)
885894
have_avx2 = ebx & bit_AVX2;

0 commit comments

Comments
 (0)