Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: src/wasm/module-decoder.cc

Issue 2014533003: [wasm] Refactor encoder.h to use a proper buffer and remove OldFunctions section. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 int string_leb_length = 0; 111 int string_leb_length = 0;
112 uint32_t string_length = 112 uint32_t string_length =
113 consume_u32v(&string_leb_length, "section name length"); 113 consume_u32v(&string_leb_length, "section name length");
114 const byte* section_name_start = pc_; 114 const byte* section_name_start = pc_;
115 consume_bytes(string_length); 115 consume_bytes(string_length);
116 if (failed()) { 116 if (failed()) {
117 TRACE("Section name of length %u couldn't be read\n", string_length); 117 TRACE("Section name of length %u couldn't be read\n", string_length);
118 break; 118 break;
119 } 119 }
120 120
121 TRACE(" +%d section name : \"%.*s\"\n",
122 static_cast<int>(section_name_start - start_),
123 string_length < 20 ? string_length : 20, section_name_start);
124
121 WasmSection::Code section = 125 WasmSection::Code section =
122 WasmSection::lookup(section_name_start, string_length); 126 WasmSection::lookup(section_name_start, string_length);
123 127
124 // Read and check the section size. 128 // Read and check the section size.
125 int section_leb_length = 0; 129 int section_leb_length = 0;
126 uint32_t section_length = 130 uint32_t section_length =
127 consume_u32v(&section_leb_length, "section length"); 131 consume_u32v(&section_leb_length, "section length");
128 if (!checkAvailable(section_length)) { 132 if (!checkAvailable(section_length)) {
129 // The section would extend beyond the end of the module. 133 // The section would extend beyond the end of the module.
130 break; 134 break;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 int length; 168 int length;
165 uint32_t functions_count = consume_u32v(&length, "functions count"); 169 uint32_t functions_count = consume_u32v(&length, "functions count");
166 module->functions.reserve(SafeReserve(functions_count)); 170 module->functions.reserve(SafeReserve(functions_count));
167 for (uint32_t i = 0; i < functions_count; i++) { 171 for (uint32_t i = 0; i < functions_count; i++) {
168 module->functions.push_back({nullptr, // sig 172 module->functions.push_back({nullptr, // sig
169 i, // func_index 173 i, // func_index
170 0, // sig_index 174 0, // sig_index
171 0, // name_offset 175 0, // name_offset
172 0, // name_length 176 0, // name_length
173 0, // code_start_offset 177 0, // code_start_offset
174 0, // code_end_offset 178 0}); // code_end_offset
175 false}); // exported
176 WasmFunction* function = &module->functions.back(); 179 WasmFunction* function = &module->functions.back();
177 function->sig_index = consume_sig_index(module, &function->sig); 180 function->sig_index = consume_sig_index(module, &function->sig);
178 } 181 }
179 break; 182 break;
180 } 183 }
181 case WasmSection::Code::FunctionBodies: { 184 case WasmSection::Code::FunctionBodies: {
182 int length; 185 int length;
183 const byte* pos = pc_; 186 const byte* pos = pc_;
184 uint32_t functions_count = consume_u32v(&length, "functions count"); 187 uint32_t functions_count = consume_u32v(&length, "functions count");
185 if (functions_count != module->functions.size()) { 188 if (functions_count != module->functions.size()) {
(...skipping 11 matching lines...) Expand all
197 200
198 TRACE(" +%d %-20s: (%d bytes)\n", pc_offset(), "function body", 201 TRACE(" +%d %-20s: (%d bytes)\n", pc_offset(), "function body",
199 size); 202 size);
200 pc_ += size; 203 pc_ += size;
201 if (pc_ > limit_) { 204 if (pc_ > limit_) {
202 error(pc_, "function body extends beyond end of file"); 205 error(pc_, "function body extends beyond end of file");
203 } 206 }
204 } 207 }
205 break; 208 break;
206 } 209 }
207 case WasmSection::Code::OldFunctions: {
208 int length;
209 uint32_t functions_count = consume_u32v(&length, "functions count");
210 module->functions.reserve(SafeReserve(functions_count));
211 // Set up module environment for verification.
212 ModuleEnv menv;
213 menv.module = module;
214 menv.instance = nullptr;
215 menv.origin = origin_;
216 // Decode functions.
217 for (uint32_t i = 0; i < functions_count; i++) {
218 if (failed()) break;
219 TRACE("DecodeFunction[%d] module+%d\n", i,
220 static_cast<int>(pc_ - start_));
221
222 module->functions.push_back({nullptr, // sig
223 i, // func_index
224 0, // sig_index
225 0, // name_offset
226 0, // name_length
227 0, // code_start_offset
228 0, // code_end_offset
229 false}); // exported
230 WasmFunction* function = &module->functions.back();
231 DecodeFunctionInModule(module, function, false);
232 }
233 if (ok() && verify_functions) {
234 for (uint32_t i = 0; i < functions_count; i++) {
235 if (failed()) break;
236 WasmFunction* function = &module->functions[i];
237 VerifyFunctionBody(i, &menv, function);
238 if (result_.failed()) {
239 error(result_.error_pc, result_.error_msg.get());
240 }
241 }
242 }
243 break;
244 }
245 case WasmSection::Code::Names: { 210 case WasmSection::Code::Names: {
246 int length; 211 int length;
247 const byte* pos = pc_; 212 const byte* pos = pc_;
248 uint32_t functions_count = consume_u32v(&length, "functions count"); 213 uint32_t functions_count = consume_u32v(&length, "functions count");
249 if (functions_count != module->functions.size()) { 214 if (functions_count != module->functions.size()) {
250 error(pos, pos, "function name count %u mismatch (%u expected)", 215 error(pos, pos, "function name count %u mismatch (%u expected)",
251 functions_count, 216 functions_count,
252 static_cast<uint32_t>(module->functions.size())); 217 static_cast<uint32_t>(module->functions.size()));
253 break; 218 break;
254 } 219 }
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
457 422
458 // Decodes a single anonymous function starting at {start_}. 423 // Decodes a single anonymous function starting at {start_}.
459 FunctionResult DecodeSingleFunction(ModuleEnv* module_env, 424 FunctionResult DecodeSingleFunction(ModuleEnv* module_env,
460 WasmFunction* function) { 425 WasmFunction* function) {
461 pc_ = start_; 426 pc_ = start_;
462 function->sig = consume_sig(); // read signature 427 function->sig = consume_sig(); // read signature
463 function->name_offset = 0; // ---- name 428 function->name_offset = 0; // ---- name
464 function->name_length = 0; // ---- name length 429 function->name_length = 0; // ---- name length
465 function->code_start_offset = off(pc_); // ---- code start 430 function->code_start_offset = off(pc_); // ---- code start
466 function->code_end_offset = off(limit_); // ---- code end 431 function->code_end_offset = off(limit_); // ---- code end
467 function->exported = false; // ---- exported
468 432
469 if (ok()) VerifyFunctionBody(0, module_env, function); 433 if (ok()) VerifyFunctionBody(0, module_env, function);
470 434
471 FunctionResult result; 435 FunctionResult result;
472 result.CopyFrom(result_); // Copy error code and location. 436 result.CopyFrom(result_); // Copy error code and location.
473 result.val = function; 437 result.val = function;
474 return result; 438 return result;
475 } 439 }
476 440
477 // Decodes a single function signature at {start}. 441 // Decodes a single function signature at {start}.
(...skipping 13 matching lines...) Expand all
491 // Decodes a single global entry inside a module starting at {pc_}. 455 // Decodes a single global entry inside a module starting at {pc_}.
492 void DecodeGlobalInModule(WasmGlobal* global) { 456 void DecodeGlobalInModule(WasmGlobal* global) {
493 global->name_offset = consume_string(&global->name_length, false); 457 global->name_offset = consume_string(&global->name_length, false);
494 DCHECK(unibrow::Utf8::Validate(start_ + global->name_offset, 458 DCHECK(unibrow::Utf8::Validate(start_ + global->name_offset,
495 global->name_length)); 459 global->name_length));
496 global->type = mem_type(); 460 global->type = mem_type();
497 global->offset = 0; 461 global->offset = 0;
498 global->exported = consume_u8("exported") != 0; 462 global->exported = consume_u8("exported") != 0;
499 } 463 }
500 464
501 // Decodes a single function entry inside a module starting at {pc_}.
502 // TODO(titzer): legacy function body; remove
503 void DecodeFunctionInModule(WasmModule* module, WasmFunction* function,
504 bool verify_body = true) {
505 byte decl_bits = consume_u8("function decl");
506
507 const byte* sigpos = pc_;
508 function->sig_index = consume_u16("signature index");
509
510 if (function->sig_index >= module->signatures.size()) {
511 return error(sigpos, "invalid signature index");
512 } else {
513 function->sig = module->signatures[function->sig_index];
514 }
515
516 TRACE(" +%d <function attributes:%s%s>\n", static_cast<int>(pc_ - start_),
517 decl_bits & kDeclFunctionName ? " name" : "",
518 decl_bits & kDeclFunctionExport ? " exported" : "");
519
520 function->exported = decl_bits & kDeclFunctionExport;
521
522 if (decl_bits & kDeclFunctionName) {
523 function->name_offset =
524 consume_string(&function->name_length, function->exported);
525 }
526
527 uint16_t size = consume_u16("body size");
528 if (ok()) {
529 if ((pc_ + size) > limit_) {
530 return error(pc_, limit_,
531 "expected %d bytes for function body, fell off end", size);
532 }
533 function->code_start_offset = static_cast<uint32_t>(pc_ - start_);
534 function->code_end_offset = function->code_start_offset + size;
535 TRACE(" +%d %-20s: (%d bytes)\n", static_cast<int>(pc_ - start_),
536 "function body", size);
537 pc_ += size;
538 }
539 }
540
541 bool IsWithinLimit(uint32_t limit, uint32_t offset, uint32_t size) { 465 bool IsWithinLimit(uint32_t limit, uint32_t offset, uint32_t size) {
542 if (offset > limit) return false; 466 if (offset > limit) return false;
543 if ((offset + size) < offset) return false; // overflow 467 if ((offset + size) < offset) return false; // overflow
544 return (offset + size) <= limit; 468 return (offset + size) <= limit;
545 } 469 }
546 470
547 // Decodes a single data segment entry inside a module starting at {pc_}. 471 // Decodes a single data segment entry inside a module starting at {pc_}.
548 void DecodeDataSegmentInModule(WasmModule* module, WasmDataSegment* segment) { 472 void DecodeDataSegmentInModule(WasmModule* module, WasmDataSegment* segment) {
549 const byte* start = pc_; 473 const byte* start = pc_;
550 int length; 474 int length;
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
832 return FunctionError("size > maximum function size"); 756 return FunctionError("size > maximum function size");
833 isolate->counters()->wasm_function_size_bytes()->AddSample( 757 isolate->counters()->wasm_function_size_bytes()->AddSample(
834 static_cast<int>(size)); 758 static_cast<int>(size));
835 WasmFunction* function = new WasmFunction(); 759 WasmFunction* function = new WasmFunction();
836 ModuleDecoder decoder(zone, function_start, function_end, kWasmOrigin); 760 ModuleDecoder decoder(zone, function_start, function_end, kWasmOrigin);
837 return decoder.DecodeSingleFunction(module_env, function); 761 return decoder.DecodeSingleFunction(module_env, function);
838 } 762 }
839 } // namespace wasm 763 } // namespace wasm
840 } // namespace internal 764 } // namespace internal
841 } // namespace v8 765 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698