Skip to content

Commit b91b772

Browse files
authored
Merge pull request #81679 from meg-gupta/fixsubstne
[6.2] Fix use-after-free on substituting function type involving conditional ~Escapable with Escapable type
2 parents 4283d2b + 00dcffc commit b91b772

File tree

5 files changed

+53
-25
lines changed

5 files changed

+53
-25
lines changed

include/swift/AST/ExtInfo.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,12 +740,19 @@ class ASTExtInfoBuilder {
740740
globalActor, thrownError, lifetimeDependencies);
741741
}
742742

743+
/// \p lifetimeDependencies should be arena allocated and not a temporary
744+
/// Function types are allocated on the are arena and their ExtInfo should be
745+
/// valid throughout their lifetime.
743746
[[nodiscard]] ASTExtInfoBuilder withLifetimeDependencies(
744747
llvm::ArrayRef<LifetimeDependenceInfo> lifetimeDependencies) const {
745748
return ASTExtInfoBuilder(bits, clangTypeInfo, globalActor, thrownError,
746749
lifetimeDependencies);
747750
}
748751

752+
[[nodiscard]] ASTExtInfoBuilder withLifetimeDependencies(
753+
SmallVectorImpl<LifetimeDependenceInfo> lifetimeDependencies) const =
754+
delete;
755+
749756
[[nodiscard]]
750757
ASTExtInfoBuilder withIsolation(FunctionTypeIsolation isolation) const {
751758
return ASTExtInfoBuilder(
@@ -920,11 +927,18 @@ class ASTExtInfo {
920927
.build();
921928
}
922929

930+
/// \p lifetimeDependencies should be arena allocated and not a temporary
931+
/// Function types are allocated on the are arena and their ExtInfo should be
932+
/// valid throughout their lifetime.
923933
[[nodiscard]] ASTExtInfo withLifetimeDependencies(
924934
ArrayRef<LifetimeDependenceInfo> lifetimeDependencies) const {
925935
return builder.withLifetimeDependencies(lifetimeDependencies).build();
926936
}
927937

938+
[[nodiscard]] ASTExtInfo withLifetimeDependencies(
939+
SmallVectorImpl<LifetimeDependenceInfo> lifetimeDependencies) const =
940+
delete;
941+
928942
void Profile(llvm::FoldingSetNodeID &ID) const { builder.Profile(ID); }
929943

930944
bool isEqualTo(ASTExtInfo other, bool useClangTypes) const {
@@ -1227,11 +1241,19 @@ class SILExtInfoBuilder {
12271241
return SILExtInfoBuilder(bits, ClangTypeInfo(type).getCanonical(),
12281242
lifetimeDependencies);
12291243
}
1244+
1245+
/// \p lifetimeDependencies should be arena allocated and not a temporary
1246+
/// Function types are allocated on the are arena and their ExtInfo should be
1247+
/// valid throughout their lifetime.
12301248
[[nodiscard]] SILExtInfoBuilder withLifetimeDependencies(
12311249
ArrayRef<LifetimeDependenceInfo> lifetimeDependenceInfo) const {
12321250
return SILExtInfoBuilder(bits, clangTypeInfo, lifetimeDependenceInfo);
12331251
}
12341252

1253+
[[nodiscard]] ASTExtInfoBuilder withLifetimeDependencies(
1254+
SmallVectorImpl<LifetimeDependenceInfo> lifetimeDependencies) const =
1255+
delete;
1256+
12351257
void Profile(llvm::FoldingSetNodeID &ID) const {
12361258
ID.AddInteger(bits);
12371259
ID.AddPointer(clangTypeInfo.getType());
@@ -1368,11 +1390,17 @@ class SILExtInfo {
13681390
return builder.withUnimplementable(isUnimplementable).build();
13691391
}
13701392

1393+
/// \p lifetimeDependencies should be arena allocated and not a temporary
1394+
/// Function types are allocated on the are arena and their ExtInfo should be
1395+
/// valid throughout their lifetime.
13711396
SILExtInfo withLifetimeDependencies(
13721397
ArrayRef<LifetimeDependenceInfo> lifetimeDependencies) const {
13731398
return builder.withLifetimeDependencies(lifetimeDependencies);
13741399
}
13751400

1401+
SILExtInfo withLifetimeDependencies(SmallVectorImpl<LifetimeDependenceInfo>
1402+
lifetimeDependencies) const = delete;
1403+
13761404
void Profile(llvm::FoldingSetNodeID &ID) const { builder.Profile(ID); }
13771405

13781406
bool isEqualTo(SILExtInfo other, bool useClangTypes) const {

include/swift/AST/TypeTransform.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,8 @@ case TypeKind::Id:
358358
}
359359
});
360360
if (didRemoveLifetimeDependencies) {
361-
extInfo = extInfo.withLifetimeDependencies(substDependenceInfos);
361+
extInfo = extInfo.withLifetimeDependencies(
362+
ctx.AllocateCopy(substDependenceInfos));
362363
}
363364
}
364365

@@ -939,7 +940,8 @@ case TypeKind::Id:
939940
});
940941

