Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_WASM_MODULE_H_ |
| 6 | #define V8_WASM_MODULE_H_ |
| 7 | |
| 8 | #include "src/wasm/wasm-opcodes.h" |
| 9 | #include "src/wasm/wasm-result.h" |
| 10 | |
| 11 | #include "src/api.h" |
| 12 | #include "src/handles.h" |
| 13 | |
| 14 | namespace v8 { |
| 15 | namespace internal { |
| 16 | |
| 17 | namespace compiler { |
| 18 | class CallDescriptor; |
| 19 | } |
| 20 | |
| 21 | namespace wasm { |
| 22 | const size_t kMaxModuleSize = 1024 * 1024 * 1024; |
| 23 | const size_t kMaxFunctionSize = 128 * 1024; |
| 24 | const size_t kMaxStringSize = 256; |
| 25 | |
| 26 | enum WasmSectionDeclCode { |
| 27 | kDeclMemory = 0x00, |
| 28 | kDeclSignatures = 0x01, |
| 29 | kDeclFunctions = 0x02, |
| 30 | kDeclGlobals = 0x03, |
| 31 | kDeclDataSegments = 0x04, |
| 32 | kDeclFunctionTable = 0x05, |
| 33 | kDeclWLL = 0x11, |
| 34 | kDeclEnd = 0x06, |
| 35 | }; |
| 36 | |
| 37 | static const int kMaxModuleSectionCode = 6; |
| 38 | |
| 39 | enum WasmFunctionDeclBit { |
| 40 | kDeclFunctionName = 0x01, |
| 41 | kDeclFunctionImport = 0x02, |
| 42 | kDeclFunctionLocals = 0x04, |
| 43 | kDeclFunctionExport = 0x08 |
| 44 | }; |
| 45 | |
| 46 | // Constants for fixed-size elements within a module. |
| 47 | static const size_t kDeclMemorySize = 3; |
| 48 | static const size_t kDeclGlobalSize = 6; |
| 49 | static const size_t kDeclDataSegmentSize = 13; |
| 50 | |
| 51 | // Static representation of a wasm function. |
| 52 | struct WasmFunction { |
| 53 | FunctionSig* sig; // signature of the function. |
| 54 | uint16_t sig_index; // index into the signature table. |
| 55 | uint32_t name_offset; // offset in the module bytes of the name, if any. |
| 56 | uint32_t code_start_offset; // offset in the module bytes of code start. |
| 57 | uint32_t code_end_offset; // offset in the module bytes of code end. |
| 58 | uint16_t local_int32_count; // number of int32 local variables. |
| 59 | uint16_t local_int64_count; // number of int64 local variables. |
| 60 | uint16_t local_float32_count; // number of float32 local variables. |
| 61 | uint16_t local_float64_count; // number of float64 local variables. |
| 62 | bool exported; // true if this function is exported. |
| 63 | bool external; // true if this function is externally supplied. |
| 64 | }; |
| 65 | |
| 66 | struct ModuleEnv; // forward declaration of decoder interface. |
| 67 | |
| 68 | // Static representation of a wasm global variable. |
| 69 | struct WasmGlobal { |
| 70 | uint32_t name_offset; // offset in the module bytes of the name, if any. |
| 71 | MachineType type; // type of the global. |
| 72 | uint32_t offset; // offset from beginning of globals area. |
| 73 | bool exported; // true if this global is exported. |
| 74 | }; |
| 75 | |
| 76 | // Static representation of a wasm data segment. |
| 77 | struct WasmDataSegment { |
| 78 | uint32_t dest_addr; // destination memory address of the data. |
| 79 | uint32_t source_offset; // start offset in the module bytes. |
| 80 | uint32_t source_size; // end offset in the module bytes. |
| 81 | bool init; // true if loaded upon instantiation. |
| 82 | }; |
| 83 | |
| 84 | // Static representation of a module. |
| 85 | struct WasmModule { |
| 86 | static const uint8_t kMinMemSize = 12; // Minimum memory size = 4kb |
| 87 | static const uint8_t kMaxMemSize = 30; // Maximum memory size = 1gb |
| 88 | |
| 89 | Isolate* shared_isolate; // isolate for storing shared code. |
| 90 | const byte* module_start; // starting address for the module bytes. |
| 91 | const byte* module_end; // end address for the module bytes. |
| 92 | uint8_t min_mem_size_log2; // minimum size of the memory (log base 2). |
| 93 | uint8_t max_mem_size_log2; // maximum size of the memory (log base 2). |
| 94 | bool mem_export; // true if the memory is exported. |
| 95 | bool mem_external; // true if the memory is external. |
| 96 | |
| 97 | std::vector<WasmGlobal>* globals; // globals in this module. |
| 98 | std::vector<FunctionSig*>* signatures; // signatures in this module. |
| 99 | std::vector<WasmFunction>* functions; // functions in this module. |
| 100 | std::vector<WasmDataSegment>* data_segments; // data segments in this module. |
| 101 | std::vector<uint16_t>* function_table; // function table. |
| 102 | |
| 103 | WasmModule(); |
| 104 | ~WasmModule(); |
| 105 | |
| 106 | // Get a pointer to a string stored in the module bytes representing a name. |
| 107 | const char* GetName(uint32_t offset) { |
| 108 | CHECK(BoundsCheck(offset, offset + 1)); |
| 109 | if (offset == 0) return "<?>"; // no name. |
| 110 | return reinterpret_cast<const char*>(module_start + offset); |
| 111 | } |
| 112 | |
| 113 | // Checks the given offset range is contained within the module bytes. |
| 114 | bool BoundsCheck(uint32_t start, uint32_t end) { |
| 115 | size_t size = module_end - module_start; |
| 116 | return start < size && end < size; |
| 117 | } |
| 118 | |
| 119 | // Creates a new instantiation of the module in the given isolate. |
| 120 | MaybeHandle<JSObject> Instantiate(Isolate* isolate, Handle<JSObject> ffi, |
| 121 | Handle<JSArrayBuffer> memory); |
| 122 | }; |
| 123 | |
| 124 | // forward declaration. |
| 125 | class WasmLinker; |
| 126 | |
| 127 | // Interface provided to the decoder/graph builder which contains only |
| 128 | // minimal information about the globals, functions, and function tables. |
| 129 | struct ModuleEnv { |
| 130 | uintptr_t globals_area; // address of the globals area. |
| 131 | uintptr_t mem_start; // address of the start of linear memory. |
| 132 | uintptr_t mem_end; // address of the end of linear memory. |
| 133 | |
| 134 | WasmModule* module; |
| 135 | WasmLinker* linker; |
| 136 | std::vector<Handle<Code>>* function_code; |
| 137 | Handle<FixedArray> function_table; |
| 138 | Handle<JSArrayBuffer> memory; |
| 139 | Handle<Context> context; |
| 140 | bool asm_js; // true if the module originated from asm.js. |
| 141 | |
| 142 | bool IsValidGlobal(uint32_t index) { |
| 143 | return module && index < module->globals->size(); |
| 144 | } |
| 145 | bool IsValidFunction(uint32_t index) { |
| 146 | return module && index < module->functions->size(); |
| 147 | } |
| 148 | bool IsValidSignature(uint32_t index) { |
| 149 | return module && index < module->signatures->size(); |
| 150 | } |
| 151 | MachineType GetGlobalType(uint32_t index) { |
| 152 | DCHECK(IsValidGlobal(index)); |
| 153 | return module->globals->at(index).type; |
| 154 | } |
| 155 | FunctionSig* GetFunctionSignature(uint32_t index) { |
| 156 | DCHECK(IsValidFunction(index)); |
| 157 | return module->functions->at(index).sig; |
| 158 | } |
| 159 | FunctionSig* GetSignature(uint32_t index) { |
| 160 | DCHECK(IsValidSignature(index)); |
| 161 | return module->signatures->at(index); |
| 162 | } |
| 163 | size_t FunctionTableSize() { |
| 164 | return module ? module->function_table->size() : 0; |
| 165 | } |
| 166 | |
| 167 | Handle<Code> GetFunctionCode(uint32_t index); |
| 168 | Handle<FixedArray> GetFunctionTable(); |
| 169 | |
| 170 | compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone, FunctionSig* sig); |
| 171 | compiler::CallDescriptor* GetCallDescriptor(Zone* zone, uint32_t index); |
| 172 | }; |
| 173 | |
| 174 | std::ostream& operator<<(std::ostream& os, const WasmModule& module); |
| 175 | std::ostream& operator<<(std::ostream& os, const WasmFunction& function); |
| 176 | |
| 177 | typedef Result<WasmModule*> ModuleResult; |
| 178 | typedef Result<WasmFunction*> FunctionResult; |
| 179 | |
| 180 | // For testing. Decode, verify, and run the last exported function in the |
| 181 | // given encoded module. |
| 182 | int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start, |
| 183 | const byte* module_end, bool asm_js = false); |
| 184 | |
| 185 | // For testing. Decode, verify, and run the last exported function in the |
| 186 | // given decoded module. |
| 187 | int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module); |
| 188 | } // namespace wasm |
| 189 | } // namespace internal |
| 190 | } // namespace v8 |
| 191 | |
| 192 | #endif // V8_WASM_MODULE_H_ |