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

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

Issue 2103983003: Revert "[wasm] Complete separation of compilation and instantiation" (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: 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
167 uint32_t GetMinModuleMemSize(const WasmModule* module) { 127 uint32_t GetMinModuleMemSize(const WasmModule* module) {
168 return WasmModule::kPageSize * module->min_mem_pages; 128 return WasmModule::kPageSize * module->min_mem_pages;
169 } 129 }
170 130
171 void LoadDataSegments(Handle<FixedArray> compiled_module, Address mem_addr, 131 void LoadDataSegments(const WasmModule* module, byte* mem_addr,
172 size_t mem_size) { 132 size_t mem_size) {
173 MaybeHandle<ByteArray> maybe_data = 133 for (const WasmDataSegment& segment : module->data_segments) {
174 compiled_module->GetValue<ByteArray>(kDataSegments); 134 if (!segment.init) continue;
175 MaybeHandle<FixedArray> maybe_segments = 135 if (!segment.source_size) continue;
176 compiled_module->GetValue<FixedArray>(kDataSegmentsInfo); 136 CHECK_LT(segment.dest_addr, mem_size);
177 137 CHECK_LE(segment.source_size, mem_size);
178 // We either have both or neither. 138 CHECK_LE(segment.dest_addr + segment.source_size, mem_size);
179 CHECK(maybe_data.is_null() == maybe_segments.is_null()); 139 byte* addr = mem_addr + segment.dest_addr;
180 // If we have neither, we're done. 140 memcpy(addr, module->module_start + segment.source_offset,
181 if (maybe_data.is_null()) return; 141 segment.source_size);
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;
198 } 142 }
199 } 143 }
200 144
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
232 Handle<FixedArray> BuildFunctionTable(Isolate* isolate, 145 Handle<FixedArray> BuildFunctionTable(Isolate* isolate,
233 const WasmModule* module) { 146 const WasmModule* module) {
234 // Compute the size of the indirect function table 147 // Compute the size of the indirect function table
235 uint32_t table_size = module->FunctionTableSize(); 148 uint32_t table_size = module->FunctionTableSize();
236 if (table_size == 0) { 149 if (table_size == 0) {
237 return Handle<FixedArray>::null(); 150 return Handle<FixedArray>::null();
238 } 151 }
239 152
240 Handle<FixedArray> fixed = isolate->factory()->NewFixedArray(2 * table_size); 153 Handle<FixedArray> fixed = isolate->factory()->NewFixedArray(2 * table_size);
241 for (uint32_t i = 0; 154 for (uint32_t i = 0;
242 i < static_cast<uint32_t>(module->function_table.size()); 155 i < static_cast<uint32_t>(module->function_table.size());
243 ++i) { 156 ++i) {
244 const WasmFunction* function = 157 const WasmFunction* function =
245 &module->functions[module->function_table[i]]; 158 &module->functions[module->function_table[i]];
246 fixed->set(i, Smi::FromInt(function->sig_index)); 159 fixed->set(i, Smi::FromInt(function->sig_index));
247 } 160 }
248 return fixed; 161 return fixed;
249 } 162 }
250 163
251 Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size) { 164 Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size,
165 byte** backing_store) {
166 *backing_store = nullptr;
252 if (size > (WasmModule::kMaxMemPages * WasmModule::kPageSize)) { 167 if (size > (WasmModule::kMaxMemPages * WasmModule::kPageSize)) {
253 // TODO(titzer): lift restriction on maximum memory allocated here. 168 // TODO(titzer): lift restriction on maximum memory allocated here.
254 return Handle<JSArrayBuffer>::null(); 169 return Handle<JSArrayBuffer>::null();
255 } 170 }
256 void* memory = isolate->array_buffer_allocator()->Allocate(size); 171 void* memory = isolate->array_buffer_allocator()->Allocate(size);
257 if (memory == nullptr) { 172 if (memory == nullptr) {
258 return Handle<JSArrayBuffer>::null(); 173 return Handle<JSArrayBuffer>::null();
259 } 174 }
260 175
176 *backing_store = reinterpret_cast<byte*>(memory);
177
261 #if DEBUG 178 #if DEBUG
262 // Double check the API allocator actually zero-initialized the memory. 179 // Double check the API allocator actually zero-initialized the memory.
263 const byte* bytes = reinterpret_cast<const byte*>(memory); 180 byte* bytes = reinterpret_cast<byte*>(*backing_store);
264 for (size_t i = 0; i < size; ++i) { 181 for (size_t i = 0; i < size; ++i) {
265 DCHECK_EQ(0, bytes[i]); 182 DCHECK_EQ(0, bytes[i]);
266 } 183 }
267 #endif 184 #endif
268 185
269 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer(); 186 Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer();
270 JSArrayBuffer::Setup(buffer, isolate, false, memory, static_cast<int>(size)); 187 JSArrayBuffer::Setup(buffer, isolate, false, memory, static_cast<int>(size));
271 buffer->set_is_neuterable(false); 188 buffer->set_is_neuterable(false);
272 return buffer; 189 return buffer;
273 } 190 }
274 191
275 void RelocateInstanceCode(Handle<JSObject> instance, Address start, 192 void RelocateInstanceCode(WasmModuleInstance* instance) {
276 uint32_t prev_size, uint32_t new_size) { 193 for (uint32_t i = 0; i < instance->function_code.size(); ++i) {
277 Handle<FixedArray> functions = Handle<FixedArray>( 194 Handle<Code> function = instance->function_code[i];
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)));
281 AllowDeferredHandleDereference embedding_raw_address; 195 AllowDeferredHandleDereference embedding_raw_address;
282 int mask = (1 << RelocInfo::WASM_MEMORY_REFERENCE) | 196 int mask = (1 << RelocInfo::WASM_MEMORY_REFERENCE) |
283 (1 << RelocInfo::WASM_MEMORY_SIZE_REFERENCE); 197 (1 << RelocInfo::WASM_MEMORY_SIZE_REFERENCE);
284 for (RelocIterator it(*function, mask); !it.done(); it.next()) { 198 for (RelocIterator it(*function, mask); !it.done(); it.next()) {
285 it.rinfo()->update_wasm_memory_reference(nullptr, start, prev_size, 199 it.rinfo()->update_wasm_memory_reference(
286 new_size); 200 nullptr, instance->mem_start, GetMinModuleMemSize(instance->module),
201 static_cast<uint32_t>(instance->mem_size));
287 } 202 }
288 } 203 }
289 } 204 }
290 205
291 // Allocate memory for a module instance as a new JSArrayBuffer. 206 // Set the memory for a module instance to be the {memory} array buffer.
292 Handle<JSArrayBuffer> AllocateMemory(ErrorThrower* thrower, Isolate* isolate, 207 void SetMemory(WasmModuleInstance* instance, Handle<JSArrayBuffer> memory) {
293 uint32_t min_mem_pages) { 208 memory->set_is_neuterable(false);
294 if (min_mem_pages > WasmModule::kMaxMemPages) { 209 instance->mem_start = reinterpret_cast<byte*>(memory->backing_store());
295 thrower->Error("Out of memory: wasm memory too large"); 210 instance->mem_size = memory->byte_length()->Number();
296 return Handle<JSArrayBuffer>::null(); 211 instance->mem_buffer = memory;
297 } 212 RelocateInstanceCode(instance);
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;
305 } 213 }
306 214
307 void RelocateGlobals(Handle<JSObject> instance, Address globals_start) { 215 // Allocate memory for a module instance as a new JSArrayBuffer.
308 Handle<FixedArray> functions = Handle<FixedArray>( 216 bool AllocateMemory(ErrorThrower* thrower, Isolate* isolate,
309 FixedArray::cast(instance->GetInternalField(kWasmModuleCodeTable))); 217 WasmModuleInstance* instance) {
310 uint32_t function_count = static_cast<uint32_t>(functions->length()); 218 DCHECK(instance->module);
311 for (uint32_t i = 0; i < function_count; ++i) { 219 DCHECK(instance->mem_buffer.is_null());
312 Handle<Code> function = Handle<Code>(Code::cast(functions->get(i))); 220
313 AllowDeferredHandleDereference embedding_raw_address; 221 if (instance->module->min_mem_pages > WasmModule::kMaxMemPages) {
314 int mask = 1 << RelocInfo::WASM_GLOBAL_REFERENCE; 222 thrower->Error("Out of memory: wasm memory too large");
315 for (RelocIterator it(*function, mask); !it.done(); it.next()) { 223 return false;
316 it.rinfo()->update_wasm_global_reference(nullptr, globals_start); 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 }
317 } 257 }
318 } 258 }
259 return true;
319 } 260 }
320 261
321 Handle<Code> CreatePlaceholder(Factory* factory, uint32_t index, 262 Handle<Code> CreatePlaceholder(Factory* factory, uint32_t index,
322 Code::Kind kind) { 263 Code::Kind kind) {
323 // Create a placeholder code object and encode the corresponding index in 264 // Create a placeholder code object and encode the corresponding index in
324 // the {constant_pool_offset} field of the code object. 265 // the {constant_pool_offset} field of the code object.
325 // TODO(titzer): placeholder code objects are somewhat dangerous. 266 // TODO(titzer): placeholder code objects are somewhat dangerous.
326 static byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions. 267 static byte buffer[] = {0, 0, 0, 0, 0, 0, 0, 0}; // fake instructions.
327 static CodeDesc desc = {buffer, 8, 8, 0, 0, nullptr, 0, nullptr}; 268 static CodeDesc desc = {buffer, 8, 8, 0, 0, nullptr, 0, nullptr};
328 Handle<Code> code = factory->NewCode(desc, Code::KindField::encode(kind), 269 Handle<Code> code = factory->NewCode(desc, Code::KindField::encode(kind),
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 min_mem_pages(0), 344 min_mem_pages(0),
404 max_mem_pages(0), 345 max_mem_pages(0),
405 mem_export(false), 346 mem_export(false),
406 mem_external(false), 347 mem_external(false),
407 start_function_index(-1), 348 start_function_index(-1),
408 origin(kWasmOrigin), 349 origin(kWasmOrigin),
409 globals_size(0), 350 globals_size(0),
410 indirect_table_size(0), 351 indirect_table_size(0),
411 pending_tasks(new base::Semaphore(0)) {} 352 pending_tasks(new base::Semaphore(0)) {}
412 353
413 static MaybeHandle<JSFunction> ReportFFIError( 354 static MaybeHandle<JSFunction> ReportFFIError(ErrorThrower& thrower,
414 ErrorThrower& thrower, const char* error, uint32_t index, 355 const char* error, uint32_t index,
415 Handle<String> module_name, MaybeHandle<String> function_name) { 356 wasm::WasmName module_name,
416 if (!function_name.is_null()) { 357 wasm::WasmName function_name) {
417 Handle<String> function_name_handle = function_name.ToHandleChecked(); 358 if (!function_name.is_empty()) {
418 thrower.Error("Import #%d module=\"%.*s\" function=\"%.*s\" error: %s", 359 thrower.Error("Import #%d module=\"%.*s\" function=\"%.*s\" error: %s",
419 index, module_name->length(), module_name->ToCString().get(), 360 index, module_name.length(), module_name.start(),
420 function_name_handle->length(), 361 function_name.length(), function_name.start(), error);
421 function_name_handle->ToCString().get(), error);
422 } else { 362 } else {
423 thrower.Error("Import #%d module=\"%.*s\" error: %s", index, 363 thrower.Error("Import #%d module=\"%.*s\" error: %s", index,
424 module_name->length(), module_name->ToCString().get(), error); 364 module_name.length(), module_name.start(), error);
425 } 365 }
426 thrower.Error("Import "); 366 thrower.Error("Import ");
427 return MaybeHandle<JSFunction>(); 367 return MaybeHandle<JSFunction>();
428 } 368 }
429 369
430 static MaybeHandle<JSFunction> LookupFunction( 370 static MaybeHandle<JSFunction> LookupFunction(
431 ErrorThrower& thrower, Factory* factory, Handle<JSReceiver> ffi, 371 ErrorThrower& thrower, Factory* factory, Handle<JSReceiver> ffi,
432 uint32_t index, Handle<String> module_name, 372 uint32_t index, wasm::WasmName module_name, wasm::WasmName function_name) {
433 MaybeHandle<String> function_name) {
434 if (ffi.is_null()) { 373 if (ffi.is_null()) {
435 return ReportFFIError(thrower, "FFI is not an object", index, module_name, 374 return ReportFFIError(thrower, "FFI is not an object", index, module_name,
436 function_name); 375 function_name);
437 } 376 }
438 377
439 // Look up the module first. 378 // Look up the module first.
440 MaybeHandle<Object> result = Object::GetProperty(ffi, module_name); 379 Handle<String> name = factory->InternalizeUtf8String(module_name);
380 MaybeHandle<Object> result = Object::GetProperty(ffi, name);
441 if (result.is_null()) { 381 if (result.is_null()) {
442 return ReportFFIError(thrower, "module not found", index, module_name, 382 return ReportFFIError(thrower, "module not found", index, module_name,
443 function_name); 383 function_name);
444 } 384 }
445 385
446 Handle<Object> module = result.ToHandleChecked(); 386 Handle<Object> module = result.ToHandleChecked();
447 387
448 if (!module->IsJSReceiver()) { 388 if (!module->IsJSReceiver()) {
449 return ReportFFIError(thrower, "module is not an object or function", index, 389 return ReportFFIError(thrower, "module is not an object or function", index,
450 module_name, function_name); 390 module_name, function_name);
451 } 391 }
452 392
453 Handle<Object> function; 393 Handle<Object> function;
454 if (!function_name.is_null()) { 394 if (!function_name.is_empty()) {
455 // Look up the function in the module. 395 // Look up the function in the module.
456 MaybeHandle<Object> result = 396 Handle<String> name = factory->InternalizeUtf8String(function_name);
457 Object::GetProperty(module, function_name.ToHandleChecked()); 397 MaybeHandle<Object> result = Object::GetProperty(module, name);
458 if (result.is_null()) { 398 if (result.is_null()) {
459 return ReportFFIError(thrower, "function not found", index, module_name, 399 return ReportFFIError(thrower, "function not found", index, module_name,
460 function_name); 400 function_name);
461 } 401 }
462 function = result.ToHandleChecked(); 402 function = result.ToHandleChecked();
463 } else { 403 } else {
464 // No function specified. Use the "default export". 404 // No function specified. Use the "default export".
465 function = module; 405 function = module;
466 } 406 }
467 407
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 }; 476 };
537 477
538 // Records statistics on the code generated by compiling WASM functions. 478 // Records statistics on the code generated by compiling WASM functions.
539 struct CodeStats { 479 struct CodeStats {
540 size_t code_size; 480 size_t code_size;
541 size_t reloc_size; 481 size_t reloc_size;
542 482
543 inline CodeStats() : code_size(0), reloc_size(0) {} 483 inline CodeStats() : code_size(0), reloc_size(0) {}
544 484
545 inline void Record(Code* code) { 485 inline void Record(Code* code) {
546 if (FLAG_print_wasm_code_size) { 486 code_size += code->body_size();
547 code_size += code->body_size(); 487 reloc_size += code->relocation_info()->length();
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 }
566 } 488 }
567 489
568 inline void Report() { 490 inline void Report() {
569 if (FLAG_print_wasm_code_size) { 491 PrintF("Total generated wasm code: %zu bytes\n", code_size);
570 PrintF("Total generated wasm code: %zu bytes\n", code_size); 492 PrintF("Total generated wasm reloc: %zu bytes\n", reloc_size);
571 PrintF("Total generated wasm reloc: %zu bytes\n", reloc_size);
572 }
573 } 493 }
574 }; 494 };
575 495
576 Handle<FixedArray> GetImportsMetadata(Factory* factory, 496 bool CompileWrappersToImportedFunctions(
577 const WasmModule* module) { 497 Isolate* isolate, const WasmModule* module, const Handle<JSReceiver> ffi,
578 Handle<FixedArray> ret = factory->NewFixedArray( 498 WasmModuleInstance* instance, ErrorThrower* thrower, Factory* factory) {
579 static_cast<int>(module->import_table.size()), TENURED); 499 if (module->import_table.size() > 0) {
580 for (size_t i = 0; i < module->import_table.size(); ++i) { 500 instance->import_code.reserve(module->import_table.size());
581 const WasmImport& import = module->import_table[i]; 501 for (uint32_t index = 0; index < module->import_table.size(); ++index) {
582 WasmName module_name = module->GetNameOrNull(import.module_name_offset, 502 const WasmImport& import = module->import_table[index];
583 import.module_name_length); 503 WasmName module_name = module->GetNameOrNull(import.module_name_offset,
584 WasmName function_name = module->GetNameOrNull(import.function_name_offset, 504 import.module_name_length);
585 import.function_name_length); 505 WasmName function_name = module->GetNameOrNull(
586 506 import.function_name_offset, import.function_name_length);
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
636 MaybeHandle<JSFunction> function = LookupFunction( 507 MaybeHandle<JSFunction> function = LookupFunction(
637 *thrower, isolate->factory(), ffi, index, module_name, function_name); 508 *thrower, factory, ffi, index, module_name, function_name);
638 if (function.is_null()) return false; 509 if (function.is_null()) return false;
639 510
640 FunctionSig sig(
641 ret_count, param_count,
642 reinterpret_cast<const MachineRepresentation*>(sig_data->data()));
643
644 Handle<Code> code = compiler::CompileWasmToJSWrapper( 511 Handle<Code> code = compiler::CompileWasmToJSWrapper(
645 isolate, function.ToHandleChecked(), &sig, index, module_name, 512 isolate, function.ToHandleChecked(), import.sig, module_name,
646 function_name); 513 function_name);
647 514 instance->import_code[index] = code;
648 imports.push_back(code);
649 } 515 }
650 } 516 }
651 return true; 517 return true;
652 } 518 }
653 519
654 void InitializeParallelCompilation( 520 void InitializeParallelCompilation(
655 Isolate* isolate, const std::vector<WasmFunction>& functions, 521 Isolate* isolate, const std::vector<WasmFunction>& functions,
656 std::vector<compiler::WasmCompilationUnit*>& compilation_units, 522 std::vector<compiler::WasmCompilationUnit*>& compilation_units,
657 ModuleEnv& module_env, ErrorThrower& thrower) { 523 ModuleEnv& module_env, ErrorThrower& thrower) {
658 for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); ++i) { 524 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
810 DCHECK_EQ(table_size * 2, instance->function_table->length()); 676 DCHECK_EQ(table_size * 2, instance->function_table->length());
811 uint32_t populated_table_size = 677 uint32_t populated_table_size =
812 static_cast<uint32_t>(instance->module->function_table.size()); 678 static_cast<uint32_t>(instance->module->function_table.size());
813 for (uint32_t i = 0; i < populated_table_size; ++i) { 679 for (uint32_t i = 0; i < populated_table_size; ++i) {
814 instance->function_table->set( 680 instance->function_table->set(
815 i + table_size, 681 i + table_size,
816 *instance->function_code[instance->module->function_table[i]]); 682 *instance->function_code[instance->module->function_table[i]]);
817 } 683 }
818 } 684 }
819 } 685 }
686 } // namespace
820 687
821 void SetDebugSupport(Factory* factory, Handle<FixedArray> compiled_module, 688 void SetDeoptimizationData(Factory* factory, Handle<JSObject> js_object,
822 Handle<JSObject> js_object) { 689 std::vector<Handle<Code>>& functions) {
823 MaybeHandle<String> module_bytes_string = 690 for (size_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); ++i) {
824 compiled_module->GetValue<String>(kModuleBytes); 691 Handle<Code> code = functions[i];
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);
834 DCHECK(code->deoptimization_data() == nullptr || 692 DCHECK(code->deoptimization_data() == nullptr ||
835 code->deoptimization_data()->length() == 0); 693 code->deoptimization_data()->length() == 0);
836 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED); 694 Handle<FixedArray> deopt_data = factory->NewFixedArray(2, TENURED);
837 if (!js_object.is_null()) { 695 if (!js_object.is_null()) {
838 deopt_data->set(0, *js_object); 696 deopt_data->set(0, *js_object);
839 } 697 }
840 deopt_data->set(1, Smi::FromInt(static_cast<int>(i))); 698 deopt_data->set(1, Smi::FromInt(static_cast<int>(i)));
841 deopt_data->set_length(2); 699 deopt_data->set_length(2);
842 code->set_deoptimization_data(*deopt_data); 700 code->set_deoptimization_data(*deopt_data);
843 } 701 }
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 }
851 } 702 }
852 703
853 bool SetupGlobals(Isolate* isolate, Handle<FixedArray> compiled_module, 704 Handle<FixedArray> WasmModule::CompileFunctions(Isolate* isolate) const {
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 {
991 Factory* factory = isolate->factory(); 705 Factory* factory = isolate->factory();
992 ErrorThrower thrower(isolate, "WasmModule::CompileFunctions()"); 706 ErrorThrower thrower(isolate, "WasmModule::CompileFunctions()");
993 707
994 MaybeHandle<FixedArray> nothing;
995
996 WasmModuleInstance temp_instance_for_compilation(this); 708 WasmModuleInstance temp_instance_for_compilation(this);
997 temp_instance_for_compilation.function_table = 709 temp_instance_for_compilation.function_table =
998 BuildFunctionTable(isolate, this); 710 BuildFunctionTable(isolate, this);
999 temp_instance_for_compilation.context = isolate->native_context(); 711 temp_instance_for_compilation.context = isolate->native_context();
1000 temp_instance_for_compilation.mem_size = GetMinModuleMemSize(this); 712 temp_instance_for_compilation.mem_size = GetMinModuleMemSize(this);
1001 temp_instance_for_compilation.mem_start = nullptr; 713 temp_instance_for_compilation.mem_start = nullptr;
1002 temp_instance_for_compilation.globals_start = nullptr; 714 temp_instance_for_compilation.globals_start = nullptr;
1003 715
1004 HistogramTimerScope wasm_compile_module_time_scope(
1005 isolate->counters()->wasm_compile_module_time());
1006
1007 ModuleEnv module_env; 716 ModuleEnv module_env;
1008 module_env.module = this; 717 module_env.module = this;
1009 module_env.instance = &temp_instance_for_compilation; 718 module_env.instance = &temp_instance_for_compilation;
1010 module_env.origin = origin; 719 module_env.origin = origin;
1011 InitializePlaceholders(factory, &module_env.placeholders, functions.size()); 720 InitializePlaceholders(factory, &module_env.placeholders, functions.size());
1012 721
1013 Handle<FixedArray> compiled_functions = 722 Handle<FixedArray> ret =
1014 factory->NewFixedArray(static_cast<int>(functions.size()), TENURED); 723 factory->NewFixedArray(static_cast<int>(functions.size()), TENURED);
1015 724
1016 temp_instance_for_compilation.import_code.resize(import_table.size()); 725 temp_instance_for_compilation.import_code.resize(import_table.size());
1017 for (uint32_t i = 0; i < import_table.size(); ++i) { 726 for (uint32_t i = 0; i < import_table.size(); ++i) {
1018 temp_instance_for_compilation.import_code[i] = 727 temp_instance_for_compilation.import_code[i] =
1019 CreatePlaceholder(factory, i, Code::WASM_TO_JS_FUNCTION); 728 CreatePlaceholder(factory, i, Code::WASM_TO_JS_FUNCTION);
1020 } 729 }
1021 isolate->counters()->wasm_functions_per_module()->AddSample( 730 isolate->counters()->wasm_functions_per_module()->AddSample(
1022 static_cast<int>(functions.size())); 731 static_cast<int>(functions.size()));
1023 if (FLAG_wasm_num_compilation_tasks != 0) { 732 if (FLAG_wasm_num_compilation_tasks != 0) {
1024 CompileInParallel(isolate, this, 733 CompileInParallel(isolate, this,
1025 temp_instance_for_compilation.function_code, &thrower, 734 temp_instance_for_compilation.function_code, &thrower,
1026 &module_env); 735 &module_env);
1027 } else { 736 } else {
1028 CompileSequentially(isolate, this, 737 CompileSequentially(isolate, this,
1029 temp_instance_for_compilation.function_code, &thrower, 738 temp_instance_for_compilation.function_code, &thrower,
1030 &module_env); 739 &module_env);
1031 } 740 }
1032 if (thrower.error()) return nothing; 741 if (thrower.error()) {
742 return Handle<FixedArray>::null();
743 }
1033 744
1034 LinkModuleFunctions(isolate, temp_instance_for_compilation.function_code); 745 LinkModuleFunctions(isolate, temp_instance_for_compilation.function_code);
1035 746
1036 // At this point, compilation has completed. Update the code table. 747 // At this point, compilation has completed. Update the code table
748 // and record sizes.
1037 for (size_t i = FLAG_skip_compiling_wasm_funcs; 749 for (size_t i = FLAG_skip_compiling_wasm_funcs;
1038 i < temp_instance_for_compilation.function_code.size(); ++i) { 750 i < temp_instance_for_compilation.function_code.size(); ++i) {
1039 Code* code = *temp_instance_for_compilation.function_code[i]; 751 Code* code = *temp_instance_for_compilation.function_code[i];
1040 compiled_functions->set(static_cast<int>(i), code); 752 ret->set(static_cast<int>(i), code);
1041 } 753 }
1042 754
1043 PopulateFunctionTable(&temp_instance_for_compilation); 755 PopulateFunctionTable(&temp_instance_for_compilation);
1044 756
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));
1112 return ret; 757 return ret;
1113 } 758 }
1114 759
1115 // Instantiates a wasm module as a JSObject. 760 // Instantiates a wasm module as a JSObject.
1116 // * allocates a backing store of {mem_size} bytes. 761 // * allocates a backing store of {mem_size} bytes.
1117 // * installs a named property "memory" for that buffer if exported 762 // * installs a named property "memory" for that buffer if exported
1118 // * installs named properties on the object for exported functions 763 // * installs named properties on the object for exported functions
1119 // * compiles wasm code to machine code 764 // * compiles wasm code to machine code
1120 MaybeHandle<JSObject> WasmModule::Instantiate( 765 MaybeHandle<JSObject> WasmModule::Instantiate(
1121 Isolate* isolate, Handle<FixedArray> compiled_module, 766 Isolate* isolate, Handle<JSReceiver> ffi,
1122 Handle<JSReceiver> ffi, Handle<JSArrayBuffer> memory) { 767 Handle<JSArrayBuffer> memory) const {
1123 HistogramTimerScope wasm_instantiate_module_time_scope( 768 HistogramTimerScope wasm_instantiate_module_time_scope(
1124 isolate->counters()->wasm_instantiate_module_time()); 769 isolate->counters()->wasm_instantiate_module_time());
1125 ErrorThrower thrower(isolate, "WasmModule::Instantiate()"); 770 ErrorThrower thrower(isolate, "WasmModule::Instantiate()");
1126 Factory* factory = isolate->factory(); 771 Factory* factory = isolate->factory();
1127 772
1128 CodeStats code_stats; 773 //-------------------------------------------------------------------------
1129 // These fields are compulsory. 774 // Allocate the instance and its JS counterpart.
1130 Handle<FixedArray> code_table = 775 //-------------------------------------------------------------------------
1131 compiled_module->GetValueChecked<FixedArray>(kFunctions);
1132
1133 code_stats.Record(code_table);
1134
1135 MaybeHandle<JSObject> nothing;
1136
1137 Handle<Map> map = factory->NewMap( 776 Handle<Map> map = factory->NewMap(
1138 JS_OBJECT_TYPE, 777 JS_OBJECT_TYPE,
1139 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); 778 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize);
1140 Handle<JSObject> js_object = factory->NewJSObjectFromMap(map, TENURED); 779 WasmModuleInstance instance(this);
1141 js_object->SetInternalField(kWasmModuleCodeTable, *code_table); 780 instance.context = isolate->native_context();
781 instance.js_object = factory->NewJSObjectFromMap(map, TENURED);
1142 782
1143 if (!(SetupInstanceHeap(isolate, compiled_module, js_object, memory, 783 Handle<FixedArray> code_table = CompileFunctions(isolate);
1144 &thrower) && 784 if (code_table.is_null()) return Handle<JSObject>::null();
1145 SetupGlobals(isolate, compiled_module, js_object, &thrower) && 785
1146 SetupImports(isolate, compiled_module, js_object, &thrower, ffi, 786 instance.js_object->SetInternalField(kWasmModuleCodeTable, *code_table);
1147 &code_stats) && 787 size_t module_bytes_len =
1148 SetupExportsObject(compiled_module, isolate, js_object, &thrower, 788 instance.module->module_end - instance.module->module_start;
1149 &code_stats))) { 789 DCHECK_LE(module_bytes_len, static_cast<size_t>(kMaxInt));
1150 return nothing; 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 =
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;
1151 } 801 }
1152 802
1153 SetDebugSupport(factory, compiled_module, js_object); 803 //-------------------------------------------------------------------------
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
821 //-------------------------------------------------------------------------
822 // Allocate the globals area if necessary.
823 //-------------------------------------------------------------------------
824 if (!AllocateGlobals(&thrower, isolate, &instance)) {
825 return MaybeHandle<JSObject>();
826 }
827 if (!instance.globals_buffer.is_null()) {
828 instance.js_object->SetInternalField(kWasmGlobalsArrayBuffer,
829 *instance.globals_buffer);
830 }
831
832 HistogramTimerScope wasm_compile_module_time_scope(
833 isolate->counters()->wasm_compile_module_time());
834
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 }
913 }
914
915 if (FLAG_print_wasm_code_size) {
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);
1154 925
1155 // Run the start function if one was specified. 926 // Run the start function if one was specified.
1156 MaybeHandle<JSFunction> maybe_startup_fct = 927 if (this->start_function_index >= 0) {
1157 compiled_module->GetValue<JSFunction>(kStartupFunction);
1158 if (!maybe_startup_fct.is_null()) {
1159 HandleScope scope(isolate); 928 HandleScope scope(isolate);
1160 Handle<JSFunction> startup_fct = maybe_startup_fct.ToHandleChecked(); 929 uint32_t index = static_cast<uint32_t>(this->start_function_index);
1161 code_stats.Record(startup_fct->code()); 930 Handle<String> name = isolate->factory()->NewStringFromStaticChars("start");
1162 startup_fct->SetInternalField(0, *js_object); 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
1163 // Call the JS function. 935 // Call the JS function.
1164 Handle<Object> undefined = isolate->factory()->undefined_value(); 936 Handle<Object> undefined = isolate->factory()->undefined_value();
1165 MaybeHandle<Object> retval = 937 MaybeHandle<Object> retval =
1166 Execution::Call(isolate, startup_fct, undefined, 0, nullptr); 938 Execution::Call(isolate, jsfunc, undefined, 0, nullptr);
1167 939
1168 if (retval.is_null()) { 940 if (retval.is_null()) {
1169 thrower.Error("WASM.instantiateModule(): start function failed"); 941 thrower.Error("WASM.instantiateModule(): start function failed");
1170 return nothing;
1171 } 942 }
1172 } 943 }
1173 944 return instance.js_object;
1174 code_stats.Report();
1175
1176 DCHECK(wasm::IsWasmObject(*js_object));
1177 return js_object;
1178 } 945 }
1179 946
1180 // TODO(mtrofin): remove this once we move to WASM_DIRECT_CALL 947 // TODO(mtrofin): remove this once we move to WASM_DIRECT_CALL
1181 Handle<Code> ModuleEnv::GetCodeOrPlaceholder(uint32_t index) const { 948 Handle<Code> ModuleEnv::GetCodeOrPlaceholder(uint32_t index) const {
1182 DCHECK(IsValidFunction(index)); 949 DCHECK(IsValidFunction(index));
1183 if (!placeholders.empty()) return placeholders[index]; 950 if (!placeholders.empty()) return placeholders[index];
1184 DCHECK_NOT_NULL(instance); 951 DCHECK_NOT_NULL(instance);
1185 return instance->function_code[index]; 952 return instance->function_code[index];
1186 } 953 }
1187 954
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1222 Handle<Object> name_or_null = 989 Handle<Object> name_or_null =
1223 GetWasmFunctionNameOrNull(isolate, wasm, func_index); 990 GetWasmFunctionNameOrNull(isolate, wasm, func_index);
1224 if (!name_or_null->IsNull(isolate)) { 991 if (!name_or_null->IsNull(isolate)) {
1225 return Handle<String>::cast(name_or_null); 992 return Handle<String>::cast(name_or_null);
1226 } 993 }
1227 return isolate->factory()->NewStringFromStaticChars("<WASM UNNAMED>"); 994 return isolate->factory()->NewStringFromStaticChars("<WASM UNNAMED>");
1228 } 995 }
1229 996
1230 bool IsWasmObject(Object* object) { 997 bool IsWasmObject(Object* object) {
1231 if (!object->IsJSObject()) return false; 998 if (!object->IsJSObject()) return false;
1232
1233 JSObject* obj = JSObject::cast(object); 999 JSObject* obj = JSObject::cast(object);
1234 Isolate* isolate = obj->GetIsolate(); 1000 if (obj->GetInternalFieldCount() != kWasmModuleInternalFieldCount ||
1235 if (obj->GetInternalFieldCount() != kWasmModuleInternalFieldCount) { 1001 !obj->GetInternalField(kWasmModuleCodeTable)->IsFixedArray() ||
1002 !obj->GetInternalField(kWasmMemArrayBuffer)->IsJSArrayBuffer() ||
1003 !obj->GetInternalField(kWasmFunctionNamesArray)->IsByteArray() ||
1004 !obj->GetInternalField(kWasmModuleBytesString)->IsSeqOneByteString()) {
1236 return false; 1005 return false;
1237 } 1006 }
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;
1238 1012
1239 Object* mem = obj->GetInternalField(kWasmMemArrayBuffer); 1013 // All checks passed.
1240 if (obj->GetInternalField(kWasmModuleCodeTable)->IsFixedArray() && 1014 return true;
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;
1257 } 1015 }
1258 1016
1259 SeqOneByteString* GetWasmBytes(JSObject* wasm) { 1017 SeqOneByteString* GetWasmBytes(JSObject* wasm) {
1260 return SeqOneByteString::cast(wasm->GetInternalField(kWasmModuleBytesString)); 1018 return SeqOneByteString::cast(wasm->GetInternalField(kWasmModuleBytesString));
1261 } 1019 }
1262 1020
1263 WasmDebugInfo* GetDebugInfo(JSObject* wasm) { 1021 WasmDebugInfo* GetDebugInfo(JSObject* wasm) {
1264 Object* info = wasm->GetInternalField(kWasmDebugInfo); 1022 Object* info = wasm->GetInternalField(kWasmDebugInfo);
1265 if (!info->IsUndefined(wasm->GetIsolate())) return WasmDebugInfo::cast(info); 1023 if (!info->IsUndefined(wasm->GetIsolate())) return WasmDebugInfo::cast(info);
1266 Handle<WasmDebugInfo> new_info = WasmDebugInfo::New(handle(wasm)); 1024 Handle<WasmDebugInfo> new_info = WasmDebugInfo::New(handle(wasm));
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
1322 } 1080 }
1323 1081
1324 if (module->import_table.size() > 0) { 1082 if (module->import_table.size() > 0) {
1325 thrower.Error("Not supported: module has imports."); 1083 thrower.Error("Not supported: module has imports.");
1326 } 1084 }
1327 if (module->export_table.size() == 0) { 1085 if (module->export_table.size() == 0) {
1328 thrower.Error("Not supported: module has no exports."); 1086 thrower.Error("Not supported: module has no exports.");
1329 } 1087 }
1330 1088
1331 if (thrower.error()) return -1; 1089 if (thrower.error()) return -1;
1332 MaybeHandle<FixedArray> compiled_module = module->CompileFunctions(isolate);
1333 1090
1334 if (compiled_module.is_null()) return -1;
1335 Handle<JSObject> instance = 1091 Handle<JSObject> instance =
1336 module 1092 module
1337 ->Instantiate(isolate, compiled_module.ToHandleChecked(), 1093 ->Instantiate(isolate, Handle<JSReceiver>::null(),
1338 Handle<JSReceiver>::null(),
1339 Handle<JSArrayBuffer>::null()) 1094 Handle<JSArrayBuffer>::null())
1340 .ToHandleChecked(); 1095 .ToHandleChecked();
1341 1096
1342 Handle<Name> exports = isolate->factory()->InternalizeUtf8String("exports"); 1097 Handle<Name> exports = isolate->factory()->InternalizeUtf8String("exports");
1343 Handle<JSObject> exports_object = Handle<JSObject>::cast( 1098 Handle<JSObject> exports_object = Handle<JSObject>::cast(
1344 JSObject::GetProperty(instance, exports).ToHandleChecked()); 1099 JSObject::GetProperty(instance, exports).ToHandleChecked());
1345 Handle<Name> main_name = isolate->factory()->NewStringFromStaticChars("main"); 1100 Handle<Name> main_name = isolate->factory()->NewStringFromStaticChars("main");
1346 PropertyDescriptor desc; 1101 PropertyDescriptor desc;
1347 Maybe<bool> property_found = JSReceiver::GetOwnPropertyDescriptor( 1102 Maybe<bool> property_found = JSReceiver::GetOwnPropertyDescriptor(
1348 isolate, exports_object, main_name, &desc); 1103 isolate, exports_object, main_name, &desc);
(...skipping 19 matching lines...) Expand all
1368 return static_cast<int32_t>(HeapNumber::cast(*result)->value()); 1123 return static_cast<int32_t>(HeapNumber::cast(*result)->value());
1369 } 1124 }
1370 thrower.Error("WASM.compileRun() failed: Return value should be number"); 1125 thrower.Error("WASM.compileRun() failed: Return value should be number");
1371 return -1; 1126 return -1;
1372 } 1127 }
1373 1128
1374 } // namespace testing 1129 } // namespace testing
1375 } // namespace wasm 1130 } // namespace wasm
1376 } // namespace internal 1131 } // namespace internal
1377 } // namespace v8 1132 } // 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