Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Add 'wasmtime: custom syscall' and 'wasmtime: UDP echo-server' demos #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1,305 changes: 999 additions & 306 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[workspace]

members = [
"common",
"amd-sev",
"wasmtime-basic",
"wasmtime-native-embed",
"wasmtime-custom-syscall",
"wasmtime-udp-echoserver",
]
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ A demonstration of remote attestation for an SGX enclave. The attesting enclave
Quoting Enclave, which verifies the Report and signs it with its Attestation Key, producing a Quote. The Quote is
sent off-platform to the tenant, who can verify the Quote with a certificate chain from Intel. An attestation daemon
(located on the enclave's same platform) communicates between the tenant and enclave.

## [Wasmtime - Simple Custom Syscall](wasmtime-custom-syscall)
Providing a simple _println_ syscall to a pre-compiled WASM binary written in Rust.

## [Wasmtime - UDP Echo-Server](wasmtime-udp-echoserver)
Providing a rudimentary UDP implementation to a pre-compiled WASM binary written in Rust, which implements a UDP Echo-Server on top of the provided syscalls.
9 changes: 9 additions & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "common"
version = "0.1.0"
authors = ["Stefan Junker <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
1 change: 1 addition & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod wasm;
86 changes: 86 additions & 0 deletions common/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
pub mod enarx_syscalls {
use std::convert::TryInto;

#[link(wasm_import_module = "enarx_syscalls_stdio")]
extern "C" {
fn __print_str(ptr: *const u8, len: u64);
}

#[link(wasm_import_module = "enarx_syscalls_net")]
extern "C" {
fn __socket_udp_bind(ptr: *const u8, len: u64);

fn __socket_udp_receive(
data_ptr: *mut u8,
data_len: u64,
src_ptr: *mut u8,
src_len: u64,
) -> u64;

fn __socket_udp_send_to(
data_ptr: *const u8,
data_len: u64,
dst_ptr: *const u8,
dst_len: u64,
) -> u64;
}

fn _print_str(s: &str) {
unsafe { __print_str(s.as_ptr(), s.len().try_into().unwrap()) }
}

pub fn print(s: &str) {
_print_str(s)
}

pub fn println(s: &str) {
_print_str(&format!("{}\n", s));
}

pub fn socket_udp_bind(addr: &str) {
unsafe { __socket_udp_bind(addr.as_ptr(), addr.len().try_into().unwrap()) }
}

pub fn socket_udp_receive(content_buf: &mut [u8], source_buf: &mut [u8]) -> u64 {
unsafe {
__socket_udp_receive(
content_buf.as_mut_ptr(),
content_buf.len().try_into().unwrap(),
source_buf.as_mut_ptr(),
source_buf.len().try_into().unwrap(),
)
}
}

pub fn socket_udp_send_to(data: &[u8], dst: &[u8]) -> u64 {
unsafe {
__socket_udp_send_to(
data.as_ptr(),
data.len().try_into().unwrap(),
dst.as_ptr(),
dst.len().try_into().unwrap(),
)
}
}
}

pub mod wasmstr {
use std::convert::TryInto;

pub struct WasmStr(pub *const u8, pub u64);

impl WasmStr {
pub fn to_str(&self) -> &str {
self.into()
}
}

impl std::convert::From<&WasmStr> for &str {
fn from(wasm_string: &WasmStr) -> Self {
std::str::from_utf8(unsafe {
std::slice::from_raw_parts(wasm_string.0, wasm_string.1.try_into().unwrap())
})
.unwrap()
}
}
}
Loading