Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions expected/wasm32-wasi-eh/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,10 @@ __wasi_sock_open
__wasi_sock_pair
__wasi_sock_recv
__wasi_sock_recv_from
__wasi_sock_recv_msg
__wasi_sock_send
__wasi_sock_send_file
__wasi_sock_send_msg
__wasi_sock_send_to
__wasi_sock_set_opt_flag
__wasi_sock_set_opt_size
Expand Down
13 changes: 13 additions & 0 deletions expected/wasm32-wasi-eh/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@
#define CMPLX(x,y) __CMPLX(x, y, double)
#define CMPLXF(x,y) __CMPLX(x, y, float)
#define CMPLXL(x,y) __CMPLX(x, y, long double)
#define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1))
#define CMSG_DATA(cmsg) ((unsigned char *) (((struct cmsghdr *)(cmsg)) + 1))
#define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0)
#define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
#define CMSG_NXTHDR(mhdr,cmsg) ((cmsg)->cmsg_len < sizeof (struct cmsghdr) || __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) ? 0 : (struct cmsghdr *)__CMSG_NEXT(cmsg))
#define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr)))
#define CMSPAR 010000000000
#define CODESET 14
#define COLL_WEIGHTS_MAX 2
Expand Down Expand Up @@ -4148,6 +4154,8 @@
#define SCHED_OTHER 0
#define SCHED_RESET_ON_FORK 0x40000000
#define SCHED_RR 2
#define SCM_CREDENTIALS 0x02
#define SCM_RIGHTS 0x01
#define SCNd16 __INT16_FMTd__
#define SCNd32 __INT32_FMTd__
#define SCNd64 __INT64_FMTd__
Expand Down Expand Up @@ -5474,6 +5482,8 @@
#define __compiler_ATOMIC_WCHAR_T_LOCK_FREE 2
#define __CLANG_MAX_ALIGN_T_DEFINED
#define __CMPLX(x,y,t) (__builtin_complex((t)(x), (t)(y)))
#define __CMSG_LEN(cmsg) (((cmsg)->cmsg_len + sizeof(long) - 1) & ~(long)(sizeof(long) - 1))
#define __CMSG_NEXT(cmsg) ((unsigned char *)(cmsg) + __CMSG_LEN(cmsg))
#define __CONSTANT_CFSTRINGS__ 1
#define __CPU_op_S(i,size,set,op) ( (i)/8U >= (size) ? 0 : (((unsigned long *)(set))[(i)/8/sizeof(long)] op (1UL<<((i)%(8*sizeof(long))))) )
#define __CPU_op_func_S(func,op) static __inline void __CPU_##func##_S(size_t __size, cpu_set_t *__dest, const cpu_set_t *__src1, const cpu_set_t *__src2) { size_t __i; for (__i=0; __i<__size/sizeof(long); __i++) ((unsigned long *)__dest)[__i] = ((unsigned long *)__src1)[__i] op ((unsigned long *)__src2)[__i] ; }
Expand Down Expand Up @@ -5707,6 +5717,7 @@
#define __MEMORY_SCOPE_SYSTEM 0
#define __MEMORY_SCOPE_WRKGRP 2
#define __MEMORY_SCOPE_WVFRNT 3
#define __MHDR_END(mhdr) ((unsigned char *)(mhdr)->msg_control + (mhdr)->msg_controllen)
#define __NAMESER 19991006
#define __NEED_FILE
#define __NEED___isoc_va_list
Expand Down Expand Up @@ -6202,6 +6213,8 @@
#define __WASI_SIGNAL_WINCH (UINT8_C(28))
#define __WASI_SIGNAL_XCPU (UINT8_C(24))
#define __WASI_SIGNAL_XFSZ (UINT8_C(25))
#define __WASI_SOCK_CMSG_LEVEL_SOCKET (UINT16_C(0))
#define __WASI_SOCK_CMSG_TYPE_RIGHTS (UINT16_C(0))
#define __WASI_SOCK_OPTION_ACCEPT_TIMEOUT (UINT8_C(22))
#define __WASI_SOCK_OPTION_BROADCAST (UINT8_C(6))
#define __WASI_SOCK_OPTION_CONNECT_TIMEOUT (UINT8_C(21))
Expand Down
2 changes: 2 additions & 0 deletions expected/wasm32-wasi-eh/undefined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ __imported_wasix_32v1_sock_listen
__imported_wasix_32v1_sock_open
__imported_wasix_32v1_sock_pair
__imported_wasix_32v1_sock_recv_from
__imported_wasix_32v1_sock_recv_msg
__imported_wasix_32v1_sock_send_file
__imported_wasix_32v1_sock_send_msg
__imported_wasix_32v1_sock_send_to
__imported_wasix_32v1_sock_set_opt_flag
__imported_wasix_32v1_sock_set_opt_size
Expand Down
2 changes: 2 additions & 0 deletions expected/wasm32-wasi-ehpic/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,10 @@ __wasi_sock_open
__wasi_sock_pair
__wasi_sock_recv
__wasi_sock_recv_from
__wasi_sock_recv_msg
__wasi_sock_send
__wasi_sock_send_file
__wasi_sock_send_msg
__wasi_sock_send_to
__wasi_sock_set_opt_flag
__wasi_sock_set_opt_size
Expand Down
13 changes: 13 additions & 0 deletions expected/wasm32-wasi-ehpic/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@
#define CMPLX(x,y) __CMPLX(x, y, double)
#define CMPLXF(x,y) __CMPLX(x, y, float)
#define CMPLXL(x,y) __CMPLX(x, y, long double)
#define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1))
#define CMSG_DATA(cmsg) ((unsigned char *) (((struct cmsghdr *)(cmsg)) + 1))
#define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0)
#define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
#define CMSG_NXTHDR(mhdr,cmsg) ((cmsg)->cmsg_len < sizeof (struct cmsghdr) || __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) ? 0 : (struct cmsghdr *)__CMSG_NEXT(cmsg))
#define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr)))
#define CMSPAR 010000000000
#define CODESET 14
#define COLL_WEIGHTS_MAX 2
Expand Down Expand Up @@ -4155,6 +4161,8 @@
#define SCHED_OTHER 0
#define SCHED_RESET_ON_FORK 0x40000000
#define SCHED_RR 2
#define SCM_CREDENTIALS 0x02
#define SCM_RIGHTS 0x01
#define SCNd16 __INT16_FMTd__
#define SCNd32 __INT32_FMTd__
#define SCNd64 __INT64_FMTd__
Expand Down Expand Up @@ -5482,6 +5490,8 @@
#define __compiler_ATOMIC_WCHAR_T_LOCK_FREE 2
#define __CLANG_MAX_ALIGN_T_DEFINED
#define __CMPLX(x,y,t) (__builtin_complex((t)(x), (t)(y)))
#define __CMSG_LEN(cmsg) (((cmsg)->cmsg_len + sizeof(long) - 1) & ~(long)(sizeof(long) - 1))
#define __CMSG_NEXT(cmsg) ((unsigned char *)(cmsg) + __CMSG_LEN(cmsg))
#define __CONSTANT_CFSTRINGS__ 1
#define __CPU_op_S(i,size,set,op) ( (i)/8U >= (size) ? 0 : (((unsigned long *)(set))[(i)/8/sizeof(long)] op (1UL<<((i)%(8*sizeof(long))))) )
#define __CPU_op_func_S(func,op) static __inline void __CPU_##func##_S(size_t __size, cpu_set_t *__dest, const cpu_set_t *__src1, const cpu_set_t *__src2) { size_t __i; for (__i=0; __i<__size/sizeof(long); __i++) ((unsigned long *)__dest)[__i] = ((unsigned long *)__src1)[__i] op ((unsigned long *)__src2)[__i] ; }
Expand Down Expand Up @@ -5715,6 +5725,7 @@
#define __MEMORY_SCOPE_SYSTEM 0
#define __MEMORY_SCOPE_WRKGRP 2
#define __MEMORY_SCOPE_WVFRNT 3
#define __MHDR_END(mhdr) ((unsigned char *)(mhdr)->msg_control + (mhdr)->msg_controllen)
#define __NAMESER 19991006
#define __NEED_FILE
#define __NEED___isoc_va_list
Expand Down Expand Up @@ -6211,6 +6222,8 @@
#define __WASI_SIGNAL_WINCH (UINT8_C(28))
#define __WASI_SIGNAL_XCPU (UINT8_C(24))
#define __WASI_SIGNAL_XFSZ (UINT8_C(25))
#define __WASI_SOCK_CMSG_LEVEL_SOCKET (UINT16_C(0))
#define __WASI_SOCK_CMSG_TYPE_RIGHTS (UINT16_C(0))
#define __WASI_SOCK_OPTION_ACCEPT_TIMEOUT (UINT8_C(22))
#define __WASI_SOCK_OPTION_BROADCAST (UINT8_C(6))
#define __WASI_SOCK_OPTION_CONNECT_TIMEOUT (UINT8_C(21))
Expand Down
2 changes: 2 additions & 0 deletions expected/wasm32-wasi-ehpic/undefined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ __imported_wasix_32v1_sock_listen
__imported_wasix_32v1_sock_open
__imported_wasix_32v1_sock_pair
__imported_wasix_32v1_sock_recv_from
__imported_wasix_32v1_sock_recv_msg
__imported_wasix_32v1_sock_send_file
__imported_wasix_32v1_sock_send_msg
__imported_wasix_32v1_sock_send_to
__imported_wasix_32v1_sock_set_opt_flag
__imported_wasix_32v1_sock_set_opt_size
Expand Down
2 changes: 2 additions & 0 deletions expected/wasm32-wasi-threads/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,11 @@ __wasi_sock_leave_multicast_v6
__wasi_sock_listen
__wasi_sock_open
__wasi_sock_recv
__wasi_sock_recv_msg
__wasi_sock_recv_from
__wasi_sock_send
__wasi_sock_send_file
__wasi_sock_send_msg
__wasi_sock_send_to
__wasi_sock_set_opt_flag
__wasi_sock_set_opt_size
Expand Down
2 changes: 2 additions & 0 deletions expected/wasm32-wasi-threads/undefined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ __imported_wasix_32v1_sock_leave_multicast_v4
__imported_wasix_32v1_sock_leave_multicast_v6
__imported_wasix_32v1_sock_listen
__imported_wasix_32v1_sock_open
__imported_wasix_32v1_sock_recv_msg
__imported_wasix_32v1_sock_recv_from
__imported_wasix_32v1_sock_send_file
__imported_wasix_32v1_sock_send_msg
__imported_wasix_32v1_sock_send_to
__imported_wasix_32v1_sock_set_opt_flag
__imported_wasix_32v1_sock_set_opt_size
Expand Down
2 changes: 2 additions & 0 deletions expected/wasm32-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,10 @@ __wasi_sock_open
__wasi_sock_pair
__wasi_sock_recv
__wasi_sock_recv_from
__wasi_sock_recv_msg
__wasi_sock_send
__wasi_sock_send_file
__wasi_sock_send_msg
__wasi_sock_send_to
__wasi_sock_set_opt_flag
__wasi_sock_set_opt_size
Expand Down
13 changes: 13 additions & 0 deletions expected/wasm32-wasi/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@
#define CMPLX(x,y) __CMPLX(x, y, double)
#define CMPLXF(x,y) __CMPLX(x, y, float)
#define CMPLXL(x,y) __CMPLX(x, y, long double)
#define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1))
#define CMSG_DATA(cmsg) ((unsigned char *) (((struct cmsghdr *)(cmsg)) + 1))
#define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0)
#define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
#define CMSG_NXTHDR(mhdr,cmsg) ((cmsg)->cmsg_len < sizeof (struct cmsghdr) || __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) ? 0 : (struct cmsghdr *)__CMSG_NEXT(cmsg))
#define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr)))
#define CMSPAR 010000000000
#define CODESET 14
#define COLL_WEIGHTS_MAX 2
Expand Down Expand Up @@ -4155,6 +4161,8 @@
#define SCHED_OTHER 0
#define SCHED_RESET_ON_FORK 0x40000000
#define SCHED_RR 2
#define SCM_CREDENTIALS 0x02
#define SCM_RIGHTS 0x01
#define SCNd16 __INT16_FMTd__
#define SCNd32 __INT32_FMTd__
#define SCNd64 __INT64_FMTd__
Expand Down Expand Up @@ -5482,6 +5490,8 @@
#define __compiler_ATOMIC_WCHAR_T_LOCK_FREE 2
#define __CLANG_MAX_ALIGN_T_DEFINED
#define __CMPLX(x,y,t) (__builtin_complex((t)(x), (t)(y)))
#define __CMSG_LEN(cmsg) (((cmsg)->cmsg_len + sizeof(long) - 1) & ~(long)(sizeof(long) - 1))
#define __CMSG_NEXT(cmsg) ((unsigned char *)(cmsg) + __CMSG_LEN(cmsg))
#define __CONSTANT_CFSTRINGS__ 1
#define __CPU_op_S(i,size,set,op) ( (i)/8U >= (size) ? 0 : (((unsigned long *)(set))[(i)/8/sizeof(long)] op (1UL<<((i)%(8*sizeof(long))))) )
#define __CPU_op_func_S(func,op) static __inline void __CPU_##func##_S(size_t __size, cpu_set_t *__dest, const cpu_set_t *__src1, const cpu_set_t *__src2) { size_t __i; for (__i=0; __i<__size/sizeof(long); __i++) ((unsigned long *)__dest)[__i] = ((unsigned long *)__src1)[__i] op ((unsigned long *)__src2)[__i] ; }
Expand Down Expand Up @@ -5715,6 +5725,7 @@
#define __MEMORY_SCOPE_SYSTEM 0
#define __MEMORY_SCOPE_WRKGRP 2
#define __MEMORY_SCOPE_WVFRNT 3
#define __MHDR_END(mhdr) ((unsigned char *)(mhdr)->msg_control + (mhdr)->msg_controllen)
#define __NAMESER 19991006
#define __NEED_FILE
#define __NEED___isoc_va_list
Expand Down Expand Up @@ -6210,6 +6221,8 @@
#define __WASI_SIGNAL_WINCH (UINT8_C(28))
#define __WASI_SIGNAL_XCPU (UINT8_C(24))
#define __WASI_SIGNAL_XFSZ (UINT8_C(25))
#define __WASI_SOCK_CMSG_LEVEL_SOCKET (UINT16_C(0))
#define __WASI_SOCK_CMSG_TYPE_RIGHTS (UINT16_C(0))
#define __WASI_SOCK_OPTION_ACCEPT_TIMEOUT (UINT8_C(22))
#define __WASI_SOCK_OPTION_BROADCAST (UINT8_C(6))
#define __WASI_SOCK_OPTION_CONNECT_TIMEOUT (UINT8_C(21))
Expand Down
2 changes: 2 additions & 0 deletions expected/wasm32-wasi/undefined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ __imported_wasix_32v1_sock_listen
__imported_wasix_32v1_sock_open
__imported_wasix_32v1_sock_pair
__imported_wasix_32v1_sock_recv_from
__imported_wasix_32v1_sock_recv_msg
__imported_wasix_32v1_sock_send_file
__imported_wasix_32v1_sock_send_msg
__imported_wasix_32v1_sock_send_to
__imported_wasix_32v1_sock_set_opt_flag
__imported_wasix_32v1_sock_set_opt_size
Expand Down
2 changes: 2 additions & 0 deletions expected/wasm64-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,11 @@ __wasi_sock_leave_multicast_v6
__wasi_sock_listen
__wasi_sock_open
__wasi_sock_recv
__wasi_sock_recv_msg
__wasi_sock_recv_from
__wasi_sock_send
__wasi_sock_send_file
__wasi_sock_send_msg
__wasi_sock_send_to
__wasi_sock_set_opt_flag
__wasi_sock_set_opt_size
Expand Down
109 changes: 103 additions & 6 deletions libc-bottom-half/cloudlibc/src/libc/sys/socket/recvmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,67 @@
#include <assert.h>
#include <wasi/api.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

