blob: f737e588aaf748d4426bc0c5e1884a5055b961a0 [file] [log] [blame]
Thomas Van Lenten30650d82015-05-01 08:57:16 -04001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
Thomas Van Lenten30650d82015-05-01 08:57:16 -04003//
Joshua Haberman44bd65b2023-09-08 17:43:14 -07004// 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 Lenten30650d82015-05-01 08:57:16 -04007
8#import "GPBRootObject_PackagePrivate.h"
9
10#import <objc/runtime.h>
Thomas Van Lentend7d780c2022-10-06 17:14:26 -040011#import <os/lock.h>
Thomas Van Lenten30650d82015-05-01 08:57:16 -040012
13#import <CoreFoundation/CoreFoundation.h>
14
15#import "GPBDescriptor.h"
Thomas Van Lenten8c889572015-06-16 16:45:14 -040016#import "GPBExtensionRegistry.h"
Thomas Van Lenten30650d82015-05-01 08:57:16 -040017#import "GPBUtilities_PackagePrivate.h"
18
19@interface GPBExtensionDescriptor (GPBRootObject)
20// Get singletonName as a c string.
21- (const char *)singletonNameC;
22@end
23
Dave MacLachlan37a66722017-11-14 15:16:04 -080024// 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 Lenten30650d82015-05-01 08:57:16 -040032@implementation GPBRootObject
33
34// Taken from http://www.burtleburtle.net/bob/hash/doobs.html
35// Public Domain
36static 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 Lenten2fb33b82022-09-20 09:14:32 -040054static const void *GPBRootExtensionKeyRetain(__unused CFAllocatorRef allocator, const void *value) {
Thomas Van Lenten30650d82015-05-01 08:57:16 -040055 return value;
56}
57
Thomas Van Lenten2fb33b82022-09-20 09:14:32 -040058static void GPBRootExtensionKeyRelease(__unused CFAllocatorRef allocator,
59 __unused const void *value) {}
Thomas Van Lenten30650d82015-05-01 08:57:16 -040060
61static CFStringRef GPBRootExtensionCopyKeyDescription(const void *value) {
62 const char *key = (const char *)value;
Thomas Van Lenten189f6322022-09-19 17:21:13 -040063 return CFStringCreateWithCString(kCFAllocatorDefault, key, kCFStringEncodingUTF8);
Thomas Van Lenten30650d82015-05-01 08:57:16 -040064}
65
Thomas Van Lenten189f6322022-09-19 17:21:13 -040066static Boolean GPBRootExtensionKeyEqual(const void *value1, const void *value2) {
Thomas Van Lenten30650d82015-05-01 08:57:16 -040067 const char *key1 = (const char *)value1;
68 const char *key2 = (const char *)value2;
69 return strcmp(key1, key2) == 0;
70}
71
72static CFHashCode GPBRootExtensionKeyHash(const void *value) {
73 const char *key = (const char *)value;
74 return jenkins_one_at_a_time_hash(key);
75}
76
dmaclach890377d2022-10-11 08:41:43 -070077// Long ago, this was an OSSpinLock, but then it came to light that there were issues for that on
Thomas Van Lentend7d780c2022-10-06 17:14:26 -040078// iOS:
Thomas Van Lentend6590d62015-12-17 14:35:44 -050079// http://mjtsai.com/blog/2015/12/16/osspinlock-is-unsafe/
80// https://lists.swift.org/pipermail/swift-dev/Week-of-Mon-20151214/000372.html
dmaclach890377d2022-10-11 08:41:43 -070081// It was changed to a dispatch_semaphore_t, but that has potential for priority inversion issues.
Protobuf Team Bot46d11452022-11-02 13:42:04 -070082// The minOS versions are now high enough that os_unfair_lock can be used, and should provide
dmaclach890377d2022-10-11 08:41:43 -070083// all the support we need. For more information in the concurrency/locking space see:
Thomas Van Lentend7d780c2022-10-06 17:14:26 -040084// 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/
87static os_unfair_lock gExtensionSingletonDictionaryLock = OS_UNFAIR_LOCK_INIT;
Thomas Van Lenten30650d82015-05-01 08:57:16 -040088static CFMutableDictionaryRef gExtensionSingletonDictionary = NULL;
Thomas Van Lenten8c889572015-06-16 16:45:14 -040089static GPBExtensionRegistry *gDefaultExtensionRegistry = NULL;
Thomas Van Lenten30650d82015-05-01 08:57:16 -040090
91+ (void)initialize {
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -040092 // Ensure the global is started up.
Thomas Van Lenten30650d82015-05-01 08:57:16 -040093 if (!gExtensionSingletonDictionary) {
94 CFDictionaryKeyCallBacks keyCallBacks = {
Thomas Van Lenten189f6322022-09-19 17:21:13 -040095 // See description above for reason for using custom dictionary.
96 0,
97 GPBRootExtensionKeyRetain,
98 GPBRootExtensionKeyRelease,
99 GPBRootExtensionCopyKeyDescription,
100 GPBRootExtensionKeyEqual,
101 GPBRootExtensionKeyHash,
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400102 };
Thomas Van Lenten189f6322022-09-19 17:21:13 -0400103 gExtensionSingletonDictionary = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &keyCallBacks,
104 &kCFTypeDictionaryValueCallBacks);
Thomas Van Lenten8c889572015-06-16 16:45:14 -0400105 gDefaultExtensionRegistry = [[GPBExtensionRegistry alloc] init];
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400106 }
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -0400107
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 Lenten30650d82015-05-01 08:57:16 -0400114}
115
116+ (GPBExtensionRegistry *)extensionRegistry {
117 // Is overridden in all the subclasses that provide extensions to provide the
118 // per class one.
Thomas Van Lenten8c889572015-06-16 16:45:14 -0400119 return gDefaultExtensionRegistry;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400120}
121
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400122+ (void)globallyRegisterExtension:(GPBExtensionDescriptor *)field {
123 const char *key = [field singletonNameC];
Thomas Van Lentend7d780c2022-10-06 17:14:26 -0400124 os_unfair_lock_lock(&gExtensionSingletonDictionaryLock);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400125 CFDictionarySetValue(gExtensionSingletonDictionary, key, field);
Thomas Van Lentend7d780c2022-10-06 17:14:26 -0400126 os_unfair_lock_unlock(&gExtensionSingletonDictionaryLock);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400127}
128
Thomas Van Lentend846b0b2015-06-08 16:24:57 -0400129static id ExtensionForName(id self, SEL _cmd) {
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400130 // 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 Lenten30650d82015-05-01 08:57:16 -0400133 const char *selName = sel_getName(_cmd);
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -0400134 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 Lenten30650d82015-05-01 08:57:16 -0400150 size_t classNameLen = strlen(className);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400151 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 Lentend6590d62015-12-17 14:35:44 -0500156
157 // NOTE: Even though this method is called from another C function,
Thomas Van Lentend7d780c2022-10-06 17:14:26 -0400158 // gExtensionSingletonDictionaryLock and gExtensionSingletonDictionary
Thomas Van Lentend6590d62015-12-17 14:35:44 -0500159 // 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 Lentend7d780c2022-10-06 17:14:26 -0400165 os_unfair_lock_lock(&gExtensionSingletonDictionaryLock);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400166 id extension = (id)CFDictionaryGetValue(gExtensionSingletonDictionary, key);
Thomas Van Lenten2d1c5e22017-03-02 14:50:10 -0500167 // 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 Lentend7d780c2022-10-06 17:14:26 -0400171 os_unfair_lock_unlock(&gExtensionSingletonDictionaryLock);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400172 return extension;
173}
174
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -0400175BOOL GPBResolveExtensionClassMethod(Class self, SEL sel) {
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400176 // 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 Lenten30650d82015-05-01 08:57:16 -0400180 // 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 Lenten189f6322022-09-19 17:21:13 -0400187 const char *encoding = GPBMessageEncodingForSelector(@selector(getClassValue), NO);
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400188 Class metaClass = objc_getMetaClass(class_getName(self));
Thomas Van Lenten2fb33b82022-09-20 09:14:32 -0400189 IMP imp = imp_implementationWithBlock(^(__unused id obj) {
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400190 return extension;
191 });
Thomas Van Lenten2d1c5e22017-03-02 14:50:10 -0500192 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 Lenten1dcc3292015-05-21 17:14:52 -0400201 }
Thomas Van Lenten2d1c5e22017-03-02 14:50:10 -0500202 return methodAdded;
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -0400203 }
204 return NO;
205}
206
Thomas Van Lenten1dcc3292015-05-21 17:14:52 -0400207+ (BOOL)resolveClassMethod:(SEL)sel {
208 if (GPBResolveExtensionClassMethod(self, sel)) {
209 return YES;
Thomas Van Lenten30650d82015-05-01 08:57:16 -0400210 }
211 return [super resolveClassMethod:sel];
212}
213
214@end