Skip to content

Commit 6ad4314

Browse files
committed
Rust binding for LLVM MC disassembler
1 parent 5fe848c commit 6ad4314

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

rust/src/llvm.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
use binaryninjacore_sys::BNLlvmServicesDisasmInstruction;
2-
use std::ffi::CStr;
2+
use std::ffi::{CStr, CString};
3+
use std::os::raw::{c_char, c_int};
34

45
pub fn disas_instruction(triplet: &str, data: &[u8], address64: u64) -> Option<(usize, String)> {
56
unsafe {
6-
let mut buf = vec![0; 256];
7+
let triplet = CString::new(triplet).ok()?;
8+
let mut src = data.to_vec();
9+
let mut buf = vec![0u8; 256];
710
let instr_len = BNLlvmServicesDisasmInstruction(
8-
triplet.as_ptr() as *const i8,
9-
data.as_ptr() as *mut u8,
10-
data.len() as i32,
11+
triplet.as_ptr(),
12+
src.as_mut_ptr(),
13+
src.len() as c_int,
1114
address64,
12-
buf.as_mut_ptr() as *mut i8,
15+
buf.as_mut_ptr() as *mut c_char,
1316
buf.len(),
1417
);
18+
1519
if instr_len > 0 {
16-
let cstr = CStr::from_ptr(buf.as_ptr() as *const i8);
17-
let string = cstr.to_str().unwrap().to_string();
18-
Some((instr_len as usize, string))
20+
// Convert buf (u8) → &CStr by finding the first NUL
21+
if let Some(z) = buf.iter().position(|&b| b == 0) {
22+
let s = CStr::from_bytes_with_nul(&buf[..=z])
23+
.unwrap()
24+
.to_string_lossy()
25+
.into_owned();
26+
Some((instr_len as usize, s))
27+
} else {
28+
// Callee didn't NULL terminate, return an empty string
29+
Some((instr_len as usize, String::new()))
30+
}
1931
} else {
2032
None
2133
}

0 commit comments

Comments
 (0)