v8.cc 8.85 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/init/v8.h"
6

7 8
#include <fstream>

9
#include "include/cppgc/platform.h"
10
#include "src/api/api.h"
11
#include "src/base/atomicops.h"
12
#include "src/base/once.h"
13
#include "src/base/platform/platform.h"
14 15
#include "src/codegen/cpu-features.h"
#include "src/codegen/interface-descriptors.h"
16
#include "src/debug/debug.h"
17
#include "src/deoptimizer/deoptimizer.h"
18 19 20 21
#include "src/execution/frames.h"
#include "src/execution/isolate.h"
#include "src/execution/runtime-profiler.h"
#include "src/execution/simulator.h"
22
#include "src/init/bootstrapper.h"
23
#include "src/init/vm-cage.h"
24
#include "src/libsampler/sampler.h"
25
#include "src/objects/elements.h"
26
#include "src/objects/objects-inl.h"
27
#include "src/profiler/heap-profiler.h"
28
#include "src/snapshot/snapshot.h"
29
#include "src/tracing/tracing-category-observer.h"
30 31

#if V8_ENABLE_WEBASSEMBLY
32
#include "src/wasm/wasm-engine.h"
33
#endif  // V8_ENABLE_WEBASSEMBLY
34

35
#if defined(V8_OS_WIN) && defined(V8_ENABLE_SYSTEM_INSTRUMENTATION)
36 37 38
#include "src/diagnostics/system-jit-win.h"
#endif

