OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_WASM_MODULE_H_ | 5 #ifndef V8_WASM_MODULE_H_ |
6 #define V8_WASM_MODULE_H_ | 6 #define V8_WASM_MODULE_H_ |
7 | 7 |
8 #include "src/api.h" | 8 #include "src/api.h" |
9 #include "src/handles.h" | 9 #include "src/handles.h" |
10 #include "src/wasm/wasm-opcodes.h" | 10 #include "src/wasm/wasm-opcodes.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 F(Signatures, 1, "type") \ | 34 F(Signatures, 1, "type") \ |
35 F(ImportTable, 2, "import") \ | 35 F(ImportTable, 2, "import") \ |
36 F(FunctionSignatures, 3, "function") \ | 36 F(FunctionSignatures, 3, "function") \ |
37 F(FunctionTable, 4, "table") \ | 37 F(FunctionTable, 4, "table") \ |
38 F(Memory, 5, "memory") \ | 38 F(Memory, 5, "memory") \ |
39 F(ExportTable, 6, "export") \ | 39 F(ExportTable, 6, "export") \ |
40 F(StartFunction, 7, "start") \ | 40 F(StartFunction, 7, "start") \ |
41 F(FunctionBodies, 8, "code") \ | 41 F(FunctionBodies, 8, "code") \ |
42 F(DataSegments, 9, "data") \ | 42 F(DataSegments, 9, "data") \ |
43 F(Names, 10, "name") \ | 43 F(Names, 10, "name") \ |
| 44 F(FunctionTablePad, 11, "table_pad") \ |
44 F(Globals, 0, "global") \ | 45 F(Globals, 0, "global") \ |
45 F(End, 0, "end") | 46 F(End, 0, "end") |
46 | 47 |
47 // Contants for the above section types: {LEB128 length, characters...}. | 48 // Contants for the above section types: {LEB128 length, characters...}. |
48 #define WASM_SECTION_MEMORY 6, 'm', 'e', 'm', 'o', 'r', 'y' | 49 #define WASM_SECTION_MEMORY 6, 'm', 'e', 'm', 'o', 'r', 'y' |
49 #define WASM_SECTION_SIGNATURES 4, 't', 'y', 'p', 'e' | 50 #define WASM_SECTION_SIGNATURES 4, 't', 'y', 'p', 'e' |
50 #define WASM_SECTION_GLOBALS 6, 'g', 'l', 'o', 'b', 'a', 'l' | 51 #define WASM_SECTION_GLOBALS 6, 'g', 'l', 'o', 'b', 'a', 'l' |
51 #define WASM_SECTION_DATA_SEGMENTS 4, 'd', 'a', 't', 'a' | 52 #define WASM_SECTION_DATA_SEGMENTS 4, 'd', 'a', 't', 'a' |
52 #define WASM_SECTION_FUNCTION_TABLE 5, 't', 'a', 'b', 'l', 'e' | 53 #define WASM_SECTION_FUNCTION_TABLE 5, 't', 'a', 'b', 'l', 'e' |
53 #define WASM_SECTION_END 3, 'e', 'n', 'd' | 54 #define WASM_SECTION_END 3, 'e', 'n', 'd' |
54 #define WASM_SECTION_START_FUNCTION 5, 's', 't', 'a', 'r', 't' | 55 #define WASM_SECTION_START_FUNCTION 5, 's', 't', 'a', 'r', 't' |
55 #define WASM_SECTION_IMPORT_TABLE 6, 'i', 'm', 'p', 'o', 'r', 't' | 56 #define WASM_SECTION_IMPORT_TABLE 6, 'i', 'm', 'p', 'o', 'r', 't' |
56 #define WASM_SECTION_EXPORT_TABLE 6, 'e', 'x', 'p', 'o', 'r', 't' | 57 #define WASM_SECTION_EXPORT_TABLE 6, 'e', 'x', 'p', 'o', 'r', 't' |
57 #define WASM_SECTION_FUNCTION_SIGNATURES \ | 58 #define WASM_SECTION_FUNCTION_SIGNATURES \ |
58 8, 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n' | 59 8, 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n' |
59 #define WASM_SECTION_FUNCTION_BODIES 4, 'c', 'o', 'd', 'e' | 60 #define WASM_SECTION_FUNCTION_BODIES 4, 'c', 'o', 'd', 'e' |
60 #define WASM_SECTION_NAMES 4, 'n', 'a', 'm', 'e' | 61 #define WASM_SECTION_NAMES 4, 'n', 'a', 'm', 'e' |
| 62 #define WASM_SECTION_FUNCTION_TABLE_PAD \ |
| 63 9, 't', 'a', 'b', 'l', 'e', '_', 'p', 'a', 'd' |
61 | 64 |
62 // Constants for the above section headers' size (LEB128 + characters). | 65 // Constants for the above section headers' size (LEB128 + characters). |
63 #define WASM_SECTION_MEMORY_SIZE ((size_t)7) | 66 #define WASM_SECTION_MEMORY_SIZE ((size_t)7) |
64 #define WASM_SECTION_SIGNATURES_SIZE ((size_t)5) | 67 #define WASM_SECTION_SIGNATURES_SIZE ((size_t)5) |
65 #define WASM_SECTION_GLOBALS_SIZE ((size_t)7) | 68 #define WASM_SECTION_GLOBALS_SIZE ((size_t)7) |
66 #define WASM_SECTION_DATA_SEGMENTS_SIZE ((size_t)5) | 69 #define WASM_SECTION_DATA_SEGMENTS_SIZE ((size_t)5) |
67 #define WASM_SECTION_FUNCTION_TABLE_SIZE ((size_t)6) | 70 #define WASM_SECTION_FUNCTION_TABLE_SIZE ((size_t)6) |
68 #define WASM_SECTION_END_SIZE ((size_t)4) | 71 #define WASM_SECTION_END_SIZE ((size_t)4) |
69 #define WASM_SECTION_START_FUNCTION_SIZE ((size_t)6) | 72 #define WASM_SECTION_START_FUNCTION_SIZE ((size_t)6) |
70 #define WASM_SECTION_IMPORT_TABLE_SIZE ((size_t)7) | 73 #define WASM_SECTION_IMPORT_TABLE_SIZE ((size_t)7) |
71 #define WASM_SECTION_EXPORT_TABLE_SIZE ((size_t)7) | 74 #define WASM_SECTION_EXPORT_TABLE_SIZE ((size_t)7) |
72 #define WASM_SECTION_FUNCTION_SIGNATURES_SIZE ((size_t)9) | 75 #define WASM_SECTION_FUNCTION_SIGNATURES_SIZE ((size_t)9) |
73 #define WASM_SECTION_FUNCTION_BODIES_SIZE ((size_t)5) | 76 #define WASM_SECTION_FUNCTION_BODIES_SIZE ((size_t)5) |
74 #define WASM_SECTION_NAMES_SIZE ((size_t)5) | 77 #define WASM_SECTION_NAMES_SIZE ((size_t)5) |
| 78 #define WASM_SECTION_FUNCTION_TABLE_PAD_SIZE ((size_t)10) |
75 | 79 |
76 class WasmDebugInfo; | 80 class WasmDebugInfo; |
77 | 81 |
78 struct WasmSection { | 82 struct WasmSection { |
79 enum class Code : uint32_t { | 83 enum class Code : uint32_t { |
80 #define F(enumerator, order, string) enumerator, | 84 #define F(enumerator, order, string) enumerator, |
81 FOR_EACH_WASM_SECTION_TYPE(F) | 85 FOR_EACH_WASM_SECTION_TYPE(F) |
82 #undef F | 86 #undef F |
83 Max | 87 Max |
84 }; | 88 }; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 const byte* module_end; // end address for the module bytes. | 163 const byte* module_end; // end address for the module bytes. |
160 uint32_t min_mem_pages; // minimum size of the memory in 64k pages. | 164 uint32_t min_mem_pages; // minimum size of the memory in 64k pages. |
161 uint32_t max_mem_pages; // maximum size of the memory in 64k pages. | 165 uint32_t max_mem_pages; // maximum size of the memory in 64k pages. |
162 bool mem_export; // true if the memory is exported. | 166 bool mem_export; // true if the memory is exported. |
163 bool mem_external; // true if the memory is external. | 167 bool mem_external; // true if the memory is external. |
164 int start_function_index; // start function, if any. | 168 int start_function_index; // start function, if any. |
165 ModuleOrigin origin; // origin of the module | 169 ModuleOrigin origin; // origin of the module |
166 | 170 |
167 std::vector<WasmGlobal> globals; // globals in this module. | 171 std::vector<WasmGlobal> globals; // globals in this module. |
168 uint32_t globals_size; // size of globals table. | 172 uint32_t globals_size; // size of globals table. |
| 173 uint32_t indirect_table_size; // size of indirect function |
| 174 // table (includes padding). |
169 std::vector<FunctionSig*> signatures; // signatures in this module. | 175 std::vector<FunctionSig*> signatures; // signatures in this module. |
170 std::vector<WasmFunction> functions; // functions in this module. | 176 std::vector<WasmFunction> functions; // functions in this module. |
171 std::vector<WasmDataSegment> data_segments; // data segments in this module. | 177 std::vector<WasmDataSegment> data_segments; // data segments in this module. |
172 std::vector<uint16_t> function_table; // function table. | 178 std::vector<uint16_t> function_table; // function table. |
173 std::vector<WasmImport> import_table; // import table. | 179 std::vector<WasmImport> import_table; // import table. |
174 std::vector<WasmExport> export_table; // export table. | 180 std::vector<WasmExport> export_table; // export table. |
175 | 181 |
176 WasmModule(); | 182 WasmModule(); |
177 | 183 |
178 // Get a string stored in the module bytes representing a name. | 184 // Get a string stored in the module bytes representing a name. |
(...skipping 28 matching lines...) Expand all Loading... |
207 bool BoundsCheck(uint32_t start, uint32_t end) const { | 213 bool BoundsCheck(uint32_t start, uint32_t end) const { |
208 size_t size = module_end - module_start; | 214 size_t size = module_end - module_start; |
209 return start <= size && end <= size; | 215 return start <= size && end <= size; |
210 } | 216 } |
211 | 217 |
212 // Creates a new instantiation of the module in the given isolate. | 218 // Creates a new instantiation of the module in the given isolate. |
213 MaybeHandle<JSObject> Instantiate(Isolate* isolate, Handle<JSReceiver> ffi, | 219 MaybeHandle<JSObject> Instantiate(Isolate* isolate, Handle<JSReceiver> ffi, |
214 Handle<JSArrayBuffer> memory) const; | 220 Handle<JSArrayBuffer> memory) const; |
215 | 221 |
216 Handle<FixedArray> CompileFunctions(Isolate* isolate) const; | 222 Handle<FixedArray> CompileFunctions(Isolate* isolate) const; |
| 223 |
| 224 uint32_t FunctionTableSize() const { |
| 225 if (indirect_table_size > 0) { |
| 226 return indirect_table_size; |
| 227 } |
| 228 DCHECK_LE(function_table.size(), UINT32_MAX); |
| 229 return static_cast<uint32_t>(function_table.size()); |
| 230 } |
217 }; | 231 }; |
218 | 232 |
219 // An instantiated WASM module, including memory, function table, etc. | 233 // An instantiated WASM module, including memory, function table, etc. |
220 struct WasmModuleInstance { | 234 struct WasmModuleInstance { |
221 const WasmModule* module; // static representation of the module. | 235 const WasmModule* module; // static representation of the module. |
222 // -- Heap allocated -------------------------------------------------------- | 236 // -- Heap allocated -------------------------------------------------------- |
223 Handle<JSObject> js_object; // JavaScript module object. | 237 Handle<JSObject> js_object; // JavaScript module object. |
224 Handle<Context> context; // JavaScript native context. | 238 Handle<Context> context; // JavaScript native context. |
225 Handle<JSArrayBuffer> mem_buffer; // Handle to array buffer of memory. | 239 Handle<JSArrayBuffer> mem_buffer; // Handle to array buffer of memory. |
226 Handle<JSArrayBuffer> globals_buffer; // Handle to array buffer of globals. | 240 Handle<JSArrayBuffer> globals_buffer; // Handle to array buffer of globals. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 return module->functions[index].sig; | 287 return module->functions[index].sig; |
274 } | 288 } |
275 FunctionSig* GetImportSignature(uint32_t index) { | 289 FunctionSig* GetImportSignature(uint32_t index) { |
276 DCHECK(IsValidImport(index)); | 290 DCHECK(IsValidImport(index)); |
277 return module->import_table[index].sig; | 291 return module->import_table[index].sig; |
278 } | 292 } |
279 FunctionSig* GetSignature(uint32_t index) { | 293 FunctionSig* GetSignature(uint32_t index) { |
280 DCHECK(IsValidSignature(index)); | 294 DCHECK(IsValidSignature(index)); |
281 return module->signatures[index]; | 295 return module->signatures[index]; |
282 } | 296 } |
283 size_t FunctionTableSize() { | 297 uint32_t FunctionTableSize() const { |
284 return module ? module->function_table.size() : 0; | 298 return module->FunctionTableSize(); |
285 } | 299 } |
286 | 300 |
287 bool asm_js() { return origin == kAsmJsOrigin; } | 301 bool asm_js() { return origin == kAsmJsOrigin; } |
288 | 302 |
289 Handle<Code> GetCodeOrPlaceholder(uint32_t index) const; | 303 Handle<Code> GetCodeOrPlaceholder(uint32_t index) const; |
290 Handle<Code> GetImportCode(uint32_t index); | 304 Handle<Code> GetImportCode(uint32_t index); |
291 Handle<FixedArray> GetFunctionTable(); | |
292 | 305 |
293 static compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone, | 306 static compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone, |
294 FunctionSig* sig); | 307 FunctionSig* sig); |
295 static compiler::CallDescriptor* GetI32WasmCallDescriptor( | 308 static compiler::CallDescriptor* GetI32WasmCallDescriptor( |
296 Zone* zone, compiler::CallDescriptor* descriptor); | 309 Zone* zone, compiler::CallDescriptor* descriptor); |
297 compiler::CallDescriptor* GetCallDescriptor(Zone* zone, uint32_t index); | 310 compiler::CallDescriptor* GetCallDescriptor(Zone* zone, uint32_t index); |
298 }; | 311 }; |
299 | 312 |
300 // A helper for printing out the names of functions. | 313 // A helper for printing out the names of functions. |
301 struct WasmFunctionName { | 314 struct WasmFunctionName { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 // secure. If it turns out that we need more complete checks, we could add a | 360 // secure. If it turns out that we need more complete checks, we could add a |
348 // special marker as internal field, which will definitely never occur anywhere | 361 // special marker as internal field, which will definitely never occur anywhere |
349 // else. | 362 // else. |
350 bool IsWasmObject(Object* object); | 363 bool IsWasmObject(Object* object); |
351 | 364 |
352 } // namespace wasm | 365 } // namespace wasm |
353 } // namespace internal | 366 } // namespace internal |
354 } // namespace v8 | 367 } // namespace v8 |
355 | 368 |
356 #endif // V8_WASM_MODULE_H_ | 369 #endif // V8_WASM_MODULE_H_ |
OLD | NEW |