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

Commit

Permalink
fixup! 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 8935ddf commit fcc9e1e
Show file tree
Hide file tree
Showing 9 changed files with 376 additions and 166 deletions.
117 changes: 61 additions & 56 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]

members = [
"common",
"amd-sev",
"wasmtime-basic",
"wasmtime-native-embed",
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()
}
}
}
19 changes: 15 additions & 4 deletions wasmtime-custom-syscall/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@ build = "build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
wasmtime = { git = "https://github.com/yurydelendik/wasmtime", rev = "0c30c4f1ac29030c24124171f76a07c2edb42c4b" }
wasmtime-bindings = { git = "https://github.com/yurydelendik/wasmtime", rev = "0c30c4f1ac29030c24124171f76a07c2edb42c4b" }
wasmtime-bindings-macro = { git = "https://github.com/yurydelendik/wasmtime", rev = "0c30c4f1ac29030c24124171f76a07c2edb42c4b" }
wasmtime-wasi = { git = "https://github.com/yurydelendik/wasmtime", rev = "0c30c4f1ac29030c24124171f76a07c2edb42c4b" }
common = { path = "../common" }
# wasmtime = { git = "https://github.com/yurydelendik/wasmtime", rev = "0c30c4f1ac29030c24124171f76a07c2edb42c4b" }
# wasmtime-bindings = { git = "https://github.com/yurydelendik/wasmtime", rev = "0c30c4f1ac29030c24124171f76a07c2edb42c4b" }
# wasmtime-bindings-macro = { git = "https://github.com/yurydelendik/wasmtime", rev = "0c30c4f1ac29030c24124171f76a07c2edb42c4b" }
# wasmtime-wasi = { git = "https://github.com/yurydelendik/wasmtime", rev = "0c30c4f1ac29030c24124171f76a07c2edb42c4b" }
wabt = "0.9"

wasmtime = { git = "https://github.com/steveeJ-forks/wasmtime", rev = "16421f31b622c7e11fda038909d1e82cb4432cee" }
wasmtime-bindings = { git = "https://github.com/steveeJ-forks/wasmtime", rev = "16421f31b622c7e11fda038909d1e82cb4432cee" }
wasmtime-bindings-macro = { git = "https://github.com/steveeJ-forks/wasmtime", rev = "16421f31b622c7e11fda038909d1e82cb4432cee" }
wasmtime-wasi = { git = "https://github.com/steveeJ-forks/wasmtime", rev = "16421f31b622c7e11fda038909d1e82cb4432cee" }

# wasmtime = { path = "/home/steveej/src/job-redhat/enarx/wasmtime_yury/crates/api" }
# wasmtime-bindings = { path = "/home/steveej/src/job-redhat/enarx/wasmtime_yury/crates/bindings" }
# wasmtime-bindings-macro = { path = "/home/steveej/src/job-redhat/enarx/wasmtime_yury/crates/bindings/macro" }
# wasmtime-wasi = { path = "/home/steveej/src/job-redhat/enarx/wasmtime_yury/crates/wasi" }
38 changes: 31 additions & 7 deletions wasmtime-custom-syscall/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,42 @@
// See the License for the specific language governing permissions and
// limitations under the License.

mod lib;
mod wasm {
include!("../../common/src/wasm.rs");
}

use wasm::enarx_syscalls;

use lib::{enarx_syscalls, CustomNumbers};
fn main() -> ! {
udp_echo_server()
}

fn main() {
fn simple_print() {
let (a, b) = (5, 7);
let result = a + b;

enarx_syscalls::println(&format!("{} + {} = {}", a, b, result));
}

fn udp_echo_server() -> ! {
enarx_syscalls::socket_udp_bind("127.0.0.1:34254");
loop {
let mut data_buf = [0; 1024];
let mut src_buf = [0; 1024];

enarx_syscalls::println(&format!(
"{:?}",
enarx_syscalls::mutate_custom_numbers(&mut CustomNumbers(0, 1))
));
let len = enarx_syscalls::socket_udp_receive(&mut data_buf, &mut src_buf);
let data = {
let data_str = std::str::from_utf8(&data_buf).unwrap();
let newline_index = data_str.find("\n").unwrap_or(data_str.len());
data_str.split_at(newline_index).0
};
let src = std::str::from_utf8(&src_buf)
.unwrap()
.split("\0")
.nth(0)
.unwrap();
enarx_syscalls::println(&format!("received {} bytes: '{}' from {}", len, data, src));
enarx_syscalls::println("sending back...");
enarx_syscalls::socket_udp_send_to(format!("{}\n", data).as_bytes(), src.as_bytes());
}
}
32 changes: 0 additions & 32 deletions wasmtime-custom-syscall/src/lib.rs

This file was deleted.

Loading

0 comments on commit fcc9e1e

Please sign in to comment.