Skip to content

Commit 14a20ee

Browse files
authored
Merge pull request #24616 from mikeash/hardcode-swift-isamask
[Runtime] Use a #define for the value of swift_isaMask.
2 parents fca1128 + b85c600 commit 14a20ee

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

stdlib/public/runtime/Private.h

+23-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include "swift/Runtime/Metadata.h"
2323
#include "llvm/Support/Compiler.h"
2424

25+
#if defined(__APPLE__) && defined(__MACH__)
26+
#include <TargetConditionals.h>
27+
#endif
28+
2529
// Opaque ISAs need to use object_getClass which is in runtime.h
2630
#if SWIFT_HAS_OPAQUE_ISAS
2731
#include <objc/runtime.h>
@@ -84,6 +88,24 @@ class TypeInfo {
8488
#if SWIFT_HAS_ISA_MASKING
8589
SWIFT_RUNTIME_EXPORT
8690
uintptr_t swift_isaMask;
91+
92+
// Hardcode the mask. We have our own copy of the value, as it's hard to work
93+
// out the proper includes from libobjc. The values MUST match the ones from
94+
// libobjc. Debug builds check these values against objc_debug_isa_class_mask
95+
// from libobjc.
96+
# if TARGET_OS_SIMULATOR
97+
// Simulators don't currently use isa masking, but we still want to emit
98+
// swift_isaMask and the corresponding code in case that changes. libobjc's
99+
// mask has the bottom bits clear to include pointer alignment, match that
100+
// value here.
101+
# define SWIFT_ISA_MASK 0xfffffffffffffff8ULL
102+
# elif __arm64__
103+
# define SWIFT_ISA_MASK 0x0000000ffffffff8ULL
104+
# elif __x86_64__
105+
# define SWIFT_ISA_MASK 0x00007ffffffffff8ULL
106+
# else
107+
# error Unknown architecture for masked isa.
108+
# endif
87109
#endif
88110

89111
#if SWIFT_OBJC_INTEROP
@@ -134,7 +156,7 @@ class TypeInfo {
134156

135157
#if SWIFT_HAS_ISA_MASKING
136158
// Apply the mask.
137-
bits &= swift_isaMask;
159+
bits &= SWIFT_ISA_MASK;
138160
#endif
139161

140162
// The result is a class pointer.

stdlib/public/runtime/SwiftObject.mm

+7-12
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,7 @@
5858
OBJC_EXPORT __attribute__((__weak_import__))
5959
const uintptr_t objc_debug_isa_class_mask;
6060

61-
static uintptr_t computeISAMask() {
62-
// The versions of the Objective-C runtime which use non-pointer
63-
// ISAs also export this symbol.
64-
if (auto runtimeSymbol = &objc_debug_isa_class_mask)
65-
return *runtimeSymbol;
66-
return ~uintptr_t(0);
67-
}
68-
69-
SWIFT_ALLOWED_RUNTIME_GLOBAL_CTOR_BEGIN
70-
uintptr_t swift::swift_isaMask = computeISAMask();
71-
SWIFT_ALLOWED_RUNTIME_GLOBAL_CTOR_END
61+
uintptr_t swift::swift_isaMask = SWIFT_ISA_MASK;
7262
#endif
7363

7464
const ClassMetadata *swift::_swift_getClass(const void *object) {
@@ -204,7 +194,12 @@ Class _swift_classOfObjCHeapObject(OpaqueValue *value) {
204194

205195

206196
@implementation SwiftObject
207-
+ (void)initialize {}
197+
+ (void)initialize {
198+
#if SWIFT_HAS_ISA_MASKING
199+
assert(&objc_debug_isa_class_mask);
200+
assert(objc_debug_isa_class_mask == SWIFT_ISA_MASK);
201+
#endif
202+
}
208203

209204
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
210205
assert(zone == nullptr);

0 commit comments

Comments
 (0)