Skip to content

Commit a630fa7

Browse files
committed
SILOptimzer: correct a case of UB
The current `UpdatingInstructionIteratorRegistry` referenced `this` in the member initializer list. As per class.cdtor 11.9.5p1, this is UB as for any class with a non-trivial constructor, referencing the base class of the object before the constructor begins execution is not permitted. We attempted to capture `this` in the lambda that was used to initialise the member. This was being exploited by the MSVC compiler resulting in incorrect execution of the instruction deleter.
1 parent bf84d8d commit a630fa7

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

include/swift/SILOptimizer/Utils/UpdatingInstructionIterator.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,24 +177,24 @@ class UpdatingInstructionIteratorRegistry {
177177

178178

179179
public:
180-
UpdatingInstructionIteratorRegistry() :
181-
callbacks(InstModCallbacks()
180+
UpdatingInstructionIteratorRegistry() {
181+
callbacks = std::move(InstModCallbacks()
182182
.onDelete([this](SILInstruction *toDelete) {
183183
notifyDelete(toDelete);
184184
toDelete->eraseFromParent();
185185
})
186186
.onCreateNewInst(
187187
[this](SILInstruction *newlyCreatedInst) {
188188
notifyNew(newlyCreatedInst);
189-
}))
190-
{}
189+
}));
190+
}
191191

192192
UpdatingInstructionIteratorRegistry(InstModCallbacks &&chainedCallbacks) :
193193
// Copy the two std::functions that we need. The rest of the callbacks are
194194
// copied implicitly by assignment.
195195
chainedDelete(std::move(chainedCallbacks.deleteInstFunc)),
196-
chainedNew(std::move(chainedCallbacks.createdNewInstFunc)),
197-
callbacks(std::move(chainedCallbacks
196+
chainedNew(std::move(chainedCallbacks.createdNewInstFunc)) {
197+
callbacks = std::move(chainedCallbacks
198198
.onDelete([this](SILInstruction *toDelete) {
199199
notifyDelete(toDelete);
200200
if (chainedDelete) {
@@ -209,8 +209,8 @@ class UpdatingInstructionIteratorRegistry {
209209
if (chainedNew) {
210210
chainedNew(newlyCreatedInst);
211211
}
212-
})))
213-
{}
212+
}));
213+
}
214214

215215
// The callbacks capture 'this'. So copying is invalid.
216216
UpdatingInstructionIteratorRegistry(

0 commit comments

Comments
 (0)