Skip to content

Commit

Permalink
Changes the default text_section_vaddr.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Oct 15, 2024
1 parent eda17c6 commit 5fc2bdd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 54 deletions.
7 changes: 6 additions & 1 deletion src/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ fn get_host_ptr<C: ContextObject>(
interpreter: &mut Interpreter<C>,
mut vm_addr: u64,
) -> Result<*mut u8, EbpfError> {
if vm_addr < ebpf::MM_RODATA_START {
if !interpreter
.executable
.get_sbpf_version()
.enable_lower_bytecode_vaddr()
&& vm_addr < ebpf::MM_RODATA_START
{
vm_addr += ebpf::MM_RODATA_START;
}
match interpreter.vm.memory_mapping.map(
Expand Down
6 changes: 5 additions & 1 deletion src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,11 @@ impl<C: ContextObject> Executable<C> {
elf_bytes,
sbpf_version,
ro_section: Section::Borrowed(ebpf::MM_RODATA_START as usize, 0..text_bytes.len()),
text_section_vaddr: ebpf::MM_RODATA_START,
text_section_vaddr: if sbpf_version.enable_lower_bytecode_vaddr() {
ebpf::MM_BYTECODE_START
} else {
ebpf::MM_RODATA_START
},
text_section_range: 0..text_bytes.len(),
entry_pc,
function_registry,
Expand Down
8 changes: 6 additions & 2 deletions src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ impl SBPFVersion {
self != &SBPFVersion::V1
}

/// Allow sh_addr != sh_offset in elf sections. Used in V2 to align
/// section vaddrs to MM_RODATA_START.
/// Allow sh_addr != sh_offset in elf sections.
pub fn enable_elf_vaddr(&self) -> bool {
self != &SBPFVersion::V1
}

/// Separates the bytecode from the read only data in virtual address space
pub fn enable_lower_bytecode_vaddr(&self) -> bool {
self != &SBPFVersion::V1
}

/// Use dynamic stack frame sizes
pub fn dynamic_stack_frames(&self) -> bool {
self != &SBPFVersion::V1
Expand Down
71 changes: 21 additions & 50 deletions tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2389,17 +2389,15 @@ fn test_callx() {
test_interpreter_and_jit_asm!(
"
mov64 r0, 0x0
mov64 r8, 0x1
lsh64 r8, 0x20
or64 r8, 0x30
or64 r8, 0x20
callx r8
exit
function_foo:
mov64 r0, 0x2A
exit",
[],
(),
TestContextObject::new(8),
TestContextObject::new(6),
ProgramResult::Ok(42),
);
}
Expand All @@ -2409,27 +2407,30 @@ fn test_err_callx_unregistered() {
test_interpreter_and_jit_asm!(
"
mov64 r0, 0x0
mov64 r8, 0x1
lsh64 r8, 0x20
or64 r8, 0x30
or64 r8, 0x20
callx r8
exit
mov64 r0, 0x2A
exit",
[],
(),
TestContextObject::new(6),
TestContextObject::new(4),
ProgramResult::Err(EbpfError::UnsupportedInstruction),
);
}

#[test]
fn test_err_callx_oob_low() {
let config = Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};
test_interpreter_and_jit_asm!(
"
mov64 r0, 0x3
callx r0
exit",
config,
[],
(),
TestContextObject::new(2),
Expand Down Expand Up @@ -2518,14 +2519,12 @@ fn test_err_reg_stack_depth() {
};
test_interpreter_and_jit_asm!(
"
mov64 r0, 0x1
lsh64 r0, 0x20
callx r0
exit",
config,
[],
(),
TestContextObject::new(max_call_depth as u64 * 3),
TestContextObject::new(max_call_depth as u64),
ProgramResult::Err(EbpfError::CallDepthExceeded),
);
}
Expand Down Expand Up @@ -2765,9 +2764,7 @@ fn test_tight_infinite_recursion() {
fn test_tight_infinite_recursion_callx() {
test_interpreter_and_jit_asm!(
"
mov64 r8, 0x1
lsh64 r8, 0x20
or64 r8, 0x28
or64 r8, 0x18
call function_foo
exit
function_foo:
Expand All @@ -2776,7 +2773,7 @@ fn test_tight_infinite_recursion_callx() {
exit",
[],
(),
TestContextObject::new(8),
TestContextObject::new(6),
ProgramResult::Err(EbpfError::ExceededMaxInstructions),
);
}
Expand Down Expand Up @@ -2815,27 +2812,6 @@ fn test_err_instruction_count_syscall_capped() {
);
}

#[test]
fn test_non_terminate_early() {
test_interpreter_and_jit_asm!(
"
mov64 r6, 0x0
mov64 r1, 0x0
mov64 r2, 0x0
mov64 r3, 0x0
mov64 r4, 0x0
mov64 r5, r6
callx r6
add64 r6, 0x1
ja -0x8
exit",
[],
(),
TestContextObject::new(7),
ProgramResult::Err(EbpfError::CallOutsideTextSegment),
);
}

#[test]
fn test_err_non_terminate_capped() {
test_interpreter_and_jit_asm!(
Expand Down Expand Up @@ -2896,7 +2872,7 @@ fn test_err_capped_before_exception() {
test_interpreter_and_jit_asm!(
"
mov64 r1, 0x0
hor64 r2, 0x1
mov64 r2, 0x0
callx r2
mov64 r0, 0x0
exit",
Expand All @@ -2911,33 +2887,29 @@ fn test_err_capped_before_exception() {
fn test_err_exit_capped() {
test_interpreter_and_jit_asm!(
"
mov64 r1, 0x1
lsh64 r1, 0x20
or64 r1, 0x28
callx r1
or64 r0, 0x18
callx r0
exit
function_foo:
exit
",
[],
(),
TestContextObject::new(5),
TestContextObject::new(3),
ProgramResult::Err(EbpfError::ExceededMaxInstructions),
);
test_interpreter_and_jit_asm!(
"
mov64 r1, 0x1
lsh64 r1, 0x20
or64 r1, 0x28
callx r1
or64 r0, 0x18
callx r0
exit
function_foo:
mov r0, r0
exit
",
[],
(),
TestContextObject::new(6),
TestContextObject::new(4),
ProgramResult::Err(EbpfError::ExceededMaxInstructions),
);
test_interpreter_and_jit_asm!(
Expand Down Expand Up @@ -2966,13 +2938,12 @@ fn test_far_jumps() {
.fill 1024, 0x0F
exit
function_c:
mov32 r1, 0x00000010
hor64 r1, 0x00000001
mov32 r1, 0x10
callx r1
exit",
[],
(),
TestContextObject::new(7),
TestContextObject::new(6),
ProgramResult::Ok(0),
);
}
Expand Down

0 comments on commit 5fc2bdd

Please sign in to comment.