You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
9.2 KiB

6 years ago
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import "GPBRootObject_PackagePrivate.h"
  31. #import <objc/runtime.h>
  32. #import <CoreFoundation/CoreFoundation.h>
  33. #import "GPBDescriptor.h"
  34. #import "GPBExtensionRegistry.h"
  35. #import "GPBUtilities_PackagePrivate.h"
  36. @interface GPBExtensionDescriptor (GPBRootObject)
  37. // Get singletonName as a c string.
  38. - (const char *)singletonNameC;
  39. @end
  40. @implementation GPBRootObject
  41. // Taken from http://www.burtleburtle.net/bob/hash/doobs.html
  42. // Public Domain
  43. static uint32_t jenkins_one_at_a_time_hash(const char *key) {
  44. uint32_t hash = 0;
  45. for (uint32_t i = 0; key[i] != '\0'; ++i) {
  46. hash += key[i];
  47. hash += (hash << 10);
  48. hash ^= (hash >> 6);
  49. }
  50. hash += (hash << 3);
  51. hash ^= (hash >> 11);
  52. hash += (hash << 15);
  53. return hash;
  54. }
  55. // Key methods for our custom CFDictionary.
  56. // Note that the dictionary lasts for the lifetime of our app, so no need
  57. // to worry about deallocation. All of the items are added to it at
  58. // startup, and so the keys don't need to be retained/released.
  59. // Keys are NULL terminated char *.
  60. static const void *GPBRootExtensionKeyRetain(CFAllocatorRef allocator,
  61. const void *value) {
  62. #pragma unused(allocator)
  63. return value;
  64. }
  65. static void GPBRootExtensionKeyRelease(CFAllocatorRef allocator,
  66. const void *value) {
  67. #pragma unused(allocator)
  68. #pragma unused(value)
  69. }
  70. static CFStringRef GPBRootExtensionCopyKeyDescription(const void *value) {
  71. const char *key = (const char *)value;
  72. return CFStringCreateWithCString(kCFAllocatorDefault, key,
  73. kCFStringEncodingUTF8);
  74. }
  75. static Boolean GPBRootExtensionKeyEqual(const void *value1,
  76. const void *value2) {
  77. const char *key1 = (const char *)value1;
  78. const char *key2 = (const char *)value2;
  79. return strcmp(key1, key2) == 0;
  80. }
  81. static CFHashCode GPBRootExtensionKeyHash(const void *value) {
  82. const char *key = (const char *)value;
  83. return jenkins_one_at_a_time_hash(key);
  84. }
  85. // NOTE: OSSpinLock may seem like a good fit here but Apple engineers have
  86. // pointed out that they are vulnerable to live locking on iOS in cases of
  87. // priority inversion:
  88. // http://mjtsai.com/blog/2015/12/16/osspinlock-is-unsafe/
  89. // https://lists.swift.org/pipermail/swift-dev/Week-of-Mon-20151214/000372.html
  90. static dispatch_semaphore_t gExtensionSingletonDictionarySemaphore;
  91. static CFMutableDictionaryRef gExtensionSingletonDictionary = NULL;
  92. static GPBExtensionRegistry *gDefaultExtensionRegistry = NULL;
  93. + (void)initialize {
  94. // Ensure the global is started up.
  95. if (!gExtensionSingletonDictionary) {
  96. gExtensionSingletonDictionarySemaphore = dispatch_semaphore_create(1);
  97. CFDictionaryKeyCallBacks keyCallBacks = {
  98. // See description above for reason for using custom dictionary.
  99. 0,
  100. GPBRootExtensionKeyRetain,
  101. GPBRootExtensionKeyRelease,
  102. GPBRootExtensionCopyKeyDescription,
  103. GPBRootExtensionKeyEqual,
  104. GPBRootExtensionKeyHash,
  105. };
  106. gExtensionSingletonDictionary =
  107. CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &keyCallBacks,
  108. &kCFTypeDictionaryValueCallBacks);
  109. gDefaultExtensionRegistry = [[GPBExtensionRegistry alloc] init];
  110. }
  111. if ([self superclass] == [GPBRootObject class]) {
  112. // This is here to start up all the per file "Root" subclasses.
  113. // This must be done in initialize to enforce thread safety of start up of
  114. // the protocol buffer library.
  115. [self extensionRegistry];
  116. }
  117. }
  118. + (GPBExtensionRegistry *)extensionRegistry {
  119. // Is overridden in all the subclasses that provide extensions to provide the
  120. // per class one.
  121. return gDefaultExtensionRegistry;
  122. }
  123. + (void)globallyRegisterExtension:(GPBExtensionDescriptor *)field {
  124. const char *key = [field singletonNameC];
  125. dispatch_semaphore_wait(gExtensionSingletonDictionarySemaphore,
  126. DISPATCH_TIME_FOREVER);
  127. CFDictionarySetValue(gExtensionSingletonDictionary, key, field);
  128. dispatch_semaphore_signal(gExtensionSingletonDictionarySemaphore);
  129. }
  130. static id ExtensionForName(id self, SEL _cmd) {
  131. // Really fast way of doing "classname_selName".
  132. // This came up as a hotspot (creation of NSString *) when accessing a
  133. // lot of extensions.
  134. const char *selName = sel_getName(_cmd);
  135. if (selName[0] == '_') {
  136. return nil; // Apple internal selector.
  137. }
  138. size_t selNameLen = 0;
  139. while (1) {
  140. char c = selName[selNameLen];
  141. if (c == '\0') { // String end.
  142. break;
  143. }
  144. if (c == ':') {
  145. return nil; // Selector took an arg, not one of the runtime methods.
  146. }
  147. ++selNameLen;
  148. }
  149. const char *className = class_getName(self);
  150. size_t classNameLen = strlen(className);
  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';
  156. // NOTE: Even though this method is called from another C function,
  157. // gExtensionSingletonDictionarySemaphore and gExtensionSingletonDictionary
  158. // will always be initialized. This is because this call flow is just to
  159. // lookup the Extension, meaning the code is calling an Extension class
  160. // message on a Message or Root class. This guarantees that the class was
  161. // initialized and Message classes ensure their Root was also initialized.
  162. NSAssert(gExtensionSingletonDictionary, @"Startup order broken!");
  163. dispatch_semaphore_wait(gExtensionSingletonDictionarySemaphore,
  164. DISPATCH_TIME_FOREVER);
  165. id extension = (id)CFDictionaryGetValue(gExtensionSingletonDictionary, key);
  166. // We can't remove the key from the dictionary here (as an optimization),
  167. // two threads could have gone into +resolveClassMethod: for the same method,
  168. // and ended up here; there's no way to ensure both return YES without letting
  169. // both try to wire in the method.
  170. dispatch_semaphore_signal(gExtensionSingletonDictionarySemaphore);
  171. return extension;
  172. }
  173. BOOL GPBResolveExtensionClassMethod(Class self, SEL sel) {
  174. // Another option would be to register the extensions with the class at
  175. // globallyRegisterExtension:
  176. // Timing the two solutions, this solution turned out to be much faster
  177. // and reduced startup time, and runtime memory.
  178. // The advantage to globallyRegisterExtension is that it would reduce the
  179. // size of the protos somewhat because the singletonNameC wouldn't need
  180. // to include the class name. For a class with a lot of extensions it
  181. // can add up. You could also significantly reduce the code complexity of this
  182. // file.
  183. id extension = ExtensionForName(self, sel);
  184. if (extension != nil) {
  185. const char *encoding =
  186. GPBMessageEncodingForSelector(@selector(getClassValue), NO);
  187. Class metaClass = objc_getMetaClass(class_getName(self));
  188. IMP imp = imp_implementationWithBlock(^(id obj) {
  189. #pragma unused(obj)
  190. return extension;
  191. });
  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);
  201. }
  202. return methodAdded;
  203. }
  204. return NO;
  205. }
  206. + (BOOL)resolveClassMethod:(SEL)sel {
  207. if (GPBResolveExtensionClassMethod(self, sel)) {
  208. return YES;
  209. }
  210. return [super resolveClassMethod:sel];
  211. }
  212. @end