static size_t wasi_cmsg_align(size_t len) {
return (len + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1);
}

static int convert_control_from_wasi(struct msghdr* msg,
const uint8_t* control,
size_t control_len) {
if (msg->msg_control == NULL || msg->msg_controllen == 0) {
if (control_len != 0)
msg->msg_flags |= MSG_CTRUNC;
msg->msg_controllen = 0;
return 0;
}

uint8_t* out = msg->msg_control;
size_t out_capacity = msg->msg_controllen;
size_t out_used = 0;
size_t in_offset = 0;

while (in_offset + sizeof(__wasi_sock_cmsg_t) <= control_len) {
const __wasi_sock_cmsg_t* wasi_cmsg =
(const __wasi_sock_cmsg_t*)(control + in_offset);
if (wasi_cmsg->cmsg_len < sizeof(__wasi_sock_cmsg_t) ||
in_offset + wasi_cmsg->cmsg_len > control_len) {
msg->msg_flags |= MSG_CTRUNC;
break;
}

size_t payload_len = wasi_cmsg->cmsg_len - sizeof(__wasi_sock_cmsg_t);
size_t posix_space = CMSG_SPACE(payload_len);
size_t posix_len = CMSG_LEN(payload_len);
if (out_used + posix_space > out_capacity) {
msg->msg_flags |= MSG_CTRUNC;
break;
}

struct cmsghdr* cmsg = (struct cmsghdr*)(out + out_used);
if (wasi_cmsg->cmsg_level == __WASI_SOCK_CMSG_LEVEL_SOCKET &&
wasi_cmsg->cmsg_type == __WASI_SOCK_CMSG_TYPE_RIGHTS) {
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
} else {
msg->msg_flags |= MSG_CTRUNC;
break;
}

cmsg->cmsg_len = posix_len;
memcpy(CMSG_DATA(cmsg),
control + in_offset + wasi_cmsg_align(sizeof(__wasi_sock_cmsg_t)),
payload_len);
out_used += posix_space;
in_offset += wasi_cmsg_align(sizeof(__wasi_sock_cmsg_t)) + wasi_cmsg_align(payload_len);
}

msg->msg_controllen = out_used;
return 0;
}

