Skip to content

Commit 8dd1c8a

Browse files
authored
Copy only received bytes from socket recv buffer into the app buffer (#1497)
**What** * Updated `copy_buffer_to_iovec_app` so that it copies as much of the buffer into the iovec as specified * Throw invalid value when allocating an iovec of size 0 **Why** * A bug found from TCP client example which allocates 1024 for the iovec size (where the buf size is also 1024) but received bytes is passed in as the `buf_size` argument to `copy_buffer_to_iovec_app`. This would return early after hitting this check `buf + data->buf_len > buf_begin + buf_size`. However, if the amount to copy is less than the iovec size, we should copy that much of the buf size. Eg TCP client sample receives 27(?) bytes at a time, and this copies 27 bytes into the iovec of size 1024 * The TCP client example attempts to recv bytes of size 0, this attempts to wasm malloc size 0, which outputs a warning. We should early return if recv bytes of size 0
1 parent c072b51 commit 8dd1c8a

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,6 +1841,11 @@ allocate_iovec_app_buffer(wasm_module_inst_t module_inst,
18411841
for (total_size = 0, i = 0; i < data_len; i++, data++) {
18421842
total_size += data->buf_len;
18431843
}
1844+
1845+
if (total_size == 0) {
1846+
return __WASI_EINVAL;
1847+
}
1848+
18441849
if (total_size >= UINT32_MAX
18451850
|| !(buf_begin = wasm_runtime_malloc((uint32)total_size))) {
18461851
return __WASI_ENOMEM;
@@ -1852,12 +1857,24 @@ allocate_iovec_app_buffer(wasm_module_inst_t module_inst,
18521857
return __WASI_ESUCCESS;
18531858
}
18541859

1860+
static inline size_t
1861+
min(size_t a, size_t b)
1862+
{
1863+
return a > b ? b : a;
1864+
}
1865+
18551866
static wasi_errno_t
18561867
copy_buffer_to_iovec_app(wasm_module_inst_t module_inst, uint8 *buf_begin,
1857-
uint32 buf_size, iovec_app_t *data, uint32 data_len)
1868+
uint32 buf_size, iovec_app_t *data, uint32 data_len,
1869+
uint32 size_to_copy)
18581870
{
18591871
uint8 *buf = buf_begin;
18601872
uint32 i;
1873+
uint32 size_to_copy_into_iovec;
1874+
1875+
if (buf_size < size_to_copy) {
1876+
return __WASI_EINVAL;
1877+
}
18611878

18621879
for (i = 0; i < data_len; data++, i++) {
18631880
char *native_addr;
@@ -1868,13 +1885,23 @@ copy_buffer_to_iovec_app(wasm_module_inst_t module_inst, uint8 *buf_begin,
18681885

18691886
if (buf >= buf_begin + buf_size
18701887
|| buf + data->buf_len < buf /* integer overflow */
1871-
|| buf + data->buf_len > buf_begin + buf_size) {
1888+
|| buf + data->buf_len > buf_begin + buf_size
1889+
|| size_to_copy == 0) {
18721890
break;
18731891
}
18741892

1893+
/**
1894+
* If our app buffer size is smaller than the amount to be copied,
1895+
* only copy the amount in the app buffer. Otherwise, we fill the iovec
1896+
* buffer and reduce size to copy on the next iteration
1897+
*/
1898+
size_to_copy_into_iovec = min(data->buf_len, size_to_copy);
1899+
18751900
native_addr = (void *)addr_app_to_native(data->buf_offset);
1876-
bh_memcpy_s(native_addr, data->buf_len, buf, data->buf_len);
1877-
buf += data->buf_len;
1901+
bh_memcpy_s(native_addr, size_to_copy_into_iovec, buf,
1902+
size_to_copy_into_iovec);
1903+
buf += size_to_copy_into_iovec;
1904+
size_to_copy -= size_to_copy_into_iovec;
18781905
}
18791906

18801907
return __WASI_ESUCCESS;
@@ -1921,8 +1948,8 @@ wasi_sock_recv_from(wasm_exec_env_t exec_env, wasi_fd_t sock,
19211948
}
19221949
*ro_data_len = (uint32)recv_bytes;
19231950

1924-
err = copy_buffer_to_iovec_app(module_inst, buf_begin, (uint32)recv_bytes,
1925-
ri_data, ri_data_len);
1951+
err = copy_buffer_to_iovec_app(module_inst, buf_begin, (uint32)total_size,
1952+
ri_data, ri_data_len, (uint32)recv_bytes);
19261953

19271954
fail:
19281955
if (buf_begin) {

samples/socket-api/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ ExternalProject_Add(wasm-app
9191
tcp_server.wasm ${CMAKE_BINARY_DIR}
9292
send_recv.wasm ${CMAKE_BINARY_DIR}
9393
socket_opts.wasm ${CMAKE_BINARY_DIR}
94+
udp_client.wasm ${CMAKE_BINARY_DIR}
95+
udp_server.wasm ${CMAKE_BINARY_DIR}
9496
)
9597

9698
add_executable(tcp_server ${CMAKE_CURRENT_SOURCE_DIR}/wasm-src/tcp_server.c)

0 commit comments

Comments
 (0)