v8.cc 11.1 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/common/code-memory-access.h"
17
#include "src/debug/debug.h"
18
#include "src/deoptimizer/deoptimizer.h"
19 20 21
#include "src/execution/frames.h"
#include "src/execution/isolate.h"
#include "src/execution/simulator.h"
22
#include "src/init/bootstrapper.h"
23
#include "src/libsampler/sampler.h"
24
#include "src/objects/elements.h"
25
#include "src/objects/objects-inl.h"
26
#include "src/profiler/heap-profiler.h"
Samuel Groß's avatar
Samuel Groß committed
27
#include "src/sandbox/sandbox.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 36
#if defined(V8_OS_WIN) && defined(V8_ENABLE_ETW_STACK_WALKING)
#include "src/diagnostics/etw-jit-win.h"
37 38
#endif

39 40
namespace v8 {
namespace internal {
41

42
// static
43
v8::Platform* V8::platform_ = nullptr;
44 45
const OOMDetails V8::kNoOOMDetails{false, nullptr};
const OOMDetails V8::kHeapOOM{true, nullptr};
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

namespace {
enum class V8StartupState {
  kIdle,
  kPlatformInitializing,
  kPlatformInitialized,
  kV8Initializing,
  kV8Initialized,
  kV8Disposing,
  kV8Disposed,
  kPlatformDisposing,
  kPlatformDisposed
};

std::atomic<V8StartupState> v8_startup_state_(V8StartupState::kIdle);

void AdvanceStartupState(V8StartupState expected_next_state) {
  V8StartupState current_state = v8_startup_state_;
  CHECK_NE(current_state, V8StartupState::kPlatformDisposed);
  V8StartupState next_state =
      static_cast<V8StartupState>(static_cast<int>(current_state) + 1);
  if (next_state != expected_next_state) {
    // Ensure the following order:
    // v8::V8::InitializePlatform(platform);
    // v8::V8::Initialize();
    // v8::Isolate* isolate = v8::Isolate::New(...);
    // ...
    // isolate->Dispose();
    // v8::V8::Dispose();
    // v8::V8::DisposePlatform();
76
    FATAL("Wrong initialization order: got %d expected %d!",
77
          static_cast<int>(current_state), static_cast<int>(next_state));
78 79 80 81 82
  }
  if (!v8_startup_state_.compare_exchange_strong(current_state, next_state)) {
    FATAL(
        "Multiple threads are initializating V8 in the wrong order: expected "
        "%d got %d!",
83 84
        static_cast<int>(current_state),
        static_cast<int>(v8_startup_state_.load()));
85 86 87 88
  }
}

}  // namespace
89

90 91 92 93
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
V8_DECLARE_ONCE(init_snapshot_once);
#endif

94 95 96 97 98 99 100
void V8::InitializePlatform(v8::Platform* platform) {
  AdvanceStartupState(V8StartupState::kPlatformInitializing);
  CHECK(!platform_);
  CHECK_NOT_NULL(platform);
  platform_ = platform;
  v8::base::SetPrintStackTrace(platform_->GetStackTracePrinter());
  v8::tracing::TracingCategoryObserver::SetUp();
101 102
#if defined(V8_OS_WIN) && defined(V8_ENABLE_ETW_STACK_WALKING)
  if (FLAG_enable_etw_stack_walking) {
103 104 105
    // TODO(sartang@microsoft.com): Move to platform specific diagnostics object
    v8::internal::ETWJITInterface::Register();
  }
106
#endif
107 108 109 110 111 112

  // Initialization needs to happen on platform-level, as this sets up some
  // cppgc internals that are needed to allow gracefully failing during cppgc
  // platform setup.
  CppHeap::InitializeOncePerProcess();

113
  AdvanceStartupState(V8StartupState::kPlatformInitialized);
114 115
}

116 117 118 119 120 121 122
#define DISABLE_FLAG(flag)                                                    \
  if (FLAG_##flag) {                                                          \
    PrintF(stderr,                                                            \
           "Warning: disabling flag --" #flag " due to conflicting flags\n"); \
    FLAG_##flag = false;                                                      \
  }

123 124
void V8::Initialize() {
  AdvanceStartupState(V8StartupState::kV8Initializing);
125 126
  CHECK(platform_);

127
  // Update logging information before enforcing flag implications.
128 129 130 131 132 133 134 135 136 137
  FlagValue<bool>* log_all_flags[] = {&FLAG_turbo_profiling_log_builtins,
                                      &FLAG_log_all,
                                      &FLAG_log_code,
                                      &FLAG_log_code_disassemble,
                                      &FLAG_log_source_code,
                                      &FLAG_log_function_events,
                                      &FLAG_log_internal_timer_events,
                                      &FLAG_log_deopt,
                                      &FLAG_log_ic,
                                      &FLAG_log_maps};
138 139 140 141 142 143 144 145 146 147 148 149 150
  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;
    }
151
    // Profiling flags depend on logging.
152 153
    FLAG_log = FLAG_log || FLAG_perf_prof || FLAG_perf_basic_prof ||
               FLAG_ll_prof || FLAG_prof || FLAG_prof_cpp || FLAG_gdbjit;
154 155
#if defined(V8_OS_WIN) && defined(V8_ENABLE_ETW_STACK_WALKING)
    FLAG_log = FLAG_log || FLAG_enable_etw_stack_walking;
156
#endif
157 158
  }

159
  FlagList::EnforceFlagImplications();
160

161 162 163
  if (FLAG_predictable && FLAG_random_seed == 0) {
    // Avoid random seeds in predictable mode.
    FLAG_random_seed = 12347;
164 165
  }

166 167 168
  if (FLAG_stress_compaction) {
    FLAG_force_marking_deque_overflows = true;
    FLAG_gc_global = true;
169
    FLAG_max_semi_space_size = 1;
170
  }
171

172 173 174 175 176 177
  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);
  }

