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 #include "src/wasm/module-decoder.h" | 5 #include "src/wasm/module-decoder.h" |
6 | 6 |
7 #include "src/base/functional.h" | 7 #include "src/base/functional.h" |
8 #include "src/base/platform/platform.h" | 8 #include "src/base/platform/platform.h" |
9 #include "src/macro-assembler.h" | 9 #include "src/macro-assembler.h" |
10 #include "src/objects.h" | 10 #include "src/objects.h" |
11 #include "src/v8.h" | 11 #include "src/v8.h" |
12 | 12 |
13 #include "src/wasm/decoder.h" | 13 #include "src/wasm/decoder.h" |
14 | 14 |
15 namespace v8 { | 15 namespace v8 { |
16 namespace internal { | 16 namespace internal { |
17 namespace wasm { | 17 namespace wasm { |
18 | 18 |
19 #if DEBUG | 19 #if DEBUG |
20 #define TRACE(...) \ | 20 #define TRACE(...) \ |
21 do { \ | 21 do { \ |
22 if (FLAG_trace_wasm_decoder) PrintF(__VA_ARGS__); \ | 22 if (FLAG_trace_wasm_decoder) PrintF(__VA_ARGS__); \ |
23 } while (false) | 23 } while (false) |
24 #else | 24 #else |
25 #define TRACE(...) | 25 #define TRACE(...) |
26 #endif | 26 #endif |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
| 30 LocalType TypeOf(WasmModule* module, WasmInitExpr& expr) { |
| 31 switch (expr.kind) { |
| 32 case WasmInitExpr::kNone: |
| 33 return kAstStmt; |
| 34 case WasmInitExpr::kGlobalIndex: |
| 35 return expr.val.global_index < module->globals.size() |
| 36 ? module->globals[expr.val.global_index].type |
| 37 : kAstStmt; |
| 38 case WasmInitExpr::kI32Const: |
| 39 return kAstI32; |
| 40 case WasmInitExpr::kI64Const: |
| 41 return kAstI64; |
| 42 case WasmInitExpr::kF32Const: |
| 43 return kAstF32; |
| 44 case WasmInitExpr::kF64Const: |
| 45 return kAstF64; |
| 46 default: |
| 47 UNREACHABLE(); |
| 48 return kAstStmt; |
| 49 } |
| 50 } |
| 51 |
| 52 // An iterator over the sections in a WASM binary module. |
| 53 // Automatically skips all unknown sections. |
| 54 class WasmSectionIterator { |
| 55 public: |
| 56 explicit WasmSectionIterator(Decoder& decoder) |
| 57 : decoder_(decoder), |
| 58 section_code_(kUnknownSectionCode), |
| 59 section_start_(decoder.pc()), |
| 60 section_end_(decoder.pc()) { |
| 61 next(); |
| 62 } |
| 63 |
| 64 inline bool more() const { |
| 65 return section_code_ != kUnknownSectionCode && decoder_.more(); |
| 66 } |
| 67 |
| 68 inline WasmSectionCode section_code() const { return section_code_; } |
| 69 |
| 70 inline const byte* section_start() const { return section_start_; } |
| 71 |
| 72 inline uint32_t section_length() const { |
| 73 return static_cast<uint32_t>(section_end_ - section_start_); |
| 74 } |
| 75 |
| 76 inline const byte* section_end() const { return section_end_; } |
| 77 |
| 78 // Advances to the next section, checking that decoding the current section |
| 79 // stopped at {section_end_}. |
| 80 void advance() { |
| 81 if (decoder_.pc() != section_end_) { |
| 82 const char* msg = decoder_.pc() < section_end_ ? "shorter" : "longer"; |
| 83 decoder_.error(decoder_.pc(), decoder_.pc(), |
| 84 "section was %s than expected size " |
| 85 "(%u bytes expected, %zu decoded)", |
| 86 msg, section_length(), |
| 87 static_cast<size_t>(decoder_.pc() - section_start_)); |
| 88 } |
| 89 next(); |
| 90 } |
| 91 |
| 92 private: |
| 93 Decoder& decoder_; |
| 94 WasmSectionCode section_code_; |
| 95 const byte* section_start_; |
| 96 const byte* section_end_; |
| 97 |
| 98 // Reads the section code/name at the current position and sets up |
| 99 // the internal fields. |
| 100 void next() { |
| 101 while (true) { |
| 102 if (!decoder_.more()) { |
| 103 section_code_ = kUnknownSectionCode; |
| 104 return; |
| 105 } |
| 106 uint8_t section_code = decoder_.consume_u8("section code"); |
| 107 |
| 108 if (section_code == kUnknownSectionCode) { |
| 109 uint32_t string_length = decoder_.consume_u32v("section name length"); |
| 110 const byte* section_name_start = decoder_.pc(); |
| 111 decoder_.consume_bytes(string_length); |
| 112 if (decoder_.failed()) { |
| 113 TRACE("Section name of length %u couldn't be read\n", string_length); |
| 114 section_code_ = kUnknownSectionCode; |
| 115 return; |
| 116 } |
| 117 |
| 118 TRACE(" +%d section name : \"%.*s\"\n", |
| 119 static_cast<int>(section_name_start - decoder_.start()), |
| 120 string_length < 20 ? string_length : 20, section_name_start); |
| 121 |
| 122 if (string_length == kNameStringLength && |
| 123 strncmp(reinterpret_cast<const char*>(section_name_start), |
| 124 kNameString, kNameStringLength) == 0) { |
| 125 section_code = kNameSectionCode; |
| 126 } else { |
| 127 section_code = kUnknownSectionCode; |
| 128 } |
| 129 } else if (!IsValidSectionCode(section_code)) { |
| 130 decoder_.error(decoder_.pc(), decoder_.pc(), |
| 131 "unknown section code #0x%2x", section_code); |
| 132 section_code_ = kUnknownSectionCode; |
| 133 } |
| 134 section_code_ = static_cast<WasmSectionCode>(section_code); |
| 135 |
| 136 // Read and check the section size. |
| 137 uint32_t section_length = decoder_.consume_u32v("section length"); |
| 138 section_start_ = decoder_.pc(); |
| 139 if (decoder_.checkAvailable(section_length)) { |
| 140 // Get the limit of the section within the module. |
| 141 section_end_ = section_start_ + section_length; |
| 142 } else { |
| 143 // The section would extend beyond the end of the module. |
| 144 section_end_ = section_start_; |
| 145 } |
| 146 TRACE("Section: %s\n", SectionName(section_code_)); |
| 147 if (section_code_ == kUnknownSectionCode) { |
| 148 // skip unknown sections. |
| 149 decoder_.consume_bytes(section_length); |
| 150 } else { |
| 151 // known section found. |
| 152 return; |
| 153 } |
| 154 } |
| 155 } |
| 156 }; |
| 157 |
30 // The main logic for decoding the bytes of a module. | 158 // The main logic for decoding the bytes of a module. |
31 class ModuleDecoder : public Decoder { | 159 class ModuleDecoder : public Decoder { |
32 public: | 160 public: |
33 ModuleDecoder(Zone* zone, const byte* module_start, const byte* module_end, | 161 ModuleDecoder(Zone* zone, const byte* module_start, const byte* module_end, |
34 ModuleOrigin origin) | 162 ModuleOrigin origin) |
35 : Decoder(module_start, module_end), module_zone(zone), origin_(origin) { | 163 : Decoder(module_start, module_end), module_zone(zone), origin_(origin) { |
36 result_.start = start_; | 164 result_.start = start_; |
37 if (limit_ < start_) { | 165 if (limit_ < start_) { |
38 error(start_, "end is less than start"); | 166 error(start_, "end is less than start"); |
39 limit_ = start_; | 167 limit_ = start_; |
(...skipping 30 matching lines...) Expand all Loading... |
70 } | 198 } |
71 | 199 |
72 // Decodes an entire module. | 200 // Decodes an entire module. |
73 ModuleResult DecodeModule(WasmModule* module, bool verify_functions = true) { | 201 ModuleResult DecodeModule(WasmModule* module, bool verify_functions = true) { |
74 pc_ = start_; | 202 pc_ = start_; |
75 module->module_start = start_; | 203 module->module_start = start_; |
76 module->module_end = limit_; | 204 module->module_end = limit_; |
77 module->min_mem_pages = 0; | 205 module->min_mem_pages = 0; |
78 module->max_mem_pages = 0; | 206 module->max_mem_pages = 0; |
79 module->mem_export = false; | 207 module->mem_export = false; |
80 module->mem_external = false; | |
81 module->origin = origin_; | 208 module->origin = origin_; |
82 | 209 |
83 const byte* pos = pc_; | 210 const byte* pos = pc_; |
84 int current_order = 0; | |
85 uint32_t magic_word = consume_u32("wasm magic"); | 211 uint32_t magic_word = consume_u32("wasm magic"); |
86 #define BYTES(x) (x & 0xff), (x >> 8) & 0xff, (x >> 16) & 0xff, (x >> 24) & 0xff | 212 #define BYTES(x) (x & 0xff), (x >> 8) & 0xff, (x >> 16) & 0xff, (x >> 24) & 0xff |
87 if (magic_word != kWasmMagic) { | 213 if (magic_word != kWasmMagic) { |
88 error(pos, pos, | 214 error(pos, pos, |
89 "expected magic word %02x %02x %02x %02x, " | 215 "expected magic word %02x %02x %02x %02x, " |
90 "found %02x %02x %02x %02x", | 216 "found %02x %02x %02x %02x", |
91 BYTES(kWasmMagic), BYTES(magic_word)); | 217 BYTES(kWasmMagic), BYTES(magic_word)); |
92 goto done; | |
93 } | 218 } |
94 | 219 |
95 pos = pc_; | 220 pos = pc_; |
96 { | 221 { |
97 uint32_t magic_version = consume_u32("wasm version"); | 222 uint32_t magic_version = consume_u32("wasm version"); |
98 if (magic_version != kWasmVersion) { | 223 if (magic_version != kWasmVersion) { |
99 error(pos, pos, | 224 error(pos, pos, |
100 "expected version %02x %02x %02x %02x, " | 225 "expected version %02x %02x %02x %02x, " |
101 "found %02x %02x %02x %02x", | 226 "found %02x %02x %02x %02x", |
102 BYTES(kWasmVersion), BYTES(magic_version)); | 227 BYTES(kWasmVersion), BYTES(magic_version)); |
103 goto done; | 228 } |
104 } | 229 } |
105 } | 230 |
106 | 231 WasmSectionIterator section_iter(*this); |
107 // Decode the module sections. | 232 |
108 while (pc_ < limit_) { | 233 // ===== Type section ==================================================== |
109 TRACE("DecodeSection\n"); | 234 if (section_iter.section_code() == kTypeSectionCode) { |
110 pos = pc_; | 235 uint32_t signatures_count = consume_u32v("signatures count"); |
111 | 236 module->signatures.reserve(SafeReserve(signatures_count)); |
112 // Read the section name. | 237 for (uint32_t i = 0; ok() && i < signatures_count; ++i) { |
113 uint32_t string_length = consume_u32v("section name length"); | 238 TRACE("DecodeSignature[%d] module+%d\n", i, |
114 const byte* section_name_start = pc_; | 239 static_cast<int>(pc_ - start_)); |
115 consume_bytes(string_length); | 240 FunctionSig* s = consume_sig(); |
116 if (failed()) { | 241 module->signatures.push_back(s); |
117 TRACE("Section name of length %u couldn't be read\n", string_length); | 242 } |
118 break; | 243 section_iter.advance(); |
119 } | 244 } |
120 | 245 |
121 TRACE(" +%d section name : \"%.*s\"\n", | 246 // ===== Import section ================================================== |
122 static_cast<int>(section_name_start - start_), | 247 if (section_iter.section_code() == kImportSectionCode) { |
123 string_length < 20 ? string_length : 20, section_name_start); | 248 uint32_t import_table_count = consume_u32v("import table count"); |
124 | 249 module->import_table.reserve(SafeReserve(import_table_count)); |
125 WasmSection::Code section = | 250 for (uint32_t i = 0; ok() && i < import_table_count; ++i) { |
126 WasmSection::lookup(section_name_start, string_length); | 251 TRACE("DecodeImportTable[%d] module+%d\n", i, |
127 | 252 static_cast<int>(pc_ - start_)); |
128 // Read and check the section size. | 253 |
129 uint32_t section_length = consume_u32v("section length"); | 254 module->import_table.push_back({ |
130 if (!checkAvailable(section_length)) { | 255 0, // module_name_length |
131 // The section would extend beyond the end of the module. | 256 0, // module_name_offset |
132 break; | 257 0, // field_name_offset |
133 } | 258 0, // field_name_length |
134 const byte* section_start = pc_; | 259 kExternalFunction, // kind |
135 const byte* expected_section_end = pc_ + section_length; | 260 0 // index |
136 | 261 }); |
137 current_order = CheckSectionOrder(current_order, section); | 262 WasmImport* import = &module->import_table.back(); |
138 | 263 const byte* pos = pc_; |
139 switch (section) { | 264 import->module_name_offset = |
140 case WasmSection::Code::End: | 265 consume_string(&import->module_name_length, true); |
141 // Terminate section decoding. | 266 if (import->module_name_length == 0) { |
142 limit_ = pc_; | 267 error(pos, "import module name cannot be NULL"); |
143 break; | 268 } |
144 case WasmSection::Code::Memory: { | 269 import->field_name_offset = |
145 module->min_mem_pages = consume_u32v("min memory"); | 270 consume_string(&import->field_name_length, true); |
146 module->max_mem_pages = consume_u32v("max memory"); | 271 |
147 module->mem_export = consume_u8("export memory") != 0; | 272 import->kind = static_cast<WasmExternalKind>(consume_u8("import kind")); |
148 break; | 273 switch (import->kind) { |
149 } | 274 case kExternalFunction: { |
150 case WasmSection::Code::Signatures: { | 275 // ===== Imported function ======================================= |
151 uint32_t signatures_count = consume_u32v("signatures count"); | 276 import->index = static_cast<uint32_t>(module->functions.size()); |
152 module->signatures.reserve(SafeReserve(signatures_count)); | 277 module->num_imported_functions++; |
153 // Decode signatures. | 278 module->functions.push_back({nullptr, // sig |
154 for (uint32_t i = 0; i < signatures_count; ++i) { | 279 import->index, // func_index |
155 if (failed()) break; | 280 0, // sig_index |
156 TRACE("DecodeSignature[%d] module+%d\n", i, | 281 0, // name_offset |
157 static_cast<int>(pc_ - start_)); | 282 0, // name_length |
158 FunctionSig* s = consume_sig(); | 283 0, // code_start_offset |
159 module->signatures.push_back(s); | 284 0, // code_end_offset |
160 } | 285 true, // imported |
161 break; | 286 false}); // exported |
162 } | |
163 case WasmSection::Code::FunctionSignatures: { | |
164 uint32_t functions_count = consume_u32v("functions count"); | |
165 module->functions.reserve(SafeReserve(functions_count)); | |
166 for (uint32_t i = 0; i < functions_count; ++i) { | |
167 module->functions.push_back({nullptr, // sig | |
168 i, // func_index | |
169 0, // sig_index | |
170 0, // name_offset | |
171 0, // name_length | |
172 0, // code_start_offset | |
173 0}); // code_end_offset | |
174 WasmFunction* function = &module->functions.back(); | 287 WasmFunction* function = &module->functions.back(); |
175 function->sig_index = consume_sig_index(module, &function->sig); | 288 function->sig_index = consume_sig_index(module, &function->sig); |
176 } | 289 break; |
177 break; | 290 } |
178 } | 291 case kExternalTable: { |
179 case WasmSection::Code::FunctionBodies: { | 292 // ===== Imported table ========================================== |
180 const byte* pos = pc_; | 293 import->index = |
181 uint32_t functions_count = consume_u32v("functions count"); | 294 static_cast<uint32_t>(module->function_tables.size()); |
182 if (functions_count != module->functions.size()) { | 295 module->function_tables.push_back( |
183 error(pos, pos, "function body count %u mismatch (%u expected)", | 296 {0, 0, std::vector<int32_t>(), true, false}); |
184 functions_count, | 297 expect_u8("element type", 0x20); |
185 static_cast<uint32_t>(module->functions.size())); | 298 WasmIndirectFunctionTable* table = &module->function_tables.back(); |
186 break; | 299 consume_resizable_limits("element count", "elements", kMaxUInt32, |
187 } | 300 &table->size, &table->max_size); |
188 for (uint32_t i = 0; i < functions_count; ++i) { | 301 break; |
189 WasmFunction* function = &module->functions[i]; | 302 } |
190 uint32_t size = consume_u32v("body size"); | 303 case kExternalMemory: { |
191 function->code_start_offset = pc_offset(); | 304 // ===== Imported memory ========================================= |
192 function->code_end_offset = pc_offset() + size; | 305 // import->index = |
193 | 306 // static_cast<uint32_t>(module->memories.size()); |
194 TRACE(" +%d %-20s: (%d bytes)\n", pc_offset(), "function body", | 307 // TODO(titzer): imported memories |
195 size); | 308 break; |
196 pc_ += size; | 309 } |
197 if (pc_ > limit_) { | 310 case kExternalGlobal: { |
198 error(pc_, "function body extends beyond end of file"); | 311 // ===== Imported global ========================================= |
199 } | 312 import->index = static_cast<uint32_t>(module->globals.size()); |
200 } | 313 module->globals.push_back( |
201 break; | 314 {kAstStmt, false, NO_INIT, 0, true, false}); |
202 } | |
203 case WasmSection::Code::Names: { | |
204 const byte* pos = pc_; | |
205 uint32_t functions_count = consume_u32v("functions count"); | |
206 if (functions_count != module->functions.size()) { | |
207 error(pos, pos, "function name count %u mismatch (%u expected)", | |
208 functions_count, | |
209 static_cast<uint32_t>(module->functions.size())); | |
210 break; | |
211 } | |
212 | |
213 for (uint32_t i = 0; i < functions_count; ++i) { | |
214 WasmFunction* function = &module->functions[i]; | |
215 function->name_offset = | |
216 consume_string(&function->name_length, false); | |
217 | |
218 uint32_t local_names_count = consume_u32v("local names count"); | |
219 for (uint32_t j = 0; j < local_names_count; j++) { | |
220 uint32_t unused = 0; | |
221 uint32_t offset = consume_string(&unused, false); | |
222 USE(unused); | |
223 USE(offset); | |
224 } | |
225 } | |
226 break; | |
227 } | |
228 case WasmSection::Code::Globals: { | |
229 uint32_t globals_count = consume_u32v("globals count"); | |
230 module->globals.reserve(SafeReserve(globals_count)); | |
231 // Decode globals. | |
232 for (uint32_t i = 0; i < globals_count; ++i) { | |
233 if (failed()) break; | |
234 TRACE("DecodeGlobal[%d] module+%d\n", i, | |
235 static_cast<int>(pc_ - start_)); | |
236 // Add an uninitialized global and pass a pointer to it. | |
237 module->globals.push_back({0, 0, kAstStmt, 0, false}); | |
238 WasmGlobal* global = &module->globals.back(); | 315 WasmGlobal* global = &module->globals.back(); |
239 DecodeGlobalInModule(global); | 316 global->type = consume_value_type(); |
240 } | 317 global->mutability = consume_u8("mutability") != 0; |
241 break; | 318 break; |
242 } | 319 } |
243 case WasmSection::Code::DataSegments: { | 320 default: |
244 uint32_t data_segments_count = consume_u32v("data segments count"); | 321 error(pos, pos, "unknown import kind 0x%02x", import->kind); |
245 module->data_segments.reserve(SafeReserve(data_segments_count)); | 322 break; |
246 // Decode data segments. | 323 } |
247 for (uint32_t i = 0; i < data_segments_count; ++i) { | 324 } |
248 if (failed()) break; | 325 section_iter.advance(); |
249 TRACE("DecodeDataSegment[%d] module+%d\n", i, | 326 } |
250 static_cast<int>(pc_ - start_)); | 327 |
251 module->data_segments.push_back({0, // dest_addr | 328 // ===== Function section ================================================ |
252 0, // source_offset | 329 if (section_iter.section_code() == kFunctionSectionCode) { |
253 0, // source_size | 330 uint32_t functions_count = consume_u32v("functions count"); |
254 false}); // init | 331 module->functions.reserve(SafeReserve(functions_count)); |
255 WasmDataSegment* segment = &module->data_segments.back(); | 332 module->num_declared_functions = functions_count; |
256 DecodeDataSegmentInModule(module, segment); | 333 for (uint32_t i = 0; ok() && i < functions_count; ++i) { |
257 } | 334 uint32_t func_index = static_cast<uint32_t>(module->functions.size()); |
258 break; | 335 module->functions.push_back({nullptr, // sig |
259 } | 336 func_index, // func_index |
260 case WasmSection::Code::FunctionTable: { | 337 0, // sig_index |
261 // An indirect function table requires functions first. | 338 0, // name_offset |
262 CheckForFunctions(module, section); | 339 0, // name_length |
263 // Assume only one table for now. | 340 0, // code_start_offset |
264 static const uint32_t kSupportedTableCount = 1; | 341 0, // code_end_offset |
265 module->function_tables.reserve(SafeReserve(kSupportedTableCount)); | 342 false, // imported |
266 // Decode function table. | 343 false}); // exported |
267 for (uint32_t i = 0; i < kSupportedTableCount; ++i) { | 344 WasmFunction* function = &module->functions.back(); |
268 if (failed()) break; | 345 function->sig_index = consume_sig_index(module, &function->sig); |
269 TRACE("DecodeFunctionTable[%d] module+%d\n", i, | 346 } |
270 static_cast<int>(pc_ - start_)); | 347 section_iter.advance(); |
271 module->function_tables.push_back({0, 0, std::vector<uint16_t>()}); | 348 } |
272 DecodeFunctionTableInModule(module, &module->function_tables[i]); | 349 |
273 } | 350 // ===== Table section =================================================== |
274 break; | 351 if (section_iter.section_code() == kTableSectionCode) { |
275 } | 352 const byte* pos = pc_; |
276 case WasmSection::Code::StartFunction: { | 353 uint32_t table_count = consume_u32v("table count"); |
277 // Declares a start function for a module. | 354 // Require at most one table for now. |
278 CheckForFunctions(module, section); | 355 if (table_count > 1) { |
279 if (module->start_function_index >= 0) { | 356 error(pos, pos, "invalid table count %d, maximum 1", table_count); |
280 error("start function already declared"); | 357 } |
281 break; | 358 |
282 } | 359 for (uint32_t i = 0; ok() && i < table_count; i++) { |
283 WasmFunction* func; | 360 module->function_tables.push_back( |
284 const byte* pos = pc_; | 361 {0, 0, std::vector<int32_t>(), false, false}); |
285 module->start_function_index = consume_func_index(module, &func); | 362 WasmIndirectFunctionTable* table = &module->function_tables.back(); |
286 if (func && func->sig->parameter_count() > 0) { | 363 expect_u8("table type", kWasmAnyFunctionTypeForm); |
287 error(pos, "invalid start function: non-zero parameter count"); | 364 consume_resizable_limits("table elements", "elements", kMaxUInt32, |
288 break; | 365 &table->size, &table->max_size); |
289 } | 366 } |
290 break; | 367 section_iter.advance(); |
291 } | 368 } |
292 case WasmSection::Code::ImportTable: { | 369 |
293 uint32_t import_table_count = consume_u32v("import table count"); | 370 // ===== Memory section ================================================== |
294 module->import_table.reserve(SafeReserve(import_table_count)); | 371 if (section_iter.section_code() == kMemorySectionCode) { |
295 // Decode import table. | 372 const byte* pos = pc_; |
296 for (uint32_t i = 0; i < import_table_count; ++i) { | 373 uint32_t memory_count = consume_u32v("memory count"); |
297 if (failed()) break; | 374 // Require at most one memory for now. |
298 TRACE("DecodeImportTable[%d] module+%d\n", i, | 375 if (memory_count > 1) { |
299 static_cast<int>(pc_ - start_)); | 376 error(pos, pos, "invalid memory count %d, maximum 1", memory_count); |
300 | 377 } |
301 module->import_table.push_back({nullptr, // sig | 378 |
302 0, // sig_index | 379 for (uint32_t i = 0; ok() && i < memory_count; i++) { |
303 0, // module_name_offset | 380 consume_resizable_limits("memory", "pages", 65536, |
304 0, // module_name_length | 381 &module->min_mem_pages, |
305 0, // function_name_offset | 382 &module->max_mem_pages); |
306 0}); // function_name_length | 383 } |
307 WasmImport* import = &module->import_table.back(); | 384 section_iter.advance(); |
308 | 385 } |
309 import->sig_index = consume_sig_index(module, &import->sig); | 386 |
310 const byte* pos = pc_; | 387 // ===== Global section ================================================== |
311 import->module_name_offset = | 388 if (section_iter.section_code() == kGlobalSectionCode) { |
312 consume_string(&import->module_name_length, true); | 389 uint32_t globals_count = consume_u32v("globals count"); |
313 if (import->module_name_length == 0) { | 390 module->globals.reserve(SafeReserve(globals_count)); |
314 error(pos, "import module name cannot be NULL"); | 391 for (uint32_t i = 0; ok() && i < globals_count; ++i) { |
315 } | 392 TRACE("DecodeGlobal[%d] module+%d\n", i, |
316 import->function_name_offset = | 393 static_cast<int>(pc_ - start_)); |
317 consume_string(&import->function_name_length, true); | 394 // Add an uninitialized global and pass a pointer to it. |
318 } | 395 module->globals.push_back({kAstStmt, false, NO_INIT, 0, false, false}); |
319 break; | 396 WasmGlobal* global = &module->globals.back(); |
320 } | 397 DecodeGlobalInModule(module, i, global); |
321 case WasmSection::Code::ExportTable: { | 398 } |
322 // Declares an export table. | 399 section_iter.advance(); |
323 CheckForFunctions(module, section); | 400 } |
324 uint32_t export_table_count = consume_u32v("export table count"); | 401 |
325 module->export_table.reserve(SafeReserve(export_table_count)); | 402 // ===== Export section ================================================== |
326 // Decode export table. | 403 if (section_iter.section_code() == kExportSectionCode) { |
327 for (uint32_t i = 0; i < export_table_count; ++i) { | 404 uint32_t export_table_count = consume_u32v("export table count"); |
328 if (failed()) break; | 405 module->export_table.reserve(SafeReserve(export_table_count)); |
329 TRACE("DecodeExportTable[%d] module+%d\n", i, | 406 for (uint32_t i = 0; ok() && i < export_table_count; ++i) { |
330 static_cast<int>(pc_ - start_)); | 407 TRACE("DecodeExportTable[%d] module+%d\n", i, |
331 | 408 static_cast<int>(pc_ - start_)); |
332 module->export_table.push_back({0, // func_index | 409 |
333 0, // name_offset | 410 module->export_table.push_back({ |
334 0}); // name_length | 411 0, // name_length |
335 WasmExport* exp = &module->export_table.back(); | 412 0, // name_offset |
336 | 413 kExternalFunction, // kind |
337 WasmFunction* func; | 414 0 // index |
338 exp->func_index = consume_func_index(module, &func); | 415 }); |
339 exp->name_offset = consume_string(&exp->name_length, true); | 416 WasmExport* exp = &module->export_table.back(); |
340 } | 417 |
341 // Check for duplicate exports. | 418 exp->name_offset = consume_string(&exp->name_length, true); |
342 if (ok() && module->export_table.size() > 1) { | 419 const byte* pos = pc(); |
343 std::vector<WasmExport> sorted_exports(module->export_table); | 420 exp->kind = static_cast<WasmExternalKind>(consume_u8("export kind")); |
344 const byte* base = start_; | 421 switch (exp->kind) { |
345 auto cmp_less = [base](const WasmExport& a, const WasmExport& b) { | 422 case kExternalFunction: { |
346 // Return true if a < b. | 423 WasmFunction* func = nullptr; |
347 uint32_t len = a.name_length; | 424 exp->index = consume_func_index(module, &func); |
348 if (len != b.name_length) return len < b.name_length; | 425 module->num_exported_functions++; |
349 return memcmp(base + a.name_offset, base + b.name_offset, len) < | 426 if (func) func->exported = true; |
350 0; | 427 break; |
351 }; | 428 } |
352 std::stable_sort(sorted_exports.begin(), sorted_exports.end(), | 429 case kExternalTable: { |
353 cmp_less); | 430 WasmIndirectFunctionTable* table = nullptr; |
354 auto it = sorted_exports.begin(); | 431 exp->index = consume_table_index(module, &table); |
355 WasmExport* last = &*it++; | 432 if (table) table->exported = true; |
356 for (auto end = sorted_exports.end(); it != end; last = &*it++) { | 433 break; |
357 DCHECK(!cmp_less(*it, *last)); // Vector must be sorted. | 434 } |
358 if (!cmp_less(*last, *it)) { | 435 case kExternalMemory: { |
359 const byte* pc = start_ + it->name_offset; | 436 uint32_t index = consume_u32v("memory index"); |
360 error(pc, pc, | 437 if (index != 0) error("invalid memory index != 0"); |
361 "Duplicate export name '%.*s' for functions %d and %d", | 438 module->mem_export = true; |
362 it->name_length, pc, last->func_index, it->func_index); | 439 break; |
363 break; | 440 } |
364 } | 441 case kExternalGlobal: { |
365 } | 442 WasmGlobal* global = nullptr; |
366 } | 443 exp->index = consume_global_index(module, &global); |
367 break; | 444 if (global) global->exported = true; |
368 } | 445 break; |
369 case WasmSection::Code::Max: | 446 } |
370 // Skip unknown sections. | 447 default: |
371 TRACE("Unknown section: '"); | 448 error(pos, pos, "invalid export kind 0x%02x", exp->kind); |
372 for (uint32_t i = 0; i != string_length; ++i) { | 449 break; |
373 TRACE("%c", *(section_name_start + i)); | 450 } |
374 } | 451 } |
375 TRACE("'\n"); | 452 // Check for duplicate exports. |
376 consume_bytes(section_length); | 453 if (ok() && module->export_table.size() > 1) { |
377 break; | 454 std::vector<WasmExport> sorted_exports(module->export_table); |
378 } | 455 const byte* base = start_; |
379 | 456 auto cmp_less = [base](const WasmExport& a, const WasmExport& b) { |
380 if (pc_ != expected_section_end) { | 457 // Return true if a < b. |
381 const char* diff = pc_ < expected_section_end ? "shorter" : "longer"; | 458 uint32_t len = a.name_length; |
382 size_t expected_length = static_cast<size_t>(section_length); | 459 if (len != b.name_length) return len < b.name_length; |
383 size_t actual_length = static_cast<size_t>(pc_ - section_start); | 460 return memcmp(base + a.name_offset, base + b.name_offset, len) < 0; |
384 error(pc_, pc_, | 461 }; |
385 "section \"%s\" %s (%zu bytes) than specified (%zu bytes)", | 462 std::stable_sort(sorted_exports.begin(), sorted_exports.end(), |
386 WasmSection::getName(section), diff, actual_length, | 463 cmp_less); |
387 expected_length); | 464 auto it = sorted_exports.begin(); |
388 break; | 465 WasmExport* last = &*it++; |
389 } | 466 for (auto end = sorted_exports.end(); it != end; last = &*it++) { |
390 } | 467 DCHECK(!cmp_less(*it, *last)); // Vector must be sorted. |
391 | 468 if (!cmp_less(*last, *it)) { |
392 done: | 469 const byte* pc = start_ + it->name_offset; |
393 if (ok()) CalculateGlobalsOffsets(module); | 470 error(pc, pc, |
| 471 "Duplicate export name '%.*s' for functions %d and %d", |
| 472 it->name_length, pc, last->index, it->index); |
| 473 break; |
| 474 } |
| 475 } |
| 476 } |
| 477 section_iter.advance(); |
| 478 } |
| 479 |
| 480 // ===== Start section =================================================== |
| 481 if (section_iter.section_code() == kStartSectionCode) { |
| 482 if (module->start_function_index >= 0) { |
| 483 error("start function already declared"); |
| 484 } |
| 485 WasmFunction* func; |
| 486 const byte* pos = pc_; |
| 487 module->start_function_index = consume_func_index(module, &func); |
| 488 if (func && func->sig->parameter_count() > 0) { |
| 489 error(pos, "invalid start function: non-zero parameter count"); |
| 490 } |
| 491 section_iter.advance(); |
| 492 } |
| 493 |
| 494 // ===== Elements section ================================================ |
| 495 if (section_iter.section_code() == kElementSectionCode) { |
| 496 uint32_t element_count = consume_u32v("element count"); |
| 497 for (uint32_t i = 0; ok() && i < element_count; ++i) { |
| 498 uint32_t table_index = consume_u32v("table index"); |
| 499 if (table_index != 0) error("illegal table index != 0"); |
| 500 WasmInitExpr offset = consume_init_expr(module, kAstI32); |
| 501 uint32_t num_elem = consume_u32v("number of elements"); |
| 502 std::vector<uint32_t> vector; |
| 503 module->table_inits.push_back({table_index, offset, vector}); |
| 504 WasmTableInit* init = &module->table_inits.back(); |
| 505 init->entries.reserve(SafeReserve(num_elem)); |
| 506 for (uint32_t j = 0; ok() && j < num_elem; j++) { |
| 507 WasmFunction* func = nullptr; |
| 508 init->entries.push_back(consume_func_index(module, &func)); |
| 509 } |
| 510 } |
| 511 |
| 512 section_iter.advance(); |
| 513 } |
| 514 |
| 515 // ===== Code section ==================================================== |
| 516 if (section_iter.section_code() == kCodeSectionCode) { |
| 517 const byte* pos = pc_; |
| 518 uint32_t functions_count = consume_u32v("functions count"); |
| 519 if (functions_count != module->num_declared_functions) { |
| 520 error(pos, pos, "function body count %u mismatch (%u expected)", |
| 521 functions_count, module->num_declared_functions); |
| 522 } |
| 523 for (uint32_t i = 0; ok() && i < functions_count; ++i) { |
| 524 WasmFunction* function = |
| 525 &module->functions[i + module->num_imported_functions]; |
| 526 uint32_t size = consume_u32v("body size"); |
| 527 function->code_start_offset = pc_offset(); |
| 528 function->code_end_offset = pc_offset() + size; |
| 529 |
| 530 TRACE(" +%d %-20s: (%d bytes)\n", pc_offset(), "function body", size); |
| 531 pc_ += size; |
| 532 if (pc_ > limit_) { |
| 533 error(pc_, "function body extends beyond end of file"); |
| 534 } |
| 535 } |
| 536 section_iter.advance(); |
| 537 } |
| 538 |
| 539 // ===== Data section ==================================================== |
| 540 if (section_iter.section_code() == kDataSectionCode) { |
| 541 uint32_t data_segments_count = consume_u32v("data segments count"); |
| 542 module->data_segments.reserve(SafeReserve(data_segments_count)); |
| 543 for (uint32_t i = 0; ok() && i < data_segments_count; ++i) { |
| 544 TRACE("DecodeDataSegment[%d] module+%d\n", i, |
| 545 static_cast<int>(pc_ - start_)); |
| 546 module->data_segments.push_back({ |
| 547 NO_INIT, // dest_addr |
| 548 0, // source_offset |
| 549 0 // source_size |
| 550 }); |
| 551 WasmDataSegment* segment = &module->data_segments.back(); |
| 552 DecodeDataSegmentInModule(module, segment); |
| 553 } |
| 554 section_iter.advance(); |
| 555 } |
| 556 |
| 557 // ===== Name section ==================================================== |
| 558 if (section_iter.section_code() == kNameSectionCode) { |
| 559 const byte* pos = pc_; |
| 560 uint32_t functions_count = consume_u32v("functions count"); |
| 561 if (functions_count != module->num_declared_functions) { |
| 562 error(pos, pos, "function name count %u mismatch (%u expected)", |
| 563 functions_count, module->num_declared_functions); |
| 564 } |
| 565 |
| 566 for (uint32_t i = 0; ok() && i < functions_count; ++i) { |
| 567 WasmFunction* function = |
| 568 &module->functions[i + module->num_imported_functions]; |
| 569 function->name_offset = consume_string(&function->name_length, false); |
| 570 |
| 571 uint32_t local_names_count = consume_u32v("local names count"); |
| 572 for (uint32_t j = 0; j < local_names_count; j++) { |
| 573 uint32_t unused = 0; |
| 574 uint32_t offset = consume_string(&unused, false); |
| 575 USE(unused); |
| 576 USE(offset); |
| 577 } |
| 578 } |
| 579 section_iter.advance(); |
| 580 } |
| 581 |
| 582 // ===== Remaining sections ============================================== |
| 583 if (section_iter.more() && ok()) { |
| 584 error(pc(), pc(), "unexpected section: %s", |
| 585 SectionName(section_iter.section_code())); |
| 586 } |
| 587 |
| 588 if (ok()) { |
| 589 CalculateGlobalOffsets(module); |
| 590 PreinitializeIndirectFunctionTables(module); |
| 591 } |
394 const WasmModule* finished_module = module; | 592 const WasmModule* finished_module = module; |
395 ModuleResult result = toResult(finished_module); | 593 ModuleResult result = toResult(finished_module); |
396 if (FLAG_dump_wasm_module) { | 594 if (FLAG_dump_wasm_module) DumpModule(module, result); |
397 DumpModule(module, result); | |
398 } | |
399 return result; | 595 return result; |
400 } | 596 } |
401 | 597 |
402 uint32_t SafeReserve(uint32_t count) { | 598 uint32_t SafeReserve(uint32_t count) { |
403 // Avoid OOM by only reserving up to a certain size. | 599 // Avoid OOM by only reserving up to a certain size. |
404 const uint32_t kMaxReserve = 20000; | 600 const uint32_t kMaxReserve = 20000; |
405 return count < kMaxReserve ? count : kMaxReserve; | 601 return count < kMaxReserve ? count : kMaxReserve; |
406 } | 602 } |
407 | 603 |
408 void CheckForFunctions(WasmModule* module, WasmSection::Code section) { | |
409 if (module->functions.size() == 0) { | |
410 error(pc_ - 1, nullptr, "functions must appear before section %s", | |
411 WasmSection::getName(section)); | |
412 } | |
413 } | |
414 | |
415 int CheckSectionOrder(int current_order, WasmSection::Code section) { | |
416 int next_order = WasmSection::getOrder(section); | |
417 if (next_order == 0) return current_order; | |
418 if (next_order == current_order) { | |
419 error(pc_, pc_, "section \"%s\" already defined", | |
420 WasmSection::getName(section)); | |
421 } | |
422 if (next_order < current_order) { | |
423 error(pc_, pc_, "section \"%s\" out of order", | |
424 WasmSection::getName(section)); | |
425 } | |
426 return next_order; | |
427 } | |
428 | |
429 // Decodes a single anonymous function starting at {start_}. | 604 // Decodes a single anonymous function starting at {start_}. |
430 FunctionResult DecodeSingleFunction(ModuleEnv* module_env, | 605 FunctionResult DecodeSingleFunction(ModuleEnv* module_env, |
431 WasmFunction* function) { | 606 WasmFunction* function) { |
432 pc_ = start_; | 607 pc_ = start_; |
433 function->sig = consume_sig(); // read signature | 608 function->sig = consume_sig(); // read signature |
434 function->name_offset = 0; // ---- name | 609 function->name_offset = 0; // ---- name |
435 function->name_length = 0; // ---- name length | 610 function->name_length = 0; // ---- name length |
436 function->code_start_offset = off(pc_); // ---- code start | 611 function->code_start_offset = off(pc_); // ---- code start |
437 function->code_end_offset = off(limit_); // ---- code end | 612 function->code_end_offset = off(limit_); // ---- code end |
438 | 613 |
439 if (ok()) VerifyFunctionBody(0, module_env, function); | 614 if (ok()) VerifyFunctionBody(0, module_env, function); |
440 | 615 |
441 FunctionResult result; | 616 FunctionResult result; |
442 result.MoveFrom(result_); // Copy error code and location. | 617 result.MoveFrom(result_); // Copy error code and location. |
443 result.val = function; | 618 result.val = function; |
444 return result; | 619 return result; |
445 } | 620 } |
446 | 621 |
447 // Decodes a single function signature at {start}. | 622 // Decodes a single function signature at {start}. |
448 FunctionSig* DecodeFunctionSignature(const byte* start) { | 623 FunctionSig* DecodeFunctionSignature(const byte* start) { |
449 pc_ = start; | 624 pc_ = start; |
450 FunctionSig* result = consume_sig(); | 625 FunctionSig* result = consume_sig(); |
451 return ok() ? result : nullptr; | 626 return ok() ? result : nullptr; |
452 } | 627 } |
453 | 628 |
| 629 WasmInitExpr DecodeInitExpr(const byte* start) { |
| 630 pc_ = start; |
| 631 return consume_init_expr(nullptr, kAstStmt); |
| 632 } |
| 633 |
454 private: | 634 private: |
455 Zone* module_zone; | 635 Zone* module_zone; |
456 ModuleResult result_; | 636 ModuleResult result_; |
457 ModuleOrigin origin_; | 637 ModuleOrigin origin_; |
458 | 638 |
459 uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); } | 639 uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); } |
460 | 640 |
461 // Decodes a single global entry inside a module starting at {pc_}. | 641 // Decodes a single global entry inside a module starting at {pc_}. |
462 void DecodeGlobalInModule(WasmGlobal* global) { | 642 void DecodeGlobalInModule(WasmModule* module, uint32_t index, |
463 global->name_offset = consume_string(&global->name_length, false); | 643 WasmGlobal* global) { |
464 if (ok() && | 644 global->type = consume_value_type(); |
465 !unibrow::Utf8::Validate(start_ + global->name_offset, | 645 global->mutability = consume_u8("mutability") != 0; |
466 global->name_length)) { | 646 const byte* pos = pc(); |
467 error("global name is not valid utf8"); | 647 global->init = consume_init_expr(module, kAstStmt); |
| 648 switch (global->init.kind) { |
| 649 case WasmInitExpr::kGlobalIndex: |
| 650 if (global->init.val.global_index >= index) { |
| 651 error("invalid global index in init expression"); |
| 652 } else if (module->globals[index].type != global->type) { |
| 653 error("type mismatch in global initialization"); |
| 654 } |
| 655 break; |
| 656 default: |
| 657 if (global->type != TypeOf(module, global->init)) { |
| 658 error(pos, pos, |
| 659 "type error in global initialization, expected %s, got %s", |
| 660 WasmOpcodes::TypeName(global->type), |
| 661 WasmOpcodes::TypeName(TypeOf(module, global->init))); |
| 662 } |
468 } | 663 } |
469 global->type = consume_local_type(); | |
470 if (global->type == kAstStmt) { | |
471 error("invalid global type"); | |
472 } | |
473 global->offset = 0; | |
474 global->exported = consume_u8("exported") != 0; | |
475 } | 664 } |
476 | 665 |
477 bool IsWithinLimit(uint32_t limit, uint32_t offset, uint32_t size) { | 666 bool IsWithinLimit(uint32_t limit, uint32_t offset, uint32_t size) { |
478 if (offset > limit) return false; | 667 if (offset > limit) return false; |
479 if ((offset + size) < offset) return false; // overflow | 668 if ((offset + size) < offset) return false; // overflow |
480 return (offset + size) <= limit; | 669 return (offset + size) <= limit; |
481 } | 670 } |
482 | 671 |
483 // Decodes a single data segment entry inside a module starting at {pc_}. | 672 // Decodes a single data segment entry inside a module starting at {pc_}. |
484 void DecodeDataSegmentInModule(WasmModule* module, WasmDataSegment* segment) { | 673 void DecodeDataSegmentInModule(WasmModule* module, WasmDataSegment* segment) { |
485 const byte* start = pc_; | 674 const byte* start = pc_; |
486 segment->dest_addr = consume_u32v("destination"); | 675 expect_u8("linear memory index", 0); |
| 676 segment->dest_addr = consume_init_expr(module, kAstI32); |
487 segment->source_size = consume_u32v("source size"); | 677 segment->source_size = consume_u32v("source size"); |
488 segment->source_offset = static_cast<uint32_t>(pc_ - start_); | 678 segment->source_offset = static_cast<uint32_t>(pc_ - start_); |
489 segment->init = true; | |
490 | 679 |
491 // Validate the data is in the module. | 680 // Validate the data is in the module. |
492 uint32_t module_limit = static_cast<uint32_t>(limit_ - start_); | 681 uint32_t module_limit = static_cast<uint32_t>(limit_ - start_); |
493 if (!IsWithinLimit(module_limit, segment->source_offset, | 682 if (!IsWithinLimit(module_limit, segment->source_offset, |
494 segment->source_size)) { | 683 segment->source_size)) { |
495 error(start, "segment out of bounds of module"); | 684 error(start, "segment out of bounds of module"); |
496 } | 685 } |
497 | 686 |
498 // Validate that the segment will fit into the (minimum) memory. | |
499 uint32_t memory_limit = | |
500 WasmModule::kPageSize * (module ? module->min_mem_pages | |
501 : WasmModule::kMaxMemPages); | |
502 if (!IsWithinLimit(memory_limit, segment->dest_addr, | |
503 segment->source_size)) { | |
504 error(start, "segment out of bounds of memory"); | |
505 } | |
506 | |
507 consume_bytes(segment->source_size); | 687 consume_bytes(segment->source_size); |
508 } | 688 } |
509 | 689 |
510 // Decodes a single function table inside a module starting at {pc_}. | |
511 void DecodeFunctionTableInModule(WasmModule* module, | |
512 WasmIndirectFunctionTable* table) { | |
513 table->size = consume_u32v("function table entry count"); | |
514 table->max_size = table->size; | |
515 | |
516 if (table->max_size != table->size) { | |
517 error("invalid table maximum size"); | |
518 } | |
519 | |
520 for (uint32_t i = 0; i < table->size; ++i) { | |
521 uint16_t index = consume_u32v(); | |
522 if (index >= module->functions.size()) { | |
523 error(pc_ - sizeof(index), "invalid function index"); | |
524 break; | |
525 } | |
526 table->values.push_back(index); | |
527 } | |
528 } | |
529 | |
530 // Calculate individual global offsets and total size of globals table. | 690 // Calculate individual global offsets and total size of globals table. |
531 void CalculateGlobalsOffsets(WasmModule* module) { | 691 void CalculateGlobalOffsets(WasmModule* module) { |
532 uint32_t offset = 0; | 692 uint32_t offset = 0; |
533 if (module->globals.size() == 0) { | 693 if (module->globals.size() == 0) { |
534 module->globals_size = 0; | 694 module->globals_size = 0; |
535 return; | 695 return; |
536 } | 696 } |
537 for (WasmGlobal& global : module->globals) { | 697 for (WasmGlobal& global : module->globals) { |
538 byte size = | 698 byte size = |
539 WasmOpcodes::MemSize(WasmOpcodes::MachineTypeFor(global.type)); | 699 WasmOpcodes::MemSize(WasmOpcodes::MachineTypeFor(global.type)); |
540 offset = (offset + size - 1) & ~(size - 1); // align | 700 offset = (offset + size - 1) & ~(size - 1); // align |
541 global.offset = offset; | 701 global.offset = offset; |
542 offset += size; | 702 offset += size; |
543 } | 703 } |
544 module->globals_size = offset; | 704 module->globals_size = offset; |
545 } | 705 } |
546 | 706 |
| 707 // TODO(titzer): this only works without overlapping initializations from |
| 708 // global bases for entries |
| 709 void PreinitializeIndirectFunctionTables(WasmModule* module) { |
| 710 // Fill all tables with invalid entries first. |
| 711 for (WasmIndirectFunctionTable& table : module->function_tables) { |
| 712 table.values.resize(table.size); |
| 713 for (size_t i = 0; i < table.size; i++) { |
| 714 table.values[i] = kInvalidFunctionIndex; |
| 715 } |
| 716 } |
| 717 for (WasmTableInit& init : module->table_inits) { |
| 718 if (init.offset.kind != WasmInitExpr::kI32Const) continue; |
| 719 if (init.table_index >= module->function_tables.size()) continue; |
| 720 WasmIndirectFunctionTable& table = |
| 721 module->function_tables[init.table_index]; |
| 722 for (size_t i = 0; i < init.entries.size(); i++) { |
| 723 size_t index = i + init.offset.val.i32_const; |
| 724 if (index < table.values.size()) { |
| 725 table.values[index] = init.entries[i]; |
| 726 } |
| 727 } |
| 728 } |
| 729 } |
| 730 |
547 // Verifies the body (code) of a given function. | 731 // Verifies the body (code) of a given function. |
548 void VerifyFunctionBody(uint32_t func_num, ModuleEnv* menv, | 732 void VerifyFunctionBody(uint32_t func_num, ModuleEnv* menv, |
549 WasmFunction* function) { | 733 WasmFunction* function) { |
550 if (FLAG_trace_wasm_decoder || FLAG_trace_wasm_decode_time) { | 734 if (FLAG_trace_wasm_decoder || FLAG_trace_wasm_decode_time) { |
551 OFStream os(stdout); | 735 OFStream os(stdout); |
552 os << "Verifying WASM function " << WasmFunctionName(function, menv) | 736 os << "Verifying WASM function " << WasmFunctionName(function, menv) |
553 << std::endl; | 737 << std::endl; |
554 } | 738 } |
555 FunctionBody body = {menv, function->sig, start_, | 739 FunctionBody body = {menv, function->sig, start_, |
556 start_ + function->code_start_offset, | 740 start_ + function->code_start_offset, |
(...skipping 10 matching lines...) Expand all Loading... |
567 char* buffer = new char[len]; | 751 char* buffer = new char[len]; |
568 strncpy(buffer, raw, len); | 752 strncpy(buffer, raw, len); |
569 buffer[len - 1] = 0; | 753 buffer[len - 1] = 0; |
570 | 754 |
571 // Copy error code and location. | 755 // Copy error code and location. |
572 result_.MoveFrom(result); | 756 result_.MoveFrom(result); |
573 result_.error_msg.reset(buffer); | 757 result_.error_msg.reset(buffer); |
574 } | 758 } |
575 } | 759 } |
576 | 760 |
577 // Reads a single 32-bit unsigned integer interpreted as an offset, checking | |
578 // the offset is within bounds and advances. | |
579 uint32_t consume_offset(const char* name = nullptr) { | |
580 uint32_t offset = consume_u32(name ? name : "offset"); | |
581 if (offset > static_cast<uint32_t>(limit_ - start_)) { | |
582 error(pc_ - sizeof(uint32_t), "offset out of bounds of module"); | |
583 } | |
584 return offset; | |
585 } | |
586 | |
587 // Reads a length-prefixed string, checking that it is within bounds. Returns | 761 // Reads a length-prefixed string, checking that it is within bounds. Returns |
588 // the offset of the string, and the length as an out parameter. | 762 // the offset of the string, and the length as an out parameter. |
589 uint32_t consume_string(uint32_t* length, bool validate_utf8) { | 763 uint32_t consume_string(uint32_t* length, bool validate_utf8) { |
590 *length = consume_u32v("string length"); | 764 *length = consume_u32v("string length"); |
591 uint32_t offset = pc_offset(); | 765 uint32_t offset = pc_offset(); |
592 TRACE(" +%u %-20s: (%u bytes)\n", offset, "string", *length); | 766 TRACE(" +%u %-20s: (%u bytes)\n", offset, "string", *length); |
593 const byte* string_start = pc_; | 767 const byte* string_start = pc_; |
594 // Consume bytes before validation to guarantee that the string is not oob. | 768 // Consume bytes before validation to guarantee that the string is not oob. |
595 consume_bytes(*length); | 769 consume_bytes(*length); |
596 if (ok() && validate_utf8 && | 770 if (ok() && validate_utf8 && |
(...skipping 10 matching lines...) Expand all Loading... |
607 error(pos, pos, "signature index %u out of bounds (%d signatures)", | 781 error(pos, pos, "signature index %u out of bounds (%d signatures)", |
608 sig_index, static_cast<int>(module->signatures.size())); | 782 sig_index, static_cast<int>(module->signatures.size())); |
609 *sig = nullptr; | 783 *sig = nullptr; |
610 return 0; | 784 return 0; |
611 } | 785 } |
612 *sig = module->signatures[sig_index]; | 786 *sig = module->signatures[sig_index]; |
613 return sig_index; | 787 return sig_index; |
614 } | 788 } |
615 | 789 |
616 uint32_t consume_func_index(WasmModule* module, WasmFunction** func) { | 790 uint32_t consume_func_index(WasmModule* module, WasmFunction** func) { |
| 791 return consume_index("function index", module->functions, func); |
| 792 } |
| 793 |
| 794 uint32_t consume_global_index(WasmModule* module, WasmGlobal** global) { |
| 795 return consume_index("global index", module->globals, global); |
| 796 } |
| 797 |
| 798 uint32_t consume_table_index(WasmModule* module, |
| 799 WasmIndirectFunctionTable** table) { |
| 800 return consume_index("table index", module->function_tables, table); |
| 801 } |
| 802 |
| 803 template <typename T> |
| 804 uint32_t consume_index(const char* name, std::vector<T>& vector, T** ptr) { |
617 const byte* pos = pc_; | 805 const byte* pos = pc_; |
618 uint32_t func_index = consume_u32v("function index"); | 806 uint32_t index = consume_u32v(name); |
619 if (func_index >= module->functions.size()) { | 807 if (index >= vector.size()) { |
620 error(pos, pos, "function index %u out of bounds (%d functions)", | 808 error(pos, pos, "%s %u out of bounds (%d entries)", name, index, |
621 func_index, static_cast<int>(module->functions.size())); | 809 static_cast<int>(vector.size())); |
622 *func = nullptr; | 810 *ptr = nullptr; |
623 return 0; | 811 return 0; |
624 } | 812 } |
625 *func = &module->functions[func_index]; | 813 *ptr = &vector[index]; |
626 return func_index; | 814 return index; |
| 815 } |
| 816 |
| 817 void consume_resizable_limits(const char* name, const char* units, |
| 818 uint32_t max_value, uint32_t* initial, |
| 819 uint32_t* maximum) { |
| 820 uint32_t flags = consume_u32v("resizable limits flags"); |
| 821 const byte* pos = pc(); |
| 822 *initial = consume_u32v("initial size"); |
| 823 if (*initial > max_value) { |
| 824 error(pos, pos, |
| 825 "initial %s size (%u %s) is larger than maximum allowable (%u)", |
| 826 name, *initial, units, max_value); |
| 827 } |
| 828 if (flags & 1) { |
| 829 pos = pc(); |
| 830 *maximum = consume_u32v("maximum size"); |
| 831 if (*maximum > max_value) { |
| 832 error(pos, pos, |
| 833 "maximum %s size (%u %s) is larger than maximum allowable (%u)", |
| 834 name, *maximum, units, max_value); |
| 835 } |
| 836 if (*maximum < *initial) { |
| 837 error(pos, pos, "maximum %s size (%u %s) is less than initial (%u %s)", |
| 838 name, *maximum, units, *initial, units); |
| 839 } |
| 840 } else { |
| 841 *maximum = 0; |
| 842 } |
| 843 } |
| 844 |
| 845 bool expect_u8(const char* name, uint8_t expected) { |
| 846 const byte* pos = pc(); |
| 847 uint8_t value = consume_u8(name); |
| 848 if (value != expected) { |
| 849 error(pos, pos, "expected %s 0x%02x, got 0x%02x", name, expected, value); |
| 850 return false; |
| 851 } |
| 852 return true; |
| 853 } |
| 854 |
| 855 WasmInitExpr consume_init_expr(WasmModule* module, LocalType expected) { |
| 856 const byte* pos = pc(); |
| 857 uint8_t opcode = consume_u8("opcode"); |
| 858 WasmInitExpr expr; |
| 859 unsigned len = 0; |
| 860 switch (opcode) { |
| 861 case kExprGetGlobal: { |
| 862 GlobalIndexOperand operand(this, pc() - 1); |
| 863 expr.kind = WasmInitExpr::kGlobalIndex; |
| 864 expr.val.global_index = operand.index; |
| 865 len = operand.length; |
| 866 break; |
| 867 } |
| 868 case kExprI32Const: { |
| 869 ImmI32Operand operand(this, pc() - 1); |
| 870 expr.kind = WasmInitExpr::kI32Const; |
| 871 expr.val.i32_const = operand.value; |
| 872 len = operand.length; |
| 873 break; |
| 874 } |
| 875 case kExprF32Const: { |
| 876 ImmF32Operand operand(this, pc() - 1); |
| 877 expr.kind = WasmInitExpr::kF32Const; |
| 878 expr.val.f32_const = operand.value; |
| 879 len = operand.length; |
| 880 break; |
| 881 } |
| 882 case kExprI64Const: { |
| 883 ImmI64Operand operand(this, pc() - 1); |
| 884 expr.kind = WasmInitExpr::kI64Const; |
| 885 expr.val.i64_const = operand.value; |
| 886 len = operand.length; |
| 887 break; |
| 888 } |
| 889 case kExprF64Const: { |
| 890 ImmF64Operand operand(this, pc() - 1); |
| 891 expr.kind = WasmInitExpr::kF64Const; |
| 892 expr.val.f64_const = operand.value; |
| 893 len = operand.length; |
| 894 break; |
| 895 } |
| 896 default: { |
| 897 error("invalid opcode in initialization expression"); |
| 898 expr.kind = WasmInitExpr::kNone; |
| 899 expr.val.i32_const = 0; |
| 900 } |
| 901 } |
| 902 consume_bytes(len); |
| 903 if (!expect_u8("end opcode", kExprEnd)) { |
| 904 expr.kind = WasmInitExpr::kNone; |
| 905 } |
| 906 if (expected != kAstStmt && TypeOf(module, expr) != kAstI32) { |
| 907 error(pos, pos, "type error in init expression, expected %s, got %s", |
| 908 WasmOpcodes::TypeName(expected), |
| 909 WasmOpcodes::TypeName(TypeOf(module, expr))); |
| 910 } |
| 911 return expr; |
627 } | 912 } |
628 | 913 |
629 // Reads a single 8-bit integer, interpreting it as a local type. | 914 // Reads a single 8-bit integer, interpreting it as a local type. |
630 LocalType consume_local_type() { | 915 LocalType consume_value_type() { |
631 byte val = consume_u8("local type"); | 916 byte val = consume_u8("value type"); |
632 LocalTypeCode t = static_cast<LocalTypeCode>(val); | 917 LocalTypeCode t = static_cast<LocalTypeCode>(val); |
633 switch (t) { | 918 switch (t) { |
634 case kLocalVoid: | |
635 return kAstStmt; | |
636 case kLocalI32: | 919 case kLocalI32: |
637 return kAstI32; | 920 return kAstI32; |
638 case kLocalI64: | 921 case kLocalI64: |
639 return kAstI64; | 922 return kAstI64; |
640 case kLocalF32: | 923 case kLocalF32: |
641 return kAstF32; | 924 return kAstF32; |
642 case kLocalF64: | 925 case kLocalF64: |
643 return kAstF64; | 926 return kAstF64; |
644 case kLocalS128: | 927 case kLocalS128: |
645 return kAstS128; | 928 return kAstS128; |
646 default: | 929 default: |
647 error(pc_ - 1, "invalid local type"); | 930 error(pc_ - 1, "invalid local type"); |
648 return kAstStmt; | 931 return kAstStmt; |
649 } | 932 } |
650 } | 933 } |
651 | 934 |
652 // Parses a type entry, which is currently limited to functions only. | 935 // Parses a type entry, which is currently limited to functions only. |
653 FunctionSig* consume_sig() { | 936 FunctionSig* consume_sig() { |
654 const byte* pos = pc_; | 937 if (!expect_u8("type form", kWasmFunctionTypeForm)) return nullptr; |
655 byte form = consume_u8("type form"); | |
656 if (form != kWasmFunctionTypeForm) { | |
657 error(pos, pos, "expected function type form (0x%02x), got: 0x%02x", | |
658 kWasmFunctionTypeForm, form); | |
659 return nullptr; | |
660 } | |
661 // parse parameter types | 938 // parse parameter types |
662 uint32_t param_count = consume_u32v("param count"); | 939 uint32_t param_count = consume_u32v("param count"); |
663 std::vector<LocalType> params; | 940 std::vector<LocalType> params; |
664 for (uint32_t i = 0; i < param_count; ++i) { | 941 for (uint32_t i = 0; i < param_count; ++i) { |
665 LocalType param = consume_local_type(); | 942 LocalType param = consume_value_type(); |
666 if (param == kAstStmt) error(pc_ - 1, "invalid void parameter type"); | |
667 params.push_back(param); | 943 params.push_back(param); |
668 } | 944 } |
669 | 945 |
670 // parse return types | 946 // parse return types |
671 const byte* pt = pc_; | 947 const byte* pt = pc_; |
672 uint32_t return_count = consume_u32v("return count"); | 948 uint32_t return_count = consume_u32v("return count"); |
673 if (return_count > kMaxReturnCount) { | 949 if (return_count > kMaxReturnCount) { |
674 error(pt, pt, "return count of %u exceeds maximum of %u", return_count, | 950 error(pt, pt, "return count of %u exceeds maximum of %u", return_count, |
675 kMaxReturnCount); | 951 kMaxReturnCount); |
676 return nullptr; | 952 return nullptr; |
677 } | 953 } |
678 std::vector<LocalType> returns; | 954 std::vector<LocalType> returns; |
679 for (uint32_t i = 0; i < return_count; ++i) { | 955 for (uint32_t i = 0; i < return_count; ++i) { |
680 LocalType ret = consume_local_type(); | 956 LocalType ret = consume_value_type(); |
681 if (ret == kAstStmt) error(pc_ - 1, "invalid void return type"); | |
682 returns.push_back(ret); | 957 returns.push_back(ret); |
683 } | 958 } |
684 | 959 |
685 // FunctionSig stores the return types first. | 960 // FunctionSig stores the return types first. |
686 LocalType* buffer = | 961 LocalType* buffer = |
687 module_zone->NewArray<LocalType>(param_count + return_count); | 962 module_zone->NewArray<LocalType>(param_count + return_count); |
688 uint32_t b = 0; | 963 uint32_t b = 0; |
689 for (uint32_t i = 0; i < return_count; ++i) buffer[b++] = returns[i]; | 964 for (uint32_t i = 0; i < return_count; ++i) buffer[b++] = returns[i]; |
690 for (uint32_t i = 0; i < param_count; ++i) buffer[b++] = params[i]; | 965 for (uint32_t i = 0; i < param_count; ++i) buffer[b++] = params[i]; |
691 | 966 |
(...skipping 21 matching lines...) Expand all Loading... |
713 error_code = kError; | 988 error_code = kError; |
714 size_t len = strlen(msg) + 1; | 989 size_t len = strlen(msg) + 1; |
715 char* result = new char[len]; | 990 char* result = new char[len]; |
716 strncpy(result, msg, len); | 991 strncpy(result, msg, len); |
717 result[len - 1] = 0; | 992 result[len - 1] = 0; |
718 error_msg.reset(result); | 993 error_msg.reset(result); |
719 } | 994 } |
720 }; | 995 }; |
721 | 996 |
722 Vector<const byte> FindSection(const byte* module_start, const byte* module_end, | 997 Vector<const byte> FindSection(const byte* module_start, const byte* module_end, |
723 WasmSection::Code code) { | 998 WasmSectionCode code) { |
724 Decoder decoder(module_start, module_end); | 999 Decoder decoder(module_start, module_end); |
725 | 1000 |
726 uint32_t magic_word = decoder.consume_u32("wasm magic"); | 1001 uint32_t magic_word = decoder.consume_u32("wasm magic"); |
727 if (magic_word != kWasmMagic) decoder.error("wrong magic word"); | 1002 if (magic_word != kWasmMagic) decoder.error("wrong magic word"); |
728 | 1003 |
729 uint32_t magic_version = decoder.consume_u32("wasm version"); | 1004 uint32_t magic_version = decoder.consume_u32("wasm version"); |
730 if (magic_version != kWasmVersion) decoder.error("wrong wasm version"); | 1005 if (magic_version != kWasmVersion) decoder.error("wrong wasm version"); |
731 | 1006 |
732 while (decoder.more() && decoder.ok()) { | 1007 WasmSectionIterator section_iter(decoder); |
733 // Read the section name. | 1008 while (section_iter.more()) { |
734 uint32_t string_length = decoder.consume_u32v("section name length"); | 1009 if (section_iter.section_code() == code) { |
735 const byte* section_name_start = decoder.pc(); | 1010 return Vector<const uint8_t>(section_iter.section_start(), |
736 decoder.consume_bytes(string_length); | 1011 section_iter.section_length()); |
737 if (decoder.failed()) break; | |
738 | |
739 WasmSection::Code section = | |
740 WasmSection::lookup(section_name_start, string_length); | |
741 | |
742 // Read and check the section size. | |
743 uint32_t section_length = decoder.consume_u32v("section length"); | |
744 | |
745 const byte* section_start = decoder.pc(); | |
746 decoder.consume_bytes(section_length); | |
747 if (section == code && decoder.ok()) { | |
748 return Vector<const uint8_t>(section_start, section_length); | |
749 } | 1012 } |
| 1013 decoder.consume_bytes(section_iter.section_length()); |
| 1014 section_iter.advance(); |
750 } | 1015 } |
751 | 1016 |
752 return Vector<const uint8_t>(); | 1017 return Vector<const uint8_t>(); |
753 } | 1018 } |
754 | 1019 |
755 } // namespace | 1020 } // namespace |
756 | 1021 |
757 ModuleResult DecodeWasmModule(Isolate* isolate, Zone* zone, | 1022 ModuleResult DecodeWasmModule(Isolate* isolate, Zone* zone, |
758 const byte* module_start, const byte* module_end, | 1023 const byte* module_start, const byte* module_end, |
759 bool verify_functions, ModuleOrigin origin) { | 1024 bool verify_functions, ModuleOrigin origin) { |
(...skipping 14 matching lines...) Expand all Loading... |
774 static_cast<int>(zone->allocation_size() - decode_memory_start)); | 1039 static_cast<int>(zone->allocation_size() - decode_memory_start)); |
775 return result; | 1040 return result; |
776 } | 1041 } |
777 | 1042 |
778 FunctionSig* DecodeWasmSignatureForTesting(Zone* zone, const byte* start, | 1043 FunctionSig* DecodeWasmSignatureForTesting(Zone* zone, const byte* start, |
779 const byte* end) { | 1044 const byte* end) { |
780 ModuleDecoder decoder(zone, start, end, kWasmOrigin); | 1045 ModuleDecoder decoder(zone, start, end, kWasmOrigin); |
781 return decoder.DecodeFunctionSignature(start); | 1046 return decoder.DecodeFunctionSignature(start); |
782 } | 1047 } |
783 | 1048 |
| 1049 WasmInitExpr DecodeWasmInitExprForTesting(const byte* start, const byte* end) { |
| 1050 base::AccountingAllocator allocator; |
| 1051 Zone zone(&allocator); |
| 1052 ModuleDecoder decoder(&zone, start, end, kWasmOrigin); |
| 1053 return decoder.DecodeInitExpr(start); |
| 1054 } |
| 1055 |
784 FunctionResult DecodeWasmFunction(Isolate* isolate, Zone* zone, | 1056 FunctionResult DecodeWasmFunction(Isolate* isolate, Zone* zone, |
785 ModuleEnv* module_env, | 1057 ModuleEnv* module_env, |
786 const byte* function_start, | 1058 const byte* function_start, |
787 const byte* function_end) { | 1059 const byte* function_end) { |
788 HistogramTimerScope wasm_decode_function_time_scope( | 1060 HistogramTimerScope wasm_decode_function_time_scope( |
789 isolate->counters()->wasm_decode_function_time()); | 1061 isolate->counters()->wasm_decode_function_time()); |
790 size_t size = function_end - function_start; | 1062 size_t size = function_end - function_start; |
791 if (function_start > function_end) return FunctionError("start > end"); | 1063 if (function_start > function_end) return FunctionError("start > end"); |
792 if (size > kMaxFunctionSize) | 1064 if (size > kMaxFunctionSize) |
793 return FunctionError("size > maximum function size"); | 1065 return FunctionError("size > maximum function size"); |
794 isolate->counters()->wasm_function_size_bytes()->AddSample( | 1066 isolate->counters()->wasm_function_size_bytes()->AddSample( |
795 static_cast<int>(size)); | 1067 static_cast<int>(size)); |
796 WasmFunction* function = new WasmFunction(); | 1068 WasmFunction* function = new WasmFunction(); |
797 ModuleDecoder decoder(zone, function_start, function_end, kWasmOrigin); | 1069 ModuleDecoder decoder(zone, function_start, function_end, kWasmOrigin); |
798 return decoder.DecodeSingleFunction(module_env, function); | 1070 return decoder.DecodeSingleFunction(module_env, function); |
799 } | 1071 } |
800 | 1072 |
801 FunctionOffsetsResult DecodeWasmFunctionOffsets(const byte* module_start, | 1073 FunctionOffsetsResult DecodeWasmFunctionOffsets( |
802 const byte* module_end) { | 1074 const byte* module_start, const byte* module_end, |
| 1075 uint32_t num_imported_functions) { |
| 1076 // Find and decode the code section. |
803 Vector<const byte> code_section = | 1077 Vector<const byte> code_section = |
804 FindSection(module_start, module_end, WasmSection::Code::FunctionBodies); | 1078 FindSection(module_start, module_end, kCodeSectionCode); |
805 Decoder decoder(code_section.start(), code_section.end()); | 1079 Decoder decoder(code_section.start(), code_section.end()); |
806 if (!code_section.start()) decoder.error("no code section"); | 1080 FunctionOffsets table; |
| 1081 if (!code_section.start()) { |
| 1082 decoder.error("no code section"); |
| 1083 return decoder.toResult(std::move(table)); |
| 1084 } |
| 1085 |
| 1086 // Reserve entries for the imported functions. |
| 1087 table.reserve(num_imported_functions); |
| 1088 for (uint32_t i = 0; i < num_imported_functions; i++) { |
| 1089 table.push_back(std::make_pair(0, 0)); |
| 1090 } |
807 | 1091 |
808 uint32_t functions_count = decoder.consume_u32v("functions count"); | 1092 uint32_t functions_count = decoder.consume_u32v("functions count"); |
809 FunctionOffsets table; | |
810 // Take care of invalid input here. | 1093 // Take care of invalid input here. |
811 if (functions_count < static_cast<unsigned>(code_section.length()) / 2) | 1094 if (functions_count < static_cast<unsigned>(code_section.length()) / 2) |
812 table.reserve(functions_count); | 1095 table.reserve(functions_count); |
813 int section_offset = static_cast<int>(code_section.start() - module_start); | 1096 int section_offset = static_cast<int>(code_section.start() - module_start); |
814 DCHECK_LE(0, section_offset); | 1097 DCHECK_LE(0, section_offset); |
815 for (uint32_t i = 0; i < functions_count && decoder.ok(); ++i) { | 1098 for (uint32_t i = 0; i < functions_count && decoder.ok(); ++i) { |
816 uint32_t size = decoder.consume_u32v("body size"); | 1099 uint32_t size = decoder.consume_u32v("body size"); |
817 int offset = static_cast<int>(section_offset + decoder.pc_offset()); | 1100 int offset = static_cast<int>(section_offset + decoder.pc_offset()); |
818 table.push_back(std::make_pair(offset, static_cast<int>(size))); | 1101 table.push_back(std::make_pair(offset, static_cast<int>(size))); |
819 DCHECK(table.back().first >= 0 && table.back().second >= 0); | 1102 DCHECK(table.back().first >= 0 && table.back().second >= 0); |
820 decoder.consume_bytes(size); | 1103 decoder.consume_bytes(size); |
821 } | 1104 } |
822 if (decoder.more()) decoder.error("unexpected additional bytes"); | 1105 if (decoder.more()) decoder.error("unexpected additional bytes"); |
823 | 1106 |
824 return decoder.toResult(std::move(table)); | 1107 return decoder.toResult(std::move(table)); |
825 } | 1108 } |
826 | 1109 |
827 } // namespace wasm | 1110 } // namespace wasm |
828 } // namespace internal | 1111 } // namespace internal |
829 } // namespace v8 | 1112 } // namespace v8 |
OLD | NEW |