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 <memory> | 8 #include <memory> |
9 | 9 |
10 #include "src/api.h" | 10 #include "src/api.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 F(Signatures, 1, "type") \ | 36 F(Signatures, 1, "type") \ |
37 F(ImportTable, 2, "import") \ | 37 F(ImportTable, 2, "import") \ |
38 F(FunctionSignatures, 3, "function") \ | 38 F(FunctionSignatures, 3, "function") \ |
39 F(FunctionTable, 4, "table") \ | 39 F(FunctionTable, 4, "table") \ |
40 F(Memory, 5, "memory") \ | 40 F(Memory, 5, "memory") \ |
41 F(ExportTable, 6, "export") \ | 41 F(ExportTable, 6, "export") \ |
42 F(StartFunction, 7, "start") \ | 42 F(StartFunction, 7, "start") \ |
43 F(FunctionBodies, 8, "code") \ | 43 F(FunctionBodies, 8, "code") \ |
44 F(DataSegments, 9, "data") \ | 44 F(DataSegments, 9, "data") \ |
45 F(Names, 10, "name") \ | 45 F(Names, 10, "name") \ |
46 F(FunctionTablePad, 11, "table_pad") \ | |
47 F(Globals, 0, "global") \ | 46 F(Globals, 0, "global") \ |
48 F(End, 0, "end") | 47 F(End, 0, "end") |
49 | 48 |
50 // Contants for the above section types: {LEB128 length, characters...}. | 49 // Contants for the above section types: {LEB128 length, characters...}. |
51 #define WASM_SECTION_MEMORY 6, 'm', 'e', 'm', 'o', 'r', 'y' | 50 #define WASM_SECTION_MEMORY 6, 'm', 'e', 'm', 'o', 'r', 'y' |
52 #define WASM_SECTION_SIGNATURES 4, 't', 'y', 'p', 'e' | 51 #define WASM_SECTION_SIGNATURES 4, 't', 'y', 'p', 'e' |
53 #define WASM_SECTION_GLOBALS 6, 'g', 'l', 'o', 'b', 'a', 'l' | 52 #define WASM_SECTION_GLOBALS 6, 'g', 'l', 'o', 'b', 'a', 'l' |
54 #define WASM_SECTION_DATA_SEGMENTS 4, 'd', 'a', 't', 'a' | 53 #define WASM_SECTION_DATA_SEGMENTS 4, 'd', 'a', 't', 'a' |
55 #define WASM_SECTION_FUNCTION_TABLE 5, 't', 'a', 'b', 'l', 'e' | 54 #define WASM_SECTION_FUNCTION_TABLE 5, 't', 'a', 'b', 'l', 'e' |
56 #define WASM_SECTION_END 3, 'e', 'n', 'd' | 55 #define WASM_SECTION_END 3, 'e', 'n', 'd' |
57 #define WASM_SECTION_START_FUNCTION 5, 's', 't', 'a', 'r', 't' | 56 #define WASM_SECTION_START_FUNCTION 5, 's', 't', 'a', 'r', 't' |
58 #define WASM_SECTION_IMPORT_TABLE 6, 'i', 'm', 'p', 'o', 'r', 't' | 57 #define WASM_SECTION_IMPORT_TABLE 6, 'i', 'm', 'p', 'o', 'r', 't' |
59 #define WASM_SECTION_EXPORT_TABLE 6, 'e', 'x', 'p', 'o', 'r', 't' | 58 #define WASM_SECTION_EXPORT_TABLE 6, 'e', 'x', 'p', 'o', 'r', 't' |
60 #define WASM_SECTION_FUNCTION_SIGNATURES \ | 59 #define WASM_SECTION_FUNCTION_SIGNATURES \ |
61 8, 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n' | 60 8, 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n' |
62 #define WASM_SECTION_FUNCTION_BODIES 4, 'c', 'o', 'd', 'e' | 61 #define WASM_SECTION_FUNCTION_BODIES 4, 'c', 'o', 'd', 'e' |
63 #define WASM_SECTION_NAMES 4, 'n', 'a', 'm', 'e' | 62 #define WASM_SECTION_NAMES 4, 'n', 'a', 'm', 'e' |
64 #define WASM_SECTION_FUNCTION_TABLE_PAD \ | |
65 9, 't', 'a', 'b', 'l', 'e', '_', 'p', 'a', 'd' | |
66 | 63 |
67 // Constants for the above section headers' size (LEB128 + characters). | 64 // Constants for the above section headers' size (LEB128 + characters). |
68 #define WASM_SECTION_MEMORY_SIZE ((size_t)7) | 65 #define WASM_SECTION_MEMORY_SIZE ((size_t)7) |
69 #define WASM_SECTION_SIGNATURES_SIZE ((size_t)5) | 66 #define WASM_SECTION_SIGNATURES_SIZE ((size_t)5) |
70 #define WASM_SECTION_GLOBALS_SIZE ((size_t)7) | 67 #define WASM_SECTION_GLOBALS_SIZE ((size_t)7) |
71 #define WASM_SECTION_DATA_SEGMENTS_SIZE ((size_t)5) | 68 #define WASM_SECTION_DATA_SEGMENTS_SIZE ((size_t)5) |
72 #define WASM_SECTION_FUNCTION_TABLE_SIZE ((size_t)6) | 69 #define WASM_SECTION_FUNCTION_TABLE_SIZE ((size_t)6) |
73 #define WASM_SECTION_END_SIZE ((size_t)4) | 70 #define WASM_SECTION_END_SIZE ((size_t)4) |
74 #define WASM_SECTION_START_FUNCTION_SIZE ((size_t)6) | 71 #define WASM_SECTION_START_FUNCTION_SIZE ((size_t)6) |
75 #define WASM_SECTION_IMPORT_TABLE_SIZE ((size_t)7) | 72 #define WASM_SECTION_IMPORT_TABLE_SIZE ((size_t)7) |
76 #define WASM_SECTION_EXPORT_TABLE_SIZE ((size_t)7) | 73 #define WASM_SECTION_EXPORT_TABLE_SIZE ((size_t)7) |
77 #define WASM_SECTION_FUNCTION_SIGNATURES_SIZE ((size_t)9) | 74 #define WASM_SECTION_FUNCTION_SIGNATURES_SIZE ((size_t)9) |
78 #define WASM_SECTION_FUNCTION_BODIES_SIZE ((size_t)5) | 75 #define WASM_SECTION_FUNCTION_BODIES_SIZE ((size_t)5) |
79 #define WASM_SECTION_NAMES_SIZE ((size_t)5) | 76 #define WASM_SECTION_NAMES_SIZE ((size_t)5) |
80 #define WASM_SECTION_FUNCTION_TABLE_PAD_SIZE ((size_t)10) | |
81 | 77 |
82 class WasmDebugInfo; | 78 class WasmDebugInfo; |
83 | 79 |
84 struct WasmSection { | 80 struct WasmSection { |
85 enum class Code : uint32_t { | 81 enum class Code : uint32_t { |
86 #define F(enumerator, order, string) enumerator, | 82 #define F(enumerator, order, string) enumerator, |
87 FOR_EACH_WASM_SECTION_TYPE(F) | 83 FOR_EACH_WASM_SECTION_TYPE(F) |
88 #undef F | 84 #undef F |
89 Max | 85 Max |
90 }; | 86 }; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 }; | 142 }; |
147 | 143 |
148 // Static representation of a wasm data segment. | 144 // Static representation of a wasm data segment. |
149 struct WasmDataSegment { | 145 struct WasmDataSegment { |
150 uint32_t dest_addr; // destination memory address of the data. | 146 uint32_t dest_addr; // destination memory address of the data. |
151 uint32_t source_offset; // start offset in the module bytes. | 147 uint32_t source_offset; // start offset in the module bytes. |
152 uint32_t source_size; // end offset in the module bytes. | 148 uint32_t source_size; // end offset in the module bytes. |
153 bool init; // true if loaded upon instantiation. | 149 bool init; // true if loaded upon instantiation. |
154 }; | 150 }; |
155 | 151 |
| 152 // Static representation of a wasm indirect call table. |
| 153 struct WasmIndirectFunctionTable { |
| 154 uint32_t size; // initial table size. |
| 155 uint32_t max_size; // maximum table size. |
| 156 std::vector<uint16_t> values; // function table. |
| 157 }; |
| 158 |
156 enum ModuleOrigin { kWasmOrigin, kAsmJsOrigin }; | 159 enum ModuleOrigin { kWasmOrigin, kAsmJsOrigin }; |
157 | 160 |
158 // Static representation of a module. | 161 // Static representation of a module. |
159 struct WasmModule { | 162 struct WasmModule { |
160 static const uint32_t kPageSize = 0x10000; // Page size, 64kb. | 163 static const uint32_t kPageSize = 0x10000; // Page size, 64kb. |
161 static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb | 164 static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb |
162 static const uint32_t kMaxMemPages = 16384; // Maximum memory size = 1gb | 165 static const uint32_t kMaxMemPages = 16384; // Maximum memory size = 1gb |
163 | 166 |
164 const byte* module_start; // starting address for the module bytes. | 167 const byte* module_start; // starting address for the module bytes. |
165 const byte* module_end; // end address for the module bytes. | 168 const byte* module_end; // end address for the module bytes. |
166 uint32_t min_mem_pages; // minimum size of the memory in 64k pages. | 169 uint32_t min_mem_pages; // minimum size of the memory in 64k pages. |
167 uint32_t max_mem_pages; // maximum size of the memory in 64k pages. | 170 uint32_t max_mem_pages; // maximum size of the memory in 64k pages. |
168 bool mem_export; // true if the memory is exported. | 171 bool mem_export; // true if the memory is exported. |
169 bool mem_external; // true if the memory is external. | 172 bool mem_external; // true if the memory is external. |
170 // TODO(wasm): reconcile start function index being an int with | 173 // TODO(wasm): reconcile start function index being an int with |
171 // the fact that we index on uint32_t, so we may technically not be | 174 // the fact that we index on uint32_t, so we may technically not be |
172 // able to represent some start_function_index -es. | 175 // able to represent some start_function_index -es. |
173 int start_function_index; // start function, if any. | 176 int start_function_index; // start function, if any. |
174 ModuleOrigin origin; // origin of the module | 177 ModuleOrigin origin; // origin of the module |
175 | 178 |
176 std::vector<WasmGlobal> globals; // globals in this module. | 179 std::vector<WasmGlobal> globals; // globals in this module. |
177 uint32_t globals_size; // size of globals table. | 180 uint32_t globals_size; // size of globals table. |
178 uint32_t indirect_table_size; // size of indirect function | |
179 // table (includes padding). | |
180 std::vector<FunctionSig*> signatures; // signatures in this module. | 181 std::vector<FunctionSig*> signatures; // signatures in this module. |
181 std::vector<WasmFunction> functions; // functions in this module. | 182 std::vector<WasmFunction> functions; // functions in this module. |
182 std::vector<WasmDataSegment> data_segments; // data segments in this module. | 183 std::vector<WasmDataSegment> data_segments; // data segments in this module. |
183 std::vector<uint16_t> function_table; // function table. | 184 std::vector<WasmIndirectFunctionTable> function_tables; // function tables. |
184 std::vector<WasmImport> import_table; // import table. | 185 std::vector<WasmImport> import_table; // import table. |
185 std::vector<WasmExport> export_table; // export table. | 186 std::vector<WasmExport> export_table; // export table. |
186 // We store the semaphore here to extend its lifetime. In <libc-2.21, which we | 187 // We store the semaphore here to extend its lifetime. In <libc-2.21, which we |
187 // use on the try bots, semaphore::Wait() can return while some compilation | 188 // use on the try bots, semaphore::Wait() can return while some compilation |
188 // tasks are still executing semaphore::Signal(). If the semaphore is cleaned | 189 // tasks are still executing semaphore::Signal(). If the semaphore is cleaned |
189 // up right after semaphore::Wait() returns, then this can cause an | 190 // up right after semaphore::Wait() returns, then this can cause an |
190 // invalid-semaphore error in the compilation tasks. | 191 // invalid-semaphore error in the compilation tasks. |
191 // TODO(wasm): Move this semaphore back to CompileInParallel when the try bots | 192 // TODO(wasm): Move this semaphore back to CompileInParallel when the try bots |
192 // switch to libc-2.21 or higher. | 193 // switch to libc-2.21 or higher. |
193 std::unique_ptr<base::Semaphore> pending_tasks; | 194 std::unique_ptr<base::Semaphore> pending_tasks; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 | 232 |
232 // Creates a new instantiation of the module in the given isolate. | 233 // Creates a new instantiation of the module in the given isolate. |
233 static MaybeHandle<JSObject> Instantiate(Isolate* isolate, | 234 static MaybeHandle<JSObject> Instantiate(Isolate* isolate, |
234 Handle<FixedArray> compiled_module, | 235 Handle<FixedArray> compiled_module, |
235 Handle<JSReceiver> ffi, | 236 Handle<JSReceiver> ffi, |
236 Handle<JSArrayBuffer> memory); | 237 Handle<JSArrayBuffer> memory); |
237 | 238 |
238 MaybeHandle<FixedArray> CompileFunctions(Isolate* isolate, | 239 MaybeHandle<FixedArray> CompileFunctions(Isolate* isolate, |
239 ErrorThrower* thrower) const; | 240 ErrorThrower* thrower) const; |
240 | 241 |
241 uint32_t FunctionTableSize() const { | |
242 if (indirect_table_size > 0) { | |
243 return indirect_table_size; | |
244 } | |
245 DCHECK_LE(function_table.size(), UINT32_MAX); | |
246 return static_cast<uint32_t>(function_table.size()); | |
247 } | |
248 | |
249 private: | 242 private: |
250 DISALLOW_COPY_AND_ASSIGN(WasmModule); | 243 DISALLOW_COPY_AND_ASSIGN(WasmModule); |
251 }; | 244 }; |
252 | 245 |
253 // An instantiated WASM module, including memory, function table, etc. | 246 // An instantiated WASM module, including memory, function table, etc. |
254 struct WasmModuleInstance { | 247 struct WasmModuleInstance { |
255 const WasmModule* module; // static representation of the module. | 248 const WasmModule* module; // static representation of the module. |
256 // -- Heap allocated -------------------------------------------------------- | 249 // -- Heap allocated -------------------------------------------------------- |
257 Handle<JSObject> js_object; // JavaScript module object. | 250 Handle<JSObject> js_object; // JavaScript module object. |
258 Handle<Context> context; // JavaScript native context. | 251 Handle<Context> context; // JavaScript native context. |
259 Handle<JSArrayBuffer> mem_buffer; // Handle to array buffer of memory. | 252 Handle<JSArrayBuffer> mem_buffer; // Handle to array buffer of memory. |
260 Handle<JSArrayBuffer> globals_buffer; // Handle to array buffer of globals. | 253 Handle<JSArrayBuffer> globals_buffer; // Handle to array buffer of globals. |
261 Handle<FixedArray> function_table; // indirect function table. | 254 std::vector<Handle<FixedArray>> function_tables; // indirect function tables. |
262 std::vector<Handle<Code>> function_code; // code objects for each function. | 255 std::vector<Handle<Code>> function_code; // code objects for each function. |
263 std::vector<Handle<Code>> import_code; // code objects for each import. | 256 std::vector<Handle<Code>> import_code; // code objects for each import. |
264 // -- raw memory ------------------------------------------------------------ | 257 // -- raw memory ------------------------------------------------------------ |
265 byte* mem_start; // start of linear memory. | 258 byte* mem_start; // start of linear memory. |
266 uint32_t mem_size; // size of the linear memory. | 259 uint32_t mem_size; // size of the linear memory. |
267 // -- raw globals ----------------------------------------------------------- | 260 // -- raw globals ----------------------------------------------------------- |
268 byte* globals_start; // start of the globals area. | 261 byte* globals_start; // start of the globals area. |
269 | 262 |
270 explicit WasmModuleInstance(const WasmModule* m) | 263 explicit WasmModuleInstance(const WasmModule* m) |
271 : module(m), | 264 : module(m), |
| 265 function_tables(m->function_tables.size()), |
272 function_code(m->functions.size()), | 266 function_code(m->functions.size()), |
273 import_code(m->import_table.size()), | 267 import_code(m->import_table.size()), |
274 mem_start(nullptr), | 268 mem_start(nullptr), |
275 mem_size(0), | 269 mem_size(0), |
276 globals_start(nullptr) {} | 270 globals_start(nullptr) {} |
277 }; | 271 }; |
278 | 272 |
279 // Interface provided to the decoder/graph builder which contains only | 273 // Interface provided to the decoder/graph builder which contains only |
280 // minimal information about the globals, functions, and function tables. | 274 // minimal information about the globals, functions, and function tables. |
281 struct ModuleEnv { | 275 struct ModuleEnv { |
282 const WasmModule* module; | 276 const WasmModule* module; |
283 WasmModuleInstance* instance; | 277 WasmModuleInstance* instance; |
284 ModuleOrigin origin; | 278 ModuleOrigin origin; |
285 // TODO(mtrofin): remove this once we introduce WASM_DIRECT_CALL | 279 // TODO(mtrofin): remove this once we introduce WASM_DIRECT_CALL |
286 // reloc infos. | 280 // reloc infos. |
287 std::vector<Handle<Code>> placeholders; | 281 std::vector<Handle<Code>> placeholders; |
288 | 282 |
289 bool IsValidGlobal(uint32_t index) { | 283 bool IsValidGlobal(uint32_t index) const { |
290 return module && index < module->globals.size(); | 284 return module && index < module->globals.size(); |
291 } | 285 } |
292 bool IsValidFunction(uint32_t index) const { | 286 bool IsValidFunction(uint32_t index) const { |
293 return module && index < module->functions.size(); | 287 return module && index < module->functions.size(); |
294 } | 288 } |
295 bool IsValidSignature(uint32_t index) { | 289 bool IsValidSignature(uint32_t index) const { |
296 return module && index < module->signatures.size(); | 290 return module && index < module->signatures.size(); |
297 } | 291 } |
298 bool IsValidImport(uint32_t index) { | 292 bool IsValidImport(uint32_t index) const { |
299 return module && index < module->import_table.size(); | 293 return module && index < module->import_table.size(); |
300 } | 294 } |
| 295 bool IsValidTable(uint32_t index) const { |
| 296 return module && index < module->function_tables.size(); |
| 297 } |
301 LocalType GetGlobalType(uint32_t index) { | 298 LocalType GetGlobalType(uint32_t index) { |
302 DCHECK(IsValidGlobal(index)); | 299 DCHECK(IsValidGlobal(index)); |
303 return module->globals[index].type; | 300 return module->globals[index].type; |
304 } | 301 } |
305 FunctionSig* GetFunctionSignature(uint32_t index) { | 302 FunctionSig* GetFunctionSignature(uint32_t index) { |
306 DCHECK(IsValidFunction(index)); | 303 DCHECK(IsValidFunction(index)); |
307 return module->functions[index].sig; | 304 return module->functions[index].sig; |
308 } | 305 } |
309 FunctionSig* GetImportSignature(uint32_t index) { | 306 FunctionSig* GetImportSignature(uint32_t index) { |
310 DCHECK(IsValidImport(index)); | 307 DCHECK(IsValidImport(index)); |
311 return module->import_table[index].sig; | 308 return module->import_table[index].sig; |
312 } | 309 } |
313 FunctionSig* GetSignature(uint32_t index) { | 310 FunctionSig* GetSignature(uint32_t index) { |
314 DCHECK(IsValidSignature(index)); | 311 DCHECK(IsValidSignature(index)); |
315 return module->signatures[index]; | 312 return module->signatures[index]; |
316 } | 313 } |
317 uint32_t FunctionTableSize() const { | 314 const WasmIndirectFunctionTable* GetTable(uint32_t index) const { |
318 return module->FunctionTableSize(); | 315 DCHECK(IsValidTable(index)); |
| 316 return &module->function_tables[index]; |
319 } | 317 } |
320 | 318 |
321 bool asm_js() { return origin == kAsmJsOrigin; } | 319 bool asm_js() { return origin == kAsmJsOrigin; } |
322 | 320 |
323 Handle<Code> GetCodeOrPlaceholder(uint32_t index) const; | 321 Handle<Code> GetCodeOrPlaceholder(uint32_t index) const; |
324 Handle<Code> GetImportCode(uint32_t index); | 322 Handle<Code> GetImportCode(uint32_t index); |
325 | 323 |
326 static compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone, | 324 static compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone, |
327 FunctionSig* sig); | 325 FunctionSig* sig); |
328 static compiler::CallDescriptor* GetI32WasmCallDescriptor( | 326 static compiler::CallDescriptor* GetI32WasmCallDescriptor( |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 // secure. If it turns out that we need more complete checks, we could add a | 378 // secure. If it turns out that we need more complete checks, we could add a |
381 // special marker as internal field, which will definitely never occur anywhere | 379 // special marker as internal field, which will definitely never occur anywhere |
382 // else. | 380 // else. |
383 bool IsWasmObject(Object* object); | 381 bool IsWasmObject(Object* object); |
384 | 382 |
385 // Update memory references of code objects associated with the module | 383 // Update memory references of code objects associated with the module |
386 bool UpdateWasmModuleMemory(Handle<JSObject> object, Address old_start, | 384 bool UpdateWasmModuleMemory(Handle<JSObject> object, Address old_start, |
387 Address new_start, uint32_t old_size, | 385 Address new_start, uint32_t old_size, |
388 uint32_t new_size); | 386 uint32_t new_size); |
389 | 387 |
| 388 // Constructs a single function table as a FixedArray of double size, |
| 389 // populating it with function signature indices and function indices. |
| 390 Handle<FixedArray> BuildFunctionTable(Isolate* isolate, uint32_t index, |
| 391 const WasmModule* module); |
| 392 |
| 393 // Populates a function table by replacing function indices with handles to |
| 394 // the compiled code. |
| 395 void PopulateFunctionTable(Handle<FixedArray> table, uint32_t table_size, |
| 396 const std::vector<Handle<Code>>* code_table); |
| 397 |
390 namespace testing { | 398 namespace testing { |
391 | 399 |
392 // Decode, verify, and run the function labeled "main" in the | 400 // Decode, verify, and run the function labeled "main" in the |
393 // given encoded module. The module should have no imports. | 401 // given encoded module. The module should have no imports. |
394 int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start, | 402 int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start, |
395 const byte* module_end, bool asm_js = false); | 403 const byte* module_end, bool asm_js = false); |
396 | 404 |
397 } // namespace testing | 405 } // namespace testing |
398 } // namespace wasm | 406 } // namespace wasm |
399 } // namespace internal | 407 } // namespace internal |
400 } // namespace v8 | 408 } // namespace v8 |
401 | 409 |
402 #endif // V8_WASM_MODULE_H_ | 410 #endif // V8_WASM_MODULE_H_ |
OLD | NEW |