forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rust: add Proxy-WASM for Rust (MVP).
Signed-off-by: Piotr Sikora <piotrsikora@google.com>
- Loading branch information
1 parent
098f146
commit 415522a
Showing
14 changed files
with
13,973 additions
and
344 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ TAGS | |
.vimrc | ||
.vs | ||
.vscode | ||
**/Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
description = "Proxy-WASM for Rust" | ||
name = "proxy_wasm" | ||
version = "0.0.1" | ||
authors = ["Piotr Sikora <piotrsikora@google.com>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
log = "0.4" | ||
|
||
[profile.release] | ||
lto = true | ||
opt-level = 3 | ||
panic = "abort" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
RUST_API:=$(shell git rev-parse --show-toplevel)/api/wasm/rust | ||
|
||
%.wasm %.wat: %/src/lib.rs %/Cargo.toml ${RUST_API}/src/lib.rs ${RUST_API}/Cargo.toml | ||
cd $* && cargo build --target=wasm32-unknown-unknown --release | ||
mv $*/target/wasm32-unknown-unknown/release/$*.wasm . | ||
rm -rf $*/target | ||
wasm-gc $*.wasm | ||
wavm-disas $*.wasm $*.wat | ||
chmod 644 $*.wat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/// Logger that integrates with host's logging system. | ||
pub struct Logger; | ||
|
||
static LOGGER: Logger = Logger; | ||
|
||
impl Logger { | ||
pub fn init() -> Result<(), log::SetLoggerError> { | ||
log::set_logger(&LOGGER).map(|()| log::set_max_level(log::LevelFilter::Trace)) | ||
} | ||
|
||
fn proxywasm_loglevel(level: log::Level) -> u32 { | ||
match level { | ||
log::Level::Trace => 0, | ||
log::Level::Debug => 1, | ||
log::Level::Info => 2, | ||
log::Level::Warn => 3, | ||
log::Level::Error => 4, | ||
} | ||
} | ||
} | ||
|
||
impl log::Log for Logger { | ||
fn enabled(&self, _metadata: &log::Metadata) -> bool { | ||
true | ||
} | ||
|
||
fn log(&self, record: &log::Record) { | ||
let level = Logger::proxywasm_loglevel(record.level()); | ||
let message = record.args().to_string(); | ||
unsafe { | ||
host::proxy_log(level, message.as_ptr(), message.len()); | ||
} | ||
} | ||
|
||
fn flush(&self) {} | ||
} | ||
|
||
/// Always hook into host's logging system. | ||
#[no_mangle] | ||
fn __post_instantiate() { | ||
Logger::init().unwrap(); | ||
} | ||
|
||
/// Allow host to allocate memory. | ||
#[no_mangle] | ||
fn _malloc(size: usize) -> *mut u8 { | ||
let mut vec: Vec<u8> = Vec::with_capacity(size); | ||
unsafe { | ||
vec.set_len(size); | ||
} | ||
let slice = vec.into_boxed_slice(); | ||
Box::into_raw(slice) as *mut u8 | ||
} | ||
|
||
/// Allow host to free memory. | ||
// TODO(PiotrSikora): make sure ptr is within acceptable range. | ||
#[no_mangle] | ||
fn _free(ptr: *mut u8) { | ||
if !ptr.is_null() { | ||
unsafe { | ||
Box::from_raw(ptr); | ||
} | ||
} | ||
} | ||
|
||
/// Low-level Proxy-WASM APIs for the host functions. | ||
pub mod host { | ||
extern "C" { | ||
#[link_name = "_proxy_log"] | ||
pub fn proxy_log(level: u32, message_data: *const u8, message_size: usize); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
NO_CONTEXT = true | ||
|
||
all: logging_cpp.wasm bad_signature_cpp.wasm segv_cpp.wasm emscripten_cpp.wasm asm2wasm_cpp.wasm | ||
all: logging_cpp.wasm logging_rust.wasm bad_signature_cpp.wasm segv_cpp.wasm emscripten_cpp.wasm asm2wasm_cpp.wasm | ||
|
||
include ../../../../api/wasm/cpp/Makefile.base | ||
include ../../../../api/wasm/rust/Makefile.base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.