Skip to content

Commit

Permalink
Adjusts SBPFVersion.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Nov 21, 2024
1 parent b69ff79 commit dcf9245
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 106 deletions.
8 changes: 4 additions & 4 deletions benches/vm_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn bench_jit_vs_interpreter_address_translation_stack_fixed(bencher: &mut Benche
bencher,
ADDRESS_TRANSLATION_STACK_CODE,
Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
enabled_sbpf_versions: SBPFVersion::V0..=SBPFVersion::V0,
..Config::default()
},
524289,
Expand All @@ -186,7 +186,7 @@ fn bench_jit_vs_interpreter_address_translation_stack_dynamic(bencher: &mut Benc
bencher,
ADDRESS_TRANSLATION_STACK_CODE,
Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V2,
enabled_sbpf_versions: SBPFVersion::V0..=SBPFVersion::V2,
..Config::default()
},
524289,
Expand Down Expand Up @@ -233,7 +233,7 @@ fn bench_jit_vs_interpreter_call_depth_fixed(bencher: &mut Bencher) {
call function_foo
exit",
Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
enabled_sbpf_versions: SBPFVersion::V0..=SBPFVersion::V0,
..Config::default()
},
137218,
Expand Down Expand Up @@ -264,7 +264,7 @@ fn bench_jit_vs_interpreter_call_depth_dynamic(bencher: &mut Bencher) {
add r11, 4
exit",
Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V2,
enabled_sbpf_versions: SBPFVersion::V0..=SBPFVersion::V2,
..Config::default()
},
176130,
Expand Down
4 changes: 2 additions & 2 deletions src/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fn make_instruction_map(sbpf_version: SBPFVersion) -> HashMap<String, (Instructi
result.insert(name.to_string(), (inst_type, opc))
};

