Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the missing sock_accept for preview1 #3971

Merged
merged 4 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 5 additions & 4 deletions lib/wasix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ fn wasi_snapshot_preview1_exports(
"proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
"random_get" => Function::new_typed_with_env(&mut store, env, random_get::<Memory32>),
"sched_yield" => Function::new_typed_with_env(&mut store, env, sched_yield::<Memory32>),
"sock_accept" => Function::new_typed_with_env(&mut store, env, sock_accept::<Memory32>),
"sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv::<Memory32>),
"sock_send" => Function::new_typed_with_env(&mut store, env, sock_send::<Memory32>),
"sock_shutdown" => Function::new_typed_with_env(&mut store, env, sock_shutdown),
Expand Down Expand Up @@ -504,8 +505,8 @@ fn wasix_exports_32(mut store: &mut impl AsStoreMut, env: &FunctionEnv<WasiEnv>)
"sock_leave_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v6::<Memory32>),
"sock_bind" => Function::new_typed_with_env(&mut store, env, sock_bind::<Memory32>),
"sock_listen" => Function::new_typed_with_env(&mut store, env, sock_listen::<Memory32>),
"sock_accept" => Function::new_typed_with_env(&mut store, env, sock_accept::<Memory32>),
"sock_accept_v2" => Function::new_typed_with_env(&mut store, env, sock_accept::<Memory32>),
"sock_accept" => Function::new_typed_with_env(&mut store, env, sock_accept_v2::<Memory32>),
"sock_accept_v2" => Function::new_typed_with_env(&mut store, env, sock_accept_v2::<Memory32>),
"sock_connect" => Function::new_typed_with_env(&mut store, env, sock_connect::<Memory32>),
"sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv::<Memory32>),
"sock_recv_from" => Function::new_typed_with_env(&mut store, env, sock_recv_from::<Memory32>),
Expand Down Expand Up @@ -622,8 +623,8 @@ fn wasix_exports_64(mut store: &mut impl AsStoreMut, env: &FunctionEnv<WasiEnv>)
"sock_leave_multicast_v6" => Function::new_typed_with_env(&mut store, env, sock_leave_multicast_v6::<Memory64>),
"sock_bind" => Function::new_typed_with_env(&mut store, env, sock_bind::<Memory64>),
"sock_listen" => Function::new_typed_with_env(&mut store, env, sock_listen::<Memory64>),
"sock_accept" => Function::new_typed_with_env(&mut store, env, sock_accept::<Memory64>),
"sock_accept_v2" => Function::new_typed_with_env(&mut store, env, sock_accept::<Memory64>),
"sock_accept" => Function::new_typed_with_env(&mut store, env, sock_accept_v2::<Memory64>),
"sock_accept_v2" => Function::new_typed_with_env(&mut store, env, sock_accept_v2::<Memory64>),
"sock_connect" => Function::new_typed_with_env(&mut store, env, sock_connect::<Memory64>),
"sock_recv" => Function::new_typed_with_env(&mut store, env, sock_recv::<Memory64>),
"sock_recv_from" => Function::new_typed_with_env(&mut store, env, sock_recv_from::<Memory64>),
Expand Down
80 changes: 62 additions & 18 deletions lib/wasix/src/syscalls/wasix/sock_accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,68 @@ pub fn sock_accept<M: MemorySize>(
sock: WasiFd,
mut fd_flags: Fdflags,
ro_fd: WasmPtr<WasiFd, M>,
) -> Result<Errno, WasiError> {
theduke marked this conversation as resolved.
Show resolved Hide resolved
wasi_try_ok!(WasiEnv::process_signals_and_exit(&mut ctx)?);

let env = ctx.data();
let (memory, state, _) = unsafe { env.get_memory_and_wasi_state_and_inodes(&ctx, 0) };

let (fd, addr) = wasi_try_ok!(sock_accept_internal::<M>(env, sock, fd_flags));

wasi_try_mem_ok!(ro_fd.write(&memory, fd));

Ok(Errno::Success)
}

/// ### `sock_accept()`
/// Accept a new incoming connection.
/// Note: This is similar to `accept` in POSIX.
///
/// ## Parameters
///
/// * `fd` - The listening socket.
/// * `flags` - The desired values of the file descriptor flags.
///
/// ## Return
///
/// New socket connection
#[instrument(level = "debug", skip_all, fields(%sock, fd = field::Empty), ret, err)]
pub fn sock_accept_v2<M: MemorySize>(
mut ctx: FunctionEnvMut<'_, WasiEnv>,
sock: WasiFd,
mut fd_flags: Fdflags,
ro_fd: WasmPtr<WasiFd, M>,
ro_addr: WasmPtr<__wasi_addr_port_t, M>,
) -> Result<Errno, WasiError> {
wasi_try_ok!(WasiEnv::process_signals_and_exit(&mut ctx)?);

let tasks = ctx.data().tasks().clone();
let (child, addr, fd_flags) = wasi_try_ok!(__sock_asyncify(
ctx.data(),
let env = ctx.data();
let (memory, state, _) = unsafe { env.get_memory_and_wasi_state_and_inodes(&ctx, 0) };

let (fd, addr) = wasi_try_ok!(sock_accept_internal::<M>(env, sock, fd_flags));

wasi_try_mem_ok!(ro_fd.write(&memory, fd));
wasi_try_ok!(crate::net::write_ip_port(
&memory,
ro_addr,
addr.ip(),
addr.port()
));

Ok(Errno::Success)
}

pub fn sock_accept_internal<M: MemorySize>(
env: &WasiEnv,
sock: WasiFd,
mut fd_flags: Fdflags,
) -> Result<(WasiFd, SocketAddr), Errno> {
let state = env.state();
let inodes = &state.inodes;

let tasks = env.tasks().clone();
let (child, addr, fd_flags) = __sock_asyncify(
env,
sock,
Rights::SOCK_ACCEPT,
move |socket, fd| async move {
Expand All @@ -36,11 +91,8 @@ pub fn sock_accept<M: MemorySize>(
.accept(tasks.deref(), fd_flags)
.await
.map(|a| (a.0, a.1, fd_flags))
}
));

let env = ctx.data();
let (memory, state, inodes) = unsafe { env.get_memory_and_wasi_state_and_inodes(&ctx, 0) };
},
)?;

let kind = Kind::Socket {
socket: InodeSocket::new(InodeSocketKind::TcpStream {
Expand All @@ -64,16 +116,8 @@ pub fn sock_accept<M: MemorySize>(
}

let rights = Rights::all_socket();
let fd = wasi_try_ok!(state.fs.create_fd(rights, rights, new_flags, 0, inode));
let fd = state.fs.create_fd(rights, rights, new_flags, 0, inode)?;
Span::current().record("fd", fd);

wasi_try_mem_ok!(ro_fd.write(&memory, fd));
wasi_try_ok!(crate::net::write_ip_port(
&memory,
ro_addr,
addr.ip(),
addr.port()
));

Ok(Errno::Success)
Ok((fd, addr))
}