Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasSte committed Sep 5, 2024
1 parent e848b5b commit 27ceb7d
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ macro_rules! test_interpreter_and_jit {
.unwrap();
};
($executable:expr, $mem:tt, $context_object:expr, $expected_result:expr $(,)?) => {
test_interpreter_and_jit!(true, $executable, $mem, $context_object, $expected_result)
};
($verify:literal, $executable:expr, $mem:tt, $context_object:expr, $expected_result:expr $(,)?) => {
let expected_instruction_count = $context_object.get_remaining();
#[allow(unused_mut)]
let mut context_object = $context_object;
let expected_result = format!("{:?}", $expected_result);
if !expected_result.contains("ExceededMaxInstructions") {
context_object.remaining = INSTRUCTION_METER_BUDGET;
}
$executable.verify::<RequisiteVerifier>().unwrap();
if $verify {
$executable.verify::<RequisiteVerifier>().unwrap();
}
let (instruction_count_interpreter, interpreter_final_pc, _tracer_interpreter) = {
let mut mem = $mem;
let mem_region = MemoryRegion::new_writable(&mut mem, ebpf::MM_INPUT_START);
Expand Down Expand Up @@ -159,7 +164,7 @@ macro_rules! test_interpreter_and_jit_asm {
}

macro_rules! test_interpreter_and_jit_elf {
($source:tt, $config:tt, $mem:tt, ($($location:expr => $syscall_function:expr),* $(,)?), $context_object:expr, $expected_result:expr $(,)?) => {
($verify:literal, $source:tt, $config:tt, $mem:tt, ($($location:expr => $syscall_function:expr),* $(,)?), $context_object:expr, $expected_result:expr $(,)?) => {
let mut file = File::open($source).unwrap();
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
Expand All @@ -169,9 +174,12 @@ macro_rules! test_interpreter_and_jit_elf {
$(test_interpreter_and_jit!(register, function_registry, $location => $syscall_function);)*
let loader = Arc::new(BuiltinProgram::new_loader($config, function_registry));
let mut executable = Executable::<TestContextObject>::from_elf(&elf, loader).unwrap();
test_interpreter_and_jit!(executable, $mem, $context_object, $expected_result);
test_interpreter_and_jit!($verify, executable, $mem, $context_object, $expected_result);
}
};
($source:tt, $config:tt, $mem:tt, ($($location:expr => $syscall_function:expr),* $(,)?), $context_object:expr, $expected_result:expr $(,)?) => {
test_interpreter_and_jit_elf!(true, $source, $config, $mem, ($($location => $syscall_function),*), $context_object, $expected_result);
};
($source:tt, $mem:tt, ($($location:expr => $syscall_function:expr),* $(,)?), $context_object:expr, $expected_result:expr $(,)?) => {
let config = Config {
enable_instruction_tracing: true,
Expand Down Expand Up @@ -2684,6 +2692,11 @@ fn test_err_instruction_count_syscall_capped() {

#[test]
fn test_non_terminate_early() {
let config = Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};

test_interpreter_and_jit_asm!(
"
mov64 r6, 0x0
Expand All @@ -2696,6 +2709,7 @@ fn test_non_terminate_early() {
add64 r6, 0x1
ja -0x8
exit",
config.clone(),
[],
(),
TestContextObject::new(7),
Expand Down Expand Up @@ -2761,6 +2775,12 @@ fn test_err_capped_before_exception() {
TestContextObject::new(4),
ProgramResult::Err(EbpfError::ExceededMaxInstructions),
);

let config = Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};

test_interpreter_and_jit_asm!(
"
mov64 r1, 0x0
Expand All @@ -2770,6 +2790,7 @@ fn test_err_capped_before_exception() {
syscall Unresolved
add64 r0, 0x0
exit",
config.clone(),
[],
(),
TestContextObject::new(4),
Expand Down Expand Up @@ -2870,6 +2891,11 @@ fn test_symbol_relocation() {

#[test]
fn test_err_call_unresolved() {
let config = Config {
enabled_sbpf_versions: SBPFVersion::V1..=SBPFVersion::V1,
..Config::default()
};

test_interpreter_and_jit_asm!(
"
mov r1, 1
Expand All @@ -2880,6 +2906,7 @@ fn test_err_call_unresolved() {
syscall Unresolved
mov64 r0, 0x0
exit",
config.clone(),
[],
(),
TestContextObject::new(6),
Expand Down Expand Up @@ -2933,8 +2960,15 @@ fn test_err_unresolved_syscall_reloc_64_32() {

#[test]
fn test_err_unresolved_syscall_static() {
let config = Config {
enable_instruction_tracing: true,
..Config::default()
};
// This case only works if we skip verification.
test_interpreter_and_jit_elf!(
false,
"tests/elfs/syscall_static.so",
config,
[],
(),
TestContextObject::new(4),
Expand Down

0 comments on commit 27ceb7d

Please sign in to comment.