Skip to content

Commit b982335

Browse files
joyeecheungtargos
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> PR-URL: #50115 Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
1 parent 481a901 commit b982335

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.10',
39+
'v8_embedder_string': '-node.11',
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
@@ -11134,6 +11134,16 @@ int HeapProfiler::GetSnapshotCount() {
1113411134
return reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshotsCount();
1113511135
}
1113611136

11137+
void HeapProfiler::QueryObjects(Local<Context> v8_context,
11138+
QueryObjectPredicate* predicate,
11139+
std::vector<Global<Object>>* objects) {
11140+
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_context->GetIsolate());
11141+
i::HeapProfiler* profiler = reinterpret_cast<i::HeapProfiler*>(this);
11142+
DCHECK_EQ(isolate, profiler->isolate());
11143+
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
11144+
profiler->QueryObjects(Utils::OpenHandle(*v8_context), predicate, objects);
11145+
}
11146+
1113711147
const HeapSnapshot* HeapProfiler::GetHeapSnapshot(int index) {
1113811148
return reinterpret_cast<const HeapSnapshot*>(
1113911149
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
@@ -283,7 +283,7 @@ Heap* HeapProfiler::heap() const { return ids_->heap(); }
283283
Isolate* HeapProfiler::isolate() const { return heap()->isolate(); }
284284

285285
void HeapProfiler::QueryObjects(Handle<Context> context,
286-
debug::QueryObjectPredicate* predicate,
286+
v8::QueryObjectPredicate* predicate,
287287
std::vector<v8::Global<v8::Object>>* objects) {
288288
// We need a stack marker here to allow deterministic passes over the stack.
289289
// 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;
@@ -119,8 +119,7 @@ class HeapProfiler : public HeapObjectAllocationTracker {
119119

120120
Isolate* isolate() const;
121121

122-
void QueryObjects(Handle<Context> context,
123-
debug::QueryObjectPredicate* predicate,
122+
void QueryObjects(Handle<Context> context, QueryObjectPredicate* predicate,
124123
std::vector<v8::Global<v8::Object>>* objects);
125124
void set_native_move_listener(
126125
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"
@@ -4068,6 +4069,67 @@ TEST(SamplingHeapProfilerSampleDuringDeopt) {
40684069
heap_profiler->StopSamplingHeapProfiler();
40694070
}
40704071

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

0 commit comments

Comments
 (0)