Skip to content

Commit 281088e

Browse files
authored
Merge pull request swiftlang#63204 from meg-gupta/ptrauthcodegenpr
Initial support for ptrauth qualified function pointers in C
2 parents f5c7e4f + 901c279 commit 281088e

23 files changed

+269
-9
lines changed

docs/SIL.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4522,6 +4522,7 @@ begin_access
45224522
sil-enforcement ::= static
45234523
sil-enforcement ::= dynamic
45244524
sil-enforcement ::= unsafe
4525+
sil-enforcement ::= signed
45254526
%1 = begin_access [read] [unknown] %0 : $*T
45264527
// %0 must be of $*T type.
45274528

@@ -4564,6 +4565,9 @@ runtime for the duration of its scope. This access may still conflict with an
45644565
outer access scope; therefore may still require dynamic enforcement at a single
45654566
point.
45664567

4568+
A ``signed`` access is for pointers that are signed in architectures that support
4569+
pointer signing.
4570+
45674571
A ``builtin`` access was emitted for a user-controlled Builtin (e.g. the
45684572
standard library's KeyPath access). Non-builtin accesses are auto-generated by
45694573
the compiler to enforce formal access that derives from the language. A

include/swift/AST/Decl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5958,6 +5958,8 @@ class VarDecl : public AbstractStorageDecl {
59585958
});
59595959
}
59605960

