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

Rust bindings (validate only) #538

Merged
merged 3 commits into from
Oct 1, 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
3 changes: 3 additions & 0 deletions .bumpversion.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ values =
rel

[bumpversion:file:CMakeLists.txt]

[bumpversion:file:bindings/rust/Cargo.toml]
search = version = \"{current_version}\"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/build
/cmake-build-*
/.idea
Cargo.lock
/target
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[workspace]
members = [
"bindings/rust"
]
3 changes: 3 additions & 0 deletions bindings/rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
**/*.rs.bk
/Cargo.lock
17 changes: 17 additions & 0 deletions bindings/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Fizzy: A fast WebAssembly interpreter
# Copyright 2019-2020 The Fizzy Authors.
# SPDX-License-Identifier: Apache-2.0

[package]
name = "fizzy"
version = "0.6.0-dev"
authors = ["Alex Beregszaszi <alex@rtfs.hu>"]
license = "Apache-2.0"
repository = "https://github.com/wasmx/fizzy"
description = "Bindings to Fizzy"
categories = ["external-ffi-bindings"]
edition = "2018"

[build-dependencies]
bindgen = "0.54.0"
cmake = "0.1"
59 changes: 59 additions & 0 deletions bindings/rust/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Fizzy: A fast WebAssembly interpreter
// Copyright 2019-2020 The Fizzy Authors.
// SPDX-License-Identifier: Apache-2.0

extern crate bindgen;
extern crate cmake;

use cmake::Config;

use std::env;
use std::path::PathBuf;

fn main() {
// This is the root directory.
let src = "../../";

let dst = Config::new(src).define("FIZZY_TESTING", "OFF").build();

println!("cargo:rustc-link-lib=static=fizzy");
println!("cargo:rustc-link-search=native={}/lib", dst.display());

// We need to link against C++ std lib
if let Some(cpp_stdlib) = get_cpp_stdlib() {
println!("cargo:rustc-link-lib={}", cpp_stdlib);
}

let bindings = bindgen::Builder::default()
.header(format!("{}/include/fizzy/fizzy.h", src))
// See https://github.com/rust-lang-nursery/rust-bindgen/issues/947
.trust_clang_mangling(false)
.generate_comments(true)
// https://github.com/rust-lang-nursery/rust-bindgen/issues/947#issuecomment-327100002
.layout_tests(false)
.whitelist_function("fizzy_.*")
// TODO: consider removing this
.size_t_is_usize(true)
.generate()
.expect("Unable to generate bindings");

let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Could not write bindings");
}

// See https://github.com/alexcrichton/gcc-rs/blob/88ac58e25/src/lib.rs#L1197
fn get_cpp_stdlib() -> Option<String> {
env::var("TARGET").ok().and_then(|target| {
if target.contains("msvc") {
None
} else if target.contains("darwin") || target.contains("freebsd") {
Some("c++".to_string())
} else if target.contains("musl") {
Some("static=stdc++".to_string())
} else {
Some("stdc++".to_string())
}
})
}
32 changes: 32 additions & 0 deletions bindings/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Fizzy: A fast WebAssembly interpreter
chfast marked this conversation as resolved.
Show resolved Hide resolved
// Copyright 2019-2020 The Fizzy Authors.
// SPDX-License-Identifier: Apache-2.0

mod sys;

pub fn validate(input: &[u8]) -> bool {
unsafe { sys::fizzy_validate(input.as_ptr(), input.len()) }
}

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

#[test]
fn validate_wasm() {
// Empty
assert_eq!(validate(&[]), false);
// Too short
assert_eq!(validate(&[0x00]), false);
// Valid
assert_eq!(
validate(&[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]),
true
);
// Invalid version
assert_eq!(
validate(&[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x01]),
false
);
}
}
9 changes: 9 additions & 0 deletions bindings/rust/src/sys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Fizzy: A fast WebAssembly interpreter
// Copyright 2019-2020 The Fizzy Authors.
// SPDX-License-Identifier: Apache-2.0

#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
49 changes: 49 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,51 @@ jobs:
- spectest
- collect_coverage_data

bindings-rust:
docker:
- image: rust:1-buster
steps:
- checkout
- run:
name: Update environment
command: |
echo 'deb http://deb.debian.org/debian buster-backports main' >> /etc/apt/sources.list
apt -qq update
apt -yq -t buster-backports install cmake --no-install-recommends
apt -yq install libclang-dev clang cmake --no-install-recommends
axic marked this conversation as resolved.
Show resolved Hide resolved
rustup component add rustfmt
- run:
name: Check formatting
command: |
rustfmt --version
cargo fmt --all -- --check
- run:
name: Build
command: cargo build
- run:
name: Test
command: cargo test

bindings-rust-asan:
docker:
- image: rust:1-buster
steps:
- checkout
- run:
name: Update environment
command: |
echo 'deb http://deb.debian.org/debian buster-backports main' >> /etc/apt/sources.list
apt -qq update
apt -yq -t buster-backports install cmake --no-install-recommends
apt -yq install llvm-8-dev clang-8 --no-install-recommends
rustup toolchain install nightly-x86_64-unknown-linux-gnu
- run:
name: Build
command: RUSTFLAGS="-Z sanitizer=address" ASAN_OPTIONS=detect_leaks=1 cargo +nightly build --target x86_64-unknown-linux-gnu
- run:
name: Test
command: RUSTFLAGS="-Z sanitizer=address -C opt-level=0" ASAN_OPTIONS=detect_leaks=1 cargo +nightly test --target x86_64-unknown-linux-gnu

workflows:
version: 2

Expand All @@ -537,6 +582,10 @@ workflows:
- spectest:
requires:
- coverage-clang
- bindings-rust
- bindings-rust-asan:
requires:
- bindings-rust

benchmarking:
when: <<pipeline.parameters.benchmark>>
Expand Down