[clang] nonblocking/nonallocating attributes: 2nd pass caller/callee analysis (PR #99656)

Chris Apple via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 13 11:36:54 PDT 2024


cjappl wrote:

Hi @dougsonos 

Seeing a possible bug when using `malloc` in a [[nonallocating]] context.

Specifically this test program (partially stolen from one of your test cases)

```cpp
#include <cstdlib>
void nb4_inline() {}
void nb4_not_inline();

void nb4() noexcept [[clang::nonallocating]]
{
  float* ptr = (float*)malloc(100 * sizeof(float)); // SHOULD WARN BUT DOES NOT
  nb4_inline(); // OK
  nb4_not_inline(); // expected-warning {{'nonblocking' function must not call non-'nonblocking' function}}
}
```

Produces warnings:
```
/Users/topher/code/rtsan_example/main.cpp:15:3: warning: 'nonallocating' function must not call non-'nonallocating' function 'nb4_not_inline' [-Wfunction-effects]
   15 |   nb4_not_inline(); // expected-warning {{'nonblocking' function must not call non-'nonblocking' function}}
      |   ^
/Users/topher/code/rtsan_example/main.cpp:9:6: note: declaration cannot be inferred 'nonallocating' because it has no definition in this translation unit
    9 | void nb4_not_inline();
      |      ^
```
But we are missing the warning for the malloc call (which should be non-nonallocating)

More simply:
```
#include <cstdlib>
float* nb4() noexcept [[clang::nonallocating]]
{
  float* ptr = (float*)malloc(100 * sizeof(float));
  return ptr;
}
```

Produces no warnings:
```
[2/2] Linking CXX executable helloWorld
Build finished
```


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


More information about the cfe-commits mailing list