From 4301ae2263d214d1c4b8e5d3d210e0aafff19e47 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Mon, 19 Jun 2017 17:27:40 +0200 Subject: [PATCH] re PR sanitizer/81111 (Cannot build libstdc++ with -fsanitize=undefined) PR sanitizer/81111 * ubsan.c (ubsan_encode_value): If current_function_decl is NULL, use create_tmp_var_raw instead of create_tmp_var, mark it addressable just by setting TREE_ADDRESSABLE on the result and use a TARGET_EXPR. * g++.dg/ubsan/pr81111.C: New test. From-SVN: r249375 --- gcc/ChangeLog | 7 +++++ gcc/testsuite/ChangeLog | 5 ++++ gcc/testsuite/g++.dg/ubsan/pr81111.C | 45 ++++++++++++++++++++++++++++ gcc/ubsan.c | 27 +++++++++++++---- 4 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/g++.dg/ubsan/pr81111.C diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 64fa3893090..c165b7f517f 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2017-06-19 Jakub Jelinek + + PR sanitizer/81111 + * ubsan.c (ubsan_encode_value): If current_function_decl is NULL, + use create_tmp_var_raw instead of create_tmp_var, mark it addressable + just by setting TREE_ADDRESSABLE on the result and use a TARGET_EXPR. + 2017-06-19 Richard Biener PR middle-end/81118 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index a27eac2395e..b38e45edb8a 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2017-06-19 Jakub Jelinek + + PR sanitizer/81111 + * g++.dg/ubsan/pr81111.C: New test. + 2017-06-19 Richard Biener PR middle-end/81118 diff --git a/gcc/testsuite/g++.dg/ubsan/pr81111.C b/gcc/testsuite/g++.dg/ubsan/pr81111.C new file mode 100644 index 00000000000..6d54a2fdf91 --- /dev/null +++ b/gcc/testsuite/g++.dg/ubsan/pr81111.C @@ -0,0 +1,45 @@ +// PR sanitizer/81111 +// { dg-do compile } +// { dg-options "-fsanitize=shift" } + +template +struct N +{ + static const V m = (((V)(-1) < 0) + ? (V)1 << (sizeof(V) * __CHAR_BIT__ - ((V)(-1) < 0)) + : (V) 0); +}; + +template +const V N::m; + +template +struct O +{ + static const V m = (V)1 << sizeof(V) * __CHAR_BIT__; +}; + +template +const V O::m; + +void +foo () +{ + N::m; + N::m; +#ifdef __SIZEOF_INT128__ + N<__int128>::m; + N::m; +#endif +} + +void +bar () +{ + O::m; + O::m; +#ifdef __SIZEOF_INT128__ + O<__int128>::m; + O::m; +#endif +} diff --git a/gcc/ubsan.c b/gcc/ubsan.c index 52ba5b586bd..855b2ea0e79 100644 --- a/gcc/ubsan.c +++ b/gcc/ubsan.c @@ -143,9 +143,17 @@ ubsan_encode_value (tree t, bool in_expand_p) { /* The reason for this is that we don't want to pessimize code by making vars unnecessarily addressable. */ - tree var = create_tmp_var (type); - tree tem = build2 (MODIFY_EXPR, void_type_node, var, t); - mark_addressable (var); + tree var; + if (current_function_decl) + { + var = create_tmp_var (type); + mark_addressable (var); + } + else + { + var = create_tmp_var_raw (type); + TREE_ADDRESSABLE (var) = 1; + } if (in_expand_p) { rtx mem @@ -156,8 +164,17 @@ ubsan_encode_value (tree t, bool in_expand_p) expand_assignment (var, t, false); return build_fold_addr_expr (var); } - t = build_fold_addr_expr (var); - return build2 (COMPOUND_EXPR, TREE_TYPE (t), tem, t); + if (current_function_decl) + { + tree tem = build2 (MODIFY_EXPR, void_type_node, var, t); + t = build_fold_addr_expr (var); + return build2 (COMPOUND_EXPR, TREE_TYPE (t), tem, t); + } + else + { + var = build4 (TARGET_EXPR, type, var, t, NULL_TREE, NULL_TREE); + return build_fold_addr_expr (var); + } } else return build_fold_addr_expr (t); -- 2.30.2