Line data Source code
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 :
31 : namespace v8 {
32 : namespace internal {
33 :
34 : V8_DECLARE_ONCE(init_once);
35 :
36 : #ifdef V8_USE_EXTERNAL_STARTUP_DATA
37 : V8_DECLARE_ONCE(init_natives_once);
38 : V8_DECLARE_ONCE(init_snapshot_once);
39 : #endif
40 :
41 : v8::Platform* V8::platform_ = nullptr;
42 :
43 61316 : bool V8::Initialize() {
44 61316 : InitializeOncePerProcess();
45 61316 : return true;
46 : }
47 :
48 :
49 59887 : void V8::TearDown() {
50 59887 : wasm::WasmEngine::GlobalTearDown();
51 : #if defined(USE_SIMULATOR)
52 : Simulator::GlobalTearDown();
53 : #endif
54 59887 : CallDescriptors::TearDown();
55 59887 : ElementsAccessor::TearDown();
56 59887 : RegisteredExtension::UnregisterAll();
57 59887 : FlagList::ResetAllFlags(); // Frees memory held by string arguments.
58 59887 : }
59 :
60 :
61 60992 : void V8::InitializeOncePerProcessImpl() {
62 60992 : FlagList::EnforceFlagImplications();
63 :
64 60992 : if (FLAG_predictable && FLAG_random_seed == 0) {
65 : // Avoid random seeds in predictable mode.
66 0 : FLAG_random_seed = 12347;
67 : }
68 :
69 60992 : if (FLAG_stress_compaction) {
70 86 : FLAG_force_marking_deque_overflows = true;
71 86 : FLAG_gc_global = true;
72 86 : FLAG_max_semi_space_size = 1;
73 : }
74 :
75 60992 : if (FLAG_trace_turbo) {
76 : // Create an empty file shared by the process (e.g. the wasm engine).
77 10 : std::ofstream(Isolate::GetTurboCfgFileName(nullptr).c_str(),
78 5 : 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 60992 : if (FLAG_jitless && !FLAG_abort_on_stack_or_string_length_overflow) {
92 8146 : FLAG_expose_wasm = false;
93 : }
94 :
95 60992 : base::OS::Initialize(FLAG_hard_abort, FLAG_gc_fake_mmap);
96 :
97 60992 : if (FLAG_random_seed) SetRandomMmapSeed(FLAG_random_seed);
98 :
99 60992 : Isolate::InitializeOncePerProcess();
100 :
101 : #if defined(USE_SIMULATOR)
102 : Simulator::InitializeOncePerProcess();
103 : #endif
104 : CpuFeatures::Probe(false);
105 60992 : ElementsAccessor::InitializeOncePerProcess();
106 60992 : Bootstrapper::InitializeOncePerProcess();
107 60992 : CallDescriptors::InitializeOncePerProcess();
108 60992 : wasm::WasmEngine::InitializeOncePerProcess();
109 60992 : }
110 :
111 :
112 61316 : void V8::InitializeOncePerProcess() {
113 122632 : base::CallOnce(&init_once, &InitializeOncePerProcessImpl);
114 61316 : }
115 :
116 :
117 60992 : void V8::InitializePlatform(v8::Platform* platform) {
118 60992 : CHECK(!platform_);
119 60992 : CHECK(platform);
120 60992 : platform_ = platform;
121 60992 : v8::base::SetPrintStackTrace(platform_->GetStackTracePrinter());
122 60992 : v8::tracing::TracingCategoryObserver::SetUp();
123 60992 : }
124 :
125 :
126 59843 : void V8::ShutdownPlatform() {
127 59843 : CHECK(platform_);
128 59843 : v8::tracing::TracingCategoryObserver::TearDown();
129 59843 : v8::base::SetPrintStackTrace(nullptr);
130 59843 : platform_ = nullptr;
131 59843 : }
132 :
133 :
134 38934320 : v8::Platform* V8::GetCurrentPlatform() {
135 : v8::Platform* platform = reinterpret_cast<v8::Platform*>(
136 38934320 : base::Relaxed_Load(reinterpret_cast<base::AtomicWord*>(&platform_)));
137 : DCHECK(platform);
138 38934320 : return platform;
139 : }
140 :
141 548 : void V8::SetPlatformForTesting(v8::Platform* platform) {
142 : base::Relaxed_Store(reinterpret_cast<base::AtomicWord*>(&platform_),
143 : reinterpret_cast<base::AtomicWord>(platform));
144 548 : }
145 :
146 61001 : void V8::SetNativesBlob(StartupData* natives_blob) {
147 : #ifdef V8_USE_EXTERNAL_STARTUP_DATA
148 61001 : base::CallOnce(&init_natives_once, &SetNativesFromFile, natives_blob);
149 : #else
150 : UNREACHABLE();
151 : #endif
152 61001 : }
153 :
154 :
155 61001 : void V8::SetSnapshotBlob(StartupData* snapshot_blob) {
156 : #ifdef V8_USE_EXTERNAL_STARTUP_DATA
157 61001 : base::CallOnce(&init_snapshot_once, &SetSnapshotFromFile, snapshot_blob);
158 : #else
159 : UNREACHABLE();
160 : #endif
161 61001 : }
162 : } // namespace internal
163 :
164 : // static
165 0 : double Platform::SystemClockTimeMillis() {
166 0 : return base::OS::TimeCurrentMillis();
167 : }
168 122004 : } // namespace v8
|