diff options
| author | Mason Reed <mason@vector35.com> | 2025-05-30 16:39:32 -0400 |
|---|---|---|
| committer | Mason Reed <mason@vector35.com> | 2025-05-30 19:17:10 -0400 |
| commit | 68b00cdf45551ba64c4f7217963055fc5a1a0cc4 (patch) | |
| tree | f6bdef06bc02d16596bdf62870c8a1cfafe49859 /rust/tests | |
| parent | 77ab5e812af200b8e3bca0aac31d5fe0f638fba3 (diff) | |
[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
Diffstat (limited to 'rust/tests')
| -rw-r--r-- | rust/tests/high_level_il.rs | 7 | ||||
| -rw-r--r-- | rust/tests/language_representation.rs | 10 | ||||
| -rw-r--r-- | rust/tests/medium_level_il.rs | 48 |
3 files changed, 47 insertions, 18 deletions
diff --git a/rust/tests/high_level_il.rs b/rust/tests/high_level_il.rs index 4466678c..e757301e 100644 --- a/rust/tests/high_level_il.rs +++ b/rust/tests/high_level_il.rs @@ -1,6 +1,8 @@ use binaryninja::binary_view::BinaryViewExt; use binaryninja::headless::Session; -use binaryninja::high_level_il::{HighLevelILInstructionKind, HighLevelInstructionIndex}; +use binaryninja::high_level_il::{ + HighLevelExpressionIndex, HighLevelILInstructionKind, HighLevelInstructionIndex, +}; use std::path::PathBuf; #[test] @@ -23,7 +25,8 @@ fn test_hlil_info() { // 00025f22 HLIL_CALL (HLIL_CONST_PTR.d __crt_interlocked_read_32)((HLIL_VAR.d arg1)))) // 00025f10 ) let instr_0 = hlil_instr_iter.next().unwrap(); - assert_eq!(instr_0.expr_index, HighLevelInstructionIndex(5)); + assert_eq!(instr_0.instr_index, HighLevelInstructionIndex(0)); + assert_eq!(instr_0.expr_index, HighLevelExpressionIndex(5)); assert_eq!(instr_0.address, image_base + 0x00025f22); println!("{:?}", instr_0.kind); match instr_0.kind { diff --git a/rust/tests/language_representation.rs b/rust/tests/language_representation.rs index c3d60966..f4aeb7bd 100644 --- a/rust/tests/language_representation.rs +++ b/rust/tests/language_representation.rs @@ -8,7 +8,7 @@ use binaryninja::disassembly::{ use binaryninja::function::Function; use binaryninja::headless::Session; use binaryninja::high_level_il::token_emitter::HighLevelILTokenEmitter; -use binaryninja::high_level_il::{HighLevelILFunction, HighLevelInstructionIndex}; +use binaryninja::high_level_il::{HighLevelExpressionIndex, HighLevelILFunction}; use binaryninja::language_representation::{ register_language_representation_function_type, CoreLanguageRepresentationFunction, CoreLanguageRepresentationFunctionType, LanguageRepresentationFunction, @@ -60,7 +60,7 @@ impl LanguageRepresentationFunction for MyLangRepr { fn expr_text( &self, il: &HighLevelILFunction, - expr_index: HighLevelInstructionIndex, + expr_index: HighLevelExpressionIndex, tokens: &HighLevelILTokenEmitter, _settings: &DisassemblySettings, _as_full_ast: bool, @@ -101,7 +101,7 @@ impl LanguageRepresentationFunction for MyLangRepr { fn begin_lines( &self, _il: &HighLevelILFunction, - _expr_index: HighLevelInstructionIndex, + _expr_index: HighLevelExpressionIndex, _tokens: &HighLevelILTokenEmitter, ) { } @@ -109,7 +109,7 @@ impl LanguageRepresentationFunction for MyLangRepr { fn end_lines( &self, _il: &HighLevelILFunction, - _expr_index: HighLevelInstructionIndex, + _expr_index: HighLevelExpressionIndex, _tokens: &HighLevelILTokenEmitter, ) { } @@ -149,7 +149,7 @@ fn test_custom_language_representation() { let il = func.high_level_il(false).unwrap(); let settings = DisassemblySettings::new(); - let root_idx = il.root_instruction_index(); + let root_idx = il.root_expression_index(); let result = _repr.linear_lines(&il, root_idx, &settings, false); let output: String = result.iter().map(|dis| dis.to_string()).collect(); assert_eq!( 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::<PathBuf>().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()); + } + } + } +} |
