public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "andrew.cooper3 at citrix dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/104971] New: Optimisation for __builtin_ia32_readeflags corrupts the stack
Date: Thu, 17 Mar 2022 18:49:41 +0000	[thread overview]
Message-ID: <bug-104971-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 104971
           Summary: Optimisation for __builtin_ia32_readeflags corrupts
                    the stack
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andrew.cooper3 at citrix dot com
  Target Milestone: ---

Full example: https://godbolt.org/z/xGq3c4Mnc

Given:

int broken(void)
{
    int fl = __builtin_ia32_readeflags_u64();
}

gcc -O2 generates:

broken:
        pushfq
        ret

Which is going explode very quickly.

Code generation appears to be safe without optimisation, but even -O alone is
enough to create problems.

At a guess, the optimiser has concluded that the result is unused, drops the
`pop %reg`, but fails to also drop the `pushf` too.

Looking through history on Godbolt, it appears that GCC 4.9 (which introduced
this builtin) has correct optimised code generation, and it regressed between
4.9 and 5.1.

             reply	other threads:[~2022-03-17 18:49 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-17 18:49 andrew.cooper3 at citrix dot com [this message]
2022-03-17 19:05 ` [Bug middle-end/104971] [9/10/11/12 Regression] " jakub at gcc dot gnu.org
2022-03-17 19:35 ` jakub at gcc dot gnu.org
2022-03-17 20:49 ` andrew.cooper3 at citrix dot com
2022-03-17 21:12 ` mpolacek at gcc dot gnu.org
2022-03-18 17:32 ` jakub at gcc dot gnu.org
2022-03-19 12:53 ` cvs-commit at gcc dot gnu.org
2022-03-25 16:31 ` [Bug middle-end/104971] [9/10/11 " cvs-commit at gcc dot gnu.org
2022-03-29  5:54 ` cvs-commit at gcc dot gnu.org
2022-03-30  8:17 ` [Bug middle-end/104971] [9/10 " jakub at gcc dot gnu.org
2022-05-10  8:25 ` cvs-commit at gcc dot gnu.org
2022-05-11  6:25 ` cvs-commit at gcc dot gnu.org

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=bug-104971-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).