binutils-gdb/gdb/tui/tui-layout.h
Simon Marchi 9b30da15e7 Add virtual destructor to tui_layout_base
I stumbled on some ASan failures when using the TUI, when tearing down a
TUI layout.  The simplest way to trigger it is to run:

  $ ./gdb --data-directory=data-directory -batch -ex "layout next"

The ASan report is:

    =================================================================
    ==2829136==ERROR: AddressSanitizer: new-delete-type-mismatch on 0x608000009a20 in thread T0:
      object passed to delete has wrong type:
      size of the allocated type:   88 bytes;
      size of the deallocated type: 24 bytes.
        #0 0x7f470fe2507e in operator delete(void*, unsigned long) /build/gcc/src/gcc/libsanitizer/asan/asan_new_delete.cc:177
        #1 0x55f88c75700d in std::default_delete<tui_layout_base>::operator()(tui_layout_base*) const /usr/include/c++/9.2.0/bits/unique_ptr.h:81
        #2 0x55f88c756328 in std::unique_ptr<tui_layout_base, std::default_delete<tui_layout_base> >::~unique_ptr() /usr/include/c++/9.2.0/bits/unique_ptr.h:284
        #3 0x7f470ee536a6 in __run_exit_handlers (/usr/lib/libc.so.6+0x3e6a6)
        #4 0x7f470ee5385d in __GI_exit (/usr/lib/libc.so.6+0x3e85d)
        #5 0x55f88c69f2ac in quit_force(int*, int) /home/simark/src/binutils-gdb/gdb/top.c:1766
        #6 0x55f88becc29a in captured_main_1 /home/simark/src/binutils-gdb/gdb/main.c:1183
        #7 0x55f88becc814 in captured_main /home/simark/src/binutils-gdb/gdb/main.c:1192
        #8 0x55f88becc8a9 in gdb_main(captured_main_args*) /home/simark/src/binutils-gdb/gdb/main.c:1217
        #9 0x55f88b3159cd in main /home/simark/src/binutils-gdb/gdb/gdb.c:32
        #10 0x7f470ee3c152 in __libc_start_main (/usr/lib/libc.so.6+0x27152)
        #11 0x55f88b31579d in _start (/home/simark/build/binutils-gdb/gdb/gdb+0x11fb79d)

    0x608000009a20 is located 0 bytes inside of 88-byte region [0x608000009a20,0x608000009a78)
    allocated by thread T0 here:
        #0 0x7f470fe238f8 in operator new(unsigned long) /build/gcc/src/gcc/libsanitizer/asan/asan_new_delete.cc:104
        #1 0x55f88c750906 in tui_layout_split::clone() const /home/simark/src/binutils-gdb/gdb/tui/tui-layout.c:515
        #2 0x55f88c74e60e in show_layout /home/simark/src/binutils-gdb/gdb/tui/tui-layout.c:90
        #3 0x55f88c74e7db in tui_set_layout(tui_layout_type) /home/simark/src/binutils-gdb/gdb/tui/tui-layout.c:116
        #4 0x55f88c782f4f in tui_enable() /home/simark/src/binutils-gdb/gdb/tui/tui.c:481
        #5 0x55f88c74eeb2 in tui_layout_command /home/simark/src/binutils-gdb/gdb/tui/tui-layout.c:286
        #6 0x55f88b6f969b in do_const_cfunc /home/simark/src/binutils-gdb/gdb/cli/cli-decode.c:107
        #7 0x55f88b701859 in cmd_func(cmd_list_element*, char const*, int) /home/simark/src/binutils-gdb/gdb/cli/cli-decode.c:1952
        #8 0x55f88c69b455 in execute_command(char const*, int) /home/simark/src/binutils-gdb/gdb/top.c:652
        #9 0x55f88bec9026 in catch_command_errors /home/simark/src/binutils-gdb/gdb/main.c:400
        #10 0x55f88becc1f2 in captured_main_1 /home/simark/src/binutils-gdb/gdb/main.c:1167
        #11 0x55f88becc814 in captured_main /home/simark/src/binutils-gdb/gdb/main.c:1192
        #12 0x55f88becc8a9 in gdb_main(captured_main_args*) /home/simark/src/binutils-gdb/gdb/main.c:1217
        #13 0x55f88b3159cd in main /home/simark/src/binutils-gdb/gdb/gdb.c:32
        #14 0x7f470ee3c152 in __libc_start_main (/usr/lib/libc.so.6+0x27152)

The problem is that the tui_layout_base is missing a virtual destructor.
We allocate a derived object (tui_layout_split), but delete it through a
tui_layout_base pointer.  Since the tui_layout_base destructor is not
virtual, the derived (tui_layout_split) destructor is not called, only
the base destructor.

That code is not in gdb-9-branch, so I don't think this patch is
relevant for the stable branch.