178 179 180 181 182 183 184 185 186 187
  // 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.
188
#if V8_ENABLE_WEBASSEMBLY
189
  if (FLAG_jitless && !FLAG_correctness_fuzzer_suppressions) {
190
    DISABLE_FLAG(expose_wasm);
191
  }
192
#endif
193

194 195 196 197 198 199
  // 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) {
200 201 202 203 204 205 206 207 208 209 210 211
    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);
212 213
  }

214 215 216
  // The --jitless and --interpreted-frames-native-stack flags are incompatible
  // since the latter requires code generation while the former prohibits code
  // generation.
217
  CHECK(!FLAG_interpreted_frames_native_stack || !FLAG_jitless);
218

219 220
  base::OS::Initialize(FLAG_hard_abort, FLAG_gc_fake_mmap);

221 222 223 224
  if (FLAG_random_seed) {
    GetPlatformPageAllocator()->SetRandomMmapSeed(FLAG_random_seed);
    GetPlatformVirtualAddressSpace()->SetRandomSeed(FLAG_random_seed);
  }
225

226 227
  if (FLAG_print_flag_values) FlagList::PrintValues();

228
  // Initialize the default FlagList::Hash.
229 230
  FlagList::Hash();

231 232 233 234 235
  // Before initializing internals, freeze the flags such that further changes
  // are not allowed. Global initialization of the Isolate or the WasmEngine
  // already reads flags, so they should not be changed afterwards.
  if (FLAG_freeze_flags_after_init) FlagList::FreezeFlags();

236 237 238 239 240 241
#if defined(V8_ENABLE_SANDBOX)
  // If enabled, the sandbox must be initialized first.
  GetProcessWideSandbox()->Initialize(GetPlatformVirtualAddressSpace());
  CHECK_EQ(kSandboxSize, GetProcessWideSandbox()->size());
#endif

242
#if defined(V8_USE_PERFETTO)
243
  if (perfetto::Tracing::IsInitialized()) TrackEvent::Register();
