Skip to content

Commit 8da9d96

Browse files
joyeecheungRafaelGSS
authored andcommitted
deps: V8: cherry-pick 0fd478bcdabd
Original commit message: [heap-profiler]: expose QueryObjects() to v8::HeapProfiler This allows embedders to use this API for testing memory leaks more reliably. See #50572 for an example about how the API can be used. Change-Id: Ic3d1268e2b331c37e8ec92997b764b9b5486f8c2 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/5006373 Reviewed-by: Camillo Bruni <cbruni@chromium.org> Reviewed-by: Simon Zünd <szuend@chromium.org> Commit-Queue: Joyee Cheung <joyee@igalia.com> Cr-Commit-Position: refs/heads/main@{#91123} Refs: v8/v8@0fd478b PR-URL: #50572 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent aee01ff commit 8da9d96

File tree

9 files changed

+89
-26
lines changed

9 files changed

+89
-26
lines changed

common.gypi

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
# Reset this number to 0 on major V8 upgrades.
3838
# Increment by one for each non-official patch applied to deps/v8.
39-
'v8_embedder_string': '-node.17',
39+
'v8_embedder_string': '-node.18',
4040

4141
##### V8 defaults for Node.js #####
4242

deps/v8/include/v8-profiler.h

+10
Original file line numberDiff line numberDiff line change
@@ -921,12 +921,22 @@ class V8_EXPORT EmbedderGraph {
921921
virtual ~EmbedderGraph() = default;
922922
};
923923

924+
class QueryObjectPredicate {
925+
public:
926+
virtual ~QueryObjectPredicate() = default;
927+
virtual bool Filter(v8::Local<v8::Object> object) = 0;
928+
};
929+
924930
/**
925931
* Interface for controlling heap profiling. Instance of the
926932
* profiler can be retrieved using v8::Isolate::GetHeapProfiler.
927933
*/
928934
class V8_EXPORT HeapProfiler {
929935
public:
936+
void QueryObjects(v8::Local<v8::Context> context,
937+
QueryObjectPredicate* predicate,
938+
std::vector<v8::Global<v8::Object>>* objects);
939+
930940
enum SamplingFlags {
931941
kSamplingNoFlags = 0,
932942
kSamplingForceGC = 1 << 0,

deps/v8/src/api/api.cc

+10
Original file line numberDiff line numberDiff line change
@@ -10917,6 +10917,16 @@ int HeapProfiler::GetSnapshotCount() {
1091710917
return reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshotsCount();
1091810918
}
1091910919

10920+
void HeapProfiler::QueryObjects(Local<Context> v8_context,
10921+
QueryObjectPredicate* predicate,
10922+
std::vector<Global<Object>>* objects) {
10923+
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_context->GetIsolate());
10924+
i::HeapProfiler* profiler = reinterpret_cast<i::HeapProfiler*>(this);
10925+
DCHECK_EQ(isolate, profiler->isolate());
10926+
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
10927+
profiler->QueryObjects(Utils::OpenHandle(*v8_context), predicate, objects);
10928+
}
10929+
1092010930
const HeapSnapshot* HeapProfiler::GetHeapSnapshot(int index) {
1092110931
return reinterpret_cast<const HeapSnapshot*>(
1092210932
reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshot(index));

deps/v8/src/debug/debug-interface.cc

-9
Original file line numberDiff line numberDiff line change
@@ -1211,15 +1211,6 @@ v8::MaybeLocal<v8::Value> EvaluateGlobalForTesting(
12111211
RETURN_ESCAPED(result);
12121212
}
12131213

1214-
void QueryObjects(v8::Local<v8::Context> v8_context,
1215-
QueryObjectPredicate* predicate,
1216-
std::vector<v8::Global<v8::Object>>* objects) {
1217-
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_context->GetIsolate());
1218-
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1219-
isolate->heap_profiler()->QueryObjects(Utils::OpenHandle(*v8_context),
1220-
predicate, objects);
1221-
}
1222-
12231214
void GlobalLexicalScopeNames(v8::Local<v8::Context> v8_context,
12241215
std::vector<v8::Global<v8::String>>* names) {
12251216
i::Handle<i::Context> context = Utils::OpenHandle(*v8_context);

deps/v8/src/debug/debug-interface.h

-10
Original file line numberDiff line numberDiff line change
@@ -530,16 +530,6 @@ class V8_EXPORT_PRIVATE StackTraceIterator {
530530
bool throw_on_side_effect) = 0;
531531
};
532532

533-
class QueryObjectPredicate {
534-
public:
535-
virtual ~QueryObjectPredicate() = default;
536-
virtual bool Filter(v8::Local<v8::Object> object) = 0;
537-
};
538-
539-
void QueryObjects(v8::Local<v8::Context> context,
540-
QueryObjectPredicate* predicate,
541-
std::vector<v8::Global<v8::Object>>* objects);
542-
543533
void GlobalLexicalScopeNames(v8::Local<v8::Context> context,
544534
std::vector<v8::Global<v8::String>>* names);
545535

deps/v8/src/inspector/v8-debugger.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "include/v8-context.h"
99
#include "include/v8-function.h"
1010
#include "include/v8-microtask-queue.h"
11+
#include "include/v8-profiler.h"
1112
#include "include/v8-util.h"
1213
#include "src/inspector/inspected-context.h"
1314
#include "src/inspector/protocol/Protocol.h"
@@ -38,7 +39,7 @@ void cleanupExpiredWeakPointers(Map& map) {
3839
}
3940
}
4041

