Bug 88256 - [8/9 Regression] ICE: Segmentation fault (in make_ssa_name_fn) with VLA cast, C++ FE missing DECL_EXPRs
Summary: [8/9 Regression] ICE: Segmentation fault (in make_ssa_name_fn) with VLA cast,...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0
: P2 normal
Target Milestone: 9.3
Assignee: Jason Merrill
URL:
Keywords: ice-on-valid-code
: 89439 89910 90494 91002 (view as bug list)
Depends on:
Blocks: C++VLA
  Show dependency treegraph
 
Reported: 2018-11-29 07:04 UTC by Arseny Solokha
Modified: 2020-03-02 21:31 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-11-29 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Arseny Solokha 2018-11-29 07:04:56 UTC
g++-9.0.0-alpha20181125 snapshot (r266441), 8.2, 7.3 ICE when compiling gcc/testsuite/gcc.c-torture/compile/pr84305.c at any optimization level w/ -fipa-cp:

% g++-9.0.0-alpha20181125 -O1 -fipa-cp -c gcc/testsuite/gcc.c-torture/compile/pr84305.c
during IPA pass: materialize-all-clones
gcc/testsuite/gcc.c-torture/compile/pr84305.c: In function 'void f2(int)':
gcc/testsuite/gcc.c-torture/compile/pr84305.c:4:26: internal compiler error: Segmentation fault
    4 | void f1 (void) { f2 (a); }
      |                          ^
0xf07ebf crash_signal
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/toplev.c:326
0x11136c9 make_ssa_name_fn(function*, tree_node*, gimple*, unsigned int)
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree-ssanames.c:268
0xf82a04 make_ssa_name
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree-ssanames.h:114
0xf82a04 remap_ssa_name
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree-inline.c:245
0xf87d97 copy_tree_body_r(tree_node**, int*, void*)
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree-inline.c:1118
0x11b5782 walk_tree_1(tree_node**, tree_node* (*)(tree_node**, int*, void*), void*, hash_set<tree_node*, default_hash_traits<tree_node*> >*, tree_node* (*)(tree_node**, int*, tree_node* (*)(tree_node**, int*, void*), void*, hash_set<tree_node*, default_hash_traits<tree_node*> >*))
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree.c:11892
0x11b5ead walk_tree_1(tree_node**, tree_node* (*)(tree_node**, int*, void*), void*, hash_set<tree_node*, default_hash_traits<tree_node*> >*, tree_node* (*)(tree_node**, int*, tree_node* (*)(tree_node**, int*, void*), void*, hash_set<tree_node*, default_hash_traits<tree_node*> >*))
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree.c:12214
0x11b5ead walk_tree_1(tree_node**, tree_node* (*)(tree_node**, int*, void*), void*, hash_set<tree_node*, default_hash_traits<tree_node*> >*, tree_node* (*)(tree_node**, int*, tree_node* (*)(tree_node**, int*, void*), void*, hash_set<tree_node*, default_hash_traits<tree_node*> >*))
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree.c:12214
0xf820e5 remap_type_1
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree-inline.c:578
0xf81021 remap_type(tree_node*, copy_body_data*)
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree-inline.c:607
0xf8914b remap_gimple_op_r
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree-inline.c:999
0x11b5782 walk_tree_1(tree_node**, tree_node* (*)(tree_node**, int*, void*), void*, hash_set<tree_node*, default_hash_traits<tree_node*> >*, tree_node* (*)(tree_node**, int*, tree_node* (*)(tree_node**, int*, void*), void*, hash_set<tree_node*, default_hash_traits<tree_node*> >*))
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree.c:11892
0x11b5ead walk_tree_1(tree_node**, tree_node* (*)(tree_node**, int*, void*), void*, hash_set<tree_node*, default_hash_traits<tree_node*> >*, tree_node* (*)(tree_node**, int*, tree_node* (*)(tree_node**, int*, void*), void*, hash_set<tree_node*, default_hash_traits<tree_node*> >*))
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree.c:12214
0x11b5ead walk_tree_1(tree_node**, tree_node* (*)(tree_node**, int*, void*), void*, hash_set<tree_node*, default_hash_traits<tree_node*> >*, tree_node* (*)(tree_node**, int*, tree_node* (*)(tree_node**, int*, void*), void*, hash_set<tree_node*, default_hash_traits<tree_node*> >*))
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree.c:12214
0xc8ca3d walk_gimple_op(gimple*, tree_node* (*)(tree_node**, int*, void*), walk_stmt_info*)
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/gimple-walk.c:202
0xf83541 remap_gimple_stmt
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree-inline.c:1778
0xf84536 copy_bb
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree-inline.c:1828
0xf85ddd copy_cfg_body
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree-inline.c:2720
0xf85ddd copy_body
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree-inline.c:2962
0xf8e3ea tree_function_versioning(tree_node*, tree_node*, vec<ipa_replace_map*, va_gc, vl_embed>*, bool, bitmap_head*, bool, bitmap_head*, basic_block_def*)
	/var/tmp/portage/sys-devel/gcc-9.0.0_alpha20181125/work/gcc-9-20181125/gcc/tree-inline.c:5994
