From 68b00cdf45551ba64c4f7217963055fc5a1a0cc4 Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Fri, 30 May 2025 16:39:32 -0400 Subject: [Rust] Express expression vs instruction index for MLIL and HLIL APIs This was done to fix unintended behavior using an instruction or expression index in place of the other. Fixes https://github.com/Vector35/binaryninja-api/issues/6897 --- rust/tests/medium_level_il.rs | 48 +++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 11 deletions(-) (limited to 'rust/tests/medium_level_il.rs') diff --git a/rust/tests/medium_level_il.rs b/rust/tests/medium_level_il.rs index b8c1c108..7d4d8af1 100644 --- a/rust/tests/medium_level_il.rs +++ b/rust/tests/medium_level_il.rs @@ -1,7 +1,8 @@ use binaryninja::binary_view::BinaryViewExt; use binaryninja::headless::Session; use binaryninja::medium_level_il::{ - MediumLevelILInstructionKind, MediumLevelILLiftedInstructionKind, MediumLevelInstructionIndex, + MediumLevelExpressionIndex, MediumLevelILInstructionKind, MediumLevelILLiftedInstructionKind, + MediumLevelInstructionIndex, }; use std::path::PathBuf; @@ -21,50 +22,54 @@ fn test_mlil_info() { // 0 @ 00025f10 (MLIL_SET_VAR.d edi_1 = (MLIL_VAR.d edi)) let instr_0 = mlil_instr_iter.next().unwrap(); - assert_eq!(instr_0.expr_index, MediumLevelInstructionIndex(1)); + assert_eq!(instr_0.instr_index, MediumLevelInstructionIndex(0)); + assert_eq!(instr_0.expr_index, MediumLevelExpressionIndex(1)); assert_eq!(instr_0.address, image_base + 0x00025f10); println!("{:?}", instr_0.kind); match instr_0.kind { MediumLevelILInstructionKind::SetVar(op) => { assert_eq!(op.dest.index, 524288); - assert_eq!(op.src, 0); + assert_eq!(op.src, MediumLevelExpressionIndex(0)); } _ => panic!("Expected SetVar"), } // 1 @ 00025f15 (MLIL_SET_VAR.d eax = (MLIL_VAR.d arg1)) let instr_1 = mlil_instr_iter.next().unwrap(); - assert_eq!(instr_1.expr_index, MediumLevelInstructionIndex(3)); + assert_eq!(instr_1.instr_index, MediumLevelInstructionIndex(1)); + assert_eq!(instr_1.expr_index, MediumLevelExpressionIndex(3)); assert_eq!(instr_1.address, image_base + 0x00025f15); println!("{:?}", instr_1.kind); match instr_1.kind { MediumLevelILInstructionKind::SetVar(op) => { assert_eq!(op.dest.index, 5); - assert_eq!(op.src, 2); + assert_eq!(op.src, MediumLevelExpressionIndex(2)); } _ => panic!("Expected SetVar"), } // 2 @ 00025f18 (MLIL_SET_VAR.d var_8 = (MLIL_VAR.d eax)) let instr_2 = mlil_instr_iter.next().unwrap(); - assert_eq!(instr_2.expr_index, MediumLevelInstructionIndex(5)); + assert_eq!(instr_2.instr_index, MediumLevelInstructionIndex(2)); + assert_eq!(instr_2.expr_index, MediumLevelExpressionIndex(5)); assert_eq!(instr_2.address, image_base + 0x00025f18); println!("{:?}", instr_2.kind); match instr_2.kind { MediumLevelILInstructionKind::SetVar(op) => { assert_eq!(op.dest.index, 8); - assert_eq!(op.src, 4); + assert_eq!(op.src, MediumLevelExpressionIndex(4)); } _ => panic!("Expected SetVar"), } // 3 @ 00025f19 (MLIL_CALL eax_1 = (MLIL_CONST_PTR.d __crt_interlocked_read_32)((MLIL_VAR.d var_8))) let instr_3 = mlil_instr_iter.next().unwrap(); - assert_eq!(instr_3.expr_index, MediumLevelInstructionIndex(10)); + assert_eq!(instr_3.instr_index, MediumLevelInstructionIndex(3)); + assert_eq!(instr_3.expr_index, MediumLevelExpressionIndex(10)); assert_eq!(instr_3.address, image_base + 0x00025f19); println!("{:?}", instr_3.kind); match instr_3.kind { MediumLevelILInstructionKind::Call(op) => { assert_eq!(op.first_output, 8); assert_eq!(op.num_outputs, 1); - assert_eq!(op.dest, 7); + assert_eq!(op.dest, MediumLevelExpressionIndex(7)); assert_eq!(op.first_param, 9); assert_eq!(op.num_params, 1); } @@ -72,7 +77,7 @@ fn test_mlil_info() { } match instr_3.lift().kind { MediumLevelILLiftedInstructionKind::Call(lifted_call) => { - assert_eq!(lifted_call.dest.index, MediumLevelInstructionIndex(7)); + assert_eq!(lifted_call.dest.expr_index, MediumLevelExpressionIndex(7)); assert_eq!(lifted_call.output.len(), 1); assert_eq!(lifted_call.params.len(), 1); } @@ -80,7 +85,8 @@ fn test_mlil_info() { } // 4 @ 00025f22 (MLIL_RET return (MLIL_VAR.d eax_1)) let instr_4 = mlil_instr_iter.next().unwrap(); - assert_eq!(instr_4.expr_index, MediumLevelInstructionIndex(13)); + assert_eq!(instr_4.instr_index, MediumLevelInstructionIndex(4)); + assert_eq!(instr_4.expr_index, MediumLevelExpressionIndex(13)); assert_eq!(instr_4.address, image_base + 0x00025f22); println!("{:?}", instr_4.kind); match instr_4.kind { @@ -91,3 +97,23 @@ fn test_mlil_info() { _ => panic!("Expected Ret"), } } + +#[test] +fn test_mlil_basic_blocks() { + let _session = Session::new().expect("Failed to initialize session"); + let out_dir = env!("OUT_DIR").parse::().unwrap(); + let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view"); + + // Make sure that all basic blocks are correctly associated with the instruction. + for func in &view.functions() { + let mlil_function = func.medium_level_il().expect("Failed to get MLIL"); + for mlil_basic_block in &mlil_function.basic_blocks() { + for instr in mlil_basic_block.iter() { + let instr_basic_block = instr + .basic_block() + .expect("Instruction without basic block"); + assert_eq!(instr_basic_block, mlil_basic_block.to_owned()); + } + } + } +} -- cgit v1.3.1