Commit 42a38d2a authored by Anna Henningsen's avatar Anna Henningsen Committed by Commit Bot

[inspector] Do not leak Handles from V8InspectorSession methods

Do not leak handles to the outer scopes from inspector methods.
Add `SealHandleScope`s to the tests and the d8 binding, and
`HandleScope`s in the places in the inspector source where
handles are actually used.

Change-Id: I80b1bb0ccc4778b32e9198513f63d5c0652c8f59
Reviewed-on: https://chromium-review.googlesource.com/c/1484304Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Commit-Queue: Yang Guo <yangguo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59812}
parent 5bba1e46
......@@ -2310,6 +2310,7 @@ class InspectorFrontend final : public v8_inspector::V8Inspector::Channel {
void Send(const v8_inspector::StringView& string) {
v8::Isolate::AllowJavascriptExecutionScope allow_script(isolate_);
v8::HandleScope handle_scope(isolate_);
int length = static_cast<int>(string.length());
DCHECK_LT(length, v8::String::kMaxLength);
Local<String> message =
......@@ -2407,7 +2408,10 @@ class InspectorClient : public v8_inspector::V8InspectorClient {
std::unique_ptr<uint16_t[]> buffer(new uint16_t[length]);
message->Write(isolate, buffer.get(), 0, length);
v8_inspector::StringView message_view(buffer.get(), length);
session->dispatchProtocolMessage(message_view);
{
v8::SealHandleScope seal_handle_scope(isolate);
session->dispatchProtocolMessage(message_view);
}
args.GetReturnValue().Set(True(isolate));
}
......
......@@ -64,6 +64,7 @@ InspectedContext::InspectedContext(V8InspectorImpl* inspector,
v8::WeakCallbackType::kParameter);
if (!info.hasMemoryOnConsole) return;
v8::Context::Scope contextScope(info.context);
v8::HandleScope handleScope(info.context->GetIsolate());
v8::Local<v8::Object> global = info.context->Global();
v8::Local<v8::Value> console;
if (global->Get(info.context, toV8String(m_inspector->isolate(), "console"))
......
......@@ -704,6 +704,7 @@ Response V8DebuggerAgentImpl::continueToLocation(
InspectedContext* inspected = m_inspector->getContext(contextId);
if (!inspected)
return Response::Error("Cannot continue to specified location");
v8::HandleScope handleScope(m_isolate);
v8::Context::Scope contextScope(inspected->context());
return m_debugger->continueToLocation(
m_session->contextGroupId(), script, std::move(location),
......@@ -1124,6 +1125,7 @@ Response V8DebuggerAgentImpl::setReturnValue(
std::unique_ptr<protocol::Runtime::CallArgument> protocolNewValue) {
if (!enabled()) return Response::Error(kDebuggerNotEnabled);
if (!isPaused()) return Response::Error(kDebuggerNotPaused);
v8::HandleScope handleScope(m_isolate);
auto iterator = v8::debug::StackTraceIterator::Create(m_isolate);
if (iterator->Done()) {
return Response::Error("Could not find top call frame");
......
......@@ -249,6 +249,7 @@ void V8Debugger::stepOutOfFunction(int targetContextGroupId) {
bool V8Debugger::asyncStepOutOfFunction(int targetContextGroupId,
bool onlyAtReturn) {
v8::HandleScope handleScope(m_isolate);
auto iterator = v8::debug::StackTraceIterator::Create(m_isolate);
DCHECK(!iterator->Done());
bool atReturn = !iterator->GetReturnValue().IsEmpty();
......@@ -1014,6 +1015,7 @@ std::unique_ptr<V8StackTraceImpl> V8Debugger::captureStackTrace(
int V8Debugger::currentContextGroupId() {
if (!m_isolate->InContext()) return 0;
v8::HandleScope handleScope(m_isolate);
return m_inspector->contextGroupId(m_isolate->GetCurrentContext());
}
......
......@@ -111,6 +111,7 @@ v8::Local<v8::Context> IsolateData::GetContext(int context_group_id) {
}
void IsolateData::ResetContextGroup(int context_group_id) {
v8::SealHandleScope seal_handle_scope(isolate());
inspector_->resetContextGroup(context_group_id);
}
......@@ -149,6 +150,7 @@ v8::MaybeLocal<v8::Module> IsolateData::ModuleResolveCallback(
int IsolateData::ConnectSession(int context_group_id,
const v8_inspector::StringView& state,
v8_inspector::V8Inspector::Channel* channel) {
v8::SealHandleScope seal_handle_scope(isolate());
int session_id = ++last_session_id_;
sessions_[session_id] = inspector_->connect(context_group_id, channel, state);
context_group_by_session_[sessions_[session_id].get()] = context_group_id;
......@@ -157,6 +159,7 @@ int IsolateData::ConnectSession(int context_group_id,
std::unique_ptr<v8_inspector::StringBuffer> IsolateData::DisconnectSession(
int session_id) {
v8::SealHandleScope seal_handle_scope(isolate());
auto it = sessions_.find(session_id);
CHECK(it != sessions_.end());
context_group_by_session_.erase(it->second.get());
......@@ -167,6 +170,7 @@ std::unique_ptr<v8_inspector::StringBuffer> IsolateData::DisconnectSession(
void IsolateData::SendMessage(int session_id,
const v8_inspector::StringView& message) {
v8::SealHandleScope seal_handle_scope(isolate());
auto it = sessions_.find(session_id);
if (it != sessions_.end()) it->second->dispatchProtocolMessage(message);
}
......@@ -174,6 +178,7 @@ void IsolateData::SendMessage(int session_id,
void IsolateData::BreakProgram(int context_group_id,
const v8_inspector::StringView& reason,
const v8_inspector::StringView& details) {
v8::SealHandleScope seal_handle_scope(isolate());
for (int session_id : GetSessionIds(context_group_id)) {
auto it = sessions_.find(session_id);
if (it != sessions_.end()) it->second->breakProgram(reason, details);
......@@ -183,6 +188,7 @@ void IsolateData::BreakProgram(int context_group_id,
void IsolateData::SchedulePauseOnNextStatement(
int context_group_id, const v8_inspector::StringView& reason,
const v8_inspector::StringView& details) {
v8::SealHandleScope seal_handle_scope(isolate());
for (int session_id : GetSessionIds(context_group_id)) {
auto it = sessions_.find(session_id);
if (it != sessions_.end())
......@@ -191,6 +197,7 @@ void IsolateData::SchedulePauseOnNextStatement(
}
void IsolateData::CancelPauseOnNextStatement(int context_group_id) {
v8::SealHandleScope seal_handle_scope(isolate());
for (int session_id : GetSessionIds(context_group_id)) {
auto it = sessions_.find(session_id);
if (it != sessions_.end()) it->second->cancelPauseOnNextStatement();
......@@ -199,34 +206,41 @@ void IsolateData::CancelPauseOnNextStatement(int context_group_id) {
void IsolateData::AsyncTaskScheduled(const v8_inspector::StringView& name,
void* task, bool recurring) {
v8::SealHandleScope seal_handle_scope(isolate());
inspector_->asyncTaskScheduled(name, task, recurring);
}
void IsolateData::AsyncTaskStarted(void* task) {
v8::SealHandleScope seal_handle_scope(isolate());
inspector_->asyncTaskStarted(task);
}
void IsolateData::AsyncTaskFinished(void* task) {
v8::SealHandleScope seal_handle_scope(isolate());
inspector_->asyncTaskFinished(task);
}
v8_inspector::V8StackTraceId IsolateData::StoreCurrentStackTrace(
const v8_inspector::StringView& description) {
v8::SealHandleScope seal_handle_scope(isolate());
return inspector_->storeCurrentStackTrace(description);
}
void IsolateData::ExternalAsyncTaskStarted(
const v8_inspector::V8StackTraceId& parent) {
v8::SealHandleScope seal_handle_scope(isolate());
inspector_->externalAsyncTaskStarted(parent);
}
void IsolateData::ExternalAsyncTaskFinished(
const v8_inspector::V8StackTraceId& parent) {
v8::SealHandleScope seal_handle_scope(isolate());
inspector_->externalAsyncTaskFinished(parent);
}
void IsolateData::AddInspectedObject(int session_id,
v8::Local<v8::Value> object) {
v8::SealHandleScope seal_handle_scope(isolate());
auto it = sessions_.find(session_id);
if (it == sessions_.end()) return;
std::unique_ptr<Inspectable> inspectable(
......@@ -235,10 +249,12 @@ void IsolateData::AddInspectedObject(int session_id,
}
void IsolateData::SetMaxAsyncTaskStacksForTest(int limit) {
v8::SealHandleScope seal_handle_scope(isolate());
v8_inspector::SetMaxAsyncTaskStacksForTest(inspector_.get(), limit);
}
void IsolateData::DumpAsyncTaskStacksStateForTest() {
v8::SealHandleScope seal_handle_scope(isolate());
v8_inspector::DumpAsyncTaskStacksStateForTest(inspector_.get());
}
......@@ -275,6 +291,7 @@ int IsolateData::HandleMessage(v8::Local<v8::Message> message,
}
v8_inspector::StringView url(url_string.start(), url_string.length());
v8::SealHandleScope seal_handle_scope(isolate);
return inspector->exceptionThrown(
context, message_text, exception, detailed_message, url, line_number,
column_number, inspector->createStackTrace(stack), script_id);
......@@ -303,6 +320,7 @@ void IsolateData::PromiseRejectHandler(v8::PromiseRejectMessage data) {
if (!id->IsInt32()) return;
v8_inspector::V8Inspector* inspector =
IsolateData::FromContext(context)->inspector_.get();
v8::SealHandleScope seal_handle_scope(isolate);
const char* reason_str = "Handler added to rejected promise";
inspector->exceptionRevoked(
context, id.As<v8::Int32>()->Value(),
......@@ -327,10 +345,12 @@ void IsolateData::FireContextCreated(v8::Local<v8::Context> context,
v8_inspector::V8ContextInfo info(context, context_group_id,
v8_inspector::StringView());
info.hasMemoryOnConsole = true;
v8::SealHandleScope seal_handle_scope(isolate());
inspector_->contextCreated(info);
}
void IsolateData::FireContextDestroyed(v8::Local<v8::Context> context) {
v8::SealHandleScope seal_handle_scope(isolate());
inspector_->contextDestroyed(context);
}
......@@ -406,10 +426,14 @@ v8::MaybeLocal<v8::Value> IsolateData::memoryInfo(v8::Isolate* isolate,
}
void IsolateData::runMessageLoopOnPause(int) {
v8::SealHandleScope seal_handle_scope(isolate());
task_runner_->RunMessageLoop(true);
}
void IsolateData::quitMessageLoopOnPause() { task_runner_->QuitMessageLoop(); }
void IsolateData::quitMessageLoopOnPause() {
v8::SealHandleScope seal_handle_scope(isolate());
task_runner_->QuitMessageLoop();
}
void IsolateData::consoleAPIMessage(int contextGroupId,
v8::Isolate::MessageErrorLevel level,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment