blob: 2ac04253d72b27b09897a5620f6a569b7bdf064f [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;
Ben Murdochc5610432016-08-08 18:44:38 +010019class WasmCompilationUnit;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000020}
21
22namespace wasm {
23const size_t kMaxModuleSize = 1024 * 1024 * 1024;
24const size_t kMaxFunctionSize = 128 * 1024;
25const size_t kMaxStringSize = 256;
Ben Murdochda12d292016-06-02 14:46:10 +010026const uint32_t kWasmMagic = 0x6d736100;
Ben Murdochc5610432016-08-08 18:44:38 +010027const uint32_t kWasmVersion = 0x0b;
28const uint8_t kWasmFunctionTypeForm = 0x40;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000029
Ben Murdochda12d292016-06-02 14:46:10 +010030// WebAssembly sections are named as strings in the binary format, but
31// internally V8 uses an enum to handle them.
32//
33// Entries have the form F(enumerator, string).
Ben Murdochc5610432016-08-08 18:44:38 +010034#define FOR_EACH_WASM_SECTION_TYPE(F) \
35 F(Signatures, 1, "type") \
36 F(ImportTable, 2, "import") \
37 F(FunctionSignatures, 3, "function") \
38 F(FunctionTable, 4, "table") \
39 F(Memory, 5, "memory") \
40 F(ExportTable, 6, "export") \
41 F(StartFunction, 7, "start") \
42 F(FunctionBodies, 8, "code") \
43 F(DataSegments, 9, "data") \
44 F(Names, 10, "name") \
45 F(OldFunctions, 0, "old_function") \
46 F(Globals, 0, "global") \
47 F(End, 0, "end")
Ben Murdochda12d292016-06-02 14:46:10 +010048
49// Contants for the above section types: {LEB128 length, characters...}.
50#define WASM_SECTION_MEMORY 6, 'm', 'e', 'm', 'o', 'r', 'y'
Ben Murdochc5610432016-08-08 18:44:38 +010051#define WASM_SECTION_SIGNATURES 4, 't', 'y', 'p', 'e'
52#define WASM_SECTION_OLD_FUNCTIONS \
53 12, 'o', 'l', 'd', '_', 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n'
54#define WASM_SECTION_GLOBALS 6, 'g', 'l', 'o', 'b', 'a', 'l'
55#define WASM_SECTION_DATA_SEGMENTS 4, 'd', 'a', 't', 'a'
56#define WASM_SECTION_FUNCTION_TABLE 5, 't', 'a', 'b', 'l', 'e'
Ben Murdochda12d292016-06-02 14:46:10 +010057#define WASM_SECTION_END 3, 'e', 'n', 'd'
Ben Murdochc5610432016-08-08 18:44:38 +010058#define WASM_SECTION_START_FUNCTION 5, 's', 't', 'a', 'r', 't'
59#define WASM_SECTION_IMPORT_TABLE 6, 'i', 'm', 'p', 'o', 'r', 't'
60#define WASM_SECTION_EXPORT_TABLE 6, 'e', 'x', 'p', 'o', 'r', 't'
61#define WASM_SECTION_FUNCTION_SIGNATURES \
62 8, 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n'
63#define WASM_SECTION_FUNCTION_BODIES 4, 'c', 'o', 'd', 'e'
64#define WASM_SECTION_NAMES 4, 'n', 'a', 'm', 'e'
Ben Murdochda12d292016-06-02 14:46:10 +010065
66// Constants for the above section headers' size (LEB128 + characters).
67#define WASM_SECTION_MEMORY_SIZE ((size_t)7)
Ben Murdochc5610432016-08-08 18:44:38 +010068#define WASM_SECTION_SIGNATURES_SIZE ((size_t)5)
69#define WASM_SECTION_OLD_FUNCTIONS_SIZE ((size_t)13)
70#define WASM_SECTION_GLOBALS_SIZE ((size_t)7)
71#define WASM_SECTION_DATA_SEGMENTS_SIZE ((size_t)5)
72#define WASM_SECTION_FUNCTION_TABLE_SIZE ((size_t)6)
Ben Murdochda12d292016-06-02 14:46:10 +010073#define WASM_SECTION_END_SIZE ((size_t)4)
Ben Murdochc5610432016-08-08 18:44:38 +010074#define WASM_SECTION_START_FUNCTION_SIZE ((size_t)6)
75#define WASM_SECTION_IMPORT_TABLE_SIZE ((size_t)7)
76#define WASM_SECTION_EXPORT_TABLE_SIZE ((size_t)7)
77#define WASM_SECTION_FUNCTION_SIGNATURES_SIZE ((size_t)9)
78#define WASM_SECTION_FUNCTION_BODIES_SIZE ((size_t)5)
79#define WASM_SECTION_NAMES_SIZE ((size_t)5)
Ben Murdochda12d292016-06-02 14:46:10 +010080
81struct WasmSection {
82 enum class Code : uint32_t {
Ben Murdochc5610432016-08-08 18:44:38 +010083#define F(enumerator, order, string) enumerator,
Ben Murdochda12d292016-06-02 14:46:10 +010084 FOR_EACH_WASM_SECTION_TYPE(F)
85#undef F
86 Max
87 };
88 static WasmSection::Code begin();
89 static WasmSection::Code end();
90 static WasmSection::Code next(WasmSection::Code code);
91 static const char* getName(Code code);
Ben Murdochc5610432016-08-08 18:44:38 +010092 static int getOrder(Code code);
Ben Murdochda12d292016-06-02 14:46:10 +010093 static size_t getNameLength(Code code);
Ben Murdochc5610432016-08-08 18:44:38 +010094 static WasmSection::Code lookup(const byte* string, uint32_t length);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000095};
96
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097enum WasmFunctionDeclBit {
98 kDeclFunctionName = 0x01,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099 kDeclFunctionExport = 0x08
100};
101
102// Constants for fixed-size elements within a module.
103static const size_t kDeclMemorySize = 3;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000104static const size_t kDeclDataSegmentSize = 13;
105
Ben Murdochc5610432016-08-08 18:44:38 +0100106static const uint32_t kMaxReturnCount = 1;
107
Ben Murdoch097c5b22016-05-18 11:27:45 +0100108// Static representation of a WASM function.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000109struct WasmFunction {
110 FunctionSig* sig; // signature of the function.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100111 uint32_t func_index; // index into the function table.
Ben Murdochda12d292016-06-02 14:46:10 +0100112 uint32_t sig_index; // index into the signature table.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000113 uint32_t name_offset; // offset in the module bytes of the name, if any.
Ben Murdochda12d292016-06-02 14:46:10 +0100114 uint32_t name_length; // length in bytes of the name.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000115 uint32_t code_start_offset; // offset in the module bytes of code start.
116 uint32_t code_end_offset; // offset in the module bytes of code end.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000117 bool exported; // true if this function is exported.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000118};
119
Ben Murdoch097c5b22016-05-18 11:27:45 +0100120// Static representation of an imported WASM function.
121struct WasmImport {
122 FunctionSig* sig; // signature of the function.
Ben Murdochda12d292016-06-02 14:46:10 +0100123 uint32_t sig_index; // index into the signature table.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100124 uint32_t module_name_offset; // offset in module bytes of the module name.
Ben Murdochda12d292016-06-02 14:46:10 +0100125 uint32_t module_name_length; // length in bytes of the module name.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100126 uint32_t function_name_offset; // offset in module bytes of the import name.
Ben Murdochda12d292016-06-02 14:46:10 +0100127 uint32_t function_name_length; // length in bytes of the import name.
128};
129
130// Static representation of an exported WASM function.
131struct WasmExport {
132 uint32_t func_index; // index into the function table.
133 uint32_t name_offset; // offset in module bytes of the name to export.
134 uint32_t name_length; // length in bytes of the exported name.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100135};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000136
137// Static representation of a wasm global variable.
138struct WasmGlobal {
139 uint32_t name_offset; // offset in the module bytes of the name, if any.
Ben Murdochda12d292016-06-02 14:46:10 +0100140 uint32_t name_length; // length in bytes of the global name.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000141 MachineType type; // type of the global.
142 uint32_t offset; // offset from beginning of globals area.
143 bool exported; // true if this global is exported.
144};
145
146// Static representation of a wasm data segment.
147struct WasmDataSegment {
148 uint32_t dest_addr; // destination memory address of the data.
149 uint32_t source_offset; // start offset in the module bytes.
150 uint32_t source_size; // end offset in the module bytes.
151 bool init; // true if loaded upon instantiation.
152};
153
Ben Murdochda12d292016-06-02 14:46:10 +0100154enum ModuleOrigin { kWasmOrigin, kAsmJsOrigin };
155
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000156// Static representation of a module.
157struct WasmModule {
Ben Murdochda12d292016-06-02 14:46:10 +0100158 static const uint32_t kPageSize = 0x10000; // Page size, 64kb.
159 static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb
160 static const uint32_t kMaxMemPages = 16384; // Maximum memory size = 1gb
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000161
162 Isolate* shared_isolate; // isolate for storing shared code.
163 const byte* module_start; // starting address for the module bytes.
164 const byte* module_end; // end address for the module bytes.
Ben Murdochda12d292016-06-02 14:46:10 +0100165 uint32_t min_mem_pages; // minimum size of the memory in 64k pages.
166 uint32_t max_mem_pages; // maximum size of the memory in 64k pages.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000167 bool mem_export; // true if the memory is exported.
168 bool mem_external; // true if the memory is external.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100169 int start_function_index; // start function, if any.
Ben Murdochda12d292016-06-02 14:46:10 +0100170 ModuleOrigin origin; // origin of the module
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000171
Ben Murdochda12d292016-06-02 14:46:10 +0100172 std::vector<WasmGlobal> globals; // globals in this module.
173 std::vector<FunctionSig*> signatures; // signatures in this module.
174 std::vector<WasmFunction> functions; // functions in this module.
175 std::vector<WasmDataSegment> data_segments; // data segments in this module.
176 std::vector<uint16_t> function_table; // function table.
177 std::vector<WasmImport> import_table; // import table.
178 std::vector<WasmExport> export_table; // export table.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000179
180 WasmModule();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000181
Ben Murdochda12d292016-06-02 14:46:10 +0100182 // Get a string stored in the module bytes representing a name.
183 WasmName GetName(uint32_t offset, uint32_t length) const {
184 if (length == 0) return {"<?>", 3}; // no name.
185 CHECK(BoundsCheck(offset, offset + length));
Ben Murdochc5610432016-08-08 18:44:38 +0100186 DCHECK_GE(static_cast<int>(length), 0);
187 return {reinterpret_cast<const char*>(module_start + offset),
188 static_cast<int>(length)};
189 }
190
191 // Get a string stored in the module bytes representing a function name.
192 WasmName GetName(WasmFunction* function) const {
193 return GetName(function->name_offset, function->name_length);
Ben Murdochda12d292016-06-02 14:46:10 +0100194 }
195
196 // Get a string stored in the module bytes representing a name.
197 WasmName GetNameOrNull(uint32_t offset, uint32_t length) const {
198 if (length == 0) return {NULL, 0}; // no name.
199 CHECK(BoundsCheck(offset, offset + length));
Ben Murdochc5610432016-08-08 18:44:38 +0100200 DCHECK_GE(static_cast<int>(length), 0);
201 return {reinterpret_cast<const char*>(module_start + offset),
202 static_cast<int>(length)};
203 }
204
205 // Get a string stored in the module bytes representing a function name.
206 WasmName GetNameOrNull(WasmFunction* function) const {
207 return GetNameOrNull(function->name_offset, function->name_length);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000208 }
209
210 // Checks the given offset range is contained within the module bytes.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100211 bool BoundsCheck(uint32_t start, uint32_t end) const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000212 size_t size = module_end - module_start;
Ben Murdochc5610432016-08-08 18:44:38 +0100213 return start <= size && end <= size;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000214 }
215
216 // Creates a new instantiation of the module in the given isolate.
Ben Murdochc5610432016-08-08 18:44:38 +0100217 MaybeHandle<JSObject> Instantiate(Isolate* isolate, Handle<JSReceiver> ffi,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000218 Handle<JSArrayBuffer> memory);
219};
220
Ben Murdoch097c5b22016-05-18 11:27:45 +0100221// An instantiated WASM module, including memory, function table, etc.
222struct WasmModuleInstance {
223 WasmModule* module; // static representation of the module.
224 // -- Heap allocated --------------------------------------------------------
225 Handle<JSObject> js_object; // JavaScript module object.
226 Handle<Context> context; // JavaScript native context.
227 Handle<JSArrayBuffer> mem_buffer; // Handle to array buffer of memory.
228 Handle<JSArrayBuffer> globals_buffer; // Handle to array buffer of globals.
229 Handle<FixedArray> function_table; // indirect function table.
Ben Murdochda12d292016-06-02 14:46:10 +0100230 std::vector<Handle<Code>> function_code; // code objects for each function.
231 std::vector<Handle<Code>> import_code; // code objects for each import.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100232 // -- raw memory ------------------------------------------------------------
233 byte* mem_start; // start of linear memory.
234 size_t mem_size; // size of the linear memory.
235 // -- raw globals -----------------------------------------------------------
236 byte* globals_start; // start of the globals area.
237 size_t globals_size; // size of the globals area.
238
239 explicit WasmModuleInstance(WasmModule* m)
240 : module(m),
Ben Murdoch097c5b22016-05-18 11:27:45 +0100241 mem_start(nullptr),
242 mem_size(0),
243 globals_start(nullptr),
244 globals_size(0) {}
245};
246
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000247// forward declaration.
248class WasmLinker;
249
250// Interface provided to the decoder/graph builder which contains only
251// minimal information about the globals, functions, and function tables.
252struct ModuleEnv {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000253 WasmModule* module;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100254 WasmModuleInstance* instance;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000255 WasmLinker* linker;
Ben Murdochda12d292016-06-02 14:46:10 +0100256 ModuleOrigin origin;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000257
258 bool IsValidGlobal(uint32_t index) {
Ben Murdochda12d292016-06-02 14:46:10 +0100259 return module && index < module->globals.size();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000260 }
261 bool IsValidFunction(uint32_t index) {
Ben Murdochda12d292016-06-02 14:46:10 +0100262 return module && index < module->functions.size();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000263 }
264 bool IsValidSignature(uint32_t index) {
Ben Murdochda12d292016-06-02 14:46:10 +0100265 return module && index < module->signatures.size();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000266 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100267 bool IsValidImport(uint32_t index) {
Ben Murdochda12d292016-06-02 14:46:10 +0100268 return module && index < module->import_table.size();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100269 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000270 MachineType GetGlobalType(uint32_t index) {
271 DCHECK(IsValidGlobal(index));
Ben Murdochda12d292016-06-02 14:46:10 +0100272 return module->globals[index].type;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000273 }
274 FunctionSig* GetFunctionSignature(uint32_t index) {
275 DCHECK(IsValidFunction(index));
Ben Murdochda12d292016-06-02 14:46:10 +0100276 return module->functions[index].sig;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000277 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100278 FunctionSig* GetImportSignature(uint32_t index) {
279 DCHECK(IsValidImport(index));
Ben Murdochda12d292016-06-02 14:46:10 +0100280 return module->import_table[index].sig;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100281 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000282 FunctionSig* GetSignature(uint32_t index) {
283 DCHECK(IsValidSignature(index));
Ben Murdochda12d292016-06-02 14:46:10 +0100284 return module->signatures[index];
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000285 }
286 size_t FunctionTableSize() {
Ben Murdochda12d292016-06-02 14:46:10 +0100287 return module ? module->function_table.size() : 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000288 }
289
Ben Murdochda12d292016-06-02 14:46:10 +0100290 bool asm_js() { return origin == kAsmJsOrigin; }
291
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000292 Handle<Code> GetFunctionCode(uint32_t index);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100293 Handle<Code> GetImportCode(uint32_t index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000294 Handle<FixedArray> GetFunctionTable();
295
Ben Murdoch097c5b22016-05-18 11:27:45 +0100296 static compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone,
297 FunctionSig* sig);
298 static compiler::CallDescriptor* GetI32WasmCallDescriptor(
299 Zone* zone, compiler::CallDescriptor* descriptor);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000300 compiler::CallDescriptor* GetCallDescriptor(Zone* zone, uint32_t index);
301};
302
Ben Murdoch097c5b22016-05-18 11:27:45 +0100303// A helper for printing out the names of functions.
304struct WasmFunctionName {
305 const WasmFunction* function_;
306 const WasmModule* module_;
307 WasmFunctionName(const WasmFunction* function, const ModuleEnv* menv)
308 : function_(function), module_(menv ? menv->module : nullptr) {}
309};
310
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000311std::ostream& operator<<(std::ostream& os, const WasmModule& module);
312std::ostream& operator<<(std::ostream& os, const WasmFunction& function);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100313std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000314
315typedef Result<WasmModule*> ModuleResult;
316typedef Result<WasmFunction*> FunctionResult;
317
318// For testing. Decode, verify, and run the last exported function in the
319// given encoded module.
320int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start,
321 const byte* module_end, bool asm_js = false);
322
323// For testing. Decode, verify, and run the last exported function in the
324// given decoded module.
325int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100326
Ben Murdochc5610432016-08-08 18:44:38 +0100327// Extract a function name from the given wasm object.
328// Returns undefined if the function is unnamed or the function index is
329// invalid.
330Handle<Object> GetWasmFunctionName(Handle<JSObject> wasm, uint32_t func_index);
331
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000332} // namespace wasm
333} // namespace internal
334} // namespace v8
335
336#endif // V8_WASM_MODULE_H_