Line data Source code
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 :
16 : namespace compiler {
17 : struct ModuleEnv;
18 : }
19 :
20 : namespace wasm {
21 :
22 : const uint8_t kWasmFunctionTypeForm = 0x60;
23 : const uint8_t kWasmAnyFunctionTypeForm = 0x70;
24 : const uint8_t kHasMaximumFlag = 1;
25 : const uint8_t kNoMaximumFlag = 0;
26 :
27 : enum MemoryFlags : uint8_t {
28 : kNoMaximum = 0,
29 : kMaximum = 1,
30 : kSharedNoMaximum = 2,
31 : kSharedAndMaximum = 3
32 : };
33 :
34 : enum SectionCode : int8_t {
35 : kUnknownSectionCode = 0, // code for unknown sections
36 : kTypeSectionCode = 1, // Function signature declarations
37 : kImportSectionCode = 2, // Import declarations
38 : kFunctionSectionCode = 3, // Function declarations
39 : kTableSectionCode = 4, // Indirect function table and other tables
40 : kMemorySectionCode = 5, // Memory attributes
41 : kGlobalSectionCode = 6, // Global declarations
42 : kExportSectionCode = 7, // Exports
43 : kStartSectionCode = 8, // Start function declaration
44 : kElementSectionCode = 9, // Elements section
45 : kCodeSectionCode = 10, // Function code
46 : kDataSectionCode = 11, // Data segments
47 : kNameSectionCode = 12, // Name section (encoded as a string)
48 : kExceptionSectionCode = 13, // Exception section
49 :
50 : // Helper values
51 : kFirstSectionInModule = kTypeSectionCode,
52 : kLastKnownModuleSection = kExceptionSectionCode,
53 : };
54 :
55 : enum NameSectionType : uint8_t { kModule = 0, kFunction = 1, kLocal = 2 };
56 :
57 : inline bool IsValidSectionCode(uint8_t byte) {
58 1448124 : return kTypeSectionCode <= byte && byte <= kLastKnownModuleSection;
59 : }
60 :
61 : const char* SectionName(SectionCode code);
62 :
63 : typedef Result<std::unique_ptr<WasmModule>> ModuleResult;
64 : typedef Result<std::unique_ptr<WasmFunction>> FunctionResult;
65 : typedef std::vector<std::pair<int, int>> FunctionOffsets;
66 : typedef Result<FunctionOffsets> FunctionOffsetsResult;
67 :
68 : struct AsmJsOffsetEntry {
69 : int byte_offset;
70 : int source_position_call;
71 : int source_position_number_conversion;
72 : };
73 : typedef std::vector<std::vector<AsmJsOffsetEntry>> AsmJsOffsets;
74 : typedef Result<AsmJsOffsets> AsmJsOffsetsResult;
75 :
76 : struct LocalName {
77 : int local_index;
78 : WireBytesRef name;
79 : LocalName(int local_index, WireBytesRef name)
80 10 : : local_index(local_index), name(name) {}
81 : };
82 0 : struct LocalNamesPerFunction {
83 : int function_index;
84 : int max_local_index = -1;
85 : std::vector<LocalName> names;
86 : explicit LocalNamesPerFunction(int function_index)
87 5 : : function_index(function_index) {}
88 : };
89 30 : struct LocalNames {
90 : int max_function_index = -1;
91 : std::vector<LocalNamesPerFunction> names;
92 : };
93 :
94 : // Decodes the bytes of a wasm module between {module_start} and {module_end}.
95 : V8_EXPORT_PRIVATE ModuleResult SyncDecodeWasmModule(Isolate* isolate,
96 : const byte* module_start,
97 : const byte* module_end,
98 : bool verify_functions,
99 : ModuleOrigin origin);
100 :
101 : V8_EXPORT_PRIVATE ModuleResult AsyncDecodeWasmModule(
102 : Isolate* isolate, const byte* module_start, const byte* module_end,
103 : bool verify_functions, ModuleOrigin origin,
104 : const std::shared_ptr<Counters> async_counters);
105 :
106 : // Exposed for testing. Decodes a single function signature, allocating it
107 : // in the given zone. Returns {nullptr} upon failure.
108 : V8_EXPORT_PRIVATE FunctionSig* DecodeWasmSignatureForTesting(Zone* zone,
109 : const byte* start,
110 : const byte* end);
111 :
112 : // Decodes the bytes of a wasm function between
113 : // {function_start} and {function_end}.
114 : V8_EXPORT_PRIVATE FunctionResult SyncDecodeWasmFunction(
115 : Isolate* isolate, Zone* zone, const ModuleWireBytes& wire_bytes,
116 : const WasmModule* module, const byte* function_start,
117 : const byte* function_end);
118 :
119 : V8_EXPORT_PRIVATE FunctionResult
120 : AsyncDecodeWasmFunction(Isolate* isolate, Zone* zone, compiler::ModuleEnv* env,
121 : const byte* function_start, const byte* function_end,
122 : const std::shared_ptr<Counters> async_counters);
123 :
124 : V8_EXPORT_PRIVATE WasmInitExpr DecodeWasmInitExprForTesting(const byte* start,
125 : const byte* end);
126 :
127 : struct CustomSectionOffset {
128 : WireBytesRef section;
129 : WireBytesRef name;
130 : WireBytesRef payload;
131 : };
132 :
133 : V8_EXPORT_PRIVATE std::vector<CustomSectionOffset> DecodeCustomSections(
134 : const byte* start, const byte* end);
135 :
136 : // Extracts the mapping from wasm byte offset to asm.js source position per
137 : // function.
138 : // Returns a vector of vectors with <byte_offset, source_position> entries, or
139 : // failure if the wasm bytes are detected as invalid. Note that this validation
140 : // is not complete.
141 : AsmJsOffsetsResult DecodeAsmJsOffsets(const byte* module_start,
142 : const byte* module_end);
143 :
144 : // Decode the local names assignment from the name section.
145 : // Stores the result in the given {LocalNames} structure. The result will be
146 : // empty if no name section is present. On encountering an error in the name
147 : // section, returns all information decoded up to the first error.
148 : void DecodeLocalNames(const byte* module_start, const byte* module_end,
149 : LocalNames* result);
150 :
151 : class ModuleDecoderImpl;
152 :
153 1050 : class ModuleDecoder {
154 : public:
155 : ModuleDecoder();
156 : ~ModuleDecoder();
157 :
158 : void StartDecoding(Isolate* isolate,
159 : ModuleOrigin origin = ModuleOrigin::kWasmOrigin);
160 :
161 : void DecodeModuleHeader(Vector<const uint8_t> bytes, uint32_t offset);
162 :
163 : void DecodeSection(SectionCode section_code, Vector<const uint8_t> bytes,
164 : uint32_t offset, bool verify_functions = true);
165 :
166 : bool CheckFunctionsCount(uint32_t functions_count, uint32_t offset);
167 :
168 : void DecodeFunctionBody(uint32_t index, uint32_t size, uint32_t offset,
169 : bool verify_functions = true);
170 :
171 : ModuleResult FinishDecoding(bool verify_functions = true);
172 :
173 : WasmModule* module() const;
174 :
175 : bool ok();
176 :
177 : private:
178 : std::unique_ptr<ModuleDecoderImpl> impl_;
179 : };
180 :
181 : } // namespace wasm
182 : } // namespace internal
183 : } // namespace v8
184 :
185 : #endif // V8_WASM_MODULE_DECODER_H_
|