| Issue |
242921
|
| Summary |
[compiler-rt][ASan][Darwin] dispatch_apply_f interceptor leaks its context (24 bytes per call)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
mclam009
|
### Summary
On Darwin, the ASan `dispatch_apply_f` interceptor (added in #149238) heap-allocates an `asan_block_context_t` for
every call and never frees it. Every `dispatch_apply_f` call therefore leaks 24 bytes, and LeakSanitizer reports
one leak per call. This includes every libc++ parallel algorithm, because libc++'s libdispatch PSTL backend calls
`dispatch_apply_f`.
### Reproducer
```c
// clang -fsanitize=address -g repro.c && ASAN_OPTIONS=detect_leaks=1 ./a.out
#include <dispatch/dispatch.h>
#include <stdio.h>
static void work(void *ctx, size_t i) { (void)ctx; (void)i; }
int main(void) {
dispatch_queue_t q = dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0);
for (int i = 0; i < 10; ++i)
dispatch_apply_f(4, q, NULL, work);
puts("done");
return 0;
}
```
Output (Homebrew clang 23.1.5, macOS 26.7.3, arm64):
```text
done
=================================================================
==…==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 240 byte(s) in 10 object(s) allocated from:
#0 0x… in dispatch_apply_f+0x38 (libclang_rt.asan_osx_dynamic.dylib:arm64+0x5436c)
#1 0x… in main repro.c:10
SUMMARY: AddressSanitizer: 240 byte(s) leaked in 10 allocation(s).
```
The same program built with `-fsanitize=leak` (no ASan interceptors) reports no leak. With libc++,
`std::for_each(std::execution::par, …)` (`-fexperimental-library`) leaks one 24-byte object per call in the same
way. The stack runs `std::__pstl::__libdispatch::__dispatch_apply` → `dispatch_apply_f`.
### Other runtimes affected
The interceptor is also present in the ASan runtime of Apple clang 21.0.0 (clang-2100.4.1.1, Xcode 27.2), and in
GCC 16.2's `libasan.8.dylib`, whose libsanitizer is merged from compiler-rt (`nm` shows `wrap_dispatch_apply_f` in
both). Apple clang's runtime has no LeakSanitizer, so the leak was measured there by the growth of live ASan heap
(`__sanitizer_get_current_allocated_bytes()`) across a loop of calls. The block form `dispatch_apply` serves as a
control, because its interceptor allocates nothing:
| ASan runtime | `dispatch_apply` (control), 20,000 calls | `dispatch_apply_f`, 20,000 calls |
| --- | --- | --- |
| Apple clang 21.0.0 | +1,024 B (0.1 B/call) | +491,680 B (24.6 B/call) |
| Homebrew clang 23.1.5 | no growth (net decrease) | +471,680 B (23.6 B/call) |
| GCC 16.2 libasan | — (no blocks in GCC) | +499,520 B (25.0 B/call) |
The steady state of about 24 bytes per call matches `sizeof(asan_block_context_t)`.
<details><summary>Counting reproducer (no LSan needed)</summary>
```c
// cc -fsanitize=address -g repro_count2.c && ./a.out
#include <dispatch/dispatch.h>
#include <stdio.h>
size_t __sanitizer_get_current_allocated_bytes(void);
static void work(void *ctx, size_t i) { (void)ctx; (void)i; }
int main(void) {
dispatch_queue_t q = dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0);
dispatch_apply_f(4, q, NULL, work); // warm-up
size_t before = __sanitizer_get_current_allocated_bytes();
for (int i = 0; i < 20000; ++i) dispatch_apply_f(4, q, NULL, work);
size_t after = __sanitizer_get_current_allocated_bytes();
printf("%.1f bytes per call\n", (double)(after - before) / 20000);
return 0;
}
```
</details>
### Cause
`compiler-rt/lib/asan/asan_mac.cpp` (current `main`):
```c
INTERCEPTOR(void, dispatch_apply_f, size_t iterations, dispatch_queue_t queue,
void *ctxt, dispatch_apply_function_t work) {
GET_STACK_TRACE_THREAD;
asan_block_context_t *asan_ctxt =
(asan_block_context_t *)asan_malloc(sizeof(asan_block_context_t), &stack);
asan_ctxt->block = ctxt;
asan_ctxt->dispatch_apply_func = work;
asan_ctxt->parent_tid = GetCurrentTidOrInvalid();
REAL(dispatch_apply_f)(iterations, queue, (void *)asan_ctxt,
asan_dispatch_apply_f_work);
}
```
The asynchronous interceptors free their context in `asan_dispatch_call_block_and_release`. This trampoline,
`asan_dispatch_apply_f_work`, runs once per iteration, so it cannot free the context. Nothing frees it after
`REAL(dispatch_apply_f)` returns.
### Suggested fix
`dispatch_apply_f` is synchronous: it returns only after every iteration has completed. The context can therefore
be freed right after the real call, with `asan_free(asan_ctxt, &stack);`. It could also live on the interceptor's
stack. The `dispatch_apply` (block) interceptor does not allocate, so it is not affected.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs