libc_wasi_wrapper.c: Fix min func issue for size_t < 8 bytes on some platforms (#2152)

According to the 1999 ISO C standard (C99), size_t is an unsigned integer type of
at least 16 bit (see sections 7.17 and 7.18.3), it may be uint32 in 32-bit platforms:
https://en.cppreference.com/w/cpp/types/size_t

Calling function `size_t min(size_t, size_t)` with two uint64 arguments may get
invalid result.

Co-authored-by: Georgii Rylov <godjan@amazon.co.uk>
This commit is contained in:
Georgii Rylov 2023-04-26 14:03:02 +01:00 committed by GitHub
parent 5c497e5a14
commit c1723b8f3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -56,8 +56,14 @@ typedef struct WASIContext *wasi_ctx_t;
wasi_ctx_t wasi_ctx_t
wasm_runtime_get_wasi_ctx(wasm_module_inst_t module_inst); wasm_runtime_get_wasi_ctx(wasm_module_inst_t module_inst);
static inline size_t static inline uint64_t
min(size_t a, size_t b) min_uint64(uint64_t a, uint64_t b)
{
return a > b ? b : a;
}
static inline uint32_t
min_uint32(uint32_t a, uint32_t b)
{ {
return a > b ? b : a; return a > b ? b : a;
} }
@ -962,7 +968,7 @@ get_timeout_for_poll_oneoff(const wasi_subscription_t *in,
const __wasi_subscription_t *s = &in[i]; const __wasi_subscription_t *s = &in[i];
if (s->u.type == __WASI_EVENTTYPE_CLOCK if (s->u.type == __WASI_EVENTTYPE_CLOCK
&& (s->u.u.clock.flags & __WASI_SUBSCRIPTION_CLOCK_ABSTIME) == 0) { && (s->u.u.clock.flags & __WASI_SUBSCRIPTION_CLOCK_ABSTIME) == 0) {
timeout = min(timeout, s->u.u.clock.timeout); timeout = min_uint64(timeout, s->u.u.clock.timeout);
} }
} }
return timeout; return timeout;
@ -1016,8 +1022,8 @@ execute_interruptible_poll_oneoff(
while (timeout == (__wasi_timestamp_t)-1 || elapsed <= timeout) { while (timeout == (__wasi_timestamp_t)-1 || elapsed <= timeout) {
/* update timeout for clock subscription events */ /* update timeout for clock subscription events */
update_clock_subscription_data(in_copy, nsubscriptions, update_clock_subscription_data(
min(time_quant, timeout - elapsed)); in_copy, nsubscriptions, min_uint64(time_quant, timeout - elapsed));
err = wasmtime_ssp_poll_oneoff(curfds, in_copy, out, nsubscriptions, err = wasmtime_ssp_poll_oneoff(curfds, in_copy, out, nsubscriptions,
nevents); nevents);
elapsed += time_quant; elapsed += time_quant;
@ -1999,7 +2005,7 @@ copy_buffer_to_iovec_app(wasm_module_inst_t module_inst, uint8 *buf_begin,
* only copy the amount in the app buffer. Otherwise, we fill the iovec * only copy the amount in the app buffer. Otherwise, we fill the iovec
* buffer and reduce size to copy on the next iteration * buffer and reduce size to copy on the next iteration
*/ */
size_to_copy_into_iovec = min(data->buf_len, size_to_copy); size_to_copy_into_iovec = min_uint32(data->buf_len, size_to_copy);
native_addr = (void *)addr_app_to_native(data->buf_offset); native_addr = (void *)addr_app_to_native(data->buf_offset);
bh_memcpy_s(native_addr, size_to_copy_into_iovec, buf, bh_memcpy_s(native_addr, size_to_copy_into_iovec, buf,