[gdb/build] Fix frame_list position in frame.c

Message ID 20230503175826.4242-1-tdevries@suse.de
State Committed
Headers
Series [gdb/build] Fix frame_list position in frame.c |

Commit Message

Tom de Vries May 3, 2023, 5:58 p.m. UTC
  In commit 995a34b1772 ("Guard against frame.c destructors running before
frame-info.c's") the following problem was addressed.

The frame_info_ptr destructor:
...
  ~frame_info_ptr ()
  {
    frame_list.erase (frame_list.iterator_to (*this));
  }
...
uses frame_list, which is a static member of class frame_info_ptr,
instantiated in frame-info.c:
...
intrusive_list<frame_info_ptr> frame_info_ptr::frame_list;
...

Then there's a static frame_info_pointer variable named selected_frame in
frame.c:
...
static frame_info_ptr selected_frame;
...

Because the destructor of selected_frame uses frame_list, its destructor needs
to be called before the destructor of frame_list.

But because they're in different compilation units, the initialization order and
consequently destruction order is not guarantueed.

The commit fixed this by handling the case that the destructor of frame_list
is called first, adding a check on is_linked ():
...
   ~frame_info_ptr ()
   {
-    frame_list.erase (frame_list.iterator_to (*this));
+    /* If this node has static storage, it may be deleted after
+       frame_list.  Attempting to erase ourselves would then trigger
+       internal errors, so make sure we are still linked first.  */
+    if (is_linked ())
+      frame_list.erase (frame_list.iterator_to (*this));
   }
...

However, since then frame_list has been moved into frame.c, and
initialization/destruction order is guarantueed inside a compilation unit.

Revert aforementioned commit, and fix the destruction order problem by moving
frame_list before selected_frame.

Reverting the commit is another way of fixing the already fixed
Wdangling-pointer warning reported in PR build/30413, in a different way than
commit 9b0ccb1ebae ("Pass const frame_info_ptr reference for
skip_[language_]trampoline").

Tested on x86_64-linux.

PR build/30413
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30413
---
 gdb/frame.c | 11 +++++++----
 gdb/frame.h |  9 ++++-----
 2 files changed, 11 insertions(+), 9 deletions(-)


base-commit: 2ad00a4b42f89b61fdab24940b67713daf81c988
  

Comments

Simon Marchi May 3, 2023, 6:47 p.m. UTC | #1
On 5/3/23 13:58, Tom de Vries wrote:
> In commit 995a34b1772 ("Guard against frame.c destructors running before
> frame-info.c's") the following problem was addressed.
> 
> The frame_info_ptr destructor:
> ...
>   ~frame_info_ptr ()
>   {
>     frame_list.erase (frame_list.iterator_to (*this));
>   }
> ...
> uses frame_list, which is a static member of class frame_info_ptr,
> instantiated in frame-info.c:
> ...
> intrusive_list<frame_info_ptr> frame_info_ptr::frame_list;
> ...
> 
> Then there's a static frame_info_pointer variable named selected_frame in
> frame.c:
> ...
> static frame_info_ptr selected_frame;
> ...
> 
> Because the destructor of selected_frame uses frame_list, its destructor needs
> to be called before the destructor of frame_list.
> 
> But because they're in different compilation units, the initialization order and
> consequently destruction order is not guarantueed.
> 
> The commit fixed this by handling the case that the destructor of frame_list
> is called first, adding a check on is_linked ():
> ...
>    ~frame_info_ptr ()
>    {
> -    frame_list.erase (frame_list.iterator_to (*this));
> +    /* If this node has static storage, it may be deleted after
> +       frame_list.  Attempting to erase ourselves would then trigger
> +       internal errors, so make sure we are still linked first.  */
> +    if (is_linked ())
> +      frame_list.erase (frame_list.iterator_to (*this));
>    }
> ...
> 
> However, since then frame_list has been moved into frame.c, and
> initialization/destruction order is guarantueed inside a compilation unit.
> 
> Revert aforementioned commit, and fix the destruction order problem by moving
> frame_list before selected_frame.
> 
> Reverting the commit is another way of fixing the already fixed
> Wdangling-pointer warning reported in PR build/30413, in a different way than
> commit 9b0ccb1ebae ("Pass const frame_info_ptr reference for
> skip_[language_]trampoline").
> 
> Tested on x86_64-linux.
> 
> PR build/30413
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30413
> ---
>  gdb/frame.c | 11 +++++++----
>  gdb/frame.h |  9 ++++-----
>  2 files changed, 11 insertions(+), 9 deletions(-)
> 
> diff --git a/gdb/frame.c b/gdb/frame.c
> index 36fb02f3c8e..531eadf3d54 100644
> --- a/gdb/frame.c
> +++ b/gdb/frame.c
> @@ -1733,6 +1733,13 @@ get_current_frame (void)
>  static frame_id selected_frame_id = null_frame_id;
>  static int selected_frame_level = -1;
>  
> +/* See frame.h.  This definition should come before any definition of a static
> +   frame_info_ptr, to ensure that frame_list is destroyed after any static
> +   frame_info_ptr.  This is  necessary because the destructor of frame_info_ptr

Spurious double space.

> +   uses frame_list.  */
> +
> +intrusive_list<frame_info_ptr> frame_info_ptr::frame_list;
> +
>  /* The cached frame_info object pointing to the selected frame.
>     Looked up on demand by get_selected_frame.  */
>  static frame_info_ptr selected_frame;
> @@ -3275,10 +3282,6 @@ maintenance_print_frame_id (const char *args, int from_tty)
>  
>  /* See frame-info-ptr.h.  */
>  
> -intrusive_list<frame_info_ptr> frame_info_ptr::frame_list;
> -
> -/* See frame-info-ptr.h.  */
> -
>  frame_info_ptr::frame_info_ptr (struct frame_info *ptr)
>    : m_ptr (ptr)
>  {
> diff --git a/gdb/frame.h b/gdb/frame.h
> index 6ed8db0af56..ed19dfdc090 100644
> --- a/gdb/frame.h
> +++ b/gdb/frame.h
> @@ -254,11 +254,10 @@ class frame_info_ptr : public intrusive_list_node<frame_info_ptr>
>  
>    ~frame_info_ptr ()
>    {
> -    /* If this node has static storage, it may be deleted after
> -       frame_list.  Attempting to erase ourselves would then trigger
> -       internal errors, so make sure we are still linked first.  */
> -    if (is_linked ())
> -      frame_list.erase (frame_list.iterator_to (*this));
> +    /* If this node has static storage, it should be be deleted before
> +       frame_list.  Verify this by checking that it is still in the list.  */
> +    gdb_assert (is_linked ());
> +    frame_list.erase (frame_list.iterator_to (*this));

The assert is a bit redundant with the assertions in
intrusive_list::erase_element:

    gdb_assert (elem_node->prev != INTRUSIVE_LIST_UNLINKED_VALUE);
    gdb_assert (elem_node->next != INTRUSIVE_LIST_UNLINKED_VALUE);

I would maybe remove the assert, but keep the comment (at least the
first sentence)?

In any case, this LGTM, thanks for doing this.

Approved-By: Simon Marchi <simon.marchi@efficios.com>

Simon
  
Tom de Vries May 3, 2023, 7:45 p.m. UTC | #2
On 5/3/23 20:47, Simon Marchi wrote:
> On 5/3/23 13:58, Tom de Vries wrote:
>> In commit 995a34b1772 ("Guard against frame.c destructors running before
>> frame-info.c's") the following problem was addressed.
>>
>> The frame_info_ptr destructor:
>> ...
>>    ~frame_info_ptr ()
>>    {
>>      frame_list.erase (frame_list.iterator_to (*this));
>>    }
>> ...
>> uses frame_list, which is a static member of class frame_info_ptr,
>> instantiated in frame-info.c:
>> ...
>> intrusive_list<frame_info_ptr> frame_info_ptr::frame_list;
>> ...
>>
>> Then there's a static frame_info_pointer variable named selected_frame in
>> frame.c:
>> ...
>> static frame_info_ptr selected_frame;
>> ...
>>
>> Because the destructor of selected_frame uses frame_list, its destructor needs
>> to be called before the destructor of frame_list.
>>
>> But because they're in different compilation units, the initialization order and
>> consequently destruction order is not guarantueed.
>>
>> The commit fixed this by handling the case that the destructor of frame_list
>> is called first, adding a check on is_linked ():
>> ...
>>     ~frame_info_ptr ()
>>     {
>> -    frame_list.erase (frame_list.iterator_to (*this));
>> +    /* If this node has static storage, it may be deleted after
>> +       frame_list.  Attempting to erase ourselves would then trigger
>> +       internal errors, so make sure we are still linked first.  */
>> +    if (is_linked ())
>> +      frame_list.erase (frame_list.iterator_to (*this));
>>     }
>> ...
>>
>> However, since then frame_list has been moved into frame.c, and
>> initialization/destruction order is guarantueed inside a compilation unit.
>>
>> Revert aforementioned commit, and fix the destruction order problem by moving
>> frame_list before selected_frame.
>>
>> Reverting the commit is another way of fixing the already fixed
>> Wdangling-pointer warning reported in PR build/30413, in a different way than
>> commit 9b0ccb1ebae ("Pass const frame_info_ptr reference for
>> skip_[language_]trampoline").
>>
>> Tested on x86_64-linux.
>>
>> PR build/30413
>> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30413
>> ---
>>   gdb/frame.c | 11 +++++++----
>>   gdb/frame.h |  9 ++++-----
>>   2 files changed, 11 insertions(+), 9 deletions(-)
>>
>> diff --git a/gdb/frame.c b/gdb/frame.c
>> index 36fb02f3c8e..531eadf3d54 100644
>> --- a/gdb/frame.c
>> +++ b/gdb/frame.c
>> @@ -1733,6 +1733,13 @@ get_current_frame (void)
>>   static frame_id selected_frame_id = null_frame_id;
>>   static int selected_frame_level = -1;
>>   
>> +/* See frame.h.  This definition should come before any definition of a static
>> +   frame_info_ptr, to ensure that frame_list is destroyed after any static
>> +   frame_info_ptr.  This is  necessary because the destructor of frame_info_ptr
> 
> Spurious double space.
> 

Fixed.

>> +   uses frame_list.  */
>> +
>> +intrusive_list<frame_info_ptr> frame_info_ptr::frame_list;
>> +
>>   /* The cached frame_info object pointing to the selected frame.
>>      Looked up on demand by get_selected_frame.  */
>>   static frame_info_ptr selected_frame;
>> @@ -3275,10 +3282,6 @@ maintenance_print_frame_id (const char *args, int from_tty)
>>   
>>   /* See frame-info-ptr.h.  */
>>   
>> -intrusive_list<frame_info_ptr> frame_info_ptr::frame_list;
>> -
>> -/* See frame-info-ptr.h.  */
>> -
>>   frame_info_ptr::frame_info_ptr (struct frame_info *ptr)
>>     : m_ptr (ptr)
>>   {
>> diff --git a/gdb/frame.h b/gdb/frame.h
>> index 6ed8db0af56..ed19dfdc090 100644
>> --- a/gdb/frame.h
>> +++ b/gdb/frame.h
>> @@ -254,11 +254,10 @@ class frame_info_ptr : public intrusive_list_node<frame_info_ptr>
>>   
>>     ~frame_info_ptr ()
>>     {
>> -    /* If this node has static storage, it may be deleted after
>> -       frame_list.  Attempting to erase ourselves would then trigger
>> -       internal errors, so make sure we are still linked first.  */
>> -    if (is_linked ())
>> -      frame_list.erase (frame_list.iterator_to (*this));
>> +    /* If this node has static storage, it should be be deleted before
>> +       frame_list.  Verify this by checking that it is still in the list.  */
>> +    gdb_assert (is_linked ());
>> +    frame_list.erase (frame_list.iterator_to (*this));
> 
> The assert is a bit redundant with the assertions in
> intrusive_list::erase_element:
> 
>      gdb_assert (elem_node->prev != INTRUSIVE_LIST_UNLINKED_VALUE);
>      gdb_assert (elem_node->next != INTRUSIVE_LIST_UNLINKED_VALUE);
> 
> I would maybe remove the assert, but keep the comment (at least the
> first sentence)?
> 

Ack, I checked by doing:
...
+static frame_info_ptr bad_frame;
  intrusive_list<frame_info_ptr> frame_info_ptr::frame_list;
...
and indeed those assertions trigger, so done.

> In any case, this LGTM, thanks for doing this.
> 
> Approved-By: Simon Marchi <simon.marchi@efficios.com>
> 

Committed, thanks for the review.

- Tom
  
Kévin Le Gouguec May 4, 2023, 9:01 a.m. UTC | #3
Tom de Vries <tdevries@suse.de> writes:

> On 5/3/23 20:47, Simon Marchi wrote:
>
>> In any case, this LGTM, thanks for doing this.

Seconded, thanks for getting rid of that if guard.  I find the new state
of affairs (everything defined in the same unit, in the correct order)
easier to grok.
  
Tom de Vries May 4, 2023, 9:12 a.m. UTC | #4
On 5/4/23 11:01, Kévin Le Gouguec wrote:
> Tom de Vries <tdevries@suse.de> writes:
> 
>> On 5/3/23 20:47, Simon Marchi wrote:
>>
>>> In any case, this LGTM, thanks for doing this.
> 
> Seconded, thanks for getting rid of that if guard.  I find the new state
> of affairs (everything defined in the same unit, in the correct order)
> easier to grok.

Hi Kévin,

thanks for the review and the confirmation.

- Tom
  

Patch

diff --git a/gdb/frame.c b/gdb/frame.c
index 36fb02f3c8e..531eadf3d54 100644
--- a/gdb/frame.c
+++ b/gdb/frame.c
@@ -1733,6 +1733,13 @@  get_current_frame (void)
 static frame_id selected_frame_id = null_frame_id;
 static int selected_frame_level = -1;
 
+/* See frame.h.  This definition should come before any definition of a static
+   frame_info_ptr, to ensure that frame_list is destroyed after any static
+   frame_info_ptr.  This is  necessary because the destructor of frame_info_ptr
+   uses frame_list.  */
+
+intrusive_list<frame_info_ptr> frame_info_ptr::frame_list;
+
 /* The cached frame_info object pointing to the selected frame.
    Looked up on demand by get_selected_frame.  */
 static frame_info_ptr selected_frame;
@@ -3275,10 +3282,6 @@  maintenance_print_frame_id (const char *args, int from_tty)
 
 /* See frame-info-ptr.h.  */
 
-intrusive_list<frame_info_ptr> frame_info_ptr::frame_list;
-
-/* See frame-info-ptr.h.  */
-
 frame_info_ptr::frame_info_ptr (struct frame_info *ptr)
   : m_ptr (ptr)
 {
diff --git a/gdb/frame.h b/gdb/frame.h
index 6ed8db0af56..ed19dfdc090 100644
--- a/gdb/frame.h
+++ b/gdb/frame.h
@@ -254,11 +254,10 @@  class frame_info_ptr : public intrusive_list_node<frame_info_ptr>
 
   ~frame_info_ptr ()
   {
-    /* If this node has static storage, it may be deleted after
-       frame_list.  Attempting to erase ourselves would then trigger
-       internal errors, so make sure we are still linked first.  */
-    if (is_linked ())
-      frame_list.erase (frame_list.iterator_to (*this));
+    /* If this node has static storage, it should be be deleted before
+       frame_list.  Verify this by checking that it is still in the list.  */
+    gdb_assert (is_linked ());
+    frame_list.erase (frame_list.iterator_to (*this));
   }
 
   frame_info_ptr &operator= (const frame_info_ptr &other)