|
|
|||
File indexing completed on 2026-09-15 09:13:51
0001 // Copyright 2021 the V8 project authors. All rights reserved. 0002 // Use of this source code is governed by a BSD-style license that can be 0003 // found in the LICENSE file. 0004 0005 #ifndef INCLUDE_V8_INITIALIZATION_H_ 0006 #define INCLUDE_V8_INITIALIZATION_H_ 0007 0008 #include <stddef.h> 0009 #include <stdint.h> 0010 0011 #include "v8-callbacks.h" // NOLINT(build/include_directory) 0012 #include "v8-internal.h" // NOLINT(build/include_directory) 0013 #include "v8-isolate.h" // NOLINT(build/include_directory) 0014 #include "v8-platform.h" // NOLINT(build/include_directory) 0015 #include "v8config.h" // NOLINT(build/include_directory) 0016 0017 // We reserve the V8_* prefix for macros defined in V8 public API and 0018 // assume there are no name conflicts with the embedder's code. 0019 0020 /** 0021 * The v8 JavaScript engine. 0022 */ 0023 namespace v8 { 0024 0025 class PageAllocator; 0026 class Platform; 0027 template <class K, class V, class T> 0028 class PersistentValueMapBase; 0029 0030 /** 0031 * EntropySource is used as a callback function when v8 needs a source 0032 * of entropy. 0033 */ 0034 using EntropySource = bool (*)(unsigned char* buffer, size_t length); 0035 0036 /** 0037 * ReturnAddressLocationResolver is used as a callback function when v8 is 0038 * resolving the location of a return address on the stack. Profilers that 0039 * change the return address on the stack can use this to resolve the stack 0040 * location to wherever the profiler stashed the original return address. 0041 * 0042 * \param return_addr_location A location on stack where a machine 0043 * return address resides. 0044 * \returns Either return_addr_location, or else a pointer to the profiler's 0045 * copy of the original return address. 0046 * 0047 * \note The resolver function must not cause garbage collection. 0048 */ 0049 using ReturnAddressLocationResolver = 0050 uintptr_t (*)(uintptr_t return_addr_location); 0051 0052 using DcheckErrorCallback = void (*)(const char* file, int line, 0053 const char* message); 0054 0055 using V8FatalErrorCallback = void (*)(const char* file, int line, 0056 const char* message); 0057 0058 /** 0059 * Container class for static utility functions. 0060 */ 0061 class V8_EXPORT V8 { 0062 public: 0063 /** 0064 * Hand startup data to V8, in case the embedder has chosen to build 0065 * V8 with external startup data. 0066 * 0067 * Note: 0068 * - By default the startup data is linked into the V8 library, in which 0069 * case this function is not meaningful. 0070 * - If this needs to be called, it needs to be called before V8 0071 * tries to make use of its built-ins. 0072 * - To avoid unnecessary copies of data, V8 will point directly into the 0073 * given data blob, so pretty please keep it around until V8 exit. 0074 * - Compression of the startup blob might be useful, but needs to 0075 * handled entirely on the embedders' side. 0076 * - The call will abort if the data is invalid. 0077 */ 0078 static void SetSnapshotDataBlob(StartupData* startup_blob); 0079 0080 /** Set the callback to invoke in case of Dcheck failures. */ 0081 static void SetDcheckErrorHandler(DcheckErrorCallback that); 0082 0083 /** Set the callback to invoke in the case of CHECK failures or fatal 0084 * errors. This is distinct from Isolate::SetFatalErrorHandler, which 0085 * is invoked in response to API usage failures. 0086 * */ 0087 static void SetFatalErrorHandler(V8FatalErrorCallback that); 0088 0089 /** 0090 * Sets V8 flags from a string. 0091 */ 0092 static void SetFlagsFromString(const char* str); 0093 static void SetFlagsFromString(const char* str, size_t length); 0094 0095 /** 0096 * Sets V8 flags from the command line. 0097 */ 0098 static void SetFlagsFromCommandLine(int* argc, char** argv, 0099 bool remove_flags); 0100 0101 /** Get the version string. */ 0102 static const char* GetVersion(); 0103 0104 /** 0105 * Initializes V8. This function needs to be called before the first Isolate 0106 * is created. It always returns true. 0107 */ 0108 V8_INLINE static bool Initialize() { 0109 #ifdef V8_TARGET_OS_ANDROID 0110 const bool kV8TargetOsIsAndroid = true; 0111 #else 0112 const bool kV8TargetOsIsAndroid = false; 0113 #endif 0114 0115 #ifdef V8_ENABLE_CHECKS 0116 const bool kV8EnableChecks = true; 0117 #else 0118 const bool kV8EnableChecks = false; 0119 #endif 0120 0121 const int kBuildConfiguration = 0122 (internal::PointerCompressionIsEnabled() ? kPointerCompression : 0) | 0123 (internal::SmiValuesAre31Bits() ? k31BitSmis : 0) | 0124 (internal::SandboxIsEnabled() ? kSandbox : 0) | 0125 (kV8TargetOsIsAndroid ? kTargetOsIsAndroid : 0) | 0126 (kV8EnableChecks ? kEnableChecks : 0); 0127 return Initialize(kBuildConfiguration); 0128 } 0129 0130 /** 0131 * Allows the host application to provide a callback which can be used 0132 * as a source of entropy for random number generators. 0133 */ 0134 static void SetEntropySource(EntropySource source); 0135 0136 /** 0137 * Allows the host application to provide a callback that allows v8 to 0138 * cooperate with a profiler that rewrites return addresses on stack. 0139 */ 0140 static void SetReturnAddressLocationResolver( 0141 ReturnAddressLocationResolver return_address_resolver); 0142 0143 /** 0144 * Releases any resources used by v8 and stops any utility threads 0145 * that may be running. Note that disposing v8 is permanent, it 0146 * cannot be reinitialized. 0147 * 0148 * It should generally not be necessary to dispose v8 before exiting 0149 * a process, this should happen automatically. It is only necessary 0150 * to use if the process needs the resources taken up by v8. 0151 */ 0152 static bool Dispose(); 0153 0154 /** 0155 * Initialize the ICU library bundled with V8. The embedder should only 0156 * invoke this method when using the bundled ICU. Returns true on success. 0157 * 0158 * If V8 was compiled with the ICU data in an external file, the location 0159 * of the data file has to be provided. 0160 */ 0161 static bool InitializeICU(const char* icu_data_file = nullptr); 0162 0163 /** 0164 * Initialize the ICU library bundled with V8. The embedder should only 0165 * invoke this method when using the bundled ICU. If V8 was compiled with 0166 * the ICU data in an external file and when the default location of that 0167 * file should be used, a path to the executable must be provided. 0168 * Returns true on success. 0169 * 0170 * The default is a file called icudtl.dat side-by-side with the executable. 0171 * 0172 * Optionally, the location of the data file can be provided to override the 0173 * default. 0174 */ 0175 static bool InitializeICUDefaultLocation(const char* exec_path, 0176 const char* icu_data_file = nullptr); 0177 0178 /** 0179 * Initialize the external startup data. The embedder only needs to 0180 * invoke this method when external startup data was enabled in a build. 0181 * 0182 * If V8 was compiled with the startup data in an external file, then 0183 * V8 needs to be given those external files during startup. There are 0184 * three ways to do this: 0185 * - InitializeExternalStartupData(const char*) 0186 * This will look in the given directory for the file "snapshot_blob.bin". 0187 * - InitializeExternalStartupDataFromFile(const char*) 0188 * As above, but will directly use the given file name. 0189 * - Call SetSnapshotDataBlob. 0190 * This will read the blobs from the given data structure and will 0191 * not perform any file IO. 0192 */ 0193 static void InitializeExternalStartupData(const char* directory_path); 0194 static void InitializeExternalStartupDataFromFile(const char* snapshot_blob); 0195 0196 /** 0197 * Sets the v8::Platform to use. This should be invoked before V8 is 0198 * initialized. 0199 */ 0200 static void InitializePlatform(Platform* platform); 0201 0202 /** 0203 * Clears all references to the v8::Platform. This should be invoked after 0204 * V8 was disposed. 0205 */ 0206 static void DisposePlatform(); 0207 0208 #if defined(V8_ENABLE_SANDBOX) 0209 /** 0210 * Returns true if the sandbox is configured securely. 0211 * 0212 * If V8 cannot create a regular sandbox during initialization, for example 0213 * because not enough virtual address space can be reserved, it will instead 0214 * create a fallback sandbox that still allows it to function normally but 0215 * does not have the same security properties as a regular sandbox. This API 0216 * can be used to determine if such a fallback sandbox is being used, in 0217 * which case it will return false. 0218 */ 0219 static bool IsSandboxConfiguredSecurely(); 0220 0221 /** 0222 * Provides access to the virtual address subspace backing the sandbox. 0223 * 0224 * This can be used to allocate pages inside the sandbox, for example to 0225 * obtain virtual memory for ArrayBuffer backing stores, which must be 0226 * located inside the sandbox. 0227 * 0228 * It should be assumed that an attacker can corrupt data inside the sandbox, 0229 * and so in particular the contents of pages allocagted in this virtual 0230 * address space, arbitrarily and concurrently. Due to this, it is 0231 * recommended to to only place pure data buffers in them. 0232 */ 0233 static VirtualAddressSpace* GetSandboxAddressSpace(); 0234 0235 /** 0236 * Returns the size of the sandbox in bytes. 0237 * 0238 * This represents the size of the address space that V8 can directly address 0239 * and in which it allocates its objects. 0240 */ 0241 static size_t GetSandboxSizeInBytes(); 0242 0243 /** 0244 * Returns the size of the address space reservation backing the sandbox. 0245 * 0246 * This may be larger than the sandbox (i.e. |GetSandboxSizeInBytes()|) due 0247 * to surrounding guard regions, or may be smaller than the sandbox in case a 0248 * fallback sandbox is being used, which will use a smaller virtual address 0249 * space reservation. In the latter case this will also be different from 0250 * |GetSandboxAddressSpace()->size()| as that will cover a larger part of the 0251 * address space than what has actually been reserved. 0252 */ 0253 static size_t GetSandboxReservationSizeInBytes(); 0254 #endif // V8_ENABLE_SANDBOX 0255 0256 enum class WasmMemoryType { 0257 kMemory32, 0258 kMemory64, 0259 }; 0260 0261 /** 0262 * Returns the virtual address space reservation size (in bytes) needed 0263 * for one WebAssembly memory instance of the given capacity. 0264 * 0265 * \param type Whether this is a memory32 or memory64 instance. 0266 * \param byte_capacity The maximum size, in bytes, of the WebAssembly 0267 * memory. Values exceeding the engine's maximum allocatable memory 0268 * size for the given type (determined by max_mem32_pages or 0269 * max_mem64_pages) are clamped. 0270 * 0271 * When trap-based bounds checking is enabled by 0272 * EnableWebAssemblyTrapHandler(), the amount of virtual address space 0273 * that V8 needs to reserve for each WebAssembly memory instance can 0274 * be much bigger than the requested size. If the process does 0275 * not have enough virtual memory available, WebAssembly memory allocation 0276 * would fail. During the initialization of V8, embedders can use this method 0277 * to estimate whether the process has enough virtual memory for their 0278 * usage of WebAssembly, and decide whether to enable the trap handler 0279 * via EnableWebAssemblyTrapHandler(), or to skip it and reduce the amount of 0280 * virtual memory required to keep the application running. 0281 */ 0282 static size_t GetWasmMemoryReservationSizeInBytes(WasmMemoryType type, 0283 size_t byte_capacity); 0284 0285 /** 0286 * Activate trap-based bounds checking for WebAssembly. 0287 * 0288 * \param use_v8_signal_handler Whether V8 should install its own signal 0289 * handler or rely on the embedder's. 0290 */ 0291 static bool EnableWebAssemblyTrapHandler(bool use_v8_signal_handler); 0292 0293 #if defined(V8_OS_WIN) 0294 /** 0295 * On Win64, by default V8 does not emit unwinding data for jitted code, 0296 * which means the OS cannot walk the stack frames and the system Structured 0297 * Exception Handling (SEH) cannot unwind through V8-generated code: 0298 * https://code.google.com/p/v8/issues/detail?id=3598. 0299 * 0300 * This function allows embedders to register a custom exception handler for 0301 * exceptions in V8-generated code. 0302 */ 0303 static void SetUnhandledExceptionCallback( 0304 UnhandledExceptionCallback callback); 0305 #endif 0306 0307 /** 0308 * Allows the host application to provide a callback that will be called when 0309 * v8 has encountered a fatal failure to allocate memory and is about to 0310 * terminate. 0311 */ 0312 static void SetFatalMemoryErrorCallback(OOMErrorCallback callback); 0313 0314 /** 0315 * Get statistics about the shared memory usage. 0316 */ 0317 static void GetSharedMemoryStatistics(SharedMemoryStatistics* statistics); 0318 0319 private: 0320 V8(); 0321 0322 enum BuildConfigurationFeatures { 0323 kPointerCompression = 1 << 0, 0324 k31BitSmis = 1 << 1, 0325 kSandbox = 1 << 2, 0326 kTargetOsIsAndroid = 1 << 3, 0327 kEnableChecks = 1 << 4, 0328 }; 0329 0330 /** 0331 * Checks that the embedder build configuration is compatible with 0332 * the V8 binary and if so initializes V8. 0333 */ 0334 static bool Initialize(int build_config); 0335 0336 friend class Context; 0337 template <class K, class V, class T> 0338 friend class PersistentValueMapBase; 0339 }; 0340 0341 } // namespace v8 0342 0343 #endif // INCLUDE_V8_INITIALIZATION_H_
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|