|
| 1 | +// Copyright (c) The Move Contributors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use crate::unit_tests::production_config; |
| 5 | +use move_binary_format::file_format::{ |
| 6 | + empty_module, Bytecode, CodeUnit, FunctionDefinition, FunctionHandle, FunctionHandleIndex, |
| 7 | + IdentifierIndex, ModuleHandleIndex, Signature, SignatureIndex, SignatureToken, |
| 8 | + Visibility::Public, |
| 9 | +}; |
| 10 | +use move_core_types::{identifier::Identifier, vm_status::StatusCode}; |
| 11 | + |
| 12 | +const NUM_LOCALS: u8 = 64; |
| 13 | +const NUM_CALLS: u16 = 77; |
| 14 | +const NUM_FUNCTIONS: u16 = 177; |
| 15 | + |
| 16 | +fn get_nested_vec_type(len: usize) -> SignatureToken { |
| 17 | + let mut ret = SignatureToken::Bool; |
| 18 | + for _ in 0..len { |
| 19 | + ret = SignatureToken::Vector(Box::new(ret)); |
| 20 | + } |
| 21 | + ret |
| 22 | +} |
| 23 | + |
| 24 | +#[test] |
| 25 | +fn test_large_types() { |
| 26 | + // See also: github.com/aptos-labs/aptos-core/security/advisories/GHSA-37qw-jfpw-8899 |
| 27 | + let mut m = empty_module(); |
| 28 | + |
| 29 | + m.signatures.push(Signature( |
| 30 | + std::iter::repeat(SignatureToken::Reference(Box::new(get_nested_vec_type(64)))) |
| 31 | + .take(NUM_LOCALS as usize) |
| 32 | + .collect(), |
| 33 | + )); |
| 34 | + |
| 35 | + m.function_handles.push(FunctionHandle { |
| 36 | + module: ModuleHandleIndex(0), |
| 37 | + name: IdentifierIndex(0), |
| 38 | + parameters: SignatureIndex(0), |
| 39 | + return_: SignatureIndex(0), |
| 40 | + type_parameters: vec![], |
| 41 | + }); |
| 42 | + m.function_defs.push(FunctionDefinition { |
| 43 | + function: FunctionHandleIndex(0), |
| 44 | + visibility: Public, |
| 45 | + is_entry: false, |
| 46 | + acquires_global_resources: vec![], |
| 47 | + code: Some(CodeUnit { |
| 48 | + locals: SignatureIndex(0), |
| 49 | + code: vec![Bytecode::Call(FunctionHandleIndex(0)), Bytecode::Ret], |
| 50 | + }), |
| 51 | + }); |
| 52 | + |
| 53 | + // returns_vecs |
| 54 | + m.identifiers.push(Identifier::new("returns_vecs").unwrap()); |
| 55 | + m.function_handles.push(FunctionHandle { |
| 56 | + module: ModuleHandleIndex(0), |
| 57 | + name: IdentifierIndex(1), |
| 58 | + parameters: SignatureIndex(0), |
| 59 | + return_: SignatureIndex(1), |
| 60 | + type_parameters: vec![], |
| 61 | + }); |
| 62 | + m.function_defs.push(FunctionDefinition { |
| 63 | + function: FunctionHandleIndex(1), |
| 64 | + visibility: Public, |
| 65 | + is_entry: false, |
| 66 | + acquires_global_resources: vec![], |
| 67 | + code: Some(CodeUnit { |
| 68 | + locals: SignatureIndex(0), |
| 69 | + code: vec![Bytecode::Call(FunctionHandleIndex(1)), Bytecode::Ret], |
| 70 | + }), |
| 71 | + }); |
| 72 | + |
| 73 | + // takes_and_returns_vecs |
| 74 | + m.identifiers |
| 75 | + .push(Identifier::new("takes_and_returns_vecs").unwrap()); |
| 76 | + m.function_handles.push(FunctionHandle { |
| 77 | + module: ModuleHandleIndex(0), |
| 78 | + name: IdentifierIndex(2), |
| 79 | + parameters: SignatureIndex(1), |
| 80 | + return_: SignatureIndex(1), |
| 81 | + type_parameters: vec![], |
| 82 | + }); |
| 83 | + m.function_defs.push(FunctionDefinition { |
| 84 | + function: FunctionHandleIndex(2), |
| 85 | + visibility: Public, |
| 86 | + is_entry: false, |
| 87 | + acquires_global_resources: vec![], |
| 88 | + code: Some(CodeUnit { |
| 89 | + locals: SignatureIndex(0), |
| 90 | + code: vec![Bytecode::Call(FunctionHandleIndex(1)), Bytecode::Ret], |
| 91 | + }), |
| 92 | + }); |
| 93 | + |
| 94 | + // takes_vecs |
| 95 | + m.identifiers.push(Identifier::new("takes_vecs").unwrap()); |
| 96 | + m.function_handles.push(FunctionHandle { |
| 97 | + module: ModuleHandleIndex(0), |
| 98 | + name: IdentifierIndex(3), |
| 99 | + parameters: SignatureIndex(1), |
| 100 | + return_: SignatureIndex(0), |
| 101 | + type_parameters: vec![], |
| 102 | + }); |
| 103 | + m.function_defs.push(FunctionDefinition { |
| 104 | + function: FunctionHandleIndex(3), |
| 105 | + visibility: Public, |
| 106 | + is_entry: false, |
| 107 | + acquires_global_resources: vec![], |
| 108 | + code: Some(CodeUnit { |
| 109 | + locals: SignatureIndex(0), |
| 110 | + code: vec![Bytecode::Ret], |
| 111 | + }), |
| 112 | + }); |
| 113 | + |
| 114 | + // other fcts |
| 115 | + for i in 0..NUM_FUNCTIONS { |
| 116 | + m.identifiers |
| 117 | + .push(Identifier::new(format!("f{}", i)).unwrap()); |
| 118 | + m.function_handles.push(FunctionHandle { |
| 119 | + module: ModuleHandleIndex(0), |
| 120 | + name: IdentifierIndex(i + 4), |
| 121 | + parameters: SignatureIndex(0), |
| 122 | + return_: SignatureIndex(0), |
| 123 | + type_parameters: vec![], |
| 124 | + }); |
| 125 | + m.function_defs.push(FunctionDefinition { |
| 126 | + function: FunctionHandleIndex(i + 4), |
| 127 | + visibility: Public, |
| 128 | + is_entry: false, |
| 129 | + acquires_global_resources: vec![], |
| 130 | + code: Some(CodeUnit { |
| 131 | + locals: SignatureIndex(0), |
| 132 | + code: vec![], |
| 133 | + }), |
| 134 | + }); |
| 135 | + |
| 136 | + let code = &mut m.function_defs[i as usize + 4].code.as_mut().unwrap().code; |
| 137 | + code.clear(); |
| 138 | + code.push(Bytecode::Call(FunctionHandleIndex(1))); |
| 139 | + for _ in 0..NUM_CALLS { |
| 140 | + code.push(Bytecode::Call(FunctionHandleIndex(2))); |
| 141 | + } |
| 142 | + code.push(Bytecode::Call(FunctionHandleIndex(3))); |
| 143 | + code.push(Bytecode::Ret); |
| 144 | + } |
| 145 | + |
| 146 | + let result = move_bytecode_verifier::verify_module_with_config_for_test( |
| 147 | + "test_large_types", |
| 148 | + &production_config(), |
| 149 | + &m, |
| 150 | + ); |
| 151 | + assert_eq!( |
| 152 | + result.unwrap_err().major_status(), |
| 153 | + StatusCode::CONSTRAINT_NOT_SATISFIED, |
| 154 | + ); |
| 155 | +} |
0 commit comments