Skip to content

wasmer: initial integration #9276

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

Merged
merged 6 commits into from
Dec 29, 2022
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
28 changes: 28 additions & 0 deletions projects/wasmer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################

FROM gcr.io/oss-fuzz-base/base-builder-rust

RUN apt-get update && apt-get install -y make autoconf automake libtool \
zlib1g-dev libffi-dev build-essential \
clang-12 llvm-12 llvm-12-dev llvm-12-tools

RUN git clone --depth 1 https://github.com/wasmerio/wasmer wasmer

WORKDIR wasmer

COPY *.rs fuzz/fuzz_targets/
COPY build.sh default.options $SRC/
37 changes: 37 additions & 0 deletions projects/wasmer/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash -eu
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by ap plicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################

export LLVM_SYS_120_PREFIX=$(llvm-config-12 --prefix)

cargo +nightly fuzz build universal_cranelift --features=universal,cranelift -O
cargo +nightly fuzz build universal_llvm --features=universal,llvm -O
cargo +nightly fuzz build universal_singlepass --features=universal,singlepass -O
cargo +nightly fuzz build metering --features=universal,cranelift -O
cargo +nightly fuzz build deterministic --features=universal,cranelift,llvm,singlepass -O

fuzz_targets="universal_cranelift \
universal_llvm \
universal_singlepass \
metering \
deterministic"
fuzz_target_output_dir=target/x86_64-unknown-linux-gnu/release

for target in $fuzz_targets
do
cp $fuzz_target_output_dir/$target $OUT/
cp $SRC/default.options $OUT/$target.options
done
2 changes: 2 additions & 0 deletions projects/wasmer/default.options
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[libfuzzer]
detect_leaks=0
85 changes: 85 additions & 0 deletions projects/wasmer/deterministic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
*/
#![no_main]

use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target};
use wasm_smith::{Config, ConfiguredModule};
use wasmer::{CompilerConfig, Engine, EngineBuilder, Module, Store};
use wasmer_compiler_cranelift::Cranelift;
use wasmer_compiler_llvm::LLVM;
use wasmer_compiler_singlepass::Singlepass;

#[derive(Arbitrary, Debug, Default, Copy, Clone)]
struct NoImportsConfig;
impl Config for NoImportsConfig {
fn max_imports(&self) -> usize {
0
}
fn max_memory_pages(&self) -> u32 {
// https://github.com/wasmerio/wasmer/issues/2187
65535
}
fn allow_start_export(&self) -> bool {
false
}
}

fn compile_and_compare(name: &str, engine: Engine, wasm: &[u8]) {
let store = Store::new(engine);

// compile for first time
let module = Module::new(&store, wasm).unwrap();
let first = module.serialize().unwrap();

// compile for second time
let module = Module::new(&store, wasm).unwrap();
let second = module.serialize().unwrap();

if first != second {
panic!("non-deterministic compilation from {}", name);
}
}

fuzz_target!(|module: ConfiguredModule<NoImportsConfig>| {
let wasm_bytes = module.to_bytes();

let mut compiler = Cranelift::default();
compiler.canonicalize_nans(true);
compiler.enable_verifier();
compile_and_compare(
"universal-cranelift",
EngineBuilder::new(compiler.clone()).engine(),
&wasm_bytes,
);

let mut compiler = LLVM::default();
compiler.canonicalize_nans(true);
compiler.enable_verifier();
compile_and_compare(
"universal-llvm",
EngineBuilder::new(compiler.clone()).engine(),
&wasm_bytes,
);

let compiler = Singlepass::default();
compile_and_compare(
"universal-singlepass",
EngineBuilder::new(compiler.clone()).engine(),
&wasm_bytes,
);
});
89 changes: 89 additions & 0 deletions projects/wasmer/metering.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
*/
#![no_main]

use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target};
use std::sync::Arc;
use wasm_smith::{Config, ConfiguredModule};
use wasmer::wasmparser::Operator;
use wasmer::{imports, CompilerConfig, Instance, Module, Store};
use wasmer_compiler_cranelift::Cranelift;
use wasmer_middlewares::Metering;

