Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:05:23

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_ISOLATE_CALLBACKS_H_
0006 #define INCLUDE_V8_ISOLATE_CALLBACKS_H_
0007 
0008 #include <stddef.h>
0009 
0010 #include <functional>
0011 #include <string>
0012 
0013 #include "cppgc/common.h"
0014 #include "v8-data.h"          // NOLINT(build/include_directory)
0015 #include "v8-local-handle.h"  // NOLINT(build/include_directory)
0016 #include "v8-promise.h"       // NOLINT(build/include_directory)
0017 #include "v8config.h"         // NOLINT(build/include_directory)
0018 
0019 #if defined(V8_OS_WIN)
0020 struct _EXCEPTION_POINTERS;
0021 #endif
0022 
0023 namespace v8 {
0024 
0025 template <typename T>
0026 class FunctionCallbackInfo;
0027 class Isolate;
0028 class Message;
0029 class Module;
0030 class Object;
0031 class Promise;
0032 class ScriptOrModule;
0033 class String;
0034 class UnboundScript;
0035 class Value;
0036 
0037 /**
0038  * A JIT code event is issued each time code is added, moved or removed.
0039  *
0040  * \note removal events are not currently issued.
0041  */
0042 struct JitCodeEvent {
0043   enum EventType {
0044     CODE_ADDED,
0045     CODE_MOVED,
0046     CODE_REMOVED,
0047     CODE_ADD_LINE_POS_INFO,
0048     CODE_START_LINE_INFO_RECORDING,
0049     CODE_END_LINE_INFO_RECORDING
0050   };
0051   // Definition of the code position type. The "POSITION" type means the place
0052   // in the source code which are of interest when making stack traces to
0053   // pin-point the source location of a stack frame as close as possible.
0054   // The "STATEMENT_POSITION" means the place at the beginning of each
0055   // statement, and is used to indicate possible break locations.
0056   enum PositionType { POSITION, STATEMENT_POSITION };
0057 
0058   // There are three different kinds of CodeType, one for JIT code generated
0059   // by the optimizing compiler, one for byte code generated for the
0060   // interpreter, and one for code generated from Wasm. For JIT_CODE and
0061   // WASM_CODE, |code_start| points to the beginning of jitted assembly code,
0062   // while for BYTE_CODE events, |code_start| points to the first bytecode of
0063   // the interpreted function.
0064   enum CodeType { BYTE_CODE, JIT_CODE, WASM_CODE };
0065 
0066   // Type of event.
0067   EventType type;
0068   CodeType code_type;
0069   // Start of the instructions.
0070   void* code_start;
0071   // Size of the instructions.
0072   size_t code_len;
0073   // Script info for CODE_ADDED event.
0074   Local<UnboundScript> script;
0075   // User-defined data for *_LINE_INFO_* event. It's used to hold the source
0076   // code line information which is returned from the
0077   // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
0078   // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
0079   void* user_data;
0080 
0081   struct name_t {
0082     // Name of the object associated with the code, note that the string is not
0083     // zero-terminated.
0084     const char* str;
0085     // Number of chars in str.
0086     size_t len;
0087   };
0088 
0089   struct line_info_t {
0090     // PC offset
0091     size_t offset;
0092     // Code position
0093     size_t pos;
0094     // The position type.
0095     PositionType position_type;
0096   };
0097 
0098   struct wasm_source_info_t {
0099     // Source file name.
0100     const char* filename;
0101     // Length of filename.
0102     size_t filename_size;
0103     // Line number table, which maps offsets of JITted code to line numbers of
0104     // source file.
0105     const line_info_t* line_number_table;
0106     // Number of entries in the line number table.
0107     size_t line_number_table_size;
0108   };
0109 
0110   wasm_source_info_t* wasm_source_info = nullptr;
0111 
0112   union {
0113     // Only valid for CODE_ADDED.
0114     struct name_t name;
0115 
0116     // Only valid for CODE_ADD_LINE_POS_INFO
0117     struct line_info_t line_info;
0118 
0119     // New location of instructions. Only valid for CODE_MOVED.
0120     void* new_code_start;
0121   };
0122 
0123   Isolate* isolate;
0124 };
0125 
0126 /**
0127  * Option flags passed to the SetJitCodeEventHandler function.
0128  */
0129 enum JitCodeEventOptions {
0130   kJitCodeEventDefault = 0,
0131   // Generate callbacks for already existent code.
0132   kJitCodeEventEnumExisting = 1
0133 };
0134 
0135 /**
0136  * Callback function passed to SetJitCodeEventHandler.
0137  *
0138  * \param event code add, move or removal event.
0139  */
0140 using JitCodeEventHandler = void (*)(const JitCodeEvent* event);
0141 
0142 // --- Garbage Collection Callbacks ---
0143 
0144 /**
0145  * Applications can register callback functions which will be called before and
0146  * after certain garbage collection operations.  Allocations are not allowed in
0147  * the callback functions, you therefore cannot manipulate objects (set or
0148  * delete properties for example) since it is possible such operations will
0149  * result in the allocation of objects.
0150  * TODO(v8:12612): Deprecate kGCTypeMinorMarkSweep after updating blink.
0151  */
0152 enum GCType {
0153   kGCTypeScavenge = 1 << 0,
0154   kGCTypeMinorMarkSweep = 1 << 1,
0155   kGCTypeMinorMarkCompact V8_DEPRECATE_SOON(
0156       "Use kGCTypeMinorMarkSweep instead of kGCTypeMinorMarkCompact.") =
0157       kGCTypeMinorMarkSweep,
0158   kGCTypeMarkSweepCompact = 1 << 2,
0159   kGCTypeIncrementalMarking = 1 << 3,
0160   kGCTypeProcessWeakCallbacks = 1 << 4,
0161   kGCTypeAll = kGCTypeScavenge | kGCTypeMinorMarkSweep |
0162                kGCTypeMarkSweepCompact | kGCTypeIncrementalMarking |
0163                kGCTypeProcessWeakCallbacks
0164 };
0165 
0166 /**
0167  * GCCallbackFlags is used to notify additional information about the GC
0168  * callback.
0169  *   - kGCCallbackFlagConstructRetainedObjectInfos: The GC callback is for
0170  *     constructing retained object infos.
0171  *   - kGCCallbackFlagForced: The GC callback is for a forced GC for testing.
0172  *   - kGCCallbackFlagSynchronousPhantomCallbackProcessing: The GC callback
0173  *     is called synchronously without getting posted to an idle task.
0174  *   - kGCCallbackFlagCollectAllAvailableGarbage: The GC callback is called
0175  *     in a phase where V8 is trying to collect all available garbage
0176  *     (e.g., handling a low memory notification).
0177  *   - kGCCallbackScheduleIdleGarbageCollection: The GC callback is called to
0178  *     trigger an idle garbage collection.
0179  */
0180 enum GCCallbackFlags {
0181   kNoGCCallbackFlags = 0,
0182   kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1,
0183   kGCCallbackFlagForced = 1 << 2,
0184   kGCCallbackFlagSynchronousPhantomCallbackProcessing = 1 << 3,
0185   kGCCallbackFlagCollectAllAvailableGarbage = 1 << 4,
0186   kGCCallbackFlagCollectAllExternalMemory = 1 << 5,
0187   kGCCallbackScheduleIdleGarbageCollection = 1 << 6,
0188 };
0189 
0190 using GCCallback = void (*)(GCType type, GCCallbackFlags flags);
0191 
0192 using InterruptCallback = void (*)(Isolate* isolate, void* data);
0193 
0194 /**
0195  * This callback is invoked when the heap size is close to the heap limit and
0196  * V8 is likely to abort with out-of-memory error.
0197  * The callback can extend the heap limit by returning a value that is greater
0198  * than the current_heap_limit. The initial heap limit is the limit that was
0199  * set after heap setup.
0200  */
0201 using NearHeapLimitCallback = size_t (*)(void* data, size_t current_heap_limit,
0202                                          size_t initial_heap_limit);
0203 
0204 /**
0205  * Callback function passed to SetUnhandledExceptionCallback.
0206  */
0207 #if defined(V8_OS_WIN)
0208 using UnhandledExceptionCallback =
0209     int (*)(_EXCEPTION_POINTERS* exception_pointers);
0210 #endif
0211 
0212 // --- Counters Callbacks ---
0213 
0214 using CounterLookupCallback = int* (*)(const char* name);
0215 
0216 using CreateHistogramCallback = void* (*)(const char* name, int min, int max,
0217                                           size_t buckets);
0218 
0219 using AddHistogramSampleCallback = void (*)(void* histogram, int sample);
0220 
0221 // --- Exceptions ---
0222 
0223 using FatalErrorCallback = void (*)(const char* location, const char* message);
0224 
0225 struct OOMDetails {
0226   bool is_heap_oom = false;
0227   const char* detail = nullptr;
0228 };
0229 
0230 using OOMErrorCallback = void (*)(const char* location,
0231                                   const OOMDetails& details);
0232 
0233 using MessageCallback = void (*)(Local<Message> message, Local<Value> data);
0234 
0235 // --- Tracing ---
0236 
0237 enum LogEventStatus : int { kStart = 0, kEnd = 1, kStamp = 2 };
0238 using LogEventCallback = void (*)(const char* name,
0239                                   int /* LogEventStatus */ status);
0240 
0241 // --- Crashkeys Callback ---
0242 enum class CrashKeyId {
0243   kIsolateAddress,
0244   kReadonlySpaceFirstPageAddress,
0245   kMapSpaceFirstPageAddress V8_ENUM_DEPRECATE_SOON("Map space got removed"),
0246   kOldSpaceFirstPageAddress,
0247   kCodeRangeBaseAddress,
0248   kCodeSpaceFirstPageAddress,
0249   kDumpType,
0250   kSnapshotChecksumCalculated,
0251   kSnapshotChecksumExpected,
0252 };
0253 
0254 using AddCrashKeyCallback = void (*)(CrashKeyId id, const std::string& value);
0255 
0256 // --- Enter/Leave Script Callback ---
0257 using BeforeCallEnteredCallback = void (*)(Isolate*);
0258 using CallCompletedCallback = void (*)(Isolate*);
0259 
0260 // --- AllowCodeGenerationFromStrings callbacks ---
0261 
0262 /**
0263  * Callback to check if code generation from strings is allowed. See
0264  * Context::AllowCodeGenerationFromStrings.
0265  */
0266 using AllowCodeGenerationFromStringsCallback = bool (*)(Local<Context> context,
0267                                                         Local<String> source);
0268 
0269 struct ModifyCodeGenerationFromStringsResult {
0270   // If true, proceed with the codegen algorithm. Otherwise, block it.
0271   bool codegen_allowed = false;
0272   // Overwrite the original source with this string, if present.
0273   // Use the original source if empty.
0274   // This field is considered only if codegen_allowed is true.
0275   MaybeLocal<String> modified_source;
0276 };
0277 
0278 /**
0279  * Access type specification.
0280  */
0281 enum AccessType {
0282   ACCESS_GET,
0283   ACCESS_SET,
0284   ACCESS_HAS,
0285   ACCESS_DELETE,
0286   ACCESS_KEYS
0287 };
0288 
0289 // --- Failed Access Check Callback ---
0290 
0291 using FailedAccessCheckCallback = void (*)(Local<Object> target,
0292                                            AccessType type, Local<Value> data);
0293 
0294 /**
0295  * Callback to check if codegen is allowed from a source object, and convert
0296  * the source to string if necessary. See: ModifyCodeGenerationFromStrings.
0297  */
0298 using ModifyCodeGenerationFromStringsCallback =
0299     ModifyCodeGenerationFromStringsResult (*)(Local<Context> context,
0300                                               Local<Value> source);
0301 using ModifyCodeGenerationFromStringsCallback2 =
0302     ModifyCodeGenerationFromStringsResult (*)(Local<Context> context,
0303                                               Local<Value> source,
0304                                               bool is_code_like);
0305 
0306 // --- WebAssembly compilation callbacks ---
0307 using ExtensionCallback = bool (*)(const FunctionCallbackInfo<Value>&);
0308 
0309 using AllowWasmCodeGenerationCallback = bool (*)(Local<Context> context,
0310                                                  Local<String> source);
0311 
0312 // --- Callback for APIs defined on v8-supported objects, but implemented
0313 // by the embedder. Example: WebAssembly.{compile|instantiate}Streaming ---
0314 using ApiImplementationCallback = void (*)(const FunctionCallbackInfo<Value>&);
0315 
0316 // --- Callback for WebAssembly.compileStreaming ---
0317 using WasmStreamingCallback = void (*)(const FunctionCallbackInfo<Value>&);
0318 
0319 enum class WasmAsyncSuccess { kSuccess, kFail };
0320 
0321 // --- Callback called when async WebAssembly operations finish ---
0322 using WasmAsyncResolvePromiseCallback = void (*)(
0323     Isolate* isolate, Local<Context> context, Local<Promise::Resolver> resolver,
0324     Local<Value> result, WasmAsyncSuccess success);
0325 
0326 // --- Callback for loading source map file for Wasm profiling support
0327 using WasmLoadSourceMapCallback = Local<String> (*)(Isolate* isolate,
0328                                                     const char* name);
0329 
0330 // --- Callback for checking if WebAssembly imported strings are enabled ---
0331 using WasmImportedStringsEnabledCallback = bool (*)(Local<Context> context);
0332 
0333 // --- Callback for checking if the SharedArrayBuffer constructor is enabled ---
0334 using SharedArrayBufferConstructorEnabledCallback =
0335     bool (*)(Local<Context> context);
0336 
0337 // --- Callback for checking if the compile hints magic comments are enabled ---
0338 using JavaScriptCompileHintsMagicEnabledCallback =
0339     bool (*)(Local<Context> context);
0340 
0341 // --- Callback for checking if WebAssembly JSPI is enabled ---
0342 using WasmJSPIEnabledCallback = bool (*)(Local<Context> context);
0343 
0344 /**
0345  * HostImportModuleDynamicallyCallback is called when we
0346  * require the embedder to load a module. This is used as part of the dynamic
0347  * import syntax.
0348  *
0349  * The referrer contains metadata about the script/module that calls
0350  * import.
0351  *
0352  * The specifier is the name of the module that should be imported.
0353  *
0354  * The import_attributes are import attributes for this request in the form:
0355  * [key1, value1, key2, value2, ...] where the keys and values are of type
0356  * v8::String. Note, unlike the FixedArray passed to ResolveModuleCallback and
0357  * returned from ModuleRequest::GetImportAssertions(), this array does not
0358  * contain the source Locations of the attributes.
0359  *
0360  * The embedder must compile, instantiate, evaluate the Module, and
0361  * obtain its namespace object.
0362  *
0363  * The Promise returned from this function is forwarded to userland
0364  * JavaScript. The embedder must resolve this promise with the module
0365  * namespace object. In case of an exception, the embedder must reject
0366  * this promise with the exception. If the promise creation itself
0367  * fails (e.g. due to stack overflow), the embedder must propagate
0368  * that exception by returning an empty MaybeLocal.
0369  */
0370 using HostImportModuleDynamicallyCallback = MaybeLocal<Promise> (*)(
0371     Local<Context> context, Local<Data> host_defined_options,
0372     Local<Value> resource_name, Local<String> specifier,
0373     Local<FixedArray> import_attributes);
0374 
0375 /**
0376  * Callback for requesting a compile hint for a function from the embedder. The
0377  * first parameter is the position of the function in source code and the second
0378  * parameter is embedder data to be passed back.
0379  */
0380 using CompileHintCallback = bool (*)(int, void*);
0381 
0382 /**
0383  * HostInitializeImportMetaObjectCallback is called the first time import.meta
0384  * is accessed for a module. Subsequent access will reuse the same value.
0385  *
0386  * The method combines two implementation-defined abstract operations into one:
0387  * HostGetImportMetaProperties and HostFinalizeImportMeta.
0388  *
0389  * The embedder should use v8::Object::CreateDataProperty to add properties on
0390  * the meta object.
0391  */
0392 using HostInitializeImportMetaObjectCallback = void (*)(Local<Context> context,
0393                                                         Local<Module> module,
0394                                                         Local<Object> meta);
0395 
0396 /**
0397  * HostCreateShadowRealmContextCallback is called each time a ShadowRealm is
0398  * being constructed in the initiator_context.
0399  *
0400  * The method combines Context creation and implementation defined abstract
0401  * operation HostInitializeShadowRealm into one.
0402  *
0403  * The embedder should use v8::Context::New or v8::Context:NewFromSnapshot to
0404  * create a new context. If the creation fails, the embedder must propagate
0405  * that exception by returning an empty MaybeLocal.
0406  */
0407 using HostCreateShadowRealmContextCallback =
0408     MaybeLocal<Context> (*)(Local<Context> initiator_context);
0409 
0410 /**
0411  * PrepareStackTraceCallback is called when the stack property of an error is
0412  * first accessed. The return value will be used as the stack value. If this
0413  * callback is registed, the |Error.prepareStackTrace| API will be disabled.
0414  * |sites| is an array of call sites, specified in
0415  * https://v8.dev/docs/stack-trace-api
0416  */
0417 using PrepareStackTraceCallback = MaybeLocal<Value> (*)(Local<Context> context,
0418                                                         Local<Value> error,
0419                                                         Local<Array> sites);
0420 
0421 #if defined(V8_OS_WIN)
0422 /**
0423  * Callback to selectively enable ETW tracing based on the document URL.
0424  * Implemented by the embedder, it should never call back into V8.
0425  *
0426  * Windows allows passing additional data to the ETW EnableCallback:
0427  * https://learn.microsoft.com/en-us/windows/win32/api/evntprov/nc-evntprov-penablecallback
0428  *
0429  * This data can be configured in a WPR (Windows Performance Recorder)
0430  * profile, adding a CustomFilter to an EventProvider like the following:
0431  *
0432  * <EventProvider Id=".." Name="57277741-3638-4A4B-BDBA-0AC6E45DA56C" Level="5">
0433  *   <CustomFilter Type="0x80000000" Value="AQABAAAAAAA..." />
0434  * </EventProvider>
0435  *
0436  * Where:
0437  * - Name="57277741-3638-4A4B-BDBA-0AC6E45DA56C" is the GUID of the V8
0438  *     ETW provider, (see src/libplatform/etw/etw-provider-win.h),
0439  * - Type="0x80000000" is EVENT_FILTER_TYPE_SCHEMATIZED,
0440  * - Value="AQABAAAAAA..." is a base64-encoded byte array that is
0441  *     base64-decoded by Windows and passed to the ETW enable callback in
0442  *     the 'PEVENT_FILTER_DESCRIPTOR FilterData' argument; see:
0443  * https://learn.microsoft.com/en-us/windows/win32/api/evntprov/ns-evntprov-event_filter_descriptor.
0444  *
0445  * This array contains a struct EVENT_FILTER_HEADER followed by a
0446  * variable length payload, and as payload we pass a string in JSON format,
0447  * with a list of regular expressions that should match the document URL
0448  * in order to enable ETW tracing:
0449  *   {
0450  *     "version": "1.0",
0451  *     "filtered_urls": [
0452  *         "https:\/\/.*\.chromium\.org\/.*", "https://v8.dev/";, "..."
0453  *     ]
0454  *  }
0455  */
0456 using FilterETWSessionByURLCallback =
0457     bool (*)(Local<Context> context, const std::string& etw_filter_payload);
0458 #endif  // V8_OS_WIN
0459 
0460 }  // namespace v8
0461 
0462 #endif  // INCLUDE_V8_ISOLATE_CALLBACKS_H_