// Copyright 2015 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_WASM_MODULE_H_ #define V8_WASM_MODULE_H_ #include #include "src/api.h" #include "src/debug/debug-interface.h" #include "src/globals.h" #include "src/handles.h" #include "src/parsing/preparse-data.h" #include "src/wasm/managed.h" #include "src/wasm/signature-map.h" #include "src/wasm/wasm-opcodes.h" namespace v8 { namespace internal { class WasmCompiledModule; class WasmDebugInfo; class WasmModuleObject; class WasmInstanceObject; class WasmMemoryObject; namespace compiler { class CallDescriptor; class WasmCompilationUnit; } namespace wasm { class ErrorThrower; const uint32_t kWasmMagic = 0x6d736100; const uint32_t kWasmVersion = 0x01; const uint8_t kWasmFunctionTypeForm = 0x60; const uint8_t kWasmAnyFunctionTypeForm = 0x70; enum WasmSectionCode { kUnknownSectionCode = 0, // code for unknown sections kTypeSectionCode = 1, // Function signature declarations kImportSectionCode = 2, // Import declarations kFunctionSectionCode = 3, // Function declarations kTableSectionCode = 4, // Indirect function table and other tables kMemorySectionCode = 5, // Memory attributes kGlobalSectionCode = 6, // Global declarations kExportSectionCode = 7, // Exports kStartSectionCode = 8, // Start function declaration kElementSectionCode = 9, // Elements section kCodeSectionCode = 10, // Function code kDataSectionCode = 11, // Data segments kNameSectionCode = 12, // Name section (encoded as a string) }; inline bool IsValidSectionCode(uint8_t byte) { return kTypeSectionCode <= byte && byte <= kDataSectionCode; } const char* SectionName(WasmSectionCode code); // Constants for fixed-size elements within a module. static const uint8_t kResizableMaximumFlag = 1; static const int32_t kInvalidFunctionIndex = -1; enum WasmExternalKind { kExternalFunction = 0, kExternalTable = 1, kExternalMemory = 2, kExternalGlobal = 3 }; // Representation of an initializer expression. struct WasmInitExpr { enum WasmInitKind { kNone, kGlobalIndex, kI32Const, kI64Const, kF32Const, kF64Const } kind; union { int32_t i32_const; int64_t i64_const; float f32_const; double f64_const; uint32_t global_index; } val; WasmInitExpr() : kind(kNone) {} explicit WasmInitExpr(int32_t v) : kind(kI32Const) { val.i32_const = v; } explicit WasmInitExpr(int64_t v) : kind(kI64Const) { val.i64_const = v; } explicit WasmInitExpr(float v) : kind(kF32Const) { val.f32_const = v; } explicit WasmInitExpr(double v) : kind(kF64Const) { val.f64_const = v; } WasmInitExpr(WasmInitKind kind, uint32_t global_index) : kind(kGlobalIndex) { val.global_index = global_index; } }; // Static representation of a WASM function. struct WasmFunction { FunctionSig* sig; // signature of the function. uint32_t func_index; // index into the function table. uint32_t sig_index; // index into the signature table. uint32_t name_offset; // offset in the module bytes of the name, if any. uint32_t name_length; // length in bytes of the name. uint32_t code_start_offset; // offset in the module bytes of code start. uint32_t code_end_offset; // offset in the module bytes of code end. bool imported; bool exported; }; // Static representation of a wasm global variable. struct WasmGlobal { ValueType type; // type of the global. bool mutability; // {true} if mutable. WasmInitExpr init; // the initialization expression of the global. uint32_t offset; // offset into global memory. bool imported; // true if imported. bool exported; // true if exported. }; // Static representation of a wasm data segment. struct WasmDataSegment { WasmInitExpr dest_addr; // destination memory address of the data. uint32_t source_offset; // start offset in the module bytes. uint32_t source_size; // end offset in the module bytes. }; // Static representation of a wasm indirect call table. struct WasmIndirectFunctionTable { uint32_t min_size; // minimum table size. uint32_t max_size; // maximum table size. bool has_max; // true if there is a maximum size. // TODO(titzer): Move this to WasmInstance. Needed by interpreter only. std::vector values; // function table, -1 indicating invalid. bool imported; // true if imported. bool exported; // true if exported. SignatureMap map; // canonicalizing map for sig indexes. }; // Static representation of how to initialize a table. struct WasmTableInit { uint32_t table_index; WasmInitExpr offset; std::vector entries; }; // Static representation of a WASM import. struct WasmImport { uint32_t module_name_length; // length in bytes of the module name. uint32_t module_name_offset; // offset in module bytes of the module name. uint32_t field_name_length; // length in bytes of the import name. uint32_t field_name_offset; // offset in module bytes of the import name. WasmExternalKind kind; // kind of the import. uint32_t index; // index into the respective space. }; // Static representation of a WASM export. struct WasmExport { uint32_t name_length; // length in bytes of the exported name. uint32_t name_offset; // offset in module bytes of the name to export. WasmExternalKind kind; // kind of the export. uint32_t index; // index into the respective space. }; enum ModuleOrigin : uint8_t { kWasmOrigin, kAsmJsOrigin }; struct ModuleWireBytes; // Static representation of a module. struct V8_EXPORT_PRIVATE WasmModule { static const uint32_t kPageSize = 0x10000; // Page size, 64kb. static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb Zone* owned_zone; uint32_t min_mem_pages = 0; // minimum size of the memory in 64k pages uint32_t max_mem_pages = 0; // maximum size of the memory in 64k pages bool has_max_mem = false; // try if a maximum memory size exists bool has_memory = false; // true if the memory was defined or imported bool mem_export = false; // true if the memory is exported // TODO(wasm): reconcile start function index being an int with // the fact that we index on uint32_t, so we may technically not be // able to represent some start_function_index -es. int start_function_index = -1; // start function, if any ModuleOrigin origin = kWasmOrigin; // origin of the module std::vector globals; // globals in this module. uint32_t globals_size = 0; // size of globals table. uint32_t num_imported_functions = 0; // number of imported functions. uint32_t num_declared_functions = 0; // number of declared functions. uint32_t num_exported_functions = 0; // number of exported functions. std::vector signatures; // signatures in this module. std::vector functions; // functions in this module. std::vector data_segments; // data segments in this module. std::vector function_tables; // function tables. std::vector import_table; // import table. std::vector export_table; // export table. std::vector table_inits; // initializations of tables // We store the semaphore here to extend its lifetime. In pending_tasks; WasmModule() : WasmModule(nullptr) {} WasmModule(Zone* owned_zone); ~WasmModule() { if (owned_zone) delete owned_zone; } // Creates a new instantiation of the module in the given isolate. static MaybeHandle Instantiate( Isolate* isolate, ErrorThrower* thrower, Handle wasm_module, Handle ffi, Handle memory = Handle::null()); MaybeHandle CompileFunctions( Isolate* isolate, Handle> module_wrapper, ErrorThrower* thrower, const ModuleWireBytes& wire_bytes, Handle