-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v8: implement v8.queryObjects() for memory leak regression testing
This is similar to the `queryObjects()` console API provided by the Chromium DevTools console. It can be used to search for objects that have the matching constructor on its prototype chain in the entire heap, which can be useful for memory leak regression tests. To avoid surprising results, users should avoid using this API on constructors whose implementation they don't control, or on constructors that can be invoked by other parties in the application. To avoid accidental leaks, this API does not return raw references to the objects found. By default, it returns the count of the objects found. If `options.format` is `'summary'`, it returns an array containing brief string representations for each object. The visibility provided in this API is similar to what the heap snapshot provides, while users can save the cost of serialization and parsing and directly filer the target objects during the search. We have been using this API internally for the test suite, which has been more stable than any other leak regression testing strategies in the CI. With a public implementation we can now use the public API instead. PR-URL: #51927 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]>
- Loading branch information
1 parent
9930f11
commit 7f2d61f
Showing
14 changed files
with
359 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,33 @@ | ||
'use strict'; | ||
|
||
const { | ||
Error, | ||
StringPrototypeStartsWith, | ||
globalThis, | ||
} = primordials; | ||
|
||
process.emitWarning( | ||
'These APIs are for internal testing only. Do not use them.', | ||
'internal/test/binding'); | ||
|
||
function filteredInternalBinding(id) { | ||
// Disallows internal bindings with names that start with 'internal_only' | ||
// which means it should not be exposed to users even with | ||
// --expose-internals. | ||
if (StringPrototypeStartsWith(id, 'internal_only')) { | ||
// This code is only intended for internal errors and is not documented. | ||
// Do not use the normal error system. | ||
// eslint-disable-next-line no-restricted-syntax | ||
const error = new Error(`No such binding: ${id}`); | ||
error.code = 'ERR_INVALID_MODULE'; | ||
throw error; | ||
} | ||
return internalBinding(id); | ||
} | ||
|
||
if (module.isPreloading) { | ||
globalThis.internalBinding = internalBinding; | ||
globalThis.internalBinding = filteredInternalBinding; | ||
globalThis.primordials = primordials; | ||
} | ||
|
||
module.exports = { internalBinding, primordials }; | ||
module.exports = { internalBinding: filteredInternalBinding, primordials }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include "node_binding.h" | ||
#include "node_external_reference.h" | ||
#include "util-inl.h" | ||
#include "v8-profiler.h" | ||
#include "v8.h" | ||
|
||
using v8::Array; | ||
using v8::Context; | ||
using v8::FunctionCallbackInfo; | ||
using v8::Global; | ||
using v8::Isolate; | ||
using v8::Local; | ||
using v8::Object; | ||
using v8::Value; | ||
|
||
namespace node { | ||
namespace internal_only_v8 { | ||
|
||
class PrototypeChainHas : public v8::QueryObjectPredicate { | ||
public: | ||
PrototypeChainHas(Local<Context> context, Local<Object> search) | ||
: context_(context), search_(search) {} | ||
|
||
// What we can do in the filter can be quite limited, but looking up | ||
// the prototype chain is something that the inspector console API | ||
// queryObject() does so it is supported. | ||
bool Filter(Local<Object> object) override { | ||
Local<Context> creation_context; | ||
if (!object->GetCreationContext().ToLocal(&creation_context)) { | ||
return false; | ||
} | ||
if (creation_context != context_) { | ||
return false; | ||
} | ||
for (Local<Value> proto = object->GetPrototype(); proto->IsObject(); | ||
proto = proto.As<Object>()->GetPrototype()) { | ||
if (search_ == proto) return true; | ||
} | ||
return false; | ||
} | ||
|
||
private: | ||
Local<Context> context_; | ||
Local<Object> search_; | ||
}; | ||
|
||
void QueryObjects(const FunctionCallbackInfo<Value>& args) { | ||
CHECK_EQ(args.Length(), 1); | ||
Isolate* isolate = args.GetIsolate(); | ||
if (!args[0]->IsObject()) { | ||
args.GetReturnValue().Set(Array::New(isolate)); | ||
return; | ||
} | ||
Local<Object> proto = args[0].As<Object>(); | ||
Local<Context> context = isolate->GetCurrentContext(); | ||
PrototypeChainHas prototype_chain_has(context, proto.As<Object>()); | ||
std::vector<Global<Object>> out; | ||
isolate->GetHeapProfiler()->QueryObjects(context, &prototype_chain_has, &out); | ||
std::vector<Local<Value>> result; | ||
result.reserve(out.size()); | ||
for (size_t i = 0; i < out.size(); ++i) { | ||
result.push_back(out[i].Get(isolate)); | ||
} | ||
|
||
args.GetReturnValue().Set(Array::New(isolate, result.data(), result.size())); | ||
} | ||
|
||
void Initialize(Local<Object> target, | ||
Local<Value> unused, | ||
Local<Context> context, | ||
void* priv) { | ||
SetMethod(context, target, "queryObjects", QueryObjects); | ||
} | ||
|
||
void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | ||
registry->Register(QueryObjects); | ||
} | ||
|
||
} // namespace internal_only_v8 | ||
} // namespace node | ||
|
||
NODE_BINDING_CONTEXT_AWARE_INTERNAL(internal_only_v8, | ||
node::internal_only_v8::Initialize) | ||
NODE_BINDING_EXTERNAL_REFERENCE( | ||
internal_only_v8, node::internal_only_v8::RegisterExternalReferences) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.