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

Return uint32 from WASI functions #2749

Merged
merged 1 commit into from
Nov 13, 2023

Conversation

zoraaver
Copy link
Contributor

Returning uint16 from WASI functions is technically correct. However, the smallest integer type in WASM is int32 and since we don't guarantee that the upper 16 bits of the result are zero'ed, it can result in tricky bugs if the language SDK being used in the WASM app does not cast back immediately to uint16. To prevent this, we directly return uint32 instead, so that the result is well-defined as a 32-bit number.

I originally noticed this issue when building WAMR with clangcl on Windows and running a rust WASM app but there's no reason it wouldn't affect WAMR on other platforms built with different toolchains. I'll look to fix the rust SDK as well but this is still probably a good idea in case some other SDKs are not handling the error result correctly (i.e. casting to uint16).

Returning uint16 from WASI functions is technically correct. However,
the smallest integer type in WASM is int32 and since we don't guarantee
that the upper 16 bits of the result are zero'ed, it can result in
tricky bugs if the language SDK being used in the WASM app does not cast
back immediately to uint16. To prevent this, we directly return uint32
instead, so that the result is well-defined as a 32-bit number.
Copy link
Collaborator

@loganek loganek left a comment

Choose a reason for hiding this comment

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

I think that looks reasonable to me. I guess the alternative would be to wrap each wasi function by a function that converts the return value to int32 but that seems to be an overkill.

// contains garbage from the WASM app perspective. To prevent this, we return
// uint32 directly instead so as not to be reliant on the correct behaviour of
// any current/future WASI SDK implemenations.
typedef uint32_t wasi_errno_t;
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@zoraaver zoraaver Nov 13, 2023

Choose a reason for hiding this comment

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

I didn't want to change __wasi_errno_t since it's used in one of the WASI datatypes, https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/shared/platform/include/platform_wasi_types.h#L311-L323 (meaning the struct would become incompatible with the WASI ABI). We could change __wasi_event_t to use uint16 directly for the error field but I think it's a bit less confusing if we keep the internal error type consistent with the ABI and modify wasi_errno_t which is only used for the return type of WASI functions.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, so let's merge this PR.

@wenyongh wenyongh merged commit 8c27676 into bytecodealliance:main Nov 13, 2023
383 checks passed
@zoraaver zoraaver deleted the fix-errno-size branch November 13, 2023 12:29
victoryang00 pushed a commit to victoryang00/wamr-aot-gc-checkpoint-restore that referenced this pull request May 27, 2024
Returning uint16 from WASI functions is technically correct. However,
the smallest integer type in WASM is int32 and since we don't guarantee
that the upper 16 bits of the result are zero'ed, it can result in
tricky bugs if the language SDK being used in the WASM app does not cast
back immediately to uint16. To prevent this, we directly return uint32
instead, so that the result is well-defined as a 32-bit number.
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.

3 participants