Skip to content
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

[12.0.0]: x64: Remove recursion in to_amode helper #6995

Merged
merged 1 commit into from
Sep 11, 2023

Conversation

alexcrichton
Copy link
Member

This is a backport of #6968 to the 12.0.0 release branch of Wasmtime. Note that AArch64 is not backported as its recursion is not present on this branch.

This commit removes the recursion present in the x64 backend's
`to_amode` and `to_amode_add` helpers. The recursion is currently
unbounded and controlled by user input meaning it's not too hard to
craft user input which triggers stack overflow in the host. By removing
recursion there's no need to worry about this since the stack depth will
never be hit.

The main concern with removing recursion is that code quality may not be
quite as good any more. The purpose of the recursion here is to "hunt
for constants" and update the immediate `Offset32`, and now without
recursion only at most one constant is found and folded instead of an
arbitrary number of constants as before. This should continue to produce
the same code as before so long as optimizations are enabled, but
without optimizations this will produce worse code than before.

Note with a hypothetical mid-end optimization that does this constant
folding for us the rules here could be further simplified to purely
consider the shape of the input `Value` to amode computation without
considering constants at all.
@alexcrichton alexcrichton requested review from a team as code owners September 11, 2023 16:17
@alexcrichton alexcrichton requested review from abrown and pchickey and removed request for a team September 11, 2023 16:17
@alexcrichton alexcrichton removed the request for review from abrown September 11, 2023 16:35
@alexcrichton alexcrichton merged commit 9c2ed4e into bytecodealliance:release-12.0.0 Sep 11, 2023
32 checks passed
@alexcrichton alexcrichton deleted the amode-12 branch September 11, 2023 17:05
veeshi pushed a commit to nethunterslabs/wasmtime that referenced this pull request Sep 19, 2023
This commit removes the recursion present in the x64 backend's
`to_amode` and `to_amode_add` helpers. The recursion is currently
unbounded and controlled by user input meaning it's not too hard to
craft user input which triggers stack overflow in the host. By removing
recursion there's no need to worry about this since the stack depth will
never be hit.

The main concern with removing recursion is that code quality may not be
quite as good any more. The purpose of the recursion here is to "hunt
for constants" and update the immediate `Offset32`, and now without
recursion only at most one constant is found and folded instead of an
arbitrary number of constants as before. This should continue to produce
the same code as before so long as optimizations are enabled, but
without optimizations this will produce worse code than before.

Note with a hypothetical mid-end optimization that does this constant
folding for us the rules here could be further simplified to purely
consider the shape of the input `Value` to amode computation without
considering constants at all.
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.

None yet

2 participants