public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/99207] New: #pragma pack(1) and __int128 lead to bad optimization under O2 and higher optimization
@ 2021-02-22 17:03 zhan3299 at purdue dot edu
  2021-02-22 17:10 ` [Bug c/99207] " jakub at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: zhan3299 at purdue dot edu @ 2021-02-22 17:03 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 99207
           Summary: #pragma pack(1) and __int128 lead to bad optimization
                    under O2 and higher optimization
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zhan3299 at purdue dot edu
  Target Milestone: ---

Following code behaves differently with different optimizations.


$ cat test.c
#pragma pack(1)

struct {
  char a;
  __int128 b;
} c;

__int128 *d = &c.b;

int main() { *d = 0; }



In short, with O2 and higher optimization, gcc tries to use xmm register to
initialize *d. However, as #pragma pack(1) enforces a different alignments, xmm
operation will trigger a segment fault.

O0 (program returns 0): https://godbolt.org/z/nc997z
O1 (program returns 0): https://godbolt.org/z/GqGfnf
O2 (program returns 139): https://godbolt.org/z/4PWKMo
O3 (program returns 139): https://godbolt.org/z/Y81sKK
Os (program returns 139): https://godbolt.org/z/M3KGnh

With asan, we can get: https://godbolt.org/z/34oW7e
AddressSanitizer:DEADLYSIGNAL
=================================================================
==1==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x0000004010ae bp
0x000000000000 sp 0x7ffd361619b8 T0)
==1==The signal is caused by a READ memory access.
==1==Hint: this fault was caused by a dereference of a high value address (see
register values below).  Dissassemble the provided pc to learn which register
was used.
    #0 0x4010ae in main example.c:10
    #1 0x7f8c4b3b60b2 in __libc_start_main
(/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
    #2 0x40111d in _start (/app/output.s+0x40111d)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV example.c:10 in main
==1==ABORTING

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

* [Bug c/99207] #pragma pack(1) and __int128 lead to bad optimization under O2 and higher optimization
  2021-02-22 17:03 [Bug c/99207] New: #pragma pack(1) and __int128 lead to bad optimization under O2 and higher optimization zhan3299 at purdue dot edu
@ 2021-02-22 17:10 ` jakub at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-02-22 17:10 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
That is a user error, don't do that.
The fact that d doesn't point to properly aligned object needs to be visible on
the pointer dereference location, so you would need to use e.g. aligned(1)
attribute on e.g. a typedef to __int128, e.g.
typedef __int128 T __attribute__((aligned (1)));
T *d = &c.b;
Otherwise it is UB and -fsanitize=undefined would have diagnosed it.

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

end of thread, other threads:[~2021-02-22 17:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-22 17:03 [Bug c/99207] New: #pragma pack(1) and __int128 lead to bad optimization under O2 and higher optimization zhan3299 at purdue dot edu
2021-02-22 17:10 ` [Bug c/99207] " jakub 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).