Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@ericsson.com>
To: Patrick Frants <osscontribute@gmail.com>, <gdb-patches@sourceware.org>
Subject: Re: [PATCH] Fix faulty use of obstack_free() to *shrink* dont_print_statmem_obstack. Instead use obstack_blank_fast() with a "negative" size. A real stack data structured would be appropriate here. Added unit test gdb/testsuite/gdb.cp/printstaticrecursion.exp.
Date: Mon, 23 Oct 2017 20:04:00 -0000	[thread overview]
Message-ID: <74923492-b63d-b4fa-9682-3394fe47184c@ericsson.com> (raw)
In-Reply-To: <6cc773d7-ee68-5f5c-46f4-cc4a044c9d3e@ericsson.com>

On 2017-10-23 03:37 PM, Simon Marchi wrote:
> I've stepped over the obstack_free call and noticed something strange, it changes the
> value of obstack::object_base...
> 
> (top-gdb) p dont_print_statmem_obstack
> $6 = {chunk_size = 256, chunk = 0x3b8b5a0, object_base = 0x3b8b5b0 "2\020`",
>   next_free = 0x3b8sb5c0 "0\270\270\003", chunk_limit = 0x3b8b6a0 "p\265\270\003", temp = {i = 0,
>     p = 0x0}, alignment_mask = 15, chunkfun = {plain = 0x798604 <xmalloc(size_t)>,
>     extra = 0x798604 <xmalloc(size_t)>}, freefun = {plain = 0x798732 <xfree(void*)>,
>     extra = 0x798732 <xfree(void*)>}, extra_arg = 0x0, use_extra_arg = 0, maybe_empty_object = 0,
>   alloc_failed = 0}
> (top-gdb) p free_to_ptr
> $7 = (void *) 0x3b8b5b8
> (top-gdb) n
> 383		  if (last_set_recurse != recurse)
> (top-gdb) p dont_print_statmem_obstack
> $8 = {chunk_size = 256, chunk = 0x3b8b5a0, object_base = 0x3b8b5b8 "1\020`",
>   next_free = 0x3b8b5b8 "1\020`", chunk_limit = 0x3b8b6a0 "p\265\270\003", temp = {i = 0,
>     p = 0x0}, alignment_mask = 15, chunkfun = {plain = 0x798604 <xmalloc(size_t)>,
>     extra = 0x798604 <xmalloc(size_t)>}, freefun = {plain = 0x798732 <xfree(void*)>,
>     extra = 0x798732 <xfree(void*)>}, extra_arg = 0x0, use_extra_arg = 0, maybe_empty_object = 0,
>   alloc_failed = 0}
> 
> 
> As you can see, object_base goes from 0x3b8b5b0 to 0x3b8b5b8.  And indeed, looking at obstack_free,
> I see:
> 
>        if (__obj > (void *) __o->chunk && __obj < (void *) __o->chunk_limit)  \
> 	 __o->next_free = __o->object_base = (char *) __obj;		      \
> 
> So when you free, it resets object_base to that point... why does it do that?  It doesn't make sense
> to me.
> 
> At least, that seems to explain why the obstack is empty after having called
> obstack_free, even though we didn't ask it to free the whole obstack.
> 
> Simon
> 

After following a crash course about obstacks on IRC (thanks Tom!), I understood that obstack_free
is normally used to free completed objects.  object_base points to the beginning of the object
currently being constructed, so obstack_free sets it to the beginning of the newly freed area,
so that the next object to be constructed will be placed there.

In the libc manual [1], it is stated clearly that obstack_blank should be used with a negative
size to make the current object smaller:

  You can use obstack_blank 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.

For negative sizes, obstack_blank seems equivalent to obstack_blank_fast.  So I am now
pretty much convinced that your fix is good, in that it does what the original code
intended (but failed) to do.  Could you address the comments from my previous mails
and submit a new version?  While at it, I noticed there is a missing space in:

  obstack_blank_fast(&dont_print_statmem_obstack, shrink_bytes);

before the opening parenthesis.

Thanks!

Simon

[1] https://www.gnu.org/software/libc/manual/html_node/Growing-Objects.html#Growing-Objects


      parent reply	other threads:[~2017-10-23 20:04 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-19  9:31 Patrick Frants
2017-10-19 10:18 ` Patrick Frants
2017-10-23 16:09 ` Simon Marchi
2017-10-23 17:17   ` Simon Marchi
2017-10-23 19:37     ` Simon Marchi
2017-10-23 19:55       ` osscontribute
2017-10-23 20:07         ` Simon Marchi
2017-10-23 20:04       ` Simon Marchi [this message]

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=74923492-b63d-b4fa-9682-3394fe47184c@ericsson.com \
    --to=simon.marchi@ericsson.com \
    --cc=gdb-patches@sourceware.org \
    --cc=osscontribute@gmail.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