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

            Bug ID: 129183
           Summary: [14/15/16/17 Regression] RTL if-conversion hoists a
                    store that is only reachable after the second return
                    of a nested setjmp
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lzy20000419 at outlook dot com
  Target Milestone: ---
              Host: x86_64-pc-linux-gnu
            Target: x86_64-pc-linux-gnu

Fails with trunk (gcc 17.0.0 20261201, 96693d6f00bd) and with 4.9.0 through
16.2.0; 4.7.3, 4.7.4, 4.8.1 and 4.8.5 work. Target: x86_64-pc-linux-gnu.

The testcase below must print 6, but at -O1, -O2, -O3 and -Os it prints 106.
After longjmp(outer_jb, 1), x must still be 6, because nothing stores to x
between setjmp(outer_jb) and the longjmp; x += 100 only runs on the second
return of the inner setjmp, which never happens.

    #include <setjmp.h>
    #include <stdio.h>
    static jmp_buf outer_jb, inner_jb;
    int main(void) {
        int x = 6;
        if (setjmp(outer_jb) == 0) {
            if (setjmp(inner_jb) == 0) {
                longjmp(outer_jb, 1);
            }
            x += 100;                  /* not reached */
        }
        printf("%d\n", x);             /* must be 6 */
        return x != 6;
    }

    gcc -O2 t.c && ./a.out

The program is expected to print 6 and exit 0. On a native x86_64-pc-linux-gnu
build of GCC 17.0.0 20261201 (experimental), git commit 96693d6f00bd, compiling
with -O2 prints 106 and exits 1. -O1, -O3 and -Os also print 106. -O0 prints 6.
-O2 -fno-if-conversion prints 6, so the store is speculated by RTL
if-conversion. -O2 -fwrapv still prints 106; there is no signed overflow in the
program. An -O0 -fsanitize=undefined -fno-sanitize-recover=all build with gcc
14.2.0 prints 6 and exits 0.

The same -O2 command also prints 106 on 4.9.0 through 16.2.0 (4.9.4 could not
be executed on Compiler Explorer). 4.7.3, 4.7.4, 4.8.1 and 4.8.5 print 6 at -O0
and -O2; 4.8.5 also prints 6 at -O1 and -Os. clang trunk prints 6 at -O0, -O2
and -Os.

Godbolt (trunk): https://godbolt.org/z/cPbrP5zMs

Trunk -O2 code (abridged):

    movl    $6, 12(%rsp)
    call    _setjmp
    testl   %eax, %eax
    jne     .L3                     ; outer setjmp second return
    movl    $inner_jb, %edi
    call    _setjmp
    movl    $106, 12(%rsp)          ; store for x += 100, hoisted here
    testl   %eax, %eax
    je      .L7                     ; inner setjmp first return -> longjmp
    ...
.L7:
    movl    $1, %esi
    movl    $outer_jb, %edi
    call    longjmp

C11/C17 7.13.1.1 paragraph 5 says that after longjmp, the values of
non-volatile automatic objects that have been changed between the setjmp
invocation and the longjmp call are indeterminate, which implies that objects
not changed in that window retain their values. There is no store to x between
setjmp(outer_jb) and longjmp(outer_jb, 1): the only later store is x += 100,
which is reachable only on the second return from setjmp(inner_jb), and no
longjmp to inner_jb ever occurs. x is therefore required to be 6 after the
longjmp.

This is not PR128520, which is a GCC 17 fwprop / late-combine wrong-code bug
involving setjmp. RTL if-conversion has no check for calls_setjmp.

See Also PR57067 (RTL lacks abnormal edges for setjmp). That report does not
include this testcase.

The store appears to be speculated by RTL if-conversion; -fno-if-conversion is
enough to get the correct result. I have not tested a patch.

Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.

Reply via email to