Skip to content

Commit

Permalink
Wraps SyscallRegistry in std::sync::Arc. (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso authored Dec 1, 2022
1 parent ffbfc9f commit 7c6d03b
Show file tree
Hide file tree
Showing 19 changed files with 116 additions and 85 deletions.
15 changes: 9 additions & 6 deletions benches/elf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,25 @@ use solana_rbpf::{
syscalls::bpf_syscall_u64,
vm::{Config, SyscallRegistry, TestContextObject},
};
use std::{fs::File, io::Read};
use std::{fs::File, io::Read, sync::Arc};
use test::Bencher;

fn syscall_registry() -> SyscallRegistry<TestContextObject> {
fn syscall_registry() -> Arc<SyscallRegistry<TestContextObject>> {
let mut syscall_registry = SyscallRegistry::default();
syscall_registry
.register_syscall_by_name(b"log_64", bpf_syscall_u64)
.unwrap();
syscall_registry
Arc::new(syscall_registry)
}

#[bench]
fn bench_load_elf(bencher: &mut Bencher) {
let mut file = File::open("tests/elfs/noro.so").unwrap();
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
let syscall_registry = syscall_registry();
bencher.iter(|| {
Executable::<TestContextObject>::from_elf(&elf, Config::default(), syscall_registry())
Executable::<TestContextObject>::from_elf(&elf, Config::default(), syscall_registry.clone())
.unwrap()
});
}
Expand All @@ -42,8 +43,9 @@ fn bench_load_elf_without_syscall(bencher: &mut Bencher) {
let mut file = File::open("tests/elfs/noro.so").unwrap();
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
let syscall_registry = syscall_registry();
bencher.iter(|| {
Executable::<TestContextObject>::from_elf(&elf, Config::default(), syscall_registry())
Executable::<TestContextObject>::from_elf(&elf, Config::default(), syscall_registry.clone())
.unwrap()
});
}
Expand All @@ -53,8 +55,9 @@ fn bench_load_elf_with_syscall(bencher: &mut Bencher) {
let mut file = File::open("tests/elfs/noro.so").unwrap();
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
let syscall_registry = syscall_registry();
bencher.iter(|| {
Executable::<TestContextObject>::from_elf(&elf, Config::default(), syscall_registry())
Executable::<TestContextObject>::from_elf(&elf, Config::default(), syscall_registry.clone())
.unwrap()
});
}
6 changes: 3 additions & 3 deletions benches/jit_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use solana_rbpf::{
elf::Executable,
vm::{Config, EbpfVm, SyscallRegistry, TestContextObject, VerifiedExecutable},
};
use std::{fs::File, io::Read};
use std::{fs::File, io::Read, sync::Arc};
use test::Bencher;
use test_utils::TautologyVerifier;

Expand All @@ -25,7 +25,7 @@ fn bench_init_vm(bencher: &mut Bencher) {
let executable = Executable::<TestContextObject>::from_elf(
&elf,
Config::default(),
SyscallRegistry::default(),
Arc::new(SyscallRegistry::default()),
)
.unwrap();
let verified_executable =
Expand All @@ -52,7 +52,7 @@ fn bench_jit_compile(bencher: &mut Bencher) {
let executable = Executable::<TestContextObject>::from_elf(
&elf,
Config::default(),
SyscallRegistry::default(),
Arc::new(SyscallRegistry::default()),
)
.unwrap();
let mut verified_executable =
Expand Down
8 changes: 4 additions & 4 deletions benches/vm_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use solana_rbpf::{
memory_region::MemoryRegion,
vm::{Config, EbpfVm, SyscallRegistry, TestContextObject, VerifiedExecutable},
};
use std::{fs::File, io::Read};
use std::{fs::File, io::Read, sync::Arc};
use test::Bencher;
use test_utils::TautologyVerifier;

Expand All @@ -27,7 +27,7 @@ fn bench_init_interpreter_execution(bencher: &mut Bencher) {
let executable = Executable::<TestContextObject>::from_elf(
&elf,
Config::default(),
SyscallRegistry::default(),
Arc::new(SyscallRegistry::default()),
)
.unwrap();
let verified_executable =
Expand Down Expand Up @@ -56,7 +56,7 @@ fn bench_init_jit_execution(bencher: &mut Bencher) {
let executable = Executable::<TestContextObject>::from_elf(
&elf,
Config::default(),
SyscallRegistry::default(),
Arc::new(SyscallRegistry::default()),
)
.unwrap();
let mut verified_executable =
Expand Down Expand Up @@ -88,7 +88,7 @@ fn bench_jit_vs_interpreter(
let executable = solana_rbpf::assembler::assemble::<TestContextObject>(
assembly,
config,
SyscallRegistry::default(),
Arc::new(SyscallRegistry::default()),
)
.unwrap();
let mut verified_executable =
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use solana_rbpf::{
verifier::RequisiteVerifier,
vm::{Config, DynamicAnalysis, EbpfVm, SyscallRegistry, TestContextObject, VerifiedExecutable},
};
use std::{fs::File, io::Read, path::Path};
use std::{fs::File, io::Read, path::Path, sync::Arc};

fn main() {
let matches = App::new("Solana RBPF CLI")
Expand Down Expand Up @@ -97,7 +97,7 @@ fn main() {
enable_symbol_and_section_labels: true,
..Config::default()
};
let syscall_registry = SyscallRegistry::default();
let syscall_registry = Arc::new(SyscallRegistry::default());
let executable = match matches.value_of("assembler") {
Some(asm_file_name) => {
let mut file = File::open(Path::new(asm_file_name)).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion examples/disassemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use solana_rbpf::{
static_analysis::Analysis,
vm::{Config, FunctionRegistry, SyscallRegistry, TestContextObject},
};
use std::sync::Arc;

// Simply disassemble a program into human-readable instructions.
fn main() {
Expand All @@ -29,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 syscall_registry = SyscallRegistry::default();
let syscall_registry = Arc::new(SyscallRegistry::default());
let config = Config::default();
let executable = Executable::<TestContextObject>::from_text_bytes(
program,
Expand Down
3 changes: 2 additions & 1 deletion examples/to_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use solana_rbpf::{
static_analysis::Analysis,
vm::{Config, FunctionRegistry, SyscallRegistry, TestContextObject},
};
use std::sync::Arc;
// Turn a program into a JSON string.
//
// Relies on `json` crate.
Expand All @@ -30,7 +31,7 @@ fn to_json(program: &[u8]) -> String {
let executable = Executable::<TestContextObject>::from_text_bytes(
program,
Config::default(),
SyscallRegistry::default(),
Arc::new(SyscallRegistry::default()),
FunctionRegistry::default(),
)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/dumb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fuzz_target!(|data: DumbFuzzData| {
let executable = Executable::<TestContextObject>::from_text_bytes(
&prog,
config,
SyscallRegistry::default(),
std::sync::Arc::new(SyscallRegistry::default()),
function_registry,
)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/smart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fuzz_target!(|data: FuzzData| {
let executable = Executable::<TestContextObject>::from_text_bytes(
prog.into_bytes(),
config,
SyscallRegistry::default(),
std::sync::Arc::new(SyscallRegistry::default()),
function_registry,
)
.unwrap();
Expand Down
3 changes: 2 additions & 1 deletion fuzz/fuzz_targets/smart_jit_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use solana_rbpf::{
verifier::{RequisiteVerifier, Verifier},
vm::{EbpfVm, FunctionRegistry, SyscallRegistry, TestContextObject, VerifiedExecutable},
};
use std::sync::Arc;
use test_utils::TautologyVerifier;

use crate::common::ConfigTemplate;
Expand Down Expand Up @@ -48,7 +49,7 @@ fuzz_target!(|data: FuzzData| {
let executable = Executable::<TestContextObject>::from_text_bytes(
prog.into_bytes(),
config,
SyscallRegistry::default(),
Arc::new(SyscallRegistry::default()),
function_registry,
)
.unwrap();
Expand Down
3 changes: 2 additions & 1 deletion fuzz/fuzz_targets/smarter_jit_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use solana_rbpf::{
VerifiedExecutable,
},
};
use std::sync::Arc;
use test_utils::TautologyVerifier;

use crate::common::ConfigTemplate;
Expand Down Expand Up @@ -49,7 +50,7 @@ fuzz_target!(|data: FuzzData| {
let executable = Executable::<TestContextObject>::from_text_bytes(
prog.into_bytes(),
config,
SyscallRegistry::default(),
Arc::new(SyscallRegistry::default()),
function_registry,
)
.unwrap();
Expand Down
6 changes: 3 additions & 3 deletions src/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
elf::{register_bpf_function, Executable},
vm::{Config, ContextObject, FunctionRegistry, SyscallRegistry},
};
use std::collections::HashMap;
use std::{collections::HashMap, sync::Arc};

#[derive(Clone, Copy, Debug, PartialEq)]
enum InstructionType {
Expand Down Expand Up @@ -191,7 +191,7 @@ fn insn(opc: u8, dst: i64, src: i64, off: i64, imm: i64) -> Result<Insn, String>
/// neg64 r2
/// exit",
/// Config::default(),
/// SyscallRegistry::default(),
/// std::sync::Arc::new(SyscallRegistry::default()),
/// ).unwrap();
/// let program = executable.get_text_bytes().1;
/// println!("{:?}", program);
Expand All @@ -217,7 +217,7 @@ fn insn(opc: u8, dst: i64, src: i64, off: i64, imm: i64) -> Result<Insn, String>
pub fn assemble<C: ContextObject>(
src: &str,
config: Config,
syscall_registry: SyscallRegistry<C>,
syscall_registry: Arc<SyscallRegistry<C>>,
) -> Result<Executable<C>, String> {
fn resolve_label(
insn_ptr: usize,
Expand Down
Loading

0 comments on commit 7c6d03b

Please sign in to comment.