#[derive(Arbitrary, Debug, Default, Copy, Clone)]
struct NoImportsConfig;
impl Config for NoImportsConfig {
fn max_imports(&self) -> usize {
0
}
fn max_memory_pages(&self) -> u32 {
// https://github.com/wasmerio/wasmer/issues/2187
65535
}
fn allow_start_export(&self) -> bool {
false
}
}

#[derive(Arbitrary)]
struct WasmSmithModule(ConfiguredModule<NoImportsConfig>);
impl std::fmt::Debug for WasmSmithModule {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&wasmprinter::print_bytes(self.0.to_bytes()).unwrap())
}
}

fn cost(operator: &Operator) -> u64 {
match operator {
Operator::LocalGet { .. } | Operator::I32Const { .. } => 1,
Operator::I32Add { .. } => 2,
_ => 0,
}
}

fuzz_target!(|module: WasmSmithModule| {
let wasm_bytes = module.0.to_bytes();

if let Ok(path) = std::env::var("DUMP_TESTCASE") {
use std::fs::File;
use std::io::Write;
let mut file = File::create(path).unwrap();
file.write_all(&wasm_bytes).unwrap();
return;
}

let mut compiler = Cranelift::default();
compiler.canonicalize_nans(true);
compiler.enable_verifier();
let metering = Arc::new(Metering::new(10, cost));
compiler.push_middleware(metering);
let mut store = Store::new(compiler);
let module = Module::new(&store, &wasm_bytes).unwrap();
match Instance::new(&mut store, &module, &imports! {}) {
Ok(_) => {}
Err(e) => {
let error_message = format!("{}", e);
if error_message.starts_with("RuntimeError: ")
&& error_message.contains("out of bounds")
{
return;
}
panic!("{}", e);
}
}
});
14 changes: 14 additions & 0 deletions projects/wasmer/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
homepage: "https://github.com/wasmerio/wasmer"
language: rust
main_repo: "https://github.com/wasmerio/wasmer"
primary_contact: "[email protected]"
auto_ccs:
- [email protected]
- [email protected]
- [email protected]
sanitizers:
- address
fuzzing_engines:
- libfuzzer
vendor_ccs:
- [email protected]
75 changes: 75 additions & 0 deletions projects/wasmer/universal_cranelift.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
*/
#![no_main]

use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target};
use wasm_smith::{Config, ConfiguredModule};
use wasmer::{imports, CompilerConfig, Instance, Module, Store};
use wasmer_compiler_cranelift::Cranelift;

#[derive(Arbitrary, Debug, Default, Copy, Clone)]
struct NoImportsConfig;
impl Config for NoImportsConfig {
fn max_imports(&self) -> usize {
0
}
fn max_memory_pages(&self) -> u32 {
// https://github.com/wasmerio/wasmer/issues/2187
65535
}
fn allow_start_export(&self) -> bool {
false
}
}
#[derive(Arbitrary)]
struct WasmSmithModule(ConfiguredModule<NoImportsConfig>);
impl std::fmt::Debug for WasmSmithModule {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&wasmprinter::print_bytes(self.0.to_bytes()).unwrap())
}
}

fuzz_target!(|module: WasmSmithModule| {
let wasm_bytes = module.0.to_bytes();

if let Ok(path) = std::env::var("DUMP_TESTCASE") {
use std::fs::File;
use std::io::Write;
let mut file = File::create(path).unwrap();
file.write_all(&wasm_bytes).unwrap();
return;
}

let mut compiler = Cranelift::default();
compiler.canonicalize_nans(true);
compiler.enable_verifier();
let mut store = Store::new(compiler);
let module = Module::new(&store, &wasm_bytes).unwrap();
match Instance::new(&mut store, &module, &imports! {}) {
Ok(_) => {}
Err(e) => {
let error_message = format!("{}", e);
if error_message.starts_with("RuntimeError: ")
&& error_message.contains("out of bounds")
{
return;
}
panic!("{}", e);
}
}
});
Loading