public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "slyfox at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/114872] [13/14/15 Regression] Miscompilation with -O2 after commit r13-8037
Date: Wed, 08 May 2024 18:10:22 +0000	[thread overview]
Message-ID: <bug-114872-4-6wBqFUx73q@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-114872-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #25 from Sergei Trofimovich <slyfox at gcc dot gnu.org> ---
(In reply to Richard Biener from comment #24)
> (In reply to Sergei Trofimovich from comment #23)
> [...]
> > Why did `gcc` generate unconditional NULL dereference here? I suspect it
> > somehow inferred that `__pyx_t_6 = NULL;` in that branch, but not before
> > comparison.
> 
> That's what happens if we isolate an unreachable path because of a NULL
> dereference (like if exposed by jump-threading).  We make the NULL
> dereference volatile so it stays but DCE/DSE can cleanup code on the path
> leading to it.
> 
> If you run into such path the this might suggest that jump-threading
> triggered
> a problem with the setjmp/longjmp, so it's then likely some condition that's
> evaluated in a wrong way after the longjmp, either because a dependent
> value wasn't properly preserved or by GCC breaking that.  Seeing stack memory
> arguments used on a call in a previous comment

Yeah, that makes sense. Having stared a bit more at
__pyx_pf_4sage_4libs_3gap_7element_19GapElement_Function_2__call__() I
think I get the problem now. We deal with the code similar to the following:

__pyx_pf_4sage_4libs_3gap_7element_19GapElement_Function_2__call__() {
    __pyx_t_6 = NULL;

    // the loop is not very important, but it forces `__pyx_t_6` initialization
before `setjmp()`
    for (;;) {
        __pyx_t_6 = something();
        int done = use_pre_setjmp(__pyx_t_6);
        __pyx_t_6 = NULL;
        if (done) break;
    }

    int mode = setjmp(jb);

    switch (mode) {
      case 1: // longjmp() case
        break;
      case 0: // regular case (`case 3:` in real code)
        __pyx_t_6 = something_else(); // set __pyx_t_6 to non-zero
        int done = use_post_setjmp(__pyx_t_6); // call longjmp(jb, 1) here
        __pyx_t_6 = NULL;
        break;
    }

    // get here via longjmp()
    if (__pyx_t_6 != NULL) deref(__pyx_t_6);
}

AFAIU `gcc` is smart enough to see that all paths to `deref()` reach with
`__pyx_t_6 = NULL`, but it does not eliminate the `deref()` entirely
and uses `if (__pyx_t_6 != NULL) deref(NULL);` as Richard explained above.

Now due to `longjmp()` `__pyx_t_6 = NULL;` does not get executed (even
though it's present in assembly code as `movq $0, -200(%rbp)` in all the
places where it's present in C code.

As a result after the `longjmp()` `__pyx_t_6` is not `NULL` and we get
to `deref(NNULL)` and SIGSEGV.

Thus it's a matter of missing `volatile __pyx_t_6`. Sounds about right?

> I wondered if POSIX suggests
> that even non-register variables need to be made volatile and thus whether
> SRA or FRE might impose problems with code using setjmp/longjmp.

That matches my understanding as well. Would it be fair to say that sprinkling
`volatile` has to be done for every single local variable in the function to
prevent
possible stack reuse? And that rule should extend to the functions that could
host an inline variant of the function using setjmp()/longjmp() and not just
immediate caller of setjmp()/longjmp()?

  parent reply	other threads:[~2024-05-08 18:10 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-27 18:57 [Bug c/114872] New: Miscompilation with -O2 after commit 049ec9b981d1f4f97736061d5cf7d0ae990b57d7 arojas at archlinux dot org
2024-04-27 18:59 ` [Bug c/114872] " arojas at archlinux dot org
2024-04-27 19:03 ` pinskia at gcc dot gnu.org
2024-04-27 19:05 ` pinskia at gcc dot gnu.org
2024-04-27 19:06 ` arojas at archlinux dot org
2024-04-27 20:00 ` [Bug tree-optimization/114872] [13/14/15 Regression] Miscompilation with -O2 after commit r13-8037 pinskia at gcc dot gnu.org
2024-04-29 15:53 ` sjames at gcc dot gnu.org
2024-04-29 16:00 ` jakub at gcc dot gnu.org
2024-04-29 20:05 ` arojas at archlinux dot org
2024-05-01  1:00 ` sjames at gcc dot gnu.org
2024-05-01  1:01 ` sjames at gcc dot gnu.org
2024-05-01  1:01 ` sjames at gcc dot gnu.org
2024-05-01  1:03 ` sjames at gcc dot gnu.org
2024-05-03 11:07 ` dima.pasechnik at cs dot ox.ac.uk
2024-05-03 11:26 ` dima.pasechnik at cs dot ox.ac.uk
2024-05-06 13:53 ` slyfox at gcc dot gnu.org
2024-05-06 17:29 ` jakub at gcc dot gnu.org
2024-05-06 17:59 ` jakub at gcc dot gnu.org
2024-05-06 21:41 ` slyfox at gcc dot gnu.org
2024-05-06 22:08 ` slyfox at gcc dot gnu.org
2024-05-06 22:45 ` dima.pasechnik at cs dot ox.ac.uk
2024-05-07 10:45 ` jakub at gcc dot gnu.org
2024-05-07 11:05 ` slyfox at gcc dot gnu.org
2024-05-07 22:13 ` slyfox at gcc dot gnu.org
2024-05-08  8:18 ` slyfox at gcc dot gnu.org
2024-05-08  8:27 ` rguenth at gcc dot gnu.org
2024-05-08 18:10 ` slyfox at gcc dot gnu.org [this message]
2024-05-08 19:17 ` dima.pasechnik at cs dot ox.ac.uk
2024-05-08 19:35 ` pinskia at gcc dot gnu.org
2024-05-08 19:58 ` jakub 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-114872-4-6wBqFUx73q@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).