Skip to content

Commit 150122b

Browse files
lum1n0usdkouba-atym
andcommitted
Update zephyr file socket branch (#4406)
* Added os_is_* methods for stdin/stdout/stderr. Fixed issues in os_renameat * Zephyr platform fixes for WASI sockets Addressed numerous of build warnings on Zephyr * Updated `os_writev` to use `fwrite` for STDOUT/STDERR * Temporarily reverted change to `writev` to work around an issue. * Fixes: fstat, fstatat, and unlink * Add initial support for directories in os_openat. Partial implementation — just avoids a hard fault. Signed-off-by: Stephen Berard <[email protected]> Co-authored-by: Dan Kouba <[email protected]>
1 parent 068426f commit 150122b

File tree

7 files changed

+277
-132
lines changed

7 files changed

+277
-132
lines changed

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
@@ -1016,12 +1016,10 @@ update_clock_subscription_data(wasi_subscription_t *in, uint32 nsubscriptions,
10161016
}
10171017

10181018
static wasi_errno_t
1019-
execute_interruptible_poll_oneoff(
1020-
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
1021-
struct fd_table *curfds,
1022-
#endif
1023-
const __wasi_subscription_t *in, __wasi_event_t *out, size_t nsubscriptions,
1024-
size_t *nevents, wasm_exec_env_t exec_env)
1019+
execute_interruptible_poll_oneoff(struct fd_table *curfds,
1020+
const __wasi_subscription_t *in,
1021+
__wasi_event_t *out, size_t nsubscriptions,
1022+
size_t *nevents, wasm_exec_env_t exec_env)
10251023
{
10261024
if (nsubscriptions == 0) {
10271025
*nevents = 0;
@@ -2118,15 +2116,16 @@ wasi_sock_recv(wasm_exec_env_t exec_env, wasi_fd_t sock, iovec_app_t *ri_data,
21182116
wasi_roflags_t *ro_flags)
21192117
{
21202118
wasm_module_inst_t module_inst = get_module_inst(exec_env);
2121-
__wasi_addr_t src_addr;
21222119
wasi_errno_t error;
21232120

21242121
if (!validate_native_addr(ro_flags, (uint64)sizeof(wasi_roflags_t)))
21252122
return __WASI_EINVAL;
21262123

2124+
// We call `recvfrom` with NULL source address as `recv` doesn't
2125+
// return the source address and this parameter is not used.
2126+
*ro_data_len = 0;
21272127
error = wasi_sock_recv_from(exec_env, sock, ri_data, ri_data_len, ri_flags,
2128-
&src_addr, ro_data_len);
2129-
*ro_flags = ri_flags;
2128+
NULL, ro_data_len);
21302129

21312130
return error;
21322131
}

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,10 +1904,10 @@ convert_timestamp(__wasi_timestamp_t in, os_timespec *out)
19041904
#else
19051905
out->tv_nsec = (long)(in % 1000000000);
19061906
#endif
1907-
in /= 1000000000;
1907+
__wasi_timestamp_t temp = in / 1000000000;
19081908

19091909
// Clamp to the maximum in case it would overflow our system's time_t.
1910-
out->tv_sec = (time_t)in < BH_TIME_T_MAX ? (time_t)in : BH_TIME_T_MAX;
1910+
out->tv_sec = (time_t)temp < BH_TIME_T_MAX ? (time_t)temp : BH_TIME_T_MAX;
19111911
}
19121912

19131913
__wasi_errno_t
@@ -2094,7 +2094,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds,
20942094
size_t nsubscriptions,
20952095
size_t *nevents) NO_LOCK_ANALYSIS
20962096
{
2097-
#if defined(BH_PLATFORM_WINDOWS) || defined(BH_PLATFORM_ZEPHYR)
2097+
#if defined(BH_PLATFORM_WINDOWS)
20982098
return __WASI_ENOSYS;
20992099
#else
21002100
// Sleeping.
@@ -2212,7 +2212,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds,
22122212
if (error == 0) {
22132213
// Proper file descriptor on which we can poll().
22142214
pfds[i] = (os_poll_file_handle){
2215-
.fd = fos[i]->file_handle,
2215+
.fd = fos[i]->file_handle->fd,
22162216
.events = s->u.type == __WASI_EVENTTYPE_FD_READ
22172217
? POLLIN
22182218
: POLLOUT,
@@ -2845,23 +2845,37 @@ wasmtime_ssp_sock_recv_from(wasm_exec_env_t exec_env, struct fd_table *curfds,
28452845
{
28462846
struct fd_object *fo;
28472847
__wasi_errno_t error;
2848-
bh_sockaddr_t sockaddr;
2848+
bh_sockaddr_t sockaddr, *sockaddr_ptr = NULL;
28492849
int ret;
28502850

28512851
error = fd_object_get(curfds, &fo, sock, __WASI_RIGHT_FD_READ, 0);
28522852
if (error != 0) {
28532853
return error;
28542854
}
28552855

2856+
// If the source address is not NULL, the caller is requesting the source
2857+
// address to be returned if the protocol supports it. As such, we convert
2858+
// the format of the structure pass in prior to the call to the OS
2859+
// implementation. If the value is NULL, the POSIX standard states that
2860+
// the address is not returned.
2861+
if (src_addr != NULL) {
2862+
sockaddr_ptr = &sockaddr;
2863+
wasi_addr_to_bh_sockaddr(src_addr, &sockaddr);
2864+
}
2865+
28562866
/* Consume bh_sockaddr_t instead of __wasi_addr_t */
28572867
ret = blocking_op_socket_recv_from(exec_env, fo->file_handle, buf, buf_len,
2858-
0, &sockaddr);
2868+
0, sockaddr_ptr);
28592869
fd_object_release(exec_env, fo);
28602870
if (-1 == ret) {
28612871
return convert_errno(errno);
28622872
}
28632873

2864-
bh_sockaddr_to_wasi_addr(&sockaddr, src_addr);
2874+
// If the source address is not NULL, we need to convert the sockaddr
2875+
// back to __wasi_addr_t format.
2876+
if (src_addr != NULL) {
2877+
bh_sockaddr_to_wasi_addr(sockaddr_ptr, src_addr);
2878+
}
28652879

28662880
*recv_len = (size_t)ret;
28672881
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: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,24 @@
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+
89+
#ifndef STDIN_FILENO
90+
#define STDIN_FILENO 0
91+
#endif
92+
93+
#ifndef STDOUT_FILENO
94+
#define STDOUT_FILENO 1
95+
#endif
96+
97+
#ifndef STDERR_FILENO
98+
#define STDERR_FILENO 2
99+
#endif
100+
83101
/* Synchronization primitives for usermode.
84102
* The macros are prefixed with 'z' because when building
85103
* 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,
222240
typedef int os_dir_stream;
223241
typedef int os_raw_file_handle;
224242

243+
#define OS_DIR_STREAM_INVALID 0
244+
225245
// handle for file system descriptor
226246
typedef struct zephyr_fs_desc {
227247
char *path;
@@ -259,20 +279,20 @@ typedef unsigned int os_nfds_t;
259279

260280
#define FIONREAD ZFD_IOCTL_FIONREAD
261281

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

284+
#ifndef CLOCK_REALTIME
267285
#define CLOCK_REALTIME 1
286+
#endif
287+
268288
#define CLOCK_MONOTONIC 4
269289

270-
// TODO: use it in sandboxed posix.c.
271-
// int os_sched_yield(void)
272-
// {
273-
// k_yield();
274-
// return 0;
275-
// }
290+
static inline int
291+
os_sched_yield(void)
292+
{
293+
k_yield();
294+
return 0;
295+
}
276296

277297
static inline os_file_handle
278298
os_get_invalid_handle(void)

0 commit comments

Comments
 (0)