Skip to content

[BoundsSafety] Miscompilation with "-O3" and "-fbounds-safety" #11072

@helloqirun

Description

@helloqirun

Tested on the latest version (as of today) on Linux:

$ clang-fbound -v
clang version 22.0.0git (https://github.com/swiftlang/llvm-project.git 54d626d2454f3821661d541bea933cb97122e255)
Target: x86_64-unknown-linux-gnu

Normal "-O3" produces a correct binary:

$ clang-fbound -O3 abc.c ; ./a.out
0

With "bounds-safety" and "-O3", it produces an incorrect binary:

$ clang-fbound -O3 -fbounds-safety abc.c ; ./a.out
1

The test C program:

$ cat abc.c
#include <ptrcheck.h>

int printf(const char *, ...);
static int a, c;
static char b[] = {228, 228};
static long d(void) {
  unsigned e = 0;
  for (; e <= 7; e++)
    ;
  for (;;) {
    a = 0;
    for (; a <= 5; a++) {
      if (b[a]) {
        c = e <= 0;
        return 8;
      }
      e = 1;
    }
  }
}
int main(void) {
  d();
  printf("%d\n", c);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions