OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <memory> | 5 #include <memory> |
6 | 6 |
7 #include "src/base/atomic-utils.h" | 7 #include "src/base/atomic-utils.h" |
8 #include "src/code-stubs.h" | 8 #include "src/code-stubs.h" |
9 | 9 |
10 #include "src/macro-assembler.h" | 10 #include "src/macro-assembler.h" |
11 #include "src/objects.h" | 11 #include "src/objects.h" |
12 #include "src/property-descriptor.h" | 12 #include "src/property-descriptor.h" |
13 #include "src/simulator.h" | 13 #include "src/simulator.h" |
14 #include "src/snapshot/snapshot.h" | 14 #include "src/snapshot/snapshot.h" |
15 #include "src/v8.h" | 15 #include "src/v8.h" |
16 | 16 |
17 #include "src/wasm/ast-decoder.h" | 17 #include "src/wasm/ast-decoder.h" |
18 #include "src/wasm/module-decoder.h" | 18 #include "src/wasm/module-decoder.h" |
19 #include "src/wasm/wasm-debug.h" | 19 #include "src/wasm/wasm-debug.h" |
20 #include "src/wasm/wasm-function-name-table.h" | 20 #include "src/wasm/wasm-function-name-table.h" |
21 #include "src/wasm/wasm-module.h" | 21 #include "src/wasm/wasm-module.h" |
22 #include "src/wasm/wasm-result.h" | 22 #include "src/wasm/wasm-result.h" |
23 | 23 |
24 #include "src/compiler/wasm-compiler.h" | 24 #include "src/compiler/wasm-compiler.h" |
25 | 25 |
26 namespace v8 { | 26 namespace v8 { |
27 namespace internal { | 27 namespace internal { |
28 namespace wasm { | 28 namespace wasm { |
29 | 29 |
| 30 const char* SectionName(WasmSectionCode code) { |
| 31 switch (code) { |
| 32 case kUnknownSectionCode: |
| 33 return "Unknown"; |
| 34 case kTypeSectionCode: |
| 35 return "Type"; |
| 36 case kImportSectionCode: |
| 37 return "Import"; |
| 38 case kFunctionSectionCode: |
| 39 return "Function"; |
| 40 case kTableSectionCode: |
| 41 return "Table"; |
| 42 case kMemorySectionCode: |
| 43 return "Memory"; |
| 44 case kGlobalSectionCode: |
| 45 return "Global"; |
| 46 case kExportSectionCode: |
| 47 return "Export"; |
| 48 case kStartSectionCode: |
| 49 return "Start"; |
| 50 case kCodeSectionCode: |
| 51 return "Code"; |
| 52 case kElementSectionCode: |
| 53 return "Element"; |
| 54 case kDataSectionCode: |
| 55 return "Data"; |
| 56 case kNameSectionCode: |
| 57 return "Name"; |
| 58 default: |
| 59 return "<unknown>"; |
| 60 } |
| 61 } |
| 62 |
30 enum JSFunctionExportInternalField { | 63 enum JSFunctionExportInternalField { |
31 kInternalModuleInstance, | 64 kInternalModuleInstance, |
32 kInternalArity, | 65 kInternalArity, |
33 kInternalSignature | 66 kInternalSignature |
34 }; | 67 }; |
35 | 68 |
36 static const int kPlaceholderMarker = 1000000000; | 69 static const int kPlaceholderMarker = 1000000000; |
37 | 70 |
38 static const char* wasmSections[] = { | |
39 #define F(enumerator, order, string) string, | |
40 FOR_EACH_WASM_SECTION_TYPE(F) | |
41 #undef F | |
42 "<unknown>" // entry for "Max" | |
43 }; | |
44 | |
45 static uint8_t wasmSectionsLengths[]{ | |
46 #define F(enumerator, order, string) sizeof(string) - 1, | |
47 FOR_EACH_WASM_SECTION_TYPE(F) | |
48 #undef F | |
49 9 // entry for "Max" | |
50 }; | |
51 | |
52 static uint8_t wasmSectionsOrders[]{ | |
53 #define F(enumerator, order, string) order, | |
54 FOR_EACH_WASM_SECTION_TYPE(F) | |
55 #undef F | |
56 0 // entry for "Max" | |
57 }; | |
58 | |
59 static_assert(sizeof(wasmSections) / sizeof(wasmSections[0]) == | |
60 (size_t)WasmSection::Code::Max + 1, | |
61 "expected enum WasmSection::Code to be monotonic from 0"); | |
62 | |
63 WasmSection::Code WasmSection::begin() { return (WasmSection::Code)0; } | |
64 WasmSection::Code WasmSection::end() { return WasmSection::Code::Max; } | |
65 WasmSection::Code WasmSection::next(WasmSection::Code code) { | |
66 return (WasmSection::Code)(1 + (uint32_t)code); | |
67 } | |
68 | |
69 const char* WasmSection::getName(WasmSection::Code code) { | |
70 return wasmSections[(size_t)code]; | |
71 } | |
72 | |
73 size_t WasmSection::getNameLength(WasmSection::Code code) { | |
74 return wasmSectionsLengths[(size_t)code]; | |
75 } | |
76 | |
77 int WasmSection::getOrder(WasmSection::Code code) { | |
78 return wasmSectionsOrders[(size_t)code]; | |
79 } | |
80 | |
81 WasmSection::Code WasmSection::lookup(const byte* string, uint32_t length) { | |
82 // TODO(jfb) Linear search, it may be better to do a common-prefix search. | |
83 for (Code i = begin(); i != end(); i = next(i)) { | |
84 if (getNameLength(i) == length && 0 == memcmp(getName(i), string, length)) { | |
85 return i; | |
86 } | |
87 } | |
88 return Code::Max; | |
89 } | |
90 | |
91 std::ostream& operator<<(std::ostream& os, const WasmModule& module) { | 71 std::ostream& operator<<(std::ostream& os, const WasmModule& module) { |
92 os << "WASM module with "; | 72 os << "WASM module with "; |
93 os << (module.min_mem_pages * module.kPageSize) << " min mem"; | 73 os << (module.min_mem_pages * module.kPageSize) << " min mem"; |
94 os << (module.max_mem_pages * module.kPageSize) << " max mem"; | 74 os << (module.max_mem_pages * module.kPageSize) << " max mem"; |
95 os << module.functions.size() << " functions"; | 75 os << module.functions.size() << " functions"; |
96 os << module.functions.size() << " globals"; | 76 os << module.functions.size() << " globals"; |
97 os << module.functions.size() << " data segments"; | 77 os << module.functions.size() << " data segments"; |
98 return os; | 78 return os; |
99 } | 79 } |
100 | 80 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 DCHECK_NOT_NULL(deopt_data); | 131 DCHECK_NOT_NULL(deopt_data); |
152 DCHECK(deopt_data->length() == 2); | 132 DCHECK(deopt_data->length() == 2); |
153 Object* weak_link = deopt_data->get(0); | 133 Object* weak_link = deopt_data->get(0); |
154 if (weak_link == undefined) return undefined; | 134 if (weak_link == undefined) return undefined; |
155 WeakCell* cell = WeakCell::cast(weak_link); | 135 WeakCell* cell = WeakCell::cast(weak_link); |
156 return cell->value(); | 136 return cell->value(); |
157 } | 137 } |
158 | 138 |
159 namespace { | 139 namespace { |
160 // Internal constants for the layout of the module object. | 140 // Internal constants for the layout of the module object. |
161 enum WasmInstanceFields { | 141 enum WasmInstanceObjectFields { |
162 kWasmCompiledModule = 0, | 142 kWasmCompiledModule = 0, |
163 kWasmModuleFunctionTable, | 143 kWasmModuleFunctionTable, |
164 kWasmModuleCodeTable, | 144 kWasmModuleCodeTable, |
165 kWasmMemArrayBuffer, | 145 kWasmMemArrayBuffer, |
166 kWasmGlobalsArrayBuffer, | 146 kWasmGlobalsArrayBuffer, |
167 // TODO(clemensh): Remove function name array, extract names from module | 147 // TODO(clemensh): Remove function name array, extract names from module |
168 // bytes. | 148 // bytes. |
169 kWasmFunctionNamesArray, | 149 kWasmFunctionNamesArray, |
170 kWasmModuleBytesString, | 150 kWasmModuleBytesString, |
171 kWasmDebugInfo, | 151 kWasmDebugInfo, |
| 152 kWasmNumImportedFunctions, |
172 kWasmModuleInternalFieldCount | 153 kWasmModuleInternalFieldCount |
173 }; | 154 }; |
174 | 155 |
175 // TODO(mtrofin): Unnecessary once we stop using JS Heap for wasm code. | 156 // TODO(mtrofin): Unnecessary once we stop using JS Heap for wasm code. |
176 // For now, each field is expected to have the type commented by its side. | 157 // For now, each field is expected to have the type commented by its side. |
177 // The elements typed as "maybe" are optional. The others are mandatory. Since | 158 // The elements typed as "maybe" are optional. The others are mandatory. Since |
178 // the compiled module is either obtained from the current v8 instance, or from | 159 // the compiled module is either obtained from the current v8 instance, or from |
179 // a snapshot produced by a compatible (==identical) v8 instance, we simply | 160 // a snapshot produced by a compatible (==identical) v8 instance, we simply |
180 // fail at instantiation time, in the face of invalid data. | 161 // fail at instantiation time, in the face of invalid data. |
181 enum CompiledWasmObjectFields { | 162 enum WasmCompiledModuleFields { |
182 kFunctions, // FixedArray of Code | 163 kCodeTable, // FixedArray of Code |
183 kImportData, // maybe FixedArray of FixedArray respecting the | 164 kImportData, // maybe FixedArray of FixedArray respecting the |
184 // WasmImportMetadata structure. | 165 // WasmImportData structure. |
185 kImportMap, // FixedArray. The i-th element is the Code object used for | 166 kExportData, // maybe FixedArray of FixedArray of WasmExportData |
186 // import i | 167 // structure |
187 kExports, // maybe FixedArray of FixedArray of WasmExportMetadata | 168 kStartupData, // maybe FixedArray of WasmExportData structure |
188 // structure | |
189 kStartupFunction, // maybe FixedArray of WasmExportMetadata structure | |
190 kTableOfIndirectFunctionTables, // maybe FixedArray of FixedArray of | 169 kTableOfIndirectFunctionTables, // maybe FixedArray of FixedArray of |
191 // WasmIndirectFunctionTableMetadata | 170 // WasmIndirectFunctionTableData |
192 kModuleBytes, // maybe String | 171 kModuleBytes, // maybe String |
193 kFunctionNameTable, // maybe ByteArray | 172 kFunctionNameTable, // maybe ByteArray |
194 kMinRequiredMemory, // Smi. an uint32_t | 173 kMinRequiredMemory, // Smi. an uint32_t |
195 // The following 2 are either together present or absent: | 174 // The following 2 are either together present or absent: |
196 kDataSegmentsInfo, // maybe FixedArray of FixedArray respecting the | 175 kDataSegmentsInfo, // maybe FixedArray of FixedArray respecting the |
197 // WasmSegmentInfo structure | 176 // WasmSegmentInfo structure |
198 kDataSegments, // maybe ByteArray. | 177 kDataSegments, // maybe ByteArray. |
199 | 178 |
200 kGlobalsSize, // Smi. an uint32_t | 179 kGlobalsSize, // Smi. an uint32_t |
201 kMemSize, // Smi.an uint32_t | 180 kMemSize, // Smi.an uint32_t |
202 kMemStart, // MaybeHandle<ArrayBuffer> | 181 kMemStart, // MaybeHandle<ArrayBuffer> |
203 kExportMem, // Smi. bool | 182 kExportMem, // Smi. bool |
204 kOrigin, // Smi. ModuleOrigin | 183 kOrigin, // Smi. ModuleOrigin |
205 kNextInstance, // WeakCell. See compiled code cloning. | 184 kNextInstance, // WeakCell. See compiled code cloning. |
206 kPrevInstance, // WeakCell. See compiled code cloning. | 185 kPrevInstance, // WeakCell. See compiled code cloning. |
207 kOwningInstance, // WeakCell, pointing to the owning instance. | 186 kOwningInstance, // WeakCell, pointing to the owning instance. |
208 kModuleObject, // WeakCell, pointing to the module object. | 187 kModuleObject, // WeakCell, pointing to the module object. |
209 kCompiledWasmObjectTableSize // Sentinel value. | 188 kCompiledWasmObjectTableSize // Sentinel value. |
210 }; | 189 }; |
211 | 190 |
212 enum WasmImportMetadata { | 191 enum WasmImportData { |
213 kModuleName, // String | 192 kModuleName, // String |
214 kFunctionName, // maybe String | 193 kFunctionName, // maybe String |
215 kOutputCount, // Smi. an uint32_t | 194 kOutputCount, // Smi. an uint32_t |
216 kSignature, // ByteArray. A copy of the data in FunctionSig | 195 kSignature, // ByteArray. A copy of the data in FunctionSig |
217 kWasmImportDataTableSize // Sentinel value. | 196 kWasmImportDataTableSize // Sentinel value. |
218 }; | 197 }; |
219 | 198 |
220 enum WasmExportMetadata { | 199 enum WasmExportData { |
221 kExportCode, // Code | 200 kExportName, // String |
222 kExportName, // String | 201 kExportArity, // Smi, an int |
223 kExportArity, // Smi, an int | 202 kExportedFunctionIndex, // Smi, an uint32_t |
224 kExportedFunctionIndex, // Smi, an uint32_t | 203 kExportedSignature, // ByteArray. A copy of the data in FunctionSig |
225 kExportedSignature, // ByteArray. A copy of the data in FunctionSig | 204 kWasmExportDataTableSize // Sentinel value. |
226 kWasmExportMetadataTableSize // Sentinel value. | |
227 }; | 205 }; |
228 | 206 |
229 enum WasmSegmentInfo { | 207 enum WasmSegmentInfo { |
230 kDestAddr, // Smi. an uint32_t | 208 kDestAddr, // Smi. an uint32_t |
231 kSourceSize, // Smi. an uint32_t | 209 kSourceSize, // Smi. an uint32_t |
232 kWasmSegmentInfoSize // Sentinel value. | 210 kWasmSegmentInfoSize // Sentinel value. |
233 }; | 211 }; |
234 | 212 |
235 enum WasmIndirectFunctionTableMetadata { | 213 enum WasmIndirectFunctionTableData { |
236 kSize, // Smi. an uint32_t | 214 kSize, // Smi. an uint32_t |
237 kTable, // FixedArray of indirect function table | 215 kTable, // FixedArray of indirect function table |
238 kWasmIndirectFunctionTableMetadataSize // Sentinel value. | 216 kWasmIndirectFunctionTableDataSize // Sentinel value. |
239 }; | 217 }; |
240 | 218 |
241 uint32_t GetMinModuleMemSize(const WasmModule* module) { | 219 uint32_t GetMinModuleMemSize(const WasmModule* module) { |
242 return WasmModule::kPageSize * module->min_mem_pages; | 220 return WasmModule::kPageSize * module->min_mem_pages; |
243 } | 221 } |
244 | 222 |
245 void LoadDataSegments(Handle<FixedArray> compiled_module, Address mem_addr, | 223 void LoadDataSegments(Handle<FixedArray> compiled_module, Address mem_addr, |
246 size_t mem_size) { | 224 size_t mem_size) { |
247 Isolate* isolate = compiled_module->GetIsolate(); | 225 Isolate* isolate = compiled_module->GetIsolate(); |
248 MaybeHandle<ByteArray> maybe_data = | 226 MaybeHandle<ByteArray> maybe_data = |
(...skipping 23 matching lines...) Expand all Loading... |
272 last_extraction_pos += source_size; | 250 last_extraction_pos += source_size; |
273 } | 251 } |
274 } | 252 } |
275 | 253 |
276 void SaveDataSegmentInfo(Factory* factory, const WasmModule* module, | 254 void SaveDataSegmentInfo(Factory* factory, const WasmModule* module, |
277 Handle<FixedArray> compiled_module) { | 255 Handle<FixedArray> compiled_module) { |
278 Handle<FixedArray> segments = factory->NewFixedArray( | 256 Handle<FixedArray> segments = factory->NewFixedArray( |
279 static_cast<int>(module->data_segments.size()), TENURED); | 257 static_cast<int>(module->data_segments.size()), TENURED); |
280 uint32_t data_size = 0; | 258 uint32_t data_size = 0; |
281 for (const WasmDataSegment& segment : module->data_segments) { | 259 for (const WasmDataSegment& segment : module->data_segments) { |
282 if (!segment.init) continue; | |
283 if (segment.source_size == 0) continue; | 260 if (segment.source_size == 0) continue; |
284 data_size += segment.source_size; | 261 data_size += segment.source_size; |
285 } | 262 } |
286 Handle<ByteArray> data = factory->NewByteArray(data_size, TENURED); | 263 Handle<ByteArray> data = factory->NewByteArray(data_size, TENURED); |
287 | 264 |
288 uint32_t last_insertion_pos = 0; | 265 uint32_t last_insertion_pos = 0; |
289 for (uint32_t i = 0; i < module->data_segments.size(); ++i) { | 266 for (uint32_t i = 0; i < module->data_segments.size(); ++i) { |
290 const WasmDataSegment& segment = module->data_segments[i]; | 267 const WasmDataSegment& segment = module->data_segments[i]; |
291 if (!segment.init) continue; | |
292 if (segment.source_size == 0) continue; | 268 if (segment.source_size == 0) continue; |
293 Handle<ByteArray> js_segment = | 269 Handle<ByteArray> js_segment = |
294 factory->NewByteArray(kWasmSegmentInfoSize * sizeof(uint32_t), TENURED); | 270 factory->NewByteArray(kWasmSegmentInfoSize * sizeof(uint32_t), TENURED); |
295 js_segment->set_int(kDestAddr, segment.dest_addr); | 271 // TODO(titzer): add support for global offsets for dest_addr |
| 272 CHECK_EQ(WasmInitExpr::kI32Const, segment.dest_addr.kind); |
| 273 js_segment->set_int(kDestAddr, segment.dest_addr.val.i32_const); |
296 js_segment->set_int(kSourceSize, segment.source_size); | 274 js_segment->set_int(kSourceSize, segment.source_size); |
297 segments->set(i, *js_segment); | 275 segments->set(i, *js_segment); |
298 data->copy_in(last_insertion_pos, | 276 data->copy_in(last_insertion_pos, |
299 module->module_start + segment.source_offset, | 277 module->module_start + segment.source_offset, |
300 segment.source_size); | 278 segment.source_size); |
301 last_insertion_pos += segment.source_size; | 279 last_insertion_pos += segment.source_size; |
302 } | 280 } |
303 compiled_module->set(kDataSegmentsInfo, *segments); | 281 compiled_module->set(kDataSegmentsInfo, *segments); |
304 compiled_module->set(kDataSegments, *data); | 282 compiled_module->set(kDataSegments, *data); |
305 } | 283 } |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 // TODO(titzer): placeholder code objects are somewhat dangerous. | 372 // TODO(titzer): placeholder code objects are somewhat dangerous. |
395 static byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions. | 373 static byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions. |
396 static CodeDesc desc = { | 374 static CodeDesc desc = { |
397 buffer, arraysize(buffer), arraysize(buffer), 0, 0, nullptr, 0, nullptr}; | 375 buffer, arraysize(buffer), arraysize(buffer), 0, 0, nullptr, 0, nullptr}; |
398 Handle<Code> code = factory->NewCode(desc, Code::KindField::encode(kind), | 376 Handle<Code> code = factory->NewCode(desc, Code::KindField::encode(kind), |
399 Handle<Object>::null()); | 377 Handle<Object>::null()); |
400 code->set_constant_pool_offset(static_cast<int>(index) + kPlaceholderMarker); | 378 code->set_constant_pool_offset(static_cast<int>(index) + kPlaceholderMarker); |
401 return code; | 379 return code; |
402 } | 380 } |
403 | 381 |
404 // TODO(mtrofin): remove when we stop relying on placeholders. | 382 bool LinkFunction(Handle<Code> unlinked, |
405 void InitializePlaceholders(Factory* factory, | 383 std::vector<Handle<Code>>& code_table) { |
406 std::vector<Handle<Code>>* placeholders, | |
407 size_t size) { | |
408 DCHECK(placeholders->empty()); | |
409 placeholders->reserve(size); | |
410 | |
411 for (uint32_t i = 0; i < size; ++i) { | |
412 placeholders->push_back(CreatePlaceholder(factory, i, Code::WASM_FUNCTION)); | |
413 } | |
414 } | |
415 | |
416 bool LinkFunction(Isolate* isolate, Handle<Code> unlinked, | |
417 Handle<FixedArray> code_targets, | |
418 Code::Kind kind = Code::WASM_FUNCTION) { | |
419 bool modified = false; | 384 bool modified = false; |
420 int mode_mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET); | 385 int mode_mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET); |
421 AllowDeferredHandleDereference embedding_raw_address; | 386 AllowDeferredHandleDereference embedding_raw_address; |
422 for (RelocIterator it(*unlinked, mode_mask); !it.done(); it.next()) { | 387 for (RelocIterator it(*unlinked, mode_mask); !it.done(); it.next()) { |
423 Code* target = Code::GetCodeFromTargetAddress(it.rinfo()->target_address()); | 388 RelocInfo::Mode mode = it.rinfo()->rmode(); |
424 if (target->kind() == kind && | 389 if (RelocInfo::IsCodeTarget(mode)) { |
425 target->constant_pool_offset() >= kPlaceholderMarker) { | 390 Code* target = |
426 // Patch direct calls to placeholder code objects. | 391 Code::GetCodeFromTargetAddress(it.rinfo()->target_address()); |
427 uint32_t index = target->constant_pool_offset() - kPlaceholderMarker; | 392 if (target->constant_pool_offset() < kPlaceholderMarker) continue; |
428 CHECK(index < static_cast<uint32_t>(code_targets->length())); | 393 switch (target->kind()) { |
429 Handle<Code> new_target = | 394 case Code::WASM_FUNCTION: // fall through |
430 code_targets->GetValueChecked<Code>(isolate, index); | 395 case Code::WASM_TO_JS_FUNCTION: // fall through |
431 if (target != *new_target) { | 396 case Code::JS_TO_WASM_FUNCTION: { |
432 it.rinfo()->set_target_address(new_target->instruction_start(), | 397 // Patch direct calls to placeholder code objects. |
433 UPDATE_WRITE_BARRIER, SKIP_ICACHE_FLUSH); | 398 uint32_t index = target->constant_pool_offset() - kPlaceholderMarker; |
434 modified = true; | 399 Handle<Code> new_target = code_table[index]; |
| 400 if (target != *new_target) { |
| 401 it.rinfo()->set_target_address(new_target->instruction_start(), |
| 402 UPDATE_WRITE_BARRIER, |
| 403 SKIP_ICACHE_FLUSH); |
| 404 modified = true; |
| 405 } |
| 406 break; |
| 407 } |
| 408 default: |
| 409 break; |
435 } | 410 } |
436 } | 411 } |
437 } | 412 } |
438 return modified; | 413 return modified; |
439 } | 414 } |
440 | 415 |
441 void LinkModuleFunctions(Isolate* isolate, Handle<FixedArray> functions) { | 416 void FlushICache(Isolate* isolate, Handle<FixedArray> functions) { |
442 for (int i = 0; i < functions->length(); ++i) { | |
443 Handle<Code> code = functions->GetValueChecked<Code>(isolate, i); | |
444 LinkFunction(isolate, code, functions); | |
445 } | |
446 } | |
447 | |
448 void FlushAssemblyCache(Isolate* isolate, Handle<FixedArray> functions) { | |
449 for (int i = 0; i < functions->length(); ++i) { | 417 for (int i = 0; i < functions->length(); ++i) { |
450 Handle<Code> code = functions->GetValueChecked<Code>(isolate, i); | 418 Handle<Code> code = functions->GetValueChecked<Code>(isolate, i); |
451 Assembler::FlushICache(isolate, code->instruction_start(), | 419 Assembler::FlushICache(isolate, code->instruction_start(), |
452 code->instruction_size()); | 420 code->instruction_size()); |
453 } | 421 } |
454 } | 422 } |
| 423 } // namespace |
455 | 424 |
456 void SetRuntimeSupport(Isolate* isolate, Handle<JSObject> js_object) { | 425 uint32_t GetNumImportedFunctions(Handle<JSObject> wasm_object) { |
457 Handle<FixedArray> functions = Handle<FixedArray>( | 426 return static_cast<uint32_t>( |
458 FixedArray::cast(js_object->GetInternalField(kWasmModuleCodeTable))); | 427 Smi::cast(wasm_object->GetInternalField(kWasmNumImportedFunctions)) |
459 Handle<WeakCell> weak_link = isolate->factory()->NewWeakCell(js_object); | 428 ->value()); |
460 | |
461 for (int i = FLAG_skip_compiling_wasm_funcs; i < functions->length(); ++i) { | |
462 Handle<Code> code = functions->GetValueChecked<Code>(isolate, i); | |
463 Handle<FixedArray> deopt_data = | |
464 isolate->factory()->NewFixedArray(2, TENURED); | |
465 deopt_data->set(0, *weak_link); | |
466 deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); | |
467 deopt_data->set_length(2); | |
468 code->set_deoptimization_data(*deopt_data); | |
469 } | |
470 } | 429 } |
471 | 430 |
472 } // namespace | |
473 | |
474 WasmModule::WasmModule(byte* module_start) | 431 WasmModule::WasmModule(byte* module_start) |
475 : module_start(module_start), | 432 : module_start(module_start), |
476 module_end(nullptr), | 433 module_end(nullptr), |
477 min_mem_pages(0), | 434 min_mem_pages(0), |
478 max_mem_pages(0), | 435 max_mem_pages(0), |
479 mem_export(false), | 436 mem_export(false), |
480 mem_external(false), | |
481 start_function_index(-1), | 437 start_function_index(-1), |
482 origin(kWasmOrigin), | 438 origin(kWasmOrigin), |
483 globals_size(0), | 439 globals_size(0), |
| 440 num_imported_functions(0), |
| 441 num_declared_functions(0), |
| 442 num_exported_functions(0), |
484 pending_tasks(new base::Semaphore(0)) {} | 443 pending_tasks(new base::Semaphore(0)) {} |
485 | 444 |
486 static MaybeHandle<JSFunction> ReportFFIError( | 445 static MaybeHandle<JSFunction> ReportFFIError( |
487 ErrorThrower* thrower, const char* error, uint32_t index, | 446 ErrorThrower* thrower, const char* error, uint32_t index, |
488 Handle<String> module_name, MaybeHandle<String> function_name) { | 447 Handle<String> module_name, MaybeHandle<String> function_name) { |
489 Handle<String> function_name_handle; | 448 Handle<String> function_name_handle; |
490 if (function_name.ToHandle(&function_name_handle)) { | 449 if (function_name.ToHandle(&function_name_handle)) { |
491 thrower->Error("Import #%d module=\"%.*s\" function=\"%.*s\" error: %s", | 450 thrower->Error("Import #%d module=\"%.*s\" function=\"%.*s\" error: %s", |
492 index, module_name->length(), module_name->ToCString().get(), | 451 index, module_name->length(), module_name->ToCString().get(), |
493 function_name_handle->length(), | 452 function_name_handle->length(), |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 bool FetchAndExecuteCompilationUnit( | 512 bool FetchAndExecuteCompilationUnit( |
554 Isolate* isolate, | 513 Isolate* isolate, |
555 std::vector<compiler::WasmCompilationUnit*>* compilation_units, | 514 std::vector<compiler::WasmCompilationUnit*>* compilation_units, |
556 std::queue<compiler::WasmCompilationUnit*>* executed_units, | 515 std::queue<compiler::WasmCompilationUnit*>* executed_units, |
557 base::Mutex* result_mutex, base::AtomicNumber<size_t>* next_unit) { | 516 base::Mutex* result_mutex, base::AtomicNumber<size_t>* next_unit) { |
558 DisallowHeapAllocation no_allocation; | 517 DisallowHeapAllocation no_allocation; |
559 DisallowHandleAllocation no_handles; | 518 DisallowHandleAllocation no_handles; |
560 DisallowHandleDereference no_deref; | 519 DisallowHandleDereference no_deref; |
561 DisallowCodeDependencyChange no_dependency_change; | 520 DisallowCodeDependencyChange no_dependency_change; |
562 | 521 |
563 // - 1 because AtomicIntrement returns the value after the atomic increment. | 522 // - 1 because AtomicIncrement returns the value after the atomic increment. |
564 size_t index = next_unit->Increment(1) - 1; | 523 size_t index = next_unit->Increment(1) - 1; |
565 if (index >= compilation_units->size()) { | 524 if (index >= compilation_units->size()) { |
566 return false; | 525 return false; |
567 } | 526 } |
568 | 527 |
569 compiler::WasmCompilationUnit* unit = compilation_units->at(index); | 528 compiler::WasmCompilationUnit* unit = compilation_units->at(index); |
570 if (unit != nullptr) { | 529 if (unit != nullptr) { |
571 unit->ExecuteCompilation(); | 530 unit->ExecuteCompilation(); |
572 { | 531 base::LockGuard<base::Mutex> guard(result_mutex); |
573 base::LockGuard<base::Mutex> guard(result_mutex); | 532 executed_units->push(unit); |
574 executed_units->push(unit); | |
575 } | |
576 } | 533 } |
577 return true; | 534 return true; |
578 } | 535 } |
579 | 536 |
580 class WasmCompilationTask : public CancelableTask { | 537 class WasmCompilationTask : public CancelableTask { |
581 public: | 538 public: |
582 WasmCompilationTask( | 539 WasmCompilationTask( |
583 Isolate* isolate, | 540 Isolate* isolate, |
584 std::vector<compiler::WasmCompilationUnit*>* compilation_units, | 541 std::vector<compiler::WasmCompilationUnit*>* compilation_units, |
585 std::queue<compiler::WasmCompilationUnit*>* executed_units, | 542 std::queue<compiler::WasmCompilationUnit*>* executed_units, |
(...skipping 22 matching lines...) Expand all Loading... |
608 base::Mutex* result_mutex_; | 565 base::Mutex* result_mutex_; |
609 base::AtomicNumber<size_t>* next_unit_; | 566 base::AtomicNumber<size_t>* next_unit_; |
610 }; | 567 }; |
611 | 568 |
612 static void RecordStats(Isolate* isolate, Code* code) { | 569 static void RecordStats(Isolate* isolate, Code* code) { |
613 isolate->counters()->wasm_generated_code_size()->Increment(code->body_size()); | 570 isolate->counters()->wasm_generated_code_size()->Increment(code->body_size()); |
614 isolate->counters()->wasm_reloc_size()->Increment( | 571 isolate->counters()->wasm_reloc_size()->Increment( |
615 code->relocation_info()->length()); | 572 code->relocation_info()->length()); |
616 } | 573 } |
617 | 574 |
618 static void RecordStats(Isolate* isolate, | |
619 const std::vector<Handle<Code>>& functions) { | |
620 for (Handle<Code> c : functions) RecordStats(isolate, *c); | |
621 } | |
622 | |
623 static void RecordStats(Isolate* isolate, Handle<FixedArray> functions) { | 575 static void RecordStats(Isolate* isolate, Handle<FixedArray> functions) { |
624 DisallowHeapAllocation no_gc; | 576 DisallowHeapAllocation no_gc; |
625 for (int i = 0; i < functions->length(); ++i) { | 577 for (int i = 0; i < functions->length(); ++i) { |
626 RecordStats(isolate, Code::cast(functions->get(i))); | 578 RecordStats(isolate, Code::cast(functions->get(i))); |
627 } | 579 } |
628 } | 580 } |
629 | 581 |
630 Address GetGlobalStartAddressFromCodeTemplate(Object* undefined, | 582 Address GetGlobalStartAddressFromCodeTemplate(Object* undefined, |
631 JSObject* owner) { | 583 JSObject* owner) { |
632 Address old_address = nullptr; | 584 Address old_address = nullptr; |
633 Object* stored_value = owner->GetInternalField(kWasmGlobalsArrayBuffer); | 585 Object* stored_value = owner->GetInternalField(kWasmGlobalsArrayBuffer); |
634 if (stored_value != undefined) { | 586 if (stored_value != undefined) { |
635 old_address = static_cast<Address>( | 587 old_address = static_cast<Address>( |
636 JSArrayBuffer::cast(stored_value)->backing_store()); | 588 JSArrayBuffer::cast(stored_value)->backing_store()); |
637 } | 589 } |
638 return old_address; | 590 return old_address; |
639 } | 591 } |
640 | 592 |
641 Handle<FixedArray> GetImportsMetadata(Factory* factory, | 593 Handle<FixedArray> GetImportsData(Factory* factory, const WasmModule* module) { |
642 const WasmModule* module) { | |
643 Handle<FixedArray> ret = factory->NewFixedArray( | 594 Handle<FixedArray> ret = factory->NewFixedArray( |
644 static_cast<int>(module->import_table.size()), TENURED); | 595 static_cast<int>(module->import_table.size()), TENURED); |
645 for (size_t i = 0; i < module->import_table.size(); ++i) { | 596 for (size_t i = 0; i < module->import_table.size(); ++i) { |
646 const WasmImport& import = module->import_table[i]; | 597 const WasmImport& import = module->import_table[i]; |
| 598 if (import.kind != kExternalFunction) continue; |
647 WasmName module_name = module->GetNameOrNull(import.module_name_offset, | 599 WasmName module_name = module->GetNameOrNull(import.module_name_offset, |
648 import.module_name_length); | 600 import.module_name_length); |
649 WasmName function_name = module->GetNameOrNull(import.function_name_offset, | 601 WasmName function_name = module->GetNameOrNull(import.field_name_offset, |
650 import.function_name_length); | 602 import.field_name_length); |
651 | 603 |
652 Handle<String> module_name_string = | 604 Handle<String> module_name_string = |
653 factory->InternalizeUtf8String(module_name); | 605 factory->InternalizeUtf8String(module_name); |
654 Handle<String> function_name_string = | 606 Handle<String> function_name_string = |
655 function_name.is_empty() | 607 function_name.is_empty() |
656 ? Handle<String>::null() | 608 ? Handle<String>::null() |
657 : factory->InternalizeUtf8String(function_name); | 609 : factory->InternalizeUtf8String(function_name); |
658 Handle<ByteArray> sig = | 610 FunctionSig* fsig = module->functions[import.index].sig; |
659 factory->NewByteArray(static_cast<int>(import.sig->parameter_count() + | 611 Handle<ByteArray> sig = factory->NewByteArray( |
660 import.sig->return_count()), | 612 static_cast<int>(fsig->parameter_count() + fsig->return_count()), |
661 TENURED); | 613 TENURED); |
662 sig->copy_in(0, reinterpret_cast<const byte*>(import.sig->raw_data()), | 614 sig->copy_in(0, reinterpret_cast<const byte*>(fsig->raw_data()), |
663 sig->length()); | 615 sig->length()); |
664 Handle<FixedArray> encoded_import = | 616 Handle<FixedArray> encoded_import = |
665 factory->NewFixedArray(kWasmImportDataTableSize, TENURED); | 617 factory->NewFixedArray(kWasmImportDataTableSize, TENURED); |
666 encoded_import->set(kModuleName, *module_name_string); | 618 encoded_import->set(kModuleName, *module_name_string); |
667 if (!function_name_string.is_null()) { | 619 if (!function_name_string.is_null()) { |
668 encoded_import->set(kFunctionName, *function_name_string); | 620 encoded_import->set(kFunctionName, *function_name_string); |
669 } | 621 } |
670 encoded_import->set( | 622 encoded_import->set(kOutputCount, |
671 kOutputCount, | 623 Smi::FromInt(static_cast<int>(fsig->return_count()))); |
672 Smi::FromInt(static_cast<int>(import.sig->return_count()))); | |
673 encoded_import->set(kSignature, *sig); | 624 encoded_import->set(kSignature, *sig); |
674 ret->set(static_cast<int>(i), *encoded_import); | 625 ret->set(static_cast<int>(i), *encoded_import); |
675 } | 626 } |
676 return ret; | 627 return ret; |
677 } | 628 } |
678 | 629 |
679 bool CompileWrappersToImportedFunctions(Isolate* isolate, | 630 Handle<Code> CompileImportWrapper(Isolate* isolate, |
680 const Handle<JSReceiver> ffi, | 631 const Handle<JSReceiver> ffi, int index, |
681 std::vector<Handle<Code>>& imports, | 632 Handle<FixedArray> import_data, |
682 Handle<FixedArray> import_data, | 633 ErrorThrower* thrower) { |
683 ErrorThrower* thrower) { | 634 Handle<FixedArray> data = |
684 uint32_t import_count = static_cast<uint32_t>(import_data->length()); | 635 import_data->GetValueChecked<FixedArray>(isolate, index); |
685 if (import_count > 0) { | 636 Handle<String> module_name = |
686 imports.reserve(import_count); | 637 data->GetValueChecked<String>(isolate, kModuleName); |
687 for (uint32_t index = 0; index < import_count; ++index) { | 638 MaybeHandle<String> function_name = |
688 Handle<FixedArray> data = | 639 data->GetValue<String>(isolate, kFunctionName); |
689 import_data->GetValueChecked<FixedArray>(isolate, index); | |
690 Handle<String> module_name = | |
691 data->GetValueChecked<String>(isolate, kModuleName); | |
692 MaybeHandle<String> function_name = | |
693 data->GetValue<String>(isolate, kFunctionName); | |
694 | 640 |
695 // TODO(mtrofin): this is an uint32_t, actually. We should rationalize | 641 // TODO(mtrofin): this is an uint32_t, actually. We should rationalize |
696 // it when we rationalize signed/unsigned stuff. | 642 // it when we rationalize signed/unsigned stuff. |
697 int ret_count = Smi::cast(data->get(kOutputCount))->value(); | 643 int ret_count = Smi::cast(data->get(kOutputCount))->value(); |
698 CHECK(ret_count >= 0); | 644 CHECK_GE(ret_count, 0); |
699 Handle<ByteArray> sig_data = | 645 Handle<ByteArray> sig_data = |
700 data->GetValueChecked<ByteArray>(isolate, kSignature); | 646 data->GetValueChecked<ByteArray>(isolate, kSignature); |
701 int sig_data_size = sig_data->length(); | 647 int sig_data_size = sig_data->length(); |
702 int param_count = sig_data_size - ret_count; | 648 int param_count = sig_data_size - ret_count; |
703 CHECK(param_count >= 0); | 649 CHECK(param_count >= 0); |
704 | 650 |
705 MaybeHandle<JSReceiver> function = LookupFunction( | 651 MaybeHandle<JSReceiver> function = LookupFunction( |
706 thrower, isolate->factory(), ffi, index, module_name, function_name); | 652 thrower, isolate->factory(), ffi, index, module_name, function_name); |
707 if (function.is_null()) return false; | 653 if (function.is_null()) return Handle<Code>::null(); |
708 Handle<Code> code; | 654 Handle<Code> code; |
709 Handle<JSReceiver> target = function.ToHandleChecked(); | 655 Handle<JSReceiver> target = function.ToHandleChecked(); |
710 bool isMatch = false; | 656 bool isMatch = false; |
711 Handle<Code> export_wrapper_code; | 657 Handle<Code> export_wrapper_code; |
712 if (target->IsJSFunction()) { | 658 if (target->IsJSFunction()) { |
713 Handle<JSFunction> func = Handle<JSFunction>::cast(target); | 659 Handle<JSFunction> func = Handle<JSFunction>::cast(target); |
714 export_wrapper_code = handle(func->code()); | 660 export_wrapper_code = handle(func->code()); |
715 if (export_wrapper_code->kind() == Code::JS_TO_WASM_FUNCTION) { | 661 if (export_wrapper_code->kind() == Code::JS_TO_WASM_FUNCTION) { |
716 int exported_param_count = | 662 int exported_param_count = |
717 Smi::cast(func->GetInternalField(kInternalArity))->value(); | 663 Smi::cast(func->GetInternalField(kInternalArity))->value(); |
718 Handle<ByteArray> exportedSig = Handle<ByteArray>( | 664 Handle<ByteArray> exportedSig = Handle<ByteArray>( |
719 ByteArray::cast(func->GetInternalField(kInternalSignature))); | 665 ByteArray::cast(func->GetInternalField(kInternalSignature))); |
720 if (exported_param_count == param_count && | 666 if (exported_param_count == param_count && |
721 exportedSig->length() == sig_data->length() && | 667 exportedSig->length() == sig_data->length() && |
722 memcmp(exportedSig->data(), sig_data->data(), | 668 memcmp(exportedSig->data(), sig_data->data(), |
723 exportedSig->length()) == 0) { | 669 exportedSig->length()) == 0) { |
724 isMatch = true; | 670 isMatch = true; |
725 } | |
726 } | |
727 } | 671 } |
728 if (isMatch) { | |
729 int wasm_count = 0; | |
730 int const mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET); | |
731 for (RelocIterator it(*export_wrapper_code, mask); !it.done(); | |
732 it.next()) { | |
733 RelocInfo* rinfo = it.rinfo(); | |
734 Address target_address = rinfo->target_address(); | |
735 Code* target = Code::GetCodeFromTargetAddress(target_address); | |
736 if (target->kind() == Code::WASM_FUNCTION) { | |
737 ++wasm_count; | |
738 code = handle(target); | |
739 } | |
740 } | |
741 DCHECK(wasm_count == 1); | |
742 } else { | |
743 // Copy the signature to avoid a raw pointer into a heap object when | |
744 // GC can happen. | |
745 Zone zone(isolate->allocator()); | |
746 MachineRepresentation* reps = | |
747 zone.NewArray<MachineRepresentation>(sig_data_size); | |
748 memcpy(reps, sig_data->data(), | |
749 sizeof(MachineRepresentation) * sig_data_size); | |
750 FunctionSig sig(ret_count, param_count, reps); | |
751 | |
752 code = compiler::CompileWasmToJSWrapper(isolate, target, &sig, index, | |
753 module_name, function_name); | |
754 } | |
755 imports.push_back(code); | |
756 } | 672 } |
757 } | 673 } |
758 return true; | 674 if (isMatch) { |
| 675 int wasm_count = 0; |
| 676 int const mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET); |
| 677 for (RelocIterator it(*export_wrapper_code, mask); !it.done(); it.next()) { |
| 678 RelocInfo* rinfo = it.rinfo(); |
| 679 Address target_address = rinfo->target_address(); |
| 680 Code* target = Code::GetCodeFromTargetAddress(target_address); |
| 681 if (target->kind() == Code::WASM_FUNCTION) { |
| 682 ++wasm_count; |
| 683 code = handle(target); |
| 684 } |
| 685 } |
| 686 DCHECK(wasm_count == 1); |
| 687 return code; |
| 688 } else { |
| 689 // Copy the signature to avoid a raw pointer into a heap object when |
| 690 // GC can happen. |
| 691 Zone zone(isolate->allocator()); |
| 692 MachineRepresentation* reps = |
| 693 zone.NewArray<MachineRepresentation>(sig_data_size); |
| 694 memcpy(reps, sig_data->data(), |
| 695 sizeof(MachineRepresentation) * sig_data_size); |
| 696 FunctionSig sig(ret_count, param_count, reps); |
| 697 |
| 698 return compiler::CompileWasmToJSWrapper(isolate, target, &sig, index, |
| 699 module_name, function_name); |
| 700 } |
759 } | 701 } |
760 | 702 |
761 void InitializeParallelCompilation( | 703 void InitializeParallelCompilation( |
762 Isolate* isolate, const std::vector<WasmFunction>& functions, | 704 Isolate* isolate, const std::vector<WasmFunction>& functions, |
763 std::vector<compiler::WasmCompilationUnit*>& compilation_units, | 705 std::vector<compiler::WasmCompilationUnit*>& compilation_units, |
764 ModuleEnv& module_env, ErrorThrower* thrower) { | 706 ModuleEnv& module_env, ErrorThrower* thrower) { |
765 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); ++i) { | 707 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); ++i) { |
766 compilation_units[i] = new compiler::WasmCompilationUnit( | 708 const WasmFunction* func = &functions[i]; |
767 thrower, isolate, &module_env, &functions[i], i); | 709 compilation_units[i] = |
| 710 func->imported ? nullptr : new compiler::WasmCompilationUnit( |
| 711 thrower, isolate, &module_env, func, i); |
768 } | 712 } |
769 } | 713 } |
770 | 714 |
771 uint32_t* StartCompilationTasks( | 715 uint32_t* StartCompilationTasks( |
772 Isolate* isolate, | 716 Isolate* isolate, |
773 std::vector<compiler::WasmCompilationUnit*>& compilation_units, | 717 std::vector<compiler::WasmCompilationUnit*>& compilation_units, |
774 std::queue<compiler::WasmCompilationUnit*>& executed_units, | 718 std::queue<compiler::WasmCompilationUnit*>& executed_units, |
775 base::Semaphore* pending_tasks, base::Mutex& result_mutex, | 719 base::Semaphore* pending_tasks, base::Mutex& result_mutex, |
776 base::AtomicNumber<size_t>& next_unit) { | 720 base::AtomicNumber<size_t>& next_unit) { |
777 const size_t num_tasks = | 721 const size_t num_tasks = |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
887 } | 831 } |
888 | 832 |
889 void CompileSequentially(Isolate* isolate, const WasmModule* module, | 833 void CompileSequentially(Isolate* isolate, const WasmModule* module, |
890 std::vector<Handle<Code>>& functions, | 834 std::vector<Handle<Code>>& functions, |
891 ErrorThrower* thrower, ModuleEnv* module_env) { | 835 ErrorThrower* thrower, ModuleEnv* module_env) { |
892 DCHECK(!thrower->error()); | 836 DCHECK(!thrower->error()); |
893 | 837 |
894 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; | 838 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; |
895 i < module->functions.size(); ++i) { | 839 i < module->functions.size(); ++i) { |
896 const WasmFunction& func = module->functions[i]; | 840 const WasmFunction& func = module->functions[i]; |
| 841 if (func.imported) continue; // Imports are compiled at instantiation time. |
897 | 842 |
898 DCHECK_EQ(i, func.func_index); | |
899 WasmName str = module->GetName(func.name_offset, func.name_length); | 843 WasmName str = module->GetName(func.name_offset, func.name_length); |
900 Handle<Code> code = Handle<Code>::null(); | 844 Handle<Code> code = Handle<Code>::null(); |
901 // Compile the function. | 845 // Compile the function. |
902 code = compiler::WasmCompilationUnit::CompileWasmFunction( | 846 code = compiler::WasmCompilationUnit::CompileWasmFunction( |
903 thrower, isolate, module_env, &func); | 847 thrower, isolate, module_env, &func); |
904 if (code.is_null()) { | 848 if (code.is_null()) { |
905 thrower->Error("Compilation of #%d:%.*s failed.", i, str.length(), | 849 thrower->Error("Compilation of #%d:%.*s failed.", i, str.length(), |
906 str.start()); | 850 str.start()); |
907 break; | 851 break; |
908 } | 852 } |
909 // Install the code into the linker table. | 853 // Install the code into the linker table. |
910 functions[i] = code; | 854 functions[i] = code; |
911 } | 855 } |
912 } | 856 } |
913 | 857 |
914 void SetDebugSupport(Factory* factory, Handle<FixedArray> compiled_module, | 858 void PatchDirectCalls(Handle<FixedArray> old_functions, |
915 Handle<JSObject> js_object) { | 859 Handle<FixedArray> new_functions, int start) { |
916 Isolate* isolate = compiled_module->GetIsolate(); | |
917 MaybeHandle<String> module_bytes_string = | |
918 compiled_module->GetValue<String>(isolate, kModuleBytes); | |
919 if (!module_bytes_string.is_null()) { | |
920 js_object->SetInternalField(kWasmModuleBytesString, | |
921 *module_bytes_string.ToHandleChecked()); | |
922 } | |
923 | |
924 MaybeHandle<ByteArray> function_name_table = | |
925 compiled_module->GetValue<ByteArray>(isolate, kFunctionNameTable); | |
926 if (!function_name_table.is_null()) { | |
927 js_object->SetInternalField(kWasmFunctionNamesArray, | |
928 *function_name_table.ToHandleChecked()); | |
929 } | |
930 } | |
931 | |
932 bool SetupGlobals(Isolate* isolate, MaybeHandle<JSObject> template_owner, | |
933 Handle<FixedArray> compiled_module, Handle<JSObject> instance, | |
934 ErrorThrower* thrower) { | |
935 uint32_t globals_size = static_cast<uint32_t>( | |
936 Smi::cast(compiled_module->get(kGlobalsSize))->value()); | |
937 if (globals_size > 0) { | |
938 Handle<JSArrayBuffer> globals_buffer = | |
939 NewArrayBuffer(isolate, globals_size); | |
940 if (globals_buffer.is_null()) { | |
941 thrower->Error("Out of memory: wasm globals"); | |
942 return false; | |
943 } | |
944 Address old_address = | |
945 template_owner.is_null() | |
946 ? nullptr | |
947 : GetGlobalStartAddressFromCodeTemplate( | |
948 *isolate->factory()->undefined_value(), | |
949 JSObject::cast(*template_owner.ToHandleChecked())); | |
950 RelocateGlobals(instance, old_address, | |
951 static_cast<Address>(globals_buffer->backing_store())); | |
952 instance->SetInternalField(kWasmGlobalsArrayBuffer, *globals_buffer); | |
953 } | |
954 return true; | |
955 } | |
956 | |
957 bool SetupInstanceHeap(Isolate* isolate, Handle<FixedArray> compiled_module, | |
958 Handle<JSObject> instance, Handle<JSArrayBuffer> memory, | |
959 ErrorThrower* thrower) { | |
960 uint32_t min_mem_pages = static_cast<uint32_t>( | |
961 Smi::cast(compiled_module->get(kMinRequiredMemory))->value()); | |
962 isolate->counters()->wasm_min_mem_pages_count()->AddSample(min_mem_pages); | |
963 // TODO(wasm): re-enable counter for max_mem_pages when we use that field. | |
964 | |
965 if (memory.is_null() && min_mem_pages > 0) { | |
966 memory = AllocateMemory(thrower, isolate, min_mem_pages); | |
967 if (memory.is_null()) { | |
968 return false; | |
969 } | |
970 } | |
971 | |
972 if (!memory.is_null()) { | |
973 instance->SetInternalField(kWasmMemArrayBuffer, *memory); | |
974 Address mem_start = static_cast<Address>(memory->backing_store()); | |
975 uint32_t mem_size = static_cast<uint32_t>(memory->byte_length()->Number()); | |
976 uint32_t old_mem_size = static_cast<uint32_t>( | |
977 compiled_module->GetValueChecked<HeapNumber>(isolate, kMemSize) | |
978 ->value()); | |
979 MaybeHandle<JSArrayBuffer> old_mem = | |
980 compiled_module->GetValue<JSArrayBuffer>(isolate, kMemStart); | |
981 Address old_mem_start = | |
982 old_mem.is_null() | |
983 ? nullptr | |
984 : static_cast<Address>(old_mem.ToHandleChecked()->backing_store()); | |
985 RelocateInstanceCode(instance, old_mem_start, mem_start, old_mem_size, | |
986 mem_size); | |
987 LoadDataSegments(compiled_module, mem_start, mem_size); | |
988 compiled_module->GetValueChecked<HeapNumber>(isolate, kMemSize) | |
989 ->set_value(static_cast<double>(mem_size)); | |
990 compiled_module->set(kMemStart, *memory); | |
991 } | |
992 return true; | |
993 } | |
994 | |
995 void FixupFunctionsAndImports(Handle<FixedArray> old_functions, | |
996 Handle<FixedArray> new_functions, | |
997 MaybeHandle<FixedArray> maybe_old_imports, | |
998 MaybeHandle<FixedArray> maybe_new_imports) { | |
999 DCHECK_EQ(new_functions->length(), old_functions->length()); | 860 DCHECK_EQ(new_functions->length(), old_functions->length()); |
1000 | 861 |
1001 DisallowHeapAllocation no_gc; | 862 DisallowHeapAllocation no_gc; |
1002 std::map<Code*, Code*> old_to_new_code; | 863 std::map<Code*, Code*> old_to_new_code; |
1003 for (int i = 0; i < new_functions->length(); ++i) { | 864 for (int i = 0; i < new_functions->length(); ++i) { |
1004 old_to_new_code.insert(std::make_pair(Code::cast(old_functions->get(i)), | 865 old_to_new_code.insert(std::make_pair(Code::cast(old_functions->get(i)), |
1005 Code::cast(new_functions->get(i)))); | 866 Code::cast(new_functions->get(i)))); |
1006 } | 867 } |
1007 DCHECK_EQ(maybe_old_imports.is_null(), maybe_new_imports.is_null()); | |
1008 if (!maybe_old_imports.is_null()) { | |
1009 Handle<FixedArray> old_imports = maybe_old_imports.ToHandleChecked(); | |
1010 Handle<FixedArray> new_imports = maybe_new_imports.ToHandleChecked(); | |
1011 DCHECK_EQ(new_imports->length(), old_imports->length()); | |
1012 for (int i = 0; i < new_imports->length(); ++i) { | |
1013 old_to_new_code.insert(std::make_pair(Code::cast(old_imports->get(i)), | |
1014 Code::cast(new_imports->get(i)))); | |
1015 } | |
1016 } | |
1017 int mode_mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET); | 868 int mode_mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET); |
1018 AllowDeferredHandleDereference embedding_raw_address; | 869 AllowDeferredHandleDereference embedding_raw_address; |
1019 for (int i = 0; i < new_functions->length(); ++i) { | 870 for (int i = start; i < new_functions->length(); ++i) { |
1020 Code* wasm_function = Code::cast(new_functions->get(i)); | 871 Code* wasm_function = Code::cast(new_functions->get(i)); |
1021 for (RelocIterator it(wasm_function, mode_mask); !it.done(); it.next()) { | 872 for (RelocIterator it(wasm_function, mode_mask); !it.done(); it.next()) { |
1022 Code* old_code = | 873 Code* old_code = |
1023 Code::GetCodeFromTargetAddress(it.rinfo()->target_address()); | 874 Code::GetCodeFromTargetAddress(it.rinfo()->target_address()); |
1024 if (old_code->kind() == Code::WASM_TO_JS_FUNCTION || | 875 if (old_code->kind() == Code::WASM_TO_JS_FUNCTION || |
1025 old_code->kind() == Code::WASM_FUNCTION) { | 876 old_code->kind() == Code::WASM_FUNCTION) { |
1026 auto found = old_to_new_code.find(old_code); | 877 auto found = old_to_new_code.find(old_code); |
1027 DCHECK(found != old_to_new_code.end()); | 878 DCHECK(found != old_to_new_code.end()); |
1028 Code* new_code = found->second; | 879 Code* new_code = found->second; |
1029 // Avoid redundant updates, expected for wasm functions, if we're at the | 880 if (new_code != old_code) { |
1030 // first instance. | 881 it.rinfo()->set_target_address(new_code->instruction_start(), |
1031 if (new_code == old_code) { | 882 UPDATE_WRITE_BARRIER, |
1032 DCHECK(new_code->kind() == Code::WASM_FUNCTION); | 883 SKIP_ICACHE_FLUSH); |
1033 continue; | |
1034 } | 884 } |
1035 it.rinfo()->set_target_address(new_code->instruction_start(), | |
1036 UPDATE_WRITE_BARRIER, SKIP_ICACHE_FLUSH); | |
1037 } | 885 } |
1038 } | 886 } |
1039 } | 887 } |
1040 } | 888 } |
1041 | 889 |
1042 bool SetupImports(Isolate* isolate, Handle<FixedArray> compiled_module, | |
1043 Handle<JSObject> instance, ErrorThrower* thrower, | |
1044 Handle<JSReceiver> ffi) { | |
1045 //------------------------------------------------------------------------- | |
1046 // Compile wrappers to imported functions. | |
1047 //------------------------------------------------------------------------- | |
1048 std::vector<Handle<Code>> import_code; | |
1049 MaybeHandle<FixedArray> maybe_import_data = | |
1050 compiled_module->GetValue<FixedArray>(isolate, kImportData); | |
1051 Handle<FixedArray> import_data; | |
1052 if (maybe_import_data.ToHandle(&import_data)) { | |
1053 if (!CompileWrappersToImportedFunctions(isolate, ffi, import_code, | |
1054 import_data, thrower)) { | |
1055 return false; | |
1056 } | |
1057 } | |
1058 | |
1059 RecordStats(isolate, import_code); | |
1060 if (import_code.empty()) return true; | |
1061 | |
1062 Handle<FixedArray> new_imports = | |
1063 isolate->factory()->NewFixedArray(static_cast<int>(import_code.size())); | |
1064 for (int i = 0; i < new_imports->length(); ++i) { | |
1065 new_imports->set(i, *import_code[i]); | |
1066 } | |
1067 compiled_module->set(kImportMap, *new_imports); | |
1068 return true; | |
1069 } | |
1070 | |
1071 bool SetupExportsObject(Handle<FixedArray> compiled_module, Isolate* isolate, | |
1072 Handle<JSObject> instance, ErrorThrower* thrower) { | |
1073 Factory* factory = isolate->factory(); | |
1074 bool mem_export = | |
1075 static_cast<bool>(Smi::cast(compiled_module->get(kExportMem))->value()); | |
1076 ModuleOrigin origin = static_cast<ModuleOrigin>( | |
1077 Smi::cast(compiled_module->get(kOrigin))->value()); | |
1078 | |
1079 MaybeHandle<FixedArray> maybe_exports = | |
1080 compiled_module->GetValue<FixedArray>(isolate, kExports); | |
1081 if (!maybe_exports.is_null() || mem_export) { | |
1082 PropertyDescriptor desc; | |
1083 desc.set_writable(false); | |
1084 | |
1085 Handle<JSObject> exports_object = instance; | |
1086 if (origin == kWasmOrigin) { | |
1087 // Create the "exports" object. | |
1088 Handle<JSFunction> object_function = Handle<JSFunction>( | |
1089 isolate->native_context()->object_function(), isolate); | |
1090 exports_object = factory->NewJSObject(object_function, TENURED); | |
1091 Handle<String> exports_name = factory->InternalizeUtf8String("exports"); | |
1092 JSObject::AddProperty(instance, exports_name, exports_object, READ_ONLY); | |
1093 } | |
1094 Handle<FixedArray> exports; | |
1095 if (maybe_exports.ToHandle(&exports)) { | |
1096 int exports_size = exports->length(); | |
1097 for (int i = 0; i < exports_size; ++i) { | |
1098 if (thrower->error()) return false; | |
1099 Handle<FixedArray> export_metadata = | |
1100 exports->GetValueChecked<FixedArray>(isolate, i); | |
1101 Handle<Code> export_code = | |
1102 export_metadata->GetValueChecked<Code>(isolate, kExportCode); | |
1103 RecordStats(isolate, *export_code); | |
1104 Handle<String> name = | |
1105 export_metadata->GetValueChecked<String>(isolate, kExportName); | |
1106 int arity = Smi::cast(export_metadata->get(kExportArity))->value(); | |
1107 MaybeHandle<ByteArray> signature = | |
1108 export_metadata->GetValue<ByteArray>(isolate, kExportedSignature); | |
1109 Handle<JSFunction> function = WrapExportCodeAsJSFunction( | |
1110 isolate, export_code, name, arity, signature, instance); | |
1111 desc.set_value(function); | |
1112 Maybe<bool> status = JSReceiver::DefineOwnProperty( | |
1113 isolate, exports_object, name, &desc, Object::THROW_ON_ERROR); | |
1114 if (!status.IsJust()) { | |
1115 thrower->Error("export of %.*s failed.", name->length(), | |
1116 name->ToCString().get()); | |
1117 return false; | |
1118 } | |
1119 } | |
1120 } | |
1121 if (mem_export) { | |
1122 // Export the memory as a named property. | |
1123 Handle<String> name = factory->InternalizeUtf8String("memory"); | |
1124 Handle<JSArrayBuffer> memory = Handle<JSArrayBuffer>( | |
1125 JSArrayBuffer::cast(instance->GetInternalField(kWasmMemArrayBuffer))); | |
1126 JSObject::AddProperty(exports_object, name, memory, READ_ONLY); | |
1127 } | |
1128 } | |
1129 return true; | |
1130 } | |
1131 | |
1132 #define GET_COMPILED_MODULE_WEAK_RELATION_OR_NULL(Field) \ | 890 #define GET_COMPILED_MODULE_WEAK_RELATION_OR_NULL(Field) \ |
1133 WeakCell* Get##Field(const FixedArray* compiled_module) { \ | 891 WeakCell* Get##Field(const FixedArray* compiled_module) { \ |
1134 Object* obj = compiled_module->get(k##Field); \ | 892 Object* obj = compiled_module->get(k##Field); \ |
1135 DCHECK_NOT_NULL(obj); \ | 893 DCHECK_NOT_NULL(obj); \ |
1136 if (obj->IsWeakCell()) { \ | 894 if (obj->IsWeakCell()) { \ |
1137 return WeakCell::cast(obj); \ | 895 return WeakCell::cast(obj); \ |
1138 } else { \ | 896 } else { \ |
1139 return nullptr; \ | 897 return nullptr; \ |
1140 } \ | 898 } \ |
1141 } | 899 } |
(...skipping 17 matching lines...) Expand all Loading... |
1159 | 917 |
1160 if (old_mem_size > 0) { | 918 if (old_mem_size > 0) { |
1161 CHECK_NE(mem_start, undefined); | 919 CHECK_NE(mem_start, undefined); |
1162 old_mem_address = | 920 old_mem_address = |
1163 static_cast<Address>(JSArrayBuffer::cast(mem_start)->backing_store()); | 921 static_cast<Address>(JSArrayBuffer::cast(mem_start)->backing_store()); |
1164 } | 922 } |
1165 int mode_mask = RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_REFERENCE) | | 923 int mode_mask = RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_REFERENCE) | |
1166 RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_SIZE_REFERENCE) | | 924 RelocInfo::ModeMask(RelocInfo::WASM_MEMORY_SIZE_REFERENCE) | |
1167 RelocInfo::ModeMask(RelocInfo::WASM_GLOBAL_REFERENCE); | 925 RelocInfo::ModeMask(RelocInfo::WASM_GLOBAL_REFERENCE); |
1168 | 926 |
1169 Object* fct_obj = compiled_module->get(kFunctions); | 927 Object* fct_obj = compiled_module->get(kCodeTable); |
1170 if (fct_obj != nullptr && fct_obj != undefined && | 928 if (fct_obj != nullptr && fct_obj != undefined && |
1171 (old_mem_size > 0 || globals_start != nullptr)) { | 929 (old_mem_size > 0 || globals_start != nullptr)) { |
1172 FixedArray* functions = FixedArray::cast(fct_obj); | 930 FixedArray* functions = FixedArray::cast(fct_obj); |
1173 for (int i = 0; i < functions->length(); ++i) { | 931 for (int i = 0; i < functions->length(); ++i) { |
1174 Code* code = Code::cast(functions->get(i)); | 932 Code* code = Code::cast(functions->get(i)); |
1175 bool changed = false; | 933 bool changed = false; |
1176 for (RelocIterator it(code, mode_mask); !it.done(); it.next()) { | 934 for (RelocIterator it(code, mode_mask); !it.done(); it.next()) { |
1177 RelocInfo::Mode mode = it.rinfo()->rmode(); | 935 RelocInfo::Mode mode = it.rinfo()->rmode(); |
1178 if (RelocInfo::IsWasmMemoryReference(mode) || | 936 if (RelocInfo::IsWasmMemoryReference(mode) || |
1179 RelocInfo::IsWasmMemorySizeReference(mode)) { | 937 RelocInfo::IsWasmMemorySizeReference(mode)) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1262 MaybeHandle<FixedArray> indirect_table = | 1020 MaybeHandle<FixedArray> indirect_table = |
1263 function_tables.size() | 1021 function_tables.size() |
1264 ? factory->NewFixedArray(static_cast<int>(function_tables.size()), | 1022 ? factory->NewFixedArray(static_cast<int>(function_tables.size()), |
1265 TENURED) | 1023 TENURED) |
1266 : MaybeHandle<FixedArray>(); | 1024 : MaybeHandle<FixedArray>(); |
1267 for (uint32_t i = 0; i < function_tables.size(); ++i) { | 1025 for (uint32_t i = 0; i < function_tables.size(); ++i) { |
1268 Handle<FixedArray> values = wasm::BuildFunctionTable(isolate, i, this); | 1026 Handle<FixedArray> values = wasm::BuildFunctionTable(isolate, i, this); |
1269 temp_instance_for_compilation.function_tables[i] = values; | 1027 temp_instance_for_compilation.function_tables[i] = values; |
1270 | 1028 |
1271 Handle<FixedArray> metadata = isolate->factory()->NewFixedArray( | 1029 Handle<FixedArray> metadata = isolate->factory()->NewFixedArray( |
1272 kWasmIndirectFunctionTableMetadataSize, TENURED); | 1030 kWasmIndirectFunctionTableDataSize, TENURED); |
1273 metadata->set(kSize, Smi::FromInt(function_tables[i].size)); | 1031 metadata->set(kSize, Smi::FromInt(function_tables[i].size)); |
1274 metadata->set(kTable, *values); | 1032 metadata->set(kTable, *values); |
1275 indirect_table.ToHandleChecked()->set(i, *metadata); | 1033 indirect_table.ToHandleChecked()->set(i, *metadata); |
1276 } | 1034 } |
1277 | 1035 |
1278 HistogramTimerScope wasm_compile_module_time_scope( | 1036 HistogramTimerScope wasm_compile_module_time_scope( |
1279 isolate->counters()->wasm_compile_module_time()); | 1037 isolate->counters()->wasm_compile_module_time()); |
1280 | 1038 |
1281 ModuleEnv module_env; | 1039 ModuleEnv module_env; |
1282 module_env.module = this; | 1040 module_env.module = this; |
1283 module_env.instance = &temp_instance_for_compilation; | 1041 module_env.instance = &temp_instance_for_compilation; |
1284 module_env.origin = origin; | 1042 module_env.origin = origin; |
1285 InitializePlaceholders(factory, &module_env.placeholders, functions.size()); | |
1286 | 1043 |
1287 Handle<FixedArray> compiled_functions = | 1044 // The {code_table} array contains import wrappers, functions, export |
1288 factory->NewFixedArray(static_cast<int>(functions.size()), TENURED); | 1045 // wrappers, and the start function wrapper. |
| 1046 int code_table_size = |
| 1047 static_cast<int>(functions.size() + num_exported_functions); |
| 1048 Handle<FixedArray> code_table = |
| 1049 factory->NewFixedArray(static_cast<int>(code_table_size), TENURED); |
1289 | 1050 |
1290 MaybeHandle<FixedArray> maybe_imports; | 1051 // Initialize the code table with placeholders. |
1291 if (import_table.size() > 0) { | 1052 for (uint32_t i = 0; i < functions.size(); i++) { |
1292 temp_instance_for_compilation.import_code.resize(import_table.size()); | 1053 Code::Kind kind = Code::WASM_FUNCTION; |
1293 Handle<FixedArray> imports = | 1054 if (i < num_imported_functions) kind = Code::WASM_TO_JS_FUNCTION; |
1294 factory->NewFixedArray(static_cast<int>(import_table.size())); | 1055 Handle<Code> placeholder = CreatePlaceholder(factory, i, kind); |
1295 for (uint32_t i = 0; i < import_table.size(); ++i) { | 1056 code_table->set(static_cast<int>(i), *placeholder); |
1296 Handle<Code> placeholder = | 1057 temp_instance_for_compilation.function_code[i] = placeholder; |
1297 CreatePlaceholder(factory, i, Code::WASM_TO_JS_FUNCTION); | |
1298 temp_instance_for_compilation.import_code[i] = placeholder; | |
1299 imports->set(i, *placeholder); | |
1300 } | |
1301 maybe_imports = imports; | |
1302 } | 1058 } |
| 1059 |
1303 isolate->counters()->wasm_functions_per_module()->AddSample( | 1060 isolate->counters()->wasm_functions_per_module()->AddSample( |
1304 static_cast<int>(functions.size())); | 1061 static_cast<int>(functions.size())); |
1305 if (FLAG_wasm_num_compilation_tasks != 0) { | 1062 if (!FLAG_trace_wasm_decoder && FLAG_wasm_num_compilation_tasks != 0) { |
1306 CompileInParallel(isolate, this, | 1063 CompileInParallel(isolate, this, |
1307 temp_instance_for_compilation.function_code, thrower, | 1064 temp_instance_for_compilation.function_code, thrower, |
1308 &module_env); | 1065 &module_env); |
1309 } else { | 1066 } else { |
1310 CompileSequentially(isolate, this, | 1067 CompileSequentially(isolate, this, |
1311 temp_instance_for_compilation.function_code, thrower, | 1068 temp_instance_for_compilation.function_code, thrower, |
1312 &module_env); | 1069 &module_env); |
1313 } | 1070 } |
1314 if (thrower->error()) return nothing; | 1071 if (thrower->error()) return nothing; |
1315 | 1072 |
1316 // At this point, compilation has completed. Update the code table. | 1073 // At this point, compilation has completed. Update the code table. |
1317 for (size_t i = FLAG_skip_compiling_wasm_funcs; | 1074 for (size_t i = FLAG_skip_compiling_wasm_funcs; |
1318 i < temp_instance_for_compilation.function_code.size(); ++i) { | 1075 i < temp_instance_for_compilation.function_code.size(); ++i) { |
1319 Code* code = *temp_instance_for_compilation.function_code[i]; | 1076 Code* code = *temp_instance_for_compilation.function_code[i]; |
1320 compiled_functions->set(static_cast<int>(i), code); | 1077 code_table->set(static_cast<int>(i), code); |
1321 } | 1078 } |
1322 | 1079 |
1323 LinkModuleFunctions(isolate, compiled_functions); | 1080 // Link the functions in the module. |
1324 | 1081 for (size_t i = FLAG_skip_compiling_wasm_funcs; |
1325 // TODO(mtrofin): do we need to flush the cache here? | 1082 i < temp_instance_for_compilation.function_code.size(); ++i) { |
1326 FlushAssemblyCache(isolate, compiled_functions); | 1083 Handle<Code> code = temp_instance_for_compilation.function_code[i]; |
| 1084 bool modified = |
| 1085 LinkFunction(code, temp_instance_for_compilation.function_code); |
| 1086 if (modified) { |
| 1087 // TODO(mtrofin): do we need to flush the cache here? |
| 1088 Assembler::FlushICache(isolate, code->instruction_start(), |
| 1089 code->instruction_size()); |
| 1090 } |
| 1091 } |
1327 | 1092 |
1328 // Create the compiled module object, and populate with compiled functions | 1093 // Create the compiled module object, and populate with compiled functions |
1329 // and information needed at instantiation time. This object needs to be | 1094 // and information needed at instantiation time. This object needs to be |
1330 // serializable. Instantiation may occur off a deserialized version of this | 1095 // serializable. Instantiation may occur off a deserialized version of this |
1331 // object. | 1096 // object. |
1332 Handle<FixedArray> ret = | 1097 Handle<FixedArray> ret = |
1333 factory->NewFixedArray(kCompiledWasmObjectTableSize, TENURED); | 1098 factory->NewFixedArray(kCompiledWasmObjectTableSize, TENURED); |
1334 ret->set(kFunctions, *compiled_functions); | 1099 ret->set(kCodeTable, *code_table); |
1335 if (!indirect_table.is_null()) { | 1100 if (!indirect_table.is_null()) { |
1336 ret->set(kTableOfIndirectFunctionTables, *indirect_table.ToHandleChecked()); | 1101 ret->set(kTableOfIndirectFunctionTables, *indirect_table.ToHandleChecked()); |
1337 } | 1102 } |
1338 if (!maybe_imports.is_null()) { | 1103 Handle<FixedArray> import_data = GetImportsData(factory, this); |
1339 ret->set(kImportMap, *maybe_imports.ToHandleChecked()); | |
1340 } | |
1341 Handle<FixedArray> import_data = GetImportsMetadata(factory, this); | |
1342 ret->set(kImportData, *import_data); | 1104 ret->set(kImportData, *import_data); |
1343 | 1105 |
1344 // Compile export functions. | 1106 // Compile exported function wrappers. |
1345 int export_size = static_cast<int>(export_table.size()); | 1107 int export_size = static_cast<int>(num_exported_functions); |
1346 Handle<Code> startup_fct; | |
1347 if (export_size > 0) { | 1108 if (export_size > 0) { |
1348 Handle<FixedArray> exports = factory->NewFixedArray(export_size, TENURED); | 1109 Handle<FixedArray> exports = factory->NewFixedArray(export_size, TENURED); |
1349 for (int i = 0; i < export_size; ++i) { | 1110 int index = -1; |
1350 Handle<FixedArray> export_metadata = | 1111 |
1351 factory->NewFixedArray(kWasmExportMetadataTableSize, TENURED); | 1112 for (const WasmExport& exp : export_table) { |
1352 const WasmExport& exp = export_table[i]; | 1113 if (exp.kind != kExternalFunction) |
1353 FunctionSig* funcSig = functions[exp.func_index].sig; | 1114 continue; // skip non-function exports. |
| 1115 index++; |
| 1116 Handle<FixedArray> export_data = |
| 1117 factory->NewFixedArray(kWasmExportDataTableSize, TENURED); |
| 1118 FunctionSig* funcSig = functions[exp.index].sig; |
1354 Handle<ByteArray> exportedSig = | 1119 Handle<ByteArray> exportedSig = |
1355 factory->NewByteArray(static_cast<int>(funcSig->parameter_count() + | 1120 factory->NewByteArray(static_cast<int>(funcSig->parameter_count() + |
1356 funcSig->return_count()), | 1121 funcSig->return_count()), |
1357 TENURED); | 1122 TENURED); |
1358 exportedSig->copy_in(0, | 1123 exportedSig->copy_in(0, |
1359 reinterpret_cast<const byte*>(funcSig->raw_data()), | 1124 reinterpret_cast<const byte*>(funcSig->raw_data()), |
1360 exportedSig->length()); | 1125 exportedSig->length()); |
1361 export_metadata->set(kExportedSignature, *exportedSig); | 1126 export_data->set(kExportedSignature, *exportedSig); |
1362 WasmName str = GetName(exp.name_offset, exp.name_length); | 1127 WasmName str = GetName(exp.name_offset, exp.name_length); |
1363 Handle<String> name = factory->InternalizeUtf8String(str); | 1128 Handle<String> name = factory->InternalizeUtf8String(str); |
1364 Handle<Code> code = | 1129 Handle<Code> code = code_table->GetValueChecked<Code>(isolate, exp.index); |
1365 temp_instance_for_compilation.function_code[exp.func_index]; | |
1366 Handle<Code> export_code = compiler::CompileJSToWasmWrapper( | 1130 Handle<Code> export_code = compiler::CompileJSToWasmWrapper( |
1367 isolate, &module_env, code, exp.func_index); | 1131 isolate, &module_env, code, exp.index); |
1368 if (thrower->error()) return nothing; | 1132 if (thrower->error()) return nothing; |
1369 export_metadata->set(kExportCode, *export_code); | 1133 export_data->set(kExportName, *name); |
1370 export_metadata->set(kExportName, *name); | 1134 export_data->set(kExportArity, |
1371 export_metadata->set( | 1135 Smi::FromInt(static_cast<int>( |
1372 kExportArity, Smi::FromInt(static_cast<int>( | 1136 functions[exp.index].sig->parameter_count()))); |
1373 functions[exp.func_index].sig->parameter_count()))); | 1137 export_data->set(kExportedFunctionIndex, |
1374 export_metadata->set(kExportedFunctionIndex, | 1138 Smi::FromInt(static_cast<int>(exp.index))); |
1375 Smi::FromInt(static_cast<int>(exp.func_index))); | 1139 exports->set(index, *export_data); |
1376 exports->set(i, *export_metadata); | 1140 code_table->set(static_cast<int>(functions.size() + index), *export_code); |
1377 if (exp.func_index == start_function_index) { | |
1378 startup_fct = export_code; | |
1379 } | |
1380 } | 1141 } |
1381 ret->set(kExports, *exports); | 1142 ret->set(kExportData, *exports); |
1382 } | 1143 } |
1383 | 1144 |
1384 // Compile startup function, if we haven't already. | 1145 // Record data for startup function. |
1385 if (start_function_index >= 0) { | 1146 if (start_function_index >= 0) { |
1386 uint32_t index = static_cast<uint32_t>(start_function_index); | |
1387 HandleScope scope(isolate); | 1147 HandleScope scope(isolate); |
1388 if (startup_fct.is_null()) { | 1148 Handle<FixedArray> startup_data = |
1389 Handle<Code> code = temp_instance_for_compilation.function_code[index]; | 1149 factory->NewFixedArray(kWasmExportDataTableSize, TENURED); |
1390 DCHECK_EQ(0, functions[index].sig->parameter_count()); | 1150 startup_data->set(kExportArity, Smi::FromInt(0)); |
1391 startup_fct = | 1151 startup_data->set(kExportedFunctionIndex, |
1392 compiler::CompileJSToWasmWrapper(isolate, &module_env, code, index); | 1152 Smi::FromInt(start_function_index)); |
1393 } | 1153 ret->set(kStartupData, *startup_data); |
1394 Handle<FixedArray> metadata = | |
1395 factory->NewFixedArray(kWasmExportMetadataTableSize, TENURED); | |
1396 metadata->set(kExportCode, *startup_fct); | |
1397 metadata->set(kExportArity, Smi::FromInt(0)); | |
1398 metadata->set(kExportedFunctionIndex, Smi::FromInt(start_function_index)); | |
1399 ret->set(kStartupFunction, *metadata); | |
1400 } | 1154 } |
1401 | 1155 |
1402 // TODO(wasm): saving the module bytes for debugging is wasteful. We should | 1156 // TODO(wasm): saving the module bytes for debugging is wasteful. We should |
1403 // consider downloading this on-demand. | 1157 // consider downloading this on-demand. |
1404 { | 1158 { |
1405 size_t module_bytes_len = module_end - module_start; | 1159 size_t module_bytes_len = module_end - module_start; |
1406 DCHECK_LE(module_bytes_len, static_cast<size_t>(kMaxInt)); | 1160 DCHECK_LE(module_bytes_len, static_cast<size_t>(kMaxInt)); |
1407 Vector<const uint8_t> module_bytes_vec(module_start, | 1161 Vector<const uint8_t> module_bytes_vec(module_start, |
1408 static_cast<int>(module_bytes_len)); | 1162 static_cast<int>(module_bytes_len)); |
1409 Handle<String> module_bytes_string = | 1163 Handle<String> module_bytes_string = |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1469 ->GetValueChecked<FixedArray>(isolate, kTable); | 1223 ->GetValueChecked<FixedArray>(isolate, kTable); |
1470 for (int fct_index = 0; fct_index < wasm_functions->length(); ++fct_index) { | 1224 for (int fct_index = 0; fct_index < wasm_functions->length(); ++fct_index) { |
1471 Handle<Code> wasm_function = | 1225 Handle<Code> wasm_function = |
1472 wasm_functions->GetValueChecked<Code>(isolate, fct_index); | 1226 wasm_functions->GetValueChecked<Code>(isolate, fct_index); |
1473 PatchFunctionTable(wasm_function, table_to_replace, cloned_table); | 1227 PatchFunctionTable(wasm_function, table_to_replace, cloned_table); |
1474 } | 1228 } |
1475 } | 1229 } |
1476 return cloned_indirect_tables; | 1230 return cloned_indirect_tables; |
1477 } | 1231 } |
1478 | 1232 |
1479 Handle<FixedArray> CloneModuleForInstance(Isolate* isolate, | 1233 // Instantiates a WASM module, creating a WebAssembly.Instance from a |
1480 Handle<JSObject> module_object, | 1234 // WebAssembly.Module. |
1481 bool* template_is_owned, | |
1482 Handle<FixedArray>* module_template) { | |
1483 Factory* factory = isolate->factory(); | |
1484 | |
1485 Handle<FixedArray> original; | |
1486 for (int i = 0; i < 2; ++i) { | |
1487 original = handle(FixedArray::cast(module_object->GetInternalField(0))); | |
1488 if (GetOwningInstance(*original) == nullptr) { | |
1489 *template_is_owned = false; | |
1490 *module_template = original; | |
1491 return original; | |
1492 } | |
1493 if (i < 1) { | |
1494 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, | |
1495 GarbageCollectionReason::kRuntime); | |
1496 } | |
1497 } | |
1498 *template_is_owned = true; | |
1499 *module_template = original; | |
1500 | |
1501 // We insert the latest clone in front. | |
1502 Handle<FixedArray> clone = factory->CopyFixedArray(original); | |
1503 Handle<WeakCell> weak_link_to_wasm_obj = | |
1504 original->GetValueChecked<WeakCell>(isolate, kModuleObject); | |
1505 | |
1506 clone->set(kModuleObject, *weak_link_to_wasm_obj); | |
1507 Handle<WeakCell> link_to_original = factory->NewWeakCell(original); | |
1508 clone->set(kNextInstance, *link_to_original); | |
1509 Handle<WeakCell> link_to_clone = factory->NewWeakCell(clone); | |
1510 original->set(kPrevInstance, *link_to_clone); | |
1511 JSObject::cast(weak_link_to_wasm_obj->value())->SetInternalField(0, *clone); | |
1512 | |
1513 // Clone each wasm code object. | |
1514 Handle<FixedArray> orig_wasm_functions = | |
1515 original->GetValueChecked<FixedArray>(isolate, kFunctions); | |
1516 Handle<FixedArray> clone_wasm_functions = | |
1517 factory->CopyFixedArray(orig_wasm_functions); | |
1518 clone->set(kFunctions, *clone_wasm_functions); | |
1519 for (int i = 0; i < clone_wasm_functions->length(); ++i) { | |
1520 Handle<Code> orig_code = | |
1521 clone_wasm_functions->GetValueChecked<Code>(isolate, i); | |
1522 Handle<Code> cloned_code = factory->CopyCode(orig_code); | |
1523 clone_wasm_functions->set(i, *cloned_code); | |
1524 } | |
1525 | |
1526 MaybeHandle<FixedArray> maybe_orig_exports = | |
1527 original->GetValue<FixedArray>(isolate, kExports); | |
1528 Handle<FixedArray> orig_exports; | |
1529 if (maybe_orig_exports.ToHandle(&orig_exports)) { | |
1530 Handle<FixedArray> cloned_exports = factory->CopyFixedArray(orig_exports); | |
1531 clone->set(kExports, *cloned_exports); | |
1532 for (int i = 0; i < orig_exports->length(); ++i) { | |
1533 Handle<FixedArray> export_metadata = | |
1534 orig_exports->GetValueChecked<FixedArray>(isolate, i); | |
1535 Handle<FixedArray> clone_metadata = | |
1536 factory->CopyFixedArray(export_metadata); | |
1537 cloned_exports->set(i, *clone_metadata); | |
1538 Handle<Code> orig_code = | |
1539 export_metadata->GetValueChecked<Code>(isolate, kExportCode); | |
1540 Handle<Code> cloned_code = factory->CopyCode(orig_code); | |
1541 clone_metadata->set(kExportCode, *cloned_code); | |
1542 // TODO(wasm): This is actually a uint32_t, but since FixedArray indexes | |
1543 // in int, we are taking the risk of invalid values. | |
1544 int exported_fct_index = | |
1545 Smi::cast(export_metadata->get(kExportedFunctionIndex))->value(); | |
1546 CHECK_GE(exported_fct_index, 0); | |
1547 CHECK_LT(exported_fct_index, clone_wasm_functions->length()); | |
1548 Handle<Code> new_target = clone_wasm_functions->GetValueChecked<Code>( | |
1549 isolate, exported_fct_index); | |
1550 PatchJSWrapper(isolate, cloned_code, new_target); | |
1551 } | |
1552 } | |
1553 | |
1554 MaybeHandle<FixedArray> maybe_startup = | |
1555 original->GetValue<FixedArray>(isolate, kStartupFunction); | |
1556 if (!maybe_startup.is_null()) { | |
1557 Handle<FixedArray> startup_metadata = | |
1558 factory->CopyFixedArray(maybe_startup.ToHandleChecked()); | |
1559 Handle<Code> startup_fct_clone = factory->CopyCode( | |
1560 startup_metadata->GetValueChecked<Code>(isolate, kExportCode)); | |
1561 startup_metadata->set(kExportCode, *startup_fct_clone); | |
1562 clone->set(kStartupFunction, *startup_metadata); | |
1563 // TODO(wasm): see todo above about int vs size_t indexing in FixedArray. | |
1564 int startup_fct_index = | |
1565 Smi::cast(startup_metadata->get(kExportedFunctionIndex))->value(); | |
1566 CHECK_GE(startup_fct_index, 0); | |
1567 CHECK_LT(startup_fct_index, clone_wasm_functions->length()); | |
1568 Handle<Code> new_target = | |
1569 clone_wasm_functions->GetValueChecked<Code>(isolate, startup_fct_index); | |
1570 PatchJSWrapper(isolate, startup_fct_clone, new_target); | |
1571 } | |
1572 clone->set(kImportMap, *isolate->factory()->undefined_value()); | |
1573 return clone; | |
1574 } | |
1575 | |
1576 // Instantiates a wasm module as a JSObject. | |
1577 // * allocates a backing store of {mem_size} bytes. | |
1578 // * installs a named property "memory" for that buffer if exported | |
1579 // * installs named properties on the object for exported functions | |
1580 // * compiles wasm code to machine code | |
1581 MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, | 1235 MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, |
1582 Handle<JSObject> module_object, | 1236 Handle<JSObject> module_object, |
1583 Handle<JSReceiver> ffi, | 1237 Handle<JSReceiver> ffi, |
1584 Handle<JSArrayBuffer> memory) { | 1238 Handle<JSArrayBuffer> memory) { |
| 1239 MaybeHandle<JSObject> nothing; |
1585 HistogramTimerScope wasm_instantiate_module_time_scope( | 1240 HistogramTimerScope wasm_instantiate_module_time_scope( |
1586 isolate->counters()->wasm_instantiate_module_time()); | 1241 isolate->counters()->wasm_instantiate_module_time()); |
1587 ErrorThrower thrower(isolate, "WasmModule::Instantiate()"); | 1242 ErrorThrower thrower(isolate, "WasmModule::Instantiate()"); |
1588 Factory* factory = isolate->factory(); | 1243 Factory* factory = isolate->factory(); |
1589 | 1244 |
1590 bool template_is_owned = false; | 1245 //-------------------------------------------------------------------------- |
1591 Handle<FixedArray> compiled_module_template; | 1246 // Reuse the compiled module (if no owner), otherwise clone. |
1592 Handle<FixedArray> compiled_module = CloneModuleForInstance( | 1247 //-------------------------------------------------------------------------- |
1593 isolate, module_object, &template_is_owned, &compiled_module_template); | 1248 Handle<FixedArray> compiled_module; |
1594 | 1249 Handle<FixedArray> code_table; |
1595 MaybeHandle<JSObject> template_owner; | 1250 Handle<FixedArray> old_code_table; |
1596 if (template_is_owned) { | 1251 Handle<JSObject> owner; |
1597 Handle<WeakCell> weak_owner = | 1252 { |
1598 compiled_module_template->GetValueChecked<WeakCell>(isolate, | 1253 Handle<FixedArray> original( |
1599 kOwningInstance); | 1254 FixedArray::cast(module_object->GetInternalField(0)), isolate); |
1600 template_owner = handle(JSObject::cast(weak_owner->value())); | 1255 // Always make a new copy of the code_table, since the old_code_table |
1601 } | 1256 // may still have placeholders for imports. |
1602 // These fields are compulsory. | 1257 old_code_table = original->GetValueChecked<FixedArray>(isolate, kCodeTable); |
1603 Handle<FixedArray> code_table = | 1258 code_table = factory->CopyFixedArray(old_code_table); |
1604 compiled_module->GetValueChecked<FixedArray>(isolate, kFunctions); | 1259 |
1605 | 1260 WeakCell* tmp = GetOwningInstance(*original); |
1606 RecordStats(isolate, code_table); | 1261 if (tmp != nullptr) { |
1607 | 1262 // There is already an owner, clone everything. |
1608 MaybeHandle<JSObject> nothing; | 1263 owner = Handle<JSObject>(JSObject::cast(tmp->value()), isolate); |
1609 | 1264 // Insert the latest clone in front. |
| 1265 compiled_module = factory->CopyFixedArray(original); |
| 1266 Handle<WeakCell> weak_link_to_wasm_obj = |
| 1267 original->GetValueChecked<WeakCell>(isolate, kModuleObject); |
| 1268 |
| 1269 compiled_module->set(kModuleObject, *weak_link_to_wasm_obj); |
| 1270 Handle<WeakCell> link_to_original = factory->NewWeakCell(original); |
| 1271 compiled_module->set(kNextInstance, *link_to_original); |
| 1272 Handle<WeakCell> link_to_clone = factory->NewWeakCell(compiled_module); |
| 1273 original->set(kPrevInstance, *link_to_clone); |
| 1274 JSObject::cast(weak_link_to_wasm_obj->value()) |
| 1275 ->SetInternalField(0, *compiled_module); |
| 1276 |
| 1277 // Clone the code for WASM functions and exports. |
| 1278 for (int i = 0; i < code_table->length(); ++i) { |
| 1279 Handle<Code> orig_code = code_table->GetValueChecked<Code>(isolate, i); |
| 1280 switch (orig_code->kind()) { |
| 1281 case Code::WASM_TO_JS_FUNCTION: |
| 1282 // Imports will be overwritten with newly compiled wrappers. |
| 1283 break; |
| 1284 case Code::JS_TO_WASM_FUNCTION: |
| 1285 case Code::WASM_FUNCTION: |
| 1286 code_table->set(i, *factory->CopyCode(orig_code)); |
| 1287 break; |
| 1288 default: |
| 1289 UNREACHABLE(); |
| 1290 } |
| 1291 } |
| 1292 RecordStats(isolate, code_table); |
| 1293 } else { |
| 1294 // There was no owner, so we can reuse the original. |
| 1295 compiled_module = original; |
| 1296 } |
| 1297 compiled_module->set(kCodeTable, *code_table); |
| 1298 } |
| 1299 |
| 1300 //-------------------------------------------------------------------------- |
| 1301 // Allocate the instance object. |
| 1302 //-------------------------------------------------------------------------- |
1610 Handle<Map> map = factory->NewMap( | 1303 Handle<Map> map = factory->NewMap( |
1611 JS_OBJECT_TYPE, | 1304 JS_OBJECT_TYPE, |
1612 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); | 1305 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); |
1613 Handle<JSObject> js_object = factory->NewJSObjectFromMap(map, TENURED); | 1306 Handle<JSObject> instance = factory->NewJSObjectFromMap(map, TENURED); |
1614 js_object->SetInternalField(kWasmModuleCodeTable, *code_table); | 1307 instance->SetInternalField(kWasmModuleCodeTable, *code_table); |
1615 | 1308 |
1616 // Remember the old imports, for the case when we are at the first instance - | 1309 //-------------------------------------------------------------------------- |
1617 // they will be replaced with the instance's actual imports in SetupImports. | 1310 // Set up the memory for the new module. |
1618 MaybeHandle<FixedArray> old_imports = | 1311 //-------------------------------------------------------------------------- |
1619 compiled_module_template->GetValue<FixedArray>(isolate, kImportMap); | 1312 MaybeHandle<JSArrayBuffer> old_memory; |
1620 if (!(SetupInstanceHeap(isolate, compiled_module, js_object, memory, | 1313 // TODO(titzer): handle imported memory properly. |
1621 &thrower) && | 1314 |
1622 SetupGlobals(isolate, template_owner, compiled_module, js_object, | 1315 uint32_t min_mem_pages = static_cast<uint32_t>( |
1623 &thrower) && | 1316 Smi::cast(compiled_module->get(kMinRequiredMemory))->value()); |
1624 SetupImports(isolate, compiled_module, js_object, &thrower, ffi) && | 1317 isolate->counters()->wasm_min_mem_pages_count()->AddSample(min_mem_pages); |
1625 SetupExportsObject(compiled_module, isolate, js_object, &thrower))) { | 1318 // TODO(wasm): re-enable counter for max_mem_pages when we use that field. |
1626 return nothing; | 1319 |
1627 } | 1320 if (memory.is_null() && min_mem_pages > 0) { |
1628 | 1321 memory = AllocateMemory(&thrower, isolate, min_mem_pages); |
1629 FixupFunctionsAndImports( | 1322 if (memory.is_null()) return nothing; // failed to allocate memory |
1630 compiled_module_template->GetValueChecked<FixedArray>(isolate, | 1323 } |
1631 kFunctions), | 1324 |
1632 code_table, old_imports, | 1325 if (!memory.is_null()) { |
1633 compiled_module->GetValue<FixedArray>(isolate, kImportMap)); | 1326 instance->SetInternalField(kWasmMemArrayBuffer, *memory); |
1634 | 1327 Address mem_start = static_cast<Address>(memory->backing_store()); |
1635 SetDebugSupport(factory, compiled_module, js_object); | 1328 uint32_t mem_size = static_cast<uint32_t>(memory->byte_length()->Number()); |
1636 SetRuntimeSupport(isolate, js_object); | 1329 LoadDataSegments(compiled_module, mem_start, mem_size); |
1637 | 1330 |
1638 FlushAssemblyCache(isolate, code_table); | 1331 uint32_t old_mem_size = static_cast<uint32_t>( |
1639 | 1332 compiled_module->GetValueChecked<HeapNumber>(isolate, kMemSize) |
| 1333 ->value()); |
| 1334 MaybeHandle<JSArrayBuffer> old_mem = |
| 1335 compiled_module->GetValue<JSArrayBuffer>(isolate, kMemStart); |
| 1336 Address old_mem_start = |
| 1337 old_mem.is_null() |
| 1338 ? nullptr |
| 1339 : static_cast<Address>(old_mem.ToHandleChecked()->backing_store()); |
| 1340 RelocateInstanceCode(instance, old_mem_start, mem_start, old_mem_size, |
| 1341 mem_size); |
| 1342 compiled_module->GetValueChecked<HeapNumber>(isolate, kMemSize) |
| 1343 ->set_value(static_cast<double>(mem_size)); |
| 1344 compiled_module->set(kMemStart, *memory); |
| 1345 } |
| 1346 |
| 1347 //-------------------------------------------------------------------------- |
| 1348 // Set up the globals for the new module. |
| 1349 //-------------------------------------------------------------------------- |
| 1350 MaybeHandle<JSArrayBuffer> old_globals; |
| 1351 MaybeHandle<JSArrayBuffer> globals; |
| 1352 uint32_t globals_size = static_cast<uint32_t>( |
| 1353 Smi::cast(compiled_module->get(kGlobalsSize))->value()); |
| 1354 if (globals_size > 0) { |
| 1355 Handle<JSArrayBuffer> global_buffer = NewArrayBuffer(isolate, globals_size); |
| 1356 globals = global_buffer; |
| 1357 if (globals.is_null()) { |
| 1358 thrower.Error("Out of memory: wasm globals"); |
| 1359 return nothing; |
| 1360 } |
| 1361 Address old_address = |
| 1362 owner.is_null() ? nullptr : GetGlobalStartAddressFromCodeTemplate( |
| 1363 *isolate->factory()->undefined_value(), |
| 1364 JSObject::cast(*owner)); |
| 1365 RelocateGlobals(instance, old_address, |
| 1366 static_cast<Address>(global_buffer->backing_store())); |
| 1367 instance->SetInternalField(kWasmGlobalsArrayBuffer, *global_buffer); |
| 1368 } |
| 1369 |
| 1370 //-------------------------------------------------------------------------- |
| 1371 // Compile the import wrappers for the new module. |
| 1372 //-------------------------------------------------------------------------- |
| 1373 // TODO(titzer): handle imported globals and function tables. |
| 1374 Handle<FixedArray> import_data; |
| 1375 int num_imported_functions = 0; |
| 1376 if (compiled_module->GetValue<FixedArray>(isolate, kImportData) |
| 1377 .ToHandle(&import_data)) { |
| 1378 num_imported_functions = import_data->length(); |
| 1379 for (int index = 0; index < num_imported_functions; index++) { |
| 1380 Handle<Code> import_wrapper = |
| 1381 CompileImportWrapper(isolate, ffi, index, import_data, &thrower); |
| 1382 if (thrower.error()) return nothing; |
| 1383 code_table->set(index, *import_wrapper); |
| 1384 RecordStats(isolate, *import_wrapper); |
| 1385 } |
| 1386 } |
| 1387 |
| 1388 //-------------------------------------------------------------------------- |
| 1389 // Set up the debug support for the new module. |
| 1390 //-------------------------------------------------------------------------- |
| 1391 MaybeHandle<String> module_bytes_string = |
| 1392 compiled_module->GetValue<String>(isolate, kModuleBytes); |
| 1393 if (!module_bytes_string.is_null()) { |
| 1394 instance->SetInternalField(kWasmModuleBytesString, |
| 1395 *module_bytes_string.ToHandleChecked()); |
| 1396 } |
| 1397 |
| 1398 MaybeHandle<ByteArray> function_name_table = |
| 1399 compiled_module->GetValue<ByteArray>(isolate, kFunctionNameTable); |
| 1400 if (!function_name_table.is_null()) { |
| 1401 instance->SetInternalField(kWasmFunctionNamesArray, |
| 1402 *function_name_table.ToHandleChecked()); |
| 1403 } |
| 1404 |
| 1405 instance->SetInternalField(kWasmNumImportedFunctions, |
| 1406 *factory->NewNumber(num_imported_functions)); |
| 1407 |
| 1408 //-------------------------------------------------------------------------- |
| 1409 // Set up the runtime support for the new module. |
| 1410 //-------------------------------------------------------------------------- |
| 1411 Handle<WeakCell> weak_link = isolate->factory()->NewWeakCell(instance); |
| 1412 |
| 1413 for (int i = num_imported_functions + FLAG_skip_compiling_wasm_funcs; |
| 1414 i < code_table->length(); ++i) { |
| 1415 Handle<Code> code = code_table->GetValueChecked<Code>(isolate, i); |
| 1416 if (code->kind() == Code::WASM_FUNCTION) { |
| 1417 Handle<FixedArray> deopt_data = |
| 1418 isolate->factory()->NewFixedArray(2, TENURED); |
| 1419 deopt_data->set(0, *weak_link); |
| 1420 deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); |
| 1421 deopt_data->set_length(2); |
| 1422 code->set_deoptimization_data(*deopt_data); |
| 1423 } |
| 1424 } |
| 1425 |
| 1426 //-------------------------------------------------------------------------- |
| 1427 // Set up the indirect function tables for the new module. |
| 1428 //-------------------------------------------------------------------------- |
1640 { | 1429 { |
1641 std::vector<Handle<Code>> functions( | 1430 std::vector<Handle<Code>> functions( |
1642 static_cast<size_t>(code_table->length())); | 1431 static_cast<size_t>(code_table->length())); |
1643 for (int i = 0; i < code_table->length(); ++i) { | 1432 for (int i = 0; i < code_table->length(); ++i) { |
1644 functions[static_cast<size_t>(i)] = | 1433 functions[i] = code_table->GetValueChecked<Code>(isolate, i); |
1645 code_table->GetValueChecked<Code>(isolate, i); | |
1646 } | 1434 } |
1647 | 1435 |
1648 MaybeHandle<FixedArray> maybe_indirect_tables = | 1436 MaybeHandle<FixedArray> maybe_indirect_tables = |
1649 compiled_module->GetValue<FixedArray>(isolate, | 1437 compiled_module->GetValue<FixedArray>(isolate, |
1650 kTableOfIndirectFunctionTables); | 1438 kTableOfIndirectFunctionTables); |
1651 Handle<FixedArray> indirect_tables_template; | 1439 Handle<FixedArray> indirect_tables_template; |
1652 if (maybe_indirect_tables.ToHandle(&indirect_tables_template)) { | 1440 if (maybe_indirect_tables.ToHandle(&indirect_tables_template)) { |
1653 Handle<FixedArray> to_replace = | 1441 Handle<FixedArray> to_replace = |
1654 template_owner.is_null() | 1442 owner.is_null() ? indirect_tables_template |
1655 ? indirect_tables_template | 1443 : handle(FixedArray::cast(owner->GetInternalField( |
1656 : handle(FixedArray::cast( | 1444 kWasmModuleFunctionTable))); |
1657 template_owner.ToHandleChecked()->GetInternalField( | |
1658 kWasmModuleFunctionTable))); | |
1659 Handle<FixedArray> indirect_tables = SetupIndirectFunctionTable( | 1445 Handle<FixedArray> indirect_tables = SetupIndirectFunctionTable( |
1660 isolate, code_table, indirect_tables_template, to_replace); | 1446 isolate, code_table, indirect_tables_template, to_replace); |
1661 for (int i = 0; i < indirect_tables->length(); ++i) { | 1447 for (int i = 0; i < indirect_tables->length(); ++i) { |
1662 Handle<FixedArray> metadata = | 1448 Handle<FixedArray> metadata = |
1663 indirect_tables->GetValueChecked<FixedArray>(isolate, i); | 1449 indirect_tables->GetValueChecked<FixedArray>(isolate, i); |
1664 uint32_t size = Smi::cast(metadata->get(kSize))->value(); | 1450 uint32_t size = Smi::cast(metadata->get(kSize))->value(); |
1665 Handle<FixedArray> table = | 1451 Handle<FixedArray> table = |
1666 metadata->GetValueChecked<FixedArray>(isolate, kTable); | 1452 metadata->GetValueChecked<FixedArray>(isolate, kTable); |
1667 wasm::PopulateFunctionTable(table, size, &functions); | 1453 wasm::PopulateFunctionTable(table, size, &functions); |
1668 } | 1454 } |
1669 js_object->SetInternalField(kWasmModuleFunctionTable, *indirect_tables); | 1455 instance->SetInternalField(kWasmModuleFunctionTable, *indirect_tables); |
1670 } | 1456 } |
1671 } | 1457 } |
1672 | 1458 |
| 1459 //-------------------------------------------------------------------------- |
| 1460 // Set up the exports for the object. |
| 1461 //-------------------------------------------------------------------------- |
| 1462 bool mem_export = |
| 1463 static_cast<bool>(Smi::cast(compiled_module->get(kExportMem))->value()); |
| 1464 ModuleOrigin origin = static_cast<ModuleOrigin>( |
| 1465 Smi::cast(compiled_module->get(kOrigin))->value()); |
| 1466 |
| 1467 MaybeHandle<FixedArray> maybe_exports = |
| 1468 compiled_module->GetValue<FixedArray>(isolate, kExportData); |
| 1469 if (!maybe_exports.is_null() || mem_export) { |
| 1470 PropertyDescriptor desc; |
| 1471 desc.set_writable(false); |
| 1472 |
| 1473 Handle<JSObject> exports_object = instance; |
| 1474 if (origin == kWasmOrigin) { |
| 1475 // Create the "exports" object. |
| 1476 Handle<JSFunction> object_function = Handle<JSFunction>( |
| 1477 isolate->native_context()->object_function(), isolate); |
| 1478 exports_object = factory->NewJSObject(object_function, TENURED); |
| 1479 Handle<String> exports_name = factory->InternalizeUtf8String("exports"); |
| 1480 JSObject::AddProperty(instance, exports_name, exports_object, READ_ONLY); |
| 1481 } |
| 1482 Handle<FixedArray> exports; |
| 1483 int first_export = -1; |
| 1484 // TODO(wasm): another iteration over the code objects. |
| 1485 for (int i = 0; i < code_table->length(); i++) { |
| 1486 Handle<Code> code = code_table->GetValueChecked<Code>(isolate, i); |
| 1487 if (code->kind() == Code::JS_TO_WASM_FUNCTION) { |
| 1488 first_export = i; |
| 1489 break; |
| 1490 } |
| 1491 } |
| 1492 if (maybe_exports.ToHandle(&exports)) { |
| 1493 int export_size = exports->length(); |
| 1494 for (int i = 0; i < export_size; ++i) { |
| 1495 Handle<FixedArray> export_data = |
| 1496 exports->GetValueChecked<FixedArray>(isolate, i); |
| 1497 Handle<String> name = |
| 1498 export_data->GetValueChecked<String>(isolate, kExportName); |
| 1499 int arity = Smi::cast(export_data->get(kExportArity))->value(); |
| 1500 MaybeHandle<ByteArray> signature = |
| 1501 export_data->GetValue<ByteArray>(isolate, kExportedSignature); |
| 1502 Handle<Code> export_code = |
| 1503 code_table->GetValueChecked<Code>(isolate, first_export + i); |
| 1504 Handle<JSFunction> function = WrapExportCodeAsJSFunction( |
| 1505 isolate, export_code, name, arity, signature, instance); |
| 1506 desc.set_value(function); |
| 1507 Maybe<bool> status = JSReceiver::DefineOwnProperty( |
| 1508 isolate, exports_object, name, &desc, Object::THROW_ON_ERROR); |
| 1509 if (!status.IsJust()) { |
| 1510 thrower.Error("export of %.*s failed.", name->length(), |
| 1511 name->ToCString().get()); |
| 1512 return nothing; |
| 1513 } |
| 1514 } |
| 1515 } |
| 1516 if (mem_export) { |
| 1517 // Export the memory as a named property. |
| 1518 Handle<String> name = factory->InternalizeUtf8String("memory"); |
| 1519 Handle<JSArrayBuffer> memory = Handle<JSArrayBuffer>( |
| 1520 JSArrayBuffer::cast(instance->GetInternalField(kWasmMemArrayBuffer))); |
| 1521 JSObject::AddProperty(exports_object, name, memory, READ_ONLY); |
| 1522 } |
| 1523 } |
| 1524 |
| 1525 if (num_imported_functions > 0 || !owner.is_null()) { |
| 1526 // If the code was cloned, or new imports were compiled, patch. |
| 1527 PatchDirectCalls(old_code_table, code_table, num_imported_functions); |
| 1528 } |
| 1529 |
| 1530 FlushICache(isolate, code_table); |
| 1531 |
| 1532 //-------------------------------------------------------------------------- |
1673 // Run the start function if one was specified. | 1533 // Run the start function if one was specified. |
1674 MaybeHandle<FixedArray> maybe_startup_fct = | 1534 //-------------------------------------------------------------------------- |
1675 compiled_module->GetValue<FixedArray>(isolate, kStartupFunction); | 1535 Handle<FixedArray> startup_data; |
1676 Handle<FixedArray> metadata; | 1536 if (compiled_module->GetValue<FixedArray>(isolate, kStartupData) |
1677 if (maybe_startup_fct.ToHandle(&metadata)) { | 1537 .ToHandle(&startup_data)) { |
1678 HandleScope scope(isolate); | 1538 HandleScope scope(isolate); |
| 1539 int32_t start_index = |
| 1540 startup_data->GetValueChecked<Smi>(isolate, kExportedFunctionIndex) |
| 1541 ->value(); |
1679 Handle<Code> startup_code = | 1542 Handle<Code> startup_code = |
1680 metadata->GetValueChecked<Code>(isolate, kExportCode); | 1543 code_table->GetValueChecked<Code>(isolate, start_index); |
1681 int arity = Smi::cast(metadata->get(kExportArity))->value(); | 1544 int arity = Smi::cast(startup_data->get(kExportArity))->value(); |
1682 MaybeHandle<ByteArray> startup_signature = | 1545 MaybeHandle<ByteArray> startup_signature = |
1683 metadata->GetValue<ByteArray>(isolate, kExportedSignature); | 1546 startup_data->GetValue<ByteArray>(isolate, kExportedSignature); |
1684 Handle<JSFunction> startup_fct = WrapExportCodeAsJSFunction( | 1547 Handle<JSFunction> startup_fct = WrapExportCodeAsJSFunction( |
1685 isolate, startup_code, factory->InternalizeUtf8String("start"), arity, | 1548 isolate, startup_code, factory->InternalizeUtf8String("start"), arity, |
1686 startup_signature, js_object); | 1549 startup_signature, instance); |
1687 RecordStats(isolate, *startup_code); | 1550 RecordStats(isolate, *startup_code); |
1688 // Call the JS function. | 1551 // Call the JS function. |
1689 Handle<Object> undefined = isolate->factory()->undefined_value(); | 1552 Handle<Object> undefined = isolate->factory()->undefined_value(); |
1690 MaybeHandle<Object> retval = | 1553 MaybeHandle<Object> retval = |
1691 Execution::Call(isolate, startup_fct, undefined, 0, nullptr); | 1554 Execution::Call(isolate, startup_fct, undefined, 0, nullptr); |
1692 | 1555 |
1693 if (retval.is_null()) { | 1556 if (retval.is_null()) { |
1694 thrower.Error("WASM.instantiateModule(): start function failed"); | 1557 thrower.Error("WASM.instantiateModule(): start function failed"); |
1695 return nothing; | 1558 return nothing; |
1696 } | 1559 } |
1697 } | 1560 } |
1698 | 1561 |
1699 DCHECK(wasm::IsWasmObject(*js_object)); | 1562 DCHECK(wasm::IsWasmObject(*instance)); |
1700 | 1563 |
1701 if (!compiled_module->GetValue<WeakCell>(isolate, kModuleObject).is_null()) { | 1564 if (!compiled_module->GetValue<WeakCell>(isolate, kModuleObject).is_null()) { |
1702 js_object->SetInternalField(kWasmCompiledModule, *compiled_module); | 1565 instance->SetInternalField(kWasmCompiledModule, *compiled_module); |
1703 Handle<WeakCell> link_to_owner = factory->NewWeakCell(js_object); | 1566 Handle<WeakCell> link_to_owner = factory->NewWeakCell(instance); |
1704 compiled_module->set(kOwningInstance, *link_to_owner); | 1567 compiled_module->set(kOwningInstance, *link_to_owner); |
1705 | 1568 |
1706 Handle<Object> global_handle = | 1569 Handle<Object> global_handle = isolate->global_handles()->Create(*instance); |
1707 isolate->global_handles()->Create(*js_object); | |
1708 GlobalHandles::MakeWeak(global_handle.location(), global_handle.location(), | 1570 GlobalHandles::MakeWeak(global_handle.location(), global_handle.location(), |
1709 &InstanceFinalizer, | 1571 &InstanceFinalizer, |
1710 v8::WeakCallbackType::kFinalizer); | 1572 v8::WeakCallbackType::kFinalizer); |
1711 } | 1573 } |
1712 | 1574 |
1713 return js_object; | 1575 return instance; |
1714 } | |
1715 | |
1716 // TODO(mtrofin): remove this once we move to WASM_DIRECT_CALL | |
1717 Handle<Code> ModuleEnv::GetCodeOrPlaceholder(uint32_t index) const { | |
1718 DCHECK(IsValidFunction(index)); | |
1719 if (!placeholders.empty()) return placeholders[index]; | |
1720 DCHECK_NOT_NULL(instance); | |
1721 return instance->function_code[index]; | |
1722 } | |
1723 | |
1724 Handle<Code> ModuleEnv::GetImportCode(uint32_t index) { | |
1725 DCHECK(IsValidImport(index)); | |
1726 return instance ? instance->import_code[index] : Handle<Code>::null(); | |
1727 } | 1576 } |
1728 | 1577 |
1729 compiler::CallDescriptor* ModuleEnv::GetCallDescriptor(Zone* zone, | 1578 compiler::CallDescriptor* ModuleEnv::GetCallDescriptor(Zone* zone, |
1730 uint32_t index) { | 1579 uint32_t index) { |
1731 DCHECK(IsValidFunction(index)); | 1580 DCHECK(IsValidFunction(index)); |
1732 // Always make a direct call to whatever is in the table at that location. | 1581 // Always make a direct call to whatever is in the table at that location. |
1733 // A wrapper will be generated for FFI calls. | 1582 // A wrapper will be generated for FFI calls. |
1734 const WasmFunction* function = &module->functions[index]; | 1583 const WasmFunction* function = &module->functions[index]; |
1735 return GetWasmCallDescriptor(zone, function->sig); | 1584 return GetWasmCallDescriptor(zone, function->sig); |
1736 } | 1585 } |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1987 FixedArray* compiled_module = | 1836 FixedArray* compiled_module = |
1988 FixedArray::cast(instance->GetInternalField(kWasmCompiledModule)); | 1837 FixedArray::cast(instance->GetInternalField(kWasmCompiledModule)); |
1989 CHECK_NOT_NULL(GetModuleObject(compiled_module)); | 1838 CHECK_NOT_NULL(GetModuleObject(compiled_module)); |
1990 CHECK(GetModuleObject(compiled_module)->cleared()); | 1839 CHECK(GetModuleObject(compiled_module)->cleared()); |
1991 } | 1840 } |
1992 | 1841 |
1993 } // namespace testing | 1842 } // namespace testing |
1994 } // namespace wasm | 1843 } // namespace wasm |
1995 } // namespace internal | 1844 } // namespace internal |
1996 } // namespace v8 | 1845 } // namespace v8 |
OLD | NEW |