Bug 80287 - C++ crash with __attribute((may_alias))
Summary: C++ crash with __attribute((may_alias))
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-04-02 15:21 UTC by Bernd Edlinger
Modified: 2017-12-15 08:58 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Bernd Edlinger 2017-04-02 15:21:33 UTC
the following change produces a crash:

Index: libstdc++-v3/testsuite/20_util/any/assign/2.cc
===================================================================
--- libstdc++-v3/testsuite/20_util/any/assign/2.cc	(Revision 246605)
+++ libstdc++-v3/testsuite/20_util/any/assign/2.cc	(Arbeitskopie)
@@ -39,7 +39,7 @@
   X2() = default;
   X2(const X2&) { copied = true; }
   X2(X2&& x) noexcept { moved = true; }
-};
+}__attribute((may_alias));
 
 void test01()
 {


2.cc: In constructor 'X2::X2(X2&&)':
2.cc:92:1: error: type variant differs by TYPE_REF_CAN_ALIAS_ALL.
 }
 ^
 <pointer_type 0x7fa46d95a5e8
    type <record_type 0x7fa46d95a348 X2 sizes-gimplified addressable asm_written needs-constructing type_1 type_5 type_6 BLK
        size <integer_cst 0x7fa46de49fa8 constant 8>
        unit size <integer_cst 0x7fa46de49fc0 constant 1>
        align 8 symtab 1838508144 alias set -1 canonical type 0x7fa46d95a348
        attributes <tree_list 0x7fa46ddf1348
            purpose <identifier_node 0x7fa46e044360 may_alias
                bindings <(nil)>
                local bindings <(nil)>>>
        fields <type_decl 0x7fa46d95c000 X2 type <record_type 0x7fa46d95a3f0 X2>
            used nonlocal decl_4 VOID file 2.cc line 38 col 1
            align 1 context <record_type 0x7fa46d95a348 X2> result <record_type 0x7fa46d95a348 X2>
           > context <translation_unit_decl 0x7fa46de58168 2.cc>
        full-name "struct X2"
        needs-constructor X() X(constX&) this=(X&) n_parents=0 use_template=0 interface-unknown
        pointer_to_this <pointer_type 0x7fa46d95a5e8> reference_to_this <reference_type 0x7fa46d95a9d8> chain <type_decl 0x7fa46d93fed8 X2>>
    asm_written static unsigned type_6 DI
    size <integer_cst 0x7fa46de49eb8 type <integer_type 0x7fa46de6a0a8 bitsizetype> constant 64>
    unit size <integer_cst 0x7fa46de49ed0 type <integer_type 0x7fa46de6a000 sizetype> constant 8>
    align 64 symtab 1838508384 alias set -1 canonical type 0x7fa46d95a5e8>
 <pointer_type 0x7fa46d95a690
    type <record_type 0x7fa46d95a348 X2 sizes-gimplified addressable asm_written needs-constructing type_1 type_5 type_6 BLK
        size <integer_cst 0x7fa46de49fa8 constant 8>
        unit size <integer_cst 0x7fa46de49fc0 constant 1>
        align 8 symtab 1838508144 alias set -1 canonical type 0x7fa46d95a348
        attributes <tree_list 0x7fa46ddf1348
            purpose <identifier_node 0x7fa46e044360 may_alias
                bindings <(nil)>
                local bindings <(nil)>>>
        fields <type_decl 0x7fa46d95c000 X2 type <record_type 0x7fa46d95a3f0 X2>
            used nonlocal decl_4 VOID file 2.cc line 38 col 1
            align 1 context <record_type 0x7fa46d95a348 X2> result <record_type 0x7fa46d95a348 X2>
           > context <translation_unit_decl 0x7fa46de58168 2.cc>
        full-name "struct X2"
        needs-constructor X() X(constX&) this=(X&) n_parents=0 use_template=0 interface-unknown
        pointer_to_this <pointer_type 0x7fa46d95a5e8> reference_to_this <reference_type 0x7fa46d95a9d8> chain <type_decl 0x7fa46d93fed8 X2>>
    readonly unsigned DI
    size <integer_cst 0x7fa46de49eb8 type <integer_type 0x7fa46de6a0a8 bitsizetype> constant 64>
    unit size <integer_cst 0x7fa46de49ed0 type <integer_type 0x7fa46de6a000 sizetype> constant 8>
    align 64 symtab 0 alias set -1 canonical type 0x7fa46d95a690>
