|
| 1 | +use move_binary_format::file_format::{ |
| 2 | + Bytecode, CodeUnit, FunctionDefinition, FunctionHandle, |
| 3 | + IdentifierIndex, ModuleHandleIndex, SignatureIndex, |
| 4 | + FunctionDefinitionIndex, empty_module |
| 5 | +}; |
| 6 | + |
| 7 | +use move_core_types::vm_status::StatusCode; |
| 8 | +use move_binary_format::CompiledModule; |
| 9 | +use move_bytecode_verifier_meter::dummy::DummyMeter; |
| 10 | +use crate::absint::FunctionContext; |
| 11 | +use crate::type_safety; |
| 12 | + |
| 13 | + |
| 14 | +fn make_module(code: Vec<Bytecode>) -> CompiledModule { |
| 15 | + let code_unit = CodeUnit { |
| 16 | + code, |
| 17 | + ..Default::default() |
| 18 | + }; |
| 19 | + |
| 20 | + let fun_def = FunctionDefinition { |
| 21 | + code: Some(code_unit.clone()), |
| 22 | + ..Default::default() |
| 23 | + }; |
| 24 | + |
| 25 | + let fun_handle = FunctionHandle { |
| 26 | + module: ModuleHandleIndex(0), |
| 27 | + name: IdentifierIndex(0), |
| 28 | + parameters: SignatureIndex(0), |
| 29 | + return_: SignatureIndex(0), |
| 30 | + type_parameters: vec![], |
| 31 | + }; |
| 32 | + |
| 33 | + let mut module = empty_module(); |
| 34 | + module.function_handles.push(fun_handle); |
| 35 | + module.function_defs.push(fun_def); |
| 36 | + |
| 37 | + module |
| 38 | +} |
| 39 | + |
| 40 | +fn get_fun_context(module: &CompiledModule) -> FunctionContext { |
| 41 | + FunctionContext::new( |
| 42 | + &module, |
| 43 | + FunctionDefinitionIndex(0), |
| 44 | + module.function_defs[0].code.as_ref().unwrap(), |
| 45 | + &module.function_handles[0], |
| 46 | + ) |
| 47 | +} |
| 48 | + |
| 49 | +#[test] |
| 50 | +fn test_br_true_bool() { |
| 51 | + let code = vec![Bytecode::LdTrue, Bytecode::BrTrue(0)]; |
| 52 | + let module = make_module(code); |
| 53 | + let fun_context = get_fun_context(&module); |
| 54 | + let result = type_safety::verify(&module, &fun_context, &mut DummyMeter); |
| 55 | + assert!(result.is_ok()); |
| 56 | +} |
| 57 | + |
| 58 | +#[test] |
| 59 | +fn test_br_true_u32() { |
| 60 | + let code = vec![Bytecode::LdU32(0), Bytecode::BrTrue(0)]; |
| 61 | + let module = make_module(code); |
| 62 | + let fun_context = get_fun_context(&module); |
| 63 | + let result = type_safety::verify(&module, &fun_context, &mut DummyMeter); |
| 64 | + assert_eq!( |
| 65 | + result.unwrap_err().major_status(), |
| 66 | + StatusCode::BR_TYPE_MISMATCH_ERROR |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +#[test] |
| 71 | +#[should_panic] |
| 72 | +fn test_br_true_no_arg() { |
| 73 | + let code = vec![Bytecode::BrTrue(0)]; |
| 74 | + let module = make_module(code); |
| 75 | + let fun_context = get_fun_context(&module); |
| 76 | + let _result = type_safety::verify(&module, &fun_context, &mut DummyMeter); |
| 77 | +} |
0 commit comments