Skip to content

Commit a2f39e9

Browse files
joyeecheungrichardlau
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 <[email protected]> Reviewed-by: Simon Zünd <[email protected]> Commit-Queue: Joyee Cheung <[email protected]> Cr-Commit-Position: refs/heads/main@{#91123} Refs: v8/v8@0fd478b PR-URL: #50572 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Stephen Belanger <[email protected]>
1 parent 6319ea6 commit a2f39e9

File tree

8 files changed

+87
-24
lines changed

8 files changed

+87
-24
lines changed

deps/v8/include/v8-profiler.h

+10
Original file line numberDiff line numberDiff line change
@@ -912,12 +912,22 @@ class V8_EXPORT EmbedderGraph {
912912
virtual ~EmbedderGraph() = default;
913913
};
914914

915+
class QueryObjectPredicate {
916+
public:
917+
virtual ~QueryObjectPredicate() = default;
918+
virtual bool Filter(v8::Local<v8::Object> object) = 0;
919+
};
920+
915921
/**
916922
* Interface for controlling heap profiling. Instance of the
917923
* profiler can be retrieved using v8::Isolate::GetHeapProfiler.
918924
*/
919925
class V8_EXPORT HeapProfiler {
920926
public:
927+
void QueryObjects(v8::Local<v8::Context> context,
928+
QueryObjectPredicate* predicate,
929+
std::vector<v8::Global<v8::Object>>* objects);
930+
921931
enum SamplingFlags {
922932
kSamplingNoFlags = 0,
923933
kSamplingForceGC = 1 << 0,

deps/v8/src/api/api.cc

+10
Original file line numberDiff line numberDiff line change
@@ -11011,6 +11011,16 @@ int HeapProfiler::GetSnapshotCount() {
1101111011
return reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshotsCount();
1101211012
}
1101311013

11014+
void HeapProfiler::QueryObjects(Local<Context> v8_context,
11015+
QueryObjectPredicate* predicate,
11016+
std::vector<Global<Object>>* objects) {
11017+
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_context->GetIsolate());
11018+
i::HeapProfiler* profiler = reinterpret_cast<i::HeapProfiler*>(this);
11019+
DCHECK_EQ(isolate, profiler->isolate());
11020+
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
11021+
profiler->QueryObjects(Utils::OpenHandle(*v8_context), predicate, objects);
11022+
}
11023+
1101411024
const HeapSnapshot* HeapProfiler::GetHeapSnapshot(int index) {
1101511025
return reinterpret_cast<const HeapSnapshot*>(
1101611026
reinterpret_cast<i::HeapProfiler*>(this)->GetSnapshot(index));

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

-9
Original file line numberDiff line numberDiff line change
@@ -1203,15 +1203,6 @@ v8::MaybeLocal<v8::Value> EvaluateGlobalForTesting(
12031203
RETURN_ESCAPED(result);
12041204
}
12051205

1206-
void QueryObjects(v8::Local<v8::Context> v8_context,
1207-
QueryObjectPredicate* predicate,
1208-
std::vector<v8::Global<v8::Object>>* objects) {
1209-
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_context->GetIsolate());
1210-
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1211-
isolate->heap_profiler()->QueryObjects(Utils::OpenHandle(*v8_context),
1212-
predicate, objects);
1213-
}
1214-
12151206
void GlobalLexicalScopeNames(v8::Local<v8::Context> v8_context,
12161207
std::vector<v8::Global<v8::String>>* names) {
12171208
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
@@ -523,16 +523,6 @@ class V8_EXPORT_PRIVATE StackTraceIterator {
523523
bool throw_on_side_effect) = 0;
524524
};
525525

526-
class QueryObjectPredicate {
527-
public:
528-
virtual ~QueryObjectPredicate() = default;
529-
virtual bool Filter(v8::Local<v8::Object> object) = 0;
530-
};
531-
532-
void QueryObjects(v8::Local<v8::Context> context,
533-
QueryObjectPredicate* predicate,
534-
std::vector<v8::Global<v8::Object>>* objects);
535-
536526
void GlobalLexicalScopeNames(v8::Local<v8::Context> context,
537527
std::vector<v8::Global<v8::String>>* names);
538528

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"
@@ -37,7 +38,7 @@ void cleanupExpiredWeakPointers(Map& map) {
3738
}
3839
}
3940

40-
class MatchPrototypePredicate : public v8::debug::QueryObjectPredicate {
41+
class MatchPrototypePredicate : public v8::QueryObjectPredicate {
4142
public:
4243
MatchPrototypePredicate(V8InspectorImpl* inspector,
4344
v8::Local<v8::Context> context,
@@ -1004,7 +1005,7 @@ v8::Local<v8::Array> V8Debugger::queryObjects(v8::Local<v8::Context> context,
10041005
v8::Isolate* isolate = context->GetIsolate();
10051006
std::vector<v8::Global<v8::Object>> v8_objects;
10061007
MatchPrototypePredicate predicate(m_inspector, context, prototype);
1007-
v8::debug::QueryObjects(context, &predicate, &v8_objects);
1008+
isolate->GetHeapProfiler()->QueryObjects(context, &predicate, &v8_objects);
10081009

10091010
v8::MicrotasksScope microtasksScope(context,
10101011
v8::MicrotasksScope::kDoNotRunMicrotasks);

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

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

247247
void HeapProfiler::QueryObjects(Handle<Context> context,
248-
debug::QueryObjectPredicate* predicate,
248+
v8::QueryObjectPredicate* predicate,
249249
std::vector<v8::Global<v8::Object>>* objects) {
250250
{
251251
HandleScope handle_scope(isolate());

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ class HeapProfiler : public HeapObjectAllocationTracker {
8888

8989
Isolate* isolate() const;
9090

91-
void QueryObjects(Handle<Context> context,
92-
debug::QueryObjectPredicate* predicate,
91+
void QueryObjects(Handle<Context> context, QueryObjectPredicate* predicate,
9392
std::vector<v8::Global<v8::Object>>* objects);
9493

9594
private:

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"
@@ -4060,6 +4061,67 @@ TEST(SamplingHeapProfilerSampleDuringDeopt) {
40604061
heap_profiler->StopSamplingHeapProfiler();
40614062
}
40624063

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

0 commit comments

Comments
 (0)