public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: David Malcolm <dmalcolm@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-6398] analyzer: fixes to side-effects for built-in functions [PR107565] Date: Wed, 1 Mar 2023 22:26:13 +0000 (GMT) [thread overview] Message-ID: <20230301222613.450283858D33@sourceware.org> (raw) https://gcc.gnu.org/g:24ebc5404b88b765221b551dc5288f6d64ba3dc7 commit r13-6398-g24ebc5404b88b765221b551dc5288f6d64ba3dc7 Author: David Malcolm <dmalcolm@redhat.com> Date: Wed Mar 1 17:24:32 2023 -0500 analyzer: fixes to side-effects for built-in functions [PR107565] 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. 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 <dmalcolm@redhat.com> Diff: --- gcc/analyzer/region-model.cc | 44 ++++++++++------------- gcc/testsuite/gcc.dg/analyzer/builtins-pr107565.c | 29 +++++++++++++++ gcc/testsuite/gcc.dg/analyzer/pr99716-1.c | 6 +++- 3 files changed, 53 insertions(+), 26 deletions(-) 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;
reply other threads:[~2023-03-01 22:26 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20230301222613.450283858D33@sourceware.org \ --to=dmalcolm@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).