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