Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit aa887db

Browse files
committed
Merge branch 'master' of github.com:apple/swift into tensorflow-stage
* 'master' of github.com:apple/swift: Fix use-after-free in SILCombine (swiftlang#34145)
2 parents cf08521 + e2a9bf2 commit aa887db

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

include/swift/SILOptimizer/Utils/InstOptUtils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ void getConsumedPartialApplyArgs(PartialApplyInst *pai,
357357
SmallVectorImpl<Operand *> &argOperands,
358358
bool includeTrivialAddrArgs);
359359

360+
/// Emit destroy operation for \p operand, and call appropriate functions from
361+
/// \p callbacks for newly created instructions and deleted instructions.
362+
void emitDestroyOperation(SILBuilder &builder, SILLocation loc,
363+
SILValue operand, InstModCallbacks callbacks);
364+
360365
/// Collect all (transitive) users of \p inst which just copy or destroy \p
361366
/// inst.
362367
///
@@ -366,6 +371,7 @@ void getConsumedPartialApplyArgs(PartialApplyInst *pai,
366371
/// destroys, i.e. if \p inst can be considered as "dead".
367372
bool collectDestroys(SingleValueInstruction *inst,
368373
SmallVectorImpl<SILInstruction *> &destroys);
374+
369375
/// If Closure is a partial_apply or thin_to_thick_function with only local
370376
/// ref count users and a set of post-dominating releases:
371377
///

lib/SILOptimizer/Utils/InstOptUtils.cpp

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,28 +1187,16 @@ static bool shouldDestroyPartialApplyCapturedArg(SILValue arg,
11871187
return true;
11881188
}
11891189

1190-
// *HEY YOU, YES YOU, PLEASE READ*. Even though a textual partial apply is
1191-
// printed with the convention of the closed over function upon it, all
1192-
// non-inout arguments to a partial_apply are passed at +1. This includes
1193-
// arguments that will eventually be passed as guaranteed or in_guaranteed to
1194-
// the closed over function. This is because the partial apply is building up a
1195-
// boxed aggregate to send off to the closed over function. Of course when you
1196-
// call the function, the proper conventions will be used.
1197-
void swift::releasePartialApplyCapturedArg(SILBuilder &builder, SILLocation loc,
1198-
SILValue arg,
1199-
SILParameterInfo paramInfo,
1200-
InstModCallbacks callbacks) {
1201-
if (!shouldDestroyPartialApplyCapturedArg(arg, paramInfo,
1202-
builder.getFunction()))
1203-
return;
1204-
1205-
// Otherwise, we need to destroy the argument. If we have an address, we
1206-
// insert a destroy_addr and return. Any live range issues must have been
1207-
// dealt with by our caller.
1208-
if (arg->getType().isAddress()) {
1209-
// Then emit the destroy_addr for this arg
1210-
SILInstruction *newInst = builder.emitDestroyAddrAndFold(loc, arg);
1211-
callbacks.createdNewInst(newInst);
1190+
void swift::emitDestroyOperation(SILBuilder &builder, SILLocation loc,
1191+
SILValue operand, InstModCallbacks callbacks) {
1192+
// If we have an address, we insert a destroy_addr and return. Any live range
1193+
// issues must have been dealt with by our caller.
1194+
if (operand->getType().isAddress()) {
1195+
// Then emit the destroy_addr for this operand. This function does not
1196+
// delete any instructions
1197+
SILInstruction *newInst = builder.emitDestroyAddrAndFold(loc, operand);
1198+
if (newInst != nullptr)
1199+
callbacks.createdNewInst(newInst);
12121200
return;
12131201
}
12141202

@@ -1217,12 +1205,12 @@ void swift::releasePartialApplyCapturedArg(SILBuilder &builder, SILLocation loc,
12171205

12181206
// If we have qualified ownership, we should just emit a destroy value.
12191207
if (builder.getFunction().hasOwnership()) {
1220-
callbacks.createdNewInst(builder.createDestroyValue(loc, arg));
1208+
callbacks.createdNewInst(builder.createDestroyValue(loc, operand));
12211209
return;
12221210
}
12231211

1224-
if (arg->getType().hasReferenceSemantics()) {
1225-
auto u = builder.emitStrongRelease(loc, arg);
1212+
if (operand->getType().hasReferenceSemantics()) {
1213+
auto u = builder.emitStrongRelease(loc, operand);
12261214
if (u.isNull())
12271215
return;
12281216

@@ -1235,7 +1223,7 @@ void swift::releasePartialApplyCapturedArg(SILBuilder &builder, SILLocation loc,
12351223
return;
12361224
}
12371225

1238-
auto u = builder.emitReleaseValue(loc, arg);
1226+
auto u = builder.emitReleaseValue(loc, operand);
12391227
if (u.isNull())
12401228
return;
12411229

@@ -1247,6 +1235,24 @@ void swift::releasePartialApplyCapturedArg(SILBuilder &builder, SILLocation loc,
12471235
callbacks.createdNewInst(u.get<ReleaseValueInst *>());
12481236
}
12491237

1238+
// *HEY YOU, YES YOU, PLEASE READ*. Even though a textual partial apply is
1239+
// printed with the convention of the closed over function upon it, all
1240+
// non-inout arguments to a partial_apply are passed at +1. This includes
1241+
// arguments that will eventually be passed as guaranteed or in_guaranteed to
1242+
// the closed over function. This is because the partial apply is building up a
1243+
// boxed aggregate to send off to the closed over function. Of course when you
1244+
// call the function, the proper conventions will be used.
1245+
void swift::releasePartialApplyCapturedArg(SILBuilder &builder, SILLocation loc,
1246+
SILValue arg,
1247+
SILParameterInfo paramInfo,
1248+
InstModCallbacks callbacks) {
1249+
if (!shouldDestroyPartialApplyCapturedArg(arg, paramInfo,
1250+
builder.getFunction()))
1251+
return;
1252+
1253+
emitDestroyOperation(builder, loc, arg, callbacks);
1254+
}
1255+
12501256
void swift::deallocPartialApplyCapturedArg(SILBuilder &builder, SILLocation loc,
12511257
SILValue arg,
12521258
SILParameterInfo paramInfo) {
@@ -1415,11 +1421,7 @@ bool swift::tryDeleteDeadClosure(SingleValueInstruction *closure,
14151421
for (Operand *argOp : argsToHandle) {
14161422
SILValue arg = argOp->get();
14171423
SILBuilderWithScope builder(pai, builderCtxt);
1418-
if (arg->getType().isObject()) {
1419-
builder.emitDestroyValueOperation(pai->getLoc(), arg);
1420-
} else {
1421-
builder.emitDestroyAddr(pai->getLoc(), arg);
1422-
}
1424+
emitDestroyOperation(builder, pai->getLoc(), arg, callbacks);
14231425
}
14241426
}
14251427
}

0 commit comments

Comments
 (0)