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

            Bug ID: 128914
           Summary: wrong code with -fopenmp-simd: '#pragma omp simd
                    reduction' over a whole VLA stores the result to the
                    VLA pointer temporary (stack corruption)
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 0599jiangyc at gmail dot com
  Target Milestone: ---

The following code:

```c
/* Godbolt: x86-64 gcc (trunk), flags: -O2 -fopenmp-simd
   omp simd reduction over a whole VLA: the merge loop stores the partial
results to &<pointer temp>
   (the address of the VLA's pointer variable) instead of into the VLA -> stack
corruption, VLA unchanged.
   gcc -O0 -fopenmp-simd repro.c  (also -O2/-O3/-Os; GCC 11 too)
   expected v = {1^4^25, 2^7, 3^10, 4^13, 5^16} computed sequentially; returns
1 when wrong.  */
#include <stdio.h>
unsigned u[64];
__attribute__((noipa)) int f(int n, int m, unsigned *out) {
  unsigned v[m];
  for (int k = 0; k < m; k++) v[k] = 0;
#pragma omp simd reduction(^:v)
  for (int i = 0; i < n; i++)
    v[i % m] ^= u[i];
  for (int k = 0; k < m; k++) out[k] = v[k];
  return m;
}
int main(void) {
  unsigned o[5], e[5] = {0};
  for (int i = 0; i < 64; i++) u[i] = i * 3 + 1;
  for (int i = 0; i < 8; i++) e[i % 5] ^= u[i];
  f(8, 5, o);
  int bad = 0;
  for (int k = 0; k < 5; k++) bad |= o[k] != e[k];
  printf("v[0]=%x expected %x -> %s\n", o[0], e[0], bad ? "WRONG" : "ok");
  return bad;
}
```

Resulted in this output:
```
$ gcc -O2 -fopenmp-simd repro.c && ./a.out
v[0]=0 expected 11 -> WRONG
(exit 1; without -fopenmp-simd and with clang -fopenmp-simd: ok; GCC 11: stack
smashing detected)
```

Trunk: https://godbolt.org/z/vsn9YqsGr (x86-64 gcc trunk, -O2 -fopenmp-simd,
executed; wrong as of 2026-11-14; also GCC 14.2 and GCC 11.4; -O0 -fopenmp-simd
is wrong too)

Reply via email to