Skip to content

Commit

Permalink
chore(wasm-instrument): rename syscall table tests to core-backend
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop committed Oct 30, 2023
1 parent a34fcfc commit f530af9
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 61 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions core-backend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,62 @@ fn test_register_write_as_with_zero_size() {
core::mem::size_of::<u8>() as u32
);
}

/// Check that all sys calls are supported by backend.
#[test]
fn test_sys_calls_table() {
use crate::{
env::{BackendReport, Environment},
error::ActorTerminationReason,
mock::MockExt,
};
use gear_core::message::DispatchKind;
use gear_wasm_instrument::{
gas_metering::ConstantCostRules,
inject,
parity_wasm::{self, builder},
SysCallName,
};

// Make module with one empty function.
let mut module = builder::module()
.function()
.signature()
.build()
.build()
.build();

// Insert syscalls imports.
for name in SysCallName::instrumentable() {
let sign = name.signature();
let types = module.type_section_mut().unwrap().types_mut();
let type_no = types.len() as u32;
types.push(parity_wasm::elements::Type::Function(sign.func_type()));

module = builder::from_module(module)
.import()
.module("env")
.external()
.func(type_no)
.field(name.to_str())
.build()
.build();
}

let module = inject(module, &ConstantCostRules::default(), "env").unwrap();
let code = module.into_bytes().unwrap();

// Execute wasm and check success.
let ext = MockExt::default();
let env =
Environment::new(ext, &code, DispatchKind::Init, Default::default(), 0.into()).unwrap();
let report = env
.execute(|_, _, _| -> Result<(), u32> { Ok(()) })
.unwrap();

let BackendReport {
termination_reason, ..
} = report;

assert_eq!(termination_reason, ActorTerminationReason::Success.into());
}
2 changes: 0 additions & 2 deletions utils/wasm-instrument/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ enum-iterator.workspace = true
[dev-dependencies]
wasmparser.workspace = true
wat.workspace = true
gear-core-backend = { workspace = true, features = ["mock"] }
gear-core.workspace = true

[features]
default = ["std"]
std = [
"gear-core-backend/std",
"gwasm-instrument/std",
]
6 changes: 3 additions & 3 deletions utils/wasm-instrument/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ extern crate alloc;
use alloc::vec;

use gwasm_instrument::{
gas_metering::{self, Rules},
gas_metering::Rules,
parity_wasm::{
builder,
elements::{self, BlockType, ImportCountType, Instruction, Instructions, Local, ValueType},
},
};

use crate::syscalls::SysCallName;
pub use gwasm_instrument::{self as wasm_instrument, parity_wasm};
pub use crate::syscalls::SysCallName;
pub use gwasm_instrument::{self as wasm_instrument, gas_metering, parity_wasm};

#[cfg(test)]
mod tests;
Expand Down
55 changes: 0 additions & 55 deletions utils/wasm-instrument/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,61 +618,6 @@ test_gas_counter_injection! {
"#
}

/// Check that all sys calls are supported by backend.
#[test]
fn test_sys_calls_table() {
use gas_metering::ConstantCostRules;
use gear_core::message::DispatchKind;
use gear_core_backend::{
env::{BackendReport, Environment},
error::ActorTerminationReason,
mock::MockExt,
};
use parity_wasm::builder;

// Make module with one empty function.
let mut module = builder::module()
.function()
.signature()
.build()
.build()
.build();

// Insert syscalls imports.
for name in SysCallName::instrumentable() {
let sign = name.signature();
let types = module.type_section_mut().unwrap().types_mut();
let type_no = types.len() as u32;
types.push(parity_wasm::elements::Type::Function(sign.func_type()));

module = builder::from_module(module)
.import()
.module("env")
.external()
.func(type_no)
.field(name.to_str())
.build()
.build();
}

let module = inject(module, &ConstantCostRules::default(), "env").unwrap();
let code = module.into_bytes().unwrap();

// Execute wasm and check success.
let ext = MockExt::default();
let env =
Environment::new(ext, &code, DispatchKind::Init, Default::default(), 0.into()).unwrap();
let report = env
.execute(|_, _, _| -> Result<(), u32> { Ok(()) })
.unwrap();

let BackendReport {
termination_reason, ..
} = report;

assert_eq!(termination_reason, ActorTerminationReason::Success.into());
}

#[test]
fn check_memory_array_pointers_definition_correctness() {
let sys_calls = SysCallName::instrumentable();
Expand Down

0 comments on commit f530af9

Please sign in to comment.