From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2140) id 9A429385E02E; Thu, 20 Oct 2022 04:09:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9A429385E02E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666238997; bh=Cs+wdWWoxOC7q2VO3S6ZYhPFgnauRdXIQWwfqDHOpN8=; h=From:To:Subject:Date:From; b=kMXGcWv6yBmoydB6GjtZ1/6nRTLW5WDS0N5FrqBJP8gy62VFcsxSjylYFelCEWnLk G16g511IoNua3JVaCPNXPvsChKPY0RjZd6JGVypk7GmwCcEZHUz87OiY2Ob66ylLwZ p9ymDRB+ewxGoLWvNLUtELXpglb5I5lN6yGcoujY= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Alexandre Oliva To: gcc-cvs@gcc.gnu.org Subject: [gcc(refs/users/aoliva/heads/testme)] hardcfr: optionally disable in leaf functions X-Act-Checkin: gcc X-Git-Author: Alexandre Oliva X-Git-Refname: refs/users/aoliva/heads/testme X-Git-Oldrev: 4273bf679dccd279a75727c48d5d6312f34e4bbc X-Git-Newrev: 3559895d311ffa663a25fe467eb2deaf3ee94ff4 Message-Id: <20221020040957.9A429385E02E@sourceware.org> Date: Thu, 20 Oct 2022 04:09:57 +0000 (GMT) List-Id: https://gcc.gnu.org/g:3559895d311ffa663a25fe467eb2deaf3ee94ff4 commit 3559895d311ffa663a25fe467eb2deaf3ee94ff4 Author: Alexandre Oliva Date: Wed Oct 19 20:36:30 2022 -0300 hardcfr: optionally disable in leaf functions Diff: --- gcc/common.opt | 4 ++++ gcc/doc/invoke.texi | 7 +++++- gcc/gimple-harden-control-flow.cc | 27 ++++++++++++++++++++++ .../c-c++-common/torture/harden-cfr-skip-leaf.c | 10 ++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/gcc/common.opt b/gcc/common.opt index e57646ce125..1f929428bc0 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -1798,6 +1798,10 @@ fharden-control-flow-redundancy Common Var(flag_harden_control_flow_redundancy) Optimization Harden control flow by recording and checking execution paths. +fhardcfr-skip-leaf +Common Var(flag_harden_control_flow_redundancy_skip_leaf) Optimization +Disable CFR in leaf functions. + fhardcfr-check-returning-calls Common Var(flag_harden_control_flow_redundancy_check_returning_calls) Init(-1) Optimization Check CFR execution paths also before calls followed by returns of their results. diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 24ff8e36721..427006608af 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -627,7 +627,8 @@ Objective-C and Objective-C++ Dialects}. -fsanitize-undefined-trap-on-error -fbounds-check @gol -fcf-protection=@r{[}full@r{|}branch@r{|}return@r{|}none@r{|}check@r{]} @gol -fharden-compares -fharden-conditional-branches @gol --fharden-control-flow-redundancy -fhardcfr-check-exceptions @gol +-fharden-control-flow-redundancy -fhardcfr-skip-leaf @gol +-fhardcfr-check-exceptions @gol -fhardcfr-check-returning-calls @gol -fhardcfr-check-noreturn-calls=@r{[}always@r{|}nothrow@r{|}never@r{]} @gol -fstack-protector -fstack-protector-all -fstack-protector-strong @gol @@ -16664,6 +16665,10 @@ would also have verification issued before the call, but these possibilities are merely theoretical, as these conditions can only be met when using custom compiler plugins. +@item -fhardcfr-skip-leaf +@opindex fhardcfr-skip-leaf +Disable @option{-fharden-control-flow-redundancy} in leaf functions. + @item -fhardcfr-check-exceptions @opindex fhardcfr-check-exceptions @opindex fno-hardcfr-check-exceptions diff --git a/gcc/gimple-harden-control-flow.cc b/gcc/gimple-harden-control-flow.cc index 4d40fc98779..e07c237cf8c 100644 --- a/gcc/gimple-harden-control-flow.cc +++ b/gcc/gimple-harden-control-flow.cc @@ -1121,6 +1121,33 @@ pass_harden_control_flow_redundancy::execute (function *fun) basic_block bb; basic_block bb_eh_cleanup = NULL; + if (flag_harden_control_flow_redundancy_skip_leaf) + { + bool found_calls_p = false; + + FOR_EACH_BB_FN (bb, fun) + { + for (gimple_stmt_iterator gsi = gsi_last_bb (bb); + !gsi_end_p (gsi); gsi_prev (&gsi)) + if (is_a (gsi_stmt (gsi))) + { + found_calls_p = true; + break; + } + if (found_calls_p) + break; + } + + if (!found_calls_p) + { + if (dump_file) + fprintf (dump_file, + "Disabling CFR for leaf function, as requested\n"); + + return 0; + } + } + if (check_at_escaping_exceptions) { int lp_eh_cleanup = -1; diff --git a/gcc/testsuite/c-c++-common/torture/harden-cfr-skip-leaf.c b/gcc/testsuite/c-c++-common/torture/harden-cfr-skip-leaf.c new file mode 100644 index 00000000000..85ecaa04d04 --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/harden-cfr-skip-leaf.c @@ -0,0 +1,10 @@ +/* { dg-do run } */ +/* { dg-options "-fharden-control-flow-redundancy -fhardcfr-skip-leaf -fdump-tree-hardcfr -ffat-lto-objects" } */ + +/* Test skipping instrumentation of leaf functions. */ + +#include "harden-cfr.c" + +/* { dg-final { scan-tree-dump-times "__builtin_trap" 0 "hardcfr" } } */ +/* Only main isn't leaf. */ +/* { dg-final { scan-tree-dump-times "__hardcfr_check" 2 "hardcfr" } } */