1 // Copyright 2017 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_WASM_OBJECTS_INL_H_
6 #define V8_WASM_WASM_OBJECTS_INL_H_
7
8 #include "src/wasm/wasm-objects.h"
9
10 #include "src/contexts-inl.h"
11 #include "src/heap/heap-inl.h"
12 #include "src/objects/js-array-buffer-inl.h"
13 #include "src/objects/managed.h"
14 #include "src/v8memory.h"
15 #include "src/wasm/wasm-code-manager.h"
16 #include "src/wasm/wasm-module.h"
17
18 // Has to be the last include (doesn't have include guards)
19 #include "src/objects/object-macros.h"
20
21 namespace v8 {
22 namespace internal {
23
24 CAST_ACCESSOR(WasmDebugInfo)
CAST_ACCESSOR(WasmExportedFunctionData)25 CAST_ACCESSOR(WasmExportedFunctionData)
26 CAST_ACCESSOR(WasmGlobalObject)
27 CAST_ACCESSOR(WasmInstanceObject)
28 CAST_ACCESSOR(WasmMemoryObject)
29 CAST_ACCESSOR(WasmModuleObject)
30 CAST_ACCESSOR(WasmTableObject)
31
32 #define OPTIONAL_ACCESSORS(holder, name, type, offset) \
33 bool holder::has_##name() { \
34 return !READ_FIELD(this, offset)->IsUndefined(); \
35 } \
36 ACCESSORS(holder, name, type, offset)
37
38 #define READ_PRIMITIVE_FIELD(p, type, offset) \
39 (*reinterpret_cast<type const*>(FIELD_ADDR(p, offset)))
40
41 #define WRITE_PRIMITIVE_FIELD(p, type, offset, value) \
42 (*reinterpret_cast<type*>(FIELD_ADDR(p, offset)) = value)
43
44 #define PRIMITIVE_ACCESSORS(holder, name, type, offset) \
45 type holder::name() const { \
46 return READ_PRIMITIVE_FIELD(this, type, offset); \
47 } \
48 void holder::set_##name(type value) { \
49 WRITE_PRIMITIVE_FIELD(this, type, offset, value); \
50 }
51
52 // WasmModuleObject
53 ACCESSORS(WasmModuleObject, managed_native_module, Managed<wasm::NativeModule>,
54 kNativeModuleOffset)
55 ACCESSORS(WasmModuleObject, export_wrappers, FixedArray, kExportWrappersOffset)
56 ACCESSORS(WasmModuleObject, script, Script, kScriptOffset)
57 ACCESSORS(WasmModuleObject, weak_instance_list, WeakArrayList,
58 kWeakInstanceListOffset)
59 OPTIONAL_ACCESSORS(WasmModuleObject, asm_js_offset_table, ByteArray,
60 kAsmJsOffsetTableOffset)
61 OPTIONAL_ACCESSORS(WasmModuleObject, breakpoint_infos, FixedArray,
62 kBreakPointInfosOffset)
63 wasm::NativeModule* WasmModuleObject::native_module() const {
64 return managed_native_module()->raw();
65 }
module()66 const wasm::WasmModule* WasmModuleObject::module() const {
67 // TODO(clemensh): Remove this helper (inline in callers).
68 return native_module()->module();
69 }
reset_breakpoint_infos()70 void WasmModuleObject::reset_breakpoint_infos() {
71 WRITE_FIELD(this, kBreakPointInfosOffset,
72 GetReadOnlyRoots().undefined_value());
73 }
is_asm_js()74 bool WasmModuleObject::is_asm_js() {
75 bool asm_js = module()->origin == wasm::kAsmJsOrigin;
76 DCHECK_EQ(asm_js, script()->IsUserJavaScript());
77 DCHECK_EQ(asm_js, has_asm_js_offset_table());
78 return asm_js;
79 }
80
81 // WasmTableObject
ACCESSORS(WasmTableObject,functions,FixedArray,kFunctionsOffset)82 ACCESSORS(WasmTableObject, functions, FixedArray, kFunctionsOffset)
83 ACCESSORS(WasmTableObject, maximum_length, Object, kMaximumLengthOffset)
84 ACCESSORS(WasmTableObject, dispatch_tables, FixedArray, kDispatchTablesOffset)
85
86 // WasmMemoryObject
87 ACCESSORS(WasmMemoryObject, array_buffer, JSArrayBuffer, kArrayBufferOffset)
88 SMI_ACCESSORS(WasmMemoryObject, maximum_pages, kMaximumPagesOffset)
89 OPTIONAL_ACCESSORS(WasmMemoryObject, instances, WeakArrayList, kInstancesOffset)
90
91 // WasmGlobalObject
92 ACCESSORS(WasmGlobalObject, array_buffer, JSArrayBuffer, kArrayBufferOffset)
93 SMI_ACCESSORS(WasmGlobalObject, offset, kOffsetOffset)
94 SMI_ACCESSORS(WasmGlobalObject, flags, kFlagsOffset)
95 BIT_FIELD_ACCESSORS(WasmGlobalObject, flags, type, WasmGlobalObject::TypeBits)
96 BIT_FIELD_ACCESSORS(WasmGlobalObject, flags, is_mutable,
97 WasmGlobalObject::IsMutableBit)
98
99 int WasmGlobalObject::type_size() const {
100 return wasm::ValueTypes::ElementSizeInBytes(type());
101 }
102
address()103 Address WasmGlobalObject::address() const {
104 uint32_t buffer_size = 0;
105 DCHECK(array_buffer()->byte_length()->ToUint32(&buffer_size));
106 DCHECK_LE(offset() + type_size(), buffer_size);
107 USE(buffer_size);
108 return Address(array_buffer()->backing_store()) + offset();
109 }
110
GetI32()111 int32_t WasmGlobalObject::GetI32() {
112 return ReadLittleEndianValue<int32_t>(address());
113 }
114
GetI64()115 int64_t WasmGlobalObject::GetI64() {
116 return ReadLittleEndianValue<int64_t>(address());
117 }
118
GetF32()119 float WasmGlobalObject::GetF32() {
120 return ReadLittleEndianValue<float>(address());
121 }
122
GetF64()123 double WasmGlobalObject::GetF64() {
124 return ReadLittleEndianValue<double>(address());
125 }
126
SetI32(int32_t value)127 void WasmGlobalObject::SetI32(int32_t value) {
128 WriteLittleEndianValue<int32_t>(address(), value);
129 }
130
SetI64(int64_t value)131 void WasmGlobalObject::SetI64(int64_t value) {
132 WriteLittleEndianValue<int64_t>(address(), value);
133 }
134
SetF32(float value)135 void WasmGlobalObject::SetF32(float value) {
136 WriteLittleEndianValue<float>(address(), value);
137 }
138
SetF64(double value)139 void WasmGlobalObject::SetF64(double value) {
140 WriteLittleEndianValue<double>(address(), value);
141 }
142
143 // WasmInstanceObject
PRIMITIVE_ACCESSORS(WasmInstanceObject,memory_start,byte *,kMemoryStartOffset)144 PRIMITIVE_ACCESSORS(WasmInstanceObject, memory_start, byte*, kMemoryStartOffset)
145 PRIMITIVE_ACCESSORS(WasmInstanceObject, memory_size, size_t, kMemorySizeOffset)
146 PRIMITIVE_ACCESSORS(WasmInstanceObject, memory_mask, size_t, kMemoryMaskOffset)
147 PRIMITIVE_ACCESSORS(WasmInstanceObject, roots_array_address, Address,
148 kRootsArrayAddressOffset)
149 PRIMITIVE_ACCESSORS(WasmInstanceObject, stack_limit_address, Address,
150 kStackLimitAddressOffset)
151 PRIMITIVE_ACCESSORS(WasmInstanceObject, real_stack_limit_address, Address,
152 kRealStackLimitAddressOffset)
153 PRIMITIVE_ACCESSORS(WasmInstanceObject, imported_function_targets, Address*,
154 kImportedFunctionTargetsOffset)
155 PRIMITIVE_ACCESSORS(WasmInstanceObject, globals_start, byte*,
156 kGlobalsStartOffset)
157 PRIMITIVE_ACCESSORS(WasmInstanceObject, imported_mutable_globals, Address*,
158 kImportedMutableGlobalsOffset)
159 PRIMITIVE_ACCESSORS(WasmInstanceObject, indirect_function_table_size, uint32_t,
160 kIndirectFunctionTableSizeOffset)
161 PRIMITIVE_ACCESSORS(WasmInstanceObject, indirect_function_table_sig_ids,
162 uint32_t*, kIndirectFunctionTableSigIdsOffset)
163 PRIMITIVE_ACCESSORS(WasmInstanceObject, indirect_function_table_targets,
164 Address*, kIndirectFunctionTableTargetsOffset)
165 PRIMITIVE_ACCESSORS(WasmInstanceObject, jump_table_start, Address,
166 kJumpTableStartOffset)
167
168 ACCESSORS(WasmInstanceObject, module_object, WasmModuleObject,
169 kModuleObjectOffset)
170 ACCESSORS(WasmInstanceObject, exports_object, JSObject, kExportsObjectOffset)
171 ACCESSORS(WasmInstanceObject, native_context, Context, kNativeContextOffset)
172 OPTIONAL_ACCESSORS(WasmInstanceObject, memory_object, WasmMemoryObject,
173 kMemoryObjectOffset)
174 OPTIONAL_ACCESSORS(WasmInstanceObject, globals_buffer, JSArrayBuffer,
175 kGlobalsBufferOffset)
176 OPTIONAL_ACCESSORS(WasmInstanceObject, imported_mutable_globals_buffers,
177 FixedArray, kImportedMutableGlobalsBuffersOffset)
178 OPTIONAL_ACCESSORS(WasmInstanceObject, debug_info, WasmDebugInfo,
179 kDebugInfoOffset)
180 OPTIONAL_ACCESSORS(WasmInstanceObject, table_object, WasmTableObject,
181 kTableObjectOffset)
182 ACCESSORS(WasmInstanceObject, imported_function_instances, FixedArray,
183 kImportedFunctionInstancesOffset)
184 ACCESSORS(WasmInstanceObject, imported_function_callables, FixedArray,
185 kImportedFunctionCallablesOffset)
186 OPTIONAL_ACCESSORS(WasmInstanceObject, indirect_function_table_instances,
187 FixedArray, kIndirectFunctionTableInstancesOffset)
188 OPTIONAL_ACCESSORS(WasmInstanceObject, managed_native_allocations, Foreign,
189 kManagedNativeAllocationsOffset)
190 ACCESSORS(WasmInstanceObject, undefined_value, Oddball, kUndefinedValueOffset)
191 ACCESSORS(WasmInstanceObject, null_value, Oddball, kNullValueOffset)
192 ACCESSORS(WasmInstanceObject, centry_stub, Code, kCEntryStubOffset)
193
194 inline bool WasmInstanceObject::has_indirect_function_table() {
195 return indirect_function_table_sig_ids() != nullptr;
196 }
197
IndirectFunctionTableEntry(Handle<WasmInstanceObject> instance,int index)198 IndirectFunctionTableEntry::IndirectFunctionTableEntry(
199 Handle<WasmInstanceObject> instance, int index)
200 : instance_(instance), index_(index) {
201 DCHECK_GE(index, 0);
202 DCHECK_LT(index, instance->indirect_function_table_size());
203 }
204
ImportedFunctionEntry(Handle<WasmInstanceObject> instance,int index)205 ImportedFunctionEntry::ImportedFunctionEntry(
206 Handle<WasmInstanceObject> instance, int index)
207 : instance_(instance), index_(index) {
208 DCHECK_GE(index, 0);
209 DCHECK_LT(index, instance->module()->num_imported_functions);
210 }
211
212 // WasmExportedFunctionData
ACCESSORS(WasmExportedFunctionData,wrapper_code,Code,kWrapperCodeOffset)213 ACCESSORS(WasmExportedFunctionData, wrapper_code, Code, kWrapperCodeOffset)
214 ACCESSORS(WasmExportedFunctionData, instance, WasmInstanceObject,
215 kInstanceOffset)
216 SMI_ACCESSORS(WasmExportedFunctionData, jump_table_offset,
217 kJumpTableOffsetOffset)
218 SMI_ACCESSORS(WasmExportedFunctionData, function_index, kFunctionIndexOffset)
219
220 // WasmDebugInfo
221 ACCESSORS(WasmDebugInfo, wasm_instance, WasmInstanceObject, kInstanceOffset)
222 ACCESSORS(WasmDebugInfo, interpreter_handle, Object, kInterpreterHandleOffset)
223 ACCESSORS(WasmDebugInfo, interpreted_functions, Object,
224 kInterpretedFunctionsOffset)
225 OPTIONAL_ACCESSORS(WasmDebugInfo, locals_names, FixedArray, kLocalsNamesOffset)
226 OPTIONAL_ACCESSORS(WasmDebugInfo, c_wasm_entries, FixedArray,
227 kCWasmEntriesOffset)
228 OPTIONAL_ACCESSORS(WasmDebugInfo, c_wasm_entry_map, Managed<wasm::SignatureMap>,
229 kCWasmEntryMapOffset)
230
231 #undef OPTIONAL_ACCESSORS
232 #undef READ_PRIMITIVE_FIELD
233 #undef WRITE_PRIMITIVE_FIELD
234 #undef PRIMITIVE_ACCESSORS
235
236 uint32_t WasmTableObject::current_length() { return functions()->length(); }
237
has_maximum_pages()238 bool WasmMemoryObject::has_maximum_pages() { return maximum_pages() >= 0; }
239
240 #include "src/objects/object-macros-undef.h"
241
242 } // namespace internal
243 } // namespace v8
244
245 #endif // V8_WASM_WASM_OBJECTS_INL_H_
246