This is the mail archive of the gdb-cvs@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[binutils-gdb] Fix broken recursion detection when printing static members


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=a43f3893f6cb66dfca7f628cd159a008009ad06f

commit a43f3893f6cb66dfca7f628cd159a008009ad06f
Author: Patrick Frants <osscontribute@gmail.com>
Date:   Thu Oct 26 22:26:08 2017 -0400

    Fix broken recursion detection when printing static members
    
    Recursion detection for static members was broken.  The implementation
    uses a growing (and shrinking) obstack object to simulate a stack of
    addresses (CORE_ADDR).  Pushing addresses is implemented by calling
    obstack_grow(), while popping is implemented by calling obstack_free().
    The latter is problematic because obstack_free() expects a pointer to
    the base of an object.  When popping elements of the stack however,
    obstack_free() was called with the new top, which potentially is not the
    same as the base of the stack.  This is unintended use and the effect is
    that obstack->next_free and obstack->object_base members are assigned
    the value of the new top, which equals an empty stack.  Summary: popping
    elements would always result in an empty stack, which breaks the
    recursion detection.
    
    The fix shrinks the stack using obstack_blank_fast() with a negative
    value as described at the bottom of this page:
    https://gcc.gnu.org/onlinedocs/libiberty/Extra-Fast-Growing.html "You
    can use obstack_blank_fast with a â??negativeâ?? size argument to make the
    current object smaller.  Just donâ??t try to shrink it beyond zero
    lengthâ??thereâ??s no telling what will happen if you do that. Earlier
    versions of obstacks allowed you to use obstack_blank to shrink objects.
    This will no longer work."
    
    The reproducer is added to gdb.cp/classes.exp, which fails without this
    patch.
    
    gdb/ChangeLog:
    
    	* cp-valprint.c (cp_print_value_fields): Use obstack_blank_fast
    	to rewind obstack.
    
    gdb/testsuite/ChangeLog:
    
    	* gdb.cp/classes.exp (test_static_members): Test printing
    	Outer::instance.
    	* gdb.cp/classes.c (struct Inner, struct Outer): New.
    	(Inner::instance, Outer::instance): New.

Diff:
---
 gdb/ChangeLog                    |  5 +++++
 gdb/cp-valprint.c                | 10 +++-------
 gdb/testsuite/ChangeLog          |  7 +++++++
 gdb/testsuite/gdb.cp/classes.cc  | 13 +++++++++++++
 gdb/testsuite/gdb.cp/classes.exp |  5 +++++
 5 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index caa6484..1d18881 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2017-10-26  Patrick Frants  <osscontribute@gmail.com>
+
+	* cp-valprint.c (cp_print_value_fields): Use obstack_blank_fast
+	to rewind obstack.
+
 2017-10-26  Pedro Alves  <palves@redhat.com>
 
 	* remote.c (remote_async_terminal_ours_p): Delete.
diff --git a/gdb/cp-valprint.c b/gdb/cp-valprint.c
index fb9bfd9..609dabb 100644
--- a/gdb/cp-valprint.c
+++ b/gdb/cp-valprint.c
@@ -371,13 +371,9 @@ cp_print_value_fields (struct type *type, struct type *real_type,
 	  if (obstack_final_size > statmem_obstack_initial_size)
 	    {
 	      /* In effect, a pop of the printed-statics stack.  */
-
-	      void *free_to_ptr =
-		(char *) obstack_next_free (&dont_print_statmem_obstack) -
-		(obstack_final_size - statmem_obstack_initial_size);
-
-	      obstack_free (&dont_print_statmem_obstack,
-			    free_to_ptr);
+	      size_t shrink_bytes
+		= statmem_obstack_initial_size - obstack_final_size;
+	      obstack_blank_fast (&dont_print_statmem_obstack, shrink_bytes);
 	    }
 
 	  if (last_set_recurse != recurse)
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 0c6c93a..0e2bc23 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,10 @@
+2017-10-26  Patrick Frants  <osscontribute@gmail.com>
+
+	* gdb.cp/classes.exp (test_static_members): Test printing
+	Outer::instance.
+	* gdb.cp/classes.c (struct Inner, struct Outer): New.
+	(Inner::instance, Outer::instance): New.
+
 2017-10-24  Pedro Alves  <palves@redhat.com>
 
 	* gdb.base/new-ui.exp (do_test): Split "delete all breakpoints on
diff --git a/gdb/testsuite/gdb.cp/classes.cc b/gdb/testsuite/gdb.cp/classes.cc
index 50f0740..a650846 100644
--- a/gdb/testsuite/gdb.cp/classes.cc
+++ b/gdb/testsuite/gdb.cp/classes.cc
@@ -665,6 +665,19 @@ void use_methods ()
   base1 b (3);
 }
 
+struct Inner
+{
+  static Inner instance;
+};
+
+struct Outer
+{
+  Inner inner;
+  static Outer instance;
+};
+
+Inner Inner::instance;
+Outer Outer::instance;
 
 int
 main()
diff --git a/gdb/testsuite/gdb.cp/classes.exp b/gdb/testsuite/gdb.cp/classes.exp
index 9e2630a..374b632 100644
--- a/gdb/testsuite/gdb.cp/classes.exp
+++ b/gdb/testsuite/gdb.cp/classes.exp
@@ -602,6 +602,11 @@ proc test_static_members {} {
     gdb_test "print cnsi" \
 	"{x = 30, y = 40, static null = {x = 0, y = 0, static null = <same as static member of an already seen type>, static yy = {z = 5, static xx = {x = 1, y = 2, static null = <same as static member of an already seen type>, static yy = <same as static member of an already seen type>}}}, static yy = <same as static member of an already seen type>}" \
 	"print cnsi with static members"
+
+    # Another case of infinite recursion.
+    gdb_test "print Outer::instance" \
+	"{inner = {static instance = {static instance = <same as static member of an already seen type>}}, static instance = {inner = {static instance = {static instance = <same as static member of an already seen type>}}, static instance = <same as static member of an already seen type>}}" \
+	"print recursive static member"
 }
 
 proc do_tests {} {


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]