blob: 0c3df51d76b4a87a44579c80f2f63137373efe78 [file] [log] [blame]
Ben Murdoch014dc512016-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
Ben Murdochf91f0612016-11-29 16:50:11 +00008#include <memory>
9
Ben Murdoch014dc512016-03-22 12:00:34 +000010#include "src/api.h"
11#include "src/handles.h"
Ben Murdochf91f0612016-11-29 16:50:11 +000012#include "src/parsing/preparse-data.h"
13
Ben Murdoch13e2dad2016-09-16 13:49:30 +010014#include "src/wasm/wasm-opcodes.h"
15#include "src/wasm/wasm-result.h"
Ben Murdoch014dc512016-03-22 12:00:34 +000016
17namespace v8 {
18namespace internal {
19
20namespace compiler {
21class CallDescriptor;
Ben Murdochbcf72ee2016-08-08 18:44:38 +010022class WasmCompilationUnit;
Ben Murdoch014dc512016-03-22 12:00:34 +000023}
24
25namespace wasm {
26const size_t kMaxModuleSize = 1024 * 1024 * 1024;
27const size_t kMaxFunctionSize = 128 * 1024;
28const size_t kMaxStringSize = 256;
Ben Murdoch3b9bc312016-06-02 14:46:10 +010029const uint32_t kWasmMagic = 0x6d736100;
Ben Murdochbcf72ee2016-08-08 18:44:38 +010030const uint32_t kWasmVersion = 0x0b;
31const uint8_t kWasmFunctionTypeForm = 0x40;
Ben Murdoch014dc512016-03-22 12:00:34 +000032
Ben Murdoch3b9bc312016-06-02 14:46:10 +010033// WebAssembly sections are named as strings in the binary format, but
34// internally V8 uses an enum to handle them.
35//
36// Entries have the form F(enumerator, string).
Ben Murdochbcf72ee2016-08-08 18:44:38 +010037#define FOR_EACH_WASM_SECTION_TYPE(F) \
38 F(Signatures, 1, "type") \
39 F(ImportTable, 2, "import") \
40 F(FunctionSignatures, 3, "function") \
41 F(FunctionTable, 4, "table") \
42 F(Memory, 5, "memory") \
43 F(ExportTable, 6, "export") \
44 F(StartFunction, 7, "start") \
45 F(FunctionBodies, 8, "code") \
46 F(DataSegments, 9, "data") \
47 F(Names, 10, "name") \
Ben Murdochbcf72ee2016-08-08 18:44:38 +010048 F(Globals, 0, "global") \
49 F(End, 0, "end")
Ben Murdoch3b9bc312016-06-02 14:46:10 +010050
51// Contants for the above section types: {LEB128 length, characters...}.
52#define WASM_SECTION_MEMORY 6, 'm', 'e', 'm', 'o', 'r', 'y'
Ben Murdochbcf72ee2016-08-08 18:44:38 +010053#define WASM_SECTION_SIGNATURES 4, 't', 'y', 'p', 'e'
Ben Murdochbcf72ee2016-08-08 18:44:38 +010054#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 Murdoch3b9bc312016-06-02 14:46:10 +010057#define WASM_SECTION_END 3, 'e', 'n', 'd'
Ben Murdochbcf72ee2016-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 Murdoch3b9bc312016-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 Murdochbcf72ee2016-08-08 18:44:38 +010068#define WASM_SECTION_SIGNATURES_SIZE ((size_t)5)
Ben Murdochbcf72ee2016-08-08 18:44:38 +010069#define WASM_SECTION_GLOBALS_SIZE ((size_t)7)
70#define WASM_SECTION_DATA_SEGMENTS_SIZE ((size_t)5)
71#define WASM_SECTION_FUNCTION_TABLE_SIZE ((size_t)6)
Ben Murdoch3b9bc312016-06-02 14:46:10 +010072#define WASM_SECTION_END_SIZE ((size_t)4)
Ben Murdochbcf72ee2016-08-08 18:44:38 +010073#define WASM_SECTION_START_FUNCTION_SIZE ((size_t)6)
74#define WASM_SECTION_IMPORT_TABLE_SIZE ((size_t)7)
75#define WASM_SECTION_EXPORT_TABLE_SIZE ((size_t)7)
76#define WASM_SECTION_FUNCTION_SIGNATURES_SIZE ((size_t)9)
77#define WASM_SECTION_FUNCTION_BODIES_SIZE ((size_t)5)
78#define WASM_SECTION_NAMES_SIZE ((size_t)5)
Ben Murdoch13e2dad2016-09-16 13:49:30 +010079
80class WasmDebugInfo;
Ben Murdoch3b9bc312016-06-02 14:46:10 +010081
82struct WasmSection {
83 enum class Code : uint32_t {
Ben Murdochbcf72ee2016-08-08 18:44:38 +010084#define F(enumerator, order, string) enumerator,
Ben Murdoch3b9bc312016-06-02 14:46:10 +010085 FOR_EACH_WASM_SECTION_TYPE(F)
86#undef F
87 Max
88 };
89 static WasmSection::Code begin();
90 static WasmSection::Code end();
91 static WasmSection::Code next(WasmSection::Code code);
92 static const char* getName(Code code);
Ben Murdochbcf72ee2016-08-08 18:44:38 +010093 static int getOrder(Code code);
Ben Murdoch3b9bc312016-06-02 14:46:10 +010094 static size_t getNameLength(Code code);
Ben Murdochbcf72ee2016-08-08 18:44:38 +010095 static WasmSection::Code lookup(const byte* string, uint32_t length);
Ben Murdoch014dc512016-03-22 12:00:34 +000096};
97
Ben Murdoch014dc512016-03-22 12:00:34 +000098enum WasmFunctionDeclBit {
99 kDeclFunctionName = 0x01,
Ben Murdoch014dc512016-03-22 12:00:34 +0000100 kDeclFunctionExport = 0x08
101};
102
103// Constants for fixed-size elements within a module.
104static const size_t kDeclMemorySize = 3;
Ben Murdoch014dc512016-03-22 12:00:34 +0000105static const size_t kDeclDataSegmentSize = 13;
106
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100107static const uint32_t kMaxReturnCount = 1;
108
Ben Murdoch109988c2016-05-18 11:27:45 +0100109// Static representation of a WASM function.
Ben Murdoch014dc512016-03-22 12:00:34 +0000110struct WasmFunction {
111 FunctionSig* sig; // signature of the function.
Ben Murdoch109988c2016-05-18 11:27:45 +0100112 uint32_t func_index; // index into the function table.
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100113 uint32_t sig_index; // index into the signature table.
Ben Murdoch014dc512016-03-22 12:00:34 +0000114 uint32_t name_offset; // offset in the module bytes of the name, if any.
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100115 uint32_t name_length; // length in bytes of the name.
Ben Murdoch014dc512016-03-22 12:00:34 +0000116 uint32_t code_start_offset; // offset in the module bytes of code start.
117 uint32_t code_end_offset; // offset in the module bytes of code end.
Ben Murdoch014dc512016-03-22 12:00:34 +0000118};
119
Ben Murdoch109988c2016-05-18 11:27:45 +0100120// Static representation of an imported WASM function.
121struct WasmImport {
122 FunctionSig* sig; // signature of the function.
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100123 uint32_t sig_index; // index into the signature table.
Ben Murdoch109988c2016-05-18 11:27:45 +0100124 uint32_t module_name_offset; // offset in module bytes of the module name.
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100125 uint32_t module_name_length; // length in bytes of the module name.
Ben Murdoch109988c2016-05-18 11:27:45 +0100126 uint32_t function_name_offset; // offset in module bytes of the import name.
Ben Murdoch3b9bc312016-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 Murdoch109988c2016-05-18 11:27:45 +0100135};
Ben Murdoch014dc512016-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 Murdoch3b9bc312016-06-02 14:46:10 +0100140 uint32_t name_length; // length in bytes of the global name.
Ben Murdochf91f0612016-11-29 16:50:11 +0000141 LocalType type; // type of the global.
Ben Murdoch014dc512016-03-22 12:00:34 +0000142 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 Murdochf91f0612016-11-29 16:50:11 +0000154// Static representation of a wasm indirect call table.
155struct WasmIndirectFunctionTable {
156 uint32_t size; // initial table size.
157 uint32_t max_size; // maximum table size.
158 std::vector<uint16_t> values; // function table.
159};
160
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100161enum ModuleOrigin { kWasmOrigin, kAsmJsOrigin };
162
Ben Murdoch014dc512016-03-22 12:00:34 +0000163// Static representation of a module.
164struct WasmModule {
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100165 static const uint32_t kPageSize = 0x10000; // Page size, 64kb.
166 static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb
167 static const uint32_t kMaxMemPages = 16384; // Maximum memory size = 1gb
Ben Murdoch014dc512016-03-22 12:00:34 +0000168
Ben Murdoch014dc512016-03-22 12:00:34 +0000169 const byte* module_start; // starting address for the module bytes.
170 const byte* module_end; // end address for the module bytes.
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100171 uint32_t min_mem_pages; // minimum size of the memory in 64k pages.
172 uint32_t max_mem_pages; // maximum size of the memory in 64k pages.
Ben Murdoch014dc512016-03-22 12:00:34 +0000173 bool mem_export; // true if the memory is exported.
174 bool mem_external; // true if the memory is external.
Ben Murdochf91f0612016-11-29 16:50:11 +0000175 // TODO(wasm): reconcile start function index being an int with
176 // the fact that we index on uint32_t, so we may technically not be
177 // able to represent some start_function_index -es.
Ben Murdoch109988c2016-05-18 11:27:45 +0100178 int start_function_index; // start function, if any.
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100179 ModuleOrigin origin; // origin of the module
Ben Murdoch014dc512016-03-22 12:00:34 +0000180
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100181 std::vector<WasmGlobal> globals; // globals in this module.
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100182 uint32_t globals_size; // size of globals table.
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100183 std::vector<FunctionSig*> signatures; // signatures in this module.
184 std::vector<WasmFunction> functions; // functions in this module.
185 std::vector<WasmDataSegment> data_segments; // data segments in this module.
Ben Murdochf91f0612016-11-29 16:50:11 +0000186 std::vector<WasmIndirectFunctionTable> function_tables; // function tables.
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100187 std::vector<WasmImport> import_table; // import table.
188 std::vector<WasmExport> export_table; // export table.
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100189 // We store the semaphore here to extend its lifetime. In <libc-2.21, which we
190 // use on the try bots, semaphore::Wait() can return while some compilation
191 // tasks are still executing semaphore::Signal(). If the semaphore is cleaned
192 // up right after semaphore::Wait() returns, then this can cause an
193 // invalid-semaphore error in the compilation tasks.
194 // TODO(wasm): Move this semaphore back to CompileInParallel when the try bots
195 // switch to libc-2.21 or higher.
Ben Murdochf91f0612016-11-29 16:50:11 +0000196 std::unique_ptr<base::Semaphore> pending_tasks;
Ben Murdoch014dc512016-03-22 12:00:34 +0000197
Ben Murdochf91f0612016-11-29 16:50:11 +0000198 WasmModule() : WasmModule(nullptr) {}
199 explicit WasmModule(byte* module_start);
Ben Murdoch014dc512016-03-22 12:00:34 +0000200
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100201 // Get a string stored in the module bytes representing a name.
202 WasmName GetName(uint32_t offset, uint32_t length) const {
203 if (length == 0) return {"<?>", 3}; // no name.
204 CHECK(BoundsCheck(offset, offset + length));
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100205 DCHECK_GE(static_cast<int>(length), 0);
206 return {reinterpret_cast<const char*>(module_start + offset),
207 static_cast<int>(length)};
208 }
209
210 // Get a string stored in the module bytes representing a function name.
211 WasmName GetName(WasmFunction* function) const {
212 return GetName(function->name_offset, function->name_length);
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100213 }
214
215 // Get a string stored in the module bytes representing a name.
216 WasmName GetNameOrNull(uint32_t offset, uint32_t length) const {
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100217 if (offset == 0 && length == 0) return {NULL, 0}; // no name.
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100218 CHECK(BoundsCheck(offset, offset + length));
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100219 DCHECK_GE(static_cast<int>(length), 0);
220 return {reinterpret_cast<const char*>(module_start + offset),
221 static_cast<int>(length)};
222 }
223
224 // Get a string stored in the module bytes representing a function name.
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100225 WasmName GetNameOrNull(const WasmFunction* function) const {
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100226 return GetNameOrNull(function->name_offset, function->name_length);
Ben Murdoch014dc512016-03-22 12:00:34 +0000227 }
228
229 // Checks the given offset range is contained within the module bytes.
Ben Murdoch109988c2016-05-18 11:27:45 +0100230 bool BoundsCheck(uint32_t start, uint32_t end) const {
Ben Murdoch014dc512016-03-22 12:00:34 +0000231 size_t size = module_end - module_start;
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100232 return start <= size && end <= size;
Ben Murdoch014dc512016-03-22 12:00:34 +0000233 }
234
235 // Creates a new instantiation of the module in the given isolate.
Ben Murdochf91f0612016-11-29 16:50:11 +0000236 static MaybeHandle<JSObject> Instantiate(Isolate* isolate,
237 Handle<FixedArray> compiled_module,
238 Handle<JSReceiver> ffi,
239 Handle<JSArrayBuffer> memory);
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100240
Ben Murdochf91f0612016-11-29 16:50:11 +0000241 MaybeHandle<FixedArray> CompileFunctions(Isolate* isolate,
242 ErrorThrower* thrower) const;
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100243
Ben Murdochf91f0612016-11-29 16:50:11 +0000244 private:
245 DISALLOW_COPY_AND_ASSIGN(WasmModule);
Ben Murdoch014dc512016-03-22 12:00:34 +0000246};
247
Ben Murdoch109988c2016-05-18 11:27:45 +0100248// An instantiated WASM module, including memory, function table, etc.
249struct WasmModuleInstance {
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100250 const WasmModule* module; // static representation of the module.
Ben Murdoch109988c2016-05-18 11:27:45 +0100251 // -- Heap allocated --------------------------------------------------------
252 Handle<JSObject> js_object; // JavaScript module object.
253 Handle<Context> context; // JavaScript native context.
254 Handle<JSArrayBuffer> mem_buffer; // Handle to array buffer of memory.
255 Handle<JSArrayBuffer> globals_buffer; // Handle to array buffer of globals.
Ben Murdochf91f0612016-11-29 16:50:11 +0000256 std::vector<Handle<FixedArray>> function_tables; // indirect function tables.
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100257 std::vector<Handle<Code>> function_code; // code objects for each function.
258 std::vector<Handle<Code>> import_code; // code objects for each import.
Ben Murdoch109988c2016-05-18 11:27:45 +0100259 // -- raw memory ------------------------------------------------------------
260 byte* mem_start; // start of linear memory.
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100261 uint32_t mem_size; // size of the linear memory.
Ben Murdoch109988c2016-05-18 11:27:45 +0100262 // -- raw globals -----------------------------------------------------------
263 byte* globals_start; // start of the globals area.
Ben Murdoch109988c2016-05-18 11:27:45 +0100264
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100265 explicit WasmModuleInstance(const WasmModule* m)
Ben Murdoch109988c2016-05-18 11:27:45 +0100266 : module(m),
Ben Murdochf91f0612016-11-29 16:50:11 +0000267 function_tables(m->function_tables.size()),
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100268 function_code(m->functions.size()),
269 import_code(m->import_table.size()),
Ben Murdoch109988c2016-05-18 11:27:45 +0100270 mem_start(nullptr),
271 mem_size(0),
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100272 globals_start(nullptr) {}
Ben Murdoch109988c2016-05-18 11:27:45 +0100273};
274
Ben Murdoch014dc512016-03-22 12:00:34 +0000275// Interface provided to the decoder/graph builder which contains only
276// minimal information about the globals, functions, and function tables.
277struct ModuleEnv {
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100278 const WasmModule* module;
Ben Murdoch109988c2016-05-18 11:27:45 +0100279 WasmModuleInstance* instance;
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100280 ModuleOrigin origin;
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100281 // TODO(mtrofin): remove this once we introduce WASM_DIRECT_CALL
282 // reloc infos.
283 std::vector<Handle<Code>> placeholders;
Ben Murdoch014dc512016-03-22 12:00:34 +0000284
Ben Murdochf91f0612016-11-29 16:50:11 +0000285 bool IsValidGlobal(uint32_t index) const {
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100286 return module && index < module->globals.size();
Ben Murdoch014dc512016-03-22 12:00:34 +0000287 }
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100288 bool IsValidFunction(uint32_t index) const {
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100289 return module && index < module->functions.size();
Ben Murdoch014dc512016-03-22 12:00:34 +0000290 }
Ben Murdochf91f0612016-11-29 16:50:11 +0000291 bool IsValidSignature(uint32_t index) const {
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100292 return module && index < module->signatures.size();
Ben Murdoch014dc512016-03-22 12:00:34 +0000293 }
Ben Murdochf91f0612016-11-29 16:50:11 +0000294 bool IsValidImport(uint32_t index) const {
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100295 return module && index < module->import_table.size();
Ben Murdoch109988c2016-05-18 11:27:45 +0100296 }
Ben Murdochf91f0612016-11-29 16:50:11 +0000297 bool IsValidTable(uint32_t index) const {
298 return module && index < module->function_tables.size();
299 }
300 LocalType GetGlobalType(uint32_t index) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000301 DCHECK(IsValidGlobal(index));
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100302 return module->globals[index].type;
Ben Murdoch014dc512016-03-22 12:00:34 +0000303 }
304 FunctionSig* GetFunctionSignature(uint32_t index) {
305 DCHECK(IsValidFunction(index));
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100306 return module->functions[index].sig;
Ben Murdoch014dc512016-03-22 12:00:34 +0000307 }
Ben Murdoch109988c2016-05-18 11:27:45 +0100308 FunctionSig* GetImportSignature(uint32_t index) {
309 DCHECK(IsValidImport(index));
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100310 return module->import_table[index].sig;
Ben Murdoch109988c2016-05-18 11:27:45 +0100311 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000312 FunctionSig* GetSignature(uint32_t index) {
313 DCHECK(IsValidSignature(index));
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100314 return module->signatures[index];
Ben Murdoch014dc512016-03-22 12:00:34 +0000315 }
Ben Murdochf91f0612016-11-29 16:50:11 +0000316 const WasmIndirectFunctionTable* GetTable(uint32_t index) const {
317 DCHECK(IsValidTable(index));
318 return &module->function_tables[index];
Ben Murdoch014dc512016-03-22 12:00:34 +0000319 }
320
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100321 bool asm_js() { return origin == kAsmJsOrigin; }
322
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100323 Handle<Code> GetCodeOrPlaceholder(uint32_t index) const;
Ben Murdoch109988c2016-05-18 11:27:45 +0100324 Handle<Code> GetImportCode(uint32_t index);
Ben Murdoch014dc512016-03-22 12:00:34 +0000325
Ben Murdoch109988c2016-05-18 11:27:45 +0100326 static compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone,
327 FunctionSig* sig);
328 static compiler::CallDescriptor* GetI32WasmCallDescriptor(
329 Zone* zone, compiler::CallDescriptor* descriptor);
Ben Murdoch014dc512016-03-22 12:00:34 +0000330 compiler::CallDescriptor* GetCallDescriptor(Zone* zone, uint32_t index);
331};
332
Ben Murdoch109988c2016-05-18 11:27:45 +0100333// A helper for printing out the names of functions.
334struct WasmFunctionName {
335 const WasmFunction* function_;
336 const WasmModule* module_;
337 WasmFunctionName(const WasmFunction* function, const ModuleEnv* menv)
338 : function_(function), module_(menv ? menv->module : nullptr) {}
339};
340
Ben Murdoch014dc512016-03-22 12:00:34 +0000341std::ostream& operator<<(std::ostream& os, const WasmModule& module);
342std::ostream& operator<<(std::ostream& os, const WasmFunction& function);
Ben Murdoch109988c2016-05-18 11:27:45 +0100343std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name);
Ben Murdoch014dc512016-03-22 12:00:34 +0000344
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100345typedef Result<const WasmModule*> ModuleResult;
Ben Murdoch014dc512016-03-22 12:00:34 +0000346typedef Result<WasmFunction*> FunctionResult;
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100347typedef std::vector<std::pair<int, int>> FunctionOffsets;
348typedef Result<FunctionOffsets> FunctionOffsetsResult;
Ben Murdoch014dc512016-03-22 12:00:34 +0000349
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100350// Extract a function name from the given wasm object.
351// Returns "<WASM UNNAMED>" if the function is unnamed or the name is not a
352// valid UTF-8 string.
353Handle<String> GetWasmFunctionName(Isolate* isolate, Handle<Object> wasm,
354 uint32_t func_index);
355
356// Extract a function name from the given wasm object.
357// Returns a null handle if the function is unnamed or the name is not a valid
358// UTF-8 string.
359Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm,
360 uint32_t func_index);
361
362// Return the binary source bytes of a wasm module.
363SeqOneByteString* GetWasmBytes(JSObject* wasm);
364
365// Get the debug info associated with the given wasm object.
366// If no debug info exists yet, it is created automatically.
Ben Murdochf91f0612016-11-29 16:50:11 +0000367Handle<WasmDebugInfo> GetDebugInfo(Handle<JSObject> wasm);
368
369// Return the number of functions in the given wasm object.
370int GetNumberOfFunctions(JSObject* wasm);
371
372// Create and export JSFunction
373Handle<JSFunction> WrapExportCodeAsJSFunction(Isolate* isolate,
374 Handle<Code> export_code,
375 Handle<String> name, int arity,
376 MaybeHandle<ByteArray> signature,
377 Handle<JSObject> module_instance);
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100378
379// Check whether the given object is a wasm object.
380// This checks the number and type of internal fields, so it's not 100 percent
381// secure. If it turns out that we need more complete checks, we could add a
382// special marker as internal field, which will definitely never occur anywhere
383// else.
384bool IsWasmObject(Object* object);
385
Ben Murdochf91f0612016-11-29 16:50:11 +0000386// Update memory references of code objects associated with the module
387bool UpdateWasmModuleMemory(Handle<JSObject> object, Address old_start,
388 Address new_start, uint32_t old_size,
389 uint32_t new_size);
390
391// Constructs a single function table as a FixedArray of double size,
392// populating it with function signature indices and function indices.
393Handle<FixedArray> BuildFunctionTable(Isolate* isolate, uint32_t index,
394 const WasmModule* module);
395
396// Populates a function table by replacing function indices with handles to
397// the compiled code.
398void PopulateFunctionTable(Handle<FixedArray> table, uint32_t table_size,
399 const std::vector<Handle<Code>>* code_table);
400
401Handle<JSObject> CreateCompiledModuleObject(Isolate* isolate,
402 Handle<FixedArray> compiled_module);
403
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100404namespace testing {
405
406// Decode, verify, and run the function labeled "main" in the
407// given encoded module. The module should have no imports.
Ben Murdoch014dc512016-03-22 12:00:34 +0000408int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start,
409 const byte* module_end, bool asm_js = false);
410
Ben Murdochf91f0612016-11-29 16:50:11 +0000411int32_t CallFunction(Isolate* isolate, Handle<JSObject> instance,
412 ErrorThrower* thrower, const char* name, int argc,
413 Handle<Object> argv[]);
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100414} // namespace testing
Ben Murdoch014dc512016-03-22 12:00:34 +0000415} // namespace wasm
416} // namespace internal
417} // namespace v8
418
419#endif // V8_WASM_MODULE_H_