Comment 1 Richard Biener 2018-11-29 09:46:23 UTC
I will have a look.
Comment 2 Martin Liška 2018-11-29 10:03:42 UTC
I see it fixed on trunk since r257620.
Comment 3 Martin Liška 2018-11-29 10:05:07 UTC
And appeared on trunk before that in r235817.
Comment 4 Arseny Solokha 2018-11-29 10:11:46 UTC
But the fix seems to be specific to C front-end and does not cover C++?

This time it's not (only) my mistake in the way I configure gcc: https://gcc.godbolt.org/z/2xRIE_
Comment 5 Richard Biener 2018-11-29 10:13:51 UTC
Program received signal SIGSEGV, Segmentation fault.
0x000000000174d104 in make_ssa_name_fn (fn=0x7ffff69e6160, var=<tree 0x0>, 
    stmt=<gimple 0x0>, version=0)
    at /space/rguenther/src/gcc-sccvn/gcc/tree-ssanames.c:268
268       gcc_assert (VAR_P (var)

we are remapping a released SSA name referenced from TYPE_SIZE of
int[0:(sizetype) D.2308][0:(sizetype) D.2307]

Thus this is a FE issue where we lack a DECL_EXPR for the type used in
some memory reference:

(gdb) p debug_gimple_stmt (stmt)
# VUSE <.MEM_6(D)>
_5 = MEM[(int[0:(sizetype) D.2308][0:(sizetype) D.2307] *)foo.2_4][0]{lb: 0 sz: _3 * 4}[0];

static void f2 (int arg) { res = ((int (*)[arg][b]) foo)[0][0][0]; }

is in .original

;; Function void f2(int) (null)
;; enabled by -tree-original


<<cleanup_point <<< Unknown tree: expr_stmt
  (void) (res = (*(((sizetype) (SAVE_EXPR <(ssizetype) b + -1>) + 1) * ((sizetype) (SAVE_EXPR <(ssizetype) arg + -1>) + 1);, (int[0:(sizetype) (SAVE_EXPR <(ssizetype) arg + -1>)][0:(sizetype) (SAVE_EXPR <(ssizetype) b + -1>)] *) foo;))[0][0]) >>>>>;


there's no DECL_EXPR so size expressions are not properly unshared.  IIRC
there's a duplicate PR where I ran into a similar issue.  I'm probably
thinking of PR86216 which sadly also has no attention from FE maintainers.

Anyway, C++ FE bug.
Comment 6 Richard Biener 2018-11-29 10:14:45 UTC
(In reply to Arseny Solokha from comment #4)
> But the fix seems to be specific to C front-end and does not cover C++?

Yeah, I can reproduce with the C++ FE which probably needs a similar fix.
Comment 7 Jakub Jelinek 2019-02-25 15:40:48 UTC
*** Bug 89439 has been marked as a duplicate of this bug. ***
Comment 8 Jason Merrill 2019-04-22 19:24:46 UTC
*** Bug 89910 has been marked as a duplicate of this bug. ***
Comment 9 Nathan Sidwell 2019-05-17 10:51:59 UTC
*** Bug 90494 has been marked as a duplicate of this bug. ***
Comment 10 Nathan Sidwell 2019-05-17 11:01:40 UTC
digging into the C++ FE's grokdeclarator shows this to be trickier than C.  C has a global variable of the expression component currently being built.  it hooks a COMPOUND_EXPR into there, in its own binding layer, when the grokking context is TYPENAME.  C++ does not have such a mechanism.

We cant just push the typedecl into the current statement list for three reasons

1) if we're in an initializer of a var decl, we'll push the typedecl /after/ the expression to which it refers.

2) if we're in a conditionally reached subexpression, we'll push the typedecl into an unconditional region of code.

    thing = cond ? (VLA_TYPE)expr : NULL;

