Commit 2040b55
authored
[IR] Fix User use-after-destroy by zapping in ~User (llvm#170575)
First, this moves the removal of operands from use lists from
`User::operator delete` to `User::~User`. This is straightforward, and
nothing blocks that.
Second, this makes LLVM more compatible with bug finding tools like
MSan, GCC `-flifetime-dse`, and forthcoming enhancements to Clang itself
through `dead_on_return` annotations.
However, the complication is that `User::operator delete` needs to
recover the start of the allocation, and it needs to recover that
information somehow without examining the fields of the `User` object.
The natural way to handle this is for the destructor to return an
adjusted `this` pointer, and that's in fact how deleting destructors are
often implemented, but it requires making assumptions about the C++ ABI.
Another solution to this problem in C++20 would be to use [destroying
delete](https://en.cppreference.com/w/cpp/memory/new/destroying_delete_t),
which should be on our roadmap, since it would allow us to eliminate
`deleteValue`, and move that polymorphic switch into the destroying
delete operator, instead of having to use this funky method.
Since we don't have C++20 yet, it seems practical to store the
information into the operand memory, to the left of `this`, and to
reload the start of the allocation from `((void**)this)[-1]` after the
destructor runs. The downside is that zero-operand Users such as `ret
void`, `unreachable`, `fence`, and `ConstantInt` must allocate one more
pointer worth of memory to the left of the main allocation, just to
thread this information through to `User::operator delete`.
This change avoids increasing the effective size of all `ConstantData`
instances by specializing `ConstantData` new and delete, and adding a
type check to `~User`. When we have C++20, we should definitely replace
all of this with the destroying delete solution, which is much clearer,
but for now, this is a low-cost fix to long-standing UB and it unblocks
other work, so it deserves to land.
Fixes llvm#249521 parent c907d7d commit 2040b55
3 files changed
Lines changed: 47 additions & 29 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | | - | |
| 68 | + | |
69 | 69 | | |
70 | 70 | | |
71 | | - | |
| 71 | + | |
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
144 | | - | |
| 144 | + | |
| 145 | + | |
145 | 146 | | |
146 | 147 | | |
147 | 148 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
144 | 145 | | |
145 | 146 | | |
146 | 147 | | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
152 | 158 | | |
153 | 159 | | |
154 | 160 | | |
155 | | - | |
156 | | - | |
| 161 | + | |
| 162 | + | |
157 | 163 | | |
158 | 164 | | |
159 | | - | |
| 165 | + | |
160 | 166 | | |
161 | 167 | | |
162 | 168 | | |
| |||
189 | 195 | | |
190 | 196 | | |
191 | 197 | | |
192 | | - | |
193 | | - | |
194 | | - | |
| 198 | + | |
195 | 199 | | |
196 | 200 | | |
197 | | - | |
198 | | - | |
199 | | - | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
200 | 204 | | |
201 | | - | |
| 205 | + | |
202 | 206 | | |
203 | | - | |
| 207 | + | |
204 | 208 | | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
209 | 213 | | |
210 | 214 | | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
216 | 219 | | |
217 | | - | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
218 | 224 | | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
219 | 234 | | |
| 235 | + | |
| 236 | + | |
0 commit comments