Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: src/wasm/wasm-module.h

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

Powered by Google App Engine
This is Rietveld 408576698