From 1c847bb5908b4a7b8e8d571d004b57ffdb4497c0 Mon Sep 17 00:00:00 2001 From: Lucas Steuernagel Date: Thu, 17 Oct 2024 14:08:32 -0300 Subject: [PATCH] Rename new_sparse to default --- benches/elf_loader.rs | 3 +- benches/vm_execution.rs | 2 +- cli/src/main.rs | 2 +- examples/disassemble.rs | 2 +- examples/to_json.rs | 2 +- fuzz/fuzz_targets/dumb.rs | 6 ++-- fuzz/fuzz_targets/smart.rs | 6 ++-- fuzz/fuzz_targets/smart_jit_diff.rs | 6 ++-- fuzz/fuzz_targets/smarter_jit_diff.rs | 6 ++-- fuzz/fuzz_targets/verify_semantic_aware.rs | 4 +-- src/assembler.rs | 2 +- src/elf.rs | 6 ++-- src/jit.rs | 4 +-- src/program.rs | 19 +++++-------- src/vm.rs | 2 +- tests/assembler.rs | 4 +-- tests/disassembler.rs | 2 +- tests/execution.rs | 25 ++++++++-------- tests/exercise_instructions.rs | 2 +- tests/verifier.rs | 33 +++++++++++----------- 20 files changed, 65 insertions(+), 73 deletions(-) diff --git a/benches/elf_loader.rs b/benches/elf_loader.rs index 8dc72ab49..323be403d 100644 --- a/benches/elf_loader.rs +++ b/benches/elf_loader.rs @@ -20,8 +20,7 @@ use std::{fs::File, io::Read, sync::Arc}; use test::Bencher; fn loader() -> Arc> { - let mut function_registry = - FunctionRegistry::>::default_sparse(); + let mut function_registry = FunctionRegistry::>::default(); function_registry .register_function_hashed(*b"log", syscalls::SyscallString::vm) .unwrap(); diff --git a/benches/vm_execution.rs b/benches/vm_execution.rs index e59057e33..4404c9271 100644 --- a/benches/vm_execution.rs +++ b/benches/vm_execution.rs @@ -87,7 +87,7 @@ fn bench_jit_vs_interpreter( assembly, Arc::new(BuiltinProgram::new_loader( config, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), )), ) .unwrap(); diff --git a/cli/src/main.rs b/cli/src/main.rs index daa3a9a7f..cded744a0 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -100,7 +100,7 @@ fn main() { enable_symbol_and_section_labels: true, ..Config::default() }, - FunctionRegistry::default_dense(), + FunctionRegistry::default(), )); #[allow(unused_mut)] let mut executable = match matches.value_of("assembler") { diff --git a/examples/disassemble.rs b/examples/disassemble.rs index 53bf80e88..454fec2e5 100644 --- a/examples/disassemble.rs +++ b/examples/disassemble.rs @@ -36,7 +36,7 @@ fn main() { program, loader, SBPFVersion::V2, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), ) .unwrap(); let analysis = Analysis::from_executable(&executable).unwrap(); diff --git a/examples/to_json.rs b/examples/to_json.rs index 18cf4e5ea..0e2950aa3 100644 --- a/examples/to_json.rs +++ b/examples/to_json.rs @@ -32,7 +32,7 @@ fn to_json(program: &[u8]) -> String { program, Arc::new(BuiltinProgram::new_mock()), SBPFVersion::V2, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), ) .unwrap(); let analysis = Analysis::from_executable(&executable).unwrap(); diff --git a/fuzz/fuzz_targets/dumb.rs b/fuzz/fuzz_targets/dumb.rs index fc2edf2bd..c1b1743d2 100644 --- a/fuzz/fuzz_targets/dumb.rs +++ b/fuzz/fuzz_targets/dumb.rs @@ -28,8 +28,8 @@ struct DumbFuzzData { fuzz_target!(|data: DumbFuzzData| { let prog = data.prog; let config = data.template.into(); - let function_registry = FunctionRegistry::default_sparse(); - let syscall_registry = FunctionRegistry::>::default_sparse(); + let function_registry = FunctionRegistry::default(); + let syscall_registry = FunctionRegistry::>::default(); if RequisiteVerifier::verify(&prog, &config, &SBPFVersion::V2, &function_registry, &syscall_registry).is_err() { // verify please @@ -40,7 +40,7 @@ fuzz_target!(|data: DumbFuzzData| { &prog, std::sync::Arc::new(BuiltinProgram::new_loader( config, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), )), SBPFVersion::V2, function_registry, diff --git a/fuzz/fuzz_targets/smart.rs b/fuzz/fuzz_targets/smart.rs index a32f4cba7..0ecb535bf 100644 --- a/fuzz/fuzz_targets/smart.rs +++ b/fuzz/fuzz_targets/smart.rs @@ -32,8 +32,8 @@ struct FuzzData { fuzz_target!(|data: FuzzData| { let prog = make_program(&data.prog, data.arch); let config = data.template.into(); - let function_registry = FunctionRegistry::default_sparse(); - let syscall_registry = FunctionRegistry::>::default_sparse(); + let function_registry = FunctionRegistry::default(); + let syscall_registry = FunctionRegistry::>::default(); if RequisiteVerifier::verify( prog.into_bytes(), @@ -52,7 +52,7 @@ fuzz_target!(|data: FuzzData| { prog.into_bytes(), std::sync::Arc::new(BuiltinProgram::new_loader( config, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), )), SBPFVersion::V2, function_registry, diff --git a/fuzz/fuzz_targets/smart_jit_diff.rs b/fuzz/fuzz_targets/smart_jit_diff.rs index 12f1b4cc8..87203ca28 100644 --- a/fuzz/fuzz_targets/smart_jit_diff.rs +++ b/fuzz/fuzz_targets/smart_jit_diff.rs @@ -39,8 +39,8 @@ fuzz_target!(|data: FuzzData| { .set_imm(data.exit_imm) .push(); let config = data.template.into(); - let function_registry = FunctionRegistry::default_sparse(); - let syscall_registry = FunctionRegistry::>::default_sparse(); + let function_registry = FunctionRegistry::default(); + let syscall_registry = FunctionRegistry::>::default(); if RequisiteVerifier::verify( prog.into_bytes(), @@ -60,7 +60,7 @@ fuzz_target!(|data: FuzzData| { prog.into_bytes(), std::sync::Arc::new(BuiltinProgram::new_loader( config, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), )), SBPFVersion::V2, function_registry, diff --git a/fuzz/fuzz_targets/smarter_jit_diff.rs b/fuzz/fuzz_targets/smarter_jit_diff.rs index 739d7afe1..6d77df41f 100644 --- a/fuzz/fuzz_targets/smarter_jit_diff.rs +++ b/fuzz/fuzz_targets/smarter_jit_diff.rs @@ -29,8 +29,8 @@ struct FuzzData { fuzz_target!(|data: FuzzData| { let prog = make_program(&data.prog); let config = data.template.into(); - let function_registry = FunctionRegistry::default_sparse(); - let syscall_registry = FunctionRegistry::>::default_sparse(); + let function_registry = FunctionRegistry::default(); + let syscall_registry = FunctionRegistry::>::default(); if RequisiteVerifier::verify( prog.into_bytes(), @@ -50,7 +50,7 @@ fuzz_target!(|data: FuzzData| { prog.into_bytes(), std::sync::Arc::new(BuiltinProgram::new_loader( config, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), )), SBPFVersion::V2, function_registry, diff --git a/fuzz/fuzz_targets/verify_semantic_aware.rs b/fuzz/fuzz_targets/verify_semantic_aware.rs index 918b5783f..0c2cc09f6 100644 --- a/fuzz/fuzz_targets/verify_semantic_aware.rs +++ b/fuzz/fuzz_targets/verify_semantic_aware.rs @@ -24,8 +24,8 @@ struct FuzzData { fuzz_target!(|data: FuzzData| { let prog = make_program(&data.prog); let config = data.template.into(); - let function_registry = FunctionRegistry::default_sparse(); - let syscall_registry = FunctionRegistry::>::default_sparse(); + let function_registry = FunctionRegistry::default(); + let syscall_registry = FunctionRegistry::>::default(); RequisiteVerifier::verify( prog.into_bytes(), diff --git a/src/assembler.rs b/src/assembler.rs index 28e8f6b88..ffdb35083 100644 --- a/src/assembler.rs +++ b/src/assembler.rs @@ -333,7 +333,7 @@ pub fn assemble( let statements = parse(src)?; let instruction_map = make_instruction_map(sbpf_version); let mut insn_ptr = 0; - let mut function_registry = FunctionRegistry::default_sparse(); + let mut function_registry = FunctionRegistry::default(); let mut labels = HashMap::new(); labels.insert("entrypoint", 0); for statement in statements.iter() { diff --git a/src/elf.rs b/src/elf.rs index 79da05c3d..072c8d5c6 100644 --- a/src/elf.rs +++ b/src/elf.rs @@ -422,7 +422,7 @@ impl Executable { } // relocate symbols - let mut function_registry = FunctionRegistry::default_sparse(); + let mut function_registry = FunctionRegistry::default(); Self::relocate( &mut function_registry, &loader, @@ -1167,7 +1167,7 @@ mod test { fn loader() -> Arc> { let mut function_registry = - FunctionRegistry::>::default_sparse(); + FunctionRegistry::>::default(); function_registry .register_function_hashed(*b"log", syscalls::SyscallString::vm) .unwrap(); @@ -1931,7 +1931,7 @@ mod test { reject_broken_elfs: true, ..Config::default() }, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), ); let elf_bytes = std::fs::read("tests/elfs/syscall_reloc_64_32_sbpfv1.so") .expect("failed to read elf file"); diff --git a/src/jit.rs b/src/jit.rs index 08b150783..1f48034e4 100644 --- a/src/jit.rs +++ b/src/jit.rs @@ -1744,12 +1744,12 @@ mod tests { fn create_mockup_executable(config: Config, program: &[u8]) -> Executable { let sbpf_version = *config.enabled_sbpf_versions.end(); let mut function_registry = - FunctionRegistry::>::default_sparse(); + FunctionRegistry::>::default(); function_registry .register_function_hashed(*b"gather_bytes", syscalls::SyscallGatherBytes::vm) .unwrap(); let loader = BuiltinProgram::new_loader(config, function_registry); - let mut function_registry = FunctionRegistry::default_sparse(); + let mut function_registry = FunctionRegistry::default(); function_registry .register_function(8, *b"function_foo", 8) .unwrap(); diff --git a/src/program.rs b/src/program.rs index f2230667a..8fe58d1c1 100644 --- a/src/program.rs +++ b/src/program.rs @@ -93,16 +93,11 @@ pub enum FunctionRegistry { Dense(Vec<(Vec, T)>), } -impl FunctionRegistry { - /// Create an empty sparse function registry - pub fn default_sparse() -> Self { +impl Default for FunctionRegistry { + /// Default for FunctionRegistry returns a sparse registry + fn default() -> Self { FunctionRegistry::Sparse(BTreeMap::new()) } - - /// Create and empty dense function registry - pub fn default_dense() -> Self { - FunctionRegistry::Dense(Vec::new()) - } } impl FunctionRegistry> { @@ -325,7 +320,7 @@ impl BuiltinProgram { pub fn new_mock() -> Self { Self { config: Some(Box::default()), - functions: FunctionRegistry::default_sparse(), + functions: FunctionRegistry::default(), } } @@ -463,7 +458,7 @@ mod tests { #[test] fn test_builtin_program_eq() { let mut function_registry_a = - FunctionRegistry::>::default_sparse(); + FunctionRegistry::>::default(); function_registry_a .register_function_hashed(*b"log", syscalls::SyscallString::vm) .unwrap(); @@ -471,7 +466,7 @@ mod tests { .register_function_hashed(*b"log_64", syscalls::SyscallU64::vm) .unwrap(); let mut function_registry_b = - FunctionRegistry::>::default_sparse(); + FunctionRegistry::>::default(); function_registry_b .register_function_hashed(*b"log_64", syscalls::SyscallU64::vm) .unwrap(); @@ -479,7 +474,7 @@ mod tests { .register_function_hashed(*b"log", syscalls::SyscallString::vm) .unwrap(); let mut function_registry_c = - FunctionRegistry::>::default_sparse(); + FunctionRegistry::>::default(); function_registry_c .register_function_hashed(*b"log_64", syscalls::SyscallU64::vm) .unwrap(); diff --git a/src/vm.rs b/src/vm.rs index 15df1e794..7bff55c4f 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -250,7 +250,7 @@ pub struct CallFrame { /// ]; /// /// let loader = std::sync::Arc::new(BuiltinProgram::new_mock()); -/// let function_registry = FunctionRegistry::default_sparse(); +/// let function_registry = FunctionRegistry::default(); /// let mut executable = Executable::::from_text_bytes(prog, loader.clone(), SBPFVersion::V2, function_registry).unwrap(); /// executable.verify::().unwrap(); /// let mut context_object = TestContextObject::new(1); diff --git a/tests/assembler.rs b/tests/assembler.rs index 7493c1794..66df864d9 100644 --- a/tests/assembler.rs +++ b/tests/assembler.rs @@ -19,7 +19,7 @@ fn asm(src: &str) -> Result, String> { } fn asm_with_config(src: &str, config: Config) -> Result, String> { - let loader = BuiltinProgram::new_loader(config, FunctionRegistry::default_sparse()); + let loader = BuiltinProgram::new_loader(config, FunctionRegistry::default()); let executable = assemble::(src, Arc::new(loader))?; let (_program_vm_addr, program) = executable.get_text_bytes(); Ok((0..program.len() / ebpf::INSN_SIZE) @@ -526,7 +526,7 @@ fn test_tcp_sack() { TCP_SACK_ASM, Arc::new(BuiltinProgram::new_loader( config, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), )), ) .unwrap(); diff --git a/tests/disassembler.rs b/tests/disassembler.rs index 2f1fbea68..e77d274fe 100644 --- a/tests/disassembler.rs +++ b/tests/disassembler.rs @@ -27,7 +27,7 @@ macro_rules! disasm { }}; ($src:expr, $config:expr) => {{ let src = $src; - let loader = BuiltinProgram::new_loader($config, FunctionRegistry::default_sparse()); + let loader = BuiltinProgram::new_loader($config, FunctionRegistry::default()); let executable = assemble::(src, Arc::new(loader)).unwrap(); let analysis = Analysis::from_executable(&executable).unwrap(); let mut reasm = Vec::new(); diff --git a/tests/execution.rs b/tests/execution.rs index ca9fc8d5a..cf63966b5 100644 --- a/tests/execution.rs +++ b/tests/execution.rs @@ -148,7 +148,7 @@ macro_rules! test_interpreter_and_jit_asm { { let mut config = $config; config.enable_instruction_tracing = true; - let mut function_registry = FunctionRegistry::>::default_sparse(); + let mut function_registry = FunctionRegistry::>::default(); $(test_interpreter_and_jit!(register, function_registry, $location => $syscall_function);)* let loader = Arc::new(BuiltinProgram::new_loader(config, function_registry)); let mut executable = assemble($source, loader).unwrap(); @@ -170,7 +170,7 @@ macro_rules! test_interpreter_and_jit_elf { file.read_to_end(&mut elf).unwrap(); #[allow(unused_mut)] { - let mut function_registry = FunctionRegistry::>::default_sparse(); + let mut function_registry = FunctionRegistry::>::default(); $(test_interpreter_and_jit!(register, function_registry, $location => $syscall_function);)* let loader = Arc::new(BuiltinProgram::new_loader($config, function_registry)); let mut executable = Executable::::from_elf(&elf, loader).unwrap(); @@ -810,7 +810,7 @@ fn test_pqr() { &prog, loader.clone(), SBPFVersion::V2, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), ) .unwrap(); test_interpreter_and_jit!( @@ -825,7 +825,7 @@ fn test_pqr() { &prog, loader.clone(), SBPFVersion::V2, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), ) .unwrap(); test_interpreter_and_jit!( @@ -859,7 +859,7 @@ fn test_err_divide_by_zero() { &prog, loader.clone(), SBPFVersion::V2, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), ) .unwrap(); test_interpreter_and_jit!( @@ -901,7 +901,7 @@ fn test_err_divide_overflow() { &prog, loader.clone(), SBPFVersion::V2, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), ) .unwrap(); test_interpreter_and_jit!( @@ -2303,7 +2303,7 @@ fn test_err_mem_access_out_of_bound() { &prog, loader.clone(), SBPFVersion::V2, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), ) .unwrap(); test_interpreter_and_jit!( @@ -2681,7 +2681,7 @@ declare_builtin_function!( #[allow(unused_mut)] if depth > 0 { let mut function_registry = - FunctionRegistry::>::default_sparse(); + FunctionRegistry::>::default(); function_registry .register_function_hashed(*b"nested_vm_syscall", SyscallNestedVm::vm) .unwrap(); @@ -3383,10 +3383,10 @@ fn execute_generated_program(prog: &[u8]) -> bool { enable_instruction_tracing: true, ..Config::default() }, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), )), SBPFVersion::V2, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), ); let mut executable = if let Ok(executable) = executable { executable @@ -4094,14 +4094,13 @@ fn test_invalid_exit_or_return() { enable_instruction_tracing: true, ..Config::default() }; - let function_registry = - FunctionRegistry::>::default_sparse(); + let function_registry = FunctionRegistry::>::default(); let loader = Arc::new(BuiltinProgram::new_loader(config, function_registry)); let mut executable = Executable::::from_text_bytes( prog, loader, sbpf_version, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), ) .unwrap(); diff --git a/tests/exercise_instructions.rs b/tests/exercise_instructions.rs index 7a5772d1b..659c5770b 100644 --- a/tests/exercise_instructions.rs +++ b/tests/exercise_instructions.rs @@ -122,7 +122,7 @@ macro_rules! test_interpreter_and_jit_asm { { let mut config = $config; config.enable_instruction_tracing = true; - let mut function_registry = FunctionRegistry::>::default_sparse(); + let mut function_registry = FunctionRegistry::>::default(); $(test_interpreter_and_jit!(register, function_registry, $location => $syscall_function);)* let loader = Arc::new(BuiltinProgram::new_loader(config, function_registry)); let mut executable = assemble($source, loader).unwrap(); diff --git a/tests/verifier.rs b/tests/verifier.rs index 7f0b609e9..24df55d1a 100644 --- a/tests/verifier.rs +++ b/tests/verifier.rs @@ -128,7 +128,7 @@ fn test_verifier_err_endian_size() { prog, Arc::new(BuiltinProgram::new_mock()), SBPFVersion::V2, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), ) .unwrap(); executable.verify::().unwrap(); @@ -147,7 +147,7 @@ fn test_verifier_err_incomplete_lddw() { prog, Arc::new(BuiltinProgram::new_mock()), SBPFVersion::V1, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), ) .unwrap(); executable.verify::().unwrap(); @@ -165,10 +165,10 @@ fn test_verifier_err_lddw_cannot_be_last() { enabled_sbpf_versions: SBPFVersion::V1..=highest_sbpf_version, ..Config::default() }, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), )), highest_sbpf_version, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), ) .unwrap(); executable.verify::().unwrap(); @@ -189,7 +189,7 @@ fn test_verifier_err_invalid_reg_dst() { enabled_sbpf_versions: SBPFVersion::V1..=highest_sbpf_version, ..Config::default() }, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), )), ) .unwrap(); @@ -212,7 +212,7 @@ fn test_verifier_err_invalid_reg_src() { enabled_sbpf_versions: SBPFVersion::V1..=highest_sbpf_version, ..Config::default() }, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), )), ) .unwrap(); @@ -233,7 +233,7 @@ fn test_verifier_resize_stack_ptr_success() { enable_stack_frame_gaps: false, ..Config::default() }, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), )), ) .unwrap(); @@ -282,7 +282,7 @@ fn test_verifier_err_callx_cannot_use_r10() { enabled_sbpf_versions: SBPFVersion::V1..=highest_sbpf_version, ..Config::default() }, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), )), ) .unwrap(); @@ -341,7 +341,7 @@ fn test_verifier_err_unknown_opcode() { prog, Arc::new(BuiltinProgram::new_mock()), SBPFVersion::V2, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), ) .unwrap(); executable.verify::().unwrap(); @@ -358,7 +358,7 @@ fn test_verifier_unknown_sycall() { prog, Arc::new(BuiltinProgram::new_mock()), SBPFVersion::V2, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), ) .unwrap(); executable.verify::().unwrap(); @@ -370,8 +370,7 @@ fn test_verifier_known_syscall() { 0x85, 0x00, 0x00, 0x00, 0xfe, 0xc3, 0xf5, 0x6b, // call 0x6bf5c3fe 0x9d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // return ]; - let mut function_registry = - FunctionRegistry::>::default_sparse(); + let mut function_registry = FunctionRegistry::>::default(); function_registry .register_function(0x6bf5c3fe, b"my_syscall", syscalls::SyscallString::vm) .unwrap(); @@ -382,7 +381,7 @@ fn test_verifier_known_syscall() { function_registry, )), SBPFVersion::V2, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), ) .unwrap(); executable.verify::().unwrap(); @@ -458,7 +457,7 @@ fn test_sdiv_disabled() { enabled_sbpf_versions: SBPFVersion::V1..=highest_sbpf_version, ..Config::default() }, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), )), ) .unwrap(); @@ -485,7 +484,7 @@ fn return_instr() { prog, Arc::new(BuiltinProgram::new_mock()), sbpf_version, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), ) .unwrap(); let result = executable.verify::(); @@ -507,7 +506,7 @@ fn return_in_v2() { enabled_sbpf_versions: SBPFVersion::V2..=SBPFVersion::V2, ..Config::default() }, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), )), ) .unwrap(); @@ -525,7 +524,7 @@ fn function_without_return() { enabled_sbpf_versions: SBPFVersion::V2..=SBPFVersion::V2, ..Config::default() }, - FunctionRegistry::default_sparse(), + FunctionRegistry::default(), )), ) .unwrap();