From patchwork Wed Mar 1 22:28:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 65873 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 17C333858C30 for ; Wed, 1 Mar 2023 22:29:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 17C333858C30 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677709762; bh=zAaul8WdqZQJIuGD54+YcprT3oSgjCfOMgh3Aib7jyI=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=chIKbhBTBXnYTI8EmJPYnJ3i5aFjaonQ0LGRvBef2XY2WqhO8m5ooMKHAEUq7kgBh +FedFcz4KXkj6amY5Ux7EZxG6CsvDwI2xBfF8LDISVDLZVtikZahPo4p0Fbv9ly7qc mxre3MNs4Ap9P3fV/sox62g27rdzF2MWK3nfv9tQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 4E2F83858410 for ; Wed, 1 Mar 2023 22:28:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4E2F83858410 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-126-1YrHh7d5N5a0Zat13vWvgA-1; Wed, 01 Mar 2023 17:28:48 -0500 X-MC-Unique: 1YrHh7d5N5a0Zat13vWvgA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9B1F61C02D32 for ; Wed, 1 Mar 2023 22:28:48 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.16.186]) by smtp.corp.redhat.com (Postfix) with ESMTP id 74163C15BA0; Wed, 1 Mar 2023 22:28:48 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] analyzer: fixes to side-effects for built-in functions [PR107565] Date: Wed, 1 Mar 2023 17:28:47 -0500 Message-Id: <20230301222847.2606616-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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-Patchwork-Original-From: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Previously, if the analyzer saw a call to a non-pure and non-const built-in function that it didn't have explicit knowledge of the behavior of, it would fall back to assuming that the builtin could have arbitrary behavior, similar to a function defined outside of the current TU. However, this only worked for BUILTIN_NORMAL functions that matched gimple_builtin_call_types_compatible_p; for BUILT_IN_FRONTEND and BUILT_IN_MD, and for mismatched types the analyzer would erroneously assume that the builtin had no side-effects, leading e.g. to PR analyzer/107565, where the analyzer falsely reported that x was still uninitialized after this target-specific builtin: _1 = __builtin_ia32_rdrand64_step (&x); This patch generalizes the handling to cover all classes of builtin, fixing the above false positive. Unfortunately this patch regresses gcc.dg/analyzer/pr99716-1.c due to the: fprintf (fp, "hello"); being optimized to: __builtin_fwrite ("hello", 1, (ssizetype)5, fp_6); and the latter has gimple_builtin_call_types_compatible_p return false, whereas the original call had it return true. I'm assuming that this is an optimization bug, and have filed it as PR middle-end/108988. The effect on the analyzer is that it fails to recognize the call to __builtin_fwrite and instead assumes arbitraty side-effects (including that it could call fclose on fp, hence the report about the leak goes away). I tried various more involved fixes with new heuristics for handling built-ins that aren't explicitly covered by the analyzer, but those fixes tended to introduce many more regressions, so I'm going with this simpler fix. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Integration testing shows no change in results. Pushed to trunk as r13-6398-g24ebc5404b88b7. gcc/analyzer/ChangeLog: PR analyzer/107565 * region-model.cc (region_model::on_call_pre): Flatten logic by returning early. Consolidate logic for detecting const and pure functions. When considering whether an unhandled built-in function has side-effects, consider all kinds of builtin, rather than just BUILT_IN_NORMAL, and don't require gimple_builtin_call_types_compatible_p. gcc/testsuite/ChangeLog: PR analyzer/107565 * gcc.dg/analyzer/builtins-pr107565.c: New test. * gcc.dg/analyzer/pr99716-1.c (test_2): Mark the leak as xfailing. Signed-off-by: David Malcolm --- gcc/analyzer/region-model.cc | 44 ++++++++----------- .../gcc.dg/analyzer/builtins-pr107565.c | 29 ++++++++++++ gcc/testsuite/gcc.dg/analyzer/pr99716-1.c | 6 ++- 3 files changed, 53 insertions(+), 26 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/analyzer/builtins-pr107565.c diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc index f844b519f61..2187aecbe91 100644 --- a/gcc/analyzer/region-model.cc +++ b/gcc/analyzer/region-model.cc @@ -1477,8 +1477,6 @@ region_model::on_call_pre (const gcall *call, region_model_context *ctxt) { call_details cd (call, this, ctxt); - bool unknown_side_effects = false; - /* Special-case for IFN_DEFERRED_INIT. We want to report uninitialized variables with -fanalyzer (treating -ftrivial-auto-var-init= as purely a mitigation feature). @@ -1487,7 +1485,7 @@ region_model::on_call_pre (const gcall *call, region_model_context *ctxt) view of the analyzer. */ if (gimple_call_internal_p (call) && gimple_call_internal_fn (call) == IFN_DEFERRED_INIT) - return false; + return false; /* No side effects. */ /* Get svalues for all of the arguments at the callsite, to ensure that we complain about any uninitialized arguments. This might lead to @@ -1532,33 +1530,29 @@ region_model::on_call_pre (const gcall *call, region_model_context *ctxt) = get_known_function (gimple_call_internal_fn (call))) { kf->impl_call_pre (cd); - return false; + return false; /* No further side effects. */ } - if (callee_fndecl) - { - int callee_fndecl_flags = flags_from_decl_or_type (callee_fndecl); + if (!callee_fndecl) + return true; /* Unknown side effects. */ - if (const known_function *kf = get_known_function (callee_fndecl, cd)) - { - kf->impl_call_pre (cd); - return false; - } - else if (fndecl_built_in_p (callee_fndecl, BUILT_IN_NORMAL) - && gimple_builtin_call_types_compatible_p (call, callee_fndecl)) - { - if (!(callee_fndecl_flags & (ECF_CONST | ECF_PURE))) - unknown_side_effects = true; - } - else if (!fndecl_has_gimple_body_p (callee_fndecl) - && (!(callee_fndecl_flags & (ECF_CONST | ECF_PURE))) - && !fndecl_built_in_p (callee_fndecl)) - unknown_side_effects = true; + if (const known_function *kf = get_known_function (callee_fndecl, cd)) + { + kf->impl_call_pre (cd); + return false; /* No further side effects. */ } - else - unknown_side_effects = true; - return unknown_side_effects; + const int callee_fndecl_flags = flags_from_decl_or_type (callee_fndecl); + if (callee_fndecl_flags & (ECF_CONST | ECF_PURE)) + return false; /* No side effects. */ + + if (fndecl_built_in_p (callee_fndecl)) + return true; /* Unknown side effects. */ + + if (!fndecl_has_gimple_body_p (callee_fndecl)) + return true; /* Unknown side effects. */ + + return false; /* No side effects. */ } /* Update this model for the CALL stmt, using CTXT to report any diff --git a/gcc/testsuite/gcc.dg/analyzer/builtins-pr107565.c b/gcc/testsuite/gcc.dg/analyzer/builtins-pr107565.c new file mode 100644 index 00000000000..fb340aa5981 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/builtins-pr107565.c @@ -0,0 +1,29 @@ +/* { dg-do compile { target { x86_64-*-* && lp64 } } } */ +/* { dg-additional-options "-mrdrnd" } */ + +unsigned short +hardware_rand16 (void) +{ + unsigned short x; + while (! __builtin_ia32_rdrand16_step (&x)) + continue; + return x; /* { dg-bogus "uninit" } */ +} + +unsigned int +hardware_rand32 (void) +{ + unsigned int x; + while (! __builtin_ia32_rdrand32_step (&x)) + continue; + return x; /* { dg-bogus "uninit" } */ +} + +unsigned long long +hardware_rand64 (void) +{ + unsigned long long int x; + while (! __builtin_ia32_rdrand64_step (&x)) + continue; + return x; /* { dg-bogus "uninit" } */ +} diff --git a/gcc/testsuite/gcc.dg/analyzer/pr99716-1.c b/gcc/testsuite/gcc.dg/analyzer/pr99716-1.c index 2ccdcc73a5c..4fae368f321 100644 --- a/gcc/testsuite/gcc.dg/analyzer/pr99716-1.c +++ b/gcc/testsuite/gcc.dg/analyzer/pr99716-1.c @@ -27,7 +27,11 @@ test_2 (void) FILE *fp = fopen ("/tmp/test", "w"); fprintf (fp, "hello"); } -} /* { dg-warning "leak of FILE 'fp'" } */ +} /* { dg-warning "leak of FILE 'fp'" "" { xfail *-*-* } } */ +/* TODO: fails on some targets due to fprintf call being optimized to + __builtin_fwrite with a size argument (idx 2) that fails + gimple_builtin_call_types_compatible_p, and thus the known_function + for __builtin_fwrite not being used (PR middle-end/108988). */ FILE *fp3;