Skip to content

Commit 3381b23

Browse files
committed
Fix use-after-free error caused by SILBuilderWithScope.
The problem occurs if one uses a SILBuilderWithScope to create an instruction and then later on delete that instruction before the SILBuilderWithScope's destructor has been called. In such a case when the SILBuilderWithScope's destructor runs, the destructor will attempt to set the debug scope of the deleted instruction. This will be caught by ASAN once a patch by Roman lands that changes instructions to use malloc instead of BumpPtrAllocators. <rdar://problem/23548378>
1 parent 26cac78 commit 3381b23

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

lib/SILPasses/Scalar/SimplifyCFG.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -2468,16 +2468,20 @@ bool ArgumentSplitter::createNewArguments() {
24682468
NewArgumentValues.push_back(NewArg);
24692469
}
24702470

2471-
SILBuilderWithScope<> B(ParentBB->begin(), nullptr);
2471+
SILInstruction *Agg = nullptr;
2472+
2473+
{
2474+
SILBuilderWithScope<> B(ParentBB->begin(), nullptr);
2475+
2476+
// Reform the original structure
2477+
//
2478+
// TODO: What is the right location to use here.
2479+
auto Loc = RegularLocation::getAutoGeneratedLocation();
2480+
Agg = Projection::createAggFromFirstLevelProjections(
2481+
B, Loc, Arg->getType(), NewArgumentValues).get();
2482+
assert(Agg->getNumTypes() == 1 && "Expected only one result");
2483+
}
24722484

2473-
// Reform the original structure
2474-
//
2475-
// TODO: What is the right location to use here.
2476-
auto Loc = RegularLocation::getAutoGeneratedLocation();
2477-
SILInstruction *Agg = Projection::createAggFromFirstLevelProjections(
2478-
B, Loc, Arg->getType(), NewArgumentValues)
2479-
.get();
2480-
assert(Agg->getNumTypes() == 1 && "Expected only one result");
24812485
SILValue(Arg).replaceAllUsesWith(SILValue(Agg));
24822486

24832487
// Look at all users of agg and see if we can simplify any of them. This will

0 commit comments

Comments
 (0)