Skip to content

Add declaration limit checks to parser#2736

Merged
zherczeg merged 1 commit intoWebAssembly:mainfrom
zherczeg:limit_checks
Apr 2, 2026
Merged

Add declaration limit checks to parser#2736
zherczeg merged 1 commit intoWebAssembly:mainfrom
zherczeg:limit_checks

Conversation

@zherczeg
Copy link
Copy Markdown
Collaborator

@zherczeg zherczeg commented Apr 2, 2026

The "focus on correctness" in #2735 made me thinking. This was something I was aware for a longer time, but never did anything to it. The binary reader reads the number of items from the file, so the maximum amount of declarations is always less or equal than kInvalidIndex (equals to the maximum uint32_t value). These declarations are indexed from [0 .. kInvalidIndex - 1]. As far as I see there is no such limit for the text parser. This patch adds some limit checks.

@@ -1620,7 +1634,7 @@ Result WastParser::ParseTagModuleField(Module* module) {
EXPECT(Lpar);
EXPECT(Tag);
Location loc = GetLocation();
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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.

size_t size,
const char* decl) {
if (size >= kInvalidIndex) {
Error(loc, "Too many %s declarations", decl);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the convention is not to start error messages with a capital letter like this.

Result WastParser::CheckDeclarationLimit(Location& loc,
size_t size,
const char* decl) {
if (size >= kInvalidIndex) {
Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Collaborator Author

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?

Copy link
Copy Markdown
Member

@sbc100 sbc100 Apr 2, 2026

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.

@zherczeg
Copy link
Copy Markdown
Collaborator Author

zherczeg commented Apr 2, 2026

Changed the capital letter.

Result Synchronize(SynchronizeFunc);

// Check the maximum allowed declarations.
Result CheckDeclarationLimit(Location& loc, size_t size, const char* decl);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can come up with a better name? Maybe CheckIndexRange, since we are verifying that its doesn't exceed the size of Index?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@zherczeg zherczeg merged commit 2f731a7 into WebAssembly:main Apr 2, 2026
17 checks passed
@zherczeg zherczeg deleted the limit_checks branch April 2, 2026 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants