OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_WASM_MODULE_H_ | 5 #ifndef V8_WASM_MODULE_H_ |
6 #define V8_WASM_MODULE_H_ | 6 #define V8_WASM_MODULE_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "src/api.h" | 10 #include "src/api.h" |
11 #include "src/globals.h" | 11 #include "src/globals.h" |
12 #include "src/handles.h" | 12 #include "src/handles.h" |
13 #include "src/parsing/preparse-data.h" | 13 #include "src/parsing/preparse-data.h" |
14 | 14 |
15 #include "src/wasm/managed.h" | 15 #include "src/wasm/managed.h" |
16 #include "src/wasm/signature-map.h" | 16 #include "src/wasm/signature-map.h" |
17 #include "src/wasm/wasm-opcodes.h" | 17 #include "src/wasm/wasm-opcodes.h" |
18 | 18 |
19 namespace v8 { | 19 namespace v8 { |
20 namespace internal { | 20 namespace internal { |
21 | 21 |
| 22 class WasmCompiledModule; |
| 23 class WasmDebugInfo; |
| 24 class WasmModuleObject; |
| 25 |
22 namespace compiler { | 26 namespace compiler { |
23 class CallDescriptor; | 27 class CallDescriptor; |
24 class WasmCompilationUnit; | 28 class WasmCompilationUnit; |
25 } | 29 } |
26 | 30 |
27 namespace wasm { | 31 namespace wasm { |
28 class ErrorThrower; | 32 class ErrorThrower; |
29 | 33 |
30 const size_t kMaxModuleSize = 1024 * 1024 * 1024; | 34 const size_t kMaxModuleSize = 1024 * 1024 * 1024; |
31 const size_t kMaxFunctionSize = 128 * 1024; | 35 const size_t kMaxFunctionSize = 128 * 1024; |
(...skipping 19 matching lines...) Loading... |
51 kDataSectionCode = 11, // Data segments | 55 kDataSectionCode = 11, // Data segments |
52 kNameSectionCode = 12, // Name section (encoded as a string) | 56 kNameSectionCode = 12, // Name section (encoded as a string) |
53 }; | 57 }; |
54 | 58 |
55 inline bool IsValidSectionCode(uint8_t byte) { | 59 inline bool IsValidSectionCode(uint8_t byte) { |
56 return kTypeSectionCode <= byte && byte <= kDataSectionCode; | 60 return kTypeSectionCode <= byte && byte <= kDataSectionCode; |
57 } | 61 } |
58 | 62 |
59 const char* SectionName(WasmSectionCode code); | 63 const char* SectionName(WasmSectionCode code); |
60 | 64 |
61 class WasmDebugInfo; | |
62 | |
63 // Constants for fixed-size elements within a module. | 65 // Constants for fixed-size elements within a module. |
64 static const uint32_t kMaxReturnCount = 1; | 66 static const uint32_t kMaxReturnCount = 1; |
65 static const uint8_t kResizableMaximumFlag = 1; | 67 static const uint8_t kResizableMaximumFlag = 1; |
66 static const int32_t kInvalidFunctionIndex = -1; | 68 static const int32_t kInvalidFunctionIndex = -1; |
67 | 69 |
68 enum WasmExternalKind { | 70 enum WasmExternalKind { |
69 kExternalFunction = 0, | 71 kExternalFunction = 0, |
70 kExternalTable = 1, | 72 kExternalTable = 1, |
71 kExternalMemory = 2, | 73 kExternalMemory = 2, |
72 kExternalGlobal = 3 | 74 kExternalGlobal = 3 |
(...skipping 90 matching lines...) Loading... |
163 // Static representation of a WASM export. | 165 // Static representation of a WASM export. |
164 struct WasmExport { | 166 struct WasmExport { |
165 uint32_t name_length; // length in bytes of the exported name. | 167 uint32_t name_length; // length in bytes of the exported name. |
166 uint32_t name_offset; // offset in module bytes of the name to export. | 168 uint32_t name_offset; // offset in module bytes of the name to export. |
167 WasmExternalKind kind; // kind of the export. | 169 WasmExternalKind kind; // kind of the export. |
168 uint32_t index; // index into the respective space. | 170 uint32_t index; // index into the respective space. |
169 }; | 171 }; |
170 | 172 |
171 enum ModuleOrigin { kWasmOrigin, kAsmJsOrigin }; | 173 enum ModuleOrigin { kWasmOrigin, kAsmJsOrigin }; |
172 | 174 |
173 class WasmCompiledModule; | |
174 | |
175 // Static representation of a module. | 175 // Static representation of a module. |
176 struct V8_EXPORT_PRIVATE WasmModule { | 176 struct V8_EXPORT_PRIVATE WasmModule { |
177 static const uint32_t kPageSize = 0x10000; // Page size, 64kb. | 177 static const uint32_t kPageSize = 0x10000; // Page size, 64kb. |
178 static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb | 178 static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb |
179 static const size_t kV8MaxPages = 16384; // Maximum memory size = 1gb | 179 static const size_t kV8MaxPages = 16384; // Maximum memory size = 1gb |
180 static const size_t kSpecMaxPages = 65536; // Maximum according to the spec | 180 static const size_t kSpecMaxPages = 65536; // Maximum according to the spec |
181 static const size_t kV8MaxTableSize = 16 * 1024 * 1024; | 181 static const size_t kV8MaxTableSize = 16 * 1024 * 1024; |
182 | 182 |
183 Zone* owned_zone; | 183 Zone* owned_zone; |
184 const byte* module_start = nullptr; // starting address for the module bytes | 184 const byte* module_start = nullptr; // starting address for the module bytes |
(...skipping 160 matching lines...) Loading... |
345 const WasmFunction* function_; | 345 const WasmFunction* function_; |
346 const WasmModule* module_; | 346 const WasmModule* module_; |
347 WasmFunctionName(const WasmFunction* function, const ModuleEnv* menv) | 347 WasmFunctionName(const WasmFunction* function, const ModuleEnv* menv) |
348 : function_(function), module_(menv ? menv->module : nullptr) {} | 348 : function_(function), module_(menv ? menv->module : nullptr) {} |
349 }; | 349 }; |
350 | 350 |
351 std::ostream& operator<<(std::ostream& os, const WasmModule& module); | 351 std::ostream& operator<<(std::ostream& os, const WasmModule& module); |
352 std::ostream& operator<<(std::ostream& os, const WasmFunction& function); | 352 std::ostream& operator<<(std::ostream& os, const WasmFunction& function); |
353 std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name); | 353 std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name); |
354 | 354 |
355 class WasmCompiledModule : public FixedArray { | |
356 public: | |
357 static WasmCompiledModule* cast(Object* fixed_array) { | |
358 SLOW_DCHECK(IsWasmCompiledModule(fixed_array)); | |
359 return reinterpret_cast<WasmCompiledModule*>(fixed_array); | |
360 } | |
361 | |
362 #define WCM_OBJECT_OR_WEAK(TYPE, NAME, ID) \ | |
363 Handle<TYPE> NAME() const { return handle(ptr_to_##NAME()); } \ | |
364 \ | |
365 MaybeHandle<TYPE> maybe_##NAME() const { \ | |
366 if (has_##NAME()) return NAME(); \ | |
367 return MaybeHandle<TYPE>(); \ | |
368 } \ | |
369 \ | |
370 TYPE* ptr_to_##NAME() const { \ | |
371 Object* obj = get(ID); \ | |
372 if (!obj->Is##TYPE()) return nullptr; \ | |
373 return TYPE::cast(obj); \ | |
374 } \ | |
375 \ | |
376 void set_##NAME(Handle<TYPE> value) { set_ptr_to_##NAME(*value); } \ | |
377 \ | |
378 void set_ptr_to_##NAME(TYPE* value) { set(ID, value); } \ | |
379 \ | |
380 bool has_##NAME() const { return get(ID)->Is##TYPE(); } \ | |
381 \ | |
382 void reset_##NAME() { set_undefined(ID); } | |
383 | |
384 #define WCM_OBJECT(TYPE, NAME) WCM_OBJECT_OR_WEAK(TYPE, NAME, kID_##NAME) | |
385 | |
386 #define WCM_SMALL_NUMBER(TYPE, NAME) \ | |
387 TYPE NAME() const { \ | |
388 return static_cast<TYPE>(Smi::cast(get(kID_##NAME))->value()); \ | |
389 } \ | |
390 void set_##NAME(TYPE value) { set(kID_##NAME, Smi::FromInt(value)); } | |
391 | |
392 #define WCM_WEAK_LINK(TYPE, NAME) \ | |
393 WCM_OBJECT_OR_WEAK(WeakCell, weak_##NAME, kID_##NAME); \ | |
394 \ | |
395 Handle<TYPE> NAME() const { \ | |
396 return handle(TYPE::cast(weak_##NAME()->value())); \ | |
397 } | |
398 | |
399 #define CORE_WCM_PROPERTY_TABLE(MACRO) \ | |
400 MACRO(OBJECT, FixedArray, code_table) \ | |
401 MACRO(OBJECT, Foreign, module_wrapper) \ | |
402 MACRO(OBJECT, SeqOneByteString, module_bytes) \ | |
403 MACRO(OBJECT, Script, asm_js_script) \ | |
404 MACRO(OBJECT, FixedArray, function_tables) \ | |
405 MACRO(OBJECT, FixedArray, empty_function_tables) \ | |
406 MACRO(OBJECT, ByteArray, asm_js_offset_tables) \ | |
407 MACRO(OBJECT, JSArrayBuffer, memory) \ | |
408 MACRO(SMALL_NUMBER, uint32_t, min_mem_pages) \ | |
409 MACRO(SMALL_NUMBER, uint32_t, max_mem_pages) \ | |
410 MACRO(WEAK_LINK, WasmCompiledModule, next_instance) \ | |
411 MACRO(WEAK_LINK, WasmCompiledModule, prev_instance) \ | |
412 MACRO(WEAK_LINK, JSObject, owning_instance) \ | |
413 MACRO(WEAK_LINK, JSObject, wasm_module) | |
414 | |
415 #if DEBUG | |
416 #define DEBUG_ONLY_TABLE(MACRO) MACRO(SMALL_NUMBER, uint32_t, instance_id) | |
417 #else | |
418 #define DEBUG_ONLY_TABLE(IGNORE) | |
419 uint32_t instance_id() const { return -1; } | |
420 #endif | |
421 | |
422 #define WCM_PROPERTY_TABLE(MACRO) \ | |
423 CORE_WCM_PROPERTY_TABLE(MACRO) \ | |
424 DEBUG_ONLY_TABLE(MACRO) | |
425 | |
426 private: | |
427 enum PropertyIndices { | |
428 #define INDICES(IGNORE1, IGNORE2, NAME) kID_##NAME, | |
429 WCM_PROPERTY_TABLE(INDICES) Count | |
430 #undef INDICES | |
431 }; | |
432 | |
433 public: | |
434 static Handle<WasmCompiledModule> New( | |
435 Isolate* isolate, Handle<Managed<WasmModule>> module_wrapper); | |
436 | |
437 static Handle<WasmCompiledModule> Clone(Isolate* isolate, | |
438 Handle<WasmCompiledModule> module) { | |
439 Handle<WasmCompiledModule> ret = Handle<WasmCompiledModule>::cast( | |
440 isolate->factory()->CopyFixedArray(module)); | |
441 ret->InitId(); | |
442 ret->reset_weak_owning_instance(); | |
443 ret->reset_weak_next_instance(); | |
444 ret->reset_weak_prev_instance(); | |
445 return ret; | |
446 } | |
447 | |
448 uint32_t mem_size() const { | |
449 return has_memory() ? memory()->byte_length()->Number() | |
450 : default_mem_size(); | |
451 } | |
452 | |
453 uint32_t default_mem_size() const { | |
454 return min_mem_pages() * WasmModule::kPageSize; | |
455 } | |
456 | |
457 #define DECLARATION(KIND, TYPE, NAME) WCM_##KIND(TYPE, NAME) | |
458 WCM_PROPERTY_TABLE(DECLARATION) | |
459 #undef DECLARATION | |
460 | |
461 static bool IsWasmCompiledModule(Object* obj); | |
462 | |
463 void PrintInstancesChain(); | |
464 | |
465 static void RecreateModuleWrapper(Isolate* isolate, | |
466 Handle<FixedArray> compiled_module); | |
467 | |
468 private: | |
469 void InitId(); | |
470 | |
471 DISALLOW_IMPLICIT_CONSTRUCTORS(WasmCompiledModule); | |
472 }; | |
473 | |
474 // Extract a function name from the given wasm object. | 355 // Extract a function name from the given wasm object. |
475 // Returns "<WASM UNNAMED>" if the function is unnamed or the name is not a | 356 // Returns "<WASM UNNAMED>" if the function is unnamed or the name is not a |
476 // valid UTF-8 string. | 357 // valid UTF-8 string. |
477 Handle<String> GetWasmFunctionName(Isolate* isolate, Handle<Object> wasm, | 358 Handle<String> GetWasmFunctionName(Isolate* isolate, Handle<Object> wasm, |
478 uint32_t func_index); | 359 uint32_t func_index); |
479 | 360 |
480 // Extract a function name from the given wasm object. | 361 // Extract a function name from the given wasm object. |
481 // Returns a null handle if the function is unnamed or the name is not a valid | 362 // Returns a null handle if the function is unnamed or the name is not a valid |
482 // UTF-8 string. | 363 // UTF-8 string. |
483 Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm, | 364 Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm, |
(...skipping 44 matching lines...) Loading... |
528 | 409 |
529 // Populates a function table by replacing function indices with handles to | 410 // Populates a function table by replacing function indices with handles to |
530 // the compiled code. | 411 // the compiled code. |
531 void PopulateFunctionTable(Handle<FixedArray> table, uint32_t table_size, | 412 void PopulateFunctionTable(Handle<FixedArray> table, uint32_t table_size, |
532 const std::vector<Handle<Code>>* code_table); | 413 const std::vector<Handle<Code>>* code_table); |
533 | 414 |
534 Handle<JSObject> CreateWasmModuleObject( | 415 Handle<JSObject> CreateWasmModuleObject( |
535 Isolate* isolate, Handle<WasmCompiledModule> compiled_module, | 416 Isolate* isolate, Handle<WasmCompiledModule> compiled_module, |
536 ModuleOrigin origin); | 417 ModuleOrigin origin); |
537 | 418 |
538 V8_EXPORT_PRIVATE MaybeHandle<JSObject> CreateModuleObjectFromBytes( | 419 V8_EXPORT_PRIVATE MaybeHandle<WasmModuleObject> CreateModuleObjectFromBytes( |
539 Isolate* isolate, const byte* start, const byte* end, ErrorThrower* thrower, | 420 Isolate* isolate, const byte* start, const byte* end, ErrorThrower* thrower, |
540 ModuleOrigin origin, Handle<Script> asm_js_script, | 421 ModuleOrigin origin, Handle<Script> asm_js_script, |
541 const byte* asm_offset_tables_start, const byte* asm_offset_tables_end); | 422 const byte* asm_offset_tables_start, const byte* asm_offset_tables_end); |
542 | 423 |
543 V8_EXPORT_PRIVATE bool ValidateModuleBytes(Isolate* isolate, const byte* start, | 424 V8_EXPORT_PRIVATE bool ValidateModuleBytes(Isolate* isolate, const byte* start, |
544 const byte* end, | 425 const byte* end, |
545 ErrorThrower* thrower, | 426 ErrorThrower* thrower, |
546 ModuleOrigin origin); | 427 ModuleOrigin origin); |
547 | 428 |
548 // Get the number of imported functions for a WASM instance. | 429 // Get the number of imported functions for a WASM instance. |
(...skipping 21 matching lines...) Loading... |
570 int instance_count); | 451 int instance_count); |
571 void ValidateModuleState(Isolate* isolate, Handle<JSObject> wasm_module); | 452 void ValidateModuleState(Isolate* isolate, Handle<JSObject> wasm_module); |
572 void ValidateOrphanedInstance(Isolate* isolate, Handle<JSObject> instance); | 453 void ValidateOrphanedInstance(Isolate* isolate, Handle<JSObject> instance); |
573 | 454 |
574 } // namespace testing | 455 } // namespace testing |
575 } // namespace wasm | 456 } // namespace wasm |
576 } // namespace internal | 457 } // namespace internal |
577 } // namespace v8 | 458 } // namespace v8 |
578 | 459 |
579 #endif // V8_WASM_MODULE_H_ | 460 #endif // V8_WASM_MODULE_H_ |
OLD | NEW |