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, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 33 | kDeclEnd = 0x06, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 34 | kDeclStartFunction = 0x07, |
| 35 | kDeclImportTable = 0x08, |
| 36 | kDeclWLL = 0x11, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 39 | static const int kMaxModuleSectionCode = 0x11; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 40 | |
| 41 | enum WasmFunctionDeclBit { |
| 42 | kDeclFunctionName = 0x01, |
| 43 | kDeclFunctionImport = 0x02, |
| 44 | kDeclFunctionLocals = 0x04, |
| 45 | kDeclFunctionExport = 0x08 |
| 46 | }; |
| 47 | |
| 48 | // Constants for fixed-size elements within a module. |
| 49 | static const size_t kDeclMemorySize = 3; |
| 50 | static const size_t kDeclGlobalSize = 6; |
| 51 | static const size_t kDeclDataSegmentSize = 13; |
| 52 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 53 | // Static representation of a WASM function. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 54 | struct WasmFunction { |
| 55 | FunctionSig* sig; // signature of the function. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 56 | uint32_t func_index; // index into the function table. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 57 | uint16_t sig_index; // index into the signature table. |
| 58 | uint32_t name_offset; // offset in the module bytes of the name, if any. |
| 59 | uint32_t code_start_offset; // offset in the module bytes of code start. |
| 60 | uint32_t code_end_offset; // offset in the module bytes of code end. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 61 | uint16_t local_i32_count; // number of i32 local variables. |
| 62 | uint16_t local_i64_count; // number of i64 local variables. |
| 63 | uint16_t local_f32_count; // number of f32 local variables. |
| 64 | uint16_t local_f64_count; // number of f64 local variables. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 65 | bool exported; // true if this function is exported. |
| 66 | bool external; // true if this function is externally supplied. |
| 67 | }; |
| 68 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 69 | // Static representation of an imported WASM function. |
| 70 | struct WasmImport { |
| 71 | FunctionSig* sig; // signature of the function. |
| 72 | uint16_t sig_index; // index into the signature table. |
| 73 | uint32_t module_name_offset; // offset in module bytes of the module name. |
| 74 | uint32_t function_name_offset; // offset in module bytes of the import name. |
| 75 | }; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 76 | |
| 77 | // Static representation of a wasm global variable. |
| 78 | struct WasmGlobal { |
| 79 | uint32_t name_offset; // offset in the module bytes of the name, if any. |
| 80 | MachineType type; // type of the global. |
| 81 | uint32_t offset; // offset from beginning of globals area. |
| 82 | bool exported; // true if this global is exported. |
| 83 | }; |
| 84 | |
| 85 | // Static representation of a wasm data segment. |
| 86 | struct WasmDataSegment { |
| 87 | uint32_t dest_addr; // destination memory address of the data. |
| 88 | uint32_t source_offset; // start offset in the module bytes. |
| 89 | uint32_t source_size; // end offset in the module bytes. |
| 90 | bool init; // true if loaded upon instantiation. |
| 91 | }; |
| 92 | |
| 93 | // Static representation of a module. |
| 94 | struct WasmModule { |
| 95 | static const uint8_t kMinMemSize = 12; // Minimum memory size = 4kb |
| 96 | static const uint8_t kMaxMemSize = 30; // Maximum memory size = 1gb |
| 97 | |
| 98 | Isolate* shared_isolate; // isolate for storing shared code. |
| 99 | const byte* module_start; // starting address for the module bytes. |
| 100 | const byte* module_end; // end address for the module bytes. |
| 101 | uint8_t min_mem_size_log2; // minimum size of the memory (log base 2). |
| 102 | uint8_t max_mem_size_log2; // maximum size of the memory (log base 2). |
| 103 | bool mem_export; // true if the memory is exported. |
| 104 | bool mem_external; // true if the memory is external. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 105 | int start_function_index; // start function, if any. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 106 | |
| 107 | std::vector<WasmGlobal>* globals; // globals in this module. |
| 108 | std::vector<FunctionSig*>* signatures; // signatures in this module. |
| 109 | std::vector<WasmFunction>* functions; // functions in this module. |
| 110 | std::vector<WasmDataSegment>* data_segments; // data segments in this module. |
| 111 | std::vector<uint16_t>* function_table; // function table. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 112 | std::vector<WasmImport>* import_table; // import table. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 113 | |
| 114 | WasmModule(); |
| 115 | ~WasmModule(); |
| 116 | |
| 117 | // Get a pointer to a string stored in the module bytes representing a name. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 118 | const char* GetName(uint32_t offset) const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 119 | if (offset == 0) return "<?>"; // no name. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 120 | CHECK(BoundsCheck(offset, offset + 1)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 121 | return reinterpret_cast<const char*>(module_start + offset); |
| 122 | } |
| 123 | |
| 124 | // Checks the given offset range is contained within the module bytes. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 125 | bool BoundsCheck(uint32_t start, uint32_t end) const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 126 | size_t size = module_end - module_start; |
| 127 | return start < size && end < size; |
| 128 | } |
| 129 | |
| 130 | // Creates a new instantiation of the module in the given isolate. |
| 131 | MaybeHandle<JSObject> Instantiate(Isolate* isolate, Handle<JSObject> ffi, |
| 132 | Handle<JSArrayBuffer> memory); |
| 133 | }; |
| 134 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 135 | // An instantiated WASM module, including memory, function table, etc. |
| 136 | struct WasmModuleInstance { |
| 137 | WasmModule* module; // static representation of the module. |
| 138 | // -- Heap allocated -------------------------------------------------------- |
| 139 | Handle<JSObject> js_object; // JavaScript module object. |
| 140 | Handle<Context> context; // JavaScript native context. |
| 141 | Handle<JSArrayBuffer> mem_buffer; // Handle to array buffer of memory. |
| 142 | Handle<JSArrayBuffer> globals_buffer; // Handle to array buffer of globals. |
| 143 | Handle<FixedArray> function_table; // indirect function table. |
| 144 | std::vector<Handle<Code>>* function_code; // code objects for each function. |
| 145 | std::vector<Handle<Code>>* import_code; // code objects for each import. |
| 146 | // -- raw memory ------------------------------------------------------------ |
| 147 | byte* mem_start; // start of linear memory. |
| 148 | size_t mem_size; // size of the linear memory. |
| 149 | // -- raw globals ----------------------------------------------------------- |
| 150 | byte* globals_start; // start of the globals area. |
| 151 | size_t globals_size; // size of the globals area. |
| 152 | |
| 153 | explicit WasmModuleInstance(WasmModule* m) |
| 154 | : module(m), |
| 155 | function_code(nullptr), |
| 156 | mem_start(nullptr), |
| 157 | mem_size(0), |
| 158 | globals_start(nullptr), |
| 159 | globals_size(0) {} |
| 160 | }; |
| 161 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 162 | // forward declaration. |
| 163 | class WasmLinker; |
| 164 | |
| 165 | // Interface provided to the decoder/graph builder which contains only |
| 166 | // minimal information about the globals, functions, and function tables. |
| 167 | struct ModuleEnv { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 168 | WasmModule* module; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 169 | WasmModuleInstance* instance; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 170 | WasmLinker* linker; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 171 | bool asm_js; // true if the module originated from asm.js. |
| 172 | |
| 173 | bool IsValidGlobal(uint32_t index) { |
| 174 | return module && index < module->globals->size(); |
| 175 | } |
| 176 | bool IsValidFunction(uint32_t index) { |
| 177 | return module && index < module->functions->size(); |
| 178 | } |
| 179 | bool IsValidSignature(uint32_t index) { |
| 180 | return module && index < module->signatures->size(); |
| 181 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 182 | bool IsValidImport(uint32_t index) { |
| 183 | return module && index < module->import_table->size(); |
| 184 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 185 | MachineType GetGlobalType(uint32_t index) { |
| 186 | DCHECK(IsValidGlobal(index)); |
| 187 | return module->globals->at(index).type; |
| 188 | } |
| 189 | FunctionSig* GetFunctionSignature(uint32_t index) { |
| 190 | DCHECK(IsValidFunction(index)); |
| 191 | return module->functions->at(index).sig; |
| 192 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 193 | FunctionSig* GetImportSignature(uint32_t index) { |
| 194 | DCHECK(IsValidImport(index)); |
| 195 | return module->import_table->at(index).sig; |
| 196 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 197 | FunctionSig* GetSignature(uint32_t index) { |
| 198 | DCHECK(IsValidSignature(index)); |
| 199 | return module->signatures->at(index); |
| 200 | } |
| 201 | size_t FunctionTableSize() { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 202 | return module && module->function_table ? module->function_table->size() |
| 203 | : 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | Handle<Code> GetFunctionCode(uint32_t index); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 207 | Handle<Code> GetImportCode(uint32_t index); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 208 | Handle<FixedArray> GetFunctionTable(); |
| 209 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 210 | static compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone, |
| 211 | FunctionSig* sig); |
| 212 | static compiler::CallDescriptor* GetI32WasmCallDescriptor( |
| 213 | Zone* zone, compiler::CallDescriptor* descriptor); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 214 | compiler::CallDescriptor* GetCallDescriptor(Zone* zone, uint32_t index); |
| 215 | }; |
| 216 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 217 | // A helper for printing out the names of functions. |
| 218 | struct WasmFunctionName { |
| 219 | const WasmFunction* function_; |
| 220 | const WasmModule* module_; |
| 221 | WasmFunctionName(const WasmFunction* function, const ModuleEnv* menv) |
| 222 | : function_(function), module_(menv ? menv->module : nullptr) {} |
| 223 | }; |
| 224 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 225 | std::ostream& operator<<(std::ostream& os, const WasmModule& module); |
| 226 | std::ostream& operator<<(std::ostream& os, const WasmFunction& function); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 227 | std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 228 | |
| 229 | typedef Result<WasmModule*> ModuleResult; |
| 230 | typedef Result<WasmFunction*> FunctionResult; |
| 231 | |
| 232 | // For testing. Decode, verify, and run the last exported function in the |
| 233 | // given encoded module. |
| 234 | int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start, |
| 235 | const byte* module_end, bool asm_js = false); |
| 236 | |
| 237 | // For testing. Decode, verify, and run the last exported function in the |
| 238 | // given decoded module. |
| 239 | int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 240 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 241 | } // namespace wasm |
| 242 | } // namespace internal |
| 243 | } // namespace v8 |
| 244 | |
| 245 | #endif // V8_WASM_MODULE_H_ |