244
#endif
245
  IsolateAllocator::InitializeOncePerProcess();
246 247
  Isolate::InitializeOncePerProcess();

248 249 250
#if defined(USE_SIMULATOR)
  Simulator::InitializeOncePerProcess();
#endif
251
  CpuFeatures::Probe(false);
252
  ElementsAccessor::InitializeOncePerProcess();
253
  Bootstrapper::InitializeOncePerProcess();
254
  CallDescriptors::InitializeOncePerProcess();
255 256 257 258 259 260

#if V8_HAS_PKU_JIT_WRITE_PROTECT
  base::MemoryProtectionKey::InitializeMemoryProtectionKeySupport();
  RwxMemoryWriteScope::InitializeMemoryProtectionKey();
#endif

261
#if V8_ENABLE_WEBASSEMBLY
262
  wasm::WasmEngine::InitializeOncePerProcess();
263
#endif  // V8_ENABLE_WEBASSEMBLY
264 265

  ExternalReferenceTable::InitializeOncePerProcess();
266

267
  AdvanceStartupState(V8StartupState::kV8Initialized);
268 269
}

270 271 272 273
#undef DISABLE_FLAG

void V8::Dispose() {
  AdvanceStartupState(V8StartupState::kV8Disposing);
274
  CHECK(platform_);
275 276 277 278 279
#if V8_ENABLE_WEBASSEMBLY
  wasm::WasmEngine::GlobalTearDown();
#endif  // V8_ENABLE_WEBASSEMBLY
#if defined(USE_SIMULATOR)
  Simulator::GlobalTearDown();
280
#endif
281 282 283 284
  CallDescriptors::TearDown();
  ElementsAccessor::TearDown();
  RegisteredExtension::UnregisterAll();
  Isolate::DisposeOncePerProcess();
285
  FlagList::ReleaseDynamicAllocations();
286 287
  AdvanceStartupState(V8StartupState::kV8Disposed);
}
288

289
void V8::DisposePlatform() {
290
  AdvanceStartupState(V8StartupState::kPlatformDisposing);
291
  CHECK(platform_);
292 293
#if defined(V8_OS_WIN) && defined(V8_ENABLE_ETW_STACK_WALKING)
  if (FLAG_enable_etw_stack_walking) {
294 295
    v8::internal::ETWJITInterface::Unregister();
  }
296
#endif
297
  v8::tracing::TracingCategoryObserver::TearDown();
298
  v8::base::SetPrintStackTrace(nullptr);
299

300
#ifdef V8_ENABLE_SANDBOX
301
  // TODO(chromium:1218005) alternatively, this could move to its own
Samuel Groß's avatar
Samuel Groß committed
302 303
  // public TearDownSandbox function.
  GetProcessWideSandbox()->TearDown();
304
#endif  // V8_ENABLE_SANDBOX
305

306
  platform_ = nullptr;
307
  AdvanceStartupState(V8StartupState::kPlatformDisposed);
308 309 310
}

v8::Platform* V8::GetCurrentPlatform() {
311 312 313 314
  v8::Platform* platform = reinterpret_cast<v8::Platform*>(
      base::Relaxed_Load(reinterpret_cast<base::AtomicWord*>(&platform_)));
  DCHECK(platform);
  return platform;
315 316
}

317 318 319 320
void V8::SetPlatformForTesting(v8::Platform* platform) {
  base::Relaxed_Store(reinterpret_cast<base::AtomicWord*>(&platform_),
                      reinterpret_cast<base::AtomicWord>(platform));
}
321

322 323 324 325
void V8::SetSnapshotBlob(StartupData* snapshot_blob) {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
  base::CallOnce(&init_snapshot_once, &SetSnapshotFromFile, snapshot_blob);
#else
326
  UNREACHABLE();
327 328
#endif
}
329
}  // namespace internal
330 331 332 333 334

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