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

Add misc tests #751

Merged
merged 6 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

- [#751](https://github.com/FuelLabs/fuel-vm/pull/751): Improve test coverage.

## [Version 0.52.0]

### Changed
Expand Down
6 changes: 3 additions & 3 deletions fuel-vm/benches/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn execution(c: &mut Criterion) {
);
let script = TransactionBuilder::script(
vec![
op::meq(RegId::new(0x10), RegId::ZERO, RegId::ZERO, RegId::ZERO),
op::meq(RegId::WRITABLE, RegId::ZERO, RegId::ZERO, RegId::ZERO),
op::jmpb(RegId::ZERO, 0),
]
.into_iter()
Expand Down Expand Up @@ -84,7 +84,7 @@ fn execution(c: &mut Criterion) {

let script = TransactionBuilder::script(
vec![
op::add(RegId::new(0x10), RegId::ZERO, RegId::ONE),
op::add(RegId::WRITABLE, RegId::ZERO, RegId::ONE),
op::jmpb(RegId::ZERO, 0),
]
.into_iter()
Expand Down Expand Up @@ -129,7 +129,7 @@ fn execution(c: &mut Criterion) {

let script = TransactionBuilder::script(
vec![
op::not(RegId::new(0x10), RegId::ZERO),
op::not(RegId::WRITABLE, RegId::ZERO),
op::jmpb(RegId::ZERO, 0),
]
.into_iter()
Expand Down
1 change: 1 addition & 0 deletions fuel-vm/src/interpreter/alu/muldiv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ mod tests {
use super::*;

#[rstest::rstest]
#[case(0, 0, 0, 0, 0)]
#[case(0, 0, 1, 0, 0)]
#[case(0, 5, 1, 0, 0)]
#[case(9, 9, 1, 81, 0)]
Expand Down
312 changes: 179 additions & 133 deletions fuel-vm/src/interpreter/debug.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
#[cfg(test)]
use alloc::{
vec,
vec::Vec,
};

use super::Interpreter;
use crate::prelude::*;
use fuel_asm::RegId;
Expand Down Expand Up @@ -63,140 +57,192 @@ where
}
}

#[test]
fn breakpoint_script() {
use fuel_asm::op;
use fuel_tx::ConsensusParameters;

let mut vm = Interpreter::<_, _, _>::with_memory_storage();

let gas_limit = 1_000_000;
let gas_price = 0;
let height = Default::default();

let script = [
op::addi(0x10, RegId::ZERO, 8),
op::addi(0x11, RegId::ZERO, 16),
op::addi(0x12, RegId::ZERO, 32),
op::addi(0x13, RegId::ZERO, 64),
op::addi(0x14, RegId::ZERO, 128),
op::ret(0x10),
]
.into_iter()
.collect();

let consensus_params = ConsensusParameters::standard();

let tx = TransactionBuilder::script(script, vec![])
.script_gas_limit(gas_limit)
.add_random_fee_input()
.finalize()
.into_checked(height, &consensus_params)
.expect("failed to generate checked tx")
.into_ready(
gas_price,
consensus_params.gas_costs(),
consensus_params.fee_params(),
)
.unwrap();

let suite = vec![
(
Breakpoint::script(0),
vec![(0x10, 0), (0x11, 0), (0x12, 0), (0x13, 0), (0x14, 0)],
),
(
Breakpoint::script(2),
vec![(0x10, 8), (0x11, 16), (0x12, 0), (0x13, 0), (0x14, 0)],
),
(
Breakpoint::script(3),
vec![(0x10, 8), (0x11, 16), (0x12, 32), (0x13, 0), (0x14, 0)],
),
(
Breakpoint::script(5),
vec![(0x10, 8), (0x11, 16), (0x12, 32), (0x13, 64), (0x14, 128)],
),
];

suite.iter().for_each(|(b, _)| vm.set_breakpoint(*b));

let state = vm
.transact(tx)
.map(ProgramState::from)
.expect("Failed to execute script!");

suite
#[cfg(test)]
mod tests {
use alloc::{
vec,
vec::Vec,
};

use super::Interpreter;
use crate::prelude::*;
use fuel_asm::RegId;

#[test]
fn breakpoint_script() {
use fuel_asm::op;
use fuel_tx::ConsensusParameters;

let mut vm = Interpreter::<_, _, _>::with_memory_storage();

let gas_limit = 1_000_000;
let gas_price = 0;
let height = Default::default();

let script = [
op::addi(0x10, RegId::ZERO, 8),
op::addi(0x11, RegId::ZERO, 16),
op::addi(0x12, RegId::ZERO, 32),
op::addi(0x13, RegId::ZERO, 64),
op::addi(0x14, RegId::ZERO, 128),
op::ret(0x10),
Comment on lines +83 to +87
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to use some constant names for registers. It will connect them here with the values in suite

]
.into_iter()
.collect();

let consensus_params = ConsensusParameters::standard();

let tx = TransactionBuilder::script(script, vec![])
.script_gas_limit(gas_limit)
.add_random_fee_input()
.finalize()
.into_checked(height, &consensus_params)
.expect("failed to generate checked tx")
.into_ready(
gas_price,
consensus_params.gas_costs(),
consensus_params.fee_params(),
)
.unwrap();

let suite = vec![
(
Breakpoint::script(0),
vec![(0x10, 0), (0x11, 0), (0x12, 0), (0x13, 0), (0x14, 0)],
),
(
Breakpoint::script(2),
vec![(0x10, 8), (0x11, 16), (0x12, 0), (0x13, 0), (0x14, 0)],
),
(
Breakpoint::script(3),
vec![(0x10, 8), (0x11, 16), (0x12, 32), (0x13, 0), (0x14, 0)],
),
(
Breakpoint::script(5),
vec![(0x10, 8), (0x11, 16), (0x12, 32), (0x13, 64), (0x14, 128)],
),
];

suite.iter().for_each(|(b, _)| vm.set_breakpoint(*b));

let state = vm
.transact(tx)
.map(ProgramState::from)
.expect("Failed to execute script!");

suite
.into_iter()
.fold(state, |state, (breakpoint, registers)| {
let debug = state.debug_ref().expect("Expected breakpoint");
let b = debug
.breakpoint()
.expect("State without expected breakpoint");

assert_eq!(&breakpoint, b);
registers.into_iter().for_each(|(r, w)| {
assert_eq!(w, vm.registers()[r]);
});

vm.resume().expect("Failed to resume")
});
}

#[test]
fn single_stepping() {
use fuel_asm::op;
use fuel_tx::ConsensusParameters;

let mut vm = Interpreter::<_, _, _>::with_memory_storage();

let gas_limit = 1_000_000;
let height = Default::default();
let gas_price = 0;

// Repeats the middle two instructions five times
let script = [
op::addi(0x10, RegId::ZERO, 5),
op::addi(0x11, 0x11, 1),
op::jnei(0x10, 0x11, 1),
op::ret(0x10),
]
.into_iter()
.fold(state, |state, (breakpoint, registers)| {
let debug = state.debug_ref().expect("Expected breakpoint");
.collect();

let consensus_params = ConsensusParameters::standard();

let tx = TransactionBuilder::script(script, vec![])
.script_gas_limit(gas_limit)
.add_random_fee_input()
.finalize()
.into_checked(height, &consensus_params)
.expect("failed to generate checked tx")
.into_ready(
gas_price,
consensus_params.gas_costs(),
consensus_params.fee_params(),
)
.unwrap();

Comment on lines +172 to +185
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we repeat this part several times, maybe makes sense to move it into a function

vm.set_single_stepping(true);

let mut state = vm
.transact(tx)
.map(ProgramState::from)
.expect("Failed to execute script!");

let mut stops = Vec::new();

while let Some(debug) = state.debug_ref() {
let b = debug
.breakpoint()
.expect("State without expected breakpoint");

assert_eq!(&breakpoint, b);
registers.into_iter().for_each(|(r, w)| {
assert_eq!(w, vm.registers()[r]);
});
stops.push(b.pc());

vm.resume().expect("Failed to resume")
});
}
state = vm.resume().expect("Failed to resume");
}

#[test]
fn single_stepping() {
use fuel_asm::op;
use fuel_tx::ConsensusParameters;

let mut vm = Interpreter::<_, _, _>::with_memory_storage();

let gas_limit = 1_000_000;
let height = Default::default();
let gas_price = 0;

// Repeats the middle two instructions five times
let script = [
op::addi(0x10, RegId::ZERO, 5),
op::addi(0x11, 0x11, 1),
op::jnei(0x10, 0x11, 1),
op::ret(0x10),
]
.into_iter()
.collect();

let consensus_params = ConsensusParameters::standard();

let tx = TransactionBuilder::script(script, vec![])
.script_gas_limit(gas_limit)
.add_random_fee_input()
.finalize()
.into_checked(height, &consensus_params)
.expect("failed to generate checked tx")
.into_ready(
gas_price,
consensus_params.gas_costs(),
consensus_params.fee_params(),
)
.unwrap();

vm.set_single_stepping(true);

let mut state = vm
.transact(tx)
.map(ProgramState::from)
.expect("Failed to execute script!");

let mut stops = Vec::new();

while let Some(debug) = state.debug_ref() {
let b = debug
.breakpoint()
.expect("State without expected breakpoint");

stops.push(b.pc());

state = vm.resume().expect("Failed to resume");
assert_eq!(stops, vec![0, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 12]);
}

assert_eq!(stops, vec![0, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 12]);
#[test]
fn resume_without_debug() {
use fuel_asm::op;
use fuel_tx::ConsensusParameters;

let mut vm = Interpreter::<_, _, _>::with_memory_storage();
vm.resume()
.expect_err("Expected error when resuming without debug");

let gas_limit = 1_000_000;
let height = Default::default();
let gas_price = 0;

// Just returns
let script: Vec<u8> = [op::ret(0x10)].into_iter().collect();

let consensus_params = ConsensusParameters::standard();

let tx = TransactionBuilder::script(script, vec![])
.script_gas_limit(gas_limit)
.add_random_fee_input()
.finalize()
.into_checked(height, &consensus_params)
.expect("failed to generate checked tx")
.into_ready(
gas_price,
consensus_params.gas_costs(),
consensus_params.fee_params(),
)
.unwrap();

let _ = vm
.transact(tx)
.map(ProgramState::from)
.expect("Failed to execute script!");

vm.resume()
.expect_err("Expected error when resuming without debug");
}
}
2 changes: 1 addition & 1 deletion fuel-vm/src/interpreter/ecal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
M: Memory;
}

/// Default ECAL opcode handler function, which charges for `noop` and does nothing.
/// Default ECAL opcode handler function, which just errors immediately.
impl EcalHandler for NotSupportedEcal {
fn ecal<M, S, Tx>(
_: &mut Interpreter<M, S, Tx, Self>,
Expand Down
Loading
Loading