• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "src/globals.h"
9 #include "src/wasm/function-body-decoder.h"
10 #include "src/wasm/wasm-module.h"
11 #include "src/wasm/wasm-result.h"
12 
13 namespace v8 {
14 namespace internal {
15 namespace wasm {
16 
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 
IsValidSectionCode(uint8_t byte)39 inline bool IsValidSectionCode(uint8_t byte) {
40   return kTypeSectionCode <= byte && byte <= kDataSectionCode;
41 }
42 
43 const char* SectionName(WasmSectionCode code);
44 
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;
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;
55 typedef Result<AsmJsOffsets> AsmJsOffsetsResult;
56 
57 // Decodes the bytes of a WASM module between {module_start} and {module_end}.
58 V8_EXPORT_PRIVATE ModuleResult DecodeWasmModule(Isolate* isolate,
59                                                 const byte* module_start,
60                                                 const byte* module_end,
61                                                 bool verify_functions,
62                                                 ModuleOrigin origin);
63 
64 // Exposed for testing. Decodes a single function signature, allocating it
65 // in the given zone. Returns {nullptr} upon failure.
66 V8_EXPORT_PRIVATE FunctionSig* DecodeWasmSignatureForTesting(Zone* zone,
67                                                              const byte* start,
68                                                              const byte* end);
69 
70 // Decodes the bytes of a WASM function between
71 // {function_start} and {function_end}.
72 V8_EXPORT_PRIVATE FunctionResult DecodeWasmFunction(Isolate* isolate,
73                                                     Zone* zone,
74                                                     ModuleBytesEnv* env,
75                                                     const byte* function_start,
76                                                     const byte* function_end);
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.
81 FunctionOffsetsResult DecodeWasmFunctionOffsets(const byte* module_start,
82                                                 const byte* module_end);
83 
84 V8_EXPORT_PRIVATE WasmInitExpr DecodeWasmInitExprForTesting(const byte* start,
85                                                             const byte* end);
86 
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 
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);
106 
107 }  // namespace wasm
108 }  // namespace internal
109 }  // namespace v8
110 
111 #endif  // V8_WASM_MODULE_DECODER_H_
112