isolate-data.cc 18.6 KB
Newer Older
1 2 3 4 5 6
// Copyright 2017 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/isolate-data.h"

7
#include "src/inspector/test-interface.h"
8
#include "src/utils/vector.h"
9
#include "test/inspector/task-runner.h"
10 11 12 13
#include "test/inspector/utils.h"

namespace v8 {
namespace internal {
14 15 16 17

namespace {

const int kIsolateDataIndex = 2;
18
const int kContextGroupIdIndex = 3;
19

20
void Print(v8::Isolate* isolate, const v8_inspector::StringView& string) {
21
  v8::Local<v8::String> v8_string = ToV8String(isolate, string);
22
  v8::String::Utf8Value utf8_string(isolate, v8_string);
23 24 25
  fwrite(*utf8_string, sizeof(**utf8_string), utf8_string.length(), stdout);
}

26 27 28 29
class Inspectable : public v8_inspector::V8InspectorSession::Inspectable {
 public:
  Inspectable(v8::Isolate* isolate, v8::Local<v8::Value> object)
      : object_(isolate, object) {}
30
  ~Inspectable() override = default;
31 32 33 34 35 36 37 38
  v8::Local<v8::Value> get(v8::Local<v8::Context> context) override {
    return object_.Get(context->GetIsolate());
  }

