1
1
use move_binary_format:: file_format:: {
2
- Bytecode , CodeUnit , FunctionDefinition , FunctionHandle ,
3
- IdentifierIndex , ModuleHandleIndex , SignatureIndex ,
4
- FunctionDefinitionIndex , empty_module
2
+ empty_module, Bytecode , CodeUnit , FunctionDefinition , FunctionDefinitionIndex , FunctionHandle , IdentifierIndex , ModuleHandleIndex , SignatureIndex , StructDefinitionIndex
5
3
} ;
6
4
7
5
use move_core_types:: {
@@ -10,7 +8,9 @@ use move_core_types::{
10
8
11
9
use move_binary_format:: {
12
10
CompiledModule ,
13
- file_format:: { ConstantPoolIndex , Constant , SignatureToken } ,
11
+ file_format:: {
12
+ ConstantPoolIndex , Constant , SignatureToken , AbilitySet , StructHandle , TypeSignature , FieldDefinition , StructHandleIndex , StructFieldInformation , StructDefinition
13
+ } ,
14
14
} ;
15
15
16
16
use move_bytecode_verifier_meter:: dummy:: DummyMeter ;
@@ -45,6 +45,32 @@ fn make_module(code: Vec<Bytecode>) -> CompiledModule {
45
45
module
46
46
}
47
47
48
+ fn add_simple_struct ( module : & mut CompiledModule ) {
49
+ let struct_def = StructDefinition {
50
+ struct_handle : StructHandleIndex ( 0 ) ,
51
+ field_information : StructFieldInformation :: Declared ( vec ! [
52
+ FieldDefinition {
53
+ name: IdentifierIndex ( 5 ) ,
54
+ signature: TypeSignature ( SignatureToken :: U32 ) ,
55
+ } ,
56
+ FieldDefinition {
57
+ name: IdentifierIndex ( 6 ) ,
58
+ signature: TypeSignature ( SignatureToken :: Bool ) ,
59
+ } ,
60
+ ] ) ,
61
+ } ;
62
+
63
+ let struct_handle = StructHandle {
64
+ module : ModuleHandleIndex ( 0 ) ,
65
+ name : IdentifierIndex ( 0 ) ,
66
+ abilities : AbilitySet :: EMPTY ,
67
+ type_parameters : vec ! [ ] ,
68
+ } ;
69
+
70
+ module. struct_defs . push ( struct_def) ;
71
+ module. struct_handles . push ( struct_handle) ;
72
+ }
73
+
48
74
fn get_fun_context ( module : & CompiledModule ) -> FunctionContext {
49
75
FunctionContext :: new (
50
76
& module,
@@ -631,3 +657,37 @@ fn test_ld_const_ok() {
631
657
let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
632
658
assert ! ( result. is_ok( ) ) ;
633
659
}
660
+
661
+
662
+ #[ test]
663
+ fn test_pack_correct_types ( ) {
664
+ let code = vec ! [ Bytecode :: LdU32 ( 42 ) , Bytecode :: LdTrue , Bytecode :: Pack ( StructDefinitionIndex ( 0 ) ) ] ;
665
+ let mut module: CompiledModule = make_module ( code) ;
666
+ add_simple_struct ( & mut module) ;
667
+ let fun_context = get_fun_context ( & module) ;
668
+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
669
+ assert ! ( result. is_ok( ) ) ;
670
+ }
671
+
672
+ #[ test]
673
+ fn test_pack_mismatched_types ( ) {
674
+ let code = vec ! [ Bytecode :: LdTrue , Bytecode :: LdU32 ( 42 ) , Bytecode :: Pack ( StructDefinitionIndex ( 0 ) ) ] ;
675
+ let mut module: CompiledModule = make_module ( code) ;
676
+ add_simple_struct ( & mut module) ;
677
+ let fun_context = get_fun_context ( & module) ;
678
+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
679
+ assert_eq ! (
680
+ result. unwrap_err( ) . major_status( ) ,
681
+ StatusCode :: PACK_TYPE_MISMATCH_ERROR
682
+ ) ;
683
+ }
684
+
685
+ #[ test]
686
+ #[ should_panic]
687
+ fn test_pack_too_few_args ( ) {
688
+ let code = vec ! [ Bytecode :: LdTrue , Bytecode :: Pack ( StructDefinitionIndex ( 0 ) ) ] ;
689
+ let mut module: CompiledModule = make_module ( code) ;
690
+ add_simple_struct ( & mut module) ;
691
+ let fun_context = get_fun_context ( & module) ;
692
+ let _result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
693
+ }
0 commit comments