[compiler-rt] [LSan] Use exit caller SP for exit-triggerring leak checks (PR #195238)

Kunqiu Chen via llvm-commits llvm-commits at lists.llvm.org
Mon May 4 08:24:01 PDT 2026


Camsyn wrote:

> I don't think it's appropriate.
> 
> I consider to do exactly an opposite: abort at_exit leak check if exit called. Something "do explicit __lsan_do_leak_check and exit" on your own risk, and dial with consequences, but no automatic checks.
> 
> This is potentially false-leak if exit called, we see a lot of them actually.
> 
> ```
> int foo(bool condition) {
>    auto p = make_unique<T>();
>    if (condition) {
>      exit(0);
>    }
> }
> ```
> 
> exit() is noreturn, compiler don't have to keep pointer to p anymore.
> 
> Arguably it's a true leak, but no one should be forced to fix it.
> 
> So essentially exit() and leak check should be strongly discourage, and not worked around.

If this idea hasn't yet been made progress on by anyone, I'm interested in working on it.

This requires LSan to distinguish whether the `at_exit` leak check is performed by an explicit `exit` or an implicit `exit` after `main` returns.

AFAICS, there is no stable (e.g., specification of some standards), cross-platform, link-only (for LSan) approach to implement such a distinction. 
Maybe we can 
- intercept `__libc_start_main` and wrap the `main`, or 
- still intercept `exit` as the implicit libc-called `exit` cannot be intercepted (glibc/musl links it internally).

But if we can add an instrumentation pass for LSan, maybe adding a wrapper for `main` is a more elegant approach.

Could you give some suggestions, @vitalybuka ?

BTW, for atexit leak check after main returns, should we disable the stack scan for the main thread?

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


More information about the llvm-commits mailing list