 private:
  v8::Global<v8::Value> object_;
};

39 40 41 42
}  //  namespace

IsolateData::IsolateData(TaskRunner* task_runner,
                         IsolateData::SetupGlobalTasks setup_global_tasks,
43 44
                         v8::StartupData* startup_data,
                         WithInspector with_inspector)
45
    : task_runner_(task_runner),
46
      setup_global_tasks_(std::move(setup_global_tasks)) {
47
  v8::Isolate::CreateParams params;
48 49 50
  array_buffer_allocator_.reset(
      v8::ArrayBuffer::Allocator::NewDefaultAllocator());
  params.array_buffer_allocator = array_buffer_allocator_.get();
51
  params.snapshot_blob = startup_data;
52
  params.only_terminate_in_safe_scope = true;
53
  isolate_.reset(v8::Isolate::New(params));
54
  isolate_->SetMicrotasksPolicy(v8::MicrotasksPolicy::kScoped);
55
  if (with_inspector) {
56
    isolate_->AddMessageListener(&IsolateData::MessageHandler);
57
    isolate_->SetPromiseRejectCallback(&IsolateData::PromiseRejectHandler);
58
    inspector_ = v8_inspector::V8Inspector::create(isolate_.get(), this);
59
  }
60
  v8::HandleScope handle_scope(isolate_.get());
61
  not_inspectable_private_.Reset(
62
      isolate_.get(),
63 64 65
      v8::Private::ForApi(
          isolate_.get(),
          v8::String::NewFromUtf8Literal(isolate_.get(), "notInspectable")));
66 67 68 69 70 71 72 73
}

IsolateData* IsolateData::FromContext(v8::Local<v8::Context> context) {
  return static_cast<IsolateData*>(
      context->GetAlignedPointerFromEmbedderData(kIsolateDataIndex));
}

int IsolateData::CreateContextGroup() {
74
  int context_group_id = ++last_context_group_id_;
75 76 77 78
  if (!CreateContext(context_group_id, v8_inspector::StringView())) {
    DCHECK(isolate_->IsExecutionTerminating());
    return -1;
  }
79 80 81
  return context_group_id;
}

82
bool IsolateData::CreateContext(int context_group_id,
83
                                v8_inspector::StringView name) {
84
  v8::HandleScope handle_scope(isolate_.get());
85
  v8::Local<v8::ObjectTemplate> global_template =
86
      v8::ObjectTemplate::New(isolate_.get());
87 88
  for (auto it = setup_global_tasks_.begin(); it != setup_global_tasks_.end();
       ++it) {
89
    (*it)->Run(isolate_.get(), global_template);
90 91
  }
  v8::Local<v8::Context> context =
92
      v8::Context::New(isolate_.get(), nullptr, global_template);
93
  if (context.IsEmpty()) return false;
94
  context->SetAlignedPointerInEmbedderData(kIsolateDataIndex, this);
95 96 97
  // Should be 2-byte aligned.
  context->SetAlignedPointerInEmbedderData(
      kContextGroupIdIndex, reinterpret_cast<void*>(context_group_id * 2));
98 99
  contexts_[context_group_id].emplace_back(isolate_.get(), context);
  if (inspector_) FireContextCreated(context, context_group_id, name);
100
  return true;
101 102
}

103 104
v8::Local<v8::Context> IsolateData::GetDefaultContext(int context_group_id) {
  return contexts_[context_group_id].begin()->Get(isolate_.get());
105 106
}

107
void IsolateData::ResetContextGroup(int context_group_id) {
108
  v8::SealHandleScope seal_handle_scope(isolate());
109 110 111
  inspector_->resetContextGroup(context_group_id);
}

112 113 114 115 116 117 118
int IsolateData::GetContextGroupId(v8::Local<v8::Context> context) {
  return static_cast<int>(
      reinterpret_cast<intptr_t>(
          context->GetAlignedPointerFromEmbedderData(kContextGroupIdIndex)) /
      2);
}

119
void IsolateData::RegisterModule(v8::Local<v8::Context> context,
120
                                 std::vector<uint16_t> name,
121 122 123 124
                                 v8::ScriptCompiler::Source* source) {
  v8::Local<v8::Module> module;
  if (!v8::ScriptCompiler::CompileModule(isolate(), source).ToLocal(&module))
    return;
125 126
  if (!module->InstantiateModule(context, &IsolateData::ModuleResolveCallback)
           .FromMaybe(false)) {
127
    return;
128
  }
129 130
  v8::Local<v8::Value> result;
  if (!module->Evaluate(context).ToLocal(&result)) return;
131
  modules_[name] = v8::Global<v8::Module>(isolate_.get(), module);
132 133
}

134
// static
135 136
v8::MaybeLocal<v8::Module> IsolateData::ModuleResolveCallback(
    v8::Local<v8::Context> context, v8::Local<v8::String> specifier,
137
    v8::Local<v8::FixedArray> import_assertions,
138
    v8::Local<v8::Module> referrer) {
139
  // TODO(v8:11189) Consider JSON modules support in the InspectorClient
140
  IsolateData* data = IsolateData::FromContext(context);
141
  std::string str = *v8::String::Utf8Value(data->isolate(), specifier);
142
  return data->modules_[ToVector(data->isolate(), specifier)].Get(
143
      data->isolate());
144
}
145 146

int IsolateData::ConnectSession(int context_group_id,
147 148
                                const v8_inspector::StringView& state,
                                v8_inspector::V8Inspector::Channel* channel) {
149
  v8::SealHandleScope seal_handle_scope(isolate());
150
  int session_id = ++last_session_id_;
151
  sessions_[session_id] = inspector_->connect(context_group_id, channel, state);
152 153 154 155
  context_group_by_session_[sessions_[session_id].get()] = context_group_id;
  return session_id;
}

156
std::vector<uint8_t> IsolateData::DisconnectSession(int session_id) {
157
  v8::SealHandleScope seal_handle_scope(isolate());
158 159 160
  auto it = sessions_.find(session_id);
  CHECK(it != sessions_.end());
  context_group_by_session_.erase(it->second.get());
161
  std::vector<uint8_t> result = it->second->state();
162 163 164 165 166 167
  sessions_.erase(it);
  return result;
}

void IsolateData::SendMessage(int session_id,
                              const v8_inspector::StringView& message) {
168
  v8::SealHandleScope seal_handle_scope(isolate());
169 170 171 172 173 174 175
  auto it = sessions_.find(session_id);
  if (it != sessions_.end()) it->second->dispatchProtocolMessage(message);
}

void IsolateData::BreakProgram(int context_group_id,
                               const v8_inspector::StringView& reason,
                               const v8_inspector::StringView& details) {
176
  v8::SealHandleScope seal_handle_scope(isolate());
177 178 179 180 181 182 183 184 185
  for (int session_id : GetSessionIds(context_group_id)) {
    auto it = sessions_.find(session_id);
    if (it != sessions_.end()) it->second->breakProgram(reason, details);
  }
}

void IsolateData::SchedulePauseOnNextStatement(
    int context_group_id, const v8_inspector::StringView& reason,
    const v8_inspector::StringView& details) {
186
  v8::SealHandleScope seal_handle_scope(isolate());
187 188 189 190 191 192 193 194
  for (int session_id : GetSessionIds(context_group_id)) {
    auto it = sessions_.find(session_id);
    if (it != sessions_.end())
      it->second->schedulePauseOnNextStatement(reason, details);
  }
}

void IsolateData::CancelPauseOnNextStatement(int context_group_id) {
195
  v8::SealHandleScope seal_handle_scope(isolate());
196 197 198 199 200 201
  for (int session_id : GetSessionIds(context_group_id)) {
    auto it = sessions_.find(session_id);
    if (it != sessions_.end()) it->second->cancelPauseOnNextStatement();
  }
}

202 203
void IsolateData::AsyncTaskScheduled(const v8_inspector::StringView& name,
                                     void* task, bool recurring) {
204
  v8::SealHandleScope seal_handle_scope(isolate());
205 206 207 208
  inspector_->asyncTaskScheduled(name, task, recurring);
}

void IsolateData::AsyncTaskStarted(void* task) {
209
  v8::SealHandleScope seal_handle_scope(isolate());
210 211 212 213
  inspector_->asyncTaskStarted(task);
}

void IsolateData::AsyncTaskFinished(void* task) {
214
  v8::SealHandleScope seal_handle_scope(isolate());
215 216 217
  inspector_->asyncTaskFinished(task);
}

218 219
v8_inspector::V8StackTraceId IsolateData::StoreCurrentStackTrace(
    const v8_inspector::StringView& description) {
220
  v8::SealHandleScope seal_handle_scope(isolate());
221 222 223 224 225
  return inspector_->storeCurrentStackTrace(description);
}

void IsolateData::ExternalAsyncTaskStarted(
    const v8_inspector::V8StackTraceId& parent) {
226
  v8::SealHandleScope seal_handle_scope(isolate());
227 228 229 230 231
  inspector_->externalAsyncTaskStarted(parent);
}

void IsolateData::ExternalAsyncTaskFinished(
    const v8_inspector::V8StackTraceId& parent) {
232
  v8::SealHandleScope seal_handle_scope(isolate());
233 234 235
  inspector_->externalAsyncTaskFinished(parent);
}

236 237
void IsolateData::AddInspectedObject(int session_id,
                                     v8::Local<v8::Value> object) {
238
  v8::SealHandleScope seal_handle_scope(isolate());
239 240
  auto it = sessions_.find(session_id);
  if (it == sessions_.end()) return;
241 242
  std::unique_ptr<Inspectable> inspectable(
      new Inspectable(isolate_.get(), object));
243 244 245
  it->second->addInspectedObject(std::move(inspectable));
}

246
void IsolateData::SetMaxAsyncTaskStacksForTest(int limit) {
247
  v8::SealHandleScope seal_handle_scope(isolate());
248 249 250 251
  v8_inspector::SetMaxAsyncTaskStacksForTest(inspector_.get(), limit);
}

void IsolateData::DumpAsyncTaskStacksStateForTest() {
252
  v8::SealHandleScope seal_handle_scope(isolate());
253 254 255
  v8_inspector::DumpAsyncTaskStacksStateForTest(inspector_.get());
}

256
// static
257 258
int IsolateData::HandleMessage(v8::Local<v8::Message> message,
                               v8::Local<v8::Value> exception) {
259
  v8::Isolate* isolate = message->GetIsolate();
260
  v8::Local<v8::Context> context = isolate->GetEnteredOrMicrotaskContext();
261
  if (context.IsEmpty()) return 0;
262 263 264 265
  v8_inspector::V8Inspector* inspector =
      IsolateData::FromContext(context)->inspector_.get();

  v8::Local<v8::StackTrace> stack = message->GetStackTrace();
266
  int script_id = message->GetScriptOrigin().ScriptId();
267
  if (!stack.IsEmpty() && stack->GetFrameCount() > 0) {
268
    int top_script_id = stack->GetFrame(isolate, 0)->GetScriptId();
269 270 271 272 273 274 275 276
    if (top_script_id == script_id) script_id = 0;
  }
  int line_number = message->GetLineNumber(context).FromMaybe(0);
  int column_number = 0;
  if (message->GetStartColumn(context).IsJust())
    column_number = message->GetStartColumn(context).FromJust() + 1;

  v8_inspector::StringView detailed_message;
277 278 279 280
  std::vector<uint16_t> message_text_string = ToVector(isolate, message->Get());
  v8_inspector::StringView message_text(message_text_string.data(),
                                        message_text_string.size());
  std::vector<uint16_t> url_string;
281
  if (message->GetScriptOrigin().ResourceName()->IsString()) {
282
    url_string = ToVector(
283
        isolate, message->GetScriptOrigin().ResourceName().As<v8::String>());
284
  }
285
  v8_inspector::StringView url(url_string.data(), url_string.size());
286

287
  v8::SealHandleScope seal_handle_scope(isolate);
288 289 290 291 292 293 294 295 296 297 298 299 300
  return inspector->exceptionThrown(
      context, message_text, exception, detailed_message, url, line_number,
      column_number, inspector->createStackTrace(stack), script_id);
}

// static
void IsolateData::MessageHandler(v8::Local<v8::Message> message,
                                 v8::Local<v8::Value> exception) {
  HandleMessage(message, exception);
}

// static
void IsolateData::PromiseRejectHandler(v8::PromiseRejectMessage data) {
301
  v8::Isolate* isolate = data.GetPromise()->GetIsolate();
302
  v8::Local<v8::Context> context = isolate->GetEnteredOrMicrotaskContext();
303 304 305
  if (context.IsEmpty()) return;
  v8::Local<v8::Promise> promise = data.GetPromise();
  v8::Local<v8::Private> id_private = v8::Private::ForApi(
306
      isolate, v8::String::NewFromUtf8Literal(isolate, "id"));
307 308 309 310 311 312 313

  if (data.GetEvent() == v8::kPromiseHandlerAddedAfterReject) {
    v8::Local<v8::Value> id;
    if (!promise->GetPrivate(context, id_private).ToLocal(&id)) return;
    if (!id->IsInt32()) return;
    v8_inspector::V8Inspector* inspector =
        IsolateData::FromContext(context)->inspector_.get();
314
    v8::SealHandleScope seal_handle_scope(isolate);
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    const char* reason_str = "Handler added to rejected promise";
    inspector->exceptionRevoked(
        context, id.As<v8::Int32>()->Value(),
        v8_inspector::StringView(reinterpret_cast<const uint8_t*>(reason_str),
                                 strlen(reason_str)));
    return;
  }

  v8::Local<v8::Value> exception = data.GetValue();
  int exception_id = HandleMessage(
      v8::Exception::CreateMessage(isolate, exception), exception);
  if (exception_id) {
    promise
        ->SetPrivate(isolate->GetCurrentContext(), id_private,
                     v8::Int32::New(isolate, exception_id))
        .ToChecked();
  }
332 333 334
}

void IsolateData::FireContextCreated(v8::Local<v8::Context> context,
335 336 337
                                     int context_group_id,
                                     v8_inspector::StringView name) {
  v8_inspector::V8ContextInfo info(context, context_group_id, name);
338
  info.hasMemoryOnConsole = true;
339
  v8::SealHandleScope seal_handle_scope(isolate());
340 341 342 343
  inspector_->contextCreated(info);
}

void IsolateData::FireContextDestroyed(v8::Local<v8::Context> context) {
344
  v8::SealHandleScope seal_handle_scope(isolate());
345 346 347
  inspector_->contextDestroyed(context);
}

348 349 350 351 352 353 354
void IsolateData::FreeContext(v8::Local<v8::Context> context) {
  int context_group_id = GetContextGroupId(context);
  auto it = contexts_.find(context_group_id);
  if (it == contexts_.end()) return;
  contexts_.erase(it);
}

355 356 357 358 359 360 361 362 363 364
std::vector<int> IsolateData::GetSessionIds(int context_group_id) {
  std::vector<int> result;
  for (auto& it : sessions_) {
    if (context_group_by_session_[it.second.get()] == context_group_id)
      result.push_back(it.first);
  }
  return result;
}

bool IsolateData::formatAccessorsAsProperties(v8::Local<v8::Value> object) {
365
  v8::Local<v8::Context> context = isolate()->GetCurrentContext();
366
  v8::Local<v8::Private> shouldFormatAccessorsPrivate = v8::Private::ForApi(
367 368
      isolate(),
      v8::String::NewFromUtf8Literal(isolate(), "allowAccessorFormatting"));
369 370 371 372 373 374
  CHECK(object->IsObject());
  return object.As<v8::Object>()
      ->HasPrivate(context, shouldFormatAccessorsPrivate)
      .FromMaybe(false);
}

375
bool IsolateData::isInspectableHeapObject(v8::Local<v8::Object> object) {
376
  v8::Local<v8::Context> context = isolate()->GetCurrentContext();
377
  v8::MicrotasksScope microtasks_scope(
378 379
      isolate(), v8::MicrotasksScope::kDoNotRunMicrotasks);
  return !object->HasPrivate(context, not_inspectable_private_.Get(isolate()))
380 381 382
              .FromMaybe(false);
}

383 384
v8::Local<v8::Context> IsolateData::ensureDefaultContextInGroup(
    int context_group_id) {
385
  return GetDefaultContext(context_group_id);
386 387 388 389 390 391 392 393 394
}

void IsolateData::SetCurrentTimeMS(double time) {
  current_time_ = time;
  current_time_set_ = true;
}

double IsolateData::currentTimeMS() {
  if (current_time_set_) return current_time_;
395
  return V8::GetCurrentPlatform()->CurrentClockTimeMillis();
396 397 398
}

void IsolateData::SetMemoryInfo(v8::Local<v8::Value> memory_info) {
399
  memory_info_.Reset(isolate_.get(), memory_info);
400 401 402 403 404 405
}

void IsolateData::SetLogConsoleApiMessageCalls(bool log) {
  log_console_api_message_calls_ = log;
}

406 407 408 409
void IsolateData::SetLogMaxAsyncCallStackDepthChanged(bool log) {
  log_max_async_call_stack_depth_changed_ = log;
}

410 411
void IsolateData::SetAdditionalConsoleApi(v8_inspector::StringView api_script) {
  v8::HandleScope handle_scope(isolate());
412
  additional_console_api_.Reset(isolate(), ToV8String(isolate(), api_script));
413 414
}

415 416 417 418 419 420 421
v8::MaybeLocal<v8::Value> IsolateData::memoryInfo(v8::Isolate* isolate,
                                                  v8::Local<v8::Context>) {
  if (memory_info_.IsEmpty()) return v8::MaybeLocal<v8::Value>();
  return memory_info_.Get(isolate);
}

void IsolateData::runMessageLoopOnPause(int) {
422
  v8::SealHandleScope seal_handle_scope(isolate());
423 424 425
  task_runner_->RunMessageLoop(true);
}

426 427 428 429
void IsolateData::quitMessageLoopOnPause() {
  v8::SealHandleScope seal_handle_scope(isolate());
  task_runner_->QuitMessageLoop();
}
430

431 432 433 434 435 436
void IsolateData::installAdditionalCommandLineAPI(
    v8::Local<v8::Context> context, v8::Local<v8::Object> object) {
  if (additional_console_api_.IsEmpty()) return;
  CHECK(context->GetIsolate() == isolate());
  v8::HandleScope handle_scope(isolate());
  v8::Context::Scope context_scope(context);
437 438
  v8::ScriptOrigin origin(isolate(), v8::String::NewFromUtf8Literal(
                                         isolate(), "internal-console-api"));
439 440 441 442 443 444 445
  v8::ScriptCompiler::Source scriptSource(
      additional_console_api_.Get(isolate()), origin);
  v8::MaybeLocal<v8::Script> script =
      v8::ScriptCompiler::Compile(context, &scriptSource);
  CHECK(!script.ToLocalChecked()->Run(context).IsEmpty());
}

446 447 448 449 450 451 452
void IsolateData::consoleAPIMessage(int contextGroupId,
                                    v8::Isolate::MessageErrorLevel level,
                                    const v8_inspector::StringView& message,
                                    const v8_inspector::StringView& url,
                                    unsigned lineNumber, unsigned columnNumber,
                                    v8_inspector::V8StackTrace* stack) {
  if (!log_console_api_message_calls_) return;
453
  Print(isolate_.get(), message);
454
  fprintf(stdout, " (");
455
  Print(isolate_.get(), url);
456
  fprintf(stdout, ":%d:%d)", lineNumber, columnNumber);
457
  Print(isolate_.get(), stack->toString()->string());
458 459
  fprintf(stdout, "\n");
}
460 461 462 463 464

void IsolateData::maxAsyncCallStackDepthChanged(int depth) {
  if (!log_max_async_call_stack_depth_changed_) return;
  fprintf(stdout, "maxAsyncCallStackDepthChanged: %d\n", depth);
}
465 466

void IsolateData::SetResourceNamePrefix(v8::Local<v8::String> prefix) {
467
  resource_name_prefix_.Reset(isolate(), prefix);
468 469 470 471 472 473
}

namespace {
class StringBufferImpl : public v8_inspector::StringBuffer {
 public:
  StringBufferImpl(v8::Isolate* isolate, v8::Local<v8::String> string)
474
      : data_(ToVector(isolate, string)) {}
475 476

  v8_inspector::StringView string() const override {
477
    return v8_inspector::StringView(data_.data(), data_.size());
478
  }
479 480

 private:
481
  std::vector<uint16_t> data_;
482 483 484 485 486 487
};
}  // anonymous namespace

std::unique_ptr<v8_inspector::StringBuffer> IsolateData::resourceNameToUrl(
    const v8_inspector::StringView& resourceName) {
  if (resource_name_prefix_.IsEmpty()) return nullptr;
488
  v8::HandleScope handle_scope(isolate());
489
  v8::Local<v8::String> name = ToV8String(isolate(), resourceName);
490 491
  v8::Local<v8::String> prefix = resource_name_prefix_.Get(isolate());
  v8::Local<v8::String> url = v8::String::Concat(isolate(), prefix, name);
492
  return std::make_unique<StringBufferImpl>(isolate(), url);
493
}
494

495 496 497 498 499 500
int64_t IsolateData::generateUniqueId() {
  static int64_t last_unique_id = 0L;
  // Keep it not too random for tests.
  return ++last_unique_id;
}

501 502
}  // namespace internal
}  // namespace v8