Skip to content

Commit 2d238bc

Browse files
authored
Merge pull request #67168 from mikeash/ptrauth-struct-context-descriptors
[Runtime][IRGen] Sign type context descriptor pointers.
2 parents cd0d4dc + fe7e13b commit 2d238bc

29 files changed

+519
-75
lines changed

include/swift/ABI/Metadata.h

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,24 @@ template <typename Runtime> struct TargetOpaqueMetadata;
5656
template <typename Runtime> struct TargetValueMetadata;
5757
template <typename Runtime> struct TargetForeignClassMetadata;
5858
template <typename Runtime> struct TargetForeignReferenceTypeMetadata;
59-
template <typename Runtime> struct TargetContextDescriptor;
60-
template <typename Runtime> class TargetTypeContextDescriptor;
61-
template <typename Runtime> class TargetClassDescriptor;
62-
template <typename Runtime> class TargetValueTypeDescriptor;
63-
template <typename Runtime> class TargetEnumDescriptor;
64-
template <typename Runtime> class TargetStructDescriptor;
59+
template <typename Runtime>
60+
struct swift_ptrauth_struct_context_descriptor(ContextDescriptor)
61+
TargetContextDescriptor;
62+
template <typename Runtime>
63+
class swift_ptrauth_struct_context_descriptor(TypeContextDescriptor)
64+
TargetTypeContextDescriptor;
65+
template <typename Runtime>
66+
class swift_ptrauth_struct_context_descriptor(ClassDescriptor)
67+
TargetClassDescriptor;
68+
template <typename Runtime>
69+
class swift_ptrauth_struct_context_descriptor(ValueTypeDescriptor)
70+
TargetValueTypeDescriptor;
71+
template <typename Runtime>
72+
class swift_ptrauth_struct_context_descriptor(EnumDescriptor)
73+
TargetEnumDescriptor;
74+
template <typename Runtime>
75+
class swift_ptrauth_struct_context_descriptor(StructDescriptor)
76+
TargetStructDescriptor;
6577
template <typename Runtime> struct TargetGenericMetadataPattern;
6678
template <typename Runtime> struct TargetProtocolConformanceDescriptor;
6779

@@ -1632,7 +1644,9 @@ TargetTupleTypeMetadata<Runtime>::getOffsetToNumElements() -> StoredSize {
16321644
return offsetof(TargetTupleTypeMetadata<Runtime>, NumElements);
16331645
}
16341646

1635-
template <typename Runtime> struct TargetProtocolDescriptor;
1647+
template <typename Runtime>
1648+
struct swift_ptrauth_struct_context_descriptor(ProtocolDescriptor)
1649+
TargetProtocolDescriptor;
16361650

