• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <errno.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/stat.h>
9 
10 #include <algorithm>
11 #include <fstream>
12 #include <iomanip>
13 #include <iterator>
14 #include <string>
15 #include <tuple>
16 #include <type_traits>
17 #include <unordered_map>
18 #include <utility>
19 #include <vector>
20 
21 #ifdef ENABLE_VTUNE_JIT_INTERFACE
22 #include "src/third_party/vtune/v8-vtune.h"
23 #endif
24 
25 #include "include/libplatform/libplatform.h"
26 #include "include/libplatform/v8-tracing.h"
27 #include "include/v8-function.h"
28 #include "include/v8-initialization.h"
29 #include "include/v8-inspector.h"
30 #include "include/v8-json.h"
31 #include "include/v8-locker.h"
32 #include "include/v8-profiler.h"
33 #include "include/v8-wasm.h"
34 #include "src/api/api-inl.h"
35 #include "src/base/cpu.h"
36 #include "src/base/logging.h"
37 #include "src/base/platform/platform.h"
38 #include "src/base/platform/time.h"
39 #include "src/base/platform/wrappers.h"
40 #include "src/base/sanitizer/msan.h"
41 #include "src/base/sys-info.h"
42 #include "src/base/utils/random-number-generator.h"
43 #include "src/d8/d8-console.h"
44 #include "src/d8/d8-platforms.h"
45 #include "src/d8/d8.h"
46 #include "src/debug/debug-interface.h"
47 #include "src/deoptimizer/deoptimizer.h"
48 #include "src/diagnostics/basic-block-profiler.h"
49 #include "src/execution/v8threads.h"
50 #include "src/execution/vm-state-inl.h"
51 #include "src/flags/flags.h"
52 #include "src/handles/maybe-handles.h"
53 #include "src/heap/parked-scope.h"
54 #include "src/init/v8.h"
55 #include "src/interpreter/interpreter.h"
56 #include "src/logging/counters.h"
57 #include "src/logging/log-utils.h"
58 #include "src/objects/managed-inl.h"
59 #include "src/objects/objects-inl.h"
60 #include "src/objects/objects.h"
61 #include "src/parsing/parse-info.h"
62 #include "src/parsing/parsing.h"
63 #include "src/parsing/scanner-character-streams.h"
64 #include "src/profiler/profile-generator.h"
65 #include "src/snapshot/snapshot.h"
66 #include "src/tasks/cancelable-task.h"
67 #include "src/trap-handler/trap-handler.h"
68 #include "src/utils/ostreams.h"
69 #include "src/utils/utils.h"
70 #include "src/web-snapshot/web-snapshot.h"
71 
72 #ifdef V8_FUZZILLI
73 #include "src/d8/cov.h"
74 #endif  // V8_FUZZILLI
75 
76 #ifdef V8_USE_PERFETTO
77 #include "perfetto/tracing.h"
78 #endif  // V8_USE_PERFETTO
79 
80 #ifdef V8_INTL_SUPPORT
81 #include "unicode/locid.h"
82 #endif  // V8_INTL_SUPPORT
83 
84 #ifdef V8_OS_LINUX
85 #include <sys/mman.h>  // For MultiMappedAllocator.
86 #endif
87 
88 #if !defined(_WIN32) && !defined(_WIN64)
89 #include <unistd.h>
90 #else
91 #include <windows.h>
92 #endif                // !defined(_WIN32) && !defined(_WIN64)
93 
94 #ifndef DCHECK
95 #define DCHECK(condition) assert(condition)
96 #endif
97 
98 #ifndef CHECK
99 #define CHECK(condition) assert(condition)
100 #endif
101 
102 #define TRACE_BS(...)                                     \
103   do {                                                    \
104     if (i::FLAG_trace_backing_store) PrintF(__VA_ARGS__); \
105   } while (false)
106 
107 namespace v8 {
108 
109 namespace {
110 
111 const int kMB = 1024 * 1024;
112 
113 #ifdef V8_FUZZILLI
114 // REPRL = read-eval-print-reset-loop
115 // These file descriptors are being opened when Fuzzilli uses fork & execve to
116 // run V8.
117 #define REPRL_CRFD 100  // Control read file decriptor
118 #define REPRL_CWFD 101  // Control write file decriptor
119 #define REPRL_DRFD 102  // Data read file decriptor
120 #define REPRL_DWFD 103  // Data write file decriptor
121 bool fuzzilli_reprl = true;
122 #else
123 bool fuzzilli_reprl = false;
124 #endif  // V8_FUZZILLI
125 
126 const int kMaxSerializerMemoryUsage =
127     1 * kMB;  // Arbitrary maximum for testing.
128 
129 // Base class for shell ArrayBuffer allocators. It forwards all opertions to
130 // the default v8 allocator.
131 class ArrayBufferAllocatorBase : public v8::ArrayBuffer::Allocator {
132  public:
Allocate(size_t length)133   void* Allocate(size_t length) override {
134     return allocator_->Allocate(length);
135   }
136 
AllocateUninitialized(size_t length)137   void* AllocateUninitialized(size_t length) override {
138     return allocator_->AllocateUninitialized(length);
139   }
140 
Free(void * data,size_t length)141   void Free(void* data, size_t length) override {
142     allocator_->Free(data, length);
143   }
144 
145  private:
146   std::unique_ptr<Allocator> allocator_ =
147       std::unique_ptr<Allocator>(NewDefaultAllocator());
148 };
149 
150 // ArrayBuffer allocator that can use virtual memory to improve performance.
151 class ShellArrayBufferAllocator : public ArrayBufferAllocatorBase {
152  public:
Allocate(size_t length)153   void* Allocate(size_t length) override {
154     if (length >= kVMThreshold) return AllocateVM(length);
155     return ArrayBufferAllocatorBase::Allocate(length);
156   }
157 
AllocateUninitialized(size_t length)158   void* AllocateUninitialized(size_t length) override {
159     if (length >= kVMThreshold) return AllocateVM(length);
160     return ArrayBufferAllocatorBase::AllocateUninitialized(length);
161   }
162 
Free(void * data,size_t length)163   void Free(void* data, size_t length) override {
164     if (length >= kVMThreshold) {
165       FreeVM(data, length);
166     } else {
167       ArrayBufferAllocatorBase::Free(data, length);
168     }
169   }
170 
171  private:
172   static constexpr size_t kVMThreshold = 65536;
173 
AllocateVM(size_t length)174   void* AllocateVM(size_t length) {
175     DCHECK_LE(kVMThreshold, length);
176     v8::PageAllocator* page_allocator = i::GetArrayBufferPageAllocator();
177     size_t page_size = page_allocator->AllocatePageSize();
178     size_t allocated = RoundUp(length, page_size);
179     return i::AllocatePages(page_allocator, nullptr, allocated, page_size,
180                             PageAllocator::kReadWrite);
181   }
182 
FreeVM(void * data,size_t length)183   void FreeVM(void* data, size_t length) {
184     v8::PageAllocator* page_allocator = i::GetArrayBufferPageAllocator();
185     size_t page_size = page_allocator->AllocatePageSize();
186     size_t allocated = RoundUp(length, page_size);
187     i::FreePages(page_allocator, data, allocated);
188   }
189 };
190 
191 // ArrayBuffer allocator that never allocates over 10MB.
192 class MockArrayBufferAllocator : public ArrayBufferAllocatorBase {
193  protected:
Allocate(size_t length)194   void* Allocate(size_t length) override {
195     return ArrayBufferAllocatorBase::Allocate(Adjust(length));
196   }
197 
AllocateUninitialized(size_t length)198   void* AllocateUninitialized(size_t length) override {
199     return ArrayBufferAllocatorBase::AllocateUninitialized(Adjust(length));
200   }
201 
Free(void * data,size_t length)202   void Free(void* data, size_t length) override {
203     return ArrayBufferAllocatorBase::Free(data, Adjust(length));
204   }
205 
206  private:
Adjust(size_t length)207   size_t Adjust(size_t length) {
208     const size_t kAllocationLimit = 10 * kMB;
209     return length > kAllocationLimit ? i::AllocatePageSize() : length;
210   }
211 };
212 
213 // ArrayBuffer allocator that can be equipped with a limit to simulate system
214 // OOM.
215 class MockArrayBufferAllocatiorWithLimit : public MockArrayBufferAllocator {
216  public:
MockArrayBufferAllocatiorWithLimit(size_t allocation_limit)217   explicit MockArrayBufferAllocatiorWithLimit(size_t allocation_limit)
218       : space_left_(allocation_limit) {}
219 
220  protected:
Allocate(size_t length)221   void* Allocate(size_t length) override {
222     if (length > space_left_) {
223       return nullptr;
224     }
225     space_left_ -= length;
226     return MockArrayBufferAllocator::Allocate(length);
227   }
228 
AllocateUninitialized(size_t length)229   void* AllocateUninitialized(size_t length) override {
230     if (length > space_left_) {
231       return nullptr;
232     }
233     space_left_ -= length;
234     return MockArrayBufferAllocator::AllocateUninitialized(length);
235   }
236 
Free(void * data,size_t length)237   void Free(void* data, size_t length) override {
238     space_left_ += length;
239     return MockArrayBufferAllocator::Free(data, length);
240   }
241 
242  private:
243   std::atomic<size_t> space_left_;
244 };
245 
246 #if MULTI_MAPPED_ALLOCATOR_AVAILABLE
247 
248 // This is a mock allocator variant that provides a huge virtual allocation
249 // backed by a small real allocation that is repeatedly mapped. If you create an
250 // array on memory allocated by this allocator, you will observe that elements
251 // will alias each other as if their indices were modulo-divided by the real
252 // allocation length.
253 // The purpose is to allow stability-testing of huge (typed) arrays without
254 // actually consuming huge amounts of physical memory.
255 // This is currently only available on Linux because it relies on {mremap}.
256 class MultiMappedAllocator : public ArrayBufferAllocatorBase {
257  protected:
Allocate(size_t length)258   void* Allocate(size_t length) override {
259     if (length < kChunkSize) {
260       return ArrayBufferAllocatorBase::Allocate(length);
261     }
262     // We use mmap, which initializes pages to zero anyway.
263     return AllocateUninitialized(length);
264   }
265 
AllocateUninitialized(size_t length)266   void* AllocateUninitialized(size_t length) override {
267     if (length < kChunkSize) {
268       return ArrayBufferAllocatorBase::AllocateUninitialized(length);
269     }
270     size_t rounded_length = RoundUp(length, kChunkSize);
271     int prot = PROT_READ | PROT_WRITE;
272     // We have to specify MAP_SHARED to make {mremap} below do what we want.
273     int flags = MAP_SHARED | MAP_ANONYMOUS;
274     void* real_alloc = mmap(nullptr, kChunkSize, prot, flags, -1, 0);
275     if (reinterpret_cast<intptr_t>(real_alloc) == -1) {
276       // If we ran into some limit (physical or virtual memory, or number
277       // of mappings, etc), return {nullptr}, which callers can handle.
278       if (errno == ENOMEM) {
279         return nullptr;
280       }
281       // Other errors may be bugs which we want to learn about.
282       FATAL("mmap (real) failed with error %d: %s", errno, strerror(errno));
283     }
284     void* virtual_alloc =
285         mmap(nullptr, rounded_length, prot, flags | MAP_NORESERVE, -1, 0);
286     if (reinterpret_cast<intptr_t>(virtual_alloc) == -1) {
287       if (errno == ENOMEM) {
288         // Undo earlier, successful mappings.
289         munmap(real_alloc, kChunkSize);
290         return nullptr;
291       }
292       FATAL("mmap (virtual) failed with error %d: %s", errno, strerror(errno));
293     }
294     i::Address virtual_base = reinterpret_cast<i::Address>(virtual_alloc);
295     i::Address virtual_end = virtual_base + rounded_length;
296     for (i::Address to_map = virtual_base; to_map < virtual_end;
297          to_map += kChunkSize) {
298       // Specifying 0 as the "old size" causes the existing map entry to not
299       // get deleted, which is important so that we can remap it again in the
300       // next iteration of this loop.
301       void* result =
302           mremap(real_alloc, 0, kChunkSize, MREMAP_MAYMOVE | MREMAP_FIXED,
303                  reinterpret_cast<void*>(to_map));
304       if (reinterpret_cast<intptr_t>(result) == -1) {
305         if (errno == ENOMEM) {
306           // Undo earlier, successful mappings.
307           munmap(real_alloc, kChunkSize);
308           munmap(virtual_alloc, (to_map - virtual_base));
309           return nullptr;
310         }
311         FATAL("mremap failed with error %d: %s", errno, strerror(errno));
312       }
313     }
314     base::MutexGuard lock_guard(&regions_mutex_);
315     regions_[virtual_alloc] = real_alloc;
316     return virtual_alloc;
317   }
318 
Free(void * data,size_t length)319   void Free(void* data, size_t length) override {
320     if (length < kChunkSize) {
321       return ArrayBufferAllocatorBase::Free(data, length);
322     }
323     base::MutexGuard lock_guard(&regions_mutex_);
324     void* real_alloc = regions_[data];
325     munmap(real_alloc, kChunkSize);
326     size_t rounded_length = RoundUp(length, kChunkSize);
327     munmap(data, rounded_length);
328     regions_.erase(data);
329   }
330 
331  private:
332   // Aiming for a "Huge Page" (2M on Linux x64) to go easy on the TLB.
333   static constexpr size_t kChunkSize = 2 * 1024 * 1024;
334 
335   std::unordered_map<void*, void*> regions_;
336   base::Mutex regions_mutex_;
337 };
338 
339 #endif  // MULTI_MAPPED_ALLOCATOR_AVAILABLE
340 
341 v8::Platform* g_default_platform;
342 std::unique_ptr<v8::Platform> g_platform;
343 
TryGetValue(v8::Isolate * isolate,Local<Context> context,Local<v8::Object> object,const char * property)344 static MaybeLocal<Value> TryGetValue(v8::Isolate* isolate,
345                                      Local<Context> context,
346                                      Local<v8::Object> object,
347                                      const char* property) {
348   MaybeLocal<String> v8_str = String::NewFromUtf8(isolate, property);
349   if (v8_str.IsEmpty()) return {};
350   return object->Get(context, v8_str.ToLocalChecked());
351 }
352 
GetValue(v8::Isolate * isolate,Local<Context> context,Local<v8::Object> object,const char * property)353 static Local<Value> GetValue(v8::Isolate* isolate, Local<Context> context,
354                              Local<v8::Object> object, const char* property) {
355   return TryGetValue(isolate, context, object, property).ToLocalChecked();
356 }
357 
GetWorkerFromInternalField(Isolate * isolate,Local<Object> object)358 std::shared_ptr<Worker> GetWorkerFromInternalField(Isolate* isolate,
359                                                    Local<Object> object) {
360   if (object->InternalFieldCount() != 1) {
361     isolate->ThrowError("this is not a Worker");
362     return nullptr;
363   }
364 
365   i::Handle<i::Object> handle = Utils::OpenHandle(*object->GetInternalField(0));
366   if (handle->IsSmi()) {
367     isolate->ThrowError("Worker is defunct because main thread is terminating");
368     return nullptr;
369   }
370   auto managed = i::Handle<i::Managed<Worker>>::cast(handle);
371   return managed->get();
372 }
373 
GetThreadOptions(const char * name)374 base::Thread::Options GetThreadOptions(const char* name) {
375   // On some systems (OSX 10.6) the stack size default is 0.5Mb or less
376   // which is not enough to parse the big literal expressions used in tests.
377   // The stack size should be at least StackGuard::kLimitSize + some
378   // OS-specific padding for thread startup code.  2Mbytes seems to be enough.
379   return base::Thread::Options(name, 2 * kMB);
380 }
381 
382 }  // namespace
383 
384 namespace tracing {
385 
386 namespace {
387 
388 static constexpr char kIncludedCategoriesParam[] = "included_categories";
389 
390 class TraceConfigParser {
391  public:
FillTraceConfig(v8::Isolate * isolate,platform::tracing::TraceConfig * trace_config,const char * json_str)392   static void FillTraceConfig(v8::Isolate* isolate,
393                               platform::tracing::TraceConfig* trace_config,
394                               const char* json_str) {
395     HandleScope outer_scope(isolate);
396     Local<Context> context = Context::New(isolate);
397     Context::Scope context_scope(context);
398     HandleScope inner_scope(isolate);
399 
400     Local<String> source =
401         String::NewFromUtf8(isolate, json_str).ToLocalChecked();
402     Local<Value> result = JSON::Parse(context, source).ToLocalChecked();
403     Local<v8::Object> trace_config_object = result.As<v8::Object>();
404 
405     UpdateIncludedCategoriesList(isolate, context, trace_config_object,
406                                  trace_config);
407   }
408 
409  private:
UpdateIncludedCategoriesList(v8::Isolate * isolate,Local<Context> context,Local<v8::Object> object,platform::tracing::TraceConfig * trace_config)410   static int UpdateIncludedCategoriesList(
411       v8::Isolate* isolate, Local<Context> context, Local<v8::Object> object,
412       platform::tracing::TraceConfig* trace_config) {
413     Local<Value> value =
414         GetValue(isolate, context, object, kIncludedCategoriesParam);
415     if (value->IsArray()) {
416       Local<Array> v8_array = value.As<Array>();
417       for (int i = 0, length = v8_array->Length(); i < length; ++i) {
418         Local<Value> v = v8_array->Get(context, i)
419                              .ToLocalChecked()
420                              ->ToString(context)
421                              .ToLocalChecked();
422         String::Utf8Value str(isolate, v->ToString(context).ToLocalChecked());
423         trace_config->AddIncludedCategory(*str);
424       }
425       return v8_array->Length();
426     }
427     return 0;
428   }
429 };
430 
431 }  // namespace
432 
CreateTraceConfigFromJSON(v8::Isolate * isolate,const char * json_str)433 static platform::tracing::TraceConfig* CreateTraceConfigFromJSON(
434     v8::Isolate* isolate, const char* json_str) {
435   platform::tracing::TraceConfig* trace_config =
436       new platform::tracing::TraceConfig();
437   TraceConfigParser::FillTraceConfig(isolate, trace_config, json_str);
438   return trace_config;
439 }
440 
441 }  // namespace tracing
442 
443 class ExternalOwningOneByteStringResource
444     : public String::ExternalOneByteStringResource {
445  public:
446   ExternalOwningOneByteStringResource() = default;
ExternalOwningOneByteStringResource(std::unique_ptr<base::OS::MemoryMappedFile> file)447   ExternalOwningOneByteStringResource(
448       std::unique_ptr<base::OS::MemoryMappedFile> file)
449       : file_(std::move(file)) {}
data() const450   const char* data() const override {
451     return static_cast<char*>(file_->memory());
452   }
length() const453   size_t length() const override { return file_->size(); }
454 
455  private:
456   std::unique_ptr<base::OS::MemoryMappedFile> file_;
457 };
458 
459 // static variables:
460 CounterMap* Shell::counter_map_;
461 base::SharedMutex Shell::counter_mutex_;
462 base::OS::MemoryMappedFile* Shell::counters_file_ = nullptr;
463 CounterCollection Shell::local_counters_;
464 CounterCollection* Shell::counters_ = &local_counters_;
465 base::LazyMutex Shell::context_mutex_;
466 const base::TimeTicks Shell::kInitialTicks = base::TimeTicks::Now();
467 Global<Function> Shell::stringify_function_;
468 base::LazyMutex Shell::workers_mutex_;
469 bool Shell::allow_new_workers_ = true;
470 std::unordered_set<std::shared_ptr<Worker>> Shell::running_workers_;
471 std::atomic<bool> Shell::script_executed_{false};
472 std::atomic<bool> Shell::valid_fuzz_script_{false};
473 base::LazyMutex Shell::isolate_status_lock_;
474 std::map<v8::Isolate*, bool> Shell::isolate_status_;
475 std::map<v8::Isolate*, int> Shell::isolate_running_streaming_tasks_;
476 base::LazyMutex Shell::cached_code_mutex_;
477 std::map<std::string, std::unique_ptr<ScriptCompiler::CachedData>>
478     Shell::cached_code_map_;
479 std::atomic<int> Shell::unhandled_promise_rejections_{0};
480 
481 Global<Context> Shell::evaluation_context_;
482 ArrayBuffer::Allocator* Shell::array_buffer_allocator;
483 Isolate* Shell::shared_isolate = nullptr;
484 bool check_d8_flag_contradictions = true;
485 ShellOptions Shell::options;
486 base::OnceType Shell::quit_once_ = V8_ONCE_INIT;
487 
LookupCodeCache(Isolate * isolate,Local<Value> source)488 ScriptCompiler::CachedData* Shell::LookupCodeCache(Isolate* isolate,
489                                                    Local<Value> source) {
490   base::MutexGuard lock_guard(cached_code_mutex_.Pointer());
491   CHECK(source->IsString());
492   v8::String::Utf8Value key(isolate, source);
493   DCHECK(*key);
494   auto entry = cached_code_map_.find(*key);
495   if (entry != cached_code_map_.end() && entry->second) {
496     int length = entry->second->length;
497     uint8_t* cache = new uint8_t[length];
498     memcpy(cache, entry->second->data, length);
499     ScriptCompiler::CachedData* cached_data = new ScriptCompiler::CachedData(
500         cache, length, ScriptCompiler::CachedData::BufferOwned);
501     return cached_data;
502   }
503   return nullptr;
504 }
505 
StoreInCodeCache(Isolate * isolate,Local<Value> source,const ScriptCompiler::CachedData * cache_data)506 void Shell::StoreInCodeCache(Isolate* isolate, Local<Value> source,
507                              const ScriptCompiler::CachedData* cache_data) {
508   base::MutexGuard lock_guard(cached_code_mutex_.Pointer());
509   CHECK(source->IsString());
510   if (cache_data == nullptr) return;
511   v8::String::Utf8Value key(isolate, source);
512   DCHECK(*key);
513   int length = cache_data->length;
514   uint8_t* cache = new uint8_t[length];
515   memcpy(cache, cache_data->data, length);
516   cached_code_map_[*key] = std::unique_ptr<ScriptCompiler::CachedData>(
517       new ScriptCompiler::CachedData(cache, length,
518                                      ScriptCompiler::CachedData::BufferOwned));
519 }
520 
521 // Dummy external source stream which returns the whole source in one go.
522 // TODO(leszeks): Also test chunking the data.
523 class DummySourceStream : public v8::ScriptCompiler::ExternalSourceStream {
524  public:
DummySourceStream(Local<String> source)525   explicit DummySourceStream(Local<String> source) : done_(false) {
526     source_buffer_ = Utils::OpenHandle(*source)->ToCString(
527         i::ALLOW_NULLS, i::FAST_STRING_TRAVERSAL, &source_length_);
528   }
529 
GetMoreData(const uint8_t ** src)530   size_t GetMoreData(const uint8_t** src) override {
531     if (done_) {
532       return 0;
533     }
534     *src = reinterpret_cast<uint8_t*>(source_buffer_.release());
535     done_ = true;
536 
537     return source_length_;
538   }
539 
540  private:
541   int source_length_;
542   std::unique_ptr<char[]> source_buffer_;
543   bool done_;
544 };
545 
546 class StreamingCompileTask final : public v8::Task {
547  public:
StreamingCompileTask(Isolate * isolate,v8::ScriptCompiler::StreamedSource * streamed_source,v8::ScriptType type)548   StreamingCompileTask(Isolate* isolate,
549                        v8::ScriptCompiler::StreamedSource* streamed_source,
550                        v8::ScriptType type)
551       : isolate_(isolate),
552         script_streaming_task_(v8::ScriptCompiler::StartStreaming(
553             isolate, streamed_source, type)) {
554     Shell::NotifyStartStreamingTask(isolate_);
555   }
556 
Run()557   void Run() override {
558     script_streaming_task_->Run();
559     // Signal that the task has finished using the task runner to wake the
560     // message loop.
561     Shell::PostForegroundTask(isolate_, std::make_unique<FinishTask>(isolate_));
562   }
563 
564  private:
565   class FinishTask final : public v8::Task {
566    public:
FinishTask(Isolate * isolate)567     explicit FinishTask(Isolate* isolate) : isolate_(isolate) {}
Run()568     void Run() final { Shell::NotifyFinishStreamingTask(isolate_); }
569     Isolate* isolate_;
570   };
571 
572   Isolate* isolate_;
573   std::unique_ptr<v8::ScriptCompiler::ScriptStreamingTask>
574       script_streaming_task_;
575 };
576 
577 namespace {
578 template <class T>
CompileStreamed(Local<Context> context,ScriptCompiler::StreamedSource * v8_source,Local<String> full_source_string,const ScriptOrigin & origin)579 MaybeLocal<T> CompileStreamed(Local<Context> context,
580                               ScriptCompiler::StreamedSource* v8_source,
581                               Local<String> full_source_string,
582                               const ScriptOrigin& origin) {}
583 
584 template <>
CompileStreamed(Local<Context> context,ScriptCompiler::StreamedSource * v8_source,Local<String> full_source_string,const ScriptOrigin & origin)585 MaybeLocal<Script> CompileStreamed(Local<Context> context,
586                                    ScriptCompiler::StreamedSource* v8_source,
587                                    Local<String> full_source_string,
588                                    const ScriptOrigin& origin) {
589   return ScriptCompiler::Compile(context, v8_source, full_source_string,
590                                  origin);
591 }
592 
593 template <>
CompileStreamed(Local<Context> context,ScriptCompiler::StreamedSource * v8_source,Local<String> full_source_string,const ScriptOrigin & origin)594 MaybeLocal<Module> CompileStreamed(Local<Context> context,
595                                    ScriptCompiler::StreamedSource* v8_source,
596                                    Local<String> full_source_string,
597                                    const ScriptOrigin& origin) {
598   return ScriptCompiler::CompileModule(context, v8_source, full_source_string,
599                                        origin);
600 }
601 
602 template <class T>
Compile(Local<Context> context,ScriptCompiler::Source * source,ScriptCompiler::CompileOptions options)603 MaybeLocal<T> Compile(Local<Context> context, ScriptCompiler::Source* source,
604                       ScriptCompiler::CompileOptions options) {}
605 template <>
Compile(Local<Context> context,ScriptCompiler::Source * source,ScriptCompiler::CompileOptions options)606 MaybeLocal<Script> Compile(Local<Context> context,
607                            ScriptCompiler::Source* source,
608                            ScriptCompiler::CompileOptions options) {
609   return ScriptCompiler::Compile(context, source, options);
610 }
611 
612 template <>
Compile(Local<Context> context,ScriptCompiler::Source * source,ScriptCompiler::CompileOptions options)613 MaybeLocal<Module> Compile(Local<Context> context,
614                            ScriptCompiler::Source* source,
615                            ScriptCompiler::CompileOptions options) {
616   return ScriptCompiler::CompileModule(context->GetIsolate(), source, options);
617 }
618 
619 }  // namespace
620 
621 template <class T>
CompileString(Isolate * isolate,Local<Context> context,Local<String> source,const ScriptOrigin & origin)622 MaybeLocal<T> Shell::CompileString(Isolate* isolate, Local<Context> context,
623                                    Local<String> source,
624                                    const ScriptOrigin& origin) {
625   if (options.streaming_compile) {
626     v8::ScriptCompiler::StreamedSource streamed_source(
627         std::make_unique<DummySourceStream>(source),
628         v8::ScriptCompiler::StreamedSource::UTF8);
629     PostBlockingBackgroundTask(std::make_unique<StreamingCompileTask>(
630         isolate, &streamed_source,
631         std::is_same<T, Module>::value ? v8::ScriptType::kModule
632                                        : v8::ScriptType::kClassic));
633     // Pump the loop until the streaming task completes.
634     Shell::CompleteMessageLoop(isolate);
635     return CompileStreamed<T>(context, &streamed_source, source, origin);
636   }
637 
638   ScriptCompiler::CachedData* cached_code = nullptr;
639   if (options.compile_options == ScriptCompiler::kConsumeCodeCache) {
640     cached_code = LookupCodeCache(isolate, source);
641   }
642   ScriptCompiler::Source script_source(source, origin, cached_code);
643   MaybeLocal<T> result =
644       Compile<T>(context, &script_source,
645                  cached_code ? ScriptCompiler::kConsumeCodeCache
646                              : ScriptCompiler::kNoCompileOptions);
647   if (cached_code) CHECK(!cached_code->rejected);
648   return result;
649 }
650 
651 namespace {
652 // For testing.
653 const int kHostDefinedOptionsLength = 2;
654 const uint32_t kHostDefinedOptionsMagicConstant = 0xF1F2F3F0;
655 
CreateScriptOrigin(Isolate * isolate,Local<String> resource_name,v8::ScriptType type)656 ScriptOrigin CreateScriptOrigin(Isolate* isolate, Local<String> resource_name,
657                                 v8::ScriptType type) {
658   Local<PrimitiveArray> options =
659       PrimitiveArray::New(isolate, kHostDefinedOptionsLength);
660   options->Set(isolate, 0,
661                v8::Uint32::New(isolate, kHostDefinedOptionsMagicConstant));
662   options->Set(isolate, 1, resource_name);
663   return ScriptOrigin(isolate, resource_name, 0, 0, false, -1, Local<Value>(),
664                       false, false, type == v8::ScriptType::kModule, options);
665 }
666 
IsValidHostDefinedOptions(Local<Context> context,Local<Data> options,Local<Value> resource_name)667 bool IsValidHostDefinedOptions(Local<Context> context, Local<Data> options,
668                                Local<Value> resource_name) {
669   if (!options->IsFixedArray()) return false;
670   Local<FixedArray> array = options.As<FixedArray>();
671   if (array->Length() != kHostDefinedOptionsLength) return false;
672   uint32_t magic = 0;
673   if (!array->Get(context, 0).As<Value>()->Uint32Value(context).To(&magic)) {
674     return false;
675   }
676   if (magic != kHostDefinedOptionsMagicConstant) return false;
677   return array->Get(context, 1).As<String>()->StrictEquals(resource_name);
678 }
679 }  // namespace
680 
681 // Executes a string within the current v8 context.
ExecuteString(Isolate * isolate,Local<String> source,Local<String> name,PrintResult print_result,ReportExceptions report_exceptions,ProcessMessageQueue process_message_queue)682 bool Shell::ExecuteString(Isolate* isolate, Local<String> source,
683                           Local<String> name, PrintResult print_result,
684                           ReportExceptions report_exceptions,
685                           ProcessMessageQueue process_message_queue) {
686   i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
687   if (i::FLAG_parse_only) {
688     i::VMState<PARSER> state(i_isolate);
689     i::Handle<i::String> str = Utils::OpenHandle(*(source));
690 
691     // Set up ParseInfo.
692     i::UnoptimizedCompileState compile_state;
693     i::ReusableUnoptimizedCompileState reusable_state(i_isolate);
694 
695     i::UnoptimizedCompileFlags flags =
696         i::UnoptimizedCompileFlags::ForToplevelCompile(
697             i_isolate, true, i::construct_language_mode(i::FLAG_use_strict),
698             i::REPLMode::kNo, ScriptType::kClassic, i::FLAG_lazy);
699 
700     if (options.compile_options == v8::ScriptCompiler::kEagerCompile) {
701       flags.set_is_eager(true);
702     }
703 
704     i::ParseInfo parse_info(i_isolate, flags, &compile_state, &reusable_state);
705 
706     i::Handle<i::Script> script = parse_info.CreateScript(
707         i_isolate, str, i::kNullMaybeHandle, ScriptOriginOptions());
708     if (!i::parsing::ParseProgram(&parse_info, script, i_isolate,
709                                   i::parsing::ReportStatisticsMode::kYes)) {
710       parse_info.pending_error_handler()->PrepareErrors(
711           i_isolate, parse_info.ast_value_factory());
712       parse_info.pending_error_handler()->ReportErrors(i_isolate, script);
713 
714       fprintf(stderr, "Failed parsing\n");
715       return false;
716     }
717     return true;
718   }
719 
720   HandleScope handle_scope(isolate);
721   TryCatch try_catch(isolate);
722   try_catch.SetVerbose(report_exceptions == kReportExceptions);
723 
724   // Explicitly check for stack overflows. This method can be called
725   // recursively, and since we consume quite some stack space for the C++
726   // frames, the stack check in the called frame might be too late.
727   if (i::StackLimitCheck{i_isolate}.HasOverflowed()) {
728     i_isolate->StackOverflow();
729     i_isolate->OptionalRescheduleException(false);
730     return false;
731   }
732 
733   MaybeLocal<Value> maybe_result;
734   bool success = true;
735   {
736     PerIsolateData* data = PerIsolateData::Get(isolate);
737     Local<Context> realm =
738         Local<Context>::New(isolate, data->realms_[data->realm_current_]);
739     Context::Scope context_scope(realm);
740     Local<Context> context(isolate->GetCurrentContext());
741     ScriptOrigin origin =
742         CreateScriptOrigin(isolate, name, ScriptType::kClassic);
743 
744     for (int i = 1; i < options.repeat_compile; ++i) {
745       HandleScope handle_scope_for_compiling(isolate);
746       if (CompileString<Script>(isolate, context, source, origin).IsEmpty()) {
747         return false;
748       }
749     }
750     Local<Script> script;
751     if (!CompileString<Script>(isolate, context, source, origin)
752              .ToLocal(&script)) {
753       return false;
754     }
755 
756     if (options.code_cache_options ==
757         ShellOptions::CodeCacheOptions::kProduceCache) {
758       // Serialize and store it in memory for the next execution.
759       ScriptCompiler::CachedData* cached_data =
760           ScriptCompiler::CreateCodeCache(script->GetUnboundScript());
761       StoreInCodeCache(isolate, source, cached_data);
762       delete cached_data;
763     }
764     if (options.compile_only) return true;
765     if (options.compile_options == ScriptCompiler::kConsumeCodeCache) {
766       i::Handle<i::Script> i_script(
767           i::Script::cast(Utils::OpenHandle(*script)->shared().script()),
768           i_isolate);
769       // TODO(cbruni, chromium:1244145): remove once context-allocated.
770       i_script->set_host_defined_options(i::FixedArray::cast(
771           *Utils::OpenHandle(*(origin.GetHostDefinedOptions()))));
772     }
773     maybe_result = script->Run(realm);
774     if (options.code_cache_options ==
775         ShellOptions::CodeCacheOptions::kProduceCacheAfterExecute) {
776       // Serialize and store it in memory for the next execution.
777       ScriptCompiler::CachedData* cached_data =
778           ScriptCompiler::CreateCodeCache(script->GetUnboundScript());
779       StoreInCodeCache(isolate, source, cached_data);
780       delete cached_data;
781     }
782     if (process_message_queue) {
783       if (!EmptyMessageQueues(isolate)) success = false;
784       if (!HandleUnhandledPromiseRejections(isolate)) success = false;
785     }
786     data->realm_current_ = data->realm_switch_;
787 
788     if (options.web_snapshot_config) {
789       const char* web_snapshot_output_file_name = "web.snap";
790       if (options.web_snapshot_output) {
791         web_snapshot_output_file_name = options.web_snapshot_output;
792       }
793 
794       MaybeLocal<PrimitiveArray> maybe_exports =
795           ReadLines(isolate, options.web_snapshot_config);
796       Local<PrimitiveArray> exports;
797       if (!maybe_exports.ToLocal(&exports)) {
798         isolate->ThrowError("Web snapshots: unable to read config");
799         CHECK(try_catch.HasCaught());
800         ReportException(isolate, &try_catch);
801         return false;
802       }
803 
804       i::WebSnapshotSerializer serializer(isolate);
805       i::WebSnapshotData snapshot_data;
806       if (serializer.TakeSnapshot(context, exports, snapshot_data)) {
807         DCHECK_NOT_NULL(snapshot_data.buffer);
808         WriteChars(web_snapshot_output_file_name, snapshot_data.buffer,
809                    snapshot_data.buffer_size);
810       } else {
811         CHECK(try_catch.HasCaught());
812         return false;
813       }
814     } else if (options.web_snapshot_output) {
815       isolate->ThrowError(
816           "Web snapshots: --web-snapshot-config is needed when "
817           "--web-snapshot-output is passed");
818     }
819   }
820   Local<Value> result;
821   if (!maybe_result.ToLocal(&result)) {
822     DCHECK(try_catch.HasCaught());
823     return false;
824   }
825   // It's possible that a FinalizationRegistry cleanup task threw an error.
826   if (try_catch.HasCaught()) success = false;
827   if (print_result) {
828     if (options.test_shell) {
829       if (!result->IsUndefined()) {
830         // If all went well and the result wasn't undefined then print
831         // the returned value.
832         v8::String::Utf8Value str(isolate, result);
833         fwrite(*str, sizeof(**str), str.length(), stdout);
834         printf("\n");
835       }
836     } else {
837       v8::String::Utf8Value str(isolate, Stringify(isolate, result));
838       fwrite(*str, sizeof(**str), str.length(), stdout);
839       printf("\n");
840     }
841   }
842   return success;
843 }
844 
845 namespace {
846 
ToSTLString(Isolate * isolate,Local<String> v8_str)847 std::string ToSTLString(Isolate* isolate, Local<String> v8_str) {
848   String::Utf8Value utf8(isolate, v8_str);
849   // Should not be able to fail since the input is a String.
850   CHECK(*utf8);
851   return *utf8;
852 }
853 
IsAbsolutePath(const std::string & path)854 bool IsAbsolutePath(const std::string& path) {
855 #if defined(_WIN32) || defined(_WIN64)
856   // This is an incorrect approximation, but should
857   // work for all our test-running cases.
858   return path.find(':') != std::string::npos;
859 #else
860   return path[0] == '/';
861 #endif
862 }
863 
GetWorkingDirectory()864 std::string GetWorkingDirectory() {
865 #if defined(_WIN32) || defined(_WIN64)
866   char system_buffer[MAX_PATH];
867   // Unicode paths are unsupported, which is fine as long as
868   // the test directory doesn't include any such paths.
869   DWORD len = GetCurrentDirectoryA(MAX_PATH, system_buffer);
870   CHECK_GT(len, 0);
871   return system_buffer;
872 #else
873   char curdir[PATH_MAX];
874   CHECK_NOT_NULL(getcwd(curdir, PATH_MAX));
875   return curdir;
876 #endif
877 }
878 
879 // Returns the directory part of path, without the trailing '/'.
DirName(const std::string & path)880 std::string DirName(const std::string& path) {
881   DCHECK(IsAbsolutePath(path));
882   size_t last_slash = path.find_last_of('/');
883   DCHECK(last_slash != std::string::npos);
884   return path.substr(0, last_slash);
885 }
886 
887 // Resolves path to an absolute path if necessary, and does some
888 // normalization (eliding references to the current directory
889 // and replacing backslashes with slashes).
NormalizePath(const std::string & path,const std::string & dir_name)890 std::string NormalizePath(const std::string& path,
891                           const std::string& dir_name) {
892   std::string absolute_path;
893   if (IsAbsolutePath(path)) {
894     absolute_path = path;
895   } else {
896     absolute_path = dir_name + '/' + path;
897   }
898   std::replace(absolute_path.begin(), absolute_path.end(), '\\', '/');
899   std::vector<std::string> segments;
900   std::istringstream segment_stream(absolute_path);
901   std::string segment;
902   while (std::getline(segment_stream, segment, '/')) {
903     if (segment == "..") {
904       if (!segments.empty()) segments.pop_back();
905     } else if (segment != ".") {
906       segments.push_back(segment);
907     }
908   }
909   // Join path segments.
910   std::ostringstream os;
911   if (segments.size() > 1) {
912     std::copy(segments.begin(), segments.end() - 1,
913               std::ostream_iterator<std::string>(os, "/"));
914     os << *segments.rbegin();
915   } else {
916     os << "/";
917     if (!segments.empty()) os << segments[0];
918   }
919   return os.str();
920 }
921 
922 // Per-context Module data, allowing sharing of module maps
923 // across top-level module loads.
924 class ModuleEmbedderData {
925  private:
926   class ModuleGlobalHash {
927    public:
ModuleGlobalHash(Isolate * isolate)928     explicit ModuleGlobalHash(Isolate* isolate) : isolate_(isolate) {}
operator ()(const Global<Module> & module) const929     size_t operator()(const Global<Module>& module) const {
930       return module.Get(isolate_)->GetIdentityHash();
931     }
932 
933    private:
934     Isolate* isolate_;
935   };
936 
937  public:
ModuleEmbedderData(Isolate * isolate)938   explicit ModuleEmbedderData(Isolate* isolate)
939       : module_to_specifier_map(10, ModuleGlobalHash(isolate)),
940         json_module_to_parsed_json_map(10, ModuleGlobalHash(isolate)) {}
941 
ModuleTypeFromImportAssertions(Local<Context> context,Local<FixedArray> import_assertions,bool hasPositions)942   static ModuleType ModuleTypeFromImportAssertions(
943       Local<Context> context, Local<FixedArray> import_assertions,
944       bool hasPositions) {
945     Isolate* isolate = context->GetIsolate();
946     const int kV8AssertionEntrySize = hasPositions ? 3 : 2;
947     for (int i = 0; i < import_assertions->Length();
948          i += kV8AssertionEntrySize) {
949       Local<String> v8_assertion_key =
950           import_assertions->Get(context, i).As<v8::String>();
951       std::string assertion_key = ToSTLString(isolate, v8_assertion_key);
952 
953       if (assertion_key == "type") {
954         Local<String> v8_assertion_value =
955             import_assertions->Get(context, i + 1).As<String>();
956         std::string assertion_value = ToSTLString(isolate, v8_assertion_value);
957         if (assertion_value == "json") {
958           return ModuleType::kJSON;
959         } else {
960           // JSON is currently the only supported non-JS type
961           return ModuleType::kInvalid;
962         }
963       }
964     }
965 
966     // If no type is asserted, default to JS.
967     return ModuleType::kJavaScript;
968   }
969 
970   // Map from (normalized module specifier, module type) pair to Module.
971   std::map<std::pair<std::string, ModuleType>, Global<Module>> module_map;
972   // Map from Module to its URL as defined in the ScriptOrigin
973   std::unordered_map<Global<Module>, std::string, ModuleGlobalHash>
974       module_to_specifier_map;
975   // Map from JSON Module to its parsed content, for use in module
976   // JSONModuleEvaluationSteps
977   std::unordered_map<Global<Module>, Global<Value>, ModuleGlobalHash>
978       json_module_to_parsed_json_map;
979 };
980 
981 enum { kModuleEmbedderDataIndex, kInspectorClientIndex };
982 
InitializeModuleEmbedderData(Local<Context> context)983 void InitializeModuleEmbedderData(Local<Context> context) {
984   context->SetAlignedPointerInEmbedderData(
985       kModuleEmbedderDataIndex, new ModuleEmbedderData(context->GetIsolate()));
986 }
987 
GetModuleDataFromContext(Local<Context> context)988 ModuleEmbedderData* GetModuleDataFromContext(Local<Context> context) {
989   return static_cast<ModuleEmbedderData*>(
990       context->GetAlignedPointerFromEmbedderData(kModuleEmbedderDataIndex));
991 }
992 
DisposeModuleEmbedderData(Local<Context> context)993 void DisposeModuleEmbedderData(Local<Context> context) {
994   delete GetModuleDataFromContext(context);
995   context->SetAlignedPointerInEmbedderData(kModuleEmbedderDataIndex, nullptr);
996 }
997 
ResolveModuleCallback(Local<Context> context,Local<String> specifier,Local<FixedArray> import_assertions,Local<Module> referrer)998 MaybeLocal<Module> ResolveModuleCallback(Local<Context> context,
999                                          Local<String> specifier,
1000                                          Local<FixedArray> import_assertions,
1001                                          Local<Module> referrer) {
1002   Isolate* isolate = context->GetIsolate();
1003   ModuleEmbedderData* d = GetModuleDataFromContext(context);
1004   auto specifier_it =
1005       d->module_to_specifier_map.find(Global<Module>(isolate, referrer));
1006   CHECK(specifier_it != d->module_to_specifier_map.end());
1007   std::string absolute_path = NormalizePath(ToSTLString(isolate, specifier),
1008                                             DirName(specifier_it->second));
1009   ModuleType module_type = ModuleEmbedderData::ModuleTypeFromImportAssertions(
1010       context, import_assertions, true);
1011   auto module_it =
1012       d->module_map.find(std::make_pair(absolute_path, module_type));
1013   CHECK(module_it != d->module_map.end());
1014   return module_it->second.Get(isolate);
1015 }
1016 
1017 }  // anonymous namespace
1018 
FetchModuleTree(Local<Module> referrer,Local<Context> context,const std::string & file_name,ModuleType module_type)1019 MaybeLocal<Module> Shell::FetchModuleTree(Local<Module> referrer,
1020                                           Local<Context> context,
1021                                           const std::string& file_name,
1022                                           ModuleType module_type) {
1023   DCHECK(IsAbsolutePath(file_name));
1024   Isolate* isolate = context->GetIsolate();
1025   MaybeLocal<String> source_text = ReadFile(isolate, file_name.c_str(), false);
1026   if (source_text.IsEmpty() && options.fuzzy_module_file_extensions) {
1027     std::string fallback_file_name = file_name + ".js";
1028     source_text = ReadFile(isolate, fallback_file_name.c_str(), false);
1029     if (source_text.IsEmpty()) {
1030       fallback_file_name = file_name + ".mjs";
1031       source_text = ReadFile(isolate, fallback_file_name.c_str());
1032     }
1033   }
1034 
1035   ModuleEmbedderData* d = GetModuleDataFromContext(context);
1036   if (source_text.IsEmpty()) {
1037     std::string msg = "d8: Error reading  module from " + file_name;
1038     if (!referrer.IsEmpty()) {
1039       auto specifier_it =
1040           d->module_to_specifier_map.find(Global<Module>(isolate, referrer));
1041       CHECK(specifier_it != d->module_to_specifier_map.end());
1042       msg += "\n    imported by " + specifier_it->second;
1043     }
1044     isolate->ThrowError(
1045         v8::String::NewFromUtf8(isolate, msg.c_str()).ToLocalChecked());
1046     return MaybeLocal<Module>();
1047   }
1048 
1049   Local<String> resource_name =
1050       String::NewFromUtf8(isolate, file_name.c_str()).ToLocalChecked();
1051   ScriptOrigin origin =
1052       CreateScriptOrigin(isolate, resource_name, ScriptType::kModule);
1053 
1054   Local<Module> module;
1055   if (module_type == ModuleType::kJavaScript) {
1056     ScriptCompiler::Source source(source_text.ToLocalChecked(), origin);
1057     if (!CompileString<Module>(isolate, context, source_text.ToLocalChecked(),
1058                                origin)
1059              .ToLocal(&module)) {
1060       return MaybeLocal<Module>();
1061     }
1062   } else if (module_type == ModuleType::kJSON) {
1063     Local<Value> parsed_json;
1064     if (!v8::JSON::Parse(context, source_text.ToLocalChecked())
1065              .ToLocal(&parsed_json)) {
1066       return MaybeLocal<Module>();
1067     }
1068 
1069     std::vector<Local<String>> export_names{
1070         String::NewFromUtf8(isolate, "default").ToLocalChecked()};
1071 
1072     module = v8::Module::CreateSyntheticModule(
1073         isolate,
1074         String::NewFromUtf8(isolate, file_name.c_str()).ToLocalChecked(),
1075         export_names, Shell::JSONModuleEvaluationSteps);
1076 
1077     CHECK(d->json_module_to_parsed_json_map
1078               .insert(std::make_pair(Global<Module>(isolate, module),
1079                                      Global<Value>(isolate, parsed_json)))
1080               .second);
1081   } else {
1082     UNREACHABLE();
1083   }
1084 
1085   CHECK(d->module_map
1086             .insert(std::make_pair(std::make_pair(file_name, module_type),
1087                                    Global<Module>(isolate, module)))
1088             .second);
1089   CHECK(d->module_to_specifier_map
1090             .insert(std::make_pair(Global<Module>(isolate, module), file_name))
1091             .second);
1092 
1093   std::string dir_name = DirName(file_name);
1094 
1095   Local<FixedArray> module_requests = module->GetModuleRequests();
1096   for (int i = 0, length = module_requests->Length(); i < length; ++i) {
1097     Local<ModuleRequest> module_request =
1098         module_requests->Get(context, i).As<ModuleRequest>();
1099     Local<String> name = module_request->GetSpecifier();
1100     std::string absolute_path =
1101         NormalizePath(ToSTLString(isolate, name), dir_name);
1102     Local<FixedArray> import_assertions = module_request->GetImportAssertions();
1103     ModuleType request_module_type =
1104         ModuleEmbedderData::ModuleTypeFromImportAssertions(
1105             context, import_assertions, true);
1106 
1107     if (request_module_type == ModuleType::kInvalid) {
1108       isolate->ThrowError("Invalid module type was asserted");
1109       return MaybeLocal<Module>();
1110     }
1111 
1112     if (d->module_map.count(
1113             std::make_pair(absolute_path, request_module_type))) {
1114       continue;
1115     }
1116 
1117     if (FetchModuleTree(module, context, absolute_path, request_module_type)
1118             .IsEmpty()) {
1119       return MaybeLocal<Module>();
1120     }
1121   }
1122 
1123   return module;
1124 }
1125 
JSONModuleEvaluationSteps(Local<Context> context,Local<Module> module)1126 MaybeLocal<Value> Shell::JSONModuleEvaluationSteps(Local<Context> context,
1127                                                    Local<Module> module) {
1128   Isolate* isolate = context->GetIsolate();
1129 
1130   ModuleEmbedderData* d = GetModuleDataFromContext(context);
1131   auto json_value_it =
1132       d->json_module_to_parsed_json_map.find(Global<Module>(isolate, module));
1133   CHECK(json_value_it != d->json_module_to_parsed_json_map.end());
1134   Local<Value> json_value = json_value_it->second.Get(isolate);
1135 
1136   TryCatch try_catch(isolate);
1137   Maybe<bool> result = module->SetSyntheticModuleExport(
1138       isolate,
1139       String::NewFromUtf8Literal(isolate, "default",
1140                                  NewStringType::kInternalized),
1141       json_value);
1142 
1143   // Setting the default export should never fail.
1144   CHECK(!try_catch.HasCaught());
1145   CHECK(!result.IsNothing() && result.FromJust());
1146 
1147   Local<Promise::Resolver> resolver =
1148       Promise::Resolver::New(context).ToLocalChecked();
1149   resolver->Resolve(context, Undefined(isolate)).ToChecked();
1150   return resolver->GetPromise();
1151 }
1152 
1153 struct DynamicImportData {
DynamicImportDatav8::DynamicImportData1154   DynamicImportData(Isolate* isolate_, Local<String> referrer_,
1155                     Local<String> specifier_,
1156                     Local<FixedArray> import_assertions_,
1157                     Local<Promise::Resolver> resolver_)
1158       : isolate(isolate_) {
1159     referrer.Reset(isolate, referrer_);
1160     specifier.Reset(isolate, specifier_);
1161     import_assertions.Reset(isolate, import_assertions_);
1162     resolver.Reset(isolate, resolver_);
1163   }
1164 
1165   Isolate* isolate;
1166   Global<String> referrer;
1167   Global<String> specifier;
1168   Global<FixedArray> import_assertions;
1169   Global<Promise::Resolver> resolver;
1170 };
1171 
1172 namespace {
1173 struct ModuleResolutionData {
ModuleResolutionDatav8::__anon33b0cb620711::ModuleResolutionData1174   ModuleResolutionData(Isolate* isolate_, Local<Value> module_namespace_,
1175                        Local<Promise::Resolver> resolver_)
1176       : isolate(isolate_) {
1177     module_namespace.Reset(isolate, module_namespace_);
1178     resolver.Reset(isolate, resolver_);
1179   }
1180 
1181   Isolate* isolate;
1182   Global<Value> module_namespace;
1183   Global<Promise::Resolver> resolver;
1184 };
1185 
1186 }  // namespace
1187 
ModuleResolutionSuccessCallback(const FunctionCallbackInfo<Value> & info)1188 void Shell::ModuleResolutionSuccessCallback(
1189     const FunctionCallbackInfo<Value>& info) {
1190   std::unique_ptr<ModuleResolutionData> module_resolution_data(
1191       static_cast<ModuleResolutionData*>(
1192           info.Data().As<v8::External>()->Value()));
1193   Isolate* isolate(module_resolution_data->isolate);
1194   HandleScope handle_scope(isolate);
1195 
1196   Local<Promise::Resolver> resolver(
1197       module_resolution_data->resolver.Get(isolate));
1198   Local<Value> module_namespace(
1199       module_resolution_data->module_namespace.Get(isolate));
1200 
1201   PerIsolateData* data = PerIsolateData::Get(isolate);
1202   Local<Context> realm = data->realms_[data->realm_current_].Get(isolate);
1203   Context::Scope context_scope(realm);
1204 
1205   resolver->Resolve(realm, module_namespace).ToChecked();
1206 }
1207 
ModuleResolutionFailureCallback(const FunctionCallbackInfo<Value> & info)1208 void Shell::ModuleResolutionFailureCallback(
1209     const FunctionCallbackInfo<Value>& info) {
1210   std::unique_ptr<ModuleResolutionData> module_resolution_data(
1211       static_cast<ModuleResolutionData*>(
1212           info.Data().As<v8::External>()->Value()));
1213   Isolate* isolate(module_resolution_data->isolate);
1214   HandleScope handle_scope(isolate);
1215 
1216   Local<Promise::Resolver> resolver(
1217       module_resolution_data->resolver.Get(isolate));
1218 
1219   PerIsolateData* data = PerIsolateData::Get(isolate);
1220   Local<Context> realm = data->realms_[data->realm_current_].Get(isolate);
1221   Context::Scope context_scope(realm);
1222 
1223   DCHECK_EQ(info.Length(), 1);
1224   resolver->Reject(realm, info[0]).ToChecked();
1225 }
1226 
HostImportModuleDynamically(Local<Context> context,Local<Data> host_defined_options,Local<Value> resource_name,Local<String> specifier,Local<FixedArray> import_assertions)1227 MaybeLocal<Promise> Shell::HostImportModuleDynamically(
1228     Local<Context> context, Local<Data> host_defined_options,
1229     Local<Value> resource_name, Local<String> specifier,
1230     Local<FixedArray> import_assertions) {
1231   Isolate* isolate = context->GetIsolate();
1232 
1233   MaybeLocal<Promise::Resolver> maybe_resolver =
1234       Promise::Resolver::New(context);
1235   Local<Promise::Resolver> resolver;
1236   if (!maybe_resolver.ToLocal(&resolver)) return MaybeLocal<Promise>();
1237 
1238   if (!IsValidHostDefinedOptions(context, host_defined_options,
1239                                  resource_name)) {
1240     resolver
1241         ->Reject(context, v8::Exception::TypeError(String::NewFromUtf8Literal(
1242                               isolate, "Invalid host defined options")))
1243         .ToChecked();
1244   } else {
1245     DynamicImportData* data =
1246         new DynamicImportData(isolate, resource_name.As<String>(), specifier,
1247                               import_assertions, resolver);
1248     PerIsolateData::Get(isolate)->AddDynamicImportData(data);
1249     isolate->EnqueueMicrotask(Shell::DoHostImportModuleDynamically, data);
1250   }
1251   return resolver->GetPromise();
1252 }
1253 
HostInitializeImportMetaObject(Local<Context> context,Local<Module> module,Local<Object> meta)1254 void Shell::HostInitializeImportMetaObject(Local<Context> context,
1255                                            Local<Module> module,
1256                                            Local<Object> meta) {
1257   Isolate* isolate = context->GetIsolate();
1258   HandleScope handle_scope(isolate);
1259 
1260   ModuleEmbedderData* d = GetModuleDataFromContext(context);
1261   auto specifier_it =
1262       d->module_to_specifier_map.find(Global<Module>(isolate, module));
1263   CHECK(specifier_it != d->module_to_specifier_map.end());
1264 
1265   Local<String> url_key =
1266       String::NewFromUtf8Literal(isolate, "url", NewStringType::kInternalized);
1267   Local<String> url = String::NewFromUtf8(isolate, specifier_it->second.c_str())
1268                           .ToLocalChecked();
1269   meta->CreateDataProperty(context, url_key, url).ToChecked();
1270 }
1271 
HostCreateShadowRealmContext(Local<Context> initiator_context)1272 MaybeLocal<Context> Shell::HostCreateShadowRealmContext(
1273     Local<Context> initiator_context) {
1274   return v8::Context::New(initiator_context->GetIsolate());
1275 }
1276 
DoHostImportModuleDynamically(void * import_data)1277 void Shell::DoHostImportModuleDynamically(void* import_data) {
1278   DynamicImportData* import_data_ =
1279       static_cast<DynamicImportData*>(import_data);
1280 
1281   Isolate* isolate(import_data_->isolate);
1282   HandleScope handle_scope(isolate);
1283 
1284   Local<String> referrer(import_data_->referrer.Get(isolate));
1285   Local<String> specifier(import_data_->specifier.Get(isolate));
1286   Local<FixedArray> import_assertions(
1287       import_data_->import_assertions.Get(isolate));
1288   Local<Promise::Resolver> resolver(import_data_->resolver.Get(isolate));
1289 
1290   PerIsolateData* data = PerIsolateData::Get(isolate);
1291   PerIsolateData::Get(isolate)->DeleteDynamicImportData(import_data_);
1292 
1293   Local<Context> realm = data->realms_[data->realm_current_].Get(isolate);
1294   Context::Scope context_scope(realm);
1295 
1296   ModuleType module_type = ModuleEmbedderData::ModuleTypeFromImportAssertions(
1297       realm, import_assertions, false);
1298 
1299   TryCatch try_catch(isolate);
1300   try_catch.SetVerbose(true);
1301 
1302   if (module_type == ModuleType::kInvalid) {
1303     isolate->ThrowError("Invalid module type was asserted");
1304     CHECK(try_catch.HasCaught());
1305     resolver->Reject(realm, try_catch.Exception()).ToChecked();
1306     return;
1307   }
1308 
1309   std::string source_url = ToSTLString(isolate, referrer);
1310   std::string dir_name =
1311       DirName(NormalizePath(source_url, GetWorkingDirectory()));
1312   std::string file_name = ToSTLString(isolate, specifier);
1313   std::string absolute_path = NormalizePath(file_name, dir_name);
1314 
1315   ModuleEmbedderData* d = GetModuleDataFromContext(realm);
1316   Local<Module> root_module;
1317   auto module_it =
1318       d->module_map.find(std::make_pair(absolute_path, module_type));
1319   if (module_it != d->module_map.end()) {
1320     root_module = module_it->second.Get(isolate);
1321   } else if (!FetchModuleTree(Local<Module>(), realm, absolute_path,
1322                               module_type)
1323                   .ToLocal(&root_module)) {
1324     CHECK(try_catch.HasCaught());
1325     resolver->Reject(realm, try_catch.Exception()).ToChecked();
1326     return;
1327   }
1328 
1329   MaybeLocal<Value> maybe_result;
1330   if (root_module->InstantiateModule(realm, ResolveModuleCallback)
1331           .FromMaybe(false)) {
1332     maybe_result = root_module->Evaluate(realm);
1333     CHECK(!maybe_result.IsEmpty());
1334     EmptyMessageQueues(isolate);
1335   }
1336 
1337   Local<Value> result;
1338   if (!maybe_result.ToLocal(&result)) {
1339     DCHECK(try_catch.HasCaught());
1340     resolver->Reject(realm, try_catch.Exception()).ToChecked();
1341     return;
1342   }
1343 
1344   Local<Value> module_namespace = root_module->GetModuleNamespace();
1345   Local<Promise> result_promise(result.As<Promise>());
1346 
1347   // Setup callbacks, and then chain them to the result promise.
1348   // ModuleResolutionData will be deleted by the callbacks.
1349   auto module_resolution_data =
1350       new ModuleResolutionData(isolate, module_namespace, resolver);
1351   Local<v8::External> edata = External::New(isolate, module_resolution_data);
1352   Local<Function> callback_success;
1353   CHECK(Function::New(realm, ModuleResolutionSuccessCallback, edata)
1354             .ToLocal(&callback_success));
1355   Local<Function> callback_failure;
1356   CHECK(Function::New(realm, ModuleResolutionFailureCallback, edata)
1357             .ToLocal(&callback_failure));
1358   result_promise->Then(realm, callback_success, callback_failure)
1359       .ToLocalChecked();
1360 }
1361 
ExecuteModule(Isolate * isolate,const char * file_name)1362 bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) {
1363   HandleScope handle_scope(isolate);
1364 
1365   PerIsolateData* data = PerIsolateData::Get(isolate);
1366   Local<Context> realm = data->realms_[data->realm_current_].Get(isolate);
1367   Context::Scope context_scope(realm);
1368 
1369   std::string absolute_path = NormalizePath(file_name, GetWorkingDirectory());
1370 
1371   // Use a non-verbose TryCatch and report exceptions manually using
1372   // Shell::ReportException, because some errors (such as file errors) are
1373   // thrown without entering JS and thus do not trigger
1374   // isolate->ReportPendingMessages().
1375   TryCatch try_catch(isolate);
1376 
1377   ModuleEmbedderData* d = GetModuleDataFromContext(realm);
1378   Local<Module> root_module;
1379   auto module_it = d->module_map.find(
1380       std::make_pair(absolute_path, ModuleType::kJavaScript));
1381   if (module_it != d->module_map.end()) {
1382     root_module = module_it->second.Get(isolate);
1383   } else if (!FetchModuleTree(Local<Module>(), realm, absolute_path,
1384                               ModuleType::kJavaScript)
1385                   .ToLocal(&root_module)) {
1386     CHECK(try_catch.HasCaught());
1387     ReportException(isolate, &try_catch);
1388     return false;
1389   }
1390 
1391   MaybeLocal<Value> maybe_result;
1392   if (root_module->InstantiateModule(realm, ResolveModuleCallback)
1393           .FromMaybe(false)) {
1394     maybe_result = root_module->Evaluate(realm);
1395     CHECK(!maybe_result.IsEmpty());
1396     EmptyMessageQueues(isolate);
1397   }
1398   Local<Value> result;
1399   if (!maybe_result.ToLocal(&result)) {
1400     DCHECK(try_catch.HasCaught());
1401     ReportException(isolate, &try_catch);
1402     return false;
1403   }
1404 
1405   // Loop until module execution finishes
1406   Local<Promise> result_promise(result.As<Promise>());
1407   while (result_promise->State() == Promise::kPending) {
1408     Shell::CompleteMessageLoop(isolate);
1409   }
1410 
1411   if (result_promise->State() == Promise::kRejected) {
1412     // If the exception has been caught by the promise pipeline, we rethrow
1413     // here in order to ReportException.
1414     // TODO(cbruni): Clean this up after we create a new API for the case
1415     // where TLA is enabled.
1416     if (!try_catch.HasCaught()) {
1417       isolate->ThrowException(result_promise->Result());
1418     } else {
1419       DCHECK_EQ(try_catch.Exception(), result_promise->Result());
1420     }
1421     ReportException(isolate, &try_catch);
1422     return false;
1423   }
1424 
1425   DCHECK(!try_catch.HasCaught());
1426   return true;
1427 }
1428 
ExecuteWebSnapshot(Isolate * isolate,const char * file_name)1429 bool Shell::ExecuteWebSnapshot(Isolate* isolate, const char* file_name) {
1430   HandleScope handle_scope(isolate);
1431 
1432   PerIsolateData* data = PerIsolateData::Get(isolate);
1433   Local<Context> realm = data->realms_[data->realm_current_].Get(isolate);
1434   Context::Scope context_scope(realm);
1435   TryCatch try_catch(isolate);
1436   bool success = false;
1437 
1438   std::string absolute_path = NormalizePath(file_name, GetWorkingDirectory());
1439 
1440   int length = 0;
1441   std::unique_ptr<uint8_t[]> snapshot_data(
1442       reinterpret_cast<uint8_t*>(ReadChars(absolute_path.c_str(), &length)));
1443   if (length == 0) {
1444     isolate->ThrowError("Could not read the web snapshot file");
1445   } else {
1446     for (int r = 0; r < DeserializationRunCount(); ++r) {
1447       bool skip_exports = r > 0;
1448       i::WebSnapshotDeserializer deserializer(isolate, snapshot_data.get(),
1449                                               static_cast<size_t>(length));
1450       success = deserializer.Deserialize({}, skip_exports);
1451     }
1452   }
1453   if (!success) {
1454     CHECK(try_catch.HasCaught());
1455     ReportException(isolate, &try_catch);
1456   }
1457   return success;
1458 }
1459 
1460 // Treat every line as a JSON value and parse it.
LoadJSON(Isolate * isolate,const char * file_name)1461 bool Shell::LoadJSON(Isolate* isolate, const char* file_name) {
1462   HandleScope handle_scope(isolate);
1463   PerIsolateData* isolate_data = PerIsolateData::Get(isolate);
1464   Local<Context> realm =
1465       isolate_data->realms_[isolate_data->realm_current_].Get(isolate);
1466   Context::Scope context_scope(realm);
1467   TryCatch try_catch(isolate);
1468 
1469   std::string absolute_path = NormalizePath(file_name, GetWorkingDirectory());
1470   int length = 0;
1471   std::unique_ptr<char[]> data(ReadChars(absolute_path.c_str(), &length));
1472   if (length == 0) {
1473     printf("Error reading '%s'\n", file_name);
1474     base::OS::ExitProcess(1);
1475   }
1476   std::stringstream stream(data.get());
1477   std::string line;
1478   while (std::getline(stream, line, '\n')) {
1479     for (int r = 0; r < DeserializationRunCount(); ++r) {
1480       Local<String> source =
1481           String::NewFromUtf8(isolate, line.c_str()).ToLocalChecked();
1482       MaybeLocal<Value> maybe_value = JSON::Parse(realm, source);
1483 
1484       Local<Value> value;
1485       if (!maybe_value.ToLocal(&value)) {
1486         DCHECK(try_catch.HasCaught());
1487         ReportException(isolate, &try_catch);
1488         return false;
1489       }
1490     }
1491   }
1492   return true;
1493 }
1494 
PerIsolateData(Isolate * isolate)1495 PerIsolateData::PerIsolateData(Isolate* isolate)
1496     : isolate_(isolate), realms_(nullptr) {
1497   isolate->SetData(0, this);
1498   if (i::FLAG_expose_async_hooks) {
1499     async_hooks_wrapper_ = new AsyncHooks(isolate);
1500   }
1501   ignore_unhandled_promises_ = false;
1502   // TODO(v8:11525): Use methods on global Snapshot objects with
1503   // signature checks.
1504   HandleScope scope(isolate);
1505   Shell::CreateSnapshotTemplate(isolate);
1506 }
1507 
~PerIsolateData()1508 PerIsolateData::~PerIsolateData() {
1509   isolate_->SetData(0, nullptr);  // Not really needed, just to be sure...
1510   if (i::FLAG_expose_async_hooks) {
1511     delete async_hooks_wrapper_;  // This uses the isolate
1512   }
1513 #if defined(LEAK_SANITIZER)
1514   for (DynamicImportData* data : import_data_) {
1515     delete data;
1516   }
1517 #endif
1518 }
1519 
SetTimeout(Local<Function> callback,Local<Context> context)1520 void PerIsolateData::SetTimeout(Local<Function> callback,
1521                                 Local<Context> context) {
1522   set_timeout_callbacks_.emplace(isolate_, callback);
1523   set_timeout_contexts_.emplace(isolate_, context);
1524 }
1525 
GetTimeoutCallback()1526 MaybeLocal<Function> PerIsolateData::GetTimeoutCallback() {
1527   if (set_timeout_callbacks_.empty()) return MaybeLocal<Function>();
1528   Local<Function> result = set_timeout_callbacks_.front().Get(isolate_);
1529   set_timeout_callbacks_.pop();
1530   return result;
1531 }
1532 
GetTimeoutContext()1533 MaybeLocal<Context> PerIsolateData::GetTimeoutContext() {
1534   if (set_timeout_contexts_.empty()) return MaybeLocal<Context>();
1535   Local<Context> result = set_timeout_contexts_.front().Get(isolate_);
1536   set_timeout_contexts_.pop();
1537   return result;
1538 }
1539 
RemoveUnhandledPromise(Local<Promise> promise)1540 void PerIsolateData::RemoveUnhandledPromise(Local<Promise> promise) {
1541   if (ignore_unhandled_promises_) return;
1542   // Remove handled promises from the list
1543   DCHECK_EQ(promise->GetIsolate(), isolate_);
1544   for (auto it = unhandled_promises_.begin(); it != unhandled_promises_.end();
1545        ++it) {
1546     v8::Local<v8::Promise> unhandled_promise = std::get<0>(*it).Get(isolate_);
1547     if (unhandled_promise == promise) {
1548       unhandled_promises_.erase(it--);
1549     }
1550   }
1551 }
1552 
AddUnhandledPromise(Local<Promise> promise,Local<Message> message,Local<Value> exception)1553 void PerIsolateData::AddUnhandledPromise(Local<Promise> promise,
1554                                          Local<Message> message,
1555                                          Local<Value> exception) {
1556   if (ignore_unhandled_promises_) return;
1557   DCHECK_EQ(promise->GetIsolate(), isolate_);
1558   unhandled_promises_.emplace_back(v8::Global<v8::Promise>(isolate_, promise),
1559                                    v8::Global<v8::Message>(isolate_, message),
1560                                    v8::Global<v8::Value>(isolate_, exception));
1561 }
1562 
HandleUnhandledPromiseRejections()1563 int PerIsolateData::HandleUnhandledPromiseRejections() {
1564   // Avoid recursive calls to HandleUnhandledPromiseRejections.
1565   if (ignore_unhandled_promises_) return 0;
1566   ignore_unhandled_promises_ = true;
1567   v8::HandleScope scope(isolate_);
1568   // Ignore promises that get added during error reporting.
1569   size_t i = 0;
1570   for (; i < unhandled_promises_.size(); i++) {
1571     const auto& tuple = unhandled_promises_[i];
1572     Local<v8::Message> message = std::get<1>(tuple).Get(isolate_);
1573     Local<v8::Value> value = std::get<2>(tuple).Get(isolate_);
1574     Shell::ReportException(isolate_, message, value);
1575   }
1576   unhandled_promises_.clear();
1577   ignore_unhandled_promises_ = false;
1578   return static_cast<int>(i);
1579 }
1580 
AddDynamicImportData(DynamicImportData * data)1581 void PerIsolateData::AddDynamicImportData(DynamicImportData* data) {
1582 #if defined(LEAK_SANITIZER)
1583   import_data_.insert(data);
1584 #endif
1585 }
DeleteDynamicImportData(DynamicImportData * data)1586 void PerIsolateData::DeleteDynamicImportData(DynamicImportData* data) {
1587 #if defined(LEAK_SANITIZER)
1588   import_data_.erase(data);
1589 #endif
1590   delete data;
1591 }
1592 
GetTestApiObjectCtor() const1593 Local<FunctionTemplate> PerIsolateData::GetTestApiObjectCtor() const {
1594   return test_api_object_ctor_.Get(isolate_);
1595 }
1596 
SetTestApiObjectCtor(Local<FunctionTemplate> ctor)1597 void PerIsolateData::SetTestApiObjectCtor(Local<FunctionTemplate> ctor) {
1598   test_api_object_ctor_.Reset(isolate_, ctor);
1599 }
1600 
GetSnapshotObjectCtor() const1601 Local<FunctionTemplate> PerIsolateData::GetSnapshotObjectCtor() const {
1602   return snapshot_object_ctor_.Get(isolate_);
1603 }
1604 
SetSnapshotObjectCtor(Local<FunctionTemplate> ctor)1605 void PerIsolateData::SetSnapshotObjectCtor(Local<FunctionTemplate> ctor) {
1606   snapshot_object_ctor_.Reset(isolate_, ctor);
1607 }
1608 
RealmScope(PerIsolateData * data)1609 PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) {
1610   data_->realm_count_ = 1;
1611   data_->realm_current_ = 0;
1612   data_->realm_switch_ = 0;
1613   data_->realms_ = new Global<Context>[1];
1614   data_->realms_[0].Reset(data_->isolate_,
1615                           data_->isolate_->GetEnteredOrMicrotaskContext());
1616 }
1617 
~RealmScope()1618 PerIsolateData::RealmScope::~RealmScope() {
1619   // Drop realms to avoid keeping them alive. We don't dispose the
1620   // module embedder data for the first realm here, but instead do
1621   // it in RunShell or in RunMain, if not running in interactive mode
1622   for (int i = 1; i < data_->realm_count_; ++i) {
1623     Global<Context>& realm = data_->realms_[i];
1624     if (realm.IsEmpty()) continue;
1625     DisposeModuleEmbedderData(realm.Get(data_->isolate_));
1626   }
1627   data_->realm_count_ = 0;
1628   delete[] data_->realms_;
1629 }
1630 
ExplicitRealmScope(PerIsolateData * data,int index)1631 PerIsolateData::ExplicitRealmScope::ExplicitRealmScope(PerIsolateData* data,
1632                                                        int index)
1633     : data_(data), index_(index) {
1634   realm_ = Local<Context>::New(data->isolate_, data->realms_[index_]);
1635   realm_->Enter();
1636   previous_index_ = data->realm_current_;
1637   data->realm_current_ = data->realm_switch_ = index_;
1638 }
1639 
~ExplicitRealmScope()1640 PerIsolateData::ExplicitRealmScope::~ExplicitRealmScope() {
1641   realm_->Exit();
1642   data_->realm_current_ = data_->realm_switch_ = previous_index_;
1643 }
1644 
context() const1645 Local<Context> PerIsolateData::ExplicitRealmScope::context() const {
1646   return realm_;
1647 }
1648 
RealmFind(Local<Context> context)1649 int PerIsolateData::RealmFind(Local<Context> context) {
1650   for (int i = 0; i < realm_count_; ++i) {
1651     if (realms_[i] == context) return i;
1652   }
1653   return -1;
1654 }
1655 
RealmIndexOrThrow(const v8::FunctionCallbackInfo<v8::Value> & args,int arg_offset)1656 int PerIsolateData::RealmIndexOrThrow(
1657     const v8::FunctionCallbackInfo<v8::Value>& args, int arg_offset) {
1658   if (args.Length() < arg_offset || !args[arg_offset]->IsNumber()) {
1659     args.GetIsolate()->ThrowError("Invalid argument");
1660     return -1;
1661   }
1662   int index = args[arg_offset]
1663                   ->Int32Value(args.GetIsolate()->GetCurrentContext())
1664                   .FromMaybe(-1);
1665   if (index < 0 || index >= realm_count_ || realms_[index].IsEmpty()) {
1666     args.GetIsolate()->ThrowError("Invalid realm index");
1667     return -1;
1668   }
1669   return index;
1670 }
1671 
1672 // performance.now() returns a time stamp as double, measured in milliseconds.
1673 // When FLAG_verify_predictable mode is enabled it returns result of
1674 // v8::Platform::MonotonicallyIncreasingTime().
PerformanceNow(const v8::FunctionCallbackInfo<v8::Value> & args)1675 void Shell::PerformanceNow(const v8::FunctionCallbackInfo<v8::Value>& args) {
1676   if (i::FLAG_verify_predictable) {
1677     args.GetReturnValue().Set(g_platform->MonotonicallyIncreasingTime());
1678   } else {
1679     base::TimeDelta delta = base::TimeTicks::Now() - kInitialTicks;
1680     args.GetReturnValue().Set(delta.InMillisecondsF());
1681   }
1682 }
1683 
1684 // performance.measureMemory() implements JavaScript Memory API proposal.
1685 // See https://github.com/ulan/javascript-agent-memory/blob/master/explainer.md.
PerformanceMeasureMemory(const v8::FunctionCallbackInfo<v8::Value> & args)1686 void Shell::PerformanceMeasureMemory(
1687     const v8::FunctionCallbackInfo<v8::Value>& args) {
1688   v8::MeasureMemoryMode mode = v8::MeasureMemoryMode::kSummary;
1689   v8::Isolate* isolate = args.GetIsolate();
1690   Local<Context> context = isolate->GetCurrentContext();
1691   if (args.Length() >= 1 && args[0]->IsObject()) {
1692     Local<Object> object = args[0].As<Object>();
1693     Local<Value> value = TryGetValue(isolate, context, object, "detailed")
1694                              .FromMaybe(Local<Value>());
1695     if (value.IsEmpty()) {
1696       // Exception was thrown and scheduled, so return from the callback.
1697       return;
1698     }
1699     if (value->IsBoolean() && value->BooleanValue(isolate)) {
1700       mode = v8::MeasureMemoryMode::kDetailed;
1701     }
1702   }
1703   Local<v8::Promise::Resolver> promise_resolver =
1704       v8::Promise::Resolver::New(context).ToLocalChecked();
1705   args.GetIsolate()->MeasureMemory(
1706       v8::MeasureMemoryDelegate::Default(isolate, context, promise_resolver,
1707                                          mode),
1708       v8::MeasureMemoryExecution::kEager);
1709   args.GetReturnValue().Set(promise_resolver->GetPromise());
1710 }
1711 
1712 // Realm.current() returns the index of the currently active realm.
RealmCurrent(const v8::FunctionCallbackInfo<v8::Value> & args)1713 void Shell::RealmCurrent(const v8::FunctionCallbackInfo<v8::Value>& args) {
1714   Isolate* isolate = args.GetIsolate();
1715   PerIsolateData* data = PerIsolateData::Get(isolate);
1716   int index = data->RealmFind(isolate->GetEnteredOrMicrotaskContext());
1717   if (index == -1) return;
1718   args.GetReturnValue().Set(index);
1719 }
1720 
1721 // Realm.owner(o) returns the index of the realm that created o.
RealmOwner(const v8::FunctionCallbackInfo<v8::Value> & args)1722 void Shell::RealmOwner(const v8::FunctionCallbackInfo<v8::Value>& args) {
1723   Isolate* isolate = args.GetIsolate();
1724   PerIsolateData* data = PerIsolateData::Get(isolate);
1725   if (args.Length() < 1 || !args[0]->IsObject()) {
1726     args.GetIsolate()->ThrowError("Invalid argument");
1727     return;
1728   }
1729   Local<Object> object =
1730       args[0]->ToObject(isolate->GetCurrentContext()).ToLocalChecked();
1731   i::Handle<i::JSReceiver> i_object = Utils::OpenHandle(*object);
1732   if (i_object->IsJSGlobalProxy() &&
1733       i::Handle<i::JSGlobalProxy>::cast(i_object)->IsDetached()) {
1734     return;
1735   }
1736   Local<Context> creation_context;
1737   if (!object->GetCreationContext().ToLocal(&creation_context)) {
1738     args.GetIsolate()->ThrowError("object doesn't have creation context");
1739     return;
1740   }
1741   int index = data->RealmFind(creation_context);
1742   if (index == -1) return;
1743   args.GetReturnValue().Set(index);
1744 }
1745 
1746 // Realm.global(i) returns the global object of realm i.
1747 // (Note that properties of global objects cannot be read/written cross-realm.)
RealmGlobal(const v8::FunctionCallbackInfo<v8::Value> & args)1748 void Shell::RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) {
1749   PerIsolateData* data = PerIsolateData::Get(args.GetIsolate());
1750   int index = data->RealmIndexOrThrow(args, 0);
1751   if (index == -1) return;
1752   // TODO(chromium:324812): Ideally Context::Global should never return raw
1753   // global objects but return a global proxy. Currently it returns global
1754   // object when the global proxy is detached from the global object. The
1755   // following is a workaround till we fix Context::Global so we don't leak
1756   // global objects.
1757   Local<Object> global =
1758       Local<Context>::New(args.GetIsolate(), data->realms_[index])->Global();
1759   i::Handle<i::Object> i_global = Utils::OpenHandle(*global);
1760   if (i_global->IsJSGlobalObject()) {
1761     i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate());
1762     i::Handle<i::JSObject> i_global_proxy =
1763         handle(i::Handle<i::JSGlobalObject>::cast(i_global)->global_proxy(),
1764                i_isolate);
1765     global = Utils::ToLocal(i_global_proxy);
1766   }
1767   args.GetReturnValue().Set(global);
1768 }
1769 
CreateRealm(const v8::FunctionCallbackInfo<v8::Value> & args,int index,v8::MaybeLocal<Value> global_object)1770 MaybeLocal<Context> Shell::CreateRealm(
1771     const v8::FunctionCallbackInfo<v8::Value>& args, int index,
1772     v8::MaybeLocal<Value> global_object) {
1773   const char* kGlobalHandleLabel = "d8::realm";
1774   Isolate* isolate = args.GetIsolate();
1775   TryCatch try_catch(isolate);
1776   PerIsolateData* data = PerIsolateData::Get(isolate);
1777   if (index < 0) {
1778     Global<Context>* old_realms = data->realms_;
1779     index = data->realm_count_;
1780     data->realms_ = new Global<Context>[++data->realm_count_];
1781     for (int i = 0; i < index; ++i) {
1782       Global<Context>& realm = data->realms_[i];
1783       realm.Reset(isolate, old_realms[i]);
1784       if (!realm.IsEmpty()) {
1785         realm.AnnotateStrongRetainer(kGlobalHandleLabel);
1786       }
1787       old_realms[i].Reset();
1788     }
1789     delete[] old_realms;
1790   }
1791   Local<ObjectTemplate> global_template = CreateGlobalTemplate(isolate);
1792   Local<Context> context =
1793       Context::New(isolate, nullptr, global_template, global_object);
1794   if (context.IsEmpty()) return MaybeLocal<Context>();
1795   DCHECK(!try_catch.HasCaught());
1796   InitializeModuleEmbedderData(context);
1797   data->realms_[index].Reset(isolate, context);
1798   data->realms_[index].AnnotateStrongRetainer(kGlobalHandleLabel);
1799   args.GetReturnValue().Set(index);
1800   return context;
1801 }
1802 
DisposeRealm(const v8::FunctionCallbackInfo<v8::Value> & args,int index)1803 void Shell::DisposeRealm(const v8::FunctionCallbackInfo<v8::Value>& args,
1804                          int index) {
1805   Isolate* isolate = args.GetIsolate();
1806   PerIsolateData* data = PerIsolateData::Get(isolate);
1807   Local<Context> context = data->realms_[index].Get(isolate);
1808   DisposeModuleEmbedderData(context);
1809   data->realms_[index].Reset();
1810   // ContextDisposedNotification expects the disposed context to be entered.
1811   v8::Context::Scope scope(context);
1812   isolate->ContextDisposedNotification();
1813   isolate->IdleNotificationDeadline(g_platform->MonotonicallyIncreasingTime());
1814 }
1815 
1816 // Realm.create() creates a new realm with a distinct security token
1817 // and returns its index.
RealmCreate(const v8::FunctionCallbackInfo<v8::Value> & args)1818 void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) {
1819   CreateRealm(args, -1, v8::MaybeLocal<Value>());
1820 }
1821 
1822 // Realm.createAllowCrossRealmAccess() creates a new realm with the same
1823 // security token as the current realm.
RealmCreateAllowCrossRealmAccess(const v8::FunctionCallbackInfo<v8::Value> & args)1824 void Shell::RealmCreateAllowCrossRealmAccess(
1825     const v8::FunctionCallbackInfo<v8::Value>& args) {
1826   Local<Context> context;
1827   if (CreateRealm(args, -1, v8::MaybeLocal<Value>()).ToLocal(&context)) {
1828     context->SetSecurityToken(
1829         args.GetIsolate()->GetEnteredOrMicrotaskContext()->GetSecurityToken());
1830   }
1831 }
1832 
1833 // Realm.navigate(i) creates a new realm with a distinct security token
1834 // in place of realm i.
RealmNavigate(const v8::FunctionCallbackInfo<v8::Value> & args)1835 void Shell::RealmNavigate(const v8::FunctionCallbackInfo<v8::Value>& args) {
1836   Isolate* isolate = args.GetIsolate();
1837   PerIsolateData* data = PerIsolateData::Get(isolate);
1838   int index = data->RealmIndexOrThrow(args, 0);
1839   if (index == -1) return;
1840   if (index == 0 || index == data->realm_current_ ||
1841       index == data->realm_switch_) {
1842     args.GetIsolate()->ThrowError("Invalid realm index");
1843     return;
1844   }
1845 
1846   Local<Context> context = Local<Context>::New(isolate, data->realms_[index]);
1847   v8::MaybeLocal<Value> global_object = context->Global();
1848 
1849   // Context::Global doesn't return JSGlobalProxy if DetachGlobal is called in
1850   // advance.
1851   if (!global_object.IsEmpty()) {
1852     HandleScope scope(isolate);
1853     if (!Utils::OpenHandle(*global_object.ToLocalChecked())
1854              ->IsJSGlobalProxy()) {
1855       global_object = v8::MaybeLocal<Value>();
1856     }
1857   }
1858 
1859   DisposeRealm(args, index);
1860   CreateRealm(args, index, global_object);
1861 }
1862 
1863 // Realm.detachGlobal(i) detaches the global objects of realm i from realm i.
RealmDetachGlobal(const v8::FunctionCallbackInfo<v8::Value> & args)1864 void Shell::RealmDetachGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) {
1865   Isolate* isolate = args.GetIsolate();
1866   PerIsolateData* data = PerIsolateData::Get(isolate);
1867   int index = data->RealmIndexOrThrow(args, 0);
1868   if (index == -1) return;
1869   if (index == 0 || index == data->realm_current_ ||
1870       index == data->realm_switch_) {
1871     args.GetIsolate()->ThrowError("Invalid realm index");
1872     return;
1873   }
1874 
1875   HandleScope scope(isolate);
1876   Local<Context> realm = Local<Context>::New(isolate, data->realms_[index]);
1877   realm->DetachGlobal();
1878 }
1879 
1880 // Realm.dispose(i) disposes the reference to the realm i.
RealmDispose(const v8::FunctionCallbackInfo<v8::Value> & args)1881 void Shell::RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args) {
1882   Isolate* isolate = args.GetIsolate();
1883   PerIsolateData* data = PerIsolateData::Get(isolate);
1884   int index = data->RealmIndexOrThrow(args, 0);
1885   if (index == -1) return;
1886   if (index == 0 || index == data->realm_current_ ||
1887       index == data->realm_switch_) {
1888     args.GetIsolate()->ThrowError("Invalid realm index");
1889     return;
1890   }
1891   DisposeRealm(args, index);
1892 }
1893 
1894 // Realm.switch(i) switches to the realm i for consecutive interactive inputs.
RealmSwitch(const v8::FunctionCallbackInfo<v8::Value> & args)1895 void Shell::RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args) {
1896   Isolate* isolate = args.GetIsolate();
1897   PerIsolateData* data = PerIsolateData::Get(isolate);
1898   int index = data->RealmIndexOrThrow(args, 0);
1899   if (index == -1) return;
1900   data->realm_switch_ = index;
1901 }
1902 
1903 // Realm.eval(i, s) evaluates s in realm i and returns the result.
RealmEval(const v8::FunctionCallbackInfo<v8::Value> & args)1904 void Shell::RealmEval(const v8::FunctionCallbackInfo<v8::Value>& args) {
1905   Isolate* isolate = args.GetIsolate();
1906   PerIsolateData* data = PerIsolateData::Get(isolate);
1907   int index = data->RealmIndexOrThrow(args, 0);
1908   if (index == -1) return;
1909   if (args.Length() < 2) {
1910     isolate->ThrowError("Invalid argument");
1911     return;
1912   }
1913 
1914   Local<String> source;
1915   if (!ReadSource(args, 1, CodeType::kString).ToLocal(&source)) {
1916     isolate->ThrowError("Invalid argument");
1917     return;
1918   }
1919   ScriptOrigin origin =
1920       CreateScriptOrigin(isolate, String::NewFromUtf8Literal(isolate, "(d8)"),
1921                          ScriptType::kClassic);
1922 
1923   ScriptCompiler::Source script_source(source, origin);
1924   Local<UnboundScript> script;
1925   if (!ScriptCompiler::CompileUnboundScript(isolate, &script_source)
1926            .ToLocal(&script)) {
1927     return;
1928   }
1929   Local<Value> result;
1930   {
1931     PerIsolateData::ExplicitRealmScope realm_scope(data, index);
1932     if (!script->BindToCurrentContext()
1933              ->Run(realm_scope.context())
1934              .ToLocal(&result)) {
1935       return;
1936     }
1937   }
1938   args.GetReturnValue().Set(result);
1939 }
1940 
1941 // Realm.shared is an accessor for a single shared value across realms.
RealmSharedGet(Local<String> property,const PropertyCallbackInfo<Value> & info)1942 void Shell::RealmSharedGet(Local<String> property,
1943                            const PropertyCallbackInfo<Value>& info) {
1944   Isolate* isolate = info.GetIsolate();
1945   PerIsolateData* data = PerIsolateData::Get(isolate);
1946   if (data->realm_shared_.IsEmpty()) return;
1947   info.GetReturnValue().Set(data->realm_shared_);
1948 }
1949 
RealmSharedSet(Local<String> property,Local<Value> value,const PropertyCallbackInfo<void> & info)1950 void Shell::RealmSharedSet(Local<String> property, Local<Value> value,
1951                            const PropertyCallbackInfo<void>& info) {
1952   Isolate* isolate = info.GetIsolate();
1953   PerIsolateData* data = PerIsolateData::Get(isolate);
1954   data->realm_shared_.Reset(isolate, value);
1955 }
1956 
1957 // Realm.takeWebSnapshot(index, exports) takes a snapshot of the list of exports
1958 // in the realm with the specified index and returns the result.
RealmTakeWebSnapshot(const v8::FunctionCallbackInfo<v8::Value> & args)1959 void Shell::RealmTakeWebSnapshot(
1960     const v8::FunctionCallbackInfo<v8::Value>& args) {
1961   Isolate* isolate = args.GetIsolate();
1962   if (args.Length() < 2 || !args[1]->IsArray()) {
1963     isolate->ThrowError("Invalid argument");
1964     return;
1965   }
1966   PerIsolateData* data = PerIsolateData::Get(isolate);
1967   int index = data->RealmIndexOrThrow(args, 0);
1968   if (index == -1) return;
1969   // Create a Local<PrimitiveArray> from the exports array.
1970   Local<Context> current_context = isolate->GetCurrentContext();
1971   Local<Array> exports_array = args[1].As<Array>();
1972   int length = exports_array->Length();
1973   Local<PrimitiveArray> exports = PrimitiveArray::New(isolate, length);
1974   for (int i = 0; i < length; ++i) {
1975     Local<Value> value;
1976     Local<String> str;
1977     if (!exports_array->Get(current_context, i).ToLocal(&value) ||
1978         !value->ToString(current_context).ToLocal(&str) || str.IsEmpty()) {
1979       isolate->ThrowError("Invalid argument");
1980       return;
1981     }
1982     exports->Set(isolate, i, str);
1983   }
1984   // Take the snapshot in the specified Realm.
1985   auto snapshot_data_shared = std::make_shared<i::WebSnapshotData>();
1986   {
1987     TryCatch try_catch(isolate);
1988     try_catch.SetVerbose(true);
1989     PerIsolateData::ExplicitRealmScope realm_scope(data, index);
1990     i::WebSnapshotSerializer serializer(isolate);
1991     if (!serializer.TakeSnapshot(realm_scope.context(), exports,
1992                                  *snapshot_data_shared)) {
1993       CHECK(try_catch.HasCaught());
1994       args.GetReturnValue().Set(Undefined(isolate));
1995       return;
1996     }
1997   }
1998   // Create a snapshot object and store the WebSnapshotData as an embedder
1999   // field. TODO(v8:11525): Use methods on global Snapshot objects with
2000   // signature checks.
2001   i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2002   i::Handle<i::Object> snapshot_data_managed =
2003       i::Managed<i::WebSnapshotData>::FromSharedPtr(
2004           i_isolate, snapshot_data_shared->buffer_size, snapshot_data_shared);
2005   v8::Local<v8::Value> shapshot_data = Utils::ToLocal(snapshot_data_managed);
2006   Local<ObjectTemplate> snapshot_template =
2007       data->GetSnapshotObjectCtor()->InstanceTemplate();
2008   Local<Object> snapshot_instance =
2009       snapshot_template->NewInstance(isolate->GetCurrentContext())
2010           .ToLocalChecked();
2011   snapshot_instance->SetInternalField(0, shapshot_data);
2012   args.GetReturnValue().Set(snapshot_instance);
2013 }
2014 
2015 // Realm.useWebSnapshot(index, snapshot) deserializes the snapshot in the realm
2016 // with the specified index.
RealmUseWebSnapshot(const v8::FunctionCallbackInfo<v8::Value> & args)2017 void Shell::RealmUseWebSnapshot(
2018     const v8::FunctionCallbackInfo<v8::Value>& args) {
2019   Isolate* isolate = args.GetIsolate();
2020   if (args.Length() < 2 || !args[1]->IsObject()) {
2021     isolate->ThrowError("Invalid argument");
2022     return;
2023   }
2024   PerIsolateData* data = PerIsolateData::Get(isolate);
2025   int index = data->RealmIndexOrThrow(args, 0);
2026   if (index == -1) return;
2027   // Restore the snapshot data from the snapshot object.
2028   Local<Object> snapshot_instance = args[1].As<Object>();
2029   Local<FunctionTemplate> snapshot_template = data->GetSnapshotObjectCtor();
2030   if (!snapshot_template->HasInstance(snapshot_instance)) {
2031     isolate->ThrowError("Invalid argument");
2032     return;
2033   }
2034   v8::Local<v8::Value> snapshot_data = snapshot_instance->GetInternalField(0);
2035   i::Handle<i::Object> snapshot_data_handle = Utils::OpenHandle(*snapshot_data);
2036   auto snapshot_data_managed =
2037       i::Handle<i::Managed<i::WebSnapshotData>>::cast(snapshot_data_handle);
2038   std::shared_ptr<i::WebSnapshotData> snapshot_data_shared =
2039       snapshot_data_managed->get();
2040   // Deserialize the snapshot in the specified Realm.
2041   {
2042     PerIsolateData::ExplicitRealmScope realm_scope(data, index);
2043     i::WebSnapshotDeserializer deserializer(isolate,
2044                                             snapshot_data_shared->buffer,
2045                                             snapshot_data_shared->buffer_size);
2046     bool success = deserializer.Deserialize();
2047     args.GetReturnValue().Set(success);
2048   }
2049 }
2050 
LogGetAndStop(const v8::FunctionCallbackInfo<v8::Value> & args)2051 void Shell::LogGetAndStop(const v8::FunctionCallbackInfo<v8::Value>& args) {
2052   Isolate* isolate = args.GetIsolate();
2053   i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2054   HandleScope handle_scope(isolate);
2055 
2056   std::string file_name = i_isolate->logger()->file_name();
2057   if (!i::Log::IsLoggingToTemporaryFile(file_name)) {
2058     isolate->ThrowError("Only capturing from temporary files is supported.");
2059     return;
2060   }
2061   if (!i_isolate->logger()->is_logging()) {
2062     isolate->ThrowError("Logging not enabled.");
2063     return;
2064   }
2065 
2066   std::string raw_log;
2067   FILE* log_file = i_isolate->logger()->TearDownAndGetLogFile();
2068   if (!log_file) {
2069     isolate->ThrowError("Log file does not exist.");
2070     return;
2071   }
2072 
2073   bool exists = false;
2074   raw_log = i::ReadFile(log_file, &exists, true);
2075   base::Fclose(log_file);
2076 
2077   if (!exists) {
2078     isolate->ThrowError("Unable to read log file.");
2079     return;
2080   }
2081   Local<String> result =
2082       String::NewFromUtf8(isolate, raw_log.c_str(), NewStringType::kNormal,
2083                           static_cast<int>(raw_log.size()))
2084           .ToLocalChecked();
2085 
2086   args.GetReturnValue().Set(result);
2087 }
2088 
TestVerifySourcePositions(const v8::FunctionCallbackInfo<v8::Value> & args)2089 void Shell::TestVerifySourcePositions(
2090     const v8::FunctionCallbackInfo<v8::Value>& args) {
2091   Isolate* isolate = args.GetIsolate();
2092   // Check if the argument is a valid function.
2093   if (args.Length() != 1) {
2094     isolate->ThrowError("Expected function as single argument.");
2095     return;
2096   }
2097   auto arg_handle = Utils::OpenHandle(*args[0]);
2098   if (!arg_handle->IsHeapObject() ||
2099       !i::Handle<i::HeapObject>::cast(arg_handle)
2100            ->IsJSFunctionOrBoundFunctionOrWrappedFunction()) {
2101     isolate->ThrowError("Expected function as single argument.");
2102     return;
2103   }
2104 
2105   i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2106   HandleScope handle_scope(isolate);
2107 
2108   auto callable =
2109       i::Handle<i::JSFunctionOrBoundFunctionOrWrappedFunction>::cast(
2110           arg_handle);
2111   while (callable->IsJSBoundFunction()) {
2112     internal::DisallowGarbageCollection no_gc;
2113     auto bound_function = i::Handle<i::JSBoundFunction>::cast(callable);
2114     auto bound_target = bound_function->bound_target_function();
2115     if (!bound_target.IsJSFunctionOrBoundFunctionOrWrappedFunction()) {
2116       internal::AllowGarbageCollection allow_gc;
2117       isolate->ThrowError("Expected function as bound target.");
2118       return;
2119     }
2120     callable = handle(
2121         i::JSFunctionOrBoundFunctionOrWrappedFunction::cast(bound_target),
2122         i_isolate);
2123   }
2124 
2125   i::Handle<i::JSFunction> function = i::Handle<i::JSFunction>::cast(callable);
2126   if (!function->shared().HasBytecodeArray()) {
2127     isolate->ThrowError("Function has no BytecodeArray attached.");
2128     return;
2129   }
2130   i::Handle<i::BytecodeArray> bytecodes =
2131       handle(function->shared().GetBytecodeArray(i_isolate), i_isolate);
2132   i::interpreter::BytecodeArrayIterator bytecode_iterator(bytecodes);
2133   bool has_baseline = function->shared().HasBaselineCode();
2134   i::Handle<i::ByteArray> bytecode_offsets;
2135   std::unique_ptr<i::baseline::BytecodeOffsetIterator> offset_iterator;
2136   if (has_baseline) {
2137     bytecode_offsets =
2138         handle(i::ByteArray::cast(
2139                    function->shared().GetCode().bytecode_offset_table()),
2140                i_isolate);
2141     offset_iterator = std::make_unique<i::baseline::BytecodeOffsetIterator>(
2142         bytecode_offsets, bytecodes);
2143     // A freshly initiated BytecodeOffsetIterator points to the prologue.
2144     DCHECK_EQ(offset_iterator->current_pc_start_offset(), 0);
2145     DCHECK_EQ(offset_iterator->current_bytecode_offset(),
2146               i::kFunctionEntryBytecodeOffset);
2147     offset_iterator->Advance();
2148   }
2149   while (!bytecode_iterator.done()) {
2150     if (has_baseline) {
2151       if (offset_iterator->current_bytecode_offset() !=
2152           bytecode_iterator.current_offset()) {
2153         isolate->ThrowError("Baseline bytecode offset mismatch.");
2154         return;
2155       }
2156       // Check that we map every address to this bytecode correctly.
2157       // The start address is exclusive and the end address inclusive.
2158       for (i::Address pc = offset_iterator->current_pc_start_offset() + 1;
2159            pc <= offset_iterator->current_pc_end_offset(); ++pc) {
2160         i::baseline::BytecodeOffsetIterator pc_lookup(bytecode_offsets,
2161                                                       bytecodes);
2162         pc_lookup.AdvanceToPCOffset(pc);
2163         if (pc_lookup.current_bytecode_offset() !=
2164             bytecode_iterator.current_offset()) {
2165           isolate->ThrowError(
2166               "Baseline bytecode offset mismatch for PC lookup.");
2167           return;
2168         }
2169       }
2170     }
2171     bytecode_iterator.Advance();
2172     if (has_baseline && !bytecode_iterator.done()) {
2173       if (offset_iterator->done()) {
2174         isolate->ThrowError("Missing bytecode(s) in baseline offset mapping.");
2175         return;
2176       }
2177       offset_iterator->Advance();
2178     }
2179   }
2180   if (has_baseline && !offset_iterator->done()) {
2181     isolate->ThrowError("Excess offsets in baseline offset mapping.");
2182     return;
2183   }
2184 }
2185 
InstallConditionalFeatures(const v8::FunctionCallbackInfo<v8::Value> & args)2186 void Shell::InstallConditionalFeatures(
2187     const v8::FunctionCallbackInfo<v8::Value>& args) {
2188   Isolate* isolate = args.GetIsolate();
2189   isolate->InstallConditionalFeatures(isolate->GetCurrentContext());
2190 }
2191 
2192 // async_hooks.createHook() registers functions to be called for different
2193 // lifetime events of each async operation.
AsyncHooksCreateHook(const v8::FunctionCallbackInfo<v8::Value> & args)2194 void Shell::AsyncHooksCreateHook(
2195     const v8::FunctionCallbackInfo<v8::Value>& args) {
2196   Local<Object> wrap =
2197       PerIsolateData::Get(args.GetIsolate())->GetAsyncHooks()->CreateHook(args);
2198   args.GetReturnValue().Set(wrap);
2199 }
2200 
2201 // async_hooks.executionAsyncId() returns the asyncId of the current execution
2202 // context.
AsyncHooksExecutionAsyncId(const v8::FunctionCallbackInfo<v8::Value> & args)2203 void Shell::AsyncHooksExecutionAsyncId(
2204     const v8::FunctionCallbackInfo<v8::Value>& args) {
2205   Isolate* isolate = args.GetIsolate();
2206   HandleScope handle_scope(isolate);
2207   args.GetReturnValue().Set(v8::Number::New(
2208       isolate,
2209       PerIsolateData::Get(isolate)->GetAsyncHooks()->GetExecutionAsyncId()));
2210 }
2211 
AsyncHooksTriggerAsyncId(const v8::FunctionCallbackInfo<v8::Value> & args)2212 void Shell::AsyncHooksTriggerAsyncId(
2213     const v8::FunctionCallbackInfo<v8::Value>& args) {
2214   Isolate* isolate = args.GetIsolate();
2215   HandleScope handle_scope(isolate);
2216   args.GetReturnValue().Set(v8::Number::New(
2217       isolate,
2218       PerIsolateData::Get(isolate)->GetAsyncHooks()->GetTriggerAsyncId()));
2219 }
2220 
2221 static v8::debug::DebugDelegate dummy_delegate;
2222 
EnableDebugger(const v8::FunctionCallbackInfo<v8::Value> & args)2223 void Shell::EnableDebugger(const v8::FunctionCallbackInfo<v8::Value>& args) {
2224   v8::debug::SetDebugDelegate(args.GetIsolate(), &dummy_delegate);
2225 }
2226 
DisableDebugger(const v8::FunctionCallbackInfo<v8::Value> & args)2227 void Shell::DisableDebugger(const v8::FunctionCallbackInfo<v8::Value>& args) {
2228   v8::debug::SetDebugDelegate(args.GetIsolate(), nullptr);
2229 }
2230 
SetPromiseHooks(const v8::FunctionCallbackInfo<v8::Value> & args)2231 void Shell::SetPromiseHooks(const v8::FunctionCallbackInfo<v8::Value>& args) {
2232   Isolate* isolate = args.GetIsolate();
2233   if (i::FLAG_correctness_fuzzer_suppressions) {
2234     // Setting promise hoooks dynamically has unexpected timing side-effects
2235     // with certain promise optimizations. We might not get all callbacks for
2236     // previously scheduled Promises or optimized code-paths that skip Promise
2237     // creation.
2238     isolate->ThrowError(
2239         "d8.promise.setHooks is disabled with "
2240         "--correctness-fuzzer-suppressions");
2241     return;
2242   }
2243 #ifdef V8_ENABLE_JAVASCRIPT_PROMISE_HOOKS
2244   Local<Context> context = isolate->GetCurrentContext();
2245   HandleScope handle_scope(isolate);
2246 
2247   context->SetPromiseHooks(
2248     args[0]->IsFunction() ? args[0].As<Function>() : Local<Function>(),
2249     args[1]->IsFunction() ? args[1].As<Function>() : Local<Function>(),
2250     args[2]->IsFunction() ? args[2].As<Function>() : Local<Function>(),
2251     args[3]->IsFunction() ? args[3].As<Function>() : Local<Function>());
2252 
2253   args.GetReturnValue().Set(v8::Undefined(isolate));
2254 #else   // V8_ENABLE_JAVASCRIPT_PROMISE_HOOKS
2255   isolate->ThrowError(
2256       "d8.promise.setHooks is disabled due to missing build flag "
2257       "v8_enabale_javascript_in_promise_hooks");
2258 #endif  // V8_ENABLE_JAVASCRIPT_PROMISE_HOOKS
2259 }
2260 
WriteToFile(FILE * file,const v8::FunctionCallbackInfo<v8::Value> & args)2261 void WriteToFile(FILE* file, const v8::FunctionCallbackInfo<v8::Value>& args) {
2262   for (int i = 0; i < args.Length(); i++) {
2263     HandleScope handle_scope(args.GetIsolate());
2264     if (i != 0) {
2265       fprintf(file, " ");
2266     }
2267 
2268     // Explicitly catch potential exceptions in toString().
2269     v8::TryCatch try_catch(args.GetIsolate());
2270     Local<Value> arg = args[i];
2271     Local<String> str_obj;
2272 
2273     if (arg->IsSymbol()) {
2274       arg = arg.As<Symbol>()->Description(args.GetIsolate());
2275     }
2276     if (!arg->ToString(args.GetIsolate()->GetCurrentContext())
2277              .ToLocal(&str_obj)) {
2278       try_catch.ReThrow();
2279       return;
2280     }
2281 
2282     v8::String::Utf8Value str(args.GetIsolate(), str_obj);
2283     int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), file));
2284     if (n != str.length()) {
2285       printf("Error in fwrite\n");
2286       base::OS::ExitProcess(1);
2287     }
2288   }
2289 }
2290 
WriteAndFlush(FILE * file,const v8::FunctionCallbackInfo<v8::Value> & args)2291 void WriteAndFlush(FILE* file,
2292                    const v8::FunctionCallbackInfo<v8::Value>& args) {
2293   WriteToFile(file, args);
2294   fprintf(file, "\n");
2295   fflush(file);
2296 }
2297 
Print(const v8::FunctionCallbackInfo<v8::Value> & args)2298 void Shell::Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
2299   WriteAndFlush(stdout, args);
2300 }
2301 
PrintErr(const v8::FunctionCallbackInfo<v8::Value> & args)2302 void Shell::PrintErr(const v8::FunctionCallbackInfo<v8::Value>& args) {
2303   WriteAndFlush(stderr, args);
2304 }
2305 
WriteStdout(const v8::FunctionCallbackInfo<v8::Value> & args)2306 void Shell::WriteStdout(const v8::FunctionCallbackInfo<v8::Value>& args) {
2307   WriteToFile(stdout, args);
2308 }
2309 
ReadFile(const v8::FunctionCallbackInfo<v8::Value> & args)2310 void Shell::ReadFile(const v8::FunctionCallbackInfo<v8::Value>& args) {
2311   String::Utf8Value file_name(args.GetIsolate(), args[0]);
2312   if (*file_name == nullptr) {
2313     args.GetIsolate()->ThrowError("Error converting filename to string");
2314     return;
2315   }
2316   if (args.Length() == 2) {
2317     String::Utf8Value format(args.GetIsolate(), args[1]);
2318     if (*format && std::strcmp(*format, "binary") == 0) {
2319       ReadBuffer(args);
2320       return;
2321     }
2322   }
2323   Local<String> source;
2324   if (!ReadFile(args.GetIsolate(), *file_name).ToLocal(&source)) return;
2325   args.GetReturnValue().Set(source);
2326 }
2327 
ReadFromStdin(Isolate * isolate)2328 Local<String> Shell::ReadFromStdin(Isolate* isolate) {
2329   static const int kBufferSize = 256;
2330   char buffer[kBufferSize];
2331   Local<String> accumulator = String::NewFromUtf8Literal(isolate, "");
2332   int length;
2333   while (true) {
2334     // Continue reading if the line ends with an escape '\\' or the line has
2335     // not been fully read into the buffer yet (does not end with '\n').
2336     // If fgets gets an error, just give up.
2337     char* input = nullptr;
2338     input = fgets(buffer, kBufferSize, stdin);
2339     if (input == nullptr) return Local<String>();
2340     length = static_cast<int>(strlen(buffer));
2341     if (length == 0) {
2342       return accumulator;
2343     } else if (buffer[length - 1] != '\n') {
2344       accumulator = String::Concat(
2345           isolate, accumulator,
2346           String::NewFromUtf8(isolate, buffer, NewStringType::kNormal, length)
2347               .ToLocalChecked());
2348     } else if (length > 1 && buffer[length - 2] == '\\') {
2349       buffer[length - 2] = '\n';
2350       accumulator =
2351           String::Concat(isolate, accumulator,
2352                          String::NewFromUtf8(isolate, buffer,
2353                                              NewStringType::kNormal, length - 1)
2354                              .ToLocalChecked());
2355     } else {
2356       return String::Concat(
2357           isolate, accumulator,
2358           String::NewFromUtf8(isolate, buffer, NewStringType::kNormal,
2359                               length - 1)
2360               .ToLocalChecked());
2361     }
2362   }
2363 }
2364 
ExecuteFile(const v8::FunctionCallbackInfo<v8::Value> & args)2365 void Shell::ExecuteFile(const v8::FunctionCallbackInfo<v8::Value>& args) {
2366   Isolate* isolate = args.GetIsolate();
2367   for (int i = 0; i < args.Length(); i++) {
2368     HandleScope handle_scope(isolate);
2369     String::Utf8Value file_name(isolate, args[i]);
2370     if (*file_name == nullptr) {
2371       std::ostringstream oss;
2372       oss << "Cannot convert file[" << i << "] name to string.";
2373       isolate->ThrowError(
2374           String::NewFromUtf8(isolate, oss.str().c_str()).ToLocalChecked());
2375       return;
2376     }
2377     Local<String> source;
2378     if (!ReadFile(isolate, *file_name).ToLocal(&source)) return;
2379     if (!ExecuteString(
2380             args.GetIsolate(), source,
2381             String::NewFromUtf8(isolate, *file_name).ToLocalChecked(),
2382             kNoPrintResult,
2383             options.quiet_load ? kNoReportExceptions : kReportExceptions,
2384             kNoProcessMessageQueue)) {
2385       std::ostringstream oss;
2386       oss << "Error executing file: \"" << *file_name << '"';
2387       isolate->ThrowError(
2388           String::NewFromUtf8(isolate, oss.str().c_str()).ToLocalChecked());
2389       return;
2390     }
2391   }
2392 }
2393 
SetTimeout(const v8::FunctionCallbackInfo<v8::Value> & args)2394 void Shell::SetTimeout(const v8::FunctionCallbackInfo<v8::Value>& args) {
2395   Isolate* isolate = args.GetIsolate();
2396   args.GetReturnValue().Set(v8::Number::New(isolate, 0));
2397   if (args.Length() == 0 || !args[0]->IsFunction()) return;
2398   Local<Function> callback = args[0].As<Function>();
2399   Local<Context> context = isolate->GetCurrentContext();
2400   PerIsolateData::Get(isolate)->SetTimeout(callback, context);
2401 }
2402 
ReadCodeTypeAndArguments(const v8::FunctionCallbackInfo<v8::Value> & args,int index,CodeType * code_type,Local<Value> * arguments)2403 void Shell::ReadCodeTypeAndArguments(
2404     const v8::FunctionCallbackInfo<v8::Value>& args, int index,
2405     CodeType* code_type, Local<Value>* arguments) {
2406   Isolate* isolate = args.GetIsolate();
2407   if (args.Length() > index && args[index]->IsObject()) {
2408     Local<Object> object = args[index].As<Object>();
2409     Local<Context> context = isolate->GetCurrentContext();
2410     Local<Value> value;
2411     if (!TryGetValue(isolate, context, object, "type").ToLocal(&value)) {
2412       *code_type = CodeType::kNone;
2413       return;
2414     }
2415     if (!value->IsString()) {
2416       *code_type = CodeType::kInvalid;
2417       return;
2418     }
2419     Local<String> worker_type_string =
2420         value->ToString(context).ToLocalChecked();
2421     String::Utf8Value str(isolate, worker_type_string);
2422     if (strcmp("classic", *str) == 0) {
2423       *code_type = CodeType::kFileName;
2424     } else if (strcmp("string", *str) == 0) {
2425       *code_type = CodeType::kString;
2426     } else if (strcmp("function", *str) == 0) {
2427       *code_type = CodeType::kFunction;
2428     } else {
2429       *code_type = CodeType::kInvalid;
2430     }
2431     if (arguments != nullptr) {
2432       bool got_arguments =
2433           TryGetValue(isolate, context, object, "arguments").ToLocal(arguments);
2434       USE(got_arguments);
2435     }
2436   } else {
2437     *code_type = CodeType::kNone;
2438   }
2439 }
2440 
FunctionAndArgumentsToString(Local<Function> function,Local<Value> arguments,Local<String> * source,Isolate * isolate)2441 bool Shell::FunctionAndArgumentsToString(Local<Function> function,
2442                                          Local<Value> arguments,
2443                                          Local<String>* source,
2444                                          Isolate* isolate) {
2445   Local<Context> context = isolate->GetCurrentContext();
2446   MaybeLocal<String> maybe_function_string =
2447       function->FunctionProtoToString(context);
2448   Local<String> function_string;
2449   if (!maybe_function_string.ToLocal(&function_string)) {
2450     isolate->ThrowError("Failed to convert function to string");
2451     return false;
2452   }
2453   *source = String::NewFromUtf8Literal(isolate, "(");
2454   *source = String::Concat(isolate, *source, function_string);
2455   Local<String> middle = String::NewFromUtf8Literal(isolate, ")(");
2456   *source = String::Concat(isolate, *source, middle);
2457   if (!arguments.IsEmpty() && !arguments->IsUndefined()) {
2458     if (!arguments->IsArray()) {
2459       isolate->ThrowError("'arguments' must be an array");
2460       return false;
2461     }
2462     Local<String> comma = String::NewFromUtf8Literal(isolate, ",");
2463     Local<Array> array = arguments.As<Array>();
2464     for (uint32_t i = 0; i < array->Length(); ++i) {
2465       if (i > 0) {
2466         *source = String::Concat(isolate, *source, comma);
2467       }
2468       MaybeLocal<Value> maybe_argument = array->Get(context, i);
2469       Local<Value> argument;
2470       if (!maybe_argument.ToLocal(&argument)) {
2471         isolate->ThrowError("Failed to get argument");
2472         return false;
2473       }
2474       Local<String> argument_string;
2475       if (!JSON::Stringify(context, argument).ToLocal(&argument_string)) {
2476         isolate->ThrowError("Failed to convert argument to string");
2477         return false;
2478       }
2479       *source = String::Concat(isolate, *source, argument_string);
2480     }
2481   }
2482   Local<String> suffix = String::NewFromUtf8Literal(isolate, ")");
2483   *source = String::Concat(isolate, *source, suffix);
2484   return true;
2485 }
2486 
2487 // ReadSource() supports reading source code through `args[index]` as specified
2488 // by the `default_type` or an optional options bag provided in `args[index+1]`
2489 // (e.g. `options={type: 'code_type', arguments:[...]}`).
ReadSource(const v8::FunctionCallbackInfo<v8::Value> & args,int index,CodeType default_type)2490 MaybeLocal<String> Shell::ReadSource(
2491     const v8::FunctionCallbackInfo<v8::Value>& args, int index,
2492     CodeType default_type) {
2493   CodeType code_type;
2494   Local<Value> arguments;
2495   ReadCodeTypeAndArguments(args, index + 1, &code_type, &arguments);
2496 
2497   Isolate* isolate = args.GetIsolate();
2498   Local<String> source;
2499   if (code_type == CodeType::kNone) {
2500     code_type = default_type;
2501   }
2502   switch (code_type) {
2503     case CodeType::kFunction:
2504       if (!args[index]->IsFunction()) {
2505         return MaybeLocal<String>();
2506       }
2507       // Source: ( function_to_string )( params )
2508       if (!FunctionAndArgumentsToString(args[index].As<Function>(), arguments,
2509                                         &source, isolate)) {
2510         return MaybeLocal<String>();
2511       }
2512       break;
2513     case CodeType::kFileName: {
2514       if (!args[index]->IsString()) {
2515         return MaybeLocal<String>();
2516       }
2517       String::Utf8Value filename(isolate, args[index]);
2518       if (!Shell::ReadFile(isolate, *filename).ToLocal(&source)) {
2519         return MaybeLocal<String>();
2520       };
2521       break;
2522     }
2523     case CodeType::kString:
2524       if (!args[index]->IsString()) {
2525         return MaybeLocal<String>();
2526       }
2527       source = args[index].As<String>();
2528       break;
2529     case CodeType::kNone:
2530     case CodeType::kInvalid:
2531       return MaybeLocal<String>();
2532   }
2533   return source;
2534 }
2535 
WorkerNew(const v8::FunctionCallbackInfo<v8::Value> & args)2536 void Shell::WorkerNew(const v8::FunctionCallbackInfo<v8::Value>& args) {
2537   Isolate* isolate = args.GetIsolate();
2538   HandleScope handle_scope(isolate);
2539   if (args.Length() < 1 || (!args[0]->IsString() && !args[0]->IsFunction())) {
2540     isolate->ThrowError("1st argument must be a string or a function");
2541     return;
2542   }
2543 
2544   Local<String> source;
2545   if (!ReadSource(args, 0, CodeType::kFileName).ToLocal(&source)) {
2546     isolate->ThrowError("Invalid argument");
2547     return;
2548   }
2549 
2550   if (!args.IsConstructCall()) {
2551     isolate->ThrowError("Worker must be constructed with new");
2552     return;
2553   }
2554 
2555   // Initialize the embedder field to 0; if we return early without
2556   // creating a new Worker (because the main thread is terminating) we can
2557   // early-out from the instance calls.
2558   args.Holder()->SetInternalField(0, v8::Integer::New(isolate, 0));
2559 
2560   {
2561     // Don't allow workers to create more workers if the main thread
2562     // is waiting for existing running workers to terminate.
2563     base::MutexGuard lock_guard(workers_mutex_.Pointer());
2564     if (!allow_new_workers_) return;
2565 
2566     String::Utf8Value script(isolate, source);
2567     if (!*script) {
2568       isolate->ThrowError("Can't get worker script");
2569       return;
2570     }
2571 
2572     // The C++ worker object's lifetime is shared between the Managed<Worker>
2573     // object on the heap, which the JavaScript object points to, and an
2574     // internal std::shared_ptr in the worker thread itself.
2575     auto worker = std::make_shared<Worker>(*script);
2576     i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2577     const size_t kWorkerSizeEstimate = 4 * 1024 * 1024;  // stack + heap.
2578     i::Handle<i::Object> managed = i::Managed<Worker>::FromSharedPtr(
2579         i_isolate, kWorkerSizeEstimate, worker);
2580     args.Holder()->SetInternalField(0, Utils::ToLocal(managed));
2581     if (!Worker::StartWorkerThread(std::move(worker))) {
2582       isolate->ThrowError("Can't start thread");
2583       return;
2584     }
2585   }
2586 }
2587 
WorkerPostMessage(const v8::FunctionCallbackInfo<v8::Value> & args)2588 void Shell::WorkerPostMessage(const v8::FunctionCallbackInfo<v8::Value>& args) {
2589   Isolate* isolate = args.GetIsolate();
2590   HandleScope handle_scope(isolate);
2591 
2592   if (args.Length() < 1) {
2593     isolate->ThrowError("Invalid argument");
2594     return;
2595   }
2596 
2597   std::shared_ptr<Worker> worker =
2598       GetWorkerFromInternalField(isolate, args.Holder());
2599   if (!worker.get()) {
2600     return;
2601   }
2602 
2603   Local<Value> message = args[0];
2604   Local<Value> transfer =
2605       args.Length() >= 2 ? args[1] : Undefined(isolate).As<Value>();
2606   std::unique_ptr<SerializationData> data =
2607       Shell::SerializeValue(isolate, message, transfer);
2608   if (data) {
2609     worker->PostMessage(std::move(data));
2610   }
2611 }
2612 
WorkerGetMessage(const v8::FunctionCallbackInfo<v8::Value> & args)2613 void Shell::WorkerGetMessage(const v8::FunctionCallbackInfo<v8::Value>& args) {
2614   Isolate* isolate = args.GetIsolate();
2615   HandleScope handle_scope(isolate);
2616   std::shared_ptr<Worker> worker =
2617       GetWorkerFromInternalField(isolate, args.Holder());
2618   if (!worker.get()) {
2619     return;
2620   }
2621 
2622   std::unique_ptr<SerializationData> data = worker->GetMessage();
2623   if (data) {
2624     Local<Value> value;
2625     if (Shell::DeserializeValue(isolate, std::move(data)).ToLocal(&value)) {
2626       args.GetReturnValue().Set(value);
2627     }
2628   }
2629 }
2630 
WorkerTerminate(const v8::FunctionCallbackInfo<v8::Value> & args)2631 void Shell::WorkerTerminate(const v8::FunctionCallbackInfo<v8::Value>& args) {
2632   Isolate* isolate = args.GetIsolate();
2633   HandleScope handle_scope(isolate);
2634   std::shared_ptr<Worker> worker =
2635       GetWorkerFromInternalField(isolate, args.Holder());
2636   if (!worker.get()) return;
2637   worker->Terminate();
2638 }
2639 
WorkerTerminateAndWait(const v8::FunctionCallbackInfo<v8::Value> & args)2640 void Shell::WorkerTerminateAndWait(
2641     const v8::FunctionCallbackInfo<v8::Value>& args) {
2642   Isolate* isolate = args.GetIsolate();
2643   HandleScope handle_scope(isolate);
2644   std::shared_ptr<Worker> worker =
2645       GetWorkerFromInternalField(isolate, args.Holder());
2646   if (!worker.get()) {
2647     return;
2648   }
2649 
2650   worker->TerminateAndWaitForThread();
2651 }
2652 
QuitOnce(v8::FunctionCallbackInfo<v8::Value> * args)2653 void Shell::QuitOnce(v8::FunctionCallbackInfo<v8::Value>* args) {
2654   int exit_code = (*args)[0]
2655                       ->Int32Value(args->GetIsolate()->GetCurrentContext())
2656                       .FromMaybe(0);
2657   Isolate* isolate = args->GetIsolate();
2658   isolate->Exit();
2659 
2660   // As we exit the process anyway, we do not dispose the platform and other
2661   // global data and manually unlock to quell DCHECKs. Other isolates might
2662   // still be running, so disposing here can cause them to crash.
2663   i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
2664   if (i_isolate->thread_manager()->IsLockedByCurrentThread()) {
2665     i_isolate->thread_manager()->Unlock();
2666   }
2667 
2668   OnExit(isolate, false);
2669   base::OS::ExitProcess(exit_code);
2670 }
2671 
Quit(const v8::FunctionCallbackInfo<v8::Value> & args)2672 void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
2673   base::CallOnce(&quit_once_, &QuitOnce,
2674                  const_cast<v8::FunctionCallbackInfo<v8::Value>*>(&args));
2675 }
2676 
WaitUntilDone(const v8::FunctionCallbackInfo<v8::Value> & args)2677 void Shell::WaitUntilDone(const v8::FunctionCallbackInfo<v8::Value>& args) {
2678   SetWaitUntilDone(args.GetIsolate(), true);
2679 }
2680 
NotifyDone(const v8::FunctionCallbackInfo<v8::Value> & args)2681 void Shell::NotifyDone(const v8::FunctionCallbackInfo<v8::Value>& args) {
2682   SetWaitUntilDone(args.GetIsolate(), false);
2683 }
2684 
Version(const v8::FunctionCallbackInfo<v8::Value> & args)2685 void Shell::Version(const v8::FunctionCallbackInfo<v8::Value>& args) {
2686   args.GetReturnValue().Set(
2687       String::NewFromUtf8(args.GetIsolate(), V8::GetVersion())
2688           .ToLocalChecked());
2689 }
2690 
2691 #ifdef V8_FUZZILLI
2692 
2693 // We have to assume that the fuzzer will be able to call this function e.g. by
2694 // enumerating the properties of the global object and eval'ing them. As such
2695 // this function is implemented in a way that requires passing some magic value
2696 // as first argument (with the idea being that the fuzzer won't be able to
2697 // generate this value) which then also acts as a selector for the operation
2698 // to perform.
Fuzzilli(const v8::FunctionCallbackInfo<v8::Value> & args)2699 void Shell::Fuzzilli(const v8::FunctionCallbackInfo<v8::Value>& args) {
2700   HandleScope handle_scope(args.GetIsolate());
2701 
2702   String::Utf8Value operation(args.GetIsolate(), args[0]);
2703   if (*operation == nullptr) {
2704     return;
2705   }
2706 
2707   if (strcmp(*operation, "FUZZILLI_CRASH") == 0) {
2708     auto arg = args[1]
2709                    ->Int32Value(args.GetIsolate()->GetCurrentContext())
2710                    .FromMaybe(0);
2711     switch (arg) {
2712       case 0:
2713         IMMEDIATE_CRASH();
2714         break;
2715       case 1:
2716         CHECK(0);
2717         break;
2718       default:
2719         DCHECK(false);
2720         break;
2721     }
2722   } else if (strcmp(*operation, "FUZZILLI_PRINT") == 0) {
2723     static FILE* fzliout = fdopen(REPRL_DWFD, "w");
2724     if (!fzliout) {
2725       fprintf(
2726           stderr,
2727           "Fuzzer output channel not available, printing to stdout instead\n");
2728       fzliout = stdout;
2729     }
2730 
2731     String::Utf8Value string(args.GetIsolate(), args[1]);
2732     if (*string == nullptr) {
2733       return;
2734     }
2735     fprintf(fzliout, "%s\n", *string);
2736     fflush(fzliout);
2737   }
2738 }
2739 
2740 #endif  // V8_FUZZILLI
2741 
ReportException(Isolate * isolate,Local<v8::Message> message,Local<v8::Value> exception_obj)2742 void Shell::ReportException(Isolate* isolate, Local<v8::Message> message,
2743                             Local<v8::Value> exception_obj) {
2744   // Using ErrorPrototypeToString for converting the error to string will fail
2745   // if there's a pending exception.
2746   CHECK(!reinterpret_cast<i::Isolate*>(isolate)->has_pending_exception());
2747   HandleScope handle_scope(isolate);
2748   Local<Context> context = isolate->GetCurrentContext();
2749   bool enter_context = context.IsEmpty();
2750   if (enter_context) {
2751     context = Local<Context>::New(isolate, evaluation_context_);
2752     context->Enter();
2753   }
2754   // Converts a V8 value to a C string.
2755   auto ToCString = [](const v8::String::Utf8Value& value) {
2756     return *value ? *value : "<string conversion failed>";
2757   };
2758 
2759   v8::String::Utf8Value exception(isolate, exception_obj);
2760   const char* exception_string = ToCString(exception);
2761   if (message.IsEmpty()) {
2762     // V8 didn't provide any extra information about this error; just
2763     // print the exception.
2764     printf("%s\n", exception_string);
2765   } else if (message->GetScriptOrigin().Options().IsWasm()) {
2766     // Print wasm-function[(function index)]:(offset): (message).
2767     int function_index = message->GetWasmFunctionIndex();
2768     int offset = message->GetStartColumn(context).FromJust();
2769     printf("wasm-function[%d]:0x%x: %s\n", function_index, offset,
2770            exception_string);
2771   } else {
2772     // Print (filename):(line number): (message).
2773     v8::String::Utf8Value filename(isolate,
2774                                    message->GetScriptOrigin().ResourceName());
2775     const char* filename_string = ToCString(filename);
2776     int linenum = message->GetLineNumber(context).FromMaybe(-1);
2777     printf("%s:%i: %s\n", filename_string, linenum, exception_string);
2778     Local<String> sourceline;
2779     if (message->GetSourceLine(context).ToLocal(&sourceline)) {
2780       // Print line of source code.
2781       v8::String::Utf8Value sourcelinevalue(isolate, sourceline);
2782       const char* sourceline_string = ToCString(sourcelinevalue);
2783       printf("%s\n", sourceline_string);
2784       // Print wavy underline (GetUnderline is deprecated).
2785       int start = message->GetStartColumn(context).FromJust();
2786       for (int i = 0; i < start; i++) {
2787         printf(" ");
2788       }
2789       int end = message->GetEndColumn(context).FromJust();
2790       for (int i = start; i < end; i++) {
2791         printf("^");
2792       }
2793       printf("\n");
2794     }
2795   }
2796   Local<Value> stack_trace_string;
2797   if (v8::TryCatch::StackTrace(context, exception_obj)
2798           .ToLocal(&stack_trace_string) &&
2799       stack_trace_string->IsString()) {
2800     v8::String::Utf8Value stack_trace(isolate, stack_trace_string.As<String>());
2801     printf("%s\n", ToCString(stack_trace));
2802   }
2803   printf("\n");
2804   if (enter_context) context->Exit();
2805 }
2806 
ReportException(v8::Isolate * isolate,v8::TryCatch * try_catch)2807 void Shell::ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) {
2808   ReportException(isolate, try_catch->Message(), try_catch->Exception());
2809 }
2810 
Bind(const char * name,bool is_histogram)2811 void Counter::Bind(const char* name, bool is_histogram) {
2812   base::OS::StrNCpy(name_, kMaxNameSize, name, kMaxNameSize);
2813   // Explicitly null-terminate, in case {name} is longer than {kMaxNameSize}.
2814   name_[kMaxNameSize - 1] = '\0';
2815   is_histogram_ = is_histogram;
2816 }
2817 
AddSample(int sample)2818 void Counter::AddSample(int sample) {
2819   count_.fetch_add(1, std::memory_order_relaxed);
2820   sample_total_.fetch_add(sample, std::memory_order_relaxed);
2821 }
2822 
CounterCollection()2823 CounterCollection::CounterCollection() {
2824   magic_number_ = 0xDEADFACE;
2825   max_counters_ = kMaxCounters;
2826   max_name_size_ = Counter::kMaxNameSize;
2827   counters_in_use_ = 0;
2828 }
2829 
GetNextCounter()2830 Counter* CounterCollection::GetNextCounter() {
2831   if (counters_in_use_ == kMaxCounters) return nullptr;
2832   return &counters_[counters_in_use_++];
2833 }
2834 
MapCounters(v8::Isolate * isolate,const char * name)2835 void Shell::MapCounters(v8::Isolate* isolate, const char* name) {
2836   counters_file_ = base::OS::MemoryMappedFile::create(
2837       name, sizeof(CounterCollection), &local_counters_);
2838   void* memory =
2839       (counters_file_ == nullptr) ? nullptr : counters_file_->memory();
2840   if (memory == nullptr) {
2841     printf("Could not map counters file %s\n", name);
2842     base::OS::ExitProcess(1);
2843   }
2844   counters_ = static_cast<CounterCollection*>(memory);
2845   isolate->SetCounterFunction(LookupCounter);
2846   isolate->SetCreateHistogramFunction(CreateHistogram);
2847   isolate->SetAddHistogramSampleFunction(AddHistogramSample);
2848 }
2849 
GetCounter(const char * name,bool is_histogram)2850 Counter* Shell::GetCounter(const char* name, bool is_histogram) {
2851   Counter* counter = nullptr;
2852   {
2853     base::SharedMutexGuard<base::kShared> mutex_guard(&counter_mutex_);
2854     auto map_entry = counter_map_->find(name);
2855     if (map_entry != counter_map_->end()) {
2856       counter = map_entry->second;
2857     }
2858   }
2859 
2860   if (counter == nullptr) {
2861     base::SharedMutexGuard<base::kExclusive> mutex_guard(&counter_mutex_);
2862 
2863     counter = (*counter_map_)[name];
2864 
2865     if (counter == nullptr) {
2866       counter = counters_->GetNextCounter();
2867       if (counter == nullptr) {
2868         // Too many counters.
2869         return nullptr;
2870       }
2871       (*counter_map_)[name] = counter;
2872       counter->Bind(name, is_histogram);
2873     }
2874   }
2875 
2876   DCHECK_EQ(is_histogram, counter->is_histogram());
2877   return counter;
2878 }
2879 
LookupCounter(const char * name)2880 int* Shell::LookupCounter(const char* name) {
2881   Counter* counter = GetCounter(name, false);
2882   return counter ? counter->ptr() : nullptr;
2883 }
2884 
CreateHistogram(const char * name,int min,int max,size_t buckets)2885 void* Shell::CreateHistogram(const char* name, int min, int max,
2886                              size_t buckets) {
2887   return GetCounter(name, true);
2888 }
2889 
AddHistogramSample(void * histogram,int sample)2890 void Shell::AddHistogramSample(void* histogram, int sample) {
2891   Counter* counter = reinterpret_cast<Counter*>(histogram);
2892   counter->AddSample(sample);
2893 }
2894 
2895 // Turn a value into a human-readable string.
Stringify(Isolate * isolate,Local<Value> value)2896 Local<String> Shell::Stringify(Isolate* isolate, Local<Value> value) {
2897   v8::Local<v8::Context> context =
2898       v8::Local<v8::Context>::New(isolate, evaluation_context_);
2899   if (stringify_function_.IsEmpty()) {
2900     Local<String> source =
2901         String::NewFromUtf8(isolate, stringify_source_).ToLocalChecked();
2902     Local<String> name = String::NewFromUtf8Literal(isolate, "d8-stringify");
2903     ScriptOrigin origin(isolate, name);
2904     Local<Script> script =
2905         Script::Compile(context, source, &origin).ToLocalChecked();
2906     stringify_function_.Reset(
2907         isolate, script->Run(context).ToLocalChecked().As<Function>());
2908   }
2909   Local<Function> fun = Local<Function>::New(isolate, stringify_function_);
2910   Local<Value> argv[1] = {value};
2911   v8::TryCatch try_catch(isolate);
2912   MaybeLocal<Value> result = fun->Call(context, Undefined(isolate), 1, argv);
2913   if (result.IsEmpty()) return String::Empty(isolate);
2914   return result.ToLocalChecked().As<String>();
2915 }
2916 
NodeTypeCallback(const v8::FunctionCallbackInfo<v8::Value> & args)2917 void Shell::NodeTypeCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
2918   v8::Isolate* isolate = args.GetIsolate();
2919   args.GetReturnValue().Set(v8::Number::New(isolate, 1));
2920 }
2921 
CreateNodeTemplates(Isolate * isolate)2922 Local<FunctionTemplate> Shell::CreateNodeTemplates(Isolate* isolate) {
2923   Local<FunctionTemplate> node = FunctionTemplate::New(isolate);
2924   Local<ObjectTemplate> proto_template = node->PrototypeTemplate();
2925   Local<Signature> signature = v8::Signature::New(isolate, node);
2926   Local<FunctionTemplate> nodeType = FunctionTemplate::New(
2927       isolate, NodeTypeCallback, Local<Value>(), signature);
2928   nodeType->SetAcceptAnyReceiver(false);
2929   proto_template->SetAccessorProperty(
2930       String::NewFromUtf8Literal(isolate, "nodeType"), nodeType);
2931 
2932   Local<FunctionTemplate> element = FunctionTemplate::New(isolate);
2933   element->Inherit(node);
2934 
2935   Local<FunctionTemplate> html_element = FunctionTemplate::New(isolate);
2936   html_element->Inherit(element);
2937 
2938   Local<FunctionTemplate> div_element = FunctionTemplate::New(isolate);
2939   div_element->Inherit(html_element);
2940 
2941   return div_element;
2942 }
2943 
CreateGlobalTemplate(Isolate * isolate)2944 Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
2945   Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate);
2946   global_template->Set(Symbol::GetToStringTag(isolate),
2947                        String::NewFromUtf8Literal(isolate, "global"));
2948   global_template->Set(isolate, "version",
2949                        FunctionTemplate::New(isolate, Version));
2950 
2951   global_template->Set(isolate, "print", FunctionTemplate::New(isolate, Print));
2952   global_template->Set(isolate, "printErr",
2953                        FunctionTemplate::New(isolate, PrintErr));
2954   global_template->Set(isolate, "write",
2955                        FunctionTemplate::New(isolate, WriteStdout));
2956   global_template->Set(isolate, "read",
2957                        FunctionTemplate::New(isolate, ReadFile));
2958   global_template->Set(isolate, "readbuffer",
2959                        FunctionTemplate::New(isolate, ReadBuffer));
2960   global_template->Set(isolate, "readline",
2961                        FunctionTemplate::New(isolate, ReadLine));
2962   global_template->Set(isolate, "load",
2963                        FunctionTemplate::New(isolate, ExecuteFile));
2964   global_template->Set(isolate, "setTimeout",
2965                        FunctionTemplate::New(isolate, SetTimeout));
2966   // Some Emscripten-generated code tries to call 'quit', which in turn would
2967   // call C's exit(). This would lead to memory leaks, because there is no way
2968   // we can terminate cleanly then, so we need a way to hide 'quit'.
2969   if (!options.omit_quit) {
2970     global_template->Set(isolate, "quit", FunctionTemplate::New(isolate, Quit));
2971   }
2972   global_template->Set(isolate, "testRunner",
2973                        Shell::CreateTestRunnerTemplate(isolate));
2974   global_template->Set(isolate, "Realm", Shell::CreateRealmTemplate(isolate));
2975   global_template->Set(isolate, "performance",
2976                        Shell::CreatePerformanceTemplate(isolate));
2977   global_template->Set(isolate, "Worker", Shell::CreateWorkerTemplate(isolate));
2978 
2979   // Prevent fuzzers from creating side effects.
2980   if (!i::FLAG_fuzzing) {
2981     global_template->Set(isolate, "os", Shell::CreateOSTemplate(isolate));
2982   }
2983   global_template->Set(isolate, "d8", Shell::CreateD8Template(isolate));
2984 
2985 #ifdef V8_FUZZILLI
2986   global_template->Set(
2987       String::NewFromUtf8(isolate, "fuzzilli", NewStringType::kNormal)
2988           .ToLocalChecked(),
2989       FunctionTemplate::New(isolate, Fuzzilli), PropertyAttribute::DontEnum);
2990 #endif  // V8_FUZZILLI
2991 
2992   if (i::FLAG_expose_async_hooks) {
2993     global_template->Set(isolate, "async_hooks",
2994                          Shell::CreateAsyncHookTemplate(isolate));
2995   }
2996 
2997   return global_template;
2998 }
2999 
CreateOSTemplate(Isolate * isolate)3000 Local<ObjectTemplate> Shell::CreateOSTemplate(Isolate* isolate) {
3001   Local<ObjectTemplate> os_template = ObjectTemplate::New(isolate);
3002   AddOSMethods(isolate, os_template);
3003   os_template->Set(isolate, "name",
3004                    v8::String::NewFromUtf8Literal(isolate, V8_TARGET_OS_STRING),
3005                    PropertyAttribute::ReadOnly);
3006   os_template->Set(
3007       isolate, "d8Path",
3008       v8::String::NewFromUtf8(isolate, options.d8_path).ToLocalChecked(),
3009       PropertyAttribute::ReadOnly);
3010   return os_template;
3011 }
3012 
CreateWorkerTemplate(Isolate * isolate)3013 Local<FunctionTemplate> Shell::CreateWorkerTemplate(Isolate* isolate) {
3014   Local<FunctionTemplate> worker_fun_template =
3015       FunctionTemplate::New(isolate, WorkerNew);
3016   Local<Signature> worker_signature =
3017       Signature::New(isolate, worker_fun_template);
3018   worker_fun_template->SetClassName(
3019       String::NewFromUtf8Literal(isolate, "Worker"));
3020   worker_fun_template->ReadOnlyPrototype();
3021   worker_fun_template->PrototypeTemplate()->Set(
3022       isolate, "terminate",
3023       FunctionTemplate::New(isolate, WorkerTerminate, Local<Value>(),
3024                             worker_signature));
3025   worker_fun_template->PrototypeTemplate()->Set(
3026       isolate, "terminateAndWait",
3027       FunctionTemplate::New(isolate, WorkerTerminateAndWait, Local<Value>(),
3028                             worker_signature));
3029   worker_fun_template->PrototypeTemplate()->Set(
3030       isolate, "postMessage",
3031       FunctionTemplate::New(isolate, WorkerPostMessage, Local<Value>(),
3032                             worker_signature));
3033   worker_fun_template->PrototypeTemplate()->Set(
3034       isolate, "getMessage",
3035       FunctionTemplate::New(isolate, WorkerGetMessage, Local<Value>(),
3036                             worker_signature));
3037   worker_fun_template->InstanceTemplate()->SetInternalFieldCount(1);
3038   return worker_fun_template;
3039 }
3040 
CreateAsyncHookTemplate(Isolate * isolate)3041 Local<ObjectTemplate> Shell::CreateAsyncHookTemplate(Isolate* isolate) {
3042   Local<ObjectTemplate> async_hooks_templ = ObjectTemplate::New(isolate);
3043   async_hooks_templ->Set(isolate, "createHook",
3044                          FunctionTemplate::New(isolate, AsyncHooksCreateHook));
3045   async_hooks_templ->Set(
3046       isolate, "executionAsyncId",
3047       FunctionTemplate::New(isolate, AsyncHooksExecutionAsyncId));
3048   async_hooks_templ->Set(
3049       isolate, "triggerAsyncId",
3050       FunctionTemplate::New(isolate, AsyncHooksTriggerAsyncId));
3051   return async_hooks_templ;
3052 }
3053 
CreateTestRunnerTemplate(Isolate * isolate)3054 Local<ObjectTemplate> Shell::CreateTestRunnerTemplate(Isolate* isolate) {
3055   Local<ObjectTemplate> test_template = ObjectTemplate::New(isolate);
3056   test_template->Set(isolate, "notifyDone",
3057                      FunctionTemplate::New(isolate, NotifyDone));
3058   test_template->Set(isolate, "waitUntilDone",
3059                      FunctionTemplate::New(isolate, WaitUntilDone));
3060   // Reliable access to quit functionality. The "quit" method function
3061   // installed on the global object can be hidden with the --omit-quit flag
3062   // (e.g. on asan bots).
3063   test_template->Set(isolate, "quit", FunctionTemplate::New(isolate, Quit));
3064 
3065   return test_template;
3066 }
3067 
CreatePerformanceTemplate(Isolate * isolate)3068 Local<ObjectTemplate> Shell::CreatePerformanceTemplate(Isolate* isolate) {
3069   Local<ObjectTemplate> performance_template = ObjectTemplate::New(isolate);
3070   performance_template->Set(isolate, "now",
3071                             FunctionTemplate::New(isolate, PerformanceNow));
3072   performance_template->Set(
3073       isolate, "measureMemory",
3074       FunctionTemplate::New(isolate, PerformanceMeasureMemory));
3075   return performance_template;
3076 }
3077 
CreateRealmTemplate(Isolate * isolate)3078 Local<ObjectTemplate> Shell::CreateRealmTemplate(Isolate* isolate) {
3079   Local<ObjectTemplate> realm_template = ObjectTemplate::New(isolate);
3080   realm_template->Set(isolate, "current",
3081                       FunctionTemplate::New(isolate, RealmCurrent));
3082   realm_template->Set(isolate, "owner",
3083                       FunctionTemplate::New(isolate, RealmOwner));
3084   realm_template->Set(isolate, "global",
3085                       FunctionTemplate::New(isolate, RealmGlobal));
3086   realm_template->Set(isolate, "create",
3087                       FunctionTemplate::New(isolate, RealmCreate));
3088   realm_template->Set(
3089       isolate, "createAllowCrossRealmAccess",
3090       FunctionTemplate::New(isolate, RealmCreateAllowCrossRealmAccess));
3091   realm_template->Set(isolate, "navigate",
3092                       FunctionTemplate::New(isolate, RealmNavigate));
3093   realm_template->Set(isolate, "detachGlobal",
3094                       FunctionTemplate::New(isolate, RealmDetachGlobal));
3095   realm_template->Set(isolate, "dispose",
3096                       FunctionTemplate::New(isolate, RealmDispose));
3097   realm_template->Set(isolate, "switch",
3098                       FunctionTemplate::New(isolate, RealmSwitch));
3099   realm_template->Set(isolate, "eval",
3100                       FunctionTemplate::New(isolate, RealmEval));
3101   realm_template->SetAccessor(String::NewFromUtf8Literal(isolate, "shared"),
3102                               RealmSharedGet, RealmSharedSet);
3103   if (options.d8_web_snapshot_api) {
3104     realm_template->Set(isolate, "takeWebSnapshot",
3105                         FunctionTemplate::New(isolate, RealmTakeWebSnapshot));
3106     realm_template->Set(isolate, "useWebSnapshot",
3107                         FunctionTemplate::New(isolate, RealmUseWebSnapshot));
3108   }
3109   return realm_template;
3110 }
3111 
CreateSnapshotTemplate(Isolate * isolate)3112 Local<FunctionTemplate> Shell::CreateSnapshotTemplate(Isolate* isolate) {
3113   Local<FunctionTemplate> snapshot_template = FunctionTemplate::New(isolate);
3114   snapshot_template->InstanceTemplate()->SetInternalFieldCount(1);
3115   PerIsolateData::Get(isolate)->SetSnapshotObjectCtor(snapshot_template);
3116   return snapshot_template;
3117 }
CreateD8Template(Isolate * isolate)3118 Local<ObjectTemplate> Shell::CreateD8Template(Isolate* isolate) {
3119   Local<ObjectTemplate> d8_template = ObjectTemplate::New(isolate);
3120   {
3121     Local<ObjectTemplate> file_template = ObjectTemplate::New(isolate);
3122     file_template->Set(isolate, "read",
3123                        FunctionTemplate::New(isolate, Shell::ReadFile));
3124     file_template->Set(isolate, "execute",
3125                        FunctionTemplate::New(isolate, Shell::ExecuteFile));
3126     d8_template->Set(isolate, "file", file_template);
3127   }
3128   {
3129     Local<ObjectTemplate> log_template = ObjectTemplate::New(isolate);
3130     log_template->Set(isolate, "getAndStop",
3131                       FunctionTemplate::New(isolate, LogGetAndStop));
3132 
3133     d8_template->Set(isolate, "log", log_template);
3134   }
3135   {
3136     Local<ObjectTemplate> dom_template = ObjectTemplate::New(isolate);
3137     dom_template->Set(isolate, "Div", Shell::CreateNodeTemplates(isolate));
3138     d8_template->Set(isolate, "dom", dom_template);
3139   }
3140   {
3141     Local<ObjectTemplate> test_template = ObjectTemplate::New(isolate);
3142     // For different runs of correctness fuzzing the bytecode of a function
3143     // might get flushed, resulting in spurious errors.
3144     if (!i::FLAG_correctness_fuzzer_suppressions) {
3145       test_template->Set(
3146           isolate, "verifySourcePositions",
3147           FunctionTemplate::New(isolate, TestVerifySourcePositions));
3148     }
3149     // Correctness fuzzing will attempt to compare results of tests with and
3150     // without turbo_fast_api_calls, so we don't expose the fast_c_api
3151     // constructor when --correctness_fuzzer_suppressions is on.
3152     if (options.expose_fast_api && i::FLAG_turbo_fast_api_calls &&
3153         !i::FLAG_correctness_fuzzer_suppressions) {
3154       test_template->Set(isolate, "FastCAPI",
3155                          Shell::CreateTestFastCApiTemplate(isolate));
3156       test_template->Set(isolate, "LeafInterfaceType",
3157                          Shell::CreateLeafInterfaceTypeTemplate(isolate));
3158     }
3159     // Allows testing code paths that are triggered when Origin Trials are
3160     // added in the browser.
3161     test_template->Set(
3162         isolate, "installConditionalFeatures",
3163         FunctionTemplate::New(isolate, Shell::InstallConditionalFeatures));
3164 
3165     d8_template->Set(isolate, "test", test_template);
3166   }
3167   {
3168     Local<ObjectTemplate> promise_template = ObjectTemplate::New(isolate);
3169     promise_template->Set(
3170         isolate, "setHooks",
3171         FunctionTemplate::New(isolate, SetPromiseHooks, Local<Value>(),
3172                               Local<Signature>(), 4));
3173     d8_template->Set(isolate, "promise", promise_template);
3174   }
3175   {
3176     Local<ObjectTemplate> debugger_template = ObjectTemplate::New(isolate);
3177     debugger_template->Set(
3178         isolate, "enable",
3179         FunctionTemplate::New(isolate, EnableDebugger, Local<Value>(),
3180                               Local<Signature>(), 0));
3181     debugger_template->Set(
3182         isolate, "disable",
3183         FunctionTemplate::New(isolate, DisableDebugger, Local<Value>(),
3184                               Local<Signature>(), 0));
3185     d8_template->Set(isolate, "debugger", debugger_template);
3186   }
3187   return d8_template;
3188 }
3189 
PrintMessageCallback(Local<Message> message,Local<Value> error)3190 static void PrintMessageCallback(Local<Message> message, Local<Value> error) {
3191   switch (message->ErrorLevel()) {
3192     case v8::Isolate::kMessageWarning:
3193     case v8::Isolate::kMessageLog:
3194     case v8::Isolate::kMessageInfo:
3195     case v8::Isolate::kMessageDebug: {
3196       break;
3197     }
3198 
3199     case v8::Isolate::kMessageError: {
3200       Shell::ReportException(message->GetIsolate(), message, error);
3201       return;
3202     }
3203 
3204     default: {
3205       UNREACHABLE();
3206     }
3207   }
3208   // Converts a V8 value to a C string.
3209   auto ToCString = [](const v8::String::Utf8Value& value) {
3210     return *value ? *value : "<string conversion failed>";
3211   };
3212   Isolate* isolate = message->GetIsolate();
3213   v8::String::Utf8Value msg(isolate, message->Get());
3214   const char* msg_string = ToCString(msg);
3215   // Print (filename):(line number): (message).
3216   v8::String::Utf8Value filename(isolate,
3217                                  message->GetScriptOrigin().ResourceName());
3218   const char* filename_string = ToCString(filename);
3219   Maybe<int> maybeline = message->GetLineNumber(isolate->GetCurrentContext());
3220   int linenum = maybeline.IsJust() ? maybeline.FromJust() : -1;
3221   printf("%s:%i: %s\n", filename_string, linenum, msg_string);
3222 }
3223 
PromiseRejectCallback(v8::PromiseRejectMessage data)3224 void Shell::PromiseRejectCallback(v8::PromiseRejectMessage data) {
3225   if (options.ignore_unhandled_promises) return;
3226   if (data.GetEvent() == v8::kPromiseRejectAfterResolved ||
3227       data.GetEvent() == v8::kPromiseResolveAfterResolved) {
3228     // Ignore reject/resolve after resolved.
3229     return;
3230   }
3231   v8::Local<v8::Promise> promise = data.GetPromise();
3232   v8::Isolate* isolate = promise->GetIsolate();
3233   PerIsolateData* isolate_data = PerIsolateData::Get(isolate);
3234 
3235   if (data.GetEvent() == v8::kPromiseHandlerAddedAfterReject) {
3236     isolate_data->RemoveUnhandledPromise(promise);
3237     return;
3238   }
3239 
3240   i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
3241   bool capture_exceptions =
3242       i_isolate->get_capture_stack_trace_for_uncaught_exceptions();
3243   isolate->SetCaptureStackTraceForUncaughtExceptions(true);
3244   v8::Local<Value> exception = data.GetValue();
3245   v8::Local<Message> message;
3246   // Assume that all objects are stack-traces.
3247   if (exception->IsObject()) {
3248     message = v8::Exception::CreateMessage(isolate, exception);
3249   }
3250   if (!exception->IsNativeError() &&
3251       (message.IsEmpty() || message->GetStackTrace().IsEmpty())) {
3252     // If there is no real Error object, manually create a stack trace.
3253     exception = v8::Exception::Error(
3254         v8::String::NewFromUtf8Literal(isolate, "Unhandled Promise."));
3255     message = Exception::CreateMessage(isolate, exception);
3256   }
3257   isolate->SetCaptureStackTraceForUncaughtExceptions(capture_exceptions);
3258 
3259   isolate_data->AddUnhandledPromise(promise, message, exception);
3260 }
3261 
Initialize(Isolate * isolate,D8Console * console,bool isOnMainThread)3262 void Shell::Initialize(Isolate* isolate, D8Console* console,
3263                        bool isOnMainThread) {
3264   isolate->SetPromiseRejectCallback(PromiseRejectCallback);
3265   if (isOnMainThread) {
3266     // Set up counters
3267     if (i::FLAG_map_counters[0] != '\0') {
3268       MapCounters(isolate, i::FLAG_map_counters);
3269     }
3270     // Disable default message reporting.
3271     isolate->AddMessageListenerWithErrorLevel(
3272         PrintMessageCallback,
3273         v8::Isolate::kMessageError | v8::Isolate::kMessageWarning |
3274             v8::Isolate::kMessageInfo | v8::Isolate::kMessageDebug |
3275             v8::Isolate::kMessageLog);
3276   }
3277 
3278   isolate->SetHostImportModuleDynamicallyCallback(
3279       Shell::HostImportModuleDynamically);
3280   isolate->SetHostInitializeImportMetaObjectCallback(
3281       Shell::HostInitializeImportMetaObject);
3282   isolate->SetHostCreateShadowRealmContextCallback(
3283       Shell::HostCreateShadowRealmContext);
3284 
3285 #ifdef V8_FUZZILLI
3286   // Let the parent process (Fuzzilli) know we are ready.
3287   if (options.fuzzilli_enable_builtins_coverage) {
3288     cov_init_builtins_edges(static_cast<uint32_t>(
3289         i::BasicBlockProfiler::Get()
3290             ->GetCoverageBitmap(reinterpret_cast<i::Isolate*>(isolate))
3291             .size()));
3292   }
3293   char helo[] = "HELO";
3294   if (write(REPRL_CWFD, helo, 4) != 4 || read(REPRL_CRFD, helo, 4) != 4) {
3295     fuzzilli_reprl = false;
3296   }
3297 
3298   if (memcmp(helo, "HELO", 4) != 0) {
3299     fprintf(stderr, "Invalid response from parent\n");
3300     _exit(-1);
3301   }
3302 #endif  // V8_FUZZILLI
3303 
3304   debug::SetConsoleDelegate(isolate, console);
3305 }
3306 
WasmLoadSourceMapCallback(Isolate * isolate,const char * path)3307 Local<String> Shell::WasmLoadSourceMapCallback(Isolate* isolate,
3308                                                const char* path) {
3309   return Shell::ReadFile(isolate, path, false).ToLocalChecked();
3310 }
3311 
CreateEvaluationContext(Isolate * isolate)3312 Local<Context> Shell::CreateEvaluationContext(Isolate* isolate) {
3313   // This needs to be a critical section since this is not thread-safe
3314   base::MutexGuard lock_guard(context_mutex_.Pointer());
3315   // Initialize the global objects
3316   Local<ObjectTemplate> global_template = CreateGlobalTemplate(isolate);
3317   EscapableHandleScope handle_scope(isolate);
3318   Local<Context> context = Context::New(isolate, nullptr, global_template);
3319   DCHECK_IMPLIES(context.IsEmpty(), isolate->IsExecutionTerminating());
3320   if (context.IsEmpty()) return {};
3321   if (i::FLAG_perf_prof_annotate_wasm || i::FLAG_vtune_prof_annotate_wasm) {
3322     isolate->SetWasmLoadSourceMapCallback(Shell::WasmLoadSourceMapCallback);
3323   }
3324   InitializeModuleEmbedderData(context);
3325   Context::Scope scope(context);
3326   if (options.include_arguments) {
3327     const std::vector<const char*>& args = options.arguments;
3328     int size = static_cast<int>(args.size());
3329     Local<Array> array = Array::New(isolate, size);
3330     for (int i = 0; i < size; i++) {
3331       Local<String> arg =
3332           v8::String::NewFromUtf8(isolate, args[i]).ToLocalChecked();
3333       Local<Number> index = v8::Number::New(isolate, i);
3334       array->Set(context, index, arg).FromJust();
3335     }
3336     Local<String> name = String::NewFromUtf8Literal(
3337         isolate, "arguments", NewStringType::kInternalized);
3338     context->Global()->Set(context, name, array).FromJust();
3339   }
3340   {
3341     // setup console global.
3342     Local<String> name = String::NewFromUtf8Literal(
3343         isolate, "console", NewStringType::kInternalized);
3344     Local<Value> console =
3345         context->GetExtrasBindingObject()->Get(context, name).ToLocalChecked();
3346     context->Global()->Set(context, name, console).FromJust();
3347   }
3348 
3349   return handle_scope.Escape(context);
3350 }
3351 
WriteIgnitionDispatchCountersFile(v8::Isolate * isolate)3352 void Shell::WriteIgnitionDispatchCountersFile(v8::Isolate* isolate) {
3353   HandleScope handle_scope(isolate);
3354   Local<Context> context = Context::New(isolate);
3355   Context::Scope context_scope(context);
3356 
3357   i::Handle<i::JSObject> dispatch_counters =
3358       reinterpret_cast<i::Isolate*>(isolate)
3359           ->interpreter()
3360           ->GetDispatchCountersObject();
3361   std::ofstream dispatch_counters_stream(
3362       i::FLAG_trace_ignition_dispatches_output_file);
3363   dispatch_counters_stream << *String::Utf8Value(
3364       isolate, JSON::Stringify(context, Utils::ToLocal(dispatch_counters))
3365                    .ToLocalChecked());
3366 }
3367 
3368 namespace {
LineFromOffset(Local<debug::Script> script,int offset)3369 int LineFromOffset(Local<debug::Script> script, int offset) {
3370   debug::Location location = script->GetSourceLocation(offset);
3371   return location.GetLineNumber();
3372 }
3373 
WriteLcovDataForRange(std::vector<uint32_t> * lines,int start_line,int end_line,uint32_t count)3374 void WriteLcovDataForRange(std::vector<uint32_t>* lines, int start_line,
3375                            int end_line, uint32_t count) {
3376   // Ensure space in the array.
3377   lines->resize(std::max(static_cast<size_t>(end_line + 1), lines->size()), 0);
3378   // Boundary lines could be shared between two functions with different
3379   // invocation counts. Take the maximum.
3380   (*lines)[start_line] = std::max((*lines)[start_line], count);
3381   (*lines)[end_line] = std::max((*lines)[end_line], count);
3382   // Invocation counts for non-boundary lines are overwritten.
3383   for (int k = start_line + 1; k < end_line; k++) (*lines)[k] = count;
3384 }
3385 
WriteLcovDataForNamedRange(std::ostream & sink,std::vector<uint32_t> * lines,const std::string & name,int start_line,int end_line,uint32_t count)3386 void WriteLcovDataForNamedRange(std::ostream& sink,
3387                                 std::vector<uint32_t>* lines,
3388                                 const std::string& name, int start_line,
3389                                 int end_line, uint32_t count) {
3390   WriteLcovDataForRange(lines, start_line, end_line, count);
3391   sink << "FN:" << start_line + 1 << "," << name << std::endl;
3392   sink << "FNDA:" << count << "," << name << std::endl;
3393 }
3394 }  // namespace
3395 
3396 // Write coverage data in LCOV format. See man page for geninfo(1).
WriteLcovData(v8::Isolate * isolate,const char * file)3397 void Shell::WriteLcovData(v8::Isolate* isolate, const char* file) {
3398   if (!file) return;
3399   HandleScope handle_scope(isolate);
3400   debug::Coverage coverage = debug::Coverage::CollectPrecise(isolate);
3401   std::ofstream sink(file, std::ofstream::app);
3402   for (size_t i = 0; i < coverage.ScriptCount(); i++) {
3403     debug::Coverage::ScriptData script_data = coverage.GetScriptData(i);
3404     Local<debug::Script> script = script_data.GetScript();
3405     // Skip unnamed scripts.
3406     Local<String> name;
3407     if (!script->Name().ToLocal(&name)) continue;
3408     std::string file_name = ToSTLString(isolate, name);
3409     // Skip scripts not backed by a file.
3410     if (!std::ifstream(file_name).good()) continue;
3411     sink << "SF:";
3412     sink << NormalizePath(file_name, GetWorkingDirectory()) << std::endl;
3413     std::vector<uint32_t> lines;
3414     for (size_t j = 0; j < script_data.FunctionCount(); j++) {
3415       debug::Coverage::FunctionData function_data =
3416           script_data.GetFunctionData(j);
3417 
3418       // Write function stats.
3419       {
3420         debug::Location start =
3421             script->GetSourceLocation(function_data.StartOffset());
3422         debug::Location end =
3423             script->GetSourceLocation(function_data.EndOffset());
3424         int start_line = start.GetLineNumber();
3425         int end_line = end.GetLineNumber();
3426         uint32_t count = function_data.Count();
3427 
3428         Local<String> function_name;
3429         std::stringstream name_stream;
3430         if (function_data.Name().ToLocal(&function_name)) {
3431           name_stream << ToSTLString(isolate, function_name);
3432         } else {
3433           name_stream << "<" << start_line + 1 << "-";
3434           name_stream << start.GetColumnNumber() << ">";
3435         }
3436 
3437         WriteLcovDataForNamedRange(sink, &lines, name_stream.str(), start_line,
3438                                    end_line, count);
3439       }
3440 
3441       // Process inner blocks.
3442       for (size_t k = 0; k < function_data.BlockCount(); k++) {
3443         debug::Coverage::BlockData block_data = function_data.GetBlockData(k);
3444         int start_line = LineFromOffset(script, block_data.StartOffset());
3445         int end_line = LineFromOffset(script, block_data.EndOffset() - 1);
3446         uint32_t count = block_data.Count();
3447         WriteLcovDataForRange(&lines, start_line, end_line, count);
3448       }
3449     }
3450     // Write per-line coverage. LCOV uses 1-based line numbers.
3451     for (size_t j = 0; j < lines.size(); j++) {
3452       sink << "DA:" << (j + 1) << "," << lines[j] << std::endl;
3453     }
3454     sink << "end_of_record" << std::endl;
3455   }
3456 }
3457 
OnExit(v8::Isolate * isolate,bool dispose)3458 void Shell::OnExit(v8::Isolate* isolate, bool dispose) {
3459   isolate->Dispose();
3460   if (shared_isolate) {
3461     i::Isolate::Delete(reinterpret_cast<i::Isolate*>(shared_isolate));
3462   }
3463 
3464   // Simulate errors before disposing V8, as that resets flags (via
3465   // FlagList::ResetAllFlags()), but error simulation reads the random seed.
3466   if (options.simulate_errors && is_valid_fuzz_script()) {
3467     // Simulate several errors detectable by fuzzers behind a flag if the
3468     // minimum file size for fuzzing was executed.
3469     FuzzerMonitor::SimulateErrors();
3470   }
3471 
3472   if (dispose) {
3473     V8::Dispose();
3474     V8::DisposePlatform();
3475   }
3476 
3477   if (options.dump_counters || options.dump_counters_nvp) {
3478     base::SharedMutexGuard<base::kShared> mutex_guard(&counter_mutex_);
3479     std::vector<std::pair<std::string, Counter*>> counters(
3480         counter_map_->begin(), counter_map_->end());
3481     std::sort(counters.begin(), counters.end());
3482 
3483     if (options.dump_counters_nvp) {
3484       // Dump counters as name-value pairs.
3485       for (const auto& pair : counters) {
3486         std::string key = pair.first;
3487         Counter* counter = pair.second;
3488         if (counter->is_histogram()) {
3489           std::cout << "\"c:" << key << "\"=" << counter->count() << "\n";
3490           std::cout << "\"t:" << key << "\"=" << counter->sample_total()
3491                     << "\n";
3492         } else {
3493           std::cout << "\"" << key << "\"=" << counter->count() << "\n";
3494         }
3495       }
3496     } else {
3497       // Dump counters in formatted boxes.
3498       constexpr int kNameBoxSize = 64;
3499       constexpr int kValueBoxSize = 13;
3500       std::cout << "+" << std::string(kNameBoxSize, '-') << "+"
3501                 << std::string(kValueBoxSize, '-') << "+\n";
3502       std::cout << "| Name" << std::string(kNameBoxSize - 5, ' ') << "| Value"
3503                 << std::string(kValueBoxSize - 6, ' ') << "|\n";
3504       std::cout << "+" << std::string(kNameBoxSize, '-') << "+"
3505                 << std::string(kValueBoxSize, '-') << "+\n";
3506       for (const auto& pair : counters) {
3507         std::string key = pair.first;
3508         Counter* counter = pair.second;
3509         if (counter->is_histogram()) {
3510           std::cout << "| c:" << std::setw(kNameBoxSize - 4) << std::left << key
3511                     << " | " << std::setw(kValueBoxSize - 2) << std::right
3512                     << counter->count() << " |\n";
3513           std::cout << "| t:" << std::setw(kNameBoxSize - 4) << std::left << key
3514                     << " | " << std::setw(kValueBoxSize - 2) << std::right
3515                     << counter->sample_total() << " |\n";
3516         } else {
3517           std::cout << "| " << std::setw(kNameBoxSize - 2) << std::left << key
3518                     << " | " << std::setw(kValueBoxSize - 2) << std::right
3519                     << counter->count() << " |\n";
3520         }
3521       }
3522       std::cout << "+" << std::string(kNameBoxSize, '-') << "+"
3523                 << std::string(kValueBoxSize, '-') << "+\n";
3524     }
3525   }
3526 
3527   // Only delete the counters if we are done executing; after calling `quit`,
3528   // other isolates might still be running and accessing that memory. This is a
3529   // memory leak, which is OK in this case.
3530   if (dispose) {
3531     delete counters_file_;
3532     delete counter_map_;
3533   }
3534 }
3535 
Dummy(char * arg)3536 void Dummy(char* arg) {}
3537 
SimulateErrors()3538 V8_NOINLINE void FuzzerMonitor::SimulateErrors() {
3539   // Initialize a fresh RNG to not interfere with JS execution.
3540   std::unique_ptr<base::RandomNumberGenerator> rng;
3541   int64_t seed = internal::FLAG_random_seed;
3542   if (seed != 0) {
3543     rng = std::make_unique<base::RandomNumberGenerator>(seed);
3544   } else {
3545     rng = std::make_unique<base::RandomNumberGenerator>();
3546   }
3547 
3548   double p = rng->NextDouble();
3549   if (p < 0.1) {
3550     ControlFlowViolation();
3551   } else if (p < 0.2) {
3552     DCheck();
3553   } else if (p < 0.3) {
3554     Fatal();
3555   } else if (p < 0.4) {
3556     ObservableDifference();
3557   } else if (p < 0.5) {
3558     UndefinedBehavior();
3559   } else if (p < 0.6) {
3560     UseAfterFree();
3561   } else if (p < 0.7) {
3562     UseOfUninitializedValue();
3563   }
3564 }
3565 
ControlFlowViolation()3566 V8_NOINLINE void FuzzerMonitor::ControlFlowViolation() {
3567   // Control flow violation caught by CFI.
3568   void (*func)() = (void (*)()) & Dummy;
3569   func();
3570 }
3571 
DCheck()3572 V8_NOINLINE void FuzzerMonitor::DCheck() {
3573   // Caught in debug builds.
3574   DCHECK(false);
3575 }
3576 
Fatal()3577 V8_NOINLINE void FuzzerMonitor::Fatal() {
3578   // Caught in all build types.
3579   FATAL("Fake error.");
3580 }
3581 
ObservableDifference()3582 V8_NOINLINE void FuzzerMonitor::ObservableDifference() {
3583   // Observable difference caught by differential fuzzing.
3584   printf("___fake_difference___\n");
3585 }
3586 
UndefinedBehavior()3587 V8_NOINLINE void FuzzerMonitor::UndefinedBehavior() {
3588   // Caught by UBSAN.
3589   int32_t val = -1;
3590   USE(val << 8);
3591 }
3592 
UseAfterFree()3593 V8_NOINLINE void FuzzerMonitor::UseAfterFree() {
3594   // Use-after-free caught by ASAN.
3595   std::vector<bool>* storage = new std::vector<bool>(3);
3596   delete storage;
3597   USE(storage->at(1));
3598 }
3599 
UseOfUninitializedValue()3600 V8_NOINLINE void FuzzerMonitor::UseOfUninitializedValue() {
3601 // Use-of-uninitialized-value caught by MSAN.
3602 #if defined(__clang__)
3603   int uninitialized[1];
3604   if (uninitialized[0]) USE(uninitialized);
3605 #endif
3606 }
3607 
ReadChars(const char * name,int * size_out)3608 char* Shell::ReadChars(const char* name, int* size_out) {
3609   if (options.read_from_tcp_port >= 0) {
3610     return ReadCharsFromTcpPort(name, size_out);
3611   }
3612 
3613   FILE* file = base::OS::FOpen(name, "rb");
3614   if (file == nullptr) return nullptr;
3615 
3616   fseek(file, 0, SEEK_END);
3617   size_t size = ftell(file);
3618   rewind(file);
3619 
3620   char* chars = new char[size + 1];
3621   chars[size] = '\0';
3622   for (size_t i = 0; i < size;) {
3623     i += fread(&chars[i], 1, size - i, file);
3624     if (ferror(file)) {
3625       base::Fclose(file);
3626       delete[] chars;
3627       return nullptr;
3628     }
3629   }
3630   base::Fclose(file);
3631   *size_out = static_cast<int>(size);
3632   return chars;
3633 }
3634 
ReadLines(Isolate * isolate,const char * name)3635 MaybeLocal<PrimitiveArray> Shell::ReadLines(Isolate* isolate,
3636                                             const char* name) {
3637   int length;
3638   std::unique_ptr<char[]> data(ReadChars(name, &length));
3639 
3640   if (data.get() == nullptr) {
3641     return MaybeLocal<PrimitiveArray>();
3642   }
3643   std::stringstream stream(data.get());
3644   std::string line;
3645   std::vector<std::string> lines;
3646   while (std::getline(stream, line, '\n')) {
3647     lines.emplace_back(line);
3648   }
3649   // Create a Local<PrimitiveArray> off the read lines.
3650   int size = static_cast<int>(lines.size());
3651   Local<PrimitiveArray> exports = PrimitiveArray::New(isolate, size);
3652   for (int i = 0; i < size; ++i) {
3653     MaybeLocal<String> maybe_str = v8::String::NewFromUtf8(
3654         isolate, lines[i].c_str(), NewStringType::kNormal,
3655         static_cast<int>(lines[i].length()));
3656     Local<String> str;
3657     if (!maybe_str.ToLocal(&str)) {
3658       return MaybeLocal<PrimitiveArray>();
3659     }
3660     exports->Set(isolate, i, str);
3661   }
3662   return exports;
3663 }
3664 
ReadBuffer(const v8::FunctionCallbackInfo<v8::Value> & args)3665 void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) {
3666   static_assert(sizeof(char) == sizeof(uint8_t),
3667                 "char and uint8_t should both have 1 byte");
3668   Isolate* isolate = args.GetIsolate();
3669   String::Utf8Value filename(isolate, args[0]);
3670   int length;
3671   if (*filename == nullptr) {
3672     isolate->ThrowError("Error loading file");
3673     return;
3674   }
3675 
3676   uint8_t* data = reinterpret_cast<uint8_t*>(ReadChars(*filename, &length));
3677   if (data == nullptr) {
3678     isolate->ThrowError("Error reading file");
3679     return;
3680   }
3681   Local<v8::ArrayBuffer> buffer = ArrayBuffer::New(isolate, length);
3682   memcpy(buffer->GetBackingStore()->Data(), data, length);
3683   delete[] data;
3684 
3685   args.GetReturnValue().Set(buffer);
3686 }
3687 
3688 // Reads a file into a v8 string.
ReadFile(Isolate * isolate,const char * name,bool should_throw)3689 MaybeLocal<String> Shell::ReadFile(Isolate* isolate, const char* name,
3690                                    bool should_throw) {
3691   std::unique_ptr<base::OS::MemoryMappedFile> file(
3692       base::OS::MemoryMappedFile::open(
3693           name, base::OS::MemoryMappedFile::FileMode::kReadOnly));
3694   if (!file) {
3695     if (should_throw) {
3696       std::ostringstream oss;
3697       oss << "Error loading file: " << name;
3698       isolate->ThrowError(
3699           v8::String::NewFromUtf8(
3700               isolate, oss.str().substr(0, String::kMaxLength).c_str())
3701               .ToLocalChecked());
3702     }
3703     return MaybeLocal<String>();
3704   }
3705 
3706   int size = static_cast<int>(file->size());
3707   char* chars = static_cast<char*>(file->memory());
3708   if (i::FLAG_use_external_strings && i::String::IsAscii(chars, size)) {
3709     String::ExternalOneByteStringResource* resource =
3710         new ExternalOwningOneByteStringResource(std::move(file));
3711     return String::NewExternalOneByte(isolate, resource);
3712   }
3713   return String::NewFromUtf8(isolate, chars, NewStringType::kNormal, size);
3714 }
3715 
WriteChars(const char * name,uint8_t * buffer,size_t buffer_size)3716 void Shell::WriteChars(const char* name, uint8_t* buffer, size_t buffer_size) {
3717   FILE* file = base::Fopen(name, "w");
3718   if (file == nullptr) return;
3719   fwrite(buffer, 1, buffer_size, file);
3720   base::Fclose(file);
3721 }
3722 
RunShell(Isolate * isolate)3723 void Shell::RunShell(Isolate* isolate) {
3724   HandleScope outer_scope(isolate);
3725   v8::Local<v8::Context> context =
3726       v8::Local<v8::Context>::New(isolate, evaluation_context_);
3727   v8::Context::Scope context_scope(context);
3728   PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
3729   Local<String> name = String::NewFromUtf8Literal(isolate, "(d8)");
3730   printf("V8 version %s\n", V8::GetVersion());
3731   while (true) {
3732     HandleScope inner_scope(isolate);
3733     printf("d8> ");
3734     Local<String> input = Shell::ReadFromStdin(isolate);
3735     if (input.IsEmpty()) break;
3736     ExecuteString(isolate, input, name, kPrintResult, kReportExceptions,
3737                   kProcessMessageQueue);
3738   }
3739   printf("\n");
3740   // We need to explicitly clean up the module embedder data for
3741   // the interative shell context.
3742   DisposeModuleEmbedderData(context);
3743 }
3744 
3745 class InspectorFrontend final : public v8_inspector::V8Inspector::Channel {
3746  public:
InspectorFrontend(Local<Context> context)3747   explicit InspectorFrontend(Local<Context> context) {
3748     isolate_ = context->GetIsolate();
3749     context_.Reset(isolate_, context);
3750   }
3751   ~InspectorFrontend() override = default;
3752 
3753  private:
sendResponse(int callId,std::unique_ptr<v8_inspector::StringBuffer> message)3754   void sendResponse(
3755       int callId,
3756       std::unique_ptr<v8_inspector::StringBuffer> message) override {
3757     Send(message->string());
3758   }
sendNotification(std::unique_ptr<v8_inspector::StringBuffer> message)3759   void sendNotification(
3760       std::unique_ptr<v8_inspector::StringBuffer> message) override {
3761     Send(message->string());
3762   }
flushProtocolNotifications()3763   void flushProtocolNotifications() override {}
3764 
Send(const v8_inspector::StringView & string)3765   void Send(const v8_inspector::StringView& string) {
3766     v8::Isolate::AllowJavascriptExecutionScope allow_script(isolate_);
3767     v8::HandleScope handle_scope(isolate_);
3768     int length = static_cast<int>(string.length());
3769     DCHECK_LT(length, v8::String::kMaxLength);
3770     Local<String> message =
3771         (string.is8Bit()
3772              ? v8::String::NewFromOneByte(
3773                    isolate_,
3774                    reinterpret_cast<const uint8_t*>(string.characters8()),
3775                    v8::NewStringType::kNormal, length)
3776              : v8::String::NewFromTwoByte(
3777                    isolate_,
3778                    reinterpret_cast<const uint16_t*>(string.characters16()),
3779                    v8::NewStringType::kNormal, length))
3780             .ToLocalChecked();
3781     Local<String> callback_name = v8::String::NewFromUtf8Literal(
3782         isolate_, "receive", NewStringType::kInternalized);
3783     Local<Context> context = context_.Get(isolate_);
3784     Local<Value> callback =
3785         context->Global()->Get(context, callback_name).ToLocalChecked();
3786     if (callback->IsFunction()) {
3787       v8::TryCatch try_catch(isolate_);
3788       Local<Value> args[] = {message};
3789       USE(callback.As<Function>()->Call(context, Undefined(isolate_), 1, args));
3790 #ifdef DEBUG
3791       if (try_catch.HasCaught()) {
3792         Local<Object> exception = try_catch.Exception().As<Object>();
3793         Local<String> key = v8::String::NewFromUtf8Literal(
3794             isolate_, "message", NewStringType::kInternalized);
3795         Local<String> expected = v8::String::NewFromUtf8Literal(
3796             isolate_, "Maximum call stack size exceeded");
3797         Local<Value> value = exception->Get(context, key).ToLocalChecked();
3798         DCHECK(value->StrictEquals(expected));
3799       }
3800 #endif
3801     }
3802   }
3803 
3804   Isolate* isolate_;
3805   Global<Context> context_;
3806 };
3807 
3808 class InspectorClient : public v8_inspector::V8InspectorClient {
3809  public:
InspectorClient(Local<Context> context,bool connect)3810   InspectorClient(Local<Context> context, bool connect) {
3811     if (!connect) return;
3812     isolate_ = context->GetIsolate();
3813     channel_.reset(new InspectorFrontend(context));
3814     inspector_ = v8_inspector::V8Inspector::create(isolate_, this);
3815     session_ =
3816         inspector_->connect(1, channel_.get(), v8_inspector::StringView());
3817     context->SetAlignedPointerInEmbedderData(kInspectorClientIndex, this);
3818     inspector_->contextCreated(v8_inspector::V8ContextInfo(
3819         context, kContextGroupId, v8_inspector::StringView()));
3820 
3821     Local<Value> function =
3822         FunctionTemplate::New(isolate_, SendInspectorMessage)
3823             ->GetFunction(context)
3824             .ToLocalChecked();
3825     Local<String> function_name = String::NewFromUtf8Literal(
3826         isolate_, "send", NewStringType::kInternalized);
3827     CHECK(context->Global()->Set(context, function_name, function).FromJust());
3828 
3829     context_.Reset(isolate_, context);
3830   }
3831 
runMessageLoopOnPause(int contextGroupId)3832   void runMessageLoopOnPause(int contextGroupId) override {
3833     v8::Isolate::AllowJavascriptExecutionScope allow_script(isolate_);
3834     v8::HandleScope handle_scope(isolate_);
3835     Local<String> callback_name = v8::String::NewFromUtf8Literal(
3836         isolate_, "handleInspectorMessage", NewStringType::kInternalized);
3837     Local<Context> context = context_.Get(isolate_);
3838     Local<Value> callback =
3839         context->Global()->Get(context, callback_name).ToLocalChecked();
3840     if (!callback->IsFunction()) return;
3841 
3842     v8::TryCatch try_catch(isolate_);
3843     try_catch.SetVerbose(true);
3844     is_paused = true;
3845 
3846     while (is_paused) {
3847       USE(callback.As<Function>()->Call(context, Undefined(isolate_), 0, {}));
3848       if (try_catch.HasCaught()) {
3849         is_paused = false;
3850       }
3851     }
3852   }
3853 
quitMessageLoopOnPause()3854   void quitMessageLoopOnPause() override { is_paused = false; }
3855 
3856  private:
GetSession(Local<Context> context)3857   static v8_inspector::V8InspectorSession* GetSession(Local<Context> context) {
3858     InspectorClient* inspector_client = static_cast<InspectorClient*>(
3859         context->GetAlignedPointerFromEmbedderData(kInspectorClientIndex));
3860     return inspector_client->session_.get();
3861   }
3862 
ensureDefaultContextInGroup(int group_id)3863   Local<Context> ensureDefaultContextInGroup(int group_id) override {
3864     DCHECK(isolate_);
3865     DCHECK_EQ(kContextGroupId, group_id);
3866     return context_.Get(isolate_);
3867   }
3868 
SendInspectorMessage(const v8::FunctionCallbackInfo<v8::Value> & args)3869   static void SendInspectorMessage(
3870       const v8::FunctionCallbackInfo<v8::Value>& args) {
3871     Isolate* isolate = args.GetIsolate();
3872     v8::HandleScope handle_scope(isolate);
3873     Local<Context> context = isolate->GetCurrentContext();
3874     args.GetReturnValue().Set(Undefined(isolate));
3875     Local<String> message = args[0]->ToString(context).ToLocalChecked();
3876     v8_inspector::V8InspectorSession* session =
3877         InspectorClient::GetSession(context);
3878     int length = message->Length();
3879     std::unique_ptr<uint16_t[]> buffer(new uint16_t[length]);
3880     message->Write(isolate, buffer.get(), 0, length);
3881     v8_inspector::StringView message_view(buffer.get(), length);
3882     {
3883       v8::SealHandleScope seal_handle_scope(isolate);
3884       session->dispatchProtocolMessage(message_view);
3885     }
3886     args.GetReturnValue().Set(True(isolate));
3887   }
3888 
3889   static const int kContextGroupId = 1;
3890 
3891   std::unique_ptr<v8_inspector::V8Inspector> inspector_;
3892   std::unique_ptr<v8_inspector::V8InspectorSession> session_;
3893   std::unique_ptr<v8_inspector::V8Inspector::Channel> channel_;
3894   bool is_paused = false;
3895   Global<Context> context_;
3896   Isolate* isolate_;
3897 };
3898 
~SourceGroup()3899 SourceGroup::~SourceGroup() {
3900   delete thread_;
3901   thread_ = nullptr;
3902 }
3903 
ends_with(const char * input,const char * suffix)3904 bool ends_with(const char* input, const char* suffix) {
3905   size_t input_length = strlen(input);
3906   size_t suffix_length = strlen(suffix);
3907   if (suffix_length <= input_length) {
3908     return strcmp(input + input_length - suffix_length, suffix) == 0;
3909   }
3910   return false;
3911 }
3912 
Execute(Isolate * isolate)3913 bool SourceGroup::Execute(Isolate* isolate) {
3914   bool success = true;
3915 #ifdef V8_FUZZILLI
3916   if (fuzzilli_reprl) {
3917     HandleScope handle_scope(isolate);
3918     Local<String> file_name =
3919         String::NewFromUtf8(isolate, "fuzzcode.js", NewStringType::kNormal)
3920             .ToLocalChecked();
3921 
3922     size_t script_size;
3923     CHECK_EQ(read(REPRL_CRFD, &script_size, 8), 8);
3924     char* buffer = new char[script_size + 1];
3925     char* ptr = buffer;
3926     size_t remaining = script_size;
3927     while (remaining > 0) {
3928       ssize_t rv = read(REPRL_DRFD, ptr, remaining);
3929       CHECK_GE(rv, 0);
3930       remaining -= rv;
3931       ptr += rv;
3932     }
3933     buffer[script_size] = 0;
3934 
3935     Local<String> source =
3936         String::NewFromUtf8(isolate, buffer, NewStringType::kNormal)
3937             .ToLocalChecked();
3938     delete[] buffer;
3939     Shell::set_script_executed();
3940     if (!Shell::ExecuteString(isolate, source, file_name, Shell::kNoPrintResult,
3941                               Shell::kReportExceptions,
3942                               Shell::kNoProcessMessageQueue)) {
3943       return false;
3944     }
3945   }
3946 #endif  // V8_FUZZILLI
3947   for (int i = begin_offset_; i < end_offset_; ++i) {
3948     const char* arg = argv_[i];
3949     if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) {
3950       // Execute argument given to -e option directly.
3951       HandleScope handle_scope(isolate);
3952       Local<String> file_name = String::NewFromUtf8Literal(isolate, "unnamed");
3953       Local<String> source =
3954           String::NewFromUtf8(isolate, argv_[i + 1]).ToLocalChecked();
3955       Shell::set_script_executed();
3956       if (!Shell::ExecuteString(isolate, source, file_name,
3957                                 Shell::kNoPrintResult, Shell::kReportExceptions,
3958                                 Shell::kNoProcessMessageQueue)) {
3959         success = false;
3960         break;
3961       }
3962       ++i;
3963       continue;
3964     } else if (ends_with(arg, ".mjs")) {
3965       Shell::set_script_executed();
3966       if (!Shell::ExecuteModule(isolate, arg)) {
3967         success = false;
3968         break;
3969       }
3970       continue;
3971     } else if (strcmp(arg, "--module") == 0 && i + 1 < end_offset_) {
3972       // Treat the next file as a module.
3973       arg = argv_[++i];
3974       Shell::set_script_executed();
3975       if (!Shell::ExecuteModule(isolate, arg)) {
3976         success = false;
3977         break;
3978       }
3979       continue;
3980     } else if (strcmp(arg, "--web-snapshot") == 0 && i + 1 < end_offset_) {
3981       // Treat the next file as a web snapshot.
3982       arg = argv_[++i];
3983       Shell::set_script_executed();
3984       if (!Shell::ExecuteWebSnapshot(isolate, arg)) {
3985         success = false;
3986         break;
3987       }
3988       continue;
3989     } else if (strcmp(arg, "--json") == 0 && i + 1 < end_offset_) {
3990       // Treat the next file as a JSON file.
3991       arg = argv_[++i];
3992       Shell::set_script_executed();
3993       if (!Shell::LoadJSON(isolate, arg)) {
3994         success = false;
3995         break;
3996       }
3997       continue;
3998     } else if (arg[0] == '-') {
3999       // Ignore other options. They have been parsed already.
4000       continue;
4001     }
4002 
4003     // Use all other arguments as names of files to load and run.
4004     HandleScope handle_scope(isolate);
4005     Local<String> file_name =
4006         String::NewFromUtf8(isolate, arg).ToLocalChecked();
4007     Local<String> source;
4008     if (!Shell::ReadFile(isolate, arg).ToLocal(&source)) {
4009       printf("Error reading '%s'\n", arg);
4010       base::OS::ExitProcess(1);
4011     }
4012     Shell::set_script_executed();
4013     Shell::update_script_size(source->Length());
4014     if (!Shell::ExecuteString(isolate, source, file_name, Shell::kNoPrintResult,
4015                               Shell::kReportExceptions,
4016                               Shell::kProcessMessageQueue)) {
4017       success = false;
4018       break;
4019     }
4020   }
4021   return success;
4022 }
4023 
IsolateThread(SourceGroup * group)4024 SourceGroup::IsolateThread::IsolateThread(SourceGroup* group)
4025     : base::Thread(GetThreadOptions("IsolateThread")), group_(group) {}
4026 
ExecuteInThread()4027 void SourceGroup::ExecuteInThread() {
4028   Isolate::CreateParams create_params;
4029   create_params.array_buffer_allocator = Shell::array_buffer_allocator;
4030   create_params.experimental_attach_to_shared_isolate = Shell::shared_isolate;
4031   Isolate* isolate = Isolate::New(create_params);
4032   Shell::SetWaitUntilDone(isolate, false);
4033   D8Console console(isolate);
4034   Shell::Initialize(isolate, &console, false);
4035 
4036   for (int i = 0; i < Shell::options.stress_runs; ++i) {
4037     {
4038       i::ParkedScope parked_scope(
4039           reinterpret_cast<i::Isolate*>(isolate)->main_thread_local_isolate());
4040       next_semaphore_.Wait();
4041     }
4042     {
4043       Isolate::Scope iscope(isolate);
4044       PerIsolateData data(isolate);
4045       {
4046         HandleScope scope(isolate);
4047         Local<Context> context = Shell::CreateEvaluationContext(isolate);
4048         {
4049           Context::Scope cscope(context);
4050           InspectorClient inspector_client(context,
4051                                            Shell::options.enable_inspector);
4052           PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
4053           Execute(isolate);
4054           Shell::CompleteMessageLoop(isolate);
4055         }
4056         DisposeModuleEmbedderData(context);
4057       }
4058       Shell::CollectGarbage(isolate);
4059     }
4060     done_semaphore_.Signal();
4061   }
4062 
4063   isolate->Dispose();
4064 }
4065 
StartExecuteInThread()4066 void SourceGroup::StartExecuteInThread() {
4067   if (thread_ == nullptr) {
4068     thread_ = new IsolateThread(this);
4069     CHECK(thread_->Start());
4070   }
4071   next_semaphore_.Signal();
4072 }
4073 
WaitForThread()4074 void SourceGroup::WaitForThread() {
4075   if (thread_ == nullptr) return;
4076   done_semaphore_.Wait();
4077 }
4078 
JoinThread()4079 void SourceGroup::JoinThread() {
4080   if (thread_ == nullptr) return;
4081   thread_->Join();
4082 }
4083 
Enqueue(std::unique_ptr<SerializationData> data)4084 void SerializationDataQueue::Enqueue(std::unique_ptr<SerializationData> data) {
4085   base::MutexGuard lock_guard(&mutex_);
4086   data_.push_back(std::move(data));
4087 }
4088 
Dequeue(std::unique_ptr<SerializationData> * out_data)4089 bool SerializationDataQueue::Dequeue(
4090     std::unique_ptr<SerializationData>* out_data) {
4091   out_data->reset();
4092   base::MutexGuard lock_guard(&mutex_);
4093   if (data_.empty()) return false;
4094   *out_data = std::move(data_[0]);
4095   data_.erase(data_.begin());
4096   return true;
4097 }
4098 
IsEmpty()4099 bool SerializationDataQueue::IsEmpty() {
4100   base::MutexGuard lock_guard(&mutex_);
4101   return data_.empty();
4102 }
4103 
Clear()4104 void SerializationDataQueue::Clear() {
4105   base::MutexGuard lock_guard(&mutex_);
4106   data_.clear();
4107 }
4108 
Worker(const char * script)4109 Worker::Worker(const char* script) : script_(i::StrDup(script)) {
4110   state_.store(State::kReady);
4111 }
4112 
~Worker()4113 Worker::~Worker() {
4114   CHECK(state_.load() == State::kTerminated);
4115   DCHECK_NULL(isolate_);
4116   delete thread_;
4117   thread_ = nullptr;
4118   delete[] script_;
4119   script_ = nullptr;
4120 }
4121 
is_running() const4122 bool Worker::is_running() const { return state_.load() == State::kRunning; }
4123 
StartWorkerThread(std::shared_ptr<Worker> worker)4124 bool Worker::StartWorkerThread(std::shared_ptr<Worker> worker) {
4125   auto expected = State::kReady;
4126   CHECK(
4127       worker->state_.compare_exchange_strong(expected, State::kPrepareRunning));
4128   auto thread = new WorkerThread(worker);
4129   worker->thread_ = thread;
4130   if (!thread->Start()) return false;
4131   // Wait until the worker is ready to receive messages.
4132   worker->started_semaphore_.Wait();
4133   Shell::AddRunningWorker(std::move(worker));
4134   return true;
4135 }
4136 
Run()4137 void Worker::WorkerThread::Run() {
4138   // Prevent a lifetime cycle from Worker -> WorkerThread -> Worker.
4139   // We must clear the worker_ field of the thread, but we keep the
4140   // worker alive via a stack root until the thread finishes execution
4141   // and removes itself from the running set. Thereafter the only
4142   // remaining reference can be from a JavaScript object via a Managed.
4143   auto worker = std::move(worker_);
4144   worker_ = nullptr;
4145   worker->ExecuteInThread();
4146   Shell::RemoveRunningWorker(worker);
4147 }
4148 
4149 class ProcessMessageTask : public i::CancelableTask {
4150  public:
ProcessMessageTask(i::CancelableTaskManager * task_manager,std::shared_ptr<Worker> worker,std::unique_ptr<SerializationData> data)4151   ProcessMessageTask(i::CancelableTaskManager* task_manager,
4152                      std::shared_ptr<Worker> worker,
4153                      std::unique_ptr<SerializationData> data)
4154       : i::CancelableTask(task_manager),
4155         worker_(worker),
4156         data_(std::move(data)) {}
4157 
RunInternal()4158   void RunInternal() override { worker_->ProcessMessage(std::move(data_)); }
4159 
4160  private:
4161   std::shared_ptr<Worker> worker_;
4162   std::unique_ptr<SerializationData> data_;
4163 };
4164 
PostMessage(std::unique_ptr<SerializationData> data)4165 void Worker::PostMessage(std::unique_ptr<SerializationData> data) {
4166   base::MutexGuard lock_guard(&worker_mutex_);
4167   if (!is_running()) return;
4168   std::unique_ptr<v8::Task> task(new ProcessMessageTask(
4169       task_manager_, shared_from_this(), std::move(data)));
4170   task_runner_->PostNonNestableTask(std::move(task));
4171 }
4172 
4173 class TerminateTask : public i::CancelableTask {
4174  public:
TerminateTask(i::CancelableTaskManager * task_manager,std::shared_ptr<Worker> worker)4175   TerminateTask(i::CancelableTaskManager* task_manager,
4176                 std::shared_ptr<Worker> worker)
4177       : i::CancelableTask(task_manager), worker_(worker) {}
4178 
RunInternal()4179   void RunInternal() override {
4180     auto expected = Worker::State::kTerminating;
4181     CHECK(worker_->state_.compare_exchange_strong(expected,
4182                                                   Worker::State::kTerminated));
4183   }
4184 
4185  private:
4186   std::shared_ptr<Worker> worker_;
4187 };
4188 
GetMessage()4189 std::unique_ptr<SerializationData> Worker::GetMessage() {
4190   std::unique_ptr<SerializationData> result;
4191   while (!out_queue_.Dequeue(&result)) {
4192     // If the worker is no longer running, and there are no messages in the
4193     // queue, don't expect any more messages from it.
4194     if (!is_running()) break;
4195     out_semaphore_.Wait();
4196   }
4197   return result;
4198 }
4199 
TerminateAndWaitForThread()4200 void Worker::TerminateAndWaitForThread() {
4201   Terminate();
4202   {
4203     base::MutexGuard lock_guard(&worker_mutex_);
4204     // Prevent double-joining.
4205     if (is_joined_) return;
4206     is_joined_ = true;
4207   }
4208   thread_->Join();
4209 }
4210 
Terminate()4211 void Worker::Terminate() {
4212   base::MutexGuard lock_guard(&worker_mutex_);
4213   auto expected = State::kRunning;
4214   if (!state_.compare_exchange_strong(expected, State::kTerminating)) return;
4215   std::unique_ptr<v8::Task> task(
4216       new TerminateTask(task_manager_, shared_from_this()));
4217   task_runner_->PostTask(std::move(task));
4218   // Also schedule an interrupt in case the worker is running code and never
4219   // returning to the event queue. Since we checked the state before, and we are
4220   // holding the {worker_mutex_}, it's safe to access the isolate.
4221   isolate_->TerminateExecution();
4222 }
4223 
ProcessMessage(std::unique_ptr<SerializationData> data)4224 void Worker::ProcessMessage(std::unique_ptr<SerializationData> data) {
4225   if (!is_running()) return;
4226   DCHECK_NOT_NULL(isolate_);
4227   HandleScope scope(isolate_);
4228   Local<Context> context = context_.Get(isolate_);
4229   Context::Scope cscope(context);
4230   Local<Object> global = context->Global();
4231 
4232   // Get the message handler.
4233   MaybeLocal<Value> maybe_onmessage = global->Get(
4234       context, String::NewFromUtf8Literal(isolate_, "onmessage",
4235                                           NewStringType::kInternalized));
4236   Local<Value> onmessage;
4237   if (!maybe_onmessage.ToLocal(&onmessage) || !onmessage->IsFunction()) return;
4238   Local<Function> onmessage_fun = onmessage.As<Function>();
4239 
4240   v8::TryCatch try_catch(isolate_);
4241   try_catch.SetVerbose(true);
4242   Local<Value> value;
4243   if (Shell::DeserializeValue(isolate_, std::move(data)).ToLocal(&value)) {
4244     Local<Value> argv[] = {value};
4245     MaybeLocal<Value> result = onmessage_fun->Call(context, global, 1, argv);
4246     USE(result);
4247   }
4248 }
4249 
ProcessMessages()4250 void Worker::ProcessMessages() {
4251   i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate_);
4252   i::SaveAndSwitchContext saved_context(i_isolate, i::Context());
4253   SealHandleScope shs(isolate_);
4254   while (is_running() && v8::platform::PumpMessageLoop(
4255                              g_default_platform, isolate_,
4256                              platform::MessageLoopBehavior::kWaitForWork)) {
4257     if (is_running()) {
4258       MicrotasksScope::PerformCheckpoint(isolate_);
4259     }
4260   }
4261 }
4262 
ExecuteInThread()4263 void Worker::ExecuteInThread() {
4264   Isolate::CreateParams create_params;
4265   create_params.array_buffer_allocator = Shell::array_buffer_allocator;
4266   create_params.experimental_attach_to_shared_isolate = Shell::shared_isolate;
4267   isolate_ = Isolate::New(create_params);
4268 
4269   task_runner_ = g_default_platform->GetForegroundTaskRunner(isolate_);
4270   task_manager_ =
4271       reinterpret_cast<i::Isolate*>(isolate_)->cancelable_task_manager();
4272 
4273   auto expected = State::kPrepareRunning;
4274   CHECK(state_.compare_exchange_strong(expected, State::kRunning));
4275 
4276   // The Worker is now ready to receive messages.
4277   started_semaphore_.Signal();
4278 
4279   D8Console console(isolate_);
4280   Shell::Initialize(isolate_, &console, false);
4281   // This is not really a loop, but the loop allows us to break out of this
4282   // block easily.
4283   for (bool execute = true; execute; execute = false) {
4284     Isolate::Scope iscope(isolate_);
4285     {
4286       HandleScope scope(isolate_);
4287       PerIsolateData data(isolate_);
4288       Local<Context> context = Shell::CreateEvaluationContext(isolate_);
4289       if (context.IsEmpty()) break;
4290       context_.Reset(isolate_, context);
4291       {
4292         Context::Scope cscope(context);
4293         PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate_));
4294 
4295         Local<Object> global = context->Global();
4296         Local<Value> this_value = External::New(isolate_, this);
4297         Local<FunctionTemplate> postmessage_fun_template =
4298             FunctionTemplate::New(isolate_, PostMessageOut, this_value);
4299 
4300         Local<Function> postmessage_fun;
4301         if (postmessage_fun_template->GetFunction(context).ToLocal(
4302                 &postmessage_fun)) {
4303           global
4304               ->Set(context,
4305                     v8::String::NewFromUtf8Literal(
4306                         isolate_, "postMessage", NewStringType::kInternalized),
4307                     postmessage_fun)
4308               .FromJust();
4309         }
4310 
4311         // First run the script
4312         Local<String> file_name =
4313             String::NewFromUtf8Literal(isolate_, "unnamed");
4314         Local<String> source =
4315             String::NewFromUtf8(isolate_, script_).ToLocalChecked();
4316         if (Shell::ExecuteString(
4317                 isolate_, source, file_name, Shell::kNoPrintResult,
4318                 Shell::kReportExceptions, Shell::kProcessMessageQueue)) {
4319           // Check that there's a message handler
4320           MaybeLocal<Value> maybe_onmessage = global->Get(
4321               context,
4322               String::NewFromUtf8Literal(isolate_, "onmessage",
4323                                          NewStringType::kInternalized));
4324           Local<Value> onmessage;
4325           if (maybe_onmessage.ToLocal(&onmessage) && onmessage->IsFunction()) {
4326             // Now wait for messages.
4327             ProcessMessages();
4328           }
4329         }
4330       }
4331       DisposeModuleEmbedderData(context);
4332     }
4333     Shell::CollectGarbage(isolate_);
4334   }
4335 
4336   {
4337     base::MutexGuard lock_guard(&worker_mutex_);
4338     state_.store(State::kTerminated);
4339     CHECK(!is_running());
4340     task_runner_.reset();
4341     task_manager_ = nullptr;
4342   }
4343 
4344   context_.Reset();
4345   platform::NotifyIsolateShutdown(g_default_platform, isolate_);
4346   isolate_->Dispose();
4347   isolate_ = nullptr;
4348 
4349   // Post nullptr to wake the thread waiting on GetMessage() if there is one.
4350   out_queue_.Enqueue(nullptr);
4351   out_semaphore_.Signal();
4352 }
4353 
PostMessageOut(const v8::FunctionCallbackInfo<v8::Value> & args)4354 void Worker::PostMessageOut(const v8::FunctionCallbackInfo<v8::Value>& args) {
4355   Isolate* isolate = args.GetIsolate();
4356   HandleScope handle_scope(isolate);
4357 
4358   if (args.Length() < 1) {
4359     isolate->ThrowError("Invalid argument");
4360     return;
4361   }
4362 
4363   Local<Value> message = args[0];
4364   Local<Value> transfer = Undefined(isolate);
4365   std::unique_ptr<SerializationData> data =
4366       Shell::SerializeValue(isolate, message, transfer);
4367   if (data) {
4368     DCHECK(args.Data()->IsExternal());
4369     Local<External> this_value = args.Data().As<External>();
4370     Worker* worker = static_cast<Worker*>(this_value->Value());
4371     worker->out_queue_.Enqueue(std::move(data));
4372     worker->out_semaphore_.Signal();
4373   }
4374 }
4375 
4376 #if V8_TARGET_OS_WIN
4377 // Enable support for unicode filename path on windows.
4378 // We first convert ansi encoded argv[i] to utf16 encoded, and then
4379 // convert utf16 encoded to utf8 encoded with setting the argv[i]
4380 // to the utf8 encoded arg. We allocate memory for the utf8 encoded
4381 // arg, and we will free it and reset it to nullptr after using
4382 // the filename path arg. And because Execute may be called multiple
4383 // times, we need to free the allocated unicode filename when exit.
4384 
4385 // Save the allocated utf8 filenames, and we will free them when exit.
4386 std::vector<char*> utf8_filenames;
4387 #include <shellapi.h>
4388 // Convert utf-16 encoded string to utf-8 encoded.
ConvertUtf16StringToUtf8(const wchar_t * str)4389 char* ConvertUtf16StringToUtf8(const wchar_t* str) {
4390   // On Windows wchar_t must be a 16-bit value.
4391   static_assert(sizeof(wchar_t) == 2, "wrong wchar_t size");
4392   int len =
4393       WideCharToMultiByte(CP_UTF8, 0, str, -1, nullptr, 0, nullptr, FALSE);
4394   DCHECK_LT(0, len);
4395   char* utf8_str = new char[len];
4396   utf8_filenames.push_back(utf8_str);
4397   WideCharToMultiByte(CP_UTF8, 0, str, -1, utf8_str, len, nullptr, FALSE);
4398   return utf8_str;
4399 }
4400 
4401 // Convert ansi encoded argv[i] to utf8 encoded.
PreProcessUnicodeFilenameArg(char * argv[],int i)4402 void PreProcessUnicodeFilenameArg(char* argv[], int i) {
4403   int argc;
4404   wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
4405   argv[i] = ConvertUtf16StringToUtf8(wargv[i]);
4406   LocalFree(wargv);
4407 }
4408 
4409 #endif
4410 
SetOptions(int argc,char * argv[])4411 bool Shell::SetOptions(int argc, char* argv[]) {
4412   bool logfile_per_isolate = false;
4413   bool no_always_opt = false;
4414   options.d8_path = argv[0];
4415   for (int i = 0; i < argc; i++) {
4416     if (strcmp(argv[i], "--") == 0) {
4417       argv[i] = nullptr;
4418       for (int j = i + 1; j < argc; j++) {
4419         options.arguments.push_back(argv[j]);
4420         argv[j] = nullptr;
4421       }
4422       break;
4423     } else if (strcmp(argv[i], "--no-arguments") == 0) {
4424       options.include_arguments = false;
4425       argv[i] = nullptr;
4426     } else if (strcmp(argv[i], "--simulate-errors") == 0) {
4427       options.simulate_errors = true;
4428       argv[i] = nullptr;
4429     } else if (strcmp(argv[i], "--stress-opt") == 0) {
4430       options.stress_opt = true;
4431       argv[i] = nullptr;
4432     } else if (strcmp(argv[i], "--nostress-opt") == 0 ||
4433                strcmp(argv[i], "--no-stress-opt") == 0) {
4434       options.stress_opt = false;
4435       argv[i] = nullptr;
4436     } else if (strcmp(argv[i], "--noalways-opt") == 0 ||
4437                strcmp(argv[i], "--no-always-opt") == 0) {
4438       no_always_opt = true;
4439     } else if (strcmp(argv[i], "--fuzzing") == 0 ||
4440                strcmp(argv[i], "--no-abort-on-contradictory-flags") == 0 ||
4441                strcmp(argv[i], "--noabort-on-contradictory-flags") == 0) {
4442       check_d8_flag_contradictions = false;
4443     } else if (strcmp(argv[i], "--abort-on-contradictory-flags") == 0) {
4444       check_d8_flag_contradictions = true;
4445     } else if (strcmp(argv[i], "--logfile-per-isolate") == 0) {
4446       logfile_per_isolate = true;
4447       argv[i] = nullptr;
4448     } else if (strcmp(argv[i], "--shell") == 0) {
4449       options.interactive_shell = true;
4450       argv[i] = nullptr;
4451     } else if (strcmp(argv[i], "--test") == 0) {
4452       options.test_shell = true;
4453       argv[i] = nullptr;
4454     } else if (strcmp(argv[i], "--notest") == 0 ||
4455                strcmp(argv[i], "--no-test") == 0) {
4456       options.test_shell = false;
4457       argv[i] = nullptr;
4458     } else if (strcmp(argv[i], "--send-idle-notification") == 0) {
4459       options.send_idle_notification = true;
4460       argv[i] = nullptr;
4461     } else if (strcmp(argv[i], "--invoke-weak-callbacks") == 0) {
4462       options.invoke_weak_callbacks = true;
4463       // TODO(v8:3351): Invoking weak callbacks does not always collect all
4464       // available garbage.
4465       options.send_idle_notification = true;
4466       argv[i] = nullptr;
4467     } else if (strcmp(argv[i], "--omit-quit") == 0) {
4468       options.omit_quit = true;
4469       argv[i] = nullptr;
4470     } else if (strcmp(argv[i], "--no-wait-for-background-tasks") == 0) {
4471       // TODO(herhut) Remove this flag once wasm compilation is fully
4472       // isolate-independent.
4473       options.wait_for_background_tasks = false;
4474       argv[i] = nullptr;
4475     } else if (strcmp(argv[i], "-f") == 0) {
4476       // Ignore any -f flags for compatibility with other stand-alone
4477       // JavaScript engines.
4478       continue;
4479     } else if (strcmp(argv[i], "--ignore-unhandled-promises") == 0) {
4480       options.ignore_unhandled_promises = true;
4481       argv[i] = nullptr;
4482     } else if (strcmp(argv[i], "--isolate") == 0) {
4483       options.num_isolates++;
4484     } else if (strcmp(argv[i], "--throws") == 0) {
4485       options.expected_to_throw = true;
4486       argv[i] = nullptr;
4487     } else if (strcmp(argv[i], "--no-fail") == 0) {
4488       options.no_fail = true;
4489       argv[i] = nullptr;
4490     } else if (strcmp(argv[i], "--dump-counters") == 0) {
4491       i::FLAG_slow_histograms = true;
4492       options.dump_counters = true;
4493       argv[i] = nullptr;
4494     } else if (strcmp(argv[i], "--dump-counters-nvp") == 0) {
4495       i::FLAG_slow_histograms = true;
4496       options.dump_counters_nvp = true;
4497       argv[i] = nullptr;
4498     } else if (strncmp(argv[i], "--icu-data-file=", 16) == 0) {
4499       options.icu_data_file = argv[i] + 16;
4500       argv[i] = nullptr;
4501     } else if (strncmp(argv[i], "--icu-locale=", 13) == 0) {
4502       options.icu_locale = argv[i] + 13;
4503       argv[i] = nullptr;
4504 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
4505     } else if (strncmp(argv[i], "--snapshot_blob=", 16) == 0) {
4506       options.snapshot_blob = argv[i] + 16;
4507       argv[i] = nullptr;
4508 #endif  // V8_USE_EXTERNAL_STARTUP_DATA
4509     } else if (strcmp(argv[i], "--cache") == 0 ||
4510                strncmp(argv[i], "--cache=", 8) == 0) {
4511       const char* value = argv[i] + 7;
4512       if (!*value || strncmp(value, "=code", 6) == 0) {
4513         options.compile_options = v8::ScriptCompiler::kNoCompileOptions;
4514         options.code_cache_options =
4515             ShellOptions::CodeCacheOptions::kProduceCache;
4516       } else if (strncmp(value, "=none", 6) == 0) {
4517         options.compile_options = v8::ScriptCompiler::kNoCompileOptions;
4518         options.code_cache_options =
4519             ShellOptions::CodeCacheOptions::kNoProduceCache;
4520       } else if (strncmp(value, "=after-execute", 15) == 0) {
4521         options.compile_options = v8::ScriptCompiler::kNoCompileOptions;
4522         options.code_cache_options =
4523             ShellOptions::CodeCacheOptions::kProduceCacheAfterExecute;
4524       } else if (strncmp(value, "=full-code-cache", 17) == 0) {
4525         options.compile_options = v8::ScriptCompiler::kEagerCompile;
4526         options.code_cache_options =
4527             ShellOptions::CodeCacheOptions::kProduceCache;
4528       } else {
4529         printf("Unknown option to --cache.\n");
4530         return false;
4531       }
4532       argv[i] = nullptr;
4533     } else if (strcmp(argv[i], "--streaming-compile") == 0) {
4534       options.streaming_compile = true;
4535       argv[i] = nullptr;
4536     } else if ((strcmp(argv[i], "--no-streaming-compile") == 0) ||
4537                (strcmp(argv[i], "--nostreaming-compile") == 0)) {
4538       options.streaming_compile = false;
4539       argv[i] = nullptr;
4540     } else if (strcmp(argv[i], "--enable-tracing") == 0) {
4541       options.trace_enabled = true;
4542       argv[i] = nullptr;
4543     } else if (strncmp(argv[i], "--trace-path=", 13) == 0) {
4544       options.trace_path = argv[i] + 13;
4545       argv[i] = nullptr;
4546     } else if (strncmp(argv[i], "--trace-config=", 15) == 0) {
4547       options.trace_config = argv[i] + 15;
4548       argv[i] = nullptr;
4549     } else if (strcmp(argv[i], "--enable-inspector") == 0) {
4550       options.enable_inspector = true;
4551       argv[i] = nullptr;
4552     } else if (strncmp(argv[i], "--lcov=", 7) == 0) {
4553       options.lcov_file = argv[i] + 7;
4554       argv[i] = nullptr;
4555     } else if (strcmp(argv[i], "--disable-in-process-stack-traces") == 0) {
4556       options.disable_in_process_stack_traces = true;
4557       argv[i] = nullptr;
4558 #ifdef V8_OS_POSIX
4559     } else if (strncmp(argv[i], "--read-from-tcp-port=", 21) == 0) {
4560       options.read_from_tcp_port = atoi(argv[i] + 21);
4561       argv[i] = nullptr;
4562 #endif  // V8_OS_POSIX
4563     } else if (strcmp(argv[i], "--enable-os-system") == 0) {
4564       options.enable_os_system = true;
4565       argv[i] = nullptr;
4566     } else if (strcmp(argv[i], "--quiet-load") == 0) {
4567       options.quiet_load = true;
4568       argv[i] = nullptr;
4569     } else if (strncmp(argv[i], "--thread-pool-size=", 19) == 0) {
4570       options.thread_pool_size = atoi(argv[i] + 19);
4571       argv[i] = nullptr;
4572     } else if (strcmp(argv[i], "--stress-delay-tasks") == 0) {
4573       // Delay execution of tasks by 0-100ms randomly (based on --random-seed).
4574       options.stress_delay_tasks = true;
4575       argv[i] = nullptr;
4576     } else if (strcmp(argv[i], "--cpu-profiler") == 0) {
4577       options.cpu_profiler = true;
4578       argv[i] = nullptr;
4579     } else if (strcmp(argv[i], "--cpu-profiler-print") == 0) {
4580       options.cpu_profiler = true;
4581       options.cpu_profiler_print = true;
4582       argv[i] = nullptr;
4583     } else if (strcmp(argv[i], "--stress-deserialize") == 0) {
4584       options.stress_deserialize = true;
4585       argv[i] = nullptr;
4586     } else if (strncmp(argv[i], "--web-snapshot-config=", 22) == 0) {
4587       options.web_snapshot_config = argv[i] + 22;
4588       argv[i] = nullptr;
4589     } else if (strncmp(argv[i], "--web-snapshot-output=", 22) == 0) {
4590       options.web_snapshot_output = argv[i] + 22;
4591       argv[i] = nullptr;
4592     } else if (strcmp(argv[i], "--experimental-d8-web-snapshot-api") == 0) {
4593       options.d8_web_snapshot_api = true;
4594       argv[i] = nullptr;
4595     } else if (strcmp(argv[i], "--compile-only") == 0) {
4596       options.compile_only = true;
4597       argv[i] = nullptr;
4598     } else if (strncmp(argv[i], "--repeat-compile=", 17) == 0) {
4599       options.repeat_compile = atoi(argv[i] + 17);
4600       argv[i] = nullptr;
4601 #ifdef V8_FUZZILLI
4602     } else if (strcmp(argv[i], "--no-fuzzilli-enable-builtins-coverage") == 0) {
4603       options.fuzzilli_enable_builtins_coverage = false;
4604       argv[i] = nullptr;
4605     } else if (strcmp(argv[i], "--fuzzilli-coverage-statistics") == 0) {
4606       options.fuzzilli_coverage_statistics = true;
4607       argv[i] = nullptr;
4608 #endif
4609     } else if (strcmp(argv[i], "--no-fuzzy-module-file-extensions") == 0) {
4610       DCHECK(options.fuzzy_module_file_extensions);
4611       options.fuzzy_module_file_extensions = false;
4612       argv[i] = nullptr;
4613 #if defined(V8_ENABLE_SYSTEM_INSTRUMENTATION)
4614     } else if (strcmp(argv[i], "--enable-system-instrumentation") == 0) {
4615       options.enable_system_instrumentation = true;
4616       options.trace_enabled = true;
4617       // This needs to be manually triggered for JIT ETW events to work.
4618       i::FLAG_enable_system_instrumentation = true;
4619 #if defined(V8_OS_WIN)
4620       // Guard this bc the flag has a lot of overhead and is not currently used
4621       // by macos
4622       i::FLAG_interpreted_frames_native_stack = true;
4623 #endif
4624       argv[i] = nullptr;
4625 #endif
4626 #if V8_ENABLE_WEBASSEMBLY
4627     } else if (strcmp(argv[i], "--wasm-trap-handler") == 0) {
4628       options.wasm_trap_handler = true;
4629       argv[i] = nullptr;
4630     } else if (strcmp(argv[i], "--no-wasm-trap-handler") == 0) {
4631       options.wasm_trap_handler = false;
4632       argv[i] = nullptr;
4633 #endif  // V8_ENABLE_WEBASSEMBLY
4634     } else if (strcmp(argv[i], "--expose-fast-api") == 0) {
4635       options.expose_fast_api = true;
4636       argv[i] = nullptr;
4637     } else {
4638 #ifdef V8_TARGET_OS_WIN
4639       PreProcessUnicodeFilenameArg(argv, i);
4640 #endif
4641     }
4642   }
4643 
4644   if (options.stress_opt && no_always_opt && check_d8_flag_contradictions) {
4645     FATAL("Flag --no-always-opt is incompatible with --stress-opt.");
4646   }
4647 
4648   const char* usage =
4649       "Synopsis:\n"
4650       "  shell [options] [--shell] [<file>...]\n"
4651       "  d8 [options] [-e <string>] [--shell] [[--module|--web-snapshot]"
4652       " <file>...]\n\n"
4653       "  -e        execute a string in V8\n"
4654       "  --shell   run an interactive JavaScript shell\n"
4655       "  --module  execute a file as a JavaScript module\n"
4656       "  --web-snapshot  execute a file as a web snapshot\n\n";
4657   using HelpOptions = i::FlagList::HelpOptions;
4658   i::FLAG_abort_on_contradictory_flags = true;
4659   i::FlagList::SetFlagsFromCommandLine(&argc, argv, true,
4660                                        HelpOptions(HelpOptions::kExit, usage));
4661   options.mock_arraybuffer_allocator = i::FLAG_mock_arraybuffer_allocator;
4662   options.mock_arraybuffer_allocator_limit =
4663       i::FLAG_mock_arraybuffer_allocator_limit;
4664 #if MULTI_MAPPED_ALLOCATOR_AVAILABLE
4665   options.multi_mapped_mock_allocator = i::FLAG_multi_mapped_mock_allocator;
4666 #endif
4667 
4668   if (i::FLAG_stress_snapshot && options.expose_fast_api &&
4669       check_d8_flag_contradictions) {
4670     FATAL("Flag --expose-fast-api is incompatible with --stress-snapshot.");
4671   }
4672 
4673   // Set up isolated source groups.
4674   options.isolate_sources = new SourceGroup[options.num_isolates];
4675   SourceGroup* current = options.isolate_sources;
4676   current->Begin(argv, 1);
4677   for (int i = 1; i < argc; i++) {
4678     const char* str = argv[i];
4679     if (strcmp(str, "--isolate") == 0) {
4680       current->End(i);
4681       current++;
4682       current->Begin(argv, i + 1);
4683     } else if (strcmp(str, "--module") == 0 ||
4684                strcmp(str, "--web-snapshot") == 0 ||
4685                strcmp(str, "--json") == 0) {
4686       // Pass on to SourceGroup, which understands these options.
4687     } else if (strncmp(str, "--", 2) == 0) {
4688       if (!i::FLAG_correctness_fuzzer_suppressions) {
4689         printf("Warning: unknown flag %s.\nTry --help for options\n", str);
4690       }
4691     } else if (strcmp(str, "-e") == 0 && i + 1 < argc) {
4692       set_script_executed();
4693     } else if (strncmp(str, "-", 1) != 0) {
4694       // Not a flag, so it must be a script to execute.
4695       set_script_executed();
4696     }
4697   }
4698   current->End(argc);
4699 
4700   if (!logfile_per_isolate && options.num_isolates) {
4701     V8::SetFlagsFromString("--no-logfile-per-isolate");
4702   }
4703 
4704   return true;
4705 }
4706 
RunMain(Isolate * isolate,bool last_run)4707 int Shell::RunMain(Isolate* isolate, bool last_run) {
4708   for (int i = 1; i < options.num_isolates; ++i) {
4709     options.isolate_sources[i].StartExecuteInThread();
4710   }
4711   bool success = true;
4712   {
4713     SetWaitUntilDone(isolate, false);
4714     if (options.lcov_file) {
4715       debug::Coverage::SelectMode(isolate, debug::CoverageMode::kBlockCount);
4716     }
4717     HandleScope scope(isolate);
4718     Local<Context> context = CreateEvaluationContext(isolate);
4719     CreateSnapshotTemplate(isolate);
4720     bool use_existing_context = last_run && use_interactive_shell();
4721     if (use_existing_context) {
4722       // Keep using the same context in the interactive shell.
4723       evaluation_context_.Reset(isolate, context);
4724     }
4725     {
4726       Context::Scope cscope(context);
4727       InspectorClient inspector_client(context, options.enable_inspector);
4728       PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
4729       if (!options.isolate_sources[0].Execute(isolate)) success = false;
4730       if (!CompleteMessageLoop(isolate)) success = false;
4731     }
4732     if (!use_existing_context) {
4733       DisposeModuleEmbedderData(context);
4734     }
4735     WriteLcovData(isolate, options.lcov_file);
4736     if (last_run && i::FLAG_stress_snapshot) {
4737       static constexpr bool kClearRecompilableData = true;
4738       i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
4739       i::Handle<i::Context> i_context = Utils::OpenHandle(*context);
4740       // TODO(jgruber,v8:10500): Don't deoptimize once we support serialization
4741       // of optimized code.
4742       i::Deoptimizer::DeoptimizeAll(i_isolate);
4743       i::Snapshot::ClearReconstructableDataForSerialization(
4744           i_isolate, kClearRecompilableData);
4745       i::Snapshot::SerializeDeserializeAndVerifyForTesting(i_isolate,
4746                                                            i_context);
4747     }
4748   }
4749   CollectGarbage(isolate);
4750 
4751   // Park the main thread here to prevent deadlocks in shared GCs when waiting
4752   // in JoinThread.
4753   i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
4754   i::ParkedScope parked(i_isolate->main_thread_local_isolate());
4755 
4756   for (int i = 1; i < options.num_isolates; ++i) {
4757     if (last_run) {
4758       options.isolate_sources[i].JoinThread();
4759     } else {
4760       options.isolate_sources[i].WaitForThread();
4761     }
4762   }
4763   WaitForRunningWorkers();
4764   if (Shell::unhandled_promise_rejections_.load() > 0) {
4765     printf("%i pending unhandled Promise rejection(s) detected.\n",
4766            Shell::unhandled_promise_rejections_.load());
4767     success = false;
4768     // RunMain may be executed multiple times, e.g. in REPRL mode, so we have to
4769     // reset this counter.
4770     Shell::unhandled_promise_rejections_.store(0);
4771   }
4772   // In order to finish successfully, success must be != expected_to_throw.
4773   if (Shell::options.no_fail) return 0;
4774   return (success == Shell::options.expected_to_throw ? 1 : 0);
4775 }
4776 
CollectGarbage(Isolate * isolate)4777 void Shell::CollectGarbage(Isolate* isolate) {
4778   if (options.send_idle_notification) {
4779     const double kLongIdlePauseInSeconds = 1.0;
4780     isolate->ContextDisposedNotification();
4781     isolate->IdleNotificationDeadline(
4782         g_platform->MonotonicallyIncreasingTime() + kLongIdlePauseInSeconds);
4783   }
4784   if (options.invoke_weak_callbacks) {
4785     // By sending a low memory notifications, we will try hard to collect all
4786     // garbage and will therefore also invoke all weak callbacks of actually
4787     // unreachable persistent handles.
4788     isolate->LowMemoryNotification();
4789   }
4790 }
4791 
SetWaitUntilDone(Isolate * isolate,bool value)4792 void Shell::SetWaitUntilDone(Isolate* isolate, bool value) {
4793   base::MutexGuard guard(isolate_status_lock_.Pointer());
4794   isolate_status_[isolate] = value;
4795 }
4796 
NotifyStartStreamingTask(Isolate * isolate)4797 void Shell::NotifyStartStreamingTask(Isolate* isolate) {
4798   DCHECK(options.streaming_compile);
4799   base::MutexGuard guard(isolate_status_lock_.Pointer());
4800   ++isolate_running_streaming_tasks_[isolate];
4801 }
4802 
NotifyFinishStreamingTask(Isolate * isolate)4803 void Shell::NotifyFinishStreamingTask(Isolate* isolate) {
4804   DCHECK(options.streaming_compile);
4805   base::MutexGuard guard(isolate_status_lock_.Pointer());
4806   --isolate_running_streaming_tasks_[isolate];
4807   DCHECK_GE(isolate_running_streaming_tasks_[isolate], 0);
4808 }
4809 
4810 namespace {
RunSetTimeoutCallback(Isolate * isolate,bool * did_run)4811 bool RunSetTimeoutCallback(Isolate* isolate, bool* did_run) {
4812   PerIsolateData* data = PerIsolateData::Get(isolate);
4813   HandleScope handle_scope(isolate);
4814   Local<Function> callback;
4815   if (!data->GetTimeoutCallback().ToLocal(&callback)) return true;
4816   Local<Context> context;
4817   if (!data->GetTimeoutContext().ToLocal(&context)) return true;
4818   TryCatch try_catch(isolate);
4819   try_catch.SetVerbose(true);
4820   Context::Scope context_scope(context);
4821   if (callback->Call(context, Undefined(isolate), 0, nullptr).IsEmpty()) {
4822     return false;
4823   }
4824   *did_run = true;
4825   return true;
4826 }
4827 
ProcessMessages(Isolate * isolate,const std::function<platform::MessageLoopBehavior ()> & behavior)4828 bool ProcessMessages(
4829     Isolate* isolate,
4830     const std::function<platform::MessageLoopBehavior()>& behavior) {
4831   while (true) {
4832     i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
4833     i::SaveAndSwitchContext saved_context(i_isolate, i::Context());
4834     SealHandleScope shs(isolate);
4835     for (bool ran_tasks = true; ran_tasks;) {
4836       // Execute one foreground task (if one exists), then microtasks.
4837       ran_tasks = v8::platform::PumpMessageLoop(g_default_platform, isolate,
4838                                                 behavior());
4839       if (ran_tasks) MicrotasksScope::PerformCheckpoint(isolate);
4840 
4841       // In predictable mode we push all background tasks into the foreground
4842       // task queue of the {kProcessGlobalPredictablePlatformWorkerTaskQueue}
4843       // isolate. We execute all background tasks after running one foreground
4844       // task.
4845       if (i::FLAG_verify_predictable) {
4846         while (v8::platform::PumpMessageLoop(
4847             g_default_platform,
4848             kProcessGlobalPredictablePlatformWorkerTaskQueue,
4849             platform::MessageLoopBehavior::kDoNotWait)) {
4850           ran_tasks = true;
4851         }
4852       }
4853     }
4854     if (g_default_platform->IdleTasksEnabled(isolate)) {
4855       v8::platform::RunIdleTasks(g_default_platform, isolate,
4856                                  50.0 / base::Time::kMillisecondsPerSecond);
4857     }
4858     bool ran_set_timeout = false;
4859     if (!RunSetTimeoutCallback(isolate, &ran_set_timeout)) return false;
4860     if (!ran_set_timeout) return true;
4861   }
4862 }
4863 }  // anonymous namespace
4864 
CompleteMessageLoop(Isolate * isolate)4865 bool Shell::CompleteMessageLoop(Isolate* isolate) {
4866   auto get_waiting_behaviour = [isolate]() {
4867     base::MutexGuard guard(isolate_status_lock_.Pointer());
4868     DCHECK_GT(isolate_status_.count(isolate), 0);
4869     bool should_wait = (options.wait_for_background_tasks &&
4870                         isolate->HasPendingBackgroundTasks()) ||
4871                        isolate_status_[isolate] ||
4872                        isolate_running_streaming_tasks_[isolate] > 0;
4873     return should_wait ? platform::MessageLoopBehavior::kWaitForWork
4874                        : platform::MessageLoopBehavior::kDoNotWait;
4875   };
4876   if (i::FLAG_verify_predictable) {
4877     bool ran_tasks = ProcessMessages(
4878         isolate, [] { return platform::MessageLoopBehavior::kDoNotWait; });
4879     if (get_waiting_behaviour() ==
4880         platform::MessageLoopBehavior::kWaitForWork) {
4881       FATAL(
4882           "There is outstanding work after executing all tasks in predictable "
4883           "mode -- this would deadlock.");
4884     }
4885     return ran_tasks;
4886   }
4887   return ProcessMessages(isolate, get_waiting_behaviour);
4888 }
4889 
EmptyMessageQueues(Isolate * isolate)4890 bool Shell::EmptyMessageQueues(Isolate* isolate) {
4891   return ProcessMessages(
4892       isolate, []() { return platform::MessageLoopBehavior::kDoNotWait; });
4893 }
4894 
PostForegroundTask(Isolate * isolate,std::unique_ptr<Task> task)4895 void Shell::PostForegroundTask(Isolate* isolate, std::unique_ptr<Task> task) {
4896   g_default_platform->GetForegroundTaskRunner(isolate)->PostTask(
4897       std::move(task));
4898 }
4899 
PostBlockingBackgroundTask(std::unique_ptr<Task> task)4900 void Shell::PostBlockingBackgroundTask(std::unique_ptr<Task> task) {
4901   g_default_platform->CallBlockingTaskOnWorkerThread(std::move(task));
4902 }
4903 
HandleUnhandledPromiseRejections(Isolate * isolate)4904 bool Shell::HandleUnhandledPromiseRejections(Isolate* isolate) {
4905   if (options.ignore_unhandled_promises) return true;
4906   PerIsolateData* data = PerIsolateData::Get(isolate);
4907   int count = data->HandleUnhandledPromiseRejections();
4908   Shell::unhandled_promise_rejections_.store(
4909       Shell::unhandled_promise_rejections_.load() + count);
4910   return count == 0;
4911 }
4912 
4913 class Serializer : public ValueSerializer::Delegate {
4914  public:
Serializer(Isolate * isolate)4915   explicit Serializer(Isolate* isolate)
4916       : isolate_(isolate),
4917         serializer_(isolate, this),
4918         current_memory_usage_(0) {}
4919 
4920   Serializer(const Serializer&) = delete;
4921   Serializer& operator=(const Serializer&) = delete;
4922 
WriteValue(Local<Context> context,Local<Value> value,Local<Value> transfer)4923   Maybe<bool> WriteValue(Local<Context> context, Local<Value> value,
4924                          Local<Value> transfer) {
4925     bool ok;
4926     DCHECK(!data_);
4927     data_.reset(new SerializationData);
4928     if (!PrepareTransfer(context, transfer).To(&ok)) {
4929       return Nothing<bool>();
4930     }
4931     serializer_.WriteHeader();
4932 
4933     if (!serializer_.WriteValue(context, value).To(&ok)) {
4934       data_.reset();
4935       return Nothing<bool>();
4936     }
4937 
4938     if (!FinalizeTransfer().To(&ok)) {
4939       return Nothing<bool>();
4940     }
4941 
4942     std::pair<uint8_t*, size_t> pair = serializer_.Release();
4943     data_->data_.reset(pair.first);
4944     data_->size_ = pair.second;
4945     return Just(true);
4946   }
4947 
Release()4948   std::unique_ptr<SerializationData> Release() { return std::move(data_); }
4949 
AppendBackingStoresTo(std::vector<std::shared_ptr<BackingStore>> * to)4950   void AppendBackingStoresTo(std::vector<std::shared_ptr<BackingStore>>* to) {
4951     to->insert(to->end(), std::make_move_iterator(backing_stores_.begin()),
4952                std::make_move_iterator(backing_stores_.end()));
4953     backing_stores_.clear();
4954   }
4955 
4956  protected:
4957   // Implements ValueSerializer::Delegate.
ThrowDataCloneError(Local<String> message)4958   void ThrowDataCloneError(Local<String> message) override {
4959     isolate_->ThrowException(Exception::Error(message));
4960   }
4961 
GetSharedArrayBufferId(Isolate * isolate,Local<SharedArrayBuffer> shared_array_buffer)4962   Maybe<uint32_t> GetSharedArrayBufferId(
4963       Isolate* isolate, Local<SharedArrayBuffer> shared_array_buffer) override {
4964     DCHECK_NOT_NULL(data_);
4965     for (size_t index = 0; index < shared_array_buffers_.size(); ++index) {
4966       if (shared_array_buffers_[index] == shared_array_buffer) {
4967         return Just<uint32_t>(static_cast<uint32_t>(index));
4968       }
4969     }
4970 
4971     size_t index = shared_array_buffers_.size();
4972     shared_array_buffers_.emplace_back(isolate_, shared_array_buffer);
4973     data_->sab_backing_stores_.push_back(
4974         shared_array_buffer->GetBackingStore());
4975     return Just<uint32_t>(static_cast<uint32_t>(index));
4976   }
4977 
GetWasmModuleTransferId(Isolate * isolate,Local<WasmModuleObject> module)4978   Maybe<uint32_t> GetWasmModuleTransferId(
4979       Isolate* isolate, Local<WasmModuleObject> module) override {
4980     DCHECK_NOT_NULL(data_);
4981     for (size_t index = 0; index < wasm_modules_.size(); ++index) {
4982       if (wasm_modules_[index] == module) {
4983         return Just<uint32_t>(static_cast<uint32_t>(index));
4984       }
4985     }
4986 
4987     size_t index = wasm_modules_.size();
4988     wasm_modules_.emplace_back(isolate_, module);
4989     data_->compiled_wasm_modules_.push_back(module->GetCompiledModule());
4990     return Just<uint32_t>(static_cast<uint32_t>(index));
4991   }
4992 
ReallocateBufferMemory(void * old_buffer,size_t size,size_t * actual_size)4993   void* ReallocateBufferMemory(void* old_buffer, size_t size,
4994                                size_t* actual_size) override {
4995     // Not accurate, because we don't take into account reallocated buffers,
4996     // but this is fine for testing.
4997     current_memory_usage_ += size;
4998     if (current_memory_usage_ > kMaxSerializerMemoryUsage) return nullptr;
4999 
5000     void* result = base::Realloc(old_buffer, size);
5001     *actual_size = result ? size : 0;
5002     return result;
5003   }
5004 
FreeBufferMemory(void * buffer)5005   void FreeBufferMemory(void* buffer) override { base::Free(buffer); }
5006 
SupportsSharedValues() const5007   bool SupportsSharedValues() const override { return true; }
5008 
GetSharedValueId(Isolate * isolate,Local<Value> shared_value)5009   Maybe<uint32_t> GetSharedValueId(Isolate* isolate,
5010                                    Local<Value> shared_value) override {
5011     DCHECK_NOT_NULL(data_);
5012     for (size_t index = 0; index < data_->shared_values_.size(); ++index) {
5013       if (data_->shared_values_[index] == shared_value) {
5014         return Just<uint32_t>(static_cast<uint32_t>(index));
5015       }
5016     }
5017 
5018     size_t index = data_->shared_values_.size();
5019     // Shared values in transit are kept alive by global handles in the shared
5020     // isolate. No code ever runs in the shared Isolate, so locking it does not
5021     // contend with long-running tasks.
5022     {
5023       DCHECK_EQ(reinterpret_cast<i::Isolate*>(isolate)->shared_isolate(),
5024                 reinterpret_cast<i::Isolate*>(Shell::shared_isolate));
5025       v8::Locker locker(Shell::shared_isolate);
5026       data_->shared_values_.emplace_back(Shell::shared_isolate, shared_value);
5027     }
5028     return Just<uint32_t>(static_cast<uint32_t>(index));
5029   }
5030 
5031  private:
PrepareTransfer(Local<Context> context,Local<Value> transfer)5032   Maybe<bool> PrepareTransfer(Local<Context> context, Local<Value> transfer) {
5033     if (transfer->IsArray()) {
5034       Local<Array> transfer_array = transfer.As<Array>();
5035       uint32_t length = transfer_array->Length();
5036       for (uint32_t i = 0; i < length; ++i) {
5037         Local<Value> element;
5038         if (transfer_array->Get(context, i).ToLocal(&element)) {
5039           if (!element->IsArrayBuffer()) {
5040             isolate_->ThrowError(
5041                 "Transfer array elements must be an ArrayBuffer");
5042             return Nothing<bool>();
5043           }
5044 
5045           Local<ArrayBuffer> array_buffer = element.As<ArrayBuffer>();
5046 
5047           if (std::find(array_buffers_.begin(), array_buffers_.end(),
5048                         array_buffer) != array_buffers_.end()) {
5049             isolate_->ThrowError(
5050                 "ArrayBuffer occurs in the transfer array more than once");
5051             return Nothing<bool>();
5052           }
5053 
5054           serializer_.TransferArrayBuffer(
5055               static_cast<uint32_t>(array_buffers_.size()), array_buffer);
5056           array_buffers_.emplace_back(isolate_, array_buffer);
5057         } else {
5058           return Nothing<bool>();
5059         }
5060       }
5061       return Just(true);
5062     } else if (transfer->IsUndefined()) {
5063       return Just(true);
5064     } else {
5065       isolate_->ThrowError("Transfer list must be an Array or undefined");
5066       return Nothing<bool>();
5067     }
5068   }
5069 
FinalizeTransfer()5070   Maybe<bool> FinalizeTransfer() {
5071     for (const auto& global_array_buffer : array_buffers_) {
5072       Local<ArrayBuffer> array_buffer =
5073           Local<ArrayBuffer>::New(isolate_, global_array_buffer);
5074       if (!array_buffer->IsDetachable()) {
5075         isolate_->ThrowError(
5076             "ArrayBuffer is not detachable and could not be transferred");
5077         return Nothing<bool>();
5078       }
5079 
5080       auto backing_store = array_buffer->GetBackingStore();
5081       data_->backing_stores_.push_back(std::move(backing_store));
5082       array_buffer->Detach();
5083     }
5084 
5085     return Just(true);
5086   }
5087 
5088   Isolate* isolate_;
5089   ValueSerializer serializer_;
5090   std::unique_ptr<SerializationData> data_;
5091   std::vector<Global<ArrayBuffer>> array_buffers_;
5092   std::vector<Global<SharedArrayBuffer>> shared_array_buffers_;
5093   std::vector<Global<WasmModuleObject>> wasm_modules_;
5094   std::vector<std::shared_ptr<v8::BackingStore>> backing_stores_;
5095   size_t current_memory_usage_;
5096 };
5097 
ClearSharedValuesUnderLockIfNeeded()5098 void SerializationData::ClearSharedValuesUnderLockIfNeeded() {
5099   if (shared_values_.empty()) return;
5100   v8::Locker locker(Shell::shared_isolate);
5101   shared_values_.clear();
5102 }
5103 
5104 class Deserializer : public ValueDeserializer::Delegate {
5105  public:
Deserializer(Isolate * isolate,std::unique_ptr<SerializationData> data)5106   Deserializer(Isolate* isolate, std::unique_ptr<SerializationData> data)
5107       : isolate_(isolate),
5108         deserializer_(isolate, data->data(), data->size(), this),
5109         data_(std::move(data)) {
5110     deserializer_.SetSupportsLegacyWireFormat(true);
5111   }
5112 
~Deserializer()5113   ~Deserializer() {
5114     DCHECK_EQ(reinterpret_cast<i::Isolate*>(isolate_)->shared_isolate(),
5115               reinterpret_cast<i::Isolate*>(Shell::shared_isolate));
5116     data_->ClearSharedValuesUnderLockIfNeeded();
5117   }
5118 
5119   Deserializer(const Deserializer&) = delete;
5120   Deserializer& operator=(const Deserializer&) = delete;
5121 
ReadValue(Local<Context> context)5122   MaybeLocal<Value> ReadValue(Local<Context> context) {
5123     bool read_header;
5124     if (!deserializer_.ReadHeader(context).To(&read_header)) {
5125       return MaybeLocal<Value>();
5126     }
5127 
5128     uint32_t index = 0;
5129     for (const auto& backing_store : data_->backing_stores()) {
5130       Local<ArrayBuffer> array_buffer =
5131           ArrayBuffer::New(isolate_, std::move(backing_store));
5132       deserializer_.TransferArrayBuffer(index++, array_buffer);
5133     }
5134 
5135     return deserializer_.ReadValue(context);
5136   }
5137 
GetSharedArrayBufferFromId(Isolate * isolate,uint32_t clone_id)5138   MaybeLocal<SharedArrayBuffer> GetSharedArrayBufferFromId(
5139       Isolate* isolate, uint32_t clone_id) override {
5140     DCHECK_NOT_NULL(data_);
5141     if (clone_id < data_->sab_backing_stores().size()) {
5142       return SharedArrayBuffer::New(
5143           isolate_, std::move(data_->sab_backing_stores().at(clone_id)));
5144     }
5145     return MaybeLocal<SharedArrayBuffer>();
5146   }
5147 
GetWasmModuleFromId(Isolate * isolate,uint32_t transfer_id)5148   MaybeLocal<WasmModuleObject> GetWasmModuleFromId(
5149       Isolate* isolate, uint32_t transfer_id) override {
5150     DCHECK_NOT_NULL(data_);
5151     if (transfer_id >= data_->compiled_wasm_modules().size()) return {};
5152     return WasmModuleObject::FromCompiledModule(
5153         isolate_, data_->compiled_wasm_modules().at(transfer_id));
5154   }
5155 
SupportsSharedValues() const5156   bool SupportsSharedValues() const override { return true; }
5157 
GetSharedValueFromId(Isolate * isolate,uint32_t id)5158   MaybeLocal<Value> GetSharedValueFromId(Isolate* isolate,
5159                                          uint32_t id) override {
5160     DCHECK_NOT_NULL(data_);
5161     if (id < data_->shared_values().size()) {
5162       return data_->shared_values().at(id).Get(isolate);
5163     }
5164     return MaybeLocal<Value>();
5165   }
5166 
5167  private:
5168   Isolate* isolate_;
5169   ValueDeserializer deserializer_;
5170   std::unique_ptr<SerializationData> data_;
5171 };
5172 
5173 class D8Testing {
5174  public:
5175   /**
5176    * Get the number of runs of a given test that is required to get the full
5177    * stress coverage.
5178    */
GetStressRuns()5179   static int GetStressRuns() {
5180     if (internal::FLAG_stress_runs != 0) return internal::FLAG_stress_runs;
5181 #ifdef DEBUG
5182     // In debug mode the code runs much slower so stressing will only make two
5183     // runs.
5184     return 2;
5185 #else
5186     return 5;
5187 #endif
5188   }
5189 
5190   /**
5191    * Indicate the number of the run which is about to start. The value of run
5192    * should be between 0 and one less than the result from GetStressRuns()
5193    */
PrepareStressRun(int run)5194   static void PrepareStressRun(int run) {
5195     static const char* kLazyOptimizations =
5196         "--prepare-always-opt "
5197         "--max-inlined-bytecode-size=999999 "
5198         "--max-inlined-bytecode-size-cumulative=999999 "
5199         "--noalways-opt";
5200 
5201     if (run == 0) {
5202       V8::SetFlagsFromString(kLazyOptimizations);
5203     } else if (run == GetStressRuns() - 1) {
5204       i::FLAG_always_opt = true;
5205     }
5206   }
5207 
5208   /**
5209    * Force deoptimization of all functions.
5210    */
DeoptimizeAll(Isolate * isolate)5211   static void DeoptimizeAll(Isolate* isolate) {
5212     i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
5213     i::HandleScope scope(i_isolate);
5214     i::Deoptimizer::DeoptimizeAll(i_isolate);
5215   }
5216 };
5217 
SerializeValue(Isolate * isolate,Local<Value> value,Local<Value> transfer)5218 std::unique_ptr<SerializationData> Shell::SerializeValue(
5219     Isolate* isolate, Local<Value> value, Local<Value> transfer) {
5220   bool ok;
5221   Local<Context> context = isolate->GetCurrentContext();
5222   Serializer serializer(isolate);
5223   std::unique_ptr<SerializationData> data;
5224   if (serializer.WriteValue(context, value, transfer).To(&ok)) {
5225     data = serializer.Release();
5226   }
5227   return data;
5228 }
5229 
DeserializeValue(Isolate * isolate,std::unique_ptr<SerializationData> data)5230 MaybeLocal<Value> Shell::DeserializeValue(
5231     Isolate* isolate, std::unique_ptr<SerializationData> data) {
5232   Local<Value> value;
5233   Local<Context> context = isolate->GetCurrentContext();
5234   Deserializer deserializer(isolate, std::move(data));
5235   return deserializer.ReadValue(context);
5236 }
5237 
AddRunningWorker(std::shared_ptr<Worker> worker)5238 void Shell::AddRunningWorker(std::shared_ptr<Worker> worker) {
5239   workers_mutex_.Pointer()->AssertHeld();  // caller should hold the mutex.
5240   running_workers_.insert(worker);
5241 }
5242 
RemoveRunningWorker(const std::shared_ptr<Worker> & worker)5243 void Shell::RemoveRunningWorker(const std::shared_ptr<Worker>& worker) {
5244   base::MutexGuard lock_guard(workers_mutex_.Pointer());
5245   auto it = running_workers_.find(worker);
5246   if (it != running_workers_.end()) running_workers_.erase(it);
5247 }
5248 
WaitForRunningWorkers()5249 void Shell::WaitForRunningWorkers() {
5250   // Make a copy of running_workers_, because we don't want to call
5251   // Worker::Terminate while holding the workers_mutex_ lock. Otherwise, if a
5252   // worker is about to create a new Worker, it would deadlock.
5253   std::unordered_set<std::shared_ptr<Worker>> workers_copy;
5254   {
5255     base::MutexGuard lock_guard(workers_mutex_.Pointer());
5256     allow_new_workers_ = false;
5257     workers_copy.swap(running_workers_);
5258   }
5259 
5260   for (auto& worker : workers_copy) {
5261     worker->TerminateAndWaitForThread();
5262   }
5263 
5264   // Now that all workers are terminated, we can re-enable Worker creation.
5265   base::MutexGuard lock_guard(workers_mutex_.Pointer());
5266   DCHECK(running_workers_.empty());
5267   allow_new_workers_ = true;
5268 }
5269 
5270 namespace {
5271 
HasFlagThatRequiresSharedIsolate()5272 bool HasFlagThatRequiresSharedIsolate() {
5273   return i::FLAG_shared_string_table || i::FLAG_harmony_struct;
5274 }
5275 
5276 }  // namespace
5277 
Main(int argc,char * argv[])5278 int Shell::Main(int argc, char* argv[]) {
5279   v8::base::EnsureConsoleOutput();
5280   if (!SetOptions(argc, argv)) return 1;
5281 
5282   v8::V8::InitializeICUDefaultLocation(argv[0], options.icu_data_file);
5283 
5284 #ifdef V8_INTL_SUPPORT
5285   if (options.icu_locale != nullptr) {
5286     icu::Locale locale(options.icu_locale);
5287     UErrorCode error_code = U_ZERO_ERROR;
5288     icu::Locale::setDefault(locale, error_code);
5289   }
5290 #endif  // V8_INTL_SUPPORT
5291 
5292   v8::platform::InProcessStackDumping in_process_stack_dumping =
5293       options.disable_in_process_stack_traces
5294           ? v8::platform::InProcessStackDumping::kDisabled
5295           : v8::platform::InProcessStackDumping::kEnabled;
5296 
5297   std::ofstream trace_file;
5298   std::unique_ptr<platform::tracing::TracingController> tracing;
5299   if (options.trace_enabled && !i::FLAG_verify_predictable) {
5300     tracing = std::make_unique<platform::tracing::TracingController>();
5301 
5302     if (!options.enable_system_instrumentation) {
5303       const char* trace_path =
5304           options.trace_path ? options.trace_path : "v8_trace.json";
5305       trace_file.open(trace_path);
5306       if (!trace_file.good()) {
5307         printf("Cannot open trace file '%s' for writing: %s.\n", trace_path,
5308                strerror(errno));
5309         return 1;
5310       }
5311     }
5312 
5313 #ifdef V8_USE_PERFETTO
5314     // Set up the in-process backend that the tracing controller will connect
5315     // to.
5316     perfetto::TracingInitArgs init_args;
5317     init_args.backends = perfetto::BackendType::kInProcessBackend;
5318     perfetto::Tracing::Initialize(init_args);
5319 
5320     tracing->InitializeForPerfetto(&trace_file);
5321 #else
5322     platform::tracing::TraceBuffer* trace_buffer = nullptr;
5323 #if defined(V8_ENABLE_SYSTEM_INSTRUMENTATION)
5324     if (options.enable_system_instrumentation) {
5325       trace_buffer =
5326           platform::tracing::TraceBuffer::CreateTraceBufferRingBuffer(
5327               platform::tracing::TraceBuffer::kRingBufferChunks,
5328               platform::tracing::TraceWriter::
5329                   CreateSystemInstrumentationTraceWriter());
5330     }
5331 #endif  // V8_ENABLE_SYSTEM_INSTRUMENTATION
5332     if (!trace_buffer) {
5333       trace_buffer =
5334           platform::tracing::TraceBuffer::CreateTraceBufferRingBuffer(
5335               platform::tracing::TraceBuffer::kRingBufferChunks,
5336               platform::tracing::TraceWriter::CreateJSONTraceWriter(
5337                   trace_file));
5338     }
5339     tracing->Initialize(trace_buffer);
5340 #endif  // V8_USE_PERFETTO
5341   }
5342 
5343   platform::tracing::TracingController* tracing_controller = tracing.get();
5344   g_platform = v8::platform::NewDefaultPlatform(
5345       options.thread_pool_size, v8::platform::IdleTaskSupport::kEnabled,
5346       in_process_stack_dumping, std::move(tracing));
5347   g_default_platform = g_platform.get();
5348   if (i::FLAG_predictable) {
5349     g_platform = MakePredictablePlatform(std::move(g_platform));
5350   }
5351   if (options.stress_delay_tasks) {
5352     int64_t random_seed = i::FLAG_fuzzer_random_seed;
5353     if (!random_seed) random_seed = i::FLAG_random_seed;
5354     // If random_seed is still 0 here, the {DelayedTasksPlatform} will choose a
5355     // random seed.
5356     g_platform = MakeDelayedTasksPlatform(std::move(g_platform), random_seed);
5357   }
5358 
5359   if (i::FLAG_trace_turbo_cfg_file == nullptr) {
5360     V8::SetFlagsFromString("--trace-turbo-cfg-file=turbo.cfg");
5361   }
5362   if (i::FLAG_redirect_code_traces_to == nullptr) {
5363     V8::SetFlagsFromString("--redirect-code-traces-to=code.asm");
5364   }
5365   v8::V8::InitializePlatform(g_platform.get());
5366 #ifdef V8_SANDBOX
5367   if (!v8::V8::InitializeSandbox()) {
5368     FATAL("Could not initialize the sandbox");
5369   }
5370 #endif
5371   v8::V8::Initialize();
5372   if (options.snapshot_blob) {
5373     v8::V8::InitializeExternalStartupDataFromFile(options.snapshot_blob);
5374   } else {
5375     v8::V8::InitializeExternalStartupData(argv[0]);
5376   }
5377   int result = 0;
5378   Isolate::CreateParams create_params;
5379   ShellArrayBufferAllocator shell_array_buffer_allocator;
5380   MockArrayBufferAllocator mock_arraybuffer_allocator;
5381   const size_t memory_limit =
5382       options.mock_arraybuffer_allocator_limit * options.num_isolates;
5383   MockArrayBufferAllocatiorWithLimit mock_arraybuffer_allocator_with_limit(
5384       memory_limit >= options.mock_arraybuffer_allocator_limit
5385           ? memory_limit
5386           : std::numeric_limits<size_t>::max());
5387 #if MULTI_MAPPED_ALLOCATOR_AVAILABLE
5388   MultiMappedAllocator multi_mapped_mock_allocator;
5389 #endif
5390   if (options.mock_arraybuffer_allocator) {
5391     if (memory_limit) {
5392       Shell::array_buffer_allocator = &mock_arraybuffer_allocator_with_limit;
5393     } else {
5394       Shell::array_buffer_allocator = &mock_arraybuffer_allocator;
5395     }
5396 #if MULTI_MAPPED_ALLOCATOR_AVAILABLE
5397   } else if (options.multi_mapped_mock_allocator) {
5398     Shell::array_buffer_allocator = &multi_mapped_mock_allocator;
5399 #endif
5400   } else {
5401     Shell::array_buffer_allocator = &shell_array_buffer_allocator;
5402   }
5403   create_params.array_buffer_allocator = Shell::array_buffer_allocator;
5404 #ifdef ENABLE_VTUNE_JIT_INTERFACE
5405   create_params.code_event_handler = vTune::GetVtuneCodeEventHandler();
5406 #endif
5407   create_params.constraints.ConfigureDefaults(
5408       base::SysInfo::AmountOfPhysicalMemory(),
5409       base::SysInfo::AmountOfVirtualMemory());
5410 
5411   Shell::counter_map_ = new CounterMap();
5412   if (options.dump_counters || options.dump_counters_nvp ||
5413       i::TracingFlags::is_gc_stats_enabled()) {
5414     create_params.counter_lookup_callback = LookupCounter;
5415     create_params.create_histogram_callback = CreateHistogram;
5416     create_params.add_histogram_sample_callback = AddHistogramSample;
5417   }
5418 
5419 #if V8_ENABLE_WEBASSEMBLY
5420   if (V8_TRAP_HANDLER_SUPPORTED && options.wasm_trap_handler) {
5421     constexpr bool kUseDefaultTrapHandler = true;
5422     if (!v8::V8::EnableWebAssemblyTrapHandler(kUseDefaultTrapHandler)) {
5423       FATAL("Could not register trap handler");
5424     }
5425   }
5426 #endif  // V8_ENABLE_WEBASSEMBLY
5427 
5428   if (HasFlagThatRequiresSharedIsolate()) {
5429     Isolate::CreateParams shared_create_params;
5430     shared_create_params.constraints.ConfigureDefaults(
5431         base::SysInfo::AmountOfPhysicalMemory(),
5432         base::SysInfo::AmountOfVirtualMemory());
5433     shared_create_params.array_buffer_allocator = Shell::array_buffer_allocator;
5434     shared_isolate =
5435         reinterpret_cast<Isolate*>(i::Isolate::NewShared(shared_create_params));
5436     create_params.experimental_attach_to_shared_isolate = shared_isolate;
5437   }
5438 
5439   Isolate* isolate = Isolate::New(create_params);
5440 
5441   {
5442     D8Console console(isolate);
5443     Isolate::Scope scope(isolate);
5444     Initialize(isolate, &console);
5445     PerIsolateData data(isolate);
5446 
5447     // Fuzzilli REPRL = read-eval-print-loop
5448     do {
5449 #ifdef V8_FUZZILLI
5450       if (fuzzilli_reprl) {
5451         unsigned action = 0;
5452         ssize_t nread = read(REPRL_CRFD, &action, 4);
5453         if (nread != 4 || action != 'cexe') {
5454           fprintf(stderr, "Unknown action: %u\n", action);
5455           _exit(-1);
5456         }
5457       }
5458 #endif  // V8_FUZZILLI
5459 
5460       result = 0;
5461 
5462       if (options.trace_enabled) {
5463         platform::tracing::TraceConfig* trace_config;
5464         if (options.trace_config) {
5465           int size = 0;
5466           char* trace_config_json_str = ReadChars(options.trace_config, &size);
5467           trace_config = tracing::CreateTraceConfigFromJSON(
5468               isolate, trace_config_json_str);
5469           delete[] trace_config_json_str;
5470         } else {
5471           trace_config =
5472               platform::tracing::TraceConfig::CreateDefaultTraceConfig();
5473           if (options.enable_system_instrumentation) {
5474             trace_config->AddIncludedCategory("disabled-by-default-v8.compile");
5475           }
5476         }
5477         tracing_controller->StartTracing(trace_config);
5478       }
5479 
5480       CpuProfiler* cpu_profiler;
5481       if (options.cpu_profiler) {
5482         cpu_profiler = CpuProfiler::New(isolate);
5483         CpuProfilingOptions profile_options;
5484         cpu_profiler->StartProfiling(String::Empty(isolate), profile_options);
5485       }
5486 
5487       if (options.stress_opt) {
5488         options.stress_runs = D8Testing::GetStressRuns();
5489         for (int i = 0; i < options.stress_runs && result == 0; i++) {
5490           printf("============ Stress %d/%d ============\n", i + 1,
5491                  options.stress_runs.get());
5492           D8Testing::PrepareStressRun(i);
5493           bool last_run = i == options.stress_runs - 1;
5494           result = RunMain(isolate, last_run);
5495         }
5496         printf("======== Full Deoptimization =======\n");
5497         D8Testing::DeoptimizeAll(isolate);
5498       } else if (i::FLAG_stress_runs > 0) {
5499         options.stress_runs = i::FLAG_stress_runs;
5500         for (int i = 0; i < options.stress_runs && result == 0; i++) {
5501           printf("============ Run %d/%d ============\n", i + 1,
5502                  options.stress_runs.get());
5503           bool last_run = i == options.stress_runs - 1;
5504           result = RunMain(isolate, last_run);
5505         }
5506       } else if (options.code_cache_options !=
5507                  ShellOptions::CodeCacheOptions::kNoProduceCache) {
5508         {
5509           // Park the main thread here in case the new isolate wants to perform
5510           // a shared GC to prevent a deadlock.
5511           i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
5512           i::ParkedScope parked(i_isolate->main_thread_local_isolate());
5513 
5514           printf("============ Run: Produce code cache ============\n");
5515           // First run to produce the cache
5516           Isolate::CreateParams create_params2;
5517           create_params2.array_buffer_allocator = Shell::array_buffer_allocator;
5518           create_params2.experimental_attach_to_shared_isolate =
5519               Shell::shared_isolate;
5520           i::FLAG_hash_seed ^= 1337;  // Use a different hash seed.
5521           Isolate* isolate2 = Isolate::New(create_params2);
5522           i::FLAG_hash_seed ^= 1337;  // Restore old hash seed.
5523           {
5524             D8Console console2(isolate2);
5525             Initialize(isolate2, &console2);
5526             PerIsolateData data2(isolate2);
5527             Isolate::Scope isolate_scope(isolate2);
5528 
5529             result = RunMain(isolate2, false);
5530           }
5531           isolate2->Dispose();
5532         }
5533 
5534         // Change the options to consume cache
5535         DCHECK(options.compile_options == v8::ScriptCompiler::kEagerCompile ||
5536                options.compile_options ==
5537                    v8::ScriptCompiler::kNoCompileOptions);
5538         options.compile_options.Overwrite(
5539             v8::ScriptCompiler::kConsumeCodeCache);
5540         options.code_cache_options.Overwrite(
5541             ShellOptions::CodeCacheOptions::kNoProduceCache);
5542 
5543         printf("============ Run: Consume code cache ============\n");
5544         // Second run to consume the cache in current isolate
5545         result = RunMain(isolate, true);
5546         options.compile_options.Overwrite(
5547             v8::ScriptCompiler::kNoCompileOptions);
5548       } else {
5549         bool last_run = true;
5550         result = RunMain(isolate, last_run);
5551       }
5552 
5553       // Run interactive shell if explicitly requested or if no script has been
5554       // executed, but never on --test
5555       if (use_interactive_shell()) {
5556         RunShell(isolate);
5557       }
5558 
5559       if (i::FLAG_trace_ignition_dispatches_output_file != nullptr) {
5560         WriteIgnitionDispatchCountersFile(isolate);
5561       }
5562 
5563       if (options.cpu_profiler) {
5564         CpuProfile* profile =
5565             cpu_profiler->StopProfiling(String::Empty(isolate));
5566         if (options.cpu_profiler_print) {
5567           const internal::ProfileNode* root =
5568               reinterpret_cast<const internal::ProfileNode*>(
5569                   profile->GetTopDownRoot());
5570           root->Print(0);
5571         }
5572         profile->Delete();
5573         cpu_profiler->Dispose();
5574       }
5575 
5576       // Shut down contexts and collect garbage.
5577       cached_code_map_.clear();
5578       evaluation_context_.Reset();
5579       stringify_function_.Reset();
5580       CollectGarbage(isolate);
5581 #ifdef V8_FUZZILLI
5582       // Send result to parent (fuzzilli) and reset edge guards.
5583       if (fuzzilli_reprl) {
5584         int status = result << 8;
5585         std::vector<bool> bitmap;
5586         if (options.fuzzilli_enable_builtins_coverage) {
5587           bitmap = i::BasicBlockProfiler::Get()->GetCoverageBitmap(
5588               reinterpret_cast<i::Isolate*>(isolate));
5589           cov_update_builtins_basic_block_coverage(bitmap);
5590         }
5591         if (options.fuzzilli_coverage_statistics) {
5592           int tot = 0;
5593           for (bool b : bitmap) {
5594             if (b) tot++;
5595           }
5596           static int iteration_counter = 0;
5597           std::ofstream covlog("covlog.txt", std::ios::app);
5598           covlog << iteration_counter << "\t" << tot << "\t"
5599                  << sanitizer_cov_count_discovered_edges() << "\t"
5600                  << bitmap.size() << std::endl;
5601           iteration_counter++;
5602         }
5603         // In REPRL mode, stdout and stderr can be regular files, so they need
5604         // to be flushed after every execution
5605         fflush(stdout);
5606         fflush(stderr);
5607         CHECK_EQ(write(REPRL_CWFD, &status, 4), 4);
5608         sanitizer_cov_reset_edgeguards();
5609         if (options.fuzzilli_enable_builtins_coverage) {
5610           i::BasicBlockProfiler::Get()->ResetCounts(
5611               reinterpret_cast<i::Isolate*>(isolate));
5612         }
5613       }
5614 #endif  // V8_FUZZILLI
5615     } while (fuzzilli_reprl);
5616   }
5617   OnExit(isolate, true);
5618 
5619   // Delete the platform explicitly here to write the tracing output to the
5620   // tracing file.
5621   if (options.trace_enabled) {
5622     tracing_controller->StopTracing();
5623   }
5624   g_platform.reset();
5625 
5626 #ifdef V8_TARGET_OS_WIN
5627   // We need to free the allocated utf8 filenames in
5628   // PreProcessUnicodeFilenameArg.
5629   for (char* utf8_str : utf8_filenames) {
5630     delete[] utf8_str;
5631   }
5632   utf8_filenames.clear();
5633 #endif
5634 
5635   return result;
5636 }
5637 
5638 }  // namespace v8
5639 
main(int argc,char * argv[])5640 int main(int argc, char* argv[]) { return v8::Shell::Main(argc, argv); }
5641 
5642 #undef CHECK
5643 #undef DCHECK
5644 #undef TRACE_BS
5645