public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* Question about Fortran bounds and -Wanalyzer-use-of-uninitialized-value
@ 2022-10-12 13:46 David Malcolm
  2022-10-12 18:32 ` Harald Anlauf
  0 siblings, 1 reply; 2+ messages in thread
From: David Malcolm @ 2022-10-12 13:46 UTC (permalink / raw)
  To: fortran

Sorry in advance if this is a silly question; my knowledge of Fortran
is next to nothing, I'm afraid.

PR analyzer/107210 reports an ICE in -fanalyzer on this reproducer:


! { dg-additional-options "-O1" }

subroutine check_int (j)
  INTEGER(4) :: i, ia(5), ib(5,4), ip, ipa(:)
  target :: ib
  POINTER :: ip, ipa
  logical :: l(5)

  l = (/ sizeof(i) == 4, sizeof(ia) == 20, sizeof(ib) == 80, &
       sizeof(ip) == 4, sizeof(ipa) == 8 /) ! { dg-warning "use of uninitialized value" }

  if (any(.not.l)) STOP 4

end subroutine check_int


The fix for the ICE is trivial (a missing check that tree_fits_uhwi_p),
but after the fix, I see these warnings from the analyzer:


   10 |        sizeof(ip) == 4, sizeof(ipa) == 8 /)
      |                                           ^
Warning: use of uninitialized value ‘ipa.dim[0].ubound’ [CWE-457] [-Wanalyzer-use-of-uninitialized-value]
  ‘check_int’: events 1-3
    |
    |    4 |   INTEGER(4) :: i, ia(5), ib(5,4), ip, ipa(:)
    |      |                                             ^
    |      |                                             |
    |      |                                             (1) region created on stack here
    |      |                                             (2) capacity: 8 bytes
    |......
    |   10 |        sizeof(ip) == 4, sizeof(ipa) == 8 /)
    |      |                                           ~  
    |      |                                           |
    |      |                                           (3) use of uninitialized value ‘ipa.dim[0].ubound’ here
    |
../../src/gcc/testsuite/gfortran.dg/analyzer/pr107210.f90:10:43:

   10 |        sizeof(ip) == 4, sizeof(ipa) == 8 /)
      |                                           ^
Warning: use of uninitialized value ‘ipa.dim[0].lbound’ [CWE-457] [-Wanalyzer-use-of-uninitialized-value]
  ‘check_int’: events 1-3
    |
    |    4 |   INTEGER(4) :: i, ia(5), ib(5,4), ip, ipa(:)
    |      |                                             ^
    |      |                                             |
    |      |                                             (1) region created on stack here
    |      |                                             (2) capacity: 8 bytes
    |......
    |   10 |        sizeof(ip) == 4, sizeof(ipa) == 8 /)
    |      |                                           ~  
    |      |                                           |
    |      |                                           (3) use of uninitialized value ‘ipa.dim[0].lbound’ here
    |


The gimple in question is:

__attribute__((fn spec (". w ")))
void check_int (integer(kind=4) & restrict j)
{
  integer(kind=8) ipa$dim$0$lbound;
  integer(kind=8) ipa$dim$0$ubound;
  logical(kind=4) A.1[5];
  logical(kind=4) l[5];
  integer(kind=8) _1;
  logical(kind=4) _3;
  logical(kind=4) _4;
  integer(kind=8) _5;
  logical(kind=4) _6;
  integer(kind=8) S.5_7;
  logical(kind=4) test.6_8;
  integer(kind=8) S.7_9;
  integer(kind=8) S.5_16;
  integer(kind=8) S.7_18;

  <bb 2> [local count: 178992760]:
  MEM <uint128_t> [(c_char * {ref-all})&A.1] = 0x1000000010000000100000001;
  _1 = ipa$dim$0$ubound_2(D) - ipa$dim$0$lbound_12(D);
  _3 = _1 == 1;
  MEM[(logical(kind=4) *)&A.1 + 16B] = _3;

[...snip...]

where the analyzer is complaining about this gimple statement:
  _1 = ipa$dim$0$ubound_2(D) - ipa$dim$0$lbound_12(D);
where both:
  ipa$dim$0$ubound_2(D)
and:
  ipa$dim$0$lbound_12(D)
are considered by it to be uninitialized.

Is the analyzer correct here, or is there an aspect of Fortan and/or
gimple that I'm missing?

Thanks
Dave


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Question about Fortran bounds and -Wanalyzer-use-of-uninitialized-value
  2022-10-12 13:46 Question about Fortran bounds and -Wanalyzer-use-of-uninitialized-value David Malcolm
@ 2022-10-12 18:32 ` Harald Anlauf
  0 siblings, 0 replies; 2+ messages in thread
From: Harald Anlauf @ 2022-10-12 18:32 UTC (permalink / raw)
  To: fortran

Hi Dave,

