Skip to content

Commit 96a2cfb

Browse files
committed
Zephyr platform fixes for WASI sockets
Addressed numerous of build warnings on Zephyr Signed-off-by: Stephen Berard <stephen.berard@outlook.com>
1 parent cee5533 commit 96a2cfb

7 files changed

Lines changed: 183 additions & 101 deletions

File tree

core/iwasm/common/wasm_application.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
289289
exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
290290
if (exec_env) {
291291
wasm_runtime_dump_mem_consumption(exec_env);
292+
(WASMModuleInstance *)module_inst->cur_exception
292293
}
293294
#endif
294295

@@ -712,7 +713,10 @@ execute_func(WASMModuleInstanceCommon *module_inst, const char *name,
712713
}
713714
case VALUE_TYPE_F32:
714715
{
715-
os_printf("%.7g:f32", *(float32 *)(argv1 + k));
716+
// Explicit cast to double to avoid warning.
717+
// Float arguments are promoted to double in variadic
718+
// functions per section 6.5.2.2 of the C99 standard.
719+
os_printf("%.7g:f32", (double) *(float32 *)(argv1 + k));
716720
k++;
717721
break;
718722
}

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,12 +1004,10 @@ update_clock_subscription_data(wasi_subscription_t *in, uint32 nsubscriptions,
10041004
}
10051005

10061006
static wasi_errno_t
1007-
execute_interruptible_poll_oneoff(
1008-
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
1009-
struct fd_table *curfds,
1010-
#endif
1011-
const __wasi_subscription_t *in, __wasi_event_t *out, size_t nsubscriptions,
1012-
size_t *nevents, wasm_exec_env_t exec_env)
1007+
execute_interruptible_poll_oneoff(struct fd_table *curfds,
1008+
const __wasi_subscription_t *in,
1009+
__wasi_event_t *out, size_t nsubscriptions,
1010+
size_t *nevents, wasm_exec_env_t exec_env)
10131011
{
10141012
if (nsubscriptions == 0) {
10151013
*nevents = 0;
@@ -2106,15 +2104,16 @@ wasi_sock_recv(wasm_exec_env_t exec_env, wasi_fd_t sock, iovec_app_t *ri_data,
21062104
wasi_roflags_t *ro_flags)
21072105
{
21082106
wasm_module_inst_t module_inst = get_module_inst(exec_env);
2109-
__wasi_addr_t src_addr;
21102107
wasi_errno_t error;
21112108

21122109
if (!validate_native_addr(ro_flags, (uint64)sizeof(wasi_roflags_t)))
21132110
return __WASI_EINVAL;
21142111

2112+
// We call `recvfrom` with NULL source address as `recv` doesn't
2113+
// return the source address and this parameter is not used.
2114+
*ro_data_len = 0;
21152115
error = wasi_sock_recv_from(exec_env, sock, ri_data, ri_data_len, ri_flags,
2116-
&src_addr, ro_data_len);
2117-
*ro_flags = ri_flags;
2116+
NULL, ro_data_len);
21182117

21192118
return error;
21202119
}

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,10 +1891,10 @@ convert_timestamp(__wasi_timestamp_t in, os_timespec *out)
18911891
#else
18921892
out->tv_nsec = (long)(in % 1000000000);
18931893
#endif
1894-
in /= 1000000000;
1894+
__wasi_timestamp_t temp = in / 1000000000;
18951895

18961896
// Clamp to the maximum in case it would overflow our system's time_t.
1897-
out->tv_sec = (time_t)in < BH_TIME_T_MAX ? (time_t)in : BH_TIME_T_MAX;
1897+
out->tv_sec = (time_t)temp < BH_TIME_T_MAX ? (time_t)temp : BH_TIME_T_MAX;
18981898
}
18991899

