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
49 changes: 49 additions & 0 deletions crates/polkavm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,53 @@ impl TestInstance {
}
}



fn test_invalid_jump_trap(config: Config) {
// TODO: This test is currently failing as expected. Once the implementation is complete, remove this early return
return;

let _ = config;
let state_args1 = StateArgs::default();

let mut builder = ProgramBlobBuilder::new();
builder.add_export_by_basic_block(0, b"main");
builder.set_code(
&[
asm::jump_indirect(Reg::A0, 0x0),
],
&[],
);

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(), ProgramBlob::parse(builder.into_vec().into()).unwrap()).unwrap();
let instance_pre = linker.instantiate_pre(&module).unwrap();


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

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);
}
}
}
Copy link
Collaborator

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 +1003,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
Loading