Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(402)

Side by Side Diff: src/wasm/wasm-module.cc

Issue 2094563002: [wasm] Complete separation of compilation and instantiation (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Incorporated Feedback Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/wasm/wasm-module.h ('k') | test/cctest/wasm/wasm-run-utils.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // For now, each field is expected to have the type commented by its side.
129 // The elements typed as "maybe" are optional. The others are mandatory. Since
130 // the compiled module is either obtained from the current v8 instance, or from
131 // a snapshot produced by a compatible (==identical) v8 instance, we simply
132 // fail at instantiation time, in the face of invalid data.
133 enum CompiledWasmObjectFields {
134 kFunctions, // FixedArray of Code
135 kImportData, // maybe FixedArray of FixedArray respecting the
136 // WasmImportMetadata structure.
137 kExports, // maybe FixedArray of JSFunction
138 kStartupFunction, // maybe JSFunction
139 kModuleBytes, // maybe String
140 kFunctionNameTable, // maybe ByteArray
141 kMinRequiredMemory, // Smi. an uint32_t
142 // The following 2 are either together present or absent:
143 kDataSegmentsInfo, // maybe FixedArray of FixedArray respecting the
144 // WasmSegmentInfo structure
145 kDataSegments, // maybe ByteArray.
146
147 kGlobalsSize, // Smi. an uint32_t
148 kExportMem, // Smi. bool
149 kOrigin, // Smi. ModuleOrigin
150 kCompiledWasmObjectTableSize // Sentinel value.
151 };
152
153 enum WasmImportMetadata {
154 kModuleName, // String
155 kFunctionName, // maybe String
156 kOutputCount, // Smi. an uint32_t
157 kSignature, // ByteArray. A copy of the data in FunctionSig
158 kWasmImportDataTableSize // Sentinel value.
159 };
160
161 enum WasmSegmentInfo {
162 kDestAddr, // Smi. an uint32_t
163 kSourceSize, // Smi. an uint32_t
164 kWasmSegmentInfoSize // Sentinel value.
165 };
166
127 uint32_t GetMinModuleMemSize(const WasmModule* module) { 167 uint32_t GetMinModuleMemSize(const WasmModule* module) {
128 return WasmModule::kPageSize * module->min_mem_pages; 168 return WasmModule::kPageSize * module->min_mem_pages;
129 } 169 }
130 170
131 void LoadDataSegments(const WasmModule* module, byte* mem_addr, 171 void LoadDataSegments(Handle<FixedArray> compiled_module, Address mem_addr,
132 size_t mem_size) { 172 size_t mem_size) {
133 for (const WasmDataSegment& segment : module->data_segments) { 173 MaybeHandle<ByteArray> maybe_data =
134 if (!segment.init) continue; 174 compiled_module->GetValue<ByteArray>(kDataSegments);
135 if (!segment.source_size) continue; 175 MaybeHandle<FixedArray> maybe_segments =
136 CHECK_LT(segment.dest_addr, mem_size); 176 compiled_module->GetValue<FixedArray>(kDataSegmentsInfo);
137 CHECK_LE(segment.source_size, mem_size); 177
138 CHECK_LE(segment.dest_addr + segment.source_size, mem_size); 178 // We either have both or neither.
139 byte* addr = mem_addr + segment.dest_addr; 179 CHECK(maybe_data.is_null() == maybe_segments.is_null());
140 memcpy(addr, module->module_start + segment.source_offset, 180 // If we have neither, we're done.
141 segment.source_size); 181 if (maybe_data.is_null()) return;
182
183 Handle<ByteArray> data = maybe_data.ToHandleChecked();
184 Handle<FixedArray> segments = maybe_segments.ToHandleChecked();
185
186 uint32_t last_extraction_pos = 0;
187 for (int i = 0; i < segments->length(); ++i) {
188 Handle<ByteArray> segment =
189 Handle<ByteArray>(ByteArray::cast(segments->get(i)));
190 uint32_t dest_addr = static_cast<uint32_t>(segment->get_int(kDestAddr));
191 uint32_t source_size = static_cast<uint32_t>(segment->get_int(kSourceSize));
192 CHECK_LT(dest_addr, mem_size);
193 CHECK_LE(source_size, mem_size);
194 CHECK_LE(dest_addr, mem_size - source_size);
195 byte* addr = mem_addr + dest_addr;
196 data->copy_out(last_extraction_pos, addr, source_size);
197 last_extraction_pos += source_size;
142 } 198 }
143 } 199 }
144 200
201 void SaveDataSegmentInfo(Factory* factory, const WasmModule* module,
202 Handle<FixedArray> compiled_module) {
203 Handle<FixedArray> segments = factory->NewFixedArray(
204 static_cast<int>(module->data_segments.size()), TENURED);
205 uint32_t data_size = 0;
206 for (const WasmDataSegment& segment : module->data_segments) {
207 if (!segment.init) continue;
208 if (segment.source_size == 0) continue;
209 data_size += segment.source_size;
210 }
211 Handle<ByteArray> data = factory->NewByteArray(data_size, TENURED);
212
213 uint32_t last_insertion_pos = 0;
214 for (uint32_t i = 0; i < module->data_segments.size(); ++i) {
215 const WasmDataSegment& segment = module->data_segments[i];
216 if (!segment.init) continue;
217 if (segment.source_size == 0) continue;
218 Handle<ByteArray> js_segment =
219 factory->NewByteArray(kWasmSegmentInfoSize * sizeof(uint32_t), TENURED);
220 js_segment->set_int(kDestAddr, segment.dest_addr);
221 js_segment->set_int(kSourceSize, segment.source_size);
222 segments->set(i, *js_segment);
223 data->copy_in(last_insertion_pos,
224 module->module_start + segment.source_offset,
225 segment.source_size);
226 last_insertion_pos += segment.source_size;
227 }
228 compiled_module->set(kDataSegmentsInfo, *segments);
229 compiled_module->set(kDataSegments, *data);
230 }
231
145 Handle<FixedArray> BuildFunctionTable(Isolate* isolate, 232 Handle<FixedArray> BuildFunctionTable(Isolate* isolate,
146 const WasmModule* module) { 233 const WasmModule* module) {
147 // Compute the size of the indirect function table 234 // Compute the size of the indirect function table
148 uint32_t table_size = module->FunctionTableSize(); 235 uint32_t table_size = module->FunctionTableSize();
149 if (table_size == 0) { 236 if (table_size == 0) {
150 return Handle<FixedArray>::null(); 237 return Handle<FixedArray>::null();
151 } 238 }
152 239
153 Handle<FixedArray> fixed = isolate->factory()->NewFixedArray(2 * table_size); 240 Handle<FixedArray> fixed = isolate->factory()->NewFixedArray(2 * table_size);
154 for (uint32_t i = 0; 241 for (uint32_t i = 0;
155 i < static_cast<uint32_t>(module->function_table.size()); 242 i < static_cast<uint32_t>(module->function_table.size());
156 ++i) { 243 ++i) {
157 const WasmFunction* function = 244 const WasmFunction* function =
158 &module->functions[module->function_table[i]]; 245 &module->functions[module->function_table[i]];
159 fixed->set(i, Smi::FromInt(function->sig_index)); 246 fixed->set(i, Smi::FromInt(function->sig_index));
160 } 247 }
161 return fixed; 248 return fixed;
162 } 249 }
163 250
164 Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size, 251 Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size) {
165 byte** backing_store) {
166 *backing_store = nullptr;
167 if (size > (WasmModule::kMaxMemPages * WasmModule::kPageSize)) { 252 if (size > (WasmModule::kMaxMemPages * WasmModule::kPageSize)) {
168 // TODO(titzer): lift restriction on maximum memory allocated here. 253 // TODO(titzer): lift restriction on maximum memory allocated here.
169 return Handle<JSArrayBuffer>::null(); 254 return Handle<JSArrayBuffer>::null();
170 } 255 }
171 void* memory = isolate->array_buffer_allocator()->Allocate(size); 256 void* memory = isolate->array_buffer_allocator()->Allocate(size);
172 if (memory == nullptr) { 257 if (memory == nullptr) {
173 return Handle<JSArrayBuffer>::null(); 258 return Handle<JSArrayBuffer>::null();
174 } 259 }
175 260
176 *backing_store = reinterpret_cast<byte*>(memory);
177
178 #if DEBUG 261 #if DEBUG
179 // Double check the API allocator actually zero-initialized the memory. 262 // Double check the API allocator actually zero-initialized the memory.
180 byte* bytes = reinterpret_cast<byte*>(*backing_store); 263 const byte* bytes = reinterpret_cast<const byte*>(memory);
181 for (size_t i = 0; i < size; ++i) { 264 for (size_t i = 0; i < size; ++i) {
182 DCHECK_EQ(0, bytes[i]); 265 DCHECK_EQ(0, bytes[i]);
183 } 266 }
184 #endif 267 #endif
185 268
186 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); 269 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer();
187 JSArrayBuffer::Setup(buffer, isolate, false, memory, static_cast<int>(size)); 270 JSArrayBuffer::Setup(buffer, isolate, false, memory, static_cast<int>(size));
188 buffer->set_is_neuterable(false); 271 buffer->set_is_neuterable(false);
189 return buffer; 272 return buffer;
190 } 273 }
191 274
192 void RelocateInstanceCode(WasmModuleInstance* instance) { 275 void RelocateInstanceCode(Handle<JSObject> instance, Address start,
193 for (uint32_t i = 0; i < instance->function_code.size(); ++i) { 276 uint32_t prev_size, uint32_t new_size) {
194 Handle<Code> function = instance->function_code[i]; 277 Handle<FixedArray> functions = Handle<FixedArray>(
278 FixedArray::cast(instance->GetInternalField(kWasmModuleCodeTable)));
279 for (int i = 0; i < functions->length(); ++i) {
280 Handle<Code> function = Handle<Code>(Code::cast(functions->get(i)));
195 AllowDeferredHandleDereference embedding_raw_address; 281 AllowDeferredHandleDereference embedding_raw_address;
196 int mask = (1 << RelocInfo::WASM_MEMORY_REFERENCE) | 282 int mask = (1 << RelocInfo::WASM_MEMORY_REFERENCE) |
197 (1 << RelocInfo::WASM_MEMORY_SIZE_REFERENCE); 283 (1 << RelocInfo::WASM_MEMORY_SIZE_REFERENCE);
198 for (RelocIterator it(*function, mask); !it.done(); it.next()) { 284 for (RelocIterator it(*function, mask); !it.done(); it.next()) {
199 it.rinfo()->update_wasm_memory_reference( 285 it.rinfo()->update_wasm_memory_reference(nullptr, start, prev_size,
200 nullptr, instance->mem_start, GetMinModuleMemSize(instance->module), 286 new_size);
201 static_cast<uint32_t>(instance->mem_size));
202 } 287 }
203 } 288 }
204 } 289 }
205 290
206 // Set the memory for a module instance to be the {memory} array buffer. 291 // Allocate memory for a module instance as a new JSArrayBuffer.
207 void SetMemory(WasmModuleInstance* instance, Handle<JSArrayBuffer> memory) { 292 Handle<JSArrayBuffer> AllocateMemory(ErrorThrower* thrower, Isolate* isolate,
208 memory->set_is_neuterable(false); 293 uint32_t min_mem_pages) {
209 instance->mem_start = reinterpret_cast<byte*>(memory->backing_store()); 294 if (min_mem_pages > WasmModule::kMaxMemPages) {
210 instance->mem_size = memory->byte_length()->Number(); 295 thrower->Error("Out of memory: wasm memory too large");
211 instance->mem_buffer = memory; 296 return Handle<JSArrayBuffer>::null();
212 RelocateInstanceCode(instance); 297 }
298 Handle<JSArrayBuffer> mem_buffer =
299 NewArrayBuffer(isolate, min_mem_pages * WasmModule::kPageSize);
300
301 if (mem_buffer.is_null()) {
302 thrower->Error("Out of memory: wasm memory");
303 }
304 return mem_buffer;
213 } 305 }
214 306
215 // Allocate memory for a module instance as a new JSArrayBuffer. 307 void RelocateGlobals(Handle<JSObject> instance, Address globals_start) {
216 bool AllocateMemory(ErrorThrower* thrower, Isolate* isolate, 308 Handle<FixedArray> functions = Handle<FixedArray>(
217 WasmModuleInstance* instance) { 309 FixedArray::cast(instance->GetInternalField(kWasmModuleCodeTable)));
218 DCHECK(instance->module); 310 uint32_t function_count = static_cast<uint32_t>(functions->length());
219 DCHECK(instance->mem_buffer.is_null()); 311 for (uint32_t i = 0; i < function_count; ++i) {
220 312 Handle<Code> function = Handle<Code>(Code::cast(functions->get(i)));
221 if (instance->module->min_mem_pages > WasmModule::kMaxMemPages) { 313 AllowDeferredHandleDereference embedding_raw_address;
222 thrower->Error("Out of memory: wasm memory too large"); 314 int mask = 1 << RelocInfo::WASM_GLOBAL_REFERENCE;
223 return false; 315 for (RelocIterator it(*function, mask); !it.done(); it.next()) {
224 } 316 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 } 317 }
258 } 318 }
259 return true;
260 } 319 }
261 320
262 Handle<Code> CreatePlaceholder(Factory* factory, uint32_t index, 321 Handle<Code> CreatePlaceholder(Factory* factory, uint32_t index,
263 Code::Kind kind) { 322 Code::Kind kind) {
264 // Create a placeholder code object and encode the corresponding index in 323 // Create a placeholder code object and encode the corresponding index in
265 // the {constant_pool_offset} field of the code object. 324 // the {constant_pool_offset} field of the code object.
266 // TODO(titzer): placeholder code objects are somewhat dangerous. 325 // TODO(titzer): placeholder code objects are somewhat dangerous.
267 static byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions. 326 static byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions.
268 static CodeDesc desc = {buffer, 8, 8, 0, 0, nullptr, 0, nullptr}; 327 static CodeDesc desc = {buffer, 8, 8, 0, 0, nullptr, 0, nullptr};
269 Handle<Code> code = factory->NewCode(desc, Code::KindField::encode(kind), 328 Handle<Code> code = factory->NewCode(desc, Code::KindField::encode(kind),
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 min_mem_pages(0), 403 min_mem_pages(0),
345 max_mem_pages(0), 404 max_mem_pages(0),
346 mem_export(false), 405 mem_export(false),
347 mem_external(false), 406 mem_external(false),
348 start_function_index(-1), 407 start_function_index(-1),
349 origin(kWasmOrigin), 408 origin(kWasmOrigin),
350 globals_size(0), 409 globals_size(0),
351 indirect_table_size(0), 410 indirect_table_size(0),
352 pending_tasks(new base::Semaphore(0)) {} 411 pending_tasks(new base::Semaphore(0)) {}
353 412
354 static MaybeHandle<JSFunction> ReportFFIError(ErrorThrower& thrower, 413 static MaybeHandle<JSFunction> ReportFFIError(
355 const char* error, uint32_t index, 414 ErrorThrower& thrower, const char* error, uint32_t index,
356 wasm::WasmName module_name, 415 Handle<String> module_name, MaybeHandle<String> function_name) {
357 wasm::WasmName function_name) { 416 if (!function_name.is_null()) {
358 if (!function_name.is_empty()) { 417 Handle<String> function_name_handle = function_name.ToHandleChecked();
359 thrower.Error("Import #%d module=\"%.*s\" function=\"%.*s\" error: %s", 418 thrower.Error("Import #%d module=\"%.*s\" function=\"%.*s\" error: %s",
360 index, module_name.length(), module_name.start(), 419 index, module_name->length(), module_name->ToCString().get(),
361 function_name.length(), function_name.start(), error); 420 function_name_handle->length(),
421 function_name_handle->ToCString().get(), error);
362 } else { 422 } else {
363 thrower.Error("Import #%d module=\"%.*s\" error: %s", index, 423 thrower.Error("Import #%d module=\"%.*s\" error: %s", index,
364 module_name.length(), module_name.start(), error); 424 module_name->length(), module_name->ToCString().get(), error);
365 } 425 }
366 thrower.Error("Import "); 426 thrower.Error("Import ");
367 return MaybeHandle<JSFunction>(); 427 return MaybeHandle<JSFunction>();
368 } 428 }
369 429
370 static MaybeHandle<JSFunction> LookupFunction( 430 static MaybeHandle<JSFunction> LookupFunction(
371 ErrorThrower& thrower, Factory* factory, Handle<JSReceiver> ffi, 431 ErrorThrower& thrower, Factory* factory, Handle<JSReceiver> ffi,
372 uint32_t index, wasm::WasmName module_name, wasm::WasmName function_name) { 432 uint32_t index, Handle<String> module_name,
433 MaybeHandle<String> function_name) {
373 if (ffi.is_null()) { 434 if (ffi.is_null()) {
374 return ReportFFIError(thrower, "FFI is not an object", index, module_name, 435 return ReportFFIError(thrower, "FFI is not an object", index, module_name,
375 function_name); 436 function_name);
376 } 437 }
377 438
378 // Look up the module first. 439 // Look up the module first.
379 Handle<String> name = factory->InternalizeUtf8String(module_name); 440 MaybeHandle<Object> result = Object::GetProperty(ffi, module_name);
380 MaybeHandle<Object> result = Object::GetProperty(ffi, name);
381 if (result.is_null()) { 441 if (result.is_null()) {
382 return ReportFFIError(thrower, "module not found", index, module_name, 442 return ReportFFIError(thrower, "module not found", index, module_name,
383 function_name); 443 function_name);
384 } 444 }
385 445
386 Handle<Object> module = result.ToHandleChecked(); 446 Handle<Object> module = result.ToHandleChecked();
387 447
388 if (!module->IsJSReceiver()) { 448 if (!module->IsJSReceiver()) {
389 return ReportFFIError(thrower, "module is not an object or function", index, 449 return ReportFFIError(thrower, "module is not an object or function", index,
390 module_name, function_name); 450 module_name, function_name);
391 } 451 }
392 452
393 Handle<Object> function; 453 Handle<Object> function;
394 if (!function_name.is_empty()) { 454 if (!function_name.is_null()) {
395 // Look up the function in the module. 455 // Look up the function in the module.
396 Handle<String> name = factory->InternalizeUtf8String(function_name); 456 MaybeHandle<Object> result =
397 MaybeHandle<Object> result = Object::GetProperty(module, name); 457 Object::GetProperty(module, function_name.ToHandleChecked());
398 if (result.is_null()) { 458 if (result.is_null()) {
399 return ReportFFIError(thrower, "function not found", index, module_name, 459 return ReportFFIError(thrower, "function not found", index, module_name,
400 function_name); 460 function_name);
401 } 461 }
402 function = result.ToHandleChecked(); 462 function = result.ToHandleChecked();
403 } else { 463 } else {
404 // No function specified. Use the "default export". 464 // No function specified. Use the "default export".
405 function = module; 465 function = module;
406 } 466 }
407 467
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 }; 536 };
477 537
478 // Records statistics on the code generated by compiling WASM functions. 538 // Records statistics on the code generated by compiling WASM functions.
479 struct CodeStats { 539 struct CodeStats {
480 size_t code_size; 540 size_t code_size;
481 size_t reloc_size; 541 size_t reloc_size;
482 542
483 inline CodeStats() : code_size(0), reloc_size(0) {} 543 inline CodeStats() : code_size(0), reloc_size(0) {}
484 544
485 inline void Record(Code* code) { 545 inline void Record(Code* code) {
486 code_size += code->body_size(); 546 if (FLAG_print_wasm_code_size) {
487 reloc_size += code->relocation_info()->length(); 547 code_size += code->body_size();
548 reloc_size += code->relocation_info()->length();
549 }
550 }
551
552 void Record(const std::vector<Handle<Code>>& functions) {
553 if (FLAG_print_wasm_code_size) {
554 for (Handle<Code> c : functions) Record(*c);
555 }
556 }
557
558 void Record(Handle<FixedArray> functions) {
559 if (FLAG_print_wasm_code_size) {
560 DisallowHeapAllocation no_gc;
561 for (int i = 0; i < functions->length(); ++i) {
562 Code* code = Code::cast(functions->get(i));
563 Record(code);
564 }
565 }
488 } 566 }
489 567
490 inline void Report() { 568 inline void Report() {
491 PrintF("Total generated wasm code: %zu bytes\n", code_size); 569 if (FLAG_print_wasm_code_size) {
492 PrintF("Total generated wasm reloc: %zu bytes\n", reloc_size); 570 PrintF("Total generated wasm code: %zu bytes\n", code_size);
571 PrintF("Total generated wasm reloc: %zu bytes\n", reloc_size);
572 }
493 } 573 }
494 }; 574 };
495 575
496 bool CompileWrappersToImportedFunctions( 576 Handle<FixedArray> GetImportsMetadata(Factory* factory,
497 Isolate* isolate, const WasmModule* module, const Handle<JSReceiver> ffi, 577 const WasmModule* module) {
498 WasmModuleInstance* instance, ErrorThrower* thrower, Factory* factory) { 578 Handle<FixedArray> ret = factory->NewFixedArray(
499 if (module->import_table.size() > 0) { 579 static_cast<int>(module->import_table.size()), TENURED);
500 instance->import_code.reserve(module->import_table.size()); 580 for (size_t i = 0; i < module->import_table.size(); ++i) {
501 for (uint32_t index = 0; index < module->import_table.size(); ++index) { 581 const WasmImport& import = module->import_table[i];
502 const WasmImport& import = module->import_table[index]; 582 WasmName module_name = module->GetNameOrNull(import.module_name_offset,
503 WasmName module_name = module->GetNameOrNull(import.module_name_offset, 583 import.module_name_length);
504 import.module_name_length); 584 WasmName function_name = module->GetNameOrNull(import.function_name_offset,
505 WasmName function_name = module->GetNameOrNull( 585 import.function_name_length);
506 import.function_name_offset, import.function_name_length); 586
587 Handle<String> module_name_string =
588 factory->InternalizeUtf8String(module_name);
589 Handle<String> function_name_string =
590 function_name.is_empty()
591 ? Handle<String>::null()
592 : factory->InternalizeUtf8String(function_name);
593 Handle<ByteArray> sig =
594 factory->NewByteArray(static_cast<int>(import.sig->parameter_count() +
595 import.sig->return_count()),
596 TENURED);
597 sig->copy_in(0, reinterpret_cast<const byte*>(import.sig->raw_data()),
598 sig->length());
599 Handle<FixedArray> encoded_import =
600 factory->NewFixedArray(kWasmImportDataTableSize, TENURED);
601 encoded_import->set(kModuleName, *module_name_string);
602 if (!function_name_string.is_null()) {
603 encoded_import->set(kFunctionName, *function_name_string);
604 }
605 encoded_import->set(
606 kOutputCount,
607 Smi::FromInt(static_cast<int>(import.sig->return_count())));
608 encoded_import->set(kSignature, *sig);
609 ret->set(static_cast<int>(i), *encoded_import);
610 }
611 return ret;
612 }
613
614 bool CompileWrappersToImportedFunctions(Isolate* isolate,
615 const Handle<JSReceiver> ffi,
616 std::vector<Handle<Code>>& imports,
617 Handle<FixedArray> import_data,
618 ErrorThrower* thrower) {
619 uint32_t import_count = static_cast<uint32_t>(import_data->length());
620 if (import_count > 0) {
621 imports.reserve(import_count);
622 for (uint32_t index = 0; index < import_count; ++index) {
623 Handle<FixedArray> data = import_data->GetValueChecked<FixedArray>(index);
624 Handle<String> module_name = data->GetValueChecked<String>(kModuleName);
625 MaybeHandle<String> function_name = data->GetValue<String>(kFunctionName);
626
627 // TODO(mtrofin): this is an uint32_t, actually. We should rationalize
628 // it when we rationalize signed/unsigned stuff.
629 int ret_count = Smi::cast(data->get(kOutputCount))->value();
630 CHECK(ret_count >= 0);
631 Handle<ByteArray> sig_data = data->GetValueChecked<ByteArray>(kSignature);
632 int sig_data_size = sig_data->length();
633 int param_count = sig_data_size - ret_count;
634 CHECK(param_count >= 0);
635
507 MaybeHandle<JSFunction> function = LookupFunction( 636 MaybeHandle<JSFunction> function = LookupFunction(
508 *thrower, factory, ffi, index, module_name, function_name); 637 *thrower, isolate->factory(), ffi, index, module_name, function_name);
509 if (function.is_null()) return false; 638 if (function.is_null()) return false;
510 639
640 FunctionSig sig(
641 ret_count, param_count,
642 reinterpret_cast<const MachineRepresentation*>(sig_data->data()));
643
511 Handle<Code> code = compiler::CompileWasmToJSWrapper( 644 Handle<Code> code = compiler::CompileWasmToJSWrapper(
512 isolate, function.ToHandleChecked(), import.sig, module_name, 645 isolate, function.ToHandleChecked(), &sig, index, module_name,
513 function_name); 646 function_name);
514 instance->import_code[index] = code; 647
648 imports.push_back(code);
515 } 649 }
516 } 650 }
517 return true; 651 return true;
518 } 652 }
519 653
520 void InitializeParallelCompilation( 654 void InitializeParallelCompilation(
521 Isolate* isolate, const std::vector<WasmFunction>& functions, 655 Isolate* isolate, const std::vector<WasmFunction>& functions,
522 std::vector<compiler::WasmCompilationUnit*>& compilation_units, 656 std::vector<compiler::WasmCompilationUnit*>& compilation_units,
523 ModuleEnv& module_env, ErrorThrower& thrower) { 657 ModuleEnv& module_env, ErrorThrower& thrower) {
524 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); ++i) { 658 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
676 DCHECK_EQ(table_size * 2, instance->function_table->length()); 810 DCHECK_EQ(table_size * 2, instance->function_table->length());
677 uint32_t populated_table_size = 811 uint32_t populated_table_size =
678 static_cast<uint32_t>(instance->module->function_table.size()); 812 static_cast<uint32_t>(instance->module->function_table.size());
679 for (uint32_t i = 0; i < populated_table_size; ++i) { 813 for (uint32_t i = 0; i < populated_table_size; ++i) {
680 instance->function_table->set( 814 instance->function_table->set(
681 i + table_size, 815 i + table_size,
682 *instance->function_code[instance->module->function_table[i]]); 816 *instance->function_code[instance->module->function_table[i]]);
683 } 817 }
684 } 818 }
685 } 819 }
686 } // namespace
687 820
688 void SetDeoptimizationData(Factory* factory, Handle<JSObject> js_object, 821 void SetDebugSupport(Factory* factory, Handle<FixedArray> compiled_module,
689 std::vector<Handle<Code>>& functions) { 822 Handle<JSObject> js_object) {
690 for (size_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); ++i) { 823 MaybeHandle<String> module_bytes_string =
691 Handle<Code> code = functions[i]; 824 compiled_module->GetValue<String>(kModuleBytes);
825 if (!module_bytes_string.is_null()) {
826 js_object->SetInternalField(kWasmModuleBytesString,
827 *module_bytes_string.ToHandleChecked());
828 }
829 Handle<FixedArray> functions = Handle<FixedArray>(
830 FixedArray::cast(js_object->GetInternalField(kWasmModuleCodeTable)));
831
832 for (int i = FLAG_skip_compiling_wasm_funcs; i < functions->length(); ++i) {
833 Handle<Code> code = functions->GetValueChecked<Code>(i);
692 DCHECK(code->deoptimization_data() == nullptr || 834 DCHECK(code->deoptimization_data() == nullptr ||
693 code->deoptimization_data()->length() == 0); 835 code->deoptimization_data()->length() == 0);
694 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED); 836 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED);
695 if (!js_object.is_null()) { 837 if (!js_object.is_null()) {
696 deopt_data->set(0, *js_object); 838 deopt_data->set(0, *js_object);
697 } 839 }
698 deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); 840 deopt_data->set(1, Smi::FromInt(static_cast<int>(i)));
699 deopt_data->set_length(2); 841 deopt_data->set_length(2);
700 code->set_deoptimization_data(*deopt_data); 842 code->set_deoptimization_data(*deopt_data);
701 } 843 }
844
845 MaybeHandle<ByteArray> function_name_table =
846 compiled_module->GetValue<ByteArray>(kFunctionNameTable);
847 if (!function_name_table.is_null()) {
848 js_object->SetInternalField(kWasmFunctionNamesArray,
849 *function_name_table.ToHandleChecked());
850 }
702 } 851 }
703 852
704 Handle<FixedArray> WasmModule::CompileFunctions(Isolate* isolate) const { 853 bool SetupGlobals(Isolate* isolate, Handle<FixedArray> compiled_module,
854 Handle<JSObject> instance, ErrorThrower* thrower) {
855 uint32_t globals_size = static_cast<uint32_t>(
856 Smi::cast(compiled_module->get(kGlobalsSize))->value());
857 if (globals_size > 0) {
858 Handle<JSArrayBuffer> globals_buffer =
859 NewArrayBuffer(isolate, globals_size);
860 if (globals_buffer.is_null()) {
861 thrower->Error("Out of memory: wasm globals");
862 return false;
863 }
864 RelocateGlobals(instance,
865 static_cast<Address>(globals_buffer->backing_store()));
866 instance->SetInternalField(kWasmGlobalsArrayBuffer, *globals_buffer);
867 }
868 return true;
869 }
870
871 bool SetupInstanceHeap(Isolate* isolate, Handle<FixedArray> compiled_module,
872 Handle<JSObject> instance, Handle<JSArrayBuffer> memory,
873 ErrorThrower* thrower) {
874 uint32_t min_mem_pages = static_cast<uint32_t>(
875 Smi::cast(compiled_module->get(kMinRequiredMemory))->value());
876 isolate->counters()->wasm_min_mem_pages_count()->AddSample(min_mem_pages);
877 // TODO(wasm): re-enable counter for max_mem_pages when we use that field.
878
879 if (memory.is_null() && min_mem_pages > 0) {
880 memory = AllocateMemory(thrower, isolate, min_mem_pages);
881 if (memory.is_null()) {
882 return false;
883 }
884 }
885
886 if (!memory.is_null()) {
887 instance->SetInternalField(kWasmMemArrayBuffer, *memory);
888 Address mem_start = static_cast<Address>(memory->backing_store());
889 uint32_t mem_size = static_cast<uint32_t>(memory->byte_length()->Number());
890 RelocateInstanceCode(instance, mem_start,
891 WasmModule::kPageSize * min_mem_pages, mem_size);
892 LoadDataSegments(compiled_module, mem_start, mem_size);
893 }
894 return true;
895 }
896
897 bool SetupImports(Isolate* isolate, Handle<FixedArray> compiled_module,
898 Handle<JSObject> instance, ErrorThrower* thrower,
899 Handle<JSReceiver> ffi, CodeStats* stats) {
900 //-------------------------------------------------------------------------
901 // Compile wrappers to imported functions.
902 //-------------------------------------------------------------------------
903 std::vector<Handle<Code>> import_code;
904 MaybeHandle<FixedArray> maybe_import_data =
905 compiled_module->GetValue<FixedArray>(kImportData);
906 if (!maybe_import_data.is_null()) {
907 Handle<FixedArray> import_data = maybe_import_data.ToHandleChecked();
908 if (!CompileWrappersToImportedFunctions(isolate, ffi, import_code,
909 import_data, thrower)) {
910 return false;
911 }
912 }
913
914 stats->Record(import_code);
915
916 Handle<FixedArray> code_table = Handle<FixedArray>(
917 FixedArray::cast(instance->GetInternalField(kWasmModuleCodeTable)));
918 // TODO(mtrofin): get the code off std::vector and on FixedArray, for
919 // consistency.
920 std::vector<Handle<Code>> function_code(code_table->length());
921 for (int i = 0; i < code_table->length(); ++i) {
922 Handle<Code> code = Handle<Code>(Code::cast(code_table->get(i)));
923 function_code[i] = code;
924 }
925
926 instance->SetInternalField(kWasmModuleFunctionTable, Smi::FromInt(0));
927 LinkImports(isolate, function_code, import_code);
928 return true;
929 }
930
931 bool SetupExportsObject(Handle<FixedArray> compiled_module, Isolate* isolate,
932 Handle<JSObject> instance, ErrorThrower* thrower,
933 CodeStats* stats) {
934 Factory* factory = isolate->factory();
935 bool mem_export =
936 static_cast<bool>(Smi::cast(compiled_module->get(kExportMem))->value());
937 ModuleOrigin origin = static_cast<ModuleOrigin>(
938 Smi::cast(compiled_module->get(kOrigin))->value());
939
940 MaybeHandle<FixedArray> maybe_exports =
941 compiled_module->GetValue<FixedArray>(kExports);
942 if (!maybe_exports.is_null() || mem_export) {
943 PropertyDescriptor desc;
944 desc.set_writable(false);
945
946 Handle<JSObject> exports_object = instance;
947 if (origin == kWasmOrigin) {
948 // Create the "exports" object.
949 Handle<JSFunction> object_function = Handle<JSFunction>(
950 isolate->native_context()->object_function(), isolate);
951 exports_object = factory->NewJSObject(object_function, TENURED);
952 Handle<String> exports_name = factory->InternalizeUtf8String("exports");
953 JSObject::AddProperty(instance, exports_name, exports_object, READ_ONLY);
954 }
955 if (!maybe_exports.is_null()) {
956 Handle<FixedArray> exports = maybe_exports.ToHandleChecked();
957
958 int exports_size = exports->length();
959 for (int i = 0; i < exports_size; ++i) {
960 if (thrower->error()) return false;
961 Handle<JSFunction> function = exports->GetValueChecked<JSFunction>(i);
962 stats->Record(function->code());
963 Handle<String> name =
964 Handle<String>(String::cast(function->shared()->name()));
965 function->SetInternalField(0, *instance);
966
967 desc.set_value(function);
968 Maybe<bool> status = JSReceiver::DefineOwnProperty(
969 isolate, exports_object, name, &desc, Object::THROW_ON_ERROR);
970 if (!status.IsJust()) {
971 thrower->Error("export of %.*s failed.", name->length(),
972 name->ToCString().get());
973 return false;
974 }
975 }
976 }
977 if (mem_export) {
978 // Export the memory as a named property.
979 Handle<String> name = factory->InternalizeUtf8String("memory");
980 Handle<JSArrayBuffer> memory = Handle<JSArrayBuffer>(
981 JSArrayBuffer::cast(instance->GetInternalField(kWasmMemArrayBuffer)));
982 JSObject::AddProperty(exports_object, name, memory, READ_ONLY);
983 }
984 }
985 return true;
986 }
987
988 } // namespace
989
990 MaybeHandle<FixedArray> WasmModule::CompileFunctions(Isolate* isolate) const {
705 Factory* factory = isolate->factory(); 991 Factory* factory = isolate->factory();
706 ErrorThrower thrower(isolate, "WasmModule::CompileFunctions()"); 992 ErrorThrower thrower(isolate, "WasmModule::CompileFunctions()");
707 993
994 MaybeHandle<FixedArray> nothing;
995
708 WasmModuleInstance temp_instance_for_compilation(this); 996 WasmModuleInstance temp_instance_for_compilation(this);
709 temp_instance_for_compilation.function_table = 997 temp_instance_for_compilation.function_table =
710 BuildFunctionTable(isolate, this); 998 BuildFunctionTable(isolate, this);
711 temp_instance_for_compilation.context = isolate->native_context(); 999 temp_instance_for_compilation.context = isolate->native_context();
712 temp_instance_for_compilation.mem_size = GetMinModuleMemSize(this); 1000 temp_instance_for_compilation.mem_size = GetMinModuleMemSize(this);
713 temp_instance_for_compilation.mem_start = nullptr; 1001 temp_instance_for_compilation.mem_start = nullptr;
714 temp_instance_for_compilation.globals_start = nullptr; 1002 temp_instance_for_compilation.globals_start = nullptr;
715 1003
1004 HistogramTimerScope wasm_compile_module_time_scope(
1005 isolate->counters()->wasm_compile_module_time());
1006
716 ModuleEnv module_env; 1007 ModuleEnv module_env;
717 module_env.module = this; 1008 module_env.module = this;
718 module_env.instance = &temp_instance_for_compilation; 1009 module_env.instance = &temp_instance_for_compilation;
719 module_env.origin = origin; 1010 module_env.origin = origin;
720 InitializePlaceholders(factory, &module_env.placeholders, functions.size()); 1011 InitializePlaceholders(factory, &module_env.placeholders, functions.size());
721 1012
722 Handle<FixedArray> ret = 1013 Handle<FixedArray> compiled_functions =
723 factory->NewFixedArray(static_cast<int>(functions.size()), TENURED); 1014 factory->NewFixedArray(static_cast<int>(functions.size()), TENURED);
724 1015
725 temp_instance_for_compilation.import_code.resize(import_table.size()); 1016 temp_instance_for_compilation.import_code.resize(import_table.size());
726 for (uint32_t i = 0; i < import_table.size(); ++i) { 1017 for (uint32_t i = 0; i < import_table.size(); ++i) {
727 temp_instance_for_compilation.import_code[i] = 1018 temp_instance_for_compilation.import_code[i] =
728 CreatePlaceholder(factory, i, Code::WASM_TO_JS_FUNCTION); 1019 CreatePlaceholder(factory, i, Code::WASM_TO_JS_FUNCTION);
729 } 1020 }
730 isolate->counters()->wasm_functions_per_module()->AddSample( 1021 isolate->counters()->wasm_functions_per_module()->AddSample(
731 static_cast<int>(functions.size())); 1022 static_cast<int>(functions.size()));
732 if (FLAG_wasm_num_compilation_tasks != 0) { 1023 if (FLAG_wasm_num_compilation_tasks != 0) {
733 CompileInParallel(isolate, this, 1024 CompileInParallel(isolate, this,
734 temp_instance_for_compilation.function_code, &thrower, 1025 temp_instance_for_compilation.function_code, &thrower,
735 &module_env); 1026 &module_env);
736 } else { 1027 } else {
737 CompileSequentially(isolate, this, 1028 CompileSequentially(isolate, this,
738 temp_instance_for_compilation.function_code, &thrower, 1029 temp_instance_for_compilation.function_code, &thrower,
739 &module_env); 1030 &module_env);
740 } 1031 }
741 if (thrower.error()) { 1032 if (thrower.error()) return nothing;
742 return Handle<FixedArray>::null();
743 }
744 1033
745 LinkModuleFunctions(isolate, temp_instance_for_compilation.function_code); 1034 LinkModuleFunctions(isolate, temp_instance_for_compilation.function_code);
746 1035
747 // At this point, compilation has completed. Update the code table 1036 // At this point, compilation has completed. Update the code table.
748 // and record sizes.
749 for (size_t i = FLAG_skip_compiling_wasm_funcs; 1037 for (size_t i = FLAG_skip_compiling_wasm_funcs;
750 i < temp_instance_for_compilation.function_code.size(); ++i) { 1038 i < temp_instance_for_compilation.function_code.size(); ++i) {
751 Code* code = *temp_instance_for_compilation.function_code[i]; 1039 Code* code = *temp_instance_for_compilation.function_code[i];
752 ret->set(static_cast<int>(i), code); 1040 compiled_functions->set(static_cast<int>(i), code);
753 } 1041 }
754 1042
755 PopulateFunctionTable(&temp_instance_for_compilation); 1043 PopulateFunctionTable(&temp_instance_for_compilation);
756 1044
1045 // Create the compiled module object, and populate with compiled functions
1046 // and information needed at instantiation time. This object needs to be
1047 // serializable. Instantiation may occur off a deserialized version of this
1048 // object.
1049 Handle<FixedArray> ret =
1050 factory->NewFixedArray(kCompiledWasmObjectTableSize, TENURED);
1051 ret->set(kFunctions, *compiled_functions);
1052
1053 Handle<FixedArray> import_data = GetImportsMetadata(factory, this);
1054 ret->set(kImportData, *import_data);
1055
1056 // Compile export functions.
1057 int export_size = static_cast<int>(export_table.size());
1058 Handle<JSFunction> startup_fct;
1059 if (export_size > 0) {
1060 Handle<FixedArray> exports = factory->NewFixedArray(export_size, TENURED);
1061 for (int i = 0; i < export_size; ++i) {
1062 const WasmExport& exp = export_table[i];
1063 WasmName str = GetName(exp.name_offset, exp.name_length);
1064 Handle<String> name = factory->InternalizeUtf8String(str);
1065 Handle<Code> code =
1066 temp_instance_for_compilation.function_code[exp.func_index];
1067 Handle<JSFunction> function = compiler::CompileJSToWasmWrapper(
1068 isolate, &module_env, name, code, exp.func_index);
1069 if (thrower.error()) return nothing;
1070 exports->set(i, *function);
1071 if (exp.func_index == start_function_index) {
1072 startup_fct = function;
1073 }
1074 }
1075 ret->set(kExports, *exports);
1076 }
1077
1078 // Compile startup function, if we haven't already.
1079 if (start_function_index >= 0) {
1080 HandleScope scope(isolate);
1081 if (startup_fct.is_null()) {
1082 uint32_t index = static_cast<uint32_t>(start_function_index);
1083 Handle<String> name = factory->NewStringFromStaticChars("start");
1084 Handle<Code> code = temp_instance_for_compilation.function_code[index];
1085 startup_fct = compiler::CompileJSToWasmWrapper(isolate, &module_env, name,
1086 code, index);
1087 }
1088 ret->set(kStartupFunction, *startup_fct);
1089 }
1090
1091 // TODO(wasm): saving the module bytes for debugging is wasteful. We should
1092 // consider downloading this on-demand.
1093 {
1094 size_t module_bytes_len = module_end - module_start;
1095 DCHECK_LE(module_bytes_len, static_cast<size_t>(kMaxInt));
1096 Vector<const uint8_t> module_bytes_vec(module_start,
1097 static_cast<int>(module_bytes_len));
1098 Handle<String> module_bytes_string =
1099 factory->NewStringFromOneByte(module_bytes_vec, TENURED)
1100 .ToHandleChecked();
1101 ret->set(kModuleBytes, *module_bytes_string);
1102 }
1103
1104 Handle<ByteArray> function_name_table =
1105 BuildFunctionNamesTable(isolate, module_env.module);
1106 ret->set(kFunctionNameTable, *function_name_table);
1107 ret->set(kMinRequiredMemory, Smi::FromInt(min_mem_pages));
1108 if (data_segments.size() > 0) SaveDataSegmentInfo(factory, this, ret);
1109 ret->set(kGlobalsSize, Smi::FromInt(globals_size));
1110 ret->set(kExportMem, Smi::FromInt(mem_export));
1111 ret->set(kOrigin, Smi::FromInt(origin));
757 return ret; 1112 return ret;
758 } 1113 }
759 1114
760 // Instantiates a wasm module as a JSObject. 1115 // Instantiates a wasm module as a JSObject.
761 // * allocates a backing store of {mem_size} bytes. 1116 // * allocates a backing store of {mem_size} bytes.
762 // * installs a named property "memory" for that buffer if exported 1117 // * installs a named property "memory" for that buffer if exported
763 // * installs named properties on the object for exported functions 1118 // * installs named properties on the object for exported functions
764 // * compiles wasm code to machine code 1119 // * compiles wasm code to machine code
765 MaybeHandle<JSObject> WasmModule::Instantiate( 1120 MaybeHandle<JSObject> WasmModule::Instantiate(
766 Isolate* isolate, Handle<JSReceiver> ffi, 1121 Isolate* isolate, Handle<FixedArray> compiled_module,
767 Handle<JSArrayBuffer> memory) const { 1122 Handle<JSReceiver> ffi, Handle<JSArrayBuffer> memory) {
768 HistogramTimerScope wasm_instantiate_module_time_scope( 1123 HistogramTimerScope wasm_instantiate_module_time_scope(
769 isolate->counters()->wasm_instantiate_module_time()); 1124 isolate->counters()->wasm_instantiate_module_time());
770 ErrorThrower thrower(isolate, "WasmModule::Instantiate()"); 1125 ErrorThrower thrower(isolate, "WasmModule::Instantiate()");
771 Factory* factory = isolate->factory(); 1126 Factory* factory = isolate->factory();
772 1127
773 //------------------------------------------------------------------------- 1128 CodeStats code_stats;
774 // Allocate the instance and its JS counterpart. 1129 // These fields are compulsory.
775 //------------------------------------------------------------------------- 1130 Handle<FixedArray> code_table =
1131 compiled_module->GetValueChecked<FixedArray>(kFunctions);
1132
1133 code_stats.Record(code_table);
1134
1135 MaybeHandle<JSObject> nothing;
1136
776 Handle<Map> map = factory->NewMap( 1137 Handle<Map> map = factory->NewMap(
777 JS_OBJECT_TYPE, 1138 JS_OBJECT_TYPE,
778 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); 1139 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize);
779 WasmModuleInstance instance(this); 1140 Handle<JSObject> js_object = factory->NewJSObjectFromMap(map, TENURED);
780 instance.context = isolate->native_context(); 1141 js_object->SetInternalField(kWasmModuleCodeTable, *code_table);
781 instance.js_object = factory->NewJSObjectFromMap(map, TENURED);
782 1142
783 Handle<FixedArray> code_table = CompileFunctions(isolate); 1143 if (!(SetupInstanceHeap(isolate, compiled_module, js_object, memory,
784 if (code_table.is_null()) return Handle<JSObject>::null(); 1144 &thrower) &&
785 1145 SetupGlobals(isolate, compiled_module, js_object, &thrower) &&
786 instance.js_object->SetInternalField(kWasmModuleCodeTable, *code_table); 1146 SetupImports(isolate, compiled_module, js_object, &thrower, ffi,
787 size_t module_bytes_len = 1147 &code_stats) &&
788 instance.module->module_end - instance.module->module_start; 1148 SetupExportsObject(compiled_module, isolate, js_object, &thrower,
789 DCHECK_LE(module_bytes_len, static_cast<size_t>(kMaxInt)); 1149 &code_stats))) {
790 Vector<const uint8_t> module_bytes_vec(instance.module->module_start, 1150 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 } 1151 }
802 1152
803 //------------------------------------------------------------------------- 1153 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 1154
821 //------------------------------------------------------------------------- 1155 // Run the start function if one was specified.
822 // Allocate the globals area if necessary. 1156 MaybeHandle<JSFunction> maybe_startup_fct =
823 //------------------------------------------------------------------------- 1157 compiled_module->GetValue<JSFunction>(kStartupFunction);
824 if (!AllocateGlobals(&thrower, isolate, &instance)) { 1158 if (!maybe_startup_fct.is_null()) {
825 return MaybeHandle<JSObject>(); 1159 HandleScope scope(isolate);
826 } 1160 Handle<JSFunction> startup_fct = maybe_startup_fct.ToHandleChecked();
827 if (!instance.globals_buffer.is_null()) { 1161 code_stats.Record(startup_fct->code());
828 instance.js_object->SetInternalField(kWasmGlobalsArrayBuffer, 1162 startup_fct->SetInternalField(0, *js_object);
829 *instance.globals_buffer); 1163 // Call the JS function.
830 } 1164 Handle<Object> undefined = isolate->factory()->undefined_value();
1165 MaybeHandle<Object> retval =
1166 Execution::Call(isolate, startup_fct, undefined, 0, nullptr);
831 1167
832 HistogramTimerScope wasm_compile_module_time_scope( 1168 if (retval.is_null()) {
833 isolate->counters()->wasm_compile_module_time()); 1169 thrower.Error("WASM.instantiateModule(): start function failed");
834 1170 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 } 1171 }
913 } 1172 }
914 1173
915 if (FLAG_print_wasm_code_size) { 1174 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 1175
926 // Run the start function if one was specified. 1176 DCHECK(wasm::IsWasmObject(*js_object));
927 if (this->start_function_index >= 0) { 1177 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 } 1178 }
946 1179
947 // TODO(mtrofin): remove this once we move to WASM_DIRECT_CALL 1180 // TODO(mtrofin): remove this once we move to WASM_DIRECT_CALL
948 Handle<Code> ModuleEnv::GetCodeOrPlaceholder(uint32_t index) const { 1181 Handle<Code> ModuleEnv::GetCodeOrPlaceholder(uint32_t index) const {
949 DCHECK(IsValidFunction(index)); 1182 DCHECK(IsValidFunction(index));
950 if (!placeholders.empty()) return placeholders[index]; 1183 if (!placeholders.empty()) return placeholders[index];
951 DCHECK_NOT_NULL(instance); 1184 DCHECK_NOT_NULL(instance);
952 return instance->function_code[index]; 1185 return instance->function_code[index];
953 } 1186 }
954 1187
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
989 Handle<Object> name_or_null = 1222 Handle<Object> name_or_null =
990 GetWasmFunctionNameOrNull(isolate, wasm, func_index); 1223 GetWasmFunctionNameOrNull(isolate, wasm, func_index);
991 if (!name_or_null->IsNull(isolate)) { 1224 if (!name_or_null->IsNull(isolate)) {
992 return Handle<String>::cast(name_or_null); 1225 return Handle<String>::cast(name_or_null);
993 } 1226 }
994 return isolate->factory()->NewStringFromStaticChars("<WASM UNNAMED>"); 1227 return isolate->factory()->NewStringFromStaticChars("<WASM UNNAMED>");
995 } 1228 }
996 1229
997 bool IsWasmObject(Object* object) { 1230 bool IsWasmObject(Object* object) {
998 if (!object->IsJSObject()) return false; 1231 if (!object->IsJSObject()) return false;
1232
999 JSObject* obj = JSObject::cast(object); 1233 JSObject* obj = JSObject::cast(object);
1000 if (obj->GetInternalFieldCount() != kWasmModuleInternalFieldCount || 1234 Isolate* isolate = obj->GetIsolate();
1001 !obj->GetInternalField(kWasmModuleCodeTable)->IsFixedArray() || 1235 if (obj->GetInternalFieldCount() != kWasmModuleInternalFieldCount) {
1002 !obj->GetInternalField(kWasmMemArrayBuffer)->IsJSArrayBuffer() ||
1003 !obj->GetInternalField(kWasmFunctionNamesArray)->IsByteArray() ||
1004 !obj->GetInternalField(kWasmModuleBytesString)->IsSeqOneByteString()) {
1005 return false; 1236 return false;
1006 } 1237 }
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 1238
1013 // All checks passed. 1239 Object* mem = obj->GetInternalField(kWasmMemArrayBuffer);
1014 return true; 1240 if (obj->GetInternalField(kWasmModuleCodeTable)->IsFixedArray() &&
1241 (mem->IsUndefined(isolate) || mem->IsJSArrayBuffer()) &&
1242 obj->GetInternalField(kWasmFunctionNamesArray)->IsByteArray()) {
1243 Object* debug_bytes = obj->GetInternalField(kWasmModuleBytesString);
1244 if (!debug_bytes->IsUndefined(isolate)) {
1245 if (!debug_bytes->IsSeqOneByteString()) {
1246 return false;
1247 }
1248 DisallowHeapAllocation no_gc;
1249 SeqOneByteString* bytes = SeqOneByteString::cast(debug_bytes);
1250 if (bytes->length() < 4) return false;
1251 if (memcmp(bytes->GetChars(), "\0asm", 4)) return false;
1252 // All checks passed.
1253 }
1254 return true;
1255 }
1256 return false;
1015 } 1257 }
1016 1258
1017 SeqOneByteString* GetWasmBytes(JSObject* wasm) { 1259 SeqOneByteString* GetWasmBytes(JSObject* wasm) {
1018 return SeqOneByteString::cast(wasm->GetInternalField(kWasmModuleBytesString)); 1260 return SeqOneByteString::cast(wasm->GetInternalField(kWasmModuleBytesString));
1019 } 1261 }
1020 1262
1021 WasmDebugInfo* GetDebugInfo(JSObject* wasm) { 1263 WasmDebugInfo* GetDebugInfo(JSObject* wasm) {
1022 Object* info = wasm->GetInternalField(kWasmDebugInfo); 1264 Object* info = wasm->GetInternalField(kWasmDebugInfo);
1023 if (!info->IsUndefined(wasm->GetIsolate())) return WasmDebugInfo::cast(info); 1265 if (!info->IsUndefined(wasm->GetIsolate())) return WasmDebugInfo::cast(info);
1024 Handle<WasmDebugInfo> new_info = WasmDebugInfo::New(handle(wasm)); 1266 Handle<WasmDebugInfo> new_info = WasmDebugInfo::New(handle(wasm));
(...skipping 24 matching lines...) Expand all
1049 } 1291 }
1050 1292
1051 if (module->import_table.size() > 0) { 1293 if (module->import_table.size() > 0) {
1052 thrower.Error("Not supported: module has imports."); 1294 thrower.Error("Not supported: module has imports.");
1053 } 1295 }
1054 if (module->export_table.size() == 0) { 1296 if (module->export_table.size() == 0) {
1055 thrower.Error("Not supported: module has no exports."); 1297 thrower.Error("Not supported: module has no exports.");
1056 } 1298 }
1057 1299
1058 if (thrower.error()) return -1; 1300 if (thrower.error()) return -1;
1301 MaybeHandle<FixedArray> compiled_module = module->CompileFunctions(isolate);
1059 1302
1303 if (compiled_module.is_null()) return -1;
1060 Handle<JSObject> instance = 1304 Handle<JSObject> instance =
1061 module 1305 module
1062 ->Instantiate(isolate, Handle<JSReceiver>::null(), 1306 ->Instantiate(isolate, compiled_module.ToHandleChecked(),
1307 Handle<JSReceiver>::null(),
1063 Handle<JSArrayBuffer>::null()) 1308 Handle<JSArrayBuffer>::null())
1064 .ToHandleChecked(); 1309 .ToHandleChecked();
1065 1310
1066 Handle<Name> exports = isolate->factory()->InternalizeUtf8String("exports"); 1311 Handle<Name> exports = isolate->factory()->InternalizeUtf8String("exports");
1067 Handle<JSObject> exports_object = Handle<JSObject>::cast( 1312 Handle<JSObject> exports_object = Handle<JSObject>::cast(
1068 JSObject::GetProperty(instance, exports).ToHandleChecked()); 1313 JSObject::GetProperty(instance, exports).ToHandleChecked());
1069 Handle<Name> main_name = isolate->factory()->NewStringFromStaticChars("main"); 1314 Handle<Name> main_name = isolate->factory()->NewStringFromStaticChars("main");
1070 PropertyDescriptor desc; 1315 PropertyDescriptor desc;
1071 Maybe<bool> property_found = JSReceiver::GetOwnPropertyDescriptor( 1316 Maybe<bool> property_found = JSReceiver::GetOwnPropertyDescriptor(
1072 isolate, exports_object, main_name, &desc); 1317 isolate, exports_object, main_name, &desc);
(...skipping 19 matching lines...) Expand all
1092 return static_cast<int32_t>(HeapNumber::cast(*result)->value()); 1337 return static_cast<int32_t>(HeapNumber::cast(*result)->value());
1093 } 1338 }
1094 thrower.Error("WASM.compileRun() failed: Return value should be number"); 1339 thrower.Error("WASM.compileRun() failed: Return value should be number");
1095 return -1; 1340 return -1;
1096 } 1341 }
1097 1342
1098 } // namespace testing 1343 } // namespace testing
1099 } // namespace wasm 1344 } // namespace wasm
1100 } // namespace internal 1345 } // namespace internal
1101 } // namespace v8 1346 } // namespace v8
OLDNEW
« no previous file with comments | « src/wasm/wasm-module.h ('k') | test/cctest/wasm/wasm-run-utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698