From: Pedro Alves <pedro@palves.net>
To: Andrew Burgess <aburgess@redhat.com>, gdb-patches@sourceware.org
Subject: Re: [PATCHv3 5/6] gdb: ensure the cast in gdbarch_tdep is valid
Date: Fri, 24 Jun 2022 19:15:04 +0100 [thread overview]
Message-ID: <a8b5699a-fafe-02d5-de1b-dcb753c2001a@palves.net> (raw)
In-Reply-To: <aff287aff678d5eadeb25af4ecaa7a60fa87b726.1655136816.git.aburgess@redhat.com>
On 2022-06-13 17:15, Andrew Burgess via Gdb-patches wrote:
> This commit builds on the previous commit and modifies the
> gdbarch_tdep function to ensure that the cast being performed is
> valid.
>
> To do this I make use of dynamic_cast to ensure that the generic
> gdbarch_tdep pointer that we have is of the correct type.
>
> The only problem with this approach is that, in order to use
> dynamic_cast, we need RTTI information, which requires the class to
> have a vtable, which currently, is not something the various tdep
> classes have.
>
> And so, in this commit, I add a virtual destructor to the gdbarch_tdep
> class.
>
> With this change I can now add an assert in the gdbarch_tdep function.
>
> Obviously, this change comes at a cost, creation of the tdep classes
> is now slightly more expensive (due to vtable initialisation),
> however, this only happens when a new gdbarch is created, which is not
> that frequent, so I don't see that as a huge concern.
>
...
> Then, there is an increased cost each time the tdep is accessed. This
> is much more frequent, but I don't believe the cost is excessive (a
> vtable pointer comparison), at least, no worse than many of our other
> asserts.
I don't think that's true. dynamic_cast does not really do a vtable
pointer comparison. It does pointer arithmetic/adjustment, walks the vtables
to find the type info for each type in the hierarchy, and compares the type's
type info objects, which usually mean string-comparing the class names. Only
some systems can do pointer comparisons to compare types. See e.g.:
https://github.com/gcc-mirror/gcc/blob/bb8e93eb1acae30a5fbe7e13149493ce4ccd301a/libstdc%2B%2B-v3/libsupc%2B%2B/typeinfo#L52
See the benchmark I posted in this previous response to Tromey:
https://sourceware.org/pipermail/gdb-patches/2022-April/188102.html
Maybe we should revisit Tromey's idea of a new cast "operator",
but make it static_cast in release mode? This patch that I posted today
adds a DEVELOPMENT macro symbol to common-config.h, we can make use of that:
https://sourceware.org/pipermail/gdb-patches/2022-June/190301.html
Also, dynamic_cast lets you try to cast to completely unrelated types (undefined
behavior instead of compile error), while to me is seems better to make it fail
to compile. Like:
template<typename T, typename V>
T *
checked_dyn_cast (V *v)
{
/* dynamic_cast compiles even if V and T are completely unrelated.
This assert prevent that, only letting you do downcasts, and
casts to same type. */
static_assert (std::is_base_of<V, T>::value
|| std::is_base_of<T, V>::value,
"types must be related");
/* Check for polymorphism explicitly in case we're in release
mode. */
static_assert (std::is_polymorphic<V>::value,
"types must be polymorphic");
#ifdef DEVELOPMENT
T *result = dynamic_cast<T *> (v);
assert (result != nullptr);
#else
T *result = static_cast<T *> (v);
#endif
return result;
}
With that, this compiles & works as expected:
struct A
{
virtual ~A() {}
};
struct B : A {};
struct C {};
void func ()
{
A *a = new A();
B *b = new B();
C *c = new C();
// these work
checked_dyn_cast<A> (a);
checked_dyn_cast<B> (a);
checked_dyn_cast<A> (b);
checked_dyn_cast<const B> (a);
// while these fail the static assertions.
// checked_dyn_cast<C> (a);
// checked_dyn_cast<A> (c);
// checked_dyn_cast<A> (123);
}
Anyhow, this can always be done after your patch, I don't mean to suggest
we should do this before.
Other than the nit in the commit log, the patch LGTM.
next prev parent reply other threads:[~2022-06-24 18:15 UTC|newest]
Thread overview: 83+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-31 14:30 [PATCH 0/5] Handle trying to use a native target with the wrong binary Andrew Burgess
2022-05-31 14:30 ` [PATCH 1/5] gdb/arm: move fetch of arm_gdbarch_tdep to a more inner scope Andrew Burgess
2022-06-01 7:58 ` Luis Machado
2022-05-31 14:30 ` [PATCH 2/5] gdb/mips: rewrite show_mask_address Andrew Burgess
2022-05-31 14:30 ` [PATCH 3/5] gdb: move the type cast into gdbarch_tdep Andrew Burgess
2022-06-01 8:01 ` Luis Machado
2022-05-31 14:30 ` [PATCH 4/5] gdb: ensure the cast in gdbarch_tdep is valid Andrew Burgess
2022-05-31 16:04 ` John Baldwin
2022-05-31 17:22 ` Andrew Burgess
2022-05-31 14:30 ` [PATCH 5/5] gdb: native target invalid architecture detection Andrew Burgess
2022-05-31 16:08 ` John Baldwin
2022-05-31 16:51 ` Andrew Burgess
2022-06-01 8:25 ` Luis Machado
2022-06-01 21:06 ` John Baldwin
2022-06-01 21:21 ` Christophe Lyon
2022-06-02 14:56 ` John Baldwin
2022-06-06 14:38 ` Andrew Burgess
2022-06-06 17:48 ` Andrew Burgess
2022-06-07 11:03 ` Luis Machado
2022-06-07 18:42 ` Pedro Alves
2022-06-07 20:15 ` Pedro Alves
2022-06-08 8:18 ` Luis Machado
2022-06-08 10:17 ` Pedro Alves
2022-06-08 7:54 ` Luis Machado
2022-06-08 10:12 ` Pedro Alves
2022-06-08 11:20 ` [PATCH v2] aarch64: Add fallback if ARM_CC_FOR_TARGET not set (was: Re: [PATCH 5/5] gdb: native target invalid architecture detection) Pedro Alves
2022-06-08 12:50 ` Luis Machado
2022-06-08 13:23 ` Pedro Alves
2022-06-08 13:38 ` Andrew Burgess
2022-06-08 19:01 ` John Baldwin
2022-06-08 21:48 ` Pedro Alves
2022-06-09 16:31 ` John Baldwin
2022-06-10 13:08 ` [PATCHv2 0/6] Handle trying to use a native target with the wrong binary Andrew Burgess
2022-06-10 13:08 ` [PATCHv2 1/6] gdb/arm: move fetch of arm_gdbarch_tdep to a more inner scope Andrew Burgess
2022-06-10 13:08 ` [PATCHv2 2/6] gdb/mips: rewrite show_mask_address Andrew Burgess
2022-06-10 13:08 ` [PATCHv2 3/6] gdb/arm: avoid undefined behaviour in arm_frame_is_thumb Andrew Burgess
2022-06-10 15:21 ` Luis Machado
2022-06-10 15:49 ` Andrew Burgess
2022-06-10 16:29 ` Luis Machado
2022-06-10 13:08 ` [PATCHv2 4/6] gdb: move the type cast into gdbarch_tdep Andrew Burgess
2022-06-10 16:35 ` Luis Machado
2022-06-10 13:08 ` [PATCHv2 5/6] gdb: ensure the cast in gdbarch_tdep is valid Andrew Burgess
2022-06-10 13:08 ` [PATCHv2 6/6] gdb: native target invalid architecture detection Andrew Burgess
2022-06-10 16:20 ` John Baldwin
2022-06-10 16:31 ` Luis Machado
2022-06-13 16:15 ` [PATCHv3 0/6] Handle trying to use a native target with the wrong binary Andrew Burgess
2022-06-13 16:15 ` [PATCHv3 1/6] gdb/arm: move fetch of arm_gdbarch_tdep to a more inner scope Andrew Burgess
2022-06-13 16:15 ` [PATCHv3 2/6] gdb/mips: rewrite show_mask_address Andrew Burgess
2022-06-13 16:15 ` [PATCHv3 3/6] gdb: select suitable thread for gdbarch_adjust_breakpoint_address Andrew Burgess
2022-06-14 9:45 ` Luis Machado
2022-06-14 14:05 ` Andrew Burgess
2022-06-24 16:58 ` Pedro Alves
2022-06-13 16:15 ` [PATCHv3 4/6] gdb: move the type cast into gdbarch_tdep Andrew Burgess
2022-06-13 16:15 ` [PATCHv3 5/6] gdb: ensure the cast in gdbarch_tdep is valid Andrew Burgess
2022-06-24 18:15 ` Pedro Alves [this message]
2022-06-13 16:15 ` [PATCHv3 6/6] gdb: native target invalid architecture detection Andrew Burgess
2022-06-24 19:23 ` Pedro Alves
2022-06-27 16:27 ` Andrew Burgess
2022-06-27 21:38 ` Pedro Alves
2022-06-28 10:37 ` Andrew Burgess
2022-06-28 12:42 ` [PATCH v2] gdb+gdbserver/Linux: avoid reading registers while going through shell (was: Re: [PATCHv3 6/6] gdb: native target invalid architecture detection) Pedro Alves
2022-06-28 14:21 ` Andrew Burgess
2022-06-29 15:17 ` Simon Marchi
2022-06-29 16:22 ` [PATCH] Fix GDBserver regression due to change to avoid reading shell registers Pedro Alves
2022-06-29 16:38 ` Simon Marchi
2022-06-30 9:33 ` [PATCHv3 6/6] gdb: native target invalid architecture detection Andrew Burgess
2022-06-30 11:44 ` Pedro Alves
2022-07-11 10:47 ` Andrew Burgess
2022-06-24 10:15 ` [PATCHv3 0/6] Handle trying to use a native target with the wrong binary Andrew Burgess
2022-06-28 14:28 ` [PATCHv4 0/6] Detect invalid casts of gdbarch_tdep structures Andrew Burgess
2022-06-28 14:28 ` [PATCHv4 1/6] gdb/arm: move fetch of arm_gdbarch_tdep to a more inner scope Andrew Burgess
2022-06-28 14:28 ` [PATCHv4 2/6] gdb/mips: rewrite show_mask_address Andrew Burgess
2022-06-28 14:28 ` [PATCHv4 3/6] gdb: select suitable thread for gdbarch_adjust_breakpoint_address Andrew Burgess
2022-06-28 14:28 ` [PATCHv4 4/6] gdb: move the type cast into gdbarch_tdep Andrew Burgess
2022-06-28 14:28 ` [PATCHv4 5/6] gdbsupport: add checked_static_cast Andrew Burgess
2022-06-28 14:28 ` [PATCHv4 6/6] gdb: ensure the cast in gdbarch_tdep is valid Andrew Burgess
2022-07-11 10:46 ` [PATCHv4 0/6] Detect invalid casts of gdbarch_tdep structures Andrew Burgess
2022-07-21 18:21 ` Andrew Burgess
2022-07-22 0:50 ` Luis Machado
2022-07-23 0:02 ` [PATCH] Rename gdbarch_tdep template function to gdbarch_tdep_cast for g++ 4.8 Mark Wielaard
2022-07-25 11:19 ` Andrew Burgess
2022-07-25 11:27 ` Mark Wielaard
2022-07-26 11:05 ` Andrew Burgess
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=a8b5699a-fafe-02d5-de1b-dcb753c2001a@palves.net \
--to=pedro@palves.net \
--cc=aburgess@redhat.com \
--cc=gdb-patches@sourceware.org \
/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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).