Bug 105604 - [11 Regression] ICE: in tree_to_shwi with vla in struct and sprintf
Summary: [11 Regression] ICE: in tree_to_shwi with vla in struct and sprintf
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 11.2.1
: P2 normal
Target Milestone: 11.5
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, ice-on-valid-code
Depends on: 105969
Blocks: Wrestrict
  Show dependency treegraph
 
Reported: 2022-05-14 20:20 UTC by Sergei Trofimovich
Modified: 2023-07-07 10:43 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work: 12.1.1, 13.0
Known to fail: 12.1.0
Last reconfirmed: 2022-05-16 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sergei Trofimovich 2022-05-14 20:20:15 UTC
Originally reported by cpu in https://bugs.gentoo.org/844091. Here is my attempt at minimizing it:

//$ cat main.c
struct {
  long users;
  long size;
  char *data;
} * main_trans;
void *main___trans_tmp_1;
int sprintf(char *, char *, ...);
int main() {
  int users = 0;
  struct {
    long users;
    long size;
    char *data;
    int links[users];
    char buf[];
  } *trans = trans;
  trans->data = trans->buf;
  main___trans_tmp_1 = trans;
  main_trans = main___trans_tmp_1;
  sprintf(main_trans->data, "test");
}


$ /tmp/gb/gcc/xgcc -B/tmp/gb/gcc -c -Wall -O2 -pipe -fomit-frame-pointer main.c
during GIMPLE pass: strlen
main.c: In function ‘main’:
main.c:8:5: internal compiler error: in tree_to_shwi, at tree.cc:6369
    8 | int main() {
      |     ^~~~
0x1116ade tree_to_shwi(tree_node const*)
        gcc/tree.cc:6369
0x1116b36 int_byte_position(tree_node const*)
        gcc/tree.cc:3616
0x1cd1ce8 get_origin_and_offset_r
        gcc/gimple-ssa-sprintf.cc:2354
0x1cd1845 get_origin_and_offset_r
        gcc/gimple-ssa-sprintf.cc:2307
0x1cd1f29 get_origin_and_offset_r
        gcc/gimple-ssa-sprintf.cc:2370
0x1cd64ce get_origin_and_offset
        gcc/gimple-ssa-sprintf.cc:2427
0x1cd64ce handle_printf_call(gimple_stmt_iterator*, pointer_query&)
        gcc/gimple-ssa-sprintf.cc:4703
0x103ff10 strlen_pass::check_and_optimize_call(bool*)
        gcc/tree-ssa-strlen.cc:5461
0x103ffb3 strlen_pass::check_and_optimize_stmt(bool*)
        gcc/tree-ssa-strlen.cc:5665
0x1040326 strlen_pass::before_dom_children(basic_block_def*)
        gcc/tree-ssa-strlen.cc:5849
0x1c5d56b dom_walker::walk(basic_block_def*)
        gcc/domwalk.cc:309
0x10409cf printf_strlen_execute
        gcc/tree-ssa-strlen.cc:5908
0x1040c58 execute
        gcc/tree-ssa-strlen.cc:6007

I seem to be able to reproduce ICE on: 10.3.0, 11.3.0, 12.1.0.
This does not ICE: 9.3.0

$ /tmp/gb/gcc/xgcc -B/tmp/gb/gcc -v

Reading specs from /tmp/gb/gcc/specs
COLLECT_GCC=/tmp/gb/gcc/xgcc
COLLECT_LTO_WRAPPER=/tmp/gb/gcc/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /home/slyfox/dev/git/gcc/configure --disable-multilib --disable-bootstrap --with-native-system-header-dir=/<<NIX>>/glibc-2.34-115-dev/include --prefix=/tmp/gb/__td__ CFLAGS='-O1 -ggdb3' CXXFLAGS='-O1 -ggdb3' LDFLAGS='-O1 -ggdb3'
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 13.0.0 20220514 (experimental) (GCC)
Comment 1 Andrew Pinski 2022-05-14 20:26:58 UTC
Feels like someone forgot to check if the type had a non constant size/offsets.
Comment 2 Martin Liška 2022-05-16 07:09:04 UTC
Started with r10-4571-g937a86b49e4ed8ef.
Comment 3 Richard Biener 2022-05-16 07:56:07 UTC
Not only that.
Comment 4 GCC Commits 2022-05-24 22:20:19 UTC
The master branch has been updated by Martin Sebor <msebor@gcc.gnu.org>:

https://gcc.gnu.org/g:10d1986aee47c592f903527bb68546efc557735d

commit r13-750-g10d1986aee47c592f903527bb68546efc557735d
Author: Martin Sebor <msebor@redhat.com>
Date:   Tue May 24 16:01:12 2022 -0600

    PR middle-end/105604 - ICE: in tree_to_shwi with vla in struct and sprintf
    
    gcc/ChangeLog:
    
            PR middle-end/105604
            * gimple-ssa-sprintf.cc (set_aggregate_size_and_offset): Add comments.
            (get_origin_and_offset_r): Remove null handling.  Handle variable array
            sizes.
            (get_origin_and_offset): Handle null argument here.  Simplify.
            (alias_offset): Update comment.
            * pointer-query.cc (field_at_offset): Update comment.  Handle members
            of variable-length types.
    
    gcc/testsuite/ChangeLog:
    
            PR middle-end/105604
            * gcc.dg/Wrestrict-24.c: New test.
            * gcc.dg/Wrestrict-25.c: New test.
            * gcc.dg/Wrestrict-26.c: New test.
    
    Co-authored-by: Richard Biener <rguenther@suse.de>
Comment 5 Martin Sebor 2022-05-24 22:22:00 UTC
Fixed on trunk.  The changes should be safe to backport after some time.
Comment 6 GCC Commits 2022-06-02 09:39:55 UTC
The releases/gcc-12 branch has been updated by Richard Biener <rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:740771a620f661800b99dfc039b8896e2ddb9265

commit r12-8450-g740771a620f661800b99dfc039b8896e2ddb9265
Author: Martin Sebor <msebor@redhat.com>
Date:   Tue May 24 16:01:12 2022 -0600

    PR middle-end/105604 - ICE: in tree_to_shwi with vla in struct and sprintf
    
    gcc/ChangeLog:
    
            PR middle-end/105604
            * gimple-ssa-sprintf.cc (set_aggregate_size_and_offset): Add comments.
            (get_origin_and_offset_r): Remove null handling.  Handle variable array
            sizes.
            (get_origin_and_offset): Handle null argument here.  Simplify.
            (alias_offset): Update comment.
            * pointer-query.cc (field_at_offset): Update comment.  Handle members
            of variable-length types.
    
    gcc/testsuite/ChangeLog:
    
            PR middle-end/105604
            * gcc.dg/Wrestrict-24.c: New test.
            * gcc.dg/Wrestrict-25.c: New test.
            * gcc.dg/Wrestrict-26.c: New test.
    
    Co-authored-by: Richard Biener <rguenther@suse.de>
    (cherry picked from commit 10d1986aee47c592f903527bb68546efc557735d)
Comment 7 Richard Biener 2022-06-15 08:02:48 UTC
Originally (gentoo) reported against 11.2, but the fix doesn't cherry-pick nicely.
Comment 8 Jakub Jelinek 2022-06-28 10:49:21 UTC
GCC 10.4 is being released, retargeting bugs to GCC 10.5.
Comment 9 Richard Biener 2023-07-07 10:43:22 UTC
GCC 10 branch is being closed.