Skip to content

Commit

Permalink
Revert "Moves Config::optimize_rodata into SBPFVersion. (solana-labs#477
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso authored Jul 5, 2023
1 parent e538d61 commit 31b7cad
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
4 changes: 4 additions & 0 deletions fuzz/fuzz_targets/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct ConfigTemplate {
sanitize_user_provided_values: bool,
encrypt_environment_registers: bool,
reject_callx_r10: bool,
optimize_rodata: bool,
}

impl<'a> Arbitrary<'a> for ConfigTemplate {
Expand All @@ -28,6 +29,7 @@ impl<'a> Arbitrary<'a> for ConfigTemplate {
sanitize_user_provided_values: bools & (1 << 3) != 0,
encrypt_environment_registers: bools & (1 << 4) != 0,
reject_callx_r10: bools & (1 << 6) != 0,
optimize_rodata: bools & (1 << 9) != 0,
})
}

Expand All @@ -51,6 +53,7 @@ impl From<ConfigTemplate> for Config {
sanitize_user_provided_values,
encrypt_environment_registers,
reject_callx_r10,
optimize_rodata,
} => Config {
max_call_depth,
enable_stack_frame_gaps,
Expand All @@ -65,6 +68,7 @@ impl From<ConfigTemplate> for Config {
0
},
reject_callx_r10,
optimize_rodata,
..Default::default()
},
}
Expand Down
27 changes: 14 additions & 13 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,6 @@ impl SBPFVersion {
self != &SBPFVersion::V1
}

/// Avoid copying read only sections when possible
pub fn optimize_rodata(&self) -> bool {
self != &SBPFVersion::V1
}

/// Use dynamic stack frame sizes
pub fn dynamic_stack_frames(&self) -> bool {
self != &SBPFVersion::V1
Expand Down Expand Up @@ -723,7 +718,7 @@ impl<V: Verifier, C: ContextObject> Executable<V, C> {
};

if sbpf_version.enable_elf_vaddr() {
if !sbpf_version.optimize_rodata() {
if !config.optimize_rodata {
// When optimize_rodata=false, we allocate a vector and copy all
// rodata sections into it. In that case we can't allow virtual
// addresses or we'd potentially have to do huge allocations.
Expand Down Expand Up @@ -858,7 +853,7 @@ impl<V: Verifier, C: ContextObject> Executable<V, C> {
if !invalid_offsets {
if sbpf_version.enable_elf_vaddr() {
// This is enforced in validate()
debug_assert!(sbpf_version.optimize_rodata());
debug_assert!(config.optimize_rodata);
if section_addr < section_header.sh_offset() {
invalid_offsets = true;
} else {
Expand Down Expand Up @@ -910,7 +905,7 @@ impl<V: Verifier, C: ContextObject> Executable<V, C> {
.saturating_add(1)
.saturating_sub(first_ro_section)
== n_ro_sections;
let ro_section = if sbpf_version.optimize_rodata() && can_borrow {
let ro_section = if config.optimize_rodata && can_borrow {
// Read only sections are grouped together with no intermixed non-ro
// sections. We can borrow.

Expand All @@ -937,7 +932,7 @@ impl<V: Verifier, C: ContextObject> Executable<V, C> {
// Read only and other non-ro sections are mixed. Zero the non-ro
// sections and and copy the ro ones at their intended offsets.

if sbpf_version.optimize_rodata() {
if config.optimize_rodata {
// The rodata region starts at MM_PROGRAM_START + offset,
// [MM_PROGRAM_START, MM_PROGRAM_START + offset) is not
// mappable. We only need to allocate highest_addr - lowest_addr
Expand Down Expand Up @@ -1967,7 +1962,10 @@ mod test {

#[test]
fn test_owned_ro_region_initial_gap_mappable() {
let config = Config::default();
let config = Config {
optimize_rodata: false,
..Config::default()
};
let elf_bytes = [0u8; 512];

// the first section starts at a non-zero offset
Expand All @@ -1991,7 +1989,7 @@ mod test {
};

// [s1.sh_addr..s3.sh_addr + s3.sh_size] is where the readonly data is.
// But for backwards compatibility (sbpf_version.optimize_rodata()=false)
// But for backwards compatibility (config.optimize_rodata=false)
// [0..s1.sh_addr] is mappable too (and zeroed).
assert!(matches!(
ro_region.vm_to_host(ebpf::MM_PROGRAM_START, s3.sh_addr + s3.sh_size),
Expand Down Expand Up @@ -2065,7 +2063,10 @@ mod test {

#[test]
fn test_borrowed_ro_sections_disabled() {
let config = Config::default();
let config = Config {
optimize_rodata: false,
..Config::default()
};
let elf_bytes = [0u8; 512];

// s1 and s2 are contiguous, the rodata section can be borrowed from the
Expand All @@ -2078,7 +2079,7 @@ mod test {
assert!(matches!(
ElfExecutable::parse_ro_sections(
&config,
&SBPFVersion::V1,
&SBPFVersion::V1, // v2 requires optimize_rodata=true
sections,
&elf_bytes,
),
Expand Down
2 changes: 1 addition & 1 deletion src/memory_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl MemoryRegion {
/// Convert a virtual machine address into a host address
pub fn vm_to_host(&self, vm_addr: u64, len: u64) -> ProgramResult {
// This can happen if a region starts at an offset from the base region
// address, eg with rodata regions if sbpf_version.optimize_rodata()=true, see
// address, eg with rodata regions if config.optimize_rodata = true, see
// Elf::get_ro_region.
if vm_addr < self.vm_addr {
return ProgramResult::Err(Box::new(EbpfError::InvalidVirtualAddress(vm_addr)));
Expand Down
3 changes: 3 additions & 0 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ pub struct Config {
pub external_internal_function_hash_collision: bool,
/// Have the verifier reject "callx r10"
pub reject_callx_r10: bool,
/// Avoid copying read only sections when possible
pub optimize_rodata: bool,
/// Use the new ELF parser
pub new_elf_parser: bool,
/// Use aligned memory mapping
Expand Down Expand Up @@ -263,6 +265,7 @@ impl Default for Config {
>> PROGRAM_ENVIRONMENT_KEY_SHIFT,
external_internal_function_hash_collision: true,
reject_callx_r10: true,
optimize_rodata: true,
new_elf_parser: true,
aligned_memory_mapping: true,
enable_sbpf_v1: true,
Expand Down
10 changes: 8 additions & 2 deletions tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3098,7 +3098,10 @@ fn test_load_elf_empty_rodata() {

#[test]
fn test_load_elf_rodata() {
let config = Config::default();
let config = Config {
optimize_rodata: true,
..Config::default()
};
test_interpreter_and_jit_elf!(
"tests/elfs/rodata.so",
config,
Expand All @@ -3111,7 +3114,10 @@ fn test_load_elf_rodata() {

#[test]
fn test_load_elf_rodata_sbpfv1() {
let config = Config::default();
let config = Config {
optimize_rodata: false,
..Config::default()
};
test_interpreter_and_jit_elf!(
"tests/elfs/rodata_sbpfv1.so",
config,
Expand Down

0 comments on commit 31b7cad

Please sign in to comment.