v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
wasm-objects.h
Go to the documentation of this file.
1// Copyright 2016 the V8 project authors. All rights reserved. Use of
2// this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_WASM_WASM_OBJECTS_H_
6#define V8_WASM_WASM_OBJECTS_H_
7
8#if !V8_ENABLE_WEBASSEMBLY
9#error This header should only be included if WebAssembly is enabled.
10#endif // !V8_ENABLE_WEBASSEMBLY
11
12#include <memory>
13#include <optional>
14
15#include "include/v8-wasm.h"
16#include "src/base/bit-field.h"
18#include "src/heap/heap.h"
20#include "src/objects/casting.h"
21#include "src/objects/foreign.h"
25#include "src/objects/objects.h"
26#include "src/objects/struct.h"
29#include "src/wasm/stacks.h"
31#include "src/wasm/value-type.h"
34
35// Has to be the last include (doesn't have include guards)
37
38namespace v8 {
39namespace internal {
40namespace wasm {
41class NativeModule;
42class WasmCode;
43struct WasmFunction;
44struct WasmGlobal;
45struct WasmModule;
46struct WasmTag;
48class WasmValue;
49class WireBytesRef;
50} // namespace wasm
51
52class BreakPoint;
53class JSArrayBuffer;
54class SeqOneByteString;
55class StructBodyDescriptor;
56class WasmCapiFunction;
57class WasmExceptionTag;
58class WasmExportedFunction;
59class WasmExternalFunction;
61class WasmJSFunction;
62class WasmModuleObject;
63
64enum class SharedFlag : uint8_t;
65
66template <typename CppType>
67class Managed;
68template <typename CppType>
70
71#include "torque-generated/src/wasm/wasm-objects-tq.inc"
72
73#define DECL_OPTIONAL_ACCESSORS(name, type) \
74 DECL_GETTER(has_##name, bool) \
75 DECL_ACCESSORS(name, type)
76
78 public:
80 Isolate* isolate,
81 DirectHandle<WasmTrustedInstanceData> target_instance_data,
82 int target_func_index);
83 // The "implicit_arg" will be a WasmTrustedInstanceData or a WasmImportData.
84 DirectHandle<TrustedObject> implicit_arg() { return implicit_arg_; }
85 WasmCodePointer call_target() { return call_target_; }
86
87#if V8_ENABLE_DRUMBRAKE
88 int target_func_index() { return target_func_index_; }
89#endif // V8_ENABLE_DRUMBRAKE
90
91 private:
94
95#if V8_ENABLE_DRUMBRAKE
96 int target_func_index_;
97#endif // V8_ENABLE_DRUMBRAKE
98};
99
100namespace wasm {
101enum class OnResume : int { kContinue, kThrow };
102} // namespace wasm
103
104// A helper for an entry for an imported function, indexed statically.
105// The underlying storage in the instance is used by generated code to
106// call imported functions at runtime.
107// Each entry is either:
108// - Wasm to JS, which has fields
109// - object = a WasmImportData
110// - target = entrypoint to import wrapper code
111// - Wasm to Wasm, which has fields
112// - object = target instance data
113// - target = entrypoint for the function
115 public:
117 int index);
118
119 // Initialize this entry as a Wasm to JS call. This accepts the isolate as a
120 // parameter since it allocates a WasmImportData.
122 wasm::Suspend suspend, const wasm::CanonicalSig* sig,
126 wasm::WasmCode* wasm_to_js_wrapper,
127 wasm::Suspend suspend,
128 const wasm::CanonicalSig* sig,
130
131 // Initialize this entry as a Wasm to Wasm call.
132 void SetWasmToWasm(Tagged<WasmTrustedInstanceData> target_instance_object,
133 WasmCodePointer call_target,
135#if V8_ENABLE_DRUMBRAKE
136 ,
137 int exported_function_index
138#endif // V8_ENABLE_DRUMBRAKE
139 );
140
145
146#if V8_ENABLE_DRUMBRAKE
147 int function_index_in_called_module();
148#endif // V8_ENABLE_DRUMBRAKE
149
150 private:
152 int const index_;
153};
154
155enum InternalizeString : bool { kInternalize = true, kNoInternalize = false };
156
157// Representation of a WebAssembly.Module JavaScript-level object.
159 : public TorqueGeneratedWasmModuleObject<WasmModuleObject, JSObject> {
160 public:
161 inline wasm::NativeModule* native_module() const;
162 inline const std::shared_ptr<wasm::NativeModule>& shared_native_module()
163 const;
164 inline const wasm::WasmModule* module() const;
165
166 // Dispatched behavior.
168
169 // Creates a new {WasmModuleObject} for an existing {NativeModule} that is
170 // reference counted and might be shared between multiple Isolates.
172 Isolate* isolate, std::shared_ptr<wasm::NativeModule> native_module,
173 DirectHandle<Script> script);
174
175 // Check whether this module was generated from asm.js source.
176 inline bool is_asm_js();
177
178 // Get the module name, if set. Returns an empty handle otherwise.
181
182 // Get the function name of the function identified by the given index.
183 // Returns a null handle if the function is unnamed or the name is not a valid
184 // UTF-8 string.
186 Isolate*, DirectHandle<WasmModuleObject>, uint32_t func_index);
187
188 // Get the raw bytes of the function name of the function identified by the
189 // given index.
190 // Meant to be used for debugging or frame printing.
191 // Does not allocate, hence gc-safe.
193
194 // Extract a portion of the wire bytes as UTF-8 string, optionally
195 // internalized. (Prefer to internalize early if the string will be used for a
196 // property lookup anyway.)
203
205};
206
207#if V8_ENABLE_SANDBOX || DEBUG
208// This should be checked on all code paths that write into WasmDispatchTables.
209bool FunctionSigMatchesTable(wasm::CanonicalTypeIndex sig_id,
210 wasm::CanonicalValueType table_type);
211#endif
212
213// Representation of a WebAssembly.Table JavaScript-level object.
215 : public TorqueGeneratedWasmTableObject<WasmTableObject, JSObject> {
216 public:
217 class BodyDescriptor;
218
219 inline wasm::ValueType type(const wasm::WasmModule* module);
221 const wasm::WasmModule* module);
222 // Use this when you don't care whether the type stored on the in-sandbox
223 // object might have been corrupted to contain an invalid type index.
224 // That implies that you can't even canonicalize the type!
226
229
231
232 V8_EXPORT_PRIVATE static int Grow(Isolate* isolate,
234 uint32_t count,
235 DirectHandle<Object> init_value);
236
237 // TODO(jkummerow): Consider getting rid of {type}, use {canonical_type}
238 // instead.
242 uint32_t initial, bool has_maximum, uint64_t maximum,
243 DirectHandle<Object> initial_value, wasm::AddressType address_type,
244 DirectHandle<WasmDispatchTable>* out_dispatch_table = nullptr);
245
246 inline bool is_in_bounds(uint32_t entry_index);
247
248 inline bool is_table64() const;
249
250 // Get the declared maximum as uint64_t or nullopt if no maximum was declared.
251 inline std::optional<uint64_t> maximum_length_u64() const;
252
253 // Thin wrapper around {JsToWasmObject}.
256 DirectHandle<Object> entry, const char** error_message);
257
258 // This function will not handle JS objects; i.e., {entry} needs to be in wasm
259 // representation.
260 V8_EXPORT_PRIVATE static void Set(Isolate* isolate,
262 uint32_t index, DirectHandle<Object> entry);
263
265 Isolate* isolate, DirectHandle<WasmTableObject> table, uint32_t index);
266
267 V8_EXPORT_PRIVATE static void Fill(Isolate* isolate,
269 uint32_t start, DirectHandle<Object> entry,
270 uint32_t count);
271
272 // TODO(wasm): Unify these three methods into one.
273 static void UpdateDispatchTable(
274 Isolate* isolate, DirectHandle<WasmTableObject> table, int entry_index,
275 const wasm::WasmFunction* func,
277#if V8_ENABLE_DRUMBRAKE
278 ,
279 int target_func_index
280#endif // V8_ENABLE_DRUMBRAKE
281 );
282 static void UpdateDispatchTable(Isolate* isolate,
284 int entry_index,
286 static void UpdateDispatchTable(Isolate* isolate,
288 int entry_index,
289 DirectHandle<WasmCapiFunction> capi_function);
290
291 void ClearDispatchTable(int index);
292
294 Isolate* isolate, DirectHandle<WasmTableObject> table, int entry_index,
295 DirectHandle<WasmTrustedInstanceData> trusted_instance_data,
296 int func_index);
297
298 // This function reads the content of a function table entry and returns it
299 // through the output parameters.
300 static void GetFunctionTableEntry(
301 Isolate* isolate, DirectHandle<WasmTableObject> table, int entry_index,
302 bool* is_valid, bool* is_null,
304 int* function_index,
305 MaybeDirectHandle<WasmJSFunction>* maybe_js_function);
306
307 private:
308 // {entry} is either {Null} or a {WasmInternalFunction}.
309 static void SetFunctionTableEntry(Isolate* isolate,
311 int entry_index,
313
315};
316
318 : public TorqueGeneratedWasmMemoryMapDescriptor<WasmMemoryMapDescriptor,
319 JSObject> {
320 public:
322 NewFromAnonymous(Isolate* isolate, size_t length);
323
326 Isolate* isolate,
328
329 // Returns the number of bytes that got mapped into the WebAssembly.Memory.
331 size_t offset);
332
333 // Returns `false` if an error occurred, otherwise `true`.
335
336 class BodyDescriptor;
337
339};
340
341// Representation of a WebAssembly.Memory JavaScript-level object.
343 : public TorqueGeneratedWasmMemoryObject<WasmMemoryObject, JSObject> {
344 public:
345 class BodyDescriptor;
346
348
349 // Add a use of this memory object to the given instance. This updates the
350 // internal weak list of instances that use this memory and also updates the
351 // fields of the instance to reference this memory's buffer.
352 // Note that we update both the non-shared and shared (if any) parts of the
353 // instance for faster access to shared memory.
356 DirectHandle<WasmTrustedInstanceData> trusted_instance_data,
357 DirectHandle<WasmTrustedInstanceData> shared_trusted_instance_data,
358 int memory_index_in_instance);
359 inline bool has_maximum_pages();
360
361 inline bool is_memory64() const;
362
364 Isolate* isolate, DirectHandle<JSArrayBuffer> buffer, int maximum,
365 wasm::AddressType address_type);
366
368 Isolate* isolate, int initial, int maximum, SharedFlag shared,
369 wasm::AddressType address_type);
370
371 // Assign a new (grown) buffer to this memory, also updating the shortcut
372 // fields of all instances that use this memory.
373 void SetNewBuffer(Isolate* isolate, Tagged<JSArrayBuffer> new_buffer);
374
375 // Updates all WebAssembly instances that use this Memory as a memory, after
376 // growing or refreshing the memory.
377 void UpdateInstances(Isolate* isolate);
378
379 // Fix up a resizable ArrayBuffer that exposes Wasm memory.
380 //
381 // Usually, an ArrayBuffer is setup from metadata on its BackingStore.
382 // However, ABs that are actually WebAssembly memories may have metadata that
383 // diverge from their BackingStore's.
384 //
385 // SABs' is_resizable_by_js may be true even when the BackingStore's
386 // equivalent field is false. This happens when there are both growable and
387 // non-growable SABs pointing to the same shared WebAssembly memory.
388 //
389 // AB and SABs' max_byte_length is the requested max passed to the
390 // WebAssembly.Memory constructor, while the BackingStore's max is an
391 // engine-determined heuristic that may be smaller.
392 //
393 // Both divergences are impossible for JS-created buffers.
395
396 // Detaches the existing buffer, makes a new buffer backed by
397 // new_backing_store, and update all the links.
400 std::shared_ptr<BackingStore> new_backing_store);
401
402 // Makes a new SharedArrayBuffer backed by the same backing store.
405 ResizableFlag resizable_by_js);
406
407 V8_EXPORT_PRIVATE static int32_t Grow(Isolate*,
409 uint32_t pages);
410
411 // Makes the ArrayBuffer fixed-length. Assumes the current ArrayBuffer is
412 // resizable. Detaches the existing buffer if it is not shared.
414 Isolate* isolate, DirectHandle<WasmMemoryObject> memory);
415
416 // Makes the ArrayBuffer resizable by JS. Assumes the current ArrayBuffer is
417 // fixed-length. Detaches the existing buffer if it is not shared.
419 Isolate* isolate, DirectHandle<WasmMemoryObject> memory);
420
421 static constexpr int kNoMaximum = -1;
422
424};
425
426// Representation of a WebAssembly.Global JavaScript-level object.
428 : public TorqueGeneratedWasmGlobalObject<WasmGlobalObject, JSObject> {
429 public:
430 class BodyDescriptor;
431
432 DECL_ACCESSORS(untagged_buffer, Tagged<JSArrayBuffer>)
436
437 // Dispatched behavior.
439
442 MaybeDirectHandle<JSArrayBuffer> maybe_untagged_buffer,
443 MaybeDirectHandle<FixedArray> maybe_tagged_buffer, wasm::ValueType type,
444 int32_t offset, bool is_mutable);
445
446 inline int type_size() const;
447
448 inline int32_t GetI32();
449 inline int64_t GetI64();
450 inline float GetF32();
451 inline double GetF64();
452 inline uint8_t* GetS128RawBytes();
454
455 inline void SetI32(int32_t value);
456 inline void SetI64(int64_t value);
457 inline void SetF32(float value);
458 inline void SetF64(double value);
459 // {value} must be an object in Wasm representation.
460 inline void SetRef(DirectHandle<Object> value);
461
462 private:
463 // This function returns the address of the global's data in the
464 // JSArrayBuffer. This buffer may be allocated on-heap, in which case it may
465 // not have a fixed address.
466 inline Address address() const;
467
469};
470
471// The trusted part of a WebAssembly instance.
472// This object lives in trusted space and is never modified from user space.
474 public:
478#if V8_ENABLE_DRUMBRAKE
479 DECL_OPTIONAL_ACCESSORS(interpreter_object, Tagged<Tuple2>)
480#endif // V8_ENABLE_DRUMBRAKE
481 DECL_OPTIONAL_ACCESSORS(untagged_globals_buffer, Tagged<JSArrayBuffer>)
483 DECL_OPTIONAL_ACCESSORS(imported_mutable_globals_buffers, Tagged<FixedArray>)
484 // tables: FixedArray of WasmTableObject.
486 DECL_PROTECTED_POINTER_ACCESSORS(dispatch_table_for_imports,
488 DECL_ACCESSORS(imported_mutable_globals, Tagged<FixedAddressArray>)
489#if V8_ENABLE_DRUMBRAKE
490 // Points to an array that contains the function index for each imported Wasm
491 // function. This is required to call imported functions from the Wasm
492 // interpreter.
493 DECL_ACCESSORS(imported_function_indices, Tagged<FixedInt32Array>)
494#endif // V8_ENABLE_DRUMBRAKE
500 DECL_ACCESSORS(managed_object_maps, Tagged<FixedArray>)
501 DECL_ACCESSORS(feedback_vectors, Tagged<FixedArray>)
502 DECL_ACCESSORS(well_known_imports, Tagged<FixedArray>)
503 DECL_PRIMITIVE_ACCESSORS(memory0_start, uint8_t*)
504 DECL_PRIMITIVE_ACCESSORS(memory0_size, size_t)
505 DECL_PROTECTED_POINTER_ACCESSORS(managed_native_module,
507 DECL_PRIMITIVE_ACCESSORS(new_allocation_limit_address, Address*)
508 DECL_PRIMITIVE_ACCESSORS(new_allocation_top_address, Address*)
509 DECL_PRIMITIVE_ACCESSORS(old_allocation_limit_address, Address*)
510 DECL_PRIMITIVE_ACCESSORS(old_allocation_top_address, Address*)
511 DECL_PRIMITIVE_ACCESSORS(globals_start, uint8_t*)
512 DECL_PRIMITIVE_ACCESSORS(jump_table_start, Address)
513 DECL_PRIMITIVE_ACCESSORS(hook_on_function_call_address, Address)
514 DECL_PRIMITIVE_ACCESSORS(tiering_budget_array, std::atomic<uint32_t>*)
515 DECL_PROTECTED_POINTER_ACCESSORS(memory_bases_and_sizes,
517 DECL_ACCESSORS(data_segment_starts, Tagged<FixedAddressArray>)
518 DECL_ACCESSORS(data_segment_sizes, Tagged<FixedUInt32Array>)
519 DECL_ACCESSORS(element_segments, Tagged<FixedArray>)
520 DECL_PRIMITIVE_ACCESSORS(break_on_entry, uint8_t)
521 DECL_PRIMITIVE_ACCESSORS(stress_deopt_counter_address, Address)
522
523 // Clear uninitialized padding space. This ensures that the snapshot content
524 // is deterministic. Depending on the V8 build mode there could be no padding.
525 inline void clear_padding();
526
527 inline Tagged<WasmMemoryObject> memory_object(int memory_index) const;
528 inline uint8_t* memory_base(int memory_index) const;
529 inline size_t memory_size(int memory_index) const;
530
531 inline wasm::NativeModule* native_module() const;
532
533 inline Tagged<WasmModuleObject> module_object() const;
534 inline const wasm::WasmModule* module() const;
535
536 // Dispatched behavior.
539
540// Layout description.
541#define FIELD_LIST(V) \
542 /* Often-accessed fields go first to minimize generated code size. */ \
543 /* Less than system pointer sized fields come first. */ \
544 V(kProtectedDispatchTable0Offset, kTaggedSize) \
545 V(kProtectedDispatchTableForImportsOffset, kTaggedSize) \
546 V(kImportedMutableGlobalsOffset, kTaggedSize) \
547 IF_WASM_DRUMBRAKE(V, kImportedFunctionIndicesOffset, kTaggedSize) \
548 /* Optional padding to align system pointer size fields */ \
549 V(kOptionalPaddingOffset, POINTER_SIZE_PADDING(kOptionalPaddingOffset)) \
550 V(kMemory0StartOffset, kSystemPointerSize) \
551 V(kMemory0SizeOffset, kSizetSize) \
552 V(kGlobalsStartOffset, kSystemPointerSize) \
553 V(kJumpTableStartOffset, kSystemPointerSize) \
554 /* End of often-accessed fields. */ \
555 /* Continue with system pointer size fields to maintain alignment. */ \
556 V(kNewAllocationLimitAddressOffset, kSystemPointerSize) \
557 V(kNewAllocationTopAddressOffset, kSystemPointerSize) \
558 V(kOldAllocationLimitAddressOffset, kSystemPointerSize) \
559 V(kOldAllocationTopAddressOffset, kSystemPointerSize) \
560 V(kHookOnFunctionCallAddressOffset, kSystemPointerSize) \
561 V(kTieringBudgetArrayOffset, kSystemPointerSize) \
562 V(kStressDeoptCounterOffset, kSystemPointerSize) \
563 /* Less than system pointer size aligned fields are below. */ \
564 V(kProtectedMemoryBasesAndSizesOffset, kTaggedSize) \
565 V(kDataSegmentStartsOffset, kTaggedSize) \
566 V(kDataSegmentSizesOffset, kTaggedSize) \
567 V(kElementSegmentsOffset, kTaggedSize) \
568 V(kInstanceObjectOffset, kTaggedSize) \
569 V(kNativeContextOffset, kTaggedSize) \
570 V(kProtectedSharedPartOffset, kTaggedSize) \
571 V(kMemoryObjectsOffset, kTaggedSize) \
572 V(kUntaggedGlobalsBufferOffset, kTaggedSize) \
573 V(kTaggedGlobalsBufferOffset, kTaggedSize) \
574 V(kImportedMutableGlobalsBuffersOffset, kTaggedSize) \
575 IF_WASM_DRUMBRAKE(V, kInterpreterObjectOffset, kTaggedSize) \
576 V(kTablesOffset, kTaggedSize) \
577 V(kProtectedDispatchTablesOffset, kTaggedSize) \
578 V(kTagsTableOffset, kTaggedSize) \
579 V(kFuncRefsOffset, kTaggedSize) \
580 V(kManagedObjectMapsOffset, kTaggedSize) \
581 V(kFeedbackVectorsOffset, kTaggedSize) \
582 V(kWellKnownImportsOffset, kTaggedSize) \
583 V(kProtectedManagedNativeModuleOffset, kTaggedSize) \
584 V(kBreakOnEntryOffset, kUInt8Size) \
585 /* More padding to make the header pointer-size aligned */ \
586 V(kHeaderPaddingOffset, POINTER_SIZE_PADDING(kHeaderPaddingOffset)) \
587 V(kHeaderSize, 0) \
588 V(kSize, 0)
589
590 DEFINE_FIELD_OFFSET_CONSTANTS(ExposedTrustedObject::kHeaderSize, FIELD_LIST)
591 static_assert(IsAligned(kHeaderSize, kTaggedSize));
592 // TODO(ishell, v8:8875): When pointer compression is enabled 8-byte size
593 // fields (external pointers, doubles and BigInt data) are only kTaggedSize
594 // aligned so checking for alignments of fields bigger than kTaggedSize
595 // doesn't make sense until v8:8875 is fixed.
596#define ASSERT_FIELD_ALIGNED(offset, size) \
597 static_assert(size == 0 || IsAligned(offset, size) || \
598 (COMPRESS_POINTERS_BOOL && (size == kSystemPointerSize) && \
599 IsAligned(offset, kTaggedSize)));
601#undef ASSERT_FIELD_ALIGNED
602#undef FIELD_LIST
603
604 // GC support: List all tagged fields and protected fields.
605 // V(offset, name)
606#define WASM_TAGGED_INSTANCE_DATA_FIELDS(V) \
607 V(kInstanceObjectOffset, "instance_object") \
608 V(kNativeContextOffset, "native_context") \
609 V(kMemoryObjectsOffset, "memory_objects") \
610 V(kUntaggedGlobalsBufferOffset, "untagged_globals_buffer") \
611 V(kTaggedGlobalsBufferOffset, "tagged_globals_buffer") \
612 V(kImportedMutableGlobalsBuffersOffset, "imported_mutable_globals_buffers") \
613 IF_WASM_DRUMBRAKE(V, kInterpreterObjectOffset, "interpreter_object") \
614 V(kTablesOffset, "tables") \
615 V(kTagsTableOffset, "tags_table") \
616 V(kFuncRefsOffset, "func_refs") \
617 V(kManagedObjectMapsOffset, "managed_object_maps") \
618 V(kFeedbackVectorsOffset, "feedback_vectors") \
619 V(kWellKnownImportsOffset, "well_known_imports") \
620 V(kImportedMutableGlobalsOffset, "imported_mutable_globals") \
621 IF_WASM_DRUMBRAKE(V, kImportedFunctionIndicesOffset, \
622 "imported_function_indices") \
623 V(kDataSegmentStartsOffset, "data_segment_starts") \
624 V(kDataSegmentSizesOffset, "data_segment_sizes") \
625 V(kElementSegmentsOffset, "element_segments")
626#define WASM_PROTECTED_INSTANCE_DATA_FIELDS(V) \
627 V(kProtectedSharedPartOffset, "shared_part") \
628 V(kProtectedMemoryBasesAndSizesOffset, "memory_bases_and_sizes") \
629 V(kProtectedDispatchTable0Offset, "dispatch_table0") \
630 V(kProtectedDispatchTablesOffset, "dispatch_tables") \
631 V(kProtectedDispatchTableForImportsOffset, "dispatch_table_for_imports") \
632 V(kProtectedManagedNativeModuleOffset, "managed_native_module")
633
634#define WASM_INSTANCE_FIELD_OFFSET(offset, _) offset,
635#define WASM_INSTANCE_FIELD_NAME(_, name) name,
636
637#if V8_ENABLE_DRUMBRAKE
638 static constexpr size_t kWasmInterpreterAdditionalFields = 2;
639#else
640 static constexpr size_t kWasmInterpreterAdditionalFields = 0;
641#endif // V8_ENABLE_DRUMBRAKE
642 static constexpr size_t kTaggedFieldsCount =
643 16 + kWasmInterpreterAdditionalFields;
644
645 static constexpr std::array<uint16_t, kTaggedFieldsCount>
648 static constexpr std::array<const char*, kTaggedFieldsCount>
651 static constexpr std::array<uint16_t, 6> kProtectedFieldOffsets = {
653 static constexpr std::array<const char*, 6> kProtectedFieldNames = {
655
656#undef WASM_INSTANCE_FIELD_OFFSET
657#undef WASM_INSTANCE_FIELD_NAME
658#undef WASM_TAGGED_INSTANCE_DATA_FIELDS
659#undef WASM_PROTECTED_INSTANCE_DATA_FIELDS
660
661 static_assert(kTaggedFieldOffsets.size() == kTaggedFieldNames.size(),
662 "every tagged field offset needs a name");
663 static_assert(kProtectedFieldOffsets.size() == kProtectedFieldNames.size(),
664 "every protected field offset needs a name");
665
666 void SetRawMemory(int memory_index, uint8_t* mem_start, size_t mem_size);
667
668#if V8_ENABLE_DRUMBRAKE
669 // Get the interpreter object associated with the given wasm object.
670 // If no interpreter object exists yet, it is created automatically.
671 static DirectHandle<Tuple2> GetOrCreateInterpreterObject(
673 static DirectHandle<Tuple2> GetInterpreterObject(
675#endif // V8_ENABLE_DRUMBRAKE
676
679
680 WasmCodePointer GetCallTarget(uint32_t func_index);
681
682 inline Tagged<WasmDispatchTable> dispatch_table(uint32_t table_index);
683 inline bool has_dispatch_table(uint32_t table_index);
684
685 // Copies table entries. Returns {false} if the ranges are out-of-bounds.
686 static bool CopyTableEntries(
687 Isolate* isolate,
688 DirectHandle<WasmTrustedInstanceData> trusted_instance_data,
689 uint32_t table_dst_index, uint32_t table_src_index, uint32_t dst,
690 uint32_t src, uint32_t count) V8_WARN_UNUSED_RESULT;
691
692 // Loads a range of elements from element segment into a table.
693 // Returns the empty {Optional} if the operation succeeds, or an {Optional}
694 // with the error {MessageTemplate} if it fails.
695 static std::optional<MessageTemplate> InitTableEntries(
696 Isolate* isolate,
697 DirectHandle<WasmTrustedInstanceData> trusted_instance_data,
698 DirectHandle<WasmTrustedInstanceData> shared_trusted_instance_data,
699 uint32_t table_index, uint32_t segment_index, uint32_t dst, uint32_t src,
701
702 class BodyDescriptor;
703
704 // Read a WasmFuncRef from the func_refs FixedArray. Returns true on success
705 // and writes the result in the output parameter. Returns false if no func_ref
706 // exists yet for this function. Use GetOrCreateFuncRef to always create one.
707 bool try_get_func_ref(int index, Tagged<WasmFuncRef>* result);
708
709 // Acquires the {WasmFuncRef} for a given {function_index} from the cache of
710 // the given {trusted_instance_data}, or creates a new {WasmInternalFunction}
711 // and {WasmFuncRef} if it does not exist yet. The new objects are added to
712 // the cache of the {trusted_instance_data} immediately.
713 static DirectHandle<WasmFuncRef> GetOrCreateFuncRef(
714 Isolate* isolate,
715 DirectHandle<WasmTrustedInstanceData> trusted_instance_data,
716 int function_index);
717
718 // Get a raw pointer to the location where the given global is stored.
719 // {global} must not be a reference type.
720 uint8_t* GetGlobalStorage(const wasm::WasmGlobal&);
721
722 // Get the FixedArray and the index in that FixedArray for the given global,
723 // which must be a reference type.
724 std::pair<Tagged<FixedArray>, uint32_t> GetGlobalBufferAndIndex(
725 const wasm::WasmGlobal&);
726
727 // Get the value of a global.
728 wasm::WasmValue GetGlobalValue(Isolate*, const wasm::WasmGlobal&);
729
731
732 private:
733 void InitDataSegmentArrays(const wasm::NativeModule*);
734};
735
736// Representation of a WebAssembly.Instance JavaScript-level object.
737// This is mostly a wrapper around the WasmTrustedInstanceData, plus any
738// user-set properties.
740 : public TorqueGeneratedWasmInstanceObject<WasmInstanceObject, JSObject> {
741 public:
743
744 inline const wasm::WasmModule* module() const;
745
746 class BodyDescriptor;
747
750};
751
752// Representation of WebAssembly.Exception JavaScript-level object.
754 : public TorqueGeneratedWasmTagObject<WasmTagObject, JSObject> {
755 public:
756 class BodyDescriptor;
757
758 // Checks whether the given {sig} has the same parameter types as the
759 // serialized signature stored within this tag object.
760 bool MatchesSignature(wasm::CanonicalTypeIndex expected_index);
761
763 Isolate* isolate, const wasm::FunctionSig* sig,
766
768
770};
771
772// Off-heap data object owned by a WasmDispatchTable. Currently used for
773// tracking referenced WasmToJS wrappers (shared per process), so we can
774// decrement their refcounts when the WasmDispatchTable is freed.
776 public:
779
780 // This class tracks wrapper entries since it owns the corresponding
781 // CodePointerTable entries. This function can be used to check if a given
782 // entry points to a wrapper.
783 V8_EXPORT_PRIVATE bool IsAWrapper(int index) const;
784
785#ifdef DEBUG
786 WasmCodePointer WrapperCodePointerForDebugging(int index);
787#endif
788
789 private:
790 friend class WasmDispatchTable;
791
792 // This class owns the CodePointerTable entries for generic and compiled
793 // wrappers. This function adds an entry for a wrapper. If {compiled_wrapper}
794 // is nullptr, the entry is for the generic wrapper.
795 // The CodePointerTableEntry is reused for the generic and compiled wrapper.
796 WasmCodePointer Add(int index, Address call_target,
797 wasm::WasmCode* compiled_wrapper,
798 uint64_t signature_hash);
799 void Remove(int index, WasmCodePointer call_target);
800
801 // The {wrappers_} data structure tracks installed wrappers, both generic
802 // ({code} is nullptr) and compiled. It owns the CodePointerTable entry in
803 // {call_target} and manages the {code} lifetime by incrementing and
804 // decrementing the ref count as needed.
807 wasm::WasmCode* code; // {nullptr} if this is the generic wrapper.
808 };
809 std::unordered_map<int, WrapperEntry> wrappers_;
810};
811
812// The dispatch table is referenced from a WasmTableObject and from every
813// WasmTrustedInstanceData which uses the table. It is used from generated code
814// for executing indirect calls.
816 public:
817#if V8_ENABLE_DRUMBRAKE
818 static const uint32_t kInvalidFunctionIndex = UINT_MAX;
819#endif // V8_ENABLE_DRUMBRAKE
820
821 enum NewOrExistingEntry : bool { kNewEntry, kExistingEntry };
822
823 class BodyDescriptor;
824
825 static constexpr size_t kLengthOffset = kHeaderSize;
826 static constexpr size_t kCapacityOffset = kLengthOffset + kUInt32Size;
827 static constexpr size_t kProtectedOffheapDataOffset =
828 kCapacityOffset + kUInt32Size;
829 static constexpr size_t kProtectedUsesOffset =
830 kProtectedOffheapDataOffset + kTaggedSize;
831 static constexpr size_t kTableTypeOffset = kProtectedUsesOffset + kTaggedSize;
832 static constexpr size_t kPaddingSize = TAGGED_SIZE_8_BYTES ? kUInt32Size : 0;
833 static constexpr size_t kEntriesOffset =
834 kTableTypeOffset + kUInt32Size + kPaddingSize;
835
836 // Entries consist of
837 // - target (pointer)
838#if V8_ENABLE_DRUMBRAKE
839 // - function_index (uint32_t) (located in place of target pointer).
840#endif // V8_ENABLE_DRUMBRAKE
841 // - implicit_arg (protected pointer, tagged sized)
842 // - sig (int32_t); unused for imports which check the signature statically.
843 static constexpr size_t kTargetBias = 0;
844#if V8_ENABLE_DRUMBRAKE
845 // In jitless mode, reuse the 'target' field storage to hold the (uint32_t)
846 // function index.
847 static constexpr size_t kFunctionIndexBias = kTargetBias;
848#endif // V8_ENABLE_DRUMBRAKE
849 static constexpr size_t kImplicitArgBias = kTargetBias + kSystemPointerSize;
850 static constexpr size_t kSigBias = kImplicitArgBias + kTaggedSize;
851 static constexpr size_t kEntryPaddingOffset = kSigBias + kInt32Size;
852 static constexpr size_t kEntryPaddingBytes =
853 kEntryPaddingOffset % kTaggedSize;
854 static_assert(kEntryPaddingBytes == 4 || kEntryPaddingBytes == 0);
855 static constexpr size_t kEntrySize = kEntryPaddingOffset + kEntryPaddingBytes;
856
857 // Tagged and system-pointer-sized fields must be tagged-size-aligned.
858 static_assert(IsAligned(kEntriesOffset, kTaggedSize));
859 static_assert(IsAligned(kEntrySize, kTaggedSize));
860 static_assert(IsAligned(kTargetBias, kTaggedSize));
861 static_assert(IsAligned(kImplicitArgBias, kTaggedSize));
862
863 // TODO(clemensb): If we ever enable allocation alignment we will needs to add
864 // more padding to make the "target" fields system-pointer-size aligned.
865 static_assert(!USE_ALLOCATION_ALIGNMENT_BOOL);
866
867 // The total byte size must still fit in an integer.
868 static constexpr int kMaxLength = (kMaxInt - kEntriesOffset) / kEntrySize;
869
870 static constexpr int SizeFor(int length) {
871 DCHECK_LE(length, kMaxLength);
872 return kEntriesOffset + length * kEntrySize;
873 }
874
875 static constexpr int OffsetOf(int index) {
876 DCHECK_LT(index, kMaxLength);
877 return SizeFor(index);
878 }
879
880 // Clear uninitialized padding space for deterministic object content.
881 // Depending on the V8 build mode there could be no padding.
882 inline void clear_entry_padding(int index);
883
884 // The current length of this dispatch table. This is always <= the capacity.
885 inline int length() const;
886 inline int length(AcquireLoadTag) const;
887 // The current capacity. Can be bigger than the current length to allow for
888 // more efficient growing.
889 inline int capacity() const;
890
891 DECL_PROTECTED_POINTER_ACCESSORS(protected_offheap_data,
893 inline WasmDispatchTableData* offheap_data() const;
894
895 // Stores all WasmTrustedInstanceData that refer to this WasmDispatchTable.
897
898 // Stores the canonical type of the table.
900
901 // Accessors.
902 // {implicit_arg} will be a WasmImportData, a WasmTrustedInstanceData, or
903 // Smi::zero() (if the entry was cleared).
904 inline Tagged<Object> implicit_arg(int index) const;
905 inline WasmCodePointer target(int index) const;
906 inline wasm::CanonicalTypeIndex sig(int index) const;
907
908 // Set an entry for indirect calls that don't go to a WasmToJS wrapper.
909 // Wrappers are special since we own the CPT entries for the wrappers.
910 // {implicit_arg} has to be a WasmImportData, a WasmTrustedInstanceData, or
911 // Smi::zero().
912 void V8_EXPORT_PRIVATE SetForNonWrapper(int index,
913 Tagged<Object> implicit_arg,
914 WasmCodePointer call_target,
916#if V8_ENABLE_DRUMBRAKE
917 uint32_t function_index,
918#endif // V8_ENABLE_DRUMBRAKE
919 NewOrExistingEntry new_or_existing);
920
921 // Set an entry for indirect calls to a WasmToJS wrapper.
922 // {implicit_arg} has to be a WasmImportData, a WasmTrustedInstanceData.
923 // {compiled_wrapper} needs to be set to the corresponding WasmCode, or
924 // nullptr in case of the generic wrapper.
925 void V8_EXPORT_PRIVATE SetForWrapper(int index, Tagged<Object> implicit_arg,
926 Address call_target,
928 uint64_t signature_hash,
929#if V8_ENABLE_DRUMBRAKE
930 uint32_t function_index,
931#endif // V8_ENABLE_DRUMBRAKE
932 wasm::WasmCode* compiled_wrapper,
933 NewOrExistingEntry new_or_existing);
934
935#if V8_ENABLE_DRUMBRAKE
936 inline uint32_t function_index(int index) const;
937#endif // V8_ENABLE_DRUMBRAKE
938
939 void Clear(int index, NewOrExistingEntry new_or_existing);
940 void InstallCompiledWrapper(int index, wasm::WasmCode* wrapper);
941
942 bool V8_EXPORT_PRIVATE IsAWrapper(int index) const;
943
944 static void V8_EXPORT_PRIVATE
945 AddUse(Isolate* isolate, DirectHandle<WasmDispatchTable> dispatch_table,
946 DirectHandle<WasmTrustedInstanceData> instance, int table_index);
947 // Internal helpers for management of the uses list. These could be factored
948 // out into a class similar to WeakArrayList if there are additional use
949 // cases for them.
950 // The first slot in the list is the used length. After that, we store
951 // pairs of <instance, table_index>.
952 static Tagged<ProtectedWeakFixedArray> MaybeGrowUsesList(
953 Isolate* isolate, DirectHandle<WasmDispatchTable> dispatch_table);
954
956 New(Isolate* isolate, int length, wasm::CanonicalValueType table_type);
959
963};
964
965// A Wasm exception that has been thrown out of Wasm code.
967 public:
969 Isolate* isolate, DirectHandle<WasmExceptionTag> exception_tag,
970 int encoded_size);
971
973 Isolate* isolate, DirectHandle<WasmExceptionTag> exception_tag,
975
976 // The below getters return {undefined} in case the given exception package
977 // does not carry the requested values (i.e. is of a different type).
978 static DirectHandle<Object> GetExceptionTag(
979 Isolate* isolate, DirectHandle<WasmExceptionPackage> exception_package);
980 static DirectHandle<Object> GetExceptionValues(
981 Isolate* isolate, DirectHandle<WasmExceptionPackage> exception_package);
982
983 // Determines the size of the array holding all encoded exception values.
984 static uint32_t GetEncodedSize(const wasm::WasmTagSig* tag);
985 static uint32_t GetEncodedSize(const wasm::WasmTag* tag);
986
987 // In-object fields.
988 enum { kTagIndex, kValuesIndex, kInObjectFieldCount };
989 static constexpr int kSize =
990 kHeaderSize + (kTaggedSize * kInObjectFieldCount);
991
995};
996
998EncodeI32ExceptionValue(DirectHandle<FixedArray> encoded_values,
999 uint32_t* encoded_index, uint32_t value);
1000
1002EncodeI64ExceptionValue(DirectHandle<FixedArray> encoded_values,
1003 uint32_t* encoded_index, uint64_t value);
1004
1006DecodeI32ExceptionValue(DirectHandle<FixedArray> encoded_values,
1007 uint32_t* encoded_index, uint32_t* value);
1008
1010DecodeI64ExceptionValue(DirectHandle<FixedArray> encoded_values,
1011 uint32_t* encoded_index, uint64_t* value);
1012
1013bool UseGenericWasmToJSWrapper(wasm::ImportCallKind kind,
1014 const wasm::CanonicalSig* sig,
1015 wasm::Suspend suspend);
1016
1017// A Wasm function that is wrapped and exported to JavaScript.
1018// Representation of WebAssembly.Function JavaScript-level object.
1020 public:
1021 V8_EXPORT_PRIVATE static bool IsWasmExportedFunction(Tagged<Object> object);
1022
1026 DirectHandle<WasmInternalFunction> internal_function, int arity,
1027 DirectHandle<Code> export_wrapper);
1028
1029 // Return a null-terminated string with the debug name in the form
1030 // 'js-to-wasm:<sig>'.
1031 static std::unique_ptr<char[]> GetDebugName(const wasm::CanonicalSig* sig);
1032
1034};
1035
1036// A Wasm function that was created by wrapping a JavaScript callable.
1037// Representation of WebAssembly.Function JavaScript-level object.
1039 public:
1040 static bool IsWasmJSFunction(Tagged<Object> object);
1041
1042 static DirectHandle<WasmJSFunction> New(Isolate* isolate,
1043 const wasm::FunctionSig* sig,
1044 DirectHandle<JSReceiver> callable,
1045 wasm::Suspend suspend);
1046
1048};
1049
1050// An external function exposed to Wasm via the C/C++ API.
1052 public:
1053 static bool IsWasmCapiFunction(Tagged<Object> object);
1054
1055 static DirectHandle<WasmCapiFunction> New(Isolate* isolate,
1056 Address call_target,
1057 DirectHandle<Foreign> embedder_data,
1058 wasm::CanonicalTypeIndex sig_index,
1059 const wasm::CanonicalSig* sig);
1060
1061 const wasm::CanonicalSig* sig() const;
1062
1063 // Checks whether the given {sig} has the same parameter types as the
1064 // serialized signature stored within this C-API function object.
1065 bool MatchesSignature(
1066 wasm::CanonicalTypeIndex other_canonical_sig_index) const;
1067
1069};
1070
1071// Any external function that can be imported/exported in modules. This abstract
1072// class just dispatches to the following concrete classes:
1073// - {WasmExportedFunction}: A proper Wasm function exported from a module.
1074// - {WasmJSFunction}: A function constructed via WebAssembly.Function in JS.
1075// - {WasmCapiFunction}: A function constructed via the C/C++ API.
1077 public:
1078 static bool IsWasmExternalFunction(Tagged<Object> object);
1079
1080 inline Tagged<WasmFuncRef> func_ref() const;
1081
1083};
1084
1086 : public TorqueGeneratedWasmFunctionData<WasmFunctionData,
1087 ExposedTrustedObject> {
1088 public:
1089 DECL_CODE_POINTER_ACCESSORS(wrapper_code)
1091
1093
1096 WasmFunctionData, kWasmFunctionDataIndirectPointerTag>,
1097 WithStrongCodePointer<kWrapperCodeOffset>,
1098 WithProtectedPointer<kProtectedInternalOffset>>;
1099
1100 using SuspendField = base::BitField<wasm::Suspend, 0, 1>;
1102
1104};
1105
1106// Information for a WasmExportedFunction which is referenced as the function
1107// data of the SharedFunctionInfo underlying the function. For details please
1108// see the {SharedFunctionInfo::HasWasmExportedFunctionData} predicate.
1110 : public TorqueGeneratedWasmExportedFunctionData<WasmExportedFunctionData,
1112 public:
1114 DECL_CODE_POINTER_ACCESSORS(c_wrapper_code)
1115
1117 // Prefer to use this convenience wrapper of the Torque-generated
1118 // {canonical_type_index()}.
1119 inline wasm::CanonicalTypeIndex sig_index() const;
1120
1121 inline bool is_promising() const;
1122
1123 bool MatchesSignature(wasm::CanonicalTypeIndex other_canonical_sig_index);
1124
1125 // Dispatched behavior.
1128
1132 WithProtectedPointer<kProtectedInstanceDataOffset>,
1133 WithStrongCodePointer<kCWrapperCodeOffset>>;
1134
1136};
1137
1139 : public TorqueGeneratedWasmImportData<WasmImportData, TrustedObject> {
1140 public:
1141 // Dispatched behavior.
1143
1146
1148 DECL_PRIMITIVE_ACCESSORS(table_slot, uint32_t)
1149
1150 static constexpr int kInvalidCallOrigin = 0;
1151
1152 void SetIndexInTableAsCallOrigin(Tagged<WasmDispatchTable> table,
1153 int entry_index);
1154
1155 void SetFuncRefAsCallOrigin(Tagged<WasmInternalFunction> func);
1156
1159 WithProtectedPointer<kProtectedInstanceDataOffset>,
1160 WithProtectedPointer<kProtectedCallOriginOffset>>;
1161
1162 // Usage of the {bit_field()}.
1163 // "Suspend" is always present.
1164 using SuspendField = base::BitField<wasm::Suspend, 0, 1>;
1165 // "TableSlot" is populated when {protected_call_origin} is a
1166 // {WasmDispatchTable}, and describes the slot in that table.
1167 static constexpr int kTableSlotBits = 24;
1168 static_assert(wasm::kV8MaxWasmTableSize < (1u << kTableSlotBits));
1169 using TableSlotField = SuspendField::Next<uint32_t, kTableSlotBits>;
1170
1172};
1173
1175 : public TorqueGeneratedWasmInternalFunction<WasmInternalFunction,
1177 public:
1178 // Get the external function if it exists. Returns true and writes to the
1179 // output parameter if an external function exists. Returns false otherwise.
1180 bool try_get_external(Tagged<JSFunction>* result);
1181
1182 V8_EXPORT_PRIVATE static DirectHandle<JSFunction> GetOrCreateExternal(
1184
1186
1187 V8_INLINE WasmCodePointer call_target();
1188 V8_INLINE void set_call_target(WasmCodePointer code_pointer);
1189
1190 // Dispatched behavior.
1192
1195 WasmInternalFunction, kWasmInternalFunctionIndirectPointerTag>,
1196 WithProtectedPointer<kProtectedImplicitArgOffset>>;
1197
1199};
1200
1201class WasmFuncRef : public TorqueGeneratedWasmFuncRef<WasmFuncRef, HeapObject> {
1202 public:
1204
1206
1209 WithStrongTrustedPointer<kTrustedInternalOffset,
1210 kWasmInternalFunctionIndirectPointerTag>>;
1211
1213};
1214
1215// Information for a WasmJSFunction which is referenced as the function data of
1216// the SharedFunctionInfo underlying the function. For details please see the
1217// {SharedFunctionInfo::HasWasmJSFunctionData} predicate.
1219 : public TorqueGeneratedWasmJSFunctionData<WasmJSFunctionData,
1221 public:
1222 // The purpose of this class is to provide lifetime management for compiled
1223 // wrappers: the {WasmJSFunction} owns an {OffheapData} via {TrustedManaged},
1224 // which decrements the wrapper's refcount when the {WasmJSFunction} is
1225 // garbage-collected.
1227 public:
1228 explicit OffheapData(uint64_t signature_hash)
1229 : signature_hash_(signature_hash) {}
1230 ~OffheapData();
1231
1232 // These functions return the CPT entry owned by this class.
1233 WasmCodePointer set_compiled_wrapper(wasm::WasmCode* wrapper);
1234 WasmCodePointer set_generic_wrapper(Address call_target);
1235
1236 private:
1237 WasmCodePointer wrapper_code_pointer_ = wasm::kInvalidWasmCodePointer;
1238 wasm::WasmCode* wrapper_{nullptr};
1239 const uint64_t signature_hash_;
1240 };
1241
1242 DECL_PROTECTED_POINTER_ACCESSORS(protected_offheap_data,
1244 inline OffheapData* offheap_data() const;
1245
1246 Tagged<JSReceiver> GetCallable() const;
1247 wasm::Suspend GetSuspend() const;
1248 const wasm::CanonicalSig* GetSignature() const;
1249 // Prefer to use this convenience wrapper of the Torque-generated
1250 // {canonical_sig_index()}.
1251 inline wasm::CanonicalTypeIndex sig_index() const;
1252 bool MatchesSignature(
1253 wasm::CanonicalTypeIndex other_canonical_sig_index) const;
1254
1255 // Dispatched behavior.
1257
1261 WithProtectedPointer<kProtectedOffheapDataOffset>>;
1262
1263 private:
1265};
1266
1268 : public TorqueGeneratedWasmCapiFunctionData<WasmCapiFunctionData,
1270 public:
1271 // Prefer to use this convenience wrapper of the Torque-generated
1272 // {canonical_sig_index()}.
1273 inline wasm::CanonicalTypeIndex sig_index() const;
1274
1276
1280
1282};
1283
1292
1293class WasmScript : public AllStatic {
1294 public:
1295 // Position used for storing "on entry" breakpoints (a.k.a. instrumentation
1296 // breakpoints). This would be an illegal position for any other breakpoint.
1297 static constexpr int kOnEntryBreakpointPosition = -1;
1298
1299 // Set a breakpoint on the given byte position inside the given module.
1300 // This will affect all live and future instances of the module.
1301 // The passed position might be modified to point to the next breakable
1302 // location inside the same function.
1303 // If it points outside a function, or behind the last breakable location,
1304 // this function returns false and does not set any breakpoint.
1305 V8_EXPORT_PRIVATE static bool SetBreakPoint(
1307 DirectHandle<BreakPoint> break_point);
1308
1309 // Set an "on entry" breakpoint (a.k.a. instrumentation breakpoint) inside
1310 // the given module. This will affect all live and future instances of the
1311 // module.
1312 V8_EXPORT_PRIVATE static void SetInstrumentationBreakpoint(
1314
1315 // Set a breakpoint on first breakable position of the given function index
1316 // inside the given module. This will affect all live and future instances of
1317 // the module.
1318 V8_EXPORT_PRIVATE static bool SetBreakPointOnFirstBreakableForFunction(
1319 DirectHandle<Script>, int function_index,
1320 DirectHandle<BreakPoint> break_point);
1321
1322 // Set a breakpoint at the breakable offset of the given function index
1323 // inside the given module. This will affect all live and future instances of
1324 // the module.
1325 V8_EXPORT_PRIVATE static bool SetBreakPointForFunction(
1326 DirectHandle<Script>, int function_index, int breakable_offset,
1327 DirectHandle<BreakPoint> break_point);
1328
1329 // Remove a previously set breakpoint at the given byte position inside the
1330 // given module. If this breakpoint is not found this function returns false.
1331 V8_EXPORT_PRIVATE static bool ClearBreakPoint(
1333
1334 // Remove a previously set breakpoint by id. If this breakpoint is not found,
1335 // returns false.
1336 V8_EXPORT_PRIVATE static bool ClearBreakPointById(DirectHandle<Script>,
1337 int breakpoint_id);
1338
1339 // Remove all set breakpoints.
1340 static void ClearAllBreakpoints(Tagged<Script>);
1341
1342 // Get a list of all possible breakpoints within a given range of this module.
1343 V8_EXPORT_PRIVATE static bool GetPossibleBreakpoints(
1344 wasm::NativeModule* native_module, const debug::Location& start,
1345 const debug::Location& end, std::vector<debug::BreakLocation>* locations);
1346
1347 // Return an empty handle if no breakpoint is hit at that location, or a
1348 // FixedArray with all hit breakpoint objects.
1349 static MaybeDirectHandle<FixedArray> CheckBreakPoints(
1351 StackFrameId stack_frame_id);
1352
1353 private:
1354 // Helper functions that update the breakpoint info list.
1355 static void AddBreakpointToInfo(DirectHandle<Script>, int position,
1356 DirectHandle<BreakPoint> break_point);
1357};
1358
1359// Tags provide an object identity for each exception defined in a wasm module
1360// header. They are referenced by the following fields:
1361// - {WasmTagObject::tag}: The tag of the {Tag} object.
1362// - {WasmInstanceObject::tags_table}: List of tags used by an instance.
1364 : public TorqueGeneratedWasmExceptionTag<WasmExceptionTag, Struct> {
1365 public:
1367 int index);
1368
1370
1372};
1373
1374// Data annotated to the asm.js Module function. Used for later instantiation of
1375// that function.
1376class AsmWasmData : public TorqueGeneratedAsmWasmData<AsmWasmData, Struct> {
1377 public:
1378 static Handle<AsmWasmData> New(
1379 Isolate* isolate, std::shared_ptr<wasm::NativeModule> native_module,
1380 DirectHandle<HeapNumber> uses_bitset);
1381
1383
1385
1387};
1388
1390 : public TorqueGeneratedWasmTypeInfo<WasmTypeInfo, HeapObject> {
1391 public:
1392 inline wasm::CanonicalValueType type() const;
1393 inline wasm::CanonicalTypeIndex type_index() const;
1394 inline wasm::CanonicalValueType element_type() const; // Only for WasmArrays.
1395
1397
1398 class BodyDescriptor;
1399
1401};
1402
1403class WasmObject : public TorqueGeneratedWasmObject<WasmObject, JSReceiver> {
1404 protected:
1405 // Returns boxed value of the object's field/element with given type and
1406 // offset.
1407 static inline DirectHandle<Object> ReadValueAt(Isolate* isolate,
1410 uint32_t offset);
1411
1412 private:
1413 template <typename ElementType>
1414 static ElementType FromNumber(Tagged<Object> value);
1415
1417};
1418
1419class WasmStruct : public TorqueGeneratedWasmStruct<WasmStruct, WasmObject> {
1420 public:
1421 static const wasm::CanonicalStructType* GcSafeType(Tagged<Map> map);
1422 static inline int Size(const wasm::StructType* type);
1423 static inline int Size(const wasm::CanonicalStructType* type);
1424 static inline int GcSafeSize(Tagged<Map> map);
1425 static inline void EncodeInstanceSizeInMap(int instance_size,
1426 Tagged<Map> map);
1427 static inline int DecodeInstanceSizeFromMap(Tagged<Map> map);
1428
1429 // Returns the address of the field at given offset.
1430 inline Address RawFieldAddress(int raw_offset);
1431
1432 // Returns the ObjectSlot for tagged value at given offset.
1433 inline ObjectSlot RawField(int raw_offset);
1434
1435 // Only for structs whose type describes another type.
1436 static DirectHandle<WasmStruct> AllocateDescriptorUninitialized(
1439 inline Tagged<Map> get_described_rtt() const;
1440 inline void set_described_rtt(Tagged<Map> rtt);
1441
1442 V8_EXPORT_PRIVATE wasm::WasmValue GetFieldValue(uint32_t field_index);
1443
1445
1446 class BodyDescriptor;
1447
1449};
1450
1451int WasmStruct::Size(const wasm::StructType* type) {
1452 // Object size must fit into a Smi (because of filler objects), and its
1453 // computation must not overflow.
1454 static_assert(Smi::kMaxValue <= kMaxInt);
1455 DCHECK_LE(type->total_fields_size(), Smi::kMaxValue - kHeaderSize);
1456 return std::max(kHeaderSize + static_cast<int>(type->total_fields_size()),
1457 Heap::kMinObjectSizeInTaggedWords * kTaggedSize);
1458}
1459
1460int WasmStruct::Size(const wasm::CanonicalStructType* type) {
1461 // Object size must fit into a Smi (because of filler objects), and its
1462 // computation must not overflow.
1463 static_assert(Smi::kMaxValue <= kMaxInt);
1464 DCHECK_LE(type->total_fields_size(), Smi::kMaxValue - kHeaderSize);
1465 return std::max(kHeaderSize + static_cast<int>(type->total_fields_size()),
1466 Heap::kMinObjectSizeInTaggedWords * kTaggedSize);
1467}
1468
1469class WasmArray : public TorqueGeneratedWasmArray<WasmArray, WasmObject> {
1470 public:
1471 static inline wasm::CanonicalTypeIndex type_index(Tagged<Map> map);
1472 static inline const wasm::CanonicalValueType GcSafeElementType(
1473 Tagged<Map> map);
1474
1475 // Get the {ObjectSlot} corresponding to the element at {index}. Requires that
1476 // this is a reference array.
1477 inline ObjectSlot ElementSlot(uint32_t index);
1478 V8_EXPORT_PRIVATE wasm::WasmValue GetElement(uint32_t index);
1479
1480 static inline int SizeFor(Tagged<Map> map, int length);
1481
1482 // Returns boxed value of the array's element.
1483 static inline DirectHandle<Object> GetElement(Isolate* isolate,
1485 uint32_t index);
1486
1487 void SetTaggedElement(uint32_t index, DirectHandle<Object> value,
1489
1490 // Returns the offset/Address of the element at {index}.
1491 inline uint32_t element_offset(uint32_t index);
1492 inline Address ElementAddress(uint32_t index);
1493
1494 static constexpr int MaxLength(uint32_t element_size_bytes) {
1495 // The total object size must fit into a Smi, for filler objects. To make
1496 // the behavior of Wasm programs independent from the Smi configuration,
1497 // we hard-code the smaller of the two supported ranges.
1498 return (SmiTagging<4>::kSmiMaxValue - kHeaderSize) / element_size_bytes;
1499 }
1500
1501 static int MaxLength(const wasm::ArrayType* type) {
1502 return MaxLength(type->element_type().value_kind_size());
1503 }
1504
1505 static inline void EncodeElementSizeInMap(int element_size, Tagged<Map> map);
1506 static inline int DecodeElementSizeFromMap(Tagged<Map> map);
1507
1509
1510 class BodyDescriptor;
1511
1513};
1514
1515// The suspender object provides an API to suspend and resume wasm code using
1516// promises. See: https://github.com/WebAssembly/js-promise-integration.
1518 : public TorqueGeneratedWasmSuspenderObject<WasmSuspenderObject,
1519 HeapObject> {
1520 public:
1524 enum State : int { kInactive = 0, kActive, kSuspended };
1528};
1529
1531 : public TorqueGeneratedWasmSuspendingObject<WasmSuspendingObject,
1532 JSObject> {
1533 public:
1535 Isolate* isolate, DirectHandle<JSReceiver> callable);
1538};
1539
1540class WasmNull : public TorqueGeneratedWasmNull<WasmNull, HeapObject> {
1541 public:
1542#if V8_STATIC_ROOTS_BOOL || V8_STATIC_ROOTS_GENERATION_BOOL
1543 // TODO(manoskouk): Make it smaller if able and needed.
1544 static constexpr int kSize = 64 * KB + kTaggedSize;
1545 // Payload should be a multiple of page size.
1546 static_assert((kSize - kTaggedSize) % kMinimumOSPageSize == 0);
1547 // Any wasm struct offset should fit in the object.
1548 static_assert(kSize >=
1549 WasmStruct::kHeaderSize +
1550 (wasm::kMaxStructFieldIndexForImplicitNullCheck + 1) *
1551 kSimd128Size);
1552
1553 Address payload() { return ptr() + kHeaderSize - kHeapObjectTag; }
1554 static constexpr size_t kPayloadSize = kSize - kTaggedSize;
1555#else
1556 static constexpr int kSize = kTaggedSize;
1557#endif
1558
1559 // WasmNull cannot use `FixedBodyDescriptorFor()` as its map is variable size
1560 // (not fixed size) as kSize is too large for a fixed-size map.
1561 class BodyDescriptor;
1562
1564};
1565
1566#undef DECL_OPTIONAL_ACCESSORS
1567
1568// If {opt_native_context} is not null, creates a contextful map bound to
1569// that context; otherwise creates a context-independent map (which must then
1570// not point to any context-specific objects!).
1572 Isolate* isolate, wasm::CanonicalTypeIndex type,
1573 DirectHandle<Map> opt_rtt_parent,
1574 DirectHandle<NativeContext> opt_native_context);
1575
1577 wasm::CanonicalTypeIndex array_index,
1578 DirectHandle<Map> opt_rtt_parent);
1579
1582 DirectHandle<Map> opt_rtt_parent);
1583
1584namespace wasm {
1585// Takes a {value} in the JS representation and typechecks it according to
1586// {expected}. If the typecheck succeeds, returns the wasm representation of the
1587// object; otherwise, returns the empty handle.
1588MaybeDirectHandle<Object> JSToWasmObject(Isolate* isolate,
1590 CanonicalValueType expected,
1591 const char** error_message);
1592
1593// Utility which canonicalizes {expected} in addition.
1594MaybeDirectHandle<Object> JSToWasmObject(Isolate* isolate,
1595 const WasmModule* module,
1597 ValueType expected,
1598 const char** error_message);
1599
1600// Takes a {value} in the Wasm representation and transforms it to the
1601// respective JS representation. The caller is responsible for not providing an
1602// object which cannot be transformed to JS.
1603DirectHandle<Object> WasmToJSObject(Isolate* isolate,
1604 DirectHandle<Object> value);
1605
1606} // namespace wasm
1607} // namespace internal
1608} // namespace v8
1609
1611
1612#endif // V8_WASM_WASM_OBJECTS_H_
constexpr int kMinimumOSPageSize
Builtins::Kind kind
Definition builtins.cc:40
DirectHandle< TrustedObject > implicit_arg()
DirectHandle< TrustedObject > implicit_arg_
ImportedFunctionEntry(DirectHandle< WasmTrustedInstanceData >, int index)
V8_EXPORT_PRIVATE void SetCompiledWasmToJs(Isolate *, DirectHandle< JSReceiver > callable, wasm::WasmCode *wasm_to_js_wrapper, wasm::Suspend suspend, const wasm::CanonicalSig *sig, wasm::CanonicalTypeIndex sig_id)
void SetGenericWasmToJs(Isolate *, DirectHandle< JSReceiver > callable, wasm::Suspend suspend, const wasm::CanonicalSig *sig, wasm::CanonicalTypeIndex sig_id)
DirectHandle< WasmTrustedInstanceData > const instance_data_
void SetWasmToWasm(Tagged< WasmTrustedInstanceData > target_instance_object, WasmCodePointer call_target, wasm::CanonicalTypeIndex sig_id)
static int MaxLength(const wasm::ArrayType *type)
static constexpr int MaxLength(uint32_t element_size_bytes)
OBJECT_CONSTRUCTORS(WasmCapiFunction, JSFunction)
std::unordered_map< int, WrapperEntry > wrappers_
static constexpr int SizeFor(int length)
DECL_PROTECTED_POINTER_ACCESSORS(protected_offheap_data, TrustedManaged< WasmDispatchTableData >) inline WasmDispatchTableData *offheap_data() const
static constexpr int OffsetOf(int index)
OBJECT_CONSTRUCTORS(WasmExportedFunction, JSFunction)
OBJECT_CONSTRUCTORS(WasmExternalFunction, JSFunction)
DirectHandle< Object > GetRef()
void SetRef(DirectHandle< Object > value)
static V8_EXPORT_PRIVATE MaybeDirectHandle< WasmGlobalObject > New(Isolate *isolate, DirectHandle< WasmTrustedInstanceData > instance_object, MaybeDirectHandle< JSArrayBuffer > maybe_untagged_buffer, MaybeDirectHandle< FixedArray > maybe_tagged_buffer, wasm::ValueType type, int32_t offset, bool is_mutable)
const wasm::WasmModule * module() const
DECL_PROTECTED_POINTER_ACCESSORS(protected_offheap_data, TrustedManaged< OffheapData >) inline OffheapData *offheap_data() const
OBJECT_CONSTRUCTORS(WasmJSFunction, JSFunction)
V8_EXPORT_PRIVATE size_t MapDescriptor(DirectHandle< WasmMemoryObject > memory, size_t offset)
V8_EXPORT_PRIVATE bool UnmapDescriptor()
static V8_EXPORT_PRIVATE MaybeDirectHandle< WasmMemoryMapDescriptor > NewFromAnonymous(Isolate *isolate, size_t length)
static V8_EXPORT_PRIVATE DirectHandle< WasmMemoryMapDescriptor > NewFromFileDescriptor(Isolate *isolate, v8::WasmMemoryMapDescriptor::WasmFileDescriptor file_descriptor)
static DirectHandle< JSArrayBuffer > RefreshSharedBuffer(Isolate *isolate, DirectHandle< WasmMemoryObject > memory, ResizableFlag resizable_by_js)
static DirectHandle< JSArrayBuffer > ToFixedLengthBuffer(Isolate *isolate, DirectHandle< WasmMemoryObject > memory)
static DirectHandle< JSArrayBuffer > ToResizableBuffer(Isolate *isolate, DirectHandle< WasmMemoryObject > memory)
static DirectHandle< JSArrayBuffer > RefreshBuffer(Isolate *isolate, DirectHandle< WasmMemoryObject > memory, std::shared_ptr< BackingStore > new_backing_store)
static constexpr int kNoMaximum
static V8_EXPORT_PRIVATE void UseInInstance(Isolate *isolate, DirectHandle< WasmMemoryObject > memory, DirectHandle< WasmTrustedInstanceData > trusted_instance_data, DirectHandle< WasmTrustedInstanceData > shared_trusted_instance_data, int memory_index_in_instance)
void SetNewBuffer(Isolate *isolate, Tagged< JSArrayBuffer > new_buffer)
static V8_EXPORT_PRIVATE int32_t Grow(Isolate *, DirectHandle< WasmMemoryObject >, uint32_t pages)
static V8_EXPORT_PRIVATE DirectHandle< WasmMemoryObject > New(Isolate *isolate, DirectHandle< JSArrayBuffer > buffer, int maximum, wasm::AddressType address_type)
void UpdateInstances(Isolate *isolate)
void FixUpResizableArrayBuffer(Tagged< JSArrayBuffer > new_buffer)
const wasm::WasmModule * module() const
static V8_EXPORT_PRIVATE DirectHandle< WasmModuleObject > New(Isolate *isolate, std::shared_ptr< wasm::NativeModule > native_module, DirectHandle< Script > script)
static MaybeDirectHandle< String > GetFunctionNameOrNull(Isolate *, DirectHandle< WasmModuleObject >, uint32_t func_index)
static MaybeDirectHandle< String > GetModuleNameOrNull(Isolate *, DirectHandle< WasmModuleObject >)
wasm::NativeModule * native_module() const
const std::shared_ptr< wasm::NativeModule > & shared_native_module() const
base::Vector< const uint8_t > GetRawFunctionName(int func_index)
static DirectHandle< String > ExtractUtf8StringFromModuleBytes(Isolate *, DirectHandle< WasmModuleObject >, wasm::WireBytesRef, InternalizeString)
static void GetFunctionTableEntry(Isolate *isolate, DirectHandle< WasmTableObject > table, int entry_index, bool *is_valid, bool *is_null, MaybeDirectHandle< WasmTrustedInstanceData > *instance_data, int *function_index, MaybeDirectHandle< WasmJSFunction > *maybe_js_function)
static void UpdateDispatchTable(Isolate *isolate, DirectHandle< WasmTableObject > table, int entry_index, const wasm::WasmFunction *func, DirectHandle< WasmTrustedInstanceData > target_instance)
static V8_EXPORT_PRIVATE DirectHandle< WasmTableObject > New(Isolate *isolate, DirectHandle< WasmTrustedInstanceData > trusted_data, wasm::ValueType type, wasm::CanonicalValueType canonical_type, uint32_t initial, bool has_maximum, uint64_t maximum, DirectHandle< Object > initial_value, wasm::AddressType address_type, DirectHandle< WasmDispatchTable > *out_dispatch_table=nullptr)
static void SetFunctionTableEntry(Isolate *isolate, DirectHandle< WasmTableObject > table, int entry_index, DirectHandle< Object > entry)
bool is_in_bounds(uint32_t entry_index)
wasm::CanonicalValueType canonical_type(const wasm::WasmModule *module)
static V8_EXPORT_PRIVATE DirectHandle< Object > Get(Isolate *isolate, DirectHandle< WasmTableObject > table, uint32_t index)
static MaybeDirectHandle< Object > JSToWasmElement(Isolate *isolate, DirectHandle< WasmTableObject > table, DirectHandle< Object > entry, const char **error_message)
static V8_EXPORT_PRIVATE void Fill(Isolate *isolate, DirectHandle< WasmTableObject > table, uint32_t start, DirectHandle< Object > entry, uint32_t count)
static V8_EXPORT_PRIVATE void Set(Isolate *isolate, DirectHandle< WasmTableObject > table, uint32_t index, DirectHandle< Object > entry)
static V8_EXPORT_PRIVATE int Grow(Isolate *isolate, DirectHandle< WasmTableObject > table, uint32_t count, DirectHandle< Object > init_value)
static V8_EXPORT_PRIVATE void SetFunctionTablePlaceholder(Isolate *isolate, DirectHandle< WasmTableObject > table, int entry_index, DirectHandle< WasmTrustedInstanceData > trusted_instance_data, int func_index)
std::optional< uint64_t > maximum_length_u64() const
OBJECT_CONSTRUCTORS(WasmTrustedInstanceData, ExposedTrustedObject)
#define TAGGED_SIZE_8_BYTES
Definition globals.h:331
#define USE_ALLOCATION_ALIGNMENT_BOOL
Definition globals.h:1562
int start
int end
int32_t offset
TNode< Object > target
ZoneVector< RpoNumber > & result
int position
Definition liveedit.cc:290
#define FIELD_LIST(V)
FunctionSig WasmTagSig
Signature< ValueType > FunctionSig
DirectHandle< Map > CreateStructMap(Isolate *isolate, wasm::CanonicalTypeIndex struct_index, DirectHandle< Map > opt_rtt_parent, DirectHandle< NativeContext > opt_native_context)
constexpr int kTaggedSize
Definition globals.h:542
constexpr int kSimd128Size
Definition globals.h:706
@ UPDATE_WRITE_BARRIER
Definition objects.h:55
kWasmInternalFunctionIndirectPointerTag WasmTrustedInstanceData
wasm::WasmModule WasmModule
kWasmInternalFunctionIndirectPointerTag instance_data
DirectHandle< Map > CreateArrayMap(Isolate *isolate, wasm::CanonicalTypeIndex array_index, DirectHandle< Map > opt_rtt_parent)
kWasmInternalFunctionIndirectPointerTag kProtectedInstanceDataOffset sig
kMemory0SizeOffset Address kNewAllocationLimitAddressOffset Address kOldAllocationLimitAddressOffset uint8_t kGlobalsStartOffset kJumpTableStartOffset std::atomic< uint32_t > kTieringBudgetArrayOffset kDataSegmentStartsOffset kElementSegmentsOffset kInstanceObjectOffset memory_objects
constexpr int kSystemPointerSize
Definition globals.h:410
kMemory0SizeOffset Address kNewAllocationLimitAddressOffset Address kOldAllocationLimitAddressOffset uint8_t kGlobalsStartOffset kJumpTableStartOffset std::atomic< uint32_t > kTieringBudgetArrayOffset kDataSegmentStartsOffset kElementSegmentsOffset kInstanceObjectOffset kMemoryObjectsOffset kTaggedGlobalsBufferOffset tables
constexpr int kInt32Size
Definition globals.h:401
const int kHeapObjectTag
Definition v8-internal.h:72
kMemory0SizeOffset Address kNewAllocationLimitAddressOffset Address kOldAllocationLimitAddressOffset uint8_t kGlobalsStartOffset kJumpTableStartOffset std::atomic< uint32_t > kTieringBudgetArrayOffset kDataSegmentStartsOffset kElementSegmentsOffset kInstanceObjectOffset kMemoryObjectsOffset tagged_globals_buffer
constexpr int kUInt32Size
Definition globals.h:403
constexpr int kMaxInt
Definition globals.h:374
DirectHandle< Map > CreateFuncRefMap(Isolate *isolate, wasm::CanonicalTypeIndex type, DirectHandle< Map > opt_rtt_parent)
kMemory0SizeOffset Address kNewAllocationLimitAddressOffset Address kOldAllocationLimitAddressOffset uint8_t kGlobalsStartOffset kJumpTableStartOffset std::atomic< uint32_t > kTieringBudgetArrayOffset kDataSegmentStartsOffset kElementSegmentsOffset instance_object
wasm::WasmFunction WasmFunction
!IsContextMap !IsContextMap native_context
Definition map-inl.h:877
Definition c-api.cc:87
#define DECL_ACCESSORS(name,...)
#define DECL_EXTERNAL_POINTER_ACCESSORS(name, type)
#define DECL_TRUSTED_POINTER_ACCESSORS(name, type)
#define DECL_CODE_POINTER_ACCESSORS(name)
#define DECL_VERIFIER(Name)
#define DECL_PRIMITIVE_ACCESSORS(name, type)
#define DECL_PROTECTED_POINTER_ACCESSORS(name, type)
#define DECL_PRINTER(Name)
#define TQ_OBJECT_CONSTRUCTORS(Type)
#define OBJECT_CONSTRUCTORS(Type,...)
#define DCHECK_LE(v1, v2)
Definition logging.h:490
#define DCHECK_LT(v1, v2)
Definition logging.h:489
#define V8_EXPORT_PRIVATE
Definition macros.h:460
constexpr bool IsAligned(T value, U alignment)
Definition macros.h:403
#define DEFINE_FIELD_OFFSET_CONSTANTS(StartOffset, LIST_MACRO)
Definition utils.h:242
#define V8_INLINE
Definition v8config.h:500
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:671
wasm::ValueType type
#define WASM_TAGGED_INSTANCE_DATA_FIELDS(V)
#define WASM_PROTECTED_INSTANCE_DATA_FIELDS(V)
#define WASM_INSTANCE_FIELD_NAME(_, name)
#define WASM_INSTANCE_FIELD_OFFSET(offset, _)
#define DECL_OPTIONAL_ACCESSORS(name, type)
#define ASSERT_FIELD_ALIGNED(offset, size)