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

            Bug ID: 127838
           Summary: Incorrect vectorization with -march=x86-64-v2
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kristerw at gcc dot gnu.org
            Blocks: 118443
  Target Milestone: ---

The function f below is miscompiled on x86_64 with -O3 -march=x86-64-v2.

The overlap check inserted by the vectorizer overflows and therefore fails to
detect that writing to res may modify the element accessed in the next
iteration.


#include <sys/mman.h>
#include <unistd.h>
#include <stdint.h>

__attribute__((noipa))
void f (const uint32_t *s, char *res, int length)
{
  unsigned long i;

  for (i = 0; i < length; i++)
    {
      if (s[i] > 255)
        __builtin_exit (0);
      res[i] = (char)s[i];
    }
}

int main()
{
  long pgsz = sysconf (_SC_PAGESIZE);
  void *p = mmap (NULL, pgsz * 2, PROT_READ|PROT_WRITE,
     MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
  if (p == MAP_FAILED)
    return 0;
  mprotect (p + pgsz, pgsz, PROT_NONE);

  uint32_t *s = p + pgsz - 64;
  s[0] = 1;
  for (int i = 1; i < 16; i++)
    s[i] = 0;
  char *res = p + pgsz - 64 + 5;
  f(s, res, 0xfffffff0);

  return 0;
}


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118443
[Bug 118443] [Meta bug] Bugs triggered by and blocking more smtgcc testing

Reply via email to