public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/105994] New: Incorrect optimization with -ftree-sra and casting (strictly compatible) refs
@ 2022-06-16  0:10 gcc_bugzilla.calin79 at neverbox dot com
  2022-06-16  0:21 ` [Bug c++/105994] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: gcc_bugzilla.calin79 at neverbox dot com @ 2022-06-16  0:10 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105994
           Summary: Incorrect optimization with -ftree-sra and casting
                    (strictly compatible) refs
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc_bugzilla.calin79 at neverbox dot com
  Target Milestone: ---

This code (also in attachment) should return 106, but instead returns 14:


////////////////////////////////////////////////////////////////

struct bar
{
    int id;
};

struct foo
{
    int id;
};

inline const bar& h(const foo& t)
{
    return reinterpret_cast<const bar&>(t);
}

int g(foo t)
{
    const bar& test = h(foo{t});

    if(test.id == 5)
        return 7;

    return test.id;
}

int main()
{
    int a = g(foo{5});
    int b = g(foo{99});
    return a + b;
}
////////////////////////////////////////////////////////////////


It happens with -O1 - and more specifically, as long as `-finline -ftree-ccp
-ftree-sra` are enabled.


Version: gcc version 13.0.0 20220615 (experimental) (GCC)
    built from latest master as of 2022-06-15,
dc9b92facf87a6f2d8b0e5d5fc404f30c3b15a74
Target: x86_64-pc-linux-gnu
Configured with: ../src/configure
--prefix=/opt/compiler-explorer/gcc-build/install --enable-languages=c++

Triggered with:
g++ -std=c++20 -O1 -o buggy_bin_14 gcc_bug_reference_cast.cpp
# should print 106 (7 + 99)
./buggy_bin_14; echo $?
# instead prints 14 - g() always returns 7

Shortest number of optimizations that triggers it:
g++ -std=c++20 -O1 -fno-auto-inc-dec -fno-branch-count-reg
-fno-combine-stack-adjustments -fno-compare-elim -fno-cprop-registers -fno-dce
-fno-defer-pop -fno-delayed-branch -fno-dse -fno-forward-propagate
-fno-guess-branch-probability -fno-if-conversion -fno-if-conversion2
-fno-inline-functions-called-once -fno-ipa-profile -fno-ipa-pure-const
-fno-ipa-reference -fno-ipa-reference-addressable -fno-merge-constants
-fno-move-loop-invariants -fno-omit-frame-pointer -fno-reorder-blocks
-fno-shrink-wrap -fno-shrink-wrap-separate -fno-split-wide-types
-fno-ssa-backprop -fno-ssa-phiopt -fno-tree-bit-ccp -fno-tree-ch
-fno-tree-coalesce-vars -fno-tree-copy-prop -fno-tree-dce
-fno-tree-dominator-opts -fno-tree-dse -fno-tree-forwprop -fno-tree-fre
-fno-tree-phiprop -fno-tree-pta -fno-tree-scev-cprop -fno-tree-sink
-fno-tree-slsr -fno-tree-ter -fno-unit-at-a-time -finline -ftree-ccp -ftree-sra
-o buggy_bin_14 gcc_bug_reference_cast.cpp

Compiler output:
None

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

* [Bug c++/105994] Incorrect optimization with -ftree-sra and casting (strictly compatible) refs
  2022-06-16  0:10 [Bug c++/105994] New: Incorrect optimization with -ftree-sra and casting (strictly compatible) refs gcc_bugzilla.calin79 at neverbox dot com
@ 2022-06-16  0:21 ` pinskia at gcc dot gnu.org
  2022-06-16  0:22 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-16  0:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Since bar and foo are not the same/compatible types this violates aliasing
rules. They might be layout compatible but they are compatible. Two different
things.

Also

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

* [Bug c++/105994] Incorrect optimization with -ftree-sra and casting (strictly compatible) refs
  2022-06-16  0:10 [Bug c++/105994] New: Incorrect optimization with -ftree-sra and casting (strictly compatible) refs gcc_bugzilla.calin79 at neverbox dot com
  2022-06-16  0:21 ` [Bug c++/105994] " pinskia at gcc dot gnu.org
@ 2022-06-16  0:22 ` pinskia at gcc dot gnu.org
  2022-06-16  0:23 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-16  0:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Hmm, but at -O1, strict aliasing is off so this should work ...

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

