@@ -9,7 +9,7 @@ use move_core_types::{
9
9
use move_binary_format:: {
10
10
CompiledModule ,
11
11
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
13
13
} ,
14
14
} ;
15
15
@@ -45,6 +45,37 @@ fn make_module(code: Vec<Bytecode>) -> CompiledModule {
45
45
module
46
46
}
47
47
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
+
48
79
fn add_simple_struct ( module : & mut CompiledModule ) {
49
80
let struct_def = StructDefinition {
50
81
struct_handle : StructHandleIndex ( 0 ) ,
@@ -927,3 +958,40 @@ fn test_pop_no_arg() {
927
958
let fun_context = get_fun_context ( & module) ;
928
959
let _result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
929
960
}
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