| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
/* SpiderMonkey initialization and shutdown code. */ |
| 8 |
|
| 9 |
#include "js/Initialization.h" |
| 10 |
|
| 11 |
#include "mozilla/Assertions.h" |
| 12 |
#if JS_HAS_INTL_API |
| 13 |
# include "mozilla/intl/ICU4CLibrary.h" |
| 14 |
# include "mozilla/intl/ICU4XGeckoDataProvider.h" |
| 15 |
#endif |
| 16 |
#include "mozilla/TextUtils.h" |
| 17 |
|
| 18 |
#include "jstypes.h" |
| 19 |
|
| 20 |
#include "builtin/AtomicsObject.h" |
| 21 |
#include "builtin/TestingFunctions.h" |
| 22 |
#include "gc/Statistics.h" |
| 23 |
#include "jit/Assembler.h" |
| 24 |
#include "jit/Ion.h" |
| 25 |
#include "jit/JitOptions.h" |
| 26 |
#include "jit/Simulator.h" |
| 27 |
#include "js/Utility.h" |
| 28 |
#include "threading/ProtectedData.h" // js::AutoNoteSingleThreadedRegion |
| 29 |
#include "util/Poison.h" |
| 30 |
#include "vm/ArrayBufferObject.h" |
| 31 |
#include "vm/DateTime.h" |
| 32 |
#include "vm/HelperThreads.h" |
| 33 |
#include "vm/Runtime.h" |
| 34 |
#include "vm/Time.h" |
| 35 |
#ifdef MOZ_VTUNE |
| 36 |
# include "vtune/VTuneWrapper.h" |
| 37 |
#endif |
| 38 |
#include "wasm/WasmProcess.h" |
| 39 |
|
| 40 |
using js::FutexThread; |
| 41 |
using JS::detail::InitState; |
| 42 |
using JS::detail::libraryInitState; |
| 43 |
|
| 44 |
InitState JS::detail::libraryInitState; |
| 45 |
|
| 46 |
#ifdef DEBUG |
| 47 |
static unsigned MessageParameterCount(const char* format) { |
| 48 |
unsigned numfmtspecs = 0; |
| 49 |
for (const char* fmt = format; *fmt != '\0'; fmt++) { |
| 50 |
if (*fmt == '{' && mozilla::IsAsciiDigit(fmt[1])) { |
| 51 |
++numfmtspecs; |
| 52 |
} |
| 53 |
} |
| 54 |
return numfmtspecs; |
| 55 |
} |
| 56 |
|
| 57 |
static void CheckMessageParameterCounts() { |
| 58 |
// Assert that each message format has the correct number of braced |
| 59 |
// parameters. |
| 60 |
# define MSG_DEF(name, count, exception, format) \ |
| 61 |
MOZ_ASSERT(MessageParameterCount(format) == count); |
| 62 |
# include "js/friend/ErrorNumbers.msg" |
| 63 |
# undef MSG_DEF |
| 64 |
} |
| 65 |
#endif /* DEBUG */ |
| 66 |
|
| 67 |
#if defined(JS_RUNTIME_CANONICAL_NAN) |
| 68 |
namespace JS::detail { |
| 69 |
uint64_t CanonicalizedNaNBits; |
| 70 |
} // namespace JS::detail |
| 71 |
#endif |
| 72 |
|
| 73 |
static void SetupCanonicalNaN() { |
| 74 |
// Compute the standard NaN value that the hardware generates. |
| 75 |
volatile double infinity = mozilla::PositiveInfinity<double>(); |
| 76 |
volatile double hardwareNaN = infinity - infinity; |
| 77 |
uint64_t hardwareNaNBits = mozilla::BitwiseCast<uint64_t>(hardwareNaN); |
| 78 |
hardwareNaNBits &= ~mozilla::FloatingPoint<double>::kSignBit; |
| 79 |
|
| 80 |
#if defined(JS_NONCANONICAL_HARDWARE_NAN) |
| 81 |
// If the NaN generated by hardware operations is not compatible |
| 82 |
// with our canonical NaN, we must canonicalize every double. This |
| 83 |
// is implemented for C++ code in Value::bitsFromDouble, but is not |
| 84 |
// implemented for JIT code. |
| 85 |
# if !defined(JS_CODEGEN_NONE) |
| 86 |
# error "No JIT support for non-canonical hardware NaN" |
| 87 |
# endif |
| 88 |
|
| 89 |
(void)hardwareNaNBits; |
| 90 |
#elif defined(JS_RUNTIME_CANONICAL_NAN) |
| 91 |
// Determine canonical NaN at startup. It must still match the ValueIsDouble |
| 92 |
// requirements. |
| 93 |
MOZ_RELEASE_ASSERT(JS::detail::ValueIsDouble(hardwareNaNBits)); |
| 94 |
JS::detail::CanonicalizedNaNBits = hardwareNaNBits; |
| 95 |
#else |
| 96 |
// Assert that the NaN generated by hardware operations is |
| 97 |
// compatible with the canonical NaN we use for JS::Value. This is |
| 98 |
// true for all of our supported platforms, but not for SPARC. |
| 99 |
MOZ_RELEASE_ASSERT(hardwareNaNBits == JS::detail::CanonicalizedNaNBits, |
| 100 |
"Unexpected default hardware NaN value"); |
| 101 |
#endif |
| 102 |
} |
| 103 |
|
| 104 |
#define RETURN_IF_FAIL(code) \ |
| 105 |
do { \ |
| 106 |
if (!code) return #code " failed"; \ |
| 107 |
} while (0) |
| 108 |
|
| 109 |
extern "C" void install_rust_hooks(); |
| 110 |
|
| 111 |
JS_PUBLIC_API const char* JS::detail::InitWithFailureDiagnostic( |
| 112 |
bool isDebugBuild, FrontendOnly frontendOnly /* = FrontendOnly::No */) { |
| 113 |
// Verify that our DEBUG setting matches the caller's. |
| 114 |
#ifdef DEBUG |
| 115 |
MOZ_RELEASE_ASSERT(isDebugBuild); |
| 116 |
#else |
| 117 |
MOZ_RELEASE_ASSERT(!isDebugBuild); |
| 118 |
#endif |
| 119 |
|
| 120 |
MOZ_ASSERT(libraryInitState == InitState::Uninitialized, |
| 121 |
"must call JS_Init once before any JSAPI operation except " |
| 122 |
"JS_SetICUMemoryFunctions"); |
| 123 |
MOZ_ASSERT(!JSRuntime::hasLiveRuntimes(), |
| 124 |
"how do we have live runtimes before JS_Init?"); |
| 125 |
|
| 126 |
libraryInitState = InitState::Initializing; |
| 127 |
|
| 128 |
#ifdef JS_STANDALONE |
| 129 |
// The rust hooks are initialized by Gecko on non-standalone builds. |
| 130 |
install_rust_hooks(); |
| 131 |
#endif |
| 132 |
|
| 133 |
if (frontendOnly == FrontendOnly::No) { |
| 134 |
// The first invocation of `ProcessCreation` creates a temporary thread |
| 135 |
// and crashes if that fails, i.e. because we're out of memory. To prevent |
| 136 |
// that from happening at some later time, get it out of the way during |
| 137 |
// startup. |
| 138 |
mozilla::TimeStamp::ProcessCreation(); |
| 139 |
} |
| 140 |
|
| 141 |
#ifdef DEBUG |
| 142 |
CheckMessageParameterCounts(); |
| 143 |
#endif |
| 144 |
|
| 145 |
SetupCanonicalNaN(); |
| 146 |
|
| 147 |
if (frontendOnly == FrontendOnly::No) { |
| 148 |
RETURN_IF_FAIL(js::TlsContext.init()); |
| 149 |
} |
| 150 |
|
| 151 |
#if defined(DEBUG) || defined(JS_OOM_BREAKPOINT) |
| 152 |
RETURN_IF_FAIL(js::oom::InitThreadType()); |
| 153 |
#endif |
| 154 |
|
| 155 |
#if defined(FUZZING) |
| 156 |
js::oom::InitLargeAllocLimit(); |
| 157 |
#endif |
| 158 |
|
| 159 |
js::InitMallocAllocator(); |
| 160 |
|
| 161 |
RETURN_IF_FAIL(js::Mutex::Init()); |
| 162 |
|
| 163 |
js::gc::InitMemorySubsystem(); // Ensure gc::SystemPageSize() works. |
| 164 |
|
| 165 |
RETURN_IF_FAIL(js::wasm::Init()); |
| 166 |
|
| 167 |
js::coverage::InitLCov(); |
| 168 |
|
| 169 |
if (frontendOnly == FrontendOnly::No) { |
| 170 |
RETURN_IF_FAIL(js::jit::InitializeJit()); |
| 171 |
} |
| 172 |
|
| 173 |
RETURN_IF_FAIL(js::InitDateTimeState()); |
| 174 |
|
| 175 |
if (frontendOnly == FrontendOnly::No) { |
| 176 |
#ifdef MOZ_VTUNE |
| 177 |
RETURN_IF_FAIL(js::vtune::Initialize()); |
| 178 |
#endif |
| 179 |
} |
| 180 |
|
| 181 |
#if JS_HAS_INTL_API |
| 182 |
if (mozilla::intl::ICU4CLibrary::Initialize().isErr()) { |
| 183 |
return "ICU4CLibrary::Initialize() failed"; |
| 184 |
} |
| 185 |
#endif // JS_HAS_INTL_API |
| 186 |
|
| 187 |
if (frontendOnly == FrontendOnly::No) { |
| 188 |
RETURN_IF_FAIL(js::CreateHelperThreadsState()); |
| 189 |
RETURN_IF_FAIL(FutexThread::initialize()); |
| 190 |
RETURN_IF_FAIL(js::gcstats::Statistics::initialize()); |
| 191 |
RETURN_IF_FAIL(js::InitTestingFunctions()); |
| 192 |
} |
| 193 |
|
| 194 |
RETURN_IF_FAIL(js::SharedImmutableStringsCache::initSingleton()); |
| 195 |
RETURN_IF_FAIL(js::frontend::WellKnownParserAtoms::initSingleton()); |
| 196 |
|
| 197 |
if (frontendOnly == FrontendOnly::No) { |
| 198 |
#ifdef JS_SIMULATOR |
| 199 |
RETURN_IF_FAIL(js::jit::SimulatorProcess::initialize()); |
| 200 |
#endif |
| 201 |
|
| 202 |
#ifndef JS_CODEGEN_NONE |
| 203 |
// This is forced by InitializeJit. |
| 204 |
MOZ_ASSERT(js::jit::CPUFlagsHaveBeenComputed()); |
| 205 |
#endif |
| 206 |
} |
| 207 |
|
| 208 |
libraryInitState = InitState::Running; |
| 209 |
return nullptr; |
| 210 |
} |
| 211 |
|
| 212 |
#undef RETURN_IF_FAIL |
| 213 |
|
| 214 |
JS_PUBLIC_API bool JS::InitSelfHostedCode(JSContext* cx, SelfHostedCache cache, |
| 215 |
SelfHostedWriter writer) { |
| 216 |
MOZ_RELEASE_ASSERT(!cx->runtime()->hasInitializedSelfHosting(), |
| 217 |
"JS::InitSelfHostedCode() called more than once"); |
| 218 |
|
| 219 |
js::AutoNoteSingleThreadedRegion anstr; |
| 220 |
|
| 221 |
JSRuntime* rt = cx->runtime(); |
| 222 |
|
| 223 |
if (!rt->initSelfHostingStencil(cx, cache, writer)) { |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
if (!rt->initializeAtoms(cx)) { |
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
if (!rt->initSelfHostingFromStencil(cx)) { |
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
if (js::jit::HasJitBackend()) { |
| 236 |
if (!rt->createJitRuntime(cx)) { |
| 237 |
return false; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
return true; |
| 242 |
} |
| 243 |
|
| 244 |
static void ShutdownImpl(JS::detail::FrontendOnly frontendOnly) { |
| 245 |
using FrontendOnly = JS::detail::FrontendOnly; |
| 246 |
|
| 247 |
MOZ_ASSERT( |
| 248 |
libraryInitState == InitState::Running, |
| 249 |
"JS_ShutDown must only be called after JS_Init and can't race with it"); |
| 250 |
#ifdef DEBUG |
| 251 |
if (JSRuntime::hasLiveRuntimes()) { |
| 252 |
// Gecko is too buggy to assert this just yet. |
| 253 |
fprintf(stderr, |
| 254 |
"WARNING: YOU ARE LEAKING THE WORLD (at least one JSRuntime " |
| 255 |
"and everything alive inside it, that is) AT JS_ShutDown " |
| 256 |
"TIME. FIX THIS!\n"); |
| 257 |
} |
| 258 |
#endif |
| 259 |
|
| 260 |
js::frontend::WellKnownParserAtoms::freeSingleton(); |
| 261 |
js::SharedImmutableStringsCache::freeSingleton(); |
| 262 |
|
| 263 |
if (frontendOnly == FrontendOnly::No) { |
| 264 |
FutexThread::destroy(); |
| 265 |
|
| 266 |
js::DestroyHelperThreadsState(); |
| 267 |
|
| 268 |
#ifdef JS_SIMULATOR |
| 269 |
js::jit::SimulatorProcess::destroy(); |
| 270 |
#endif |
| 271 |
} |
| 272 |
|
| 273 |
js::wasm::ShutDown(); |
| 274 |
|
| 275 |
#if JS_HAS_INTL_API |
| 276 |
mozilla::intl::ICU4CLibrary::Cleanup(); |
| 277 |
mozilla::intl::CleanupDataProvider(); |
| 278 |
#endif // JS_HAS_INTL_API |
| 279 |
|
| 280 |
if (frontendOnly == FrontendOnly::No) { |
| 281 |
#ifdef MOZ_VTUNE |
| 282 |
js::vtune::Shutdown(); |
| 283 |
#endif // MOZ_VTUNE |
| 284 |
} |
| 285 |
|
| 286 |
js::FinishDateTimeState(); |
| 287 |
|
| 288 |
if (frontendOnly == FrontendOnly::No) { |
| 289 |
js::jit::ShutdownJit(); |
| 290 |
} |
| 291 |
|
| 292 |
MOZ_ASSERT_IF(!JSRuntime::hasLiveRuntimes(), !js::WasmReservedBytes()); |
| 293 |
|
| 294 |
js::ShutDownMallocAllocator(); |
| 295 |
|
| 296 |
if (!JSRuntime::hasLiveRuntimes()) { |
| 297 |
js::gc::CheckMemorySubsystemOnShutDown(); |
| 298 |
} |
| 299 |
|
| 300 |
libraryInitState = InitState::ShutDown; |
| 301 |
} |
| 302 |
|
| 303 |
JS_PUBLIC_API void JS_ShutDown(void) { |
| 304 |
ShutdownImpl(JS::detail::FrontendOnly::No); |
| 305 |
} |
| 306 |
|
| 307 |
JS_PUBLIC_API void JS_FrontendOnlyShutDown(void) { |
| 308 |
ShutdownImpl(JS::detail::FrontendOnly::Yes); |
| 309 |
} |
| 310 |
|
| 311 |
JS_PUBLIC_API bool JS_SetICUMemoryFunctions(JS_ICUAllocFn allocFn, |
| 312 |
JS_ICUReallocFn reallocFn, |
| 313 |
JS_ICUFreeFn freeFn) { |
| 314 |
MOZ_ASSERT(libraryInitState == InitState::Uninitialized, |
| 315 |
"must call JS_SetICUMemoryFunctions before any other JSAPI " |
| 316 |
"operation (including JS_Init)"); |
| 317 |
|
| 318 |
#if JS_HAS_INTL_API |
| 319 |
return mozilla::intl::ICU4CLibrary::SetMemoryFunctions( |
| 320 |
{allocFn, reallocFn, freeFn}) |
| 321 |
.isOk(); |
| 322 |
#else |
| 323 |
return true; |
| 324 |
#endif |
| 325 |
} |
| 326 |
|
| 327 |
#if defined(ENABLE_WASM_SIMD) && \ |
| 328 |
(defined(JS_CODEGEN_X64) || defined(JS_CODEGEN_X86)) |
| 329 |
void JS::SetAVXEnabled(bool enabled) { |
| 330 |
if (enabled) { |
| 331 |
js::jit::CPUInfo::SetAVXEnabled(); |
| 332 |
} else { |
| 333 |
js::jit::CPUInfo::SetAVXDisabled(); |
| 334 |
} |
| 335 |
} |
| 336 |
#endif |
| 337 |
|
| 338 |
JS_PUBLIC_API void JS::DisableJitBackend() { |
| 339 |
MOZ_ASSERT(libraryInitState == InitState::Uninitialized, |
| 340 |
"DisableJitBackend must be called before JS_Init"); |
| 341 |
MOZ_ASSERT(!JSRuntime::hasLiveRuntimes(), |
| 342 |
"DisableJitBackend must be called before creating a JSContext"); |
| 343 |
js::jit::JitOptions.disableJitBackend = true; |
| 344 |
} |