Skip to content

Commit b451efd

Browse files
committed
Update Rust tests to respect image base
1 parent f9bd8eb commit b451efd

File tree

5 files changed

+30
-21
lines changed

5 files changed

+30
-21
lines changed

rust/tests/binary_view.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ fn test_binary_saving(_session: &Session) {
2626
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
2727
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
2828
// Verify the contents before we modify.
29-
let original_contents = view.read_vec(0x1560, 4);
29+
let contents_addr = view.original_image_base() + 0x1560;
30+
let original_contents = view.read_vec(contents_addr, 4);
3031
assert_eq!(original_contents, [0x00, 0xf1, 0x00, 0x00]);
31-
assert_eq!(view.write(0x1560, &[0xff, 0xff, 0xff, 0xff]), 4);
32+
assert_eq!(view.write(contents_addr, &[0xff, 0xff, 0xff, 0xff]), 4);
3233
// Verify that we modified the binary
33-
let modified_contents = view.read_vec(0x1560, 4);
34+
let modified_contents = view.read_vec(contents_addr, 4);
3435
assert_eq!(modified_contents, [0xff, 0xff, 0xff, 0xff]);
3536

3637
// 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) {
4142
// Verify that the file exists and is modified.
4243
let new_view =
4344
binaryninja::load(out_dir.join("atox.obj.new")).expect("Failed to load new view");
44-
assert_eq!(new_view.read_vec(0x1560, 4), [0xff, 0xff, 0xff, 0xff]);
45+
assert_eq!(
46+
new_view.read_vec(contents_addr, 4),
47+
[0xff, 0xff, 0xff, 0xff]
48+
);
4549
}
4650

4751
#[rstest]

