Skip to content

Commit 11ad166

Browse files
committed
tests for ImmBorrowLoc and MutBorrowLoc instructions
1 parent d4fd2ee commit 11ad166

File tree

1 file changed

+69
-1
lines changed
  • crates/move-bytecode-verifier/src/type_safety_tests

1 file changed

+69
-1
lines changed

crates/move-bytecode-verifier/src/type_safety_tests/mod.rs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use move_core_types::{
99
use move_binary_format::{
1010
CompiledModule,
1111
file_format::{
12-
ConstantPoolIndex, Constant, SignatureToken, AbilitySet, StructHandle, TypeSignature, FieldDefinition, StructHandleIndex, StructFieldInformation, StructDefinition
12+
ConstantPoolIndex, Constant, SignatureToken, AbilitySet, StructHandle, TypeSignature, FieldDefinition, StructHandleIndex, StructFieldInformation, StructDefinition, Signature
1313
},
1414
};
1515

@@ -45,6 +45,37 @@ fn make_module(code: Vec<Bytecode>) -> CompiledModule {
4545
module
4646
}
4747

48+
49+
fn make_module_with_local(code: Vec<Bytecode>, signature: SignatureToken) -> CompiledModule {
50+
let code_unit = CodeUnit {
51+
code,
52+
locals: SignatureIndex(0),
53+
};
54+
55+
let fun_def = FunctionDefinition {
56+
code: Some(code_unit.clone()),
57+
..Default::default()
58+
};
59+
60+
let fun_handle = FunctionHandle {
61+
module: ModuleHandleIndex(0),
62+
name: IdentifierIndex(0),
63+
parameters: SignatureIndex(0),
64+
return_: SignatureIndex(0),
65+
type_parameters: vec![],
66+
};
67+
68+
let mut module = empty_module();
69+
module.function_handles.push(fun_handle);
70+
module.function_defs.push(fun_def);
71+
module.signatures = vec![
72+
Signature(vec![signature]),
73+
];
74+
75+
module
76+
}
77+
78+
4879
fn add_simple_struct(module: &mut CompiledModule) {
4980
let struct_def = StructDefinition {
5081
struct_handle: StructHandleIndex(0),
@@ -927,3 +958,40 @@ fn test_pop_no_arg() {
927958
let fun_context = get_fun_context(&module);
928959
let _result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
929960
}
961+
962+
963+
#[test]
964+
fn test_borrow_loc_ok() {
965+
for instr in vec![
966+
Bytecode::ImmBorrowLoc(1),
967+
Bytecode::MutBorrowLoc(0),
968+
] {
969+
let code = vec![instr];
970+
let module = make_module_with_local(code, SignatureToken::U64);
971+
let fun_context = get_fun_context(&module);
972+
let result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
973+
assert!(result.is_ok());
974+
}
975+
}
976+
977+
#[test]
978+
fn test_borrow_loc_reference() {
979+
for instr in vec![
980+
Bytecode::ImmBorrowLoc(1),
981+
Bytecode::MutBorrowLoc(0),
982+
] {
983+
for reference in vec![
984+
SignatureToken::Reference(Box::new(SignatureToken::U64)),
985+
SignatureToken::MutableReference(Box::new(SignatureToken::U32)),
986+
] {
987+
let code = vec![instr.clone()];
988+
let module = make_module_with_local(code, reference.clone());
989+
let fun_context = get_fun_context(&module);
990+
let result = type_safety::verify(&module, &fun_context, &mut DummyMeter);
991+
assert_eq!(
992+
result.unwrap_err().major_status(),
993+
StatusCode::BORROWLOC_REFERENCE_ERROR
994+
);
995+
}
996+
}
997+
}

0 commit comments

Comments
 (0)