Skip to content

Commit 8c4315f

Browse files
authored
Merge pull request #538 from wasmx/rust
Rust bindings (validate only)
2 parents 5687aef + 7b9166d commit 8c4315f

9 files changed

Lines changed: 178 additions & 0 deletions

File tree

.bumpversion.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ values =
1919
rel
2020

2121
[bumpversion:file:CMakeLists.txt]
22+
23+
[bumpversion:file:bindings/rust/Cargo.toml]
24+
search = version = \"{current_version}\"

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/build
22
/cmake-build-*
33
/.idea
4+
Cargo.lock
5+
/target

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[workspace]
2+
members = [
3+
"bindings/rust"
4+
]

bindings/rust/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/target
2+
**/*.rs.bk
3+
/Cargo.lock

bindings/rust/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Fizzy: A fast WebAssembly interpreter
2+
# Copyright 2019-2020 The Fizzy Authors.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
[package]
6+
name = "fizzy"
7+
version = "0.6.0-dev"
8+
authors = ["Alex Beregszaszi <alex@rtfs.hu>"]
9+
license = "Apache-2.0"
10+
repository = "https://github.com/wasmx/fizzy"
11+
description = "Bindings to Fizzy"
12+
categories = ["external-ffi-bindings"]
13+
edition = "2018"
14+
15+
[build-dependencies]
16+
bindgen = "0.54.0"
17+
cmake = "0.1"

bindings/rust/build.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Fizzy: A fast WebAssembly interpreter
2+
// Copyright 2019-2020 The Fizzy Authors.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
extern crate bindgen;
6+
extern crate cmake;
7+
8+
use cmake::Config;
9+
10+
use std::env;
11+
use std::path::PathBuf;
12+
13+
fn main() {
14+
// This is the root directory.
15+
let src = "../../";
16+
17+
let dst = Config::new(src).define("FIZZY_TESTING", "OFF").build();
18+
19+
println!("cargo:rustc-link-lib=static=fizzy");
20+
println!("cargo:rustc-link-search=native={}/lib", dst.display());
21+
22+
// We need to link against C++ std lib
23+
if let Some(cpp_stdlib) = get_cpp_stdlib() {
24+
println!("cargo:rustc-link-lib={}", cpp_stdlib);
25+
}
26+
27+
let bindings = bindgen::Builder::default()
28+
.header(format!("{}/include/fizzy/fizzy.h", src))
29+
// See https://github.com/rust-lang-nursery/rust-bindgen/issues/947
30+
.trust_clang_mangling(false)
31+
.generate_comments(true)
32+
// https://github.com/rust-lang-nursery/rust-bindgen/issues/947#issuecomment-327100002
33+
.layout_tests(false)
34+
.whitelist_function("fizzy_.*")
35+
// TODO: consider removing this
36+
.size_t_is_usize(true)
37+
.generate()
38+
.expect("Unable to generate bindings");
39+
40+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
41+
bindings
42+
.write_to_file(out_path.join("bindings.rs"))
43+
.expect("Could not write bindings");
44+
}
45+
46+
// See https://github.com/alexcrichton/gcc-rs/blob/88ac58e25/src/lib.rs#L1197
47+
fn get_cpp_stdlib() -> Option<String> {
48+
env::var("TARGET").ok().and_then(|target| {
49+
if target.contains("msvc") {
50+
None
51+
} else if target.contains("darwin") || target.contains("freebsd") {
52+
Some("c++".to_string())
53+
} else if target.contains("musl") {
54+
Some("static=stdc++".to_string())
55+
} else {
56+
Some("stdc++".to_string())
57+
}
58+
})
59+
}

bindings/rust/src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Fizzy: A fast WebAssembly interpreter
2+
// Copyright 2019-2020 The Fizzy Authors.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
mod sys;
6+
7+
pub fn validate(input: &[u8]) -> bool {
8+
unsafe { sys::fizzy_validate(input.as_ptr(), input.len()) }
9+
}
10+
11+
#[cfg(test)]
12+
mod tests {
13+
use super::*;
14+
15+
#[test]
16+
fn validate_wasm() {
17+
// Empty
18+
assert_eq!(validate(&[]), false);
19+
// Too short
20+
assert_eq!(validate(&[0x00]), false);
21+
// Valid
22+
assert_eq!(
23+
validate(&[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]),
24+
true
25+
);
26+
// Invalid version
27+
assert_eq!(
28+
validate(&[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x01]),
29+
false
30+
);
31+
}
32+
}

bindings/rust/src/sys.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Fizzy: A fast WebAssembly interpreter
2+
// Copyright 2019-2020 The Fizzy Authors.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
#![allow(non_upper_case_globals)]
6+
#![allow(non_camel_case_types)]
7+
#![allow(non_snake_case)]
8+
9+
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

circle.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,51 @@ jobs:
512512
- spectest
513513
- collect_coverage_data
514514

515+
bindings-rust:
516+
docker:
517+
- image: rust:1-buster
518+
steps:
519+
- checkout
520+
- run:
521+
name: Update environment
522+
command: |
523+
echo 'deb http://deb.debian.org/debian buster-backports main' >> /etc/apt/sources.list
524+
apt -qq update
525+
apt -yq -t buster-backports install cmake --no-install-recommends
526+
apt -yq install libclang-dev clang cmake --no-install-recommends
527+
rustup component add rustfmt
528+
- run:
529+
name: Check formatting
530+
command: |
531+
rustfmt --version
532+
cargo fmt --all -- --check
533+
- run:
534+
name: Build
535+
command: cargo build
536+
- run:
537+
name: Test
538+
command: cargo test
539+
540+
bindings-rust-asan:
541+
docker:
542+
- image: rust:1-buster
543+
steps:
544+
- checkout
545+
- run:
546+
name: Update environment
547+
command: |
548+
echo 'deb http://deb.debian.org/debian buster-backports main' >> /etc/apt/sources.list
549+
apt -qq update
550+
apt -yq -t buster-backports install cmake --no-install-recommends
551+
apt -yq install llvm-8-dev clang-8 --no-install-recommends
552+
rustup toolchain install nightly-x86_64-unknown-linux-gnu
553+
- run:
554+
name: Build
555+
command: RUSTFLAGS="-Z sanitizer=address" ASAN_OPTIONS=detect_leaks=1 cargo +nightly build --target x86_64-unknown-linux-gnu
556+
- run:
557+
name: Test
558+
command: RUSTFLAGS="-Z sanitizer=address -C opt-level=0" ASAN_OPTIONS=detect_leaks=1 cargo +nightly test --target x86_64-unknown-linux-gnu
559+
515560
workflows:
516561
version: 2
517562

@@ -537,6 +582,10 @@ workflows:
537582
- spectest:
538583
requires:
539584
- coverage-clang
585+
- bindings-rust
586+
- bindings-rust-asan:
587+
requires:
588+
- bindings-rust
540589

541590
benchmarking:
542591
when: <<pipeline.parameters.benchmark>>

0 commit comments

Comments
 (0)