Skip to content

Commit

Permalink
Fix pre-generated capstone bindings update
Browse files Browse the repository at this point in the history
Also reformatted code with rustfmt.
  • Loading branch information
tmfink committed Aug 20, 2017
1 parent 0df01e0 commit 08b3817
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 76 deletions.
66 changes: 41 additions & 25 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,20 @@ fn find_capstone_header(header_search_paths: &Vec<PathBuf>, name: &str) -> Optio

/// Create bindings using bindgen
fn write_bindgen_bindings(header_search_paths: &Vec<PathBuf>, update_pregenerated_bindings: bool) {
debug_println!("Writing bindgen bindings with search paths {:?}",
header_search_paths);
debug_println!(
"Writing bindgen bindings with search paths {:?}",
header_search_paths
);


let mut builder = bindgen::Builder::default()
.unstable_rust(false)
.header(find_capstone_header(header_search_paths, "capstone.h")
.expect("Could not find header")
.to_str()
.unwrap())
.header(
find_capstone_header(header_search_paths, "capstone.h")
.expect("Could not find header")
.to_str()
.unwrap(),
)
.disable_name_namespacing()
.prepend_enum_name(false)
.generate_comments(true)
Expand All @@ -108,17 +112,23 @@ fn write_bindgen_bindings(header_search_paths: &Vec<PathBuf>, update_pregenerate

// Write bindings to $OUT_DIR/bindings.rs
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join(BINDINGS_FILE);
bindings
.write_to_file(out_path.clone())
.expect("Unable to write bindings");

if update_pregenerated_bindings {
let stored_bindgen_header: PathBuf = [
env::var("CARGO_MANIFEST_DIR").expect("Could not find cargo environment variable"),
"pre_generated".into(),
BINDINGS_FILE.into()
].iter().collect();
debug_println!("Updating capstone bindings at \"{}\"", stored_bindgen_header.to_str().unwrap());
bindings.write_to_file(out_path.clone()).expect(
"Unable to write bindings",
);

if update_pregenerated_bindings {
debug_println!("Updating pre-generated capstone bindings");
let stored_bindgen_header: PathBuf =
[
env::var("CARGO_MANIFEST_DIR").expect("Could not find cargo environment variable"),
"pre_generated".into(),
BINDINGS_FILE.into(),
].iter()
.collect();
debug_println!(
"Updating capstone bindings at \"{}\"",
stored_bindgen_header.to_str().unwrap()
);
copy(out_path, stored_bindgen_header).expect("Unable to update capstone bindings");
}
}
Expand All @@ -132,8 +142,10 @@ fn main() {
if cfg!(feature = "use_system_capstone") {
debug_println!("Using system capstone library");

assert!(!cfg!(feature = "build_capstone_cmake"),
"build_capstone_cmake feature is only valid when using bundled cmake");
assert!(
!cfg!(feature = "build_capstone_cmake"),
"build_capstone_cmake feature is only valid when using bundled cmake"
);

let capstone_lib =
pkg_config::find_library("capstone").expect("Could not find system capstone");
Expand Down Expand Up @@ -178,11 +190,15 @@ fn main() {
// If UPDATE_CAPSTONE_BINDINGS is set, then updated the pre-generated capstone bindings
let update_pregenerated_bindings = env::var("UPDATE_CAPSTONE_BINDINGS").is_ok();
if update_pregenerated_bindings {
assert!(cfg!(feature = "use_bundled_capstone_bindings"),
concat!("Setting UPDATE_CAPSTONE_BINDINGS only makes ",
"sense when NOT enabling feature use_bundled_capstone_bindings"));
} else {
debug_println!("Creating capstone bindings with bindgen");
write_bindgen_bindings(&header_search_paths, update_pregenerated_bindings);
assert!(
!cfg!(feature = "use_bundled_capstone_bindings"),
concat!(
"Setting UPDATE_CAPSTONE_BINDINGS only makes ",
"sense when NOT enabling feature use_bundled_capstone_bindings"
)
);
}

debug_println!("Creating capstone bindings with bindgen");
write_bindgen_bindings(&header_search_paths, update_pregenerated_bindings);
}
68 changes: 35 additions & 33 deletions common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Needs to be included with include! macro

#[derive(Clone,Copy,PartialEq,Eq,Debug,Hash)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
/// Information specific to architecture
struct CapstoneArchInfo<'a> {
/// name of C header
Expand All @@ -12,37 +12,39 @@ struct CapstoneArchInfo<'a> {
cs_name: &'a str,
}

static ARCH_INCLUDES: &'static [CapstoneArchInfo<'static>] = &[CapstoneArchInfo {
header_name: "arm.h",
cs_name: "arm",
},
CapstoneArchInfo {
header_name: "arm64.h",
cs_name: "arm64",
},
CapstoneArchInfo {
header_name: "mips.h",
cs_name: "mips",
},
CapstoneArchInfo {
header_name: "ppc.h",
cs_name: "ppc",
},
CapstoneArchInfo {
header_name: "sparc.h",
cs_name: "sparc",
},
CapstoneArchInfo {
header_name: "systemz.h",
cs_name: "sysz",
},
CapstoneArchInfo {
header_name: "x86.h",
cs_name: "x86",
},
CapstoneArchInfo {
header_name: "xcore.h",
cs_name: "xcore",
}];
static ARCH_INCLUDES: &'static [CapstoneArchInfo<'static>] = &[
CapstoneArchInfo {
header_name: "arm.h",
cs_name: "arm",
},
CapstoneArchInfo {
header_name: "arm64.h",
cs_name: "arm64",
},
CapstoneArchInfo {
header_name: "mips.h",
cs_name: "mips",
},
CapstoneArchInfo {
header_name: "ppc.h",
cs_name: "ppc",
},
CapstoneArchInfo {
header_name: "sparc.h",
cs_name: "sparc",
},
CapstoneArchInfo {
header_name: "systemz.h",
cs_name: "sysz",
},
CapstoneArchInfo {
header_name: "x86.h",
cs_name: "x86",
},
CapstoneArchInfo {
header_name: "xcore.h",
cs_name: "xcore",
},
];

static BINDINGS_FILE: &'static str = "capstone.rs";
3 changes: 1 addition & 2 deletions pre_generated/capstone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ pub enum arm64_cc {
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum arm64_mrs_reg {
pub enum arm64_sysreg {
ARM64_SYSREG_INVALID = 0,
ARM64_SYSREG_MDCCSR_EL0 = 38920,
ARM64_SYSREG_DBGDTRRX_EL0 = 38952,
Expand Down Expand Up @@ -1344,7 +1344,6 @@ pub enum arm64_mrs_reg {
ARM64_SYSREG_ICH_EISR_EL2 = 58971,
ARM64_SYSREG_ICH_ELSR_EL2 = 58973,
}
pub use self::arm64_mrs_reg as arm64_sysreg;
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum arm64_msr_reg {
Expand Down
48 changes: 32 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ use std::os::raw::c_int;

// Include pre-generated bindgen bindings
#[cfg(feature = "use_bundled_capstone_bindings")]
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/pre_generated/capstone.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/pre_generated/capstone.rs"
));

// Include dynamically generated bindings
#[cfg(not(feature = "use_bundled_capstone_bindings"))]
Expand Down Expand Up @@ -228,7 +231,11 @@ mod test {
// Union structs
let op = cs_ppc_op {
type_: ppc_op_type::PPC_OP_REG,
__bindgen_anon_1: new_bindgen_union!(cs_ppc_op__bindgen_ty_1, reg, ppc_reg::PPC_REG_CARRY),
__bindgen_anon_1: new_bindgen_union!(
cs_ppc_op__bindgen_ty_1,
reg,
ppc_reg::PPC_REG_CARRY
),
};
cs_ppc {
bc: ppc_bc::PPC_BC_LT,
Expand Down Expand Up @@ -450,9 +457,11 @@ mod test {
println!("Capstone version (major, minor) = {:?}", (major, minor));

assert!(major == 3, "Invalid major version {:?}", major);
assert!(minor >= 0 && minor < 1000,
"Invalid minor version {:?}",
minor);
assert!(
minor >= 0 && minor < 1000,
"Invalid minor version {:?}",
minor
);
}

#[test]
Expand Down Expand Up @@ -485,12 +494,14 @@ mod test {
let mut insn_ptr: *mut cs_insn = 0 as *mut cs_insn;
let mut address = 0x1000;
let count = unsafe {
cs_disasm(handle,
code_bytes.as_ptr(),
code_bytes.len(),
address,
0,
&mut insn_ptr as *mut *mut cs_insn)
cs_disasm(
handle,
code_bytes.as_ptr(),
code_bytes.len(),
address,
0,
&mut insn_ptr as *mut *mut cs_insn,
)
};

assert!(count == code.len());
Expand All @@ -515,11 +526,16 @@ mod test {

#[test]
fn test_x86_disassembly() {
let code: &[(&[u8], &str, _)] =
&[(&[0x48, 0x83, 0xec, 0x08], "sub", x86_insn::X86_INS_SUB as u32),
(&[0x31, 0xdb], "xor", x86_insn::X86_INS_XOR as u32),
(&[0xc3], "ret", x86_insn::X86_INS_RET as u32),
(&[0x90], "nop", x86_insn::X86_INS_NOP as u32)];
let code: &[(&[u8], &str, _)] = &[
(
&[0x48, 0x83, 0xec, 0x08],
"sub",
x86_insn::X86_INS_SUB as u32,
),
(&[0x31, 0xdb], "xor", x86_insn::X86_INS_XOR as u32),
(&[0xc3], "ret", x86_insn::X86_INS_RET as u32),
(&[0x90], "nop", x86_insn::X86_INS_NOP as u32),
];
test_disassembly_helper(cs_arch::CS_ARCH_X86, cs_mode::CS_MODE_LITTLE_ENDIAN, code);
}
}

0 comments on commit 08b3817

Please sign in to comment.