OSDN Git Service

[clang-tidy] Introduce misc No Integer To Pointer Cast check
authorRoman Lebedev <lebedev.ri@gmail.com>
Tue, 8 Dec 2020 19:54:56 +0000 (22:54 +0300)
committerRoman Lebedev <lebedev.ri@gmail.com>
Tue, 8 Dec 2020 19:55:13 +0000 (22:55 +0300)
commit39431e479ffddc81120911f031921b2d637e967f
tree7b96ca16f829925418df9c3ca53abd0ed9a59133
parent1f67247eea13f62f26de3b0eca3755ed8b3a2b8e
[clang-tidy] Introduce misc No Integer To Pointer Cast check

While casting an (integral) pointer to an integer is obvious - you just get
the integral value of the pointer, casting an integer to an (integral) pointer
is deceivingly different. While you will get a pointer with that integral value,
if you got that integral value via a pointer-to-integer cast originally,
the new pointer will lack the provenance information from the original pointer.

So while (integral) pointer to integer casts are effectively no-ops,
and are transparent to the optimizer, integer to (integral) pointer casts
are *NOT* transparent, and may conceal information from optimizer.

While that may be the intention, it is not always so. For example,
let's take a look at a routine to align the pointer up to the multiple of 16:
The obvious, naive implementation for that is:

```
  char* src(char* maybe_underbiased_ptr) {
    uintptr_t maybe_underbiased_intptr = (uintptr_t)maybe_underbiased_ptr;
    uintptr_t aligned_biased_intptr = maybe_underbiased_intptr + 15;
    uintptr_t aligned_intptr = aligned_biased_intptr & (~15);
    return (char*)aligned_intptr; // warning: avoid integer to pointer casts [misc-no-inttoptr]
  }
```

The check will rightfully diagnose that cast.

But when provenance concealment is not the goal of the code, but an accident,
this example can be rewritten as follows, without using integer to pointer cast:

```
  char*
  tgt(char* maybe_underbiased_ptr) {
      uintptr_t maybe_underbiased_intptr = (uintptr_t)maybe_underbiased_ptr;
      uintptr_t aligned_biased_intptr = maybe_underbiased_intptr + 15;
      uintptr_t aligned_intptr = aligned_biased_intptr & (~15);
      uintptr_t bias = aligned_intptr - maybe_underbiased_intptr;
      return maybe_underbiased_ptr + bias;
  }
```

See also:
* D71499
* [[ https://www.cs.utah.edu/~regehr/oopsla18.pdf | Juneyoung Lee, Chung-Kil Hur, Ralf Jung, Zhengyang Liu, John Regehr, and Nuno P. Lopes. 2018. Reconciling High-Level Optimizations and Low-Level Code in LLVM. Proc. ACM Program. Lang. 2, OOPSLA, Article 125 (November 2018), 28 pages. ]]

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D91055
clang-tools-extra/clang-tidy/performance/CMakeLists.txt
clang-tools-extra/clang-tidy/performance/NoIntToPtrCheck.cpp [new file with mode: 0644]
clang-tools-extra/clang-tidy/performance/NoIntToPtrCheck.h [new file with mode: 0644]
clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/list.rst
clang-tools-extra/docs/clang-tidy/checks/performance-no-int-to-ptr.rst [new file with mode: 0644]
clang-tools-extra/test/clang-tidy/checkers/performance-no-int-to-ptr.c [new file with mode: 0644]
clang-tools-extra/test/clang-tidy/checkers/performance-no-int-to-ptr.cpp [new file with mode: 0644]