From e9f14361784bbf55e7a1be37b7d1772be2fe6df3 Mon Sep 17 00:00:00 2001 From: Mike Ash Date: Fri, 30 Jun 2023 11:58:17 -0400 Subject: [PATCH] [5.9][Runtime][IRGen] Sign type context descriptor pointers. Ensure that context descriptor pointers are signed in the runtime by putting the ptrauth_struct attribute on the types. We use the new __builtin_ptrauth_struct_key/disc to conditionally apply ptrauth_struct to TrailingObjects based on the signing of the base type, so that pointers to TrailingObjects get signed when used with a context descriptor pointer. We add new runtime entrypoints that take signed pointers where appropriate, and have the compiler emit calls to the new entrypoints when targeting a sufficiently new OS. rdar://111480914 (cherry picked from commit fe7e13bba5f2b9ed12dd04f51b1aab312fca0c8c) --- include/swift/ABI/Metadata.h | 86 +++++++---- include/swift/ABI/MetadataRef.h | 6 +- include/swift/ABI/MetadataValues.h | 6 + include/swift/ABI/TrailingObjects.h | 3 +- include/swift/ABI/TypeIdentity.h | 5 +- include/swift/AST/ASTContext.h | 8 ++ include/swift/AST/IRGenOptions.h | 9 ++ include/swift/Runtime/Casting.h | 19 ++- include/swift/Runtime/Config.h | 73 +++++++++- include/swift/Runtime/RuntimeFunctions.def | 62 +++++++- lib/AST/Availability.cpp | 10 ++ lib/IRGen/GenArchetype.cpp | 36 ++++- lib/IRGen/GenPointerAuth.cpp | 6 + lib/IRGen/GenPointerAuth.h | 3 + lib/IRGen/IRGen.cpp | 15 ++ lib/IRGen/IRGenModule.cpp | 18 +++ lib/IRGen/MetadataRequest.cpp | 23 ++- .../CompatibilityOverrideRuntime.def | 10 ++ stdlib/public/runtime/AnyHashableSupport.cpp | 4 +- stdlib/public/runtime/Casting.cpp | 4 +- stdlib/public/runtime/DynamicCast.cpp | 4 +- stdlib/public/runtime/ErrorObject.mm | 6 +- stdlib/public/runtime/MetadataLookup.cpp | 134 ++++++++++++++++-- stdlib/public/runtime/ProtocolConformance.cpp | 24 +++- stdlib/public/runtime/SwiftObject.mm | 2 +- stdlib/public/runtime/SwiftValue.mm | 2 +- test/IRGen/opaque_result_type.swift | 8 +- ...ype_associated_type_conformance_path.swift | 2 +- .../runtime/CompatibilityOverrideRuntime.cpp | 10 ++ 29 files changed, 521 insertions(+), 77 deletions(-) diff --git a/include/swift/ABI/Metadata.h b/include/swift/ABI/Metadata.h index ba2afed47ad25..c805f265c5714 100644 --- a/include/swift/ABI/Metadata.h +++ b/include/swift/ABI/Metadata.h @@ -56,12 +56,24 @@ template struct TargetOpaqueMetadata; template struct TargetValueMetadata; template struct TargetForeignClassMetadata; template struct TargetForeignReferenceTypeMetadata; -template struct TargetContextDescriptor; -template class TargetTypeContextDescriptor; -template class TargetClassDescriptor; -template class TargetValueTypeDescriptor; -template class TargetEnumDescriptor; -template class TargetStructDescriptor; +template +struct swift_ptrauth_struct_context_descriptor(ContextDescriptor) + TargetContextDescriptor; +template +class swift_ptrauth_struct_context_descriptor(TypeContextDescriptor) + TargetTypeContextDescriptor; +template +class swift_ptrauth_struct_context_descriptor(ClassDescriptor) + TargetClassDescriptor; +template +class swift_ptrauth_struct_context_descriptor(ValueTypeDescriptor) + TargetValueTypeDescriptor; +template +class swift_ptrauth_struct_context_descriptor(EnumDescriptor) + TargetEnumDescriptor; +template +class swift_ptrauth_struct_context_descriptor(StructDescriptor) + TargetStructDescriptor; template struct TargetGenericMetadataPattern; template struct TargetProtocolConformanceDescriptor; @@ -1632,7 +1644,9 @@ TargetTupleTypeMetadata::getOffsetToNumElements() -> StoredSize { return offsetof(TargetTupleTypeMetadata, NumElements); } -template struct TargetProtocolDescriptor; +template +struct swift_ptrauth_struct_context_descriptor(ProtocolDescriptor) + TargetProtocolDescriptor; /// A protocol requirement descriptor. This describes a single protocol /// requirement in a protocol descriptor. The index of the requirement in @@ -1660,7 +1674,9 @@ struct TargetProtocolRequirement { using ProtocolRequirement = TargetProtocolRequirement; -template struct TargetProtocolDescriptor; +template +struct swift_ptrauth_struct_context_descriptor(ProtocolDescriptor) + TargetProtocolDescriptor; using ProtocolDescriptor = TargetProtocolDescriptor; template