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

            Bug ID: 128713
           Summary: arm: wrong code at -O1 with __builtin_mul_overflow
                    into a _BitInt(65): a stack store past the end of the
                    product temporary
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: shimizu2486 at gmail dot com
  Target Milestone: ---
            Target: arm-linux-gnueabihf

Live reproducer: https://godbolt.org/z/zMzax3c9P

$ cat t1.c
#include <stdio.h>
int a, b, n;
static volatile int A = 1;
void f(void) {
  int t = 0;
  _BitInt(65) r;
  n++;
  for (int i = 0; i < n; i++)
    t = __builtin_mul_overflow(a, b, &r) ? 1 : r;
  (void)(t || a);
}
int main(void) {
  int x = A;
  f();
  printf("%d\n", x);
}

f does not change x, so the program prints A, which is 1.

$ arm-linux-gnueabihf-gcc -O0 -static t1.c -o t1 && qemu-arm ./t1
1                    (correct)
$ arm-linux-gnueabihf-gcc -O1 -static t1.c -o t1 && qemu-arm ./t1
0                    (wrong)
$ arm-linux-gnueabihf-gcc -O1 -fstack-protector-strong -static t1.c -o t1 &&
qemu-arm ./t1
*** stack smashing detected ***: terminated
qemu: uncaught target signal 6 (Aborted) - core dumped

  arm trunk (842e09f03b64)   wrong
  arm 16.2.0                 wrong

-O1 is wrong; -O0, -O2, -O3, -Os and -Og are correct.
-mthumb and -mfpu=neon are wrong too.
-fno-tree-dce or -fno-tree-fre gives the correct value.

Reply via email to