Skip to content

Commit 5b974d7

Browse files
committed
More rust cleanup
- Format all rust plugins - Fix some tests that were out of date - Simplify WARP tests to only binaries, building object files from source is a pain - Link to core in all rust plugins - Fix some memory leaks - Update warp insta snapshots - Fix some misc clippy lints
1 parent 840c95c commit 5b974d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1044
-960
lines changed

arch/msp430/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66

77
[dependencies]
88
binaryninja = { path = "../../rust" }
9+
binaryninjacore-sys = { path = "../../rust/binaryninjacore-sys" }
910
log = "0.4"
1011
msp430-asm = "^0.2"
1112

arch/msp430/build.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
fn main() {
2+
let link_path = std::env::var_os("DEP_BINARYNINJACORE_PATH")
3+
.expect("DEP_BINARYNINJACORE_PATH not specified");
4+
5+
println!("cargo::rustc-link-lib=dylib=binaryninjacore");
6+
println!("cargo::rustc-link-search={}", link_path.to_str().unwrap());
7+
8+
#[cfg(not(target_os = "windows"))]
9+
{
10+
println!(
11+
"cargo::rustc-link-arg=-Wl,-rpath,{0},-L{0}",
12+
link_path.to_string_lossy()
13+
);
14+
}
15+
}

arch/msp430/src/architecture.rs

Lines changed: 51 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::register::Register;
44

