diff options
Diffstat (limited to 'deps/v8/src/wasm/wasm-module.h')
-rw-r--r-- | deps/v8/src/wasm/wasm-module.h | 385 |
1 files changed, 260 insertions, 125 deletions
diff --git a/deps/v8/src/wasm/wasm-module.h b/deps/v8/src/wasm/wasm-module.h index 0c3df51d76..ac75042392 100644 --- a/deps/v8/src/wasm/wasm-module.h +++ b/deps/v8/src/wasm/wasm-module.h @@ -27,84 +27,71 @@ const size_t kMaxModuleSize = 1024 * 1024 * 1024; const size_t kMaxFunctionSize = 128 * 1024; const size_t kMaxStringSize = 256; const uint32_t kWasmMagic = 0x6d736100; -const uint32_t kWasmVersion = 0x0b; +const uint32_t kWasmVersion = 0x0c; + const uint8_t kWasmFunctionTypeForm = 0x40; +const uint8_t kWasmAnyFunctionTypeForm = 0x20; + +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; +} -// WebAssembly sections are named as strings in the binary format, but -// internally V8 uses an enum to handle them. -// -// Entries have the form F(enumerator, string). -#define FOR_EACH_WASM_SECTION_TYPE(F) \ - F(Signatures, 1, "type") \ - F(ImportTable, 2, "import") \ - F(FunctionSignatures, 3, "function") \ - F(FunctionTable, 4, "table") \ - F(Memory, 5, "memory") \ - F(ExportTable, 6, "export") \ - F(StartFunction, 7, "start") \ - F(FunctionBodies, 8, "code") \ - F(DataSegments, 9, "data") \ - F(Names, 10, "name") \ - F(Globals, 0, "global") \ - F(End, 0, "end") - -// Contants for the above section types: {LEB128 length, characters...}. -#define WASM_SECTION_MEMORY 6, 'm', 'e', 'm', 'o', 'r', 'y' -#define WASM_SECTION_SIGNATURES 4, 't', 'y', 'p', 'e' -#define WASM_SECTION_GLOBALS 6, 'g', 'l', 'o', 'b', 'a', 'l' -#define WASM_SECTION_DATA_SEGMENTS 4, 'd', 'a', 't', 'a' -#define WASM_SECTION_FUNCTION_TABLE 5, 't', 'a', 'b', 'l', 'e' -#define WASM_SECTION_END 3, 'e', 'n', 'd' -#define WASM_SECTION_START_FUNCTION 5, 's', 't', 'a', 'r', 't' -#define WASM_SECTION_IMPORT_TABLE 6, 'i', 'm', 'p', 'o', 'r', 't' -#define WASM_SECTION_EXPORT_TABLE 6, 'e', 'x', 'p', 'o', 'r', 't' -#define WASM_SECTION_FUNCTION_SIGNATURES \ - 8, 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n' -#define WASM_SECTION_FUNCTION_BODIES 4, 'c', 'o', 'd', 'e' -#define WASM_SECTION_NAMES 4, 'n', 'a', 'm', 'e' - -// Constants for the above section headers' size (LEB128 + characters). -#define WASM_SECTION_MEMORY_SIZE ((size_t)7) -#define WASM_SECTION_SIGNATURES_SIZE ((size_t)5) -#define WASM_SECTION_GLOBALS_SIZE ((size_t)7) -#define WASM_SECTION_DATA_SEGMENTS_SIZE ((size_t)5) -#define WASM_SECTION_FUNCTION_TABLE_SIZE ((size_t)6) -#define WASM_SECTION_END_SIZE ((size_t)4) -#define WASM_SECTION_START_FUNCTION_SIZE ((size_t)6) -#define WASM_SECTION_IMPORT_TABLE_SIZE ((size_t)7) -#define WASM_SECTION_EXPORT_TABLE_SIZE ((size_t)7) -#define WASM_SECTION_FUNCTION_SIGNATURES_SIZE ((size_t)9) -#define WASM_SECTION_FUNCTION_BODIES_SIZE ((size_t)5) -#define WASM_SECTION_NAMES_SIZE ((size_t)5) +const char* SectionName(WasmSectionCode code); class WasmDebugInfo; -struct WasmSection { - enum class Code : uint32_t { -#define F(enumerator, order, string) enumerator, - FOR_EACH_WASM_SECTION_TYPE(F) -#undef F - Max - }; - static WasmSection::Code begin(); - static WasmSection::Code end(); - static WasmSection::Code next(WasmSection::Code code); - static const char* getName(Code code); - static int getOrder(Code code); - static size_t getNameLength(Code code); - static WasmSection::Code lookup(const byte* string, uint32_t length); +// Constants for fixed-size elements within a module. +static const uint32_t kMaxReturnCount = 1; +static const uint8_t kResizableMaximumFlag = 1; +static const int32_t kInvalidFunctionIndex = -1; + +enum WasmExternalKind { + kExternalFunction = 0, + kExternalTable = 1, + kExternalMemory = 2, + kExternalGlobal = 3 }; -enum WasmFunctionDeclBit { - kDeclFunctionName = 0x01, - kDeclFunctionExport = 0x08 +// 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; }; -// Constants for fixed-size elements within a module. -static const size_t kDeclMemorySize = 3; -static const size_t kDeclDataSegmentSize = 13; - -static const uint32_t kMaxReturnCount = 1; +#define NO_INIT \ + { \ + WasmInitExpr::kNone, { 0u } \ + } // Static representation of a WASM function. struct WasmFunction { @@ -115,54 +102,69 @@ struct WasmFunction { 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. -}; - -// Static representation of an imported WASM function. -struct WasmImport { - FunctionSig* sig; // signature of the function. - uint32_t sig_index; // index into the signature table. - uint32_t module_name_offset; // offset in module bytes of the module name. - uint32_t module_name_length; // length in bytes of the module name. - uint32_t function_name_offset; // offset in module bytes of the import name. - uint32_t function_name_length; // length in bytes of the import name. -}; - -// Static representation of an exported WASM function. -struct WasmExport { - uint32_t func_index; // index into the function table. - uint32_t name_offset; // offset in module bytes of the name to export. - uint32_t name_length; // length in bytes of the exported name. + bool imported; + bool exported; }; // Static representation of a wasm global variable. struct WasmGlobal { - uint32_t name_offset; // offset in the module bytes of the name, if any. - uint32_t name_length; // length in bytes of the global name. LocalType type; // type of the global. - uint32_t offset; // offset from beginning of globals area. - bool exported; // true if this global is exported. + 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 { - uint32_t dest_addr; // destination memory address of the data. + 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. - bool init; // true if loaded upon instantiation. }; // Static representation of a wasm indirect call table. struct WasmIndirectFunctionTable { - uint32_t size; // initial table size. - uint32_t max_size; // maximum table size. - std::vector<uint16_t> values; // function table. + uint32_t size; // initial table size. + uint32_t max_size; // maximum table size. + std::vector<int32_t> values; // function table, -1 indicating invalid. + bool imported; // true if imported. + bool exported; // true if exported. +}; + +// Static representation of how to initialize a table. +struct WasmTableInit { + uint32_t table_index; + WasmInitExpr offset; + std::vector<uint32_t> 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 { kWasmOrigin, kAsmJsOrigin }; +class WasmCompiledModule; + // Static representation of a module. struct WasmModule { static const uint32_t kPageSize = 0x10000; // Page size, 64kb. + static const uint32_t kMaxLegalPages = 65536; // Maximum legal pages static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb static const uint32_t kMaxMemPages = 16384; // Maximum memory size = 1gb @@ -171,7 +173,6 @@ struct WasmModule { uint32_t min_mem_pages; // minimum size of the memory in 64k pages. uint32_t max_mem_pages; // maximum size of the memory in 64k pages. bool mem_export; // true if the memory is exported. - bool mem_external; // true if the memory is external. // 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. @@ -180,12 +181,16 @@ struct WasmModule { std::vector<WasmGlobal> globals; // globals in this module. uint32_t globals_size; // size of globals table. + uint32_t num_imported_functions; // number of imported functions. + uint32_t num_declared_functions; // number of declared functions. + uint32_t num_exported_functions; // number of exported functions. std::vector<FunctionSig*> signatures; // signatures in this module. std::vector<WasmFunction> functions; // functions in this module. std::vector<WasmDataSegment> data_segments; // data segments in this module. std::vector<WasmIndirectFunctionTable> function_tables; // function tables. std::vector<WasmImport> import_table; // import table. std::vector<WasmExport> export_table; // export table. + std::vector<WasmTableInit> table_inits; // initializations of tables // We store the semaphore here to extend its lifetime. In <libc-2.21, which we // use on the try bots, semaphore::Wait() can return while some compilation // tasks are still executing semaphore::Signal(). If the semaphore is cleaned @@ -233,13 +238,12 @@ struct WasmModule { } // Creates a new instantiation of the module in the given isolate. - static MaybeHandle<JSObject> Instantiate(Isolate* isolate, - Handle<FixedArray> compiled_module, - Handle<JSReceiver> ffi, - Handle<JSArrayBuffer> memory); + V8_EXPORT_PRIVATE static MaybeHandle<JSObject> Instantiate( + Isolate* isolate, ErrorThrower* thrower, Handle<JSObject> module_object, + Handle<JSReceiver> ffi, Handle<JSArrayBuffer> memory); - MaybeHandle<FixedArray> CompileFunctions(Isolate* isolate, - ErrorThrower* thrower) const; + MaybeHandle<WasmCompiledModule> CompileFunctions(Isolate* isolate, + ErrorThrower* thrower) const; private: DISALLOW_COPY_AND_ASSIGN(WasmModule); @@ -255,7 +259,6 @@ struct WasmModuleInstance { Handle<JSArrayBuffer> globals_buffer; // Handle to array buffer of globals. std::vector<Handle<FixedArray>> function_tables; // indirect function tables. std::vector<Handle<Code>> function_code; // code objects for each function. - std::vector<Handle<Code>> import_code; // code objects for each import. // -- raw memory ------------------------------------------------------------ byte* mem_start; // start of linear memory. uint32_t mem_size; // size of the linear memory. @@ -266,7 +269,6 @@ struct WasmModuleInstance { : module(m), function_tables(m->function_tables.size()), function_code(m->functions.size()), - import_code(m->import_table.size()), mem_start(nullptr), mem_size(0), globals_start(nullptr) {} @@ -278,9 +280,6 @@ struct ModuleEnv { const WasmModule* module; WasmModuleInstance* instance; ModuleOrigin origin; - // TODO(mtrofin): remove this once we introduce WASM_DIRECT_CALL - // reloc infos. - std::vector<Handle<Code>> placeholders; bool IsValidGlobal(uint32_t index) const { return module && index < module->globals.size(); @@ -291,9 +290,6 @@ struct ModuleEnv { bool IsValidSignature(uint32_t index) const { return module && index < module->signatures.size(); } - bool IsValidImport(uint32_t index) const { - return module && index < module->import_table.size(); - } bool IsValidTable(uint32_t index) const { return module && index < module->function_tables.size(); } @@ -305,10 +301,6 @@ struct ModuleEnv { DCHECK(IsValidFunction(index)); return module->functions[index].sig; } - FunctionSig* GetImportSignature(uint32_t index) { - DCHECK(IsValidImport(index)); - return module->import_table[index].sig; - } FunctionSig* GetSignature(uint32_t index) { DCHECK(IsValidSignature(index)); return module->signatures[index]; @@ -320,14 +312,15 @@ struct ModuleEnv { bool asm_js() { return origin == kAsmJsOrigin; } - Handle<Code> GetCodeOrPlaceholder(uint32_t index) const; - Handle<Code> GetImportCode(uint32_t index); + Handle<Code> GetFunctionCode(uint32_t index) { + DCHECK_NOT_NULL(instance); + return instance->function_code[index]; + } static compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone, FunctionSig* sig); static compiler::CallDescriptor* GetI32WasmCallDescriptor( Zone* zone, compiler::CallDescriptor* descriptor); - compiler::CallDescriptor* GetCallDescriptor(Zone* zone, uint32_t index); }; // A helper for printing out the names of functions. @@ -347,6 +340,128 @@ typedef Result<WasmFunction*> FunctionResult; typedef std::vector<std::pair<int, int>> FunctionOffsets; typedef Result<FunctionOffsets> FunctionOffsetsResult; +class WasmCompiledModule : public FixedArray { + public: + static WasmCompiledModule* cast(Object* fixed_array) { + return reinterpret_cast<WasmCompiledModule*>(fixed_array); + } + +#define WCM_OBJECT_OR_WEAK(TYPE, NAME, ID) \ + Handle<TYPE> NAME() const { return handle(ptr_to_##NAME()); } \ + \ + MaybeHandle<TYPE> maybe_##NAME() const { \ + if (has_##NAME()) return NAME(); \ + return MaybeHandle<TYPE>(); \ + } \ + \ + TYPE* ptr_to_##NAME() const { \ + Object* obj = get(ID); \ + if (!obj->Is##TYPE()) return nullptr; \ + return TYPE::cast(obj); \ + } \ + \ + void set_##NAME(Handle<TYPE> value) { set_ptr_to_##NAME(*value); } \ + \ + void set_ptr_to_##NAME(TYPE* value) { set(ID, value); } \ + \ + bool has_##NAME() const { return get(ID)->Is##TYPE(); } \ + \ + void reset_##NAME() { set_undefined(ID); } + +#define WCM_OBJECT(TYPE, NAME) WCM_OBJECT_OR_WEAK(TYPE, NAME, kID_##NAME) + +#define WCM_SMALL_NUMBER(TYPE, NAME) \ + TYPE NAME() const { \ + return static_cast<TYPE>(Smi::cast(get(kID_##NAME))->value()); \ + } + +#define WCM_WEAK_LINK(TYPE, NAME) \ + WCM_OBJECT_OR_WEAK(WeakCell, weak_##NAME, kID_##NAME); \ + \ + Handle<TYPE> NAME() const { \ + return handle(TYPE::cast(weak_##NAME()->value())); \ + } + +#define CORE_WCM_PROPERTY_TABLE(MACRO) \ + MACRO(OBJECT, FixedArray, code_table) \ + MACRO(OBJECT, FixedArray, import_data) \ + MACRO(OBJECT, FixedArray, exports) \ + MACRO(OBJECT, FixedArray, startup_function) \ + MACRO(OBJECT, FixedArray, indirect_function_tables) \ + MACRO(OBJECT, String, module_bytes) \ + MACRO(OBJECT, ByteArray, function_names) \ + MACRO(SMALL_NUMBER, uint32_t, min_memory_pages) \ + MACRO(OBJECT, FixedArray, data_segments_info) \ + MACRO(OBJECT, ByteArray, data_segments) \ + MACRO(SMALL_NUMBER, uint32_t, globals_size) \ + MACRO(OBJECT, JSArrayBuffer, heap) \ + MACRO(SMALL_NUMBER, bool, export_memory) \ + MACRO(SMALL_NUMBER, ModuleOrigin, origin) \ + MACRO(WEAK_LINK, WasmCompiledModule, next_instance) \ + MACRO(WEAK_LINK, WasmCompiledModule, prev_instance) \ + MACRO(WEAK_LINK, JSObject, owning_instance) \ + MACRO(WEAK_LINK, JSObject, module_object) + +#if DEBUG +#define DEBUG_ONLY_TABLE(MACRO) MACRO(SMALL_NUMBER, uint32_t, instance_id) +#else +#define DEBUG_ONLY_TABLE(IGNORE) + uint32_t instance_id() const { return -1; } +#endif + +#define WCM_PROPERTY_TABLE(MACRO) \ + CORE_WCM_PROPERTY_TABLE(MACRO) \ + DEBUG_ONLY_TABLE(MACRO) + + private: + enum PropertyIndices { +#define INDICES(IGNORE1, IGNORE2, NAME) kID_##NAME, + WCM_PROPERTY_TABLE(INDICES) Count +#undef INDICES + }; + + public: + static Handle<WasmCompiledModule> New(Isolate* isolate, + uint32_t min_memory_pages, + uint32_t globals_size, + bool export_memory, + ModuleOrigin origin); + + static Handle<WasmCompiledModule> Clone(Isolate* isolate, + Handle<WasmCompiledModule> module) { + Handle<WasmCompiledModule> ret = Handle<WasmCompiledModule>::cast( + isolate->factory()->CopyFixedArray(module)); + ret->Init(); + ret->reset_weak_owning_instance(); + ret->reset_weak_next_instance(); + ret->reset_weak_prev_instance(); + return ret; + } + + uint32_t mem_size() const { + DCHECK(has_heap()); + return heap()->byte_length()->Number(); + } + + uint32_t default_mem_size() const { + return min_memory_pages() * WasmModule::kPageSize; + } + +#define DECLARATION(KIND, TYPE, NAME) WCM_##KIND(TYPE, NAME) + WCM_PROPERTY_TABLE(DECLARATION) +#undef DECLARATION + + void PrintInstancesChain(); + + private: +#if DEBUG + static uint32_t instance_id_counter_; +#endif + void Init(); + + DISALLOW_IMPLICIT_CONSTRUCTORS(WasmCompiledModule); +}; + // Extract a function name from the given wasm object. // Returns "<WASM UNNAMED>" if the function is unnamed or the name is not a // valid UTF-8 string. @@ -399,18 +514,38 @@ void PopulateFunctionTable(Handle<FixedArray> table, uint32_t table_size, const std::vector<Handle<Code>>* code_table); Handle<JSObject> CreateCompiledModuleObject(Isolate* isolate, - Handle<FixedArray> compiled_module); + Handle<FixedArray> compiled_module, + ModuleOrigin origin); + +V8_EXPORT_PRIVATE MaybeHandle<JSObject> CreateModuleObjectFromBytes( + Isolate* isolate, const byte* start, const byte* end, ErrorThrower* thrower, + ModuleOrigin origin); + +V8_EXPORT_PRIVATE bool ValidateModuleBytes(Isolate* isolate, const byte* start, + const byte* end, + ErrorThrower* thrower, + ModuleOrigin origin); + +// Get the number of imported functions for a WASM instance. +uint32_t GetNumImportedFunctions(Handle<JSObject> wasm_object); + +// Assumed to be called with a code object associated to a wasm module instance. +// Intended to be called from runtime functions. +// Returns nullptr on failing to get owning instance. +Object* GetOwningWasmInstance(Code* code); + +int32_t GetInstanceMemorySize(Isolate* isolate, Handle<JSObject> instance); + +int32_t GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance, + uint32_t pages); namespace testing { -// Decode, verify, and run the function labeled "main" in the -// given encoded module. The module should have no imports. -int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start, - const byte* module_end, bool asm_js = false); +void ValidateInstancesChain(Isolate* isolate, Handle<JSObject> module_obj, + int instance_count); +void ValidateModuleState(Isolate* isolate, Handle<JSObject> module_obj); +void ValidateOrphanedInstance(Isolate* isolate, Handle<JSObject> instance); -int32_t CallFunction(Isolate* isolate, Handle<JSObject> instance, - ErrorThrower* thrower, const char* name, int argc, - Handle<Object> argv[]); } // namespace testing } // namespace wasm } // namespace internal |