2.cc:92:1: internal compiler error: verify_type failed
0x103d385 verify_type(tree_node const*)
        ../../gcc-7-20170326/gcc/tree.c:14207
0x9ef7a4 gen_type_die_with_usage
        ../../gcc-7-20170326/gcc/dwarf2out.c:24478
0x9f0f26 gen_type_die
        ../../gcc-7-20170326/gcc/dwarf2out.c:24704
0x9ec5bb gen_decl_die
        ../../gcc-7-20170326/gcc/dwarf2out.c:25371
0x9e852e gen_subprogram_die
        ../../gcc-7-20170326/gcc/dwarf2out.c:22371
0x9ebff9 gen_decl_die
        ../../gcc-7-20170326/gcc/dwarf2out.c:25284
0x9ed03e dwarf2out_decl
        ../../gcc-7-20170326/gcc/dwarf2out.c:25793
0x9eda2e dwarf2out_abstract_function
        ../../gcc-7-20170326/gcc/dwarf2out.c:21656
0x9ec716 gen_decl_die
        ../../gcc-7-20170326/gcc/dwarf2out.c:25239
0x9ed03e dwarf2out_decl
        ../../gcc-7-20170326/gcc/dwarf2out.c:25793
0x9ed4f6 dwarf2out_early_global_decl
        ../../gcc-7-20170326/gcc/dwarf2out.c:25450
0x972f35 symbol_table::finalize_compilation_unit()
        ../../gcc-7-20170326/gcc/cgraphunit.c:2613
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
Comment 1 Bernd Edlinger 2017-04-02 15:23:02 UTC
good news is, I already have a patch:

