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

            Bug ID: 129312
           Summary: wrong code at -O2: DOM removes a trapping redundant
                    store with -fno-delete-dead-exceptions
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kyuwoncho18 at gmail dot com
  Target Milestone: ---
            Target: x86_64-pc-linux-gnu

#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <sys/mman.h>

static sigjmp_buf escape;

static void fault(int sig)
{
  (void)sig;
  siglongjmp(escape, 1);
}

__attribute__((noinline))
static int probe(int *p)
{
  int x = *p;
  *p = x;
  return x;
}

int main(void)
{
  int *p = mmap(0, 4096, PROT_READ | PROT_WRITE,
                MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  struct sigaction sa = { .sa_handler = fault };
  if (p == MAP_FAILED)
    return 2;
  *p = 29;
  sigemptyset(&sa.sa_mask);
  if (mprotect(p, 4096, PROT_READ) || sigaction(SIGSEGV, &sa, 0))
    return 2;
  if (sigsetjmp(escape, 1) == 0)
    printf("no trap %d\n", probe(p));
  else
    puts("trap");
}

godbolt: https://godbolt.org/z/fj3cesPdn

At -O0 the program prints "trap", while at -O2 -fno-tree-fre it prints "no trap
29"; both use -fnon-call-exceptions -fno-delete-dead-exceptions. The store
targets a read-only mapping and must remain observable as a trap, so "trap" is
correct.

Reply via email to