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

Allow building for wasm32-wasi target. #42

Merged
merged 2 commits into from
Nov 4, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,28 @@ jobs:
run: |
rustup toolchain install stable --component clippy --component rustfmt
rustup target add wasm32-unknown-unknown
rustup target add wasm32-wasi

- name: Build
- name: Build (wasm32-unknown-unknown)
env:
RUSTFLAGS: -D warnings -C link-args=-S
run: cargo build --release --all-targets --target=wasm32-unknown-unknown

- name: Format (clippy)
- name: Clippy (wasm32-unknown-unknown)
env:
RUSTFLAGS: -D warnings -C link-args=-S
run: cargo clippy --release --all-targets --target=wasm32-unknown-unknown

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

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

- name: Format (rustfmt)
run: cargo fmt -- --check

Expand All @@ -122,18 +133,29 @@ jobs:
run: |
rustup toolchain install nightly --component clippy --component rustfmt
rustup +nightly target add wasm32-unknown-unknown
rustup +nightly target add wasm32-wasi
rustup default nightly

- name: Build
- name: Build (wasm32-unknown-unknown)
env:
RUSTFLAGS: -D warnings -C link-args=-S
run: cargo build --release --all-targets --target=wasm32-unknown-unknown

- name: Format (clippy)
- name: Clippy (wasm32-unknown-unknown)
env:
RUSTFLAGS: -D warnings -C link-args=-S
run: cargo clippy --release --all-targets --target=wasm32-unknown-unknown

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

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

- name: Format (rustfmt)
run: cargo fmt -- --check

Expand Down
6 changes: 5 additions & 1 deletion src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[cfg_attr(
all(target_arch = "wasm32", target_os = "unknown"),
export_name = "malloc"
)]
#[no_mangle]
pub extern "C" fn malloc(size: usize) -> *mut u8 {
pub extern "C" fn proxy_on_memory_allocate(size: usize) -> *mut u8 {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't recall which version of the ABI this is in -- if we change this now will it affect compatibility? Or do we need to bump the ABI version?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's part of the updated-not-yet-finalized ABI (proxy-wasm/spec#1), and it's already enabled as a fallback in Envoy (proxy-wasm/proxy-wasm-cpp-host#80).

Note that this is still exported as malloc when using wasm32-unknown-unknown target, and it's only exported as proxy_on_memory_allocate when using wasm32-wasi and other non-WebAssembly targets, which previously were impossible to use at all, so we're not breaking any existing compatibility.

It's not perfect, but it's the best we can do without making huge changes (and I'd rather avoid those now, since I want to finalize the ABI over the next week or two, and them upgrade the SDK straight to that version).

let mut vec: Vec<u8> = Vec::with_capacity(size);
unsafe {
vec.set_len(size);
Expand Down