blob: 5f5777cebebe4c5a865aaa11dd37472eb61ea3f5 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
14namespace v8 {
15namespace internal {
16
17namespace compiler {
18class CallDescriptor;
19}
20
21namespace wasm {
22const size_t kMaxModuleSize = 1024 * 1024 * 1024;
23const size_t kMaxFunctionSize = 128 * 1024;
24const size_t kMaxStringSize = 256;
25
26enum WasmSectionDeclCode {
27 kDeclMemory = 0x00,
28 kDeclSignatures = 0x01,
29 kDeclFunctions = 0x02,
30 kDeclGlobals = 0x03,
31 kDeclDataSegments = 0x04,
32 kDeclFunctionTable = 0x05,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000033 kDeclEnd = 0x06,
Ben Murdoch097c5b22016-05-18 11:27:45 +010034 kDeclStartFunction = 0x07,
35 kDeclImportTable = 0x08,
36 kDeclWLL = 0x11,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037};
38
Ben Murdoch097c5b22016-05-18 11:27:45 +010039static const int kMaxModuleSectionCode = 0x11;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000040
41enum WasmFunctionDeclBit {
42 kDeclFunctionName = 0x01,
43 kDeclFunctionImport = 0x02,
44 kDeclFunctionLocals = 0x04,
45 kDeclFunctionExport = 0x08
46};
47
48// Constants for fixed-size elements within a module.
49static const size_t kDeclMemorySize = 3;
50static const size_t kDeclGlobalSize = 6;
51static const size_t kDeclDataSegmentSize = 13;
52
Ben Murdoch097c5b22016-05-18 11:27:45 +010053// Static representation of a WASM function.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000054struct WasmFunction {
55 FunctionSig* sig; // signature of the function.
Ben Murdoch097c5b22016-05-18 11:27:45 +010056 uint32_t func_index; // index into the function table.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000057 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 Murdoch097c5b22016-05-18 11:27:45 +010061 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 Murdoch4a90d5f2016-03-22 12:00:34 +000065 bool exported; // true if this function is exported.
66 bool external; // true if this function is externally supplied.
67};
68
Ben Murdoch097c5b22016-05-18 11:27:45 +010069// Static representation of an imported WASM function.
70struct 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 Murdoch4a90d5f2016-03-22 12:00:34 +000076
77// Static representation of a wasm global variable.
78struct 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.
86struct 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.
94struct 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 Murdoch097c5b22016-05-18 11:27:45 +0100105 int start_function_index; // start function, if any.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000106
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 Murdoch097c5b22016-05-18 11:27:45 +0100112 std::vector<WasmImport>* import_table; // import table.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000113
114 WasmModule();
115 ~WasmModule();
116
117 // Get a pointer to a string stored in the module bytes representing a name.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100118 const char* GetName(uint32_t offset) const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119 if (offset == 0) return "<?>"; // no name.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100120 CHECK(BoundsCheck(offset, offset + 1));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000121 return reinterpret_cast<const char*>(module_start + offset);
122 }
123
124 // Checks the given offset range is contained within the module bytes.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100125 bool BoundsCheck(uint32_t start, uint32_t end) const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000126 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 Murdoch097c5b22016-05-18 11:27:45 +0100135// An instantiated WASM module, including memory, function table, etc.
136struct 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 Murdoch4a90d5f2016-03-22 12:00:34 +0000162// forward declaration.
163class WasmLinker;
164
165// Interface provided to the decoder/graph builder which contains only
166// minimal information about the globals, functions, and function tables.
167struct ModuleEnv {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000168 WasmModule* module;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100169 WasmModuleInstance* instance;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000170 WasmLinker* linker;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000171 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 Murdoch097c5b22016-05-18 11:27:45 +0100182 bool IsValidImport(uint32_t index) {
183 return module && index < module->import_table->size();
184 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000185 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 Murdoch097c5b22016-05-18 11:27:45 +0100193 FunctionSig* GetImportSignature(uint32_t index) {
194 DCHECK(IsValidImport(index));
195 return module->import_table->at(index).sig;
196 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000197 FunctionSig* GetSignature(uint32_t index) {
198 DCHECK(IsValidSignature(index));
199 return module->signatures->at(index);
200 }
201 size_t FunctionTableSize() {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100202 return module && module->function_table ? module->function_table->size()
203 : 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000204 }
205
206 Handle<Code> GetFunctionCode(uint32_t index);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100207 Handle<Code> GetImportCode(uint32_t index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000208 Handle<FixedArray> GetFunctionTable();
209
Ben Murdoch097c5b22016-05-18 11:27:45 +0100210 static compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone,
211 FunctionSig* sig);
212 static compiler::CallDescriptor* GetI32WasmCallDescriptor(
213 Zone* zone, compiler::CallDescriptor* descriptor);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000214 compiler::CallDescriptor* GetCallDescriptor(Zone* zone, uint32_t index);
215};
216
Ben Murdoch097c5b22016-05-18 11:27:45 +0100217// A helper for printing out the names of functions.
218struct 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 Murdoch4a90d5f2016-03-22 12:00:34 +0000225std::ostream& operator<<(std::ostream& os, const WasmModule& module);
226std::ostream& operator<<(std::ostream& os, const WasmFunction& function);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100227std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000228
229typedef Result<WasmModule*> ModuleResult;
230typedef Result<WasmFunction*> FunctionResult;
231
232// For testing. Decode, verify, and run the last exported function in the
233// given encoded module.
234int32_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.
239int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100240
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000241} // namespace wasm
242} // namespace internal
243} // namespace v8
244
245#endif // V8_WASM_MODULE_H_