public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Mark Eggleston <mark.eggleston@codethink.co.uk>
To: Jakub Jelinek <jakub@redhat.com>, Andreas Schwab <schwab@linux-m68k.org>
Cc: Jeff Law <law@redhat.com>,
	sgk@troutmask.apl.washington.edu,
	Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>,
	gcc-patches <gcc-patches@gcc.gnu.org>,
	fortran <fortran@gcc.gnu.org>
Subject: Re: [PATCH] Automatics in equivalence statements
Date: Wed, 02 Oct 2019 13:39:00 -0000	[thread overview]
Message-ID: <5717dc7c-d6be-3559-b710-5c7b20889881@codethink.co.uk> (raw)
In-Reply-To: <20190930102438.GI15914@tucnak>

Thanks, as you point out all the test needs to do is verify that that a 
variable with an AUTOMATIC attribute can be used in an EQUIVALENCE and 
and that the items in the EQUIVALENCE are on the stack by using in a 
recursive routine.

I've created a patch to replace the existing test cases and have sent it 
to this e-mail thread https://gcc.gnu.org/ml/fortran/2019-09/msg00123.html

regards,

Mark

On 30/09/2019 11:24, Jakub Jelinek wrote:
> On Sat, Sep 28, 2019 at 10:33:26PM +0200, Andreas Schwab wrote:
>> On Aug 14 2019, Mark Eggleston <mark.eggleston@codethink.co.uk> wrote:
>>
>>>      * gfortran.dg/auto_in_equiv_3.f90: New test.
>> This test fails everywhere.
> Yes, and _2 on i686-linux at -O0.
>
> To me both testcases are undefined behavior.
> E.g. the first one has:
>    subroutine suba(option)
>      integer, intent(in) :: option
>      integer, automatic :: a
>      integer :: b
>      integer :: c
>      equivalence (a, b)
>      if (option.eq.0) then
>        ! initialise a and c
>        a = 9
>        c = 99
>        if (a.ne.b) stop 1
>        if (loc(a).ne.loc(b)) stop 2
>      else
>        ! a should've been overwritten
>        if (a.eq.9) stop 3
>      end if
>    end subroutine suba
> My understanding is that because a is explicitly automatic and b is automatic too
> (implicitly), the whole equivalence is automatic, so if you call this
> subroutine with non-zero option, you read an uninitialized variable and
> compare it to 9.  That can result in anything, .false., .true., disk
> formatting, you can't rely on some other routine laying out its automatic
> variable at exactly the same spot and overwriting the memory in there, not
> to mention that the compiler can easily spot the uninitialized use too.
> Similarly in the second test, returning address of an automatic variable
> from a function is already something e.g. the C/C++ FEs warn about, because
> you really can't do anything useful with that address, it can't be
> dereferenced, or even the comparisons to addresses of other automatic
> variables that left their scope is wrong.
>
> IMHO if you want to check if a variable is SAVEd or AUTOMATIC, you want to
> recurse, either directly or indirectly, pass the address of the variable in
> the outer subroutine down to the inner one and compare there, SAVEd
> variables need to have the same address, while AUTOMATIC variables where
> both the outer and inner variable is at that point still in the scope need
> to have the addresses different.
>
> Though, in order to have in Fortran a recursively callable subroutine, one
> needs to use RECURSIVE.
> So, IMHO we want 4 testcases out of these 2, two dg-do compile only which
> will verify the tests compile when mixing automatic with no explicit
> save/automatic in equivalence and will -fdump-tree-gimple and scan the
> gimple dump to verify there is equiv.\[0-9]* variable which is not static,
> and then two runtime testcases like (one with just -fdec-static, one with
> also -fno-automatic, though guess it doesn't matter that much, as recursive
> already implies that it is automatic).
> program test
>    integer :: dummy
>    integer, parameter :: address = kind(loc(dummy))
>    integer(address) :: addr
>    addr = 0
>    call sub (0, addr)
> contains
>    recursive subroutine sub (option, addr)
>      integer, intent(in) :: option
>      integer(address), intent(in) :: addr
>      integer, automatic :: a
>      integer :: b
>      integer(address) :: c
>      equivalence (a, b)
>      if (option.eq.0) then
>        a = 9
>        if (a.ne.b) stop 1
>        if (loc(a).ne.loc(b)) stop 2
>        c = loc(a)
>        call sub (1, c)
>        if (a.ne.9) stop 3
>      else
>        a = 10
>        if (a.ne.b) stop 4
>        if (loc(a).ne.loc(b)) stop 5
>        if (addr.eq.loc(a)) stop 6
>      end if
>    end subroutine sub
> end program test
>
> 	Jakub
>
-- 
https://www.codethink.co.uk/privacy.html

  reply	other threads:[~2019-10-02 13:39 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <fe0dee5c-e2d7-5e69-94e4-0a807f9f0886@redhat.com>
     [not found] ` <20190723013806.GA12299@troutmask.apl.washington.edu>
     [not found]   ` <3497ca30-0bd6-ac0a-0069-098f25de44d3@redhat.com>
     [not found]     ` <20190723023614.GA12520@troutmask.apl.washington.edu>
     [not found]       ` <cd31af75-cc5a-3cab-1993-21f431a6f09f@redhat.com>
2019-08-14  8:49         ` Mark Eggleston
2019-08-14 17:24           ` Jeff Law
2019-08-16 15:51             ` Mark Eggleston
2019-09-28 20:33           ` Andreas Schwab
2019-09-30 10:25             ` Jakub Jelinek
2019-10-02 13:39               ` Mark Eggleston [this message]
2019-06-21 13:31 Mark Eggleston
2019-06-21 14:10 ` Steve Kargl
2019-06-24  8:19   ` Bernhard Reutner-Fischer
2019-06-24 13:47     ` Mark Eggleston
2019-06-24 23:18     ` Jeff Law
2019-06-25 13:17       ` Mark Eggleston
2019-07-01  9:35         ` Mark Eggleston
2019-07-23  0:23           ` Jeff Law

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=5717dc7c-d6be-3559-b710-5c7b20889881@codethink.co.uk \
    --to=mark.eggleston@codethink.co.uk \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=law@redhat.com \
    --cc=rep.dot.nop@gmail.com \
    --cc=schwab@linux-m68k.org \
    --cc=sgk@troutmask.apl.washington.edu \
    /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: link
Be 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).