Am 12.10.22 um 15:46 schrieb David Malcolm via Fortran:
> Sorry in advance if this is a silly question; my knowledge of Fortran
> is next to nothing, I'm afraid.
> 
> PR analyzer/107210 reports an ICE in -fanalyzer on this reproducer:
> 
> 
> ! { dg-additional-options "-O1" }
> 
> subroutine check_int (j)
>    INTEGER(4) :: i, ia(5), ib(5,4), ip, ipa(:)
>    target :: ib
>    POINTER :: ip, ipa
>    logical :: l(5)
> 
>    l = (/ sizeof(i) == 4, sizeof(ia) == 20, sizeof(ib) == 80, &
>         sizeof(ip) == 4, sizeof(ipa) == 8 /) ! { dg-warning "use of uninitialized value" }
> 
>    if (any(.not.l)) STOP 4
> 
> end subroutine check_int
> 
> 
> The fix for the ICE is trivial (a missing check that tree_fits_uhwi_p),
> but after the fix, I see these warnings from the analyzer:
> 
> 
>     10 |        sizeof(ip) == 4, sizeof(ipa) == 8 /)
>        |                                           ^
> Warning: use of uninitialized value ‘ipa.dim[0].ubound’ [CWE-457] [-Wanalyzer-use-of-uninitialized-value]
>    ‘check_int’: events 1-3
>      |
>      |    4 |   INTEGER(4) :: i, ia(5), ib(5,4), ip, ipa(:)
>      |      |                                             ^
>      |      |                                             |
>      |      |                                             (1) region created on stack here
>      |      |                                             (2) capacity: 8 bytes
>      |......
>      |   10 |        sizeof(ip) == 4, sizeof(ipa) == 8 /)
>      |      |                                           ~
>      |      |                                           |
>      |      |                                           (3) use of uninitialized value ‘ipa.dim[0].ubound’ here
>      |
> ../../src/gcc/testsuite/gfortran.dg/analyzer/pr107210.f90:10:43:
> 
>     10 |        sizeof(ip) == 4, sizeof(ipa) == 8 /)
>        |                                           ^
> Warning: use of uninitialized value ‘ipa.dim[0].lbound’ [CWE-457] [-Wanalyzer-use-of-uninitialized-value]
>    ‘check_int’: events 1-3
>      |
>      |    4 |   INTEGER(4) :: i, ia(5), ib(5,4), ip, ipa(:)
>      |      |                                             ^
>      |      |                                             |
>      |      |                                             (1) region created on stack here
>      |      |                                             (2) capacity: 8 bytes
>      |......
>      |   10 |        sizeof(ip) == 4, sizeof(ipa) == 8 /)
>      |      |                                           ~
>      |      |                                           |
>      |      |                                           (3) use of uninitialized value ‘ipa.dim[0].lbound’ here
>      |
> 
> 
> The gimple in question is:
> 
> __attribute__((fn spec (". w ")))
> void check_int (integer(kind=4) & restrict j)
> {
>    integer(kind=8) ipa$dim$0$lbound;
>    integer(kind=8) ipa$dim$0$ubound;
>    logical(kind=4) A.1[5];
>    logical(kind=4) l[5];
>    integer(kind=8) _1;
>    logical(kind=4) _3;
>    logical(kind=4) _4;
>    integer(kind=8) _5;
>    logical(kind=4) _6;
>    integer(kind=8) S.5_7;
>    logical(kind=4) test.6_8;
>    integer(kind=8) S.7_9;
>    integer(kind=8) S.5_16;
>    integer(kind=8) S.7_18;
> 
>    <bb 2> [local count: 178992760]:
>    MEM <uint128_t> [(c_char * {ref-all})&A.1] = 0x1000000010000000100000001;
>    _1 = ipa$dim$0$ubound_2(D) - ipa$dim$0$lbound_12(D);
>    _3 = _1 == 1;
>    MEM[(logical(kind=4) *)&A.1 + 16B] = _3;
> 
> [...snip...]
> 
> where the analyzer is complaining about this gimple statement:
>    _1 = ipa$dim$0$ubound_2(D) - ipa$dim$0$lbound_12(D);
> where both:
>    ipa$dim$0$ubound_2(D)
> and:
>    ipa$dim$0$lbound_12(D)
> are considered by it to be uninitialized.
> 
> Is the analyzer correct here, or is there an aspect of Fortan and/or
> gimple that I'm missing?

if you compile w/o -fanalyzer but with -O -Wall you will get
warnings, too.  Note that SIZEOF is an extension documented here:

https://gcc.gnu.org/onlinedocs/gfortran/SIZEOF.html

where is says:

..  If the argument has the POINTER attribute, the number of bytes of 
the storage area pointed to is returned. ...

which can be determined only if the arrays bounds are known.
The testcase gfortran.dg/sizeof.f90 from which the above was reduced
sets these bounds via

   ipa=>ib(2:3,1)

If I restore this, then I get no related warnings for -O -Wall.

> Thanks
> Dave





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-10-12 18:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-12 13:46 Question about Fortran bounds and -Wanalyzer-use-of-uninitialized-value David Malcolm
2022-10-12 18:32 ` Harald Anlauf

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).