Skip to content

Commit 8ba8388

Browse files
committed
[Rust] Retrieve architecture if missing from location passed to MediumLevelILFunction::instruction_from_index
Fixes #6876
1 parent c7a8197 commit 8ba8388

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

rust/src/low_level_il/function.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ where
106106
) -> Option<LowLevelInstructionIndex> {
107107
use binaryninjacore_sys::BNLowLevelILGetInstructionStart;
108108
let loc: Location = loc.into();
109-
let arch = loc.arch.unwrap_or_else(|| *self.arch().as_ref());
109+
// If the location does not specify an architecture, use the function's architecture.
110+
let arch = loc.arch.unwrap_or_else(|| self.arch());
110111
let instr_idx =
111112
unsafe { BNLowLevelILGetInstructionStart(self.handle, arch.handle, loc.addr) };
112113
// `instr_idx` will equal self.instruction_count() if the instruction is not valid.

rust/src/medium_level_il/function.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ impl MediumLevelILFunction {
4747
loc: L,
4848
) -> Option<MediumLevelInstructionIndex> {
4949
let loc: Location = loc.into();
50-
let arch = loc
51-
.arch
52-
.map(|a| a.handle)
53-
.unwrap_or_else(std::ptr::null_mut);
54-
let instr_idx = unsafe { BNMediumLevelILGetInstructionStart(self.handle, arch, loc.addr) };
50+
// If the location does not specify an architecture, use the function's architecture.
51+
let arch = loc.arch.unwrap_or_else(|| self.function().arch());
52+
let instr_idx =
53+
unsafe { BNMediumLevelILGetInstructionStart(self.handle, arch.handle, loc.addr) };
5554
// `instr_idx` will equal self.instruction_count() if the instruction is not valid.
5655
if instr_idx >= self.instruction_count() {
5756
None

rust/tests/medium_level_il.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,14 @@ fn test_mlil_possible_values() {
164164
_ => panic!("Expected SetVar"),
165165
}
166166

167+
// As an aside, we also test to make sure `instruction_index_at` works.
168+
let instr_21_idx = mlil_function
169+
.instruction_index_at(image_base + 0x0002af9a)
170+
.expect("Failed to get instruction index");
171+
167172
// 21 @ 0002af9a (MLIL_RET return (MLIL_VAR.d eax_2))
168173
let instr_21 = mlil_function
169-
.instruction_from_index(MediumLevelInstructionIndex(21))
174+
.instruction_from_index(instr_21_idx)
170175
.expect("Failed to get instruction");
171176
let lifted_instr_21 = instr_21.lift();
172177
match lifted_instr_21.kind {

0 commit comments

Comments
 (0)