Bug 81981 - [8 Regression] -fsanitize=undefined makes a -Wmaybe-uninitialized warning disappear
Summary: [8 Regression] -fsanitize=undefined makes a -Wmaybe-uninitialized warning dis...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: sanitizer (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: 8.0
Assignee: Jakub Jelinek
URL:
Keywords: diagnostic, missed-optimization, xfail
Depends on:
Blocks: Wuninitialized
  Show dependency treegraph
 
Reported: 2017-08-25 13:08 UTC by Vincent Lefèvre
Modified: 2023-08-04 11:56 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work: 10.3.0, 11.2.0, 8.5.0, 9.4.0
Known to fail: 12.0
Last reconfirmed: 2021-10-22 00:00:00


Attachments
gcc8-pr81981.patch (1003 bytes, patch)
2017-08-29 18:14 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Vincent Lefèvre 2017-08-25 13:08:04 UTC
Consider:

int foo (int i)
{
  int t[1], u[1];
  int n = 0;

  if (i)
    {
      t[n] = i;
      u[0] = i;
    }

  return t[0] + u[0];
}

With gcc (Debian 20170823-1) 8.0.0 20170823 (experimental) [trunk revision 251306], I get:

zira% gcc-snapshot -Wmaybe-uninitialized -O2 -c tst.c -fsanitize=undefined
tst.c: In function 'foo':
tst.c:12:15: warning: 'u[0]' may be used uninitialized in this function [-Wmaybe-uninitialized]
   return t[0] + u[0];
          ~~~~~^~~~~~

zira% gcc-snapshot -Wmaybe-uninitialized -O2 -c tst.c                     
tst.c: In function 'foo':
tst.c:12:15: warning: 'u[0]' may be used uninitialized in this function [-Wmaybe-uninitialized]
   return t[0] + u[0];
          ~~~~~^~~~~~
tst.c:12:15: warning: 't[0]' may be used uninitialized in this function [-Wmaybe-uninitialized]

i.e. with -fsanitize=undefined, I do not get the warning on t[0].

This is a regression. No such problem with GCC 7.2.0, 6.4.0 and 5.4.1.
Comment 1 Marek Polacek 2017-08-25 13:15:23 UTC
Started with r250656.
Comment 2 Marek Polacek 2017-08-25 13:33:47 UTC
Looks like the problem is that since that revision in .uninit there's
UBSAN_PTR (&t, 0);
and so the warning probably thinks that t escapes and so doesn't warn.
Comment 3 Jakub Jelinek 2017-08-29 18:14:27 UTC
Created attachment 42080 [details]
gcc8-pr81981.patch

Untested fix.
Comment 4 Jakub Jelinek 2017-09-04 08:13:11 UTC
Author: jakub
Date: Mon Sep  4 08:11:44 2017
New Revision: 251641

URL: https://gcc.gnu.org/viewcvs?rev=251641&root=gcc&view=rev
Log:
	PR sanitizer/81981
	* gimple-fold.c (gimple_fold_call): Optimize away useless UBSAN_PTR
	and UBSAN_BOUNDS internal calls.  Clean up IFN_UBSAN_OBJECT_SIZE
	handling.  Use replace_call_with_value with NULL instead of
	gsi_replace, unlink_stmt_vdef and release_defs.

	* gcc.dg/ubsan/pr81981.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/ubsan/pr81981.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/gimple-fold.c
    trunk/gcc/testsuite/ChangeLog
Comment 5 Jakub Jelinek 2017-09-04 13:22:11 UTC
Fixed.
Comment 6 Jakub Jelinek 2017-10-11 10:38:32 UTC
.
Comment 7 Martin Sebor 2021-10-22 14:49:08 UTC
This has come back with r12-4625 (the fix for pr102681).
Comment 8 Andrew Pinski 2023-08-03 22:48:22 UTC
Fixed, Open PR 110896 for the new issue and for the xfail testcase. Note the issue there is unrelated to -fsanitize=undefined which is why I opened a new bug report.
Comment 9 Vincent Lefèvre 2023-08-04 11:56:25 UTC
Note, however, that there is a small regression in GCC 11: the warning for t is output as expected, but if -fsanitize=undefined is given, the message for t is suboptimal, saying "*&t[0]" instead of "t[0]":

zira:~> gcc-11 -Wmaybe-uninitialized -O2 -c tst.c -fsanitize=undefined
tst.c: In function ‘foo’:
tst.c:12:15: warning: ‘*&t[0]’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   12 |   return t[0] + u[0];
      |          ~~~~~^~~~~~
tst.c:12:15: warning: ‘u[0]’ may be used uninitialized in this function [-Wmaybe-uninitialized]

No such issue without -fsanitize=undefined:

zira:~> gcc-11 -Wmaybe-uninitialized -O2 -c tst.c
tst.c: In function ‘foo’:
tst.c:12:15: warning: ‘u[0]’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   12 |   return t[0] + u[0];
      |          ~~~~~^~~~~~
tst.c:12:15: warning: ‘t[0]’ may be used uninitialized in this function [-Wmaybe-uninitialized]

It is impossible to say whether this is fixed in GCC 12 and later, because of PR 110896, i.e. the warning is always missing.