diff --git a/docs/ns-builtin-modules.md b/docs/ns-builtin-modules.md index d913b571c..21570ece4 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`, which include those made only for a weak-collection key (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` @@ -261,6 +262,40 @@ 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 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 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 +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 +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 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 (`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 fc83cbbeb..2cbbee867 100644 --- a/test-app/app/src/main/assets/app/mainpage.js +++ b/test-app/app/src/main/assets/app/mainpage.js @@ -64,6 +64,7 @@ require("./tests/testWrapperLifetime"); require("./tests/testWrapperLifetimeModes"); require("./tests/testWrapperLifetimeStateWrites"); 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..3b6231afd --- /dev/null +++ b/test-app/app/src/main/assets/app/tests/testWrapperLifetimeLeaks.js @@ -0,0 +1,214 @@ +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 = []; + // Alive for the whole suite: what leaks is the entry's value. + var probeMap = new WeakMap(); + 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) { + // 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 () { + var rebuilt = new java.lang.Runnable({ run: function () {} }); + var list = new java.util.ArrayList(); + 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 () { + 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; + 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); + 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); + }); + + 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:kept-->"); + 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("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("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("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); + }); + + 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/CMakeLists.txt b/test-app/runtime/CMakeLists.txt index 0969b0540..c04accada 100644 --- a/test-app/runtime/CMakeLists.txt +++ b/test-app/runtime/CMakeLists.txt @@ -142,9 +142,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 @@ -259,6 +267,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/build.gradle b/test-app/runtime/build.gradle index 16a6951e0..0189f0c44 100644 --- a/test-app/runtime/build.gradle +++ b/test-app/runtime/build.gradle @@ -92,6 +92,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 new file mode 100644 index 000000000..3cb185ef8 --- /dev/null +++ b/test-app/runtime/src/main/cpp/LeakProbe.cpp @@ -0,0 +1,980 @@ +#include "LeakProbe.h" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ArgConverter.h" +#include "JEnv.h" +#include "JsV8InspectorClient.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)) { + error = "truncated HPROF record"; + return false; + } + 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 + 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: + char hex[3]; + snprintf(hex, sizeof(hex), "%02x", subtag); + error = std::string("unknown HPROF heap dump subrecord 0x") + hex; + return false; + } + } + if (!sub.ok) { + error = "truncated HPROF heap dump segment"; + return false; + } + } + in.p = recordEnd; + } + if (!in.ok) { + error = "truncated HPROF record"; + } + 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(); +} + +// 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; + } + // 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; + } + *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 { + 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; + // "(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> javaToJs; + + 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]; + 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 && node == globalHandles && runtimeHandleTargets.count(it->second) != 0) { + continue; + } + if (IsEphemeronTableEdge(js, edge)) { + 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 = javaToJs.find(node); + if (cross != javaToJs.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) { + std::string name; + if (IsEphemeronEdge(edge.js, &name)) { + return "WeakMap value"; + } + return ToUtf8(isolate, edge.js->GetName()); + } + if (edge.javaLabel != kNone) { + return java.labels[edge.javaLabel]; + } + return IsJava(from) ? "ns:kept" : "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, 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, bool* limited) { + 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) { + if (!queue.empty()) { + *limited = true; + } + 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->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->CreateDataProperty(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(); + }; + + // 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. 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. 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"); + } + struct DeleteSnapshot { + HeapProfiler* profiler; + const HeapSnapshot* snapshot; + bool clearIds; + ~DeleteSnapshot() { + const_cast(snapshot)->Delete(); + if (clearIds) { + profiler->ClearObjectIds(); + } + } + } deleteSnapshot{profiler, snapshot, clearIds}; + + 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; + raw->CallStaticVoidMethod(methods.clazz, methods.collectGarbage); + if (raw->ExceptionCheck()) { + raw->ExceptionClear(); + return fail("the Java collection failed"); + } + JniLocalRef objects(objectManager->LeakProbeObjects(entries)); + if (objects.IsNull()) { + return fail("out of memory"); + } + JniLocalRef pathRef( + raw->CallStaticObjectMethod(methods.clazz, methods.dump, (jobjectArray) objects)); + 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); + 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()) { + 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]; + // 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 (wrapperNode != kNone && entry.wrapperHoldsJava) { + graph.wrapperToJava[wrapperNode].push_back(javaNode); + } + if (!kept.empty()) { + auto& edges = graph.javaToJs[javaNode]; + edges.insert(edges.end(), kept.begin(), kept.end()); + } + } + + 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); + 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 (!reached[javaNode] && wrapperOf[i] != kNone) { + leaked.push_back(i); + leakedSet.insert(i); + } + } + + // 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; + for (size_t i : leaked) { + if (covered.count(i) != 0) { + continue; + } + if (searches++ == kMaxCycleSearches) { + truncated = true; + break; + } + std::vector pathLabels; + std::vector nodeLabels; + bool limited = false; + std::vector nodes = FindCycle(graph, reached, graph.JavaNode(javaOf[i]), + wrapperOf[i], &pathLabels, &nodeLabels, &limited); + truncated = truncated || limited; + 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->CreateDataProperty(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, "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", + 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..4e12843eb --- /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 resurrection used to break. +// +// 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. +// Built into debug runtimes only (APPLICATION_IN_DEBUG). +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 4842e73a3..fe505f9ff 100644 --- a/test-app/runtime/src/main/cpp/NsBuiltinModules.cpp +++ b/test-app/runtime/src/main/cpp/NsBuiltinModules.cpp @@ -9,9 +9,13 @@ #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" +#include "NativeScriptException.h" #include "Runtime.h" #include "RuntimeState.h" #include "StructuredSerialization.h" @@ -183,6 +187,26 @@ void GetWrapperLifetimeStatsCallback(const FunctionCallbackInfo& info) { info.GetReturnValue().Set(result); } +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)) { + 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()))); + } +#endif +} + void GetWrapperClassCountsCallback(const FunctionCallbackInfo& info) { Isolate* isolate = info.GetIsolate(); Local context = isolate->GetCurrentContext(); @@ -276,13 +300,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) || @@ -296,6 +321,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 504b6cb3a..a6c941a26 100644 --- a/test-app/runtime/src/main/cpp/ObjectManager.cpp +++ b/test-app/runtime/src/main/cpp/ObjectManager.cpp @@ -1244,6 +1244,87 @@ jobject ObjectManager::JavaObjectOrNull(int javaObjectID) { return object; } +#ifdef APPLICATION_IN_DEBUG +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); + // 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)); + }); + for (const auto& [id, record] : m_canaryRecords) { + 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]; + const Global* kept[] = {&record->prototype, &record->callSuper, &record->metadata, + &record->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)); + } + } + 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() { 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 9b4d6b0ee..4899c99b5 100644 --- a/test-app/runtime/src/main/cpp/ObjectManager.h +++ b/test-app/runtime/src/main/cpp/ObjectManager.h @@ -258,6 +258,34 @@ 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 { + 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; + // 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; } + // Canary mode: adds `unwrap` (a canary -> its Java object's weak-collection // key), `wrap` (key -> live or new canary, or undefined once Java collected // the object) and `setRekey` (registers the function told about every canary 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);