| Issue |
239365
|
| Summary |
[TSan] Wrong stack traces and growing memory after exceptions pass through a non-matching catch
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
taneadam
|
_Disclaimer: I relied heavily on claude code for this. I am a TSan user but am not familiar with internals. I ran both repro cases myself and they seem to clearly show an issue._
If an exception passes through a function whose `catch` doesn't match it, every later TSan report from that thread gets an extra stack frame for a call that has already returned. The thread's memory use also grows with each such exception, and a long enough loop crashes.
Environment:
- clang and compiler-rt 21.1.8, from the AlmaLinux 10 AppStream packages (`llvm-21.1.8-1.el10.alma.1`)
- libstdc++ and libgcc 14.3.1 (`gcc-14.3.1-4.4.el10.alma.2`)
- x86_64 Linux
## Repro 1: extra frames in a report
[godbolt](https://godbolt.org/z/M178cKzqM)
```cpp
#include <pthread.h>
#include <unistd.h>
struct A {};
struct B {};
int x;
__attribute__((noinline)) void f() { try { throw A(); } catch (B&) {} }
void* writer(void*)
{
x = 1;
return nullptr;
}
int main()
{
for (int i = 0; i < 3; i++)
try { f(); } catch (A&) {}
pthread_t t;
pthread_create(&t, nullptr, writer, nullptr);
sleep(1);
x = 2;
pthread_join(t, nullptr);
}
```
```
clang++ -fsanitize=thread -g repro.cpp -o repro && ./repro
```
The race is intentional. Here is the part of the report that's wrong (paths trimmed; the output is the same at `-O0` and `-O2`):
```
Write of size 4 at 0x000001897758 by main thread:
#0 main repro.cpp:24:7
#1 main repro.cpp:19:15
#2 main repro.cpp:19:15
#3 main repro.cpp:19:15
```
Expected: only `#0 main repro.cpp:24:7`. `main` isn't recursive, and the three `f()` calls on line 19 have already returned. If `f` catches `A&` instead, the extra frames go away.
## Repro 2: memory grows with each exception
```cpp
#include <cstdlib>
struct A {};
struct B {};
__attribute__((noinline)) void f() { try { throw A(); } catch (B&) {} }
int main(int argc, char** argv)
{
int n = std::atoi(argv[1]);
for (int i = 0; i < n; i++)
try { f(); } catch (A&) {}
}
```
```
clang++ -fsanitize=thread -O2 repro_rss.cpp -o repro_rss && for n in 0 20000 40000 60000; do /usr/bin/time -f "$n exceptions: %M KB max RSS, %e s" ./repro_rss $n; done
```
| exceptions | peak RSS | time |
|---|---|---|
| 0 | 19.9 MB | 0.00 s |
| 20,000 | 65.1 MB | 0.22 s |
| 40,000 | 105.3 MB | 0.75 s |
| 60,000 | 146.1 MB | 1.63 s |
If `f` catches `A&` instead, peak RSS stays at about 20.7 MB for 60,000 exceptions.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs