Skip to content

Commit

Permalink
Replaces enable_sbpf_v1 and enable_sbpf_v2 by enabled_sbpf_versions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Jul 25, 2024
1 parent 33c0880 commit ff194ed
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 48 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
9 changes: 3 additions & 6 deletions src/vm.rs
Original file line number Diff line number Diff line change
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
26 changes: 13 additions & 13 deletions tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1943,7 +1943,7 @@ fn test_string_stack() {
#[test]
fn test_err_dynamic_stack_out_of_bound() {
let config = Config {
enable_sbpf_v2: true,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V2,
max_call_depth: 3,
..Config::default()
};
Expand Down Expand Up @@ -2083,9 +2083,9 @@ fn test_entrypoint_exit() {
// can't infer anything from the stack size so we track call depth
// explicitly. Make sure exit still works with both fixed and dynamic
// frames.
for enable_sbpf_v2 in [false, true] {
for highest_sbpf_version in [SBPFVersion::V1, SBPFVersion::V2] {
let config = Config {
enable_sbpf_v2,
enabled_sbpf_versions: SBPFVersion::V1..=highest_sbpf_version,
..Config::default()
};

Expand All @@ -2111,9 +2111,9 @@ fn test_entrypoint_exit() {

#[test]
fn test_stack_call_depth_tracking() {
for enable_sbpf_v2 in [false, true] {
for highest_sbpf_version in [SBPFVersion::V1, SBPFVersion::V2] {
let config = Config {
enable_sbpf_v2,
enabled_sbpf_versions: SBPFVersion::V1..=highest_sbpf_version,
max_call_depth: 2,
..Config::default()
};
Expand Down Expand Up @@ -3421,7 +3421,7 @@ fn test_total_chaos() {
#[test]
fn test_err_fixed_stack_out_of_bound() {
let config = Config {
enable_sbpf_v2: false,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
max_call_depth: 3,
..Config::default()
};
Expand All @@ -3445,7 +3445,7 @@ fn test_err_fixed_stack_out_of_bound() {
#[test]
fn test_lddw() {
let config = Config {
enable_sbpf_v2: false,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};
test_interpreter_and_jit_asm!(
Expand Down Expand Up @@ -3566,7 +3566,7 @@ fn test_lddw() {
#[test]
fn test_le() {
let config = Config {
enable_sbpf_v2: false,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};
test_interpreter_and_jit_asm!(
Expand Down Expand Up @@ -3629,7 +3629,7 @@ fn test_le() {
#[test]
fn test_neg() {
let config = Config {
enable_sbpf_v2: false,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};
test_interpreter_and_jit_asm!(
Expand Down Expand Up @@ -3681,7 +3681,7 @@ fn test_neg() {
#[test]
fn test_callx_imm() {
let config = Config {
enable_sbpf_v2: false,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};
test_interpreter_and_jit_asm!(
Expand All @@ -3706,7 +3706,7 @@ fn test_callx_imm() {
#[test]
fn test_mul() {
let config = Config {
enable_sbpf_v2: false,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};
test_interpreter_and_jit_asm!(
Expand Down Expand Up @@ -3783,7 +3783,7 @@ fn test_mul() {
#[test]
fn test_div() {
let config = Config {
enable_sbpf_v2: false,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};
test_interpreter_and_jit_asm!(
Expand Down Expand Up @@ -3875,7 +3875,7 @@ fn test_div() {
#[test]
fn test_mod() {
let config = Config {
enable_sbpf_v2: false,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};
test_interpreter_and_jit_asm!(
Expand Down
4 changes: 2 additions & 2 deletions tests/exercise_instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use solana_rbpf::{
assembler::assemble,
ebpf,
memory_region::MemoryRegion,
program::{BuiltinFunction, BuiltinProgram, FunctionRegistry},
program::{BuiltinFunction, BuiltinProgram, FunctionRegistry, SBPFVersion},
static_analysis::Analysis,
verifier::RequisiteVerifier,
vm::{Config, ContextObject, TestContextObject},
Expand Down Expand Up @@ -540,7 +540,7 @@ fn test_ins(v1: bool, ins: String, prng: &mut SmallRng, cu: u64) {

let mut config = Config::default();
if v1 {
config.enable_sbpf_v2 = false;
config.enabled_sbpf_versions = SBPFVersion::V1..=SBPFVersion::V1;
}
test_interpreter_and_jit_asm!(asm.as_str(), config, input, (), TestContextObject::new(cu));
}
14 changes: 7 additions & 7 deletions tests/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ fn test_verifier_err_incomplete_lddw() {
fn test_verifier_err_invalid_reg_dst() {
// r11 is disabled when sbpf_version.dynamic_stack_frames()=false, and only sub and add are
// allowed when sbpf_version.dynamic_stack_frames()=true
for enable_sbpf_v2 in [false, true] {
for highest_sbpf_version in [SBPFVersion::V1, SBPFVersion::V2] {
let executable = assemble::<TestContextObject>(
"
mov r11, 1
exit",
Arc::new(BuiltinProgram::new_loader(
Config {
enable_sbpf_v2,
enabled_sbpf_versions: SBPFVersion::V1..=highest_sbpf_version,
..Config::default()
},
FunctionRegistry::default(),
Expand All @@ -176,14 +176,14 @@ fn test_verifier_err_invalid_reg_dst() {
fn test_verifier_err_invalid_reg_src() {
// r11 is disabled when sbpf_version.dynamic_stack_frames()=false, and only sub and add are
// allowed when sbpf_version.dynamic_stack_frames()=true
for enable_sbpf_v2 in [false, true] {
for highest_sbpf_version in [SBPFVersion::V1, SBPFVersion::V2] {
let executable = assemble::<TestContextObject>(
"
mov r0, r11
exit",
Arc::new(BuiltinProgram::new_loader(
Config {
enable_sbpf_v2,
enabled_sbpf_versions: SBPFVersion::V1..=highest_sbpf_version,
..Config::default()
},
FunctionRegistry::default(),
Expand Down Expand Up @@ -360,21 +360,21 @@ fn test_sdiv_disabled() {
];

for (opc, instruction) in instructions {
for enable_sbpf_v2 in [true, false] {
for highest_sbpf_version in [SBPFVersion::V1, SBPFVersion::V2] {
let assembly = format!("\n{instruction}\nexit");
let executable = assemble::<TestContextObject>(
&assembly,
Arc::new(BuiltinProgram::new_loader(
Config {
enable_sbpf_v2,
enabled_sbpf_versions: SBPFVersion::V1..=highest_sbpf_version.clone(),
..Config::default()
},
FunctionRegistry::default(),
)),
)
.unwrap();
let result = executable.verify::<RequisiteVerifier>();
if enable_sbpf_v2 {
if highest_sbpf_version == SBPFVersion::V2 {
assert!(result.is_ok());
} else {
assert_error!(result, "VerifierError(UnknownOpCode({}, {}))", opc, 0);
Expand Down

0 comments on commit ff194ed

Please sign in to comment.