if sbpf_version == SBPFVersion::V1 {
if sbpf_version == SBPFVersion::V0 {
entry("exit", NoOperand, ebpf::EXIT);
entry("return", NoOperand, ebpf::EXIT);
} else {
Expand All @@ -125,7 +125,7 @@ fn make_instruction_map(sbpf_version: SBPFVersion) -> HashMap<String, (Instructi
entry(
"syscall",
Syscall,
if sbpf_version == SBPFVersion::V1 {
if sbpf_version == SBPFVersion::V0 {
ebpf::CALL_IMM
} else {
ebpf::SYSCALL
Expand Down
61 changes: 32 additions & 29 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,16 +379,19 @@ impl<C: ContextObject> Executable<C> {
.ok_or(ElfParserError::OutOfBounds)?,
);
let config = loader.get_config();
let sbpf_version = if config.enabled_sbpf_versions.end() == &SBPFVersion::V1 {
let sbpf_version = if config.enabled_sbpf_versions.end() == &SBPFVersion::V0 {
if e_flags == EF_SBPF_V2 {
SBPFVersion::V2
SBPFVersion::Reserved
} else {
SBPFVersion::V1
SBPFVersion::V0
}
} else {
match e_flags {
0 => SBPFVersion::V1,
EF_SBPF_V2 => SBPFVersion::V2,
0 => SBPFVersion::V0,
1 => SBPFVersion::V1,
2 => SBPFVersion::V2,
3 => SBPFVersion::V3,
4 => SBPFVersion::V4,
_ => SBPFVersion::Reserved,
}
};
Expand Down Expand Up @@ -621,9 +624,9 @@ impl<C: ContextObject> Executable<C> {
let config = loader.get_config();
let header = elf.file_header();
let sbpf_version = if header.e_flags == EF_SBPF_V2 {
SBPFVersion::V2
SBPFVersion::Reserved
} else {
SBPFVersion::V1
SBPFVersion::V0
};

Self::validate(config, &elf, elf_bytes.as_slice())?;
Expand Down Expand Up @@ -748,9 +751,9 @@ impl<C: ContextObject> Executable<C> {
}

let sbpf_version = if header.e_flags == EF_SBPF_V2 {
SBPFVersion::V2
SBPFVersion::Reserved
} else {
SBPFVersion::V1
SBPFVersion::V0
};
if !config.enabled_sbpf_versions.contains(&sbpf_version) {
return Err(ElfError::UnsupportedSBPFVersion);
Expand Down Expand Up @@ -1010,9 +1013,9 @@ impl<C: ContextObject> Executable<C> {
let mut syscall_cache = BTreeMap::new();
let text_section = get_section(elf, b".text")?;
let sbpf_version = if elf.file_header().e_flags == EF_SBPF_V2 {
SBPFVersion::V2
SBPFVersion::Reserved
} else {
SBPFVersion::V1
SBPFVersion::V0
};

// Fixup all program counter relative call instructions
Expand Down Expand Up @@ -1086,7 +1089,7 @@ impl<C: ContextObject> Executable<C> {
.file_range()
.unwrap_or_default()
.contains(&r_offset)
|| sbpf_version == SBPFVersion::V1
|| sbpf_version == SBPFVersion::V0
{
r_offset.saturating_add(BYTE_OFFSET_IMMEDIATE)
} else {
Expand Down Expand Up @@ -1121,7 +1124,7 @@ impl<C: ContextObject> Executable<C> {
.file_range()
.unwrap_or_default()
.contains(&r_offset)
|| sbpf_version == SBPFVersion::V1
|| sbpf_version == SBPFVersion::V0
{
let imm_low_offset = imm_offset;
let imm_high_offset = imm_low_offset.saturating_add(INSN_SIZE);
Expand Down Expand Up @@ -1227,7 +1230,7 @@ impl<C: ContextObject> Executable<C> {
refd_addr.checked_shr(32).unwrap_or_default() as u32,
);
} else {
let refd_addr = if sbpf_version != SBPFVersion::V1 {
let refd_addr = if sbpf_version != SBPFVersion::V0 {
// We're relocating an address inside a data section (eg .rodata). The
// address is encoded as a simple u64.

Expand Down Expand Up @@ -1298,7 +1301,7 @@ impl<C: ContextObject> Executable<C> {
.or_insert_with(|| ebpf::hash_symbol_name(name));
if config.reject_broken_elfs
&& loader
.get_function_registry(SBPFVersion::V1)
.get_function_registry(SBPFVersion::V0)
.lookup_by_key(hash)
.is_none()
{
Expand Down Expand Up @@ -1776,7 +1779,7 @@ mod test {
assert!(matches!(
ElfExecutable::parse_ro_sections(
&config,
&SBPFVersion::V1,
&SBPFVersion::V0,
sections,
&elf_bytes,
),
Expand All @@ -1803,7 +1806,7 @@ mod test {
assert!(matches!(
ElfExecutable::parse_ro_sections(
&config,
&SBPFVersion::V1,
&SBPFVersion::V0,
sections,
&elf_bytes,
),
Expand All @@ -1815,7 +1818,7 @@ mod test {
fn test_sh_offset_not_same_as_vaddr() {
let config = Config {
reject_broken_elfs: true,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
enabled_sbpf_versions: SBPFVersion::V0..=SBPFVersion::V0,
..Config::default()
};
let elf_bytes = [0u8; 512];
Expand All @@ -1826,7 +1829,7 @@ mod test {
let sections: [(Option<&[u8]>, &Elf64Shdr); 1] = [(Some(b".text"), &s1)];
assert!(ElfExecutable::parse_ro_sections(
&config,
&SBPFVersion::V1,
&SBPFVersion::V0,
sections,
&elf_bytes
)
Expand All @@ -1836,7 +1839,7 @@ mod test {
s1.sh_offset = 0;
let sections: [(Option<&[u8]>, &Elf64Shdr); 1] = [(Some(b".text"), &s1)];
assert_eq!(
ElfExecutable::parse_ro_sections(&config, &SBPFVersion::V1, sections, &elf_bytes),
ElfExecutable::parse_ro_sections(&config, &SBPFVersion::V0, sections, &elf_bytes),
Err(ElfError::ValueOutOfBounds)
);
}
Expand Down Expand Up @@ -1927,7 +1930,7 @@ mod test {
(Some(b".rodata"), &s3),
];
let ro_section =
ElfExecutable::parse_ro_sections(&config, &SBPFVersion::V1, sections, &elf_bytes)
ElfExecutable::parse_ro_sections(&config, &SBPFVersion::V0, sections, &elf_bytes)
.unwrap();
let ro_region = get_ro_region(&ro_section, &elf_bytes);
let owned_section = match &ro_section {
Expand Down Expand Up @@ -1969,7 +1972,7 @@ mod test {
];
// V2 requires optimize_rodata=true
let ro_section =
ElfExecutable::parse_ro_sections(&config, &SBPFVersion::V1, sections, &elf_bytes)
ElfExecutable::parse_ro_sections(&config, &SBPFVersion::V0, sections, &elf_bytes)
.unwrap();
let ro_region = get_ro_region(&ro_section, &elf_bytes);
let owned_section = match &ro_section {
Expand Down Expand Up @@ -2009,7 +2012,7 @@ mod test {
(Some(b".rodata"), &s3),
];
let ro_section =
ElfExecutable::parse_ro_sections(&config, &SBPFVersion::V1, sections, &elf_bytes)
ElfExecutable::parse_ro_sections(&config, &SBPFVersion::V0, sections, &elf_bytes)
.unwrap();
let owned_section = match &ro_section {
Section::Owned(_offset, data) => data.as_slice(),
Expand Down Expand Up @@ -2068,7 +2071,7 @@ mod test {
assert!(matches!(
ElfExecutable::parse_ro_sections(
&config,
&SBPFVersion::V1, // v2 requires optimize_rodata=true
&SBPFVersion::V0, // v2 requires optimize_rodata=true
sections,
&elf_bytes,
),
Expand All @@ -2081,7 +2084,7 @@ mod test {
let config = Config::default();
let elf_bytes = [0u8; 512];
for (vaddr_base, sbpf_version) in [
(0, SBPFVersion::V1),
(0, SBPFVersion::V0),
(ebpf::MM_RODATA_START, SBPFVersion::V2),
] {
let s1 = new_section(vaddr_base, 10);
Expand Down Expand Up @@ -2109,7 +2112,7 @@ mod test {
let config = Config::default();
let elf_bytes = [0u8; 512];
for (vaddr_base, sbpf_version) in [
(0, SBPFVersion::V1),
(0, SBPFVersion::V0),
(ebpf::MM_RODATA_START, SBPFVersion::V2),
] {
let s1 = new_section(vaddr_base, 10);
Expand Down Expand Up @@ -2146,7 +2149,7 @@ mod test {
let config = Config::default();
let elf_bytes = [0u8; 512];
for (vaddr_base, sbpf_version) in [
(0, SBPFVersion::V1),
(0, SBPFVersion::V0),
(ebpf::MM_RODATA_START, SBPFVersion::V2),
] {
let s1 = new_section(vaddr_base, 10);
Expand Down Expand Up @@ -2199,7 +2202,7 @@ mod test {
#[test]
fn test_reject_rodata_stack_overlap() {
let config = Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V2,
enabled_sbpf_versions: SBPFVersion::V0..=SBPFVersion::V2,
..Config::default()
};
let elf_bytes = [0u8; 512];
Expand Down Expand Up @@ -2288,7 +2291,7 @@ mod test {
fn test_err_unresolved_syscall_reloc_64_32() {
let loader = BuiltinProgram::new_loader(
Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
enabled_sbpf_versions: SBPFVersion::V0..=SBPFVersion::V0,
reject_broken_elfs: true,
..Config::default()
},
Expand Down
8 changes: 4 additions & 4 deletions src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1774,8 +1774,8 @@ mod tests {
prog[pc * ebpf::INSN_SIZE] = ebpf::ADD64_IMM;
}

let mut empty_program_machine_code_length_per_version = [0; 2];
for sbpf_version in [SBPFVersion::V1, SBPFVersion::V2] {
let mut empty_program_machine_code_length_per_version = [0; 5];
for sbpf_version in [SBPFVersion::V0, SBPFVersion::V4] {
let empty_program_machine_code_length = {
let config = Config {
noop_instruction_rate: 0,
Expand All @@ -1802,7 +1802,7 @@ mod tests {
let config = Config {
instruction_meter_checkpoint_distance: index * INSTRUCTION_COUNT * 2,
noop_instruction_rate: 0,
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
enabled_sbpf_versions: SBPFVersion::V0..=SBPFVersion::V0,
..Config::default()
};
let mut executable = create_mockup_executable(config, &prog);
Expand All @@ -1822,7 +1822,7 @@ mod tests {
MACHINE_CODE_PER_INSTRUCTION_METER_CHECKPOINT
);

for sbpf_version in [SBPFVersion::V1, SBPFVersion::V2] {
for sbpf_version in [SBPFVersion::V0, SBPFVersion::V4] {
let empty_program_machine_code_length =
empty_program_machine_code_length_per_version[sbpf_version as usize];

Expand Down
Loading

0 comments on commit dcf9245

Please sign in to comment.