Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Bruno Larsen via Gdb-patches <gdb-patches@sourceware.org>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH v3 2/5] Introduce frame_info_ptr smart pointer class
Date: Mon, 25 Jul 2022 14:06:34 -0300	[thread overview]
Message-ID: <20220725170637.79699-3-blarsen@redhat.com> (raw)
In-Reply-To: <20220725170637.79699-1-blarsen@redhat.com>

From: Tom Tromey <tom@tromey.com>

This adds frame_info_ptr, a smart pointer class.  Every instance of
the class is kept on a circular, doubly-linked list.  When
reinit_frame_cache is called, the list is traversed and all the
pointers are invalidated.  This should help catch the typical GDB bug
of keeping a frame_info pointer alive where a frame ID was needed
instead.

Co-Authored-By: Bruno Larsen <blarsen@redhat.com>
---
 gdb/frame-info.h            | 180 ++++++++++++++++++++++++++++++++++++
 gdb/frame.c                 |   6 ++
 gdb/frame.h                 |   2 +
 gdbsupport/intrusive_list.h |  10 +-
 4 files changed, 193 insertions(+), 5 deletions(-)
 create mode 100644 gdb/frame-info.h

diff --git a/gdb/frame-info.h b/gdb/frame-info.h
new file mode 100644
index 00000000000..ecbf4a56545
--- /dev/null
+++ b/gdb/frame-info.h
@@ -0,0 +1,180 @@
+/* Frame info pointer
+
+   Copyright (C) 2022 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/>.  */
+
+#ifndef GDB_FRAME_INFO_H
+#define GDB_FRAME_INFO_H
+
+#include "gdbsupport/intrusive_list.h"
+
+struct frame_info;
+
+extern void reinit_frame_cache ();
+
+/* A wrapper for "frame_info *".  frame_info objects are invalidated
+   whenever reinit_frame_cache is called.  This class arranges to
+   invalidate the pointer when appropriate.  This is done to help
+   detect a GDB bug that was relatively common.
+
+   A small amount of code must still operate on raw pointers, so a
+   "get" method is provided.  However, you should normally not use
+   this in new code.  */
+
+class frame_info_ptr : public intrusive_list_node<frame_info_ptr>
+{
+public:
+  /* Create a frame_info_ptr from a raw pointer.  */
+  explicit frame_info_ptr (struct frame_info *ptr)
+    : m_ptr (ptr)
+  {
+    root.push_back (*this);
+  }
+
+  /* Create a null frame_info_ptr.  */
+  frame_info_ptr ()
+  {
+    root.push_back (*this);
+  }
+
+  frame_info_ptr (std::nullptr_t)
+  {
+    root.push_back (*this);
+  }
+
+  frame_info_ptr (const frame_info_ptr &other)
+    : m_ptr (other.m_ptr)
+  {
+    root.push_back (*this);
+  }
+
+  frame_info_ptr (frame_info_ptr &&other)
+    : m_ptr (other.m_ptr)
+  {
+    other.m_ptr = nullptr;
+    root.push_back (*this);
+  }
+
+  ~frame_info_ptr ()
+  {
+    root.erase (*this);
+  }
+
+  frame_info_ptr &operator= (const frame_info_ptr &other)
+  {
+    m_ptr = other.m_ptr;
+    return *this;
+  }
+
+  frame_info_ptr &operator= (std::nullptr_t)
+  {
+    m_ptr = nullptr;
+    return *this;
+  }
+
+  frame_info_ptr &operator= (frame_info_ptr &&other)
+  {
+    m_ptr = other.m_ptr;
+    other.m_ptr = nullptr;
+    return *this;
+  }
+
+  frame_info *operator-> () const
+  {
+    return m_ptr;
+  }
+
+  /* Fetch the underlying pointer.  Note that new code should
+     generally not use this -- avoid it if at all possible.  */
+  frame_info *get () const
+  {
+    return m_ptr;
+  }
+
+  /* This exists for compatibility with pre-existing code that checked
+     a "frame_info *" using "!".  */
+  bool operator! () const
+  {
+    return m_ptr == nullptr;
+  }
+
+  /* This exists for compatibility with pre-existing code that checked
+     a "frame_info *" like "if (ptr)".  */
+  explicit operator bool () const
+  {
+    return m_ptr != nullptr;
+  }
+
+  /* Invalidate this pointer.  */
+  void invalidate ()
+  {
+    m_ptr = nullptr;
+  }
+
+private:
+
+  /* The underlying pointer.  */
+  frame_info *m_ptr = nullptr;
+
+  /* All frame_info_ptr objects are kept on a circular doubly-linked
+     list.  This keeps their construction and destruction costs
+     reasonably small.  To make the implementation a little simpler,
+     we guarantee that there is always at least one object on the list
+     -- this "root".  */
+  static intrusive_list<frame_info_ptr> root;
+
+  /* A friend so it can invalidate the pointers.  */
+  friend void reinit_frame_cache ();
+};
+
+static inline bool
+operator== (const frame_info *self, const frame_info_ptr &other)
+{
+  return self == other.get ();
+}
+
+static inline bool
+operator== (const frame_info_ptr &self, const frame_info_ptr &other)
+{
+  return self.get () == other.get ();
+}
+
+static inline bool
+operator== (const frame_info_ptr &self, const frame_info *other)
+{
+  return self.get () == other;
+}
+
+static inline bool
+operator!= (const frame_info *self, const frame_info_ptr &other)
+{
+  return self != other.get ();
+}
+
+static inline bool
+operator!= (const frame_info_ptr &self, const frame_info_ptr &other)
+{
+  return self.get () != other.get ();
+}
+
+static inline bool
+operator!= (const frame_info_ptr &self, const frame_info *other)
+{
+  return self.get () != other;
+}
+
+#endif /* GDB_FRAME_INFO_H */
diff --git a/gdb/frame.c b/gdb/frame.c
index c0cf3d585bf..7d62e01701d 100644
--- a/gdb/frame.c
+++ b/gdb/frame.c
@@ -56,6 +56,9 @@ static struct frame_info *sentinel_frame;
 /* Number of calls to reinit_frame_cache.  */
 static unsigned int frame_cache_generation = 0;
 
