Skip to content

Commit

Permalink
Rename new_sparse to default
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasSte committed Oct 17, 2024
1 parent a3b5fae commit 1c847bb
Show file tree
Hide file tree
Showing 20 changed files with 65 additions and 73 deletions.
3 changes: 1 addition & 2 deletions benches/elf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ use std::{fs::File, io::Read, sync::Arc};
use test::Bencher;

fn loader() -> Arc<BuiltinProgram<TestContextObject>> {
let mut function_registry =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default_sparse();
let mut function_registry = FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
function_registry
.register_function_hashed(*b"log", syscalls::SyscallString::vm)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion benches/vm_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn bench_jit_vs_interpreter(
assembly,
Arc::new(BuiltinProgram::new_loader(
config,
FunctionRegistry::default_sparse(),
FunctionRegistry::default(),
)),
)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
2 changes: 1 addition & 1 deletion examples/disassemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn main() {
program,
loader,
SBPFVersion::V2,
FunctionRegistry::default_sparse(),
FunctionRegistry::default(),
)
.unwrap();
let analysis = Analysis::from_executable(&executable).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/to_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/dumb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<BuiltinFunction<TestContextObject>>::default_sparse();
let function_registry = FunctionRegistry::default();
let syscall_registry = FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();

if RequisiteVerifier::verify(&prog, &config, &SBPFVersion::V2, &function_registry, &syscall_registry).is_err() {
// verify please
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/smart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<BuiltinFunction<TestContextObject>>::default_sparse();
let function_registry = FunctionRegistry::default();
let syscall_registry = FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();

if RequisiteVerifier::verify(
prog.into_bytes(),
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/smart_jit_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<BuiltinFunction<TestContextObject>>::default_sparse();
let function_registry = FunctionRegistry::default();
let syscall_registry = FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();

if RequisiteVerifier::verify(
prog.into_bytes(),
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/smarter_jit_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<BuiltinFunction<TestContextObject>>::default_sparse();
let function_registry = FunctionRegistry::default();
let syscall_registry = FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();

if RequisiteVerifier::verify(
prog.into_bytes(),
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/verify_semantic_aware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<BuiltinFunction<TestContextObject>>::default_sparse();
let function_registry = FunctionRegistry::default();
let syscall_registry = FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();

RequisiteVerifier::verify(
prog.into_bytes(),
Expand Down
2 changes: 1 addition & 1 deletion src/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ pub fn assemble<C: ContextObject>(
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() {
Expand Down
6 changes: 3 additions & 3 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ impl<C: ContextObject> Executable<C> {
}

// relocate symbols
let mut function_registry = FunctionRegistry::default_sparse();
let mut function_registry = FunctionRegistry::default();
Self::relocate(
&mut function_registry,
&loader,
Expand Down Expand Up @@ -1167,7 +1167,7 @@ mod test {

fn loader() -> Arc<BuiltinProgram<TestContextObject>> {
let mut function_registry =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default_sparse();
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
function_registry
.register_function_hashed(*b"log", syscalls::SyscallString::vm)
.unwrap();
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1744,12 +1744,12 @@ mod tests {
fn create_mockup_executable(config: Config, program: &[u8]) -> Executable<TestContextObject> {
let sbpf_version = *config.enabled_sbpf_versions.end();
let mut function_registry =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default_sparse();
FunctionRegistry::<BuiltinFunction<TestContextObject>>::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();
Expand Down
19 changes: 7 additions & 12 deletions src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,11 @@ pub enum FunctionRegistry<T> {
Dense(Vec<(Vec<u8>, T)>),
}

impl<T> FunctionRegistry<T> {
/// Create an empty sparse function registry
pub fn default_sparse() -> Self {
impl<T> Default for FunctionRegistry<T> {
/// 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<T: Copy + PartialEq + ContextObject> FunctionRegistry<BuiltinFunction<T>> {
Expand Down Expand Up @@ -325,7 +320,7 @@ impl<C: ContextObject> BuiltinProgram<C> {
pub fn new_mock() -> Self {
Self {
config: Some(Box::default()),
functions: FunctionRegistry::default_sparse(),
functions: FunctionRegistry::default(),
}
}

Expand Down Expand Up @@ -463,23 +458,23 @@ mod tests {
#[test]
fn test_builtin_program_eq() {
let mut function_registry_a =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default_sparse();
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
function_registry_a
.register_function_hashed(*b"log", syscalls::SyscallString::vm)
.unwrap();
function_registry_a
.register_function_hashed(*b"log_64", syscalls::SyscallU64::vm)
.unwrap();
let mut function_registry_b =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default_sparse();
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
function_registry_b
.register_function_hashed(*b"log_64", syscalls::SyscallU64::vm)
.unwrap();
function_registry_b
.register_function_hashed(*b"log", syscalls::SyscallString::vm)
.unwrap();
let mut function_registry_c =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default_sparse();
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
function_registry_c
.register_function_hashed(*b"log_64", syscalls::SyscallU64::vm)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<TestContextObject>::from_text_bytes(prog, loader.clone(), SBPFVersion::V2, function_registry).unwrap();
/// executable.verify::<RequisiteVerifier>().unwrap();
/// let mut context_object = TestContextObject::new(1);
Expand Down
4 changes: 2 additions & 2 deletions tests/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn asm(src: &str) -> Result<Vec<ebpf::Insn>, String> {
}

fn asm_with_config(src: &str, config: Config) -> Result<Vec<ebpf::Insn>, String> {
let loader = BuiltinProgram::new_loader(config, FunctionRegistry::default_sparse());
let loader = BuiltinProgram::new_loader(config, FunctionRegistry::default());
let executable = assemble::<TestContextObject>(src, Arc::new(loader))?;
let (_program_vm_addr, program) = executable.get_text_bytes();
Ok((0..program.len() / ebpf::INSN_SIZE)
Expand Down Expand Up @@ -526,7 +526,7 @@ fn test_tcp_sack() {
TCP_SACK_ASM,
Arc::new(BuiltinProgram::new_loader(
config,
FunctionRegistry::default_sparse(),
FunctionRegistry::default(),
)),
)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion tests/disassembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<TestContextObject>(src, Arc::new(loader)).unwrap();
let analysis = Analysis::from_executable(&executable).unwrap();
let mut reasm = Vec::new();
Expand Down
25 changes: 12 additions & 13 deletions tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<BuiltinFunction<TestContextObject>>::default_sparse();
let mut function_registry = FunctionRegistry::<BuiltinFunction<TestContextObject>>::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();
Expand All @@ -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::<BuiltinFunction<TestContextObject>>::default_sparse();
let mut function_registry = FunctionRegistry::<BuiltinFunction<TestContextObject>>::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::<TestContextObject>::from_elf(&elf, loader).unwrap();
Expand Down Expand Up @@ -810,7 +810,7 @@ fn test_pqr() {
&prog,
loader.clone(),
SBPFVersion::V2,
FunctionRegistry::default_sparse(),
FunctionRegistry::default(),
)
.unwrap();
test_interpreter_and_jit!(
Expand All @@ -825,7 +825,7 @@ fn test_pqr() {
&prog,
loader.clone(),
SBPFVersion::V2,
FunctionRegistry::default_sparse(),
FunctionRegistry::default(),
)
.unwrap();
test_interpreter_and_jit!(
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -901,7 +901,7 @@ fn test_err_divide_overflow() {
&prog,
loader.clone(),
SBPFVersion::V2,
FunctionRegistry::default_sparse(),
FunctionRegistry::default(),
)
.unwrap();
test_interpreter_and_jit!(
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -2681,7 +2681,7 @@ declare_builtin_function!(
#[allow(unused_mut)]
if depth > 0 {
let mut function_registry =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default_sparse();
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
function_registry
.register_function_hashed(*b"nested_vm_syscall", SyscallNestedVm::vm)
.unwrap();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -4094,14 +4094,13 @@ fn test_invalid_exit_or_return() {
enable_instruction_tracing: true,
..Config::default()
};
let function_registry =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default_sparse();
let function_registry = FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
let loader = Arc::new(BuiltinProgram::new_loader(config, function_registry));
let mut executable = Executable::<TestContextObject>::from_text_bytes(
prog,
loader,
sbpf_version,
FunctionRegistry::default_sparse(),
FunctionRegistry::default(),
)
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion tests/exercise_instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<BuiltinFunction<TestContextObject>>::default_sparse();
let mut function_registry = FunctionRegistry::<BuiltinFunction<TestContextObject>>::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();
Expand Down
Loading

0 comments on commit 1c847bb

Please sign in to comment.