Skip to content
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

Add proxy_wasm::main macro. #141

Merged
merged 6 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/rust.yml
Expand Up @@ -195,6 +195,43 @@ jobs:
- name: Package (publish)
run: cargo publish --dry-run --target=wasm32-unknown-unknown

reactor:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Update Rust
run: |
rustup toolchain install nightly --component clippy
rustup +nightly target add wasm32-wasi
rustup default nightly

- name: Rewrite Cargo.toml examples
run: |
grep -v '^crate-type' Cargo.toml > Cargo.tmp
mv Cargo.tmp Cargo.toml

- name: Build (wasm32-wasi)
env:
RUSTFLAGS: -D warnings -C link-args=-S -Z wasi-exec-model=reactor
run: cargo build --release --all-targets --target=wasm32-wasi

- name: Build (wasm32-wasi with wee-alloc)
env:
RUSTFLAGS: -D warnings -C link-args=-S -Z wasi-exec-model=reactor
run: cargo build --release --all-targets --target=wasm32-wasi --features=wee-alloc

- name: Clippy (wasm32-wasi)
env:
RUSTFLAGS: -D warnings -C link-args=-S -Z wasi-exec-model=reactor
run: cargo clippy --release --all-targets --target=wasm32-wasi

- name: Clippy (wasm32-wasi with wee-alloc)
env:
RUSTFLAGS: -D warnings -C link-args=-S -Z wasi-exec-model=reactor
run: cargo clippy --release --all-targets --target=wasm32-wasi --features=wee-alloc

outdated:
runs-on: ubuntu-latest

Expand Down
10 changes: 10 additions & 0 deletions BUILD
@@ -1,11 +1,21 @@
load("@rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script")
load("@rules_rust//rust:defs.bzl", "rust_library")

cargo_build_script(
name = "proxy_wasm_build_script",
srcs = ["build.rs"],
edition = "2018",
tags = ["manual"],
visibility = ["//visibility:private"],
)

rust_library(
name = "proxy_wasm",
srcs = glob(["src/*.rs"]),
edition = "2018",
visibility = ["//visibility:public"],
deps = [
":proxy_wasm_build_script",
"//bazel/cargo:hashbrown",
"//bazel/cargo:log",
],
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Fixed

- Fixed performance degradation with `wasm32-wasi` target in Rust v1.56.0
or newer by adding `proxy_wasm::main` macro that should be used instead
of custom `_start`, `_initialize` and/or `main` exports.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Swaagie @mathetake updated text to make it more clear that it's a fix and not simply a new feature. PTAL.


### Changed

- Updated ABI to Proxy-Wasm ABI v0.2.1.
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -7,6 +7,7 @@ readme = "README.md"
license = "Apache-2.0"
repository = "https://github.com/proxy-wasm/proxy-wasm-rust-sdk"
edition = "2018"
build = "build.rs"

[features]
wee-alloc = ["wee_alloc"]
Expand Down
33 changes: 33 additions & 0 deletions build.rs
@@ -0,0 +1,33 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-env-changed=RUSTFLAGS");

if let Some(target_os) = std::env::var_os("CARGO_CFG_TARGET_OS") {
if target_os != "wasi" {
return;
}
}

if let Some(rustflags) = std::env::var_os("CARGO_ENCODED_RUSTFLAGS") {
for flag in rustflags.to_string_lossy().split('\x1f') {
if flag.ends_with("wasi-exec-model=reactor") {
println!("cargo:rustc-cfg=wasi_exec_model_reactor");
return;
}
}
}
}
5 changes: 2 additions & 3 deletions examples/hello_world.rs
Expand Up @@ -22,11 +22,10 @@ use std::time::Duration;
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
use getrandom::getrandom;

#[no_mangle]
pub fn _start() {
proxy_wasm::main! {{
proxy_wasm::set_log_level(LogLevel::Trace);
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> { Box::new(HelloWorld) });
}
}}

struct HelloWorld;

Expand Down
5 changes: 2 additions & 3 deletions examples/http_auth_random.rs
Expand Up @@ -17,11 +17,10 @@ use proxy_wasm::traits::*;
use proxy_wasm::types::*;
use std::time::Duration;

#[no_mangle]
pub fn _start() {
proxy_wasm::main! {{
proxy_wasm::set_log_level(LogLevel::Trace);
proxy_wasm::set_http_context(|_, _| -> Box<dyn HttpContext> { Box::new(HttpAuthRandom) });
}
}}

struct HttpAuthRandom;

Expand Down
5 changes: 2 additions & 3 deletions examples/http_body.rs
Expand Up @@ -15,11 +15,10 @@
use proxy_wasm::traits::*;
use proxy_wasm::types::*;

#[no_mangle]
pub fn _start() {
proxy_wasm::main! {{
proxy_wasm::set_log_level(LogLevel::Trace);
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> { Box::new(HttpBodyRoot) });
}
}}

struct HttpBodyRoot;

Expand Down
5 changes: 2 additions & 3 deletions examples/http_config.rs
Expand Up @@ -15,15 +15,14 @@
use proxy_wasm::traits::*;
use proxy_wasm::types::*;

#[no_mangle]
pub fn _start() {
proxy_wasm::main! {{
proxy_wasm::set_log_level(LogLevel::Trace);
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> {
Box::new(HttpConfigHeaderRoot {
header_content: String::new(),
})
});
}
}}

struct HttpConfigHeader {
header_content: String,
Expand Down
5 changes: 2 additions & 3 deletions examples/http_headers.rs
Expand Up @@ -16,11 +16,10 @@ use log::trace;
use proxy_wasm::traits::*;
use proxy_wasm::types::*;

#[no_mangle]
pub fn _start() {
proxy_wasm::main! {{
proxy_wasm::set_log_level(LogLevel::Trace);
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> { Box::new(HttpHeadersRoot) });
}
}}

struct HttpHeadersRoot;

Expand Down
34 changes: 34 additions & 0 deletions src/lib.rs
Expand Up @@ -20,6 +20,40 @@ mod allocator;
mod dispatcher;
mod logger;

// For crate-type="cdylib".
#[cfg(not(wasi_exec_model_reactor))]
#[macro_export]
macro_rules! main {
($code:block) => {
#[cfg(target_os = "wasi")]
extern "C" {
fn __wasm_call_ctors();
}

#[no_mangle]
pub extern "C" fn _initialize() {
#[cfg(target_os = "wasi")]
unsafe {
__wasm_call_ctors();
}

$code;
}
};
}

// For crate-type="bin" with RUSTFLAGS="-Z wasi-exec-model=reactor".
#[cfg(wasi_exec_model_reactor)]
#[macro_export]
macro_rules! main {
($code:block) => {
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
$code;
Ok(())
}
};
}

pub fn set_log_level(level: types::LogLevel) {
logger::set_log_level(level);
}
Expand Down