llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

wrong code at -O1 on x86_64-linux-gnu

Open zhendongsu opened this issue 3 months ago • 3 comments

Compiler Explorer: https://godbolt.org/z/fEMPYYP5d

Note:

  • fails: trunk
  • works: 22.1.0 and earlier
[622] % clangtk -v
clang version 24.0.0git (https://github.com/llvm/llvm-project.git d0b4c1bde99da365581a47ea12a2bed009692550)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /local/home/suz/suz-local/software/local/clang-trunk/bin
Build config: +assertions
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/13
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/14
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/14
Candidate multilib: .;@m64
Selected multilib: .;@m64
[623] % 
[623] % clangtk -O0 small.c; ./a.out
[624] % 
[624] % clangtk -O1 small.c
[625] % ./a.out
Floating point exception
short g, ab;
int h, i, j, k, aa, **l, **m, bv, bw, bx, by, n = -239;
struct a {
  long b;
  int c[];
};
void d(short);
void e(int);
void f(int bz) {
  if (bz >= 0)
    by = 2 % bx;
}
void bb(short bc, int bd) {
  int bh = 3, *bj = 0, *bk = 0;
  struct a bi[] = {};
  l = &bk;
  m = &bj;
  i = 1879048193 - 7 * bd;
  bj = &bh;
  *bj = 1934090263 + i;
  *l = bj;
  do {
    if (-1339633854 > *bk)
      goto bt;
    *bk = -6 * bd + *bk - (1 >> i) + 403083200;
  } while (bc);
  bk = &bi[0].c[0];
bt:
  bi[0].b = h < 8;
  j = *bk;
  g = j;
  k = g % 181;
  aa = 106;
  ab = 119 * k;
  ab = ab % 181;
  aa = aa + ab;
  d(aa - 87 - 16384);
}
void d(short bd) {
  bv = (long)((unsigned long)bd << 48) >> 48;
  bw = -16383 ^ bv;
  e(bw);
}
void e(int bd) { f(bd - 9); }
int main() { bb(1, n + 268435695); }

zhendongsu avatar Aug 17 '26 11:08 zhendongsu

https://godbolt.org/z/fEMPYYP5d - this dosnt fails or crashes

im-lunex avatar Aug 17 '26 15:08 im-lunex

while looking into it i discovered that reproducer contains source-level UB (bi[0].b accesses element 0 of a zero-length array)- both clang UBSan and gcc UBSan diagnose it and removing that line eliminates the crash.

that given testcase is UB, should this issue be considered invalid or this worth addressing ?

im-lunex avatar Aug 17 '26 19:08 im-lunex

@im-lunex: good catch! Indeed, this reduced test has UB, although the original test doesn't. I'm re-reducing it.

zhendongsu avatar Aug 17 '26 20:08 zhendongsu