Skip to content

Commit

Permalink
ci: improve no_std testing, add linux arm64
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Gressmann <[email protected]>
  • Loading branch information
explodingcamera committed Sep 11, 2024
1 parent 0ed6acf commit c5f45ff
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 20 deletions.
31 changes: 21 additions & 10 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
branches: ["**"]
pull_request:
branches: ["next", "main"]
schedule:
- cron: "0 0 * * 0"

jobs:
build-wasm:
Expand All @@ -14,8 +16,13 @@ jobs:
- uses: actions/checkout@v4
with:
submodules: true
- name: Install Rust toolchain & Binaryen
run: rustup update && rustup target add wasm32-unknown-unknown && sudo apt-get install -y binaryen wabt
- name: Install Rust toolchain
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: stable
target: wasm32-unknown-unknown
- name: Install Binaryen and WABT
run: sudo apt-get install -y binaryen wabt
- name: Build wasm
run: ./examples/rust/build.sh
- name: Save artifacts
Expand All @@ -31,27 +38,31 @@ jobs:
include:
- os: ubuntu-latest
rust: stable
name: "Linux (stable)"
name: "Linux x86 (stable)"
- os: ubuntu-latest
rust: nightly
name: "Linux (nightly)"
name: "Linux x86 (nightly)"
- os: ubuntu-latest
rust: stable
name: "Linux (stable, no default features)"
name: "Linux x86 (stable, no default features)"
args: "--no-default-features"
- os: ubuntu-latest
rust: nightly
name: "Linux (nightly, no default features)"
name: "Linux x86 (nightly, no default features)"
args: "--no-default-features"
- os: macos-14
rust: stable
name: "macOS arm64 (Apple M1)"
- os: ubuntu-latest
rust: stable
name: "armv7 (32-Bit Raspberry Pi)"
name: "Linux arm64"
target: aarch64-unknown-linux-gnu
- os: ubuntu-latest
rust: stable
name: "Linux armv7"
target: armv7-unknown-linux-gnueabihf

name: Run tests on ${{ matrix.os }}, ${{ matrix.name }})
name: Run tests on ${{ matrix.name }})
runs-on: ${{ matrix.os }}
steps:
- name: Checkout code
Expand All @@ -74,11 +85,11 @@ jobs:
path: examples/rust/out

- name: Run tests
run: cargo test --workspace ${{ matrix.args }} && cargo run --example wasm-rust all
run: cargo test --workspace ${{ matrix.args }} && cargo test --workspace ${{ matrix.args }} --examples
if: matrix.target == ''

- name: Run clippy
run: cargo clippy --workspace --all-targets --all-features
run: cargo clippy --workspace ${{ matrix.args }}
if: matrix.target == ''

- name: Run tests (${{ matrix.target }})
Expand Down
4 changes: 2 additions & 2 deletions crates/parser/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ impl ModuleReader {
validator.end(offset)?;
self.end_reached = true;
}
CustomSection(reader) => {
CustomSection(_reader) => {
debug!("Found custom section");
debug!("Skipping custom section: {:?}", reader.name());
debug!("Skipping custom section: {:?}", _reader.name());
}
UnknownSection { .. } => return Err(ParseError::UnsupportedSection("Unknown section".into())),
section => return Err(ParseError::UnsupportedSection(format!("Unsupported section: {section:?}"))),
Expand Down
4 changes: 2 additions & 2 deletions crates/tinywasm/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ impl FuncHandle {
}

// 5. For each value type and the corresponding value, check if types match
if !(func_ty.params.iter().zip(params).enumerate().all(|(i, (ty, param))| {
if !(func_ty.params.iter().zip(params).enumerate().all(|(_i, (ty, param))| {
if ty != &param.val_type() {
log::error!("param type mismatch at index {}: expected {:?}, got {:?}", i, ty, param);
log::error!("param type mismatch at index {}: expected {:?}, got {:?}", _i, ty, param);
false
} else {
true
Expand Down
11 changes: 10 additions & 1 deletion examples/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ forced-target="wasm32-unknown-unknown"
edition="2021"

[dependencies]
tinywasm={path="../../crates/tinywasm", features=["parser", "std"]}
tinywasm={path="../../crates/tinywasm", default-features=false, features=["parser", "archive"]}
argon2={version="0.5"}
lol_alloc="0.4.1"

[features]
default=["std"]
std=["tinywasm/std"]

[[bin]]
name="hello"
Expand All @@ -25,6 +30,10 @@ path="src/print.rs"
name="tinywasm"
path="src/tinywasm.rs"

[[bin]]
name="tinywasm_no_std"
path="src/tinywasm_no_std.rs"

[[bin]]
name="fibonacci"
path="src/fibonacci.rs"
Expand Down
3 changes: 3 additions & 0 deletions examples/rust/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ wasmopt_features="--enable-reference-types --enable-bulk-memory --enable-mutable
# ensure out dir exists
mkdir -p "$dest_dir"

# build no_std
cargo build --target wasm32-unknown-unknown --package rust-wasm-examples --profile=wasm --bin tinywasm_no_std --no-default-features

for bin in "${bins[@]}"; do
RUSTFLAGS="-C target-feature=$rust_features -C panic=abort" cargo build --target wasm32-unknown-unknown --package rust-wasm-examples --profile=wasm --bin "$bin"

Expand Down
2 changes: 1 addition & 1 deletion examples/rust/src/tinywasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub extern "C" fn hello() {
}

fn run() -> tinywasm::Result<()> {
let module = tinywasm::Module::parse_bytes(include_bytes!("../out/print.wasm"))?;
let module = tinywasm::Module::parse_stream(&include_bytes!("../out/print.wasm")[..])?;
let mut store = tinywasm::Store::default();
let mut imports = tinywasm::Imports::new();

Expand Down
17 changes: 16 additions & 1 deletion examples/rust/src/tinywasm_no_std.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
#![no_main]
#![no_std]
use lol_alloc::{AssumeSingleThreaded, FreeListAllocator};
use tinywasm::{Extern, FuncContext};

extern crate alloc;

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

#[global_allocator]
static ALLOCATOR: AssumeSingleThreaded<FreeListAllocator> =
unsafe { AssumeSingleThreaded::new(FreeListAllocator::new()) };

#[link(wasm_import_module = "env")]
extern "C" {
fn printi32(x: i32);
Expand All @@ -13,10 +25,13 @@ pub extern "C" fn hello() {
}

fn run() -> tinywasm::Result<()> {
let module = tinywasm::Module::parse_bytes(include_bytes!("../out/print.wasm"))?;
let mut store = tinywasm::Store::default();
let mut imports = tinywasm::Imports::new();

let res = tinywasm::parser::Parser::new().parse_module_bytes(include_bytes!("../out/print.wasm"))?;
let twasm = res.serialize_twasm();
let module = tinywasm::Module::parse_bytes(&twasm)?;

imports.define(
"env",
"printi32",
Expand Down
59 changes: 56 additions & 3 deletions examples/wasm-rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fn main() -> Result<()> {
"printi32" => printi32()?,
"fibonacci" => fibonacci()?,
"tinywasm" => tinywasm()?,
"tinywasm_no_std" => tinywasm_no_std()?,
"argon2id" => argon2id()?,
"all" => {
println!("Running all examples");
Expand All @@ -54,6 +55,8 @@ fn main() -> Result<()> {
fibonacci()?;
println!("\ntinywasm.wasm:");
tinywasm()?;
println!("\ntinywasm_no_std.wasm:");
tinywasm_no_std()?;
println!("argon2id.wasm:");
argon2id()?;
}
Expand All @@ -64,7 +67,22 @@ fn main() -> Result<()> {
}

fn tinywasm() -> Result<()> {
let module = Module::parse_file("./examples/rust/out/tinywasm.wasm")?;
let module = Module::parse_file("./examples/rust/out/tinywasm.opt.wasm")?;
let mut store = Store::default();

let mut imports = Imports::new();
imports.define("env", "printi32", Extern::typed_func(|_: FuncContext<'_>, _x: i32| Ok(())))?;
let instance = module.instantiate(&mut store, Some(black_box(imports)))?;

let hello = instance.exported_func::<(), ()>(&store, "hello")?;
hello.call(&mut store, black_box(()))?;
hello.call(&mut store, black_box(()))?;
hello.call(&mut store, black_box(()))?;
Ok(())
}

fn tinywasm_no_std() -> Result<()> {
let module = Module::parse_file("./examples/rust/out/tinywasm_no_std.opt.wasm")?;
let mut store = Store::default();

let mut imports = Imports::new();
Expand All @@ -79,7 +97,7 @@ fn tinywasm() -> Result<()> {
}

fn hello() -> Result<()> {
let module = Module::parse_file("./examples/rust/out/hello.wasm")?;
let module = Module::parse_file("./examples/rust/out/hello.opt.wasm")?;
let mut store = Store::default();

let mut imports = Imports::new();
Expand Down Expand Up @@ -108,7 +126,7 @@ fn hello() -> Result<()> {
}

fn printi32() -> Result<()> {
let module = Module::parse_file("./examples/rust/out/print.wasm")?;
let module = Module::parse_file("./examples/rust/out/print.opt.wasm")?;
let mut store = Store::default();

let mut imports = Imports::new();
Expand Down Expand Up @@ -151,3 +169,38 @@ fn argon2id() -> Result<()> {

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_hello() {
hello().unwrap();
}

#[test]
fn test_printi32() {
printi32().unwrap();
}

#[test]
fn test_fibonacci() {
fibonacci().unwrap();
}

#[test]
fn test_tinywasm() {
tinywasm().unwrap();
}

#[test]
fn test_tinywasm_no_std() {
tinywasm_no_std().unwrap();
}

#[test]
fn test_argon2id() {
argon2id().unwrap();
}
}

0 comments on commit c5f45ff

Please sign in to comment.