5961+
clang::PointerAuthQualifier getPointerAuthQualifier() const;
5962+
59615963
// Implement isa/cast/dyncast/etc.
59625964
static bool classof(const Decl *D) {
59635965
return D->getKind() == DeclKind::Var || D->getKind() == DeclKind::Param;

include/swift/AST/SILOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ class SILOptions {
135135
/// temporaries for stack protection.
136136
bool EnableMoveInoutStackProtection = false;
137137

138+
/// Enables codegen support for clang imported ptrauth qualified field
139+
/// function pointers.
140+
bool EnableImportPtrauthFieldFunctionPointers = false;
141+
138142
/// Controls whether or not paranoid verification checks are run.
139143
bool VerifyAll = false;
140144

include/swift/Option/FrontendOptions.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,13 @@ def enable_move_inout_stack_protector :
11121112
Flag<["-"], "enable-move-inout-stack-protector">,
11131113
HelpText<"Enable the stack protector by moving values to temporaries">;
11141114

1115+
def enable_import_ptrauth_field_function_pointers :
1116+
Flag<["-"], "enable-import-ptrauth-field-function-pointers">,
1117+
HelpText<"Enable import of custom ptrauth qualified field function pointers">;
1118+
def disable_import_ptrauth_field_function_pointers :
1119+
Flag<["-"], "disable-import-ptrauth-field-function-pointers">,
1120+
HelpText<"Disable import of custom ptrauth qualified field function pointers">;
1121+
11151122
def enable_collocate_metadata_functions :
11161123
Flag<["-"], "enable-collocate-metadata-functions">,
11171124
HelpText<"Enable collocate metadata functions">;

include/swift/SIL/SILInstruction.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4446,8 +4446,13 @@ enum class SILAccessEnforcement : uint8_t {
44464446
/// behavior.
44474447
Unsafe,
44484448

4449+
/// Access to pointers that are signed via pointer authentication mechanishm.
4450+
/// Such pointers should be authenticated before read and signed before a
4451+
/// write. Optimizer should avoid promoting such accesses to values.
4452+
Signed,
4453+
44494454
// This enum is encoded.
4450-
Last = Unsafe
4455+
Last = Signed
44514456
};
44524457
StringRef getSILAccessEnforcementName(SILAccessEnforcement enforcement);
44534458

@@ -4520,9 +4525,9 @@ class BeginAccessInst
45204525
: BeginAccessBase(loc, accessKind, enforcement, noNestedConflict,
45214526
fromBuiltin, lvalue, lvalue->getType()) {
45224527

4523-
static_assert(unsigned(SILAccessKind::Last) < (1 << 2),
4528+
static_assert(unsigned(SILAccessKind::Last) < (1 << 3),
45244529
"reserve sufficient bits for serialized SIL");
4525-
static_assert(unsigned(SILAccessEnforcement::Last) < (1 << 2),
4530+
static_assert(unsigned(SILAccessEnforcement::Last) < (1 << 3),
45264531
"reserve sufficient bits for serialized SIL");
45274532

45284533
static_assert(unsigned(SILAccessKind::Last) <

include/swift/SIL/SILNode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class alignas(8) SILNode :
122122
enum { NumAssignOwnershipQualifierBits = 2 };
123123
enum { NumAssignByWrapperModeBits = 2 };
124124
enum { NumSILAccessKindBits = 2 };
125-
enum { NumSILAccessEnforcementBits = 2 };
125+
enum { NumSILAccessEnforcementBits = 3 };
126126
enum { NumAllocRefTailTypesBits = 4 };
127127

128128
protected:

lib/AST/TypeWrapper.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,12 @@ bool VarDecl::isTypeWrapperLocalStorageForInitializer() const {
4444
}
4545
return false;
4646
}
47+
48+
clang::PointerAuthQualifier VarDecl::getPointerAuthQualifier() const {
49+
if (auto *clangDecl = getClangDecl()) {
50+
if (auto *valueDecl = dyn_cast<clang::ValueDecl>(clangDecl)) {
51+
return valueDecl->getType().getPointerAuth();
52+
}
53+
}
54+
return clang::PointerAuthQualifier();
55+
}

lib/Frontend/CompilerInvocation.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,9 +1826,13 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
18261826
Opts.EnableStackProtection =
18271827
Args.hasFlag(OPT_enable_stack_protector, OPT_disable_stack_protector,
18281828
Opts.EnableStackProtection);
1829-
Opts.EnableMoveInoutStackProtection =
1830-
Args.hasFlag(OPT_enable_move_inout_stack_protector, OPT_disable_stack_protector,
1831-
Opts.EnableMoveInoutStackProtection);
1829+
Opts.EnableMoveInoutStackProtection = Args.hasArg(
1830+
OPT_enable_move_inout_stack_protector, OPT_disable_stack_protector,
1831+
Opts.EnableMoveInoutStackProtection);
1832+
Opts.EnableImportPtrauthFieldFunctionPointers =
1833+
Args.hasArg(OPT_enable_import_ptrauth_field_function_pointers,
1834+
OPT_disable_import_ptrauth_field_function_pointers,
1835+
Opts.EnableImportPtrauthFieldFunctionPointers);
18321836
Opts.VerifyAll |= Args.hasArg(OPT_sil_verify_all);
18331837
Opts.VerifyNone |= Args.hasArg(OPT_sil_verify_none);
18341838
Opts.DebugSerialization |= Args.hasArg(OPT_sil_debug_serialization);

lib/IRGen/Callee.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ namespace irgen {
7878
llvm::Value *storageAddress,
7979
const PointerAuthEntity &entity);
8080

81+
static PointerAuthInfo emit(IRGenModule &IGM,
82+
clang::PointerAuthQualifier pointerAuthQual);
83+
8184
static PointerAuthInfo forFunctionPointer(IRGenModule &IGM,
8285
CanSILFunctionType fnType);
8386

lib/IRGen/GenCall.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5036,7 +5036,7 @@ Callee irgen::getCFunctionPointerCallee(IRGenFunction &IGF,
50365036
CalleeInfo &&calleeInfo) {
50375037
auto sig = emitCastOfFunctionPointer(IGF, fnPtr, calleeInfo.OrigFnType);
50385038
auto authInfo =
5039-
PointerAuthInfo::forFunctionPointer(IGF.IGM, calleeInfo.OrigFnType);
5039+
PointerAuthInfo::forFunctionPointer(IGF.IGM, calleeInfo.OrigFnType);
50405040

50415041
auto fn = FunctionPointer::createSigned(FunctionPointer::Kind::Function,
50425042
fnPtr, authInfo, sig);

0 commit comments

Comments
 (0)