[llvm] [libcxx] [clang-tools-extra] [compiler-rt] [clang] [flang] [libc] [Clang] Generate the GEP instead of adding AST nodes (PR #73730)

Bill Wendling via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 15 12:06:09 PST 2023


================
@@ -4022,8 +4169,36 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
       ArrayLV = EmitArraySubscriptExpr(ASE, /*Accessed*/ true);
     else
       ArrayLV = EmitLValue(Array);
+
     auto *Idx = EmitIdxAfterBase(/*Promote*/true);
 
+    if (SanOpts.has(SanitizerKind::ArrayBounds)) {
----------------
bwendling wrote:

I have checks in place to make sure that I can find the correct enclosing type. For instance, if I have something like:

```
struct B {
  int count;
  int arr[] __counted_by(count);
};

struct A {
  int count;
  struct B *b;
};
```

when looking at `someA->b->arr[idx]`, it only cares about looking at the `count` in `struct B`. The `count` in `struct A` won't be seen. This is because we're looking for the most-enclosing struct of `arr`, which is `struct B`. Now if I have something like:

```
struct A {
  int count;
  struct B {
    int count;
    int arr[] __counted_by(count);
  } *ptr;
};
```

This could get hairy, because it might not see the `count` in `struct B`. BUT if it's not a pointer:

```
struct A {
  int count;
  struct B {
    int count;
    int arr[] __counted_by(count);
  } z;
};
```

Then I *do* want it to reference the `count` in `struct A`. Hilarious, I know! :-) The "pointer" example should work exactly as if `struct B` was defined outside of `struct A`. I'll need to make sure to document that very carefully.

As for other `-fbounds-safety` checks, I haven't seen any that do quite what we're doing here. When I looked through the attributes, few of them seemed to reference other fields within structures, or even external variables. (I know some exist, but they're not quite the same as what we're doing here.) Then again, I may quite well have not noticed one of the bounds safety code paths.

https://github.com/llvm/llvm-project/pull/73730


More information about the llvm-commits mailing list