Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Refactoring: Default & Test Instruction Meter #202

Merged
merged 1 commit into from
Jul 21, 2021
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
8 changes: 4 additions & 4 deletions benches/elf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(|| {
<dyn Executable<UserError, DefaultInstructionMeter>>::from_elf(
<dyn Executable<UserError, TestInstructionMeter>>::from_elf(
&elf,
None,
Config::default(),
Expand All @@ -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 = <dyn Executable<UserError, DefaultInstructionMeter>>::from_elf(
let executable = <dyn Executable<UserError, TestInstructionMeter>>::from_elf(
&elf,
None,
Config::default(),
Expand All @@ -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 = <dyn Executable<UserError, DefaultInstructionMeter>>::from_elf(
let executable = <dyn Executable<UserError, TestInstructionMeter>>::from_elf(
&elf,
None,
Config::default(),
Expand Down
9 changes: 4 additions & 5 deletions benches/jit_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,16 +21,15 @@ 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 = <dyn Executable<UserError, DefaultInstructionMeter>>::from_elf(
let executable = <dyn Executable<UserError, TestInstructionMeter>>::from_elf(
&elf,
None,
Config::default(),
SyscallRegistry::default(),
)
.unwrap();
bencher.iter(|| {
EbpfVm::<UserError, DefaultInstructionMeter>::new(executable.as_ref(), &mut [], &[])
.unwrap()
EbpfVm::<UserError, TestInstructionMeter>::new(executable.as_ref(), &mut [], &[]).unwrap()
});
}

Expand All @@ -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 = <dyn Executable<UserError, DefaultInstructionMeter>>::from_elf(
let mut executable = <dyn Executable<UserError, TestInstructionMeter>>::from_elf(
&elf,
None,
Config::default(),
Expand Down
40 changes: 18 additions & 22 deletions benches/vm_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,18 +21,17 @@ 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 = <dyn Executable<UserError, DefaultInstructionMeter>>::from_elf(
let executable = <dyn Executable<UserError, TestInstructionMeter>>::from_elf(
&elf,
None,
Config::default(),
SyscallRegistry::default(),
)
.unwrap();
let mut vm =
EbpfVm::<UserError, DefaultInstructionMeter>::new(executable.as_ref(), &mut [], &[])
.unwrap();
EbpfVm::<UserError, TestInstructionMeter>::new(executable.as_ref(), &mut [], &[]).unwrap();
bencher.iter(|| {
vm.execute_program_interpreted(&mut DefaultInstructionMeter {})
vm.execute_program_interpreted(&mut TestInstructionMeter { remaining: 29 })
.unwrap()
});
}
Expand All @@ -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 = <dyn Executable<UserError, DefaultInstructionMeter>>::from_elf(
let mut executable = <dyn Executable<UserError, TestInstructionMeter>>::from_elf(
&elf,
None,
Config::default(),
Expand All @@ -52,10 +51,9 @@ fn bench_init_jit_execution(bencher: &mut Bencher) {
.unwrap();
executable.jit_compile().unwrap();
let mut vm =
EbpfVm::<UserError, DefaultInstructionMeter>::new(executable.as_ref(), &mut [], &[])
.unwrap();
EbpfVm::<UserError, TestInstructionMeter>::new(executable.as_ref(), &mut [], &[]).unwrap();
bencher.iter(|| {
vm.execute_program_jit(&mut DefaultInstructionMeter {})
vm.execute_program_jit(&mut TestInstructionMeter { remaining: 29 })
.unwrap()
});
}
Expand All @@ -67,23 +65,21 @@ fn bench_jit_vs_interpreter(
instruction_meter: u64,
mem: &mut [u8],
) {
let mut executable =
solana_rbpf::assembler::assemble::<UserError, test_utils::TestInstructionMeter>(
assembly,
None,
Config::default(),
SyscallRegistry::default(),
)
.unwrap();
let mut executable = solana_rbpf::assembler::assemble::<UserError, TestInstructionMeter>(
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);
});
Expand All @@ -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());
Expand Down
3 changes: 1 addition & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions examples/disassemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 = <dyn Executable<UserError, DefaultInstructionMeter>>::from_text_bytes(
let executable = <dyn Executable<UserError, TestInstructionMeter>>::from_text_bytes(
&program,
None,
Config::default(),
Expand Down
4 changes: 2 additions & 2 deletions examples/to_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 = <dyn Executable<UserError, DefaultInstructionMeter>>::from_text_bytes(
let executable = <dyn Executable<UserError, TestInstructionMeter>>::from_text_bytes(
&program,
None,
Config::default(),
Expand Down
18 changes: 8 additions & 10 deletions examples/uptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -43,7 +43,7 @@ fn main() {
];

// Create a VM: this one takes no data. Load prog1 in it.
let executable = <dyn Executable<UserError, DefaultInstructionMeter>>::from_text_bytes(
let executable = <dyn Executable<UserError, TestInstructionMeter>>::from_text_bytes(
prog1,
None,
Config::default(),
Expand All @@ -52,11 +52,10 @@ fn main() {
)
.unwrap();
let mut vm =
EbpfVm::<UserError, DefaultInstructionMeter>::new(executable.as_ref(), &mut [], &[])
.unwrap();
EbpfVm::<UserError, TestInstructionMeter>::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
);
Expand All @@ -76,7 +75,7 @@ fn main() {
)
.unwrap();
#[allow(unused_mut)]
let mut executable = <dyn Executable<UserError, DefaultInstructionMeter>>::from_text_bytes(
let mut executable = <dyn Executable<UserError, TestInstructionMeter>>::from_text_bytes(
prog2,
None,
Config::default(),
Expand All @@ -89,23 +88,22 @@ fn main() {
executable.jit_compile().unwrap();
}
let mut vm =
EbpfVm::<UserError, DefaultInstructionMeter>::new(executable.as_ref(), &mut [], &[])
.unwrap();
EbpfVm::<UserError, TestInstructionMeter>::new(executable.as_ref(), &mut [], &[]).unwrap();
vm.bind_syscall_context_object(Box::new(syscalls::BpfTimeGetNs {}), None)
.unwrap();

let time;
#[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();
}

Expand Down
4 changes: 2 additions & 2 deletions src/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ fn insn(opc: u8, dst: i64, src: i64, off: i64, imm: i64) -> Result<Insn, String>
/// # Examples
///
/// ```
/// use solana_rbpf::{assembler::assemble, user_error::UserError, vm::{Config, DefaultInstructionMeter, SyscallRegistry}};
/// let executable = assemble::<UserError, DefaultInstructionMeter>(
/// use solana_rbpf::{assembler::assemble, user_error::UserError, vm::{Config, TestInstructionMeter, SyscallRegistry}};
/// let executable = assemble::<UserError, TestInstructionMeter>(
/// "add64 r1, 0x605
/// mov64 r2, 0x32
/// mov64 r1, r0
Expand Down
10 changes: 5 additions & 5 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserError, DefaultInstructionMeter>;
type ElfExecutable = EBpfElf<UserError, TestInstructionMeter>;

fn syscall_registry() -> SyscallRegistry {
let mut syscall_registry = SyscallRegistry::default();
Expand Down Expand Up @@ -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<UserError, DefaultInstructionMeter> = &elf;
let executable: &dyn Executable<UserError, TestInstructionMeter> = &elf;
assert_eq!(
0,
executable
Expand All @@ -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<UserError, DefaultInstructionMeter> = &elf;
let executable: &dyn Executable<UserError, TestInstructionMeter> = &elf;
assert_eq!(
1,
executable
Expand Down Expand Up @@ -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<UserError, DefaultInstructionMeter> = &elf;
let executable: &dyn Executable<UserError, TestInstructionMeter> = &elf;
assert_eq!(
0,
executable
Expand Down
Loading