Skip to content

Commit fa8dfa5

Browse files
committed
fix(runtime): leak probe reads a canary record's shell
A JS-extended object's canary record now keeps one shell object, which carries the first canary's prototype, internal fields and implementation object, instead of a handle to each. The probe lists the shell among the record's values, so it stays a runtime handle rather than a root, and a cycle through the implementation object is found through it. A planted object whose method closes over the object itself checks that such a cycle, whose only path runs through the shell, is still reported.
1 parent 761ff4c commit fa8dfa5

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

‎test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ describe("Wrapper lifetime leak probe", function () {
5151
var weak = new java.lang.Runnable({ run: function () {} });
5252
weak.probeWeak = new WeakRef(weak);
5353
})();
54+
(function () {
55+
// A method closing over its own object: the cycle runs through the object's
56+
// prototype, its implementation object. In a function of its own, so no other
57+
// planted closure shares the context that holds it.
58+
var probeClosed = new java.lang.Runnable({
59+
run: function () {
60+
return probeClosed;
61+
}
62+
});
63+
planted.push(new WeakRef(probeClosed));
64+
})();
5465
setTimeout(function () {
5566
report = runtime.findLeakCycles();
5667
console.log("leak probe: " + JSON.stringify({
@@ -137,6 +148,17 @@ describe("Wrapper lifetime leak probe", function () {
137148
expect(cycles[0].path).toContain("--WeakMap value-->");
138149
});
139150

151+
it("finds a cycle through a method that closes over its object", function () {
152+
var cycles = cyclesThrough("probeClosed");
153+
if (!canary) {
154+
expect(cycles.length).toBe(0);
155+
return;
156+
}
157+
expect(cycles.length).toBe(1);
158+
expect(cycles[0].path).toContain("--ns:kept-->");
159+
expect(cycles[0].path).toContain("--ns:wrapper-->");
160+
});
161+
140162
it("does not report an object Java holds for a reason of its own", function () {
141163
expect(cyclesThrough("probeHeld").length).toBe(0);
142164
});

‎test-app/runtime/src/main/cpp/ObjectManager.cpp‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,12 +1218,8 @@ std::vector<ObjectManager::LeakProbeEntry> ObjectManager::GetLeakProbeEntries()
12181218
entries.push_back(std::move(entry));
12191219
}
12201220
LeakProbeEntry& entry = entries[it->second];
1221-
const Global<Value>* kept[] = {&record->prototype, &record->callSuper, &record->metadata,
1222-
&record->implementationObject};
1223-
for (const Global<Value>* value : kept) {
1224-
if (!value->IsEmpty()) {
1225-
entry.recordValues.push_back(value->Get(isolate));
1226-
}
1221+
if (!record->shell.IsEmpty()) {
1222+
entry.recordValues.push_back(record->shell.Get(isolate));
12271223
}
12281224
if (!record->state.IsEmpty()) {
12291225
entry.recordValues.push_back(record->state.Get(isolate));

0 commit comments

Comments
 (0)