@@ -9,7 +9,7 @@ use move_core_types::{
99use move_binary_format:: {
1010 CompiledModule ,
1111 file_format:: {
12- ConstantPoolIndex , Constant , SignatureToken , AbilitySet , StructHandle , TypeSignature , FieldDefinition , StructHandleIndex , StructFieldInformation , StructDefinition , Signature
12+ ConstantPoolIndex , Constant , SignatureToken , AbilitySet , StructHandle , TypeSignature , FieldDefinition , StructHandleIndex , StructFieldInformation , StructDefinition , Signature , FieldHandleIndex , FieldHandle
1313 } ,
1414} ;
1515
@@ -76,6 +76,30 @@ fn make_module_with_local(code: Vec<Bytecode>, signature: SignatureToken) -> Com
7676}
7777
7878
79+ fn add_native_struct ( module : & mut CompiledModule ) {
80+ let struct_def = StructDefinition {
81+ struct_handle : StructHandleIndex ( 0 ) ,
82+ field_information : StructFieldInformation :: Native ,
83+ } ;
84+
85+ let struct_handle = StructHandle {
86+ module : ModuleHandleIndex ( 0 ) ,
87+ name : IdentifierIndex ( 0 ) ,
88+ abilities : AbilitySet :: EMPTY ,
89+ type_parameters : vec ! [ ] ,
90+ } ;
91+
92+ module. struct_defs . push ( struct_def) ;
93+ module. struct_handles . push ( struct_handle) ;
94+
95+ module. field_handles = vec ! [
96+ FieldHandle {
97+ owner: StructDefinitionIndex ( 0 ) ,
98+ field: 0 ,
99+ } ,
100+ ] ;
101+ }
102+
79103fn add_simple_struct ( module : & mut CompiledModule ) {
80104 let struct_def = StructDefinition {
81105 struct_handle : StructHandleIndex ( 0 ) ,
@@ -100,8 +124,20 @@ fn add_simple_struct(module: &mut CompiledModule) {
100124
101125 module. struct_defs . push ( struct_def) ;
102126 module. struct_handles . push ( struct_handle) ;
127+
128+ module. field_handles = vec ! [
129+ FieldHandle {
130+ owner: StructDefinitionIndex ( 0 ) ,
131+ field: 0 ,
132+ } ,
133+ FieldHandle {
134+ owner: StructDefinitionIndex ( 0 ) ,
135+ field: 1 ,
136+ }
137+ ] ;
103138}
104139
140+
105141fn add_simple_struct_with_abilities ( module : & mut CompiledModule , abilities : AbilitySet ) {
106142 let struct_def = StructDefinition {
107143 struct_handle : StructHandleIndex ( 0 ) ,
@@ -1236,3 +1272,64 @@ fn test_write_ref_no_args() {
12361272 let fun_context = get_fun_context ( & module) ;
12371273 let _result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
12381274}
1275+
1276+
1277+ #[ test]
1278+ fn test_imm_borrow_field_correct_type ( ) {
1279+ let code = vec ! [ Bytecode :: ImmBorrowLoc ( 0 ) , Bytecode :: ImmBorrowField ( FieldHandleIndex ( 0 ) ) ] ;
1280+ let mut module = make_module_with_local ( code, SignatureToken :: Struct ( StructHandleIndex ( 0 ) ) ) ;
1281+ add_simple_struct ( & mut module) ;
1282+ let fun_context = get_fun_context ( & module) ;
1283+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
1284+ assert ! ( result. is_ok( ) ) ;
1285+ }
1286+
1287+ #[ test]
1288+ fn test_imm_borrow_field_wrong_type ( ) {
1289+ let code = vec ! [ Bytecode :: LdTrue , Bytecode :: ImmBorrowField ( FieldHandleIndex ( 0 ) ) ] ;
1290+ let mut module = make_module_with_local ( code, SignatureToken :: Struct ( StructHandleIndex ( 0 ) ) ) ;
1291+ add_simple_struct ( & mut module) ;
1292+ let fun_context = get_fun_context ( & module) ;
1293+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
1294+ assert_eq ! (
1295+ result. unwrap_err( ) . major_status( ) ,
1296+ StatusCode :: BORROWFIELD_TYPE_MISMATCH_ERROR
1297+ ) ;
1298+ }
1299+
1300+ #[ test]
1301+ fn test_imm_borrow_field_mismatched_types ( ) {
1302+ let code = vec ! [ Bytecode :: ImmBorrowLoc ( 0 ) , Bytecode :: ImmBorrowField ( FieldHandleIndex ( 0 ) ) ] ;
1303+ let mut module = make_module_with_local ( code, SignatureToken :: U64 ) ;
1304+ add_simple_struct ( & mut module) ;
1305+ let fun_context = get_fun_context ( & module) ;
1306+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
1307+ assert_eq ! (
1308+ result. unwrap_err( ) . major_status( ) ,
1309+ StatusCode :: BORROWFIELD_TYPE_MISMATCH_ERROR
1310+ ) ;
1311+ }
1312+
1313+ #[ test]
1314+ fn test_imm_borrow_field_bad_field ( ) {
1315+ let code = vec ! [ Bytecode :: ImmBorrowLoc ( 0 ) , Bytecode :: ImmBorrowField ( FieldHandleIndex ( 0 ) ) ] ;
1316+ let mut module = make_module_with_local ( code, SignatureToken :: Struct ( StructHandleIndex ( 0 ) ) ) ;
1317+ add_native_struct ( & mut module) ;
1318+ let fun_context = get_fun_context ( & module) ;
1319+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
1320+ assert_eq ! (
1321+ result. unwrap_err( ) . major_status( ) ,
1322+ StatusCode :: BORROWFIELD_BAD_FIELD_ERROR
1323+ ) ;
1324+ }
1325+
1326+ #[ test]
1327+ #[ should_panic]
1328+ fn test_imm_borrow_field_no_arg ( ) {
1329+ let code = vec ! [ Bytecode :: ImmBorrowField ( FieldHandleIndex ( 0 ) ) ] ;
1330+ let mut module = make_module_with_local ( code, SignatureToken :: Struct ( StructHandleIndex ( 0 ) ) ) ;
1331+ add_simple_struct ( & mut module) ;
1332+ let fun_context = get_fun_context ( & module) ;
1333+ let _result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
1334+ }
1335+
0 commit comments