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 "src/v8.h"
6
7#include <fstream>
8
9#include "src/api.h"
10#include "src/base/atomicops.h"
11#include "src/base/once.h"
12#include "src/base/platform/platform.h"
13#include "src/bootstrapper.h"
14#include "src/cpu-features.h"
15#include "src/debug/debug.h"
16#include "src/deoptimizer.h"
17#include "src/elements.h"
18#include "src/frames.h"
19#include "src/interface-descriptors.h"
20#include "src/isolate.h"
21#include "src/libsampler/sampler.h"
22#include "src/objects-inl.h"
23#include "src/profiler/heap-profiler.h"
24#include "src/runtime-profiler.h"
25#include "src/simulator.h"
26#include "src/snapshot/natives.h"
27#include "src/snapshot/snapshot.h"
28#include "src/tracing/tracing-category-observer.h"
29#include "src/wasm/wasm-engine.h"
30
31namespace v8 {
32namespace internal {
33
34V8_DECLARE_ONCE(init_once);
35
36#ifdef V8_USE_EXTERNAL_STARTUP_DATA
37V8_DECLARE_ONCE(init_natives_once);
38V8_DECLARE_ONCE(init_snapshot_once);
39#endif
40
41v8::Platform* V8::platform_ = nullptr;
42
43bool V8::Initialize() {
44 InitializeOncePerProcess();
45 return true;
46}
47
48
49void V8::TearDown() {
50 wasm::WasmEngine::GlobalTearDown();
51#if defined(USE_SIMULATOR)
52 Simulator::GlobalTearDown();
53#endif
54 CallDescriptors::TearDown();
55 ElementsAccessor::TearDown();
56 RegisteredExtension::UnregisterAll();
57 FlagList::ResetAllFlags(); // Frees memory held by string arguments.
58}
59
60
61void V8::InitializeOncePerProcessImpl() {
62 FlagList::EnforceFlagImplications();
63
64 if (FLAG_predictable && FLAG_random_seed == 0) {
65 // Avoid random seeds in predictable mode.
66 FLAG_random_seed = 12347;
67 }
68
69 if (FLAG_stress_compaction) {
70 FLAG_force_marking_deque_overflows = true;
71 FLAG_gc_global = true;
72 FLAG_max_semi_space_size = 1;
73 }
74
75 if (FLAG_trace_turbo) {
76 // Create an empty file shared by the process (e.g. the wasm engine).
77 std::ofstream(Isolate::GetTurboCfgFileName(nullptr).c_str(),
78 std::ios_base::trunc);
79 }
80
81 // Do not expose wasm in jitless mode.
82 //
83 // Even in interpreter-only mode, wasm currently still creates executable
84 // memory at runtime. Unexpose wasm until this changes.
85 // The correctness fuzzers are a special case: many of their test cases are
86 // built by fetching a random property from the the global object, and thus
87 // the global object layout must not change between configs. That is why we
88 // continue exposing wasm on correctness fuzzers even in jitless mode.
89 // TODO(jgruber): Remove this once / if wasm can run without executable
90 // memory.
91 if (FLAG_jitless && !FLAG_abort_on_stack_or_string_length_overflow) {
92 FLAG_expose_wasm = false;
93 }
94
95 base::OS::Initialize(FLAG_hard_abort, FLAG_gc_fake_mmap);
96
97 if (FLAG_random_seed) SetRandomMmapSeed(FLAG_random_seed);
98
99 Isolate::InitializeOncePerProcess();
100
101#if defined(USE_SIMULATOR)
102 Simulator::InitializeOncePerProcess();
103#endif
104 CpuFeatures::Probe(false);
105 ElementsAccessor::InitializeOncePerProcess();
106 Bootstrapper::InitializeOncePerProcess();
107 CallDescriptors::InitializeOncePerProcess();
108 wasm::WasmEngine::InitializeOncePerProcess();
109}
110
111
112void V8::InitializeOncePerProcess() {
113 base::CallOnce(&init_once, &InitializeOncePerProcessImpl);
114}
115
116
117void V8::InitializePlatform(v8::Platform* platform) {
118 CHECK(!platform_);
119 CHECK(platform);
120 platform_ = platform;
121 v8::base::SetPrintStackTrace(platform_->GetStackTracePrinter());
122 v8::tracing::TracingCategoryObserver::SetUp();
123}
124
125
126void V8::ShutdownPlatform() {
127 CHECK(platform_);
128 v8::tracing::TracingCategoryObserver::TearDown();
129 v8::base::SetPrintStackTrace(nullptr);
130 platform_ = nullptr;
131}
132
133
134v8::Platform* V8::GetCurrentPlatform() {
135 v8::Platform* platform = reinterpret_cast<v8::Platform*>(
136 base::Relaxed_Load(reinterpret_cast<base::AtomicWord*>(&platform_)));
137 DCHECK(platform);
138 return platform;
139}
140
141void V8::SetPlatformForTesting(v8::Platform* platform) {
142 base::Relaxed_Store(reinterpret_cast<base::AtomicWord*>(&platform_),
143 reinterpret_cast<base::AtomicWord>(platform));
144}
145
146void V8::SetNativesBlob(StartupData* natives_blob) {
147#ifdef V8_USE_EXTERNAL_STARTUP_DATA
148 base::CallOnce(&init_natives_once, &SetNativesFromFile, natives_blob);
149#else
150 UNREACHABLE();
151#endif
152}
153
154
155void V8::SetSnapshotBlob(StartupData* snapshot_blob) {
156#ifdef V8_USE_EXTERNAL_STARTUP_DATA
157 base::CallOnce(&init_snapshot_once, &SetSnapshotFromFile, snapshot_blob);
158#else
159 UNREACHABLE();
160#endif
161}
162} // namespace internal
163
164// static
165double Platform::SystemClockTimeMillis() {
166 return base::OS::TimeCurrentMillis();
167}
168} // namespace v8
169