Skip to content

Commit

Permalink
Refactor - Config::enabled_sbpf_versions (#582)
Browse files Browse the repository at this point in the history
* Removes derive(Copy) from Config.

* Replaces enable_sbpf_v1 and enable_sbpf_v2 by enabled_sbpf_versions.
  • Loading branch information
Lichtso authored Jul 26, 2024
1 parent f3758ec commit c72113b
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 85 deletions.
15 changes: 10 additions & 5 deletions benches/vm_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ extern crate solana_rbpf;
extern crate test;

#[cfg(all(feature = "jit", not(target_os = "windows"), target_arch = "x86_64"))]
use solana_rbpf::{ebpf, memory_region::MemoryRegion, program::FunctionRegistry, vm::Config};
use solana_rbpf::{
ebpf,
memory_region::MemoryRegion,
program::{FunctionRegistry, SBPFVersion},
vm::Config,
};
use solana_rbpf::{
elf::Executable, program::BuiltinProgram, verifier::RequisiteVerifier, vm::TestContextObject,
};
Expand Down Expand Up @@ -166,7 +171,7 @@ fn bench_jit_vs_interpreter_address_translation_stack_fixed(bencher: &mut Benche
bencher,
ADDRESS_TRANSLATION_STACK_CODE,
Config {
enable_sbpf_v2: false,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
},
524289,
Expand All @@ -181,7 +186,7 @@ fn bench_jit_vs_interpreter_address_translation_stack_dynamic(bencher: &mut Benc
bencher,
ADDRESS_TRANSLATION_STACK_CODE,
Config {
enable_sbpf_v2: true,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V2,
..Config::default()
},
524289,
Expand Down Expand Up @@ -228,7 +233,7 @@ fn bench_jit_vs_interpreter_call_depth_fixed(bencher: &mut Bencher) {
call function_foo
exit",
Config {
enable_sbpf_v2: false,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
},
137218,
Expand Down Expand Up @@ -259,7 +264,7 @@ fn bench_jit_vs_interpreter_call_depth_dynamic(bencher: &mut Bencher) {
add r11, 4
exit",
Config {
enable_sbpf_v2: true,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V2,
..Config::default()
},
176130,
Expand Down
8 changes: 2 additions & 6 deletions src/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
},
ebpf::{self, Insn},
elf::Executable,
program::{BuiltinProgram, FunctionRegistry, SBPFVersion},
program::{BuiltinProgram, FunctionRegistry},
vm::ContextObject,
};
use std::collections::HashMap;
Expand Down Expand Up @@ -311,11 +311,7 @@ pub fn assemble<C: ContextObject>(
src: &str,
loader: Arc<BuiltinProgram<C>>,
) -> Result<Executable<C>, String> {
let sbpf_version = if loader.get_config().enable_sbpf_v2 {
SBPFVersion::V2
} else {
SBPFVersion::V1
};
let sbpf_version = loader.get_config().enabled_sbpf_versions.end().clone();

let statements = parse(src)?;
let instruction_map = make_instruction_map();
Expand Down
13 changes: 5 additions & 8 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,16 +516,13 @@ impl<C: ContextObject> Executable<C> {
}

let sbpf_version = if header.e_flags == EF_SBPF_V2 {
if !config.enable_sbpf_v2 {
return Err(ElfError::UnsupportedSBPFVersion);
}
SBPFVersion::V2
} else {
if !config.enable_sbpf_v1 {
return Err(ElfError::UnsupportedSBPFVersion);
}
SBPFVersion::V1
};
if !config.enabled_sbpf_versions.contains(&sbpf_version) {
return Err(ElfError::UnsupportedSBPFVersion);
}

if sbpf_version.enable_elf_vaddr() {
if !config.optimize_rodata {
Expand Down Expand Up @@ -1456,7 +1453,7 @@ mod test {
fn test_sh_offset_not_same_as_vaddr() {
let config = Config {
reject_broken_elfs: true,
enable_sbpf_v2: false,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};
let elf_bytes = [0u8; 512];
Expand Down Expand Up @@ -1834,7 +1831,7 @@ mod test {
#[test]
fn test_reject_rodata_stack_overlap() {
let config = Config {
enable_sbpf_v2: true,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V2,
..Config::default()
};
let elf_bytes = [0u8; 512];
Expand Down
2 changes: 1 addition & 1 deletion src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use {
};

/// Defines a set of sbpf_version of an executable
#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, PartialOrd, Eq, Clone)]
pub enum SBPFVersion {
/// The legacy format
V1,
Expand Down
11 changes: 4 additions & 7 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn get_runtime_environment_key() -> i32 {
}

/// VM configuration settings
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Config {
/// Maximum call depth
pub max_call_depth: usize,
Expand Down Expand Up @@ -80,10 +80,8 @@ pub struct Config {
pub optimize_rodata: bool,
/// Use aligned memory mapping
pub aligned_memory_mapping: bool,
/// Allow ExecutableCapability::V1
pub enable_sbpf_v1: bool,
/// Allow ExecutableCapability::V2
pub enable_sbpf_v2: bool,
/// Allowed [SBPFVersion]s
pub enabled_sbpf_versions: std::ops::RangeInclusive<SBPFVersion>,
}

impl Config {
Expand Down Expand Up @@ -111,8 +109,7 @@ impl Default for Config {
reject_callx_r10: true,
optimize_rodata: true,
aligned_memory_mapping: true,
enable_sbpf_v1: true,
enable_sbpf_v2: true,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V2,
}
}
}
Expand Down
Loading

0 comments on commit c72113b

Please sign in to comment.