From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x82e.google.com (mail-qt1-x82e.google.com [IPv6:2607:f8b0:4864:20::82e]) by sourceware.org (Postfix) with ESMTPS id 857D43848400 for ; Wed, 5 May 2021 01:32:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 857D43848400 Received: by mail-qt1-x82e.google.com with SMTP id o12so78057qtx.8 for ; Tue, 04 May 2021 18:32:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=Ogapxi9kKoVjDNx/O4XDmrUeZVLIPSHRjDUv+ovkl/g=; b=FQ3bfVPyG1si5oO+nCRCyACTos+qK+TO+v5LPEmx11LvIR2iJzrDMlJh1gNFOCUKLa UbJvKAUVFnsVYUPiErbX2GlcXkpB/bQD2xBM7EAXWjqCdb7ErbolZLomb+3DJNClQfXq yKj7TZvpBGaOnMJXCDGFCoU5KeGy+P4ibF0DrAks5lny4mBpkSDePi34kxi1JfB0b1Ox ONhIJbxvVQXoo9oDPacmz5+PRmlKwkbR809dZFtHLzSiUVsKOuuyYxMNGHbH/mBsN73M 9axIV51TPiEjIgLwOA89b3puyutQg/DNA8jB9SxqJkHvUJWZrXvU1eFtkAswHuJXZUDj i/8g== X-Gm-Message-State: AOAM531V8g4/+CIMkdlHO/W5pCbBs7VKhZZYvDx36gbJwBTuwO1OLmov /xMbZNSucMaAiTRoRcewkpfBbQp20mA= X-Google-Smtp-Source: ABdhPJxwq/Z+MkixGw0tq6RxrDDUsAwJySG1O1NAoQMv8aGdBSx/oknBuGWFC4+NOjgedwHhYc0eNQ== X-Received: by 2002:ac8:57c6:: with SMTP id w6mr24751735qta.309.1620178324031; Tue, 04 May 2021 18:32:04 -0700 (PDT) Received: from [192.168.0.41] (71-218-14-121.hlrn.qwest.net. [71.218.14.121]) by smtp.gmail.com with ESMTPSA id 189sm9999954qkh.99.2021.05.04.18.32.03 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 04 May 2021 18:32:03 -0700 (PDT) To: gcc-patches From: Martin Sebor Subject: [PATCH] -Walloca-larger-than with constant sizes at -O0 (PR 100425) Message-ID: <73469b28-307f-d2ad-6ac8-3502f6c12ba7@gmail.com> Date: Tue, 4 May 2021 19:32:02 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------55C51189063999F28A01FEF2" Content-Language: en-US X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 05 May 2021 01:32:07 -0000 This is a multi-part message in MIME format. --------------55C51189063999F28A01FEF2 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Even when explicitly enabled, -Walloca-larger-than doesn't run unless optimization is enabled as well. This prevents diagnosing alloca calls with constant arguments in excess of the limit that could otherwise be flagged even at -O0, making the warning less consistent and less useful than is possible. The attached patch enables -Walloca-larger-than for calls with constant arguments in excess of the limit even at -O0 (variable arguments are only handled with optimization, when VRP runs). Martin --------------55C51189063999F28A01FEF2 Content-Type: text/x-patch; charset=UTF-8; name="gcc-100425.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="gcc-100425.diff" PR middle-end/100425 - missing -Walloca-larger-than with -O0 gcc/ChangeLog: PR middle-end/100425 * gimple-ssa-warn-alloca.c (pass_walloca::firast_time_p): Rename... (pass_walloca::xlimit_certain_p): ...to this. (pass_walloca::gate): Execute for any kind of handled warning. (pass_walloca::execute): Avoid issuing "maybe" and "unbounded" warnings when xlimit_certain_p is set. gcc/testsuite/ChangeLog: PR middle-end/100425 * gcc.dg/Walloca-larger-than-4.c: New test. diff --git a/gcc/gimple-ssa-warn-alloca.c b/gcc/gimple-ssa-warn-alloca.c index 42c0ba1d87b..e9a24d4d1d0 100644 --- a/gcc/gimple-ssa-warn-alloca.c +++ b/gcc/gimple-ssa-warn-alloca.c @@ -56,31 +56,30 @@ class pass_walloca : public gimple_opt_pass { public: pass_walloca (gcc::context *ctxt) - : gimple_opt_pass(pass_data_walloca, ctxt), first_time_p (false) + : gimple_opt_pass(pass_data_walloca, ctxt), xlimit_certain_p (false) {} opt_pass *clone () { return new pass_walloca (m_ctxt); } void set_pass_param (unsigned int n, bool param) { gcc_assert (n == 0); - first_time_p = param; + // Set to true to enable only warnings for alloca calls that + // are certainly in excess of the limit. This includes calls + // with constant arguments but excludes those in ranges (that + // can only be determined by range analysis) as well as + // the "may be too large" kind. + xlimit_certain_p = param; } virtual bool gate (function *); virtual unsigned int execute (function *); private: // Set to TRUE the first time we run this pass on a function. - bool first_time_p; + bool xlimit_certain_p; }; bool pass_walloca::gate (function *fun ATTRIBUTE_UNUSED) { - // The first time this pass is called, it is called before - // optimizations have been run and range information is unavailable, - // so we can only perform strict alloca checking. - if (first_time_p) - return warn_alloca != 0; - // Warning is disabled when its size limit is greater than PTRDIFF_MAX // for the target maximum, which makes the limit negative since when // represented in signed HOST_WIDE_INT. @@ -317,6 +316,9 @@ pass_walloca::execute (function *fun) break; case ALLOCA_BOUND_MAYBE_LARGE: { + if (xlimit_certain_p) + break; + auto_diagnostic_group d; if (warning_at (loc, wcode, (is_vla @@ -354,6 +356,9 @@ pass_walloca::execute (function *fun) } break; case ALLOCA_UNBOUNDED: + if (xlimit_certain_p) + break; + warning_at (loc, wcode, (is_vla ? G_("%Gunbounded use of variable-length array") diff --git a/gcc/testsuite/gcc.dg/Walloca-larger-than-4.c b/gcc/testsuite/gcc.dg/Walloca-larger-than-4.c new file mode 100644 index 00000000000..f2ccc93d92b --- /dev/null +++ b/gcc/testsuite/gcc.dg/Walloca-larger-than-4.c @@ -0,0 +1,18 @@ +/* PR middle-end/100425 - missing -Walloca-larger-than with -O0 + { dg-do compile } + { dg-options "-O0 -Wall -Walloca-larger-than=128" } */ + +typedef __SIZE_TYPE__ size_t; + +void* alloca (size_t); + +void sink (void*); + +void warn_alloca_too_large (void) +{ + sink (alloca (1)); + sink (alloca (128)); + sink (alloca (129)); // { dg-warning "\\\[-Walloca-larger-than" } + sink (alloca (128 + 2)); // { dg-warning "\\\[-Walloca-larger-than" } + sink (alloca (1024)); // { dg-warning "\\\[-Walloca-larger-than" } +} --------------55C51189063999F28A01FEF2--