Skip to content

Commit c505da7

Browse files
authored
Update __wasi_sock_accept signature to match wasi_snapshot_preview1 (#1531)
The function was introduced to WASI about half a year ago after it already existed in WAMR. It caused problems with compiling `wasi_socket_ext.c` with the wasi-sdk that already had this hostcall exported (wasi-sdk >= 15). The approach we take is the following: - we update WASI interface to be compatible with the wasi_snapshot_preview1 - compilation with `wasi_socket_ext.c` supports both wasi_sdk >= 15 and wasi_sdk < 15 (although we intend to drop support for < 15 at one point of time) - we override `accept()` from wasi-libc - we do that because `accept()` in `wasi-libc` doesn't support returning address (as it doesn't have `getpeername()` implemented), so `wasi_socket_ext.c` offers more functionality right now Resolves #1167 and #1528. [1] https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/witx/wasi_snapshot_preview1.witx
1 parent dfd16f8 commit c505da7

File tree

5 files changed

+32
-20
lines changed

5 files changed

+32
-20
lines changed

core/iwasm/libraries/lib-socket/inc/wasi_socket_ext.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,10 @@ struct addrinfo {
140140
struct addrinfo *ai_next; /* Pointer to next in list. */
141141
};
142142

143+
#ifndef __WASI_RIGHTS_SOCK_ACCEPT
143144
int
144145
accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
146+
#endif
145147

146148
int
147149
bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
@@ -191,21 +193,30 @@ void
191193
freeaddrinfo(struct addrinfo *res);
192194
#endif
193195

196+
/**
197+
* __wasi_sock_accept was introduced in wasi-sdk v15. To
198+
* temporarily maintain backward compatibility with the old
199+
* wasi-sdk, we explicitly add that implementation here so it works
200+
* with older versions of the SDK.
201+
*/
202+
#ifndef __WASI_RIGHTS_SOCK_ACCEPT
194203
/**
195204
* Accept a connection on a socket
196205
* Note: This is similar to `accept`
197206
*/
198207
int32_t
199-
__imported_wasi_snapshot_preview1_sock_accept(int32_t arg0, int32_t arg1)
208+
__imported_wasi_snapshot_preview1_sock_accept(int32_t arg0, int32_t arg1,
209+
int32_t arg2)
200210
__attribute__((__import_module__("wasi_snapshot_preview1"),
201211
__import_name__("sock_accept")));
202212

203213
static inline __wasi_errno_t
204-
__wasi_sock_accept(__wasi_fd_t fd, __wasi_fd_t *fd_new)
214+
__wasi_sock_accept(__wasi_fd_t fd, __wasi_fdflags_t flags, __wasi_fd_t *fd_new)
205215
{
206216
return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_accept(
207-
(int32_t)fd, (int32_t)fd_new);
217+
(int32_t)fd, (int32_t)flags, (int32_t)fd_new);
208218
}
219+
#endif
209220

210221
/**
211222
* Returns the local address to which the socket is bound.

core/iwasm/libraries/lib-socket/src/wasi/wasi_socket_ext.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
140140
__wasi_fd_t new_sockfd;
141141
__wasi_errno_t error;
142142

143-
error = __wasi_sock_accept(sockfd, &new_sockfd);
143+
error = __wasi_sock_accept(sockfd, 0, &new_sockfd);
144144
HANDLE_ERROR(error)
145145

146146
error = getpeername(new_sockfd, addr, addrlen);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,8 @@ wasi_random_get(wasm_exec_env_t exec_env, void *buf, uint32 buf_len)
10041004
}
10051005

10061006
static wasi_errno_t
1007-
wasi_sock_accept(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_fd_t *fd_new)
1007+
wasi_sock_accept(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_fdflags_t flags,
1008+
wasi_fd_t *fd_new)
10081009
{
10091010
wasm_module_inst_t module_inst = get_module_inst(exec_env);
10101011
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
@@ -1015,7 +1016,7 @@ wasi_sock_accept(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_fd_t *fd_new)
10151016

10161017
curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
10171018

1018-
return wasi_ssp_sock_accept(curfds, fd, fd_new);
1019+
return wasi_ssp_sock_accept(curfds, fd, flags, fd_new);
10191020
}
10201021

10211022
static wasi_errno_t
@@ -2156,7 +2157,7 @@ static NativeSymbol native_symbols_libc_wasi[] = {
21562157
REG_NATIVE_FUNC(proc_exit, "(i)"),
21572158
REG_NATIVE_FUNC(proc_raise, "(i)i"),
21582159
REG_NATIVE_FUNC(random_get, "(*~)i"),
2159-
REG_NATIVE_FUNC(sock_accept, "(i*)i"),
2160+
REG_NATIVE_FUNC(sock_accept, "(ii*)i"),
21602161
REG_NATIVE_FUNC(sock_addr_local, "(i*)i"),
21612162
REG_NATIVE_FUNC(sock_addr_remote, "(i*)i"),
21622163
REG_NATIVE_FUNC(sock_addr_resolve, "($$**i*)i"),

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ wasi_ssp_sock_accept(
10081008
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
10091009
struct fd_table *curfds,
10101010
#endif
1011-
__wasi_fd_t fd, __wasi_fd_t *fd_new
1011+
__wasi_fd_t fd, __wasi_fdflags_t flags, __wasi_fd_t *fd_new
10121012
) __attribute__((__warn_unused_result__));
10131013

10141014
__wasi_errno_t
@@ -1199,7 +1199,7 @@ __wasi_errno_t wasmtime_ssp_sock_get_keep_alive(
11991199
struct fd_table *curfds,
12001200
#endif
12011201
__wasi_fd_t sock,
1202-
bool *is_enabled
1202+
bool *is_enabled
12031203
) WASMTIME_SSP_SYSCALL_NAME(sock_get_keep_alive) __attribute__((__warn_unused_result__));
12041204

12051205
__wasi_errno_t wasmtime_ssp_sock_set_reuse_addr(
@@ -1215,7 +1215,7 @@ __wasi_errno_t wasmtime_ssp_sock_get_reuse_addr(
12151215
struct fd_table *curfds,
12161216
#endif
12171217
__wasi_fd_t sock,
1218-
bool *is_enabled
1218+
bool *is_enabled
12191219
) WASMTIME_SSP_SYSCALL_NAME(sock_get_reuse_addr) __attribute__((__warn_unused_result__));
12201220

12211221
__wasi_errno_t wasmtime_ssp_sock_set_reuse_port(
@@ -1231,15 +1231,15 @@ __wasi_errno_t wasmtime_ssp_sock_get_reuse_port(
12311231
struct fd_table *curfds,
12321232
#endif
12331233
__wasi_fd_t sock,
1234-
bool *is_enabled
1234+
bool *is_enabled
12351235
) WASMTIME_SSP_SYSCALL_NAME(sock_get_reuse_port) __attribute__((__warn_unused_result__));
12361236

12371237
__wasi_errno_t wasmtime_ssp_sock_set_linger(
12381238
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
12391239
struct fd_table *curfds,
12401240
#endif
1241-
__wasi_fd_t sock,
1242-
bool is_enabled,
1241+
__wasi_fd_t sock,
1242+
bool is_enabled,
12431243
int linger_s
12441244
) WASMTIME_SSP_SYSCALL_NAME(sock_set_linger) __attribute__((__warn_unused_result__));
12451245

@@ -1263,7 +1263,7 @@ __wasi_errno_t wasmtime_ssp_sock_get_broadcast(
12631263
struct fd_table *curfds,
12641264
#endif
12651265
__wasi_fd_t sock,
1266-
bool *is_enabled
1266+
bool *is_enabled
12671267
) WASMTIME_SSP_SYSCALL_NAME(sock_get_broadcast) __attribute__((__warn_unused_result__));
12681268

12691269
__wasi_errno_t wasmtime_ssp_sock_set_tcp_no_delay(
@@ -1279,7 +1279,7 @@ __wasi_errno_t wasmtime_ssp_sock_get_tcp_no_delay(
12791279
struct fd_table *curfds,
12801280
#endif
12811281
__wasi_fd_t sock,
1282-
bool *is_enabled
1282+
bool *is_enabled
12831283
) WASMTIME_SSP_SYSCALL_NAME(sock_get_tcp_no_delay) __attribute__((__warn_unused_result__));
12841284

12851285
__wasi_errno_t wasmtime_ssp_sock_set_tcp_quick_ack(
@@ -1295,7 +1295,7 @@ __wasi_errno_t wasmtime_ssp_sock_get_tcp_quick_ack(
12951295
struct fd_table *curfds,
12961296
#endif
12971297
__wasi_fd_t sock,
1298-
bool *is_enabled
1298+
bool *is_enabled
12991299
) WASMTIME_SSP_SYSCALL_NAME(sock_get_tcp_quick_ack) __attribute__((__warn_unused_result__));
13001300

13011301
__wasi_errno_t wasmtime_ssp_sock_set_tcp_keep_idle(
@@ -1343,7 +1343,7 @@ __wasi_errno_t wasmtime_ssp_sock_get_tcp_fastopen_connect(
13431343
struct fd_table *curfds,
13441344
#endif
13451345
__wasi_fd_t sock,
1346-
bool *is_enabled
1346+
bool *is_enabled
13471347
) WASMTIME_SSP_SYSCALL_NAME(sock_get_tcp_fastopen_connect) __attribute__((__warn_unused_result__));
13481348

13491349
__wasi_errno_t wasmtime_ssp_sock_set_ip_multicast_loop(
@@ -1361,7 +1361,7 @@ __wasi_errno_t wasmtime_ssp_sock_get_ip_multicast_loop(
13611361
#endif
13621362
__wasi_fd_t sock,
13631363
bool ipv6,
1364-
bool *is_enabled
1364+
bool *is_enabled
13651365
) WASMTIME_SSP_SYSCALL_NAME(sock_get_ip_multicast_loop) __attribute__((__warn_unused_result__));
13661366

13671367
__wasi_errno_t wasmtime_ssp_sock_set_ip_add_membership(
@@ -1427,7 +1427,7 @@ __wasi_errno_t wasmtime_ssp_sock_get_ipv6_only(
14271427
struct fd_table *curfds,
14281428
#endif
14291429
__wasi_fd_t sock,
1430-
bool *is_enabled
1430+
bool *is_enabled
14311431
) WASMTIME_SSP_SYSCALL_NAME(sock_get_ipv6_only) __attribute__((__warn_unused_result__));
14321432

14331433
__wasi_errno_t wasmtime_ssp_sched_yield(void)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2905,7 +2905,7 @@ wasi_ssp_sock_accept(
29052905
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
29062906
struct fd_table *curfds,
29072907
#endif
2908-
__wasi_fd_t fd, __wasi_fd_t *fd_new)
2908+
__wasi_fd_t fd, __wasi_fdflags_t flags, __wasi_fd_t *fd_new)
29092909
{
29102910
__wasi_filetype_t wasi_type;
29112911
__wasi_rights_t max_base, max_inheriting;

0 commit comments

Comments
 (0)