39 40
namespace v8 {
namespace internal {
41

42
V8_DECLARE_ONCE(init_once);
43

44 45 46 47 48
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
V8_DECLARE_ONCE(init_natives_once);
V8_DECLARE_ONCE(init_snapshot_once);
#endif

49
v8::Platform* V8::platform_ = nullptr;
50

51
bool V8::Initialize() {
52
  InitializeOncePerProcess();
53
  return true;
54 55
}

56
void V8::TearDown() {
57
#if V8_ENABLE_WEBASSEMBLY
58
  wasm::WasmEngine::GlobalTearDown();
59
#endif  // V8_ENABLE_WEBASSEMBLY
60 61 62
#if defined(USE_SIMULATOR)
  Simulator::GlobalTearDown();
#endif
63
  CallDescriptors::TearDown();
64
  ElementsAccessor::TearDown();
65
  RegisteredExtension::UnregisterAll();
66
  FlagList::ResetAllFlags();  // Frees memory held by string arguments.
67 68
}

69 70 71 72 73 74 75
#define DISABLE_FLAG(flag)                                                    \
  if (FLAG_##flag) {                                                          \
    PrintF(stderr,                                                            \
           "Warning: disabling flag --" #flag " due to conflicting flags\n"); \
    FLAG_##flag = false;                                                      \
  }

76
void V8::InitializeOncePerProcessImpl() {
77 78 79 80 81 82
  CHECK(platform_);

#ifdef V8_VIRTUAL_MEMORY_CAGE
  if (!GetProcessWideVirtualMemoryCage()->is_initialized()) {
    // For now, we still allow the cage to be disabled even if V8 was compiled
    // with V8_VIRTUAL_MEMORY_CAGE. This will eventually be forbidden.
83
    CHECK(kAllowBackingStoresOutsideCage);
84 85 86 87
    GetProcessWideVirtualMemoryCage()->Disable();
  }
#endif

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  // Update logging information before enforcing flag implications.
  bool* log_all_flags[] = {&FLAG_turbo_profiling_log_builtins,
                           &FLAG_log_all,
                           &FLAG_log_api,
                           &FLAG_log_code,
                           &FLAG_log_code_disassemble,
                           &FLAG_log_handles,
                           &FLAG_log_suspect,
                           &FLAG_log_source_code,
                           &FLAG_log_function_events,
                           &FLAG_log_internal_timer_events,
                           &FLAG_log_deopt,
                           &FLAG_log_ic,
                           &FLAG_log_maps};
  if (FLAG_log_all) {
    // Enable all logging flags
    for (auto* flag : log_all_flags) {
      *flag = true;
    }
    FLAG_log = true;
  } else if (!FLAG_log) {
    // Enable --log if any log flag is set.
    for (const auto* flag : log_all_flags) {
      if (!*flag) continue;
      FLAG_log = true;
      break;
    }
115 116 117
    // Profiling flags depend on logging.
    FLAG_log |= FLAG_perf_prof || FLAG_perf_basic_prof || FLAG_ll_prof ||
                FLAG_prof || FLAG_prof_cpp;
118 119
  }

120
  FlagList::EnforceFlagImplications();
121

122 123 124
  if (FLAG_predictable && FLAG_random_seed == 0) {
    // Avoid random seeds in predictable mode.
    FLAG_random_seed = 12347;
125 126
  }

127 128 129
  if (FLAG_stress_compaction) {
    FLAG_force_marking_deque_overflows = true;
    FLAG_gc_global = true;
130
    FLAG_max_semi_space_size = 1;
131
  }
132

133 134 135 136 137 138
  if (FLAG_trace_turbo) {
    // Create an empty file shared by the process (e.g. the wasm engine).
    std::ofstream(Isolate::GetTurboCfgFileName(nullptr).c_str(),
                  std::ios_base::trunc);
  }

139 140 141 142 143 144 145 146 147 148
  // Do not expose wasm in jitless mode.
  //
  // Even in interpreter-only mode, wasm currently still creates executable
  // memory at runtime. Unexpose wasm until this changes.
  // The correctness fuzzers are a special case: many of their test cases are
  // built by fetching a random property from the the global object, and thus
  // the global object layout must not change between configs. That is why we
  // continue exposing wasm on correctness fuzzers even in jitless mode.
  // TODO(jgruber): Remove this once / if wasm can run without executable
  // memory.
149
#if V8_ENABLE_WEBASSEMBLY
150
  if (FLAG_jitless && !FLAG_correctness_fuzzer_suppressions) {
151
    DISABLE_FLAG(expose_wasm);
152
  }
153
#endif
154

155 156 157 158 159 160
  // When fuzzing and concurrent compilation is enabled, disable Turbofan
  // tracing flags since reading/printing heap state is not thread-safe and
  // leads to false positives on TSAN bots.
  // TODO(chromium:1205289): Teach relevant fuzzers to not pass TF tracing
  // flags instead, and remove this section.
  if (FLAG_fuzzing && FLAG_concurrent_recompilation) {
161 162 163 164 165 166 167 168 169 170 171 172
    DISABLE_FLAG(trace_turbo);
    DISABLE_FLAG(trace_turbo_graph);
    DISABLE_FLAG(trace_turbo_scheduled);
    DISABLE_FLAG(trace_turbo_reduction);
    DISABLE_FLAG(trace_turbo_trimming);
    DISABLE_FLAG(trace_turbo_jt);
    DISABLE_FLAG(trace_turbo_ceq);
    DISABLE_FLAG(trace_turbo_loop);
    DISABLE_FLAG(trace_turbo_alloc);
    DISABLE_FLAG(trace_all_uses);
    DISABLE_FLAG(trace_representation);
    DISABLE_FLAG(trace_turbo_stack_accesses);
173 174
  }

175 176 177
  // The --jitless and --interpreted-frames-native-stack flags are incompatible
  // since the latter requires code generation while the former prohibits code
  // generation.
178
  CHECK(!FLAG_interpreted_frames_native_stack || !FLAG_jitless);
179

180 181 182
  base::OS::Initialize(FLAG_hard_abort, FLAG_gc_fake_mmap);

  if (FLAG_random_seed) SetRandomMmapSeed(FLAG_random_seed);
183

184
#if defined(V8_USE_PERFETTO)
185
  if (perfetto::Tracing::IsInitialized()) TrackEvent::Register();
186
#endif
187
  IsolateAllocator::InitializeOncePerProcess();
188 189
  Isolate::InitializeOncePerProcess();

190 191 192
#if defined(USE_SIMULATOR)
  Simulator::InitializeOncePerProcess();
#endif
193
  CpuFeatures::Probe(false);
194
  ElementsAccessor::InitializeOncePerProcess();
195
  Bootstrapper::InitializeOncePerProcess();
196
  CallDescriptors::InitializeOncePerProcess();
197
#if V8_ENABLE_WEBASSEMBLY
198
  wasm::WasmEngine::InitializeOncePerProcess();
199
#endif  // V8_ENABLE_WEBASSEMBLY
200 201

  ExternalReferenceTable::InitializeOncePerProcess();
202 203 204
}

void V8::InitializeOncePerProcess() {
205
  base::CallOnce(&init_once, &InitializeOncePerProcessImpl);
206 207
}

208
void V8::InitializePlatform(v8::Platform* platform) {
209 210
  CHECK(!platform_);
  CHECK(platform);
211
  platform_ = platform;
212
  v8::base::SetPrintStackTrace(platform_->GetStackTracePrinter());
213
  v8::tracing::TracingCategoryObserver::SetUp();
214
#if defined(V8_OS_WIN) && defined(V8_ENABLE_SYSTEM_INSTRUMENTATION)
215 216 217 218
  if (FLAG_enable_system_instrumentation) {
    // TODO(sartang@microsoft.com): Move to platform specific diagnostics object
    v8::internal::ETWJITInterface::Register();
  }
219
#endif
220 221
}

222 223 224 225 226 227 228 229 230
#ifdef V8_VIRTUAL_MEMORY_CAGE
bool V8::InitializeVirtualMemoryCage() {
  // Platform must have been initialized already.
  CHECK(platform_);
  v8::PageAllocator* page_allocator = GetPlatformPageAllocator();
  return GetProcessWideVirtualMemoryCage()->Initialize(page_allocator);
}
#endif

231
void V8::ShutdownPlatform() {
232
  CHECK(platform_);
233
#if defined(V8_OS_WIN) && defined(V8_ENABLE_SYSTEM_INSTRUMENTATION)
234 235 236
  if (FLAG_enable_system_instrumentation) {
    v8::internal::ETWJITInterface::Unregister();
  }
237
#endif
238
  v8::tracing::TracingCategoryObserver::TearDown();
239
  v8::base::SetPrintStackTrace(nullptr);
240 241 242 243 244 245 246

#ifdef V8_VIRTUAL_MEMORY_CAGE
  // TODO(chromium:1218005) alternatively, this could move to its own
  // public TearDownVirtualMemoryCage function.
  GetProcessWideVirtualMemoryCage()->TearDown();
#endif

247
  platform_ = nullptr;
248 249 250
}

v8::Platform* V8::GetCurrentPlatform() {
251 252 253 254
  v8::Platform* platform = reinterpret_cast<v8::Platform*>(
      base::Relaxed_Load(reinterpret_cast<base::AtomicWord*>(&platform_)));
  DCHECK(platform);
  return platform;
255 256
}

257 258 259 260
void V8::SetPlatformForTesting(v8::Platform* platform) {
  base::Relaxed_Store(reinterpret_cast<base::AtomicWord*>(&platform_),
                      reinterpret_cast<base::AtomicWord>(platform));
}
261

262 263 264 265
void V8::SetSnapshotBlob(StartupData* snapshot_blob) {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
  base::CallOnce(&init_snapshot_once, &SetSnapshotFromFile, snapshot_blob);
#else
266
  UNREACHABLE();
267 268
#endif
}
269
}  // namespace internal
270 271 272 273 274

// static
double Platform::SystemClockTimeMillis() {
  return base::OS::TimeCurrentMillis();
}
275
}  // namespace v8