Chromium Code Reviews| 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/macro-assembler.h" | 5 #include "src/macro-assembler.h" |
| 6 #include "src/objects.h" | 6 #include "src/objects.h" |
| 7 #include "src/v8.h" | 7 #include "src/v8.h" |
| 8 | 8 |
| 9 #include "src/wasm/decoder.h" | 9 #include "src/wasm/decoder.h" |
| 10 #include "src/wasm/module-decoder.h" | 10 #include "src/wasm/module-decoder.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 error(pos, pos, | 71 error(pos, pos, |
| 72 "expected version %02x %02x %02x %02x, " | 72 "expected version %02x %02x %02x %02x, " |
| 73 "found %02x %02x %02x %02x", | 73 "found %02x %02x %02x %02x", |
| 74 BYTES(kWasmVersion), BYTES(magic_version)); | 74 BYTES(kWasmVersion), BYTES(magic_version)); |
| 75 return toResult(module); | 75 return toResult(module); |
| 76 } | 76 } |
| 77 | 77 |
| 78 // Decode the module sections. | 78 // Decode the module sections. |
| 79 while (pc_ < limit_) { | 79 while (pc_ < limit_) { |
| 80 TRACE("DecodeSection\n"); | 80 TRACE("DecodeSection\n"); |
| 81 uint8_t section_u8 = consume_u8("section"); | 81 int length; |
| 82 uint32_t section_bytes = consume_u32v(&length, "section size"); | |
|
titzer
2016/03/07 08:45:33
Should the section size also include the string si
JF
2016/03/09 02:38:57
Agreed, fixed.
| |
| 82 | 83 |
| 83 if (section_u8 >= kMaxModuleSectionCode) { | 84 WasmSectionDeclCode section = consume_section_name(); |
| 85 if (section == kMaxModuleSectionCode) { | |
| 84 // Skip unknown section. | 86 // Skip unknown section. |
| 85 int length; | |
| 86 uint32_t section_bytes = consume_u32v(&length, "section size"); | |
| 87 consume_bytes(section_bytes); | 87 consume_bytes(section_bytes); |
| 88 continue; | 88 continue; |
| 89 } | 89 } |
| 90 | 90 |
| 91 // Each section should appear at most once. | 91 // Each section should appear at most once. |
| 92 auto section = static_cast<WasmSectionDeclCode>(section_u8); | |
| 93 CheckForPreviousSection(sections, section, false); | 92 CheckForPreviousSection(sections, section, false); |
| 94 sections[section] = true; | 93 sections[section] = true; |
| 95 | 94 |
| 96 switch (section) { | 95 switch (section) { |
| 97 case kDeclEnd: | 96 case kDeclEnd: |
| 98 // Terminate section decoding. | 97 // Terminate section decoding. |
| 99 limit_ = pc_; | 98 limit_ = pc_; |
| 100 break; | 99 break; |
| 101 case kDeclMemory: | 100 case kDeclMemory: |
| 102 int length; | 101 int length; |
| (...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 514 return offset; | 513 return offset; |
| 515 } | 514 } |
| 516 | 515 |
| 517 // Reads a single 32-bit unsigned integer interpreted as an offset into the | 516 // Reads a single 32-bit unsigned integer interpreted as an offset into the |
| 518 // data and validating the string there and advances. | 517 // data and validating the string there and advances. |
| 519 uint32_t consume_string(const char* name = nullptr) { | 518 uint32_t consume_string(const char* name = nullptr) { |
| 520 // TODO(titzer): validate string | 519 // TODO(titzer): validate string |
| 521 return consume_offset(name ? name : "string"); | 520 return consume_offset(name ? name : "string"); |
| 522 } | 521 } |
| 523 | 522 |
| 523 // Reads a section name. | |
| 524 WasmSectionDeclCode consume_section_name() { | |
| 525 static const char* sections[] = { | |
| 526 #define F(enumerator, string) string, | |
| 527 FOR_EACH_WASM_SECTION_TYPE(F) | |
| 528 #undef F | |
| 529 }; | |
| 530 static uint8_t sizes[] { | |
| 531 #define F(enumerator, string) sizeof(string), | |
| 532 FOR_EACH_WASM_SECTION_TYPE(F) | |
| 533 #undef F | |
| 534 }; | |
| 535 static_assert( | |
| 536 sizeof(sections) / sizeof(sections[0]) == kMaxModuleSectionCode, | |
| 537 "expected enum WasmSectionDeclCode to be monotonic from 0"); | |
| 538 int length; | |
| 539 uint32_t string_bytes = consume_u32v(&length, "section module name"); | |
| 540 const byte* start = pc_; | |
| 541 consume_bytes(string_bytes); | |
| 542 if (failed()) return kMaxModuleSectionCode; | |
| 543 for (size_t i = 0; i != kMaxModuleSectionCode; ++i) { | |
| 544 if (sizes[i] == string_bytes && | |
| 545 0 == memcmp(sections[i], start, string_bytes)) { | |
| 546 return (WasmSectionDeclCode)i; | |
| 547 } | |
| 548 } | |
| 549 return kMaxModuleSectionCode; | |
| 550 } | |
| 551 | |
| 524 // Reads a single 8-bit integer, interpreting it as a local type. | 552 // Reads a single 8-bit integer, interpreting it as a local type. |
| 525 LocalType consume_local_type() { | 553 LocalType consume_local_type() { |
| 526 byte val = consume_u8("local type"); | 554 byte val = consume_u8("local type"); |
| 527 LocalTypeCode t = static_cast<LocalTypeCode>(val); | 555 LocalTypeCode t = static_cast<LocalTypeCode>(val); |
| 528 switch (t) { | 556 switch (t) { |
| 529 case kLocalVoid: | 557 case kLocalVoid: |
| 530 return kAstStmt; | 558 return kAstStmt; |
| 531 case kLocalI32: | 559 case kLocalI32: |
| 532 return kAstI32; | 560 return kAstI32; |
| 533 case kLocalI64: | 561 case kLocalI64: |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 644 if (function_start > function_end) return FunctionError("start > end"); | 672 if (function_start > function_end) return FunctionError("start > end"); |
| 645 if (size > kMaxFunctionSize) | 673 if (size > kMaxFunctionSize) |
| 646 return FunctionError("size > maximum function size"); | 674 return FunctionError("size > maximum function size"); |
| 647 WasmFunction* function = new WasmFunction(); | 675 WasmFunction* function = new WasmFunction(); |
| 648 ModuleDecoder decoder(zone, function_start, function_end, kWasmOrigin); | 676 ModuleDecoder decoder(zone, function_start, function_end, kWasmOrigin); |
| 649 return decoder.DecodeSingleFunction(module_env, function); | 677 return decoder.DecodeSingleFunction(module_env, function); |
| 650 } | 678 } |
| 651 } // namespace wasm | 679 } // namespace wasm |
| 652 } // namespace internal | 680 } // namespace internal |
| 653 } // namespace v8 | 681 } // namespace v8 |
| OLD | NEW |