3) if a components of the VLA is modified by an earlier piece of the current expression (i.e. comma operator), we'll push the typedecl before that modification.

    thing = (X++, (VLA_TYPE[X])expr);

I also noticed that strip_typedefs reconstructs the outer array type in 90494, but because the original isn't in the canonical hash, these get different canonical_types.  That seems wrong.

I suspect we need to do something like:
(a) create the typedecls in grokdeclarator
(b) insert the decl_exprs during the gimplify walk

that'll also handle the non function-scope cases, which we completely ignore right now.
Comment 11 Eric Gallager 2019-08-28 04:25:38 UTC
*** Bug 91002 has been marked as a duplicate of this bug. ***
Comment 12 Richard Biener 2019-11-14 07:59:15 UTC
The GCC 7 branch is being closed, re-targeting to GCC 8.4.
Comment 13 GCC Commits 2020-02-03 14:21:50 UTC
The master branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:3539fc1317267b30eb7c4ad48d52f4e46b3a198a

commit r10-6405-g3539fc1317267b30eb7c4ad48d52f4e46b3a198a
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Jan 31 21:59:48 2020 -0500

    c++: Fix cast to pointer to VLA.
    
    The C front-end fixed this issue in r257620 by adding a DECL_EXPR from
    grokdeclarator.  We don't have an easy way to do that in the C++ front-end,
    but it works fine to create and prepend a DECL_EXPR when we are genericizing
    the NOP_EXPR for the cast.
    
    The C patch wraps the DECL_EXPR in a BIND_EXPR, but that seems unnecessary
    in C++; this is just a hook to run gimplify_type_sizes, we aren't actually
    declaring anything that we need to worry about scoping for.
    
    	PR c++/88256
    	* cp-gimplify.c (predeclare_vla): New.
    	(cp_genericize_r) [NOP_EXPR]: Call it.
Comment 14 Jason Merrill 2020-02-03 14:23:19 UTC
Fixed for GCC 10 so far.
Comment 15 GCC Commits 2020-03-02 21:26:19 UTC
The releases/gcc-9 branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:f137a7c6b122e524294fb792bb97d5f3b0600c4f

commit r9-8322-gf137a7c6b122e524294fb792bb97d5f3b0600c4f
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Mar 2 14:42:47 2020 -0500

    c++: Fix cast to pointer to VLA.
    
    The C front-end fixed this issue in r257620 by adding a DECL_EXPR from
    grokdeclarator.  We don't have an easy way to do that in the C++ front-end,
    but it works fine to create and prepend a DECL_EXPR when we are genericizing
    the NOP_EXPR for the cast.
    
    The C patch wraps the DECL_EXPR in a BIND_EXPR, but that seems unnecessary
    in C++; this is just a hook to run gimplify_type_sizes, we aren't actually
    declaring anything that we need to worry about scoping for.
    
    gcc/cp/ChangeLog
    2020-03-02  Jason Merrill  <jason@redhat.com>
    
    	PR c++/88256
    	* cp-gimplify.c (predeclare_vla): New.
    	(cp_genericize_r) [NOP_EXPR]: Call it.
Comment 16 Jason Merrill 2020-03-02 21:31:31 UTC
Fixed for 9.3/10.