Skip to content

Commit ae0abeb

Browse files
committed
fix(runtime): rooted scan reads the native retainCount of a class JS replaced it on
The scan resolved -retainCount with class_getMethodImplementation on the native class. A JS write to a metadata member that declares -retainCount (NSObjectProtocol.prototype.retainCount, which resolves to NSObject) replaces the native method with a JS-backed IMP, and the GC prologue then called into JS, which V8 aborts. The scan now walks the native class's method lists and takes the original a JS replacement keeps under the swizzled-prefixed alias, so native overrides below a replaced ancestor are still read.
1 parent 6cb3a5a commit ae0abeb

4 files changed

Lines changed: 114 additions & 8 deletions

File tree

‎NativeScript/runtime/WrapperLifetime.mm‎

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ void ReadConfig() {
6767
std::vector<ObjectWeakCallbackState*> releases;
6868
bool draining = false;
6969
bool drainPosted = false;
70-
// The first -retainCount implementation that is not JS-implemented, per
71-
// class (null for timers): the scan runs in a GC prologue, where JS may not
72-
// run.
70+
// The native -retainCount implementation per class (NativeRetainCountImp):
71+
// the scan runs in a GC prologue, where JS may not run.
7372
std::unordered_map<Class, IMP> retainCountImps;
7473
};
7574

@@ -149,15 +148,46 @@ bool IsTimerClass(Class cls) {
149148
return false;
150149
}
151150

