Skip to content

Commit 3817f27

Browse files
committed
Add proxy_wasm::main macro.
This is primarly a mechanism to deliver a workaround for a breaking change in Rust v1.56.0 (which updated LLVM to v13.0), which changed existing reactors into multi-entry commands, leading to performance degradation and/or unusable Proxy-Wasm plugins. See: WebAssembly/WASI#471 This is delivered as a macro to make it somehow compatible with the unstable "-Z wasi-exec-model=reactor" feature. Signed-off-by: Piotr Sikora <[email protected]>
1 parent cf7894e commit 3817f27

File tree

11 files changed

+121
-15
lines changed

11 files changed

+121
-15
lines changed

.github/workflows/rust.yml

+35
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,41 @@ jobs:
195195
- name: Package (publish)
196196
run: cargo publish --dry-run --target=wasm32-unknown-unknown
197197

198+
reactor:
199+
runs-on: ubuntu-latest
200+
201+
steps:
202+
- uses: actions/checkout@v2
203+
204+
- name: Update Rust
205+
run: |
206+
rustup toolchain install nightly --component clippy
207+
rustup +nightly target add wasm32-wasi
208+
rustup default nightly
209+
210+
- name: Rewrite Cargo.toml examples
211+
run: grep -v '^crate-type' Cargo.toml > Cargo.tmp && mv Cargo.tmp Cargo.toml
212+
213+
- name: Build (wasm32-wasi)
214+
env:
215+
RUSTFLAGS: -D warnings -C link-args=-S -Z wasi-exec-model=reactor
216+
run: cargo build --release --all-targets --target=wasm32-wasi
217+
218+
- name: Build (wasm32-wasi with wee-alloc)
219+
env:
220+
RUSTFLAGS: -D warnings -C link-args=-S -Z wasi-exec-model=reactor
221+
run: cargo build --release --all-targets --target=wasm32-wasi --features=wee-alloc
222+
223+
- name: Clippy (wasm32-wasi)
224+
env:
225+
RUSTFLAGS: -D warnings -C link-args=-S -Z wasi-exec-model=reactor
226+
run: cargo clippy --release --all-targets --target=wasm32-wasi
227+
228+
- name: Clippy (wasm32-wasi with wee-alloc)
229+
env:
230+
RUSTFLAGS: -D warnings -C link-args=-S -Z wasi-exec-model=reactor
231+
run: cargo clippy --release --all-targets --target=wasm32-wasi --features=wee-alloc
232+
198233
outdated:
199234
runs-on: ubuntu-latest
200235

BUILD

+10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
load("@rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script")
12
load("@rules_rust//rust:defs.bzl", "rust_library")
23

4+
cargo_build_script(
5+
name = "proxy_wasm_build_script",
6+
srcs = ["build.rs"],
7+
edition = "2018",
8+
tags = ["manual"],
9+
visibility = ["//visibility:private"],
10+
)
11+
312
rust_library(
413
name = "proxy_wasm",
514
srcs = glob(["src/*.rs"]),
615
edition = "2018",
716
visibility = ["//visibility:public"],
817
deps = [
18+
":proxy_wasm_build_script",
919
"//bazel/cargo:hashbrown",
1020
"//bazel/cargo:log",
1121
],

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
88

99
### Changed
1010

11+
- Added `proxy_wasm::main` macro that should be used instead of custom `_start`,
12+
`_initialize` and/or `main` exports.
1113
- Updated ABI to Proxy-Wasm ABI v0.2.1.
1214

1315
## [0.1.4] - 2021-07-01

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ readme = "README.md"
77
license = "Apache-2.0"
88
repository = "https://github.com/proxy-wasm/proxy-wasm-rust-sdk"
99
edition = "2018"
10+
build = "build.rs"
1011

1112
[features]
1213
wee-alloc = ["wee_alloc"]

build.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
fn main() {
16+
if let Some(target_os) = std::env::var_os("CARGO_CFG_TARGET_OS") {
17+
if target_os != "wasi" {
18+
return;
19+
}
20+
}
21+
if let Some(rustflags) = std::env::var_os("CARGO_ENCODED_RUSTFLAGS") {
22+
for flag in rustflags.to_string_lossy().split('\x1f') {
23+
if flag.ends_with("wasi-exec-model=reactor") {
24+
println!("cargo:rustc-cfg=wasi_exec_model_reactor");
25+
return;
26+
}
27+
}
28+
}
29+
}

examples/hello_world.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ use std::time::Duration;
2222
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
2323
use getrandom::getrandom;
2424

25-
#[no_mangle]
26-
pub fn _start() {
25+
proxy_wasm::main! {{
2726
proxy_wasm::set_log_level(LogLevel::Trace);
2827
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> { Box::new(HelloWorld) });
29-
}
28+
}}
3029

3130
struct HelloWorld;
3231

examples/http_auth_random.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ use proxy_wasm::traits::*;
1717
use proxy_wasm::types::*;
1818
use std::time::Duration;
1919

20-
#[no_mangle]
21-
pub fn _start() {
20+
proxy_wasm::main! {{
2221
proxy_wasm::set_log_level(LogLevel::Trace);
2322
proxy_wasm::set_http_context(|_, _| -> Box<dyn HttpContext> { Box::new(HttpAuthRandom) });
24-
}
23+
}}
2524

2625
struct HttpAuthRandom;
2726

examples/http_body.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
use proxy_wasm::traits::*;
1616
use proxy_wasm::types::*;
1717

18-
#[no_mangle]
19-
pub fn _start() {
18+
proxy_wasm::main! {{
2019
proxy_wasm::set_log_level(LogLevel::Trace);
2120
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> { Box::new(HttpBodyRoot) });
22-
}
21+
}}
2322

2423
struct HttpBodyRoot;
2524

examples/http_config.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
use proxy_wasm::traits::*;
1616
use proxy_wasm::types::*;
1717

18-
#[no_mangle]
19-
pub fn _start() {
18+
proxy_wasm::main! {{
2019
proxy_wasm::set_log_level(LogLevel::Trace);
2120
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> {
2221
Box::new(HttpConfigHeaderRoot {
2322
header_content: String::new(),
2423
})
2524
});
26-
}
25+
}}
2726

2827
struct HttpConfigHeader {
2928
header_content: String,

examples/http_headers.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ use log::trace;
1616
use proxy_wasm::traits::*;
1717
use proxy_wasm::types::*;
1818

19-
#[no_mangle]
20-
pub fn _start() {
19+
proxy_wasm::main! {{
2120
proxy_wasm::set_log_level(LogLevel::Trace);
2221
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> { Box::new(HttpHeadersRoot) });
23-
}
22+
}}
2423

2524
struct HttpHeadersRoot;
2625

src/lib.rs

+34
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,40 @@ mod allocator;
2020
mod dispatcher;
2121
mod logger;
2222

23+
// For crate-type="cdylib".
24+
#[cfg(not(wasi_exec_model_reactor))]
25+
#[macro_export]
26+
macro_rules! main {
27+
($code:block) => {
28+
#[cfg(target_os = "wasi")]
29+
extern "C" {
30+
fn __wasm_call_ctors();
31+
}
32+
33+
#[no_mangle]
34+
pub extern "C" fn _initialize() {
35+
#[cfg(target_os = "wasi")]
36+
unsafe {
37+
__wasm_call_ctors();
38+
}
39+
40+
$code;
41+
}
42+
};
43+
}
44+
45+
// For crate-type="bin" with RUSTFLAGS="-Z wasi-exec-model=reactor".
46+
#[cfg(wasi_exec_model_reactor)]
47+
#[macro_export]
48+
macro_rules! main {
49+
($code:block) => {
50+
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
51+
$code;
52+
Ok(())
53+
}
54+
};
55+
}
56+
2357
pub fn set_log_level(level: types::LogLevel) {
2458
logger::set_log_level(level);
2559
}

0 commit comments

Comments
 (0)