16371651
/// A protocol requirement descriptor. This describes a single protocol
16381652
/// requirement in a protocol descriptor. The index of the requirement in
@@ -1660,7 +1674,9 @@ struct TargetProtocolRequirement {
16601674

16611675
using ProtocolRequirement = TargetProtocolRequirement<InProcess>;
16621676

1663-
template<typename Runtime> struct TargetProtocolDescriptor;
1677+
template <typename Runtime>
1678+
struct swift_ptrauth_struct_context_descriptor(ProtocolDescriptor)
1679+
TargetProtocolDescriptor;
16641680
using ProtocolDescriptor = TargetProtocolDescriptor<InProcess>;
16651681

16661682
template<template <typename Runtime> class ObjCInteropKind, unsigned PointerSize>
@@ -2803,12 +2819,14 @@ using ExternalProtocolConformanceDescriptor = TargetProtocolConformanceDescripto
28032819
template<template <typename Runtime> class ObjCInteropKind, unsigned PointerSize>
28042820
using ExternalProtocolConformanceRecord = TargetProtocolConformanceRecord<External<ObjCInteropKind<RuntimeTarget<PointerSize>>>>;
28052821

2806-
template<typename Runtime>
2807-
struct TargetModuleContextDescriptor;
2822+
template <typename Runtime>
2823+
struct swift_ptrauth_struct_context_descriptor(ModuleContextDescriptor)
2824+
TargetModuleContextDescriptor;
28082825

28092826
/// Base class for all context descriptors.
2810-
template<typename Runtime>
2811-
struct TargetContextDescriptor {
2827+
template <typename Runtime>
2828+
struct swift_ptrauth_struct_context_descriptor(ContextDescriptor)
2829+
TargetContextDescriptor {
28122830
/// Flags describing the context, including its kind and format version.
28132831
ContextDescriptorFlags Flags;
28142832

@@ -2864,8 +2882,9 @@ inline bool isCImportedModuleName(llvm::StringRef name) {
28642882
}
28652883

28662884
/// Descriptor for a module context.
2867-
template<typename Runtime>
2868-
struct TargetModuleContextDescriptor final : TargetContextDescriptor<Runtime> {
2885+
template <typename Runtime>
2886+
struct swift_ptrauth_struct_context_descriptor(ModuleContextDescriptor)
2887+
TargetModuleContextDescriptor final : TargetContextDescriptor<Runtime> {
28692888
/// The module name.
28702889
RelativeDirectPointer<const char, /*nullable*/ false> Name;
28712890

@@ -2905,8 +2924,9 @@ TargetContextDescriptor<Runtime>::getModuleContext() const {
29052924
}
29062925

29072926
/// Descriptor for an extension context.
2908-
template<typename Runtime>
2909-
struct TargetExtensionContextDescriptor final
2927+
template <typename Runtime>
2928+
struct swift_ptrauth_struct_context_descriptor(ExtensionContextDescriptor)
2929+
TargetExtensionContextDescriptor final
29102930
: TargetContextDescriptor<Runtime>,
29112931
TrailingGenericContextObjects<TargetExtensionContextDescriptor<Runtime>>
29122932
{
@@ -2944,8 +2964,9 @@ struct TargetMangledContextName {
29442964
TargetRelativeDirectPointer<Runtime, const char, /*nullable*/ false> name;
29452965
};
29462966

2947-
template<typename Runtime>
2948-
struct TargetAnonymousContextDescriptor final
2967+
template <typename Runtime>
2968+
struct swift_ptrauth_struct_context_descriptor(AnonymousContextDescriptor)
2969+
TargetAnonymousContextDescriptor final
29492970
: TargetContextDescriptor<Runtime>,
29502971
TrailingGenericContextObjects<TargetAnonymousContextDescriptor<Runtime>,
29512972
TargetGenericContextDescriptorHeader,
@@ -3025,8 +3046,9 @@ using ExternalAnonymousContextDescriptor = TargetAnonymousContextDescriptor<Exte
30253046
/// Only Swift protocols are defined by a protocol descriptor, whereas
30263047
/// Objective-C (including protocols defined in Swift as @objc) use the
30273048
/// Objective-C protocol layout.
3028-
template<typename Runtime>
3029-
struct TargetProtocolDescriptor final
3049+
template <typename Runtime>
3050+
struct swift_ptrauth_struct_context_descriptor(ProtocolDescriptor)
3051+
TargetProtocolDescriptor final
30303052
: TargetContextDescriptor<Runtime>,
30313053
swift::ABI::TrailingObjects<
30323054
TargetProtocolDescriptor<Runtime>,
@@ -3114,11 +3136,12 @@ struct TargetProtocolDescriptor final
31143136
return cd->getKind() == ContextDescriptorKind::Protocol;
31153137
}
31163138
};
3117-
3139+
31183140
/// The descriptor for an opaque type.
31193141
template <typename Runtime>
3120-
struct TargetOpaqueTypeDescriptor final
3121-
: TargetContextDescriptor<Runtime>,
3142+
struct swift_ptrauth_struct_context_descriptor(OpaqueTypeDescriptor)
3143+
TargetOpaqueTypeDescriptor final
3144+
: TargetContextDescriptor<Runtime>,
31223145
TrailingGenericContextObjects<TargetOpaqueTypeDescriptor<Runtime>,
31233146
TargetGenericContextDescriptorHeader,
31243147
RelativeDirectPointer<const char>>
@@ -3694,8 +3717,8 @@ struct TargetCanonicalSpecializedMetadatasCachingOnceToken {
36943717
};
36953718

36963719
template <typename Runtime>
3697-
class TargetTypeContextDescriptor
3698-
: public TargetContextDescriptor<Runtime> {
3720+
class swift_ptrauth_struct_context_descriptor(TypeContextDescriptor)
3721+
TargetTypeContextDescriptor : public TargetContextDescriptor<Runtime> {
36993722
public:
37003723
/// The name of the type.
37013724
TargetRelativeDirectPointer<Runtime, const char, /*nullable*/ false> Name;
@@ -3917,7 +3940,8 @@ struct TargetObjCResilientClassStubInfo {
39173940
};
39183941

39193942
template <typename Runtime>
3920-
class TargetClassDescriptor final
3943+
class swift_ptrauth_struct_context_descriptor(ClassDescriptor)
3944+
TargetClassDescriptor final
39213945
: public TargetTypeContextDescriptor<Runtime>,
39223946
public TrailingGenericContextObjects<TargetClassDescriptor<Runtime>,
39233947
TargetTypeGenericContextDescriptorHeader,
@@ -4307,8 +4331,8 @@ class TargetClassDescriptor final
43074331
using ClassDescriptor = TargetClassDescriptor<InProcess>;
43084332

43094333
template <typename Runtime>
4310-
class TargetValueTypeDescriptor
4311-
: public TargetTypeContextDescriptor<Runtime> {
4334+
class swift_ptrauth_struct_context_descriptor(ValueTypeDescriptor)
4335+
TargetValueTypeDescriptor : public TargetTypeContextDescriptor<Runtime>{
43124336
public:
43134337
static bool classof(const TargetContextDescriptor<Runtime> *cd) {
43144338
return cd->getKind() == ContextDescriptorKind::Struct ||
@@ -4318,7 +4342,8 @@ class TargetValueTypeDescriptor
43184342
using ValueTypeDescriptor = TargetValueTypeDescriptor<InProcess>;
43194343

43204344
template <typename Runtime>
4321-
class TargetStructDescriptor final
4345+
class swift_ptrauth_struct_context_descriptor(StructDescriptor)
4346+
TargetStructDescriptor final
43224347
: public TargetValueTypeDescriptor<Runtime>,
43234348
public TrailingGenericContextObjects<TargetStructDescriptor<Runtime>,
43244349
TargetTypeGenericContextDescriptorHeader,
@@ -4445,7 +4470,8 @@ class TargetStructDescriptor final
44454470
using StructDescriptor = TargetStructDescriptor<InProcess>;
44464471

44474472
template <typename Runtime>
4448-
class TargetEnumDescriptor final
4473+
class swift_ptrauth_struct_context_descriptor(EnumDescriptor)
4474+
TargetEnumDescriptor final
44494475
: public TargetValueTypeDescriptor<Runtime>,
44504476
public TrailingGenericContextObjects<TargetEnumDescriptor<Runtime>,
44514477
TargetTypeGenericContextDescriptorHeader,

include/swift/ABI/MetadataRef.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ struct TargetAnyClassMetadataObjCInterop;
3434
template <typename Runtime, typename TargetAnyClassMetadataVariant>
3535
struct TargetClassMetadata;
3636
template <typename Runtime>
37-
struct TargetContextDescriptor;
37+
struct swift_ptrauth_struct_context_descriptor(ContextDescriptor)
38+
TargetContextDescriptor;
3839
template <typename Runtime>
39-
struct TargetProtocolDescriptor;
40+
struct swift_ptrauth_struct_context_descriptor(ProtocolDescriptor)
41+
TargetProtocolDescriptor;
4042

4143
namespace detail {
4244
template <typename Runtime, bool ObjCInterop = Runtime::ObjCInterop>

include/swift/ABI/MetadataValues.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,12 @@ namespace SpecialPointerAuthDiscriminators {
13911391
/// Protocol conformance descriptors.
13921392
const uint16_t ProtocolConformanceDescriptor = 0xc6eb;
13931393

1394+
const uint16_t ProtocolDescriptor = 0xe909; // = 59657
1395+
1396+
// Type descriptors as arguments.
1397+
const uint16_t OpaqueTypeDescriptor = 0xbdd1; // = 48593
1398+
const uint16_t ContextDescriptor = 0xb5e3; // = 46563
1399+
13941400
/// Pointer to value witness table stored in type metadata.
13951401
///
13961402
/// Computed with ptrauth_string_discriminator("value_witness_table_t").

include/swift/ABI/TrailingObjects.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ class TrailingObjectsImpl<Align, BaseTy, TopTrailingObj, PrevTy>
234234
/// See the file comment for details on the usage of the
235235
/// TrailingObjects type.
236236
template <typename BaseTy, typename... TrailingTys>
237-
class TrailingObjects : private trailing_objects_internal::TrailingObjectsImpl<
237+
class swift_ptrauth_struct_derived(BaseTy) TrailingObjects
238+
: private trailing_objects_internal::TrailingObjectsImpl<
238239
trailing_objects_internal::AlignmentCalcHelper<
239240
TrailingTys...>::Alignment,
240241
BaseTy, TrailingObjects<BaseTy, TrailingTys...>,

include/swift/ABI/TypeIdentity.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
#define SWIFT_ABI_TYPEIDENTITY_H
2020

2121
#include "swift/Basic/LLVM.h"
22+
#include "swift/Runtime/Config.h"
2223
#include <llvm/ADT/Optional.h>
2324
#include <llvm/ADT/StringRef.h>
2425

2526
namespace swift {
26-
template <class> class TargetTypeContextDescriptor;
27+
template <typename Runtime>
28+
class swift_ptrauth_struct_context_descriptor(TypeContextDescriptor)
29+
TargetTypeContextDescriptor;
2730
struct InProcess;
2831
using TypeContextDescriptor = TargetTypeContextDescriptor<InProcess>;
2932

include/swift/AST/ASTContext.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,14 @@ class ASTContext final {
929929
/// variadic generic types.
930930
AvailabilityContext getVariadicGenericTypeAvailability();
931931

932+
/// Get the runtime availability of the conformsToProtocol runtime entrypoint
933+
/// that takes a signed protocol descriptor pointer.
934+
AvailabilityContext getSignedConformsToProtocolAvailability();
935+
936+
/// Get the runtime availability of runtime entrypoints that take signed type
937+
/// descriptors.
938+
AvailabilityContext getSignedDescriptorAvailability();
939+
932940
/// Get the runtime availability of features introduced in the Swift 5.2
933941
/// compiler for the target platform.
934942
AvailabilityContext getSwift52Availability();

include/swift/AST/IRGenOptions.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ struct PointerAuthOptions : clang::PointerAuthOptions {
147147
/// Protocol conformance descriptors when passed as arguments.
148148
PointerAuthSchema ProtocolConformanceDescriptorsAsArguments;
149149

150+
/// Protocol descriptors when passed as arguments.
151+
PointerAuthSchema ProtocolDescriptorsAsArguments;
152+
153+
/// Opaque type descriptors when passed as arguments.
154+
PointerAuthSchema OpaqueTypeDescriptorsAsArguments;
155+
156+
/// Type context descriptors when passed as arguments.
157+
PointerAuthSchema ContextDescriptorsAsArguments;
158+
150159
/// Resumption functions from yield-once coroutines.
151160
PointerAuthSchema YieldOnceResumeFunctions;
152161

include/swift/Runtime/Casting.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,26 @@ const Metadata *swift_getObjectType(HeapObject *object);
239239
/// \param type The metadata for the type for which to do the conformance
240240
/// check.
241241
/// \param protocol The protocol descriptor for the protocol to check
242-
/// conformance for.
242+
/// conformance for. This pointer does not have ptrauth applied.
243243
SWIFT_RUNTIME_EXPORT
244244
const WitnessTable *swift_conformsToProtocol(const Metadata *type,
245-
const ProtocolDescriptor *protocol);
245+
const void *protocol);
246246

247+
/// Check whether a type conforms to a given native Swift protocol. Identical to
248+
/// swift_conformsToProtocol, except that the protocol parameter has a ptrauth
249+
/// signature on ARM64e that is signed with a process independent key.
250+
SWIFT_RUNTIME_EXPORT
251+
const WitnessTable *
252+
swift_conformsToProtocol2(const Metadata *type,
253+
const ProtocolDescriptor *protocol);
254+
255+
/// Check whether a type conforms to a given native Swift protocol. Identical to
256+
/// swift_conformsToProtocol, except that the protocol parameter has a ptrauth
257+
/// signature on ARM64e that is signed with a process dependent key.
258+
SWIFT_RUNTIME_EXPORT
259+
const WitnessTable *
260+
swift_conformsToProtocolCommon(const Metadata *type,
261+
const ProtocolDescriptor *protocol);
247262
} // end namespace swift
248263

249264
#endif // SWIFT_RUNTIME_CASTING_H

0 commit comments

Comments
 (0)