shithub: dav1d

Download patch

ref: 2d608a5f004b7f86d291a489ed619e3412827c47
parent: 7b208fa806378083ab76d6a632ec6f64c4e7a87e
author: Henrik Gramner <gramner@twoorioles.com>
date: Sat Dec 14 11:02:18 EST 2019

Fix potential race condition in dav1d_get_cpu_flags()

--- a/src/cpu.c
+++ b/src/cpu.c
@@ -30,24 +30,22 @@
 
 #include "src/cpu.h"
 
+static unsigned flags = 0;
 static unsigned flags_mask = -1;
 
-COLD unsigned dav1d_get_cpu_flags(void) {
-    static unsigned flags;
-    static uint8_t checked = 0;
-
-    if (!checked) {
-#if (ARCH_AARCH64 || ARCH_ARM) && HAVE_ASM
-        flags = dav1d_get_cpu_flags_arm();
-#elif ARCH_PPC64LE && HAVE_ASM
-        flags = dav1d_get_cpu_flags_ppc();
-#elif ARCH_X86 && HAVE_ASM
-        flags = dav1d_get_cpu_flags_x86();
-#else
-        flags = 0;
+COLD void dav1d_init_cpu(void) {
+#if HAVE_ASM
+#if ARCH_AARCH64 || ARCH_ARM
+    flags = dav1d_get_cpu_flags_arm();
+#elif ARCH_PPC64LE
+    flags = dav1d_get_cpu_flags_ppc();
+#elif ARCH_X86
+    flags = dav1d_get_cpu_flags_x86();
 #endif
-        checked = 1;
-    }
+#endif
+}
+
+COLD unsigned dav1d_get_cpu_flags(void) {
     return flags & flags_mask;
 }
 
--- a/src/cpu.h
+++ b/src/cpu.h
@@ -42,7 +42,8 @@
 #include "src/x86/cpu.h"
 #endif
 
+void dav1d_init_cpu(void);
 unsigned dav1d_get_cpu_flags(void);
-DAV1D_API void dav1d_set_cpu_flags_mask(const unsigned mask);
+DAV1D_API void dav1d_set_cpu_flags_mask(unsigned mask);
 
 #endif /* DAV1D_SRC_CPU_H */
--- a/src/lib.c
+++ b/src/lib.c
@@ -37,6 +37,7 @@
 #include "common/mem.h"
 #include "common/validate.h"
 
+#include "src/cpu.h"
 #include "src/fg_apply.h"
 #include "src/internal.h"
 #include "src/log.h"
@@ -47,10 +48,11 @@
 #include "src/wedge.h"
 
 static COLD void init_internal(void) {
-    dav1d_init_wedge_masks();
+    dav1d_init_cpu();
     dav1d_init_interintra_masks();
     dav1d_init_qm_tables();
     dav1d_init_thread();
+    dav1d_init_wedge_masks();
 }
 
 COLD const char *dav1d_version(void) {
--- a/tests/checkasm/checkasm.c
+++ b/tests/checkasm/checkasm.c
@@ -569,6 +569,7 @@
     if (state.function_listing) {
         print_functions(state.funcs);
     } else {
+        dav1d_init_cpu();
         for (int i = 0; cpus[i].flag; i++)
             check_cpu_flag(cpus[i].name, cpus[i].flag);