Commit 63334db
Support virtual inheritance paths to std::exception in exception tracer library
Summary:
This diff implements stack trace collection for derived exceptions in virtual inheritance hierarchies.
## Description of current behavior
Stack traces for exceptions `A` at the time they are thrown are stored by `SmartExceptionTracer` in a global `map<void*, ExceptionInfo>`. Previously, when performing lookups to obtain stack traces, the address of the object `B` to obtain a stack trace for needed to match the pointer previously stored. This works for simple cases where `A` and `B` are [pointer interconvertible](https://en.cppreference.com/w/cpp/language/static_cast.html#pointer-interconvertible), but failed lookups for cases where there is a non-zero offset between addresses of B and the thrown object A due to their differing layout.
## Fix
The fix uses `dynamic_cast<void*>` to obtain the address of the [most derived object](https://eel.is/c++draft/intro.object#6) associated with the object passed into `getTrace` for the case where a reference to a derived type (like `std::exception`) is passed in. This ensures that the address used in the lookup in the global map of thrown exceptions corresponds to the address placed there by the hook (ie the address passed by the runtime to `__cxa_throw()`.
## Side effects of this change
One side effect of how the fix is implemented is that we avoid a `dynamic_cast` to `std::exception` in the lookup paths where `std::exception_ptr` or `folly::ExceptionWrapper` are passed. This enables support for collecting stack traces for thrown objects that are not derived from `std::exception` (see the changed `NonStdException` test case).
In addition to that, this diffs skips unnecessary RTTI inspection of thrown objects to find out whether they are compatible with `std::exception` as the pointer to the originally thrown exception is already available when `std::exception_ptr` or `folly::exception_wrapper` is available, which increases performance slightly (see benchmarks below).
Reviewed By: shr, yfeldblum
Differential Revision: D84131474
fbshipit-source-id: 4e5434ffa01ad5739fed4226ec5b8891ee3c2daa1 parent 5435449 commit 63334db
2 files changed
Lines changed: 65 additions & 8 deletions
File tree
- third-party/folly/src/folly/debugging/exception_tracer
- test
Lines changed: 15 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
| 44 | + | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
| 53 | + | |
54 | 54 | | |
55 | | - | |
| 55 | + | |
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
67 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
68 | 69 | | |
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
72 | 82 | | |
73 | 83 | | |
74 | 84 | | |
| |||
Lines changed: 50 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
67 | 68 | | |
68 | 69 | | |
69 | 70 | | |
70 | | - | |
| 71 | + | |
71 | 72 | | |
72 | 73 | | |
73 | 74 | | |
74 | 75 | | |
75 | 76 | | |
76 | 77 | | |
77 | 78 | | |
78 | | - | |
79 | | - | |
| 79 | + | |
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
| |||
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
146 | 193 | | |
0 commit comments