rust/tests/high_level_il.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fn session() -> Session {
1414
fn test_hlil_info(_session: &Session) {
1515
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
1616
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
17+
let image_base = view.original_image_base();
1718

1819
let entry_function = view.entry_point_function().unwrap();
1920
let hlil_function = entry_function.high_level_il(false).unwrap();
@@ -29,7 +30,7 @@ fn test_hlil_info(_session: &Session) {
2930
// 00025f10 )
3031
let instr_0 = hlil_instr_iter.next().unwrap();
3132
assert_eq!(instr_0.expr_index, HighLevelInstructionIndex(5));
32-
assert_eq!(instr_0.address, 0x00025f22);
33+
assert_eq!(instr_0.address, image_base + 0x00025f22);
3334
println!("{:?}", instr_0.kind);
3435
match instr_0.kind {
3536
HighLevelILInstructionKind::Ret(op) => {

rust/tests/low_level_il.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ fn session() -> Session {
2121
fn test_llil_info(_session: &Session) {
2222
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
2323
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
24+
let image_base = view.original_image_base();
2425

2526
let entry_function = view.entry_point_function().unwrap();
2627
let llil_function = entry_function.low_level_il().unwrap();
@@ -32,7 +33,7 @@ fn test_llil_info(_session: &Session) {
3233
// 0 @ 00025f10 (LLIL_SET_REG.d edi = (LLIL_REG.d edi))
3334
let instr_0 = llil_instr_iter.next().unwrap();
3435
assert_eq!(instr_0.index, LowLevelInstructionIndex(0));
35-
assert_eq!(instr_0.address(), 0x00025f10);
36+
assert_eq!(instr_0.address(), image_base + 0x00025f10);
3637
println!("{:?}", instr_0);
3738
println!("{:?}", instr_0.kind());
3839
match instr_0.kind() {
@@ -49,7 +50,7 @@ fn test_llil_info(_session: &Session) {
4950
// 1 @ 00025f12 (LLIL_PUSH.d push((LLIL_REG.d ebp)))
5051
let instr_1 = llil_instr_iter.next().unwrap();
5152
assert_eq!(instr_1.index, LowLevelInstructionIndex(1));
52-
assert_eq!(instr_1.address(), 0x00025f12);
53+
assert_eq!(instr_1.address(), image_base + 0x00025f12);
5354
println!("{:?}", instr_1.kind());
5455
match instr_1.kind() {
5556
LowLevelILInstructionKind::Push(op) => {
@@ -72,7 +73,7 @@ fn test_llil_info(_session: &Session) {
7273
// 2 @ 00025f13 (LLIL_SET_REG.d ebp = (LLIL_REG.d esp) {__saved_ebp})
7374
let instr_2 = llil_instr_iter.next().unwrap();
7475
assert_eq!(instr_2.index, LowLevelInstructionIndex(2));
75-
assert_eq!(instr_2.address(), 0x00025f13);
76+
assert_eq!(instr_2.address(), image_base + 0x00025f13);
7677
println!("{:?}", instr_2.kind());
7778
match instr_2.kind() {
7879
LowLevelILInstructionKind::SetReg(op) => {
@@ -88,7 +89,7 @@ fn test_llil_info(_session: &Session) {
8889
// 3 @ 00025f15 (LLIL_SET_REG.d eax = (LLIL_LOAD.d [(LLIL_ADD.d (LLIL_REG.d ebp) + (LLIL_CONST.d 8)) {arg1}].d))
8990
let instr_3 = llil_instr_iter.next().unwrap();
9091
assert_eq!(instr_3.index, LowLevelInstructionIndex(3));
91-
assert_eq!(instr_3.address(), 0x00025f15);
92+
assert_eq!(instr_3.address(), image_base + 0x00025f15);
9293
println!("{:?}", instr_3.kind());
9394
match instr_3.kind() {
9495
LowLevelILInstructionKind::SetReg(op) => {
@@ -104,7 +105,7 @@ fn test_llil_info(_session: &Session) {
104105
// 4 @ 00025f18 (LLIL_PUSH.d push((LLIL_REG.d eax)))
105106
let instr_4 = llil_instr_iter.next().unwrap();
106107
assert_eq!(instr_4.index, LowLevelInstructionIndex(4));
107-
assert_eq!(instr_4.address(), 0x00025f18);
108+
assert_eq!(instr_4.address(), image_base + 0x00025f18);
108109
println!("{:?}", instr_4.kind());
109110
match instr_4.kind() {
110111
LowLevelILInstructionKind::Push(op) => {
@@ -116,7 +117,7 @@ fn test_llil_info(_session: &Session) {
116117
// 5 @ 00025f19 (LLIL_CALL call((LLIL_CONST_PTR.d __crt_interlocked_read_32)))
117118
let instr_5 = llil_instr_iter.next().unwrap();
118119
assert_eq!(instr_5.index, LowLevelInstructionIndex(5));
119-
assert_eq!(instr_5.address(), 0x00025f19);
120+
assert_eq!(instr_5.address(), image_base + 0x00025f19);
120121
println!("{:?}", instr_5.kind());
121122
match instr_5.kind() {
122123
LowLevelILInstructionKind::Call(op) => {
@@ -127,7 +128,7 @@ fn test_llil_info(_session: &Session) {
127128
// 6 @ 00025f1e (LLIL_SET_REG.d esp = (LLIL_ADD.d (LLIL_REG.d esp) + (LLIL_CONST.d 4)))
128129
let instr_6 = llil_instr_iter.next().unwrap();
129130
assert_eq!(instr_6.index, LowLevelInstructionIndex(6));
130-
assert_eq!(instr_6.address(), 0x00025f1e);
131+
assert_eq!(instr_6.address(), image_base + 0x00025f1e);
131132
println!("{:?}", instr_6.kind());
132133
match instr_6.kind() {
133134
LowLevelILInstructionKind::SetReg(op) => {
@@ -143,7 +144,7 @@ fn test_llil_info(_session: &Session) {
143144
// 7 @ 00025f21 (LLIL_SET_REG.d ebp = (LLIL_POP.d pop))
144145
let instr_7 = llil_instr_iter.next().unwrap();
145146
assert_eq!(instr_7.index, LowLevelInstructionIndex(7));
146-
assert_eq!(instr_7.address(), 0x00025f21);
147+
assert_eq!(instr_7.address(), image_base + 0x00025f21);
147148
println!("{:?}", instr_7.kind());
148149
match instr_7.kind() {
149150
LowLevelILInstructionKind::SetReg(op) => {
@@ -159,7 +160,7 @@ fn test_llil_info(_session: &Session) {
159160
// 8 @ 00025f22 (LLIL_RET <return> jump((LLIL_POP.d pop)))
160161
let instr_8 = llil_instr_iter.next().unwrap();
161162
assert_eq!(instr_8.index, LowLevelInstructionIndex(8));
162-
assert_eq!(instr_8.address(), 0x00025f22);
163+
assert_eq!(instr_8.address(), image_base + 0x00025f22);
163164
println!("{:?}", instr_8.kind());
164165
match instr_8.kind() {
165166
LowLevelILInstructionKind::Ret(op) => {
@@ -173,10 +174,11 @@ fn test_llil_info(_session: &Session) {
173174
fn test_llil_visitor(_session: &Session) {
174175
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
175176
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
177+
let image_base = view.original_image_base();
176178
let platform = view.default_platform().unwrap();
177179

178180
// Sample function: __crt_strtox::c_string_character_source<char>::validate
179-
let sample_function = view.function_at(&platform, 0x2bd80).unwrap();
181+
let sample_function = view.function_at(&platform, image_base + 0x2bd80).unwrap();
180182
let llil_function = sample_function.low_level_il().unwrap();
181183
let llil_basic_blocks = llil_function.basic_blocks();
182184
let llil_basic_block_iter = llil_basic_blocks.iter();

rust/tests/medium_level_il.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fn session() -> Session {
1414
fn test_mlil_info(_session: &Session) {
1515
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
1616
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
17+
let image_base = view.original_image_base();
1718

1819
let entry_function = view.entry_point_function().unwrap();
1920
let mlil_function = entry_function.medium_level_il().unwrap();
@@ -25,7 +26,7 @@ fn test_mlil_info(_session: &Session) {
2526
// 0 @ 00025f10 (MLIL_SET_VAR.d edi_1 = (MLIL_VAR.d edi))
2627
let instr_0 = mlil_instr_iter.next().unwrap();
2728
assert_eq!(instr_0.expr_index, MediumLevelInstructionIndex(1));
28-
assert_eq!(instr_0.address, 0x00025f10);
29+
assert_eq!(instr_0.address, image_base + 0x00025f10);
2930
println!("{:?}", instr_0.kind);
3031
match instr_0.kind {
3132
MediumLevelILInstructionKind::SetVar(op) => {
@@ -37,7 +38,7 @@ fn test_mlil_info(_session: &Session) {
3738
// 1 @ 00025f15 (MLIL_SET_VAR.d eax = (MLIL_VAR.d arg1))
3839
let instr_1 = mlil_instr_iter.next().unwrap();
3940
assert_eq!(instr_1.expr_index, MediumLevelInstructionIndex(3));
40-
assert_eq!(instr_1.address, 0x00025f15);
41+
assert_eq!(instr_1.address, image_base + 0x00025f15);
4142
println!("{:?}", instr_1.kind);
4243
match instr_1.kind {
4344
MediumLevelILInstructionKind::SetVar(op) => {
@@ -49,7 +50,7 @@ fn test_mlil_info(_session: &Session) {
4950
// 2 @ 00025f18 (MLIL_SET_VAR.d var_8 = (MLIL_VAR.d eax))
5051
let instr_2 = mlil_instr_iter.next().unwrap();
5152
assert_eq!(instr_2.expr_index, MediumLevelInstructionIndex(5));
52-
assert_eq!(instr_2.address, 0x00025f18);
53+
assert_eq!(instr_2.address, image_base + 0x00025f18);
5354
println!("{:?}", instr_2.kind);
5455
match instr_2.kind {
5556
MediumLevelILInstructionKind::SetVar(op) => {
@@ -61,7 +62,7 @@ fn test_mlil_info(_session: &Session) {
6162
// 3 @ 00025f19 (MLIL_CALL eax_1 = (MLIL_CONST_PTR.d __crt_interlocked_read_32)((MLIL_VAR.d var_8)))
6263
let instr_3 = mlil_instr_iter.next().unwrap();
6364
assert_eq!(instr_3.expr_index, MediumLevelInstructionIndex(10));
64-
assert_eq!(instr_3.address, 0x00025f19);
65+
assert_eq!(instr_3.address, image_base + 0x00025f19);
6566
println!("{:?}", instr_3.kind);
6667
match instr_3.kind {
6768
MediumLevelILInstructionKind::Call(op) => {
@@ -76,7 +77,7 @@ fn test_mlil_info(_session: &Session) {
7677
// 4 @ 00025f22 (MLIL_RET return (MLIL_VAR.d eax_1))
7778
let instr_4 = mlil_instr_iter.next().unwrap();
7879
assert_eq!(instr_4.expr_index, MediumLevelInstructionIndex(13));
79-
assert_eq!(instr_4.address, 0x00025f22);
80+
assert_eq!(instr_4.address, image_base + 0x00025f22);
8081
println!("{:?}", instr_4.kind);
8182
match instr_4.kind {
8283
MediumLevelILInstructionKind::Ret(op) => {

rust/tests/render_layer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use binaryninja::basic_block::BasicBlock;
2+
use binaryninja::binary_view::BinaryViewExt;
23
use binaryninja::disassembly::{DisassemblyOption, DisassemblySettings, DisassemblyTextLine};
34
use binaryninja::function::NativeBlock;
45
use binaryninja::headless::Session;
@@ -51,7 +52,7 @@ fn test_render_layer_linear_view(_session: &Session) {
5152
let linear_view = LinearViewObject::disassembly(&view, &settings);
5253
let mut cursor = linear_view.create_cursor();
5354
// Seek to the start of the function `__crt_strtox::is_overflow_condition<uint64_t>`
54-
cursor.seek_to_address(0x26240);
55+
cursor.seek_to_address(view.original_image_base() + 0x26240);
5556
let current_object = cursor.current_object();
5657
let current_lines = cursor.lines().to_vec();
5758

0 commit comments

Comments
 (0)