* [Bug c++/105994] Incorrect optimization with -ftree-sra and casting (strictly compatible) refs
  2022-06-16  0:10 [Bug c++/105994] New: Incorrect optimization with -ftree-sra and casting (strictly compatible) refs gcc_bugzilla.calin79 at neverbox dot com
  2022-06-16  0:21 ` [Bug c++/105994] " pinskia at gcc dot gnu.org
  2022-06-16  0:22 ` pinskia at gcc dot gnu.org
@ 2022-06-16  0:23 ` pinskia at gcc dot gnu.org
  2022-06-16  0:25 ` pinskia at gcc dot gnu.org
  2022-06-16  0:28 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-16  0:23 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Actually it is invalid because the temp goes out of scope rather than aliasing
issues.

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

* [Bug c++/105994] Incorrect optimization with -ftree-sra and casting (strictly compatible) refs
  2022-06-16  0:10 [Bug c++/105994] New: Incorrect optimization with -ftree-sra and casting (strictly compatible) refs gcc_bugzilla.calin79 at neverbox dot com
                   ` (2 preceding siblings ...)
  2022-06-16  0:23 ` pinskia at gcc dot gnu.org
@ 2022-06-16  0:25 ` pinskia at gcc dot gnu.org
  2022-06-16  0:28 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-16  0:25 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
const bar& test = h(foo{t})

The temporary for foo here since it is not bound directly to the variable bar,
goes out of scope after the statement is finished.

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

* [Bug c++/105994] Incorrect optimization with -ftree-sra and casting (strictly compatible) refs
  2022-06-16  0:10 [Bug c++/105994] New: Incorrect optimization with -ftree-sra and casting (strictly compatible) refs gcc_bugzilla.calin79 at neverbox dot com
                   ` (3 preceding siblings ...)
  2022-06-16  0:25 ` pinskia at gcc dot gnu.org
@ 2022-06-16  0:28 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-06-16  0:28 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note with -fsanitize=address at runtime, GCC does detect this:

Program returned: 1
=================================================================
==1==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7fe456600020
at pc 0x0000004011e4 bp 0x7ffee65e3850 sp 0x7ffee65e3848
READ of size 4 at 0x7fe456600020 thread T0
    #0 0x4011e3 in g(foo) /app/example.cpp:20
    #1 0x401209 in main /app/example.cpp:28
    #2 0x7fe458c880b2 in __libc_start_main
(/lib/x86_64-linux-gnu/libc.so.6+0x240b2) (BuildId:
9fdb74e7b217d06c93172a8243f8547f947ee6d1)
    #3 0x4010ad in _start (/app/output.s+0x4010ad) (BuildId:
5bbb0ffd1be8c2b44c3b09d7f1bcf41dcd821425)

Address 0x7fe456600020 is located in stack of thread T0 at offset 32 in frame
    #0 0x401175 in g(foo) /app/example.cpp:17

  This frame has 1 object(s):
    [32, 36) '<unknown>' <== Memory access at offset 32 is inside this variable
HINT: this may be a false positive if your program uses some custom stack
unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-use-after-scope /app/example.cpp:20 in g(foo)
Shadow bytes around the buggy address:
  0x0ffd0acb7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ffd0acb7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ffd0acb7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ffd0acb7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ffd0acb7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0ffd0acb8000: f1 f1 f1 f1[f8]f3 f3 f3 00 00 00 00 00 00 00 00
  0x0ffd0acb8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ffd0acb8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ffd0acb8030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ffd0acb8040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0ffd0acb8050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==1==ABORTING

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

end of thread, other threads:[~2022-06-16  0:28 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-16  0:10 [Bug c++/105994] New: Incorrect optimization with -ftree-sra and casting (strictly compatible) refs gcc_bugzilla.calin79 at neverbox dot com
2022-06-16  0:21 ` [Bug c++/105994] " pinskia at gcc dot gnu.org
2022-06-16  0:22 ` pinskia at gcc dot gnu.org
2022-06-16  0:23 ` pinskia at gcc dot gnu.org
2022-06-16  0:25 ` pinskia at gcc dot gnu.org
2022-06-16  0:28 ` 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).