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

Commit

Permalink
wasmtime: first custom syscall demo
Browse files Browse the repository at this point in the history
  • Loading branch information
steveej committed Nov 14, 2019
1 parent 6b70407 commit f534429
Show file tree
Hide file tree
Showing 12 changed files with 2,991 additions and 306 deletions.
1,293 changes: 987 additions & 306 deletions Cargo.lock

Large diffs are not rendered by default.

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

members = [
"common",
"amd-sev",
"wasmtime-basic",
"wasmtime-native-embed",
"wasmtime-custom-syscall",
]
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Compiling either a C or Rust program to WASM and running it natively using a Rus
## [Wasmtime - Rust native in WASM](wasmtime-native-embed)
Running native Rust code in a WASM virtual machine without pre-compiling it.

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

## Intel SGX
A demonstration of remote attestation for an SGX enclave. The attesting enclave prepares a Report for the platform's
Quoting Enclave, which verifies the Report and signs it with its Attestation Key, producing a Quote. The Quote is
Expand Down
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

0 comments on commit f534429

Please sign in to comment.