intel: Fix stack overflow in intel_dump_gpu
authorBenjamin Lee <benjamin@computer.surgery>
Sat, 3 Jun 2023 22:53:33 +0000 (15:53 -0700)
committerMarge Bot <emma+marge@anholt.net>
Tue, 6 Jun 2023 11:17:06 +0000 (11:17 +0000)
Previously, the call to ensure_device_info in the intercepted ioctl
would eventually result in another call to ioctl, recursing until stack
overflow:

 - ioctl (intercepted)
 - ensure_device_info
 - intel_get_device_info_from_fd
 - intel_device_info_i915_get_info_from_fd
 - getparam
 - intel_ioctl
 - ioctl (intercepted)

Signed-off-by: Benjamin Lee <benjamin@computer.surgery>
Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23418>

src/intel/tools/intel_dump_gpu.c

index 71f29ed..2e047cd 100644 (file)
@@ -43,6 +43,7 @@
 #include "intel_aub.h"
 #include "aub_write.h"
 
+#include "c11/threads.h"
 #include "dev/intel_debug.h"
 #include "dev/intel_device_info.h"
 #include "common/intel_gem.h"
@@ -488,8 +489,8 @@ maybe_init(int fd)
              output_filename, device, devinfo.ver);
 }
 
-__attribute__ ((visibility ("default"))) int
-ioctl(int fd, unsigned long request, ...)
+static int
+intercept_ioctl(int fd, unsigned long request, ...)
 {
    va_list args;
    void *argp;
@@ -734,6 +735,31 @@ ioctl(int fd, unsigned long request, ...)
    }
 }
 
+__attribute__ ((visibility ("default"))) int
+ioctl(int fd, unsigned long request, ...)
+{
+   static thread_local bool entered = false;
+   va_list args;
+   void *argp;
+   int ret;
+
+   va_start(args, request);
+   argp = va_arg(args, void *);
+   va_end(args);
+
+   /* Some of the functions called by intercept_ioctl call ioctls of their
+    * own. These need to go to the libc ioctl instead of being passed back to
+    * intercept_ioctl to avoid a stack overflow. */
+   if (entered) {
+      return libc_ioctl(fd, request, argp);
+   } else {
+      entered = true;
+      ret = intercept_ioctl(fd, request, argp);
+      entered = false;
+      return ret;
+   }
+}
+
 static void
 init(void)
 {