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 "src/base/atomic-utils.h" | 5 #include "src/base/atomic-utils.h" |
6 #include "src/macro-assembler.h" | 6 #include "src/macro-assembler.h" |
7 #include "src/objects.h" | 7 #include "src/objects.h" |
8 #include "src/property-descriptor.h" | 8 #include "src/property-descriptor.h" |
9 #include "src/v8.h" | 9 #include "src/v8.h" |
10 | 10 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 const int kWasmModuleFunctionTable = 0; | 117 const int kWasmModuleFunctionTable = 0; |
118 const int kWasmModuleCodeTable = 1; | 118 const int kWasmModuleCodeTable = 1; |
119 const int kWasmMemArrayBuffer = 2; | 119 const int kWasmMemArrayBuffer = 2; |
120 const int kWasmGlobalsArrayBuffer = 3; | 120 const int kWasmGlobalsArrayBuffer = 3; |
121 // TODO(clemensh): Remove function name array, extract names from module bytes. | 121 // TODO(clemensh): Remove function name array, extract names from module bytes. |
122 const int kWasmFunctionNamesArray = 4; | 122 const int kWasmFunctionNamesArray = 4; |
123 const int kWasmModuleBytesString = 5; | 123 const int kWasmModuleBytesString = 5; |
124 const int kWasmDebugInfo = 6; | 124 const int kWasmDebugInfo = 6; |
125 const int kWasmModuleInternalFieldCount = 7; | 125 const int kWasmModuleInternalFieldCount = 7; |
126 | 126 |
127 // TODO(mtrofin): Unnecessary once we stop using JS Heap for wasm code. | |
128 | |
129 enum CompiledWasmObjectFields { | |
bradnelson
2016/06/27 22:28:47
This all seems structurally complicated enough I w
Mircea Trofin
2016/06/28 05:24:21
Done.
| |
130 kFunctions, | |
131 kImportData, | |
132 kExports, | |
133 kStartupFunction, | |
134 kModuleBytes, | |
135 kFunctionNameTable, | |
136 kMinRequiredMemory, | |
137 kDataSegmentsInfo, | |
138 kDataSegments, | |
139 kGlobalsSize, | |
140 kExportMem, | |
141 kOrigin, | |
142 kCompiledWasmObjectTableSize | |
143 }; | |
144 | |
145 enum WasmImportDataFields { | |
bradnelson
2016/06/27 22:28:47
Isn't this more like module meta-data?
Mircea Trofin
2016/06/28 05:24:21
Done.
| |
146 kModuleName, | |
147 kFunctionName, | |
148 kOutputCount, | |
149 kSignature, | |
150 kWasmImportDataTableSize | |
151 }; | |
152 | |
153 enum WasmSegmentInfo { kDestAddr, kSourceSize, kWasmSegmentInfoSize }; | |
154 | |
127 uint32_t GetMinModuleMemSize(const WasmModule* module) { | 155 uint32_t GetMinModuleMemSize(const WasmModule* module) { |
128 return WasmModule::kPageSize * module->min_mem_pages; | 156 return WasmModule::kPageSize * module->min_mem_pages; |
129 } | 157 } |
130 | 158 |
131 void LoadDataSegments(const WasmModule* module, byte* mem_addr, | 159 void LoadDataSegments(Handle<FixedArray> compiled_module, Address mem_addr, |
132 size_t mem_size) { | 160 size_t mem_size) { |
133 for (const WasmDataSegment& segment : module->data_segments) { | 161 Handle<ByteArray> data = compiled_module->GetValue<ByteArray>(kDataSegments); |
134 if (!segment.init) continue; | 162 Handle<FixedArray> segments = Handle<FixedArray>( |
135 if (!segment.source_size) continue; | 163 FixedArray::cast(compiled_module->get(kDataSegmentsInfo))); |
136 CHECK_LT(segment.dest_addr, mem_size); | 164 |
137 CHECK_LE(segment.source_size, mem_size); | 165 uint32_t last_extraction_pos = 0; |
138 CHECK_LE(segment.dest_addr + segment.source_size, mem_size); | 166 for (int i = 0; i < segments->length(); ++i) { |
139 byte* addr = mem_addr + segment.dest_addr; | 167 Handle<ByteArray> segment = |
140 memcpy(addr, module->module_start + segment.source_offset, | 168 Handle<ByteArray>(ByteArray::cast(segments->get(i))); |
141 segment.source_size); | 169 uint32_t dest_addr = static_cast<uint32_t>(segment->get_int(kDestAddr)); |
170 uint32_t source_size = static_cast<uint32_t>(segment->get_int(kSourceSize)); | |
171 CHECK_LT(dest_addr, mem_size); | |
172 CHECK_LE(source_size, mem_size); | |
173 CHECK_LE(dest_addr + source_size, mem_size); | |
bradnelson
2016/06/27 22:28:47
On the theme of overflow, this can wrap around.
Ma
Mircea Trofin
2016/06/28 05:24:21
Done.
| |
174 byte* addr = mem_addr + dest_addr; | |
175 data->copy_out(last_extraction_pos, addr, source_size); | |
176 last_extraction_pos += source_size; | |
142 } | 177 } |
143 } | 178 } |
144 | 179 |
180 void SaveDataSegmentInfo(Factory* factory, const WasmModule* module, | |
181 Handle<FixedArray> compiled_module) { | |
182 Handle<FixedArray> segments = factory->NewFixedArray( | |
183 static_cast<int>(module->data_segments.size()), TENURED); | |
184 uint32_t data_size = 0; | |
185 for (const WasmDataSegment& segment : module->data_segments) { | |
186 if (!segment.init) continue; | |
187 if (segment.source_size == 0) continue; | |
188 data_size += segment.source_size; | |
189 } | |
190 Handle<ByteArray> data = factory->NewByteArray(data_size, TENURED); | |
191 | |
192 uint32_t last_insertion_pos = 0; | |
193 for (uint32_t i = 0; i < module->data_segments.size(); ++i) { | |
194 const WasmDataSegment& segment = module->data_segments[i]; | |
195 if (!segment.init) continue; | |
196 if (segment.source_size == 0) continue; | |
197 Handle<ByteArray> js_segment = | |
198 factory->NewByteArray(kWasmSegmentInfoSize * sizeof(uint32_t), TENURED); | |
199 js_segment->set_int(kDestAddr, segment.dest_addr); | |
200 js_segment->set_int(kSourceSize, segment.source_size); | |
201 segments->set(i, *js_segment); | |
202 data->copy_in(last_insertion_pos, | |
203 module->module_start + segment.source_offset, | |
204 segment.source_size); | |
205 last_insertion_pos += segment.source_size; | |
206 } | |
207 compiled_module->set(kDataSegmentsInfo, *segments); | |
208 compiled_module->set(kDataSegments, *data); | |
209 } | |
210 | |
145 Handle<FixedArray> BuildFunctionTable(Isolate* isolate, | 211 Handle<FixedArray> BuildFunctionTable(Isolate* isolate, |
146 const WasmModule* module) { | 212 const WasmModule* module) { |
147 // Compute the size of the indirect function table | 213 // Compute the size of the indirect function table |
148 uint32_t table_size = module->FunctionTableSize(); | 214 uint32_t table_size = module->FunctionTableSize(); |
149 if (table_size == 0) { | 215 if (table_size == 0) { |
150 return Handle<FixedArray>::null(); | 216 return Handle<FixedArray>::null(); |
151 } | 217 } |
152 | 218 |
153 Handle<FixedArray> fixed = isolate->factory()->NewFixedArray(2 * table_size); | 219 Handle<FixedArray> fixed = isolate->factory()->NewFixedArray(2 * table_size); |
154 for (uint32_t i = 0; | 220 for (uint32_t i = 0; |
155 i < static_cast<uint32_t>(module->function_table.size()); | 221 i < static_cast<uint32_t>(module->function_table.size()); |
156 ++i) { | 222 ++i) { |
157 const WasmFunction* function = | 223 const WasmFunction* function = |
158 &module->functions[module->function_table[i]]; | 224 &module->functions[module->function_table[i]]; |
159 fixed->set(i, Smi::FromInt(function->sig_index)); | 225 fixed->set(i, Smi::FromInt(function->sig_index)); |
160 } | 226 } |
161 return fixed; | 227 return fixed; |
162 } | 228 } |
163 | 229 |
164 Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size, | 230 Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size) { |
165 byte** backing_store) { | |
166 *backing_store = nullptr; | |
167 if (size > (WasmModule::kMaxMemPages * WasmModule::kPageSize)) { | 231 if (size > (WasmModule::kMaxMemPages * WasmModule::kPageSize)) { |
168 // TODO(titzer): lift restriction on maximum memory allocated here. | 232 // TODO(titzer): lift restriction on maximum memory allocated here. |
169 return Handle<JSArrayBuffer>::null(); | 233 return Handle<JSArrayBuffer>::null(); |
170 } | 234 } |
171 void* memory = isolate->array_buffer_allocator()->Allocate(size); | 235 void* memory = isolate->array_buffer_allocator()->Allocate(size); |
172 if (memory == nullptr) { | 236 if (memory == nullptr) { |
173 return Handle<JSArrayBuffer>::null(); | 237 return Handle<JSArrayBuffer>::null(); |
174 } | 238 } |
175 | 239 |
176 *backing_store = reinterpret_cast<byte*>(memory); | |
177 | |
178 #if DEBUG | 240 #if DEBUG |
179 // Double check the API allocator actually zero-initialized the memory. | 241 // Double check the API allocator actually zero-initialized the memory. |
180 byte* bytes = reinterpret_cast<byte*>(*backing_store); | 242 const byte* bytes = reinterpret_cast<const byte*>(memory); |
181 for (size_t i = 0; i < size; ++i) { | 243 for (size_t i = 0; i < size; ++i) { |
182 DCHECK_EQ(0, bytes[i]); | 244 DCHECK_EQ(0, bytes[i]); |
183 } | 245 } |
184 #endif | 246 #endif |
185 | 247 |
186 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); | 248 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); |
187 JSArrayBuffer::Setup(buffer, isolate, false, memory, static_cast<int>(size)); | 249 JSArrayBuffer::Setup(buffer, isolate, false, memory, static_cast<int>(size)); |
188 buffer->set_is_neuterable(false); | 250 buffer->set_is_neuterable(false); |
189 return buffer; | 251 return buffer; |
190 } | 252 } |
191 | 253 |
192 void RelocateInstanceCode(WasmModuleInstance* instance) { | 254 void RelocateInstanceCode(Handle<JSObject> instance, Address start, |
193 for (uint32_t i = 0; i < instance->function_code.size(); ++i) { | 255 uint32_t prev_size, uint32_t new_size) { |
194 Handle<Code> function = instance->function_code[i]; | 256 Handle<FixedArray> functions = Handle<FixedArray>( |
257 FixedArray::cast(instance->GetInternalField(kWasmModuleCodeTable))); | |
258 for (int i = 0; i < functions->length(); ++i) { | |
259 Handle<Code> function = Handle<Code>(Code::cast(functions->get(i))); | |
195 AllowDeferredHandleDereference embedding_raw_address; | 260 AllowDeferredHandleDereference embedding_raw_address; |
196 int mask = (1 << RelocInfo::WASM_MEMORY_REFERENCE) | | 261 int mask = (1 << RelocInfo::WASM_MEMORY_REFERENCE) | |
197 (1 << RelocInfo::WASM_MEMORY_SIZE_REFERENCE); | 262 (1 << RelocInfo::WASM_MEMORY_SIZE_REFERENCE); |
198 for (RelocIterator it(*function, mask); !it.done(); it.next()) { | 263 for (RelocIterator it(*function, mask); !it.done(); it.next()) { |
199 it.rinfo()->update_wasm_memory_reference( | 264 it.rinfo()->update_wasm_memory_reference(nullptr, start, prev_size, |
200 nullptr, instance->mem_start, GetMinModuleMemSize(instance->module), | 265 new_size); |
201 static_cast<uint32_t>(instance->mem_size)); | |
202 } | 266 } |
203 } | 267 } |
204 } | 268 } |
205 | 269 |
206 // Set the memory for a module instance to be the {memory} array buffer. | 270 // Allocate memory for a module instance as a new JSArrayBuffer. |
207 void SetMemory(WasmModuleInstance* instance, Handle<JSArrayBuffer> memory) { | 271 Handle<JSArrayBuffer> AllocateMemory(ErrorThrower* thrower, Isolate* isolate, |
208 memory->set_is_neuterable(false); | 272 uint32_t min_mem_pages) { |
209 instance->mem_start = reinterpret_cast<byte*>(memory->backing_store()); | 273 if (min_mem_pages > WasmModule::kMaxMemPages) { |
210 instance->mem_size = memory->byte_length()->Number(); | 274 thrower->Error("Out of memory: wasm memory too large"); |
211 instance->mem_buffer = memory; | 275 return Handle<JSArrayBuffer>::null(); |
212 RelocateInstanceCode(instance); | 276 } |
277 Handle<JSArrayBuffer> mem_buffer = | |
278 NewArrayBuffer(isolate, min_mem_pages * WasmModule::kPageSize); | |
279 | |
280 if (mem_buffer.is_null()) { | |
281 thrower->Error("Out of memory: wasm memory"); | |
282 } | |
283 return mem_buffer; | |
213 } | 284 } |
214 | 285 |
215 // Allocate memory for a module instance as a new JSArrayBuffer. | 286 void RelocateGlobals(Handle<JSObject> instance, Address globals_start) { |
216 bool AllocateMemory(ErrorThrower* thrower, Isolate* isolate, | 287 Handle<FixedArray> functions = Handle<FixedArray>( |
217 WasmModuleInstance* instance) { | 288 FixedArray::cast(instance->GetInternalField(kWasmModuleCodeTable))); |
218 DCHECK(instance->module); | 289 uint32_t function_count = static_cast<uint32_t>(functions->length()); |
219 DCHECK(instance->mem_buffer.is_null()); | 290 for (uint32_t i = 0; i < function_count; ++i) { |
220 | 291 Handle<Code> function = Handle<Code>(Code::cast(functions->get(i))); |
221 if (instance->module->min_mem_pages > WasmModule::kMaxMemPages) { | 292 AllowDeferredHandleDereference embedding_raw_address; |
222 thrower->Error("Out of memory: wasm memory too large"); | 293 int mask = 1 << RelocInfo::WASM_GLOBAL_REFERENCE; |
223 return false; | 294 for (RelocIterator it(*function, mask); !it.done(); it.next()) { |
224 } | 295 it.rinfo()->update_wasm_global_reference(nullptr, globals_start); |
225 instance->mem_size = GetMinModuleMemSize(instance->module); | |
226 instance->mem_buffer = | |
227 NewArrayBuffer(isolate, instance->mem_size, &instance->mem_start); | |
228 if (instance->mem_start == nullptr) { | |
229 thrower->Error("Out of memory: wasm memory"); | |
230 instance->mem_size = 0; | |
231 return false; | |
232 } | |
233 RelocateInstanceCode(instance); | |
234 return true; | |
235 } | |
236 | |
237 bool AllocateGlobals(ErrorThrower* thrower, Isolate* isolate, | |
238 WasmModuleInstance* instance) { | |
239 uint32_t globals_size = instance->module->globals_size; | |
240 if (globals_size > 0) { | |
241 instance->globals_buffer = | |
242 NewArrayBuffer(isolate, globals_size, &instance->globals_start); | |
243 if (!instance->globals_start) { | |
244 // Not enough space for backing store of globals. | |
245 thrower->Error("Out of memory: wasm globals"); | |
246 return false; | |
247 } | |
248 | |
249 for (uint32_t i = 0; i < instance->function_code.size(); ++i) { | |
250 Handle<Code> function = instance->function_code[i]; | |
251 AllowDeferredHandleDereference embedding_raw_address; | |
252 int mask = 1 << RelocInfo::WASM_GLOBAL_REFERENCE; | |
253 for (RelocIterator it(*function, mask); !it.done(); it.next()) { | |
254 it.rinfo()->update_wasm_global_reference(nullptr, | |
255 instance->globals_start); | |
256 } | |
257 } | 296 } |
258 } | 297 } |
259 return true; | |
260 } | 298 } |
261 | 299 |
262 Handle<Code> CreatePlaceholder(Factory* factory, uint32_t index, | 300 Handle<Code> CreatePlaceholder(Factory* factory, uint32_t index, |
263 Code::Kind kind) { | 301 Code::Kind kind) { |
264 // Create a placeholder code object and encode the corresponding index in | 302 // Create a placeholder code object and encode the corresponding index in |
265 // the {constant_pool_offset} field of the code object. | 303 // the {constant_pool_offset} field of the code object. |
266 // TODO(titzer): placeholder code objects are somewhat dangerous. | 304 // TODO(titzer): placeholder code objects are somewhat dangerous. |
267 static byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions. | 305 static byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions. |
268 static CodeDesc desc = {buffer, 8, 8, 0, 0, nullptr}; | 306 static CodeDesc desc = {buffer, 8, 8, 0, 0, nullptr}; |
269 Handle<Code> code = factory->NewCode(desc, Code::KindField::encode(kind), | 307 Handle<Code> code = factory->NewCode(desc, Code::KindField::encode(kind), |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
344 min_mem_pages(0), | 382 min_mem_pages(0), |
345 max_mem_pages(0), | 383 max_mem_pages(0), |
346 mem_export(false), | 384 mem_export(false), |
347 mem_external(false), | 385 mem_external(false), |
348 start_function_index(-1), | 386 start_function_index(-1), |
349 origin(kWasmOrigin), | 387 origin(kWasmOrigin), |
350 globals_size(0), | 388 globals_size(0), |
351 indirect_table_size(0), | 389 indirect_table_size(0), |
352 pending_tasks(new base::Semaphore(0)) {} | 390 pending_tasks(new base::Semaphore(0)) {} |
353 | 391 |
354 static MaybeHandle<JSFunction> ReportFFIError(ErrorThrower& thrower, | 392 static MaybeHandle<JSFunction> ReportFFIError( |
355 const char* error, uint32_t index, | 393 ErrorThrower& thrower, const char* error, uint32_t index, |
356 wasm::WasmName module_name, | 394 Handle<String> module_name, MaybeHandle<String> function_name) { |
357 wasm::WasmName function_name) { | 395 if (!function_name.is_null()) { |
358 if (!function_name.is_empty()) { | 396 Handle<String> function_name_handle = function_name.ToHandleChecked(); |
359 thrower.Error("Import #%d module=\"%.*s\" function=\"%.*s\" error: %s", | 397 thrower.Error("Import #%d module=\"%.*s\" function=\"%.*s\" error: %s", |
360 index, module_name.length(), module_name.start(), | 398 index, module_name->length(), module_name->ToCString().get(), |
361 function_name.length(), function_name.start(), error); | 399 function_name_handle->length(), |
400 function_name_handle->ToCString().get(), error); | |
362 } else { | 401 } else { |
363 thrower.Error("Import #%d module=\"%.*s\" error: %s", index, | 402 thrower.Error("Import #%d module=\"%.*s\" error: %s", index, |
364 module_name.length(), module_name.start(), error); | 403 module_name->length(), module_name->ToCString().get(), error); |
365 } | 404 } |
366 thrower.Error("Import "); | 405 thrower.Error("Import "); |
367 return MaybeHandle<JSFunction>(); | 406 return MaybeHandle<JSFunction>(); |
368 } | 407 } |
369 | 408 |
370 static MaybeHandle<JSFunction> LookupFunction( | 409 static MaybeHandle<JSFunction> LookupFunction( |
371 ErrorThrower& thrower, Factory* factory, Handle<JSReceiver> ffi, | 410 ErrorThrower& thrower, Factory* factory, Handle<JSReceiver> ffi, |
372 uint32_t index, wasm::WasmName module_name, wasm::WasmName function_name) { | 411 uint32_t index, Handle<String> module_name, |
412 MaybeHandle<String> function_name) { | |
373 if (ffi.is_null()) { | 413 if (ffi.is_null()) { |
374 return ReportFFIError(thrower, "FFI is not an object", index, module_name, | 414 return ReportFFIError(thrower, "FFI is not an object", index, module_name, |
375 function_name); | 415 function_name); |
376 } | 416 } |
377 | 417 |
378 // Look up the module first. | 418 // Look up the module first. |
379 Handle<String> name = factory->InternalizeUtf8String(module_name); | 419 MaybeHandle<Object> result = Object::GetProperty(ffi, module_name); |
380 MaybeHandle<Object> result = Object::GetProperty(ffi, name); | |
381 if (result.is_null()) { | 420 if (result.is_null()) { |
382 return ReportFFIError(thrower, "module not found", index, module_name, | 421 return ReportFFIError(thrower, "module not found", index, module_name, |
383 function_name); | 422 function_name); |
384 } | 423 } |
385 | 424 |
386 Handle<Object> module = result.ToHandleChecked(); | 425 Handle<Object> module = result.ToHandleChecked(); |
387 | 426 |
388 if (!module->IsJSReceiver()) { | 427 if (!module->IsJSReceiver()) { |
389 return ReportFFIError(thrower, "module is not an object or function", index, | 428 return ReportFFIError(thrower, "module is not an object or function", index, |
390 module_name, function_name); | 429 module_name, function_name); |
391 } | 430 } |
392 | 431 |
393 Handle<Object> function; | 432 Handle<Object> function; |
394 if (!function_name.is_empty()) { | 433 if (!function_name.is_null()) { |
395 // Look up the function in the module. | 434 // Look up the function in the module. |
396 Handle<String> name = factory->InternalizeUtf8String(function_name); | 435 MaybeHandle<Object> result = |
397 MaybeHandle<Object> result = Object::GetProperty(module, name); | 436 Object::GetProperty(module, function_name.ToHandleChecked()); |
398 if (result.is_null()) { | 437 if (result.is_null()) { |
399 return ReportFFIError(thrower, "function not found", index, module_name, | 438 return ReportFFIError(thrower, "function not found", index, module_name, |
400 function_name); | 439 function_name); |
401 } | 440 } |
402 function = result.ToHandleChecked(); | 441 function = result.ToHandleChecked(); |
403 } else { | 442 } else { |
404 // No function specified. Use the "default export". | 443 // No function specified. Use the "default export". |
405 function = module; | 444 function = module; |
406 } | 445 } |
407 | 446 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
476 }; | 515 }; |
477 | 516 |
478 // Records statistics on the code generated by compiling WASM functions. | 517 // Records statistics on the code generated by compiling WASM functions. |
479 struct CodeStats { | 518 struct CodeStats { |
480 size_t code_size; | 519 size_t code_size; |
481 size_t reloc_size; | 520 size_t reloc_size; |
482 | 521 |
483 inline CodeStats() : code_size(0), reloc_size(0) {} | 522 inline CodeStats() : code_size(0), reloc_size(0) {} |
484 | 523 |
485 inline void Record(Code* code) { | 524 inline void Record(Code* code) { |
486 code_size += code->body_size(); | 525 if (FLAG_print_wasm_code_size) { |
487 reloc_size += code->relocation_info()->length(); | 526 code_size += code->body_size(); |
527 reloc_size += code->relocation_info()->length(); | |
528 } | |
529 } | |
530 | |
531 void Record(const std::vector<Handle<Code>>& functions) { | |
532 if (FLAG_print_wasm_code_size) { | |
533 for (Handle<Code> c : functions) Record(*c); | |
534 } | |
535 } | |
536 | |
537 void Record(Handle<FixedArray> functions) { | |
538 if (FLAG_print_wasm_code_size) { | |
539 DisallowHeapAllocation no_gc; | |
540 for (int i = 0; i < functions->length(); ++i) { | |
541 Code* code = Code::cast(functions->get(i)); | |
542 Record(code); | |
543 } | |
544 } | |
488 } | 545 } |
489 | 546 |
490 inline void Report() { | 547 inline void Report() { |
491 PrintF("Total generated wasm code: %zu bytes\n", code_size); | 548 if (FLAG_print_wasm_code_size) { |
492 PrintF("Total generated wasm reloc: %zu bytes\n", reloc_size); | 549 PrintF("Total generated wasm code: %zu bytes\n", code_size); |
550 PrintF("Total generated wasm reloc: %zu bytes\n", reloc_size); | |
551 } | |
493 } | 552 } |
494 }; | 553 }; |
495 | 554 |
496 bool CompileWrappersToImportedFunctions( | 555 Handle<FixedArray> GetImportsMetadata(Factory* factory, |
497 Isolate* isolate, const WasmModule* module, const Handle<JSReceiver> ffi, | 556 const WasmModule* module) { |
498 WasmModuleInstance* instance, ErrorThrower* thrower, Factory* factory) { | 557 Handle<FixedArray> ret = factory->NewFixedArray( |
499 if (module->import_table.size() > 0) { | 558 static_cast<int>(module->import_table.size()), TENURED); |
500 instance->import_code.reserve(module->import_table.size()); | 559 for (size_t i = 0; i < module->import_table.size(); ++i) { |
501 for (uint32_t index = 0; index < module->import_table.size(); ++index) { | 560 const WasmImport& import = module->import_table[i]; |
502 const WasmImport& import = module->import_table[index]; | 561 WasmName module_name = module->GetNameOrNull(import.module_name_offset, |
503 WasmName module_name = module->GetNameOrNull(import.module_name_offset, | 562 import.module_name_length); |
504 import.module_name_length); | 563 WasmName function_name = module->GetNameOrNull(import.function_name_offset, |
505 WasmName function_name = module->GetNameOrNull( | 564 import.function_name_length); |
506 import.function_name_offset, import.function_name_length); | 565 |
566 Handle<String> module_name_string = | |
567 factory->InternalizeUtf8String(module_name); | |
568 Handle<String> function_name_string = | |
569 function_name.is_empty() | |
570 ? Handle<String>::null() | |
571 : factory->InternalizeUtf8String(function_name); | |
572 Handle<ByteArray> sig = | |
573 factory->NewByteArray(static_cast<int>(import.sig->parameter_count() + | |
574 import.sig->return_count()), | |
575 TENURED); | |
576 sig->copy_in(0, reinterpret_cast<const byte*>(import.sig->raw_data()), | |
577 sig->length()); | |
578 Handle<FixedArray> encoded_import = | |
579 factory->NewFixedArray(kWasmImportDataTableSize, TENURED); | |
580 encoded_import->set(kModuleName, *module_name_string); | |
581 if (!function_name_string.is_null()) { | |
582 encoded_import->set(kFunctionName, *function_name_string); | |
583 } | |
584 encoded_import->set( | |
585 kOutputCount, | |
586 Smi::FromInt(static_cast<int>(import.sig->return_count()))); | |
587 encoded_import->set(kSignature, *sig); | |
588 ret->set(static_cast<int>(i), *encoded_import); | |
589 } | |
590 return ret; | |
591 } | |
592 | |
593 bool CompileWrappersToImportedFunctions(Isolate* isolate, | |
594 const Handle<JSReceiver> ffi, | |
595 std::vector<Handle<Code>>& imports, | |
596 Handle<FixedArray> import_data, | |
597 ErrorThrower* thrower) { | |
598 uint32_t import_count = static_cast<uint32_t>(import_data->length()); | |
599 if (import_count > 0) { | |
600 imports.reserve(import_count); | |
601 for (uint32_t index = 0; index < import_count; ++index) { | |
602 Handle<FixedArray> data = import_data->GetValue<FixedArray>(index); | |
603 Handle<String> module_name = data->GetValue<String>(kModuleName); | |
604 MaybeHandle<String> function_name = | |
605 data->GetValueOrNull<String>(kFunctionName); | |
606 | |
607 int ret_count = Smi::cast(data->get(kOutputCount))->value(); | |
608 CHECK(ret_count >= 0); | |
609 Handle<ByteArray> sig_data = data->GetValue<ByteArray>(kSignature); | |
610 int sig_data_size = sig_data->length(); | |
611 int param_count = sig_data_size - ret_count; | |
612 CHECK(param_count >= 0); | |
613 | |
507 MaybeHandle<JSFunction> function = LookupFunction( | 614 MaybeHandle<JSFunction> function = LookupFunction( |
508 *thrower, factory, ffi, index, module_name, function_name); | 615 *thrower, isolate->factory(), ffi, index, module_name, function_name); |
509 if (function.is_null()) return false; | 616 if (function.is_null()) return false; |
510 | 617 |
618 FunctionSig sig( | |
619 ret_count, param_count, | |
620 reinterpret_cast<const MachineRepresentation*>(sig_data->data())); | |
621 | |
511 Handle<Code> code = compiler::CompileWasmToJSWrapper( | 622 Handle<Code> code = compiler::CompileWasmToJSWrapper( |
512 isolate, function.ToHandleChecked(), import.sig, module_name, | 623 isolate, function.ToHandleChecked(), &sig, index, module_name, |
513 function_name); | 624 function_name); |
514 instance->import_code[index] = code; | 625 |
626 imports.push_back(code); | |
515 } | 627 } |
516 } | 628 } |
517 return true; | 629 return true; |
518 } | 630 } |
519 | 631 |
520 void InitializeParallelCompilation( | 632 void InitializeParallelCompilation( |
521 Isolate* isolate, const std::vector<WasmFunction>& functions, | 633 Isolate* isolate, const std::vector<WasmFunction>& functions, |
522 std::vector<compiler::WasmCompilationUnit*>& compilation_units, | 634 std::vector<compiler::WasmCompilationUnit*>& compilation_units, |
523 ModuleEnv& module_env, ErrorThrower& thrower) { | 635 ModuleEnv& module_env, ErrorThrower& thrower) { |
524 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); ++i) { | 636 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); ++i) { |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
676 DCHECK_EQ(table_size * 2, instance->function_table->length()); | 788 DCHECK_EQ(table_size * 2, instance->function_table->length()); |
677 uint32_t populated_table_size = | 789 uint32_t populated_table_size = |
678 static_cast<uint32_t>(instance->module->function_table.size()); | 790 static_cast<uint32_t>(instance->module->function_table.size()); |
679 for (uint32_t i = 0; i < populated_table_size; ++i) { | 791 for (uint32_t i = 0; i < populated_table_size; ++i) { |
680 instance->function_table->set( | 792 instance->function_table->set( |
681 i + table_size, | 793 i + table_size, |
682 *instance->function_code[instance->module->function_table[i]]); | 794 *instance->function_code[instance->module->function_table[i]]); |
683 } | 795 } |
684 } | 796 } |
685 } | 797 } |
686 } // namespace | |
687 | 798 |
688 void SetDeoptimizationData(Factory* factory, Handle<JSObject> js_object, | 799 void SetDebugSupport(Factory* factory, Handle<FixedArray> compiled_module, |
689 std::vector<Handle<Code>>& functions) { | 800 Handle<JSObject> js_object) { |
690 for (size_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); ++i) { | 801 MaybeHandle<String> module_bytes_string = |
691 Handle<Code> code = functions[i]; | 802 compiled_module->GetValueOrNull<String>(kModuleBytes); |
803 if (!module_bytes_string.is_null()) { | |
804 js_object->SetInternalField(kWasmModuleBytesString, | |
805 *module_bytes_string.ToHandleChecked()); | |
806 } | |
807 Handle<FixedArray> functions = Handle<FixedArray>( | |
808 FixedArray::cast(js_object->GetInternalField(kWasmModuleCodeTable))); | |
809 | |
810 for (int i = FLAG_skip_compiling_wasm_funcs; i < functions->length(); ++i) { | |
811 Handle<Code> code = functions->GetValue<Code>(i); | |
692 DCHECK(code->deoptimization_data() == nullptr || | 812 DCHECK(code->deoptimization_data() == nullptr || |
693 code->deoptimization_data()->length() == 0); | 813 code->deoptimization_data()->length() == 0); |
694 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED); | 814 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED); |
695 if (!js_object.is_null()) { | 815 if (!js_object.is_null()) { |
696 deopt_data->set(0, *js_object); | 816 deopt_data->set(0, *js_object); |
697 } | 817 } |
698 deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); | 818 deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); |
699 deopt_data->set_length(2); | 819 deopt_data->set_length(2); |
700 code->set_deoptimization_data(*deopt_data); | 820 code->set_deoptimization_data(*deopt_data); |
701 } | 821 } |
822 | |
823 MaybeHandle<ByteArray> function_name_table = | |
824 compiled_module->GetValueOrNull<ByteArray>(kFunctionNameTable); | |
825 if (!function_name_table.is_null()) { | |
826 js_object->SetInternalField(kWasmFunctionNamesArray, | |
827 *function_name_table.ToHandleChecked()); | |
828 } | |
702 } | 829 } |
703 | 830 |
831 bool SetupGlobals(Isolate* isolate, Handle<FixedArray> compiled_module, | |
832 Handle<JSObject> instance, ErrorThrower* thrower) { | |
833 uint32_t globals_size = static_cast<uint32_t>( | |
834 Smi::cast(compiled_module->get(kGlobalsSize))->value()); | |
835 if (globals_size > 0) { | |
836 Handle<JSArrayBuffer> globals_buffer = | |
837 NewArrayBuffer(isolate, globals_size); | |
838 if (globals_buffer.is_null()) { | |
839 thrower->Error("Out of memory: wasm globals"); | |
840 return false; | |
841 } | |
842 RelocateGlobals(instance, | |
843 static_cast<Address>(globals_buffer->backing_store())); | |
844 instance->SetInternalField(kWasmGlobalsArrayBuffer, *globals_buffer); | |
845 } | |
846 return true; | |
847 } | |
848 | |
849 bool SetupInstanceHeap(Isolate* isolate, Handle<FixedArray> compiled_module, | |
850 Handle<JSObject> instance, Handle<JSArrayBuffer> memory, | |
851 ErrorThrower* thrower) { | |
852 uint32_t min_mem_pages = static_cast<uint32_t>( | |
853 Smi::cast(compiled_module->get(kMinRequiredMemory))->value()); | |
854 isolate->counters()->wasm_min_mem_pages_count()->AddSample(min_mem_pages); | |
855 // TODO(wasm): re-enable counter for max_mem_pages when we use that field. | |
856 | |
857 if (memory.is_null() && min_mem_pages > 0) { | |
858 memory = AllocateMemory(thrower, isolate, min_mem_pages); | |
859 if (memory.is_null()) { | |
860 return false; | |
861 } | |
862 } | |
863 | |
864 if (!memory.is_null()) { | |
865 instance->SetInternalField(kWasmMemArrayBuffer, *memory); | |
866 Address mem_start = static_cast<Address>(memory->backing_store()); | |
867 uint32_t mem_size = static_cast<uint32_t>(memory->byte_length()->Number()); | |
868 RelocateInstanceCode(instance, mem_start, | |
869 WasmModule::kPageSize * min_mem_pages, mem_size); | |
870 LoadDataSegments(compiled_module, mem_start, mem_size); | |
871 } | |
872 return true; | |
873 } | |
874 | |
875 bool SetupImports(Isolate* isolate, Handle<FixedArray> compiled_module, | |
876 Handle<JSObject> instance, ErrorThrower* thrower, | |
877 Handle<JSReceiver> ffi, CodeStats* stats) { | |
878 //------------------------------------------------------------------------- | |
879 // Compile wrappers to imported functions. | |
880 //------------------------------------------------------------------------- | |
881 std::vector<Handle<Code>> import_code; | |
882 MaybeHandle<FixedArray> maybe_import_data = | |
883 compiled_module->GetValueOrNull<FixedArray>(kImportData); | |
884 if (!maybe_import_data.is_null()) { | |
885 Handle<FixedArray> import_data = maybe_import_data.ToHandleChecked(); | |
886 if (!CompileWrappersToImportedFunctions(isolate, ffi, import_code, | |
887 import_data, thrower)) { | |
888 return false; | |
889 } | |
890 } | |
891 | |
892 stats->Record(import_code); | |
893 | |
894 Handle<FixedArray> code_table = Handle<FixedArray>( | |
895 FixedArray::cast(instance->GetInternalField(kWasmModuleCodeTable))); | |
896 // TODO(mtrofin): get the code off std::vector and on FixedArray, for | |
897 // consistency. | |
898 std::vector<Handle<Code>> function_code(code_table->length()); | |
899 for (int i = 0; i < code_table->length(); ++i) { | |
900 Handle<Code> code = Handle<Code>(Code::cast(code_table->get(i))); | |
901 function_code[i] = code; | |
902 } | |
903 | |
904 instance->SetInternalField(kWasmModuleFunctionTable, Smi::FromInt(0)); | |
905 LinkImports(isolate, function_code, import_code); | |
906 return true; | |
907 } | |
908 | |
909 bool SetupExportsObject(Handle<FixedArray> compiled_module, Isolate* isolate, | |
910 Handle<JSObject> instance, ErrorThrower* thrower, | |
911 CodeStats* stats) { | |
912 Factory* factory = isolate->factory(); | |
913 bool mem_export = | |
914 static_cast<bool>(Smi::cast(compiled_module->get(kExportMem))->value()); | |
915 ModuleOrigin origin = static_cast<ModuleOrigin>( | |
916 Smi::cast(compiled_module->get(kOrigin))->value()); | |
917 | |
918 MaybeHandle<FixedArray> maybe_exports = | |
919 compiled_module->GetValueOrNull<FixedArray>(kExports); | |
920 if (!maybe_exports.is_null() || mem_export) { | |
921 PropertyDescriptor desc; | |
922 desc.set_writable(false); | |
923 | |
924 Handle<JSObject> exports_object = instance; | |
925 if (origin == kWasmOrigin) { | |
926 // Create the "exports" object. | |
927 Handle<JSFunction> object_function = Handle<JSFunction>( | |
928 isolate->native_context()->object_function(), isolate); | |
929 exports_object = factory->NewJSObject(object_function, TENURED); | |
930 Handle<String> exports_name = factory->InternalizeUtf8String("exports"); | |
931 JSObject::AddProperty(instance, exports_name, exports_object, READ_ONLY); | |
932 } | |
933 if (!maybe_exports.is_null()) { | |
934 Handle<FixedArray> exports = maybe_exports.ToHandleChecked(); | |
935 | |
936 int exports_size = exports->length(); | |
937 for (int i = 0; i < exports_size; ++i) { | |
938 if (thrower->error()) return false; | |
939 Handle<JSFunction> function = exports->GetValue<JSFunction>(i); | |
940 stats->Record(function->code()); | |
941 Handle<String> name = | |
942 Handle<String>(String::cast(function->shared()->name())); | |
943 function->SetInternalField(0, *instance); | |
944 | |
945 desc.set_value(function); | |
946 Maybe<bool> status = JSReceiver::DefineOwnProperty( | |
947 isolate, exports_object, name, &desc, Object::THROW_ON_ERROR); | |
948 if (!status.IsJust()) { | |
949 thrower->Error("export of %.*s failed.", name->length(), | |
950 name->ToCString().get()); | |
951 return false; | |
952 } | |
953 } | |
954 } | |
955 if (mem_export) { | |
956 // Export the memory as a named property. | |
957 Handle<String> name = factory->InternalizeUtf8String("memory"); | |
958 Handle<JSArrayBuffer> memory = Handle<JSArrayBuffer>( | |
959 JSArrayBuffer::cast(instance->GetInternalField(kWasmMemArrayBuffer))); | |
960 JSObject::AddProperty(exports_object, name, memory, READ_ONLY); | |
961 } | |
962 } | |
963 return true; | |
964 } | |
965 | |
966 } // namespace | |
967 | |
704 Handle<FixedArray> WasmModule::CompileFunctions(Isolate* isolate) const { | 968 Handle<FixedArray> WasmModule::CompileFunctions(Isolate* isolate) const { |
705 Factory* factory = isolate->factory(); | 969 Factory* factory = isolate->factory(); |
706 ErrorThrower thrower(isolate, "WasmModule::CompileFunctions()"); | 970 ErrorThrower thrower(isolate, "WasmModule::CompileFunctions()"); |
707 | 971 |
708 WasmModuleInstance temp_instance_for_compilation(this); | 972 WasmModuleInstance temp_instance_for_compilation(this); |
709 temp_instance_for_compilation.function_table = | 973 temp_instance_for_compilation.function_table = |
710 BuildFunctionTable(isolate, this); | 974 BuildFunctionTable(isolate, this); |
711 temp_instance_for_compilation.context = isolate->native_context(); | 975 temp_instance_for_compilation.context = isolate->native_context(); |
712 temp_instance_for_compilation.mem_size = GetMinModuleMemSize(this); | 976 temp_instance_for_compilation.mem_size = GetMinModuleMemSize(this); |
713 temp_instance_for_compilation.mem_start = nullptr; | 977 temp_instance_for_compilation.mem_start = nullptr; |
714 temp_instance_for_compilation.globals_start = nullptr; | 978 temp_instance_for_compilation.globals_start = nullptr; |
715 | 979 |
980 HistogramTimerScope wasm_compile_module_time_scope( | |
981 isolate->counters()->wasm_compile_module_time()); | |
982 | |
716 ModuleEnv module_env; | 983 ModuleEnv module_env; |
717 module_env.module = this; | 984 module_env.module = this; |
718 module_env.instance = &temp_instance_for_compilation; | 985 module_env.instance = &temp_instance_for_compilation; |
719 module_env.origin = origin; | 986 module_env.origin = origin; |
720 InitializePlaceholders(factory, &module_env.placeholders, functions.size()); | 987 InitializePlaceholders(factory, &module_env.placeholders, functions.size()); |
721 | 988 |
722 Handle<FixedArray> ret = | 989 Handle<FixedArray> compiled_functions = |
723 factory->NewFixedArray(static_cast<int>(functions.size()), TENURED); | 990 factory->NewFixedArray(static_cast<int>(functions.size()), TENURED); |
724 | 991 |
725 temp_instance_for_compilation.import_code.resize(import_table.size()); | 992 temp_instance_for_compilation.import_code.resize(import_table.size()); |
726 for (uint32_t i = 0; i < import_table.size(); ++i) { | 993 for (uint32_t i = 0; i < import_table.size(); ++i) { |
727 temp_instance_for_compilation.import_code[i] = | 994 temp_instance_for_compilation.import_code[i] = |
728 CreatePlaceholder(factory, i, Code::WASM_TO_JS_FUNCTION); | 995 CreatePlaceholder(factory, i, Code::WASM_TO_JS_FUNCTION); |
729 } | 996 } |
730 isolate->counters()->wasm_functions_per_module()->AddSample( | 997 isolate->counters()->wasm_functions_per_module()->AddSample( |
731 static_cast<int>(functions.size())); | 998 static_cast<int>(functions.size())); |
732 if (FLAG_wasm_num_compilation_tasks != 0) { | 999 if (FLAG_wasm_num_compilation_tasks != 0) { |
733 CompileInParallel(isolate, this, | 1000 CompileInParallel(isolate, this, |
734 temp_instance_for_compilation.function_code, &thrower, | 1001 temp_instance_for_compilation.function_code, &thrower, |
735 &module_env); | 1002 &module_env); |
736 } else { | 1003 } else { |
737 CompileSequentially(isolate, this, | 1004 CompileSequentially(isolate, this, |
738 temp_instance_for_compilation.function_code, &thrower, | 1005 temp_instance_for_compilation.function_code, &thrower, |
739 &module_env); | 1006 &module_env); |
740 } | 1007 } |
741 if (thrower.error()) { | 1008 if (thrower.error()) { |
742 return Handle<FixedArray>::null(); | 1009 return Handle<FixedArray>::null(); |
743 } | 1010 } |
744 | 1011 |
745 LinkModuleFunctions(isolate, temp_instance_for_compilation.function_code); | 1012 LinkModuleFunctions(isolate, temp_instance_for_compilation.function_code); |
746 | 1013 |
747 // At this point, compilation has completed. Update the code table | 1014 // At this point, compilation has completed. Update the code table. |
748 // and record sizes. | |
749 for (size_t i = FLAG_skip_compiling_wasm_funcs; | 1015 for (size_t i = FLAG_skip_compiling_wasm_funcs; |
750 i < temp_instance_for_compilation.function_code.size(); ++i) { | 1016 i < temp_instance_for_compilation.function_code.size(); ++i) { |
751 Code* code = *temp_instance_for_compilation.function_code[i]; | 1017 Code* code = *temp_instance_for_compilation.function_code[i]; |
752 ret->set(static_cast<int>(i), code); | 1018 compiled_functions->set(static_cast<int>(i), code); |
753 } | 1019 } |
754 | 1020 |
755 PopulateFunctionTable(&temp_instance_for_compilation); | 1021 PopulateFunctionTable(&temp_instance_for_compilation); |
756 | 1022 |
1023 // Create the compiled module object, and populate with compiled functions | |
1024 // and information needed at instantiation time. This object needs to be | |
1025 // serializable. Instantiation may occur off a deserialized version of this | |
1026 // object. | |
1027 Handle<FixedArray> ret = | |
1028 factory->NewFixedArray(kCompiledWasmObjectTableSize, TENURED); | |
1029 ret->set(kFunctions, *compiled_functions); | |
1030 | |
1031 Handle<FixedArray> import_data = GetImportsMetadata(factory, this); | |
1032 ret->set(kImportData, *import_data); | |
1033 | |
1034 // Compile export functions. | |
1035 int export_size = static_cast<int>(export_table.size()); | |
1036 Handle<JSFunction> startup_fct; | |
1037 if (export_size > 0) { | |
1038 Handle<FixedArray> exports = factory->NewFixedArray(export_size, TENURED); | |
1039 for (int i = 0; i < export_size; ++i) { | |
1040 const WasmExport& exp = export_table[i]; | |
1041 if (thrower.error()) break; | |
bradnelson
2016/06/27 22:28:47
Why here?, Probably better to bubble out after the
Mircea Trofin
2016/06/28 05:24:21
That's where it was done before. I agree, it's str
| |
1042 WasmName str = GetName(exp.name_offset, exp.name_length); | |
1043 Handle<String> name = factory->InternalizeUtf8String(str); | |
1044 Handle<Code> code = | |
1045 temp_instance_for_compilation.function_code[exp.func_index]; | |
1046 Handle<JSFunction> function = compiler::CompileJSToWasmWrapper( | |
1047 isolate, &module_env, name, code, exp.func_index); | |
1048 exports->set(i, *function); | |
1049 if (exp.func_index == start_function_index) { | |
1050 startup_fct = function; | |
1051 } | |
1052 } | |
1053 ret->set(kExports, *exports); | |
1054 } | |
1055 | |
1056 // Compile startup function, if we haven't already. | |
1057 if (start_function_index >= 0) { | |
1058 HandleScope scope(isolate); | |
1059 if (startup_fct.is_null()) { | |
1060 uint32_t index = static_cast<uint32_t>(start_function_index); | |
1061 Handle<String> name = factory->NewStringFromStaticChars("start"); | |
1062 Handle<Code> code = temp_instance_for_compilation.function_code[index]; | |
1063 startup_fct = compiler::CompileJSToWasmWrapper(isolate, &module_env, name, | |
1064 code, index); | |
1065 } | |
1066 ret->set(kStartupFunction, *startup_fct); | |
1067 } | |
1068 | |
1069 // TODO(wasm): saving the module bytes for debugging is wasteful. We should | |
bradnelson
2016/06/27 22:28:47
saving -> Saving
| |
1070 // consider downloading this on-demand. | |
1071 { | |
1072 size_t module_bytes_len = module_end - module_start; | |
1073 DCHECK_LE(module_bytes_len, static_cast<size_t>(kMaxInt)); | |
1074 Vector<const uint8_t> module_bytes_vec(module_start, | |
1075 static_cast<int>(module_bytes_len)); | |
1076 Handle<String> module_bytes_string = | |
1077 factory->NewStringFromOneByte(module_bytes_vec, TENURED) | |
1078 .ToHandleChecked(); | |
1079 ret->set(kModuleBytes, *module_bytes_string); | |
1080 } | |
1081 | |
1082 Handle<ByteArray> function_name_table = | |
1083 BuildFunctionNamesTable(isolate, module_env.module); | |
1084 ret->set(kFunctionNameTable, *function_name_table); | |
1085 ret->set(kMinRequiredMemory, Smi::FromInt(min_mem_pages)); | |
1086 SaveDataSegmentInfo(factory, this, ret); | |
1087 ret->set(kGlobalsSize, Smi::FromInt(globals_size)); | |
1088 ret->set(kExportMem, Smi::FromInt(mem_export)); | |
1089 ret->set(kOrigin, Smi::FromInt(origin)); | |
757 return ret; | 1090 return ret; |
758 } | 1091 } |
759 | 1092 |
760 // Instantiates a wasm module as a JSObject. | 1093 // Instantiates a wasm module as a JSObject. |
761 // * allocates a backing store of {mem_size} bytes. | 1094 // * allocates a backing store of {mem_size} bytes. |
762 // * installs a named property "memory" for that buffer if exported | 1095 // * installs a named property "memory" for that buffer if exported |
763 // * installs named properties on the object for exported functions | 1096 // * installs named properties on the object for exported functions |
764 // * compiles wasm code to machine code | 1097 // * compiles wasm code to machine code |
765 MaybeHandle<JSObject> WasmModule::Instantiate( | 1098 MaybeHandle<JSObject> WasmModule::Instantiate( |
766 Isolate* isolate, Handle<JSReceiver> ffi, | 1099 Isolate* isolate, Handle<FixedArray> compiled_module, |
767 Handle<JSArrayBuffer> memory) const { | 1100 Handle<JSReceiver> ffi, Handle<JSArrayBuffer> memory) { |
768 HistogramTimerScope wasm_instantiate_module_time_scope( | 1101 HistogramTimerScope wasm_instantiate_module_time_scope( |
769 isolate->counters()->wasm_instantiate_module_time()); | 1102 isolate->counters()->wasm_instantiate_module_time()); |
770 ErrorThrower thrower(isolate, "WasmModule::Instantiate()"); | 1103 ErrorThrower thrower(isolate, "WasmModule::Instantiate()"); |
771 Factory* factory = isolate->factory(); | 1104 Factory* factory = isolate->factory(); |
772 | 1105 |
773 //------------------------------------------------------------------------- | 1106 CodeStats code_stats; |
774 // Allocate the instance and its JS counterpart. | 1107 // These fields are compulsory. |
775 //------------------------------------------------------------------------- | 1108 Handle<FixedArray> code_table = |
1109 compiled_module->GetValue<FixedArray>(kFunctions); | |
1110 | |
1111 code_stats.Record(code_table); | |
1112 | |
1113 MaybeHandle<JSObject> nothing; | |
1114 | |
776 Handle<Map> map = factory->NewMap( | 1115 Handle<Map> map = factory->NewMap( |
777 JS_OBJECT_TYPE, | 1116 JS_OBJECT_TYPE, |
778 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); | 1117 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); |
779 WasmModuleInstance instance(this); | 1118 Handle<JSObject> js_object = factory->NewJSObjectFromMap(map, TENURED); |
780 instance.context = isolate->native_context(); | 1119 js_object->SetInternalField(kWasmModuleCodeTable, *code_table); |
781 instance.js_object = factory->NewJSObjectFromMap(map, TENURED); | |
782 | 1120 |
783 Handle<FixedArray> code_table = CompileFunctions(isolate); | 1121 if (!(SetupInstanceHeap(isolate, compiled_module, js_object, memory, |
784 if (code_table.is_null()) return Handle<JSObject>::null(); | 1122 &thrower) && |
785 | 1123 SetupGlobals(isolate, compiled_module, js_object, &thrower) && |
786 instance.js_object->SetInternalField(kWasmModuleCodeTable, *code_table); | 1124 SetupImports(isolate, compiled_module, js_object, &thrower, ffi, |
787 size_t module_bytes_len = | 1125 &code_stats) && |
788 instance.module->module_end - instance.module->module_start; | 1126 SetupExportsObject(compiled_module, isolate, js_object, &thrower, |
789 DCHECK_LE(module_bytes_len, static_cast<size_t>(kMaxInt)); | 1127 &code_stats))) { |
790 Vector<const uint8_t> module_bytes_vec(instance.module->module_start, | 1128 return nothing; |
791 static_cast<int>(module_bytes_len)); | |
792 Handle<String> module_bytes_string = | |
793 factory->NewStringFromOneByte(module_bytes_vec, TENURED) | |
794 .ToHandleChecked(); | |
795 instance.js_object->SetInternalField(kWasmModuleBytesString, | |
796 *module_bytes_string); | |
797 | |
798 for (uint32_t i = 0; i < functions.size(); ++i) { | |
799 Handle<Code> code = Handle<Code>(Code::cast(code_table->get(i))); | |
800 instance.function_code[i] = code; | |
801 } | 1129 } |
802 | 1130 |
803 //------------------------------------------------------------------------- | 1131 SetDebugSupport(factory, compiled_module, js_object); |
804 // Allocate and initialize the linear memory. | |
805 //------------------------------------------------------------------------- | |
806 isolate->counters()->wasm_min_mem_pages_count()->AddSample( | |
807 instance.module->min_mem_pages); | |
808 isolate->counters()->wasm_max_mem_pages_count()->AddSample( | |
809 instance.module->max_mem_pages); | |
810 if (memory.is_null()) { | |
811 if (!AllocateMemory(&thrower, isolate, &instance)) { | |
812 return MaybeHandle<JSObject>(); | |
813 } | |
814 } else { | |
815 SetMemory(&instance, memory); | |
816 } | |
817 instance.js_object->SetInternalField(kWasmMemArrayBuffer, | |
818 *instance.mem_buffer); | |
819 LoadDataSegments(this, instance.mem_start, instance.mem_size); | |
820 | 1132 |
821 //------------------------------------------------------------------------- | 1133 // Run the start function if one was specified. |
822 // Allocate the globals area if necessary. | 1134 MaybeHandle<JSFunction> maybe_startup_fct = |
823 //------------------------------------------------------------------------- | 1135 compiled_module->GetValueOrNull<JSFunction>(kStartupFunction); |
824 if (!AllocateGlobals(&thrower, isolate, &instance)) { | 1136 if (!maybe_startup_fct.is_null()) { |
825 return MaybeHandle<JSObject>(); | 1137 HandleScope scope(isolate); |
826 } | 1138 Handle<JSFunction> startup_fct = maybe_startup_fct.ToHandleChecked(); |
827 if (!instance.globals_buffer.is_null()) { | 1139 code_stats.Record(startup_fct->code()); |
828 instance.js_object->SetInternalField(kWasmGlobalsArrayBuffer, | 1140 startup_fct->SetInternalField(0, *js_object); |
829 *instance.globals_buffer); | 1141 // Call the JS function. |
830 } | 1142 Handle<Object> undefined = isolate->factory()->undefined_value(); |
1143 MaybeHandle<Object> retval = | |
1144 Execution::Call(isolate, startup_fct, undefined, 0, nullptr); | |
831 | 1145 |
832 HistogramTimerScope wasm_compile_module_time_scope( | 1146 if (retval.is_null()) { |
833 isolate->counters()->wasm_compile_module_time()); | 1147 thrower.Error("WASM.instantiateModule(): start function failed"); |
834 | 1148 return nothing; |
835 ModuleEnv module_env; | |
836 module_env.module = this; | |
837 module_env.instance = &instance; | |
838 module_env.origin = origin; | |
839 | |
840 //------------------------------------------------------------------------- | |
841 // Compile wrappers to imported functions. | |
842 //------------------------------------------------------------------------- | |
843 if (!CompileWrappersToImportedFunctions(isolate, this, ffi, &instance, | |
844 &thrower, factory)) { | |
845 return MaybeHandle<JSObject>(); | |
846 } | |
847 | |
848 // If FLAG_print_wasm_code_size is set, this aggregates the sum of all code | |
849 // objects created for this module. | |
850 // TODO(titzer): switch this to TRACE_EVENT | |
851 CodeStats code_stats; | |
852 if (FLAG_print_wasm_code_size) { | |
853 for (Handle<Code> c : instance.function_code) code_stats.Record(*c); | |
854 for (Handle<Code> c : instance.import_code) code_stats.Record(*c); | |
855 } | |
856 | |
857 { | |
858 instance.js_object->SetInternalField(kWasmModuleFunctionTable, | |
859 Smi::FromInt(0)); | |
860 LinkImports(isolate, instance.function_code, instance.import_code); | |
861 | |
862 SetDeoptimizationData(factory, instance.js_object, instance.function_code); | |
863 | |
864 //------------------------------------------------------------------------- | |
865 // Create and populate the exports object. | |
866 //------------------------------------------------------------------------- | |
867 if (export_table.size() > 0 || mem_export) { | |
868 Handle<JSObject> exports_object; | |
869 if (origin == kWasmOrigin) { | |
870 // Create the "exports" object. | |
871 Handle<JSFunction> object_function = Handle<JSFunction>( | |
872 isolate->native_context()->object_function(), isolate); | |
873 exports_object = factory->NewJSObject(object_function, TENURED); | |
874 Handle<String> exports_name = factory->InternalizeUtf8String("exports"); | |
875 JSObject::AddProperty(instance.js_object, exports_name, exports_object, | |
876 READ_ONLY); | |
877 } else { | |
878 // Just export the functions directly on the object returned. | |
879 exports_object = instance.js_object; | |
880 } | |
881 | |
882 PropertyDescriptor desc; | |
883 desc.set_writable(false); | |
884 | |
885 // Compile wrappers and add them to the exports object. | |
886 for (const WasmExport& exp : export_table) { | |
887 if (thrower.error()) break; | |
888 WasmName str = GetName(exp.name_offset, exp.name_length); | |
889 Handle<String> name = factory->InternalizeUtf8String(str); | |
890 Handle<Code> code = instance.function_code[exp.func_index]; | |
891 Handle<JSFunction> function = compiler::CompileJSToWasmWrapper( | |
892 isolate, &module_env, name, code, instance.js_object, | |
893 exp.func_index); | |
894 if (FLAG_print_wasm_code_size) { | |
895 code_stats.Record(function->code()); | |
896 } | |
897 desc.set_value(function); | |
898 Maybe<bool> status = JSReceiver::DefineOwnProperty( | |
899 isolate, exports_object, name, &desc, Object::THROW_ON_ERROR); | |
900 if (!status.IsJust()) { | |
901 thrower.Error("export of %.*s failed.", str.length(), str.start()); | |
902 break; | |
903 } | |
904 } | |
905 | |
906 if (mem_export) { | |
907 // Export the memory as a named property. | |
908 Handle<String> name = factory->InternalizeUtf8String("memory"); | |
909 JSObject::AddProperty(exports_object, name, instance.mem_buffer, | |
910 READ_ONLY); | |
911 } | |
912 } | 1149 } |
913 } | 1150 } |
914 | 1151 |
915 if (FLAG_print_wasm_code_size) { | 1152 code_stats.Report(); |
916 code_stats.Report(); | |
917 } | |
918 //------------------------------------------------------------------------- | |
919 // Attach the function name table. | |
920 //------------------------------------------------------------------------- | |
921 Handle<ByteArray> function_name_table = | |
922 BuildFunctionNamesTable(isolate, module_env.module); | |
923 instance.js_object->SetInternalField(kWasmFunctionNamesArray, | |
924 *function_name_table); | |
925 | 1153 |
926 // Run the start function if one was specified. | 1154 DCHECK(wasm::IsWasmObject(*js_object)); |
927 if (this->start_function_index >= 0) { | 1155 return js_object; |
928 HandleScope scope(isolate); | |
929 uint32_t index = static_cast<uint32_t>(this->start_function_index); | |
930 Handle<String> name = isolate->factory()->NewStringFromStaticChars("start"); | |
931 Handle<Code> code = instance.function_code[index]; | |
932 Handle<JSFunction> jsfunc = compiler::CompileJSToWasmWrapper( | |
933 isolate, &module_env, name, code, instance.js_object, index); | |
934 | |
935 // Call the JS function. | |
936 Handle<Object> undefined = isolate->factory()->undefined_value(); | |
937 MaybeHandle<Object> retval = | |
938 Execution::Call(isolate, jsfunc, undefined, 0, nullptr); | |
939 | |
940 if (retval.is_null()) { | |
941 thrower.Error("WASM.instantiateModule(): start function failed"); | |
942 } | |
943 } | |
944 return instance.js_object; | |
945 } | 1156 } |
946 | 1157 |
947 // TODO(mtrofin): remove this once we move to WASM_DIRECT_CALL | 1158 // TODO(mtrofin): remove this once we move to WASM_DIRECT_CALL |
948 Handle<Code> ModuleEnv::GetCodeOrPlaceholder(uint32_t index) const { | 1159 Handle<Code> ModuleEnv::GetCodeOrPlaceholder(uint32_t index) const { |
949 DCHECK(IsValidFunction(index)); | 1160 DCHECK(IsValidFunction(index)); |
950 if (!placeholders.empty()) return placeholders[index]; | 1161 if (!placeholders.empty()) return placeholders[index]; |
951 DCHECK_NOT_NULL(instance); | 1162 DCHECK_NOT_NULL(instance); |
952 return instance->function_code[index]; | 1163 return instance->function_code[index]; |
953 } | 1164 } |
954 | 1165 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
989 Handle<Object> name_or_null = | 1200 Handle<Object> name_or_null = |
990 GetWasmFunctionNameOrNull(isolate, wasm, func_index); | 1201 GetWasmFunctionNameOrNull(isolate, wasm, func_index); |
991 if (!name_or_null->IsNull(isolate)) { | 1202 if (!name_or_null->IsNull(isolate)) { |
992 return Handle<String>::cast(name_or_null); | 1203 return Handle<String>::cast(name_or_null); |
993 } | 1204 } |
994 return isolate->factory()->NewStringFromStaticChars("<WASM UNNAMED>"); | 1205 return isolate->factory()->NewStringFromStaticChars("<WASM UNNAMED>"); |
995 } | 1206 } |
996 | 1207 |
997 bool IsWasmObject(Object* object) { | 1208 bool IsWasmObject(Object* object) { |
998 if (!object->IsJSObject()) return false; | 1209 if (!object->IsJSObject()) return false; |
1210 | |
999 JSObject* obj = JSObject::cast(object); | 1211 JSObject* obj = JSObject::cast(object); |
1000 if (obj->GetInternalFieldCount() != kWasmModuleInternalFieldCount || | 1212 Isolate* isolate = obj->GetIsolate(); |
1001 !obj->GetInternalField(kWasmModuleCodeTable)->IsFixedArray() || | 1213 if (obj->GetInternalFieldCount() != kWasmModuleInternalFieldCount) { |
1002 !obj->GetInternalField(kWasmMemArrayBuffer)->IsJSArrayBuffer() || | |
1003 !obj->GetInternalField(kWasmFunctionNamesArray)->IsByteArray() || | |
1004 !obj->GetInternalField(kWasmModuleBytesString)->IsSeqOneByteString()) { | |
1005 return false; | 1214 return false; |
1006 } | 1215 } |
1007 DisallowHeapAllocation no_gc; | |
1008 SeqOneByteString* bytes = | |
1009 SeqOneByteString::cast(obj->GetInternalField(kWasmModuleBytesString)); | |
1010 if (bytes->length() < 4) return false; | |
1011 if (memcmp(bytes->GetChars(), "\0asm", 4)) return false; | |
1012 | 1216 |
1013 // All checks passed. | 1217 Object* mem = obj->GetInternalField(kWasmMemArrayBuffer); |
1014 return true; | 1218 if (obj->GetInternalField(kWasmModuleCodeTable)->IsFixedArray() && |
1219 (mem->IsUndefined(isolate) || mem->IsJSArrayBuffer()) && | |
1220 obj->GetInternalField(kWasmFunctionNamesArray)->IsByteArray()) { | |
1221 Object* debug_bytes = obj->GetInternalField(kWasmModuleBytesString); | |
1222 if (!debug_bytes->IsUndefined(isolate)) { | |
1223 if (!debug_bytes->IsSeqOneByteString()) { | |
1224 return false; | |
1225 } | |
1226 DisallowHeapAllocation no_gc; | |
1227 SeqOneByteString* bytes = SeqOneByteString::cast(debug_bytes); | |
1228 if (bytes->length() < 4) return false; | |
1229 if (memcmp(bytes->GetChars(), "\0asm", 4)) return false; | |
1230 // All checks passed. | |
1231 } | |
1232 return true; | |
1233 } | |
1234 return false; | |
1015 } | 1235 } |
1016 | 1236 |
1017 SeqOneByteString* GetWasmBytes(JSObject* wasm) { | 1237 SeqOneByteString* GetWasmBytes(JSObject* wasm) { |
1018 return SeqOneByteString::cast(wasm->GetInternalField(kWasmModuleBytesString)); | 1238 return SeqOneByteString::cast(wasm->GetInternalField(kWasmModuleBytesString)); |
1019 } | 1239 } |
1020 | 1240 |
1021 WasmDebugInfo* GetDebugInfo(JSObject* wasm) { | 1241 WasmDebugInfo* GetDebugInfo(JSObject* wasm) { |
1022 Object* info = wasm->GetInternalField(kWasmDebugInfo); | 1242 Object* info = wasm->GetInternalField(kWasmDebugInfo); |
1023 if (!info->IsUndefined(wasm->GetIsolate())) return WasmDebugInfo::cast(info); | 1243 if (!info->IsUndefined(wasm->GetIsolate())) return WasmDebugInfo::cast(info); |
1024 Handle<WasmDebugInfo> new_info = WasmDebugInfo::New(handle(wasm)); | 1244 Handle<WasmDebugInfo> new_info = WasmDebugInfo::New(handle(wasm)); |
(...skipping 24 matching lines...) Expand all Loading... | |
1049 } | 1269 } |
1050 | 1270 |
1051 if (module->import_table.size() > 0) { | 1271 if (module->import_table.size() > 0) { |
1052 thrower.Error("Not supported: module has imports."); | 1272 thrower.Error("Not supported: module has imports."); |
1053 } | 1273 } |
1054 if (module->export_table.size() == 0) { | 1274 if (module->export_table.size() == 0) { |
1055 thrower.Error("Not supported: module has no exports."); | 1275 thrower.Error("Not supported: module has no exports."); |
1056 } | 1276 } |
1057 | 1277 |
1058 if (thrower.error()) return -1; | 1278 if (thrower.error()) return -1; |
1279 Handle<FixedArray> compiled_module = module->CompileFunctions(isolate); | |
1059 | 1280 |
1060 Handle<JSObject> instance = | 1281 Handle<JSObject> instance = |
1061 module | 1282 module |
1062 ->Instantiate(isolate, Handle<JSReceiver>::null(), | 1283 ->Instantiate(isolate, compiled_module, Handle<JSReceiver>::null(), |
1063 Handle<JSArrayBuffer>::null()) | 1284 Handle<JSArrayBuffer>::null()) |
1064 .ToHandleChecked(); | 1285 .ToHandleChecked(); |
1065 | 1286 |
1066 Handle<Name> exports = isolate->factory()->InternalizeUtf8String("exports"); | 1287 Handle<Name> exports = isolate->factory()->InternalizeUtf8String("exports"); |
1067 Handle<JSObject> exports_object = Handle<JSObject>::cast( | 1288 Handle<JSObject> exports_object = Handle<JSObject>::cast( |
1068 JSObject::GetProperty(instance, exports).ToHandleChecked()); | 1289 JSObject::GetProperty(instance, exports).ToHandleChecked()); |
1069 Handle<Name> main_name = isolate->factory()->NewStringFromStaticChars("main"); | 1290 Handle<Name> main_name = isolate->factory()->NewStringFromStaticChars("main"); |
1070 PropertyDescriptor desc; | 1291 PropertyDescriptor desc; |
1071 Maybe<bool> property_found = JSReceiver::GetOwnPropertyDescriptor( | 1292 Maybe<bool> property_found = JSReceiver::GetOwnPropertyDescriptor( |
1072 isolate, exports_object, main_name, &desc); | 1293 isolate, exports_object, main_name, &desc); |
(...skipping 19 matching lines...) Expand all Loading... | |
1092 return static_cast<int32_t>(HeapNumber::cast(*result)->value()); | 1313 return static_cast<int32_t>(HeapNumber::cast(*result)->value()); |
1093 } | 1314 } |
1094 thrower.Error("WASM.compileRun() failed: Return value should be number"); | 1315 thrower.Error("WASM.compileRun() failed: Return value should be number"); |
1095 return -1; | 1316 return -1; |
1096 } | 1317 } |
1097 | 1318 |
1098 } // namespace testing | 1319 } // namespace testing |
1099 } // namespace wasm | 1320 } // namespace wasm |
1100 } // namespace internal | 1321 } // namespace internal |
1101 } // namespace v8 | 1322 } // namespace v8 |
OLD | NEW |