diff --git a/core/iwasm/libraries/libc-uvwasi/libc_uvwasi_wrapper.c b/core/iwasm/libraries/libc-uvwasi/libc_uvwasi_wrapper.c index 504ff7f9..6ead6540 100644 --- a/core/iwasm/libraries/libc-uvwasi/libc_uvwasi_wrapper.c +++ b/core/iwasm/libraries/libc-uvwasi/libc_uvwasi_wrapper.c @@ -30,7 +30,16 @@ wasm_runtime_module_free(module_inst, offset) /* clang-format on */ -#define wasi_errno_t uvwasi_errno_t +// uvwasi_errno_t is typedef'd to uint16 which is correct according to the ABI +// specification. However, in WASM, the smallest integer type is int32. If we +// return uint16, we would rely on language SDKs to implement the correct +// behaviour of casting to uint16 before checking the value or using it any way. +// Failure to do so can cause tricky bugs as the upper 16 bits of the error +// result are not guaranteed to be zero'ed by us so the result essentially +// 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 SDK implementations. +#define wasi_errno_t uint32_t #define wasi_fd_t uvwasi_fd_t #define wasi_clockid_t uvwasi_clockid_t #define wasi_timestamp_t uvwasi_timestamp_t diff --git a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.h b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.h index 2d149bc0..d958fa39 100644 --- a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.h +++ b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.h @@ -18,7 +18,16 @@ typedef __wasi_advice_t wasi_advice_t; typedef __wasi_ciovec_t wasi_ciovec_t; typedef __wasi_clockid_t wasi_clockid_t; typedef __wasi_dircookie_t wasi_dircookie_t; -typedef __wasi_errno_t wasi_errno_t; +// __wasi_errno_t is typedef'd to uint16 which is correct according to the ABI +// specification. However, in WASM, the smallest integer type is int32. If we +// return uint16, we would rely on language SDKs to implement the correct +// behaviour of casting to uint16 before checking the value or using it any way. +// Failure to do so can cause tricky bugs as the upper 16 bits of the error +// result are not guaranteed to be zero'ed by us so the result essentially +// 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; typedef __wasi_event_t wasi_event_t; typedef __wasi_exitcode_t wasi_exitcode_t; typedef __wasi_fdflags_t wasi_fdflags_t;