Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion NativeScript/runtime/LeakProbe.mm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "LeakProbe.h"
#include <dlfcn.h>
#include <memory>
#include <unordered_map>
#include "Caches.h"
#include "ClassBuilder.h"
#include "DataWrapper.h"
Expand Down Expand Up @@ -53,10 +54,20 @@ bool ForEachInstance(Isolate* isolate, void* context,
if (cache == nullptr || !cache->IsValid()) {
return false;
}
// A wrapper built for an object whose previous wrapper's release is still
// queued replaces its Instances entry, so a queued retain may have none.
std::unordered_map<void*, uint32_t> queued;
WrapperLifetime::ForEachQueuedRelease(isolate,
[&](id object) { queued[(__bridge void*)object]++; });
for (auto& entry : cache->Instances) {
const std::shared_ptr<Persistent<Value>>& handle = entry.second;
Instance instance{};
instance.object = (__bridge void*)entry.first;
auto pending = queued.find(instance.object);
if (pending != queued.end()) {
instance.queued = pending->second;
queued.erase(pending);
}
if (handle != nullptr && !handle->IsEmpty()) {
instance.wrapper = handle->Get(isolate);
BaseDataWrapper* data = GetValue(isolate, instance.wrapper);
Expand All @@ -71,10 +82,19 @@ bool ForEachInstance(Isolate* isolate, void* context,
ObjectWeakCallbackState* ownership = wrapper->OwnershipState();
instance.reconciledStrong = ownership != nullptr && !ownership->disposing_ &&
ownership->target_ == handle && !handle->IsWeak();
// A handle under ownership tracking is reconciled before every major
// collection, the snapshot's included, so reconciledStrong covers it.
instance.resurrects = handle->IsWeak() && wrapper->IsGcProtected();
}
}
visit(context, instance);
}
for (const auto& [object, count] : queued) {
Instance instance{};
instance.object = object;
instance.queued = count;
visit(context, instance);
}
return true;
}

Expand All @@ -84,6 +104,17 @@ void ForEachCanaryState(Isolate* isolate, void* context,
isolate, [&](id object, Local<Object> state) { visit(context, object, state); });
}

void ForEachCanaryStateHandle(Isolate* isolate, void* context,
void (*visit)(void* context, Local<Object> state)) {
WrapperLifetimeCanary::ForEachStateHandle(isolate,
[&](Local<Object> state) { visit(context, state); });
}

void DrainReleases(Isolate* isolate) {
WrapperLifetime::DrainReleases(isolate);
WrapperLifetimeCanary::ReleaseDeadStates(isolate);
}

