| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_WASM_MODULE_DECODER_H_ |
| 6 | #define V8_WASM_MODULE_DECODER_H_ |
| 7 | |
| Ben Murdoch | c8c1d9e | 2017-03-08 14:04:23 +0000 | [diff] [blame] | 8 | #include "src/globals.h" |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame^] | 9 | #include "src/wasm/function-body-decoder.h" |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10 | #include "src/wasm/wasm-module.h" |
| Ben Murdoch | c8c1d9e | 2017-03-08 14:04:23 +0000 | [diff] [blame] | 11 | #include "src/wasm/wasm-result.h" |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12 | |
| 13 | namespace v8 { |
| 14 | namespace internal { |
| 15 | namespace wasm { |
| Ben Murdoch | c8c1d9e | 2017-03-08 14:04:23 +0000 | [diff] [blame] | 16 | |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame^] | 17 | const uint32_t kWasmMagic = 0x6d736100; |
| 18 | const uint32_t kWasmVersion = 0x01; |
| 19 | const uint8_t kWasmFunctionTypeForm = 0x60; |
| 20 | const uint8_t kWasmAnyFunctionTypeForm = 0x70; |
| 21 | const uint8_t kResizableMaximumFlag = 1; |
| 22 | |
| 23 | enum WasmSectionCode { |
| 24 | kUnknownSectionCode = 0, // code for unknown sections |
| 25 | kTypeSectionCode = 1, // Function signature declarations |
| 26 | kImportSectionCode = 2, // Import declarations |
| 27 | kFunctionSectionCode = 3, // Function declarations |
| 28 | kTableSectionCode = 4, // Indirect function table and other tables |
| 29 | kMemorySectionCode = 5, // Memory attributes |
| 30 | kGlobalSectionCode = 6, // Global declarations |
| 31 | kExportSectionCode = 7, // Exports |
| 32 | kStartSectionCode = 8, // Start function declaration |
| 33 | kElementSectionCode = 9, // Elements section |
| 34 | kCodeSectionCode = 10, // Function code |
| 35 | kDataSectionCode = 11, // Data segments |
| 36 | kNameSectionCode = 12, // Name section (encoded as a string) |
| 37 | }; |
| 38 | |
| 39 | inline bool IsValidSectionCode(uint8_t byte) { |
| 40 | return kTypeSectionCode <= byte && byte <= kDataSectionCode; |
| 41 | } |
| 42 | |
| 43 | const char* SectionName(WasmSectionCode code); |
| 44 | |
| Ben Murdoch | c8c1d9e | 2017-03-08 14:04:23 +0000 | [diff] [blame] | 45 | typedef Result<const WasmModule*> ModuleResult; |
| 46 | typedef Result<WasmFunction*> FunctionResult; |
| 47 | typedef std::vector<std::pair<int, int>> FunctionOffsets; |
| 48 | typedef Result<FunctionOffsets> FunctionOffsetsResult; |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame^] | 49 | struct AsmJsOffsetEntry { |
| 50 | int byte_offset; |
| 51 | int source_position_call; |
| 52 | int source_position_number_conversion; |
| 53 | }; |
| 54 | typedef std::vector<std::vector<AsmJsOffsetEntry>> AsmJsOffsets; |
| Ben Murdoch | c8c1d9e | 2017-03-08 14:04:23 +0000 | [diff] [blame] | 55 | typedef Result<AsmJsOffsets> AsmJsOffsetsResult; |
| 56 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 57 | // Decodes the bytes of a WASM module between {module_start} and {module_end}. |
| Ben Murdoch | c8c1d9e | 2017-03-08 14:04:23 +0000 | [diff] [blame] | 58 | V8_EXPORT_PRIVATE ModuleResult DecodeWasmModule(Isolate* isolate, |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 59 | const byte* module_start, |
| 60 | const byte* module_end, |
| 61 | bool verify_functions, |
| 62 | ModuleOrigin origin); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 63 | |
| 64 | // Exposed for testing. Decodes a single function signature, allocating it |
| 65 | // in the given zone. Returns {nullptr} upon failure. |
| Ben Murdoch | c8c1d9e | 2017-03-08 14:04:23 +0000 | [diff] [blame] | 66 | V8_EXPORT_PRIVATE FunctionSig* DecodeWasmSignatureForTesting(Zone* zone, |
| 67 | const byte* start, |
| 68 | const byte* end); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 69 | |
| 70 | // Decodes the bytes of a WASM function between |
| 71 | // {function_start} and {function_end}. |
| Ben Murdoch | c8c1d9e | 2017-03-08 14:04:23 +0000 | [diff] [blame] | 72 | V8_EXPORT_PRIVATE FunctionResult DecodeWasmFunction(Isolate* isolate, |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame^] | 73 | Zone* zone, |
| 74 | ModuleBytesEnv* env, |
| Ben Murdoch | c8c1d9e | 2017-03-08 14:04:23 +0000 | [diff] [blame] | 75 | const byte* function_start, |
| 76 | const byte* function_end); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 77 | |
| 78 | // Extracts the function offset table from the wasm module bytes. |
| 79 | // Returns a vector with <offset, length> entries, or failure if the wasm bytes |
| 80 | // are detected as invalid. Note that this validation is not complete. |
| Ben Murdoch | c8c1d9e | 2017-03-08 14:04:23 +0000 | [diff] [blame] | 81 | FunctionOffsetsResult DecodeWasmFunctionOffsets(const byte* module_start, |
| 82 | const byte* module_end); |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 83 | |
| Ben Murdoch | c8c1d9e | 2017-03-08 14:04:23 +0000 | [diff] [blame] | 84 | V8_EXPORT_PRIVATE WasmInitExpr DecodeWasmInitExprForTesting(const byte* start, |
| 85 | const byte* end); |
| 86 | |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame^] | 87 | struct CustomSectionOffset { |
| 88 | uint32_t section_start; |
| 89 | uint32_t name_offset; |
| 90 | uint32_t name_length; |
| 91 | uint32_t payload_offset; |
| 92 | uint32_t payload_length; |
| 93 | uint32_t section_length; |
| 94 | }; |
| 95 | |
| 96 | V8_EXPORT_PRIVATE std::vector<CustomSectionOffset> DecodeCustomSections( |
| 97 | const byte* start, const byte* end); |
| 98 | |
| Ben Murdoch | c8c1d9e | 2017-03-08 14:04:23 +0000 | [diff] [blame] | 99 | // Extracts the mapping from wasm byte offset to asm.js source position per |
| 100 | // function. |
| 101 | // Returns a vector of vectors with <byte_offset, source_position> entries, or |
| 102 | // failure if the wasm bytes are detected as invalid. Note that this validation |
| 103 | // is not complete. |
| 104 | AsmJsOffsetsResult DecodeAsmJsOffsets(const byte* module_start, |
| 105 | const byte* module_end); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 106 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 107 | } // namespace wasm |
| 108 | } // namespace internal |
| 109 | } // namespace v8 |
| 110 | |
| 111 | #endif // V8_WASM_MODULE_DECODER_H_ |