41-
class MatchPrototypePredicate : public v8::debug::QueryObjectPredicate {
42+
class MatchPrototypePredicate : public v8::QueryObjectPredicate {
4243
public:
4344
MatchPrototypePredicate(V8InspectorImpl* inspector,
4445
v8::Local<v8::Context> context,
@@ -994,7 +995,7 @@ v8::Local<v8::Array> V8Debugger::queryObjects(v8::Local<v8::Context> context,
994995
v8::Isolate* isolate = context->GetIsolate();
995996
std::vector<v8::Global<v8::Object>> v8_objects;
996997
MatchPrototypePredicate predicate(m_inspector, context, prototype);
997-
v8::debug::QueryObjects(context, &predicate, &v8_objects);
998+
isolate->GetHeapProfiler()->QueryObjects(context, &predicate, &v8_objects);
998999

9991000
v8::MicrotasksScope microtasksScope(context,
10001001
v8::MicrotasksScope::kDoNotRunMicrotasks);

deps/v8/src/profiler/heap-profiler.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ Heap* HeapProfiler::heap() const { return ids_->heap(); }
247247
Isolate* HeapProfiler::isolate() const { return heap()->isolate(); }
248248

249249
void HeapProfiler::QueryObjects(Handle<Context> context,
250-
debug::QueryObjectPredicate* predicate,
250+
v8::QueryObjectPredicate* predicate,
251251
std::vector<v8::Global<v8::Object>>* objects) {
252252
// We need a stack marker here to allow deterministic passes over the stack.
253253
// The garbage collection and the two object heap iterators should scan the

deps/v8/src/profiler/heap-profiler.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class StringsStorage;
3030
// generate consistent IDs for moved objects.
3131
class HeapProfilerNativeMoveListener {
3232
public:
33-
HeapProfilerNativeMoveListener(HeapProfiler* profiler)
33+
explicit HeapProfilerNativeMoveListener(HeapProfiler* profiler)
3434
: profiler_(profiler) {}
3535
HeapProfilerNativeMoveListener(const HeapProfilerNativeMoveListener& other) =
3636
delete;
@@ -116,8 +116,7 @@ class HeapProfiler : public HeapObjectAllocationTracker {
116116

117117
Isolate* isolate() const;
118118

119-
void QueryObjects(Handle<Context> context,
120-
debug::QueryObjectPredicate* predicate,
119+
void QueryObjects(Handle<Context> context, QueryObjectPredicate* predicate,
121120
std::vector<v8::Global<v8::Object>>* objects);
122121
void set_native_move_listener(
123122
std::unique_ptr<HeapProfilerNativeMoveListener> listener) {

deps/v8/test/cctest/test-heap-profiler.cc

+62
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <ctype.h>
3131

3232
#include <memory>
33+
#include <vector>
3334

3435
#include "include/v8-function.h"
3536
#include "include/v8-json.h"
@@ -4064,6 +4065,67 @@ TEST(SamplingHeapProfilerSampleDuringDeopt) {
40644065
heap_profiler->StopSamplingHeapProfiler();
40654066
}
40664067

4068+
namespace {
4069+
class TestQueryObjectPredicate : public v8::QueryObjectPredicate {
4070+
public:
4071+
TestQueryObjectPredicate(v8::Local<v8::Context> context,
4072+
v8::Local<v8::Symbol> symbol)
4073+
: context_(context), symbol_(symbol) {}
4074+
4075+
bool Filter(v8::Local<v8::Object> object) override {
4076+
return object->HasOwnProperty(context_, symbol_).FromMaybe(false);
4077+
}
4078+
4079+
private:
4080+
v8::Local<v8::Context> context_;
4081+
v8::Local<v8::Symbol> symbol_;
4082+
};
4083+
4084+
class IncludeAllQueryObjectPredicate : public v8::QueryObjectPredicate {
4085+
public:
4086+
IncludeAllQueryObjectPredicate() {}
4087+
bool Filter(v8::Local<v8::Object> object) override { return true; }
4088+
};
4089+
} // anonymous namespace
4090+
4091+
TEST(QueryObjects) {
4092+
LocalContext env;
4093+
v8::Isolate* isolate = env->GetIsolate();
4094+
v8::HandleScope scope(isolate);
4095+
v8::Local<v8::Context> context = env.local();
4096+
4097+
v8::Local<v8::Symbol> sym =
4098+
v8::Symbol::New(isolate, v8_str("query_object_test"));
4099+
context->Global()->Set(context, v8_str("test_symbol"), sym).Check();
4100+
v8::Local<v8::Value> arr = CompileRun(R"(
4101+
const arr = [];
4102+
for (let i = 0; i < 10; ++i) {
4103+
arr.push({[test_symbol]: true});
4104+
}
4105+
arr;
4106+
)");
4107+
context->Global()->Set(context, v8_str("arr"), arr).Check();
4108+
v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler();
4109+
4110+
{
4111+
TestQueryObjectPredicate predicate(context, sym);
4112+
std::vector<v8::Global<v8::Object>> out;
4113+
heap_profiler->QueryObjects(context, &predicate, &out);
4114+
4115+
CHECK_EQ(out.size(), 10);
4116+
for (size_t i = 0; i < out.size(); ++i) {
4117+
CHECK(out[i].Get(isolate)->HasOwnProperty(context, sym).FromMaybe(false));
4118+
}
4119+
}
4120+
4121+
{
4122+
IncludeAllQueryObjectPredicate predicate;
4123+
std::vector<v8::Global<v8::Object>> out;
4124+
heap_profiler->QueryObjects(context, &predicate, &out);
4125+
CHECK_GE(out.size(), 10);
4126+
}
4127+
}
4128+
40674129
TEST(WeakReference) {
40684130
v8::Isolate* isolate = CcTest::isolate();
40694131
i::Isolate* i_isolate = CcTest::i_isolate();

0 commit comments

Comments
 (0)