v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
wasm-interpreter-runtime-inl.h
Go to the documentation of this file.
1// Copyright 2024 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_WASM_INTERPRETER_WASM_INTERPRETER_RUNTIME_INL_H_
6#define V8_WASM_INTERPRETER_WASM_INTERPRETER_RUNTIME_INL_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
13// Include the non-inl header before the rest of the headers.
14
19
20namespace v8 {
21namespace internal {
22namespace wasm {
23
27 DCHECK_GE(std::numeric_limits<uintptr_t>::max(),
28 trusted_data->memory0_size());
29 DCHECK_GE(trusted_data->memory0_size(), index);
30 // Compute the effective address of the access, making sure to condition
31 // the index even in the in-bounds case.
32 return reinterpret_cast<Address>(trusted_data->memory0_start()) + index;
33}
34
36 uint64_t index, uint64_t* size, Address* out_address) const {
39 DCHECK_GE(std::numeric_limits<uintptr_t>::max(),
40 trusted_data->memory0_size());
41 if (!base::ClampToBounds<uint64_t>(index, size,
42 trusted_data->memory0_size())) {
43 return false;
44 }
45 *out_address = EffectiveAddress(index);
46 return true;
47}
48
49inline uint8_t* WasmInterpreterRuntime::GetGlobalAddress(uint32_t index) {
50 DCHECK_LT(index, module_->globals.size());
52}
53
55 uint32_t index) const {
56 // This function assumes that it is executed in a HandleScope.
57 const wasm::WasmGlobal& global = module_->globals[index];
58 DCHECK(global.type.is_reference());
59 Tagged<FixedArray> global_buffer; // The buffer of the global.
60 uint32_t global_index = 0; // The index into the buffer.
61 std::tie(global_buffer, global_index) =
62 wasm_trusted_instance_data()->GetGlobalBufferAndIndex(global);
63 return DirectHandle<Object>(global_buffer->get(global_index), isolate_);
64}
65
67 uint32_t index, DirectHandle<Object> ref) const {
68 // This function assumes that it is executed in a HandleScope.
69 const wasm::WasmGlobal& global = module_->globals[index];
70 DCHECK(global.type.is_reference());
71 Tagged<FixedArray> global_buffer; // The buffer of the global.
72 uint32_t global_index = 0; // The index into the buffer.
73 std::tie(global_buffer, global_index) =
74 wasm_trusted_instance_data()->GetGlobalBufferAndIndex(global);
75 global_buffer->set(global_index, *ref);
76}
77
81
82inline uint64_t WasmInterpreterRuntime::MemorySize() const {
83 return wasm_trusted_instance_data()->memory0_size() / kWasmPageSize;
84}
85
87 return !module_->memories.empty() && module_->memories[0].is_memory64();
88}
89
91 return wasm_trusted_instance_data()->memory0_size();
92}
93
94inline void WasmInterpreterRuntime::DataDrop(uint32_t index) {
95 wasm_trusted_instance_data()->data_segment_sizes()->set(index, 0);
96}
97
98inline void WasmInterpreterRuntime::ElemDrop(uint32_t index) {
99 wasm_trusted_instance_data()->element_segments()->set(
100 index, *isolate_->factory()->empty_fixed_array());
101}
102
104 uint32_t func_index) {
105 return codemap_->GetFunctionBytecode(func_index);
106}
107
109 const WasmRef obj, const ValueType obj_type) const {
110 return IsNull(isolate_, obj, obj_type);
111}
112
113// static
115 const ValueType obj_type) const {
117 return *isolate_->factory()->null_value();
118 } else {
119 return *isolate_->factory()->wasm_null();
120 }
121}
122
123// static
124inline bool WasmInterpreterRuntime::IsNull(Isolate* isolate, const WasmRef obj,
125 const ValueType obj_type) {
127 return i::IsNull(*obj, isolate);
128 } else {
129 return i::IsWasmNull(*obj, isolate);
130 }
131}
132
134 DirectHandle<Object> object) const {
135 // This function assumes that it is executed in a HandleScope.
136 return i::IsNull(*object, isolate_) || IsWasmNull(*object, isolate_);
137}
138
140 uint32_t index) const {
141 // This function assumes that it is executed in a HandleScope.
144}
145
147 uint32_t array_index) const {
148 return module_->array_type(ModuleTypeIndex{array_index});
149}
150
155
157 const uint8_t* current_bytecode, const uint8_t*& code) {
158 StackLimitCheck stack_check(isolate_);
159
160 current_frame_.current_bytecode_ = current_bytecode;
162
163 if (stack_check.InterruptRequested()) {
164 if (stack_check.HasOverflowed()) {
165 ClearThreadInWasmScope clear_wasm_flag(isolate_);
167 current_frame_.current_function_ = nullptr;
168 SetTrap(TrapReason::kTrapUnreachable, code);
170 return false;
171 }
173 ClearThreadInWasmScope clear_wasm_flag(isolate_);
175 current_frame_.current_function_ = nullptr;
176 SetTrap(TrapReason::kTrapUnreachable, code);
178 return false;
179 }
181 }
182
183 if (V8_UNLIKELY(v8_flags.drumbrake_fuzzer_timeout &&
186 v8_flags.drumbrake_fuzzer_timeout_limit_ms))) {
187 ClearThreadInWasmScope clear_wasm_flag(isolate_);
189 current_frame_.current_function_ = nullptr;
190 SetTrap(TrapReason::kTrapUnreachable, code);
191 return false;
192 }
193
194 return true;
195}
196
201
203 WasmInterpreterThread* thread, bool called_from_js) {
204 return interpreter_.ContinueExecution(thread, called_from_js);
205}
206
207} // namespace wasm
208} // namespace internal
209} // namespace v8
210
211#endif // V8_WASM_INTERPRETER_WASM_INTERPRETER_RUNTIME_INL_H_
static constexpr TimeDelta FromMilliseconds(int64_t milliseconds)
Definition time.h:84
static TimeTicks Now()
Definition time.cc:736
StackGuard * stack_guard()
Definition isolate.h:1198
Tagged< Object > TerminateExecution()
Definition isolate.cc:1950
v8::internal::Factory * factory()
Definition isolate.h:1527
Tagged< Object > StackOverflow()
Definition isolate.cc:1862
Tagged< Object > HandleInterrupts(InterruptLevel level=InterruptLevel::kAnyEffect)
V8_INLINE bool InterruptRequested()
Definition isolate.h:3051
V8_EXPORT_PRIVATE wasm::WasmValue GetElement(uint32_t index)
static DirectHandle< WasmFuncRef > GetOrCreateFuncRef(Isolate *isolate, DirectHandle< WasmTrustedInstanceData > trusted_instance_data, int function_index)
WasmInterpreterThread::State ContinueExecution(WasmInterpreterThread *thread, bool called_from_js)
constexpr bool is_reference() const
Definition value-type.h:600
DirectHandle< Object > GetGlobalRef(uint32_t index) const
void SetTrap(TrapReason trap_reason, pc_t trap_pc)
DirectHandle< WasmTrustedInstanceData > wasm_trusted_instance_data() const
Tagged< Object > GetNullValue(const ValueType obj_type) const
DirectHandle< Object > GetFunctionRef(uint32_t index) const
IndirectHandle< WasmInstanceObject > instance_object_
WasmBytecode * GetFunctionBytecode(uint32_t func_index)
static bool IsNull(Isolate *isolate, const WasmRef obj, const ValueType obj_type)
DirectHandle< Object > GetWasmArrayRefElement(Tagged< WasmArray > array, uint32_t index) const
const ArrayType * GetArrayType(uint32_t array_index) const
bool WasmStackCheck(const uint8_t *current_bytecode, const uint8_t *&code)
bool BoundsCheckMemRange(uint64_t index, uint64_t *size, Address *out_address) const
bool IsRefNull(DirectHandle< Object > ref) const
void SetGlobalRef(uint32_t index, DirectHandle< Object > ref) const
bool IsNullTypecheck(const WasmRef obj, const ValueType obj_type) const
void SetCurrentFrame(const FrameState &frame_state)
WasmBytecode * GetFunctionBytecode(uint32_t func_index)
WasmInterpreterThread::State ContinueExecution(WasmInterpreterThread *thread, bool called_from_js)
ValueType obj_type
bool ClampToBounds(T index, T *length, T max)
Definition bounds.h:59
constexpr IndependentHeapType kWasmNullExternRef
constexpr IndependentHeapType kWasmExternRef
constexpr size_t kWasmPageSize
V8_INLINE IndirectHandle< T > handle(Tagged< T > object, Isolate *isolate)
Definition handles-inl.h:72
V8_INLINE DirectHandle< T > direct_handle(Tagged< T > object, Isolate *isolate)
V8_EXPORT_PRIVATE FlagValues v8_flags
kInstanceDescriptorsOffset kTransitionsOrPrototypeInfoOffset IsNull(value)||IsJSProxy(value)||IsWasmObject(value)||(IsJSObject(value) &&(HeapLayout
Definition map-inl.h:70
Definition c-api.cc:87
#define DCHECK_GE(v1, v2)
Definition logging.h:488
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_LT(v1, v2)
Definition logging.h:489
#define V8_UNLIKELY(condition)
Definition v8config.h:660