Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests to invalid dynamic jumps #145

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
84 changes: 84 additions & 0 deletions crates/polkavm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,88 @@ impl TestInstance {
}
}

fn blob_null_pointer_djump() -> ProgramBlob {
alk0x1 marked this conversation as resolved.
Show resolved Hide resolved
let mut builder = ProgramBlobBuilder::new();
builder.set_rw_data_size(0x4000);
alk0x1 marked this conversation as resolved.
Show resolved Hide resolved
builder.add_export_by_basic_block(0, b"main");
builder.set_code(
&[
asm::jump_indirect(Reg::A0, 0x0),
],
&[0x0],
alk0x1 marked this conversation as resolved.
Show resolved Hide resolved
);
ProgramBlob::parse(builder.into_vec().into()).unwrap()
}

fn blob_miss_aligned_pointer_djump() -> ProgramBlob {
let mut builder = ProgramBlobBuilder::new();
builder.set_rw_data_size(0x4000);
builder.add_export_by_basic_block(0, b"main");
builder.set_code(
&[
asm::load_imm(Reg::A0, 0x1000),
asm::jump_indirect(Reg::A0, 1)
alk0x1 marked this conversation as resolved.
Show resolved Hide resolved
],
&[1],
);

ProgramBlob::parse(builder.into_vec().into()).unwrap()
}

fn setup_instance(blob: ProgramBlob) -> crate::Instance<()>{
let _ = env_logger::try_init();

let mut config = crate::Config::default();
config.set_backend(Some(crate::BackendKind::Compiler));
config.set_sandbox(Some(crate::SandboxKind::Linux));

let engine = Engine::new(&config).unwrap();
let linker: Linker<()> = Linker::new(&engine);

let module = Module::from_blob(&engine, &Default::default(), blob).unwrap();
let instance_pre = linker.instantiate_pre(&module).unwrap();

instance_pre.instantiate().unwrap()
}

#[test]
#[ignore]
fn test_invalid_jump_trap() {
alk0x1 marked this conversation as resolved.
Show resolved Hide resolved
let state_args1 = StateArgs::default();
let state_args2 = StateArgs::default();

let null_pointer_instance = setup_instance(blob_null_pointer_djump());
let miss_aligned_pointer_instance = setup_instance(blob_miss_aligned_pointer_djump());

let ext_main1 = null_pointer_instance.module().lookup_export("main").unwrap();
let result1 = null_pointer_instance.call(state_args1, CallArgs::new(&mut (), ext_main1));

let ext_main2 = miss_aligned_pointer_instance.module().lookup_export("main").unwrap();
let result2 = miss_aligned_pointer_instance.call(state_args2, CallArgs::new(&mut (), ext_main2));

match result1 {
Ok(()) => panic!("Expected ExecutionError::Trap, but got Ok"),
Err(e) => {
if matches!(e, ExecutionError::Error(..)) {
panic!("Unexpected ExecutionError::Error: {:?}", e);
} else {
assert!(matches!(e, ExecutionError::Trap(..)), "Expected ExecutionError::Trap, but got {:?}", e);
}
}
}

match result2 {
Ok(()) => panic!("Expected ExecutionError::Trap, but got Ok"),
Err(e) => {
if matches!(e, ExecutionError::Error(..)) {
panic!("Unexpected ExecutionError::Error: {:?}", e);
} else {
assert!(matches!(e, ExecutionError::Trap(..)), "Expected ExecutionError::Trap, but got {:?}", e);
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

More idiomatic Rust:

if let Err(e) == instance.call(state_args, CallArgs::new(&mut (), ext_main)) {
    assert!(matches!(e, ExecutionError::Trap(_)), "Expected ExecutionError::Trap, but got {:?}", e);
} else {
    panic!("Expected ExecutionError::Trap, but got Ok");
}

}

fn test_blob_basic_test(config: Config) {
let i = TestInstance::new(&config);
assert_eq!(i.call::<(), u32>("push_one_to_global_vec", ()).unwrap(), 1);
Expand Down Expand Up @@ -956,6 +1038,8 @@ run_tests! {
doom_o3_dwarf2
pinky

// test_invalid_jump_trap

test_blob_basic_test
test_blob_atomic_fetch_add
test_blob_atomic_fetch_swap
Expand Down