]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/mi: Fix segfault when attaching a rocm process with MI
authorLancelot Six <lancelot.six@amd.com>
Tue, 11 Feb 2025 22:47:50 +0000 (22:47 +0000)
committerLancelot Six <lancelot.six@amd.com>
Wed, 19 Feb 2025 16:52:25 +0000 (16:52 +0000)
When using the MI interpreter, if someone was to attach to a ROCm
process which has active GPU waves, GDB would issue a segfault as
follows:

    attach 1994813
    &"attach 1994813\n"
    ~"Attaching to process 1994813\n"
    =thread-group-started,id="i1",pid="1994813"
    =thread-created,id="1",group-id="i1"
    =thread-created,id="2",group-id="i1"
    ~"[New LWP 1994828]\n"
    *running,thread-id="2"
    =thread-created,id="3",group-id="i1"
    ~"[New LWP 1994825]\n"
    *running,thread-id="3"
    =thread-created,id="4",group-id="i1"
    ~"[New LWP 1994823]\n"
    *running,thread-id="4"
    ^done
    =library-loaded,...
    [...]
    ~"[Thread debugging using libthread_db enabled]\n"
    ~"Using host libthread_db library \"/lib/x86_64-linux-gnu/libthread_db.so.1\".\n"
    =thread-created,id="5",group-id="i1"
    &"\n\n"
    &"Fatal signal: "
    &"Segmentation fault"
    &"\n"
    &"----- Backtrace -----\n"
    &"Backtrace unavailable\n"
    &"---------------------\n"
    &"A fatal error internal to GDB has been detected, further\ndebugging is not possible.  GDB will now terminate.\n\n"
    &"This is a bug, please report it."
    &"  For instructions, see:\n"
    &"<https://github.com/ROCm-Developer-Tools/ROCgdb/issues>"
    &"."
    &"\n\n"
    Segmentation fault

The issue comes from using a non-initialized pointer in mi_on_resume_1:

    if (!mi->running_result_record_printed && mi->mi_proceeded)
      {
        gdb_printf (mi->raw_stdout, "%s^running\n",
                    mi->current_token ? mi->current_token : "");
      }

In this instance, "mi->current_token" has an uninitialized value.  This is a
regression introduced by:

    commit def2803789208a617c429b5dcf2026decb25ce0c
    Date:   Wed Sep 6 11:02:00 2023 -0400

        gdb/mi: make current_token a field of mi_interp

Before this patch, current_token was a global implicitly 0-initialized.  Since
it is now a class field, it is not 0-initialized by default anymore.  This
patch changes this.

Change-Id: I3f00b080318a70405d881ff0abe02b2c5cb1f9d8
Approved-By: Simon Marchi <simon.marchi@efficios.com>
Approved-By: Tom Tromey <tom@tromey.com>
gdb/mi/mi-interp.h
gdb/testsuite/gdb.rocm/mi-attach.cpp [new file with mode: 0644]
gdb/testsuite/gdb.rocm/mi-attach.exp [new file with mode: 0644]

index beff1c1a98c2d43901f910970eacb7051d7530e9..2f1bef99a903192cd341d7ef337f0374590fe845 100644 (file)
@@ -103,7 +103,7 @@ public:
      command was issued.  */
   int mi_proceeded;
 
-  const char *current_token;
+  const char *current_token = nullptr;
 };
 
 /* Output the shared object attributes to UIOUT.  */
diff --git a/gdb/testsuite/gdb.rocm/mi-attach.cpp b/gdb/testsuite/gdb.rocm/mi-attach.cpp
new file mode 100644 (file)
index 0000000..da7659d
--- /dev/null
@@ -0,0 +1,38 @@
+/* Copyright 2025 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <unistd.h>
+#include <hip/hip_runtime.h>
+
+__global__ void
+kern ()
+{
+  while (true)
+    __builtin_amdgcn_s_sleep (8);
+}
+
+int
+main ()
+{
+  /* This program will run outside of GDB, make sure that if anything goes
+     wrong it eventually gets killed.  */
+  alarm (30);
+
+  kern<<<1, 1>>> ();
+  return hipDeviceSynchronize () != hipSuccess;
+}
+
diff --git a/gdb/testsuite/gdb.rocm/mi-attach.exp b/gdb/testsuite/gdb.rocm/mi-attach.exp
new file mode 100644 (file)
index 0000000..2ca610c
--- /dev/null
@@ -0,0 +1,32 @@
+# Copyright 2025 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+load_lib mi-support.exp
+set MIFLAGS "-i=mi"
+
+require can_spawn_for_attach
+
+standard_testfile .cpp
+
+if {[build_executable "failed to prepare" $testfile $srcfile {debug hip}]} {
+    return
+}
+
+set spawn_id [spawn_wait_for_attach $::binfile]
+set prog_pid [spawn_id_get_pid $spawn_id]
+
+mi_clean_restart
+
+mi_gdb_test "-target-attach $prog_pid" ".*\\^done.*" "attach \$PROG_PID"