binutils-gdb/gdb/testsuite/gdb.python
Simon Marchi 6d088eb92e gdb: don't copy entirely optimized out values in value_copy
Bug 28980 shows that trying to value_copy an entirely optimized out
value causes an internal error.  The original bug report involves MI and
some Python pretty printer, and is quite difficult to reproduce, but
another easy way to reproduce (that is believed to be equivalent) was
proposed:

    $ ./gdb -q -nx --data-directory=data-directory -ex "py print(gdb.Value(gdb.Value(5).type.optimized_out()))"
    /home/smarchi/src/binutils-gdb/gdb/value.c:1731: internal-error: value_copy: Assertion `arg->contents != nullptr' failed.

This is caused by 5f8ab46bc6 ("gdb: constify parameter of
value_copy").  It added an assertion that the contents buffer is
allocated if the value is not lazy:

  if (!value_lazy (val))
    {
      gdb_assert (arg->contents != nullptr);

This was based on the comment on value::contents, which suggest that
this is the case:

  /* Actual contents of the value.  Target byte-order.  NULL or not
     valid if lazy is nonzero.  */
  gdb::unique_xmalloc_ptr<gdb_byte> contents;

However, it turns out that it can also be nullptr also if the value is
entirely optimized out, for example on exit of
allocate_optimized_out_value.  That function creates a lazy value, marks
the entire value as optimized out, and then clears the lazy flag.  But
contents remains nullptr.

This wasn't a problem for value_copy before, because it was calling
value_contents_all_raw on the input value, which caused contents to be
allocated before doing the copy.  This means that the input value to
value_copy did not have its contents allocated on entry, but had it
allocated on exit.  The result value had it allocated on exit.  And that
we copied bytes for an entirely optimized out value (i.e. meaningless
bytes).

From here I see two choices:

 1. respect the documented invariant that contents is nullptr only and
    only if the value is lazy, which means making
    allocate_optimized_out_value allocate contents
 2. extend the cases where contents can be nullptr to also include
    values that are entirely optimized out (note that you could still
    have some entirely optimized out values that do have contents
    allocated, it depends on how they were created) and adjust
    value_copy accordingly

Choice #1 is safe, but less efficient: it's not very useful to allocate
a buffer for an entirely optimized out value.  It's even a bit less
efficient than what we had initially, because values coming out of
allocate_optimized_out_value would now always get their contents
allocated.

Choice #2 would be more efficient than what we had before: giving an
optimized out value without allocated contents to value_copy would
result in an optimized out value without allocated contents (and the
input value would still be without allocated contents on exit).  But
it's more risky, since it's difficult to ensure that all users of the
contents (through the various_contents* accessors) are all fine with
that new invariant.

In this patch, I opt for choice #2, since I think it is a better
direction than choice #1.  #1 would be a pessimization, and if we go
this way, I doubt that it will ever be revisited, it will just stay that
way forever.

Add a selftest to test this.  I initially started to write it as a
Python test (since the reproducer is in Python), but a selftest is more
straightforward.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28980
Change-Id: I6e2f5c0ea804fafa041fcc4345d47064b5900ed7
2022-04-06 16:11:13 -04:00
..
amd64-py-framefilter-invalidarg.S
compare-enum-type-a.c
compare-enum-type-b.c
compare-enum-type.exp
compare-enum-type.h
flexible-array-member.c
flexible-array-member.exp
lib-types.cc
lib-types.exp
libpy-autoloaded-pretty-printers-in-newobjfile-event.so-gdb.py
py-arch-reg-groups.exp
py-arch-reg-names.exp
py-arch.c
py-arch.exp
py-as-string.c
py-as-string.exp
py-auto-load-chaining-f1.c
py-auto-load-chaining-f1.o-gdb.py
py-auto-load-chaining-f2.c
py-auto-load-chaining-f2.o-gdb.py
py-auto-load-chaining.c
py-auto-load-chaining.exp
py-autoloaded-pretty-printers-in-newobjfile-event-lib.cc
py-autoloaded-pretty-printers-in-newobjfile-event-lib.h
py-autoloaded-pretty-printers-in-newobjfile-event-main.cc
py-autoloaded-pretty-printers-in-newobjfile-event.exp
py-autoloaded-pretty-printers-in-newobjfile-event.py
py-bad-printers.c
py-bad-printers.exp
py-bad-printers.py
py-block.c
py-block.exp
py-breakpoint-create-fail.c
py-breakpoint-create-fail.exp
py-breakpoint-create-fail.py
py-breakpoint.c
py-breakpoint.exp
py-caller-is.c
py-caller-is.exp
py-charset.exp
py-cmd.c
py-cmd.exp
py-completion.exp
py-completion.py
py-connection.c
py-connection.exp
py-error.exp
py-error.py
py-events-shlib.c
py-events.c
py-events.exp
py-events.py
py-evsignal.exp
py-evthreads.c
py-evthreads.exp
py-explore-cc.exp
py-explore.c
py-explore.cc
py-explore.exp
py-finish-breakpoint2.cc
py-finish-breakpoint2.exp
py-finish-breakpoint2.py
py-finish-breakpoint.c
py-finish-breakpoint.exp
py-finish-breakpoint.py
py-format-address.c gdb/python: add gdb.format_address function 2022-03-22 10:05:05 +00:00
py-format-address.exp gdb/testsuite: fix copy & paste error in gdb.python/py-format-address.exp 2022-03-29 13:55:48 +01:00
py-format-string.c
py-format-string.exp
py-format-string.py
py-frame-args.c
py-frame-args.exp
py-frame-args.py
py-frame-inline.c
py-frame-inline.exp
py-frame.c
py-frame.exp
py-framefilter-addr.c
py-framefilter-addr.exp
py-framefilter-addr.py
py-framefilter-gdb.py
py-framefilter-invalidarg-gdb.py
py-framefilter-invalidarg.exp
py-framefilter-invalidarg.py
py-framefilter-mi.c
py-framefilter-mi.exp
py-framefilter.c
py-framefilter.exp
py-framefilter.py
py-function.exp
py-inferior-leak.c
py-inferior-leak.exp
py-inferior-leak.py
py-inferior.c
py-inferior.exp gdb/python: remove Python 2 support 2022-03-23 07:41:19 -04:00
py-infthread.c
py-infthread.exp
py-lazy-string.c
py-lazy-string.exp
py-linetable.c
py-linetable.exp
py-linetable.S
py-lookup-type.exp
py-mi-cmd.exp gdb/python: remove gdb._mi_commands dict 2022-03-18 20:29:57 -04:00
py-mi-cmd.py gdb/python/mi: create MI commands using python 2022-03-14 14:09:09 +00:00
py-mi-events-gdb.py
py-mi-events.c
py-mi-events.exp
py-mi-objfile-gdb.py
py-mi-objfile.c
py-mi-objfile.exp
py-mi-var-info-path-expression.c
py-mi-var-info-path-expression.exp
py-mi-var-info-path-expression.py gdb/python: remove Python 2 support 2022-03-23 07:41:19 -04:00
py-mi.exp
py-nested-maps.c
py-nested-maps.exp
py-nested-maps.py
py-objfile-script-gdb.py
py-objfile-script.c
py-objfile-script.exp
py-objfile.c
py-objfile.exp
py-parameter.exp
py-pending-frame-level.c
py-pending-frame-level.exp
py-pending-frame-level.py
py-pp-integral.c
py-pp-integral.exp
py-pp-integral.py
py-pp-maint.c
py-pp-maint.exp
py-pp-maint.py
py-pp-re-notag.c
py-pp-re-notag.exp
py-pp-re-notag.py
py-pp-registration.c
py-pp-registration.exp
py-pp-registration.py
py-prettyprint.c
py-prettyprint.exp
py-prettyprint.py
py-progspace.c
py-progspace.exp
py-prompt.c
py-prompt.exp
py-rbreak-func2.c
py-rbreak.c
py-rbreak.exp
py-record-btrace-threads.c
py-record-btrace-threads.exp
py-record-btrace.c
py-record-btrace.exp gdb/python: remove Python 2 support 2022-03-23 07:41:19 -04:00
py-record-full.c
py-record-full.exp
py-recurse-unwind.c
py-recurse-unwind.exp
py-recurse-unwind.py
py-rvalue-ref-value-cc.cc
py-rvalue-ref-value-cc.exp
py-section-script.c
py-section-script.exp
py-section-script.py
py-send-packet.c
py-send-packet.exp
py-send-packet.py gdb/python: remove Python 2 support 2022-03-23 07:41:19 -04:00
py-shared-sl.c
py-shared.c
py-shared.exp gdb/python: remove Python 2 support 2022-03-23 07:41:19 -04:00
py-source-styling.c
py-source-styling.exp
py-startup-opt.exp
py-strfns.c
py-strfns.exp
py-symbol-2.c
py-symbol.c
py-symbol.exp
py-symtab.exp
py-sync-interp.c
py-sync-interp.exp
py-template.cc
py-template.exp
py-thrhandle.c
py-thrhandle.exp
py-type.c
py-type.exp
py-typeprint.cc
py-typeprint.exp
py-typeprint.py
py-unwind-inline.c
py-unwind-inline.exp
py-unwind-inline.py
py-unwind-maint.c
py-unwind-maint.exp
py-unwind-maint.py
py-unwind-user-regs.c
py-unwind-user-regs.exp
py-unwind-user-regs.py
py-unwind.c
py-unwind.exp
py-unwind.py
py-value-cc.cc
py-value-cc.exp
py-value.c
py-value.exp gdb: don't copy entirely optimized out values in value_copy 2022-04-06 16:11:13 -04:00
py-xmethods.cc
py-xmethods.exp
py-xmethods.py
python-1.c
python.c
python.exp
source1
source2.py
tui-window-disabled.c
tui-window-disabled.exp
tui-window-disabled.py
tui-window.exp
tui-window.py