From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 63432 invoked by alias); 3 Nov 2017 13:42:59 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 63420 invoked by uid 89); 3 Nov 2017 13:42:57 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,SPF_PASS autolearn=ham version=3.3.2 spammy=acceptance, Instrument, 4309 X-HELO: mx2.suse.de Received: from mx2.suse.de (HELO mx2.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 03 Nov 2017 13:42:55 +0000 Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id DB748ADF1; Fri, 3 Nov 2017 13:42:52 +0000 (UTC) Subject: Re: [PATCH][RFC] Instrument function exit with __builtin_unreachable in C++. To: Jason Merrill , Marek Polacek Cc: Jakub Jelinek , gcc-patches List , Jonathan Wakely References: <31ddd79e-1152-9dd9-663b-acd8d1bcd4ab@suse.cz> <20171012084834.GS14653@tucnak> <451b4f3d-d858-bbc8-5d1f-6056af5963d3@suse.cz> <20171018125252.GC19284@redhat.com> <99de094c-0e98-f2f2-39a9-a7394e0604ad@redhat.com> From: =?UTF-8?Q?Martin_Li=c5=a1ka?= Message-ID: <0d484e7b-782b-7c4c-cdda-723efffe15a5@suse.cz> Date: Fri, 03 Nov 2017 13:42:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <99de094c-0e98-f2f2-39a9-a7394e0604ad@redhat.com> Content-Type: multipart/mixed; boundary="------------07116AC6A6B2E11E87570016" X-IsSubscribed: yes X-SW-Source: 2017-11/txt/msg00189.txt.bz2 This is a multi-part message in MIME format. --------------07116AC6A6B2E11E87570016 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-length: 1249 On 10/24/2017 04:19 PM, Jason Merrill wrote: > On 10/18/2017 09:07 AM, Martin Liška wrote: >> @@ -1182,7 +1182,13 @@ cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun, >>      { >>        new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t), >>                         CALL_EXPR_FN (t), nargs, args); >> -      error ("%q+E is not a constant expression", new_call); >> + >> +      /* Do not allow__builtin_unreachable in constexpr function.  */ >> +      if (DECL_FUNCTION_CODE (fun) == BUILT_IN_UNREACHABLE >> +          && EXPR_LOCATION (t) == BUILTINS_LOCATION) >> +        error ("constexpr call flows off the end of the function"); >> +      else >> +        error ("%q+E is not a constant expression", new_call); > > You don't need to build new_call in the new case, since you don't use it. > > Also, please adjust the comment to say that a __builtin_unreachable call with BUILTINS_LOCATION comes from cp_maybe_instrument_return. > > OK with those changes. > > Jason Hi. Thank you for review, done that. Can you please take a look at the single problematic test-case that blocks acceptance of the patch to trunk? Martin --------------07116AC6A6B2E11E87570016 Content-Type: text/x-patch; name="0001-Instrument-function-exit-with-__builtin_unreachable-v2.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-Instrument-function-exit-with-__builtin_unreachable-v2."; filename*1="patch" Content-length: 5453 >From 0c4fc1acba49d2d5ca2e6c475286a14e465b6f6c Mon Sep 17 00:00:00 2001 From: marxin Date: Thu, 12 Oct 2017 10:14:59 +0200 Subject: [PATCH 1/3] Instrument function exit with __builtin_unreachable in C++ gcc/c-family/ChangeLog: 2017-10-12 Martin Liska PR middle-end/82404 * c-opts.c (c_common_post_options): Set -Wreturn-type for C++ FE. * c.opt: Set default value of warn_return_type. gcc/cp/ChangeLog: 2017-10-12 Martin Liska PR middle-end/82404 * constexpr.c (cxx_eval_builtin_function_call): Handle __builtin_unreachable call. * cp-gimplify.c (cp_ubsan_maybe_instrument_return): Rename to ... (cp_maybe_instrument_return): ... this. (cp_genericize): Call the function unconditionally. gcc/fortran/ChangeLog: 2017-10-12 Martin Liska PR middle-end/82404 * options.c (gfc_post_options): Set default value of -Wreturn-type to false. --- gcc/c-family/c-opts.c | 3 +++ gcc/c-family/c.opt | 2 +- gcc/cp/constexpr.c | 15 ++++++++++++--- gcc/cp/cp-gimplify.c | 20 ++++++++++++++------ gcc/fortran/options.c | 3 +++ 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c index 32120e636c2..cead15e7a63 100644 --- a/gcc/c-family/c-opts.c +++ b/gcc/c-family/c-opts.c @@ -989,6 +989,9 @@ c_common_post_options (const char **pfilename) flag_extern_tls_init = 1; } + if (warn_return_type == -1) + warn_return_type = c_dialect_cxx (); + if (num_in_fnames > 1) error ("too many filenames given. Type %s --help for usage", progname); diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index dae124ac1c2..9ab31f0e153 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -960,7 +960,7 @@ C++ ObjC++ Var(warn_reorder) Warning LangEnabledBy(C++ ObjC++,Wall) Warn when the compiler reorders code. Wreturn-type -C ObjC C++ ObjC++ Var(warn_return_type) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall) +C ObjC C++ ObjC++ Var(warn_return_type) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall) Init(-1) Warn whenever a function's return type defaults to \"int\" (C), or about inconsistent return types (C++). Wscalar-storage-order diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 483f731a49a..7c2185851e0 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -1180,9 +1180,18 @@ cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun, { if (!*non_constant_p && !ctx->quiet) { - new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t), - CALL_EXPR_FN (t), nargs, args); - error ("%q+E is not a constant expression", new_call); + /* Do not allow__builtin_unreachable in constexpr function. + The __builtin_unreachable call with BUILTINS_LOCATION + comes from cp_maybe_instrument_return. */ + if (DECL_FUNCTION_CODE (fun) == BUILT_IN_UNREACHABLE + && EXPR_LOCATION (t) == BUILTINS_LOCATION) + error ("constexpr call flows off the end of the function"); + else + { + new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t), + CALL_EXPR_FN (t), nargs, args); + error ("%q+E is not a constant expression", new_call); + } } *non_constant_p = true; return t; diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c index 262485a5c1f..014c1ee7231 100644 --- a/gcc/cp/cp-gimplify.c +++ b/gcc/cp/cp-gimplify.c @@ -1556,10 +1556,11 @@ cp_genericize_tree (tree* t_p, bool handle_invisiref_parm_p) /* If a function that should end with a return in non-void function doesn't obviously end with return, add ubsan - instrumentation code to verify it at runtime. */ + instrumentation code to verify it at runtime. If -fsanitize=return + is not enabled, instrument __builtin_unreachable. */ static void -cp_ubsan_maybe_instrument_return (tree fndecl) +cp_maybe_instrument_return (tree fndecl) { if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl))) || DECL_CONSTRUCTOR_P (fndecl) @@ -1600,7 +1601,16 @@ cp_ubsan_maybe_instrument_return (tree fndecl) tree *p = &DECL_SAVED_TREE (fndecl); if (TREE_CODE (*p) == BIND_EXPR) p = &BIND_EXPR_BODY (*p); - t = ubsan_instrument_return (DECL_SOURCE_LOCATION (fndecl)); + + location_t loc = DECL_SOURCE_LOCATION (fndecl); + if (sanitize_flags_p (SANITIZE_RETURN, fndecl)) + t = ubsan_instrument_return (loc); + else + { + tree fndecl = builtin_decl_explicit (BUILT_IN_UNREACHABLE); + t = build_call_expr_loc (BUILTINS_LOCATION, fndecl, 0); + } + append_to_statement_list (t, p); } @@ -1674,9 +1684,7 @@ cp_genericize (tree fndecl) walk_tree's hash functionality. */ cp_genericize_tree (&DECL_SAVED_TREE (fndecl), true); - if (sanitize_flags_p (SANITIZE_RETURN) - && current_function_decl != NULL_TREE) - cp_ubsan_maybe_instrument_return (fndecl); + cp_maybe_instrument_return (fndecl); /* Do everything else. */ c_genericize (fndecl); diff --git a/gcc/fortran/options.c b/gcc/fortran/options.c index f7bbd7f2cde..59e7f028b09 100644 --- a/gcc/fortran/options.c +++ b/gcc/fortran/options.c @@ -430,6 +430,9 @@ gfc_post_options (const char **pfilename) gfc_fatal_error ("Maximum subrecord length cannot exceed %d", MAX_SUBRECORD_LENGTH); + if (warn_return_type == -1) + warn_return_type = 0; + gfc_cpp_post_options (); if (gfc_option.allow_std & GFC_STD_F2008) -- 2.14.3 --------------07116AC6A6B2E11E87570016--