Skip to content

Commit 70db095

Browse files
committed
Updates Rust binding to the latest version.
Fixed build in disasmtool_lix.
1 parent fe6a937 commit 70db095

27 files changed

Lines changed: 1888 additions & 724 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# bddisasm-sys changelog
2+
3+
## Unreleased
4+
5+
### Changed
6+
7+
- the crate is now a `no_std` crate (this adds a dependency on [cty](https://crates.io/crates/cty))

bindings/rsbddisasm/bddisasm-sys/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bddisasm-sys"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
authors = ["Cristi Anichitei <ianichitei@bitdefender.com>"]
55
edition = "2018"
66
links = "bddisasm"
@@ -12,14 +12,15 @@ documentation = "https://docs.rs/bddisasm-sys"
1212
description = """
1313
Bindings to bddisasm instruction decoder library
1414
"""
15-
categories = ["external-ffi-bindings", "hardware-support"]
15+
categories = ["external-ffi-bindings", "hardware-support", "no_std"]
1616
keywords = ["disassembler", "decoder", "x86", "amd64", "x86_64"]
1717

1818
[lib]
1919
name = "bddisasm_sys"
2020
path = "src/lib.rs"
2121

2222
[dependencies]
23+
cty = "0.2.2"
2324

2425
[build-dependencies]
2526
bindgen = "0.59.1"
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# bddisasm-sys
22

3-
Rust bindings for [bddisasm](https://github.com/bitdefender/bddisasm).
3+
`no_std` Rust bindings for [bddisasm](https://github.com/bitdefender/bddisasm).
44

55
See [bddisasm](https://crates.io/crates/bddisasm) if you're looking for a Rust wrapper for these bindings.
66

77
## Requirements
88

99
[bindgen](https://crates.io/crates/bindgen) is used to generate the bindings at build time. Because of this, users need to have `clang` installed. Check the [bindgen documentation](https://rust-lang.github.io/rust-bindgen/requirements.html) for more information.
10+
11+
## Notes
12+
13+
While this crate is `no_std`, the `bddisasm` library it links against depends on a C library because it needs `vsnprintf` and `memset`. It is possible to [work around this limitation](https://github.com/bitdefender/bddisasm#nd_vsnprintf_s-and-nd_memset), but this is not currently done for these bindings.

bindings/rsbddisasm/bddisasm-sys/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ fn main() {
1111
cc::Build::new()
1212
.file("csrc/bddisasm/bddisasm.c")
1313
.file("csrc/bddisasm/bdformat.c")
14+
.file("csrc/bddisasm/bdhelpers.c")
1415
.file("csrc/bddisasm/crt.c")
1516
.include("csrc/bddisasm/include")
1617
.include("csrc/inc")
@@ -24,6 +25,8 @@ fn main() {
2425
.allowlist_type("ND.*")
2526
.allowlist_var("ND.*")
2627
.rustified_enum(".*")
28+
.ctypes_prefix("cty")
29+
.use_core()
2730
.impl_debug(true)
2831
.generate_comments(false)
2932
.parse_callbacks(Box::new(bindgen::CargoCallbacks))

bindings/rsbddisasm/bddisasm-sys/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! [bindgen](https://crates.io/crates/bindgen) is used to generate the bindings at build time. Because of this, users
1212
//! need to have `clang` installed.
1313
//! Check the [bindgen documentation](https://rust-lang.github.io/rust-bindgen/requirements.html) for more information.
14+
#![cfg_attr(not(test), no_std)]
1415
#![allow(non_camel_case_types)]
1516
#![allow(non_snake_case)]
1617
#![allow(non_upper_case_globals)]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# bddisasm changelog
2+
3+
## Unreleased
4+
5+
### Added
6+
7+
- implement `Decoder::decode_next_with_offset`
8+
- implement ``Decoder::decode_next_with_ip`
9+
- `OperandLookup` struct which makes working with operands easier
10+
- re-export `bddisasm_sys` as `ffi`
11+
- re-export commonly used items
12+
- Implement `as_*` and `is_*` accessors for the `OpInfo` enum
13+
14+
### Changed
15+
16+
- the crate is now a `no_std` crate
17+
- public types no longer implement `From` and no longer `panic!` when an unexpected value is encountered

bindings/rsbddisasm/bddisasm/Cargo.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bddisasm"
3-
version = "0.1.0"
3+
version = "0.1.2"
44
authors = ["Cristi Anichitei <ianichitei@bitdefender.com>"]
55
edition = "2018"
66
license = "Apache-2.0"
@@ -15,3 +15,13 @@ keywords = ["disassembler", "decoder", "x86", "amd64", "x86_64"]
1515

1616
[dependencies]
1717
bddisasm-sys = { version = "0.1.0", path = "../bddisasm-sys" }
18+
19+
[features]
20+
std = []
21+
22+
[package.metadata."docs.rs"]
23+
all-features = true
24+
25+
[dev-dependencies]
26+
anyhow = "1.0"
27+
clap = "2.34.0"

bindings/rsbddisasm/bddisasm/README.md

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
# bddisasm
1+
# bddisasm x86/x64 instruction decoder
22

3-
Rust bindings for the [bddisasm](https://github.com/bitdefender/bddisasm) x86/x64 decoder library, built on top
4-
of [bddisasm-sys](https://crates.io/crates/bddisasm-sys).
3+
`no_std` Rust bindings for the [bddisasm](https://github.com/bitdefender/bddisasm) x86/x64 decoder library, built
4+
on top of [bddisasm-sys](https://crates.io/crates/bddisasm-sys).
55

6-
It supports all existing x86 instruction, offering a wide range of information about each one, including:
6+
It supports all existing 16-bit, 32-bit and 64-bit instructions, offering a wide range of information about each one,
7+
including:
78

8-
- operands (implicit and explicit)
9+
- implicit operands
10+
- explicit operands
911
- access mode for each operand
1012
- CPUID feature flags
1113
- CPU modes in which an instruction is valid
@@ -23,8 +25,11 @@ bddisasm = "0.1.0"
2325

2426
### Decoding one instruction
2527

26-
```rust
27-
use bddisasm::decoded_instruction::{DecodedInstruction, DecodeMode, Mnemonic};
28+
Use [`DecodedInstruction::decode`](https://docs.rs/bddisasm/latest/bddisasm/decoded_instruction/struct.DecodedInstruction.html#method.decode)
29+
to decode an instruction from a chunk of code.
30+
31+
```Rust
32+
use bddisasm::{DecodedInstruction, DecodeMode, Mnemonic};
2833

2934
let code = vec![0x31, 0xc0];
3035
match DecodedInstruction::decode(&code, DecodeMode::Bits32) {
@@ -38,8 +43,11 @@ match DecodedInstruction::decode(&code, DecodeMode::Bits32) {
3843

3944
### Decoding multiple instructions
4045

41-
```rust
42-
use bddisasm::decoder::{Decoder, DecodeMode};
46+
Use [`Decoder`](https://docs.rs/bddisasm/latest/bddisasm/decoder/struct.Decoder.html) to decode multiple instructions
47+
from a chunk of code.
48+
49+
```Rust
50+
use bddisasm::{Decoder, DecodeMode};
4351

4452
let code = [
4553
// ENCLS
@@ -70,21 +78,58 @@ the provided input buffer is too small
7078
WRMSR
7179
```
7280

81+
Use [`Decoder::decode_next_with_info`](https://docs.rs/bddisasm/latest/bddisasm/decoder/struct.Decoder.html#method.decode_next_with_info)
82+
to get information about the offset inside the code chunk at which an instruction was decoded from.
83+
84+
```Rust
85+
use bddisasm::{Decoder, DecodeMode};
86+
87+
let code = [
88+
// ENCLS
89+
0x0f, 0x01, 0xcf,
90+
// MOV rax, qword ptr [rbx+rcx*4+0x1234]
91+
0x48, 0x8b, 0x84, 0x8b, 0x34, 0x12, 0x00, 0x00,
92+
// Not a valid instruction
93+
0x0f,
94+
// WRMSR
95+
0x0f, 0x30,
96+
];
97+
let mut decoder = Decoder::new(&code, DecodeMode::Bits64, 0x1234);
98+
99+
100+
// Keep decoding until there's nothing left to decode
101+
while let Some((result, offset, _)) = decoder.decode_next_with_info() {
102+
match result {
103+
Ok(ins) => println!("{:#x} {}", offset, ins),
104+
Err(e) => println!("Error: `{}` at offset {:#x}", e, offset),
105+
}
106+
}
107+
```
108+
109+
This will print:
110+
111+
```text
112+
0x0 ENCLS
113+
0x3 MOV rax, qword ptr [rbx+rcx*4+0x1234]
114+
Error `the provided input buffer is too small` at offset 0xb
115+
0xc WRMSR
116+
```
117+
73118
### Working with instruction operands
74119

75-
Rich informaion is offered for each type of operand. Bellow is a minimal example that looks at a memory operand.
120+
Instruction operands can be analyzed using the [operand](https://docs.rs/bddisasm/latest/bddisasm/operand/index.html)
121+
module. Rich informaion is offered for each type of operand. Bellow is a minimal example that looks at a memory operand.
76122

77-
```rust
78-
# use bddisasm::decode_error::DecodeError;
79-
# fn test() -> Result<(), DecodeError> {
80-
use bddisasm::decoded_instruction::{DecodedInstruction, DecodeMode};
81-
use bddisasm::operand::OpInfo;
123+
```Rust
124+
use bddisasm::{DecodedInstruction, DecodeMode, OpInfo};
82125

83126
// ` MOV rax, qword ptr [rcx+r15*2]`
84127
let code = b"\x4a\x8b\x04\x79";
85128
let ins = DecodedInstruction::decode(code, DecodeMode::Bits64).unwrap();
129+
86130
// Get the operands
87131
let operands = ins.operands();
132+
88133
// Get the second operand which is the source (`[rcx+r15*2]`)
89134
let src = operands[1];
90135

@@ -117,8 +162,6 @@ match src.info {
117162
},
118163
_ => unreachable!(),
119164
}
120-
# Ok(())
121-
# }
122165
```
123166

124167
Will print:
@@ -131,6 +174,11 @@ Scale: 2
131174
No displacement
132175
```
133176

134-
## Requirements
177+
## Accessing the raw bindings
178+
179+
The raw `bddisasm_sys` bindings are available via the `ffi` re-export.
180+
181+
## Feature Flags
135182

136-
Because [bddisasm-sys](https://crates.io/crates/bddisasm-sys) uses [bindgen](https://crates.io/crates/bindgen) to generate the bindings at build time, users need to have `clang` installed. Check the [bindgen documentation](https://rust-lang.github.io/rust-bindgen/requirements.html) for more information.
183+
- `std` - adds a `std` dependency - the only visible difference when doing this is that [`DecodeError`] implements
184+
the `Error` trait

0 commit comments

Comments
 (0)