Commit 3640583f authored by Clemens Backes's avatar Clemens Backes Committed by Commit Bot

[inspector][fuzzer] Extract functionality for reuse

This CL extracts some functionality from inspector-test.cc to be reused
by the inspector fuzzer.

It also puts all functions in the v8::internal namespace, and adds
separate functions in v8::internal to be called by ::main such that
we have direct access to the full namespace there.

R=szuend@chromium.org

Bug: chromium:1142437
Change-Id: I671317822bdc1c721334469811893affcc460f8f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2501847Reviewed-by: 's avatarSimon Zünd <szuend@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#70821}
parent f1a8d143
......@@ -13,6 +13,10 @@ v8_executable("inspector-test") {
"isolate-data.h",
"task-runner.cc",
"task-runner.h",
"tasks.cc",
"tasks.h",
"utils.cc",
"utils.h",
]
configs = [
......
include_rules = [
"-src",
"+src/base/atomic-utils.h",
"+src/base/macros.h",
"+src/base/platform/platform.h",
"+src/base",
"+src/flags/flags.h",
"+src/heap/read-only-heap.h",
"+src/inspector/test-interface.h",
"+src/utils/locked-queue-inl.h",
"+src/utils/utils.h",
"+src/utils/vector.h",
"+src/utils",
]
This diff is collapsed.
......@@ -5,37 +5,27 @@
#include "test/inspector/isolate-data.h"
#include "src/inspector/test-interface.h"
#include "src/utils/vector.h"
#include "test/inspector/task-runner.h"
#include "test/inspector/utils.h"
namespace v8 {
namespace internal {
namespace {
const int kIsolateDataIndex = 2;
const int kContextGroupIdIndex = 3;
v8::internal::Vector<uint16_t> ToVector(v8::Isolate* isolate,
v8::Local<v8::String> str) {
v8::internal::Vector<uint16_t> buffer =
v8::internal::Vector<uint16_t>::New(str->Length());
// TODO(clemensb): This is a memory leak; remove this helper.
Vector<uint16_t> ToV8Vector(v8::Isolate* isolate, v8::Local<v8::String> str) {
Vector<uint16_t> buffer = Vector<uint16_t>::New(str->Length());
str->Write(isolate, buffer.begin(), 0, str->Length());
return buffer;
}
v8::Local<v8::String> ToString(v8::Isolate* isolate,
const v8_inspector::StringView& string) {
if (string.is8Bit())
return v8::String::NewFromOneByte(isolate, string.characters8(),
v8::NewStringType::kNormal,
static_cast<int>(string.length()))
.ToLocalChecked();
else
return v8::String::NewFromTwoByte(isolate, string.characters16(),
v8::NewStringType::kNormal,
static_cast<int>(string.length()))
.ToLocalChecked();
}
void Print(v8::Isolate* isolate, const v8_inspector::StringView& string) {
v8::Local<v8::String> v8_string = ToString(isolate, string);
v8::Local<v8::String> v8_string = ToV8String(isolate, string);
v8::String::Utf8Value utf8_string(isolate, v8_string);
fwrite(*utf8_string, sizeof(**utf8_string), utf8_string.length(), stdout);
}
......@@ -128,7 +118,7 @@ int IsolateData::GetContextGroupId(v8::Local<v8::Context> context) {
}
void IsolateData::RegisterModule(v8::Local<v8::Context> context,
v8::internal::Vector<uint16_t> name,
Vector<uint16_t> name,
v8::ScriptCompiler::Source* source) {
v8::Local<v8::Module> module;
if (!v8::ScriptCompiler::CompileModule(isolate(), source).ToLocal(&module))
......@@ -148,7 +138,7 @@ v8::MaybeLocal<v8::Module> IsolateData::ModuleResolveCallback(
v8::Local<v8::Module> referrer) {
IsolateData* data = IsolateData::FromContext(context);
std::string str = *v8::String::Utf8Value(data->isolate(), specifier);
return data->modules_[ToVector(data->isolate(), specifier)].Get(
return data->modules_[ToV8Vector(data->isolate(), specifier)].Get(
data->isolate());
}
......@@ -284,13 +274,12 @@ int IsolateData::HandleMessage(v8::Local<v8::Message> message,
column_number = message->GetStartColumn(context).FromJust() + 1;
v8_inspector::StringView detailed_message;
v8::internal::Vector<uint16_t> message_text_string =
ToVector(isolate, message->Get());
Vector<uint16_t> message_text_string = ToV8Vector(isolate, message->Get());
v8_inspector::StringView message_text(message_text_string.begin(),
message_text_string.length());
v8::internal::Vector<uint16_t> url_string;
Vector<uint16_t> url_string;
if (message->GetScriptOrigin().ResourceName()->IsString()) {
url_string = ToVector(
url_string = ToV8Vector(
isolate, message->GetScriptOrigin().ResourceName().As<v8::String>());
}
v8_inspector::StringView url(url_string.begin(), url_string.length());
......@@ -403,7 +392,7 @@ void IsolateData::SetCurrentTimeMS(double time) {
double IsolateData::currentTimeMS() {
if (current_time_set_) return current_time_;
return v8::internal::V8::GetCurrentPlatform()->CurrentClockTimeMillis();
return V8::GetCurrentPlatform()->CurrentClockTimeMillis();
}
void IsolateData::SetMemoryInfo(v8::Local<v8::Value> memory_info) {
......@@ -420,7 +409,7 @@ void IsolateData::SetLogMaxAsyncCallStackDepthChanged(bool log) {
void IsolateData::SetAdditionalConsoleApi(v8_inspector::StringView api_script) {
v8::HandleScope handle_scope(isolate());
additional_console_api_.Reset(isolate(), ToString(isolate(), api_script));
additional_console_api_.Reset(isolate(), ToV8String(isolate(), api_script));
}
v8::MaybeLocal<v8::Value> IsolateData::memoryInfo(v8::Isolate* isolate,
......@@ -482,14 +471,14 @@ namespace {
class StringBufferImpl : public v8_inspector::StringBuffer {
public:
StringBufferImpl(v8::Isolate* isolate, v8::Local<v8::String> string)
: data_(ToVector(isolate, string)) {}
: data_(ToV8Vector(isolate, string)) {}
v8_inspector::StringView string() const override {
return v8_inspector::StringView(data_.begin(), data_.length());
}
private:
v8::internal::Vector<uint16_t> data_;
Vector<uint16_t> data_;
};
} // anonymous namespace
......@@ -497,9 +486,12 @@ std::unique_ptr<v8_inspector::StringBuffer> IsolateData::resourceNameToUrl(
const v8_inspector::StringView& resourceName) {
if (resource_name_prefix_.IsEmpty()) return nullptr;
v8::HandleScope handle_scope(isolate());
v8::Local<v8::String> name = ToString(isolate(), resourceName);
v8::Local<v8::String> name = ToV8String(isolate(), resourceName);
v8::Local<v8::String> prefix = resource_name_prefix_.Get(isolate());
v8::Local<v8::String> url = v8::String::Concat(isolate(), prefix, name);
return std::unique_ptr<StringBufferImpl>(
new StringBufferImpl(isolate(), url));
}
} // namespace internal
} // namespace v8
......@@ -15,6 +15,9 @@
#include "src/base/platform/platform.h"
#include "src/utils/vector.h"
namespace v8 {
namespace internal {
class TaskRunner;
class IsolateData : public v8_inspector::V8InspectorClient {
......@@ -158,4 +161,7 @@ class IsolateData : public v8_inspector::V8InspectorClient {
DISALLOW_COPY_AND_ASSIGN(IsolateData);
};
} // namespace internal
} // namespace v8
#endif // V8_TEST_INSPECTOR_PROTOCOL_ISOLATE_DATA_H_
......@@ -11,6 +11,9 @@
#include <unistd.h> // NOLINT
#endif // !defined(_WIN32) && !defined(_WIN64)
namespace v8 {
namespace internal {
namespace {
void ReportUncaughtException(v8::Isolate* isolate,
......@@ -123,3 +126,6 @@ TaskRunner::Task* TaskRunner::GetNext(bool only_protocol) {
}
return nullptr;
}
} // namespace internal
} // namespace v8
......@@ -17,6 +17,9 @@
#include "src/utils/vector.h"
#include "test/inspector/isolate-data.h"
namespace v8 {
namespace internal {
class TaskRunner : public v8::base::Thread {
public:
class Task {
......@@ -69,4 +72,7 @@ class TaskRunner : public v8::base::Thread {
DISALLOW_COPY_AND_ASSIGN(TaskRunner);
};
} // namespace internal
} // namespace v8
#endif // V8_TEST_INSPECTOR_PROTOCOL_TASK_RUNNER_H_
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "test/inspector/tasks.h"
#include <vector>
#include "include/v8-inspector.h"
#include "include/v8.h"
#include "test/inspector/isolate-data.h"
#include "test/inspector/utils.h"
namespace v8 {
namespace internal {
void ExecuteStringTask::Run(IsolateData* data) {
v8::MicrotasksScope microtasks_scope(data->isolate(),
v8::MicrotasksScope::kRunMicrotasks);
v8::HandleScope handle_scope(data->isolate());
v8::Local<v8::Context> context = data->GetDefaultContext(context_group_id_);
v8::Context::Scope context_scope(context);
v8::ScriptOrigin origin(
ToV8String(data->isolate(), name_),
v8::Integer::New(data->isolate(), line_offset_),
v8::Integer::New(data->isolate(), column_offset_),
/* resource_is_shared_cross_origin */ v8::Local<v8::Boolean>(),
/* script_id */ v8::Local<v8::Integer>(),
/* source_map_url */ v8::Local<v8::Value>(),
/* resource_is_opaque */ v8::Local<v8::Boolean>(),
/* is_wasm */ v8::Local<v8::Boolean>(),
v8::Boolean::New(data->isolate(), is_module_));
v8::Local<v8::String> source;
if (expression_.size() != 0)
source = ToV8String(data->isolate(), expression_);
else
source = ToV8String(data->isolate(), expression_utf8_);
v8::ScriptCompiler::Source scriptSource(source, origin);
v8::Isolate::SafeForTerminationScope allowTermination(data->isolate());
if (!is_module_) {
v8::Local<v8::Script> script;
if (!v8::ScriptCompiler::Compile(context, &scriptSource).ToLocal(&script))
return;
v8::MaybeLocal<v8::Value> result;
result = script->Run(context);
} else {
// Register Module takes ownership of {buffer}, so we need to make a copy.
int length = static_cast<int>(name_.size());
v8::internal::Vector<uint16_t> buffer =
v8::internal::Vector<uint16_t>::New(length);
std::copy(name_.begin(), name_.end(), buffer.begin());
data->RegisterModule(context, buffer, &scriptSource);
}
}
} // namespace internal
} // namespace v8
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_TEST_INSPECTOR_TASKS_H_
#define V8_TEST_INSPECTOR_TASKS_H_
#include <vector>
#include "include/v8-inspector.h"
#include "include/v8.h"
#include "src/base/platform/semaphore.h"
#include "test/inspector/task-runner.h"
#include "test/inspector/utils.h"
namespace v8 {
namespace internal {
template <typename T>
void RunSyncTask(TaskRunner* task_runner, T callback) {
class SyncTask : public TaskRunner::Task {
public:
SyncTask(v8::base::Semaphore* ready_semaphore, T callback)
: ready_semaphore_(ready_semaphore), callback_(callback) {}
~SyncTask() override = default;
bool is_priority_task() final { return true; }
private:
void Run(IsolateData* data) override {
callback_(data);
if (ready_semaphore_) ready_semaphore_->Signal();
}
v8::base::Semaphore* ready_semaphore_;
T callback_;
};
v8::base::Semaphore ready_semaphore(0);
task_runner->Append(new SyncTask(&ready_semaphore, callback));
ready_semaphore.Wait();
}
class SendMessageToBackendTask : public TaskRunner::Task {
public:
SendMessageToBackendTask(int session_id, const std::vector<uint16_t>& message)
: session_id_(session_id), message_(message) {}
bool is_priority_task() final { return true; }
private:
void Run(IsolateData* data) override {
v8_inspector::StringView message_view(message_.data(), message_.size());
data->SendMessage(session_id_, message_view);
}
int session_id_;
std::vector<uint16_t> message_;
};
inline void RunAsyncTask(TaskRunner* task_runner,
const v8_inspector::StringView& task_name,
TaskRunner::Task* task) {
class AsyncTask : public TaskRunner::Task {
public:
explicit AsyncTask(TaskRunner::Task* inner) : inner_(inner) {}
~AsyncTask() override = default;
bool is_priority_task() override { return inner_->is_priority_task(); }
void Run(IsolateData* data) override {
data->AsyncTaskStarted(inner_.get());
inner_->Run(data);
data->AsyncTaskFinished(inner_.get());
}
private:
std::unique_ptr<TaskRunner::Task> inner_;
DISALLOW_COPY_AND_ASSIGN(AsyncTask);
};
task_runner->data()->AsyncTaskScheduled(task_name, task, false);
task_runner->Append(new AsyncTask(task));
}
class ExecuteStringTask : public TaskRunner::Task {
public:
ExecuteStringTask(v8::Isolate* isolate, int context_group_id,
const std::vector<uint16_t>& expression,
v8::Local<v8::String> name,
v8::Local<v8::Integer> line_offset,
v8::Local<v8::Integer> column_offset,
v8::Local<v8::Boolean> is_module)
: expression_(expression),
name_(ToVector(isolate, name)),
line_offset_(line_offset.As<v8::Int32>()->Value()),
column_offset_(column_offset.As<v8::Int32>()->Value()),
is_module_(is_module->Value()),
context_group_id_(context_group_id) {}
ExecuteStringTask(const std::string& expression, int context_group_id)
: expression_utf8_(expression), context_group_id_(context_group_id) {}
~ExecuteStringTask() override = default;
bool is_priority_task() override { return false; }
void Run(IsolateData* data) override;
private:
std::vector<uint16_t> expression_;
std::string expression_utf8_;
std::vector<uint16_t> name_;
int32_t line_offset_ = 0;
int32_t column_offset_ = 0;
bool is_module_ = false;
int context_group_id_;
DISALLOW_COPY_AND_ASSIGN(ExecuteStringTask);
};
} // namespace internal
} // namespace v8
#endif // V8_TEST_INSPECTOR_TASKS_H_
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "test/inspector/utils.h"
#include <vector>
#include "include/v8-inspector.h"
#include "include/v8.h"
namespace v8 {
namespace internal {
std::vector<uint8_t> ToBytes(v8::Isolate* isolate, v8::Local<v8::String> str) {
std::vector<uint8_t> buffer(str->Length());
str->WriteOneByte(isolate, buffer.data(), 0, str->Length());
return buffer;
}
v8::Local<v8::String> ToV8String(v8::Isolate* isolate, const char* str) {
return v8::String::NewFromUtf8(isolate, str).ToLocalChecked();
}
v8::Local<v8::String> ToV8String(v8::Isolate* isolate,
const std::vector<uint8_t>& bytes) {
return v8::String::NewFromOneByte(isolate, bytes.data(),
v8::NewStringType::kNormal,
static_cast<int>(bytes.size()))
.ToLocalChecked();
}
v8::Local<v8::String> ToV8String(v8::Isolate* isolate,
const std::string& buffer) {
int length = static_cast<int>(buffer.size());
return v8::String::NewFromUtf8(isolate, buffer.data(),
v8::NewStringType::kNormal, length)
.ToLocalChecked();
}
v8::Local<v8::String> ToV8String(v8::Isolate* isolate,
const std::vector<uint16_t>& buffer) {
int length = static_cast<int>(buffer.size());
return v8::String::NewFromTwoByte(isolate, buffer.data(),
v8::NewStringType::kNormal, length)
.ToLocalChecked();
}
v8::Local<v8::String> ToV8String(v8::Isolate* isolate,
const v8_inspector::StringView& string) {
if (string.is8Bit()) {
return v8::String::NewFromOneByte(isolate, string.characters8(),
v8::NewStringType::kNormal,
static_cast<int>(string.length()))
.ToLocalChecked();
}
return v8::String::NewFromTwoByte(isolate, string.characters16(),
v8::NewStringType::kNormal,
static_cast<int>(string.length()))
.ToLocalChecked();
}
std::vector<uint16_t> ToVector(v8::Isolate* isolate,
v8::Local<v8::String> str) {
std::vector<uint16_t> buffer(str->Length());
str->Write(isolate, buffer.data(), 0, str->Length());
return buffer;
}
std::vector<uint16_t> ToVector(const v8_inspector::StringView& string) {
std::vector<uint16_t> buffer(string.length());
for (size_t i = 0; i < string.length(); i++) {
if (string.is8Bit())
buffer[i] = string.characters8()[i];
else
buffer[i] = string.characters16()[i];
}
return buffer;
}
} // namespace internal
} // namespace v8
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_TEST_INSPECTOR_UTILS_H_
#define V8_TEST_INSPECTOR_UTILS_H_
#include <vector>
#include "include/v8-inspector.h"
#include "include/v8.h"
#include "src/base/macros.h"
namespace v8 {
namespace internal {
std::vector<uint8_t> ToBytes(v8::Isolate*, v8::Local<v8::String>);
v8::Local<v8::String> ToV8String(v8::Isolate*, const char*);
v8::Local<v8::String> ToV8String(v8::Isolate*, const std::vector<uint8_t>&);
v8::Local<v8::String> ToV8String(v8::Isolate*, const std::string&);
v8::Local<v8::String> ToV8String(v8::Isolate*, const std::vector<uint16_t>&);
v8::Local<v8::String> ToV8String(v8::Isolate*, const v8_inspector::StringView&);
std::vector<uint16_t> ToVector(v8::Isolate*, v8::Local<v8::String>);
std::vector<uint16_t> ToVector(const v8_inspector::StringView&);
} // namespace internal
} // namespace v8
#endif // V8_TEST_INSPECTOR_UTILS_H_
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