-
Notifications
You must be signed in to change notification settings - Fork 623
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
WASI sockets: POSIX platform os_socket_recvfrom
unintended behavior
#3745
Comments
@lucasAbadFr thanks for reporting the issue! @yamt and @loganek could you help have a look? Seems we had better remove these three lines in posix os_socket_recv_from? wasm-micro-runtime/core/shared/platform/common/posix/posix_socket.c Lines 279 to 281 in e8c2952
|
why are you asking us?
at a glance, it seems broken. |
Sorry that I just think you are warmhearted and experienced😊, and may have good advice on this issue. |
heh, ok i got it. |
yeah I agree this seems like incorrect behavior. @lucasAbadFr would you like to provide a fix for it? If not, I might have some time next week to address the issue. |
Hi @loganek, I will not have that much free time in the next weeks. If you can provide a fix it would appreciated. I think that, we should just move this specific case from __wasi_errno_t
wasmtime_ssp_sock_recv(wasm_exec_env_t exec_env, struct fd_table *curfds,
__wasi_fd_t sock, void *buf, size_t buf_len,
size_t *recv_len)
{
__wasi_addr_t src_addr;
__wasi_errno_t error = -1;
error = wasmtime_ssp_sock_recv_from(exec_env, curfds, sock, buf, buf_len, 0,
&src_addr, recv_len);
if(src_addr && *recv_len > 0) {
/* Zero out the memory of `src_addr` after call to `recvfrom` */
memset(src_addr, 0, sizeof(*src_addr));
}
return error
} |
not sure about that. since |
Subject of the issue
When increasing the support for zephyr and comparing comportement between zephyr and linux, I found out with my sample that the function
recvfrom
might have a problem.Test case
Bellow is the simplified (removed includes, checks, prints, ...) code that I used to see the problem with
recvfrom
:Your environment
Steps to reproduce
Compile the code to wasm using the socket sample or standalone (need to link it with libwasi_socket_ext.a) and run it with command:
Expected behavior
Run without error.
Actual behavior
Fail after receiving first part of the data.
Extra Info
The code above is a simple client that sends a request to a server and waits for the response. The server is a simple server that sends a response to the client. The server used is the following:
My first observation was that strangely, when commenting the
else if
statement inos_socket_recv_from
the code worked as expected.wasm-micro-runtime/core/shared/platform/common/posix/posix_socket.c
Lines 258 to 284 in 67dce48
Firstly, I know that
recvfrom
is mostly used forUDP
and notTCP
, but I wanted to test the behavior of the function in wasm, because in native code there is no problem using the function this way. Even in the man page we can read: "The recvfrom() and recvmsg() calls are used to receive messages from a socket, and may be used to receive data on a socket whether or not it is connection-oriented."This is the same for
sendto
andsend
, but I will focus onrecvfrom
because it is the one that is causing the problem and the one I investigated.In the following statements, I will use POSIX name to refer to the
os_socket_*
functions.Secondly, the implementation of the
recv
function simply call therecvfrom
function, by passing an additionnal uninitialized parametersrc_addr
(of type__wasi_addr_t
). See the implementation :Secondly, the implementation of the
recv
function simply call therecvfrom
function, by passing an additionnal uninitialized parametersrc_addr
(of type__wasi_addr_t
). See the implementation:https://github.com/bytecodealliance/wasm-micro-runtime/blob/67dce482018bc3103f02018d9682dcbb5154661f/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c#L2810:L2819
Finally, I tested the code with the
recv
function and it worked fine, but I wanted to know why therecvfrom
function is not working as expected.As we use
TCP
, theaddrlen
parameter is set to 0 and we keep passing in theelse if
becausesrc_addr
is not NULL. The problem is that we will overwrite thesrc_addr
parameter withmemset
.If we are lucky and we can receive the message in one buffer we will not have any problem, but if the message is bigger than the buffer we will have a problem because the
src_addr
parameter will be overwritten and the function will return an error after the next call.The problem doesn't show with
recv
because thesrc_addr
is not coming from the user, but from the implementation, however withrecvfrom
thesrc_addr
is coming from the user.I'm not sure if this case is known or intended, but I think we are introducing a buggy behavior, because it doesn't work as expected (like in native).
The text was updated successfully, but these errors were encountered: