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) { | |
Mircea Trofin
2016/09/19 16:16:26
const WasmModule* module, const 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, "section name"); | |
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, "section payload"); | |
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_)); |
Mircea Trofin
2016/09/19 16:16:25
it'd be nice if all accessing happened with pc(),
titzer
2016/09/21 08:58:45
That's orthogonal to this CL and can be fixed late
| |
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++) { |
Mircea Trofin
2016/09/19 16:16:25
++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++) { |
Mircea Trofin
2016/09/19 16:16:26
++i
| |
303 0, // module_name_offset | 380 consume_resizable_limits("memory", "pages", 65536, |
Mircea Trofin
2016/09/19 16:16:25
is 65536 a const somewhere? or std::numeric_limits
titzer
2016/09/21 08:58:44
It's the maximum number of legal pages. I've added
| |
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 if (a.name_length != b.name_length) { |
382 size_t expected_length = static_cast<size_t>(section_length); | 459 return a.name_length < b.name_length; |
383 size_t actual_length = static_cast<size_t>(pc_ - section_start); | 460 } |
384 error(pc_, pc_, | 461 return memcmp(base + a.name_offset, base + b.name_offset, |
385 "section \"%s\" %s (%zu bytes) than specified (%zu bytes)", | 462 a.name_length) < 0; |
386 WasmSection::getName(section), diff, actual_length, | 463 }; |
387 expected_length); | 464 std::stable_sort(sorted_exports.begin(), sorted_exports.end(), |
388 break; | 465 cmp_less); |
389 } | 466 auto it = sorted_exports.begin(); |
390 } | 467 WasmExport* last = &*it++; |
391 | 468 for (auto end = sorted_exports.end(); it != end; last = &*it++) { |
392 done: | 469 DCHECK(!cmp_less(*it, *last)); // Vector must be sorted. |
393 if (ok()) CalculateGlobalsOffsets(module); | 470 if (!cmp_less(*last, *it)) { |
471 const byte* pc = start_ + it->name_offset; | |
472 error(pc, pc, | |
473 "Duplicate export name '%.*s' for functions %d and %d", | |
474 it->name_length, pc, last->index, it->index); | |
475 break; | |
476 } | |
477 } | |
478 } | |
479 section_iter.advance(); | |
480 } | |
481 | |
482 // ===== Start section =================================================== | |
483 if (section_iter.section_code() == kStartSectionCode) { | |
484 WasmFunction* func; | |
485 const byte* pos = pc_; | |
486 module->start_function_index = consume_func_index(module, &func); | |
487 if (func && func->sig->parameter_count() > 0) { | |
488 error(pos, "invalid start function: non-zero parameter count"); | |
489 } | |
490 section_iter.advance(); | |
491 } | |
492 | |
493 // ===== Elements section ================================================ | |
494 if (section_iter.section_code() == kElementSectionCode) { | |
495 uint32_t element_count = consume_u32v("element count"); | |
496 for (uint32_t i = 0; ok() && i < element_count; ++i) { | |
497 uint32_t table_index = consume_u32v("table index"); | |
498 if (table_index != 0) error("illegal table index != 0"); | |
499 WasmInitExpr offset = consume_init_expr(module, kAstI32); | |
500 uint32_t num_elem = consume_u32v("number of elements"); | |
501 std::vector<uint32_t> vector; | |
502 module->table_inits.push_back({table_index, offset, vector}); | |
503 WasmTableInit* init = &module->table_inits.back(); | |
504 init->entries.reserve(SafeReserve(num_elem)); | |
505 for (uint32_t j = 0; ok() && j < num_elem; j++) { | |
Mircea Trofin
2016/09/19 16:16:25
++j
| |
506 WasmFunction* func = nullptr; | |
507 init->entries.push_back(consume_func_index(module, &func)); | |
508 } | |
509 } | |
510 | |
511 section_iter.advance(); | |
512 } | |
513 | |
514 // ===== Code section ==================================================== | |
515 if (section_iter.section_code() == kCodeSectionCode) { | |
516 const byte* pos = pc_; | |
517 uint32_t functions_count = consume_u32v("functions count"); | |
518 if (functions_count != module->num_declared_functions) { | |
519 error(pos, pos, "function body count %u mismatch (%u expected)", | |
520 functions_count, module->num_declared_functions); | |
521 } | |
522 for (uint32_t i = 0; ok() && i < functions_count; ++i) { | |
523 WasmFunction* function = | |
524 &module->functions[i + module->num_imported_functions]; | |
525 uint32_t size = consume_u32v("body size"); | |
526 function->code_start_offset = pc_offset(); | |
527 function->code_end_offset = pc_offset() + size; | |
528 consume_bytes(size, "function body"); | |
529 } | |
530 section_iter.advance(); | |
531 } | |
532 | |
533 // ===== Data section ==================================================== | |
534 if (section_iter.section_code() == kDataSectionCode) { | |
535 uint32_t data_segments_count = consume_u32v("data segments count"); | |
536 module->data_segments.reserve(SafeReserve(data_segments_count)); | |
537 for (uint32_t i = 0; ok() && i < data_segments_count; ++i) { | |
538 TRACE("DecodeDataSegment[%d] module+%d\n", i, | |
539 static_cast<int>(pc_ - start_)); | |
540 module->data_segments.push_back({ | |
541 NO_INIT, // dest_addr | |
542 0, // source_offset | |
543 0 // source_size | |
544 }); | |
545 WasmDataSegment* segment = &module->data_segments.back(); | |
546 DecodeDataSegmentInModule(module, segment); | |
547 } | |
548 section_iter.advance(); | |
549 } | |
550 | |
551 // ===== Name section ==================================================== | |
552 if (section_iter.section_code() == kNameSectionCode) { | |
553 const byte* pos = pc_; | |
554 uint32_t functions_count = consume_u32v("functions count"); | |
555 if (functions_count != module->num_declared_functions) { | |
556 error(pos, pos, "function name count %u mismatch (%u expected)", | |
557 functions_count, module->num_declared_functions); | |
558 } | |
559 | |
560 for (uint32_t i = 0; ok() && i < functions_count; ++i) { | |
561 WasmFunction* function = | |
562 &module->functions[i + module->num_imported_functions]; | |
563 function->name_offset = consume_string(&function->name_length, false); | |
564 | |
565 uint32_t local_names_count = consume_u32v("local names count"); | |
566 for (uint32_t j = 0; j < local_names_count; j++) { | |
Mircea Trofin
2016/09/19 16:16:26
++j
| |
567 uint32_t unused = 0; | |
568 uint32_t offset = consume_string(&unused, false); | |
569 USE(unused); | |
570 USE(offset); | |
571 } | |
572 } | |
573 section_iter.advance(); | |
574 } | |
575 | |
576 // ===== Remaining sections ============================================== | |
577 if (section_iter.more() && ok()) { | |
578 error(pc(), pc(), "unexpected section: %s", | |
579 SectionName(section_iter.section_code())); | |
580 } | |
581 | |
582 if (ok()) { | |
583 CalculateGlobalOffsets(module); | |
584 PreinitializeIndirectFunctionTables(module); | |
585 } | |
394 const WasmModule* finished_module = module; | 586 const WasmModule* finished_module = module; |
395 ModuleResult result = toResult(finished_module); | 587 ModuleResult result = toResult(finished_module); |
396 if (FLAG_dump_wasm_module) { | 588 if (FLAG_dump_wasm_module) DumpModule(module, result); |
397 DumpModule(module, result); | |
398 } | |
399 return result; | 589 return result; |
400 } | 590 } |
401 | 591 |
402 uint32_t SafeReserve(uint32_t count) { | 592 uint32_t SafeReserve(uint32_t count) { |
403 // Avoid OOM by only reserving up to a certain size. | 593 // Avoid OOM by only reserving up to a certain size. |
404 const uint32_t kMaxReserve = 20000; | 594 const uint32_t kMaxReserve = 20000; |
405 return count < kMaxReserve ? count : kMaxReserve; | 595 return count < kMaxReserve ? count : kMaxReserve; |
406 } | 596 } |
407 | 597 |
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_}. | 598 // Decodes a single anonymous function starting at {start_}. |
430 FunctionResult DecodeSingleFunction(ModuleEnv* module_env, | 599 FunctionResult DecodeSingleFunction(ModuleEnv* module_env, |
431 WasmFunction* function) { | 600 WasmFunction* function) { |
432 pc_ = start_; | 601 pc_ = start_; |
433 function->sig = consume_sig(); // read signature | 602 function->sig = consume_sig(); // read signature |
434 function->name_offset = 0; // ---- name | 603 function->name_offset = 0; // ---- name |
435 function->name_length = 0; // ---- name length | 604 function->name_length = 0; // ---- name length |
436 function->code_start_offset = off(pc_); // ---- code start | 605 function->code_start_offset = off(pc_); // ---- code start |
437 function->code_end_offset = off(limit_); // ---- code end | 606 function->code_end_offset = off(limit_); // ---- code end |
438 | 607 |
439 if (ok()) VerifyFunctionBody(0, module_env, function); | 608 if (ok()) VerifyFunctionBody(0, module_env, function); |
440 | 609 |
441 FunctionResult result; | 610 FunctionResult result; |
442 result.MoveFrom(result_); // Copy error code and location. | 611 result.MoveFrom(result_); // Copy error code and location. |
443 result.val = function; | 612 result.val = function; |
444 return result; | 613 return result; |
445 } | 614 } |
446 | 615 |
447 // Decodes a single function signature at {start}. | 616 // Decodes a single function signature at {start}. |
448 FunctionSig* DecodeFunctionSignature(const byte* start) { | 617 FunctionSig* DecodeFunctionSignature(const byte* start) { |
449 pc_ = start; | 618 pc_ = start; |
450 FunctionSig* result = consume_sig(); | 619 FunctionSig* result = consume_sig(); |
451 return ok() ? result : nullptr; | 620 return ok() ? result : nullptr; |
452 } | 621 } |
453 | 622 |
623 WasmInitExpr DecodeInitExpr(const byte* start) { | |
624 pc_ = start; | |
625 return consume_init_expr(nullptr, kAstStmt); | |
626 } | |
627 | |
454 private: | 628 private: |
455 Zone* module_zone; | 629 Zone* module_zone; |
456 ModuleResult result_; | 630 ModuleResult result_; |
457 ModuleOrigin origin_; | 631 ModuleOrigin origin_; |
458 | 632 |
459 uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); } | 633 uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); } |
460 | 634 |
461 // Decodes a single global entry inside a module starting at {pc_}. | 635 // Decodes a single global entry inside a module starting at {pc_}. |
462 void DecodeGlobalInModule(WasmGlobal* global) { | 636 void DecodeGlobalInModule(WasmModule* module, uint32_t index, |
463 global->name_offset = consume_string(&global->name_length, false); | 637 WasmGlobal* global) { |
464 if (ok() && | 638 global->type = consume_value_type(); |
465 !unibrow::Utf8::Validate(start_ + global->name_offset, | 639 global->mutability = consume_u8("mutability") != 0; |
466 global->name_length)) { | 640 const byte* pos = pc(); |
467 error("global name is not valid utf8"); | 641 global->init = consume_init_expr(module, kAstStmt); |
642 switch (global->init.kind) { | |
643 case WasmInitExpr::kGlobalIndex: | |
644 if (global->init.val.global_index >= index) { | |
645 error("invalid global index in init expression"); | |
646 } else if (module->globals[index].type != global->type) { | |
647 error("type mismatch in global initialization"); | |
648 } | |
649 break; | |
650 default: | |
651 if (global->type != TypeOf(module, global->init)) { | |
652 error(pos, pos, | |
653 "type error in global initialization, expected %s, got %s", | |
654 WasmOpcodes::TypeName(global->type), | |
655 WasmOpcodes::TypeName(TypeOf(module, global->init))); | |
656 } | |
468 } | 657 } |
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 } | 658 } |
476 | 659 |
477 bool IsWithinLimit(uint32_t limit, uint32_t offset, uint32_t size) { | 660 bool IsWithinLimit(uint32_t limit, uint32_t offset, uint32_t size) { |
478 if (offset > limit) return false; | 661 if (offset > limit) return false; |
479 if ((offset + size) < offset) return false; // overflow | 662 if ((offset + size) < offset) return false; // overflow |
480 return (offset + size) <= limit; | 663 return (offset + size) <= limit; |
481 } | 664 } |
482 | 665 |
483 // Decodes a single data segment entry inside a module starting at {pc_}. | 666 // Decodes a single data segment entry inside a module starting at {pc_}. |
484 void DecodeDataSegmentInModule(WasmModule* module, WasmDataSegment* segment) { | 667 void DecodeDataSegmentInModule(WasmModule* module, WasmDataSegment* segment) { |
485 const byte* start = pc_; | 668 const byte* start = pc_; |
486 segment->dest_addr = consume_u32v("destination"); | 669 expect_u8("linear memory index", 0); |
670 segment->dest_addr = consume_init_expr(module, kAstI32); | |
487 segment->source_size = consume_u32v("source size"); | 671 segment->source_size = consume_u32v("source size"); |
488 segment->source_offset = static_cast<uint32_t>(pc_ - start_); | 672 segment->source_offset = static_cast<uint32_t>(pc_ - start_); |
489 segment->init = true; | |
490 | 673 |
491 // Validate the data is in the module. | 674 // Validate the data is in the module. |
492 uint32_t module_limit = static_cast<uint32_t>(limit_ - start_); | 675 uint32_t module_limit = static_cast<uint32_t>(limit_ - start_); |
493 if (!IsWithinLimit(module_limit, segment->source_offset, | 676 if (!IsWithinLimit(module_limit, segment->source_offset, |
494 segment->source_size)) { | 677 segment->source_size)) { |
495 error(start, "segment out of bounds of module"); | 678 error(start, "segment out of bounds of module"); |
496 } | 679 } |
497 | 680 |
498 // Validate that the segment will fit into the (minimum) memory. | 681 consume_bytes(segment->source_size, "segment data"); |
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); | |
508 } | |
509 | |
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 } | 682 } |
529 | 683 |
530 // Calculate individual global offsets and total size of globals table. | 684 // Calculate individual global offsets and total size of globals table. |
531 void CalculateGlobalsOffsets(WasmModule* module) { | 685 void CalculateGlobalOffsets(WasmModule* module) { |
532 uint32_t offset = 0; | 686 uint32_t offset = 0; |
533 if (module->globals.size() == 0) { | 687 if (module->globals.size() == 0) { |
534 module->globals_size = 0; | 688 module->globals_size = 0; |
535 return; | 689 return; |
536 } | 690 } |
537 for (WasmGlobal& global : module->globals) { | 691 for (WasmGlobal& global : module->globals) { |
538 byte size = | 692 byte size = |
539 WasmOpcodes::MemSize(WasmOpcodes::MachineTypeFor(global.type)); | 693 WasmOpcodes::MemSize(WasmOpcodes::MachineTypeFor(global.type)); |
540 offset = (offset + size - 1) & ~(size - 1); // align | 694 offset = (offset + size - 1) & ~(size - 1); // align |
541 global.offset = offset; | 695 global.offset = offset; |
542 offset += size; | 696 offset += size; |
543 } | 697 } |
544 module->globals_size = offset; | 698 module->globals_size = offset; |
545 } | 699 } |
546 | 700 |
701 // TODO(titzer): this only works without overlapping initializations from | |
702 // global bases for entries | |
703 void PreinitializeIndirectFunctionTables(WasmModule* module) { | |
704 // Fill all tables with invalid entries first. | |
705 for (WasmIndirectFunctionTable& table : module->function_tables) { | |
706 table.values.resize(table.size); | |
707 for (size_t i = 0; i < table.size; i++) { | |
Mircea Trofin
2016/09/19 16:16:26
++i
| |
708 table.values[i] = kInvalidFunctionIndex; | |
709 } | |
710 } | |
711 for (WasmTableInit& init : module->table_inits) { | |
Mircea Trofin
2016/09/19 16:16:25
const WasmTableInit&?
| |
712 if (init.offset.kind != WasmInitExpr::kI32Const) continue; | |
713 if (init.table_index >= module->function_tables.size()) continue; | |
714 WasmIndirectFunctionTable& table = | |
715 module->function_tables[init.table_index]; | |
716 for (size_t i = 0; i < init.entries.size(); i++) { | |
Mircea Trofin
2016/09/19 16:16:26
++i
| |
717 size_t index = i + init.offset.val.i32_const; | |
718 if (index < table.values.size()) { | |
719 table.values[index] = init.entries[i]; | |
720 } | |
721 } | |
722 } | |
723 } | |
724 | |
547 // Verifies the body (code) of a given function. | 725 // Verifies the body (code) of a given function. |
548 void VerifyFunctionBody(uint32_t func_num, ModuleEnv* menv, | 726 void VerifyFunctionBody(uint32_t func_num, ModuleEnv* menv, |
549 WasmFunction* function) { | 727 WasmFunction* function) { |
550 if (FLAG_trace_wasm_decoder || FLAG_trace_wasm_decode_time) { | 728 if (FLAG_trace_wasm_decoder || FLAG_trace_wasm_decode_time) { |
551 OFStream os(stdout); | 729 OFStream os(stdout); |
552 os << "Verifying WASM function " << WasmFunctionName(function, menv) | 730 os << "Verifying WASM function " << WasmFunctionName(function, menv) |
553 << std::endl; | 731 << std::endl; |
554 } | 732 } |
555 FunctionBody body = {menv, function->sig, start_, | 733 FunctionBody body = {menv, function->sig, start_, |
556 start_ + function->code_start_offset, | 734 start_ + function->code_start_offset, |
(...skipping 10 matching lines...) Expand all Loading... | |
567 char* buffer = new char[len]; | 745 char* buffer = new char[len]; |
568 strncpy(buffer, raw, len); | 746 strncpy(buffer, raw, len); |
569 buffer[len - 1] = 0; | 747 buffer[len - 1] = 0; |
570 | 748 |
571 // Copy error code and location. | 749 // Copy error code and location. |
572 result_.MoveFrom(result); | 750 result_.MoveFrom(result); |
573 result_.error_msg.reset(buffer); | 751 result_.error_msg.reset(buffer); |
574 } | 752 } |
575 } | 753 } |
576 | 754 |
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 | 755 // 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. | 756 // the offset of the string, and the length as an out parameter. |
589 uint32_t consume_string(uint32_t* length, bool validate_utf8) { | 757 uint32_t consume_string(uint32_t* length, bool validate_utf8) { |
590 *length = consume_u32v("string length"); | 758 *length = consume_u32v("string length"); |
591 uint32_t offset = pc_offset(); | 759 uint32_t offset = pc_offset(); |
592 TRACE(" +%u %-20s: (%u bytes)\n", offset, "string", *length); | |
593 const byte* string_start = pc_; | 760 const byte* string_start = pc_; |
594 // Consume bytes before validation to guarantee that the string is not oob. | 761 // Consume bytes before validation to guarantee that the string is not oob. |
595 consume_bytes(*length); | 762 consume_bytes(*length, "string"); |
596 if (ok() && validate_utf8 && | 763 if (ok() && validate_utf8 && |
597 !unibrow::Utf8::Validate(string_start, *length)) { | 764 !unibrow::Utf8::Validate(string_start, *length)) { |
598 error(string_start, "no valid UTF-8 string"); | 765 error(string_start, "no valid UTF-8 string"); |
599 } | 766 } |
600 return offset; | 767 return offset; |
601 } | 768 } |
602 | 769 |
603 uint32_t consume_sig_index(WasmModule* module, FunctionSig** sig) { | 770 uint32_t consume_sig_index(WasmModule* module, FunctionSig** sig) { |
604 const byte* pos = pc_; | 771 const byte* pos = pc_; |
605 uint32_t sig_index = consume_u32v("signature index"); | 772 uint32_t sig_index = consume_u32v("signature index"); |
606 if (sig_index >= module->signatures.size()) { | 773 if (sig_index >= module->signatures.size()) { |
607 error(pos, pos, "signature index %u out of bounds (%d signatures)", | 774 error(pos, pos, "signature index %u out of bounds (%d signatures)", |
608 sig_index, static_cast<int>(module->signatures.size())); | 775 sig_index, static_cast<int>(module->signatures.size())); |
609 *sig = nullptr; | 776 *sig = nullptr; |
610 return 0; | 777 return 0; |
611 } | 778 } |
612 *sig = module->signatures[sig_index]; | 779 *sig = module->signatures[sig_index]; |
613 return sig_index; | 780 return sig_index; |
614 } | 781 } |
615 | 782 |
616 uint32_t consume_func_index(WasmModule* module, WasmFunction** func) { | 783 uint32_t consume_func_index(WasmModule* module, WasmFunction** func) { |
784 return consume_index("function index", module->functions, func); | |
785 } | |
786 | |
787 uint32_t consume_global_index(WasmModule* module, WasmGlobal** global) { | |
788 return consume_index("global index", module->globals, global); | |
789 } | |
790 | |
791 uint32_t consume_table_index(WasmModule* module, | |
792 WasmIndirectFunctionTable** table) { | |
793 return consume_index("table index", module->function_tables, table); | |
794 } | |
795 | |
796 template <typename T> | |
797 uint32_t consume_index(const char* name, std::vector<T>& vector, T** ptr) { | |
617 const byte* pos = pc_; | 798 const byte* pos = pc_; |
618 uint32_t func_index = consume_u32v("function index"); | 799 uint32_t index = consume_u32v(name); |
619 if (func_index >= module->functions.size()) { | 800 if (index >= vector.size()) { |
620 error(pos, pos, "function index %u out of bounds (%d functions)", | 801 error(pos, pos, "%s %u out of bounds (%d entries)", name, index, |
621 func_index, static_cast<int>(module->functions.size())); | 802 static_cast<int>(vector.size())); |
622 *func = nullptr; | 803 *ptr = nullptr; |
623 return 0; | 804 return 0; |
624 } | 805 } |
625 *func = &module->functions[func_index]; | 806 *ptr = &vector[index]; |
626 return func_index; | 807 return index; |
808 } | |
809 | |
810 void consume_resizable_limits(const char* name, const char* units, | |
811 uint32_t max_value, uint32_t* initial, | |
812 uint32_t* maximum) { | |
813 uint32_t flags = consume_u32v("resizable limits flags"); | |
814 const byte* pos = pc(); | |
815 *initial = consume_u32v("initial size"); | |
816 if (*initial > max_value) { | |
817 error(pos, pos, | |
818 "initial %s size (%u %s) is larger than maximum allowable (%u)", | |
819 name, *initial, units, max_value); | |
820 } | |
821 if (flags & 1) { | |
822 pos = pc(); | |
823 *maximum = consume_u32v("maximum size"); | |
824 if (*maximum > max_value) { | |
825 error(pos, pos, | |
826 "maximum %s size (%u %s) is larger than maximum allowable (%u)", | |
827 name, *maximum, units, max_value); | |
828 } | |
829 if (*maximum < *initial) { | |
830 error(pos, pos, "maximum %s size (%u %s) is less than initial (%u %s)", | |
831 name, *maximum, units, *initial, units); | |
832 } | |
833 } else { | |
834 *maximum = 0; | |
835 } | |
836 } | |
837 | |
838 bool expect_u8(const char* name, uint8_t expected) { | |
839 const byte* pos = pc(); | |
840 uint8_t value = consume_u8(name); | |
841 if (value != expected) { | |
842 error(pos, pos, "expected %s 0x%02x, got 0x%02x", name, expected, value); | |
843 return false; | |
844 } | |
845 return true; | |
846 } | |
847 | |
848 WasmInitExpr consume_init_expr(WasmModule* module, LocalType expected) { | |
849 const byte* pos = pc(); | |
850 uint8_t opcode = consume_u8("opcode"); | |
851 WasmInitExpr expr; | |
Mircea Trofin
2016/09/19 16:16:26
I'm assuming this is initialized here (I was tryin
titzer
2016/09/21 08:58:44
No, it's initialized below in all the cases.
If I
| |
852 unsigned len = 0; | |
853 switch (opcode) { | |
854 case kExprGetGlobal: { | |
855 GlobalIndexOperand operand(this, pc() - 1); | |
856 expr.kind = WasmInitExpr::kGlobalIndex; | |
857 expr.val.global_index = operand.index; | |
858 len = operand.length; | |
859 break; | |
860 } | |
861 case kExprI32Const: { | |
862 ImmI32Operand operand(this, pc() - 1); | |
863 expr.kind = WasmInitExpr::kI32Const; | |
864 expr.val.i32_const = operand.value; | |
865 len = operand.length; | |
866 break; | |
867 } | |
868 case kExprF32Const: { | |
869 ImmF32Operand operand(this, pc() - 1); | |
870 expr.kind = WasmInitExpr::kF32Const; | |
871 expr.val.f32_const = operand.value; | |
872 len = operand.length; | |
873 break; | |
874 } | |
875 case kExprI64Const: { | |
876 ImmI64Operand operand(this, pc() - 1); | |
877 expr.kind = WasmInitExpr::kI64Const; | |
878 expr.val.i64_const = operand.value; | |
879 len = operand.length; | |
880 break; | |
881 } | |
882 case kExprF64Const: { | |
883 ImmF64Operand operand(this, pc() - 1); | |
884 expr.kind = WasmInitExpr::kF64Const; | |
885 expr.val.f64_const = operand.value; | |
886 len = operand.length; | |
887 break; | |
888 } | |
889 default: { | |
890 error("invalid opcode in initialization expression"); | |
891 expr.kind = WasmInitExpr::kNone; | |
892 expr.val.i32_const = 0; | |
893 } | |
894 } | |
895 consume_bytes(len, "init code"); | |
896 if (!expect_u8("end opcode", kExprEnd)) { | |
897 expr.kind = WasmInitExpr::kNone; | |
898 } | |
899 if (expected != kAstStmt && TypeOf(module, expr) != kAstI32) { | |
900 error(pos, pos, "type error in init expression, expected %s, got %s", | |
901 WasmOpcodes::TypeName(expected), | |
902 WasmOpcodes::TypeName(TypeOf(module, expr))); | |
903 } | |
904 return expr; | |
627 } | 905 } |
628 | 906 |
629 // Reads a single 8-bit integer, interpreting it as a local type. | 907 // Reads a single 8-bit integer, interpreting it as a local type. |
630 LocalType consume_local_type() { | 908 LocalType consume_value_type() { |
631 byte val = consume_u8("local type"); | 909 byte val = consume_u8("value type"); |
632 LocalTypeCode t = static_cast<LocalTypeCode>(val); | 910 LocalTypeCode t = static_cast<LocalTypeCode>(val); |
633 switch (t) { | 911 switch (t) { |
634 case kLocalVoid: | |
635 return kAstStmt; | |
636 case kLocalI32: | 912 case kLocalI32: |
637 return kAstI32; | 913 return kAstI32; |
638 case kLocalI64: | 914 case kLocalI64: |
639 return kAstI64; | 915 return kAstI64; |
640 case kLocalF32: | 916 case kLocalF32: |
641 return kAstF32; | 917 return kAstF32; |
642 case kLocalF64: | 918 case kLocalF64: |
643 return kAstF64; | 919 return kAstF64; |
644 case kLocalS128: | 920 case kLocalS128: |
645 return kAstS128; | 921 return kAstS128; |
646 default: | 922 default: |
647 error(pc_ - 1, "invalid local type"); | 923 error(pc_ - 1, "invalid local type"); |
648 return kAstStmt; | 924 return kAstStmt; |
649 } | 925 } |
650 } | 926 } |
651 | 927 |
652 // Parses a type entry, which is currently limited to functions only. | 928 // Parses a type entry, which is currently limited to functions only. |
653 FunctionSig* consume_sig() { | 929 FunctionSig* consume_sig() { |
654 const byte* pos = pc_; | 930 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 | 931 // parse parameter types |
662 uint32_t param_count = consume_u32v("param count"); | 932 uint32_t param_count = consume_u32v("param count"); |
663 std::vector<LocalType> params; | 933 std::vector<LocalType> params; |
664 for (uint32_t i = 0; i < param_count; ++i) { | 934 for (uint32_t i = 0; i < param_count; ++i) { |
665 LocalType param = consume_local_type(); | 935 LocalType param = consume_value_type(); |
666 if (param == kAstStmt) error(pc_ - 1, "invalid void parameter type"); | |
667 params.push_back(param); | 936 params.push_back(param); |
668 } | 937 } |
669 | 938 |
670 // parse return types | 939 // parse return types |
671 const byte* pt = pc_; | 940 const byte* pt = pc_; |
672 uint32_t return_count = consume_u32v("return count"); | 941 uint32_t return_count = consume_u32v("return count"); |
673 if (return_count > kMaxReturnCount) { | 942 if (return_count > kMaxReturnCount) { |
674 error(pt, pt, "return count of %u exceeds maximum of %u", return_count, | 943 error(pt, pt, "return count of %u exceeds maximum of %u", return_count, |
675 kMaxReturnCount); | 944 kMaxReturnCount); |
676 return nullptr; | 945 return nullptr; |
677 } | 946 } |
678 std::vector<LocalType> returns; | 947 std::vector<LocalType> returns; |
679 for (uint32_t i = 0; i < return_count; ++i) { | 948 for (uint32_t i = 0; i < return_count; ++i) { |
680 LocalType ret = consume_local_type(); | 949 LocalType ret = consume_value_type(); |
681 if (ret == kAstStmt) error(pc_ - 1, "invalid void return type"); | |
682 returns.push_back(ret); | 950 returns.push_back(ret); |
683 } | 951 } |
684 | 952 |
685 // FunctionSig stores the return types first. | 953 // FunctionSig stores the return types first. |
686 LocalType* buffer = | 954 LocalType* buffer = |
687 module_zone->NewArray<LocalType>(param_count + return_count); | 955 module_zone->NewArray<LocalType>(param_count + return_count); |
688 uint32_t b = 0; | 956 uint32_t b = 0; |
689 for (uint32_t i = 0; i < return_count; ++i) buffer[b++] = returns[i]; | 957 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]; | 958 for (uint32_t i = 0; i < param_count; ++i) buffer[b++] = params[i]; |
691 | 959 |
(...skipping 21 matching lines...) Expand all Loading... | |
713 error_code = kError; | 981 error_code = kError; |
714 size_t len = strlen(msg) + 1; | 982 size_t len = strlen(msg) + 1; |
715 char* result = new char[len]; | 983 char* result = new char[len]; |
716 strncpy(result, msg, len); | 984 strncpy(result, msg, len); |
717 result[len - 1] = 0; | 985 result[len - 1] = 0; |
718 error_msg.reset(result); | 986 error_msg.reset(result); |
719 } | 987 } |
720 }; | 988 }; |
721 | 989 |
722 Vector<const byte> FindSection(const byte* module_start, const byte* module_end, | 990 Vector<const byte> FindSection(const byte* module_start, const byte* module_end, |
723 WasmSection::Code code) { | 991 WasmSectionCode code) { |
724 Decoder decoder(module_start, module_end); | 992 Decoder decoder(module_start, module_end); |
725 | 993 |
726 uint32_t magic_word = decoder.consume_u32("wasm magic"); | 994 uint32_t magic_word = decoder.consume_u32("wasm magic"); |
727 if (magic_word != kWasmMagic) decoder.error("wrong magic word"); | 995 if (magic_word != kWasmMagic) decoder.error("wrong magic word"); |
728 | 996 |
729 uint32_t magic_version = decoder.consume_u32("wasm version"); | 997 uint32_t magic_version = decoder.consume_u32("wasm version"); |
730 if (magic_version != kWasmVersion) decoder.error("wrong wasm version"); | 998 if (magic_version != kWasmVersion) decoder.error("wrong wasm version"); |
731 | 999 |
732 while (decoder.more() && decoder.ok()) { | 1000 WasmSectionIterator section_iter(decoder); |
733 // Read the section name. | 1001 while (section_iter.more()) { |
734 uint32_t string_length = decoder.consume_u32v("section name length"); | 1002 if (section_iter.section_code() == code) { |
735 const byte* section_name_start = decoder.pc(); | 1003 return Vector<const uint8_t>(section_iter.section_start(), |
736 decoder.consume_bytes(string_length); | 1004 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 } | 1005 } |
1006 decoder.consume_bytes(section_iter.section_length(), "section payload"); | |
1007 section_iter.advance(); | |
750 } | 1008 } |
751 | 1009 |
752 return Vector<const uint8_t>(); | 1010 return Vector<const uint8_t>(); |
753 } | 1011 } |
754 | 1012 |
755 } // namespace | 1013 } // namespace |
756 | 1014 |
757 ModuleResult DecodeWasmModule(Isolate* isolate, Zone* zone, | 1015 ModuleResult DecodeWasmModule(Isolate* isolate, Zone* zone, |
758 const byte* module_start, const byte* module_end, | 1016 const byte* module_start, const byte* module_end, |
759 bool verify_functions, ModuleOrigin origin) { | 1017 bool verify_functions, ModuleOrigin origin) { |
(...skipping 14 matching lines...) Expand all Loading... | |
774 static_cast<int>(zone->allocation_size() - decode_memory_start)); | 1032 static_cast<int>(zone->allocation_size() - decode_memory_start)); |
775 return result; | 1033 return result; |
776 } | 1034 } |
777 | 1035 |
778 FunctionSig* DecodeWasmSignatureForTesting(Zone* zone, const byte* start, | 1036 FunctionSig* DecodeWasmSignatureForTesting(Zone* zone, const byte* start, |
779 const byte* end) { | 1037 const byte* end) { |
780 ModuleDecoder decoder(zone, start, end, kWasmOrigin); | 1038 ModuleDecoder decoder(zone, start, end, kWasmOrigin); |
781 return decoder.DecodeFunctionSignature(start); | 1039 return decoder.DecodeFunctionSignature(start); |
782 } | 1040 } |
783 | 1041 |
1042 WasmInitExpr DecodeWasmInitExprForTesting(const byte* start, const byte* end) { | |
1043 base::AccountingAllocator allocator; | |
1044 Zone zone(&allocator); | |
1045 ModuleDecoder decoder(&zone, start, end, kWasmOrigin); | |
1046 return decoder.DecodeInitExpr(start); | |
1047 } | |
1048 | |
784 FunctionResult DecodeWasmFunction(Isolate* isolate, Zone* zone, | 1049 FunctionResult DecodeWasmFunction(Isolate* isolate, Zone* zone, |
785 ModuleEnv* module_env, | 1050 ModuleEnv* module_env, |
786 const byte* function_start, | 1051 const byte* function_start, |
787 const byte* function_end) { | 1052 const byte* function_end) { |
788 HistogramTimerScope wasm_decode_function_time_scope( | 1053 HistogramTimerScope wasm_decode_function_time_scope( |
789 isolate->counters()->wasm_decode_function_time()); | 1054 isolate->counters()->wasm_decode_function_time()); |
790 size_t size = function_end - function_start; | 1055 size_t size = function_end - function_start; |
791 if (function_start > function_end) return FunctionError("start > end"); | 1056 if (function_start > function_end) return FunctionError("start > end"); |
792 if (size > kMaxFunctionSize) | 1057 if (size > kMaxFunctionSize) |
793 return FunctionError("size > maximum function size"); | 1058 return FunctionError("size > maximum function size"); |
794 isolate->counters()->wasm_function_size_bytes()->AddSample( | 1059 isolate->counters()->wasm_function_size_bytes()->AddSample( |
795 static_cast<int>(size)); | 1060 static_cast<int>(size)); |
796 WasmFunction* function = new WasmFunction(); | 1061 WasmFunction* function = new WasmFunction(); |
797 ModuleDecoder decoder(zone, function_start, function_end, kWasmOrigin); | 1062 ModuleDecoder decoder(zone, function_start, function_end, kWasmOrigin); |
798 return decoder.DecodeSingleFunction(module_env, function); | 1063 return decoder.DecodeSingleFunction(module_env, function); |
799 } | 1064 } |
800 | 1065 |
801 FunctionOffsetsResult DecodeWasmFunctionOffsets(const byte* module_start, | 1066 FunctionOffsetsResult DecodeWasmFunctionOffsets( |
802 const byte* module_end) { | 1067 const byte* module_start, const byte* module_end, |
1068 uint32_t num_imported_functions) { | |
1069 // Find and decode the code section. | |
803 Vector<const byte> code_section = | 1070 Vector<const byte> code_section = |
804 FindSection(module_start, module_end, WasmSection::Code::FunctionBodies); | 1071 FindSection(module_start, module_end, kCodeSectionCode); |
805 Decoder decoder(code_section.start(), code_section.end()); | 1072 Decoder decoder(code_section.start(), code_section.end()); |
806 if (!code_section.start()) decoder.error("no code section"); | 1073 FunctionOffsets table; |
Mircea Trofin
2016/09/19 16:16:25
I'm assuming FunctionOffsets has a default constru
titzer
2016/09/21 08:58:44
It's a typedef for std::vector, so yes.
| |
1074 if (!code_section.start()) { | |
1075 decoder.error("no code section"); | |
1076 return decoder.toResult(std::move(table)); | |
1077 } | |
1078 | |
1079 // Reserve entries for the imported functions. | |
1080 table.reserve(num_imported_functions); | |
1081 for (uint32_t i = 0; i < num_imported_functions; i++) { | |
Mircea Trofin
2016/09/19 16:16:25
++i
| |
1082 table.push_back(std::make_pair(0, 0)); | |
1083 } | |
807 | 1084 |
808 uint32_t functions_count = decoder.consume_u32v("functions count"); | 1085 uint32_t functions_count = decoder.consume_u32v("functions count"); |
809 FunctionOffsets table; | |
810 // Take care of invalid input here. | 1086 // Take care of invalid input here. |
811 if (functions_count < static_cast<unsigned>(code_section.length()) / 2) | 1087 if (functions_count < static_cast<unsigned>(code_section.length()) / 2) |
812 table.reserve(functions_count); | 1088 table.reserve(functions_count); |
813 int section_offset = static_cast<int>(code_section.start() - module_start); | 1089 int section_offset = static_cast<int>(code_section.start() - module_start); |
814 DCHECK_LE(0, section_offset); | 1090 DCHECK_LE(0, section_offset); |
815 for (uint32_t i = 0; i < functions_count && decoder.ok(); ++i) { | 1091 for (uint32_t i = 0; i < functions_count && decoder.ok(); ++i) { |
816 uint32_t size = decoder.consume_u32v("body size"); | 1092 uint32_t size = decoder.consume_u32v("body size"); |
817 int offset = static_cast<int>(section_offset + decoder.pc_offset()); | 1093 int offset = static_cast<int>(section_offset + decoder.pc_offset()); |
818 table.push_back(std::make_pair(offset, static_cast<int>(size))); | 1094 table.push_back(std::make_pair(offset, static_cast<int>(size))); |
819 DCHECK(table.back().first >= 0 && table.back().second >= 0); | 1095 DCHECK(table.back().first >= 0 && table.back().second >= 0); |
820 decoder.consume_bytes(size); | 1096 decoder.consume_bytes(size); |
821 } | 1097 } |
822 if (decoder.more()) decoder.error("unexpected additional bytes"); | 1098 if (decoder.more()) decoder.error("unexpected additional bytes"); |
823 | 1099 |
824 return decoder.toResult(std::move(table)); | 1100 return decoder.toResult(std::move(table)); |
825 } | 1101 } |
826 | 1102 |
827 } // namespace wasm | 1103 } // namespace wasm |
828 } // namespace internal | 1104 } // namespace internal |
829 } // namespace v8 | 1105 } // namespace v8 |
OLD | NEW |