void* IsolateState(Isolate* isolate) {
ProbeState* probe = Caches::StateFor<ProbeState>(isolate);
return probe != nullptr ? probe->state : nullptr;
Expand Down Expand Up @@ -141,6 +172,7 @@ bool LifetimeStats(Isolate* isolate, leakprobe::LifetimeStats* out) {
.size = sizeof(Host),
.forEachInstance = ForEachInstance,
.forEachCanaryState = ForEachCanaryState,
.forEachCanaryStateHandle = ForEachCanaryStateHandle,
.isJSBlock = Interop::IsJSBlock,
.jsBlockCallback = Interop::JSBlockCallback,
.nativeClass = ClassBuilder::FindNativeClass,
Expand All @@ -149,7 +181,7 @@ bool LifetimeStats(Isolate* isolate, leakprobe::LifetimeStats* out) {
.createPoster = CreatePoster,
.post = Post,
.destroyPoster = [](Poster* poster) { delete poster; },
.drainReleases = WrapperLifetime::DrainReleases,
.drainReleases = DrainReleases,
.lifetimeMode = []() { return WrapperLifetimeModeName(GetWrapperLifetimeMode()); },
.lifetimeScope = []() { return WrapperLifetimeScopeName(GetWrapperLifetimeScope()); },
.lifetimeStats = LifetimeStats,
Expand Down
23 changes: 19 additions & 4 deletions NativeScript/runtime/LeakProbeSPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@
namespace tns {
namespace leakprobe {

constexpr uint32_t kVersion = 2;
constexpr uint32_t kVersion = 3;

constexpr const char* kConnectSymbol = "NativeScriptLeakProbeConnect";

// One entry of an isolate's native object -> wrapper map. The runtime has not
// dereferenced `object`, which another thread may have freed.
// One entry of an isolate's native object -> wrapper map, or an object whose
// only runtime retains are queued ones (`wrapper` empty). The runtime has not
// dereferenced `object`, which another thread may have freed unless `queued`.
struct Instance {
void* object;
// Empty once the wrapper is gone.
v8::Local<v8::Value> wrapper;
// Retains the runtime still holds on `object` for wrappers that died, until
// the next drain (Host::drainReleases) on this isolate's thread.
uint32_t queued;
// `wrapper` wraps an Objective-C object; the fields below are meaningful
// only then.
bool objc;
Expand All @@ -44,6 +48,10 @@ struct Instance {
// The map holds the wrapper through the strong handle the runtime keeps for
// a JS-extended object native code also retains.
bool reconciledStrong;
// The map holds the wrapper through a weak handle whose disposal is refused
// while native code retains the object ("resurrecting"): the object keeps
// the wrapper alive.
bool resurrects;
};

struct LifetimeStats {
Expand Down Expand Up @@ -75,6 +83,12 @@ struct Host {
void (*forEachCanaryState)(v8::Isolate* isolate, void* context,
void (*visit)(void* context, id object,
v8::Local<v8::Object> state));
// Every canary state handle the runtime holds, those forEachCanaryState
// skips included: states of objects being deallocated, and of deallocated
// objects until the next drain lets go of them.
void (*forEachCanaryStateHandle)(v8::Isolate* isolate, void* context,
void (*visit)(void* context,
v8::Local<v8::Object> state));
// Whether the runtime made `block` (alive) for JS.
bool (*isJSBlock)(const void* block);
// The JS function a JS block made for this isolate calls; empty for other
Expand All @@ -100,7 +114,8 @@ struct Host {
// Any thread.
void (*destroyPoster)(Poster* poster);

// Releases the native objects wrapper lifetime modes queued for release.
// Releases the native objects wrapper lifetime modes queued for release,
// and lets go of the canary states of deallocated objects. Can run JS.
void (*drainReleases)(v8::Isolate* isolate);
const char* (*lifetimeMode)();
const char* (*lifetimeScope)();
Expand Down
6 changes: 6 additions & 0 deletions NativeScript/runtime/WrapperLifetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define WrapperLifetime_h

#include <atomic>
#include <functional>

#include "Common.h"
#include "DataWrapper.h"
Expand Down Expand Up @@ -94,6 +95,11 @@ class WrapperLifetime {
// run: release can reach a JS-implemented -dealloc.
static void DrainReleases(v8::Isolate* isolate);

// Calls `visit` with the object of each dead wrapper whose release
// DrainReleases has yet to make, once per queued retain.
static void ForEachQueuedRelease(v8::Isolate* isolate,
const std::function<void(id object)>& visit);

// JS->native call entry: a long synchronous stretch of JS must not hold dead
// objects until it yields. The gate is process-wide; while another isolate
// has queued releases this costs one state lookup per call.
Expand Down
11 changes: 11 additions & 0 deletions NativeScript/runtime/WrapperLifetime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,17 @@ void ReleaseAll(Isolate* isolate, RootedState* rooted) {
ReleaseAll(isolate, rooted);
}

void WrapperLifetime::ForEachQueuedRelease(Isolate* isolate,
const std::function<void(id object)>& visit) {
RootedState* rooted = Caches::ExistingStateFor<RootedState>(isolate);
if (rooted == nullptr) {
return;
}
for (ObjectWeakCallbackState* state : rooted->releases) {
visit(state->lifetimeTarget_);
}
}

void WrapperLifetime::DrainForTeardown(Isolate* isolate) {
WrapperLifetimeCanary::CloseForTeardown(isolate);
RootedState* rooted = Caches::ExistingStateFor<RootedState>(isolate);
Expand Down
11 changes: 11 additions & 0 deletions NativeScript/runtime/WrapperLifetimeCanary.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ class WrapperLifetimeCanary {
static void ForEachState(
v8::Isolate* isolate,
const std::function<void(id object, v8::Local<v8::Object> state)>& visit);

// Calls `visit` with every state handle the registry holds: ForEachState's,
// and those of objects being deallocated or deallocated but not yet let go
// of. The caller provides the HandleScope.
static void ForEachStateHandle(
v8::Isolate* isolate,
const std::function<void(v8::Local<v8::Object> state)>& visit);

// Lets go of the state of every object known to be deallocated now, rather
// than on the drain its deallocation posted.
static void ReleaseDeadStates(v8::Isolate* isolate);
};

} // namespace tns
Expand Down
44 changes: 44 additions & 0 deletions NativeScript/runtime/WrapperLifetimeCanary.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1021,4 +1021,48 @@ void WrapCallback(const FunctionCallbackInfo<Value>& info) {
}
}

void WrapperLifetimeCanary::ForEachStateHandle(
Isolate* isolate, const std::function<void(Local<Object> state)>& visit) {
CanaryState* state = Caches::ExistingStateFor<CanaryState>(isolate);
if (state == nullptr) {
return;
}
// Not gated on the live count: dead records outlast it.
std::vector<Local<Object>> handles;
{
std::lock_guard<std::mutex> lock(state->registry->mutex);
if (state->registry->closed) {
return;
}
for (auto& entry : state->registry->records) {
if (!entry.second->state.IsEmpty()) {
handles.push_back(entry.second->state.Get(isolate));
}
}
for (const std::shared_ptr<CanaryRecord>& record : state->registry->dead) {
if (!record->state.IsEmpty()) {
handles.push_back(record->state.Get(isolate));
}
}
}
for (Local<Object> handle : handles) {
visit(handle);
}
}

void WrapperLifetimeCanary::ReleaseDeadStates(Isolate* isolate) {
if (GetWrapperLifetimeMode() != WrapperLifetimeMode::kCanary) {
return;
}
CanaryState* state = Caches::ExistingStateFor<CanaryState>(isolate);
if (state == nullptr) {
return;
}
#if NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_WEAK_SWEEP
Sweep(state->registry);
#else
DrainDead(state->registry);
#endif
}

} // namespace tns
Loading
Loading