diff --git a/core/iwasm/common/wasm_application.c b/core/iwasm/common/wasm_application.c index 85f3770f6..acce2cdbf 100644 --- a/core/iwasm/common/wasm_application.c +++ b/core/iwasm/common/wasm_application.c @@ -289,6 +289,7 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, exec_env = wasm_runtime_get_exec_env_singleton(module_inst); if (exec_env) { wasm_runtime_dump_mem_consumption(exec_env); + (WASMModuleInstance *)module_inst->cur_exception } #endif @@ -712,7 +713,10 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name, } case VALUE_TYPE_F32: { - os_printf("%.7g:f32", *(float32 *)(argv1 + k)); + // Explicit cast to double to avoid warning. + // Float arguments are promoted to double in variadic + // functions per section 6.5.2.2 of the C99 standard. + os_printf("%.7g:f32", (double)*(float32 *)(argv1 + k)); k++; break; } diff --git a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c index 6d057a6a1..609a0ec3a 100644 --- a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c +++ b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c @@ -1004,12 +1004,10 @@ update_clock_subscription_data(wasi_subscription_t *in, uint32 nsubscriptions, } static wasi_errno_t -execute_interruptible_poll_oneoff( -#if !defined(WASMTIME_SSP_STATIC_CURFDS) - struct fd_table *curfds, -#endif - const __wasi_subscription_t *in, __wasi_event_t *out, size_t nsubscriptions, - size_t *nevents, wasm_exec_env_t exec_env) +execute_interruptible_poll_oneoff(struct fd_table *curfds, + const __wasi_subscription_t *in, + __wasi_event_t *out, size_t nsubscriptions, + size_t *nevents, wasm_exec_env_t exec_env) { if (nsubscriptions == 0) { *nevents = 0; @@ -2106,15 +2104,16 @@ wasi_sock_recv(wasm_exec_env_t exec_env, wasi_fd_t sock, iovec_app_t *ri_data, wasi_roflags_t *ro_flags) { wasm_module_inst_t module_inst = get_module_inst(exec_env); - __wasi_addr_t src_addr; wasi_errno_t error; if (!validate_native_addr(ro_flags, (uint64)sizeof(wasi_roflags_t))) return __WASI_EINVAL; + // We call `recvfrom` with NULL source address as `recv` doesn't + // return the source address and this parameter is not used. + *ro_data_len = 0; error = wasi_sock_recv_from(exec_env, sock, ri_data, ri_data_len, ri_flags, - &src_addr, ro_data_len); - *ro_flags = ri_flags; + NULL, ro_data_len); return error; } diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c index df6817504..f9b37ceca 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c @@ -1904,10 +1904,10 @@ convert_timestamp(__wasi_timestamp_t in, os_timespec *out) #else out->tv_nsec = (long)(in % 1000000000); #endif - in /= 1000000000; + __wasi_timestamp_t temp = in / 1000000000; // Clamp to the maximum in case it would overflow our system's time_t. - out->tv_sec = (time_t)in < BH_TIME_T_MAX ? (time_t)in : BH_TIME_T_MAX; + out->tv_sec = (time_t)temp < BH_TIME_T_MAX ? (time_t)temp : BH_TIME_T_MAX; } __wasi_errno_t @@ -2094,7 +2094,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds, size_t nsubscriptions, size_t *nevents) NO_LOCK_ANALYSIS { -#if defined(BH_PLATFORM_WINDOWS) || defined(BH_PLATFORM_ZEPHYR) +#if defined(BH_PLATFORM_WINDOWS) return __WASI_ENOSYS; #else // Sleeping. @@ -2212,7 +2212,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds, if (error == 0) { // Proper file descriptor on which we can poll(). pfds[i] = (os_poll_file_handle){ - .fd = fos[i]->file_handle, + .fd = fos[i]->file_handle->fd, .events = s->u.type == __WASI_EVENTTYPE_FD_READ ? POLLIN : POLLOUT, @@ -2845,7 +2845,7 @@ wasmtime_ssp_sock_recv_from(wasm_exec_env_t exec_env, struct fd_table *curfds, { struct fd_object *fo; __wasi_errno_t error; - bh_sockaddr_t sockaddr; + bh_sockaddr_t sockaddr, *sockaddr_ptr = NULL; int ret; error = fd_object_get(curfds, &fo, sock, __WASI_RIGHT_FD_READ, 0); @@ -2853,15 +2853,29 @@ wasmtime_ssp_sock_recv_from(wasm_exec_env_t exec_env, struct fd_table *curfds, return error; } + // If the source address is not NULL, the caller is requesting the source + // address to be returned if the protocol supports it. As such, we convert + // the format of the structure pass in prior to the call to the OS + // implementation. If the value is NULL, the POSIX standard states that + // the address is not returned. + if (src_addr != NULL) { + sockaddr_ptr = &sockaddr; + wasi_addr_to_bh_sockaddr(src_addr, &sockaddr); + } + /* Consume bh_sockaddr_t instead of __wasi_addr_t */ ret = blocking_op_socket_recv_from(exec_env, fo->file_handle, buf, buf_len, - 0, &sockaddr); + 0, sockaddr_ptr); fd_object_release(exec_env, fo); if (-1 == ret) { return convert_errno(errno); } - bh_sockaddr_to_wasi_addr(&sockaddr, src_addr); + // If the source address is not NULL, we need to convert the sockaddr + // back to __wasi_addr_t format. + if (src_addr != NULL) { + bh_sockaddr_to_wasi_addr(sockaddr_ptr, src_addr); + } *recv_len = (size_t)ret; return __WASI_ESUCCESS; diff --git a/core/shared/mem-alloc/ems/ems_gc.c b/core/shared/mem-alloc/ems/ems_gc.c index 26e83a975..e97a07bba 100644 --- a/core/shared/mem-alloc/ems/ems_gc.c +++ b/core/shared/mem-alloc/ems/ems_gc.c @@ -6,7 +6,9 @@ #include "ems_gc.h" #include "ems_gc_internal.h" +#ifndef GB // Some platforms define already, causing build warnings. #define GB (1 << 30UL) +#endif #define MARK_NODE_OBJ_CNT 256 diff --git a/core/shared/platform/zephyr/platform_internal.h b/core/shared/platform/zephyr/platform_internal.h index 0ca02dc5c..567a36926 100644 --- a/core/shared/platform/zephyr/platform_internal.h +++ b/core/shared/platform/zephyr/platform_internal.h @@ -80,6 +80,24 @@ #define BH_PLATFORM_ZEPHYR #endif +#include + +#ifndef PATH_MAX +#define PATH_MAX 256 +#endif + +#ifndef STDIN_FILENO +#define STDIN_FILENO 0 +#endif + +#ifndef STDOUT_FILENO +#define STDOUT_FILENO 1 +#endif + +#ifndef STDERR_FILENO +#define STDERR_FILENO 2 +#endif + /* Synchronization primitives for usermode. * The macros are prefixed with 'z' because when building * with WAMR_BUILD_LIBC_WASI the same functions are defined, @@ -222,6 +240,8 @@ set_exec_mem_alloc_func(exec_mem_alloc_func_t alloc_func, typedef int os_dir_stream; typedef int os_raw_file_handle; +#define OS_DIR_STREAM_INVALID 0 + // handle for file system descriptor typedef struct zephyr_fs_desc { char *path; @@ -259,20 +279,20 @@ typedef unsigned int os_nfds_t; #define FIONREAD ZFD_IOCTL_FIONREAD -typedef struct { - time_t tv_sec; - long tv_nsec; -} os_timespec; +typedef struct timespec os_timespec; +#ifndef CLOCK_REALTIME #define CLOCK_REALTIME 1 +#endif + #define CLOCK_MONOTONIC 4 -// TODO: use it in sandboxed posix.c. -// int os_sched_yield(void) -// { -// k_yield(); -// return 0; -// } +static inline int +os_sched_yield(void) +{ + k_yield(); + return 0; +} static inline os_file_handle os_get_invalid_handle(void) diff --git a/core/shared/platform/zephyr/zephyr_file.c b/core/shared/platform/zephyr/zephyr_file.c index 93f7dac8f..250a998e8 100644 --- a/core/shared/platform/zephyr/zephyr_file.c +++ b/core/shared/platform/zephyr/zephyr_file.c @@ -3,6 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +#include "platform_api_vmcore.h" #include "platform_api_extension.h" #include "libc_errno.h" @@ -45,7 +46,7 @@ // We will take the maximum number of open files // from the Zephyr POSIX configuration -#define CONFIG_WASI_MAX_OPEN_FILES CONFIG_POSIX_MAX_FDS +#define CONFIG_WASI_MAX_OPEN_FILES CONFIG_ZVFS_OPEN_MAX // Macro to retrieve a file system descriptor and check it's validity. #define GET_FILE_SYSTEM_DESCRIPTOR(fd, ptr) \ @@ -67,20 +68,63 @@ K_MUTEX_DEFINE(desc_array_mutex); static char prestat_dir[MAX_FILE_NAME + 1]; +/** + * Duplicates a string by allocating memory and copying the source string. + * Provided here because Zephyr does not have `strdup` enabled by default . + * @param s The source string to duplicate. + * @return A pointer to the duplicated string, or NULL if allocation fails. + * The caller is responsible for freeing the memory. + */ +char * +duplicate_string(const char *s) +{ + if (s == NULL) { + return NULL; + } + + size_t len = strlen(s) + 1; // Include null terminator + char *dup = malloc(len); + if (dup == NULL) { + return NULL; // Allocation failed + } + + memcpy(dup, s, len); // Copy string including null terminator + return dup; +} + +bool +build_absolute_path(char *abs_path, size_t abs_path_len, const char *path) +{ + if (!path) { + abs_path[0] = '\0'; + return false; + } + + size_t len1 = strlen(prestat_dir); + size_t len2 = strlen(path); + + if (len1 + 1 + len2 + 1 > abs_path_len) { + abs_path[0] = '\0'; // Empty string on error + return false; // Truncation would occur + } + + snprintf(abs_path, abs_path_len, "%s/%s", prestat_dir, path); + return true; +} + static struct zephyr_fs_desc * zephyr_fs_alloc_obj(bool is_dir, const char *path, int *index) { - int i; struct zephyr_fs_desc *ptr = NULL; *index = -1; // give a default value to index in case table is full k_mutex_lock(&desc_array_mutex, K_FOREVER); - for (i = 0; i < CONFIG_WASI_MAX_OPEN_FILES; i++) { + for (int i = 0; i < CONFIG_WASI_MAX_OPEN_FILES; i++) { if (desc_array[i].used == false) { ptr = &desc_array[i]; ptr->used = true; ptr->is_dir = is_dir; - ptr->path = strdup(path); + ptr->path = duplicate_string(path); if (ptr->path == NULL) { ptr->used = false; k_mutex_unlock(&desc_array_mutex); @@ -104,26 +148,6 @@ zephyr_fs_free_obj(struct zephyr_fs_desc *ptr) ptr->used = false; } -void -debug_zephyr_fs_desc(const zephyr_fs_desc *desc) -{ - if (desc == NULL) { - os_printf("Descriptor is NULL\n"); - return; - } - os_printf("Descriptor found at %p\n", desc); - os_printf(" Path: %s\n", desc->path ? desc->path : "NULL"); - os_printf(" Is Directory: %s\n", desc->is_dir ? "Yes" : "No"); - os_printf(" Used: %s\n", desc->used ? "Yes" : "No"); - - if (desc->is_dir) { - os_printf(" Directory: %p\n", desc->dir); - } - else { - os_printf(" File: %p\n", desc->file); - } -} - /* /!\ Needed for socket to work */ __wasi_errno_t os_fstat(os_file_handle handle, struct __wasi_filestat_t *buf) @@ -133,7 +157,6 @@ os_fstat(os_file_handle handle, struct __wasi_filestat_t *buf) if (!handle->is_sock) { GET_FILE_SYSTEM_DESCRIPTOR(handle->fd, ptr); - // debug_zephyr_fs_desc(ptr); /* We treat the case of std[in/out/err] */ if (ptr->path != NULL @@ -147,7 +170,28 @@ os_fstat(os_file_handle handle, struct __wasi_filestat_t *buf) return __WASI_ESUCCESS; } - return os_fstatat(handle, ptr->path, buf, 0); + // Get file information using Zephyr's fs_stat function + struct fs_dirent entry; + rc = fs_stat(ptr->path, &entry); + if (rc < 0) { + return convert_errno(-rc); + } + + // Fill in the __wasi_filestat_t structure + buf->st_dev = 0; // Zephyr's fs_stat doesn't provide a device ID + buf->st_ino = 0; // Zephyr's fs_stat doesn't provide an inode number + buf->st_filetype = entry.type == FS_DIR_ENTRY_DIR + ? __WASI_FILETYPE_DIRECTORY + : __WASI_FILETYPE_REGULAR_FILE; + buf->st_nlink = 1; // Zephyr's fs_stat doesn't provide a link count + buf->st_size = entry.size; + buf->st_atim = 0; // Zephyr's fs_stat doesn't provide timestamps + buf->st_mtim = 0; + buf->st_ctim = 0; + + return __WASI_ESUCCESS; + + // return os_fstatat(handle, ptr->path, buf, 0); } else { // socklen_t socktypelen = sizeof(socktype); @@ -191,8 +235,18 @@ os_fstatat(os_file_handle handle, const char *path, return __WASI_EBADF; } + char abs_path[MAX_FILE_NAME + 1]; + + if (handle == NULL) { + return __WASI_EINVAL; // Or another appropriate error code + } + + if (!build_absolute_path(abs_path, sizeof(abs_path), path)) { + return __WASI_ENOMEM; + } + // Get file information using Zephyr's fs_stat function - rc = fs_stat(path, &entry); + rc = fs_stat(abs_path, &entry); if (rc < 0) { return convert_errno(-rc); } @@ -381,18 +435,29 @@ os_openat(os_file_handle handle, const char *path, __wasi_oflags_t oflags, return __WASI_ENOMEM; } - snprintf(abs_path, MAX_FILE_NAME, "%s/%s", prestat_dir, path); - int zmode = - wasi_flags_to_zephyr(oflags, fd_flags, lookup_flags, access_mode); + if (!build_absolute_path(abs_path, sizeof(abs_path), path)) { + return __WASI_ENOMEM; + } - ptr = zephyr_fs_alloc_obj(false, abs_path, &index); + ptr = zephyr_fs_alloc_obj(oflags & __WASI_O_DIRECTORY, abs_path, &index); if (!ptr && (index < 0)) { BH_FREE(*out); return __WASI_EMFILE; } - fs_file_t_init(&ptr->file); - rc = fs_open(&ptr->file, abs_path, zmode); + if (oflags & __WASI_O_DIRECTORY) { + // Is a directory + fs_dir_t_init(&ptr->dir); + rc = fs_opendir(&ptr->dir, abs_path); + } + else { + // Is a file + int zmode = + wasi_flags_to_zephyr(oflags, fd_flags, lookup_flags, access_mode); + fs_file_t_init(&ptr->file); + rc = fs_open(&ptr->file, abs_path, zmode); + } + if (rc < 0) { zephyr_fs_free_obj(ptr); BH_FREE(*out); @@ -420,6 +485,12 @@ os_file_get_access_mode(os_file_handle handle, GET_FILE_SYSTEM_DESCRIPTOR(handle->fd, ptr); + if (ptr->is_dir) { + // DSK: is this actually the correct mode for a dir? + *access_mode = WASI_LIBC_ACCESS_MODE_READ_WRITE; + return __WASI_ESUCCESS; + } + if ((ptr->file.flags & FS_O_RDWR) != 0) { *access_mode = WASI_LIBC_ACCESS_MODE_READ_WRITE; } @@ -445,10 +516,17 @@ os_close(os_file_handle handle, bool is_stdio) if (is_stdio) return __WASI_ESUCCESS; - GET_FILE_SYSTEM_DESCRIPTOR(handle->fd, ptr); + if (handle->is_sock) { + rc = zsock_close(handle->fd); + } + // Handle is assumed to be a file descriptor + else { + GET_FILE_SYSTEM_DESCRIPTOR(handle->fd, ptr); + + rc = ptr->is_dir ? fs_closedir(&ptr->dir) : fs_close(&ptr->file); + zephyr_fs_free_obj(ptr); // free in any case. + } - rc = ptr->is_dir ? fs_closedir(&ptr->dir) : fs_close(&ptr->file); - zephyr_fs_free_obj(ptr); // free in any case. BH_FREE(handle); if (rc < 0) { return convert_errno(-rc); @@ -565,42 +643,50 @@ __wasi_errno_t os_writev(os_file_handle handle, const struct __wasi_ciovec_t *iov, int iovcnt, size_t *nwritten) { - struct zephyr_fs_desc *ptr = NULL; ssize_t total_written = 0; + struct zephyr_fs_desc *ptr = NULL; GET_FILE_SYSTEM_DESCRIPTOR(handle->fd, ptr); + // If the fd is stdout or stderr, we call fwrite if (strncmp(ptr->path, "std", 3) == 0) { - // for std[in/out/err] we don't write because they are not real opened - // files. Instead we emulate a write operation to make it work with - // printf. + // TODO -- FIX: if ((handle->fd == STDOUT_FILENO) || (handle->fd == + // STDERR_FILENO)) { + FILE *fd = stdout; + if (handle->fd == STDERR_FILENO) { + fd = stderr; + } + for (int i = 0; i < iovcnt; i++) { - if (iov[i].buf_len == 0) - continue; - os_printf("%s", (char *)iov[i].buf); - total_written += iov[i].buf_len; + ssize_t bytes_written = fwrite(iov[i].buf, 1, iov[i].buf_len, fd); - // Clear the buffer after printing - memset(iov[i].buf, 0, iov[i].buf_len); + if (bytes_written < 0) { + return convert_errno(-bytes_written); + } + + total_written += bytes_written; + + // If we wrote less than we asked for, stop writing + if (bytes_written < iov[i].buf_len) { + break; + } } - *nwritten = total_written; - - return __WASI_ESUCCESS; } + else { + // Write data from each buffer + for (int i = 0; i < iovcnt; i++) { + ssize_t bytes_written = + fs_write(&ptr->file, iov[i].buf, iov[i].buf_len); + if (bytes_written < 0) { + return convert_errno(-bytes_written); + } - // Write data from each buffer - for (int i = 0; i < iovcnt; i++) { - ssize_t bytes_written = - fs_write(&ptr->file, iov[i].buf, iov[i].buf_len); - if (bytes_written < 0) { - return convert_errno(-bytes_written); - } + total_written += bytes_written; - total_written += bytes_written; - - // If we wrote less than we asked for, stop writing - if (bytes_written < iov[i].buf_len) { - break; + // If we wrote less than we asked for, stop writing + if (bytes_written < iov[i].buf_len) { + break; + } } } @@ -679,7 +765,9 @@ os_mkdirat(os_file_handle handle, const char *path) return __WASI_EINVAL; // Or another appropriate error code } - snprintf(abs_path, MAX_FILE_NAME, "%s/%s", prestat_dir, path); + if (!build_absolute_path(abs_path, sizeof(abs_path), path)) { + return __WASI_ENOMEM; + } rc = fs_mkdir(abs_path); if (rc < 0) { @@ -715,13 +803,18 @@ os_renameat(os_file_handle old_handle, const char *old_path, GET_FILE_SYSTEM_DESCRIPTOR(old_handle->fd, ptr); - char *path = strdup(new_path); + char *path = duplicate_string(new_path); if (path == NULL) { return __WASI_ENOMEM; } - snprintf(abs_old_path, MAX_FILE_NAME, "%s/%s", prestat_dir, old_path); - snprintf(abs_new_path, MAX_FILE_NAME, "%s/%s", prestat_dir, new_path); + if (!build_absolute_path(abs_old_path, sizeof(abs_old_path), old_path)) { + return __WASI_ENOMEM; + } + + if (!build_absolute_path(abs_new_path, sizeof(abs_new_path), new_path)) { + return __WASI_ENOMEM; + } int rc = fs_rename(abs_old_path, abs_new_path); if (rc < 0) { @@ -743,13 +836,11 @@ os_unlinkat(os_file_handle handle, const char *path, bool is_dir) char abs_path[MAX_FILE_NAME + 1]; struct zephyr_fs_desc *ptr = NULL; - snprintf(abs_path, MAX_FILE_NAME, "%s/%s", prestat_dir, path); - - if (is_dir) { - return __WASI_ENOTDIR; + if (!build_absolute_path(abs_path, sizeof(abs_path), path)) { + return __WASI_ENOMEM; } - int rc = fs_unlink(path); + int rc = fs_unlink(abs_path); if (rc < 0) { return convert_errno(-rc); } @@ -947,7 +1038,7 @@ os_closedir(os_dir_stream dir_stream) os_dir_stream os_get_invalid_dir_stream() { - return NULL; + return OS_DIR_STREAM_INVALID; } bool @@ -976,11 +1067,12 @@ os_realpath(const char *path, char *resolved_path) * * (fs_file_t) file.mp->mnt_point * But we will just use absolute path for now. */ - if (!path) { - // Log error + if ((!path) || (strlen(path) > PATH_MAX)) { + // Invalid input, path has to be valid and less than PATH_MAX return NULL; } - return (const char *)path; + + return strncpy(resolved_path, path, PATH_MAX); } bool @@ -990,19 +1082,19 @@ os_compare_file_handle(os_file_handle handle1, os_file_handle handle2) } bool -os_is_stdin_handle(os_file_handle fd) +os_is_stdin_handle(os_file_handle handle) { - return fd == stdin; + return (handle == (os_file_handle)stdin); } bool -os_is_stdout_handle(os_file_handle fd) +os_is_stdout_handle(os_file_handle handle) { - return fd == stdout; + return (handle == (os_file_handle)stdout); } bool -os_is_stderr_handle(os_file_handle fd) +os_is_stderr_handle(os_file_handle handle) { - return fd == stderr; + return (handle == (os_file_handle)stderr); } \ No newline at end of file diff --git a/core/shared/platform/zephyr/zephyr_socket.c b/core/shared/platform/zephyr/zephyr_socket.c index 298dfd4b9..6d108623e 100644 --- a/core/shared/platform/zephyr/zephyr_socket.c +++ b/core/shared/platform/zephyr/zephyr_socket.c @@ -325,25 +325,31 @@ int os_socket_recv_from(bh_socket_t socket, void *buf, unsigned int len, int flags, bh_sockaddr_t *src_addr) { - struct sockaddr_storage addr = { 0 }; - struct sockaddr *temp_addr = (struct sockaddr *)&addr; - socklen_t socklen = sizeof(addr); + struct sockaddr_storage sock_addr = { 0 }; + socklen_t socklen = sizeof(sock_addr); int ret; - ret = zsock_recvfrom(socket->fd, buf, len, flags, (struct sockaddr *)&addr, - &socklen); + + ret = zsock_recvfrom(socket->fd, buf, len, flags, + (struct sockaddr *)&sock_addr, &socklen); + if (ret < 0) { return BHT_ERROR; } - // zsock_recvfrom doesn't seem to set `addr->sa_family` - // so we set it manually. - temp_addr->sa_family = src_addr->is_ipv4 == true ? AF_INET : AF_INET6; - if (src_addr && socklen > 0) { - if (sockaddr_to_bh_sockaddr(temp_addr, src_addr) == BHT_ERROR) { - return BHT_ERROR; + // zsock_recvfrom doesn't seem to set `addr->sa_family`, + // so we set it manually. + ((struct sockaddr *)&sock_addr)->sa_family = + src_addr->is_ipv4 == true ? AF_INET : AF_INET6; + + if (sockaddr_to_bh_sockaddr((struct sockaddr *)&sock_addr, src_addr) + == BHT_ERROR) { + return -1; } } + else if (src_addr) { + memset(src_addr, 0, sizeof(*src_addr)); + } return ret; } @@ -361,8 +367,7 @@ os_socket_send_to(bh_socket_t socket, const void *buf, unsigned int len, struct sockaddr_storage addr = { 0 }; socklen_t socklen; - (void)bh_sockaddr_to_sockaddr(dest_addr, (struct sockaddr *)&addr, - &socklen); + (void)bh_sockaddr_to_sockaddr(dest_addr, &addr, &socklen); return zsock_sendto(socket->fd, buf, len, flags, (struct sockaddr *)&addr, socklen); @@ -543,10 +548,8 @@ os_socket_get_send_buf_size(bh_socket_t socket, size_t *bufsiz) int os_socket_set_recv_buf_size(bh_socket_t socket, size_t bufsiz) { - int buf_size_int = (int)bufsiz; - - if (zsock_getsockopt(socket->fd, SOL_SOCKET, SO_RCVBUF, &buf_size_int, - sizeof(buf_size_int)) + if (zsock_setsockopt(socket->fd, SOL_SOCKET, SO_RCVBUF, &bufsiz, + sizeof(bufsiz)) != 0) { return BHT_ERROR; } @@ -805,16 +808,26 @@ int os_socket_get_tcp_keep_intvl(bh_socket_t socket, uint32_t *time_s) { #ifdef TCP_KEEPINTVL - assert(time_s); + if (!socket || !time_s || socket->fd < 0) { + errno = EINVAL; + return BHT_ERROR; + } + int time_s_int; socklen_t time_s_len = sizeof(time_s_int); - if (zsock_setsockopt(socket->fd, IPPROTO_TCP, TCP_KEEPINTVL, &time_s_int, + if (zsock_getsockopt(socket->fd, IPPROTO_TCP, TCP_KEEPINTVL, &time_s_int, &time_s_len) != 0) { return BHT_ERROR; } - *time_s = (uint32)time_s_int; + + if (time_s_int < 0) { + errno = EINVAL; + return BHT_ERROR; + } + + *time_s = (uint32_t)time_s_int; return BHT_OK; #else @@ -1031,14 +1044,15 @@ os_socket_get_broadcast(bh_socket_t socket, bool *is_enabled) int os_ioctl(os_file_handle handle, int request, ...) { - int ret = -1; - va_list args; + return __WASI_ENOSYS; + // int ret = -1; + // va_list args; - va_start(args, request); - ret = zsock_ioctl(handle->fd, request, args); - va_end(args); + // va_start(args, request); + // ret = zsock_ioctl(handle->fd, request, args); + // va_end(args); - return ret; + // return ret; } int