summaryrefslogtreecommitdiff
path: root/rust/tests
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2025-02-06 23:59:12 -0500
committerMason Reed <mason@vector35.com>2025-02-06 23:59:12 -0500
commitb451efd609f94152fbca97cb944467234dcc74d7 (patch)
tree7b4100ca9a7feaadadc7201cc54981ee7f2a073a /rust/tests
parentf9bd8eb2a8aa8b492d4145624d12e7014c7ee65c (diff)
Update Rust tests to respect image base
Diffstat (limited to 'rust/tests')
-rw-r--r--rust/tests/binary_view.rs12
-rw-r--r--rust/tests/high_level_il.rs3
-rw-r--r--rust/tests/low_level_il.rs22
-rw-r--r--rust/tests/medium_level_il.rs11
-rw-r--r--rust/tests/render_layer.rs3
5 files changed, 30 insertions, 21 deletions
diff --git a/rust/tests/binary_view.rs b/rust/tests/binary_view.rs
index 6cb617c2..08e530aa 100644
--- a/rust/tests/binary_view.rs
+++ b/rust/tests/binary_view.rs
@@ -26,11 +26,12 @@ fn test_binary_saving(_session: &Session) {
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
// Verify the contents before we modify.
- let original_contents = view.read_vec(0x1560, 4);
+ let contents_addr = view.original_image_base() + 0x1560;
+ let original_contents = view.read_vec(contents_addr, 4);
assert_eq!(original_contents, [0x00, 0xf1, 0x00, 0x00]);
- assert_eq!(view.write(0x1560, &[0xff, 0xff, 0xff, 0xff]), 4);
+ assert_eq!(view.write(contents_addr, &[0xff, 0xff, 0xff, 0xff]), 4);
// Verify that we modified the binary
- let modified_contents = view.read_vec(0x1560, 4);
+ let modified_contents = view.read_vec(contents_addr, 4);
assert_eq!(modified_contents, [0xff, 0xff, 0xff, 0xff]);
// HACK: To prevent us from deadlocking in save_to_path we wait for all main thread actions to finish.
@@ -41,7 +42,10 @@ fn test_binary_saving(_session: &Session) {
// Verify that the file exists and is modified.
let new_view =
binaryninja::load(out_dir.join("atox.obj.new")).expect("Failed to load new view");
- assert_eq!(new_view.read_vec(0x1560, 4), [0xff, 0xff, 0xff, 0xff]);
+ assert_eq!(
+ new_view.read_vec(contents_addr, 4),
+ [0xff, 0xff, 0xff, 0xff]
+ );
}
#[rstest]
diff --git a/rust/tests/high_level_il.rs b/rust/tests/high_level_il.rs
index 3a2b7209..bec1d67c 100644
--- a/rust/tests/high_level_il.rs
+++ b/rust/tests/high_level_il.rs
@@ -14,6 +14,7 @@ fn session() -> Session {
fn test_hlil_info(_session: &Session) {
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
+ let image_base = view.original_image_base();
let entry_function = view.entry_point_function().unwrap();
let hlil_function = entry_function.high_level_il(false).unwrap();
@@ -29,7 +30,7 @@ fn test_hlil_info(_session: &Session) {
// 00025f10 )
let instr_0 = hlil_instr_iter.next().unwrap();
assert_eq!(instr_0.expr_index, HighLevelInstructionIndex(5));
- assert_eq!(instr_0.address, 0x00025f22);
+ assert_eq!(instr_0.address, image_base + 0x00025f22);
println!("{:?}", instr_0.kind);
match instr_0.kind {
HighLevelILInstructionKind::Ret(op) => {
diff --git a/rust/tests/low_level_il.rs b/rust/tests/low_level_il.rs
index bbd31fc9..6131de0b 100644
--- a/rust/tests/low_level_il.rs
+++ b/rust/tests/low_level_il.rs
@@ -21,6 +21,7 @@ fn session() -> Session {
fn test_llil_info(_session: &Session) {
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
+ let image_base = view.original_image_base();
let entry_function = view.entry_point_function().unwrap();
let llil_function = entry_function.low_level_il().unwrap();
@@ -32,7 +33,7 @@ fn test_llil_info(_session: &Session) {
// 0 @ 00025f10 (LLIL_SET_REG.d edi = (LLIL_REG.d edi))
let instr_0 = llil_instr_iter.next().unwrap();
assert_eq!(instr_0.index, LowLevelInstructionIndex(0));
- assert_eq!(instr_0.address(), 0x00025f10);
+ assert_eq!(instr_0.address(), image_base + 0x00025f10);
println!("{:?}", instr_0);
println!("{:?}", instr_0.kind());
match instr_0.kind() {
@@ -49,7 +50,7 @@ fn test_llil_info(_session: &Session) {
// 1 @ 00025f12 (LLIL_PUSH.d push((LLIL_REG.d ebp)))
let instr_1 = llil_instr_iter.next().unwrap();
assert_eq!(instr_1.index, LowLevelInstructionIndex(1));
- assert_eq!(instr_1.address(), 0x00025f12);
+ assert_eq!(instr_1.address(), image_base + 0x00025f12);
println!("{:?}", instr_1.kind());
match instr_1.kind() {
LowLevelILInstructionKind::Push(op) => {
@@ -72,7 +73,7 @@ fn test_llil_info(_session: &Session) {
// 2 @ 00025f13 (LLIL_SET_REG.d ebp = (LLIL_REG.d esp) {__saved_ebp})
let instr_2 = llil_instr_iter.next().unwrap();
assert_eq!(instr_2.index, LowLevelInstructionIndex(2));
- assert_eq!(instr_2.address(), 0x00025f13);
+ assert_eq!(instr_2.address(), image_base + 0x00025f13);
println!("{:?}", instr_2.kind());
match instr_2.kind() {
LowLevelILInstructionKind::SetReg(op) => {
@@ -88,7 +89,7 @@ fn test_llil_info(_session: &Session) {
// 3 @ 00025f15 (LLIL_SET_REG.d eax = (LLIL_LOAD.d [(LLIL_ADD.d (LLIL_REG.d ebp) + (LLIL_CONST.d 8)) {arg1}].d))
let instr_3 = llil_instr_iter.next().unwrap();
assert_eq!(instr_3.index, LowLevelInstructionIndex(3));
- assert_eq!(instr_3.address(), 0x00025f15);
+ assert_eq!(instr_3.address(), image_base + 0x00025f15);
println!("{:?}", instr_3.kind());
match instr_3.kind() {
LowLevelILInstructionKind::SetReg(op) => {
@@ -104,7 +105,7 @@ fn test_llil_info(_session: &Session) {
// 4 @ 00025f18 (LLIL_PUSH.d push((LLIL_REG.d eax)))
let instr_4 = llil_instr_iter.next().unwrap();
assert_eq!(instr_4.index, LowLevelInstructionIndex(4));
- assert_eq!(instr_4.address(), 0x00025f18);
+ assert_eq!(instr_4.address(), image_base + 0x00025f18);
println!("{:?}", instr_4.kind());
match instr_4.kind() {
LowLevelILInstructionKind::Push(op) => {
@@ -116,7 +117,7 @@ fn test_llil_info(_session: &Session) {
// 5 @ 00025f19 (LLIL_CALL call((LLIL_CONST_PTR.d __crt_interlocked_read_32)))
let instr_5 = llil_instr_iter.next().unwrap();
assert_eq!(instr_5.index, LowLevelInstructionIndex(5));
- assert_eq!(instr_5.address(), 0x00025f19);
+ assert_eq!(instr_5.address(), image_base + 0x00025f19);
println!("{:?}", instr_5.kind());
match instr_5.kind() {
LowLevelILInstructionKind::Call(op) => {
@@ -127,7 +128,7 @@ fn test_llil_info(_session: &Session) {
// 6 @ 00025f1e (LLIL_SET_REG.d esp = (LLIL_ADD.d (LLIL_REG.d esp) + (LLIL_CONST.d 4)))
let instr_6 = llil_instr_iter.next().unwrap();
assert_eq!(instr_6.index, LowLevelInstructionIndex(6));
- assert_eq!(instr_6.address(), 0x00025f1e);
+ assert_eq!(instr_6.address(), image_base + 0x00025f1e);
println!("{:?}", instr_6.kind());
match instr_6.kind() {
LowLevelILInstructionKind::SetReg(op) => {
@@ -143,7 +144,7 @@ fn test_llil_info(_session: &Session) {
// 7 @ 00025f21 (LLIL_SET_REG.d ebp = (LLIL_POP.d pop))
let instr_7 = llil_instr_iter.next().unwrap();
assert_eq!(instr_7.index, LowLevelInstructionIndex(7));
- assert_eq!(instr_7.address(), 0x00025f21);
+ assert_eq!(instr_7.address(), image_base + 0x00025f21);
println!("{:?}", instr_7.kind());
match instr_7.kind() {
LowLevelILInstructionKind::SetReg(op) => {
@@ -159,7 +160,7 @@ fn test_llil_info(_session: &Session) {
// 8 @ 00025f22 (LLIL_RET <return> jump((LLIL_POP.d pop)))
let instr_8 = llil_instr_iter.next().unwrap();
assert_eq!(instr_8.index, LowLevelInstructionIndex(8));
- assert_eq!(instr_8.address(), 0x00025f22);
+ assert_eq!(instr_8.address(), image_base + 0x00025f22);
println!("{:?}", instr_8.kind());
match instr_8.kind() {
LowLevelILInstructionKind::Ret(op) => {
@@ -173,10 +174,11 @@ fn test_llil_info(_session: &Session) {
fn test_llil_visitor(_session: &Session) {
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
+ let image_base = view.original_image_base();
let platform = view.default_platform().unwrap();
// Sample function: __crt_strtox::c_string_character_source<char>::validate
- let sample_function = view.function_at(&platform, 0x2bd80).unwrap();
+ let sample_function = view.function_at(&platform, image_base + 0x2bd80).unwrap();
let llil_function = sample_function.low_level_il().unwrap();
let llil_basic_blocks = llil_function.basic_blocks();
let llil_basic_block_iter = llil_basic_blocks.iter();
diff --git a/rust/tests/medium_level_il.rs b/rust/tests/medium_level_il.rs
index 7d38797b..65746186 100644
--- a/rust/tests/medium_level_il.rs
+++ b/rust/tests/medium_level_il.rs
@@ -14,6 +14,7 @@ fn session() -> Session {
fn test_mlil_info(_session: &Session) {
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
+ let image_base = view.original_image_base();
let entry_function = view.entry_point_function().unwrap();
let mlil_function = entry_function.medium_level_il().unwrap();
@@ -25,7 +26,7 @@ fn test_mlil_info(_session: &Session) {
// 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.address, 0x00025f10);
+ assert_eq!(instr_0.address, image_base + 0x00025f10);
println!("{:?}", instr_0.kind);
match instr_0.kind {
MediumLevelILInstructionKind::SetVar(op) => {
@@ -37,7 +38,7 @@ fn test_mlil_info(_session: &Session) {
// 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.address, 0x00025f15);
+ assert_eq!(instr_1.address, image_base + 0x00025f15);
println!("{:?}", instr_1.kind);
match instr_1.kind {
MediumLevelILInstructionKind::SetVar(op) => {
@@ -49,7 +50,7 @@ fn test_mlil_info(_session: &Session) {
// 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.address, 0x00025f18);
+ assert_eq!(instr_2.address, image_base + 0x00025f18);
println!("{:?}", instr_2.kind);
match instr_2.kind {
MediumLevelILInstructionKind::SetVar(op) => {
@@ -61,7 +62,7 @@ fn test_mlil_info(_session: &Session) {
// 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.address, 0x00025f19);
+ assert_eq!(instr_3.address, image_base + 0x00025f19);
println!("{:?}", instr_3.kind);
match instr_3.kind {
MediumLevelILInstructionKind::Call(op) => {
@@ -76,7 +77,7 @@ fn test_mlil_info(_session: &Session) {
// 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.address, 0x00025f22);
+ assert_eq!(instr_4.address, image_base + 0x00025f22);
println!("{:?}", instr_4.kind);
match instr_4.kind {
MediumLevelILInstructionKind::Ret(op) => {
diff --git a/rust/tests/render_layer.rs b/rust/tests/render_layer.rs
index 88dabd87..184467b7 100644
--- a/rust/tests/render_layer.rs
+++ b/rust/tests/render_layer.rs
@@ -1,4 +1,5 @@
use binaryninja::basic_block::BasicBlock;
+use binaryninja::binary_view::BinaryViewExt;
use binaryninja::disassembly::{DisassemblyOption, DisassemblySettings, DisassemblyTextLine};
use binaryninja::function::NativeBlock;
use binaryninja::headless::Session;
@@ -51,7 +52,7 @@ fn test_render_layer_linear_view(_session: &Session) {
let linear_view = LinearViewObject::disassembly(&view, &settings);
let mut cursor = linear_view.create_cursor();
// Seek to the start of the function `__crt_strtox::is_overflow_condition<uint64_t>`
- cursor.seek_to_address(0x26240);
+ cursor.seek_to_address(view.original_image_base() + 0x26240);
let current_object = cursor.current_object();
let current_lines = cursor.lines().to_vec();