|
| 1 | +#include "OldABI.h" |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
| 5 | +#include <limits.h> |
| 6 | +#include <unistd.h> |
| 7 | +#include <sys/mman.h> |
| 8 | +#include <malloc/malloc.h> |
| 9 | +#include <objc/runtime.h> |
| 10 | +#include <Foundation/Foundation.h> |
| 11 | + |
| 12 | +// Implementation of ObjC classes |
| 13 | +// with bits set to mimic the pre-stable Swift ABI |
| 14 | +// and additional memory protection to detect mis-use |
| 15 | + |
| 16 | +#if __has_include(<objc/objc-internal.h>) |
| 17 | +#include <objc/objc-internal.h> |
| 18 | +#else |
| 19 | +extern "C" Class objc_initializeClassPair(Class superclass, const char *name, Class cls, Class metacls); |
| 20 | +extern "C" id _objc_rootRetain(id); |
| 21 | +extern "C" void _objc_rootRelease(id); |
| 22 | +extern "C" id _objc_rootAutorelease(id); |
| 23 | +extern "C" uintptr_t _objc_rootRetainCount(id); |
| 24 | +extern "C" bool _objc_rootTryRetain(id); |
| 25 | +extern "C" bool _objc_rootIsDeallocating(id); |
| 26 | +#endif |
| 27 | + |
| 28 | +// This class stands in for the pre-stable ABI's SwiftObject. |
| 29 | +// Stable Swift's class is named Swift._SwiftObject (but mangled). |
| 30 | + |
| 31 | +__attribute__((objc_root_class)) |
| 32 | +@interface SwiftObject { id isa; } @end |
| 33 | + |
| 34 | +@implementation SwiftObject |
| 35 | ++(void)initialize { } |
| 36 | ++(id)allocWithZone:(struct _malloc_zone_t *)zone { |
| 37 | + return class_createInstance(self, 0); |
| 38 | +} |
| 39 | ++(id)alloc { return [self allocWithZone:nil]; } |
| 40 | ++(id)class { return self; } |
| 41 | +-(id)class { return object_getClass(self); } |
| 42 | ++(id)superclass { return class_getSuperclass(self); } |
| 43 | +-(id)superclass { return class_getSuperclass([self class]); } |
| 44 | ++(BOOL)isMemberOfClass:(Class)cls { return object_getClass(self) == cls; } |
| 45 | +-(BOOL)isMemberOfClass:(Class)cls { return [self class] == cls; } |
| 46 | +-(id)self { return self; } |
| 47 | +-(BOOL)isProxy { return NO; } |
| 48 | +-(struct _malloc_zone_t *)zone { return malloc_default_zone(); } |
| 49 | +-(void)doesNotRecognizeSelector:(SEL)sel { |
| 50 | + Class cls = [self class]; |
| 51 | + fprintf(stderr, "unrecognized selector %c[%s %s]\n", |
| 52 | + class_isMetaClass(cls) ? '+' : '-', |
| 53 | + class_getName(cls), sel_getName(sel)); |
| 54 | + abort(); |
| 55 | +} |
| 56 | + |
| 57 | ++(id)retain { return self; } |
| 58 | ++(void)release { } |
| 59 | ++(id)autorelease { return self; } |
| 60 | ++(uintptr_t)retainCount { return ~(uintptr_t)0; } |
| 61 | ++(BOOL)_tryRetain { return YES; } |
| 62 | ++(BOOL)_isDeallocating { return NO; } |
| 63 | + |
| 64 | +-(id)retain { return _objc_rootRetain(self); } |
| 65 | +-(void)release { _objc_rootRelease(self); } |
| 66 | +-(id)autorelease { return _objc_rootAutorelease(self); } |
| 67 | +-(uintptr_t)retainCount { return _objc_rootRetainCount(self); } |
| 68 | +-(BOOL)_tryRetain { return _objc_rootTryRetain(self); } |
| 69 | +-(BOOL)_isDeallocating { return _objc_rootIsDeallocating(self); } |
| 70 | +-(void)dealloc { object_dispose(self); } |
| 71 | + |
| 72 | +-(BOOL)isKindOfClass:(Class)other { |
| 73 | + for (Class cls = object_getClass(self); cls; cls = class_getSuperclass(cls)) |
| 74 | + if (cls == other) return YES; |
| 75 | + return NO; |
| 76 | +} |
| 77 | ++(BOOL)isSubclassOfClass:(Class)other { |
| 78 | + for (Class cls = self; cls; cls = class_getSuperclass(cls)) |
| 79 | + if (cls == other) return YES; |
| 80 | + return NO; |
| 81 | +} |
| 82 | +-(BOOL)respondsToSelector:(SEL)sel { |
| 83 | + if (!sel) return NO; |
| 84 | + return class_respondsToSelector(object_getClass(self), sel); |
| 85 | +} |
| 86 | ++(BOOL)instancesRespondToSelector:(SEL)sel { |
| 87 | + if (!sel) return NO; |
| 88 | + return class_respondsToSelector(self, sel); |
| 89 | +} |
| 90 | + |
| 91 | +-(uintptr_t)hash { return (uintptr_t)self; } |
| 92 | +-(BOOL)isEqual:(id)other { return self == other; } |
| 93 | ++(NSString *)description { return @"FakeSwiftObject class"; } |
| 94 | +-(NSString *)description { return @"FakeSwiftObject instance"; } |
| 95 | +-(NSString *)debugDescription { return [self description]; } |
| 96 | + |
| 97 | +- (BOOL)isNSArray__ { return NO; } |
| 98 | +- (BOOL)isNSCFConstantString__ { return NO; } |
| 99 | +- (BOOL)isNSData__ { return NO; } |
| 100 | +- (BOOL)isNSDate__ { return NO; } |
| 101 | +- (BOOL)isNSDictionary__ { return NO; } |
| 102 | +- (BOOL)isNSObject__ { return NO; } |
| 103 | +- (BOOL)isNSOrderedSet__ { return NO; } |
| 104 | +- (BOOL)isNSNumber__ { return NO; } |
| 105 | +- (BOOL)isNSSet__ { return NO; } |
| 106 | +- (BOOL)isNSString__ { return NO; } |
| 107 | +- (BOOL)isNSTimeZone__ { return NO; } |
| 108 | +- (BOOL)isNSValue__ { return NO; } |
| 109 | + |
| 110 | +@end |
| 111 | + |
| 112 | + |
| 113 | +static char *AllocTailGuardedPointer(size_t size) { |
| 114 | + // Round up to page boundary. |
| 115 | + size_t writeableSize = (size + PAGE_MAX_SIZE - 1) & ~(PAGE_MAX_SIZE - 1); |
| 116 | + |
| 117 | + // Allocate writeable memory plus one guard page. |
| 118 | + char *writeableBuffer = (char *)mmap(0, writeableSize + PAGE_MAX_SIZE, |
| 119 | + PROT_READ | PROT_WRITE, |
| 120 | + MAP_ANON | MAP_PRIVATE, -1, 0); |
| 121 | + if (writeableBuffer == MAP_FAILED) abort(); |
| 122 | + |
| 123 | + // Mark the guard page inaccessible. |
| 124 | + mprotect(writeableBuffer + writeableSize, PAGE_MAX_SIZE, 0); |
| 125 | + |
| 126 | + // Scribble on the prefix. |
| 127 | + memset(writeableBuffer, 0x55, writeableSize - size); |
| 128 | + |
| 129 | + // Return the address just before the guard page. |
| 130 | + // FIXME: this doesn't handle alignment properly, |
| 131 | + // but we don't need to for this test's usage. |
| 132 | + return writeableBuffer + writeableSize - size; |
| 133 | +} |
| 134 | + |
| 135 | +static Class CreateOldABISubclass(Class supercls, const char *name) { |
| 136 | + // Allocate class and metaclass in tail-guarded memory. |
| 137 | + // If the Swift runtime incorrectly tries to read Swift |
| 138 | + // metadata from this class then it'll crash. |
| 139 | + char *clsbuf = AllocTailGuardedPointer(5*sizeof(uintptr_t)); |
| 140 | + char *metabuf = AllocTailGuardedPointer(5*sizeof(uintptr_t)); |
| 141 | + Class result = objc_initializeClassPair(supercls, name, |
| 142 | + (Class)clsbuf, (Class)metabuf); |
| 143 | + |
| 144 | + // Set the old is-Swift bit in the class. |
| 145 | + uintptr_t *words = (uintptr_t *)clsbuf; |
| 146 | + words[4] |= 1; |
| 147 | + |
| 148 | + return result; |
| 149 | +} |
| 150 | + |
| 151 | +static Class FakeOldABIClass; |
| 152 | + |
| 153 | +__attribute__((constructor)) |
| 154 | +static void initialize(void) { |
| 155 | + FakeOldABIClass = CreateOldABISubclass([SwiftObject class], |
| 156 | + "_TtC6OldABI8Subclass"); |
| 157 | +} |
| 158 | + |
| 159 | +bool CanTestOldABI() { |
| 160 | + // These tests don't work until the stable ABI is using its designed bit. |
| 161 | + // This check can be removed after SWIFT_DARWIN_ENABLE_STABLE_ABI_BIT |
| 162 | + // is set everywhere. |
| 163 | + Class cls = objc_getClass("_TtCs19__EmptyArrayStorage"); |
| 164 | + if (!cls) abort(); |
| 165 | + uintptr_t *words = (uintptr_t *)cls; |
| 166 | + if ((words[4] & 3) != 2) return false; // wrong stable is-Swift bit |
| 167 | + return true; |
| 168 | +} |
| 169 | + |
| 170 | +id AllocOldABIObject() { |
| 171 | + return [FakeOldABIClass alloc]; |
| 172 | +} |
0 commit comments