diff --git a/benches/elf_loader.rs b/benches/elf_loader.rs index b2e75ba9..e5d54ba1 100644 --- a/benches/elf_loader.rs +++ b/benches/elf_loader.rs @@ -13,7 +13,7 @@ extern crate test_utils; use solana_rbpf::{ syscalls::BpfSyscallU64, user_error::UserError, - vm::{Config, DefaultInstructionMeter, Executable, SyscallObject, SyscallRegistry}, + vm::{Config, Executable, SyscallObject, SyscallRegistry, TestInstructionMeter}, }; use std::{fs::File, io::Read}; use test::Bencher; @@ -32,7 +32,7 @@ fn bench_load_elf(bencher: &mut Bencher) { let mut elf = Vec::new(); file.read_to_end(&mut elf).unwrap(); bencher.iter(|| { - >::from_elf( + >::from_elf( &elf, None, Config::default(), @@ -48,7 +48,7 @@ fn bench_load_elf_without_syscall(bencher: &mut Bencher) { let mut elf = Vec::new(); file.read_to_end(&mut elf).unwrap(); bencher.iter(|| { - let executable = >::from_elf( + let executable = >::from_elf( &elf, None, Config::default(), @@ -65,7 +65,7 @@ fn bench_load_elf_with_syscall(bencher: &mut Bencher) { let mut elf = Vec::new(); file.read_to_end(&mut elf).unwrap(); bencher.iter(|| { - let executable = >::from_elf( + let executable = >::from_elf( &elf, None, Config::default(), diff --git a/benches/jit_compile.rs b/benches/jit_compile.rs index b9476aa1..1fcc1b50 100644 --- a/benches/jit_compile.rs +++ b/benches/jit_compile.rs @@ -11,7 +11,7 @@ extern crate test; use solana_rbpf::{ user_error::UserError, - vm::{Config, DefaultInstructionMeter, EbpfVm, Executable, SyscallRegistry}, + vm::{Config, EbpfVm, Executable, SyscallRegistry, TestInstructionMeter}, }; use std::{fs::File, io::Read}; use test::Bencher; @@ -21,7 +21,7 @@ fn bench_init_vm(bencher: &mut Bencher) { let mut file = File::open("tests/elfs/pass_stack_reference.so").unwrap(); let mut elf = Vec::new(); file.read_to_end(&mut elf).unwrap(); - let executable = >::from_elf( + let executable = >::from_elf( &elf, None, Config::default(), @@ -29,8 +29,7 @@ fn bench_init_vm(bencher: &mut Bencher) { ) .unwrap(); bencher.iter(|| { - EbpfVm::::new(executable.as_ref(), &mut [], &[]) - .unwrap() + EbpfVm::::new(executable.as_ref(), &mut [], &[]).unwrap() }); } @@ -40,7 +39,7 @@ fn bench_jit_compile(bencher: &mut Bencher) { let mut file = File::open("tests/elfs/pass_stack_reference.so").unwrap(); let mut elf = Vec::new(); file.read_to_end(&mut elf).unwrap(); - let mut executable = >::from_elf( + let mut executable = >::from_elf( &elf, None, Config::default(), diff --git a/benches/vm_execution.rs b/benches/vm_execution.rs index d2902375..9f1300a4 100644 --- a/benches/vm_execution.rs +++ b/benches/vm_execution.rs @@ -11,7 +11,7 @@ extern crate test; use solana_rbpf::{ user_error::UserError, - vm::{Config, DefaultInstructionMeter, EbpfVm, Executable, SyscallRegistry}, + vm::{Config, EbpfVm, Executable, SyscallRegistry, TestInstructionMeter}, }; use std::{fs::File, io::Read}; use test::Bencher; @@ -21,7 +21,7 @@ fn bench_init_interpreter_execution(bencher: &mut Bencher) { let mut file = File::open("tests/elfs/pass_stack_reference.so").unwrap(); let mut elf = Vec::new(); file.read_to_end(&mut elf).unwrap(); - let executable = >::from_elf( + let executable = >::from_elf( &elf, None, Config::default(), @@ -29,10 +29,9 @@ fn bench_init_interpreter_execution(bencher: &mut Bencher) { ) .unwrap(); let mut vm = - EbpfVm::::new(executable.as_ref(), &mut [], &[]) - .unwrap(); + EbpfVm::::new(executable.as_ref(), &mut [], &[]).unwrap(); bencher.iter(|| { - vm.execute_program_interpreted(&mut DefaultInstructionMeter {}) + vm.execute_program_interpreted(&mut TestInstructionMeter { remaining: 29 }) .unwrap() }); } @@ -43,7 +42,7 @@ fn bench_init_jit_execution(bencher: &mut Bencher) { let mut file = File::open("tests/elfs/pass_stack_reference.so").unwrap(); let mut elf = Vec::new(); file.read_to_end(&mut elf).unwrap(); - let mut executable = >::from_elf( + let mut executable = >::from_elf( &elf, None, Config::default(), @@ -52,10 +51,9 @@ fn bench_init_jit_execution(bencher: &mut Bencher) { .unwrap(); executable.jit_compile().unwrap(); let mut vm = - EbpfVm::::new(executable.as_ref(), &mut [], &[]) - .unwrap(); + EbpfVm::::new(executable.as_ref(), &mut [], &[]).unwrap(); bencher.iter(|| { - vm.execute_program_jit(&mut DefaultInstructionMeter {}) + vm.execute_program_jit(&mut TestInstructionMeter { remaining: 29 }) .unwrap() }); } @@ -67,23 +65,21 @@ fn bench_jit_vs_interpreter( instruction_meter: u64, mem: &mut [u8], ) { - let mut executable = - solana_rbpf::assembler::assemble::( - assembly, - None, - Config::default(), - SyscallRegistry::default(), - ) - .unwrap(); + let mut executable = solana_rbpf::assembler::assemble::( + assembly, + None, + Config::default(), + SyscallRegistry::default(), + ) + .unwrap(); executable.jit_compile().unwrap(); let mut vm = EbpfVm::new(executable.as_ref(), mem, &[]).unwrap(); let interpreter_summary = bencher .bench(|bencher| { bencher.iter(|| { - let result = - vm.execute_program_interpreted(&mut test_utils::TestInstructionMeter { - remaining: instruction_meter, - }); + let result = vm.execute_program_interpreted(&mut TestInstructionMeter { + remaining: instruction_meter, + }); assert!(result.is_ok()); assert_eq!(vm.get_total_instruction_count(), instruction_meter); }); @@ -92,7 +88,7 @@ fn bench_jit_vs_interpreter( let jit_summary = bencher .bench(|bencher| { bencher.iter(|| { - let result = vm.execute_program_jit(&mut test_utils::TestInstructionMeter { + let result = vm.execute_program_jit(&mut TestInstructionMeter { remaining: instruction_meter, }); assert!(result.is_ok()); diff --git a/cli/src/main.rs b/cli/src/main.rs index 635e08de..4b40fb92 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -7,10 +7,9 @@ use solana_rbpf::{ syscalls::Result, user_error::UserError, verifier::check, - vm::{Config, DynamicAnalysis, EbpfVm, Executable, SyscallObject, SyscallRegistry}, + vm::{Config, DynamicAnalysis, EbpfVm, Executable, SyscallObject, SyscallRegistry, TestInstructionMeter}, }; use std::{fs::File, io::Read, path::Path}; -use test_utils::TestInstructionMeter; struct MockSyscall { name: String, diff --git a/examples/disassemble.rs b/examples/disassemble.rs index 745ea145..b7b1f961 100644 --- a/examples/disassemble.rs +++ b/examples/disassemble.rs @@ -8,7 +8,7 @@ extern crate solana_rbpf; use solana_rbpf::{ static_analysis::Analysis, user_error::UserError, - vm::{Config, DefaultInstructionMeter, Executable, SyscallRegistry}, + vm::{Config, Executable, SyscallRegistry, TestInstructionMeter}, }; use std::collections::BTreeMap; @@ -30,7 +30,7 @@ fn main() { 0x00, 0x00, 0x00, 0x00, 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ]; - let executable = >::from_text_bytes( + let executable = >::from_text_bytes( &program, None, Config::default(), diff --git a/examples/to_json.rs b/examples/to_json.rs index 5fe8fbd1..03695cc7 100644 --- a/examples/to_json.rs +++ b/examples/to_json.rs @@ -15,7 +15,7 @@ use solana_rbpf::{ disassembler::disassemble_instruction, static_analysis::Analysis, user_error::UserError, - vm::{Config, DefaultInstructionMeter, Executable, SyscallRegistry}, + vm::{Config, Executable, SyscallRegistry, TestInstructionMeter}, }; use std::collections::BTreeMap; // Turn a program into a JSON string. @@ -28,7 +28,7 @@ use std::collections::BTreeMap; // * Print integers as integers, and not as strings containing their hexadecimal representation // (just replace the relevant `format!()` calls by the commented values. fn to_json(program: &[u8]) -> String { - let executable = >::from_text_bytes( + let executable = >::from_text_bytes( &program, None, Config::default(), diff --git a/examples/uptime.rs b/examples/uptime.rs index 7fb550b3..d37f4c0b 100644 --- a/examples/uptime.rs +++ b/examples/uptime.rs @@ -8,7 +8,7 @@ extern crate solana_rbpf; use solana_rbpf::{ syscalls, user_error::UserError, - vm::{Config, DefaultInstructionMeter, EbpfVm, Executable, SyscallObject, SyscallRegistry}, + vm::{Config, EbpfVm, Executable, SyscallObject, SyscallRegistry, TestInstructionMeter}, }; use std::collections::BTreeMap; @@ -43,7 +43,7 @@ fn main() { ]; // Create a VM: this one takes no data. Load prog1 in it. - let executable = >::from_text_bytes( + let executable = >::from_text_bytes( prog1, None, Config::default(), @@ -52,11 +52,10 @@ fn main() { ) .unwrap(); let mut vm = - EbpfVm::::new(executable.as_ref(), &mut [], &[]) - .unwrap(); + EbpfVm::::new(executable.as_ref(), &mut [], &[]).unwrap(); // Execute prog1. assert_eq!( - vm.execute_program_interpreted(&mut DefaultInstructionMeter {}) + vm.execute_program_interpreted(&mut TestInstructionMeter { remaining: 5 }) .unwrap(), 0x3 ); @@ -76,7 +75,7 @@ fn main() { ) .unwrap(); #[allow(unused_mut)] - let mut executable = >::from_text_bytes( + let mut executable = >::from_text_bytes( prog2, None, Config::default(), @@ -89,8 +88,7 @@ fn main() { executable.jit_compile().unwrap(); } let mut vm = - EbpfVm::::new(executable.as_ref(), &mut [], &[]) - .unwrap(); + EbpfVm::::new(executable.as_ref(), &mut [], &[]).unwrap(); vm.bind_syscall_context_object(Box::new(syscalls::BpfTimeGetNs {}), None) .unwrap(); @@ -98,14 +96,14 @@ fn main() { #[cfg(not(windows))] { time = vm - .execute_program_jit(&mut DefaultInstructionMeter {}) + .execute_program_jit(&mut TestInstructionMeter { remaining: 7 }) .unwrap(); } #[cfg(windows)] { time = vm - .execute_program_interpreted(&mut DefaultInstructionMeter {}) + .execute_program_interpreted(&mut TestInstructionMeter { remaining: 7 }) .unwrap(); } diff --git a/src/assembler.rs b/src/assembler.rs index c0e916ef..de6af9ec 100644 --- a/src/assembler.rs +++ b/src/assembler.rs @@ -178,8 +178,8 @@ fn insn(opc: u8, dst: i64, src: i64, off: i64, imm: i64) -> Result /// # Examples /// /// ``` -/// use solana_rbpf::{assembler::assemble, user_error::UserError, vm::{Config, DefaultInstructionMeter, SyscallRegistry}}; -/// let executable = assemble::( +/// use solana_rbpf::{assembler::assemble, user_error::UserError, vm::{Config, TestInstructionMeter, SyscallRegistry}}; +/// let executable = assemble::( /// "add64 r1, 0x605 /// mov64 r2, 0x32 /// mov64 r1, r0 diff --git a/src/elf.rs b/src/elf.rs index 70af14ff..2a92581b 100644 --- a/src/elf.rs +++ b/src/elf.rs @@ -747,11 +747,11 @@ mod test { fuzz::fuzz, syscalls::{BpfSyscallString, BpfSyscallU64}, user_error::UserError, - vm::{DefaultInstructionMeter, SyscallObject}, + vm::{SyscallObject, TestInstructionMeter}, }; use rand::{distributions::Uniform, Rng}; use std::{fs::File, io::Read}; - type ElfExecutable = EBpfElf; + type ElfExecutable = EBpfElf; fn syscall_registry() -> SyscallRegistry { let mut syscall_registry = SyscallRegistry::default(); @@ -816,7 +816,7 @@ mod test { .expect("validation failed"); let mut parsed_elf = Elf::parse(&elf_bytes).unwrap(); let initial_e_entry = parsed_elf.header.e_entry; - let executable: &dyn Executable = &elf; + let executable: &dyn Executable = &elf; assert_eq!( 0, executable @@ -829,7 +829,7 @@ mod test { elf_bytes.pwrite(parsed_elf.header, 0).unwrap(); let elf = ElfExecutable::load(Config::default(), &elf_bytes, syscall_registry()) .expect("validation failed"); - let executable: &dyn Executable = &elf; + let executable: &dyn Executable = &elf; assert_eq!( 1, executable @@ -866,7 +866,7 @@ mod test { elf_bytes.pwrite(parsed_elf.header, 0).unwrap(); let elf = ElfExecutable::load(Config::default(), &elf_bytes, syscall_registry()) .expect("validation failed"); - let executable: &dyn Executable = &elf; + let executable: &dyn Executable = &elf; assert_eq!( 0, executable diff --git a/src/vm.rs b/src/vm.rs index 05b018b9..50f4c731 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -289,13 +289,21 @@ pub trait InstructionMeter { fn get_remaining(&self) -> u64; } -/// Instruction meter without a limit -#[derive(Debug, PartialEq)] -pub struct DefaultInstructionMeter {} -impl InstructionMeter for DefaultInstructionMeter { - fn consume(&mut self, _amount: u64) {} +/// Simple instruction meter for testing +#[derive(Debug, PartialEq, Eq)] +pub struct TestInstructionMeter { + /// Maximal amount of instructions which still can be executed + pub remaining: u64, +} + +impl InstructionMeter for TestInstructionMeter { + fn consume(&mut self, amount: u64) { + debug_assert!(amount <= self.remaining, "Execution count exceeded"); + self.remaining = self.remaining.saturating_sub(amount); + } + fn get_remaining(&self) -> u64 { - std::i64::MAX as u64 + self.remaining } } @@ -433,7 +441,7 @@ macro_rules! translate_memory_access { /// # Examples /// /// ``` -/// use solana_rbpf::{ebpf, elf::register_bpf_function, vm::{Config, Executable, EbpfVm, DefaultInstructionMeter, SyscallRegistry}, user_error::UserError}; +/// use solana_rbpf::{ebpf, elf::register_bpf_function, vm::{Config, Executable, EbpfVm, TestInstructionMeter, SyscallRegistry}, user_error::UserError}; /// /// let prog = &[ /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit @@ -445,11 +453,11 @@ macro_rules! translate_memory_access { /// // Instantiate a VM. /// let mut bpf_functions = std::collections::BTreeMap::new(); /// register_bpf_function(&mut bpf_functions, 0, "entrypoint").unwrap(); -/// let mut executable = >::from_text_bytes(prog, None, Config::default(), SyscallRegistry::default(), bpf_functions).unwrap(); -/// let mut vm = EbpfVm::::new(executable.as_ref(), mem, &[]).unwrap(); +/// let mut executable = >::from_text_bytes(prog, None, Config::default(), SyscallRegistry::default(), bpf_functions).unwrap(); +/// let mut vm = EbpfVm::::new(executable.as_ref(), mem, &[]).unwrap(); /// /// // Provide a reference to the packet data. -/// let res = vm.execute_program_interpreted(&mut DefaultInstructionMeter {}).unwrap(); +/// let res = vm.execute_program_interpreted(&mut TestInstructionMeter { remaining: 1 }).unwrap(); /// assert_eq!(res, 0); /// ``` pub struct EbpfVm<'a, E: UserDefinedError, I: InstructionMeter> { @@ -472,7 +480,7 @@ impl<'a, E: UserDefinedError, I: InstructionMeter> EbpfVm<'a, E, I> { /// # Examples /// /// ``` - /// use solana_rbpf::{ebpf, elf::register_bpf_function, vm::{Config, Executable, EbpfVm, DefaultInstructionMeter, SyscallRegistry}, user_error::UserError}; + /// use solana_rbpf::{ebpf, elf::register_bpf_function, vm::{Config, Executable, EbpfVm, TestInstructionMeter, SyscallRegistry}, user_error::UserError}; /// /// let prog = &[ /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit @@ -481,8 +489,8 @@ impl<'a, E: UserDefinedError, I: InstructionMeter> EbpfVm<'a, E, I> { /// // Instantiate a VM. /// let mut bpf_functions = std::collections::BTreeMap::new(); /// register_bpf_function(&mut bpf_functions, 0, "entrypoint").unwrap(); - /// let mut executable = >::from_text_bytes(prog, None, Config::default(), SyscallRegistry::default(), bpf_functions).unwrap(); - /// let mut vm = EbpfVm::::new(executable.as_ref(), &mut [], &[]).unwrap(); + /// let mut executable = >::from_text_bytes(prog, None, Config::default(), SyscallRegistry::default(), bpf_functions).unwrap(); + /// let mut vm = EbpfVm::::new(executable.as_ref(), &mut [], &[]).unwrap(); /// ``` pub fn new( executable: &'a dyn Executable, @@ -564,7 +572,7 @@ impl<'a, E: UserDefinedError, I: InstructionMeter> EbpfVm<'a, E, I> { /// # Examples /// /// ``` - /// use solana_rbpf::{ebpf, elf::register_bpf_function, vm::{Config, Executable, EbpfVm, SyscallObject, SyscallRegistry, DefaultInstructionMeter}, syscalls::BpfTracePrintf, user_error::UserError}; + /// use solana_rbpf::{ebpf, elf::register_bpf_function, vm::{Config, Executable, EbpfVm, SyscallObject, SyscallRegistry, TestInstructionMeter}, syscalls::BpfTracePrintf, user_error::UserError}; /// /// // This program was compiled with clang, from a C program containing the following single /// // instruction: `return bpf_trace_printk("foo %c %c %c\n", 10, 1, 2, 3);` @@ -589,8 +597,8 @@ impl<'a, E: UserDefinedError, I: InstructionMeter> EbpfVm<'a, E, I> { /// // Instantiate an Executable and VM /// let mut bpf_functions = std::collections::BTreeMap::new(); /// register_bpf_function(&mut bpf_functions, 0, "entrypoint").unwrap(); - /// let mut executable = >::from_text_bytes(prog, None, Config::default(), syscall_registry, bpf_functions).unwrap(); - /// let mut vm = EbpfVm::::new(executable.as_ref(), &mut [], &[]).unwrap(); + /// let mut executable = >::from_text_bytes(prog, None, Config::default(), syscall_registry, bpf_functions).unwrap(); + /// let mut vm = EbpfVm::::new(executable.as_ref(), &mut [], &[]).unwrap(); /// // Bind a context object instance to the previously registered syscall /// vm.bind_syscall_context_object(Box::new(BpfTracePrintf {}), None); /// ``` @@ -641,7 +649,7 @@ impl<'a, E: UserDefinedError, I: InstructionMeter> EbpfVm<'a, E, I> { /// # Examples /// /// ``` - /// use solana_rbpf::{ebpf, elf::register_bpf_function, vm::{Config, Executable, EbpfVm, DefaultInstructionMeter, SyscallRegistry}, user_error::UserError}; + /// use solana_rbpf::{ebpf, elf::register_bpf_function, vm::{Config, Executable, EbpfVm, TestInstructionMeter, SyscallRegistry}, user_error::UserError}; /// /// let prog = &[ /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit @@ -653,11 +661,11 @@ impl<'a, E: UserDefinedError, I: InstructionMeter> EbpfVm<'a, E, I> { /// // Instantiate a VM. /// let mut bpf_functions = std::collections::BTreeMap::new(); /// register_bpf_function(&mut bpf_functions, 0, "entrypoint").unwrap(); - /// let mut executable = >::from_text_bytes(prog, None, Config::default(), SyscallRegistry::default(), bpf_functions).unwrap(); - /// let mut vm = EbpfVm::::new(executable.as_ref(), mem, &[]).unwrap(); + /// let mut executable = >::from_text_bytes(prog, None, Config::default(), SyscallRegistry::default(), bpf_functions).unwrap(); + /// let mut vm = EbpfVm::::new(executable.as_ref(), mem, &[]).unwrap(); /// /// // Provide a reference to the packet data. - /// let res = vm.execute_program_interpreted(&mut DefaultInstructionMeter {}).unwrap(); + /// let res = vm.execute_program_interpreted(&mut TestInstructionMeter { remaining: 1 }).unwrap(); /// assert_eq!(res, 0); /// ``` pub fn execute_program_interpreted(&mut self, instruction_meter: &mut I) -> ProgramResult { diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs index e76684b0..770a0f15 100644 --- a/test_utils/src/lib.rs +++ b/test_utils/src/lib.rs @@ -11,21 +11,6 @@ extern crate libc; extern crate solana_rbpf; -use solana_rbpf::vm::InstructionMeter; - -pub struct TestInstructionMeter { - pub remaining: u64, -} -impl InstructionMeter for TestInstructionMeter { - fn consume(&mut self, amount: u64) { - debug_assert!(amount <= self.remaining, "Execution count exceeded"); - self.remaining = self.remaining.saturating_sub(amount); - } - fn get_remaining(&self) -> u64 { - self.remaining - } -} - // Assembly code and data for tcp_sack testcases. pub const PROG_TCP_PORT_80: &str = " diff --git a/tests/assembler.rs b/tests/assembler.rs index 4f7e3728..d6812744 100644 --- a/tests/assembler.rs +++ b/tests/assembler.rs @@ -11,9 +11,9 @@ use solana_rbpf::{ assembler::assemble, ebpf, user_error::UserError, - vm::{Config, SyscallRegistry}, + vm::{Config, SyscallRegistry, TestInstructionMeter}, }; -use test_utils::{TestInstructionMeter, TCP_SACK_ASM, TCP_SACK_BIN}; +use test_utils::{TCP_SACK_ASM, TCP_SACK_BIN}; fn asm(src: &str) -> Result, String> { let executable = assemble::( diff --git a/tests/disassembler.rs b/tests/disassembler.rs index 7bc90143..9ba7f50f 100644 --- a/tests/disassembler.rs +++ b/tests/disassembler.rs @@ -11,9 +11,8 @@ use solana_rbpf::{ assembler::assemble, static_analysis::Analysis, user_error::UserError, - vm::{Config, SyscallRegistry}, + vm::{Config, SyscallRegistry, TestInstructionMeter}, }; -use test_utils::TestInstructionMeter; // Using a macro to keep actual line numbers in failure output macro_rules! disasm { diff --git a/tests/misc.rs b/tests/misc.rs index 376c01e0..fc4caa4a 100644 --- a/tests/misc.rs +++ b/tests/misc.rs @@ -26,7 +26,7 @@ use solana_rbpf::{ syscalls::{BpfSyscallString, BpfSyscallU64}, user_error::UserError, verifier::check, - vm::{Config, DefaultInstructionMeter, EbpfVm, Executable, SyscallObject, SyscallRegistry}, + vm::{Config, EbpfVm, Executable, SyscallObject, SyscallRegistry, TestInstructionMeter}, }; use std::{fs::File, io::Read}; @@ -118,13 +118,13 @@ fn test_fuzz_execute() { syscall_registry .register_syscall_by_name::(b"log_64", BpfSyscallU64::call) .unwrap(); - if let Ok(executable) = >::from_elf( + if let Ok(executable) = >::from_elf( bytes, Some(check), Config::default(), syscall_registry, ) { - let mut vm = EbpfVm::::new( + let mut vm = EbpfVm::::new( executable.as_ref(), &mut [], &[], @@ -134,7 +134,9 @@ fn test_fuzz_execute() { .unwrap(); vm.bind_syscall_context_object(Box::new(BpfSyscallU64 {}), None) .unwrap(); - let _ = vm.execute_program_interpreted(&mut DefaultInstructionMeter {}); + let _ = vm.execute_program_interpreted(&mut TestInstructionMeter { + remaining: 1_000_000, + }); } }, ); diff --git a/tests/ubpf_execution.rs b/tests/ubpf_execution.rs index 49f8a2b1..072805dc 100644 --- a/tests/ubpf_execution.rs +++ b/tests/ubpf_execution.rs @@ -19,12 +19,10 @@ use solana_rbpf::{ memory_region::AccessType, syscalls::{self, Result}, user_error::UserError, - vm::{Config, EbpfVm, Executable, SyscallObject, SyscallRegistry}, + vm::{Config, EbpfVm, Executable, SyscallObject, SyscallRegistry, TestInstructionMeter}, }; use std::{fs::File, io::Read}; -use test_utils::{ - TestInstructionMeter, PROG_TCP_PORT_80, TCP_SACK_ASM, TCP_SACK_MATCH, TCP_SACK_NOMATCH, -}; +use test_utils::{PROG_TCP_PORT_80, TCP_SACK_ASM, TCP_SACK_MATCH, TCP_SACK_NOMATCH}; macro_rules! test_interpreter_and_jit { (register, $syscall_registry:expr, $location:expr => $syscall_function:expr; $syscall_context_object:expr) => { diff --git a/tests/ubpf_verifier.rs b/tests/ubpf_verifier.rs index a4414aec..d0edd75c 100644 --- a/tests/ubpf_verifier.rs +++ b/tests/ubpf_verifier.rs @@ -27,7 +27,7 @@ use solana_rbpf::{ error::UserDefinedError, user_error::UserError, verifier::{check, VerifierError}, - vm::{Config, DefaultInstructionMeter, EbpfVm, Executable, SyscallRegistry}, + vm::{Config, EbpfVm, Executable, SyscallRegistry, TestInstructionMeter}, }; use std::collections::BTreeMap; use thiserror::Error; @@ -42,7 +42,7 @@ impl UserDefinedError for VerifierTestError {} #[test] fn test_verifier_success() { - let executable = assemble::( + let executable = assemble::( " mov32 r0, 0xBEE exit", @@ -51,8 +51,8 @@ fn test_verifier_success() { SyscallRegistry::default(), ) .unwrap(); - let _vm = EbpfVm::::new(executable.as_ref(), &mut [], &[]) - .unwrap(); + let _vm = + EbpfVm::::new(executable.as_ref(), &mut [], &[]).unwrap(); } #[test] @@ -61,7 +61,7 @@ fn test_verifier_fail() { fn verifier_fail(_prog: &[u8], _config: &Config) -> Result<(), VerifierError> { Err(VerifierError::NoProgram) } - let _executable = assemble::( + let _executable = assemble::( " mov32 r0, 0xBEE exit", @@ -75,7 +75,7 @@ fn test_verifier_fail() { #[test] #[should_panic(expected = "DivisionByZero(30)")] fn test_verifier_err_div_by_zero_imm() { - let _executable = assemble::( + let _executable = assemble::( " mov32 r0, 1 div32 r0, 0 @@ -95,7 +95,7 @@ fn test_verifier_err_endian_size() { 0xb7, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ]; - let _ = >::from_text_bytes( + let _ = >::from_text_bytes( prog, Some(check), Config::default(), @@ -113,7 +113,7 @@ fn test_verifier_err_incomplete_lddw() { 0x18, 0x00, 0x00, 0x00, 0x88, 0x77, 0x66, 0x55, // 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ]; - let _ = >::from_text_bytes( + let _ = >::from_text_bytes( prog, Some(check), Config::default(), @@ -126,7 +126,7 @@ fn test_verifier_err_incomplete_lddw() { #[test] #[should_panic(expected = "InvalidDestinationRegister(29)")] fn test_verifier_err_invalid_reg_dst() { - let _executable = assemble::( + let _executable = assemble::( " mov r11, 1 exit", @@ -140,7 +140,7 @@ fn test_verifier_err_invalid_reg_dst() { #[test] #[should_panic(expected = "InvalidSourceRegister(29)")] fn test_verifier_err_invalid_reg_src() { - let _executable = assemble::( + let _executable = assemble::( " mov r0, r11 exit", @@ -154,7 +154,7 @@ fn test_verifier_err_invalid_reg_src() { #[test] #[should_panic(expected = "JumpToMiddleOfLDDW(2, 29)")] fn test_verifier_err_jmp_lddw() { - let _executable = assemble::( + let _executable = assemble::( " ja +1 lddw r0, 0x1122334455667788 @@ -169,7 +169,7 @@ fn test_verifier_err_jmp_lddw() { #[test] #[should_panic(expected = "JumpOutOfCode(3, 29)")] fn test_verifier_err_jmp_out() { - let _executable = assemble::( + let _executable = assemble::( " ja +2 exit", @@ -187,7 +187,7 @@ fn test_verifier_err_unknown_opcode() { 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ]; - let _ = >::from_text_bytes( + let _ = >::from_text_bytes( prog, Some(check), Config::default(), @@ -200,7 +200,7 @@ fn test_verifier_err_unknown_opcode() { #[test] #[should_panic(expected = "CannotWriteR10(29)")] fn test_verifier_err_write_r10() { - let _executable = assemble::( + let _executable = assemble::( " mov r10, 1 exit",