941942
if (didRemoveLifetimeDependencies) {
942-
extInfo = extInfo->withLifetimeDependencies(substDependenceInfos);
943+
extInfo = extInfo->withLifetimeDependencies(
944+
ctx.AllocateCopy(substDependenceInfos));
943945
}
944946
}
945947

include/swift/AST/Types.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5200,12 +5200,12 @@ class SILFunctionType final
52005200
/// Given an existing ExtInfo, and a set of interface parameters and results
52015201
/// destined for a new SILFunctionType, return a new ExtInfo with only the
52025202
/// lifetime dependencies relevant after substitution.
5203-
static ExtInfo
5204-
getSubstLifetimeDependencies(GenericSignature genericSig,
5205-
ExtInfo origExtInfo,
5206-
ArrayRef<SILParameterInfo> params,
5207-
ArrayRef<SILYieldInfo> yields,
5208-
ArrayRef<SILResultInfo> results);
5203+
static ExtInfo getSubstLifetimeDependencies(GenericSignature genericSig,
5204+
ExtInfo origExtInfo,
5205+
ASTContext &context,
5206+
ArrayRef<SILParameterInfo> params,
5207+
ArrayRef<SILYieldInfo> yields,
5208+
ArrayRef<SILResultInfo> results);
52095209

52105210
/// Return a structurally-identical function type with a slightly tweaked
52115211
/// ExtInfo.

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,10 @@ SILType SILFunctionType::substInterfaceType(SILModule &M,
5959
return interfaceType;
6060
}
6161

62-
SILFunctionType::ExtInfo
63-
SILFunctionType::getSubstLifetimeDependencies(GenericSignature genericSig,
64-
ExtInfo origExtInfo,
65-
ArrayRef<SILParameterInfo> params,
66-
ArrayRef<SILYieldInfo> yields,
67-
ArrayRef<SILResultInfo> results) {
62+
SILFunctionType::ExtInfo SILFunctionType::getSubstLifetimeDependencies(
63+
GenericSignature genericSig, ExtInfo origExtInfo, ASTContext &context,
64+
ArrayRef<SILParameterInfo> params, ArrayRef<SILYieldInfo> yields,
65+
ArrayRef<SILResultInfo> results) {
6866
if (origExtInfo.getLifetimeDependencies().empty()) {
6967
return origExtInfo;
7068
}
@@ -87,7 +85,8 @@ SILFunctionType::getSubstLifetimeDependencies(GenericSignature genericSig,
8785
}
8886
});
8987
if (didRemoveLifetimeDependencies) {
90-
return origExtInfo.withLifetimeDependencies(substLifetimeDependencies);
88+
return origExtInfo.withLifetimeDependencies(
89+
context.AllocateCopy(substLifetimeDependencies));
9190
}
9291
return origExtInfo;
9392
}
@@ -131,10 +130,10 @@ CanSILFunctionType SILFunctionType::getUnsubstitutedType(SILModule &M) const {
131130

132131
auto signature = isPolymorphic() ? getInvocationGenericSignature()
133132
: CanGenericSignature();
134-
135-
auto extInfo = getSubstLifetimeDependencies(signature, getExtInfo(),
136-
params, yields, results);
137-
133+
134+
auto extInfo = getSubstLifetimeDependencies(
135+
signature, getExtInfo(), getASTContext(), params, yields, results);
136+
138137
return SILFunctionType::get(signature,
139138
extInfo,
140139
getCoroutineKind(),
@@ -2860,7 +2859,7 @@ static CanSILFunctionType getSILFunctionType(
28602859
.withSendable(isSendable)
28612860
.withAsync(isAsync)
28622861
.withUnimplementable(unimplementable)
2863-
.withLifetimeDependencies(loweredLifetimes)
2862+
.withLifetimeDependencies(TC.Context.AllocateCopy(loweredLifetimes))
28642863
.build();
28652864

28662865
return SILFunctionType::get(genericSig, silExtInfo, coroutineKind,

lib/SIL/IR/SILTypeSubstitution.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,10 @@ class SILTypeSubstituter :
240240
auto genericSig = IFS.shouldSubstituteOpaqueArchetypes()
241241
? origType->getInvocationGenericSignature()
242242
: nullptr;
243-
244-
extInfo = SILFunctionType::getSubstLifetimeDependencies(genericSig, extInfo,
245-
substParams,
246-
substYields,
247-
substResults);
243+
244+
extInfo = SILFunctionType::getSubstLifetimeDependencies(
245+
genericSig, extInfo, TC.Context, substParams, substYields,
246+
substResults);
248247

249248
return SILFunctionType::get(genericSig, extInfo,
250249
origType->getCoroutineKind(),

0 commit comments

Comments
 (0)