mirror of
https://github.com/fluencelabs/aquavm
synced 2025-06-27 21:51:34 +00:00
Run tests with sanitizers (#274)
As `wasm32-wasi` target doesn't seem to support sanitizers, we are to execute tests with native code. The feature `test_with_native_code` is introduced for that. Closes #247.
This commit is contained in:
@ -31,6 +31,10 @@ jobs:
|
|||||||
cargo fmt --all -- --check --color always
|
cargo fmt --all -- --check --color always
|
||||||
cargo check
|
cargo check
|
||||||
cargo test --release
|
cargo test --release
|
||||||
|
# The memory sanitizer on cargo test has false positive even on empty project.
|
||||||
|
for san in address leak; do
|
||||||
|
RUSTFLAGS="-Z sanitizer=$san" cargo test --features test_with_native_code --target x86_64-unknown-linux-gnu
|
||||||
|
done
|
||||||
cargo clippy -v
|
cargo clippy -v
|
||||||
|
|
||||||
- save_cache:
|
- save_cache:
|
||||||
|
@ -58,7 +58,7 @@ impl CallServiceResult {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn into_raw_result(call_results: CallResults) -> air_interpreter_interface::CallResults {
|
pub fn into_raw_result(call_results: CallResults) -> air_interpreter_interface::CallResults {
|
||||||
call_results
|
call_results
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(call_id, call_result)| (call_id, call_result.into_raw()))
|
.map(|(call_id, call_result)| (call_id, call_result.into_raw()))
|
||||||
|
@ -33,7 +33,7 @@ pub struct RawAVMOutcome {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl RawAVMOutcome {
|
impl RawAVMOutcome {
|
||||||
pub(crate) fn from_interpreter_outcome(outcome: InterpreterOutcome) -> RunnerResult<Self> {
|
pub fn from_interpreter_outcome(outcome: InterpreterOutcome) -> RunnerResult<Self> {
|
||||||
let InterpreterOutcome {
|
let InterpreterOutcome {
|
||||||
ret_code,
|
ret_code,
|
||||||
error_message,
|
error_message,
|
||||||
|
@ -23,3 +23,6 @@ fstrings = "0.2.3"
|
|||||||
object-pool = "0.5.4"
|
object-pool = "0.5.4"
|
||||||
once_cell = "1.10.0"
|
once_cell = "1.10.0"
|
||||||
serde_json = "1.0.61"
|
serde_json = "1.0.61"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
test_with_native_code = []
|
||||||
|
@ -29,6 +29,11 @@ pub mod call_services;
|
|||||||
pub mod executed_state;
|
pub mod executed_state;
|
||||||
pub mod test_runner;
|
pub mod test_runner;
|
||||||
|
|
||||||
|
#[cfg(feature = "test_with_native_code")]
|
||||||
|
mod native_test_runner;
|
||||||
|
#[cfg(not(feature = "test_with_native_code"))]
|
||||||
|
mod wasm_test_runner;
|
||||||
|
|
||||||
pub use air::interpreter_data::*;
|
pub use air::interpreter_data::*;
|
||||||
pub use avm_server::raw_outcome::*;
|
pub use avm_server::raw_outcome::*;
|
||||||
pub use avm_server::*;
|
pub use avm_server::*;
|
||||||
|
63
crates/air-lib/test-utils/src/native_test_runner.rs
Normal file
63
crates/air-lib/test-utils/src/native_test_runner.rs
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2022 Fluence Labs Limited
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use crate::test_runner::AirRunner;
|
||||||
|
use air_interpreter_interface::RunParameters;
|
||||||
|
use avm_server::avm_runner::*;
|
||||||
|
use avm_server::into_raw_result;
|
||||||
|
|
||||||
|
pub struct NativeAirRunner {
|
||||||
|
current_peer_id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AirRunner for NativeAirRunner {
|
||||||
|
fn new(current_peer_id: impl Into<String>) -> Self {
|
||||||
|
Self {
|
||||||
|
current_peer_id: current_peer_id.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(
|
||||||
|
&mut self,
|
||||||
|
air: impl Into<String>,
|
||||||
|
prev_data: impl Into<Vec<u8>>,
|
||||||
|
data: impl Into<Vec<u8>>,
|
||||||
|
init_peer_id: impl Into<String>,
|
||||||
|
timestamp: u64,
|
||||||
|
ttl: u32,
|
||||||
|
call_results: avm_server::CallResults,
|
||||||
|
) -> Result<RawAVMOutcome, Box<dyn std::error::Error>> {
|
||||||
|
// some inner parts transformations
|
||||||
|
let raw_call_results = into_raw_result(call_results);
|
||||||
|
let raw_call_results = serde_json::to_vec(&raw_call_results).unwrap();
|
||||||
|
|
||||||
|
let outcome = air::execute_air(
|
||||||
|
air.into(),
|
||||||
|
prev_data.into(),
|
||||||
|
data.into(),
|
||||||
|
RunParameters {
|
||||||
|
init_peer_id: init_peer_id.into(),
|
||||||
|
current_peer_id: self.current_peer_id.clone(),
|
||||||
|
timestamp,
|
||||||
|
ttl,
|
||||||
|
},
|
||||||
|
raw_call_results,
|
||||||
|
);
|
||||||
|
let outcome = RawAVMOutcome::from_interpreter_outcome(outcome)?;
|
||||||
|
|
||||||
|
Ok(outcome)
|
||||||
|
}
|
||||||
|
}
|
@ -14,34 +14,35 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#[cfg(feature = "test_with_native_code")]
|
||||||
|
use crate::native_test_runner::NativeAirRunner as AirRunnerImpl;
|
||||||
|
#[cfg(not(feature = "test_with_native_code"))]
|
||||||
|
use crate::wasm_test_runner::WasmAirRunner as AirRunnerImpl;
|
||||||
|
|
||||||
use super::CallServiceClosure;
|
use super::CallServiceClosure;
|
||||||
use avm_server::avm_runner::*;
|
use avm_server::avm_runner::*;
|
||||||
|
|
||||||
use once_cell::sync::OnceCell;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
// 10 Mb
|
pub trait AirRunner {
|
||||||
const AVM_MAX_HEAP_SIZE: u64 = 10 * 1024 * 1024;
|
fn new(current_call_id: impl Into<String>) -> Self;
|
||||||
const AIR_WASM_PATH: &str = "../target/wasm32-wasi/debug/air_interpreter_server.wasm";
|
|
||||||
|
|
||||||
pub struct TestRunner {
|
fn call(
|
||||||
pub runner: object_pool::Reusable<'static, AVMRunner>,
|
&mut self,
|
||||||
pub call_service: CallServiceClosure,
|
air: impl Into<String>,
|
||||||
|
prev_data: impl Into<Vec<u8>>,
|
||||||
|
data: impl Into<Vec<u8>>,
|
||||||
|
init_peer_id: impl Into<String>,
|
||||||
|
timestamp: u64,
|
||||||
|
ttl: u32,
|
||||||
|
call_results: avm_server::CallResults,
|
||||||
|
) -> Result<RawAVMOutcome, Box<dyn std::error::Error>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_pooled_avm_runner() -> AVMRunner {
|
pub struct TestRunner<R = AirRunnerImpl> {
|
||||||
let fake_current_peer_id = "";
|
pub runner: R,
|
||||||
let logging_mask = i32::MAX;
|
pub call_service: CallServiceClosure,
|
||||||
|
|
||||||
AVMRunner::new(
|
|
||||||
PathBuf::from(AIR_WASM_PATH),
|
|
||||||
fake_current_peer_id,
|
|
||||||
Some(AVM_MAX_HEAP_SIZE),
|
|
||||||
logging_mask,
|
|
||||||
)
|
|
||||||
.expect("vm should be created")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
@ -51,7 +52,7 @@ pub struct TestRunParameters {
|
|||||||
pub ttl: u32,
|
pub ttl: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestRunner {
|
impl<R: AirRunner> TestRunner<R> {
|
||||||
pub fn call(
|
pub fn call(
|
||||||
&mut self,
|
&mut self,
|
||||||
air: impl Into<String>,
|
air: impl Into<String>,
|
||||||
@ -73,7 +74,7 @@ impl TestRunner {
|
|||||||
let mut next_peer_pks = HashSet::new();
|
let mut next_peer_pks = HashSet::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut outcome = self
|
let mut outcome: RawAVMOutcome = self
|
||||||
.runner
|
.runner
|
||||||
.call(
|
.call(
|
||||||
air.clone(),
|
air.clone(),
|
||||||
@ -112,18 +113,7 @@ pub fn create_avm(
|
|||||||
call_service: CallServiceClosure,
|
call_service: CallServiceClosure,
|
||||||
current_peer_id: impl Into<String>,
|
current_peer_id: impl Into<String>,
|
||||||
) -> TestRunner {
|
) -> TestRunner {
|
||||||
static POOL_CELL: OnceCell<object_pool::Pool<AVMRunner>> = OnceCell::new();
|
let runner = AirRunnerImpl::new(current_peer_id);
|
||||||
|
|
||||||
let pool = POOL_CELL.get_or_init(|| {
|
|
||||||
object_pool::Pool::new(
|
|
||||||
// we create an empty pool and let it fill on demand
|
|
||||||
0,
|
|
||||||
|| unreachable!(),
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut runner = pool.pull(make_pooled_avm_runner);
|
|
||||||
runner.set_peer_id(current_peer_id);
|
|
||||||
|
|
||||||
TestRunner {
|
TestRunner {
|
||||||
runner,
|
runner,
|
||||||
|
80
crates/air-lib/test-utils/src/wasm_test_runner.rs
Normal file
80
crates/air-lib/test-utils/src/wasm_test_runner.rs
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2022 Fluence Labs Limited
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use crate::test_runner::AirRunner;
|
||||||
|
use avm_server::avm_runner::*;
|
||||||
|
|
||||||
|
use once_cell::sync::OnceCell;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
// 10 Mb
|
||||||
|
const AVM_MAX_HEAP_SIZE: u64 = 10 * 1024 * 1024;
|
||||||
|
const AIR_WASM_PATH: &str = "../target/wasm32-wasi/debug/air_interpreter_server.wasm";
|
||||||
|
|
||||||
|
pub struct WasmAirRunner(object_pool::Reusable<'static, AVMRunner>);
|
||||||
|
|
||||||
|
fn make_pooled_avm_runner() -> AVMRunner {
|
||||||
|
let fake_current_peer_id = "";
|
||||||
|
let logging_mask = i32::MAX;
|
||||||
|
|
||||||
|
AVMRunner::new(
|
||||||
|
PathBuf::from(AIR_WASM_PATH),
|
||||||
|
fake_current_peer_id,
|
||||||
|
Some(AVM_MAX_HEAP_SIZE),
|
||||||
|
logging_mask,
|
||||||
|
)
|
||||||
|
.expect("vm should be created")
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AirRunner for WasmAirRunner {
|
||||||
|
fn new(current_peer_id: impl Into<String>) -> Self {
|
||||||
|
static POOL_CELL: OnceCell<object_pool::Pool<AVMRunner>> = OnceCell::new();
|
||||||
|
|
||||||
|
let pool = POOL_CELL.get_or_init(|| {
|
||||||
|
object_pool::Pool::new(
|
||||||
|
// we create an empty pool and let it fill on demand
|
||||||
|
0,
|
||||||
|
|| unreachable!(),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut runner = pool.pull(make_pooled_avm_runner);
|
||||||
|
runner.set_peer_id(current_peer_id);
|
||||||
|
|
||||||
|
Self(runner)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(
|
||||||
|
&mut self,
|
||||||
|
air: impl Into<String>,
|
||||||
|
prev_data: impl Into<Vec<u8>>,
|
||||||
|
data: impl Into<Vec<u8>>,
|
||||||
|
init_peer_id: impl Into<String>,
|
||||||
|
timestamp: u64,
|
||||||
|
ttl: u32,
|
||||||
|
call_results: avm_server::CallResults,
|
||||||
|
) -> Result<RawAVMOutcome, Box<dyn std::error::Error>> {
|
||||||
|
Ok(self.0.call(
|
||||||
|
air,
|
||||||
|
prev_data,
|
||||||
|
data,
|
||||||
|
init_peer_id,
|
||||||
|
timestamp,
|
||||||
|
ttl,
|
||||||
|
call_results,
|
||||||
|
)?)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user