Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Fix ELF tests a bit #479

Merged
merged 3 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl JitProgram {
page_size,
pc_section: std::slice::from_raw_parts_mut(raw as *mut usize, pc),
text_section: std::slice::from_raw_parts_mut(
(raw as *mut u8).add(pc_loc_table_size),
raw.add(pc_loc_table_size),
over_allocated_code_size,
),
})
Expand Down
14 changes: 7 additions & 7 deletions src/memory_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ impl MappingCache {

#[cfg(test)]
mod test {
use std::{cell::RefCell, sync::Arc};
use std::{cell::RefCell, rc::Rc};
use test_utils::assert_error;

use super::*;
Expand Down Expand Up @@ -1728,9 +1728,9 @@ mod test {
..Config::default()
};
let original = [11, 22];
let copied = Arc::new(RefCell::new(Vec::new()));
let copied = Rc::new(RefCell::new(Vec::new()));

let c = Arc::clone(&copied);
let c = Rc::clone(&copied);
let m = MemoryMapping::new_with_cow(
vec![MemoryRegion::new_cow(&original, ebpf::MM_PROGRAM_START, 42)],
Box::new(move |_| {
Expand Down Expand Up @@ -1763,9 +1763,9 @@ mod test {
..Config::default()
};
let original = [11, 22];
let copied = Arc::new(RefCell::new(Vec::new()));
let copied = Rc::new(RefCell::new(Vec::new()));

let c = Arc::clone(&copied);
let c = Rc::clone(&copied);
let m = MemoryMapping::new_with_cow(
vec![MemoryRegion::new_cow(&original, ebpf::MM_PROGRAM_START, 42)],
Box::new(move |_| {
Expand Down Expand Up @@ -1803,9 +1803,9 @@ mod test {
};
let original1 = [11, 22];
let original2 = [33, 44];
let copied = Arc::new(RefCell::new(Vec::new()));
let copied = Rc::new(RefCell::new(Vec::new()));

let c = Arc::clone(&copied);
let c = Rc::clone(&copied);
let m = MemoryMapping::new_with_cow(
vec![
MemoryRegion::new_cow(&original1, ebpf::MM_PROGRAM_START, 42),
Expand Down
Binary file modified tests/elfs/bss_section.so
Binary file not shown.
46 changes: 23 additions & 23 deletions tests/elfs/elfs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
#https://github.com/solana-labs/llvm-builder/releases

LLVM_DIR=../../../solana/sdk/sbf/dependencies/sbf-tools/llvm/bin/
CC_FLAGS="-Werror -target sbf -mcpu=sbfv2 -O2 -fno-builtin -fPIC"
LD_FLAGS="-z notext -shared --Bdynamic -entry entrypoint --script elf.ld"
CC_FLAGS_COMMON="-Werror -target sbf -O2 -fno-builtin -fPIC"
CC_FLAGS="$CC_FLAGS_COMMON -mcpu=sbfv2"
CC_FLAGS_V1="$CC_FLAGS_COMMON -mcpu=generic"
LD_FLAGS_COMMON="-z notext -shared --Bdynamic -entry entrypoint --script elf.ld"
LD_FLAGS="$LD_FLAGS_COMMON --section-start=.text=0x100000000"
LD_FLAGS_V1=$LD_FLAGS_COMMON

"$LLVM_DIR"clang $CC_FLAGS -o noop.o -c noop.c
"$LLVM_DIR"ld.lld $LD_FLAGS -o noop.so noop.o
Expand Down Expand Up @@ -33,33 +37,33 @@ rm multiple_file.o
"$LLVM_DIR"ld.lld $LD_FLAGS -o relative_call.so relative_call.o
rm relative_call.o

"$LLVM_DIR"clang $CC_FLAGS_V1 -o reloc_64_64.o -c reloc_64_64.c
"$LLVM_DIR"ld.lld $LD_FLAGS_V1 -o reloc_64_64_sbpfv1.so reloc_64_64.o
rm reloc_64_64.o

"$LLVM_DIR"clang $CC_FLAGS -o reloc_64_64.o -c reloc_64_64.c
"$LLVM_DIR"ld.lld $LD_FLAGS -o reloc_64_64.so reloc_64_64.o
rm reloc_64_64.o

"$LLVM_DIR"clang $CC_FLAGS -o reloc_64_64_high_vaddr.o -c reloc_64_64.c
"$LLVM_DIR"ld.lld -z notext -shared --Bdynamic -entry entrypoint --nmagic --section-start=.text=0x100000000 -o reloc_64_64_high_vaddr.so reloc_64_64_high_vaddr.o
rm reloc_64_64_high_vaddr.o

"$LLVM_DIR"clang $CC_FLAGS -o reloc_64_relative.o -c reloc_64_relative.c
"$LLVM_DIR"ld.lld $LD_FLAGS -o reloc_64_relative.so reloc_64_relative.o
rm reloc_64_relative.o

"$LLVM_DIR"clang $CC_FLAGS -o reloc_64_relative_high_vaddr.o -c reloc_64_relative.c
"$LLVM_DIR"ld.lld $LD_FLAGS --section-start=.text=0x100000000 -o reloc_64_relative_high_vaddr.so reloc_64_relative_high_vaddr.o
rm reloc_64_relative_high_vaddr.o
"$LLVM_DIR"clang $CC_FLAGS_V1 -o reloc_64_relative.o -c reloc_64_relative.c
"$LLVM_DIR"ld.lld $LD_FLAGS_V1 -o reloc_64_relative_sbpfv1.so reloc_64_relative.o
rm reloc_64_relative.o

"$LLVM_DIR"clang $CC_FLAGS -o reloc_64_relative_data.o -c reloc_64_relative_data.c
"$LLVM_DIR"ld.lld $LD_FLAGS -o reloc_64_relative_data.so reloc_64_relative_data.o
"$LLVM_DIR"clang $CC_FLAGS_V1 -o reloc_64_relative_data.o -c reloc_64_relative_data.c
"$LLVM_DIR"ld.lld $LD_FLAGS_V1 -o reloc_64_relative_data_sbpfv1.so reloc_64_relative_data.o
rm reloc_64_relative_data.o

"$LLVM_DIR"clang $CC_FLAGS -o reloc_64_relative_data_high_vaddr.o -c reloc_64_relative_data.c
"$LLVM_DIR"ld.lld $LD_FLAGS --section-start=.text=0x100000000 -o reloc_64_relative_data_high_vaddr.so reloc_64_relative_data_high_vaddr.o
rm reloc_64_relative_data_high_vaddr.o
"$LLVM_DIR"clang $CC_FLAGS -o reloc_64_relative_data.o -c reloc_64_relative_data.c
"$LLVM_DIR"ld.lld $LD_FLAGS --section-start=.text=0x100000000 -o reloc_64_relative_data.so reloc_64_relative_data.o
rm reloc_64_relative_data.o

"$LLVM_DIR"clang $CC_FLAGS -o reloc_64_relative_data_pre_sbfv2.o -c reloc_64_relative_data.c
"$LLVM_DIR"ld.lld $LD_FLAGS -o reloc_64_relative_data_pre_sbfv2.so reloc_64_relative_data_pre_sbfv2.o
rm reloc_64_relative_data_pre_sbfv2.o
"$LLVM_DIR"clang $CC_FLAGS_V1 -o reloc_64_relative_data.o -c reloc_64_relative_data.c
"$LLVM_DIR"ld.lld $LD_FLAGS_V1 -o reloc_64_relative_data_sbpfv1.so reloc_64_relative_data.o
rm reloc_64_relative_data.o

"$LLVM_DIR"clang $CC_FLAGS -o scratch_registers.o -c scratch_registers.c
"$LLVM_DIR"ld.lld $LD_FLAGS -o scratch_registers.so scratch_registers.o
Expand All @@ -81,14 +85,10 @@ rm bss_section.o
"$LLVM_DIR"ld.lld $LD_FLAGS -o rodata.so rodata.o
rm rodata.o

"$LLVM_DIR"clang $CC_FLAGS -mcpu=generic -o rodata.o -c rodata.c
"$LLVM_DIR"ld.lld $LD_FLAGS -o rodata_sbpfv1.so rodata.o
"$LLVM_DIR"clang $CC_FLAGS_V1 -mcpu=generic -o rodata.o -c rodata.c
"$LLVM_DIR"ld.lld $LD_FLAGS_V1 -o rodata_sbpfv1.so rodata.o
rm rodata.o

"$LLVM_DIR"clang $CC_FLAGS -o rodata_high_vaddr.o -c rodata.c
"$LLVM_DIR"ld.lld -z notext -shared --Bdynamic -entry entrypoint --nmagic --section-start=.text=0x100000000 --section-start=.rodata=0x100000020 -o rodata_high_vaddr.so rodata_high_vaddr.o
rm rodata_high_vaddr.o

"$LLVM_DIR"clang $CC_FLAGS -o syscall_static_unknown.o -c syscall_static_unknown.c
"$LLVM_DIR"ld.lld $LD_FLAGS -o syscall_static_unknown.so syscall_static_unknown.o
rm syscall_static_unknown.o
Expand Down
Binary file modified tests/elfs/empty_rodata.so
Binary file not shown.
Binary file modified tests/elfs/multiple_file.so
Binary file not shown.
Binary file modified tests/elfs/noop.so
Binary file not shown.
Binary file modified tests/elfs/noro.so
Binary file not shown.
Binary file modified tests/elfs/pass_stack_reference.so
Binary file not shown.
Binary file modified tests/elfs/program_headers_overflow.so
Binary file not shown.
Binary file modified tests/elfs/relative_call.so
Binary file not shown.
Binary file modified tests/elfs/reloc_64_64.so
Binary file not shown.
Binary file removed tests/elfs/reloc_64_64_high_vaddr.so
Binary file not shown.
Binary file added tests/elfs/reloc_64_64_sbpfv1.so
Binary file not shown.
Binary file modified tests/elfs/reloc_64_relative.so
Binary file not shown.
Binary file modified tests/elfs/reloc_64_relative_data.so
Binary file not shown.
Binary file removed tests/elfs/reloc_64_relative_data_high_vaddr.so
Binary file not shown.
Binary file not shown.
Binary file removed tests/elfs/reloc_64_relative_high_vaddr.so
Binary file not shown.
Binary file added tests/elfs/reloc_64_relative_sbpfv1.so
Binary file not shown.
Binary file modified tests/elfs/rodata.so
Binary file not shown.
Binary file removed tests/elfs/rodata_high_vaddr.so
Binary file not shown.
Binary file modified tests/elfs/scratch_registers.so
Binary file not shown.
Binary file modified tests/elfs/scratch_registers_debug.so
Binary file not shown.
Binary file modified tests/elfs/struct_func_pointer.so
Binary file not shown.
Binary file modified tests/elfs/syscall_static.so
Binary file not shown.
Binary file modified tests/elfs/syscall_static_unknown.so
Binary file not shown.
Binary file modified tests/elfs/unresolved_syscall.so
Binary file not shown.
Binary file modified tests/elfs/writable_data_section.so
Binary file not shown.
44 changes: 17 additions & 27 deletions tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3097,7 +3097,7 @@ fn test_load_elf_empty_rodata() {
}

#[test]
fn test_load_elf_rodata_sbpfv2() {
fn test_load_elf_rodata() {
let config = Config::default();
test_interpreter_and_jit_elf!(
"tests/elfs/rodata.so",
Expand All @@ -3122,17 +3122,6 @@ fn test_load_elf_rodata_sbpfv1() {
);
}

#[test]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this test pointless now?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep this is the default behaviour tested by test_load_elf_rodata now. And I added test_load_elf_rodata_sbpfv1 that checks the same without virtual addresses.

fn test_load_elf_rodata_high_vaddr() {
test_interpreter_and_jit_elf!(
"tests/elfs/rodata_high_vaddr.so",
[1],
(),
TestContextObject::new(3),
ProgramResult::Ok(42),
);
}

// Instruction Meter Limit

#[test]
Expand Down Expand Up @@ -3436,7 +3425,7 @@ fn test_err_unresolved_elf() {
file.read_to_end(&mut elf).unwrap();
assert_error!(
Executable::<TautologyVerifier, TestContextObject>::from_elf(&elf, Arc::new(loader)),
"UnresolvedSymbol(\"log_64\", 67, 304)"
"UnresolvedSymbol(\"log_64\", 550, 4168)"
);
}

Expand Down Expand Up @@ -3471,12 +3460,12 @@ fn test_syscall_unknown_static() {
}

#[test]
fn test_reloc_64_64() {
fn test_reloc_64_64_sbpfv1() {
// Tests the correctness of R_BPF_64_64 relocations. The program returns the
// address of the entrypoint.
// [ 1] .text PROGBITS 00000000000000e8 0000e8 000018 00 AX 0 0 8
test_interpreter_and_jit_elf!(
"tests/elfs/reloc_64_64.so",
"tests/elfs/reloc_64_64_sbpfv1.so",
[],
(),
TestContextObject::new(2),
Expand All @@ -3485,11 +3474,12 @@ fn test_reloc_64_64() {
}

#[test]
fn test_reloc_64_64_high_vaddr() {
fn test_reloc_64_64() {
// Same as test_reloc_64_64, but with .text already alinged to
// MM_PROGRAM_START by the linker
// [ 1] .text PROGBITS 0000000100000000 001000 000018 00 AX 0 0 8
test_interpreter_and_jit_elf!(
"tests/elfs/reloc_64_64_high_vaddr.so",
"tests/elfs/reloc_64_64.so",
[],
(),
TestContextObject::new(2),
Expand All @@ -3498,13 +3488,13 @@ fn test_reloc_64_64_high_vaddr() {
}

#[test]
fn test_reloc_64_relative() {
fn test_reloc_64_relative_sbpfv1() {
// Tests the correctness of R_BPF_64_RELATIVE relocations. The program
// returns the address of the first .rodata byte.
// [ 1] .text PROGBITS 00000000000000e8 0000e8 000018 00 AX 0 0 8
// [ 2] .rodata PROGBITS 0000000000000100 000100 00000b 01 AMS 0 0 1
test_interpreter_and_jit_elf!(
"tests/elfs/reloc_64_relative.so",
"tests/elfs/reloc_64_relative_sbpfv1.so",
[],
(),
TestContextObject::new(2),
Expand All @@ -3513,13 +3503,13 @@ fn test_reloc_64_relative() {
}

#[test]
fn test_reloc_64_relative_high_vaddr() {
fn test_reloc_64_relative() {
// Same as test_reloc_64_relative, but with .text placed already within
// MM_PROGRAM_START by the linker
// [ 1] .text PROGBITS 0000000100000000 001000 000018 00 AX 0 0 8
// [ 2] .rodata PROGBITS 0000000100000018 001018 00000b 01 AMS 0 0 1
test_interpreter_and_jit_elf!(
"tests/elfs/reloc_64_relative_high_vaddr.so",
"tests/elfs/reloc_64_relative.so",
[],
(),
TestContextObject::new(2),
Expand All @@ -3528,7 +3518,7 @@ fn test_reloc_64_relative_high_vaddr() {
}

#[test]
fn test_reloc_64_relative_data() {
fn test_reloc_64_relative_data_sbfv1() {
// Tests the correctness of R_BPF_64_RELATIVE relocations in sections other
// than .text. The program returns the address of the first .rodata byte.
// [ 1] .text PROGBITS 00000000000000e8 0000e8 000020 00 AX 0 0 8
Expand All @@ -3537,7 +3527,7 @@ fn test_reloc_64_relative_data() {
// 00000000000001f8 <FILE>:
// 63: 08 01 00 00 00 00 00 00
test_interpreter_and_jit_elf!(
"tests/elfs/reloc_64_relative_data.so",
"tests/elfs/reloc_64_relative_data_sbpfv1.so",
[],
(),
TestContextObject::new(3),
Expand All @@ -3546,7 +3536,7 @@ fn test_reloc_64_relative_data() {
}

#[test]
fn test_reloc_64_relative_data_high_vaddr() {
fn test_reloc_64_relative_data() {
// Same as test_reloc_64_relative_data, but with rodata already placed
// within MM_PROGRAM_START by the linker
// [ 1] .text PROGBITS 0000000100000000 001000 000020 00 AX 0 0 8
Expand All @@ -3555,7 +3545,7 @@ fn test_reloc_64_relative_data_high_vaddr() {
// 0000000100000110 <FILE>:
// 536870946: 20 00 00 00 01 00 00 00
test_interpreter_and_jit_elf!(
"tests/elfs/reloc_64_relative_data_high_vaddr.so",
"tests/elfs/reloc_64_relative_data.so",
[],
(),
TestContextObject::new(3),
Expand All @@ -3564,7 +3554,7 @@ fn test_reloc_64_relative_data_high_vaddr() {
}

#[test]
fn test_reloc_64_relative_data_pre_sbfv2() {
fn test_reloc_64_relative_data_sbpfv1() {
// Before https://github.com/solana-labs/llvm-project/pull/35, we used to
// generate invalid R_BPF_64_RELATIVE relocations in sections other than
// .text.
Expand All @@ -3579,7 +3569,7 @@ fn test_reloc_64_relative_data_pre_sbfv2() {
// 00000000000001f8 <FILE>:
// 63: 00 00 00 00 08 01 00 00
test_interpreter_and_jit_elf!(
"tests/elfs/reloc_64_relative_data_pre_sbfv2.so",
"tests/elfs/reloc_64_relative_data_sbpfv1.so",
[],
(),
TestContextObject::new(3),
Expand Down