Avoid -Wuninitialized false negatives with sanitization and VLAs.

Resolves:
PR tree-optimization/93100 - gcc -fsanitize=address inhibits -Wuninitialized
PR middle-end/98583 - missing -Wuninitialized reading from a second VLA in its own block

gcc/ChangeLog:

	PR tree-optimization/93100
	PR middle-end/98583
	* tree-ssa-uninit.c (check_defs): Exclude intrinsic functions that
	don't modify referenced objects.

gcc/testsuite/ChangeLog:

	PR tree-optimization/93100
	PR middle-end/98583
	* g++.dg/warn/uninit-pr93100.C: New test.
	* gcc.dg/uninit-pr93100.c: New test.
	* gcc.dg/uninit-pr98583.c: New test.
This commit is contained in:
Martin Sebor 2021-05-13 16:05:50 -06:00
parent ca9bb74a5f
commit 2efe245bb8
4 changed files with 175 additions and 0 deletions

View file

@ -0,0 +1,59 @@
/* PR tree-optimization/98508 - Sanitizer disable -Wall and -Wextra
{ dg-do compile }
{ dg-options "-O0 -Wall -fsanitize=address" } */
struct S
{
int a;
};
void warn_init_self_O0 ()
{
S s = S (s); // { dg-warning "\\\[-Wuninitialized" }
(void)&s;
}
void warn_init_self_use_O0 ()
{
S s = S (s); // { dg-warning "\\\[-Wuninitialized" }
void sink (void*);
sink (&s);
}
#pragma GCC optimize ("1")
void warn_init_self_O1 ()
{
S s = S (s); // { dg-warning "\\\[-Wuninitialized" }
(void)&s;
}
void warn_init_self_use_O1 ()
{
S s = S (s); // { dg-warning "\\\[-Wuninitialized" }
void sink (void*);
sink (&s);
}
#pragma GCC optimize ("2")
void warn_init_self_O2 ()
{
S s = S (s); // { dg-warning "\\\[-Wuninitialized" }
(void)&s;
}
void warn_init_self_use_O2 ()
{
S s = S (s); // { dg-warning "\\\[-Wuninitialized" }
void sink (void*);
sink (&s);
}

View file

@ -0,0 +1,74 @@
/* PR tree-optimization/93100 - gcc -fsanitize=address inhibits -Wuninitialized
{ dg-do compile }
{ dg-options "-Wall -fsanitize=address" } */
struct A
{
_Bool b;
int i;
};
void warn_A_b_O0 (void)
{
struct A a;
if (a.b) // { dg-warning "\\\[-Wuninitialized" }
{
(void)&a;
}
}
void warn_A_i_O0 (void)
{
struct A a;
if (a.i) // { dg-warning "\\\[-Wuninitialized" }
{
(void)&a;
}
}
#pragma GCC optimize ("1")
void warn_A_b_O1 (void)
{
struct A a;
if (a.b) // { dg-warning "\\\[-Wuninitialized" }
{
(void)&a;
}
}
void warn_A_i_O1 (void)
{
struct A a;
if (a.i) // { dg-warning "\\\[-Wuninitialized" }
{
(void)&a;
}
}
#pragma GCC optimize ("2")
void warn_A_b_O2 (void)
{
struct A a;
if (a.b) // { dg-warning "\\\[-Wuninitialized" }
{
(void)&a;
}
}
void warn_A_i_O2 (void)
{
struct A a;
if (a.i) // { dg-warning "\\\[-Wuninitialized" }
{
(void)&a;
}
}

View file

@ -0,0 +1,31 @@
/* PR middle-end/98583 - missing -Wuninitialized reading from a second VLA
in its own block
{ dg-do compile }
{ dg-options "-O2 -Wall" } */
void f (int*);
void g (int);
void h1 (int n)
{
int a[n];
f (a);
int b[n];
g (b[1]); // { dg-warning "\\\[-Wuninitialized" }
}
void h2 (int n, int i, int j)
{
if (i)
{
int a[n];
f (a);
}
if (j)
{
int b[n];
g (b[1]); // { dg-warning "\\\[-Wmaybe-uninitialized" }
}
}

View file

@ -209,6 +209,16 @@ check_defs (ao_ref *ref, tree vdef, void *data_)
{
check_defs_data *data = (check_defs_data *)data_;
gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
/* The ASAN_MARK intrinsic doesn't modify the variable. */
if (is_gimple_call (def_stmt)
&& gimple_call_internal_p (def_stmt, IFN_ASAN_MARK))
return false;
/* End of VLA scope is not a kill. */
if (gimple_call_builtin_p (def_stmt, BUILT_IN_STACK_RESTORE))
return false;
/* If this is a clobber then if it is not a kill walk past it. */
if (gimple_clobber_p (def_stmt))
{
@ -216,6 +226,7 @@ check_defs (ao_ref *ref, tree vdef, void *data_)
return true;
return false;
}
/* Found a may-def on this path. */
data->found_may_defs = true;
return true;