Skip to content

Commit

Permalink
Explicitly marks SBPF-v1 ELF tests as such.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Oct 7, 2024
1 parent a0f27e9 commit c426bce
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 35 deletions.
4 changes: 2 additions & 2 deletions benches/jit_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use test_utils::create_vm;

#[bench]
fn bench_init_vm(bencher: &mut Bencher) {
let mut file = File::open("tests/elfs/relative_call.so").unwrap();
let mut file = File::open("tests/elfs/relative_call_sbpfv1.so").unwrap();
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
let executable =
Expand All @@ -42,7 +42,7 @@ fn bench_init_vm(bencher: &mut Bencher) {
#[cfg(all(feature = "jit", not(target_os = "windows"), target_arch = "x86_64"))]
#[bench]
fn bench_jit_compile(bencher: &mut Bencher) {
let mut file = File::open("tests/elfs/relative_call.so").unwrap();
let mut file = File::open("tests/elfs/relative_call_sbpfv1.so").unwrap();
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
let mut executable =
Expand Down
30 changes: 15 additions & 15 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ mod test {

#[test]
fn test_validate() {
let elf_bytes = std::fs::read("tests/elfs/relative_call.so").unwrap();
let elf_bytes = std::fs::read("tests/elfs/relative_call_sbpfv1.so").unwrap();
let elf = Elf64::parse(&elf_bytes).unwrap();
let mut header = elf.file_header().clone();

Expand Down Expand Up @@ -1244,7 +1244,7 @@ mod test {

#[test]
fn test_load() {
let mut file = File::open("tests/elfs/relative_call.so").expect("file open failed");
let mut file = File::open("tests/elfs/relative_call_sbpfv1.so").expect("file open failed");
let mut elf_bytes = Vec::new();
file.read_to_end(&mut elf_bytes)
.expect("failed to read elf file");
Expand All @@ -1254,7 +1254,7 @@ mod test {
#[test]
fn test_load_unaligned() {
let mut elf_bytes =
std::fs::read("tests/elfs/relative_call.so").expect("failed to read elf file");
std::fs::read("tests/elfs/relative_call_sbpfv1.so").expect("failed to read elf file");
// The default allocator allocates aligned memory. Move the ELF slice to
// elf_bytes.as_ptr() + 1 to make it unaligned and test unaligned
// parsing.
Expand All @@ -1266,14 +1266,14 @@ mod test {
fn test_entrypoint() {
let loader = loader();

let mut file = File::open("tests/elfs/syscall_static.so").expect("file open failed");
let mut file = File::open("tests/elfs/relative_call_sbpfv1.so").expect("file open failed");
let mut elf_bytes = Vec::new();
file.read_to_end(&mut elf_bytes)
.expect("failed to read elf file");
let elf = ElfExecutable::load(&elf_bytes, loader.clone()).expect("validation failed");
let parsed_elf = Elf64::parse(&elf_bytes).unwrap();
let executable: &Executable<TestContextObject> = &elf;
assert_eq!(0, executable.get_entrypoint_instruction_offset());
assert_eq!(4, executable.get_entrypoint_instruction_offset());

let write_header = |header: Elf64Ehdr| unsafe {
let mut bytes = elf_bytes.clone();
Expand All @@ -1288,7 +1288,7 @@ mod test {
let elf_bytes = write_header(header.clone());
let elf = ElfExecutable::load(&elf_bytes, loader.clone()).expect("validation failed");
let executable: &Executable<TestContextObject> = &elf;
assert_eq!(1, executable.get_entrypoint_instruction_offset());
assert_eq!(5, executable.get_entrypoint_instruction_offset());

header.e_entry = 1;
let elf_bytes = write_header(header.clone());
Expand All @@ -1315,7 +1315,7 @@ mod test {
let elf_bytes = write_header(header);
let elf = ElfExecutable::load(&elf_bytes, loader).expect("validation failed");
let executable: &Executable<TestContextObject> = &elf;
assert_eq!(0, executable.get_entrypoint_instruction_offset());
assert_eq!(4, executable.get_entrypoint_instruction_offset());
}

#[test]
Expand Down Expand Up @@ -1878,15 +1878,15 @@ mod test {
#[should_panic(expected = r#"validation failed: WritableSectionNotSupported(".data")"#)]
fn test_writable_data_section() {
let elf_bytes =
std::fs::read("tests/elfs/data_section.so").expect("failed to read elf file");
std::fs::read("tests/elfs/data_section_sbpfv1.so").expect("failed to read elf file");
ElfExecutable::load(&elf_bytes, loader()).expect("validation failed");
}

#[test]
#[should_panic(expected = r#"validation failed: WritableSectionNotSupported(".bss")"#)]
fn test_bss_section() {
let elf_bytes =
std::fs::read("tests/elfs/bss_section.so").expect("failed to read elf file");
std::fs::read("tests/elfs/bss_section_sbpfv1.so").expect("failed to read elf file");
ElfExecutable::load(&elf_bytes, loader()).expect("validation failed");
}

Expand All @@ -1899,20 +1899,20 @@ mod test {
}

#[test]
#[should_panic(expected = "validation failed: RelativeJumpOutOfBounds(9)")]
#[should_panic(expected = "validation failed: RelativeJumpOutOfBounds(8)")]
fn test_relative_call_oob_backward() {
let mut elf_bytes =
std::fs::read("tests/elfs/relative_call.so").expect("failed to read elf file");
LittleEndian::write_i32(&mut elf_bytes[0x104C..0x1050], -11i32);
std::fs::read("tests/elfs/relative_call_sbpfv1.so").expect("failed to read elf file");
LittleEndian::write_i32(&mut elf_bytes[0x1044..0x1048], -11i32);
ElfExecutable::load(&elf_bytes, loader()).expect("validation failed");
}

#[test]
#[should_panic(expected = "validation failed: RelativeJumpOutOfBounds(12)")]
#[should_panic(expected = "validation failed: RelativeJumpOutOfBounds(11)")]
fn test_relative_call_oob_forward() {
let mut elf_bytes =
std::fs::read("tests/elfs/relative_call.so").expect("failed to read elf file");
LittleEndian::write_i32(&mut elf_bytes[0x1064..0x1068], 5);
std::fs::read("tests/elfs/relative_call_sbpfv1.so").expect("failed to read elf file");
LittleEndian::write_i32(&mut elf_bytes[0x105C..0x1060], 5);
ElfExecutable::load(&elf_bytes, loader()).expect("validation failed");
}

Expand Down
Binary file added tests/elfs/bss_section_sbpfv1.so
Binary file not shown.
Binary file not shown.
22 changes: 11 additions & 11 deletions tests/elfs/elfs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,35 @@ LD_COMMON="$TOOLCHAIN/llvm/bin/ld.lld -z notext -shared --Bdynamic -entry entryp
LD="$LD_COMMON --section-start=.text=0x100000000"
LD_V1=$LD_COMMON

$RC -o relative_call.o relative_call.rs
$LD -o relative_call.so relative_call.o
$RC_V1 -o relative_call.o relative_call.rs
$LD_V1 -o relative_call_sbpfv1.so relative_call.o

$RC_V1 -o syscall_reloc_64_32.o syscall_reloc_64_32.rs
$LD_V1 -o syscall_reloc_64_32.so syscall_reloc_64_32.o
$LD_V1 -o syscall_reloc_64_32_sbpfv1.so syscall_reloc_64_32.o

$RC -o bss_section.o bss_section.rs
$LD -o bss_section.so bss_section.o
$RC_V1 -o bss_section.o bss_section.rs
$LD_V1 -o bss_section_sbpfv1.so bss_section.o

$RC -o data_section.o data_section.rs
$LD -o data_section.so data_section.o
$RC_V1 -o data_section.o data_section.rs
$LD_V1 -o data_section_sbpfv1.so data_section.o

$RC_V1 -o rodata_section.o rodata_section.rs
$LD_V1 -o rodata_section_sbpfv1.so rodata_section.o

$RC -o program_headers_overflow.o rodata_section.rs
"$TOOLCHAIN"/llvm/bin/ld.lld -z notext -shared --Bdynamic -entry entrypoint --script program_headers_overflow.ld --noinhibit-exec -o program_headers_overflow.so program_headers_overflow.o

$RC -o struct_func_pointer.o struct_func_pointer.rs
$LD -o struct_func_pointer.so struct_func_pointer.o
$RC_V1 -o struct_func_pointer.o struct_func_pointer.rs
$LD_V1 -o struct_func_pointer_sbpfv1.so struct_func_pointer.o

$RC_V1 -o reloc_64_64.o reloc_64_64.rs
$LD_V1 -o reloc_64_64_sbpfv1.so reloc_64_64.o

$RC_V1 -o reloc_64_relative.o reloc_64_relative.rs
$LD_V1 -o reloc_64_relative_sbpfv1.so reloc_64_relative.o

# $RC_V1 -o reloc_64_relative_data.o reloc_64_relative_data.rs
# $LD_V1 -o reloc_64_relative_data_sbpfv1.so reloc_64_relative_data.o
$RC_V1 -o reloc_64_relative_data.o reloc_64_relative_data.rs
$LD_V1 -o reloc_64_relative_data_sbpfv1.so reloc_64_relative_data.o

# $RC_V1 -o callx_unaligned.o callx_unaligned.rs
# $LD_V1 -o callx_unaligned.so callx_unaligned.o
Expand Down
Binary file removed tests/elfs/relative_call.so
Binary file not shown.
Binary file renamed tests/elfs/bss_section.so → tests/elfs/relative_call_sbpfv1.so
100755 → 100644
Binary file not shown.
Binary file not shown.
File renamed without changes.
45 changes: 38 additions & 7 deletions tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2315,11 +2315,16 @@ fn test_err_mem_access_out_of_bound() {

#[test]
fn test_relative_call() {
let config = Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};
test_interpreter_and_jit_elf!(
"tests/elfs/relative_call.so",
"tests/elfs/relative_call_sbpfv1.so",
config,
[1],
(),
TestContextObject::new(18),
TestContextObject::new(16),
ProgramResult::Ok(3),
);
}
Expand Down Expand Up @@ -2994,7 +2999,6 @@ fn test_err_call_unresolved() {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};

test_interpreter_and_jit_asm!(
"
mov r1, 1
Expand All @@ -3015,8 +3019,13 @@ fn test_err_call_unresolved() {

#[test]
fn test_syscall_reloc_64_32() {
let config = Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};
test_interpreter_and_jit_elf!(
"tests/elfs/syscall_reloc_64_32.so",
"tests/elfs/syscall_reloc_64_32_sbpfv1.so",
config,
[],
(
"log" => syscalls::SyscallString::vm,
Expand All @@ -3030,12 +3039,13 @@ fn test_syscall_reloc_64_32() {
fn test_err_unresolved_syscall_reloc_64_32() {
let loader = BuiltinProgram::new_loader(
Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
reject_broken_elfs: true,
..Config::default()
},
FunctionRegistry::default(),
);
let mut file = File::open("tests/elfs/syscall_reloc_64_32.so").unwrap();
let mut file = File::open("tests/elfs/syscall_reloc_64_32_sbpfv1.so").unwrap();
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
assert_error!(
Expand Down Expand Up @@ -3064,8 +3074,13 @@ fn test_reloc_64_relative_sbpfv1() {
// returns the address of the first .rodata byte.
// [ 1] .text PROGBITS 0000000000000120 000120 000018 00 AX 0 0 8
// [ 2] .rodata PROGBITS 0000000000000138 000138 00000a 01 AMS 0 0 1
let config = Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};
test_interpreter_and_jit_elf!(
"tests/elfs/reloc_64_relative_sbpfv1.so",
config,
[],
(),
TestContextObject::new(2),
Expand All @@ -3082,8 +3097,13 @@ fn test_reloc_64_relative_data_sbfv1() {
//
// 00000000000001f8 <FILE>:
// 63: 08 01 00 00 00 00 00 00
let config = Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};
test_interpreter_and_jit_elf!(
"tests/elfs/reloc_64_relative_data_sbpfv1.so",
config,
[],
(),
TestContextObject::new(3),
Expand All @@ -3106,8 +3126,13 @@ fn test_reloc_64_relative_data_sbpfv1() {
//
// 00000000000001f8 <FILE>:
// 63: 00 00 00 00 08 01 00 00
let config = Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};
test_interpreter_and_jit_elf!(
"tests/elfs/reloc_64_relative_data_sbpfv1.so",
config,
[],
(),
TestContextObject::new(3),
Expand All @@ -3118,6 +3143,7 @@ fn test_reloc_64_relative_data_sbpfv1() {
#[test]
fn test_load_elf_rodata_sbpfv1() {
let config = Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
optimize_rodata: false,
..Config::default()
};
Expand All @@ -3136,11 +3162,16 @@ fn test_struct_func_pointer() {
// This tests checks that a struct field adjacent to another field
// which is a relocatable function pointer is not overwritten when
// the function pointer is relocated at load time.
let config = Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};
test_interpreter_and_jit_elf!(
"tests/elfs/struct_func_pointer.so",
"tests/elfs/struct_func_pointer_sbpfv1.so",
config,
[],
(),
TestContextObject::new(3),
TestContextObject::new(2),
ProgramResult::Ok(0x102030405060708),
);
}
Expand Down

0 comments on commit c426bce

Please sign in to comment.