From 866148d22ffaae290da996b55300d9aed0f32ad6 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Fri, 20 Nov 2026 15:20:25 -0300 Subject: [PATCH 01/12] feat(runtime): find the JS/Java cycles canary mode keeps alive (experimental) Adds ns:runtime findLeakCycles(). It joins a V8 heap snapshot and an HPROF dump of the Java heap into one graph: JS roots minus the runtime's handles to canary records, Java GC roots minus the runtime's object table and Reference referents, and the runtime's cross-heap edges (a live wrapper holds its Java object, a Java object holds its canary record). A Java object the runtime holds for a live wrapper and that nothing else reaches is leaked; the report gives the path from it back to its wrapper, a signature free of ids, and its members. Slow and on demand: the dump suspends the process. --- docs/ns-builtin-modules.md | 20 + test-app/app/src/main/assets/app/mainpage.js | 1 + .../main/assets/app/tests/testNsRuntime.js | 2 +- .../app/tests/testWrapperLifetimeLeaks.js | 124 +++ test-app/runtime/CMakeLists.txt | 1 + test-app/runtime/src/main/cpp/LeakProbe.cpp | 863 ++++++++++++++++++ test-app/runtime/src/main/cpp/LeakProbe.h | 34 + .../runtime/src/main/cpp/NsBuiltinModules.cpp | 14 +- .../runtime/src/main/cpp/ObjectManager.cpp | 57 ++ test-app/runtime/src/main/cpp/ObjectManager.h | 21 + .../runtime/src/main/cpp/js/ns-runtime.js | 11 +- .../src/main/java/com/tns/LeakProbe.java | 13 + .../src/main/java/com/tns/Runtime.java | 26 + 13 files changed, 1183 insertions(+), 4 deletions(-) create mode 100644 test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js create mode 100644 test-app/runtime/src/main/cpp/LeakProbe.cpp create mode 100644 test-app/runtime/src/main/cpp/LeakProbe.h create mode 100644 test-app/runtime/src/main/java/com/tns/LeakProbe.java diff --git a/docs/ns-builtin-modules.md b/docs/ns-builtin-modules.md index f787f5d19..d766bf4b2 100644 --- a/docs/ns-builtin-modules.md +++ b/docs/ns-builtin-modules.md @@ -105,6 +105,7 @@ wrapper. | `getConfig(key)` | Returns the current value of a config key. Throws `TypeError` on an unknown key. Readable from any isolate. | | `getWrapperLifetimeStats()` | Wrapper lifetime counters for the calling isolate (see [Wrapper lifetime](#wrapper-lifetime)). A fresh object on every call. | | `getWrapperClassCounts()` | Per Java class name (`` for a link whose class was never recorded), the calling isolate's live `wrappers`, the `canaries` among them and the canary `states` (see [Wrapper lifetime](#wrapper-lifetime)). | +| `findLeakCycles()` | Experimental and slow: joins a V8 heap snapshot and a Java heap dump to find the JS ↔ Java cycles canary mode keeps alive (see [Wrapper lifetime](#wrapper-lifetime)). | | `escapeException(x)` | Returns an `Error` branded so that, when it is thrown back across a native callback boundary, the native caller receives the original native exception `x` carries rather than the runtime's wrapper exception. Idempotent: a branded error is returned unchanged. Throws `TypeError` when called with no argument. The global `interop.escapeException` is this same function (see [Legacy globals](#legacy-globals)). | *Platform note (Android):* the original exception is the Java throwable `x` @@ -274,6 +275,25 @@ object's state with its link, and the released canary is a plain JS object from then on: what the weak collections keyed on its Java object stays keyed on it, and lives as long as it does. +**Finding leaked cycles (experimental).** Each heap has its own collector, +so neither sees a cycle that crosses into the other. `findLeakCycles()` joins +them into one graph and asks what a single collector would keep: the JS heap +from a V8 heap snapshot (its roots minus the runtime's handles to canary +records), the Java heap from an HPROF dump (`android.os.Debug.dumpHprofData`; +its GC roots minus the runtime's object table, `Reference` referents skipped), +and the runtime's own edges between them (a live wrapper holds its Java object; +a Java object holds its canary record: state, prototype and implementation +object). A Java object the runtime holds for a live wrapper, and that nothing +else reaches, is leaked. For each, the report gives the path from the Java +object to its own wrapper through fields, properties and the runtime's edges, +a `signature` (the path's node labels from their smallest rotation, free of +ids), and the Java classes of the leaked objects on it (`members`). +`leakedOutsideCycles` counts leaked objects held by a reported cycle rather +than being on one. In `"resurrecting"` mode the report is always empty: +resurrection breaks these cycles. The call suspends the process for a full +heap dump and parses it, so it takes seconds and briefly needs memory of the +order of the Java heap; it is a debugging tool, not for production. + **Weak collections.** What could notice a new canary is whatever keys on the old one without keeping it, so in canary mode a builtin (`js/weak-collections.js`) replaces `WeakMap`, `WeakSet`, `WeakRef` and diff --git a/test-app/app/src/main/assets/app/mainpage.js b/test-app/app/src/main/assets/app/mainpage.js index a8b1d2014..c7a2abf70 100644 --- a/test-app/app/src/main/assets/app/mainpage.js +++ b/test-app/app/src/main/assets/app/mainpage.js @@ -66,6 +66,7 @@ require("./tests/testWrapperLifetimeStateWrites"); require("./tests/testWrapperLifetimeFieldWrites"); require("./tests/testWrapperLifetimeRecordSharing"); require("./tests/testWrapperLifetimeWeakCollections"); +require("./tests/testWrapperLifetimeLeaks"); require("./tests/testsMemoryManagement"); require("./tests/testFieldGetSet"); require("./tests/extendedClassesTests"); diff --git a/test-app/app/src/main/assets/app/tests/testNsRuntime.js b/test-app/app/src/main/assets/app/tests/testNsRuntime.js index 53f84fcaf..65963a5b5 100644 --- a/test-app/app/src/main/assets/app/tests/testNsRuntime.js +++ b/test-app/app/src/main/assets/app/tests/testNsRuntime.js @@ -11,7 +11,7 @@ describe("ns:runtime", function () { // The export set is public API, declared alongside docs/ns-builtin-modules.md // — all of them change together. it("exposes exactly the declared surface", function () { - expect(Object.keys(runtime).sort()).toEqual(["escapeException", "getConfig", "getWrapperClassCounts", "getWrapperLifetimeStats", "setConfig"]); + expect(Object.keys(runtime).sort()).toEqual(["escapeException", "findLeakCycles", "getConfig", "getWrapperClassCounts", "getWrapperLifetimeStats", "setConfig"]); }); it("rejects unknown keys", function () { diff --git a/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js b/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js new file mode 100644 index 000000000..9965130f3 --- /dev/null +++ b/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js @@ -0,0 +1,124 @@ +describe("Wrapper lifetime leak probe", function () { + var runtime = require("ns:runtime"); + var mode = runtime.getConfig("wrapperLifetimeMode"); + var canary = mode === "canary"; + var Holder = com.tns.tests.WrapperHolder; + // WeakRefs, so the spec itself keeps nothing it planted alive. + var planted = []; + var report; + + function cyclesThrough(edge) { + return report.cycles.filter(function (cycle) { + return cycle.path.indexOf("--" + edge + "-->") !== -1; + }); + } + + // Each probe dumps the Java heap, so every shape is planted once and probed once, on a + // later task: a WeakRef keeps its target alive until the job that made it ends. + beforeAll(function (done) { + (function () { + // A JS property referencing its own object. + var selfReferencing = new java.lang.Runnable({ run: function () {} }); + selfReferencing.probeSelf = selfReferencing; + planted.push(new WeakRef(selfReferencing)); + + var twin = new java.lang.Runnable({ run: function () {} }); + twin.probeSelf = twin; + planted.push(new WeakRef(twin)); + + // A cycle through Java: the object's state holds a list that holds the object. + var listed = new java.lang.Runnable({ run: function () {} }); + var list = new java.util.ArrayList(); + list.add(listed); + listed.probeList = list; + planted.push(new WeakRef(listed)); + + // Held by a Java static: alive for a reason of its own, not a leak. + var held = new java.lang.Runnable({ run: function () {} }); + held.probeHeld = held; + Holder.held = held; + planted.push(new WeakRef(held)); + + // A back reference through a WeakRef keeps nothing. + var weak = new java.lang.Runnable({ run: function () {} }); + weak.probeWeak = new WeakRef(weak); + })(); + setTimeout(function () { + gc(); + java.lang.System.nanoTime(); + report = runtime.findLeakCycles(); + console.log("leak probe: " + JSON.stringify({ + leaked: report.leaked, + leakedOutsideCycles: report.leakedOutsideCycles, + probed: report.probed, + cycles: report.cycles.length, + durationMs: report.durationMs, + dumpBytes: report.dumpBytes + })); + report.cycles.forEach(function (cycle) { + console.log("leak probe cycle: " + cycle.signature); + }); + done(); + }, 0); + }); + + afterAll(function () { + Holder.held = null; + planted.forEach(function (ref) { + var object = ref.deref(); + if (object !== undefined) { + __releaseNativeCounterpart(object); + } + }); + planted = []; + report = null; + }); + + it("reports the heaps it joined (" + mode + ")", function () { + expect(report.mode).toBe(mode); + expect(Array.isArray(report.cycles)).toBe(true); + expect(report.javaObjects).toBeGreaterThan(0); + expect(report.jsNodes).toBeGreaterThan(0); + expect(report.probed).toBeGreaterThan(0); + expect(report.durationMs).toBeGreaterThan(0); + }); + + it("finds an object whose JS state references it", function () { + var cycles = cyclesThrough("probeSelf"); + if (!canary) { + // Resurrection breaks it: nothing keeps the object once JS lets go. + expect(cycles.length).toBe(0); + return; + } + expect(cycles.length).toBe(2); + expect(cycles[0].path).toContain("--ns:record-->"); + expect(cycles[0].path).toContain("--ns:wrapper-->"); + expect(cycles[0].members.length).toBe(1); + }); + + it("gives the same shape the same signature, free of ids", function () { + if (!canary) { + return; + } + var cycles = cyclesThrough("probeSelf"); + expect(cycles[0].signature).toBe(cycles[1].signature); + }); + + it("finds a cycle through a Java collection", function () { + var cycles = cyclesThrough("probeList"); + if (!canary) { + expect(cycles.length).toBe(0); + return; + } + expect(cycles.length).toBe(1); + expect(cycles[0].path).toContain("--java.util.ArrayList.elementData-->"); + }); + + it("does not report an object Java holds for a reason of its own", function () { + expect(cyclesThrough("probeHeld").length).toBe(0); + }); + + it("does not report a back reference through a WeakRef", function () { + expect(cyclesThrough("probeWeak").length).toBe(0); + }); +}); diff --git a/test-app/runtime/CMakeLists.txt b/test-app/runtime/CMakeLists.txt index 707ecec1c..31a02f081 100644 --- a/test-app/runtime/CMakeLists.txt +++ b/test-app/runtime/CMakeLists.txt @@ -200,6 +200,7 @@ add_library( src/main/cpp/FieldAccessor.cpp src/main/cpp/File.cpp src/main/cpp/Interop.cpp + src/main/cpp/LeakProbe.cpp src/main/cpp/IsolateTracked.cpp src/main/cpp/JEnv.cpp src/main/cpp/DesugaredInterfaceCompanionClassNameResolver.cpp diff --git a/test-app/runtime/src/main/cpp/LeakProbe.cpp b/test-app/runtime/src/main/cpp/LeakProbe.cpp new file mode 100644 index 000000000..bd7090973 --- /dev/null +++ b/test-app/runtime/src/main/cpp/LeakProbe.cpp @@ -0,0 +1,863 @@ +#include "LeakProbe.h" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ArgConverter.h" +#include "JEnv.h" +#include "JniLocalRef.h" +#include "ObjectManager.h" +#include "Runtime.h" +#include "WrapperLifetime.h" +#include "v8-profiler.h" + +using namespace v8; + +namespace tns { + +namespace { + +constexpr uint32_t kNone = UINT32_MAX; + +// --- The Java heap, from an HPROF dump -------------------------------------- + +// Big-endian reader over the mapped dump; any read past the end clears ok. +struct Reader { + const uint8_t* p; + const uint8_t* end; + uint32_t idSize = 4; + bool ok = true; + + bool Has(size_t n) { + if (!ok || static_cast(end - p) < n) { + ok = false; + return false; + } + return true; + } + uint64_t Read(size_t n) { + if (!Has(n)) { + return 0; + } + uint64_t value = 0; + for (size_t i = 0; i < n; i++) { + value = (value << 8) | p[i]; + } + p += n; + return value; + } + uint8_t U1() { return static_cast(Read(1)); } + uint16_t U2() { return static_cast(Read(2)); } + uint32_t U4() { return static_cast(Read(4)); } + uint64_t Id() { return Read(idSize); } + void Skip(size_t n) { + if (Has(n)) { + p += n; + } + } +}; + +size_t TypeSize(uint8_t type, uint32_t idSize) { + switch (type) { + case 2: // object + return idSize; + case 4: // boolean + case 8: // byte + return 1; + case 5: // char + case 9: // short + return 2; + case 6: // float + case 10: // int + return 4; + case 7: // double + case 11: // long + return 8; + default: + return 0; + } +} + +struct JavaClass { + uint64_t nameId = 0; + uint64_t super = 0; + // Instance fields this class declares: name string id, type. + std::vector> fields; + // Object-typed static fields: name string id, value. + std::vector> statics; +}; + +struct JavaObject { + uint64_t id; + uint64_t classId; // the instance's class, the array's class, or the class itself + const uint8_t* data; + uint32_t length; // instance: bytes; object array: element count + uint8_t kind; // 0 instance, 1 object array, 2 class +}; + +struct JavaHeap { + uint32_t idSize = 4; + std::unordered_map strings; + std::unordered_map classNameIds; // class id -> name string id + std::unordered_map classes; + std::vector objects; + std::unordered_map indexOf; + std::vector roots; + + // Edges by object index, built after the parse. + std::vector edgeStart; + std::vector edgeTo; + std::vector edgeLabel; + std::vector labels; + std::unordered_map labelIndex; + + const std::string& String(uint64_t id) const { + static const std::string empty; + auto it = strings.find(id); + return it != strings.end() ? it->second : empty; + } + + std::string ClassName(uint64_t classId) const { + auto it = classNameIds.find(classId); + return it != classNameIds.end() ? String(it->second) : std::string(""); + } + + uint32_t Label(const std::string& label) { + auto it = labelIndex.find(label); + if (it != labelIndex.end()) { + return it->second; + } + labels.push_back(label); + return labelIndex.emplace(label, static_cast(labels.size() - 1)).first->second; + } + + uint32_t IndexOf(uint64_t id) const { + auto it = indexOf.find(id); + return it != indexOf.end() ? it->second : kNone; + } +}; + +bool ParseRecords(Reader& in, JavaHeap& heap, std::string& error) { + while (in.ok && in.p < in.end) { + uint8_t tag = in.U1(); + in.U4(); // time + uint32_t length = in.U4(); + if (!in.Has(length)) { + break; + } + const uint8_t* recordEnd = in.p + length; + if (tag == 0x01) { // STRING + uint64_t id = in.Id(); + heap.strings.emplace(id, std::string(reinterpret_cast(in.p), + recordEnd - in.p)); + } else if (tag == 0x02) { // LOAD CLASS + in.U4(); + uint64_t classId = in.Id(); + in.U4(); + heap.classNameIds[classId] = in.Id(); + } else if (tag == 0x0C || tag == 0x1C) { // HEAP DUMP (SEGMENT) + Reader sub{in.p, recordEnd, heap.idSize}; + while (sub.ok && sub.p < sub.end) { + uint8_t subtag = sub.U1(); + switch (subtag) { + case 0xFF: // ROOT UNKNOWN + case 0x05: // STICKY CLASS + case 0x07: // MONITOR USED + case 0x89: // INTERNED STRING + case 0x8A: // FINALIZING + case 0x8B: // DEBUGGER + case 0x8C: // REFERENCE CLEANUP + case 0x8D: // VM INTERNAL + heap.roots.push_back(sub.Id()); + break; + case 0x90: // UNREACHABLE: not a root + sub.Id(); + break; + case 0x01: // JNI GLOBAL + heap.roots.push_back(sub.Id()); + sub.Id(); + break; + case 0x02: // JNI LOCAL + case 0x03: // JAVA FRAME + case 0x08: // THREAD OBJECT + case 0x8E: // JNI MONITOR + heap.roots.push_back(sub.Id()); + sub.U4(); + sub.U4(); + break; + case 0x04: // NATIVE STACK + case 0x06: // THREAD BLOCK + heap.roots.push_back(sub.Id()); + sub.U4(); + break; + case 0xFE: // HEAP DUMP INFO + sub.U4(); + sub.Id(); + break; + case 0x20: { // CLASS DUMP + uint64_t classId = sub.Id(); + sub.U4(); + JavaClass& cls = heap.classes[classId]; + cls.super = sub.Id(); + sub.Skip(5 * heap.idSize); // loader, signers, protection domain, 2 reserved + sub.U4(); // instance size + uint16_t constants = sub.U2(); + for (uint16_t i = 0; i < constants && sub.ok; i++) { + sub.U2(); + sub.Skip(TypeSize(sub.U1(), heap.idSize)); + } + uint16_t statics = sub.U2(); + for (uint16_t i = 0; i < statics && sub.ok; i++) { + uint64_t name = sub.Id(); + uint8_t type = sub.U1(); + if (type == 2) { + uint64_t value = sub.Id(); + if (value != 0) { + cls.statics.emplace_back(name, value); + } + } else { + sub.Skip(TypeSize(type, heap.idSize)); + } + } + uint16_t fields = sub.U2(); + for (uint16_t i = 0; i < fields && sub.ok; i++) { + uint64_t name = sub.Id(); + cls.fields.emplace_back(name, sub.U1()); + } + heap.objects.push_back({classId, classId, nullptr, 0, 2}); + break; + } + case 0x21: { // INSTANCE DUMP + uint64_t id = sub.Id(); + sub.U4(); + uint64_t classId = sub.Id(); + uint32_t bytes = sub.U4(); + const uint8_t* data = sub.p; + sub.Skip(bytes); + heap.objects.push_back({id, classId, data, bytes, 0}); + break; + } + case 0x22: { // OBJECT ARRAY DUMP + uint64_t id = sub.Id(); + sub.U4(); + uint32_t count = sub.U4(); + uint64_t classId = sub.Id(); + const uint8_t* data = sub.p; + sub.Skip(static_cast(count) * heap.idSize); + heap.objects.push_back({id, classId, data, count, 1}); + break; + } + case 0x23: { // PRIMITIVE ARRAY DUMP + sub.Id(); + sub.U4(); + uint32_t count = sub.U4(); + sub.Skip(static_cast(count) * TypeSize(sub.U1(), heap.idSize)); + break; + } + case 0xC3: { // PRIMITIVE ARRAY NODATA + sub.Id(); + sub.U4(); + sub.U4(); + sub.U1(); + break; + } + default: + error = "unknown HPROF heap dump subrecord 0x" + std::to_string(subtag); + return false; + } + } + if (!sub.ok) { + error = "truncated HPROF heap dump segment"; + return false; + } + } + in.p = recordEnd; + } + return in.ok; +} + +// The array LeakProbe.pinned holds, by class name. +uint64_t FindPinnedArray(const JavaHeap& heap) { + for (const auto& [classId, cls] : heap.classes) { + if (heap.ClassName(classId) != "com.tns.LeakProbe") { + continue; + } + for (const auto& [name, value] : cls.statics) { + if (heap.String(name) == "pinned") { + return value; + } + } + } + return 0; +} + +// References the analysis must not follow: what only the runtime or the probe +// holds, and what Java holds weakly. +bool SkippedField(const std::string& className, const std::string& field) { + return (field == "referent" && className == "java.lang.ref.Reference") || + (field == "zombie" && className == "java.lang.ref.FinalizerReference") || + (field == "values" && className == "com.tns.ObjectTable$Leaf"); +} + +void BuildEdges(JavaHeap& heap, uint64_t pinnedArray) { + heap.indexOf.reserve(heap.objects.size()); + for (uint32_t i = 0; i < heap.objects.size(); i++) { + heap.indexOf.emplace(heap.objects[i].id, i); + } + uint32_t elementLabel = heap.Label("[]"); + heap.edgeStart.assign(heap.objects.size() + 1, 0); + std::unordered_map classNames; + auto nameOf = [&](uint64_t classId) -> const std::string& { + auto it = classNames.find(classId); + if (it == classNames.end()) { + it = classNames.emplace(classId, heap.ClassName(classId)).first; + } + return it->second; + }; + for (uint32_t i = 0; i < heap.objects.size(); i++) { + heap.edgeStart[i] = static_cast(heap.edgeTo.size()); + const JavaObject& object = heap.objects[i]; + auto add = [&](uint64_t target, uint32_t label) { + uint32_t to = heap.IndexOf(target); + if (to != kNone) { + heap.edgeTo.push_back(to); + heap.edgeLabel.push_back(label); + } + }; + if (object.kind == 1) { + if (object.id == pinnedArray) { + continue; + } + Reader elements{object.data, object.data + static_cast(object.length) * heap.idSize, + heap.idSize}; + for (uint32_t e = 0; e < object.length; e++) { + uint64_t target = elements.Id(); + if (target != 0) { + add(target, elementLabel); + } + } + } else if (object.kind == 2) { + auto it = heap.classes.find(object.id); + if (it == heap.classes.end()) { + continue; + } + const std::string& className = nameOf(object.id); + for (const auto& [name, value] : it->second.statics) { + const std::string& field = heap.String(name); + if (className == "com.tns.LeakProbe" && field == "pinned") { + continue; + } + add(value, heap.Label(className + "." + field)); + } + } else { + Reader fields{object.data, object.data + object.length, heap.idSize}; + for (uint64_t classId = object.classId; classId != 0 && fields.ok;) { + auto it = heap.classes.find(classId); + if (it == heap.classes.end()) { + break; + } + const std::string& className = nameOf(classId); + for (const auto& [name, type] : it->second.fields) { + if (type != 2) { + fields.Skip(TypeSize(type, heap.idSize)); + continue; + } + uint64_t target = fields.Id(); + if (target == 0) { + continue; + } + const std::string& field = heap.String(name); + if (!SkippedField(className, field)) { + add(target, heap.Label(className + "." + field)); + } + } + classId = it->second.super; + } + } + } + heap.edgeStart[heap.objects.size()] = static_cast(heap.edgeTo.size()); +} + +// Maps an HPROF dump read-only for the parse; the parsed objects point into it. +struct MappedFile { + void* data = MAP_FAILED; + size_t size = 0; + ~MappedFile() { + if (data != MAP_FAILED) { + munmap(data, size); + } + } + bool Open(const std::string& path) { + int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); + if (fd < 0) { + return false; + } + struct stat st; + if (fstat(fd, &st) == 0 && st.st_size > 0) { + size = static_cast(st.st_size); + data = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0); + } + close(fd); + return data != MAP_FAILED; + } +}; + +bool ParseHprof(const MappedFile& file, JavaHeap& heap, std::string& error) { + auto begin = static_cast(file.data); + Reader in{begin, begin + file.size}; + const uint8_t* nul = static_cast(memchr(begin, 0, std::min(file.size, 64))); + if (nul == nullptr) { + error = "not an HPROF file"; + return false; + } + in.p = nul + 1; + heap.idSize = in.U4(); + in.idSize = heap.idSize; + in.Read(8); // timestamp + if (!in.ok || (heap.idSize != 4 && heap.idSize != 8)) { + error = "unsupported HPROF header"; + return false; + } + return ParseRecords(in, heap, error); +} + +// --- The joined graph ------------------------------------------------------- + +std::string ToUtf8(Isolate* isolate, Local value) { + if (value.IsEmpty()) { + return std::string(); + } + if (value->IsString()) { + return ArgConverter::ConvertToString(value.As()); + } + if (value->IsNumber()) { + return "[" + std::to_string(static_cast(value.As()->Value())) + "]"; + } + return std::string(); +} + +// One followed edge: a JS edge, a Java field or element (label index), or a +// cross-heap edge of the runtime (neither). +struct EdgeRef { + const HeapGraphEdge* js = nullptr; + uint32_t javaLabel = kNone; +}; + +struct Graph { + Isolate* isolate; + const HeapSnapshot* snapshot; + JavaHeap& java; + uint32_t jsCount = 0; + std::unordered_map jsIndex; + std::vector jsNodes; + // JS root edges to these snapshot nodes are the runtime's handles to canary + // records, not real roots. + std::unordered_set runtimeHandleTargets; + // Cross-heap edges. + std::unordered_map> wrapperToJava; + std::unordered_map> javaToRecord; + + uint32_t JavaNode(uint32_t index) const { return jsCount + index; } + bool IsJava(uint32_t node) const { return node >= jsCount; } + + // Calls visit(target, labelText) for each followed edge of node. + template + void ForEachEdge(uint32_t node, bool fromRoots, F&& visit) const { + if (!IsJava(node)) { + const HeapGraphNode* js = jsNodes[node]; + bool synthetic = js->GetType() == HeapGraphNode::kSynthetic; + for (int i = 0; i < js->GetChildrenCount(); i++) { + const HeapGraphEdge* edge = js->GetChild(i); + if (edge->GetType() == HeapGraphEdge::kWeak || edge->GetType() == HeapGraphEdge::kShortcut) { + continue; + } + auto it = jsIndex.find(edge->GetToNode()); + if (it == jsIndex.end()) { + continue; + } + if (fromRoots && synthetic && runtimeHandleTargets.count(it->second) != 0) { + continue; + } + visit(it->second, EdgeRef{edge, kNone}); + } + auto cross = wrapperToJava.find(node); + if (cross != wrapperToJava.end()) { + for (uint32_t target : cross->second) { + visit(target, EdgeRef{}); + } + } + return; + } + uint32_t index = node - jsCount; + for (uint32_t e = java.edgeStart[index]; e < java.edgeStart[index + 1]; e++) { + visit(JavaNode(java.edgeTo[e]), EdgeRef{nullptr, java.edgeLabel[e]}); + } + auto cross = javaToRecord.find(node); + if (cross != javaToRecord.end()) { + for (uint32_t target : cross->second) { + visit(target, EdgeRef{}); + } + } + } + + std::string NodeLabel(uint32_t node) const { + if (IsJava(node)) { + const JavaObject& object = java.objects[node - jsCount]; + return object.kind == 2 ? "class " + java.ClassName(object.id) : java.ClassName(object.classId); + } + std::string name = ToUtf8(isolate, jsNodes[node]->GetName()); + // V8 names some nodes with their snapshot id ("system / Context / scope @123"). + size_t at = name.rfind(" @"); + if (at != std::string::npos && + name.find_first_not_of("0123456789", at + 2) == std::string::npos) { + name.resize(at); + } + return "JS " + (name.empty() ? std::string("(anonymous)") : name); + } + + std::string EdgeLabel(uint32_t from, const EdgeRef& edge) const { + if (edge.js != nullptr) { + return ToUtf8(isolate, edge.js->GetName()); + } + if (edge.javaLabel != kNone) { + return java.labels[edge.javaLabel]; + } + return IsJava(from) ? "ns:record" : "ns:wrapper"; + } +}; + +std::vector Reach(const Graph& graph, uint32_t jsRoot, + const std::vector& javaRoots) { + std::vector reached(graph.jsCount + graph.java.objects.size(), 0); + std::deque queue; + auto push = [&](uint32_t node) { + if (!reached[node]) { + reached[node] = 1; + queue.push_back(node); + } + }; + push(jsRoot); + for (uint32_t root : javaRoots) { + push(root); + } + while (!queue.empty()) { + uint32_t node = queue.front(); + queue.pop_front(); + graph.ForEachEdge(node, true, [&](uint32_t target, const EdgeRef&) { push(target); }); + } + return reached; +} + +// A shortest path from `from` to `to` through unreached nodes, closed by the +// runtime's edge from `to` (a wrapper) back to `from` (its Java object), as +// alternating node and edge labels; empty when there is none within the limit. +std::vector FindCycle(const Graph& graph, const std::vector& reached, + uint32_t from, uint32_t to, std::vector* path, + std::vector* nodeLabels) { + constexpr size_t kVisitLimit = 2'000'000; + std::unordered_map> parent; + std::deque queue; + queue.push_back(from); + parent.emplace(from, std::make_pair(kNone, EdgeRef{})); + bool found = false; + while (!queue.empty() && !found && parent.size() < kVisitLimit) { + uint32_t node = queue.front(); + queue.pop_front(); + graph.ForEachEdge(node, false, [&](uint32_t target, const EdgeRef& edge) { + if (found || reached[target] || !parent.emplace(target, std::make_pair(node, edge)).second) { + return; + } + if (target == to) { + found = true; + return; + } + queue.push_back(target); + }); + } + if (!found) { + return {}; + } + std::vector> steps; // node, edge out of it + steps.emplace_back(to, EdgeRef{}); + for (uint32_t node = to; node != from;) { + auto [previous, edge] = parent.at(node); + steps.emplace_back(previous, edge); + node = previous; + } + std::reverse(steps.begin(), steps.end()); + std::vector nodes; + for (const auto& [node, edge] : steps) { + nodes.push_back(node); + nodeLabels->push_back(graph.NodeLabel(node)); + path->push_back(graph.NodeLabel(node)); + path->push_back("--" + graph.EdgeLabel(node, edge) + "-->"); + } + path->push_back(graph.NodeLabel(from)); + return nodes; +} + +// The cycle's node labels from its lexicographically smallest rotation, so the +// same shape reads the same whichever member the search started from. +std::string Signature(const std::vector& labels) { + auto rotation = [&](size_t start) { + std::string text; + for (size_t i = 0; i < labels.size(); i++) { + text += labels[(start + i) % labels.size()]; + text += " -> "; + } + return text; + }; + std::string bestText = rotation(0); + for (size_t i = 1; i < labels.size(); i++) { + std::string text = rotation(i); + if (text < bestText) { + bestText = std::move(text); + } + } + return bestText + "(back)"; +} + +void Set(Isolate* isolate, Local context, Local target, const char* name, + Local value) { + (void) target->Set(context, ArgConverter::ConvertToV8String(isolate, name), value); +} + +Local ToArray(Isolate* isolate, Local context, + const std::vector& items) { + Local array = Array::New(isolate, static_cast(items.size())); + for (uint32_t i = 0; i < items.size(); i++) { + (void) array->Set(context, i, ArgConverter::ConvertToV8String(isolate, items[i])); + } + return array; +} + +} // namespace + +MaybeLocal LeakProbe::FindLeakCycles(Isolate* isolate) { + auto started = std::chrono::steady_clock::now(); + EscapableHandleScope scope(isolate); + Local context = isolate->GetCurrentContext(); + ObjectManager* objectManager = Runtime::GetObjectManager(isolate); + auto fail = [&](const std::string& message) -> MaybeLocal { + isolate->ThrowException(Exception::Error(ArgConverter::ConvertToV8String( + isolate, "findLeakCycles: " + message))); + return MaybeLocal(); + }; + + // The snapshot first, before this call holds a handle to any wrapper or state: + // a handle would root it. + objectManager->FlushFinalizedBatchIfPending(); + HeapProfiler* profiler = isolate->GetHeapProfiler(); + const HeapSnapshot* snapshot = profiler->TakeHeapSnapshot(); + if (snapshot == nullptr) { + return fail("could not take a heap snapshot"); + } + struct DeleteSnapshot { + const HeapSnapshot* snapshot; + ~DeleteSnapshot() { const_cast(snapshot)->Delete(); } + } deleteSnapshot{snapshot}; + + std::vector entries = objectManager->GetLeakProbeEntries(); + + // The Java heap, with the entries' objects pinned in order. + JEnv env; + JNIEnv* raw = env; + JniLocalRef ids(raw->NewIntArray(static_cast(entries.size()))); + if (ids.IsNull()) { + raw->ExceptionClear(); + return fail("out of memory"); + } + { + std::vector values; + values.reserve(entries.size()); + for (const auto& entry : entries) { + values.push_back(entry.id); + } + raw->SetIntArrayRegion((jintArray) ids, 0, static_cast(values.size()), values.data()); + } + static jmethodID dumpMethod = nullptr; + if (dumpMethod == nullptr) { + JniLocalRef runtimeClass(raw->GetObjectClass(objectManager->GetJavaRuntimeObject())); + dumpMethod = raw->GetMethodID((jclass) runtimeClass, "dumpHeapForLeakProbe", + "([I)Ljava/lang/String;"); + } + JniLocalRef pathRef(raw->CallObjectMethod(objectManager->GetJavaRuntimeObject(), dumpMethod, + (jintArray) ids)); + if (raw->ExceptionCheck()) { + raw->ExceptionClear(); + return fail("the Java heap dump failed"); + } + if (pathRef.IsNull()) { + return fail("the Java heap dump failed; see the log"); + } + std::string path = ArgConverter::jstringToString((jstring) pathRef); + struct RemoveFile { + std::string path; + ~RemoveFile() { unlink(path.c_str()); } + } removeFile{path}; + + MappedFile file; + if (!file.Open(path)) { + return fail("could not read the Java heap dump"); + } + JavaHeap java; + std::string error; + if (!ParseHprof(file, java, error)) { + return fail(error); + } + uint64_t pinnedArray = FindPinnedArray(java); + BuildEdges(java, pinnedArray); + + Graph graph{isolate, snapshot, java}; + graph.jsCount = static_cast(snapshot->GetNodesCount()); + graph.jsNodes.reserve(graph.jsCount); + graph.jsIndex.reserve(graph.jsCount); + for (uint32_t i = 0; i < graph.jsCount; i++) { + const HeapGraphNode* node = snapshot->GetNode(static_cast(i)); + graph.jsNodes.push_back(node); + graph.jsIndex.emplace(node, i); + } + auto jsNodeOf = [&](Local value) -> uint32_t { + if (value.IsEmpty() || !value->IsObject()) { + return kNone; + } + const HeapGraphNode* node = snapshot->GetNodeById(profiler->GetObjectId(value)); + auto it = graph.jsIndex.find(node); + return it != graph.jsIndex.end() ? it->second : kNone; + }; + + // The pinned array's elements, in the entries' order. + std::vector javaOf(entries.size(), kNone); + uint32_t pinnedIndex = java.IndexOf(pinnedArray); + if (pinnedIndex == kNone) { + return fail("the heap dump has no LeakProbe.pinned array"); + } + { + const JavaObject& pinned = java.objects[pinnedIndex]; + Reader elements{pinned.data, pinned.data + static_cast(pinned.length) * java.idSize, + java.idSize}; + for (uint32_t i = 0; i < pinned.length && i < entries.size(); i++) { + uint64_t id = elements.Id(); + javaOf[i] = id != 0 ? java.IndexOf(id) : kNone; + } + } + + for (size_t i = 0; i < entries.size(); i++) { + const auto& entry = entries[i]; + if (javaOf[i] == kNone) { + continue; + } + uint32_t javaNode = graph.JavaNode(javaOf[i]); + if (!entry.wrapper.IsEmpty() && entry.wrapperHoldsJava) { + uint32_t wrapperNode = jsNodeOf(entry.wrapper); + if (wrapperNode != kNone) { + graph.wrapperToJava[wrapperNode].push_back(javaNode); + } + } + for (Local value : entry.recordValues) { + uint32_t node = jsNodeOf(value); + if (node != kNone) { + graph.javaToRecord[javaNode].push_back(node); + graph.runtimeHandleTargets.insert(node); + } + } + } + + std::vector javaRoots; + javaRoots.reserve(java.roots.size()); + for (uint64_t root : java.roots) { + uint32_t index = java.IndexOf(root); + if (index != kNone) { + javaRoots.push_back(graph.JavaNode(index)); + } + } + auto rootIt = graph.jsIndex.find(snapshot->GetRoot()); + if (rootIt == graph.jsIndex.end()) { + return fail("the heap snapshot has no root"); + } + std::vector reached = Reach(graph, rootIt->second, javaRoots); + + // The runtime keeps a Java object only through a live wrapper; one nothing + // else reaches would be collected by a single collector. A canary record's + // state alone keeps nothing: once its canary died, Java holds the object + // weakly and collects it normally. Resurrection breaks these cycles, so only + // canary mode reports them. + std::unordered_map entryOfJavaNode; + std::vector leaked; + std::unordered_set leakedSet; + std::vector wrapperOf(entries.size(), kNone); + bool canaryMode = GetWrapperLifetimeMode() == WrapperLifetimeMode::kCanary; + for (size_t i = 0; i < entries.size(); i++) { + if (javaOf[i] == kNone) { + continue; + } + uint32_t javaNode = graph.JavaNode(javaOf[i]); + entryOfJavaNode.emplace(javaNode, i); + if (!entries[i].wrapper.IsEmpty() && entries[i].wrapperHoldsJava) { + wrapperOf[i] = jsNodeOf(entries[i].wrapper); + } + if (canaryMode && !reached[javaNode] && wrapperOf[i] != kNone) { + leaked.push_back(i); + leakedSet.insert(i); + } + } + + Local cycles = Array::New(isolate); + std::unordered_set covered; + uint32_t cycleCount = 0; + for (size_t i : leaked) { + if (covered.count(i) != 0) { + continue; + } + std::vector pathLabels; + std::vector nodeLabels; + std::vector nodes = FindCycle(graph, reached, graph.JavaNode(javaOf[i]), + wrapperOf[i], &pathLabels, &nodeLabels); + if (nodes.empty()) { + continue; + } + std::vector members; + for (uint32_t node : nodes) { + auto it = entryOfJavaNode.find(node); + if (it != entryOfJavaNode.end() && leakedSet.count(it->second) != 0) { + covered.insert(it->second); + members.push_back(entries[it->second].className); + } + } + std::string signature = Signature(nodeLabels); + Local cycle = Object::New(isolate); + Set(isolate, context, cycle, "signature", ArgConverter::ConvertToV8String(isolate, signature)); + Set(isolate, context, cycle, "path", ToArray(isolate, context, pathLabels)); + Set(isolate, context, cycle, "members", ToArray(isolate, context, members)); + (void) cycles->Set(context, cycleCount++, cycle); + } + + auto elapsed = std::chrono::duration(std::chrono::steady_clock::now() - started); + Local report = Object::New(isolate); + Set(isolate, context, report, "mode", + ArgConverter::ConvertToV8String(isolate, WrapperLifetimeModeName(GetWrapperLifetimeMode()))); + Set(isolate, context, report, "cycles", cycles); + Set(isolate, context, report, "leaked", Number::New(isolate, static_cast(leaked.size()))); + Set(isolate, context, report, "leakedOutsideCycles", + Number::New(isolate, static_cast(leaked.size() - covered.size()))); + Set(isolate, context, report, "probed", Number::New(isolate, static_cast(entries.size()))); + Set(isolate, context, report, "jsNodes", Number::New(isolate, graph.jsCount)); + Set(isolate, context, report, "javaObjects", + Number::New(isolate, static_cast(java.objects.size()))); + Set(isolate, context, report, "dumpBytes", Number::New(isolate, static_cast(file.size))); + Set(isolate, context, report, "durationMs", Number::New(isolate, elapsed.count())); + return scope.Escape(report); +} + +} // namespace tns diff --git a/test-app/runtime/src/main/cpp/LeakProbe.h b/test-app/runtime/src/main/cpp/LeakProbe.h new file mode 100644 index 000000000..7f2f7a9b1 --- /dev/null +++ b/test-app/runtime/src/main/cpp/LeakProbe.h @@ -0,0 +1,34 @@ +#ifndef LEAKPROBE_H_ +#define LEAKPROBE_H_ + +#include "v8.h" + +namespace tns { + +// ns:runtime's findLeakCycles(): finds the JS <-> Java cycles the canary +// wrapper lifetime mode keeps alive, which the resurrecting mode breaks. +// +// Each heap has its own collector, so neither sees a cycle that crosses into +// the other: the runtime roots a canary record's state while Java holds the +// object, and Java holds an object while a canary is alive. The probe joins +// both heaps into one graph and asks what a single collector would keep: +// - the JS heap from a V8 heap snapshot, its roots minus the runtime's own +// handles to canary records; +// - the Java heap from an HPROF dump (android.os.Debug.dumpHprofData), its GC +// roots minus the runtime's object table, with Reference referents skipped; +// - the runtime's cross-heap edges: a live wrapper holds its Java object, and +// a Java object holds its canary record (state, prototype, implementation). +// A recorded Java object nothing reaches is leaked; the report gives each +// leaked group a cycle through it, with a signature free of ids. +// +// Slow by design: it suspends the process for a full heap dump and parses it. +// A diagnostic, not something to run in production. +class LeakProbe { + public: + // The report object, or empty with a JS exception pending. + static v8::MaybeLocal FindLeakCycles(v8::Isolate* isolate); +}; + +} // namespace tns + +#endif /* LEAKPROBE_H_ */ diff --git a/test-app/runtime/src/main/cpp/NsBuiltinModules.cpp b/test-app/runtime/src/main/cpp/NsBuiltinModules.cpp index 829b6ed51..325d86c0d 100644 --- a/test-app/runtime/src/main/cpp/NsBuiltinModules.cpp +++ b/test-app/runtime/src/main/cpp/NsBuiltinModules.cpp @@ -9,6 +9,7 @@ #include "BuiltinLoader.h" #include "CallbackHandlers.h" #include "Interop.h" +#include "LeakProbe.h" #include "Messaging.h" #include "ModuleInternalCallbacks.h" #include "NativeScriptAssert.h" @@ -185,6 +186,13 @@ void GetWrapperLifetimeStatsCallback(const FunctionCallbackInfo& info) { info.GetReturnValue().Set(result); } +void FindLeakCyclesCallback(const FunctionCallbackInfo& info) { + Local report; + if (LeakProbe::FindLeakCycles(info.GetIsolate()).ToLocal(&report)) { + info.GetReturnValue().Set(report); + } +} + void GetWrapperClassCountsCallback(const FunctionCallbackInfo& info) { Isolate* isolate = info.GetIsolate(); Local context = isolate->GetCurrentContext(); @@ -278,13 +286,14 @@ MaybeLocal NsRuntimeBinding(Local context) { Isolate* isolate = v8::Isolate::GetCurrent(); Local binding = Object::New(isolate); Local setConfig, getConfig, escapeException, getWrapperLifetimeStats, - getWrapperClassCounts; + getWrapperClassCounts, findLeakCycles; if (!v8::Function::New(context, SetConfigCallback).ToLocal(&setConfig) || !v8::Function::New(context, GetConfigCallback).ToLocal(&getConfig) || !v8::Function::New(context, GetWrapperLifetimeStatsCallback) .ToLocal(&getWrapperLifetimeStats) || !v8::Function::New(context, GetWrapperClassCountsCallback) .ToLocal(&getWrapperClassCounts) || + !v8::Function::New(context, FindLeakCyclesCallback).ToLocal(&findLeakCycles) || !Interop::CreateEscapeException(context).ToLocal(&escapeException) || !binding->Set(context, ArgConverter::ConvertToV8String(isolate, "setConfig"), setConfig) .FromMaybe(false) || @@ -298,6 +307,9 @@ MaybeLocal NsRuntimeBinding(Local context) { .FromMaybe(false) || !binding->Set(context, ArgConverter::ConvertToV8String(isolate, "getWrapperClassCounts"), getWrapperClassCounts) + .FromMaybe(false) || + !binding->Set(context, ArgConverter::ConvertToV8String(isolate, "findLeakCycles"), + findLeakCycles) .FromMaybe(false)) { return MaybeLocal(); } diff --git a/test-app/runtime/src/main/cpp/ObjectManager.cpp b/test-app/runtime/src/main/cpp/ObjectManager.cpp index 65ca90fc3..701ad6849 100644 --- a/test-app/runtime/src/main/cpp/ObjectManager.cpp +++ b/test-app/runtime/src/main/cpp/ObjectManager.cpp @@ -1541,6 +1541,63 @@ jobject ObjectManager::JavaObjectOrNull(int javaObjectID) { return object; } +std::vector ObjectManager::GetLeakProbeEntries() { + auto isolate = m_isolate; + std::unordered_map names; + auto nameOf = [&names](jclass clazz) -> std::string { + auto it = names.find(clazz); + if (it != names.end()) { + return it->second; + } + std::string name = clazz != nullptr ? GetClassName(clazz) : std::string(""); + std::replace(name.begin(), name.end(), '/', '.'); + names.emplace(clazz, name); + return name; + }; + std::unordered_map byId; + std::vector entries; + m_idToObject.ForEach([&](ObjectWeakCallbackState* state) { + if (!state->linked || state->target->IsEmpty()) { + return; + } + LeakProbeEntry entry; + entry.id = static_cast(state->javaObjectID); + entry.className = nameOf(state->objectClazz); + entry.wrapper = state->target->Get(isolate); + entry.canary = state->canary; + // A resurrecting wrapper's object turns weak once its wrapper was first collected. + entry.wrapperHoldsJava = state->canary || !state->javaWeakened; + byId.emplace(entry.id, entries.size()); + entries.push_back(std::move(entry)); + }); + m_canaryRecords.ForEach([&](CanaryRecord* record) { + int id = record->id; + auto it = byId.find(id); + if (it == byId.end()) { + LeakProbeEntry entry; + entry.id = id; + entry.className = nameOf(record->clazz); + it = byId.emplace(id, entries.size()).first; + entries.push_back(std::move(entry)); + } + LeakProbeEntry& entry = entries[it->second]; + entry.hasRecord = true; + if (const CanaryClass* cls = record->cls) { + const Global* kept[] = {&cls->prototype, &cls->callSuper, &cls->metadata, + &cls->implementationObject}; + for (const Global* value : kept) { + if (!value->IsEmpty()) { + entry.recordValues.push_back(value->Get(isolate)); + } + } + } + if (!record->state.IsEmpty()) { + entry.recordValues.push_back(record->state.Get(isolate)); + } + }); + return entries; +} + void ObjectManager::ReleaseCollectedCanaryStates() { if (!m_deathInboxOpen) { return; diff --git a/test-app/runtime/src/main/cpp/ObjectManager.h b/test-app/runtime/src/main/cpp/ObjectManager.h index 57ff4f7c0..07aa0a31d 100644 --- a/test-app/runtime/src/main/cpp/ObjectManager.h +++ b/test-app/runtime/src/main/cpp/ObjectManager.h @@ -284,6 +284,27 @@ class ObjectManager { // Per Java class name (dotted), for ns:runtime's getWrapperClassCounts(). std::map GetWrapperClassCounts(); + // The runtime's cross-heap edges, for ns:runtime's findLeakCycles() + // (LeakProbe). One entry per id with a live wrapper or a canary record. + struct LeakProbeEntry { + int id = -1; + // Dotted Java class name, or "". + std::string className; + // The live wrapper, or empty. + v8::Local wrapper; + // The Java object table holds the object strongly for the wrapper. + bool wrapperHoldsJava = false; + bool canary = false; + bool hasRecord = false; + // What the canary record keeps alive while Java holds the object. + std::vector> recordValues; + }; + + // In the caller's HandleScope. + std::vector GetLeakProbeEntries(); + + jobject GetJavaRuntimeObject() const { return m_javaRuntimeObject; } + // Canary mode: adds `maybeCanary` (a V8 fast call, false for any value but a // runtime object), `unwrap` (a canary -> its Java object's weak-collection // key), `wrap` (key -> live or new canary, or undefined once Java collected diff --git a/test-app/runtime/src/main/cpp/js/ns-runtime.js b/test-app/runtime/src/main/cpp/js/ns-runtime.js index 473ff9f7f..4ee2961ef 100644 --- a/test-app/runtime/src/main/cpp/js/ns-runtime.js +++ b/test-app/runtime/src/main/cpp/js/ns-runtime.js @@ -6,8 +6,14 @@ // per-isolate) are defined and validated on the native side, so this file // stays a thin, frozen surface. -const { setConfig, getConfig, escapeException, getWrapperLifetimeStats, getWrapperClassCounts } = - binding; +const { + setConfig, + getConfig, + escapeException, + getWrapperLifetimeStats, + getWrapperClassCounts, + findLeakCycles, +} = binding; const { ObjectFreeze } = primordials; exports.setConfig = setConfig; @@ -15,4 +21,5 @@ exports.getConfig = getConfig; exports.escapeException = escapeException; exports.getWrapperLifetimeStats = getWrapperLifetimeStats; exports.getWrapperClassCounts = getWrapperClassCounts; +exports.findLeakCycles = findLeakCycles; ObjectFreeze(exports); diff --git a/test-app/runtime/src/main/java/com/tns/LeakProbe.java b/test-app/runtime/src/main/java/com/tns/LeakProbe.java new file mode 100644 index 000000000..636d54309 --- /dev/null +++ b/test-app/runtime/src/main/java/com/tns/LeakProbe.java @@ -0,0 +1,13 @@ +package com.tns; + +/** + * Holds, during ns:runtime's findLeakCycles() heap dump only, the Java objects the analysis + * maps to JS wrappers and states. The analysis finds this array through this class's static + * field by name and does not count it as a reference. + */ +final class LeakProbe { + static Object[] pinned; + + private LeakProbe() { + } +} diff --git a/test-app/runtime/src/main/java/com/tns/Runtime.java b/test-app/runtime/src/main/java/com/tns/Runtime.java index dc51b898b..6ebb672a3 100644 --- a/test-app/runtime/src/main/java/com/tns/Runtime.java +++ b/test-app/runtime/src/main/java/com/tns/Runtime.java @@ -1302,6 +1302,32 @@ private boolean strengthenInstance(int javaObjectID) { return objectTable.strengthen(javaObjectID); } + // ns:runtime findLeakCycles(): writes an HPROF dump with the objects of ids pinned in + // LeakProbe.pinned, in order, so the native analysis can find them. Returns the dump's + // path, or null if it could not be written. + @RuntimeCallable + private String dumpHeapForLeakProbe(int[] ids) { + Object[] objects = new Object[ids.length]; + for (int i = 0; i < ids.length; i++) { + objects[i] = objectTable.get(ids[i]); + } + java.io.File file = null; + try { + file = java.io.File.createTempFile("ns-leak-probe", ".hprof"); + LeakProbe.pinned = objects; + android.os.Debug.dumpHprofData(file.getAbsolutePath()); + return file.getAbsolutePath(); + } catch (Throwable t) { + android.util.Log.w("JS", "findLeakCycles could not dump the Java heap", t); + if (file != null) { + file.delete(); + } + return null; + } finally { + LeakProbe.pinned = null; + } + } + boolean removeWatched(ObjectTable.WatchedRef ref) { return objectTable.removeWatched(ref); } From 3fadd8b819754443205fa4b28e3df9b82826e90b Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Fri, 20 Nov 2026 16:01:34 -0300 Subject: [PATCH 02/12] fix(runtime): leak probe review fixes - Clear the V8 object ids the snapshot assigned, unless DevTools holds snapshots: a snapshot otherwise leaves V8 tracking object moves on every scavenge for the isolate's lifetime. - Follow a WeakMap value from its key only: V8 also links it from the map's table, which hid cycles through WeakMap values. - Collect Java before pinning, so objects only weak entries hold are gone instead of counted as leaked. - Available in debuggable builds or with "wrapperLifetime": { "leakProbe": true } only; one probe at a time in the process; stale dumps swept. - Return early outside canary mode, cap cycle searches (truncated), catch C++ exceptions, reject a malformed string record. - Docs: cost, gating, workers, non-atomic snapshot and dump. --- docs/ns-builtin-modules.md | 29 +++++-- .../app/tests/testWrapperLifetimeLeaks.js | 25 ++++++ test-app/runtime/src/main/cpp/LeakProbe.cpp | 84 ++++++++++++++++++- .../runtime/src/main/cpp/NsBuiltinModules.cpp | 15 +++- .../src/main/java/com/tns/AppConfig.java | 13 ++- .../src/main/java/com/tns/LeakProbe.java | 18 ++++ .../src/main/java/com/tns/Runtime.java | 51 +++++++---- 7 files changed, 204 insertions(+), 31 deletions(-) diff --git a/docs/ns-builtin-modules.md b/docs/ns-builtin-modules.md index d766bf4b2..e5322e53a 100644 --- a/docs/ns-builtin-modules.md +++ b/docs/ns-builtin-modules.md @@ -105,7 +105,7 @@ wrapper. | `getConfig(key)` | Returns the current value of a config key. Throws `TypeError` on an unknown key. Readable from any isolate. | | `getWrapperLifetimeStats()` | Wrapper lifetime counters for the calling isolate (see [Wrapper lifetime](#wrapper-lifetime)). A fresh object on every call. | | `getWrapperClassCounts()` | Per Java class name (`` for a link whose class was never recorded), the calling isolate's live `wrappers`, the `canaries` among them and the canary `states` (see [Wrapper lifetime](#wrapper-lifetime)). | -| `findLeakCycles()` | Experimental and slow: joins a V8 heap snapshot and a Java heap dump to find the JS ↔ Java cycles canary mode keeps alive (see [Wrapper lifetime](#wrapper-lifetime)). | +| `findLeakCycles()` | Experimental and slow, debuggable builds or `"wrapperLifetime": { "leakProbe": true }` only: joins a V8 heap snapshot and a Java heap dump to find the JS ↔ Java cycles canary mode keeps alive (see [Wrapper lifetime](#wrapper-lifetime)). | | `escapeException(x)` | Returns an `Error` branded so that, when it is thrown back across a native callback boundary, the native caller receives the original native exception `x` carries rather than the runtime's wrapper exception. Idempotent: a branded error is returned unchanged. Throws `TypeError` when called with no argument. The global `interop.escapeException` is this same function (see [Legacy globals](#legacy-globals)). | *Platform note (Android):* the original exception is the Java throwable `x` @@ -283,16 +283,29 @@ records), the Java heap from an HPROF dump (`android.os.Debug.dumpHprofData`; its GC roots minus the runtime's object table, `Reference` referents skipped), and the runtime's own edges between them (a live wrapper holds its Java object; a Java object holds its canary record: state, prototype and implementation -object). A Java object the runtime holds for a live wrapper, and that nothing -else reaches, is leaked. For each, the report gives the path from the Java +object). A `WeakMap` value is followed from its key only. A Java object the +runtime holds for a live wrapper, and that nothing else reaches, is leaked. For each, the report gives the path from the Java object to its own wrapper through fields, properties and the runtime's edges, a `signature` (the path's node labels from their smallest rotation, free of ids), and the Java classes of the leaked objects on it (`members`). -`leakedOutsideCycles` counts leaked objects held by a reported cycle rather -than being on one. In `"resurrecting"` mode the report is always empty: -resurrection breaks these cycles. The call suspends the process for a full -heap dump and parses it, so it takes seconds and briefly needs memory of the -order of the Java heap; it is a debugging tool, not for production. +`leakedOutsideCycles` counts leaked objects on no reported cycle: held by one, +or past the search limit (`truncated`, after 256 searches). Java is collected +twice before the dump, so objects only weak entries hold are gone and not +counted. In `"resurrecting"` mode the report is empty and no dump is taken: +resurrection breaks these cycles. + +The probe is available in debuggable builds, or with +`"wrapperLifetime": { "leakProbe": true }` in the app config; elsewhere it +throws. It suspends the whole process for the dump, writes the Java heap to +the app's cache directory (deleted once parsed, and swept by the next probe if +the process died first) and builds graphs of both heaps in native memory, so +it takes seconds; on the main thread that can trip an ANR. The snapshot and +the dump are taken one after the other, not atomically. Only the calling +runtime's wrappers are modeled: an object held only through another runtime's +(a worker's) wrappers can read as leaked, and probes in several runtimes run +one at a time. Cycles through JNI global references native code holds are +missed, since those are roots. The V8 object ids the snapshot assigns are +cleared afterwards unless something else (DevTools) holds heap snapshots. **Weak collections.** What could notice a new canary is whatever keys on the old one without keeping it, so in canary mode a builtin diff --git a/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js b/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js index 9965130f3..352b2a594 100644 --- a/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js +++ b/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js @@ -5,6 +5,8 @@ describe("Wrapper lifetime leak probe", function () { var Holder = com.tns.tests.WrapperHolder; // WeakRefs, so the spec itself keeps nothing it planted alive. var planted = []; + // Alive for the whole suite: what leaks is the entry's value. + var probeMap = new WeakMap(); var report; function cyclesThrough(edge) { @@ -39,6 +41,12 @@ describe("Wrapper lifetime leak probe", function () { Holder.held = held; planted.push(new WeakRef(held)); + // A WeakMap value referencing its own key: in canary mode the key is the + // object's lifetime key, which its state holds. + var mapped = new java.lang.Runnable({ run: function () {} }); + probeMap.set(mapped, { probeMapped: mapped }); + planted.push(new WeakRef(mapped)); + // A back reference through a WeakRef keeps nothing. var weak = new java.lang.Runnable({ run: function () {} }); weak.probeWeak = new WeakRef(weak); @@ -77,9 +85,16 @@ describe("Wrapper lifetime leak probe", function () { it("reports the heaps it joined (" + mode + ")", function () { expect(report.mode).toBe(mode); expect(Array.isArray(report.cycles)).toBe(true); + if (!canary) { + // Nothing to look for: no dump is taken. + expect(report.leaked).toBe(0); + expect(report.javaObjects).toBeUndefined(); + return; + } expect(report.javaObjects).toBeGreaterThan(0); expect(report.jsNodes).toBeGreaterThan(0); expect(report.probed).toBeGreaterThan(0); + expect(report.truncated).toBe(false); expect(report.durationMs).toBeGreaterThan(0); }); @@ -114,6 +129,16 @@ describe("Wrapper lifetime leak probe", function () { expect(cycles[0].path).toContain("--java.util.ArrayList.elementData-->"); }); + it("finds a cycle through a WeakMap value that references its key", function () { + var cycles = cyclesThrough("probeMapped"); + if (!canary) { + expect(cycles.length).toBe(0); + return; + } + expect(cycles.length).toBe(1); + expect(cycles[0].path).toContain("--WeakMap value-->"); + }); + it("does not report an object Java holds for a reason of its own", function () { expect(cyclesThrough("probeHeld").length).toBe(0); }); diff --git a/test-app/runtime/src/main/cpp/LeakProbe.cpp b/test-app/runtime/src/main/cpp/LeakProbe.cpp index bd7090973..c12485a43 100644 --- a/test-app/runtime/src/main/cpp/LeakProbe.cpp +++ b/test-app/runtime/src/main/cpp/LeakProbe.cpp @@ -159,6 +159,10 @@ bool ParseRecords(Reader& in, JavaHeap& heap, std::string& error) { const uint8_t* recordEnd = in.p + length; if (tag == 0x01) { // STRING uint64_t id = in.Id(); + if (!in.ok || in.p > recordEnd) { + error = "malformed HPROF string record"; + return false; + } heap.strings.emplace(id, std::string(reinterpret_cast(in.p), recordEnd - in.p)); } else if (tag == 0x02) { // LOAD CLASS @@ -447,6 +451,38 @@ std::string ToUtf8(Isolate* isolate, Local value) { return std::string(); } +// V8 gives a WeakMap value two edges with the same name, one from its key and +// one from the map's table (" / part of key (...) -> value (...) pair in +// WeakMap (table @)"). Only the key keeps the value alive, so the table's +// is not followed. +constexpr char kEphemeronEdgePrefix[] = "/ part of key ("; +constexpr char kEphemeronTableMarker[] = "(table @"; + +bool IsEphemeronEdge(const HeapGraphEdge* edge, std::string* name) { + if (edge->GetType() != HeapGraphEdge::kInternal) { + return false; + } + Local value = edge->GetName(); + if (value.IsEmpty() || !value->IsString()) { + return false; + } + *name = ArgConverter::ConvertToString(value.As()); + return name->find(kEphemeronEdgePrefix) != std::string::npos; +} + +bool IsEphemeronTableEdge(const HeapGraphNode* from, const HeapGraphEdge* edge) { + std::string name; + if (!IsEphemeronEdge(edge, &name)) { + return false; + } + size_t marker = name.rfind(kEphemeronTableMarker); + if (marker == std::string::npos) { + return false; + } + return strtoull(name.c_str() + marker + sizeof(kEphemeronTableMarker) - 1, nullptr, 10) == + from->GetId(); +} + // One followed edge: a JS edge, a Java field or element (label index), or a // cross-heap edge of the runtime (neither). struct EdgeRef { @@ -489,6 +525,9 @@ struct Graph { if (fromRoots && synthetic && runtimeHandleTargets.count(it->second) != 0) { continue; } + if (IsEphemeronTableEdge(js, edge)) { + continue; + } visit(it->second, EdgeRef{edge, kNone}); } auto cross = wrapperToJava.find(node); @@ -528,6 +567,10 @@ struct Graph { std::string EdgeLabel(uint32_t from, const EdgeRef& edge) const { if (edge.js != nullptr) { + std::string name; + if (IsEphemeronEdge(edge.js, &name)) { + return "WeakMap value"; + } return ToUtf8(isolate, edge.js->GetName()); } if (edge.javaLabel != kNone) { @@ -655,18 +698,40 @@ MaybeLocal LeakProbe::FindLeakCycles(Isolate* isolate) { return MaybeLocal(); }; + // Resurrection breaks these cycles: nothing to look for. + if (GetWrapperLifetimeMode() != WrapperLifetimeMode::kCanary) { + Local report = Object::New(isolate); + Set(isolate, context, report, "mode", + ArgConverter::ConvertToV8String(isolate, WrapperLifetimeModeName(GetWrapperLifetimeMode()))); + Set(isolate, context, report, "cycles", Array::New(isolate)); + Set(isolate, context, report, "leaked", Number::New(isolate, 0)); + Set(isolate, context, report, "leakedOutsideCycles", Number::New(isolate, 0)); + return scope.Escape(report); + } + // The snapshot first, before this call holds a handle to any wrapper or state: // a handle would root it. objectManager->FlushFinalizedBatchIfPending(); HeapProfiler* profiler = isolate->GetHeapProfiler(); + // A snapshot leaves V8 tracking object moves, at a cost on every scavenge, + // until its object ids are cleared. Cleared afterwards unless something else + // (DevTools) already holds snapshots and so relies on the ids. + bool clearIds = profiler->GetSnapshotCount() == 0; const HeapSnapshot* snapshot = profiler->TakeHeapSnapshot(); if (snapshot == nullptr) { return fail("could not take a heap snapshot"); } struct DeleteSnapshot { + HeapProfiler* profiler; const HeapSnapshot* snapshot; - ~DeleteSnapshot() { const_cast(snapshot)->Delete(); } - } deleteSnapshot{snapshot}; + bool clearIds; + ~DeleteSnapshot() { + const_cast(snapshot)->Delete(); + if (clearIds) { + profiler->ClearObjectIds(); + } + } + } deleteSnapshot{profiler, snapshot, clearIds}; std::vector entries = objectManager->GetLeakProbeEntries(); @@ -702,6 +767,11 @@ MaybeLocal LeakProbe::FindLeakCycles(Isolate* isolate) { return fail("the Java heap dump failed; see the log"); } std::string path = ArgConverter::jstringToString((jstring) pathRef); + if (path.empty()) { + return fail( + "available in debuggable builds, or with \"wrapperLifetime\": { \"leakProbe\": true } " + "in the app config"); + } struct RemoveFile { std::string path; ~RemoveFile() { unlink(path.c_str()); } @@ -813,6 +883,11 @@ MaybeLocal LeakProbe::FindLeakCycles(Isolate* isolate) { } } + // Each search can visit most of both heaps; past this many, leaked objects are + // counted but not explained. + constexpr uint32_t kMaxCycleSearches = 256; + uint32_t searches = 0; + bool truncated = false; Local cycles = Array::New(isolate); std::unordered_set covered; uint32_t cycleCount = 0; @@ -820,6 +895,10 @@ MaybeLocal LeakProbe::FindLeakCycles(Isolate* isolate) { if (covered.count(i) != 0) { continue; } + if (searches++ == kMaxCycleSearches) { + truncated = true; + break; + } std::vector pathLabels; std::vector nodeLabels; std::vector nodes = FindCycle(graph, reached, graph.JavaNode(javaOf[i]), @@ -851,6 +930,7 @@ MaybeLocal LeakProbe::FindLeakCycles(Isolate* isolate) { Set(isolate, context, report, "leaked", Number::New(isolate, static_cast(leaked.size()))); Set(isolate, context, report, "leakedOutsideCycles", Number::New(isolate, static_cast(leaked.size() - covered.size()))); + Set(isolate, context, report, "truncated", v8::Boolean::New(isolate, truncated)); Set(isolate, context, report, "probed", Number::New(isolate, static_cast(entries.size()))); Set(isolate, context, report, "jsNodes", Number::New(isolate, graph.jsCount)); Set(isolate, context, report, "javaObjects", diff --git a/test-app/runtime/src/main/cpp/NsBuiltinModules.cpp b/test-app/runtime/src/main/cpp/NsBuiltinModules.cpp index 325d86c0d..9574fe9e9 100644 --- a/test-app/runtime/src/main/cpp/NsBuiltinModules.cpp +++ b/test-app/runtime/src/main/cpp/NsBuiltinModules.cpp @@ -13,6 +13,7 @@ #include "Messaging.h" #include "ModuleInternalCallbacks.h" #include "NativeScriptAssert.h" +#include "NativeScriptException.h" #include "Runtime.h" #include "RuntimeState.h" #include "StructuredSerialization.h" @@ -187,9 +188,17 @@ void GetWrapperLifetimeStatsCallback(const FunctionCallbackInfo& info) { } void FindLeakCyclesCallback(const FunctionCallbackInfo& info) { - Local report; - if (LeakProbe::FindLeakCycles(info.GetIsolate()).ToLocal(&report)) { - info.GetReturnValue().Set(report); + Isolate* isolate = info.GetIsolate(); + try { + Local report; + if (LeakProbe::FindLeakCycles(isolate).ToLocal(&report)) { + info.GetReturnValue().Set(report); + } + } catch (NativeScriptException& e) { + e.ReThrowToV8(); + } catch (const std::exception& e) { + isolate->ThrowException(Exception::Error(ArgConverter::ConvertToV8String( + isolate, std::string("findLeakCycles: ") + e.what()))); } } diff --git a/test-app/runtime/src/main/java/com/tns/AppConfig.java b/test-app/runtime/src/main/java/com/tns/AppConfig.java index 3195cf81d..a89b8c42a 100644 --- a/test-app/runtime/src/main/java/com/tns/AppConfig.java +++ b/test-app/runtime/src/main/java/com/tns/AppConfig.java @@ -26,7 +26,8 @@ protected enum KnownKeys { UncaughtErrorPolicy("uncaughtErrorPolicy", "report"), InterfaceProxies("interfaceProxies", false), WrapperLifetimeMode("mode", "resurrecting"), - WrapperLifetimeScope("scope", "extended"); + WrapperLifetimeScope("scope", "extended"), + WrapperLifetimeLeakProbe("leakProbe", false); private final String name; private final Object defaultValue; @@ -182,6 +183,12 @@ private void readWrapperLifetime(Object config) { Log.w("JS", String.format("Unknown wrapperLifetime mode %s; using \"resurrecting\".", JSONObject.quote(String.valueOf(mode)))); } } + Object leakProbe = object.opt(KnownKeys.WrapperLifetimeLeakProbe.getName()); + if (leakProbe instanceof Boolean) { + values[KnownKeys.WrapperLifetimeLeakProbe.ordinal()] = leakProbe; + } else if (leakProbe != null) { + Log.w("JS", "wrapperLifetime leakProbe must be a boolean; using false."); + } Object scope = object.opt(KnownKeys.WrapperLifetimeScope.getName()); if (scope != null) { if ("extended".equals(scope) || "all".equals(scope)) { @@ -196,6 +203,10 @@ public String getWrapperLifetimeMode() { return (String)values[KnownKeys.WrapperLifetimeMode.ordinal()]; } + public boolean getWrapperLifetimeLeakProbe() { + return (boolean)values[KnownKeys.WrapperLifetimeLeakProbe.ordinal()]; + } + public String getWrapperLifetimeScope() { return (String)values[KnownKeys.WrapperLifetimeScope.ordinal()]; } diff --git a/test-app/runtime/src/main/java/com/tns/LeakProbe.java b/test-app/runtime/src/main/java/com/tns/LeakProbe.java index 636d54309..9f186c59b 100644 --- a/test-app/runtime/src/main/java/com/tns/LeakProbe.java +++ b/test-app/runtime/src/main/java/com/tns/LeakProbe.java @@ -1,13 +1,31 @@ package com.tns; +import java.io.File; + /** * Holds, during ns:runtime's findLeakCycles() heap dump only, the Java objects the analysis * maps to JS wrappers and states. The analysis finds this array through this class's static * field by name and does not count it as a reference. */ final class LeakProbe { + static final String DUMP_PREFIX = "ns-leak-probe"; + static Object[] pinned; private LeakProbe() { } + + // Dumps a killed process left behind; the native side deletes its own once parsed. + static void deleteStaleDumps() { + String tmp = System.getProperty("java.io.tmpdir"); + File[] files = tmp != null ? new File(tmp).listFiles() : null; + if (files == null) { + return; + } + for (File file : files) { + if (file.getName().startsWith(DUMP_PREFIX) && file.getName().endsWith(".hprof")) { + file.delete(); + } + } + } } diff --git a/test-app/runtime/src/main/java/com/tns/Runtime.java b/test-app/runtime/src/main/java/com/tns/Runtime.java index 6ebb672a3..8f24da488 100644 --- a/test-app/runtime/src/main/java/com/tns/Runtime.java +++ b/test-app/runtime/src/main/java/com/tns/Runtime.java @@ -1305,26 +1305,43 @@ private boolean strengthenInstance(int javaObjectID) { // ns:runtime findLeakCycles(): writes an HPROF dump with the objects of ids pinned in // LeakProbe.pinned, in order, so the native analysis can find them. Returns the dump's // path, or null if it could not be written. + // Returns "" when the probe is not enabled: only debuggable builds, or apps that set + // "wrapperLifetime": { "leakProbe": true }, since it suspends the process and writes the + // whole heap to disk. @RuntimeCallable private String dumpHeapForLeakProbe(int[] ids) { - Object[] objects = new Object[ids.length]; - for (int i = 0; i < ids.length; i++) { - objects[i] = objectTable.get(ids[i]); - } - java.io.File file = null; - try { - file = java.io.File.createTempFile("ns-leak-probe", ".hprof"); - LeakProbe.pinned = objects; - android.os.Debug.dumpHprofData(file.getAbsolutePath()); - return file.getAbsolutePath(); - } catch (Throwable t) { - android.util.Log.w("JS", "findLeakCycles could not dump the Java heap", t); - if (file != null) { - file.delete(); + boolean enabled = isDebuggable() || + (config != null && config.appConfig != null && config.appConfig.getWrapperLifetimeLeakProbe()); + if (!enabled) { + return ""; + } + // One probe at a time in the process: LeakProbe.pinned is shared by every runtime. + synchronized (LeakProbe.class) { + LeakProbe.deleteStaleDumps(); + // Objects only weak entries hold would read as leaked until Java collects them. + for (int i = 0; i < 2; i++) { + java.lang.Runtime.getRuntime().gc(); + System.runFinalization(); + } + Object[] objects = new Object[ids.length]; + for (int i = 0; i < ids.length; i++) { + objects[i] = objectTable.get(ids[i]); + } + java.io.File file = null; + try { + file = java.io.File.createTempFile(LeakProbe.DUMP_PREFIX, ".hprof"); + LeakProbe.pinned = objects; + android.os.Debug.dumpHprofData(file.getAbsolutePath()); + return file.getAbsolutePath(); + } catch (Throwable t) { + android.util.Log.w("JS", "findLeakCycles could not dump the Java heap", t); + if (file != null) { + file.delete(); + } + return null; + } finally { + LeakProbe.pinned = null; } - return null; - } finally { - LeakProbe.pinned = null; } } From 5114144459124018886534f494f8ad4192a5c6d8 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Fri, 20 Nov 2026 18:49:58 -0300 Subject: [PATCH 03/12] build(runtime): build the leak probe into debug runtimes only LeakProbe.cpp and the ObjectManager accessor it reads are compiled only with APPLICATION_IN_DEBUG, next to the inspector sources, so the release runtime carries none of it. ns:runtime keeps findLeakCycles in every build; in a release build it throws. The "wrapperLifetime": { "leakProbe": true } opt-in is gone: a release build has no probe to enable. --- docs/ns-builtin-modules.md | 7 +++---- test-app/runtime/CMakeLists.txt | 10 +++++++++- test-app/runtime/src/main/cpp/LeakProbe.cpp | 5 ----- test-app/runtime/src/main/cpp/LeakProbe.h | 2 +- test-app/runtime/src/main/cpp/NsBuiltinModules.cpp | 7 +++++++ test-app/runtime/src/main/cpp/ObjectManager.cpp | 2 ++ test-app/runtime/src/main/cpp/ObjectManager.h | 2 ++ .../runtime/src/main/java/com/tns/AppConfig.java | 13 +------------ test-app/runtime/src/main/java/com/tns/Runtime.java | 9 +-------- 9 files changed, 26 insertions(+), 31 deletions(-) diff --git a/docs/ns-builtin-modules.md b/docs/ns-builtin-modules.md index e5322e53a..1bd34eecc 100644 --- a/docs/ns-builtin-modules.md +++ b/docs/ns-builtin-modules.md @@ -105,7 +105,7 @@ wrapper. | `getConfig(key)` | Returns the current value of a config key. Throws `TypeError` on an unknown key. Readable from any isolate. | | `getWrapperLifetimeStats()` | Wrapper lifetime counters for the calling isolate (see [Wrapper lifetime](#wrapper-lifetime)). A fresh object on every call. | | `getWrapperClassCounts()` | Per Java class name (`` for a link whose class was never recorded), the calling isolate's live `wrappers`, the `canaries` among them and the canary `states` (see [Wrapper lifetime](#wrapper-lifetime)). | -| `findLeakCycles()` | Experimental and slow, debuggable builds or `"wrapperLifetime": { "leakProbe": true }` only: joins a V8 heap snapshot and a Java heap dump to find the JS ↔ Java cycles canary mode keeps alive (see [Wrapper lifetime](#wrapper-lifetime)). | +| `findLeakCycles()` | Experimental and slow, debug builds only (release builds throw): joins a V8 heap snapshot and a Java heap dump to find the JS ↔ Java cycles canary mode keeps alive (see [Wrapper lifetime](#wrapper-lifetime)). | | `escapeException(x)` | Returns an `Error` branded so that, when it is thrown back across a native callback boundary, the native caller receives the original native exception `x` carries rather than the runtime's wrapper exception. Idempotent: a branded error is returned unchanged. Throws `TypeError` when called with no argument. The global `interop.escapeException` is this same function (see [Legacy globals](#legacy-globals)). | *Platform note (Android):* the original exception is the Java throwable `x` @@ -294,9 +294,8 @@ twice before the dump, so objects only weak entries hold are gone and not counted. In `"resurrecting"` mode the report is empty and no dump is taken: resurrection breaks these cycles. -The probe is available in debuggable builds, or with -`"wrapperLifetime": { "leakProbe": true }` in the app config; elsewhere it -throws. It suspends the whole process for the dump, writes the Java heap to +The probe is built into debug runtimes only; in a release build the function +exists but throws `findLeakCycles is available in debug builds only`. It suspends the whole process for the dump, writes the Java heap to the app's cache directory (deleted once parsed, and swept by the next probe if the process died first) and builds graphs of both heaps in native memory, so it takes seconds; on the main thread that can trip an ANR. The snapshot and diff --git a/test-app/runtime/CMakeLists.txt b/test-app/runtime/CMakeLists.txt index 31a02f081..96ae3d008 100644 --- a/test-app/runtime/CMakeLists.txt +++ b/test-app/runtime/CMakeLists.txt @@ -151,9 +151,17 @@ if (NOT OPTIMIZED_BUILD OR OPTIMIZED_WITH_INSPECTOR_BUILD) src/main/cpp/v8_inspector/ns-v8-tracing-agent-impl.cpp src/main/cpp/v8_inspector/Utils.cpp ) + + # ns:runtime findLeakCycles(); release builds keep a stub that throws. + set( + DEBUG_TOOL_SOURCES + + src/main/cpp/LeakProbe.cpp + ) else () # When building in Release mode we do not include the V8 inspector sources set(INSPECTOR_SOURCES) + set(DEBUG_TOOL_SOURCES) endif () # The Node-API test addons back the NapiTests/NapiCoverageTests specs of the @@ -200,7 +208,6 @@ add_library( src/main/cpp/FieldAccessor.cpp src/main/cpp/File.cpp src/main/cpp/Interop.cpp - src/main/cpp/LeakProbe.cpp src/main/cpp/IsolateTracked.cpp src/main/cpp/JEnv.cpp src/main/cpp/DesugaredInterfaceCompanionClassNameResolver.cpp @@ -270,6 +277,7 @@ add_library( # V8 inspector source files will be included only in Release mode ${INSPECTOR_SOURCES} + ${DEBUG_TOOL_SOURCES} # Node-API test addons, local Debug builds only ${NAPI_TEST_SOURCES} diff --git a/test-app/runtime/src/main/cpp/LeakProbe.cpp b/test-app/runtime/src/main/cpp/LeakProbe.cpp index c12485a43..fc904a80b 100644 --- a/test-app/runtime/src/main/cpp/LeakProbe.cpp +++ b/test-app/runtime/src/main/cpp/LeakProbe.cpp @@ -767,11 +767,6 @@ MaybeLocal LeakProbe::FindLeakCycles(Isolate* isolate) { return fail("the Java heap dump failed; see the log"); } std::string path = ArgConverter::jstringToString((jstring) pathRef); - if (path.empty()) { - return fail( - "available in debuggable builds, or with \"wrapperLifetime\": { \"leakProbe\": true } " - "in the app config"); - } struct RemoveFile { std::string path; ~RemoveFile() { unlink(path.c_str()); } diff --git a/test-app/runtime/src/main/cpp/LeakProbe.h b/test-app/runtime/src/main/cpp/LeakProbe.h index 7f2f7a9b1..c0fedbd1a 100644 --- a/test-app/runtime/src/main/cpp/LeakProbe.h +++ b/test-app/runtime/src/main/cpp/LeakProbe.h @@ -22,7 +22,7 @@ namespace tns { // leaked group a cycle through it, with a signature free of ids. // // Slow by design: it suspends the process for a full heap dump and parses it. -// A diagnostic, not something to run in production. +// Built into debug runtimes only (APPLICATION_IN_DEBUG). class LeakProbe { public: // The report object, or empty with a JS exception pending. diff --git a/test-app/runtime/src/main/cpp/NsBuiltinModules.cpp b/test-app/runtime/src/main/cpp/NsBuiltinModules.cpp index 9574fe9e9..d3f6de13d 100644 --- a/test-app/runtime/src/main/cpp/NsBuiltinModules.cpp +++ b/test-app/runtime/src/main/cpp/NsBuiltinModules.cpp @@ -9,7 +9,9 @@ #include "BuiltinLoader.h" #include "CallbackHandlers.h" #include "Interop.h" +#ifdef APPLICATION_IN_DEBUG #include "LeakProbe.h" +#endif #include "Messaging.h" #include "ModuleInternalCallbacks.h" #include "NativeScriptAssert.h" @@ -189,6 +191,10 @@ void GetWrapperLifetimeStatsCallback(const FunctionCallbackInfo& info) { void FindLeakCyclesCallback(const FunctionCallbackInfo& info) { Isolate* isolate = info.GetIsolate(); +#ifndef APPLICATION_IN_DEBUG + isolate->ThrowException(Exception::Error(ArgConverter::ConvertToV8String( + isolate, "findLeakCycles is available in debug builds only"))); +#else try { Local report; if (LeakProbe::FindLeakCycles(isolate).ToLocal(&report)) { @@ -200,6 +206,7 @@ void FindLeakCyclesCallback(const FunctionCallbackInfo& info) { isolate->ThrowException(Exception::Error(ArgConverter::ConvertToV8String( isolate, std::string("findLeakCycles: ") + e.what()))); } +#endif } void GetWrapperClassCountsCallback(const FunctionCallbackInfo& info) { diff --git a/test-app/runtime/src/main/cpp/ObjectManager.cpp b/test-app/runtime/src/main/cpp/ObjectManager.cpp index 701ad6849..eb0b2c1b6 100644 --- a/test-app/runtime/src/main/cpp/ObjectManager.cpp +++ b/test-app/runtime/src/main/cpp/ObjectManager.cpp @@ -1541,6 +1541,7 @@ jobject ObjectManager::JavaObjectOrNull(int javaObjectID) { return object; } +#ifdef APPLICATION_IN_DEBUG std::vector ObjectManager::GetLeakProbeEntries() { auto isolate = m_isolate; std::unordered_map names; @@ -1597,6 +1598,7 @@ std::vector ObjectManager::GetLeakProbeEntries() }); return entries; } +#endif void ObjectManager::ReleaseCollectedCanaryStates() { if (!m_deathInboxOpen) { diff --git a/test-app/runtime/src/main/cpp/ObjectManager.h b/test-app/runtime/src/main/cpp/ObjectManager.h index 07aa0a31d..edf0c009d 100644 --- a/test-app/runtime/src/main/cpp/ObjectManager.h +++ b/test-app/runtime/src/main/cpp/ObjectManager.h @@ -284,6 +284,7 @@ class ObjectManager { // Per Java class name (dotted), for ns:runtime's getWrapperClassCounts(). std::map GetWrapperClassCounts(); +#ifdef APPLICATION_IN_DEBUG // The runtime's cross-heap edges, for ns:runtime's findLeakCycles() // (LeakProbe). One entry per id with a live wrapper or a canary record. struct LeakProbeEntry { @@ -302,6 +303,7 @@ class ObjectManager { // In the caller's HandleScope. std::vector GetLeakProbeEntries(); +#endif jobject GetJavaRuntimeObject() const { return m_javaRuntimeObject; } diff --git a/test-app/runtime/src/main/java/com/tns/AppConfig.java b/test-app/runtime/src/main/java/com/tns/AppConfig.java index a89b8c42a..3195cf81d 100644 --- a/test-app/runtime/src/main/java/com/tns/AppConfig.java +++ b/test-app/runtime/src/main/java/com/tns/AppConfig.java @@ -26,8 +26,7 @@ protected enum KnownKeys { UncaughtErrorPolicy("uncaughtErrorPolicy", "report"), InterfaceProxies("interfaceProxies", false), WrapperLifetimeMode("mode", "resurrecting"), - WrapperLifetimeScope("scope", "extended"), - WrapperLifetimeLeakProbe("leakProbe", false); + WrapperLifetimeScope("scope", "extended"); private final String name; private final Object defaultValue; @@ -183,12 +182,6 @@ private void readWrapperLifetime(Object config) { Log.w("JS", String.format("Unknown wrapperLifetime mode %s; using \"resurrecting\".", JSONObject.quote(String.valueOf(mode)))); } } - Object leakProbe = object.opt(KnownKeys.WrapperLifetimeLeakProbe.getName()); - if (leakProbe instanceof Boolean) { - values[KnownKeys.WrapperLifetimeLeakProbe.ordinal()] = leakProbe; - } else if (leakProbe != null) { - Log.w("JS", "wrapperLifetime leakProbe must be a boolean; using false."); - } Object scope = object.opt(KnownKeys.WrapperLifetimeScope.getName()); if (scope != null) { if ("extended".equals(scope) || "all".equals(scope)) { @@ -203,10 +196,6 @@ public String getWrapperLifetimeMode() { return (String)values[KnownKeys.WrapperLifetimeMode.ordinal()]; } - public boolean getWrapperLifetimeLeakProbe() { - return (boolean)values[KnownKeys.WrapperLifetimeLeakProbe.ordinal()]; - } - public String getWrapperLifetimeScope() { return (String)values[KnownKeys.WrapperLifetimeScope.ordinal()]; } diff --git a/test-app/runtime/src/main/java/com/tns/Runtime.java b/test-app/runtime/src/main/java/com/tns/Runtime.java index 8f24da488..4cb29c9a4 100644 --- a/test-app/runtime/src/main/java/com/tns/Runtime.java +++ b/test-app/runtime/src/main/java/com/tns/Runtime.java @@ -1305,16 +1305,9 @@ private boolean strengthenInstance(int javaObjectID) { // ns:runtime findLeakCycles(): writes an HPROF dump with the objects of ids pinned in // LeakProbe.pinned, in order, so the native analysis can find them. Returns the dump's // path, or null if it could not be written. - // Returns "" when the probe is not enabled: only debuggable builds, or apps that set - // "wrapperLifetime": { "leakProbe": true }, since it suspends the process and writes the - // whole heap to disk. + // Called only by debug runtimes, which carry the native probe. @RuntimeCallable private String dumpHeapForLeakProbe(int[] ids) { - boolean enabled = isDebuggable() || - (config != null && config.appConfig != null && config.appConfig.getWrapperLifetimeLeakProbe()); - if (!enabled) { - return ""; - } // One probe at a time in the process: LeakProbe.pinned is shared by every runtime. synchronized (LeakProbe.class) { LeakProbe.deleteStaleDumps(); From b5eae2425064e80dee953cc02f0d5dd50d0ac9ce Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Fri, 20 Nov 2026 18:59:11 -0300 Subject: [PATCH 04/12] fix(runtime): report leak probe results as own properties --- test-app/runtime/src/main/cpp/LeakProbe.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-app/runtime/src/main/cpp/LeakProbe.cpp b/test-app/runtime/src/main/cpp/LeakProbe.cpp index fc904a80b..86ca0597a 100644 --- a/test-app/runtime/src/main/cpp/LeakProbe.cpp +++ b/test-app/runtime/src/main/cpp/LeakProbe.cpp @@ -673,14 +673,14 @@ std::string Signature(const std::vector& labels) { void Set(Isolate* isolate, Local context, Local target, const char* name, Local value) { - (void) target->Set(context, ArgConverter::ConvertToV8String(isolate, name), value); + (void) target->CreateDataProperty(context, ArgConverter::ConvertToV8String(isolate, name), value); } Local ToArray(Isolate* isolate, Local context, const std::vector& items) { Local array = Array::New(isolate, static_cast(items.size())); for (uint32_t i = 0; i < items.size(); i++) { - (void) array->Set(context, i, ArgConverter::ConvertToV8String(isolate, items[i])); + (void) array->CreateDataProperty(context, i, ArgConverter::ConvertToV8String(isolate, items[i])); } return array; } From 38f6c2e936f757656b32f3c56f68658f1faa5313 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Fri, 20 Nov 2026 20:05:20 -0300 Subject: [PATCH 05/12] fix(runtime): leak probe review fixes - Settle before the snapshot (a full V8 collection and a flush), so wrappers that died since the last collection no longer read as holding their Java objects, and a queued wrapper never does. - Model the Java object a resurrected wrapper lives for: an edge from it to the wrapper, whose handle is not a root. - Keep the runtime's handles out of the roots even for records of objects the probe's own Java collection let go of. - Only "(Global handles)" edges to the runtime's handles are dropped. - No System.runFinalization() on the JS thread: a finalizer reaching JS would wait on it. - Keep V8 object ids while DevTools is connected. - Move the Java side to a debugTools source set the optimized runtime does not get; the native side builds the pinned array itself. - Delete only stale dumps, report a search cut short as truncated, scope edge-name handles, and fix two HPROF error messages. - Document the report's fields. --- docs/ns-builtin-modules.md | 13 +- .../app/tests/testWrapperLifetimeLeaks.js | 17 ++- test-app/runtime/build.gradle | 4 + .../debugTools/java/com/tns/LeakProbe.java | 70 +++++++++ .../src/main/cpp/JsV8InspectorClient.h | 6 + test-app/runtime/src/main/cpp/LeakProbe.cpp | 140 ++++++++++++------ .../runtime/src/main/cpp/ObjectManager.cpp | 30 +++- test-app/runtime/src/main/cpp/ObjectManager.h | 9 +- .../src/main/java/com/tns/LeakProbe.java | 31 ---- .../src/main/java/com/tns/Runtime.java | 36 ----- 10 files changed, 226 insertions(+), 130 deletions(-) create mode 100644 test-app/runtime/src/debugTools/java/com/tns/LeakProbe.java delete mode 100644 test-app/runtime/src/main/java/com/tns/LeakProbe.java diff --git a/docs/ns-builtin-modules.md b/docs/ns-builtin-modules.md index 1bd34eecc..37e6a7d77 100644 --- a/docs/ns-builtin-modules.md +++ b/docs/ns-builtin-modules.md @@ -283,15 +283,18 @@ records), the Java heap from an HPROF dump (`android.os.Debug.dumpHprofData`; its GC roots minus the runtime's object table, `Reference` referents skipped), and the runtime's own edges between them (a live wrapper holds its Java object; a Java object holds its canary record: state, prototype and implementation -object). A `WeakMap` value is followed from its key only. A Java object the +object; a Java object held weakly for a resurrected wrapper holds that wrapper). A `WeakMap` value is followed from its key only. A Java object the runtime holds for a live wrapper, and that nothing else reaches, is leaked. For each, the report gives the path from the Java object to its own wrapper through fields, properties and the runtime's edges, a `signature` (the path's node labels from their smallest rotation, free of ids), and the Java classes of the leaked objects on it (`members`). `leakedOutsideCycles` counts leaked objects on no reported cycle: held by one, -or past the search limit (`truncated`, after 256 searches). Java is collected -twice before the dump, so objects only weak entries hold are gone and not -counted. In `"resurrecting"` mode the report is empty and no dump is taken: +or past the search limits (`truncated`: after 256 searches, or a search that +visited 2,000,000 nodes). V8 collects and the runtime settles the wrappers that +died before the snapshot, and Java is collected twice before the dump, so +objects only weak entries hold are gone and not counted. The report also gives +`mode`, `probed` (the Java objects modeled), `jsNodes`, `javaObjects`, +`dumpBytes` and `durationMs`. In `"resurrecting"` mode the report is empty and no dump is taken: resurrection breaks these cycles. The probe is built into debug runtimes only; in a release build the function @@ -304,7 +307,7 @@ runtime's wrappers are modeled: an object held only through another runtime's (a worker's) wrappers can read as leaked, and probes in several runtimes run one at a time. Cycles through JNI global references native code holds are missed, since those are roots. The V8 object ids the snapshot assigns are -cleared afterwards unless something else (DevTools) holds heap snapshots. +cleared afterwards, unless DevTools is connected and relies on them. **Weak collections.** What could notice a new canary is whatever keys on the old one without keeping it, so in canary mode a builtin diff --git a/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js b/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js index 352b2a594..48eed3914 100644 --- a/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js +++ b/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js @@ -52,8 +52,6 @@ describe("Wrapper lifetime leak probe", function () { weak.probeWeak = new WeakRef(weak); })(); setTimeout(function () { - gc(); - java.lang.System.nanoTime(); report = runtime.findLeakCycles(); console.log("leak probe: " + JSON.stringify({ leaked: report.leaked, @@ -106,7 +104,7 @@ describe("Wrapper lifetime leak probe", function () { return; } expect(cycles.length).toBe(2); - expect(cycles[0].path).toContain("--ns:record-->"); + expect(cycles[0].path).toContain("--ns:kept-->"); expect(cycles[0].path).toContain("--ns:wrapper-->"); expect(cycles[0].members.length).toBe(1); }); @@ -146,4 +144,17 @@ describe("Wrapper lifetime leak probe", function () { it("does not report a back reference through a WeakRef", function () { expect(cyclesThrough("probeWeak").length).toBe(0); }); + + it("settles the wrappers that died since the last collection first", function () { + if (!canary) { + return; + } + (function () { + for (var i = 0; i < 50; i++) { + new java.lang.StringBuilder("probeGarbage"); + } + })(); + // Read as leaked if unsettled: their Java objects are still held for them. + expect(runtime.findLeakCycles().leaked).toBeLessThan(report.leaked + 50); + }, 20000); }); diff --git a/test-app/runtime/build.gradle b/test-app/runtime/build.gradle index fd4e5c4cb..bd71ec331 100644 --- a/test-app/runtime/build.gradle +++ b/test-app/runtime/build.gradle @@ -99,6 +99,10 @@ android { // embed runtime binding generator in runtime, while keeping it in a separate project java.srcDirs = [bindingGeneratorSourcePath, defaultSrcPath] + // The Java side of debug-only native tools (CMake's DEBUG_TOOL_SOURCES). + if (!optimized) { + java.srcDirs += "src/debugTools/java" + } } } diff --git a/test-app/runtime/src/debugTools/java/com/tns/LeakProbe.java b/test-app/runtime/src/debugTools/java/com/tns/LeakProbe.java new file mode 100644 index 000000000..f58a8fb25 --- /dev/null +++ b/test-app/runtime/src/debugTools/java/com/tns/LeakProbe.java @@ -0,0 +1,70 @@ +package com.tns; + +import java.io.File; + +/** + * ns:runtime findLeakCycles()'s Java side, in debug runtimes only. During the heap dump, + * {@link #pinned} holds the Java objects the analysis maps to JS wrappers and states; the + * analysis finds that array through this static field by name and does not count it as a + * reference. + */ +final class LeakProbe { + static final String DUMP_PREFIX = "ns-leak-probe"; + + // Older than any dump a probe of this process or another one may still be parsing. + private static final long STALE_DUMP_AGE_MS = 10 * 60 * 1000; + + static Object[] pinned; + + private LeakProbe() { + } + + // Before the probe takes strong references: objects only weak entries hold would + // otherwise read as leaked until Java collects them. + static void collectGarbage() { + for (int i = 0; i < 2; i++) { + java.lang.Runtime.getRuntime().gc(); + } + } + + // Writes an HPROF dump with objects pinned in order; returns its path, or null if it + // could not be written. The caller deletes the dump once parsed. + static String dump(Object[] objects) { + // pinned is shared by every runtime of the process. + synchronized (LeakProbe.class) { + deleteStaleDumps(); + File file = null; + try { + file = File.createTempFile(DUMP_PREFIX, ".hprof"); + pinned = objects; + android.os.Debug.dumpHprofData(file.getAbsolutePath()); + return file.getAbsolutePath(); + } catch (Throwable t) { + android.util.Log.w("JS", "findLeakCycles could not dump the Java heap", t); + if (file != null) { + file.delete(); + } + return null; + } finally { + pinned = null; + } + } + } + + // Dumps a killed process left behind. + private static void deleteStaleDumps() { + String tmp = System.getProperty("java.io.tmpdir"); + File[] files = tmp != null ? new File(tmp).listFiles() : null; + if (files == null) { + return; + } + long staleBefore = System.currentTimeMillis() - STALE_DUMP_AGE_MS; + for (File file : files) { + String name = file.getName(); + if (name.startsWith(DUMP_PREFIX) && name.endsWith(".hprof") + && file.lastModified() < staleBefore) { + file.delete(); + } + } + } +} diff --git a/test-app/runtime/src/main/cpp/JsV8InspectorClient.h b/test-app/runtime/src/main/cpp/JsV8InspectorClient.h index 37b278a27..4e169cb40 100644 --- a/test-app/runtime/src/main/cpp/JsV8InspectorClient.h +++ b/test-app/runtime/src/main/cpp/JsV8InspectorClient.h @@ -75,6 +75,12 @@ class JsV8InspectorClient : V8InspectorClient, v8_inspector::V8Inspector::Channe return JsV8InspectorClient::GetInstance()->isConnected_; } + // As inspectorIsConnected, without creating the client. + static bool IsConnected() { + JsV8InspectorClient* client = GetInstanceIfCreated(); + return client != nullptr && client->isConnected_; + } + static std::map*> Domains; private: diff --git a/test-app/runtime/src/main/cpp/LeakProbe.cpp b/test-app/runtime/src/main/cpp/LeakProbe.cpp index 86ca0597a..3cb185ef8 100644 --- a/test-app/runtime/src/main/cpp/LeakProbe.cpp +++ b/test-app/runtime/src/main/cpp/LeakProbe.cpp @@ -16,6 +16,7 @@ #include "ArgConverter.h" #include "JEnv.h" +#include "JsV8InspectorClient.h" #include "JniLocalRef.h" #include "ObjectManager.h" #include "Runtime.h" @@ -154,7 +155,8 @@ bool ParseRecords(Reader& in, JavaHeap& heap, std::string& error) { in.U4(); // time uint32_t length = in.U4(); if (!in.Has(length)) { - break; + error = "truncated HPROF record"; + return false; } const uint8_t* recordEnd = in.p + length; if (tag == 0x01) { // STRING @@ -277,7 +279,9 @@ bool ParseRecords(Reader& in, JavaHeap& heap, std::string& error) { break; } default: - error = "unknown HPROF heap dump subrecord 0x" + std::to_string(subtag); + char hex[3]; + snprintf(hex, sizeof(hex), "%02x", subtag); + error = std::string("unknown HPROF heap dump subrecord 0x") + hex; return false; } } @@ -288,6 +292,9 @@ bool ParseRecords(Reader& in, JavaHeap& heap, std::string& error) { } in.p = recordEnd; } + if (!in.ok) { + error = "truncated HPROF record"; + } return in.ok; } @@ -462,6 +469,8 @@ bool IsEphemeronEdge(const HeapGraphEdge* edge, std::string* name) { if (edge->GetType() != HeapGraphEdge::kInternal) { return false; } + // GetName makes a handle per call, and the walks call it once per internal edge. + HandleScope handles(Isolate::GetCurrent()); Local value = edge->GetName(); if (value.IsEmpty() || !value->IsString()) { return false; @@ -497,12 +506,13 @@ struct Graph { uint32_t jsCount = 0; std::unordered_map jsIndex; std::vector jsNodes; - // JS root edges to these snapshot nodes are the runtime's handles to canary - // records, not real roots. + // "(Global handles)": its edges to runtimeHandleTargets are the runtime's handles + // to canary records and resurrected wrappers, not real roots. + uint32_t globalHandles = kNone; std::unordered_set runtimeHandleTargets; // Cross-heap edges. std::unordered_map> wrapperToJava; - std::unordered_map> javaToRecord; + std::unordered_map> javaToJs; uint32_t JavaNode(uint32_t index) const { return jsCount + index; } bool IsJava(uint32_t node) const { return node >= jsCount; } @@ -512,7 +522,6 @@ struct Graph { void ForEachEdge(uint32_t node, bool fromRoots, F&& visit) const { if (!IsJava(node)) { const HeapGraphNode* js = jsNodes[node]; - bool synthetic = js->GetType() == HeapGraphNode::kSynthetic; for (int i = 0; i < js->GetChildrenCount(); i++) { const HeapGraphEdge* edge = js->GetChild(i); if (edge->GetType() == HeapGraphEdge::kWeak || edge->GetType() == HeapGraphEdge::kShortcut) { @@ -522,7 +531,7 @@ struct Graph { if (it == jsIndex.end()) { continue; } - if (fromRoots && synthetic && runtimeHandleTargets.count(it->second) != 0) { + if (fromRoots && node == globalHandles && runtimeHandleTargets.count(it->second) != 0) { continue; } if (IsEphemeronTableEdge(js, edge)) { @@ -542,8 +551,8 @@ struct Graph { for (uint32_t e = java.edgeStart[index]; e < java.edgeStart[index + 1]; e++) { visit(JavaNode(java.edgeTo[e]), EdgeRef{nullptr, java.edgeLabel[e]}); } - auto cross = javaToRecord.find(node); - if (cross != javaToRecord.end()) { + auto cross = javaToJs.find(node); + if (cross != javaToJs.end()) { for (uint32_t target : cross->second) { visit(target, EdgeRef{}); } @@ -576,7 +585,7 @@ struct Graph { if (edge.javaLabel != kNone) { return java.labels[edge.javaLabel]; } - return IsJava(from) ? "ns:record" : "ns:wrapper"; + return IsJava(from) ? "ns:kept" : "ns:wrapper"; } }; @@ -604,10 +613,11 @@ std::vector Reach(const Graph& graph, uint32_t jsRoot, // A shortest path from `from` to `to` through unreached nodes, closed by the // runtime's edge from `to` (a wrapper) back to `from` (its Java object), as -// alternating node and edge labels; empty when there is none within the limit. +// alternating node and edge labels; empty when there is none, or none within the +// limit, which sets *limited. std::vector FindCycle(const Graph& graph, const std::vector& reached, uint32_t from, uint32_t to, std::vector* path, - std::vector* nodeLabels) { + std::vector* nodeLabels, bool* limited) { constexpr size_t kVisitLimit = 2'000'000; std::unordered_map> parent; std::deque queue; @@ -629,6 +639,9 @@ std::vector FindCycle(const Graph& graph, const std::vector& }); } if (!found) { + if (!queue.empty()) { + *limited = true; + } return {}; } std::vector> steps; // node, edge out of it @@ -710,13 +723,16 @@ MaybeLocal LeakProbe::FindLeakCycles(Isolate* isolate) { } // The snapshot first, before this call holds a handle to any wrapper or state: - // a handle would root it. + // a handle would root it. Settled before it: a wrapper the snapshot's own + // collection finds dead would still read as holding its Java object. + isolate->LowMemoryNotification(); objectManager->FlushFinalizedBatchIfPending(); HeapProfiler* profiler = isolate->GetHeapProfiler(); // A snapshot leaves V8 tracking object moves, at a cost on every scavenge, - // until its object ids are cleared. Cleared afterwards unless something else - // (DevTools) already holds snapshots and so relies on the ids. - bool clearIds = profiler->GetSnapshotCount() == 0; + // until its object ids are cleared. DevTools relies on those ids, and while it + // records allocations on the map behind them, so they are kept while it is + // connected. + bool clearIds = profiler->GetSnapshotCount() == 0 && !JsV8InspectorClient::IsConnected(); const HeapSnapshot* snapshot = profiler->TakeHeapSnapshot(); if (snapshot == nullptr) { return fail("could not take a heap snapshot"); @@ -736,29 +752,40 @@ MaybeLocal LeakProbe::FindLeakCycles(Isolate* isolate) { std::vector entries = objectManager->GetLeakProbeEntries(); // The Java heap, with the entries' objects pinned in order. + struct ProbeMethods { + jclass clazz = nullptr; + jmethodID collectGarbage = nullptr; + jmethodID dump = nullptr; + }; + static const ProbeMethods methods = [] { + JEnv env; + JNIEnv* raw = env; + ProbeMethods found; + found.clazz = env.FindClass("com/tns/LeakProbe"); + if (found.clazz != nullptr) { + found.collectGarbage = raw->GetStaticMethodID(found.clazz, "collectGarbage", "()V"); + found.dump = raw->GetStaticMethodID(found.clazz, "dump", + "([Ljava/lang/Object;)Ljava/lang/String;"); + } + raw->ExceptionClear(); + return found; + }(); + if (methods.collectGarbage == nullptr || methods.dump == nullptr) { + return fail("this runtime has no com.tns.LeakProbe"); + } JEnv env; JNIEnv* raw = env; - JniLocalRef ids(raw->NewIntArray(static_cast(entries.size()))); - if (ids.IsNull()) { + raw->CallStaticVoidMethod(methods.clazz, methods.collectGarbage); + if (raw->ExceptionCheck()) { raw->ExceptionClear(); - return fail("out of memory"); + return fail("the Java collection failed"); } - { - std::vector values; - values.reserve(entries.size()); - for (const auto& entry : entries) { - values.push_back(entry.id); - } - raw->SetIntArrayRegion((jintArray) ids, 0, static_cast(values.size()), values.data()); - } - static jmethodID dumpMethod = nullptr; - if (dumpMethod == nullptr) { - JniLocalRef runtimeClass(raw->GetObjectClass(objectManager->GetJavaRuntimeObject())); - dumpMethod = raw->GetMethodID((jclass) runtimeClass, "dumpHeapForLeakProbe", - "([I)Ljava/lang/String;"); + JniLocalRef objects(objectManager->LeakProbeObjects(entries)); + if (objects.IsNull()) { + return fail("out of memory"); } - JniLocalRef pathRef(raw->CallObjectMethod(objectManager->GetJavaRuntimeObject(), dumpMethod, - (jintArray) ids)); + JniLocalRef pathRef( + raw->CallStaticObjectMethod(methods.clazz, methods.dump, (jobjectArray) objects)); if (raw->ExceptionCheck()) { raw->ExceptionClear(); return fail("the Java heap dump failed"); @@ -792,6 +819,12 @@ MaybeLocal LeakProbe::FindLeakCycles(Isolate* isolate) { const HeapGraphNode* node = snapshot->GetNode(static_cast(i)); graph.jsNodes.push_back(node); graph.jsIndex.emplace(node, i); + if (graph.globalHandles == kNone && node->GetType() == HeapGraphNode::kSynthetic) { + HandleScope handles(isolate); + if (ToUtf8(isolate, node->GetName()) == "(Global handles)") { + graph.globalHandles = i; + } + } } auto jsNodeOf = [&](Local value) -> uint32_t { if (value.IsEmpty() || !value->IsObject()) { @@ -820,22 +853,30 @@ MaybeLocal LeakProbe::FindLeakCycles(Isolate* isolate) { for (size_t i = 0; i < entries.size(); i++) { const auto& entry = entries[i]; + // The runtime's handles are never roots, even to what an object the probe's own + // Java collection let go of kept: the next flush lets go of it too. + std::vector kept; + for (Local value : entry.recordValues) { + uint32_t node = jsNodeOf(value); + if (node != kNone) { + kept.push_back(node); + } + } + uint32_t wrapperNode = entry.wrapper.IsEmpty() ? kNone : jsNodeOf(entry.wrapper); + if (wrapperNode != kNone && entry.javaKeepsWrapper) { + kept.push_back(wrapperNode); + } + graph.runtimeHandleTargets.insert(kept.begin(), kept.end()); if (javaOf[i] == kNone) { continue; } uint32_t javaNode = graph.JavaNode(javaOf[i]); - if (!entry.wrapper.IsEmpty() && entry.wrapperHoldsJava) { - uint32_t wrapperNode = jsNodeOf(entry.wrapper); - if (wrapperNode != kNone) { - graph.wrapperToJava[wrapperNode].push_back(javaNode); - } + if (wrapperNode != kNone && entry.wrapperHoldsJava) { + graph.wrapperToJava[wrapperNode].push_back(javaNode); } - for (Local value : entry.recordValues) { - uint32_t node = jsNodeOf(value); - if (node != kNone) { - graph.javaToRecord[javaNode].push_back(node); - graph.runtimeHandleTargets.insert(node); - } + if (!kept.empty()) { + auto& edges = graph.javaToJs[javaNode]; + edges.insert(edges.end(), kept.begin(), kept.end()); } } @@ -862,7 +903,6 @@ MaybeLocal LeakProbe::FindLeakCycles(Isolate* isolate) { std::vector leaked; std::unordered_set leakedSet; std::vector wrapperOf(entries.size(), kNone); - bool canaryMode = GetWrapperLifetimeMode() == WrapperLifetimeMode::kCanary; for (size_t i = 0; i < entries.size(); i++) { if (javaOf[i] == kNone) { continue; @@ -872,7 +912,7 @@ MaybeLocal LeakProbe::FindLeakCycles(Isolate* isolate) { if (!entries[i].wrapper.IsEmpty() && entries[i].wrapperHoldsJava) { wrapperOf[i] = jsNodeOf(entries[i].wrapper); } - if (canaryMode && !reached[javaNode] && wrapperOf[i] != kNone) { + if (!reached[javaNode] && wrapperOf[i] != kNone) { leaked.push_back(i); leakedSet.insert(i); } @@ -896,8 +936,10 @@ MaybeLocal LeakProbe::FindLeakCycles(Isolate* isolate) { } std::vector pathLabels; std::vector nodeLabels; + bool limited = false; std::vector nodes = FindCycle(graph, reached, graph.JavaNode(javaOf[i]), - wrapperOf[i], &pathLabels, &nodeLabels); + wrapperOf[i], &pathLabels, &nodeLabels, &limited); + truncated = truncated || limited; if (nodes.empty()) { continue; } @@ -914,7 +956,7 @@ MaybeLocal LeakProbe::FindLeakCycles(Isolate* isolate) { Set(isolate, context, cycle, "signature", ArgConverter::ConvertToV8String(isolate, signature)); Set(isolate, context, cycle, "path", ToArray(isolate, context, pathLabels)); Set(isolate, context, cycle, "members", ToArray(isolate, context, members)); - (void) cycles->Set(context, cycleCount++, cycle); + (void) cycles->CreateDataProperty(context, cycleCount++, cycle); } auto elapsed = std::chrono::duration(std::chrono::steady_clock::now() - started); diff --git a/test-app/runtime/src/main/cpp/ObjectManager.cpp b/test-app/runtime/src/main/cpp/ObjectManager.cpp index eb0b2c1b6..2cda07a71 100644 --- a/test-app/runtime/src/main/cpp/ObjectManager.cpp +++ b/test-app/runtime/src/main/cpp/ObjectManager.cpp @@ -1565,9 +1565,10 @@ std::vector ObjectManager::GetLeakProbeEntries() entry.id = static_cast(state->javaObjectID); entry.className = nameOf(state->objectClazz); entry.wrapper = state->target->Get(isolate); - entry.canary = state->canary; - // A resurrecting wrapper's object turns weak once its wrapper was first collected. - entry.wrapperHoldsJava = state->canary || !state->javaWeakened; + // A resurrecting wrapper's object turns weak once its wrapper was first collected; + // one still queued from its first collection is garbage the next flush settles. + entry.wrapperHoldsJava = state->canary || (!state->javaWeakened && !state->queued); + entry.javaKeepsWrapper = !state->canary && state->javaWeakened; byId.emplace(entry.id, entries.size()); entries.push_back(std::move(entry)); }); @@ -1582,7 +1583,6 @@ std::vector ObjectManager::GetLeakProbeEntries() entries.push_back(std::move(entry)); } LeakProbeEntry& entry = entries[it->second]; - entry.hasRecord = true; if (const CanaryClass* cls = record->cls) { const Global* kept[] = {&cls->prototype, &cls->callSuper, &cls->metadata, &cls->implementationObject}; @@ -1598,6 +1598,28 @@ std::vector ObjectManager::GetLeakProbeEntries() }); return entries; } + +jobjectArray ObjectManager::LeakProbeObjects(const std::vector& entries) { + JEnv env; + JNIEnv* raw = env; + jclass objectClass = env.FindClass("java/lang/Object"); + jobjectArray array = + objectClass != nullptr + ? raw->NewObjectArray(static_cast(entries.size()), objectClass, nullptr) + : nullptr; + if (array == nullptr) { + raw->ExceptionClear(); + return nullptr; + } + for (size_t i = 0; i < entries.size(); i++) { + // Released at once: a local reference to an element would root it in the dump. + JniLocalRef object(JavaObjectOrNull(entries[i].id)); + if (!object.IsNull()) { + raw->SetObjectArrayElement(array, static_cast(i), object); + } + } + return array; +} #endif void ObjectManager::ReleaseCollectedCanaryStates() { diff --git a/test-app/runtime/src/main/cpp/ObjectManager.h b/test-app/runtime/src/main/cpp/ObjectManager.h index edf0c009d..fe4bcf5b0 100644 --- a/test-app/runtime/src/main/cpp/ObjectManager.h +++ b/test-app/runtime/src/main/cpp/ObjectManager.h @@ -295,14 +295,19 @@ class ObjectManager { v8::Local wrapper; // The Java object table holds the object strongly for the wrapper. bool wrapperHoldsJava = false; - bool canary = false; - bool hasRecord = false; + // A resurrected wrapper: Java holds the object weakly, and the wrapper and what its + // JS properties reference live as long as the object. + bool javaKeepsWrapper = false; // What the canary record keeps alive while Java holds the object. std::vector> recordValues; }; // In the caller's HandleScope. std::vector GetLeakProbeEntries(); + + // An Object[] of the entries' Java objects, in order, null where Java collected one. + // Null, with any Java exception cleared, when it cannot be allocated. + jobjectArray LeakProbeObjects(const std::vector& entries); #endif jobject GetJavaRuntimeObject() const { return m_javaRuntimeObject; } diff --git a/test-app/runtime/src/main/java/com/tns/LeakProbe.java b/test-app/runtime/src/main/java/com/tns/LeakProbe.java deleted file mode 100644 index 9f186c59b..000000000 --- a/test-app/runtime/src/main/java/com/tns/LeakProbe.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.tns; - -import java.io.File; - -/** - * Holds, during ns:runtime's findLeakCycles() heap dump only, the Java objects the analysis - * maps to JS wrappers and states. The analysis finds this array through this class's static - * field by name and does not count it as a reference. - */ -final class LeakProbe { - static final String DUMP_PREFIX = "ns-leak-probe"; - - static Object[] pinned; - - private LeakProbe() { - } - - // Dumps a killed process left behind; the native side deletes its own once parsed. - static void deleteStaleDumps() { - String tmp = System.getProperty("java.io.tmpdir"); - File[] files = tmp != null ? new File(tmp).listFiles() : null; - if (files == null) { - return; - } - for (File file : files) { - if (file.getName().startsWith(DUMP_PREFIX) && file.getName().endsWith(".hprof")) { - file.delete(); - } - } - } -} diff --git a/test-app/runtime/src/main/java/com/tns/Runtime.java b/test-app/runtime/src/main/java/com/tns/Runtime.java index 4cb29c9a4..dc51b898b 100644 --- a/test-app/runtime/src/main/java/com/tns/Runtime.java +++ b/test-app/runtime/src/main/java/com/tns/Runtime.java @@ -1302,42 +1302,6 @@ private boolean strengthenInstance(int javaObjectID) { return objectTable.strengthen(javaObjectID); } - // ns:runtime findLeakCycles(): writes an HPROF dump with the objects of ids pinned in - // LeakProbe.pinned, in order, so the native analysis can find them. Returns the dump's - // path, or null if it could not be written. - // Called only by debug runtimes, which carry the native probe. - @RuntimeCallable - private String dumpHeapForLeakProbe(int[] ids) { - // One probe at a time in the process: LeakProbe.pinned is shared by every runtime. - synchronized (LeakProbe.class) { - LeakProbe.deleteStaleDumps(); - // Objects only weak entries hold would read as leaked until Java collects them. - for (int i = 0; i < 2; i++) { - java.lang.Runtime.getRuntime().gc(); - System.runFinalization(); - } - Object[] objects = new Object[ids.length]; - for (int i = 0; i < ids.length; i++) { - objects[i] = objectTable.get(ids[i]); - } - java.io.File file = null; - try { - file = java.io.File.createTempFile(LeakProbe.DUMP_PREFIX, ".hprof"); - LeakProbe.pinned = objects; - android.os.Debug.dumpHprofData(file.getAbsolutePath()); - return file.getAbsolutePath(); - } catch (Throwable t) { - android.util.Log.w("JS", "findLeakCycles could not dump the Java heap", t); - if (file != null) { - file.delete(); - } - return null; - } finally { - LeakProbe.pinned = null; - } - } - } - boolean removeWatched(ObjectTable.WatchedRef ref) { return objectTable.removeWatched(ref); } From d660d8e3db0727bea17ce6045abbb9eb72bddb49 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Sat, 21 Nov 2026 01:23:36 -0300 Subject: [PATCH 06/12] 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. --- .../app/tests/testWrapperLifetimeLeaks.js | 22 +++++++++++++++++++ .../runtime/src/main/cpp/ObjectManager.cpp | 10 ++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js b/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js index 48eed3914..4f2469a9b 100644 --- a/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js +++ b/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js @@ -51,6 +51,17 @@ describe("Wrapper lifetime leak probe", function () { var weak = new java.lang.Runnable({ run: function () {} }); weak.probeWeak = new WeakRef(weak); })(); + (function () { + // A method closing over its own object: the cycle runs through the object's + // prototype, its implementation object. In a function of its own, so no other + // planted closure shares the context that holds it. + var probeClosed = new java.lang.Runnable({ + run: function () { + return probeClosed; + } + }); + planted.push(new WeakRef(probeClosed)); + })(); setTimeout(function () { report = runtime.findLeakCycles(); console.log("leak probe: " + JSON.stringify({ @@ -137,6 +148,17 @@ describe("Wrapper lifetime leak probe", function () { expect(cycles[0].path).toContain("--WeakMap value-->"); }); + it("finds a cycle through a method that closes over its object", function () { + var cycles = cyclesThrough("probeClosed"); + if (!canary) { + expect(cycles.length).toBe(0); + return; + } + expect(cycles.length).toBe(1); + expect(cycles[0].path).toContain("--ns:kept-->"); + expect(cycles[0].path).toContain("--ns:wrapper-->"); + }); + it("does not report an object Java holds for a reason of its own", function () { expect(cyclesThrough("probeHeld").length).toBe(0); }); diff --git a/test-app/runtime/src/main/cpp/ObjectManager.cpp b/test-app/runtime/src/main/cpp/ObjectManager.cpp index 2cda07a71..0c35507ae 100644 --- a/test-app/runtime/src/main/cpp/ObjectManager.cpp +++ b/test-app/runtime/src/main/cpp/ObjectManager.cpp @@ -1583,14 +1583,8 @@ std::vector ObjectManager::GetLeakProbeEntries() entries.push_back(std::move(entry)); } LeakProbeEntry& entry = entries[it->second]; - if (const CanaryClass* cls = record->cls) { - const Global* kept[] = {&cls->prototype, &cls->callSuper, &cls->metadata, - &cls->implementationObject}; - for (const Global* value : kept) { - if (!value->IsEmpty()) { - entry.recordValues.push_back(value->Get(isolate)); - } - } + if (!record->shell.IsEmpty()) { + entry.recordValues.push_back(record->shell.Get(isolate)); } if (!record->state.IsEmpty()) { entry.recordValues.push_back(record->state.Get(isolate)); From f2984ad1b31654eb3324df51cbaf91e1516792a0 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Sat, 21 Nov 2026 02:56:27 -0300 Subject: [PATCH 07/12] fix(runtime): leak probe reads a canary record's handles and its shell A JS-extended object's canary record is back to handles for the first canary's prototype, internal fields and implementation object, held by the class data the records of one class share, and keeps a copy of its last rebuilt canary as a shell only between that rebuild and the next write. The probe lists all of them among each record's values, the shell only while it exists, so none of them reads as a root. A planted cycle through Java whose canary was rebuilt before the probe checks that the shell does not hide it. --- .../app/tests/testWrapperLifetimeLeaks.js | 142 +++++++++++------- .../runtime/src/main/cpp/ObjectManager.cpp | 9 ++ 2 files changed, 96 insertions(+), 55 deletions(-) diff --git a/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js b/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js index 4f2469a9b..3b6231afd 100644 --- a/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js +++ b/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js @@ -18,66 +18,89 @@ describe("Wrapper lifetime leak probe", function () { // Each probe dumps the Java heap, so every shape is planted once and probed once, on a // later task: a WeakRef keeps its target alive until the job that made it ends. beforeAll(function (done) { + // A cycle through Java whose object's canary was rebuilt since: the record also + // keeps a copy of the rebuilt canary, which must not read as a root. (function () { - // A JS property referencing its own object. - var selfReferencing = new java.lang.Runnable({ run: function () {} }); - selfReferencing.probeSelf = selfReferencing; - planted.push(new WeakRef(selfReferencing)); - - var twin = new java.lang.Runnable({ run: function () {} }); - twin.probeSelf = twin; - planted.push(new WeakRef(twin)); - - // A cycle through Java: the object's state holds a list that holds the object. - var listed = new java.lang.Runnable({ run: function () {} }); + var rebuilt = new java.lang.Runnable({ run: function () {} }); var list = new java.util.ArrayList(); - list.add(listed); - listed.probeList = list; - planted.push(new WeakRef(listed)); - - // Held by a Java static: alive for a reason of its own, not a leak. - var held = new java.lang.Runnable({ run: function () {} }); - held.probeHeld = held; - Holder.held = held; - planted.push(new WeakRef(held)); - - // A WeakMap value referencing its own key: in canary mode the key is the - // object's lifetime key, which its state holds. - var mapped = new java.lang.Runnable({ run: function () {} }); - probeMap.set(mapped, { probeMapped: mapped }); - planted.push(new WeakRef(mapped)); - - // A back reference through a WeakRef keeps nothing. - var weak = new java.lang.Runnable({ run: function () {} }); - weak.probeWeak = new WeakRef(weak); - })(); - (function () { - // A method closing over its own object: the cycle runs through the object's - // prototype, its implementation object. In a function of its own, so no other - // planted closure shares the context that holds it. - var probeClosed = new java.lang.Runnable({ - run: function () { - return probeClosed; - } - }); - planted.push(new WeakRef(probeClosed)); + list.add(rebuilt); + rebuilt.probeRebuiltList = list; + planted.push(new WeakRef(rebuilt)); + Holder.held = list; })(); + // The canary dies on a later job: a WeakRef keeps its target until the job ends. setTimeout(function () { - report = runtime.findLeakCycles(); - console.log("leak probe: " + JSON.stringify({ - leaked: report.leaked, - leakedOutsideCycles: report.leakedOutsideCycles, - probed: report.probed, - cycles: report.cycles.length, - durationMs: report.durationMs, - dumpBytes: report.dumpBytes - })); - report.cycles.forEach(function (cycle) { - console.log("leak probe cycle: " + cycle.signature); - }); - done(); + gc(); + java.lang.System.nanoTime(); + java.lang.Runtime.getRuntime().gc(); + gc(); + java.lang.System.nanoTime(); + (function () { + // A read: the rebuilt canary's copy stays current. + Holder.held.get(0).probeRebuiltList.size(); + })(); + Holder.held = null; + (function () { + // A JS property referencing its own object. + var selfReferencing = new java.lang.Runnable({ run: function () {} }); + selfReferencing.probeSelf = selfReferencing; + planted.push(new WeakRef(selfReferencing)); + + var twin = new java.lang.Runnable({ run: function () {} }); + twin.probeSelf = twin; + planted.push(new WeakRef(twin)); + + // A cycle through Java: the object's state holds a list that holds the object. + var listed = new java.lang.Runnable({ run: function () {} }); + var list = new java.util.ArrayList(); + list.add(listed); + listed.probeList = list; + planted.push(new WeakRef(listed)); + + // Held by a Java static: alive for a reason of its own, not a leak. + var held = new java.lang.Runnable({ run: function () {} }); + held.probeHeld = held; + Holder.held = held; + planted.push(new WeakRef(held)); + + // A WeakMap value referencing its own key: in canary mode the key is the + // object's lifetime key, which its state holds. + var mapped = new java.lang.Runnable({ run: function () {} }); + probeMap.set(mapped, { probeMapped: mapped }); + planted.push(new WeakRef(mapped)); + + // A back reference through a WeakRef keeps nothing. + var weak = new java.lang.Runnable({ run: function () {} }); + weak.probeWeak = new WeakRef(weak); + })(); + (function () { + // A method closing over its own object: the cycle runs through the object's + // prototype, its implementation object. In a function of its own, so no other + // planted closure shares the context that holds it. + var probeClosed = new java.lang.Runnable({ + run: function () { + return probeClosed; + } + }); + planted.push(new WeakRef(probeClosed)); + })(); + setTimeout(function () { + report = runtime.findLeakCycles(); + console.log("leak probe: " + JSON.stringify({ + leaked: report.leaked, + leakedOutsideCycles: report.leakedOutsideCycles, + probed: report.probed, + cycles: report.cycles.length, + durationMs: report.durationMs, + dumpBytes: report.dumpBytes + })); + report.cycles.forEach(function (cycle) { + console.log("leak probe cycle: " + cycle.signature); + }); + done(); + }, 0); }, 0); - }); + }, 20000); afterAll(function () { Holder.held = null; @@ -159,6 +182,15 @@ describe("Wrapper lifetime leak probe", function () { expect(cycles[0].path).toContain("--ns:wrapper-->"); }); + it("finds a cycle through Java after the object's canary was rebuilt", function () { + var cycles = cyclesThrough("probeRebuiltList"); + if (!canary) { + expect(cycles.length).toBe(0); + return; + } + expect(cycles.length).toBe(1); + }); + it("does not report an object Java holds for a reason of its own", function () { expect(cyclesThrough("probeHeld").length).toBe(0); }); diff --git a/test-app/runtime/src/main/cpp/ObjectManager.cpp b/test-app/runtime/src/main/cpp/ObjectManager.cpp index 0c35507ae..c15ac8838 100644 --- a/test-app/runtime/src/main/cpp/ObjectManager.cpp +++ b/test-app/runtime/src/main/cpp/ObjectManager.cpp @@ -1583,6 +1583,15 @@ std::vector ObjectManager::GetLeakProbeEntries() entries.push_back(std::move(entry)); } LeakProbeEntry& entry = entries[it->second]; + if (const CanaryClass* cls = record->cls) { + const Global* kept[] = {&cls->prototype, &cls->callSuper, &cls->metadata, + &cls->implementationObject}; + for (const Global* value : kept) { + if (!value->IsEmpty()) { + entry.recordValues.push_back(value->Get(isolate)); + } + } + } if (!record->shell.IsEmpty()) { entry.recordValues.push_back(record->shell.Get(isolate)); } From 865945ecc287394fac113c101d1c05553c261734 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Sun, 22 Nov 2026 13:34:27 -0300 Subject: [PATCH 08/12] fix(runtime): leak probe reads a canary record's weak-collection key --- docs/ns-builtin-modules.md | 4 ++-- .../app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js | 2 +- test-app/runtime/src/main/cpp/ObjectManager.cpp | 3 +++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/ns-builtin-modules.md b/docs/ns-builtin-modules.md index 37e6a7d77..c9e340daa 100644 --- a/docs/ns-builtin-modules.md +++ b/docs/ns-builtin-modules.md @@ -282,8 +282,8 @@ from a V8 heap snapshot (its roots minus the runtime's handles to canary records), the Java heap from an HPROF dump (`android.os.Debug.dumpHprofData`; its GC roots minus the runtime's object table, `Reference` referents skipped), and the runtime's own edges between them (a live wrapper holds its Java object; -a Java object holds its canary record: state, prototype and implementation -object; a Java object held weakly for a resurrected wrapper holds that wrapper). A `WeakMap` value is followed from its key only. A Java object the +a Java object holds its canary record: state, weak-collection key, prototype +and implementation object; a Java object held weakly for a resurrected wrapper holds that wrapper). A `WeakMap` value is followed from its key only. A Java object the runtime holds for a live wrapper, and that nothing else reaches, is leaked. For each, the report gives the path from the Java object to its own wrapper through fields, properties and the runtime's edges, a `signature` (the path's node labels from their smallest rotation, free of diff --git a/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js b/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js index 3b6231afd..9a0d97837 100644 --- a/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js +++ b/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js @@ -64,7 +64,7 @@ describe("Wrapper lifetime leak probe", function () { planted.push(new WeakRef(held)); // A WeakMap value referencing its own key: in canary mode the key is the - // object's lifetime key, which its state holds. + // object's lifetime key, which its record holds. var mapped = new java.lang.Runnable({ run: function () {} }); probeMap.set(mapped, { probeMapped: mapped }); planted.push(new WeakRef(mapped)); diff --git a/test-app/runtime/src/main/cpp/ObjectManager.cpp b/test-app/runtime/src/main/cpp/ObjectManager.cpp index c15ac8838..20a2d4093 100644 --- a/test-app/runtime/src/main/cpp/ObjectManager.cpp +++ b/test-app/runtime/src/main/cpp/ObjectManager.cpp @@ -1598,6 +1598,9 @@ std::vector ObjectManager::GetLeakProbeEntries() if (!record->state.IsEmpty()) { entry.recordValues.push_back(record->state.Get(isolate)); } + if (!record->key.IsEmpty()) { + entry.recordValues.push_back(record->key.Get(isolate)); + } }); return entries; } From 122279cdacae7118ad1ad283995e720894998886 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Sun, 22 Nov 2026 16:47:02 -0300 Subject: [PATCH 09/12] docs(runtime): leak probe reports cycles through plain wrappers under scope extended --- docs/ns-builtin-modules.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/ns-builtin-modules.md b/docs/ns-builtin-modules.md index c9e340daa..c98e4be82 100644 --- a/docs/ns-builtin-modules.md +++ b/docs/ns-builtin-modules.md @@ -295,7 +295,9 @@ died before the snapshot, and Java is collected twice before the dump, so objects only weak entries hold are gone and not counted. The report also gives `mode`, `probed` (the Java objects modeled), `jsNodes`, `javaObjects`, `dumpBytes` and `durationMs`. In `"resurrecting"` mode the report is empty and no dump is taken: -resurrection breaks these cycles. +resurrection breaks these cycles. Under `"canary"` every wrapper is a canary +in either scope, so under `"extended"` too a cycle through a plain wrapper, +which resurrection used to break, is reported as a leak. The probe is built into debug runtimes only; in a release build the function exists but throws `findLeakCycles is available in debug builds only`. It suspends the whole process for the dump, writes the Java heap to From c645dc5c79bda7b42464b22639a844ca84d41fb1 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Sun, 22 Nov 2026 20:53:52 -0300 Subject: [PATCH 10/12] build(runtime): ship the leak probe's Java side whenever its native side is built CMake builds LeakProbe.cpp unless the build is optimized without the inspector (NOT OPTIMIZED_BUILD OR OPTIMIZED_WITH_INSPECTOR_BUILD), but Gradle dropped src/debugTools/java for any -Poptimized build. With both -Poptimized and -PoptimizedWithInspector set, findLeakCycles() then failed with "this runtime has no com.tns.LeakProbe". Gradle now uses the same condition; -Poptimized alone still leaves the debug tools out. --- test-app/runtime/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-app/runtime/build.gradle b/test-app/runtime/build.gradle index bd71ec331..390df68d1 100644 --- a/test-app/runtime/build.gradle +++ b/test-app/runtime/build.gradle @@ -100,7 +100,7 @@ android { // embed runtime binding generator in runtime, while keeping it in a separate project java.srcDirs = [bindingGeneratorSourcePath, defaultSrcPath] // The Java side of debug-only native tools (CMake's DEBUG_TOOL_SOURCES). - if (!optimized) { + if (!optimized || optimizedWithInspector) { java.srcDirs += "src/debugTools/java" } } From aba8de6ca2408ce3d68ad3d6a1b52ad00bd99517 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Sun, 22 Nov 2026 20:54:11 -0300 Subject: [PATCH 11/12] test(runtime): check the leak probe's rebuilt-canary fixture was rebuilt The spec passed whether the probe saw the original canary or a rebuilt one, so a regression that kept the original alive went unnoticed. It now counts canaryRebuilds around the read that rebuilds the canary and requires one in canary mode. A WeakRef cannot show the original died there: canary mode's WeakRef.deref() builds a new canary while Java holds the object. --- .../app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js b/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js index 9a0d97837..ed3834c15 100644 --- a/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js +++ b/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js @@ -8,6 +8,7 @@ describe("Wrapper lifetime leak probe", function () { // Alive for the whole suite: what leaks is the entry's value. var probeMap = new WeakMap(); var report; + var canaryRebuilds; function cyclesThrough(edge) { return report.cycles.filter(function (cycle) { @@ -35,10 +36,12 @@ describe("Wrapper lifetime leak probe", function () { java.lang.Runtime.getRuntime().gc(); gc(); java.lang.System.nanoTime(); + var rebuildsBefore = runtime.getWrapperLifetimeStats().canaryRebuilds; (function () { // A read: the rebuilt canary's copy stays current. Holder.held.get(0).probeRebuiltList.size(); })(); + canaryRebuilds = runtime.getWrapperLifetimeStats().canaryRebuilds - rebuildsBefore; Holder.held = null; (function () { // A JS property referencing its own object. @@ -188,6 +191,7 @@ describe("Wrapper lifetime leak probe", function () { expect(cycles.length).toBe(0); return; } + expect(canaryRebuilds).toBeGreaterThan(0); expect(cycles.length).toBe(1); }); From abb763d900d7a66ec4f0ae28c4ddcda5485530b0 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Sun, 22 Nov 2026 20:54:25 -0300 Subject: [PATCH 12/12] test(runtime): fail the leak probe's settling spec on any unsettled wrapper The spec allowed the second probe up to 49 more leaked objects than the first, so it caught only a batch that stayed unsettled as a whole. Nothing between the two probes but the batch itself creates wrappers, so the second probe must not report more than the first. --- .../app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js b/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js index ed3834c15..3f5fa083e 100644 --- a/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js +++ b/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js @@ -213,6 +213,6 @@ describe("Wrapper lifetime leak probe", function () { } })(); // Read as leaked if unsettled: their Java objects are still held for them. - expect(runtime.findLeakCycles().leaked).toBeLessThan(report.leaked + 50); + expect(runtime.findLeakCycles().leaked).toBeLessThanOrEqual(report.leaked); }, 20000); });