egl: Fix _eglPointerIsDereferencable w/o mincore()
[mesa.git] / src / egl / main / eglglobals.c
index baf96bb1ec5ae34b093f8d5d328b1bb6a3f8be4d..5760edc3d3c11719d9a14309321044884b775c74 100644 (file)
 #include "c11/threads.h"
 
 #include "eglglobals.h"
+#include "egldevice.h"
 #include "egldisplay.h"
 #include "egldriver.h"
+#include "egllog.h"
+
+#include "util/macros.h"
+
+#ifdef HAVE_MINCORE
+#include <unistd.h>
+#include <sys/mman.h>
+#endif
 
 
 static mtx_t _eglGlobalMutex = _MTX_INITIALIZER_NP;
 
 struct _egl_global _eglGlobal =
 {
-   &_eglGlobalMutex,       /* Mutex */
-   NULL,                   /* DisplayList */
-   2,                      /* NumAtExitCalls */
-   {
+   .Mutex = &_eglGlobalMutex,
+   .DisplayList = NULL,
+   .DeviceList = &_eglSoftwareDevice,
+   .NumAtExitCalls = 3,
+   .AtExitCalls = {
       /* default AtExitCalls, called in reverse order */
-      _eglUnloadDrivers, /* always called last */
-      _eglFiniDisplay
+      _eglFiniDevice, /* always called last */
+      _eglUnloadDrivers,
+      _eglFiniDisplay,
    },
 
-   /* ClientOnlyExtensionString */
+   .ClientOnlyExtensionString =
    "EGL_EXT_client_extensions"
+   " EGL_EXT_device_base"
+   " EGL_EXT_device_enumeration"
+   " EGL_EXT_device_query"
    " EGL_EXT_platform_base"
    " EGL_KHR_client_get_all_proc_addresses"
    " EGL_KHR_debug",
 
-   /* PlatformExtensionString */
+   .PlatformExtensionString =
 #ifdef HAVE_WAYLAND_PLATFORM
    " EGL_EXT_platform_wayland"
 #endif
@@ -71,12 +85,13 @@ struct _egl_global _eglGlobal =
 #ifdef HAVE_SURFACELESS_PLATFORM
    " EGL_MESA_platform_surfaceless"
 #endif
+   " EGL_EXT_platform_device"
    "",
 
-   NULL, /* ClientExtensionsString */
+   .ClientExtensionString = NULL,
 
-   NULL, /* debugCallback */
-   _EGL_DEBUG_BIT_CRITICAL | _EGL_DEBUG_BIT_ERROR, /* debugTypesEnabled */
+   .debugCallback = NULL,
+   .debugTypesEnabled = _EGL_DEBUG_BIT_CRITICAL | _EGL_DEBUG_BIT_ERROR,
 };
 
 
@@ -142,3 +157,40 @@ _eglGetClientExtensionString(void)
    mtx_unlock(_eglGlobal.Mutex);
    return ret;
 }
+
+EGLBoolean
+_eglPointerIsDereferencable(void *p)
+{
+   uintptr_t addr = (uintptr_t) p;
+   const long page_size = getpagesize();
+#ifdef HAVE_MINCORE
+   unsigned char valid = 0;
+
+   if (p == NULL)
+      return EGL_FALSE;
+
+   /* align addr to page_size */
+   addr &= ~(page_size - 1);
+
+   if (mincore((void *) addr, page_size, &valid) < 0) {
+      _eglLog(_EGL_DEBUG, "mincore failed: %m");
+      return EGL_FALSE;
+   }
+
+   /* mincore() returns 0 on success, and -1 on failure.  The last parameter
+    * is a vector of bytes with one entry for each page queried.  mincore
+    * returns page residency information in the first bit of each byte in the
+    * vector.
+    *
+    * Residency doesn't actually matter when determining whether a pointer is
+    * dereferenceable, so the output vector can be ignored.  What matters is
+    * whether mincore succeeds. See:
+    *
+    *   http://man7.org/linux/man-pages/man2/mincore.2.html
+    */
+   return EGL_TRUE;
+#else
+   // Without mincore(), we just assume that the first page is unmapped.
+   return addr >= page_size;
+#endif
+}