Index: gcc/cp/class.c
===================================================================
--- gcc/cp/class.c	(revision 246605)
+++ gcc/cp/class.c	(working copy)
@@ -2060,12 +2060,14 @@
 static void
 fixup_may_alias (tree klass)
 {
-  tree t;
+  tree t, v;
 
   for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
-    TYPE_REF_CAN_ALIAS_ALL (t) = true;
+    for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
+      TYPE_REF_CAN_ALIAS_ALL (v) = true;
   for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
-    TYPE_REF_CAN_ALIAS_ALL (t) = true;
+    for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
+      TYPE_REF_CAN_ALIAS_ALL (v) = true;
 }
 
 /* Early variant fixups: we apply attributes at the beginning of the class
Comment 2 Bernd Edlinger 2017-04-17 20:42:11 UTC
Author: edlinger
Date: Mon Apr 17 20:41:40 2017
New Revision: 246955

URL: https://gcc.gnu.org/viewcvs?rev=246955&root=gcc&view=rev
Log:
017-04-17  Bernd Edlinger  <bernd.edlinger@hotmail.de>
        
        PR c++/80287
        * class.c (fixup_may_alias): Fix all type variants.

gcc/testsuite
2017-04-17  Bernd Edlinger  <bernd.edlinger@hotmail.de> 

        PR c++/80287
        * g++.dg/lto/pr80287_0.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/lto/pr80287_0.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/class.c
    trunk/gcc/testsuite/ChangeLog
Comment 3 Bernd Edlinger 2017-04-17 20:51:44 UTC
fixed on trunk.
It's theoretically possible that gcc-6-branch is also affected,
but I have not tried.
Comment 4 Yvan Roux 2017-07-12 13:07:26 UTC
Hi Bernd,

gcc-6-branch is impacted by this issue (observed on AArch64 target at r250109, with regression introduced at r234768).  I can prepare the backport if you whish.
Comment 5 Bernd Edlinger 2017-07-12 13:10:48 UTC
(In reply to Yvan Roux from comment #4)
> Hi Bernd,
> 
> gcc-6-branch is impacted by this issue (observed on AArch64 target at
> r250109, with regression introduced at r234768).  I can prepare the backport
> if you whish.

Yes, thanks, go ahead.
Comment 6 Yvan Roux 2017-07-13 09:05:01 UTC
Hi,

I got a small issue when testing the backport into the branch:

g++.dg/lto/pr80287_0.C is not a suitable testcase for GCC 6 branch, since it uses c++17 class std:any which is not available in GCC 6.  In the first place this bug was found when building libstdc++-v3/testsuite/20_util/any/assign/2.cc, but it is not related to std:any, I found it on my side in ceph package build, and the issue is exhibited when compiling this reduced testcase with -g:

struct A {
  operator long() {}
} __attribute__((__may_alias__));
struct {
  A ino;
} a;
char b = a.ino;

So, I wonder what is the best way to handle that, adding this new testcase on trunk as g++.dg/pr80287.C and only backport this one into GCC 6, or avoid std:any usage into g++.dg/lto/pr80287_0.C ?
Comment 7 Bernd Edlinger 2017-07-13 09:12:23 UTC
Hmm, yes, maybe having a second test case without C++17 would be fine.
Comment 8 Yvan Roux 2017-07-13 09:17:26 UTC
OK, I'll submit a patch to add this second testcase on trunk and then only include it in the backport on GCC 6.

Thanks
Comment 9 Yvan Roux 2017-08-22 09:33:10 UTC
Author: yroux
Date: Tue Aug 22 09:32:38 2017
New Revision: 251268

URL: https://gcc.gnu.org/viewcvs?rev=251268&root=gcc&view=rev
Log:
2017-08-22  Yvan Roux  <yvan.roux@linaro.org>

        PR c++/80287
        * g++.dg/pr80287.C: New test.


Added:
    trunk/gcc/testsuite/g++.dg/pr80287.C
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 10 Yvan Roux 2017-08-22 10:52:24 UTC
Author: yroux
Date: Tue Aug 22 10:51:52 2017
New Revision: 251273

URL: https://gcc.gnu.org/viewcvs?rev=251273&root=gcc&view=rev
Log:
2017-08-22  Yvan Roux  <yvan.roux@linaro.org>

	Backport from mainline
	2017-08-22  Yvan Roux  <yvan.roux@linaro.org>

        PR c++/80287
        * g++.dg/pr80287.C: New test.

Added:
    branches/gcc-7-branch/gcc/testsuite/g++.dg/pr80287.C
Modified:
    branches/gcc-7-branch/gcc/testsuite/ChangeLog
Comment 11 Yvan Roux 2017-08-24 13:20:53 UTC
Author: yroux
Date: Thu Aug 24 13:20:22 2017
New Revision: 251331

URL: https://gcc.gnu.org/viewcvs?rev=251331&root=gcc&view=rev
Log:
PR c++/80287 C++ crash with __attribute((may_alias))

gcc/
2017-08-24  Yvan Roux  <yvan.roux@linaro.org>

	Backport from mainline
	2017-04-17  Bernd Edlinger  <bernd.edlinger@hotmail.de>

	PR c++/80287
	* class.c (fixup_may_alias): Fix all type variants.

gcc/testsuite
2017-08-24  Yvan Roux  <yvan.roux@linaro.org>

	Backport from mainline
	2017-08-22  Yvan Roux  <yvan.roux@linaro.org>

	PR c++/80287
	* g++.dg/pr8028.C: New test.

Added:
    branches/gcc-6-branch/gcc/testsuite/g++.dg/pr80287.C
Modified:
    branches/gcc-6-branch/gcc/ChangeLog
    branches/gcc-6-branch/gcc/cp/class.c
    branches/gcc-6-branch/gcc/testsuite/ChangeLog
Comment 12 Aldy Hernandez 2017-09-13 17:20:00 UTC
Author: aldyh
Date: Wed Sep 13 17:19:29 2017
New Revision: 252528

URL: https://gcc.gnu.org/viewcvs?rev=252528&root=gcc&view=rev
Log:
2017-08-22  Yvan Roux  <yvan.roux@linaro.org>

        PR c++/80287
        * g++.dg/pr80287.C: New test.

Added:
    branches/range-gen2/gcc/testsuite/g++.dg/pr80287.C
Modified:
    branches/range-gen2/gcc/testsuite/ChangeLog
Comment 13 Paolo Carlini 2017-12-15 08:58:57 UTC
This can be closed, right?