| Issue |
242802
|
| Summary |
[clang-tidy] `cppcoreguidelines-avoid-capturing-lambda-coroutines`: `AllowExplicitObjectParameters` also silences by-value `this auto` lambdas whose closure is dead when they are called
|
| Labels |
clang-tidy
|
| Assignees |
|
| Reporter |
jumormt
|
## Summary
With `AllowExplicitObjectParameters: true`, the check does not report any capturing lambda coroutine that has an explicit object parameter. The option's documentation says this is safe "because the captures are moved into the coroutine frame, decoupling their lifetime from the lambda object". For a by-value parameter (`this auto self`), the copy into the frame is made when the call operator is *invoked*. So the closure object must still be alive at the call. Suppose a function receives the lambda by reference and calls it later, after the lambda object has been destroyed. Then `self` is copied from a dead object. The check is silent on such a program, and AddressSanitizer reports a stack-use-after-scope when it runs.
This is different from #221750 (open), which is about reference parameters (`this auto&`). That issue's expected behaviour keeps the exemption for `this auto`; the program below shows a case where `this auto` is not enough on its own.
## Environment
- clang-tidy 23.1.5 (release build, `LLVM version 23.1.5`)
- clang++ 18.1.8 for the AddressSanitizer run, with libstdc++ 11 headers, `-std=c++23 -g -fsanitize=address`
- Linux x86-64, glibc 2.31
- The check's source, documentation and option test at `main` (5dbd2e19f11b71c608e81bf276b4795f692c9f98, 2026-11-15) read the same as in 23.1.5 for this purpose. The matcher still exempts every lambda whose call operator `isExplicitObjectMemberFunction()`, and the option's documentation sentence is unchanged.
## Reproduction
`repro.cpp` (self-contained, standard library only):
```cpp
// A by-value explicit object parameter ("this auto self") copies the
// closure when the call operator is invoked. A receiver that keeps only a
// reference to the lambda and invokes it later copies from a dead object.
#include <coroutine>
#include <cstdio>
#include <exception>
#include <utility>
struct task {
struct promise_type {
task get_return_object() {
return task{std::coroutine_handle<promise_type>::from_promise(*this)};
}
std::suspend_always initial_suspend() noexcept { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void return_void() {}
void unhandled_exception() { std::terminate(); }
};
explicit task(std::coroutine_handle<promise_type> h) : h(h) {}
task(task&& o) noexcept : h(std::exchange(o.h, {})) {}
~task() { if (h) h.destroy(); }
std::coroutine_handle<promise_type> h;
};
// A lazy receiver: it takes the callable by forwarding reference and calls
// it only when the returned task is first resumed.
template <class F>
task run_later(F&& f) {
task inner = std::forward<F>(f)(); // "this auto self" is copied here
inner.h.resume();
co_return;
}
int main() {
task t = run_later([value = 42](this auto self) -> task {
std::printf("%d\n", value);
co_return;
}); // the lambda temporary is destroyed here
t.h.resume();
}
```
### clang-tidy, option on: nothing
```console
$ clang-tidy --checks="-*,cppcoreguidelines-avoid-capturing-lambda-coroutines" \
--config="{CheckOptions: {cppcoreguidelines-avoid-capturing-lambda-coroutines.AllowExplicitObjectParameters: true}}" \
repro.cpp -- -std=c++23
$ echo $?
0
```
### clang-tidy, option off (default): the warning
```console
$ clang-tidy --checks="-*,cppcoreguidelines-avoid-capturing-lambda-coroutines" repro.cpp -- -std=c++23
1 warning generated.
repro.cpp:35:22: warning: coroutine lambda may cause use-after-free, avoid captures or ensure lambda closure object has guaranteed lifetime [cppcoreguidelines-avoid-capturing-lambda-coroutines]
35 | task t = run_later([value = 42](this auto self) -> task {
| ^
```
### Running it
```console
$ clang++ -std=c++23 -g -fsanitize=address repro.cpp -o repro && ./repro
==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7f5b83f00040 ...
READ of size 4 at 0x7f5b83f00040 thread T0
#0 in __asan_memcpy
#1 in task run_later<main::$_0>(main::$_0&&) (.resume) repro.cpp:29:16
#2 in std::coroutine_handle<task::promise_type>::resume() const
#3 in main repro.cpp:39:7
Address 0x7f5b83f00040 is located in stack of thread T0 at offset 64 in frame
#0 in main repro.cpp:34
This frame has 2 object(s):
[32, 40) 't' (line 35)
[64, 68) 'ref.tmp' (line 35) <== Memory access at offset 64 is inside this variable
SUMMARY: AddressSanitizer: stack-use-after-scope repro.cpp:29:16 in task run_later<main::$_0>(main::$_0&&) (.resume)
```
(Addresses and the standard-library frame shortened; the full report is in a collapsed section at the end.) The program aborts before printing anything.
## Why it happens
`run_later` is a coroutine that takes its argument as `F&&`, so its frame stores a reference to the lambda temporary. The temporary dies at the end of the full _expression_ in `main`. The lambda is called only when `t` is resumed. At that point the by-value explicit object parameter `self` is initialized by copying from the referenced, already destroyed closure (`repro.cpp:29`). After that copy the captures do live in the coroutine frame, as the documentation says. But the copy itself reads a dead object.
If `run_later` takes its argument by value instead (`task run_later(F f)`, calling `f()`; nothing else changes), the copy is made from the receiver's own live parameter. AddressSanitizer is then silent and the program prints `42`. With the option on, the check is silent on both programs. So the exemption is correct for some receivers and wrong for others, and the syntax of the lambda alone cannot tell them apart.
## A suggestion
Two possibilities, either of which would make the option accurate:
- **Documentation:** qualify the sentence in the option's description. For example: "... because the captures are copied or moved into the coroutine frame when the lambda is called. The lambda object must still be alive at that call; a function that stores a reference to the lambda and calls it later (for example a lazy coroutine taking `F&&`) is not made safe by an explicit object parameter."
- **Check:** keep warning when a by-value explicit-object lambda is passed directly as an argument whose parameter is a reference (for example a forwarding reference of a function that returns a coroutine type). This is narrower and would still be heuristic; I have not prototyped it.
## Not tested
- clang-tidy built from `main`. I read the check's source, documentation and option test at the commit above; I did not run them.
- An AddressSanitizer run with Clang 23. Its runtime does not start on this machine's glibc, so the run used Clang 18.1.8.
- Receivers in real coroutine libraries. The receiver here is written for the example.
- Other compilers and standard libraries, and optimisation levels other than `-O0`.
<details>
<summary>Full AddressSanitizer report of <code>repro.cpp</code></summary>
```
$ clang++ -std=c++23 -g -fsanitize=address repro.cpp -o repro && ./repro
=================================================================
==1637298==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7f5b83f00040 at pc 0x56487a20e292 bp 0x7ffe0961bbf0 sp 0x7ffe0961b3b0
READ of size 4 at 0x7f5b83f00040 thread T0
#0 0x56487a20e291 in __asan_memcpy llvm-project/compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp:63:3
#1 0x56487a251cd9 in task run_later<main::$_0>(main::$_0&&) (.resume) repro.cpp:29:16
#2 0x56487a252cdb in std::__n4861::coroutine_handle<task::promise_type>::resume() const c++/11/coroutine:231:29
#3 0x56487a24fb62 in main repro.cpp:39:7
#4 0x7f5b86424082 in __libc_start_main /build/glibc-B3wQXB/glibc-2.31/csu/../csu/libc-start.c:308:16
#5 0x56487a17738d in _start (repro+0x2c38d)
Address 0x7f5b83f00040 is located in stack of thread T0 at offset 64 in frame
#0 0x56487a24fa6f in main repro.cpp:34
This frame has 2 object(s):
[32, 40) 't' (line 35)
[64, 68) 'ref.tmp' (line 35) <== Memory access at offset 64 is inside this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
(longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-use-after-scope repro.cpp:29:16 in task run_later<main::$_0>(main::$_0&&) (.resume)
Shadow bytes around the buggy address:
0x7f5b83effd80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7f5b83effe00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7f5b83effe80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7f5b83efff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7f5b83efff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x7f5b83f00000: f1 f1 f1 f1 00 f2 f2 f2[f8]f3 f3 f3 00 00 00 00
0x7f5b83f00080: f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5
0x7f5b83f00100: f1 f1 f1 f1 f8 f2 f2 f2 00 f3 f3 f3 00 00 00 00
0x7f5b83f00180: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7f5b83f00200: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7f5b83f00280: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
==1637298==ABORTING
exit status: 1
```
</details>
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs