![]() |
|
|||
File indexing completed on 2025-02-21 10:05:24
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 /** 0056 * Container class for static utility functions. 0057 */ 0058 class V8_EXPORT V8 { 0059 public: 0060 /** 0061 * Hand startup data to V8, in case the embedder has chosen to build 0062 * V8 with external startup data. 0063 * 0064 * Note: 0065 * - By default the startup data is linked into the V8 library, in which 0066 * case this function is not meaningful. 0067 * - If this needs to be called, it needs to be called before V8 0068 * tries to make use of its built-ins. 0069 * - To avoid unnecessary copies of data, V8 will point directly into the 0070 * given data blob, so pretty please keep it around until V8 exit. 0071 * - Compression of the startup blob might be useful, but needs to 0072 * handled entirely on the embedders' side. 0073 * - The call will abort if the data is invalid. 0074 */ 0075 static void SetSnapshotDataBlob(StartupData* startup_blob); 0076 0077 /** Set the callback to invoke in case of Dcheck failures. */ 0078 static void SetDcheckErrorHandler(DcheckErrorCallback that); 0079 0080 /** 0081 * Sets V8 flags from a string. 0082 */ 0083 static void SetFlagsFromString(const char* str); 0084 static void SetFlagsFromString(const char* str, size_t length); 0085 0086 /** 0087 * Sets V8 flags from the command line. 0088 */ 0089 static void SetFlagsFromCommandLine(int* argc, char** argv, 0090 bool remove_flags); 0091 0092 /** Get the version string. */ 0093 static const char* GetVersion(); 0094 0095 /** 0096 * Initializes V8. This function needs to be called before the first Isolate 0097 * is created. It always returns true. 0098 */ 0099 V8_INLINE static bool Initialize() { 0100 const int kBuildConfiguration = 0101 (internal::PointerCompressionIsEnabled() ? kPointerCompression : 0) | 0102 (internal::SmiValuesAre31Bits() ? k31BitSmis : 0) | 0103 (internal::SandboxIsEnabled() ? kSandbox : 0); 0104 return Initialize(kBuildConfiguration); 0105 } 0106 0107 /** 0108 * Allows the host application to provide a callback which can be used 0109 * as a source of entropy for random number generators. 0110 */ 0111 static void SetEntropySource(EntropySource source); 0112 0113 /** 0114 * Allows the host application to provide a callback that allows v8 to 0115 * cooperate with a profiler that rewrites return addresses on stack. 0116 */ 0117 static void SetReturnAddressLocationResolver( 0118 ReturnAddressLocationResolver return_address_resolver); 0119 0120 /** 0121 * Releases any resources used by v8 and stops any utility threads 0122 * that may be running. Note that disposing v8 is permanent, it 0123 * cannot be reinitialized. 0124 * 0125 * It should generally not be necessary to dispose v8 before exiting 0126 * a process, this should happen automatically. It is only necessary 0127 * to use if the process needs the resources taken up by v8. 0128 */ 0129 static bool Dispose(); 0130 0131 /** 0132 * Initialize the ICU library bundled with V8. The embedder should only 0133 * invoke this method when using the bundled ICU. Returns true on success. 0134 * 0135 * If V8 was compiled with the ICU data in an external file, the location 0136 * of the data file has to be provided. 0137 */ 0138 static bool InitializeICU(const char* icu_data_file = nullptr); 0139 0140 /** 0141 * Initialize the ICU library bundled with V8. The embedder should only 0142 * invoke this method when using the bundled ICU. If V8 was compiled with 0143 * the ICU data in an external file and when the default location of that 0144 * file should be used, a path to the executable must be provided. 0145 * Returns true on success. 0146 * 0147 * The default is a file called icudtl.dat side-by-side with the executable. 0148 * 0149 * Optionally, the location of the data file can be provided to override the 0150 * default. 0151 */ 0152 static bool InitializeICUDefaultLocation(const char* exec_path, 0153 const char* icu_data_file = nullptr); 0154 0155 /** 0156 * Initialize the external startup data. The embedder only needs to 0157 * invoke this method when external startup data was enabled in a build. 0158 * 0159 * If V8 was compiled with the startup data in an external file, then 0160 * V8 needs to be given those external files during startup. There are 0161 * three ways to do this: 0162 * - InitializeExternalStartupData(const char*) 0163 * This will look in the given directory for the file "snapshot_blob.bin". 0164 * - InitializeExternalStartupDataFromFile(const char*) 0165 * As above, but will directly use the given file name. 0166 * - Call SetSnapshotDataBlob. 0167 * This will read the blobs from the given data structure and will 0168 * not perform any file IO. 0169 */ 0170 static void InitializeExternalStartupData(const char* directory_path); 0171 static void InitializeExternalStartupDataFromFile(const char* snapshot_blob); 0172 0173 /** 0174 * Sets the v8::Platform to use. This should be invoked before V8 is 0175 * initialized. 0176 */ 0177 static void InitializePlatform(Platform* platform); 0178 0179 /** 0180 * Clears all references to the v8::Platform. This should be invoked after 0181 * V8 was disposed. 0182 */ 0183 static void DisposePlatform(); 0184 0185 #if defined(V8_ENABLE_SANDBOX) 0186 /** 0187 * Returns true if the sandbox is configured securely. 0188 * 0189 * If V8 cannot create a regular sandbox during initialization, for example 0190 * because not enough virtual address space can be reserved, it will instead 0191 * create a fallback sandbox that still allows it to function normally but 0192 * does not have the same security properties as a regular sandbox. This API 0193 * can be used to determine if such a fallback sandbox is being used, in 0194 * which case it will return false. 0195 */ 0196 static bool IsSandboxConfiguredSecurely(); 0197 0198 /** 0199 * Provides access to the virtual address subspace backing the sandbox. 0200 * 0201 * This can be used to allocate pages inside the sandbox, for example to 0202 * obtain virtual memory for ArrayBuffer backing stores, which must be 0203 * located inside the sandbox. 0204 * 0205 * It should be assumed that an attacker can corrupt data inside the sandbox, 0206 * and so in particular the contents of pages allocagted in this virtual 0207 * address space, arbitrarily and concurrently. Due to this, it is 0208 * recommended to to only place pure data buffers in them. 0209 */ 0210 static VirtualAddressSpace* GetSandboxAddressSpace(); 0211 0212 /** 0213 * Returns the size of the sandbox in bytes. 0214 * 0215 * This represents the size of the address space that V8 can directly address 0216 * and in which it allocates its objects. 0217 */ 0218 static size_t GetSandboxSizeInBytes(); 0219 0220 /** 0221 * Returns the size of the address space reservation backing the sandbox. 0222 * 0223 * This may be larger than the sandbox (i.e. |GetSandboxSizeInBytes()|) due 0224 * to surrounding guard regions, or may be smaller than the sandbox in case a 0225 * fallback sandbox is being used, which will use a smaller virtual address 0226 * space reservation. In the latter case this will also be different from 0227 * |GetSandboxAddressSpace()->size()| as that will cover a larger part of the 0228 * address space than what has actually been reserved. 0229 */ 0230 static size_t GetSandboxReservationSizeInBytes(); 0231 #endif // V8_ENABLE_SANDBOX 0232 0233 /** 0234 * Activate trap-based bounds checking for WebAssembly. 0235 * 0236 * \param use_v8_signal_handler Whether V8 should install its own signal 0237 * handler or rely on the embedder's. 0238 */ 0239 static bool EnableWebAssemblyTrapHandler(bool use_v8_signal_handler); 0240 0241 #if defined(V8_OS_WIN) 0242 /** 0243 * On Win64, by default V8 does not emit unwinding data for jitted code, 0244 * which means the OS cannot walk the stack frames and the system Structured 0245 * Exception Handling (SEH) cannot unwind through V8-generated code: 0246 * https://code.google.com/p/v8/issues/detail?id=3598. 0247 * 0248 * This function allows embedders to register a custom exception handler for 0249 * exceptions in V8-generated code. 0250 */ 0251 static void SetUnhandledExceptionCallback( 0252 UnhandledExceptionCallback callback); 0253 #endif 0254 0255 /** 0256 * Allows the host application to provide a callback that will be called when 0257 * v8 has encountered a fatal failure to allocate memory and is about to 0258 * terminate. 0259 */ 0260 static void SetFatalMemoryErrorCallback(OOMErrorCallback callback); 0261 0262 /** 0263 * Get statistics about the shared memory usage. 0264 */ 0265 static void GetSharedMemoryStatistics(SharedMemoryStatistics* statistics); 0266 0267 private: 0268 V8(); 0269 0270 enum BuildConfigurationFeatures { 0271 kPointerCompression = 1 << 0, 0272 k31BitSmis = 1 << 1, 0273 kSandbox = 1 << 2, 0274 }; 0275 0276 /** 0277 * Checks that the embedder build configuration is compatible with 0278 * the V8 binary and if so initializes V8. 0279 */ 0280 static bool Initialize(int build_config); 0281 0282 friend class Context; 0283 template <class K, class V, class T> 0284 friend class PersistentValueMapBase; 0285 }; 0286 0287 } // namespace v8 0288 0289 #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 |
![]() ![]() |