152-
// Timers read as 0, as in ObjectManager's ownership checks: the retain count
153-
// of an uninitialized NSTimer (NSTimer.alloc()) can hang.
151+
// The -retainCount the native class of `cls` answers with, never one JS
152+
// implements: JS subclasses are skipped, and a class whose -retainCount JS
153+
// replaced keeps the original under the swizzled-prefixed alias. Read from the
154+
// method lists of each class, so an alias inherited from a replaced ancestor
155+
// never hides a native override below it. Null for timers, as in
156+
// ObjectManager's ownership checks: the retain count of an uninitialized
157+
// NSTimer (NSTimer.alloc()) can hang.
158+
IMP NativeRetainCountImp(Class cls) {
159+
if (IsTimerClass(cls)) {
160+
return nullptr;
161+
}
162+
SEL selector = @selector(retainCount);
163+
static SEL original = sel_registerName((Constants::SwizzledPrefix + "retainCount").c_str());
164+
for (Class c = ClassBuilder::FindNativeClass(cls); c != nil; c = class_getSuperclass(c)) {
165+
unsigned int count = 0;
166+
Method* methods = class_copyMethodList(c, &count);
167+
IMP own = nullptr;
168+
IMP kept = nullptr;
169+
for (unsigned int i = 0; i < count; i++) {
170+
SEL name = method_getName(methods[i]);
171+
if (name == original) {
172+
kept = method_getImplementation(methods[i]);
173+
} else if (name == selector) {
174+
own = method_getImplementation(methods[i]);
175+
}
176+
}
177+
free(methods);
178+
if (kept != nullptr || own != nullptr) {
179+
return kept != nullptr ? kept : own;
180+
}
181+
}
182+
return nullptr;
183+
}
184+
185+
// Classes without a native -retainCount read as 0.
154186
NSUInteger NativeRetainCount(RootedState* state, id obj) {
155187
Class cls = object_getClass(obj);
156188
auto found = state->retainCountImps.find(cls);
157189
if (found == state->retainCountImps.end()) {
158-
IMP imp = IsTimerClass(cls) ? nullptr
159-
: ClassBuilder::FindNotOverridenMethod(cls, @selector(retainCount));
160-
found = state->retainCountImps.emplace(cls, imp).first;
190+
found = state->retainCountImps.emplace(cls, NativeRetainCountImp(cls)).first;
161191
}
162192
if (found->second == nullptr) {
163193
return 0;

‎TestFixtures/Marshalling/TNSAllocLog.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,23 @@ id TNSOwnedPrimitivesObjectAt(TNSOwnedPrimitives* holder, NSUInteger index);
116116

117117
@end
118118

119+
// Declares -retainCount and shares its name with the class adopting it, so
120+
// assigning TNSReplaceableRetainCountProtocol.prototype.retainCount replaces
121+
// the method on that class alone, as NSObjectProtocol.prototype does on
122+
// NSObject.
123+
@protocol TNSReplaceableRetainCount
124+
125+
- (NSUInteger)retainCount;
126+
127+
@end
128+
129+
@interface TNSReplaceableRetainCount : TNSAllocLog <TNSReplaceableRetainCount>
130+
131+
// Sends -retainCount to `object`.
132+
+ (NSUInteger)retainCountOf:(id)object;
133+
134+
@end
135+
119136
// Implements NSKeyedUnarchiverDelegate: answers the object it is given, or a
120137
// new TNSAllocLog once `substitutes` is set.
121138
@interface TNSDecodeDelegate : NSObject <NSKeyedUnarchiverDelegate>

‎TestFixtures/Marshalling/TNSAllocLog.m‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,14 @@ - (void)encodeWithCoder:(NSCoder*)coder {
292292

293293
@end
294294

295+
@implementation TNSReplaceableRetainCount
296+
297+
+ (NSUInteger)retainCountOf:(id)object {
298+
return ((NSUInteger (*)(id, SEL))objc_msgSend)(object, sel_registerName("retainCount"));
299+
}
300+
301+
@end
302+
295303
@implementation TNSDecodeDelegate
296304

297305
- (id)unarchiver:(NSKeyedUnarchiver*)unarchiver didDecodeObject:(id)object {

‎TestRunner/app/tests/WrapperLifetimeModesTests.js‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,57 @@ describe("Wrapper lifetime modes", function () {
271271
});
272272
});
273273

274+
// Replaced through the protocol prototype, the native class itself answers
275+
// -retainCount from JS, so the scan has to read the original. The
276+
// replacement outlives the spec, and the other modes' ownership checks
277+
// still call it from a GC prologue, so it is installed in the rooted mode
278+
// only.
279+
it("never calls a JS replacement of a native class's retainCount while scanning", function (done) {
280+
if (!rooted) {
281+
done();
282+
return;
283+
}
284+
var calls = 0;
285+
var proto = TNSReplaceableRetainCountProtocol.prototype;
286+
var original = proto.retainCount;
287+
proto.retainCount = function () {
288+
calls++;
289+
return original.call(this);
290+
};
291+
var Replaced = (function (_super) {
292+
__extends(Replaced, _super);
293+
function Replaced() {
294+
return (_super !== null && _super.apply(this, arguments)) || this;
295+
}
296+
return Replaced;
297+
})(TNSReplaceableRetainCount);
298+
var owner = NSMutableArray.new();
299+
var scanned;
300+
(function () {
301+
var obj = Replaced.new();
302+
obj.state = 3;
303+
owner.addObject(obj);
304+
var before = calls;
305+
expect(TNSReplaceableRetainCount.retainCountOf(obj)).toBeGreaterThan(1);
306+
expect(calls).toBe(before + 1);
307+
scanned = calls;
308+
})();
309+
settle(2, function () {
310+
if (rooted) {
311+
expect(calls).toBe(scanned);
312+
expect(owner.objectAtIndex(0).state).toBe(3);
313+
}
314+
owner.removeAllObjects();
315+
settle(3, function () {
316+
if (rooted) {
317+
expect(calls).toBe(scanned);
318+
expect(deallocs()).toBe(1);
319+
}
320+
done();
321+
});
322+
});
323+
});
324+
274325
it("lets an explicitly released rooted wrapper go", function (done) {
275326
var owner = NSMutableArray.new();
276327
var obj = Extended.new();

0 commit comments

Comments
 (0)