Skip to content

Commit 6e4c976

Browse files
committed
Implemented functionality needed for WASIX and Networking within Web Assembly
- Introduced the virtual BUS interface used for RPC between web assembly apps - Introduced the virtual networking interface used to implement networking for web assembly apps - Implemented a local implementation of the virtual networking (available behind the feature toggle 'host-net' on the 'wasi' package) - Fixed up some of the examples from the wasmer3 branch - Refactored the WASI implementations so they support wasm64-wasi - WASIX is behind its own namespaces for both 32bit and 64bit implementations
1 parent 9fd923c commit 6e4c976

39 files changed

+10348
-693
lines changed

Cargo.lock

+290-245
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@ members = [
4747
"lib/engine-staticlib",
4848
"lib/object",
4949
"lib/vfs",
50+
"lib/vnet",
51+
"lib/vbus",
5052
"lib/vm",
5153
"lib/wasi",
5254
"lib/wasi-types",
5355
"lib/wasi-experimental-io-devices",
56+
"lib/wasi-local-networking",
5457
"lib/types",
5558
"tests/wasi-wast",
5659
"tests/lib/wast",

lib/api/src/js/mem_access.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{
1010
string::FromUtf8Error,
1111
};
1212
use thiserror::Error;
13-
use wasmer_types::ValueType;
13+
use wasmer_types::{MemorySize, ValueType};
1414

1515
/// Error for invalid [`Memory`] access.
1616
#[derive(Clone, Copy, Debug, Error)]
@@ -85,6 +85,13 @@ impl<'a, T: ValueType> WasmRef<'a, T> {
8585
WasmPtr::new(self.offset)
8686
}
8787

88+
/// Get a `WasmPtr` fror this `WasmRef`.
89+
#[inline]
90+
pub fn as_ptr<M: MemorySize>(self) -> WasmPtr<T, M> {
91+
let offset: M::Offset = self.offset.try_into().map_err(|_| "invalid offset into memory").unwrap();
92+
WasmPtr::<T, M>::new(offset)
93+
}
94+
8895
/// Get a reference to the Wasm memory backing this reference.
8996
#[inline]
9097
pub fn memory(self) -> &'a Memory {

lib/api/src/js/ptr.rs

+3-45
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,11 @@ use std::convert::TryFrom;
44
use std::{fmt, marker::PhantomData, mem};
55
use wasmer_types::{NativeWasmType, ValueType};
66

7-
/// Trait for the `Memory32` and `Memory64` marker types.
8-
///
9-
/// This allows code to be generic over 32-bit and 64-bit memories.
10-
pub unsafe trait MemorySize {
11-
/// Type used to represent an offset into a memory. This is `u32` or `u64`.
12-
type Offset: Copy + Into<u64> + TryFrom<u64>;
13-
14-
/// Type used to pass this value as an argument or return value for a Wasm function.
15-
type Native: NativeWasmType;
16-
17-
/// Zero value used for `WasmPtr::is_null`.
18-
const ZERO: Self::Offset;
19-
20-
/// Convert an `Offset` to a `Native`.
21-
fn offset_to_native(offset: Self::Offset) -> Self::Native;
22-
23-
/// Convert a `Native` to an `Offset`.
24-
fn native_to_offset(native: Self::Native) -> Self::Offset;
25-
}
7+
pub use wasmer_types::MemorySize;
268

27-
/// Marker trait for 32-bit memories.
28-
pub struct Memory32;
29-
unsafe impl MemorySize for Memory32 {
30-
type Offset = u32;
31-
type Native = i32;
32-
const ZERO: Self::Offset = 0;
33-
fn offset_to_native(offset: Self::Offset) -> Self::Native {
34-
offset as Self::Native
35-
}
36-
fn native_to_offset(native: Self::Native) -> Self::Offset {
37-
native as Self::Offset
38-
}
39-
}
9+
pub use wasmer_types::Memory32;
4010

41-
/// Marker trait for 64-bit memories.
42-
pub struct Memory64;
43-
unsafe impl MemorySize for Memory64 {
44-
type Offset = u64;
45-
type Native = i64;
46-
const ZERO: Self::Offset = 0;
47-
fn offset_to_native(offset: Self::Offset) -> Self::Native {
48-
offset as Self::Native
49-
}
50-
fn native_to_offset(native: Self::Native) -> Self::Offset {
51-
native as Self::Offset
52-
}
53-
}
11+
pub use wasmer_types::Memory64;
5412

5513
/// Alias for `WasmPtr<T, Memory64>.
5614
pub type WasmPtr64<T> = WasmPtr<T, Memory64>;

lib/api/src/sys/mem_access.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::RuntimeError;
2-
use crate::{Memory, Memory32, Memory64, WasmPtr};
2+
use crate::{Memory, Memory32, Memory64, WasmPtr, MemorySize};
33
use std::{
44
convert::TryInto,
55
fmt,
@@ -85,6 +85,13 @@ impl<'a, T: ValueType> WasmRef<'a, T> {
8585
WasmPtr::new(self.offset)
8686
}
8787

88+
/// Get a `WasmPtr` fror this `WasmRef`.
89+
#[inline]
90+
pub fn as_ptr<M: MemorySize>(self) -> WasmPtr<T, M> {
91+
let offset: M::Offset = self.offset.try_into().map_err(|_| "invalid offset into memory").unwrap();
92+
WasmPtr::<T, M>::new(offset)
93+
}
94+
8895
/// Get a reference to the Wasm memory backing this reference.
8996
#[inline]
9097
pub fn memory(self) -> &'a Memory {

lib/api/src/sys/ptr.rs

+4-46
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,13 @@ use crate::sys::{externals::Memory, FromToNativeWasmType};
22
use crate::{MemoryAccessError, WasmRef, WasmSlice};
33
use std::convert::TryFrom;
44
use std::{fmt, marker::PhantomData, mem};
5-
use wasmer_types::{NativeWasmType, ValueType};
5+
use wasmer_types::ValueType;
66

7-
/// Trait for the `Memory32` and `Memory64` marker types.
8-
///
9-
/// This allows code to be generic over 32-bit and 64-bit memories.
10-
pub unsafe trait MemorySize {
11-
/// Type used to represent an offset into a memory. This is `u32` or `u64`.
12-
type Offset: Copy + Into<u64> + TryFrom<u64>;
13-
14-
/// Type used to pass this value as an argument or return value for a Wasm function.
15-
type Native: NativeWasmType;
16-
17-
/// Zero value used for `WasmPtr::is_null`.
18-
const ZERO: Self::Offset;
19-
20-
/// Convert an `Offset` to a `Native`.
21-
fn offset_to_native(offset: Self::Offset) -> Self::Native;
22-
23-
/// Convert a `Native` to an `Offset`.
24-
fn native_to_offset(native: Self::Native) -> Self::Offset;
25-
}
7+
pub use wasmer_types::MemorySize;
268

27-
/// Marker trait for 32-bit memories.
28-
pub struct Memory32;
29-
unsafe impl MemorySize for Memory32 {
30-
type Offset = u32;
31-
type Native = i32;
32-
const ZERO: Self::Offset = 0;
33-
fn offset_to_native(offset: Self::Offset) -> Self::Native {
34-
offset as Self::Native
35-
}
36-
fn native_to_offset(native: Self::Native) -> Self::Offset {
37-
native as Self::Offset
38-
}
39-
}
9+
pub use wasmer_types::Memory32;
4010

41-
/// Marker trait for 64-bit memories.
42-
pub struct Memory64;
43-
unsafe impl MemorySize for Memory64 {
44-
type Offset = u64;
45-
type Native = i64;
46-
const ZERO: Self::Offset = 0;
47-
fn offset_to_native(offset: Self::Offset) -> Self::Native {
48-
offset as Self::Native
49-
}
50-
fn native_to_offset(native: Self::Native) -> Self::Offset {
51-
native as Self::Offset
52-
}
53-
}
11+
pub use wasmer_types::Memory64;
5412

5513
/// Alias for `WasmPtr<T, Memory64>.
5614
pub type WasmPtr64<T> = WasmPtr<T, Memory64>;

lib/c-api/src/wasm_c_api/wasi/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,21 @@ pub enum wasi_version_t {
287287

288288
/// `wasi_snapshot_preview1`.
289289
SNAPSHOT1 = 2,
290+
291+
/// `wasix_32v1`.
292+
WASIX32V1 = 3,
293+
294+
/// `wasix_64v1`.
295+
WASIX64V1 = 4,
290296
}
291297

292298
impl From<WasiVersion> for wasi_version_t {
293299
fn from(other: WasiVersion) -> Self {
294300
match other {
295301
WasiVersion::Snapshot0 => wasi_version_t::SNAPSHOT0,
296302
WasiVersion::Snapshot1 => wasi_version_t::SNAPSHOT1,
303+
WasiVersion::Wasix32v1 => wasi_version_t::WASIX32V1,
304+
WasiVersion::Wasix64v1 => wasi_version_t::WASIX64V1,
297305
WasiVersion::Latest => wasi_version_t::LATEST,
298306
}
299307
}
@@ -307,6 +315,8 @@ impl TryFrom<wasi_version_t> for WasiVersion {
307315
wasi_version_t::INVALID_VERSION => return Err("Invalid WASI version cannot be used"),
308316
wasi_version_t::SNAPSHOT0 => WasiVersion::Snapshot0,
309317
wasi_version_t::SNAPSHOT1 => WasiVersion::Snapshot1,
318+
wasi_version_t::WASIX32V1 => WasiVersion::Wasix32v1,
319+
wasi_version_t::WASIX64V1 => WasiVersion::Wasix64v1,
310320
wasi_version_t::LATEST => WasiVersion::Latest,
311321
})
312322
}

lib/types/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ pub use types::{
9393
ExportType, ExternType, FunctionType, GlobalInit, GlobalType, ImportType, MemoryType,
9494
Mutability, TableType, Type, V128,
9595
};
96+
pub use crate::memory::{
97+
MemorySize, Memory32, Memory64
98+
};
9699

97100
#[cfg(feature = "enable-rkyv")]
98101
pub use archives::ArchivableIndexMap;

lib/types/src/memory.rs

+80-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
use crate::Pages;
1+
use crate::{Pages, ValueType};
22
#[cfg(feature = "enable-rkyv")]
33
use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
44
#[cfg(feature = "enable-serde")]
55
use serde::{Deserialize, Serialize};
6+
use std::convert::{TryFrom, TryInto};
7+
use std::iter::Sum;
8+
use std::ops::{Add, AddAssign};
69

710
/// Implementation styles for WebAssembly linear memory.
811
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -43,3 +46,79 @@ impl MemoryStyle {
4346
}
4447
}
4548
}
49+
50+
/// Trait for the `Memory32` and `Memory64` marker types.
51+
///
52+
/// This allows code to be generic over 32-bit and 64-bit memories.
53+
pub unsafe trait MemorySize: Copy {
54+
/// Type used to represent an offset into a memory. This is `u32` or `u64`.
55+
type Offset: Default +
56+
std::fmt::Debug +
57+
std::fmt::Display +
58+
Eq +
59+
Ord +
60+
PartialEq<Self::Offset> +
61+
PartialOrd<Self::Offset> +
62+
Clone +
63+
Copy +
64+
ValueType +
65+
Into<u64> +
66+
From<u32> +
67+
From<u16> +
68+
From<u8> +
69+
TryFrom<u64> +
70+
TryFrom<u32> +
71+
TryFrom<u16> +
72+
TryFrom<u8> +
73+
TryInto<usize> +
74+
TryInto<u64> +
75+
TryInto<u32> +
76+
TryInto<u16> +
77+
TryInto<u8> +
78+
TryFrom<usize> +
79+
Add<Self::Offset> +
80+
Sum<Self::Offset> +
81+
AddAssign<Self::Offset>;
82+
83+
/// Type used to pass this value as an argument or return value for a Wasm function.
84+
type Native: super::NativeWasmType;
85+
86+
/// Zero value used for `WasmPtr::is_null`.
87+
const ZERO: Self::Offset;
88+
89+
/// Convert an `Offset` to a `Native`.
90+
fn offset_to_native(offset: Self::Offset) -> Self::Native;
91+
92+
/// Convert a `Native` to an `Offset`.
93+
fn native_to_offset(native: Self::Native) -> Self::Offset;
94+
}
95+
96+
/// Marker trait for 32-bit memories.
97+
#[derive(Clone, Copy)]
98+
pub struct Memory32;
99+
unsafe impl MemorySize for Memory32 {
100+
type Offset = u32;
101+
type Native = i32;
102+
const ZERO: Self::Offset = 0;
103+
fn offset_to_native(offset: Self::Offset) -> Self::Native {
104+
offset as Self::Native
105+
}
106+
fn native_to_offset(native: Self::Native) -> Self::Offset {
107+
native as Self::Offset
108+
}
109+
}
110+
111+
/// Marker trait for 64-bit memories.
112+
#[derive(Clone, Copy)]
113+
pub struct Memory64;
114+
unsafe impl MemorySize for Memory64 {
115+
type Offset = u64;
116+
type Native = i64;
117+
const ZERO: Self::Offset = 0;
118+
fn offset_to_native(offset: Self::Offset) -> Self::Native {
119+
offset as Self::Native
120+
}
121+
fn native_to_offset(native: Self::Native) -> Self::Offset {
122+
native as Self::Offset
123+
}
124+
}

lib/vbus/Cargo.toml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "wasmer-vbus"
3+
version = "2.2.1"
4+
description = "Wasmer Virtual Bus"
5+
authors = ["Wasmer Engineering Team <[email protected]>"]
6+
license = "MIT"
7+
edition = "2018"
8+
9+
[dependencies]
10+
libc = { version = "^0.2", default-features = false, optional = true }
11+
thiserror = "1"
12+
tracing = { version = "0.1" }
13+
typetag = { version = "0.1", optional = true }
14+
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }
15+
slab = { version = "0.4", optional = true }
16+
wasmer-vfs = { path = "../vfs", version = "=2.2.1", default-features = false }
17+
18+
[features]
19+
default = []

0 commit comments

Comments
 (0)