Skip to content

Commit 2167f5c

Browse files
authored
Merge pull request swiftlang#36850 from mikeash/isa-superclass-ptrauth-qualifiers
[Runtime] Use ptrauth qualifiers on metadata and superclass fields.
2 parents ceeee45 + 3da4d20 commit 2167f5c

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

include/swift/ABI/Metadata.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,9 @@ struct TargetAnyClassMetadata : public TargetHeapMetadata<Runtime> {
10351035
// Note that ObjC classes do not have a metadata header.
10361036

10371037
/// The metadata for the superclass. This is null for the root class.
1038-
ConstTargetMetadataPointer<Runtime, swift::TargetClassMetadata> Superclass;
1038+
TargetSignedPointer<Runtime, const TargetClassMetadata<Runtime> *
1039+
__ptrauth_swift_objc_superclass>
1040+
Superclass;
10391041

10401042
#if SWIFT_OBJC_INTEROP
10411043
/// The cache data is used for certain dynamic lookups; it is owned

include/swift/ABI/MetadataValues.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,10 @@ namespace SpecialPointerAuthDiscriminators {
12091209
const uint16_t OpaqueReadResumeFunction = 56769;
12101210
const uint16_t OpaqueModifyResumeFunction = 3909;
12111211

1212+
/// ObjC class pointers.
1213+
const uint16_t ObjCISA = 0x6AE1;
1214+
const uint16_t ObjCSuperclass = 0xB5AB;
1215+
12121216
/// Resilient class stub initializer callback
12131217
const uint16_t ResilientClassStubInitCallback = 0xC671;
12141218

include/swift/Remote/MetadataReader.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ class MetadataReader {
500500
return StoredPointer();
501501

502502
auto classMeta = cast<TargetClassMetadata<Runtime>>(meta);
503-
return classMeta->Superclass;
503+
return stripSignedPointer(classMeta->Superclass);
504504
}
505505

506506
/// Given a remote pointer to class metadata, attempt to discover its class
@@ -534,9 +534,9 @@ class MetadataReader {
534534
size_t start = isaAndRetainCountSize;
535535

536536
auto classMeta = cast<TargetClassMetadata<Runtime>>(meta);
537-
while (classMeta->Superclass) {
537+
while (stripSignedPointer(classMeta->Superclass)) {
538538
classMeta = cast<TargetClassMetadata<Runtime>>(
539-
readMetadata(classMeta->Superclass));
539+
readMetadata(stripSignedPointer(classMeta->Superclass)));
540540

541541
// Subtract the size contribution of the isa and retain counts from
542542
// the super class.
@@ -1751,7 +1751,8 @@ class MetadataReader {
17511751
if (descriptorAddress || !skipArtificialSubclasses)
17521752
return static_cast<StoredPointer>(descriptorAddress);
17531753

1754-
auto superclassMetadataAddress = classMeta->Superclass;
1754+
auto superclassMetadataAddress =
1755+
stripSignedPointer(classMeta->Superclass);
17551756
if (!superclassMetadataAddress)
17561757
return 0;
17571758

@@ -2661,11 +2662,11 @@ class MetadataReader {
26612662
BuiltType BuiltObjCClass = Builder.createObjCClassType(std::move(className));
26622663
if (!BuiltObjCClass) {
26632664
// Try the superclass.
2664-
if (!classMeta->Superclass)
2665+
if (!stripSignedPointer(classMeta->Superclass))
26652666
return BuiltType();
26662667

2667-
BuiltObjCClass = readTypeFromMetadata(classMeta->Superclass,
2668-
skipArtificialSubclasses);
2668+
BuiltObjCClass = readTypeFromMetadata(
2669+
stripSignedPointer(classMeta->Superclass), skipArtificialSubclasses);
26692670
}
26702671

26712672
TypeCache[origMetadataPtr] = BuiltObjCClass;

include/swift/Runtime/Config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ extern uintptr_t __COMPATIBILITY_LIBRARIES_CANNOT_CHECK_THE_IS_SWIFT_BIT_DIRECTL
250250
#define __ptrauth_swift_dispatch_invoke_function \
251251
__ptrauth(ptrauth_key_process_independent_code, 1, \
252252
SpecialPointerAuthDiscriminators::DispatchInvokeFunction)
253+
#define __ptrauth_swift_objc_superclass \
254+
__ptrauth(ptrauth_key_process_independent_data, 1, \
255+
swift::SpecialPointerAuthDiscriminators::ObjCSuperclass)
253256
#define swift_ptrauth_sign_opaque_read_resume_function(__fn, __buffer) \
254257
ptrauth_auth_and_resign(__fn, ptrauth_key_function_pointer, 0, \
255258
ptrauth_key_process_independent_code, \
@@ -276,6 +279,7 @@ extern uintptr_t __COMPATIBILITY_LIBRARIES_CANNOT_CHECK_THE_IS_SWIFT_BIT_DIRECTL
276279
#define __ptrauth_swift_cancellation_notification_function
277280
#define __ptrauth_swift_escalation_notification_function
278281
#define __ptrauth_swift_dispatch_invoke_function
282+
#define __ptrauth_swift_objc_superclass
279283
#define __ptrauth_swift_runtime_function_entry
280284
#define __ptrauth_swift_runtime_function_entry_with_key(__key)
281285
#define __ptrauth_swift_runtime_function_entry_strip(__fn) (__fn)

stdlib/public/SwiftShims/HeapObject.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ typedef struct HeapMetadata HeapMetadata;
3535
typedef struct HeapObject HeapObject;
3636
#endif
3737

38+
#if !defined(__swift__) && __has_feature(ptrauth_calls)
39+
#include <ptrauth.h>
40+
#endif
41+
#ifndef __ptrauth_objc_isa_pointer
42+
#define __ptrauth_objc_isa_pointer
43+
#endif
44+
3845
// The members of the HeapObject header that are not shared by a
3946
// standard Objective-C instance
4047
#define SWIFT_HEAPOBJECT_NON_OBJC_MEMBERS \
@@ -44,7 +51,7 @@ typedef struct HeapObject HeapObject;
4451
/// This must match RefCountedStructTy in IRGen.
4552
struct HeapObject {
4653
/// This is always a valid pointer to a metadata object.
47-
HeapMetadata const *metadata;
54+
HeapMetadata const *__ptrauth_objc_isa_pointer metadata;
4855

4956
SWIFT_HEAPOBJECT_NON_OBJC_MEMBERS;
5057

0 commit comments

Comments
 (0)