Skip to content

Support 64 bit limits for tables - #2767

Merged
zherczeg merged 1 commit into
WebAssembly:mainfrom
zherczeg:table_limit
Jun 23, 2026
Merged

zherczeg merged 1 commit into
WebAssembly:mainfrom
zherczeg:table_limit

Conversation

@zherczeg

Copy link
Copy Markdown
Collaborator

No description provided.

Comment thread src/shared-validator.cc
}
result |= CheckLimits(loc, limits, UINT32_MAX, "elems");
uint64_t absolute_max = limits.is_64 ? UINT64_MAX : UINT32_MAX;
result |= CheckLimits(loc, limits, absolute_max, "elems");

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.

Alternative: if (!limits.is_64) { .... } since limits is 64 bit.

Comment thread src/binary-reader.cc Outdated
CHECK_RESULT(ReadU64Leb128(&max, "table max elem count"));
}
out_elem_limits->initial = initial;
out_elem_limits->max = max;

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.

Can we just always read these as 64-bit values and leave it to validation to check them?

If not then at least I think we can limit this if/else to just the 4 lines that do that ReadUXXLeb128.

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.

How the code would it look like? The ReadU32Leb128 uses a pointer to return its value, and we cannot pass the address of an uint64_t value to it.

I would not delegate this check to validation (too late for me), but we could read a 64 bit value and throw an error for !is_64 && >UINT32_MAX values. I am not sure we could make the code much nicer though.

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.

Can we just always read U64 in this case?

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.

Always? For 32 bit tables, I think a >UINT32_MAX value is malformed.

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.

But won't the validator then take care of that?

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.

It is too late for that. See these tests:
https://github.com/WebAssembly/testsuite/blob/d76759e746f3564a03f6106ae19679742f2a1831/table.wast#L27

These must fail even if validation is disabled, since they are malformed. If I read the limits as U64, they will pass.
E.g. (table 5 1 funcref) with --no-check should work (and it does), but (table 0 0x1_0000_0000 funcref) should not.

@zherczeg

Copy link
Copy Markdown
Collaborator Author

Created another code variant. The new ReadU32OrU64Leb128 function could be used elsewhere, it could even replace ReadU32Leb128/ReadU64Leb128.

Comment thread src/binary-reader.cc Outdated
ERROR_UNLESS(bytes_read > 0 && (is_64 || *out_value <= UINT32_MAX),
"unable to read u%s leb128: %s", is_64 ? "64" : "32", desc);
state_.offset += bytes_read;
return Result::Ok;

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.

Can you just call ReadU32Leb128 / ReadU64Leb128 here?

e.g. return is_64 ? ReadU64Leb128(out_value, desc) : ReadU32Leb128(out_value, desc)

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.

Changed the code. The ReadU32Leb128 requires uint32_t pointer, so the number of lines does not dropped.

Comment thread src/binary-reader.cc Outdated
}
uint32_t value = 0;
Result result = ReadU32Leb128(&value, desc);
*out_value = value;

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.

I could add a if (result == Result::Ok) here

@zherczeg
zherczeg merged commit f87a3ad into WebAssembly:main Jun 23, 2026
17 checks passed
@zherczeg
zherczeg deleted the table_limit branch June 23, 2026 08:29
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