From 3661842863344b18d736c0f9fd02983ebceaed52 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Thu, 9 Mar 2017 16:58:17 +0000 Subject: [PATCH] re PR sanitizer/79757 (ICE in declare_vars, at gimplify.c:634) PR sanitizer/79757 * c-parser.c (c_parser_declaration_or_fndef): Don't sanitize old-style parameter declarations with initializers. * gcc.dg/ubsan/pr79757-1.c: New test. * gcc.dg/ubsan/pr79757-2.c: New test. * gcc.dg/ubsan/pr79757-3.c: New test. * gcc.dg/ubsan/pr79757-4.c: New test. * gcc.dg/ubsan/pr79757-5.c: New test. From-SVN: r246010 --- gcc/c/ChangeLog | 6 ++++++ gcc/c/c-parser.c | 12 +++++++++++ gcc/testsuite/ChangeLog | 9 ++++++++ gcc/testsuite/gcc.dg/ubsan/pr79757-1.c | 24 +++++++++++++++++++++ gcc/testsuite/gcc.dg/ubsan/pr79757-2.c | 18 ++++++++++++++++ gcc/testsuite/gcc.dg/ubsan/pr79757-3.c | 18 ++++++++++++++++ gcc/testsuite/gcc.dg/ubsan/pr79757-4.c | 29 ++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/ubsan/pr79757-5.c | 29 ++++++++++++++++++++++++++ 8 files changed, 145 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/ubsan/pr79757-1.c create mode 100644 gcc/testsuite/gcc.dg/ubsan/pr79757-2.c create mode 100644 gcc/testsuite/gcc.dg/ubsan/pr79757-3.c create mode 100644 gcc/testsuite/gcc.dg/ubsan/pr79757-4.c create mode 100644 gcc/testsuite/gcc.dg/ubsan/pr79757-5.c diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index e6da629882f..c461c6dbb62 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,9 @@ +2017-03-09 Marek Polacek + + PR sanitizer/79757 + * c-parser.c (c_parser_declaration_or_fndef): Don't sanitize old-style + parameter declarations with initializers. + 2017-03-09 Jakub Jelinek PR c/79969 diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c index 8330e65fc94..1394f18b844 100644 --- a/gcc/c/c-parser.c +++ b/gcc/c/c-parser.c @@ -1859,7 +1859,13 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, init_loc = c_parser_peek_token (parser)->location; rich_location richloc (line_table, init_loc); start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc); + /* A parameter is initialized, which is invalid. Don't + attempt to instrument the initializer. */ + int flag_sanitize_save = flag_sanitize; + if (nested && !empty_ok) + flag_sanitize = 0; init = c_parser_expr_no_commas (parser, NULL); + flag_sanitize = flag_sanitize_save; if (TREE_CODE (init.value) == COMPONENT_REF && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1))) error_at (here, @@ -1917,7 +1923,13 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, init_loc = c_parser_peek_token (parser)->location; rich_location richloc (line_table, init_loc); start_init (d, asm_name, global_bindings_p (), &richloc); + /* A parameter is initialized, which is invalid. Don't + attempt to instrument the initializer. */ + int flag_sanitize_save = flag_sanitize; + if (TREE_CODE (d) == PARM_DECL) + flag_sanitize = 0; init = c_parser_initializer (parser); + flag_sanitize = flag_sanitize_save; finish_init (); } if (oacc_routine_data) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 48fba4842f6..3da89cd3c6b 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,12 @@ +2017-03-09 Marek Polacek + + PR sanitizer/79757 + * gcc.dg/ubsan/pr79757-1.c: New test. + * gcc.dg/ubsan/pr79757-2.c: New test. + * gcc.dg/ubsan/pr79757-3.c: New test. + * gcc.dg/ubsan/pr79757-4.c: New test. + * gcc.dg/ubsan/pr79757-5.c: New test. + 2017-03-09 Jakub Jelinek PR c/79969 diff --git a/gcc/testsuite/gcc.dg/ubsan/pr79757-1.c b/gcc/testsuite/gcc.dg/ubsan/pr79757-1.c new file mode 100644 index 00000000000..ca074bcff46 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/pr79757-1.c @@ -0,0 +1,24 @@ +/* PR sanitizer/79757 */ +/* { dg-do compile } */ +/* { dg-require-effective-target int128 } */ +/* { dg-options "-fsanitize=undefined" } */ + +unsigned __int128 x, y; + +void +fn1 (void) +{ + int a (z) + unsigned long long z = x / y; /* { dg-error "parameter 'z' is initialized" } */ + { + } +} + +void +fn2 (void) +{ + int a (z) + unsigned long long z = x >> y; /* { dg-error "parameter 'z' is initialized" } */ + { + } +} diff --git a/gcc/testsuite/gcc.dg/ubsan/pr79757-2.c b/gcc/testsuite/gcc.dg/ubsan/pr79757-2.c new file mode 100644 index 00000000000..b3e1939a547 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/pr79757-2.c @@ -0,0 +1,18 @@ +/* PR sanitizer/79757 */ +/* { dg-do compile } */ +/* { dg-require-effective-target int128 } */ +/* { dg-options "-fsanitize=undefined" } */ + +unsigned __int128 x, y; + +void +fn1 (z) + unsigned long long z = x / y; /* { dg-error "parameter 'z' is initialized" } */ +{ +} + +void +fn2 (z) + unsigned long long z = x >> y; /* { dg-error "parameter 'z' is initialized" } */ +{ +} diff --git a/gcc/testsuite/gcc.dg/ubsan/pr79757-3.c b/gcc/testsuite/gcc.dg/ubsan/pr79757-3.c new file mode 100644 index 00000000000..22fe3deef2d --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/pr79757-3.c @@ -0,0 +1,18 @@ +/* PR sanitizer/79757 */ +/* { dg-do compile } */ +/* { dg-require-effective-target int128 } */ +/* { dg-options "-fsanitize=undefined" } */ + +unsigned __int128 x, y; + +void +fn1 (z) + __auto_type z = x / y; /* { dg-error "parameter 'z' is initialized" } */ +{ +} + +void +fn2 (z) + __auto_type z = x >> y; /* { dg-error "parameter 'z' is initialized" } */ +{ +} diff --git a/gcc/testsuite/gcc.dg/ubsan/pr79757-4.c b/gcc/testsuite/gcc.dg/ubsan/pr79757-4.c new file mode 100644 index 00000000000..33b348f1d98 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/pr79757-4.c @@ -0,0 +1,29 @@ +/* PR sanitizer/79757 */ +/* { dg-do run } */ +/* { dg-skip-if "" { *-*-* } { "*" } { "-O2" } } */ +/* { dg-options "-fsanitize=undefined" } */ + +int +main (void) +{ + int + div (int n) + { + int i = 5 / n; + return i; + } + + int + shift (int n) + { + int i = 5 << n; + return i; + } + + int j = shift (100); + int i = div (0); + return 0; +} + +/* { dg-output "shift exponent 100 is too large for \[^\n\r]*-bit type 'int'\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*division by zero" } */ diff --git a/gcc/testsuite/gcc.dg/ubsan/pr79757-5.c b/gcc/testsuite/gcc.dg/ubsan/pr79757-5.c new file mode 100644 index 00000000000..786d81751ad --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/pr79757-5.c @@ -0,0 +1,29 @@ +/* PR sanitizer/79757 */ +/* { dg-do run } */ +/* { dg-skip-if "" { *-*-* } { "*" } { "-O2" } } */ +/* { dg-options "-fsanitize=undefined" } */ + +int +main (void) +{ + int + div (int n) + { + __auto_type i = 5 / n; + return i; + } + + int + shift (int n) + { + __auto_type i = 5 << n; + return i; + } + + int j = shift (100); + int i = div (0); + return 0; +} + +/* { dg-output "shift exponent 100 is too large for \[^\n\r]*-bit type 'int'\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*division by zero" } */ -- 2.30.2