public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: ICE with -fsanitize=vptr and constexpr dynamic_cast [PR98103]
@ 2020-12-02 23:18 Marek Polacek
  2020-12-03  2:01 ` Jason Merrill
  0 siblings, 1 reply; 4+ messages in thread
From: Marek Polacek @ 2020-12-02 23:18 UTC (permalink / raw)
  To: Jason Merrill, GCC Patches

-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.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

gcc/cp/ChangeLog:

	PR c++/98103
	* constexpr.c (cxx_eval_dynamic_cast_fn): If the evaluating of vtable
	yields a null pointer, return.

gcc/testsuite/ChangeLog:

	PR c++/98103
	* g++.dg/ubsan/vptr-18.C: New test.
---
 gcc/cp/constexpr.c                   |  4 +++-
 gcc/testsuite/g++.dg/ubsan/vptr-18.C | 27 +++++++++++++++++++++++++++
 2 files changed, 30 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 9a1a1db1267..8c6a9cf2b40 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -1996,7 +1996,9 @@ cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
   tree vtable = build_vfield_ref (obj, TREE_TYPE (obj));
   vtable = cxx_eval_constant_expression (ctx, vtable, /*lval*/false,
 					 non_constant_p, overflow_p);
-  if (*non_constant_p)
+  /* With -fsanitize=vptr, we initialize all vtable pointers to null,
+     so it's possible that we got a null pointer now.  */
+  if (*non_constant_p || integer_zerop (vtable))
     return call;
   /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A.  */
   vtable = extract_obj_from_addr_offset (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..9f421c269bc
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ubsan/vptr-18.C
@@ -0,0 +1,27 @@
+// PR c++/98103
+// { dg-do compile { target c++20 } }
+// { dg-additional-options "-fsanitize=vptr" }
+// Modified constexpr-dynamic17.C.  -fsanitize=vptr initializes all vtable
+// pointers to null, so we don't get the "accessing uninitialized member"
+// error.
+
+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);
+}
+
+constexpr D d;

base-commit: dc2b372ed1b1e9af6db45051cff95478c7616807
-- 
2.28.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-12-08 21:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-02 23:18 [PATCH] c++: ICE with -fsanitize=vptr and constexpr dynamic_cast [PR98103] Marek Polacek
2020-12-03  2:01 ` Jason Merrill
2020-12-05  3:40   ` Marek Polacek
2020-12-08 21:50     ` Jason Merrill

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).