Skip to content

Commit 3c125a7

Browse files
committed
arm64e: Workaround ptrauth-returns failure for swifttailcc
The current code generation will emit an autibsp after adjusting the stack pointe for the tail call. If callee and caller argument area does not match this would fail.
1 parent f75fbb7 commit 3c125a7

File tree

6 files changed

+28
-11
lines changed

6 files changed

+28
-11
lines changed

lib/IRGen/GenCall.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4785,8 +4785,9 @@ IRGenFunction::getFunctionPointerForResumeIntrinsic(llvm::Value *resume) {
47854785
auto *fnTy = llvm::FunctionType::get(
47864786
IGM.VoidTy, {IGM.Int8PtrTy},
47874787
false /*vaargs*/);
4788-
auto signature =
4789-
Signature(fnTy, IGM.constructInitialAttributes(), IGM.SwiftAsyncCC);
4788+
auto signature = Signature(
4789+
fnTy, IGM.constructInitialAttributes(true /*disable ptrauth-returns*/),
4790+
IGM.SwiftAsyncCC);
47904791
auto fnPtr = FunctionPointer(
47914792
FunctionPointer::Kind::Function,
47924793
Builder.CreateBitOrPointerCast(resume, fnTy->getPointerTo()),

lib/IRGen/GenDecl.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,7 +2100,11 @@ llvm::Function *irgen::createFunction(IRGenModule &IGM,
21002100
.to(fn, linkInfo.isForDefinition());
21012101

21022102
llvm::AttrBuilder initialAttrs;
2103-
IGM.constructInitialFnAttributes(initialAttrs, FuncOptMode);
2103+
// Workaround an llvm bug that does not handle this case correctly.
2104+
bool disablePtrAuthReturns =
2105+
signature.getCallingConv() == llvm::CallingConv::SwiftTail;
2106+
IGM.constructInitialFnAttributes(initialAttrs, disablePtrAuthReturns,
2107+
FuncOptMode);
21042108
// Merge initialAttrs with attrs.
21052109
auto updatedAttrs =
21062110
signature.getAttributes().addAttributes(IGM.getLLVMContext(),
@@ -2811,7 +2815,7 @@ llvm::Constant *swift::irgen::emitCXXConstructorThunkIfNeeded(
28112815
thunk->setCallingConv(llvm::CallingConv::C);
28122816

28132817
llvm::AttrBuilder attrBuilder;
2814-
IGM.constructInitialFnAttributes(attrBuilder);
2818+
IGM.constructInitialFnAttributes(attrBuilder, false /*disablePtrAuthReturns*/);
28152819
attrBuilder.addAttribute(llvm::Attribute::AlwaysInline);
28162820
llvm::AttributeList attr = signature.getAttributes().addAttributes(
28172821
IGM.getLLVMContext(), llvm::AttributeList::FunctionIndex, attrBuilder);

lib/IRGen/GenFunc.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,9 @@ static llvm::Value *emitPartialApplicationForwarder(IRGenModule &IGM,
11981198
fwd->setAttributes(outAttrs);
11991199
// Merge initial attributes with outAttrs.
12001200
llvm::AttrBuilder b;
1201-
IGM.constructInitialFnAttributes(b);
1201+
bool disablePtrAuthReturns =
1202+
outSig.getCallingConv() == llvm::CallingConv::SwiftTail;
1203+
IGM.constructInitialFnAttributes(b, disablePtrAuthReturns);
12021204
fwd->addAttributes(llvm::AttributeList::FunctionIndex, b);
12031205

12041206
IRGenFunction subIGF(IGM, fwd);

lib/IRGen/GenObjC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ static llvm::Function *emitObjCPartialApplicationForwarder(IRGenModule &IGM,
717717
fwd->setAttributes(attrs);
718718
// Merge initial attributes with attrs.
719719
llvm::AttrBuilder b;
720-
IGM.constructInitialFnAttributes(b);
720+
IGM.constructInitialFnAttributes(b, false /*disable ptr auth returns*/);
721721
fwd->addAttributes(llvm::AttributeList::FunctionIndex, b);
722722

723723
IRGenFunction subIGF(IGM, fwd);

lib/IRGen/IRGenModule.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,11 +1111,18 @@ void IRGenModule::setHasNoFramePointer(llvm::Function *F) {
11111111
}
11121112

11131113
/// Construct initial function attributes from options.
1114-
void IRGenModule::constructInitialFnAttributes(llvm::AttrBuilder &Attrs,
1115-
OptimizationMode FuncOptMode) {
1114+
void IRGenModule::constructInitialFnAttributes(
1115+
llvm::AttrBuilder &Attrs, bool disablePtrAuthReturns,
1116+
OptimizationMode FuncOptMode) {
11161117
// Add the default attributes for the Clang configuration.
11171118
clang::CodeGen::addDefaultFunctionDefinitionAttributes(getClangCGM(), Attrs);
11181119

1120+
// Disable `ptrauth-returns`. It does not work for swifttailcc lowering atm.
1121+
// The `autibsp` instruction executed before a tail call that adjust the stack
1122+
// will currently fail.
1123+
if (disablePtrAuthReturns)
1124+
Attrs.removeAttribute("ptrauth-returns");
1125+
11191126
// Add/remove MinSize based on the appropriate setting.
11201127
if (FuncOptMode == OptimizationMode::NotSet)
11211128
FuncOptMode = IRGen.Opts.OptMode;
@@ -1128,9 +1135,10 @@ void IRGenModule::constructInitialFnAttributes(llvm::AttrBuilder &Attrs,
11281135
}
11291136
}
11301137

1131-
llvm::AttributeList IRGenModule::constructInitialAttributes() {
1138+
llvm::AttributeList
1139+
IRGenModule::constructInitialAttributes(bool disablePtrAuthReturns) {
11321140
llvm::AttrBuilder b;
1133-
constructInitialFnAttributes(b);
1141+
constructInitialFnAttributes(b, disablePtrAuthReturns);
11341142
return llvm::AttributeList::get(getLLVMContext(),
11351143
llvm::AttributeList::FunctionIndex, b);
11361144
}

lib/IRGen/IRGenModule.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1362,11 +1362,13 @@ private: \
13621362
bool finalize();
13631363

13641364
void constructInitialFnAttributes(llvm::AttrBuilder &Attrs,
1365+
bool disablePtrAuthReturns,
13651366
OptimizationMode FuncOptMode =
13661367
OptimizationMode::NotSet);
13671368
void setHasNoFramePointer(llvm::AttrBuilder &Attrs);
13681369
void setHasNoFramePointer(llvm::Function *F);
1369-
llvm::AttributeList constructInitialAttributes();
1370+
llvm::AttributeList
1371+
constructInitialAttributes(bool disablePtrAuthReturns = false);
13701372

13711373
void emitProtocolDecl(ProtocolDecl *D);
13721374
void emitEnumDecl(EnumDecl *D);

0 commit comments

Comments
 (0)