Skip to content

Commit

Permalink
Refactor - BuiltinProgram (#467)
Browse files Browse the repository at this point in the history
* Renames BuiltIn => Builtin.

* Adds equivalence of config to BuiltinProgram::eq().

* Exposes the Arc in Executable::get_loader().

* integer_arithmetic => arithmetic_side_effects.

* Stop using saturating_div().
  • Loading branch information
Lichtso authored May 24, 2023
1 parent 8a1615c commit 14cd622
Show file tree
Hide file tree
Showing 31 changed files with 114 additions and 110 deletions.
6 changes: 3 additions & 3 deletions benches/elf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ use solana_rbpf::{
elf::Executable,
syscalls::bpf_syscall_u64,
verifier::TautologyVerifier,
vm::{BuiltInProgram, Config, TestContextObject},
vm::{BuiltinProgram, Config, TestContextObject},
};
use std::{fs::File, io::Read, sync::Arc};
use test::Bencher;

fn loader() -> Arc<BuiltInProgram<TestContextObject>> {
let mut loader = BuiltInProgram::new_loader(Config::default());
fn loader() -> Arc<BuiltinProgram<TestContextObject>> {
let mut loader = BuiltinProgram::new_loader(Config::default());
loader
.register_function(b"log_64", bpf_syscall_u64)
.unwrap();
Expand Down
6 changes: 3 additions & 3 deletions benches/jit_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extern crate test;
use solana_rbpf::{
elf::Executable,
verifier::{RequisiteVerifier, TautologyVerifier},
vm::{BuiltInProgram, Config, TestContextObject},
vm::{BuiltinProgram, Config, TestContextObject},
};
use std::{fs::File, io::Read, sync::Arc};
use test::Bencher;
Expand All @@ -25,7 +25,7 @@ fn bench_init_vm(bencher: &mut Bencher) {
file.read_to_end(&mut elf).unwrap();
let executable = Executable::<TautologyVerifier, TestContextObject>::from_elf(
&elf,
Arc::new(BuiltInProgram::new_loader(Config::default())),
Arc::new(BuiltinProgram::new_loader(Config::default())),
)
.unwrap();
let verified_executable =
Expand All @@ -52,7 +52,7 @@ fn bench_jit_compile(bencher: &mut Bencher) {
file.read_to_end(&mut elf).unwrap();
let executable = Executable::<TautologyVerifier, TestContextObject>::from_elf(
&elf,
Arc::new(BuiltInProgram::new_loader(Config::default())),
Arc::new(BuiltinProgram::new_loader(Config::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 @@ -14,7 +14,7 @@ use solana_rbpf::{
elf::Executable,
memory_region::MemoryRegion,
verifier::{RequisiteVerifier, TautologyVerifier},
vm::{BuiltInProgram, Config, TestContextObject},
vm::{BuiltinProgram, Config, TestContextObject},
};
use std::{fs::File, io::Read, sync::Arc};
use test::Bencher;
Expand All @@ -27,7 +27,7 @@ fn bench_init_interpreter_execution(bencher: &mut Bencher) {
file.read_to_end(&mut elf).unwrap();
let executable = Executable::<TautologyVerifier, TestContextObject>::from_elf(
&elf,
Arc::new(BuiltInProgram::new_loader(Config::default())),
Arc::new(BuiltinProgram::new_loader(Config::default())),
)
.unwrap();
let verified_executable =
Expand Down Expand Up @@ -56,7 +56,7 @@ fn bench_init_jit_execution(bencher: &mut Bencher) {
file.read_to_end(&mut elf).unwrap();
let executable = Executable::<TautologyVerifier, TestContextObject>::from_elf(
&elf,
Arc::new(BuiltInProgram::new_loader(Config::default())),
Arc::new(BuiltinProgram::new_loader(Config::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,
Arc::new(BuiltInProgram::new_loader(config)),
Arc::new(BuiltinProgram::new_loader(config)),
)
.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 @@ -7,7 +7,7 @@ use solana_rbpf::{
memory_region::{MemoryMapping, MemoryRegion},
static_analysis::Analysis,
verifier::{RequisiteVerifier, TautologyVerifier},
vm::{BuiltInProgram, Config, DynamicAnalysis, EbpfVm, TestContextObject},
vm::{BuiltinProgram, Config, DynamicAnalysis, EbpfVm, TestContextObject},
};
use std::{fs::File, io::Read, path::Path, sync::Arc};

Expand Down Expand Up @@ -92,7 +92,7 @@ fn main() {
)
.get_matches();

let loader = Arc::new(BuiltInProgram::new_loader(Config {
let loader = Arc::new(BuiltinProgram::new_loader(Config {
enable_instruction_tracing: matches.is_present("trace") || matches.is_present("profile"),
enable_symbol_and_section_labels: true,
..Config::default()
Expand Down
4 changes: 2 additions & 2 deletions examples/disassemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use solana_rbpf::{
elf::Executable,
static_analysis::Analysis,
verifier::TautologyVerifier,
vm::{BuiltInProgram, FunctionRegistry, TestContextObject},
vm::{BuiltinProgram, FunctionRegistry, TestContextObject},
};
use std::sync::Arc;

Expand All @@ -31,7 +31,7 @@ fn main() {
0x00, 0x00, 0x00, 0x00, 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00,
];
let loader = Arc::new(BuiltInProgram::default());
let loader = Arc::new(BuiltinProgram::default());
let executable = Executable::<TautologyVerifier, TestContextObject>::from_text_bytes(
program,
loader,
Expand Down
4 changes: 2 additions & 2 deletions examples/to_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use solana_rbpf::{
elf::Executable,
static_analysis::Analysis,
verifier::TautologyVerifier,
vm::{BuiltInProgram, FunctionRegistry, TestContextObject},
vm::{BuiltinProgram, FunctionRegistry, TestContextObject},
};
use std::sync::Arc;
// Turn a program into a JSON string.
Expand All @@ -30,7 +30,7 @@ use std::sync::Arc;
fn to_json(program: &[u8]) -> String {
let executable = Executable::<TautologyVerifier, TestContextObject>::from_text_bytes(
program,
Arc::new(BuiltInProgram::default()),
Arc::new(BuiltinProgram::default()),
FunctionRegistry::default(),
)
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/dumb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use solana_rbpf::{
elf::Executable,
memory_region::MemoryRegion,
verifier::{RequisiteVerifier, TautologyVerifier, Verifier},
vm::{BuiltInProgram, FunctionRegistry, TestContextObject},
vm::{BuiltinProgram, FunctionRegistry, TestContextObject},
};
use test_utils::create_vm;

Expand All @@ -35,7 +35,7 @@ fuzz_target!(|data: DumbFuzzData| {
let mut mem = data.mem;
let executable = Executable::<TautologyVerifier, TestContextObject>::from_text_bytes(
&prog,
std::sync::Arc::new(BuiltInProgram::new_loader(config)),
std::sync::Arc::new(BuiltinProgram::new_loader(config)),
function_registry,
)
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/smart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use solana_rbpf::{
insn_builder::{Arch, IntoBytes},
memory_region::MemoryRegion,
verifier::{RequisiteVerifier, TautologyVerifier, Verifier},
vm::{BuiltInProgram, FunctionRegistry, TestContextObject},
vm::{BuiltinProgram, FunctionRegistry, TestContextObject},
};
use test_utils::create_vm;

Expand Down Expand Up @@ -39,7 +39,7 @@ fuzz_target!(|data: FuzzData| {
let mut mem = data.mem;
let executable = Executable::<TautologyVerifier, TestContextObject>::from_text_bytes(
prog.into_bytes(),
std::sync::Arc::new(BuiltInProgram::new_loader(config)),
std::sync::Arc::new(BuiltinProgram::new_loader(config)),
function_registry,
)
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/smart_jit_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use solana_rbpf::{
insn_builder::{Arch, Instruction, IntoBytes},
memory_region::MemoryRegion,
verifier::{RequisiteVerifier, TautologyVerifier, Verifier},
vm::{BuiltInProgram, FunctionRegistry, TestContextObject},
vm::{BuiltinProgram, FunctionRegistry, TestContextObject},
};
use test_utils::create_vm;

Expand Down Expand Up @@ -47,7 +47,7 @@ fuzz_target!(|data: FuzzData| {
let mut jit_mem = data.mem;
let mut executable = Executable::<TautologyVerifier, TestContextObject>::from_text_bytes(
prog.into_bytes(),
std::sync::Arc::new(BuiltInProgram::new_loader(config)),
std::sync::Arc::new(BuiltinProgram::new_loader(config)),
function_registry,
)
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/smarter_jit_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use solana_rbpf::{
static_analysis::Analysis,
verifier::{RequisiteVerifier, TautologyVerifier, Verifier},
vm::{
BuiltInProgram, ContextObject, FunctionRegistry, TestContextObject,
BuiltinProgram, ContextObject, FunctionRegistry, TestContextObject,
},
};
use test_utils::create_vm;
Expand Down Expand Up @@ -46,7 +46,7 @@ fuzz_target!(|data: FuzzData| {
let mut jit_mem = data.mem;
let mut executable = Executable::<TautologyVerifier, TestContextObject>::from_text_bytes(
prog.into_bytes(),
std::sync::Arc::new(BuiltInProgram::new_loader(config)),
std::sync::Arc::new(BuiltinProgram::new_loader(config)),
function_registry,
)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/aligned_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ pub fn is_memory_aligned(ptr: usize, align: usize) -> bool {

#[cfg(test)]
mod tests {
#![allow(clippy::integer_arithmetic)]
#![allow(clippy::arithmetic_side_effects)]
use {super::*, std::io::Write};

fn do_test<const ALIGN: usize>() {
Expand Down
2 changes: 1 addition & 1 deletion src/asm_parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(clippy::integer_arithmetic)]
#![allow(clippy::arithmetic_side_effects)]
// Copyright 2017 Rich Lane <[email protected]>
//
// Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
Expand Down
10 changes: 5 additions & 5 deletions src/assembler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(clippy::integer_arithmetic)]
#![allow(clippy::arithmetic_side_effects)]
// Copyright 2017 Rich Lane <[email protected]>
//
// Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
Expand All @@ -20,7 +20,7 @@ use crate::{
ebpf::{self, Insn},
elf::{register_internal_function, Executable},
verifier::TautologyVerifier,
vm::{BuiltInProgram, ContextObject, FunctionRegistry},
vm::{BuiltinProgram, ContextObject, FunctionRegistry},
};
use std::{collections::HashMap, sync::Arc};

Expand Down Expand Up @@ -183,15 +183,15 @@ fn insn(opc: u8, dst: i64, src: i64, off: i64, imm: i64) -> Result<Insn, String>
/// # Examples
///
/// ```
/// use solana_rbpf::{assembler::assemble, vm::{Config, TestContextObject, BuiltInProgram}};
/// use solana_rbpf::{assembler::assemble, vm::{Config, TestContextObject, BuiltinProgram}};
/// let executable = assemble::<TestContextObject>(
/// "add64 r1, 0x605
/// mov64 r2, 0x32
/// mov64 r1, r0
/// be16 r0
/// neg64 r2
/// exit",
/// std::sync::Arc::new(BuiltInProgram::new_loader(Config::default())),
/// std::sync::Arc::new(BuiltinProgram::new_loader(Config::default())),
/// ).unwrap();
/// let program = executable.get_text_bytes().1;
/// println!("{:?}", program);
Expand All @@ -216,7 +216,7 @@ fn insn(opc: u8, dst: i64, src: i64, off: i64, imm: i64) -> Result<Insn, String>
/// ```
pub fn assemble<C: ContextObject>(
src: &str,
loader: Arc<BuiltInProgram<C>>,
loader: Arc<BuiltinProgram<C>>,
) -> Result<Executable<TautologyVerifier, C>, String> {
fn resolve_label(
insn_ptr: usize,
Expand Down
6 changes: 3 additions & 3 deletions src/disassembler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(clippy::integer_arithmetic)]
#![allow(clippy::arithmetic_side_effects)]
// Copyright 2017 6WIND S.A. <[email protected]>
//
// Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
Expand All @@ -10,7 +10,7 @@
use crate::ebpf;
use crate::static_analysis::CfgNode;
use crate::vm::{BuiltInProgram, ContextObject, FunctionRegistry};
use crate::vm::{BuiltinProgram, ContextObject, FunctionRegistry};
use std::collections::BTreeMap;

fn resolve_label(cfg_nodes: &BTreeMap<usize, CfgNode>, pc: usize) -> &str {
Expand Down Expand Up @@ -121,7 +121,7 @@ pub fn disassemble_instruction<C: ContextObject>(
insn: &ebpf::Insn,
cfg_nodes: &BTreeMap<usize, CfgNode>,
function_registry: &FunctionRegistry,
loader: &BuiltInProgram<C>,
loader: &BuiltinProgram<C>,
) -> String {
let name;
let desc;
Expand Down
2 changes: 1 addition & 1 deletion src/ebpf.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(clippy::integer_arithmetic)]
#![allow(clippy::arithmetic_side_effects)]
// Copyright 2016 6WIND S.A. <[email protected]>
//
// Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
Expand Down
Loading

0 comments on commit 14cd622

Please sign in to comment.