Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 3 | // |
Joshua Haberman | 44bd65b | 2023-09-08 17:43:14 -0700 | [diff] [blame] | 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file or at |
| 6 | // https://developers.google.com/open-source/licenses/bsd |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 7 | |
| 8 | #import "GPBRootObject_PackagePrivate.h" |
| 9 | |
| 10 | #import <objc/runtime.h> |
Thomas Van Lenten | d7d780c | 2022-10-06 17:14:26 -0400 | [diff] [blame] | 11 | #import <os/lock.h> |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 12 | |
| 13 | #import <CoreFoundation/CoreFoundation.h> |
| 14 | |
| 15 | #import "GPBDescriptor.h" |
Thomas Van Lenten | 8c88957 | 2015-06-16 16:45:14 -0400 | [diff] [blame] | 16 | #import "GPBExtensionRegistry.h" |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 17 | #import "GPBUtilities_PackagePrivate.h" |
| 18 | |
| 19 | @interface GPBExtensionDescriptor (GPBRootObject) |
| 20 | // Get singletonName as a c string. |
| 21 | - (const char *)singletonNameC; |
| 22 | @end |
| 23 | |
Dave MacLachlan | 37a6672 | 2017-11-14 15:16:04 -0800 | [diff] [blame] | 24 | // We need some object to conform to the MessageSignatureProtocol to make sure |
| 25 | // the selectors in it are recorded in our Objective C runtime information. |
| 26 | // GPBMessage is arguably the more "obvious" choice, but given that all messages |
| 27 | // inherit from GPBMessage, conflicts seem likely, so we are using GPBRootObject |
| 28 | // instead. |
| 29 | @interface GPBRootObject () <GPBMessageSignatureProtocol> |
| 30 | @end |
| 31 | |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 32 | @implementation GPBRootObject |
| 33 | |
| 34 | // Taken from http://www.burtleburtle.net/bob/hash/doobs.html |
| 35 | // Public Domain |
| 36 | static uint32_t jenkins_one_at_a_time_hash(const char *key) { |
| 37 | uint32_t hash = 0; |
| 38 | for (uint32_t i = 0; key[i] != '\0'; ++i) { |
| 39 | hash += key[i]; |
| 40 | hash += (hash << 10); |
| 41 | hash ^= (hash >> 6); |
| 42 | } |
| 43 | hash += (hash << 3); |
| 44 | hash ^= (hash >> 11); |
| 45 | hash += (hash << 15); |
| 46 | return hash; |
| 47 | } |
| 48 | |
| 49 | // Key methods for our custom CFDictionary. |
| 50 | // Note that the dictionary lasts for the lifetime of our app, so no need |
| 51 | // to worry about deallocation. All of the items are added to it at |
| 52 | // startup, and so the keys don't need to be retained/released. |
| 53 | // Keys are NULL terminated char *. |
Thomas Van Lenten | 2fb33b8 | 2022-09-20 09:14:32 -0400 | [diff] [blame] | 54 | static const void *GPBRootExtensionKeyRetain(__unused CFAllocatorRef allocator, const void *value) { |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 55 | return value; |
| 56 | } |
| 57 | |
Thomas Van Lenten | 2fb33b8 | 2022-09-20 09:14:32 -0400 | [diff] [blame] | 58 | static void GPBRootExtensionKeyRelease(__unused CFAllocatorRef allocator, |
| 59 | __unused const void *value) {} |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 60 | |
| 61 | static CFStringRef GPBRootExtensionCopyKeyDescription(const void *value) { |
| 62 | const char *key = (const char *)value; |
Thomas Van Lenten | 189f632 | 2022-09-19 17:21:13 -0400 | [diff] [blame] | 63 | return CFStringCreateWithCString(kCFAllocatorDefault, key, kCFStringEncodingUTF8); |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Thomas Van Lenten | 189f632 | 2022-09-19 17:21:13 -0400 | [diff] [blame] | 66 | static Boolean GPBRootExtensionKeyEqual(const void *value1, const void *value2) { |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 67 | const char *key1 = (const char *)value1; |
| 68 | const char *key2 = (const char *)value2; |
| 69 | return strcmp(key1, key2) == 0; |
| 70 | } |
| 71 | |
| 72 | static CFHashCode GPBRootExtensionKeyHash(const void *value) { |
| 73 | const char *key = (const char *)value; |
| 74 | return jenkins_one_at_a_time_hash(key); |
| 75 | } |
| 76 | |
dmaclach | 890377d | 2022-10-11 08:41:43 -0700 | [diff] [blame] | 77 | // Long ago, this was an OSSpinLock, but then it came to light that there were issues for that on |
Thomas Van Lenten | d7d780c | 2022-10-06 17:14:26 -0400 | [diff] [blame] | 78 | // iOS: |
Thomas Van Lenten | d6590d6 | 2015-12-17 14:35:44 -0500 | [diff] [blame] | 79 | // http://mjtsai.com/blog/2015/12/16/osspinlock-is-unsafe/ |
| 80 | // https://lists.swift.org/pipermail/swift-dev/Week-of-Mon-20151214/000372.html |
dmaclach | 890377d | 2022-10-11 08:41:43 -0700 | [diff] [blame] | 81 | // It was changed to a dispatch_semaphore_t, but that has potential for priority inversion issues. |
Protobuf Team Bot | 46d1145 | 2022-11-02 13:42:04 -0700 | [diff] [blame] | 82 | // The minOS versions are now high enough that os_unfair_lock can be used, and should provide |
dmaclach | 890377d | 2022-10-11 08:41:43 -0700 | [diff] [blame] | 83 | // all the support we need. For more information in the concurrency/locking space see: |
Thomas Van Lenten | d7d780c | 2022-10-06 17:14:26 -0400 | [diff] [blame] | 84 | // https://gist.github.com/tclementdev/6af616354912b0347cdf6db159c37057 |
| 85 | // https://developer.apple.com/library/archive/documentation/Performance/Conceptual/EnergyGuide-iOS/PrioritizeWorkWithQoS.html |
| 86 | // https://developer.apple.com/videos/play/wwdc2017/706/ |
| 87 | static os_unfair_lock gExtensionSingletonDictionaryLock = OS_UNFAIR_LOCK_INIT; |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 88 | static CFMutableDictionaryRef gExtensionSingletonDictionary = NULL; |
Thomas Van Lenten | 8c88957 | 2015-06-16 16:45:14 -0400 | [diff] [blame] | 89 | static GPBExtensionRegistry *gDefaultExtensionRegistry = NULL; |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 90 | |
| 91 | + (void)initialize { |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 92 | // Ensure the global is started up. |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 93 | if (!gExtensionSingletonDictionary) { |
| 94 | CFDictionaryKeyCallBacks keyCallBacks = { |
Thomas Van Lenten | 189f632 | 2022-09-19 17:21:13 -0400 | [diff] [blame] | 95 | // See description above for reason for using custom dictionary. |
| 96 | 0, |
| 97 | GPBRootExtensionKeyRetain, |
| 98 | GPBRootExtensionKeyRelease, |
| 99 | GPBRootExtensionCopyKeyDescription, |
| 100 | GPBRootExtensionKeyEqual, |
| 101 | GPBRootExtensionKeyHash, |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 102 | }; |
Thomas Van Lenten | 189f632 | 2022-09-19 17:21:13 -0400 | [diff] [blame] | 103 | gExtensionSingletonDictionary = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &keyCallBacks, |
| 104 | &kCFTypeDictionaryValueCallBacks); |
Thomas Van Lenten | 8c88957 | 2015-06-16 16:45:14 -0400 | [diff] [blame] | 105 | gDefaultExtensionRegistry = [[GPBExtensionRegistry alloc] init]; |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 106 | } |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 107 | |
| 108 | if ([self superclass] == [GPBRootObject class]) { |
| 109 | // This is here to start up all the per file "Root" subclasses. |
| 110 | // This must be done in initialize to enforce thread safety of start up of |
| 111 | // the protocol buffer library. |
| 112 | [self extensionRegistry]; |
| 113 | } |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | + (GPBExtensionRegistry *)extensionRegistry { |
| 117 | // Is overridden in all the subclasses that provide extensions to provide the |
| 118 | // per class one. |
Thomas Van Lenten | 8c88957 | 2015-06-16 16:45:14 -0400 | [diff] [blame] | 119 | return gDefaultExtensionRegistry; |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 120 | } |
| 121 | |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 122 | + (void)globallyRegisterExtension:(GPBExtensionDescriptor *)field { |
| 123 | const char *key = [field singletonNameC]; |
Thomas Van Lenten | d7d780c | 2022-10-06 17:14:26 -0400 | [diff] [blame] | 124 | os_unfair_lock_lock(&gExtensionSingletonDictionaryLock); |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 125 | CFDictionarySetValue(gExtensionSingletonDictionary, key, field); |
Thomas Van Lenten | d7d780c | 2022-10-06 17:14:26 -0400 | [diff] [blame] | 126 | os_unfair_lock_unlock(&gExtensionSingletonDictionaryLock); |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 129 | static id ExtensionForName(id self, SEL _cmd) { |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 130 | // Really fast way of doing "classname_selName". |
| 131 | // This came up as a hotspot (creation of NSString *) when accessing a |
| 132 | // lot of extensions. |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 133 | const char *selName = sel_getName(_cmd); |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 134 | if (selName[0] == '_') { |
| 135 | return nil; // Apple internal selector. |
| 136 | } |
| 137 | size_t selNameLen = 0; |
| 138 | while (1) { |
| 139 | char c = selName[selNameLen]; |
| 140 | if (c == '\0') { // String end. |
| 141 | break; |
| 142 | } |
| 143 | if (c == ':') { |
| 144 | return nil; // Selector took an arg, not one of the runtime methods. |
| 145 | } |
| 146 | ++selNameLen; |
| 147 | } |
| 148 | |
| 149 | const char *className = class_getName(self); |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 150 | size_t classNameLen = strlen(className); |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 151 | char key[classNameLen + selNameLen + 2]; |
| 152 | memcpy(key, className, classNameLen); |
| 153 | key[classNameLen] = '_'; |
| 154 | memcpy(&key[classNameLen + 1], selName, selNameLen); |
| 155 | key[classNameLen + 1 + selNameLen] = '\0'; |
Thomas Van Lenten | d6590d6 | 2015-12-17 14:35:44 -0500 | [diff] [blame] | 156 | |
| 157 | // NOTE: Even though this method is called from another C function, |
Thomas Van Lenten | d7d780c | 2022-10-06 17:14:26 -0400 | [diff] [blame] | 158 | // gExtensionSingletonDictionaryLock and gExtensionSingletonDictionary |
Thomas Van Lenten | d6590d6 | 2015-12-17 14:35:44 -0500 | [diff] [blame] | 159 | // will always be initialized. This is because this call flow is just to |
| 160 | // lookup the Extension, meaning the code is calling an Extension class |
| 161 | // message on a Message or Root class. This guarantees that the class was |
| 162 | // initialized and Message classes ensure their Root was also initialized. |
| 163 | NSAssert(gExtensionSingletonDictionary, @"Startup order broken!"); |
| 164 | |
Thomas Van Lenten | d7d780c | 2022-10-06 17:14:26 -0400 | [diff] [blame] | 165 | os_unfair_lock_lock(&gExtensionSingletonDictionaryLock); |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 166 | id extension = (id)CFDictionaryGetValue(gExtensionSingletonDictionary, key); |
Thomas Van Lenten | 2d1c5e2 | 2017-03-02 14:50:10 -0500 | [diff] [blame] | 167 | // We can't remove the key from the dictionary here (as an optimization), |
| 168 | // two threads could have gone into +resolveClassMethod: for the same method, |
| 169 | // and ended up here; there's no way to ensure both return YES without letting |
| 170 | // both try to wire in the method. |
Thomas Van Lenten | d7d780c | 2022-10-06 17:14:26 -0400 | [diff] [blame] | 171 | os_unfair_lock_unlock(&gExtensionSingletonDictionaryLock); |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 172 | return extension; |
| 173 | } |
| 174 | |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 175 | BOOL GPBResolveExtensionClassMethod(Class self, SEL sel) { |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 176 | // Another option would be to register the extensions with the class at |
| 177 | // globallyRegisterExtension: |
| 178 | // Timing the two solutions, this solution turned out to be much faster |
| 179 | // and reduced startup time, and runtime memory. |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 180 | // The advantage to globallyRegisterExtension is that it would reduce the |
| 181 | // size of the protos somewhat because the singletonNameC wouldn't need |
| 182 | // to include the class name. For a class with a lot of extensions it |
| 183 | // can add up. You could also significantly reduce the code complexity of this |
| 184 | // file. |
| 185 | id extension = ExtensionForName(self, sel); |
| 186 | if (extension != nil) { |
Thomas Van Lenten | 189f632 | 2022-09-19 17:21:13 -0400 | [diff] [blame] | 187 | const char *encoding = GPBMessageEncodingForSelector(@selector(getClassValue), NO); |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 188 | Class metaClass = objc_getMetaClass(class_getName(self)); |
Thomas Van Lenten | 2fb33b8 | 2022-09-20 09:14:32 -0400 | [diff] [blame] | 189 | IMP imp = imp_implementationWithBlock(^(__unused id obj) { |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 190 | return extension; |
| 191 | }); |
Thomas Van Lenten | 2d1c5e2 | 2017-03-02 14:50:10 -0500 | [diff] [blame] | 192 | BOOL methodAdded = class_addMethod(metaClass, sel, imp, encoding); |
| 193 | // class_addMethod() is documented as also failing if the method was already |
| 194 | // added; so we check if the method is already there and return success so |
| 195 | // the method dispatch will still happen. Why would it already be added? |
| 196 | // Two threads could cause the same method to be bound at the same time, |
| 197 | // but only one will actually bind it; the other still needs to return true |
| 198 | // so things will dispatch. |
| 199 | if (!methodAdded) { |
| 200 | methodAdded = GPBClassHasSel(metaClass, sel); |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 201 | } |
Thomas Van Lenten | 2d1c5e2 | 2017-03-02 14:50:10 -0500 | [diff] [blame] | 202 | return methodAdded; |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 203 | } |
| 204 | return NO; |
| 205 | } |
| 206 | |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 207 | + (BOOL)resolveClassMethod:(SEL)sel { |
| 208 | if (GPBResolveExtensionClassMethod(self, sel)) { |
| 209 | return YES; |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 210 | } |
| 211 | return [super resolveClassMethod:sel]; |
| 212 | } |
| 213 | |
| 214 | @end |