Commit c2d7e96
authored
[clang] Fix non-deterministic infinite recursion... (llvm#118288)
...in `ASTContext::getAutoTypeInternal`
Given
```cpp
template < typename >
concept C1 = true;
template < typename , auto >
concept C2 = true;
template < C1 auto V, C2< V > auto>
struct S;
```
Both `C1 auto V` and `C2<V> auto` end on the set `AutoType`, the former
being a template parameter for the latter.
Since the hashing is not deterministic (i.e., pointers are hashed),
every now and then, both will end on the same bucket. Given that
`FoldingSet` recomputes the `FoldingSetID` for each node in the target
bucket on lookup, this triggers an infinite recursion:
1. Look for `X` in `AutoTypes`
2. Let's assume it would be in bucket N, so it iterates over nodes in
that bucket. Let's assume the first is `C2<V> auto`.
3. Computes the `FoldingSetID` for this one, which requires the profile
of its template parameters, so they are visited.
4. In some frames below, we end on the same `FoldingSet`, and, by
chance, `C1 auto V` would be in bucket N too.
5. But the first node in the bucket is `C2<V> auto` for which we need to
profile `C1 auto V`
6. ... stack overflow!
No step individually does anything wrong, but in general, `FoldingSet`
seems not to be re-entrant, and this fact is hidden behind many nested
calls.
With this change, we store the `AutoType`s inside a `DenseMap` instead.
The `FoldingSetID` is computed once only and then kept as the map's key,
avoiding the need to do recursive lookups.
We also now make sure the key for the inserted `AutoType` is the same as
the key used for lookup. Before, this was not the case, and it caused
also non-deterministic parsing errors.
Fixes llvm#1102311 parent eadc0c9 commit c2d7e96
File tree
4 files changed
+53
-13
lines changed- clang
- include/clang/AST
- lib/AST
- test/Parser
4 files changed
+53
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
245 | 245 | | |
246 | 246 | | |
247 | 247 | | |
248 | | - | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
249 | 253 | | |
250 | 254 | | |
251 | 255 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6553 | 6553 | | |
6554 | 6554 | | |
6555 | 6555 | | |
6556 | | - | |
| 6556 | + | |
6557 | 6557 | | |
6558 | 6558 | | |
6559 | 6559 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
115 | 136 | | |
116 | 137 | | |
117 | 138 | | |
| |||
899 | 920 | | |
900 | 921 | | |
901 | 922 | | |
902 | | - | |
| 923 | + | |
903 | 924 | | |
904 | 925 | | |
905 | 926 | | |
| |||
6294 | 6315 | | |
6295 | 6316 | | |
6296 | 6317 | | |
6297 | | - | |
6298 | 6318 | | |
6299 | | - | |
6300 | | - | |
6301 | | - | |
6302 | | - | |
| 6319 | + | |
| 6320 | + | |
| 6321 | + | |
| 6322 | + | |
| 6323 | + | |
| 6324 | + | |
| 6325 | + | |
6303 | 6326 | | |
6304 | 6327 | | |
6305 | 6328 | | |
| |||
6314 | 6337 | | |
6315 | 6338 | | |
6316 | 6339 | | |
6317 | | - | |
6318 | | - | |
6319 | | - | |
6320 | | - | |
6321 | 6340 | | |
6322 | 6341 | | |
6323 | 6342 | | |
| |||
6331 | 6350 | | |
6332 | 6351 | | |
6333 | 6352 | | |
| 6353 | + | |
| 6354 | + | |
| 6355 | + | |
| 6356 | + | |
| 6357 | + | |
6334 | 6358 | | |
6335 | | - | |
| 6359 | + | |
6336 | 6360 | | |
6337 | 6361 | | |
6338 | 6362 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
0 commit comments