Note that this is caught as a diagnostic with clang:

    In file included from /home/simark/src/binutils-gdb/gdb/tui/tui-layout.c:22:
    In file included from /home/simark/src/binutils-gdb/gdb/defs.h:28:
    In file included from /home/simark/src/binutils-gdb/gdb/gdbsupport/common-defs.h:133:
    In file included from /home/simark/src/binutils-gdb/gdb/gdbsupport/common-exceptions.h:25:
    In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/9.2.0/../../../../include/c++/9.2.0/memory:80:
    /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/9.2.0/../../../../include/c++/9.2.0/bits/unique_ptr.h:81:2: error: delete called on 'tui_layout_base' that is abstract but has non-virtual destructor [-Werror,-Wdelete-abstract-non-virtual-dtor]
            delete __ptr;
            ^
    /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/9.2.0/../../../../include/c++/9.2.0/bits/unique_ptr.h:284:4: note: in instantiation of member function 'std::default_delete<tui_layout_base>::operator()' requested here
              get_deleter()(std::move(__ptr));
              ^
    /home/simark/src/binutils-gdb/gdb/tui/tui-layout.c:54:41: note: in instantiation of member function 'std::unique_ptr<tui_layout_base, std::default_delete<tui_layout_base> >::~unique_ptr' requested here
    static std::unique_ptr<tui_layout_base> applied_layout;
                                            ^
    1 error generated.

GCC has the similar -Wdelete-non-virtual-dtor, enabled by -Wall, but it
doesn't show up because warnings are inhibited for system headers, where
std::unique_ptr is defined.  There is a bug about it here:

    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58876

gdb/ChangeLog:

	* tui/tui-layout.h (class tui_layout_base): Add virtual
	destructor.
2019-12-17 15:01:15 -05:00

187 lines
4.9 KiB
C++

/* TUI layout window management.
Copyright (C) 1998-2019 Free Software Foundation, Inc.
Contributed by Hewlett-Packard Company.
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 TUI_TUI_LAYOUT_H
#define TUI_TUI_LAYOUT_H
#include "tui/tui.h"
#include "tui/tui-data.h"
/* The basic object in a TUI layout. This represents a single piece
of screen real estate. Subclasses determine the exact
behavior. */
class tui_layout_base
{
public:
DISABLE_COPY_AND_ASSIGN (tui_layout_base);
virtual ~tui_layout_base () = default;
/* Clone this object. Ordinarily a layout is cloned before it is
used, so that any necessary modifications do not affect the
"skeleton" layout. */
virtual std::unique_ptr<tui_layout_base> clone () const = 0;
/* Change the size and location of this layout. */
virtual void apply (int x, int y, int width, int height) = 0;
/* Return the minimum and maximum height of this layout. */
virtual void get_sizes (int *min_height, int *max_height) = 0;
/* True if the topmost item in this layout is boxed. */
virtual bool top_boxed_p () const = 0;
/* True if the bottommost item in this layout is boxed. */
virtual bool bottom_boxed_p () const = 0;
/* Return the name of this layout's window, or nullptr if this
layout does not represent a single window. */
virtual const char *get_name () const
{
return nullptr;
}
/* Adjust the size of the window named NAME to NEW_HEIGHT, updating
the sizes of the other windows around it. */
virtual bool adjust_size (const char *name, int new_height) = 0;
/* The most recent space allocation. */
int x = 0;
int y = 0;
int width = 0;
int height = 0;
protected:
tui_layout_base () = default;
};
/* A TUI layout object that displays a single window. The window is
given by name. */
class tui_layout_window : public tui_layout_base
{
public:
explicit tui_layout_window (const char *name)
: m_contents (name)
{
}
DISABLE_COPY_AND_ASSIGN (tui_layout_window);
std::unique_ptr<tui_layout_base> clone () const override;
void apply (int x, int y, int width, int height) override;
const char *get_name () const override
{
return m_contents.c_str ();
}
bool adjust_size (const char *name, int new_height) override
{
return false;
}
bool top_boxed_p () const override;
bool bottom_boxed_p () const override;
protected:
void get_sizes (int *min_height, int *max_height) override;
private:
/* Type of content to display. */
std::string m_contents;
/* When a layout is applied, this is updated to point to the window
object. */
tui_gen_win_info *m_window = nullptr;
};
/* A TUI layout that holds other layouts. */
class tui_layout_split : public tui_layout_base
{
public:
tui_layout_split () = default;
DISABLE_COPY_AND_ASSIGN (tui_layout_split);
/* Add a new split layout to this layout. WEIGHT is the desired
size, which is relative to the other weights given in this
layout. */
tui_layout_split *add_split (int weight);
/* Add a new window to this layout. NAME is the name of the window
to add. WEIGHT is the desired size, which is relative to the
other weights given in this layout. */
void add_window (const char *name, int weight);
std::unique_ptr<tui_layout_base> clone () const override;
void apply (int x, int y, int width, int height) override;
bool adjust_size (const char *name, int new_height) override;
bool top_boxed_p () const override;
bool bottom_boxed_p () const override;
protected:
void get_sizes (int *min_height, int *max_height) override;
private:
/* Set the weights from the current heights. */
void set_weights_from_heights ();
struct split
{
/* The requested weight. */
int weight;
/* The layout. */
std::unique_ptr<tui_layout_base> layout;
};
/* The splits. */
std::vector<split> m_splits;
/* True if this layout has already been applied at least once. */
bool m_applied = false;
};
extern void tui_add_win_to_layout (enum tui_win_type);
extern void tui_set_layout (enum tui_layout_type);
/* Apply the current layout. */
extern void tui_apply_current_layout ();
/* Adjust the window height of WIN to NEW_HEIGHT. */
extern void tui_adjust_window_height (struct tui_win_info *win,
int new_height);
#endif /* TUI_TUI_LAYOUT_H */