55
use binaryninja::{
66
architecture::{
7-
Architecture, CoreArchitecture, CustomArchitectureHandle, FlagCondition,
8-
InstructionInfo, UnusedIntrinsic, UnusedRegisterStack, UnusedRegisterStackInfo,
7+
Architecture, CoreArchitecture, CustomArchitectureHandle, FlagCondition, InstructionInfo,
8+
UnusedIntrinsic, UnusedRegisterStack, UnusedRegisterStackInfo,
99
},
1010
disassembly::{InstructionTextToken, InstructionTextTokenKind},
1111
llil::{LiftedExpr, Lifter},
@@ -17,8 +17,10 @@ use msp430_asm::{
1717
single_operand::SingleOperand, two_operand::TwoOperand,
1818
};
1919

20+
use binaryninja::architecture::{
21+
BranchKind, FlagClassId, FlagGroupId, FlagId, FlagWriteId, RegisterId,
22+
};
2023
use log::error;
21-
use binaryninja::architecture::{BranchKind, FlagClassId, FlagGroupId, FlagId, FlagWriteId, RegisterId};
2224

2325
const MIN_MNEMONIC: usize = 9;
2426

@@ -83,81 +85,51 @@ impl Architecture for Msp430 {
8385

8486
match inst {
8587
Instruction::Jnz(inst) => {
86-
info.add_branch(
87-
BranchKind::True(offset_to_absolute(addr, inst.offset())),
88-
);
89-
info.add_branch(
90-
BranchKind::False(addr + inst.size() as u64),
91-
);
88+
info.add_branch(BranchKind::True(offset_to_absolute(addr, inst.offset())));
89+
info.add_branch(BranchKind::False(addr + inst.size() as u64));
9290
}
9391
Instruction::Jz(inst) => {
94-
info.add_branch(
95-
BranchKind::True(offset_to_absolute(addr, inst.offset())),
96-
);
97-
info.add_branch(
98-
BranchKind::False(addr + inst.size() as u64),
99-
);
92+
info.add_branch(BranchKind::True(offset_to_absolute(addr, inst.offset())));
93+
info.add_branch(BranchKind::False(addr + inst.size() as u64));
10094
}
10195
Instruction::Jlo(inst) => {
102-
info.add_branch(
103-
BranchKind::True(offset_to_absolute(addr, inst.offset())),
104-
);
105-
info.add_branch(
106-
BranchKind::False(addr + inst.size() as u64),
107-
);
96+
info.add_branch(BranchKind::True(offset_to_absolute(addr, inst.offset())));
97+
info.add_branch(BranchKind::False(addr + inst.size() as u64));
10898
}
10999
Instruction::Jc(inst) => {
110-
info.add_branch(
111-
BranchKind::True(offset_to_absolute(addr, inst.offset())),
112-
);
113-
info.add_branch(
114-
BranchKind::False(addr + inst.size() as u64),
115-
);
100+
info.add_branch(BranchKind::True(offset_to_absolute(addr, inst.offset())));
101+
info.add_branch(BranchKind::False(addr + inst.size() as u64));
116102
}
117103
Instruction::Jn(inst) => {
118-
info.add_branch(
119-
BranchKind::True(offset_to_absolute(addr, inst.offset())),
120-
);
121-
info.add_branch(
122-
BranchKind::False(addr + inst.size() as u64),
123-
);
104+
info.add_branch(BranchKind::True(offset_to_absolute(addr, inst.offset())));
105+
info.add_branch(BranchKind::False(addr + inst.size() as u64));
124106
}
125107
Instruction::Jge(inst) => {
126-
info.add_branch(
127-
BranchKind::True(offset_to_absolute(addr, inst.offset())),
128-
);
129-
info.add_branch(
130-
BranchKind::False(addr + inst.size() as u64),
131-
);
108+
info.add_branch(BranchKind::True(offset_to_absolute(addr, inst.offset())));
109+
info.add_branch(BranchKind::False(addr + inst.size() as u64));
132110
}
133111
Instruction::Jl(inst) => {
134-
info.add_branch(
135-
BranchKind::True(offset_to_absolute(addr, inst.offset())),
136-
);
137-
info.add_branch(
138-
BranchKind::False(addr + inst.size() as u64),
139-
);
112+
info.add_branch(BranchKind::True(offset_to_absolute(addr, inst.offset())));
113+
info.add_branch(BranchKind::False(addr + inst.size() as u64));
140114
}
141115
Instruction::Jmp(inst) => {
142-
info.add_branch(
143-
BranchKind::Unconditional(offset_to_absolute(addr, inst.offset())),
144-
);
116+
info.add_branch(BranchKind::Unconditional(offset_to_absolute(
117+
addr,
118+
inst.offset(),
119+
)));
145120
}
146121
Instruction::Br(inst) => match inst.destination() {
147-
Some(Operand::RegisterDirect(_)) => {
148-
info.add_branch(BranchKind::Indirect)
122+
Some(Operand::RegisterDirect(_)) => info.add_branch(BranchKind::Indirect),
123+
Some(Operand::Indexed(_)) => info.add_branch(BranchKind::Indirect),
124+
Some(Operand::Absolute(value)) => {
125+
info.add_branch(BranchKind::Unconditional(*value as u64))
149126
}
150-
Some(Operand::Indexed(_)) => {
151-
info.add_branch(BranchKind::Indirect)
152-
}
153-
Some(Operand::Absolute(value)) => info.add_branch(
154-
BranchKind::Unconditional(*value as u64),
155-
),
156127
Some(Operand::Symbolic(offset)) => info.add_branch(
157128
BranchKind::Unconditional((addr as i64 + *offset as i64) as u64),
158129
),
159-
Some(Operand::Immediate(addr)) => info
160-
.add_branch(BranchKind::Unconditional(*addr as u64)),
130+
Some(Operand::Immediate(addr)) => {
131+
info.add_branch(BranchKind::Unconditional(*addr as u64))
132+
}
161133
Some(Operand::Constant(_)) => {
162134
info.add_branch(BranchKind::Unconditional(addr))
163135
}
@@ -168,24 +140,16 @@ impl Architecture for Msp430 {
168140
None => {}
169141
},
170142
Instruction::Call(inst) => match inst.source() {
171-
Operand::RegisterDirect(_) => {
172-
info.add_branch(BranchKind::Indirect)
173-
}
174-
Operand::Indexed(_) => {
175-
info.add_branch(BranchKind::Indirect)
176-
}
143+
Operand::RegisterDirect(_) => info.add_branch(BranchKind::Indirect),
144+
Operand::Indexed(_) => info.add_branch(BranchKind::Indirect),
177145
Operand::Absolute(value) => {
178146
info.add_branch(BranchKind::Call(*value as u64))
179147
}
180-
Operand::Symbolic(offset) => info.add_branch(
181-
BranchKind::Call((addr as i64 + *offset as i64) as u64),
182-
),
183-
Operand::Immediate(addr) => {
184-
info.add_branch(BranchKind::Call(*addr as u64))
185-
}
186-
Operand::Constant(_) => {
187-
info.add_branch(BranchKind::Call(addr))
148+
Operand::Symbolic(offset) => {
149+
info.add_branch(BranchKind::Call((addr as i64 + *offset as i64) as u64))
188150
}
151+
Operand::Immediate(addr) => info.add_branch(BranchKind::Call(*addr as u64)),
152+
Operand::Constant(_) => info.add_branch(BranchKind::Call(addr)),
189153
Operand::RegisterIndirect(_)
190154
| Operand::RegisterIndirectAutoIncrement(_) => {
191155
info.add_branch(BranchKind::Indirect)
@@ -468,7 +432,7 @@ fn generate_single_operand_tokens(
468432
if inst.mnemonic().len() < MIN_MNEMONIC {
469433
let padding = " ".repeat(MIN_MNEMONIC - inst.mnemonic().len());
470434
res.push(InstructionTextToken::new(
471-
&padding,
435+
padding,
472436
InstructionTextTokenKind::Text,
473437
))
474438
}
@@ -489,13 +453,13 @@ fn generate_jxx_tokens(inst: &impl Jxx, addr: u64) -> Vec<InstructionTextToken>
489453
if inst.mnemonic().len() < MIN_MNEMONIC {
490454
let padding = " ".repeat(MIN_MNEMONIC - inst.mnemonic().len());
491455
res.push(InstructionTextToken::new(
492-
&padding,
456+
padding,
493457
InstructionTextTokenKind::Text,
494458
))
495459
}
496460

497461
res.push(InstructionTextToken::new(
498-
&format!("0x{fixed_addr:4x}"),
462+
format!("0x{fixed_addr:4x}"),
499463
InstructionTextTokenKind::CodeRelativeAddress {
500464
value: fixed_addr,
501465
size: None,
@@ -514,7 +478,7 @@ fn generate_two_operand_tokens(inst: &impl TwoOperand, addr: u64) -> Vec<Instruc
514478
if inst.mnemonic().len() < MIN_MNEMONIC {
515479
let padding = " ".repeat(MIN_MNEMONIC - inst.mnemonic().len());
516480
res.push(InstructionTextToken::new(
517-
&padding,
481+
padding,
518482
InstructionTextTokenKind::Text,
519483
))
520484
}
@@ -578,7 +542,7 @@ fn generate_operand_tokens(source: &Operand, addr: u64, call: bool) -> Vec<Instr
578542
InstructionTextTokenKind::Register,
579543
)],
580544
_ => vec![InstructionTextToken::new(
581-
&format!("r{r}"),
545+
format!("r{r}"),
582546
InstructionTextTokenKind::Register,
583547
)],
584548
},
@@ -674,10 +638,7 @@ fn generate_operand_tokens(source: &Operand, addr: u64, call: bool) -> Vec<Instr
674638
},
675639
),
676640
InstructionTextToken::new("(", InstructionTextTokenKind::Text),
677-
InstructionTextToken::new(
678-
&format!("r{r}"),
679-
InstructionTextTokenKind::Register,
680-
),
641+
InstructionTextToken::new(format!("r{r}"), InstructionTextTokenKind::Register),
681642
InstructionTextToken::new(")", InstructionTextTokenKind::Text),
682643
]
683644
}
@@ -691,7 +652,7 @@ fn generate_operand_tokens(source: &Operand, addr: u64, call: bool) -> Vec<Instr
691652

692653
vec![
693654
InstructionTextToken::new("@", InstructionTextTokenKind::Text),
694-
InstructionTextToken::new(&r_text, InstructionTextTokenKind::Register),
655+
InstructionTextToken::new(r_text, InstructionTextTokenKind::Register),
695656
]
696657
}
697658
Operand::RegisterIndirectAutoIncrement(r) => {
@@ -703,32 +664,29 @@ fn generate_operand_tokens(source: &Operand, addr: u64, call: bool) -> Vec<Instr
703664

704665
vec![
705666
InstructionTextToken::new("@", InstructionTextTokenKind::Text),
706-
InstructionTextToken::new(&r_text, InstructionTextTokenKind::Register),
667+
InstructionTextToken::new(r_text, InstructionTextTokenKind::Register),
707668
InstructionTextToken::new("+", InstructionTextTokenKind::Text),
708669
]
709670
}
710671
Operand::Symbolic(i) => {
711672
let value = (addr as i64 + *i as i64) as u64;
712673
vec![InstructionTextToken::new(
713-
&format!("{value:#x}"),
714-
InstructionTextTokenKind::CodeRelativeAddress {
715-
value,
716-
size: None,
717-
},
674+
format!("{value:#x}"),
675+
InstructionTextTokenKind::CodeRelativeAddress { value, size: None },
718676
)]
719677
}
720678
Operand::Immediate(i) => {
721679
if call {
722680
vec![InstructionTextToken::new(
723-
&format!("{i:#x}"),
681+
format!("{i:#x}"),
724682
InstructionTextTokenKind::CodeRelativeAddress {
725683
value: *i as u64,
726684
size: None,
727685
},
728686
)]
729687
} else {
730688
vec![InstructionTextToken::new(
731-
&format!("{i:#x}"),
689+
format!("{i:#x}"),
732690
InstructionTextTokenKind::PossibleAddress {
733691
value: *i as u64,
734692
size: None,
@@ -739,15 +697,15 @@ fn generate_operand_tokens(source: &Operand, addr: u64, call: bool) -> Vec<Instr
739697
Operand::Absolute(a) => {
740698
if call {
741699
vec![InstructionTextToken::new(
742-
&format!("{a:#x}"),
700+
format!("{a:#x}"),
743701
InstructionTextTokenKind::CodeRelativeAddress {
744702
value: *a as u64,
745703
size: None,
746704
},
747705
)]
748706
} else {
749707
vec![InstructionTextToken::new(
750-
&format!("{a:#x}"),
708+
format!("{a:#x}"),
751709
InstructionTextTokenKind::PossibleAddress {
752710
value: *a as u64,
753711
size: None,
@@ -765,7 +723,7 @@ fn generate_operand_tokens(source: &Operand, addr: u64, call: bool) -> Vec<Instr
765723
vec![
766724
InstructionTextToken::new("#", InstructionTextTokenKind::Text),
767725
InstructionTextToken::new(
768-
&num_text,
726+
num_text,
769727
InstructionTextTokenKind::Integer {
770728
value: *i as u64,
771729
size: None,

arch/msp430/src/flag.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ impl architecture::Flag for Flag {
4040
Self::Z => 1,
4141
Self::N => 2,
4242
Self::V => 8,
43-
}.into()
43+
}
44+
.into()
4445
}
4546
}
4647

@@ -125,7 +126,8 @@ impl architecture::FlagWrite for FlagWrite {
125126
Self::Nz => 2,
126127
Self::Nvz => 3,
127128
Self::Cnz => 4,
128-
}.into()
129+
}
130+
.into()
129131
}
130132

131133
fn flags_written(&self) -> Vec<Self::FlagType> {

arch/msp430/src/lib.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ extern crate binaryninja;
22
extern crate log;
33
extern crate msp430_asm;
44

5+
use binaryninja::{
6+
add_optional_plugin_dependency,
7+
architecture::ArchitectureExt,
8+
callingconvention,
9+
custombinaryview::{BinaryViewType, BinaryViewTypeExt},
10+
Endianness,
11+
};
512
use log::LevelFilter;
6-
use binaryninja::{add_optional_plugin_dependency, architecture::ArchitectureExt, callingconvention, custombinaryview::{BinaryViewType, BinaryViewTypeExt}, Endianness};
713

814
mod architecture;
915
mod flag;
@@ -17,10 +23,10 @@ use binaryninja::logger::Logger;
1723
#[allow(non_snake_case)]
1824
pub extern "C" fn CorePluginInit() -> bool {
1925
Logger::new("MSP430").with_level(LevelFilter::Info).init();
20-
let arch = binaryninja::architecture::register_architecture(
21-
"msp430",
22-
|custom_handle, handle| Msp430::new(handle, custom_handle),
23-
);
26+
let arch =
27+
binaryninja::architecture::register_architecture("msp430", |custom_handle, handle| {
28+
Msp430::new(handle, custom_handle)
29+
});
2430

2531
// we may need to introduce additional calling conventions here to
2632
// support additional ABIs. MSPGCC's calling convention (what
@@ -55,4 +61,4 @@ pub extern "C" fn CorePluginInit() -> bool {
5561
#[allow(non_snake_case)]
5662
pub extern "C" fn CorePluginDependencies() {
5763
add_optional_plugin_dependency("view_elf");
58-
}
64+
}

0 commit comments

Comments
 (0)