-
Notifications
You must be signed in to change notification settings - Fork 804
Add declaration limit checks to parser #2736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+35
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -706,6 +706,16 @@ Result WastParser::Synchronize(SynchronizeFunc func) { | |
| return Result::Error; | ||
| } | ||
|
|
||
| Result WastParser::CheckIndexRange(Location& loc, | ||
| size_t size, | ||
| const char* decl) { | ||
| if (size >= kInvalidIndex) { | ||
| Error(loc, "too many %s declarations", decl); | ||
| return Result::Error; | ||
| } | ||
| return Result::Ok; | ||
| } | ||
|
|
||
| void WastParser::ErrorUnlessOpcodeEnabled(const Token& token) { | ||
| Opcode opcode = token.opcode(); | ||
| if (!opcode.IsEnabled(options_->features)) { | ||
|
|
@@ -1506,6 +1516,7 @@ Result WastParser::ParseDataModuleField(Module* module) { | |
| EXPECT(Lpar); | ||
| Location loc = GetLocation(); | ||
| EXPECT(Data); | ||
| CHECK_RESULT(CheckIndexRange(loc, module->data_segments.size(), "data")); | ||
| std::string name; | ||
| CHECK_RESULT(ParseBindVarOpt(&name)); | ||
| auto field = std::make_unique<DataSegmentModuleField>(loc, name); | ||
|
|
@@ -1543,6 +1554,7 @@ Result WastParser::ParseElemModuleField(Module* module) { | |
| EXPECT(Lpar); | ||
| Location loc = GetLocation(); | ||
| EXPECT(Elem); | ||
| CHECK_RESULT(CheckIndexRange(loc, module->elem_segments.size(), "elem")); | ||
|
|
||
| // With MVP text format the name here was intended to refer to the table | ||
| // that the elem segment was part of, but we never did anything with this name | ||
|
|
@@ -1620,7 +1632,7 @@ Result WastParser::ParseTagModuleField(Module* module) { | |
| EXPECT(Lpar); | ||
| EXPECT(Tag); | ||
| Location loc = GetLocation(); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usually the location refers to the field type token, not the token after it. Perhaps this is a mistake. |
||
|
|
||
| CHECK_RESULT(CheckIndexRange(loc, module->tags.size(), "tag")); | ||
| std::string name; | ||
| CHECK_RESULT(ParseBindVarOpt(&name)); | ||
|
|
||
|
|
@@ -1629,6 +1641,7 @@ Result WastParser::ParseTagModuleField(Module* module) { | |
|
|
||
| if (PeekMatchLpar(TokenType::Import)) { | ||
| CheckImportOrdering(module); | ||
| CHECK_RESULT(CheckIndexRange(loc, module->imports.size(), "import")); | ||
| auto import = std::make_unique<TagImport>(name); | ||
| Tag& tag = import->tag; | ||
| CHECK_RESULT(ParseInlineImport(import.get())); | ||
|
|
@@ -1655,7 +1668,9 @@ Result WastParser::ParseExportModuleField(Module* module) { | |
| WABT_TRACE(ParseExportModuleField); | ||
| EXPECT(Lpar); | ||
| auto field = std::make_unique<ExportModuleField>(GetLocation()); | ||
| Location loc = GetLocation(); | ||
| EXPECT(Export); | ||
| CHECK_RESULT(CheckIndexRange(loc, module->exports.size(), "export")); | ||
| CHECK_RESULT(ParseQuotedText(&field->export_.name)); | ||
| CHECK_RESULT(ParseExportDesc(&field->export_)); | ||
| EXPECT(Rpar); | ||
|
|
@@ -1668,6 +1683,7 @@ Result WastParser::ParseFuncModuleField(Module* module) { | |
| EXPECT(Lpar); | ||
| Location loc = GetLocation(); | ||
| EXPECT(Func); | ||
| CHECK_RESULT(CheckIndexRange(loc, module->funcs.size(), "func")); | ||
| std::string name; | ||
| CHECK_RESULT(ParseBindVarOpt(&name)); | ||
|
|
||
|
|
@@ -1676,6 +1692,7 @@ Result WastParser::ParseFuncModuleField(Module* module) { | |
|
|
||
| if (PeekMatchLpar(TokenType::Import)) { | ||
| CheckImportOrdering(module); | ||
| CHECK_RESULT(CheckIndexRange(loc, module->imports.size(), "import")); | ||
| auto import = std::make_unique<FuncImport>(name); | ||
| Func& func = import->func; | ||
| CHECK_RESULT(ParseInlineImport(import.get())); | ||
|
|
@@ -1725,6 +1742,7 @@ Result WastParser::ParseTypeModuleField(Module* module) { | |
| CHECK_RESULT(ParseBindVarOpt(&name)); | ||
| EXPECT(Lpar); | ||
| Location loc = GetLocation(); | ||
| CHECK_RESULT(CheckIndexRange(loc, module->types.size(), "type")); | ||
|
|
||
| if (Match(TokenType::Func)) { | ||
| auto func_type = std::make_unique<FuncType>(name); | ||
|
|
@@ -1802,6 +1820,7 @@ Result WastParser::ParseGlobalModuleField(Module* module) { | |
| EXPECT(Lpar); | ||
| Location loc = GetLocation(); | ||
| EXPECT(Global); | ||
| CHECK_RESULT(CheckIndexRange(loc, module->globals.size(), "global")); | ||
| std::string name; | ||
| CHECK_RESULT(ParseBindVarOpt(&name)); | ||
|
|
||
|
|
@@ -1810,6 +1829,7 @@ Result WastParser::ParseGlobalModuleField(Module* module) { | |
|
|
||
| if (PeekMatchLpar(TokenType::Import)) { | ||
| CheckImportOrdering(module); | ||
| CHECK_RESULT(CheckIndexRange(loc, module->imports.size(), "import")); | ||
| auto import = std::make_unique<GlobalImport>(name); | ||
| CHECK_RESULT(ParseInlineImport(import.get())); | ||
| CHECK_RESULT(ParseGlobalType(&import->global)); | ||
|
|
@@ -1835,6 +1855,7 @@ Result WastParser::ParseImportModuleField(Module* module) { | |
| Location loc = GetLocation(); | ||
| CheckImportOrdering(module); | ||
| EXPECT(Import); | ||
| CHECK_RESULT(CheckIndexRange(loc, module->imports.size(), "import")); | ||
| std::string module_name; | ||
| std::string field_name; | ||
| CHECK_RESULT(ParseQuotedText(&module_name)); | ||
|
|
@@ -1846,6 +1867,7 @@ Result WastParser::ParseImportModuleField(Module* module) { | |
|
|
||
| switch (Peek()) { | ||
| case TokenType::Func: { | ||
| CHECK_RESULT(CheckIndexRange(loc, module->funcs.size(), "func")); | ||
| DropToken(); | ||
| CHECK_RESULT(ParseBindVarOpt(&name)); | ||
| auto import = std::make_unique<FuncImport>(name); | ||
|
|
@@ -1859,6 +1881,7 @@ Result WastParser::ParseImportModuleField(Module* module) { | |
| } | ||
|
|
||
| case TokenType::Table: { | ||
| CHECK_RESULT(CheckIndexRange(loc, module->tables.size(), "table")); | ||
| DropToken(); | ||
| CHECK_RESULT(ParseBindVarOpt(&name)); | ||
| auto import = std::make_unique<TableImport>(name); | ||
|
|
@@ -1873,6 +1896,7 @@ Result WastParser::ParseImportModuleField(Module* module) { | |
| } | ||
|
|
||
| case TokenType::Memory: { | ||
| CHECK_RESULT(CheckIndexRange(loc, module->memories.size(), "memory")); | ||
| DropToken(); | ||
| CHECK_RESULT(ParseBindVarOpt(&name)); | ||
| auto import = std::make_unique<MemoryImport>(name); | ||
|
|
@@ -1886,6 +1910,7 @@ Result WastParser::ParseImportModuleField(Module* module) { | |
| } | ||
|
|
||
| case TokenType::Global: { | ||
| CHECK_RESULT(CheckIndexRange(loc, module->globals.size(), "global")); | ||
| DropToken(); | ||
| CHECK_RESULT(ParseBindVarOpt(&name)); | ||
| auto import = std::make_unique<GlobalImport>(name); | ||
|
|
@@ -1896,6 +1921,7 @@ Result WastParser::ParseImportModuleField(Module* module) { | |
| } | ||
|
|
||
| case TokenType::Tag: { | ||
| CHECK_RESULT(CheckIndexRange(loc, module->tags.size(), "tag")); | ||
| DropToken(); | ||
| CHECK_RESULT(ParseBindVarOpt(&name)); | ||
| auto import = std::make_unique<TagImport>(name); | ||
|
|
@@ -1923,6 +1949,7 @@ Result WastParser::ParseMemoryModuleField(Module* module) { | |
| EXPECT(Lpar); | ||
| Location loc = GetLocation(); | ||
| EXPECT(Memory); | ||
| CHECK_RESULT(CheckIndexRange(loc, module->memories.size(), "memory")); | ||
| std::string name; | ||
| CHECK_RESULT(ParseBindVarOpt(&name)); | ||
|
|
||
|
|
@@ -1931,6 +1958,7 @@ Result WastParser::ParseMemoryModuleField(Module* module) { | |
|
|
||
| if (PeekMatchLpar(TokenType::Import)) { | ||
| CheckImportOrdering(module); | ||
| CHECK_RESULT(CheckIndexRange(loc, module->imports.size(), "import")); | ||
| auto import = std::make_unique<MemoryImport>(name); | ||
| import->memory.page_size = WABT_DEFAULT_PAGE_SIZE; | ||
| CHECK_RESULT(ParseInlineImport(import.get())); | ||
|
|
@@ -2004,6 +2032,7 @@ Result WastParser::ParseTableModuleField(Module* module) { | |
| EXPECT(Lpar); | ||
| Location loc = GetLocation(); | ||
| EXPECT(Table); | ||
| CHECK_RESULT(CheckIndexRange(loc, module->tables.size(), "table")); | ||
| std::string name; | ||
| CHECK_RESULT(ParseBindVarOpt(&name)); | ||
|
|
||
|
|
@@ -2012,6 +2041,7 @@ Result WastParser::ParseTableModuleField(Module* module) { | |
|
|
||
| if (PeekMatchLpar(TokenType::Import)) { | ||
| CheckImportOrdering(module); | ||
| CHECK_RESULT(CheckIndexRange(loc, module->imports.size(), "import")); | ||
| auto import = std::make_unique<TableImport>(name); | ||
| CHECK_RESULT(ParseInlineImport(import.get())); | ||
| CHECK_RESULT(ParseLimitsIndex(&import->table.elem_limits)); | ||
|
|
@@ -2032,6 +2062,7 @@ Result WastParser::ParseTableModuleField(Module* module) { | |
|
|
||
| EXPECT(Lpar); | ||
| EXPECT(Elem); | ||
| CHECK_RESULT(CheckIndexRange(loc, module->elem_segments.size(), "elem")); | ||
|
|
||
| auto elem_segment_field = std::make_unique<ElemSegmentModuleField>(loc); | ||
| ElemSegment& elem_segment = elem_segment_field->elem_segment; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess there are likely implemenation limits that are much lower than 2^32 that we could apply here?
With this check we are going to get OOM way before we hit it on 32-bit platforms right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, on 32 bit platforms this cannot happen. What do you suggest? A 64 bit specific macro? Or this patch is a bad idea?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this patch is good, but we we might also want to enforce much lower limits such as the ones defined in https://webassembly.github.io/spec/core/appendix/implementation.html#implementation-limitations.
That doesn't have to happen as part of this PR though.