From c4097045c896c542f4c42a2261b85d9ca89219ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Mei=C3=9Fner?= Date: Thu, 25 Jul 2024 18:02:15 +0200 Subject: [PATCH] Replaces enable_sbpf_v1 and enable_sbpf_v2 by enabled_sbpf_versions. --- benches/vm_execution.rs | 13 ++++++++----- src/assembler.rs | 8 ++------ src/elf.rs | 13 +++++-------- src/program.rs | 2 +- src/vm.rs | 9 +++------ tests/execution.rs | 26 +++++++++++++------------- tests/exercise_instructions.rs | 4 ++-- tests/verifier.rs | 14 +++++++------- 8 files changed, 41 insertions(+), 48 deletions(-) diff --git a/benches/vm_execution.rs b/benches/vm_execution.rs index 3fa14c03c..321b471c9 100644 --- a/benches/vm_execution.rs +++ b/benches/vm_execution.rs @@ -12,7 +12,10 @@ 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::{ - elf::Executable, program::BuiltinProgram, verifier::RequisiteVerifier, vm::TestContextObject, + elf::Executable, + program::{BuiltinProgram, SBPFVersion}, + verifier::RequisiteVerifier, + vm::TestContextObject, }; use std::{fs::File, io::Read, sync::Arc}; use test::Bencher; @@ -166,7 +169,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, @@ -181,7 +184,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, @@ -228,7 +231,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, @@ -259,7 +262,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, diff --git a/src/assembler.rs b/src/assembler.rs index 4e9a6b579..08c852f3a 100644 --- a/src/assembler.rs +++ b/src/assembler.rs @@ -19,7 +19,7 @@ use crate::{ }, ebpf::{self, Insn}, elf::Executable, - program::{BuiltinProgram, FunctionRegistry, SBPFVersion}, + program::{BuiltinProgram, FunctionRegistry}, vm::ContextObject, }; use std::collections::HashMap; @@ -311,11 +311,7 @@ pub fn assemble( src: &str, loader: Arc>, ) -> Result, 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(); diff --git a/src/elf.rs b/src/elf.rs index e63f736b2..c5b531d4d 100644 --- a/src/elf.rs +++ b/src/elf.rs @@ -516,16 +516,13 @@ impl Executable { } 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 { @@ -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]; @@ -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]; diff --git a/src/program.rs b/src/program.rs index 7fce2394a..0ef76bcc8 100644 --- a/src/program.rs +++ b/src/program.rs @@ -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, diff --git a/src/vm.rs b/src/vm.rs index 3ed7c9267..969a339ee 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -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, } impl Config { @@ -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, } } } diff --git a/tests/execution.rs b/tests/execution.rs index e6d55064a..6d8f151fc 100644 --- a/tests/execution.rs +++ b/tests/execution.rs @@ -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() }; @@ -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() }; @@ -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() }; @@ -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() }; @@ -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!( @@ -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!( @@ -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!( @@ -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!( @@ -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!( @@ -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!( @@ -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!( diff --git a/tests/exercise_instructions.rs b/tests/exercise_instructions.rs index 889bf0ba5..e2fc04b88 100644 --- a/tests/exercise_instructions.rs +++ b/tests/exercise_instructions.rs @@ -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}, @@ -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)); } diff --git a/tests/verifier.rs b/tests/verifier.rs index 9146f7982..b7fb9c232 100644 --- a/tests/verifier.rs +++ b/tests/verifier.rs @@ -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::( " 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(), @@ -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::( " 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(), @@ -360,13 +360,13 @@ 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::( &assembly, Arc::new(BuiltinProgram::new_loader( Config { - enable_sbpf_v2, + enabled_sbpf_versions: SBPFVersion::V1..=highest_sbpf_version.clone(), ..Config::default() }, FunctionRegistry::default(), @@ -374,7 +374,7 @@ fn test_sdiv_disabled() { ) .unwrap(); let result = executable.verify::(); - if enable_sbpf_v2 { + if highest_sbpf_version == SBPFVersion::V2 { assert!(result.is_ok()); } else { assert_error!(result, "VerifierError(UnknownOpCode({}, {}))", opc, 0);