initializeVersusWeak.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // TEST_CONFIG MEM=arc
  2. // TEST_CFLAGS -framework Foundation
  3. // Problem: If weak reference operations provoke +initialize, the runtime
  4. // can deadlock (recursive weak lock, or lock inversion between weak lock
  5. // and +initialize lock).
  6. // Solution: object_setClass() and objc_storeWeak() perform +initialize
  7. // if needed so that no weakly-referenced object can ever have an
  8. // un-+initialized isa.
  9. #include <Foundation/Foundation.h>
  10. #include <objc/objc-internal.h>
  11. #include "test.h"
  12. #pragma clang diagnostic ignored "-Warc-unsafe-retained-assign"
  13. // This is StripedMap's pointer hash
  14. #if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR
  15. enum { StripeCount = 8 };
  16. #else
  17. enum { StripeCount = 64 };
  18. #endif
  19. uintptr_t stripehash(id obj) {
  20. uintptr_t addr = (uintptr_t)obj;
  21. return ((addr >> 4) ^ (addr >> 9)) % StripeCount;
  22. }
  23. bool sameAlignment(id o1, id o2)
  24. {
  25. return stripehash(o1) == stripehash(o2);
  26. }
  27. // Return a new non-tagged object that uses the same striped weak locks as `obj`
  28. NSObject *newAlignedObject(id obj)
  29. {
  30. // Use immutable arrays because their contents are stored inline,
  31. // which prevents Guard Malloc from using the same alignment for all of them
  32. NSArray *result = [NSArray new];
  33. while (!sameAlignment(obj, result)) {
  34. result = [result arrayByAddingObject:result];
  35. }
  36. return result;
  37. }
  38. __weak NSObject *weak1;
  39. __weak NSObject *weak2;
  40. NSObject *strong2;
  41. @interface A : NSObject @end
  42. @implementation A
  43. +(void)initialize {
  44. weak2 = strong2; // weak store #2
  45. strong2 = nil;
  46. }
  47. @end
  48. void testA()
  49. {
  50. // Weak store #1 provokes +initialize which performs weak store #2.
  51. // Solution: weak store #1 runs +initialize if needed
  52. // without holding locks.
  53. @autoreleasepool {
  54. A *obj = [A new];
  55. strong2 = newAlignedObject(obj);
  56. [obj addObserver:obj forKeyPath:@"foo" options:0 context:0];
  57. weak1 = obj; // weak store #1
  58. [obj removeObserver:obj forKeyPath:@"foo"];
  59. obj = nil;
  60. }
  61. }
  62. __weak NSObject *weak3;
  63. __weak NSObject *weak4;
  64. NSObject *strong4;
  65. @interface B : NSObject @end
  66. @implementation B
  67. +(void)initialize {
  68. weak4 = strong4; // weak store #4
  69. strong4 = nil;
  70. }
  71. @end
  72. void testB()
  73. {
  74. // Weak load #3 provokes +initialize which performs weak store #4.
  75. // Solution: object_setClass() runs +initialize if needed
  76. // without holding locks.
  77. @autoreleasepool {
  78. B *obj = [B new];
  79. strong4 = newAlignedObject(obj);
  80. weak3 = obj;
  81. [obj addObserver:obj forKeyPath:@"foo" options:0 context:0];
  82. [weak3 self]; // weak load #3
  83. [obj removeObserver:obj forKeyPath:@"foo"];
  84. obj = nil;
  85. }
  86. }
  87. __weak id weak5;
  88. @interface C : NSObject @end
  89. @implementation C
  90. +(void)initialize {
  91. weak5 = [self new];
  92. }
  93. @end
  94. void testC()
  95. {
  96. // +initialize performs a weak store of itself.
  97. // Make sure the retry in objc_storeWeak() doesn't spin.
  98. @autoreleasepool {
  99. [C self];
  100. }
  101. }
  102. __weak id weak6;
  103. NSObject *strong6;
  104. semaphore_t Dgo;
  105. semaphore_t Ddone;
  106. void *Dthread(void *arg __unused)
  107. {
  108. @autoreleasepool {
  109. semaphore_wait(Dgo);
  110. for (int i = 0; i < 1000; i++) {
  111. id x = weak6;
  112. testassert(x == strong6);
  113. }
  114. return nil;
  115. }
  116. }
  117. @interface D : NSObject @end
  118. @implementation D
  119. +(void)initialize {
  120. strong6 = [self new];
  121. weak6 = strong6;
  122. semaphore_signal(Dgo);
  123. for (int i = 0; i < 1000; i++) {
  124. id x = weak6;
  125. testassert(x == strong6);
  126. }
  127. }
  128. @end
  129. void testD()
  130. {
  131. // +initialize performs a weak store of itself, then another thread
  132. // tries to load that weak variable before +initialize completes.
  133. // Deadlock occurs if the +initialize thread tries to acquire the
  134. // sidetable lock for another operation and the second thread holds
  135. // the sidetable lock while waiting for +initialize.
  136. @autoreleasepool {
  137. semaphore_create(mach_task_self(), &Dgo, 0, 0);
  138. semaphore_create(mach_task_self(), &Ddone, 0, 0);
  139. pthread_t th;
  140. pthread_create(&th, nil, Dthread, nil);
  141. [D self];
  142. pthread_join(th, nil);
  143. }
  144. }
  145. int main()
  146. {
  147. if (is_guardmalloc() && getenv("MALLOC_PROTECT_BEFORE")) {
  148. testwarn("fixme malloc guard before breaks this with debug libobjc");
  149. }
  150. else {
  151. alarm(10); // replace hangs with crashes
  152. testA();
  153. testB();
  154. testC();
  155. testD();
  156. }
  157. succeed(__FILE__);
  158. }