Skip to content

Commit

Permalink
Refactor: Use verifier in tests (#341)
Browse files Browse the repository at this point in the history
* Use the verifier in most tests.

* Adjusts test_nested_vm_syscall() as ldabsb instructions are deprecated.
  • Loading branch information
Lichtso authored Jun 3, 2022
1 parent 1425e19 commit 5969481
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 28 deletions.
3 changes: 2 additions & 1 deletion examples/disassemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use solana_rbpf::{
elf::Executable,
static_analysis::Analysis,
user_error::UserError,
verifier::check,
vm::{Config, SyscallRegistry, TestInstructionMeter},
};
use std::collections::BTreeMap;
Expand All @@ -33,7 +34,7 @@ fn main() {
];
let executable = Executable::<UserError, TestInstructionMeter>::from_text_bytes(
&program,
None,
Some(check),
Config::default(),
SyscallRegistry::default(),
BTreeMap::default(),
Expand Down
3 changes: 2 additions & 1 deletion examples/to_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use solana_rbpf::{
elf::Executable,
static_analysis::Analysis,
user_error::UserError,
verifier::check,
vm::{Config, SyscallRegistry, TestInstructionMeter},
};
use std::collections::BTreeMap;
Expand All @@ -31,7 +32,7 @@ use std::collections::BTreeMap;
fn to_json(program: &[u8]) -> String {
let executable = Executable::<UserError, TestInstructionMeter>::from_text_bytes(
&program,
None,
Some(check),
Config::default(),
SyscallRegistry::default(),
BTreeMap::default(),
Expand Down
4 changes: 2 additions & 2 deletions src/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,15 @@ 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, TestInstructionMeter, SyscallRegistry}};
/// use solana_rbpf::{assembler::assemble, user_error::UserError, vm::{Config, TestInstructionMeter, SyscallRegistry}, verifier::check};
/// let executable = assemble::<UserError, TestInstructionMeter>(
/// "add64 r1, 0x605
/// mov64 r2, 0x32
/// mov64 r1, r0
/// be16 r0
/// neg64 r2
/// exit",
/// None,
/// Some(check),
/// Config::default(),
/// SyscallRegistry::default(),
/// ).unwrap();
Expand Down
10 changes: 7 additions & 3 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2086,9 +2086,13 @@ mod test {
let mut elf_bytes = Vec::new();
file.read_to_end(&mut elf_bytes)
.expect("failed to read elf file");
let mut executable =
ElfExecutable::from_elf(&elf_bytes, None, Config::default(), syscall_registry())
.expect("validation failed");
let mut executable = ElfExecutable::from_elf(
&elf_bytes,
Some(crate::verifier::check),
Config::default(),
syscall_registry(),
)
.expect("validation failed");
{
Executable::jit_compile(&mut executable).unwrap();
}
Expand Down
16 changes: 8 additions & 8 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ impl Tracer {
/// # Examples
///
/// ```
/// use solana_rbpf::{ebpf, elf::{Executable, register_bpf_function}, memory_region::MemoryRegion, vm::{Config, EbpfVm, TestInstructionMeter, SyscallRegistry}, user_error::UserError};
/// use solana_rbpf::{ebpf, elf::{Executable, register_bpf_function}, memory_region::MemoryRegion, vm::{Config, EbpfVm, TestInstructionMeter, SyscallRegistry}, verifier::check, user_error::UserError};
///
/// let prog = &[
/// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
Expand All @@ -451,7 +451,7 @@ impl Tracer {
/// let mut bpf_functions = std::collections::BTreeMap::new();
/// let syscall_registry = SyscallRegistry::default();
/// register_bpf_function(&config, &mut bpf_functions, &syscall_registry, 0, "entrypoint").unwrap();
/// let mut executable = Executable::<UserError, TestInstructionMeter>::from_text_bytes(prog, None, config, syscall_registry, bpf_functions).unwrap();
/// let mut executable = Executable::<UserError, TestInstructionMeter>::from_text_bytes(prog, Some(check), config, syscall_registry, bpf_functions).unwrap();
/// let mem_region = MemoryRegion::new_writable(mem, ebpf::MM_INPUT_START);
/// let mut vm = EbpfVm::<UserError, TestInstructionMeter>::new(&executable, &mut [], vec![mem_region]).unwrap();
///
Expand All @@ -478,7 +478,7 @@ impl<'a, E: UserDefinedError, I: InstructionMeter> EbpfVm<'a, E, I> {
/// # Examples
///
/// ```
/// use solana_rbpf::{ebpf, elf::{Executable, register_bpf_function}, vm::{Config, EbpfVm, TestInstructionMeter, SyscallRegistry}, user_error::UserError};
/// use solana_rbpf::{ebpf, elf::{Executable, register_bpf_function}, vm::{Config, EbpfVm, TestInstructionMeter, SyscallRegistry}, verifier::check, user_error::UserError};
///
/// let prog = &[
/// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
Expand All @@ -489,7 +489,7 @@ impl<'a, E: UserDefinedError, I: InstructionMeter> EbpfVm<'a, E, I> {
/// let mut bpf_functions = std::collections::BTreeMap::new();
/// let syscall_registry = SyscallRegistry::default();
/// register_bpf_function(&config, &mut bpf_functions, &syscall_registry, 0, "entrypoint").unwrap();
/// let mut executable = Executable::<UserError, TestInstructionMeter>::from_text_bytes(prog, None, config, syscall_registry, bpf_functions).unwrap();
/// let mut executable = Executable::<UserError, TestInstructionMeter>::from_text_bytes(prog, Some(check), config, syscall_registry, bpf_functions).unwrap();
/// let mut vm = EbpfVm::<UserError, TestInstructionMeter>::new(&executable, &mut [], Vec::new()).unwrap();
/// ```
pub fn new(
Expand Down Expand Up @@ -554,7 +554,7 @@ impl<'a, E: UserDefinedError, I: InstructionMeter> EbpfVm<'a, E, I> {
/// # Examples
///
/// ```
/// use solana_rbpf::{ebpf, elf::{Executable, register_bpf_function}, vm::{Config, EbpfVm, SyscallObject, SyscallRegistry, TestInstructionMeter}, syscalls::BpfTracePrintf, user_error::UserError};
/// use solana_rbpf::{ebpf, elf::{Executable, register_bpf_function}, vm::{Config, EbpfVm, SyscallObject, SyscallRegistry, TestInstructionMeter}, verifier::check, 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);`
Expand All @@ -580,7 +580,7 @@ impl<'a, E: UserDefinedError, I: InstructionMeter> EbpfVm<'a, E, I> {
/// let config = Config::default();
/// let mut bpf_functions = std::collections::BTreeMap::new();
/// register_bpf_function(&config, &mut bpf_functions, &syscall_registry, 0, "entrypoint").unwrap();
/// let mut executable = Executable::<UserError, TestInstructionMeter>::from_text_bytes(prog, None, config, syscall_registry, bpf_functions).unwrap();
/// let mut executable = Executable::<UserError, TestInstructionMeter>::from_text_bytes(prog, Some(check), config, syscall_registry, bpf_functions).unwrap();
/// let mut vm = EbpfVm::<UserError, TestInstructionMeter>::new(&executable, &mut [], Vec::new()).unwrap();
/// // Bind a context object instance to the previously registered syscall
/// vm.bind_syscall_context_objects(0);
Expand Down Expand Up @@ -632,7 +632,7 @@ impl<'a, E: UserDefinedError, I: InstructionMeter> EbpfVm<'a, E, I> {
/// # Examples
///
/// ```
/// use solana_rbpf::{ebpf, elf::{Executable, register_bpf_function}, memory_region::MemoryRegion, vm::{Config, EbpfVm, TestInstructionMeter, SyscallRegistry}, user_error::UserError};
/// use solana_rbpf::{ebpf, elf::{Executable, register_bpf_function}, memory_region::MemoryRegion, vm::{Config, EbpfVm, TestInstructionMeter, SyscallRegistry}, verifier::check, user_error::UserError};
///
/// let prog = &[
/// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
Expand All @@ -646,7 +646,7 @@ impl<'a, E: UserDefinedError, I: InstructionMeter> EbpfVm<'a, E, I> {
/// let mut bpf_functions = std::collections::BTreeMap::new();
/// let syscall_registry = SyscallRegistry::default();
/// register_bpf_function(&config, &mut bpf_functions, &syscall_registry, 0, "entrypoint").unwrap();
/// let mut executable = Executable::<UserError, TestInstructionMeter>::from_text_bytes(prog, None, config, syscall_registry, bpf_functions).unwrap();
/// let mut executable = Executable::<UserError, TestInstructionMeter>::from_text_bytes(prog, Some(check), config, syscall_registry, bpf_functions).unwrap();
/// let mem_region = MemoryRegion::new_writable(mem, ebpf::MM_INPUT_START);
/// let mut vm = EbpfVm::<UserError, TestInstructionMeter>::new(&executable, &mut [], vec![mem_region]).unwrap();
///
Expand Down
3 changes: 2 additions & 1 deletion tests/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use solana_rbpf::{
assembler::assemble,
ebpf,
user_error::UserError,
verifier::check,
vm::{Config, SyscallRegistry, TestInstructionMeter},
};
use test_utils::{TCP_SACK_ASM, TCP_SACK_BIN};
Expand Down Expand Up @@ -577,7 +578,7 @@ fn test_large_immediate() {
fn test_tcp_sack() {
let executable = assemble::<UserError, TestInstructionMeter>(
TCP_SACK_ASM,
None,
Some(check),
Config::default(),
SyscallRegistry::default(),
)
Expand Down
23 changes: 11 additions & 12 deletions tests/ubpf_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use solana_rbpf::{
memory_region::{AccessType, MemoryMapping, MemoryRegion},
syscalls::{self, BpfSyscallContext, Result},
user_error::UserError,
verifier::check,
vm::{Config, EbpfVm, SyscallObject, SyscallRegistry, TestInstructionMeter},
};
use std::{collections::BTreeMap, fs::File, io::Read};
Expand Down Expand Up @@ -99,7 +100,7 @@ macro_rules! test_interpreter_and_jit_asm {
{
let mut syscall_registry = SyscallRegistry::default();
$(test_interpreter_and_jit!(register, syscall_registry, $location => $syscall_init; $syscall_function);)*
let mut executable = assemble($source, Some(solana_rbpf::verifier::check), $config, syscall_registry).unwrap();
let mut executable = assemble($source, Some(check), $config, syscall_registry).unwrap();
test_interpreter_and_jit!(executable, $mem, $syscall_context, $check, $expected_instruction_count);
}
};
Expand All @@ -125,7 +126,7 @@ macro_rules! test_interpreter_and_jit_elf {
{
let mut syscall_registry = SyscallRegistry::default();
$(test_interpreter_and_jit!(register, syscall_registry, $location => $syscall_init; $syscall_function);)*
let mut executable = Executable::<UserError, TestInstructionMeter>::from_elf(&elf, Some(solana_rbpf::verifier::check), $config, syscall_registry).unwrap();
let mut executable = Executable::<UserError, TestInstructionMeter>::from_elf(&elf, Some(check), $config, syscall_registry).unwrap();
test_interpreter_and_jit!(executable, $mem, $syscall_context, $check, $expected_instruction_count);
}
};
Expand Down Expand Up @@ -2890,7 +2891,7 @@ fn test_err_mem_access_out_of_bound() {
#[allow(unused_mut)]
let mut executable = Executable::<UserError, TestInstructionMeter>::from_text_bytes(
&prog,
None,
Some(check),
config,
syscall_registry,
bpf_functions,
Expand Down Expand Up @@ -3411,13 +3412,11 @@ impl SyscallObject<UserError> for NestedVmSyscall {
let mem = [depth as u8 - 1, throw as u8];
let mut executable = assemble::<UserError, TestInstructionMeter>(
"
ldabsb 0
mov64 r1, r0
ldabsb 1
mov64 r2, r0
ldxb r2, [r1+1]
ldxb r1, [r1]
syscall NestedVmSyscall
exit",
None,
Some(check),
Config::default(),
syscall_registry,
)
Expand All @@ -3432,7 +3431,7 @@ impl SyscallObject<UserError> for NestedVmSyscall {
true
}
},
if throw == 0 { 6 } else { 5 }
if throw == 0 { 4 } else { 3 }
);
} else {
*result = if throw == 0 {
Expand Down Expand Up @@ -3555,7 +3554,7 @@ fn test_custom_entrypoint() {
#[allow(unused_mut)]
let mut executable = Executable::<UserError, TestInstructionMeter>::from_elf(
&elf,
None,
Some(check),
config,
syscall_registry,
)
Expand Down Expand Up @@ -3982,7 +3981,7 @@ fn test_err_unresolved_elf() {
..Config::default()
};
assert!(
matches!(Executable::<UserError, TestInstructionMeter>::from_elf(&elf, None, config, syscall_registry), Err(EbpfError::ElfError(ElfError::UnresolvedSymbol(symbol, pc, offset))) if symbol == "log_64" && pc == 550 && offset == 4168)
matches!(Executable::<UserError, TestInstructionMeter>::from_elf(&elf, Some(check), config, syscall_registry), Err(EbpfError::ElfError(ElfError::UnresolvedSymbol(symbol, pc, offset))) if symbol == "log_64" && pc == 550 && offset == 4168)
);
}

Expand Down Expand Up @@ -4383,7 +4382,7 @@ fn execute_generated_program(prog: &[u8]) -> bool {
.unwrap();
let executable = Executable::<UserError, TestInstructionMeter>::from_text_bytes(
prog,
Some(solana_rbpf::verifier::check),
Some(check),
config,
syscall_registry,
bpf_functions,
Expand Down

0 comments on commit 5969481

Please sign in to comment.