ssize_t recvmsg(int socket, struct msghdr *restrict msg, int flags) {
__wasi_iovec_t *ri_data = (__wasi_iovec_t *)msg->msg_iov;
Expand All @@ -21,12 +81,39 @@ ssize_t recvmsg(int socket, struct msghdr *restrict msg, int flags) {

__wasi_size_t ro_datalen;
__wasi_roflags_t ro_flags;
__wasi_size_t ro_control_len = 0;
__wasi_errno_t error;
if (msg->msg_name == NULL) {

uint8_t* control = NULL;
size_t control_capacity = msg->msg_control == NULL ? 0 : msg->msg_controllen;
if (control_capacity != 0) {
control = malloc(control_capacity);
if (control == NULL) {
errno = ENOMEM;
return -1;
}
}

if (control_capacity != 0) {
__wasi_addr_port_t peer_addr;
__wasi_addr_port_t* peer_addr_ptr = msg->msg_name == NULL ? NULL : &peer_addr;
error = __wasi_sock_recv_msg(socket,
ri_data, ri_data_len, ri_flags,
peer_addr_ptr,
control, control_capacity,
&ro_datalen,
&ro_flags,
&ro_control_len);
if (error == 0 && msg->msg_name != NULL) {
struct sockaddr *addr = (struct sockaddr *)msg->msg_name;
socklen_t *addrlen = &msg->msg_namelen;
error = wasi_to_sockaddr(&peer_addr, addr, addrlen);
}
} else if (msg->msg_name == NULL) {
error = __wasi_sock_recv(socket,
ri_data, ri_data_len, ri_flags,
&ro_datalen,
&ro_flags);
ri_data, ri_data_len, ri_flags,
&ro_datalen,
&ro_flags);
} else {
__wasi_addr_port_t peer_addr;
error = __wasi_sock_recv_from(socket,
Expand All @@ -41,9 +128,19 @@ ssize_t recvmsg(int socket, struct msghdr *restrict msg, int flags) {

struct sockaddr *addr = (struct sockaddr *)msg->msg_name;
socklen_t *addrlen = &msg->msg_namelen;
error = wasi_to_sockaddr(&peer_addr, addr, addrlen);
error = wasi_to_sockaddr(&peer_addr, addr, addrlen);
}
msg->msg_flags = 0;
if ((ro_flags & __WASI_ROFLAGS_RECV_DATA_TRUNCATED) != 0)
msg->msg_flags |= MSG_TRUNC;
if (error == 0 && control_capacity != 0) {
if (ro_control_len > control_capacity)
msg->msg_flags |= MSG_CTRUNC;
convert_control_from_wasi(msg, control, ro_control_len > control_capacity ? control_capacity : ro_control_len);
} else if (control_capacity == 0) {
msg->msg_controllen = 0;
}
msg->msg_flags = ro_flags;
free(control);

if (error != 0) {
errno = error;
Expand Down
Loading
Loading