Skip to content

Commit

Permalink
Fix wasmer-wasi networking for 3.0 API
Browse files Browse the repository at this point in the history
  • Loading branch information
epilys committed Jun 17, 2022
1 parent 42f18f8 commit 66e1315
Show file tree
Hide file tree
Showing 11 changed files with 1,038 additions and 1,044 deletions.
4 changes: 2 additions & 2 deletions lib/c-api/src/wasm_c_api/unstable/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::super::{
wasi::wasi_env_t,
};
use wasmer_api::{Exportable, Extern};
use wasmer_wasi::{generate_import_object_from_env, get_wasi_version};
use wasmer_wasi::{generate_import_object_from_ctx, get_wasi_version};

/// Unstable non-standard type wrapping `wasm_extern_t` with the
/// addition of two `wasm_name_t` respectively for the module name and
Expand Down Expand Up @@ -170,7 +170,7 @@ fn wasi_get_unordered_imports_inner(
let version = c_try!(get_wasi_version(&module.inner, false)
.ok_or("could not detect a WASI version on the given module"));

let import_object = generate_import_object_from_env(store, wasi_env.inner.clone(), version);
let import_object = generate_import_object_from_ctx(store, wasi_env.inner.clone(), version);

imports.set_buffer(
import_object
Expand Down
4 changes: 2 additions & 2 deletions lib/c-api/src/wasm_c_api/wasi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::os::raw::c_char;
use std::slice;
use wasmer_api::{Exportable, Extern};
use wasmer_wasi::{
generate_import_object_from_env, get_wasi_version, Pipe, WasiEnv, WasiFile, WasiState,
generate_import_object_from_ctx, get_wasi_version, Pipe, WasiEnv, WasiFile, WasiState,
WasiStateBuilder, WasiVersion,
};

Expand Down Expand Up @@ -343,7 +343,7 @@ fn wasi_get_imports_inner(
let version = c_try!(get_wasi_version(&module.inner, false)
.ok_or("could not detect a WASI version on the given module"));

let import_object = generate_import_object_from_env(store, wasi_env.inner.clone(), version);
let import_object = generate_import_object_from_ctx(store, wasi_env.inner.clone(), version);

imports.set_buffer(c_try!(module
.inner
Expand Down
648 changes: 274 additions & 374 deletions lib/wasi/src/lib.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/wasi/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ macro_rules! get_input_str {
}

macro_rules! get_input_str_bus {
($memory:expr, $data:expr, $len:expr) => {{
wasi_try_mem_bus!($data.read_utf8_string($memory, $len))
($ctx:expr, $memory:expr, $data:expr, $len:expr) => {{
wasi_try_mem_bus!($data.read_utf8_string($ctx, $memory, $len))
}};
}
9 changes: 6 additions & 3 deletions lib/wasi/src/state/pipe.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::syscalls::types::*;
use crate::syscalls::{read_bytes, write_bytes};
use crate::WasiEnv;
use bytes::{Buf, Bytes};
use std::convert::TryInto;
use std::io::{self, Read};
use std::ops::DerefMut;
use std::sync::mpsc;
use std::sync::Mutex;
use wasmer::MemorySize;
use wasmer::{Memory, WasmSlice};
use wasmer::{ContextMut, Memory, WasmSlice};

#[derive(Debug)]
pub struct WasiPipe {
Expand Down Expand Up @@ -41,6 +42,7 @@ impl WasiPipe {

pub fn recv<M: MemorySize>(
&mut self,
ctx: &ContextMut<'_, WasiEnv>,
memory: &Memory,
iov: WasmSlice<__wasi_iovec_t<M>>,
) -> Result<usize, __wasi_errno_t> {
Expand All @@ -49,7 +51,7 @@ impl WasiPipe {
let buf_len = buf.len();
if buf_len > 0 {
let reader = buf.as_ref();
let read = read_bytes(reader, memory, iov).map(|_| buf_len as usize)?;
let read = read_bytes(ctx, reader, memory, iov).map(|_| buf_len as usize)?;
buf.advance(read);
return Ok(read);
}
Expand All @@ -62,6 +64,7 @@ impl WasiPipe {

pub fn send<M: MemorySize>(
&mut self,
ctx: &ContextMut<'_, WasiEnv>,
memory: &Memory,
iov: WasmSlice<__wasi_ciovec_t<M>>,
) -> Result<usize, __wasi_errno_t> {
Expand All @@ -72,7 +75,7 @@ impl WasiPipe {
.sum();
let buf_len: usize = buf_len.try_into().map_err(|_| __WASI_EINVAL)?;
let mut buf = Vec::with_capacity(buf_len);
write_bytes(&mut buf, memory, iov)?;
write_bytes(ctx, &mut buf, memory, iov)?;
let tx = self.tx.lock().unwrap();
tx.send(buf).map_err(|_| __WASI_EIO)?;
Ok(buf_len)
Expand Down
49 changes: 32 additions & 17 deletions lib/wasi/src/state/socket.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::types::net_error_into_wasi_err;
use crate::syscalls::types::*;
use crate::syscalls::{read_bytes, write_bytes};
use crate::WasiEnv;
use bytes::{Buf, Bytes};
use std::convert::TryInto;
use std::io::{self, Read};
Expand All @@ -10,7 +11,7 @@ use std::sync::Mutex;
use std::time::Duration;
#[allow(unused_imports)]
use tracing::{debug, error, info, warn};
use wasmer::{Memory, MemorySize, WasmPtr, WasmSlice};
use wasmer::{ContextMut, Memory, MemorySize, WasmPtr, WasmSlice};
use wasmer_vnet::{net_error_into_io_err, TimeType};
use wasmer_vnet::{
IpCidr, IpRoute, SocketHttpRequest, VirtualIcmpSocket, VirtualNetworking, VirtualRawSocket,
Expand Down Expand Up @@ -768,6 +769,7 @@ impl InodeSocket {

pub fn send<M: MemorySize>(
&mut self,
ctx: &ContextMut<'_, WasiEnv>,
memory: &Memory,
iov: WasmSlice<__wasi_ciovec_t<M>>,
) -> Result<usize, __wasi_errno_t> {
Expand All @@ -778,7 +780,7 @@ impl InodeSocket {
.sum();
let buf_len: usize = buf_len.try_into().map_err(|_| __WASI_EINVAL)?;
let mut buf = Vec::with_capacity(buf_len);
write_bytes(&mut buf, memory, iov)?;
write_bytes(ctx, &mut buf, memory, iov)?;
match &mut self.kind {
InodeSocketKind::HttpRequest(sock, ty) => {
let sock = sock.get_mut().unwrap();
Expand Down Expand Up @@ -852,11 +854,12 @@ impl InodeSocket {

pub fn send_to<M: MemorySize>(
&mut self,
ctx: &ContextMut<'_, WasiEnv>,
memory: &Memory,
iov: WasmSlice<__wasi_ciovec_t<M>>,
addr: WasmPtr<__wasi_addr_port_t, M>,
) -> Result<usize, __wasi_errno_t> {
let (addr_ip, addr_port) = read_ip_port(memory, addr)?;
let (addr_ip, addr_port) = read_ip_port(ctx, memory, addr)?;
let addr = SocketAddr::new(addr_ip, addr_port);
let buf_len: M::Offset = iov
.iter()
Expand All @@ -865,7 +868,7 @@ impl InodeSocket {
.sum();
let buf_len: usize = buf_len.try_into().map_err(|_| __WASI_EINVAL)?;
let mut buf = Vec::with_capacity(buf_len);
write_bytes(&mut buf, memory, iov)?;
write_bytes(ctx, &mut buf, memory, iov)?;
match &mut self.kind {
InodeSocketKind::Icmp(sock) => sock
.send_to(Bytes::from(buf), addr)
Expand All @@ -882,6 +885,7 @@ impl InodeSocket {

pub fn recv<M: MemorySize>(
&mut self,
ctx: &ContextMut<'_, WasiEnv>,
memory: &Memory,
iov: WasmSlice<__wasi_iovec_t<M>>,
) -> Result<usize, __wasi_errno_t> {
Expand All @@ -890,7 +894,7 @@ impl InodeSocket {
let buf_len = buf.len();
if buf_len > 0 {
let reader = buf.as_ref();
let read = read_bytes(reader, memory, iov).map(|_| buf_len)?;
let read = read_bytes(ctx, reader, memory, iov).map(|_| buf_len)?;
if let InodeSocketKind::TcpStream(..) = &self.kind {
buf.advance(read);
} else {
Expand Down Expand Up @@ -951,6 +955,7 @@ impl InodeSocket {

pub fn recv_from<M: MemorySize>(
&mut self,
ctx: &ContextMut<'_, WasiEnv>,
memory: &Memory,
iov: WasmSlice<__wasi_iovec_t<M>>,
addr: WasmPtr<__wasi_addr_port_t, M>,
Expand All @@ -959,11 +964,11 @@ impl InodeSocket {
if let Some(buf) = self.read_buffer.as_mut() {
if !buf.is_empty() {
let reader = buf.as_ref();
let ret = read_bytes(reader, memory, iov)?;
let ret = read_bytes(ctx, reader, memory, iov)?;
let peer = self
.read_addr
.unwrap_or_else(|| SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0));
write_ip_port(memory, addr, peer.ip(), peer.port())?;
write_ip_port(ctx, memory, addr, peer.ip(), peer.port())?;
return Ok(ret);
}
}
Expand Down Expand Up @@ -1130,10 +1135,11 @@ impl Drop for InodeSocket {

#[allow(dead_code)]
pub(crate) fn read_ip<M: MemorySize>(
ctx: &ContextMut<'_, WasiEnv>,
memory: &Memory,
ptr: WasmPtr<__wasi_addr_t, M>,
) -> Result<IpAddr, __wasi_errno_t> {
let addr_ptr = ptr.deref(memory);
let addr_ptr = ptr.deref(ctx, memory);
let addr = addr_ptr.read().map_err(crate::mem_error_to_wasi)?;

let o = addr.u.octs;
Expand All @@ -1148,28 +1154,31 @@ pub(crate) fn read_ip<M: MemorySize>(
}

pub(crate) fn read_ip_v4<M: MemorySize>(
ctx: &ContextMut<'_, WasiEnv>,
memory: &Memory,
ptr: WasmPtr<__wasi_addr_ip4_t, M>,
) -> Result<Ipv4Addr, __wasi_errno_t> {
let addr_ptr = ptr.deref(memory);
let addr_ptr = ptr.deref(ctx, memory);
let addr = addr_ptr.read().map_err(crate::mem_error_to_wasi)?;

let o = addr.octs;
Ok(Ipv4Addr::new(o[0], o[1], o[2], o[3]))
}

pub(crate) fn read_ip_v6<M: MemorySize>(
ctx: &ContextMut<'_, WasiEnv>,
memory: &Memory,
ptr: WasmPtr<__wasi_addr_ip6_t, M>,
) -> Result<Ipv6Addr, __wasi_errno_t> {
let addr_ptr = ptr.deref(memory);
let addr_ptr = ptr.deref(ctx, memory);
let addr = addr_ptr.read().map_err(crate::mem_error_to_wasi)?;

let [a, b, c, d, e, f, g, h] = unsafe { transmute::<_, [u16; 8]>(addr.segs) };
Ok(Ipv6Addr::new(a, b, c, d, e, f, g, h))
}

pub(crate) fn write_ip<M: MemorySize>(
ctx: &ContextMut<'_, WasiEnv>,
memory: &Memory,
ptr: WasmPtr<__wasi_addr_t, M>,
ip: IpAddr,
Expand All @@ -1193,17 +1202,18 @@ pub(crate) fn write_ip<M: MemorySize>(
}
};

let addr_ptr = ptr.deref(memory);
let addr_ptr = ptr.deref(ctx, memory);
addr_ptr.write(ip).map_err(crate::mem_error_to_wasi)?;
Ok(())
}

#[allow(dead_code)]
pub(crate) fn read_cidr<M: MemorySize>(
ctx: &ContextMut<'_, WasiEnv>,
memory: &Memory,
ptr: WasmPtr<__wasi_cidr_t, M>,
) -> Result<IpCidr, __wasi_errno_t> {
let addr_ptr = ptr.deref(memory);
let addr_ptr = ptr.deref(ctx, memory);
let addr = addr_ptr.read().map_err(crate::mem_error_to_wasi)?;

let o = addr.u.octs;
Expand Down Expand Up @@ -1231,6 +1241,7 @@ pub(crate) fn read_cidr<M: MemorySize>(

#[allow(dead_code)]
pub(crate) fn write_cidr<M: MemorySize>(
ctx: &ContextMut<'_, WasiEnv>,
memory: &Memory,
ptr: WasmPtr<__wasi_cidr_t, M>,
cidr: IpCidr,
Expand Down Expand Up @@ -1262,16 +1273,17 @@ pub(crate) fn write_cidr<M: MemorySize>(
}
};

let addr_ptr = ptr.deref(memory);
let addr_ptr = ptr.deref(ctx, memory);
addr_ptr.write(cidr).map_err(crate::mem_error_to_wasi)?;
Ok(())
}

pub(crate) fn read_ip_port<M: MemorySize>(
ctx: &ContextMut<'_, WasiEnv>,
memory: &Memory,
ptr: WasmPtr<__wasi_addr_port_t, M>,
) -> Result<(IpAddr, u16), __wasi_errno_t> {
let addr_ptr = ptr.deref(memory);
let addr_ptr = ptr.deref(ctx, memory);
let addr = addr_ptr.read().map_err(crate::mem_error_to_wasi)?;

let o = addr.u.octs;
Expand Down Expand Up @@ -1299,6 +1311,7 @@ pub(crate) fn read_ip_port<M: MemorySize>(

#[allow(dead_code)]
pub(crate) fn write_ip_port<M: MemorySize>(
ctx: &ContextMut<'_, WasiEnv>,
memory: &Memory,
ptr: WasmPtr<__wasi_addr_port_t, M>,
ip: IpAddr,
Expand Down Expand Up @@ -1331,17 +1344,18 @@ pub(crate) fn write_ip_port<M: MemorySize>(
}
};

let addr_ptr = ptr.deref(memory);
let addr_ptr = ptr.deref(ctx, memory);
addr_ptr.write(ipport).map_err(crate::mem_error_to_wasi)?;
Ok(())
}

#[allow(dead_code)]
pub(crate) fn read_route<M: MemorySize>(
ctx: &ContextMut<'_, WasiEnv>,
memory: &Memory,
ptr: WasmPtr<__wasi_route_t, M>,
) -> Result<IpRoute, __wasi_errno_t> {
let route_ptr = ptr.deref(memory);
let route_ptr = ptr.deref(ctx, memory);
let route = route_ptr.read().map_err(crate::mem_error_to_wasi)?;

Ok(IpRoute {
Expand Down Expand Up @@ -1393,6 +1407,7 @@ pub(crate) fn read_route<M: MemorySize>(
}

pub(crate) fn write_route<M: MemorySize>(
ctx: &ContextMut<'_, WasiEnv>,
memory: &Memory,
ptr: WasmPtr<__wasi_route_t, M>,
route: IpRoute,
Expand Down Expand Up @@ -1471,7 +1486,7 @@ pub(crate) fn write_route<M: MemorySize>(
expires_at,
};

let route_ptr = ptr.deref(memory);
let route_ptr = ptr.deref(ctx, memory);
route_ptr.write(route).map_err(crate::mem_error_to_wasi)?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion lib/wasi/src/syscalls/legacy/snapshot0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn fd_filestat_get(

// Set up complete, make the call with the pointer that will write to the
// struct and some unrelated memory after the struct.
let result = syscalls::fd_filestat_get::<Memory32>(env, fd, new_buf);
let result = syscalls::fd_filestat_get::<Memory32>(ctx.as_context_mut(), fd, new_buf);

// reborrow memory
let env = ctx.data();
Expand Down
Loading

0 comments on commit 66e1315

Please sign in to comment.