Skip to content

Commit ecf2ebb

Browse files
committed
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.
1 parent d6d61df commit ecf2ebb

9 files changed

Lines changed: 26 additions & 31 deletions

File tree

‎docs/ns-builtin-modules.md‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ wrapper.
105105
| `getConfig(key)` | Returns the current value of a config key. Throws `TypeError` on an unknown key. Readable from any isolate. |
106106
| `getWrapperLifetimeStats()` | Wrapper lifetime counters for the calling isolate (see [Wrapper lifetime](#wrapper-lifetime)). A fresh object on every call. |
107107
| `getWrapperClassCounts()` | Per Java class name (`<unknown>` 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)). |
108-
| `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)). |
108+
| `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)). |
109109
| `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)). |
110110

111111
*Platform note (Android):* the original exception is the Java throwable `x`
@@ -280,9 +280,8 @@ twice before the dump, so objects only weak entries hold are gone and not
280280
counted. In `"resurrecting"` mode the report is empty and no dump is taken:
281281
resurrection breaks these cycles.
282282

283-
The probe is available in debuggable builds, or with
284-
`"wrapperLifetime": { "leakProbe": true }` in the app config; elsewhere it
285-
throws. It suspends the whole process for the dump, writes the Java heap to
283+
The probe is built into debug runtimes only; in a release build the function
284+
exists but throws `findLeakCycles is available in debug builds only`. It suspends the whole process for the dump, writes the Java heap to
286285
the app's cache directory (deleted once parsed, and swept by the next probe if
287286
the process died first) and builds graphs of both heaps in native memory, so
288287
it takes seconds; on the main thread that can trip an ANR. The snapshot and

‎test-app/runtime/CMakeLists.txt‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,17 @@ if (NOT OPTIMIZED_BUILD OR OPTIMIZED_WITH_INSPECTOR_BUILD)
142142
src/main/cpp/v8_inspector/ns-v8-tracing-agent-impl.cpp
143143
src/main/cpp/v8_inspector/Utils.cpp
144144
)
145+
146+
# ns:runtime findLeakCycles(); release builds keep a stub that throws.
147+
set(
148+
DEBUG_TOOL_SOURCES
149+
150+
src/main/cpp/LeakProbe.cpp
151+
)
145152
else ()
146153
# When building in Release mode we do not include the V8 inspector sources
147154
set(INSPECTOR_SOURCES)
155+
set(DEBUG_TOOL_SOURCES)
148156
endif ()
149157

150158
# The Node-API test addons back the NapiTests/NapiCoverageTests specs of the
@@ -191,7 +199,6 @@ add_library(
191199
src/main/cpp/FieldAccessor.cpp
192200
src/main/cpp/File.cpp
193201
src/main/cpp/Interop.cpp
194-
src/main/cpp/LeakProbe.cpp
195202
src/main/cpp/IsolateTracked.cpp
196203
src/main/cpp/JEnv.cpp
197204
src/main/cpp/DesugaredInterfaceCompanionClassNameResolver.cpp
@@ -260,6 +267,7 @@ add_library(
260267

261268
# V8 inspector source files will be included only in Release mode
262269
${INSPECTOR_SOURCES}
270+
${DEBUG_TOOL_SOURCES}
263271

264272
# Node-API test addons, local Debug builds only
265273
${NAPI_TEST_SOURCES}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,11 +767,6 @@ MaybeLocal<Object> LeakProbe::FindLeakCycles(Isolate* isolate) {
767767
return fail("the Java heap dump failed; see the log");
768768
}
769769
std::string path = ArgConverter::jstringToString((jstring) pathRef);
770-
if (path.empty()) {
771-
return fail(
772-
"available in debuggable builds, or with \"wrapperLifetime\": { \"leakProbe\": true } "
773-
"in the app config");
774-
}
775770
struct RemoveFile {
776771
std::string path;
777772
~RemoveFile() { unlink(path.c_str()); }

‎test-app/runtime/src/main/cpp/LeakProbe.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace tns {
2222
// leaked group a cycle through it, with a signature free of ids.
2323
//
2424
// Slow by design: it suspends the process for a full heap dump and parses it.
25-
// A diagnostic, not something to run in production.
25+
// Built into debug runtimes only (APPLICATION_IN_DEBUG).
2626
class LeakProbe {
2727
public:
2828
// The report object, or empty with a JS exception pending.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include "BuiltinLoader.h"
1010
#include "CallbackHandlers.h"
1111
#include "Interop.h"
12+
#ifdef APPLICATION_IN_DEBUG
1213
#include "LeakProbe.h"
14+
#endif
1315
#include "Messaging.h"
1416
#include "ModuleInternalCallbacks.h"
1517
#include "NativeScriptAssert.h"
@@ -186,6 +188,10 @@ void GetWrapperLifetimeStatsCallback(const FunctionCallbackInfo<Value>& info) {
186188

187189
void FindLeakCyclesCallback(const FunctionCallbackInfo<Value>& info) {
188190
Isolate* isolate = info.GetIsolate();
191+
#ifndef APPLICATION_IN_DEBUG
192+
isolate->ThrowException(Exception::Error(ArgConverter::ConvertToV8String(
193+
isolate, "findLeakCycles is available in debug builds only")));
194+
#else
189195
try {
190196
Local<Object> report;
191197
if (LeakProbe::FindLeakCycles(isolate).ToLocal(&report)) {
@@ -197,6 +203,7 @@ void FindLeakCyclesCallback(const FunctionCallbackInfo<Value>& info) {
197203
isolate->ThrowException(Exception::Error(ArgConverter::ConvertToV8String(
198204
isolate, std::string("findLeakCycles: ") + e.what())));
199205
}
206+
#endif
200207
}
201208

202209
void GetWrapperClassCountsCallback(const FunctionCallbackInfo<Value>& info) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,7 @@ jobject ObjectManager::JavaObjectOrNull(int javaObjectID) {
10961096
return object;
10971097
}
10981098

1099+
#ifdef APPLICATION_IN_DEBUG
10991100
std::vector<ObjectManager::LeakProbeEntry> ObjectManager::GetLeakProbeEntries() {
11001101
auto isolate = m_isolate;
11011102
std::unordered_map<jclass, std::string> names;
@@ -1149,6 +1150,7 @@ std::vector<ObjectManager::LeakProbeEntry> ObjectManager::GetLeakProbeEntries()
11491150
}
11501151
return entries;
11511152
}
1153+
#endif
11521154

11531155
void ObjectManager::ReleaseCollectedCanaryStates() {
11541156
if (!m_deathInboxOpen) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ class ObjectManager {
204204
// Per Java class name (dotted), for ns:runtime's getWrapperClassCounts().
205205
std::map<std::string, ClassCounts> GetWrapperClassCounts();
206206

207+
#ifdef APPLICATION_IN_DEBUG
207208
// The runtime's cross-heap edges, for ns:runtime's findLeakCycles()
208209
// (LeakProbe). One entry per id with a live wrapper or a canary record.
209210
struct LeakProbeEntry {
@@ -222,6 +223,7 @@ class ObjectManager {
222223

223224
// In the caller's HandleScope.
224225
std::vector<LeakProbeEntry> GetLeakProbeEntries();
226+
#endif
225227

226228
jobject GetJavaRuntimeObject() const { return m_javaRuntimeObject; }
227229

‎test-app/runtime/src/main/java/com/tns/AppConfig.java‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ protected enum KnownKeys {
2626
UncaughtErrorPolicy("uncaughtErrorPolicy", "report"),
2727
InterfaceProxies("interfaceProxies", false),
2828
WrapperLifetimeMode("mode", "resurrecting"),
29-
WrapperLifetimeScope("scope", "extended"),
30-
WrapperLifetimeLeakProbe("leakProbe", false);
29+
WrapperLifetimeScope("scope", "extended");
3130

3231
private final String name;
3332
private final Object defaultValue;
@@ -183,12 +182,6 @@ private void readWrapperLifetime(Object config) {
183182
Log.w("JS", String.format("Unknown wrapperLifetime mode %s; using \"resurrecting\".", JSONObject.quote(String.valueOf(mode))));
184183
}
185184
}
186-
Object leakProbe = object.opt(KnownKeys.WrapperLifetimeLeakProbe.getName());
187-
if (leakProbe instanceof Boolean) {
188-
values[KnownKeys.WrapperLifetimeLeakProbe.ordinal()] = leakProbe;
189-
} else if (leakProbe != null) {
190-
Log.w("JS", "wrapperLifetime leakProbe must be a boolean; using false.");
191-
}
192185
Object scope = object.opt(KnownKeys.WrapperLifetimeScope.getName());
193186
if (scope != null) {
194187
if ("extended".equals(scope) || "all".equals(scope)) {
@@ -203,10 +196,6 @@ public String getWrapperLifetimeMode() {
203196
return (String)values[KnownKeys.WrapperLifetimeMode.ordinal()];
204197
}
205198

206-
public boolean getWrapperLifetimeLeakProbe() {
207-
return (boolean)values[KnownKeys.WrapperLifetimeLeakProbe.ordinal()];
208-
}
209-
210199
public String getWrapperLifetimeScope() {
211200
return (String)values[KnownKeys.WrapperLifetimeScope.ordinal()];
212201
}

‎test-app/runtime/src/main/java/com/tns/Runtime.java‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,16 +1305,9 @@ private boolean strengthenInstance(int javaObjectID) {
13051305
// ns:runtime findLeakCycles(): writes an HPROF dump with the objects of ids pinned in
13061306
// LeakProbe.pinned, in order, so the native analysis can find them. Returns the dump's
13071307
// path, or null if it could not be written.
1308-
// Returns "" when the probe is not enabled: only debuggable builds, or apps that set
1309-
// "wrapperLifetime": { "leakProbe": true }, since it suspends the process and writes the
1310-
// whole heap to disk.
1308+
// Called only by debug runtimes, which carry the native probe.
13111309
@RuntimeCallable
13121310
private String dumpHeapForLeakProbe(int[] ids) {
1313-
boolean enabled = isDebuggable() ||
1314-
(config != null && config.appConfig != null && config.appConfig.getWrapperLifetimeLeakProbe());
1315-
if (!enabled) {
1316-
return "";
1317-
}
13181311
// One probe at a time in the process: LeakProbe.pinned is shared by every runtime.
13191312
synchronized (LeakProbe.class) {
13201313
LeakProbe.deleteStaleDumps();

0 commit comments

Comments
 (0)