From 24ebc5404b88b765221b551dc5288f6d64ba3dc7 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Wed, 1 Mar 2023 17:24:32 -0500 Subject: [PATCH] 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 --- 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(-) 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; -- 2.11.4.GIT