# \`checkLocation\` vs \`checkBind\` when \`isLoad=false\`

**URL:** <https://discourse.llvm.org/t/checklocation-vs-checkbind-when-isload-false/72728>\
**Category:** Static Analyzer\
**Created:** [August 11, 2023, 3:52pm UTC](https://discourse.llvm.org/t/checklocation-vs-checkbind-when-isload-false/72728 "2023-08-11T15:52:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![steakhal](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/steakhal/32/28401_2.png) [@steakhal](https://discourse.llvm.org/u/steakhal)\
**Post date:** [August 11, 2023, 3:52pm UTC](https://discourse.llvm.org/t/checklocation-vs-checkbind-when-isload-false/72728/1 "2023-08-11T15:52:00Z")

</div>

Hi all, while doing some refactorings, I noticed that `checkLocation` is not always called on storing a value to a location.  
I was expecting that `checkLocation` can be replaced by `checkBind` assuming that the `isLoad=false` of course. However, it turns out there are cases when `checkBind` gets called but the `checkLocation` doesn’t. I could probably get a concrete example, but I already moved on, but l still post this question.

After all this, I wonder, why do we even have these two if they essentially achieve the same thing (for stores), or at least they should do the same?

Shouldn’t we “merge” the two and create a separate callback, say `checkLoad` for the cases when we would have called `checkLocation` with `isLoad=true`?

---

<div class="post-metadata">

**Author:** ![steakhal](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/steakhal/32/28401_2.png) [@steakhal](https://discourse.llvm.org/u/steakhal)\
**Post date:** [August 29, 2023, 12:18pm UTC](https://discourse.llvm.org/t/checklocation-vs-checkbind-when-isload-false/72728/2 "2023-08-29T12:18:20Z")

</div>

@Xazax-hun @NoQ @DonatNagyE

---

<div class="post-metadata">

**Author:** ![DonatNagyE](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/donatnagye/32/18608_2.png) [@DonatNagyE](https://discourse.llvm.org/u/DonatNagyE)\
**Post date:** [August 29, 2023, 5:05pm UTC](https://discourse.llvm.org/t/checklocation-vs-checkbind-when-isload-false/72728/3 "2023-08-29T17:05:35Z")

</div>

This sounds to be a reasonable idea. I don’t know too much about the relationship and small differences between these two callbacks, so I cannot contribute much at this early stage.

---

<div class="post-metadata">

**Author:** ![Xazax-hun](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/xazax-hun/32/2571_2.png) [@Xazax-hun](https://discourse.llvm.org/u/Xazax-hun)\
**Post date:** [August 30, 2023, 10:59pm UTC](https://discourse.llvm.org/t/checklocation-vs-checkbind-when-isload-false/72728/4 "2023-08-30T22:59:23Z")

</div>

Renaming `checkLocation` to `checkLoad` and remove the `isLoad=false` case sounds good to me. It will make the API simpler. The difference between `checkBind` and `checkLocation` with `isLoad=false` was never documented, and I am not aware of any deliberate decisions that would prevent us from making this simplification.

---

<div class="post-metadata">

**Author:** ![steakhal](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/steakhal/32/28401_2.png) [@steakhal](https://discourse.llvm.org/u/steakhal)\
**Post date:** [September 1, 2023, 3:22pm UTC](https://discourse.llvm.org/t/checklocation-vs-checkbind-when-isload-false/72728/5 "2023-09-01T15:22:46Z")

</div>

It turns out it’s quite easy to make the transition for almost all checkers. There are two exceptions:

- **MallocChecker** : When replacing `checkLocation(/*isLoad=*/false)` callbacks with `checkBind`, we need to adjust the `S` if it’s an assignment operator to `BinOp->getLHS()`. After this, these tests also pass.
- **DereferenceChecker** : Well, this has problems. I couldn’t yet figure out how to split `checkLocation` here.

When I was reading through the code, I found this inside `evalStore()`:

```auto
/// @param AssignE The assignment expression if the store happens in an assignment.
/// @param LocationE The location expression that is stored to.
void ExprEngine::evalStore(...) {
  // Proceed with the store. We use AssignE as the anchor for the PostStore
  // ProgramPoint if it is non-NULL, and LocationE otherwise.
  const Expr *StoreE = AssignE ? AssignE : LocationE;

  // Evaluate the location (checks for bad dereferences).
  ExplodedNodeSet Tmp;
  evalLocation(Tmp, AssignE, LocationE, Pred, state, location, false);

  if (Tmp.empty())
    return;

  if (location.isUndef())
    return;

  for (const auto I : Tmp)
    evalBind(Dst, StoreE, I, location, Val, false);
}

```

This is probably (the/one) snippet that causes me headaches, and passes a different Stmt than I’m expecting in `evalBind`. Aside from that, it’s also weird to me that `evalStore` actually _reads_ from a location. I think, if we get this separation done, this _load_ will also disappear from here.

---

<div class="post-metadata">

**Author:** ![Xazax-hun](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/xazax-hun/32/2571_2.png) [@Xazax-hun](https://discourse.llvm.org/u/Xazax-hun)\
**Post date:** [September 1, 2023, 3:45pm UTC](https://discourse.llvm.org/t/checklocation-vs-checkbind-when-isload-false/72728/6 "2023-09-01T15:45:02Z")

</div>

> [@steakhal](#):
>
> Aside from that, it’s also weird to me that `evalStore` actually _reads_ from a location. I think, if we get this separation done, this _load_ will also disappear from here.

I might be missing something, but where is the read in `evalStore`? Since we invoke `evalLocation` with `isLoad=false`, I’d expect that to be a store.

There is also another difference here. It looks like `evalLocation` is called unconditionally, but `evalBind` is only called when we have a proper location. This almost sounds like as if `evalLocation` would have the purpose of emitting diagnostics (e.g., checking if we are loading an undef value), and `evalBind` would be for modeling extra logic on a successful store.

I am not 100% convinced whether this distinction is useful, but if we want `evalBind` to replace `checkLocation`, we’d need to invoke `checkBind` unconditionally here.

Indeed, it looks like this one is not a trivial change to make.

---

<div class="post-metadata">

**Author:** ![steakhal](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/steakhal/32/28401_2.png) [@steakhal](https://discourse.llvm.org/u/steakhal)\
**Post date:** [September 12, 2023, 7:31am UTC](https://discourse.llvm.org/t/checklocation-vs-checkbind-when-isload-false/72728/7 "2023-09-12T07:31:31Z")

</div>

In some rare cases, some of the `ArrayBoundV2` hits are replaced by `cplusplus.NewDelete`, e.g.:

```auto
void top() {
  int *p = new int[0];
  int n = *p; // warn: either ArrayBoundV2 or NewDelete checkers
  (void)n;
}

```

This is likely because of how checker callbacks are ordered, and the `MallocChecker` wins over `ArrayBoundV2`. If I enable/disable the V2 checker, the other report shows up, [see](https://godbolt.org/z/8xb3jEbM5).  
The new report offers more specific messages, thus I favor this change.

Now, the numbers look like this:  
Out of like 180+ projects, we have differences in only 18 projects, constituting to around 40 report diffs.  
Many (if not all) of the report diffs are of the mentioned case, where we have `new T[0]`, and dereference the resulting pointer; and we have a better message.

I also experimented with making the change completely NFC, but it’s really tedious doing so I dropped the idea.

So, if this is still a favored refactoring, I could prepare a PR.  
Let me know.
