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

Commit

Permalink
Renames register_function_by_name() => register_function(). (#457)
Browse files Browse the repository at this point in the history
Makes register_function() take &[u8] instead of &str.
  • Loading branch information
Lichtso authored Apr 10, 2023
1 parent 44487c3 commit febc1c7
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion benches/elf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use test::Bencher;
fn loader() -> Arc<BuiltInProgram<TestContextObject>> {
let mut loader = BuiltInProgram::new_loader(Config::default());
loader
.register_function_by_name("log_64", bpf_syscall_u64)
.register_function(b"log_64", bpf_syscall_u64)
.unwrap();
Arc::new(loader)
}
Expand Down
2 changes: 1 addition & 1 deletion src/disassembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ pub fn disassemble_instruction<C: ContextObject>(
function_name
} else {
name = "syscall";
loader.lookup_function(insn.imm as u32).map(|(function_name, _)| function_name).unwrap_or("[invalid]")
loader.lookup_function(insn.imm as u32).map(|(function_name, _)| std::str::from_utf8(function_name).unwrap()).unwrap_or("[invalid]")
};
desc = format!("{name} {function_name}");
},
Expand Down
4 changes: 2 additions & 2 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1212,10 +1212,10 @@ mod test {
fn loader() -> Arc<BuiltInProgram<TestContextObject>> {
let mut loader = BuiltInProgram::new_loader(Config::default());
loader
.register_function_by_name("log", syscalls::bpf_syscall_string)
.register_function(b"log", syscalls::bpf_syscall_string)
.unwrap();
loader
.register_function_by_name("log_64", syscalls::bpf_syscall_u64)
.register_function(b"log_64", syscalls::bpf_syscall_u64)
.unwrap();
Arc::new(loader)
}
Expand Down
2 changes: 1 addition & 1 deletion src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,7 @@ mod tests {
..Config::default()
});
loader
.register_function_by_name("gather_bytes", syscalls::bpf_gather_bytes)
.register_function(b"gather_bytes", syscalls::bpf_gather_bytes)
.unwrap();
let mut function_registry = FunctionRegistry::default();
function_registry.insert(8, (8, "function_foo".to_string()));
Expand Down
2 changes: 1 addition & 1 deletion src/static_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl<'a> Analysis<'a> {
.get_loader()
.lookup_function(insn.imm as u32)
{
if function_name == "abort" {
if function_name == b"abort" {
self.cfg_nodes
.entry(insn.ptr + 1)
.or_insert_with(CfgNode::default);
Expand Down
12 changes: 6 additions & 6 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub struct BuiltInProgram<C: ContextObject> {
/// Holds the Config if this is a loader program
config: Option<Box<Config>>,
/// Function pointers by symbol
functions: HashMap<u32, (&'static str, BuiltInFunction<C>)>,
functions: HashMap<u32, (&'static [u8], BuiltInFunction<C>)>,
}

impl<C: ContextObject> BuiltInProgram<C> {
Expand All @@ -125,12 +125,12 @@ impl<C: ContextObject> BuiltInProgram<C> {
}

/// Register a built-in function
pub fn register_function_by_name(
pub fn register_function(
&mut self,
name: &'static str,
name: &'static [u8],
function: BuiltInFunction<C>,
) -> Result<(), EbpfError> {
let key = ebpf::hash_symbol_name(name.as_bytes());
let key = ebpf::hash_symbol_name(name);
if self.functions.insert(key, (name, function)).is_some() {
Err(EbpfError::FunctionAlreadyRegistered(key as usize))
} else {
Expand All @@ -139,7 +139,7 @@ impl<C: ContextObject> BuiltInProgram<C> {
}

/// Get a symbol's function pointer
pub fn lookup_function(&self, key: u32) -> Option<(&'static str, BuiltInFunction<C>)> {
pub fn lookup_function(&self, key: u32) -> Option<(&'static [u8], BuiltInFunction<C>)> {
self.functions.get(&key).cloned()
}

Expand All @@ -152,7 +152,7 @@ impl<C: ContextObject> BuiltInProgram<C> {
0
}
+ self.functions.capacity()
* mem::size_of::<(u32, (&'static str, BuiltInFunction<C>))>()
* mem::size_of::<(u32, (&'static [u8], BuiltInFunction<C>))>()
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ fn test_fuzz_execute() {

let mut loader = BuiltInProgram::default();
loader
.register_function_by_name("log", syscalls::bpf_syscall_string)
.register_function(b"log", syscalls::bpf_syscall_string)
.unwrap();
loader
.register_function_by_name("log_64", syscalls::bpf_syscall_u64)
.register_function(b"log_64", syscalls::bpf_syscall_u64)
.unwrap();
let loader = Arc::new(loader);

Expand Down
4 changes: 2 additions & 2 deletions tests/ubpf_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const INSTRUCTION_METER_BUDGET: u64 = 1024;
macro_rules! test_interpreter_and_jit {
(register, $loader:expr, $location:expr => $syscall_function:expr) => {
$loader
.register_function_by_name($location, $syscall_function)
.register_function($location.as_bytes(), $syscall_function)
.unwrap();
};
($executable:expr, $mem:tt, $context_object:expr, $expected_result:expr $(,)?) => {
Expand Down Expand Up @@ -3015,7 +3015,7 @@ fn nested_vm_syscall(
if depth > 0 {
let mut loader = BuiltInProgram::new_loader(Config::default());
loader
.register_function_by_name("nested_vm_syscall", nested_vm_syscall)
.register_function(b"nested_vm_syscall", nested_vm_syscall)
.unwrap();
let mem = [depth as u8 - 1, throw as u8];
let mut executable = assemble::<TestContextObject>(
Expand Down

0 comments on commit febc1c7

Please sign in to comment.