From: Jason Merrill <jason@redhat.com>
To: Marek Polacek <polacek@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] c++: ICE with -fsanitize=vptr and constexpr dynamic_cast [PR98103]
Date: Tue, 8 Dec 2020 16:50:11 -0500 [thread overview]
Message-ID: <60dd2210-f3bf-a05f-1d9f-58c0041d34d0@redhat.com> (raw)
In-Reply-To: <20201205034033.GD2286574@redhat.com>
On 12/4/20 10:40 PM, Marek Polacek wrote:
> On Wed, Dec 02, 2020 at 09:01:48PM -0500, Jason Merrill wrote:
>> On 12/2/20 6:18 PM, Marek Polacek wrote:
>>> -fsanitize=vptr initializes all vtable pointers to null so that it can
>>> catch invalid calls; see cp_ubsan_maybe_initialize_vtbl_ptrs. That
>>> means that evaluating a vtable reference can produce a null pointer
>>> in this mode, so cxx_eval_dynamic_cast_fn should check that.
>>
>> Yes, but we shouldn't accept it silently; sanitize is supposed to flag
>> undefined behavior, not allow it. If we see a null vptr, we should complain
>> and set *non_constant_p.
>
> True, I shouldn't have left it for the run-time diagnostic. How's this, then?
>
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
OK.
> -- >8 --
> -fsanitize=vptr initializes all vtable pointers to null so that it can
> catch invalid calls; see cp_ubsan_maybe_initialize_vtbl_ptrs. That
> means that evaluating a vtable reference can produce a null pointer
> in this mode, so cxx_eval_dynamic_cast_fn should check that and give
> and error.
>
> gcc/cp/ChangeLog:
>
> PR c++/98103
> * constexpr.c (cxx_eval_dynamic_cast_fn): If the evaluating of vtable
> yields a null pointer, give an error and return. Use objtype.
>
> gcc/testsuite/ChangeLog:
>
> PR c++/98103
> * g++.dg/ubsan/vptr-18.C: New test.
> ---
> gcc/cp/constexpr.c | 11 ++++++++++-
> gcc/testsuite/g++.dg/ubsan/vptr-18.C | 25 +++++++++++++++++++++++++
> 2 files changed, 35 insertions(+), 1 deletion(-)
> create mode 100644 gcc/testsuite/g++.dg/ubsan/vptr-18.C
>
> diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
> index e0d358027c9..c413313fbe1 100644
> --- a/gcc/cp/constexpr.c
> +++ b/gcc/cp/constexpr.c
> @@ -1998,11 +1998,20 @@ cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
> to the object under construction or destruction, this object is
> considered to be a most derived object that has the type of the
> constructor or destructor's class. */
> - tree vtable = build_vfield_ref (obj, TREE_TYPE (obj));
> + tree vtable = build_vfield_ref (obj, objtype);
> vtable = cxx_eval_constant_expression (ctx, vtable, /*lval*/false,
> non_constant_p, overflow_p);
> if (*non_constant_p)
> return call;
> + /* With -fsanitize=vptr, we initialize all vtable pointers to null,
> + so it's possible that we got a null pointer now. */
> + if (integer_zerop (vtable))
> + {
> + if (!ctx->quiet)
> + error_at (loc, "virtual table pointer is used uninitialized");
> + *non_constant_p = true;
> + return integer_zero_node;
> + }
> /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
> vtable = extract_obj_from_addr_offset (vtable);
> const tree mdtype = DECL_CONTEXT (vtable);
> diff --git a/gcc/testsuite/g++.dg/ubsan/vptr-18.C b/gcc/testsuite/g++.dg/ubsan/vptr-18.C
> new file mode 100644
> index 00000000000..cd2ca0a9fb6
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ubsan/vptr-18.C
> @@ -0,0 +1,25 @@
> +// PR c++/98103
> +// { dg-do compile { target c++20 } }
> +// { dg-additional-options "-fsanitize=vptr -fno-sanitize-recover=vptr" }
> +// Modified constexpr-dynamic17.C.
> +
> +struct V {
> + virtual void f();
> +};
> +
> +struct A : V { };
> +
> +struct B : V {
> + constexpr B(V*, A*);
> +};
> +
> +struct D : B, A {
> + constexpr D() : B((A*)this, this) { }
> +};
> +
> +constexpr B::B(V* v, A* a)
> +{
> + dynamic_cast<B*>(a); // { dg-error "uninitialized" }
> +}
> +
> +constexpr D d;
>
> base-commit: df933e307b1950ce12472660dcac1765b8eb431d
>
prev parent reply other threads:[~2020-12-08 21:50 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-02 23:18 Marek Polacek
2020-12-03 2:01 ` Jason Merrill
2020-12-05 3:40 ` Marek Polacek
2020-12-08 21:50 ` Jason Merrill [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=60dd2210-f3bf-a05f-1d9f-58c0041d34d0@redhat.com \
--to=jason@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=polacek@redhat.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).