19001900
__wasi_errno_t
@@ -2081,7 +2081,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds,
20812081
size_t nsubscriptions,
20822082
size_t *nevents) NO_LOCK_ANALYSIS
20832083
{
2084-
#if defined(BH_PLATFORM_WINDOWS) || defined(BH_PLATFORM_ZEPHYR)
2084+
#if defined(BH_PLATFORM_WINDOWS)
20852085
return __WASI_ENOSYS;
20862086
#else
20872087
// Sleeping.
@@ -2199,7 +2199,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds,
21992199
if (error == 0) {
22002200
// Proper file descriptor on which we can poll().
22012201
pfds[i] = (os_poll_file_handle){
2202-
.fd = fos[i]->file_handle,
2202+
.fd = fos[i]->file_handle->fd,
22032203
.events = s->u.type == __WASI_EVENTTYPE_FD_READ
22042204
? POLLIN
22052205
: POLLOUT,
@@ -2832,25 +2832,37 @@ wasmtime_ssp_sock_recv_from(wasm_exec_env_t exec_env, struct fd_table *curfds,
28322832
{
28332833
struct fd_object *fo;
28342834
__wasi_errno_t error;
2835-
bh_sockaddr_t sockaddr;
2835+
bh_sockaddr_t sockaddr, *sockaddr_ptr = NULL;
28362836
int ret;
28372837

28382838
error = fd_object_get(curfds, &fo, sock, __WASI_RIGHT_FD_READ, 0);
28392839
if (error != 0) {
28402840
return error;
28412841
}
28422842

2843-
wasi_addr_to_bh_sockaddr(src_addr, &sockaddr);
2843+
// If the source address is not NULL, the caller is requesting the source
2844+
// address to be returned if the protocol supports it. As such, we convert
2845+
// the format of the structure pass in prior to the call to the OS
2846+
// implementation. If the value is NULL, the POSIX standard states that
2847+
// the address is not returned.
2848+
if (src_addr != NULL) {
2849+
sockaddr_ptr = &sockaddr;
2850+
wasi_addr_to_bh_sockaddr(src_addr, &sockaddr);
2851+
}
28442852

28452853
/* Consume bh_sockaddr_t instead of __wasi_addr_t */
28462854
ret = blocking_op_socket_recv_from(exec_env, fo->file_handle, buf, buf_len,
2847-
0, &sockaddr);
2855+
0, sockaddr_ptr);
28482856
fd_object_release(exec_env, fo);
28492857
if (-1 == ret) {
28502858
return convert_errno(errno);
28512859
}
28522860

2853-
bh_sockaddr_to_wasi_addr(&sockaddr, src_addr);
2861+
// If the source address is not NULL, we need to convert the sockaddr
2862+
// back to __wasi_addr_t format.
2863+
if (src_addr != NULL) {
2864+
bh_sockaddr_to_wasi_addr(sockaddr_ptr, src_addr);
2865+
}
28542866

28552867
*recv_len = (size_t)ret;
28562868
return __WASI_ESUCCESS;

core/shared/mem-alloc/ems/ems_gc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
#include "ems_gc.h"
77
#include "ems_gc_internal.h"
88

9+
#ifndef GB // Some platforms define already, causing build warnings.
910
#define GB (1 << 30UL)
11+
#endif
1012

1113
#define MARK_NODE_OBJ_CNT 256
1214

core/shared/platform/zephyr/platform_internal.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@
8080
#define BH_PLATFORM_ZEPHYR
8181
#endif
8282

83+
#include <limits.h>
84+
85+
#ifndef PATH_MAX
86+
#define PATH_MAX 256
87+
#endif
88+
8389
/* Synchronization primitives for usermode.
8490
* The macros are prefixed with 'z' because when building
8591
* with WAMR_BUILD_LIBC_WASI the same functions are defined,
@@ -222,6 +228,8 @@ set_exec_mem_alloc_func(exec_mem_alloc_func_t alloc_func,
222228
typedef int os_dir_stream;
223229
typedef int os_raw_file_handle;
224230

231+
#define OS_DIR_STREAM_INVALID 0
232+
225233
// handle for file system descriptor
226234
typedef struct zephyr_fs_desc {
227235
char *path;
@@ -259,20 +267,19 @@ typedef unsigned int os_nfds_t;
259267

260268
#define FIONREAD ZFD_IOCTL_FIONREAD
261269

262-
typedef struct {
263-
time_t tv_sec;
264-
long tv_nsec;
265-
} os_timespec;
270+
typedef struct timespec os_timespec;
266271

272+
#ifndef CLOCK_REALTIME
267273
#define CLOCK_REALTIME 1
274+
#endif
275+
268276
#define CLOCK_MONOTONIC 4
269277

270-
// TODO: use it in sandboxed posix.c.
271-
// int os_sched_yield(void)
272-
// {
273-
// k_yield();
274-
// return 0;
275-
// }
278+
static inline int os_sched_yield(void)
279+
{
280+
k_yield();
281+
return 0;
282+
}
276283

277284
static inline os_file_handle
278285
os_get_invalid_handle(void)

0 commit comments

Comments
 (0)