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

            Bug ID: 127601
           Summary: wrong code at -O3 due to vectorizer
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kyuwoncho18 at gmail dot com
  Target Milestone: ---

### Reproducer
#include <stdio.h>
#include <sys/mman.h>

#define PG 4096

static long
rev (long *a, int n)
{
  long s = 0;
  for (int i = n - 1; i >= 0; i--)
    s += a[4 * i] + a[4 * i + 1] + a[4 * i + 2];
  return s;
}

int main (void)
{
  unsigned char *p = mmap (0, 2 * PG, PROT_READ | PROT_WRITE,
                           MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  if (p == MAP_FAILED || mprotect (p + PG, PG, PROT_NONE))
    { printf ("setup failed\n"); return 2; }

  int n = 9;
  unsigned long ne = 4UL * (n - 1) + 3;         /* 35 longs are touched */
  long *a = (long *) (p + PG - ne * sizeof (long));
  for (unsigned long i = 0; i < ne; i++)
    a[i] = (long) (i * 3 + 1);

  printf ("%ld\n", rev (a, n));
  return 0;
}

Result:
O{0,1,2}: 1404
O3: SIGSEGV

https://gcc.godbolt.org/z/Ph56qMKvs

Reply via email to