forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rust: add Proxy-WASM for Rust (MVP). #46
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,71 @@ | ||
/// 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. | ||
#[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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is Rust edition (think: C/C++ standard revisions), 2018 is the latest.