public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/96956] New: When gcc does not see a label used in a goto it gives the wrong label  address &&label
@ 2020-09-07 14:01 thomas.lynch at reasoningtechnology dot com
  2020-09-07 15:35 ` [Bug c/96956] " jakub at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: thomas.lynch at reasoningtechnology dot com @ 2020-09-07 14:01 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96956

            Bug ID: 96956
           Summary: When gcc does not see a label used in a goto it gives
                    the wrong label  address &&label
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: thomas.lynch at reasoningtechnology dot com
  Target Milestone: ---

Created attachment 49193
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49193&action=edit
.c .i .s gcc version system info

The attached is an example distilled from much larger code.  It makes use of
the gcc extension  of  taking the address of a label. All optimizations are
turned off `-O0`.

The && operator returns the address for a different label than the one
requested.  Specificaly in this exmaple `&&nominal` returns the address for a
different label, `&&test0`.

The code example shown makes use of an inlined nested function to hide the goto
from gcc. However, the error occurs before the nested function is called. Note
the printf %p printf statements. 

With small variations of this code, gcc will return the correct address for
`&&nominal`.  If gcc sees an explicit goto to the label for
`&&nominal`, then the printf %p will give the correct value.

See attached for .c .i .s gcc version and sys info.


/* broken.c ----------------------------------------------------------------

gcc -std=gnu2x -Wall -O0 -ggdb  -o broken broken.c 

*/

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

typedef void **CV·Ptr; 
CV·Ptr target_pt;
uint i = 0;

int main(){

  goto test0;

  inline void do_jmp(CV·Ptr target_pt){
    goto *target_pt;
  }

  test0:;

  i++; // gets optimized away
  printf("%x\n", i);
  goto report;

  nominal:
  i++;
  goto tests_finished;

  report:;
  target_pt = &&nominal;
  printf("test0: %p\n", &&test0);
  // this will be identical to &&test0 and we haven't even called do_jmp:
  printf("nominal: %p\n", target_pt); 

  if( i == 2 ){
    printf("foo!\n");
    goto tests_finished;
  }
  do_jmp(target_pt);

  tests_finished:;
}

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

* [Bug c/96956] When gcc does not see a label used in a goto it gives the wrong label  address &&label
  2020-09-07 14:01 [Bug c/96956] New: When gcc does not see a label used in a goto it gives the wrong label address &&label thomas.lynch at reasoningtechnology dot com
@ 2020-09-07 15:35 ` jakub at gcc dot gnu.org
  2021-03-26  7:43 ` pinskia at gcc dot gnu.org
  2024-03-16 17:58 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-09-07 15:35 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96956

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Your testcase is invalid.
https://gcc.gnu.org/onlinedocs/gcc-10.2.0/gcc/Labels-as-Values.html#Labels-as-Values
"You may not use this mechanism to jump to code in a different function. If you
do that, totally unpredictable things happen."
GCC has a different extension, described in the Local labels chapter, non-local
labels, but in that case the goto needs to be direct to the non-local label,
not using a computed goto.
What you see is the result of the compiler not adding any edges in the cfg from
the nested function to the label with address taken, the compiler only does
that for non-local labels, or between computed gotos in the current function
and the labels whose address is taken.

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

* [Bug c/96956] When gcc does not see a label used in a goto it gives the wrong label  address &&label
  2020-09-07 14:01 [Bug c/96956] New: When gcc does not see a label used in a goto it gives the wrong label address &&label thomas.lynch at reasoningtechnology dot com
  2020-09-07 15:35 ` [Bug c/96956] " jakub at gcc dot gnu.org
@ 2021-03-26  7:43 ` pinskia at gcc dot gnu.org
  2024-03-16 17:58 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-03-26  7:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96956

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mh+gcc at glandium dot org

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 99784 has been marked as a duplicate of this bug. ***

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

* [Bug c/96956] When gcc does not see a label used in a goto it gives the wrong label  address &&label
  2020-09-07 14:01 [Bug c/96956] New: When gcc does not see a label used in a goto it gives the wrong label address &&label thomas.lynch at reasoningtechnology dot com
  2020-09-07 15:35 ` [Bug c/96956] " jakub at gcc dot gnu.org
  2021-03-26  7:43 ` pinskia at gcc dot gnu.org
@ 2024-03-16 17:58 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-16 17:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96956

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|INVALID                     |DUPLICATE

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
.

*** This bug has been marked as a duplicate of bug 44298 ***

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

end of thread, other threads:[~2024-03-16 17:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-07 14:01 [Bug c/96956] New: When gcc does not see a label used in a goto it gives the wrong label address &&label thomas.lynch at reasoningtechnology dot com
2020-09-07 15:35 ` [Bug c/96956] " jakub at gcc dot gnu.org
2021-03-26  7:43 ` pinskia at gcc dot gnu.org
2024-03-16 17:58 ` pinskia at gcc dot gnu.org

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