mesa: fix out of bounds access in glGetFramebufferParameterivEXT
[mesa.git] / src / mesa / x86 / common_x86.c
index ac77c9d77433e0fa937e59621142089e8725a7f0..a237fc0c9973a74723e15852ff53377bc5af4fab 100644 (file)
  */
 
 /* XXX these includes should probably go into imports.h or glheader.h */
-#if defined(USE_SSE_ASM) && defined(__linux__)
-#include <linux/version.h>
-#endif
 #if defined(USE_SSE_ASM) && defined(__FreeBSD__)
 #include <sys/types.h>
 #include <sys/sysctl.h>
 #endif
-#if defined(USE_SSE_ASM) && defined(__OpenBSD__)
+#if defined(USE_SSE_ASM) && (defined(__OpenBSD__) || defined(__NetBSD__))
 #include <sys/param.h>
 #include <sys/sysctl.h>
 #include <machine/cpu.h>
 #endif
 #if defined(USE_X86_64_ASM)
 #include <cpuid.h>
+#if !defined(bit_SSE4_1) && defined(bit_SSE41)
+/* XXX: clang defines bit_SSE41 instead of bit_SSE4_1 */
+#define bit_SSE4_1 bit_SSE41
+#elif !defined(bit_SSE4_1) && !defined(bit_SSE41)
+#define bit_SSE4_1 0x00080000
+#endif
 #endif
 
-#include "main/imports.h"
+#include <stdlib.h>
+
+#include "main/errors.h"
+
 #include "common_x86_asm.h"
 
 
@@ -62,12 +68,12 @@ static int detection_debug = GL_FALSE;
 
 /* No reason for this to be public.
  */
-extern GLuint  _ASMAPI _mesa_x86_has_cpuid(void);
-extern void    _ASMAPI _mesa_x86_cpuid(GLuint op, GLuint *reg_eax, GLuint *reg_ebx, GLuint *reg_ecx, GLuint *reg_edx);
-extern GLuint  _ASMAPI _mesa_x86_cpuid_eax(GLuint op);
-extern GLuint  _ASMAPI _mesa_x86_cpuid_ebx(GLuint op);
-extern GLuint  _ASMAPI _mesa_x86_cpuid_ecx(GLuint op);
-extern GLuint  _ASMAPI _mesa_x86_cpuid_edx(GLuint op);
+extern GLuint _mesa_x86_has_cpuid(void);
+extern void _mesa_x86_cpuid(GLuint op, GLuint *reg_eax, GLuint *reg_ebx, GLuint *reg_ecx, GLuint *reg_edx);
+extern GLuint _mesa_x86_cpuid_eax(GLuint op);
+extern GLuint _mesa_x86_cpuid_ebx(GLuint op);
+extern GLuint _mesa_x86_cpuid_ecx(GLuint op);
+extern GLuint _mesa_x86_cpuid_edx(GLuint op);
 
 
 #if defined(USE_SSE_ASM)
@@ -154,10 +160,10 @@ void _mesa_check_os_sse_support( void )
    }
 #elif defined(_WIN32)
    LPTOP_LEVEL_EXCEPTION_FILTER oldFilter;
-   
+
    /* Install our ExceptionFilter */
    oldFilter = SetUnhandledExceptionFilter( ExceptionFilter );
-   
+
    if ( cpu_has_xmm ) {
       _mesa_debug(NULL, "Testing OS support for SSE...\n");
 
@@ -218,7 +224,7 @@ _mesa_get_x86_features(void)
 #ifdef USE_X86_ASM
    _mesa_x86_cpu_features = 0x0;
 
-   if (_mesa_getenv( "MESA_NO_ASM")) {
+   if (getenv( "MESA_NO_ASM")) {
       return;
    }
 
@@ -301,7 +307,7 @@ _mesa_get_x86_features(void)
 
 #ifdef USE_MMX_ASM
    if ( cpu_has_mmx ) {
-      if ( _mesa_getenv( "MESA_NO_MMX" ) == 0 ) {
+      if ( getenv( "MESA_NO_MMX" ) == 0 ) {
         if (detection_debug)
            _mesa_debug(NULL, "MMX cpu detected.\n");
       } else {
@@ -312,7 +318,7 @@ _mesa_get_x86_features(void)
 
 #ifdef USE_3DNOW_ASM
    if ( cpu_has_3dnow ) {
-      if ( _mesa_getenv( "MESA_NO_3DNOW" ) == 0 ) {
+      if ( getenv( "MESA_NO_3DNOW" ) == 0 ) {
         if (detection_debug)
            _mesa_debug(NULL, "3DNow! cpu detected.\n");
       } else {
@@ -323,10 +329,10 @@ _mesa_get_x86_features(void)
 
 #ifdef USE_SSE_ASM
    if ( cpu_has_xmm ) {
-      if ( _mesa_getenv( "MESA_NO_SSE" ) == 0 ) {
+      if ( getenv( "MESA_NO_SSE" ) == 0 ) {
         if (detection_debug)
            _mesa_debug(NULL, "SSE cpu detected.\n");
-         if ( _mesa_getenv( "MESA_FORCE_SSE" ) == 0 ) {
+         if ( getenv( "MESA_FORCE_SSE" ) == 0 ) {
             _mesa_check_os_sse_support();
          }
       } else {
@@ -337,16 +343,18 @@ _mesa_get_x86_features(void)
 #endif
 
 #elif defined(USE_X86_64_ASM)
-   unsigned int uninitialized_var(eax), uninitialized_var(ebx),
-                uninitialized_var(ecx), uninitialized_var(edx);
+   {
+      unsigned int eax, ebx, ecx, edx;
 
-   /* Always available on x86-64. */
-   _mesa_x86_cpu_features |= X86_FEATURE_XMM | X86_FEATURE_XMM2;
+      /* Always available on x86-64. */
+      _mesa_x86_cpu_features |= X86_FEATURE_XMM | X86_FEATURE_XMM2;
 
-   __get_cpuid(1, &eax, &ebx, &ecx, &edx);
+      if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx))
+         return;
 
-   if (ecx & bit_SSE4_1)
-      _mesa_x86_cpu_features |= X86_FEATURE_SSE4_1;
+      if (ecx & bit_SSE4_1)
+         _mesa_x86_cpu_features |= X86_FEATURE_SSE4_1;
+   }
 #endif /* USE_X86_64_ASM */
 
    (void) detection_debug;