+/* See frame-info.h.  */
+intrusive_list<frame_info_ptr> frame_info_ptr::root;
+
 /* See frame.h.  */
 
 unsigned int
@@ -2006,6 +2009,9 @@ reinit_frame_cache (void)
   select_frame (NULL);
   frame_stash_invalidate ();
 
+  for (frame_info_ptr &iter : frame_info_ptr::root)
+    iter.invalidate ();
+
   frame_debug_printf ("generation=%d", frame_cache_generation);
 }
 
diff --git a/gdb/frame.h b/gdb/frame.h
index 75bb3bd2aa0..9ad2599331f 100644
--- a/gdb/frame.h
+++ b/gdb/frame.h
@@ -20,6 +20,8 @@
 #if !defined (FRAME_H)
 #define FRAME_H 1
 
+#include "frame-info.h"
+
 /* The following is the intended naming schema for frame functions.
    It isn't 100% consistent, but it is approaching that.  Frame naming
    schema:
diff --git a/gdbsupport/intrusive_list.h b/gdbsupport/intrusive_list.h
index 6812266159a..48b2123582f 100644
--- a/gdbsupport/intrusive_list.h
+++ b/gdbsupport/intrusive_list.h
@@ -391,13 +391,13 @@ class intrusive_list
   void pop_front ()
   {
     gdb_assert (!this->empty ());
-    erase_element (*m_front);
+    erase (*m_front);
   }
 
   void pop_back ()
   {
     gdb_assert (!this->empty ());
-    erase_element (*m_back);
+    erase (*m_back);
   }
 
 private:
@@ -451,7 +451,8 @@ class intrusive_list
     m_back = &elem;
   }
 
-  void erase_element (T &elem)
+public:
+  void erase (T &elem)
   {
     intrusive_list_node<T> *elem_node = as_node (&elem);
 
@@ -486,7 +487,6 @@ class intrusive_list
     elem_node->prev = INTRUSIVE_LIST_UNLINKED_VALUE;
   }
 
-public:
   /* Remove the element pointed by I from the list.  The element
      pointed by I is not destroyed.  */
   iterator erase (const_iterator i)
@@ -494,7 +494,7 @@ class intrusive_list
     iterator ret = i;
     ++ret;
 
-    erase_element (*i);
+    erase (*i);
 
     return ret;
   }
-- 
2.31.1


  parent reply	other threads:[~2022-07-25 17:07 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-25 17:06 [PATCH v3 0/5] Smart pointer wrapper for frame_info Bruno Larsen via Gdb-patches
2022-07-25 17:06 ` [PATCH v3 1/5] Remove frame_id_eq Bruno Larsen via Gdb-patches
2022-07-25 17:06 ` Bruno Larsen via Gdb-patches [this message]
2022-07-25 17:52   ` [PATCH v3 2/5] Introduce frame_info_ptr smart pointer class Pedro Alves
2022-08-24 14:24     ` Bruno Larsen via Gdb-patches
2022-08-24 15:20       ` Pedro Alves
2022-08-24 15:49         ` Bruno Larsen via Gdb-patches
2022-07-25 17:06 ` [PATCH v3 3/5] Change GDB to use frame_info_ptr Bruno Larsen via Gdb-patches
2022-07-25 17:06 ` [PATCH v3 4/5] Continue making GDB " Bruno Larsen via Gdb-patches
2022-07-28 16:44   ` Andrew Burgess via Gdb-patches
2022-07-25 17:06 ` [PATCH v3 5/5] gdb/frame: Add reinflation method for frame_info_ptr Bruno Larsen via Gdb-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220725170637.79699-3-blarsen@redhat.com \
    --to=gdb-patches@sourceware.org \
    --cc=blarsen@redhat.com \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox