Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private DirectMappingKeystoneNative() {
* @param numberOfStatements number of statements successfully processed
* @return 0 on success, or -1 on failure.
*/
public static native int ks_asm(Pointer engine, String assembly, int address, PointerByReference machineCodeBuffer,
public static native int ks_asm(Pointer engine, String assembly, long address, PointerByReference machineCodeBuffer,
IntByReference machineCodeSize, IntByReference numberOfStatements);

/**
Expand Down Expand Up @@ -176,4 +176,4 @@ public static native int ks_asm(Pointer engine, String assembly, int address, Po
* @return An hexadecimal number as (major << 8 | minor), which encodes both major & minor versions.
*/
public static native int ks_version(IntByReference major, IntByReference minor);
}
}
3 changes: 3 additions & 0 deletions bindings/python/keystone/keystone_const.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
KS_MODE_ARM = 1
KS_MODE_THUMB = 16
KS_MODE_V8 = 64
KS_MODE_V4 = 128
KS_MODE_XSCALE = 256
KS_MODE_V5 = 512
KS_MODE_MICRO = 16
KS_MODE_MIPS3 = 32
KS_MODE_MIPS32R6 = 64
Expand Down
8 changes: 7 additions & 1 deletion bindings/python/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ def sym_resolver(symbol, value):
test_ks(KS_ARCH_ARM, KS_MODE_ARM + KS_MODE_BIG_ENDIAN, b"sub r1, r2, r5")
test_ks(KS_ARCH_ARM, KS_MODE_THUMB, b"movs r4, #0xf0")
test_ks(KS_ARCH_ARM, KS_MODE_THUMB + KS_MODE_BIG_ENDIAN, b"movs r4, #0xf0")

test_ks(KS_ARCH_ARM, KS_MODE_ARM + KS_MODE_V4, b"nop")
test_ks(KS_ARCH_ARM, KS_MODE_ARM + KS_MODE_BIG_ENDIAN + KS_MODE_V4, b"nop")
test_ks(KS_ARCH_ARM, KS_MODE_ARM + KS_MODE_XSCALE, b"nop")
test_ks(KS_ARCH_ARM, KS_MODE_ARM + KS_MODE_BIG_ENDIAN + KS_MODE_XSCALE, b"nop")
test_ks(KS_ARCH_ARM, KS_MODE_ARM + KS_MODE_V5, b"nop")
test_ks(KS_ARCH_ARM, KS_MODE_ARM + KS_MODE_BIG_ENDIAN + KS_MODE_V5, b"nop")

# ARM64
test_ks(KS_ARCH_ARM64, KS_MODE_LITTLE_ENDIAN, b"ldr w1, [sp, #0x8]")

Expand Down
1 change: 1 addition & 0 deletions bindings/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def copy_sources():

dir_util.copy_tree("../../llvm", "src/llvm/")
dir_util.copy_tree("../../include", "src/include/")
dir_util.copy_tree("../../suite", "src/suite")

src.extend(glob.glob("../../*.h"))
src.extend(glob.glob("../../*.cpp"))
Expand Down
10 changes: 5 additions & 5 deletions bindings/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "keystone"
version = "0.9.1"
version = "0.10.0"
authors = [
"Remco Verhoef <[email protected]>",
"Tasuku SUENAGA a.k.a. gunyarakun <[email protected]>"
Expand All @@ -10,17 +10,17 @@ license = "GPL-2.0"
readme = "README.md"
repository = "https://github.com/keystone-engine/keystone"
include = [
"src/*", "tests/*", "Cargo.toml", "COPYING", "README.md",
"keystone-sys/*"
"src/*", "tests/*", "Cargo.toml", "COPYING", "README.md"
]

[dependencies]
bitflags = "1.0"
libc = "0.2"
keystone-sys = { path = "keystone-sys", version = "0.9.1" }
keystone-sys = { path = "keystone-sys", version = "0.10" }

[features]
default = []

use_system_keystone = ["keystone-sys/use_system_keystone"]
build_keystone_cmake = ["keystone-sys/build_keystone_cmake"]

[workspace]
9 changes: 6 additions & 3 deletions bindings/rust/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ package: keystone-sys/keystone
cd keystone-sys && cargo package -vv
cargo package -vv

# For packaging we need to embed the keystone source in the crate
keystone-sys/keystone:
rsync -a ../.. keystone-sys/keystone --exclude bindings --filter ":- ../../.gitignore"

clean:
rm -rf target/ keystone-sys/target/ keystone-sys/keystone/
rm -rf keystone-sys/keystone/
cargo clean

check:
cargo test
# Make sure to only use one test thread as keystone isn't thread-safe
cargo test -- --test-threads=1

gen_const:
cd .. && python const_generator.py rust
cd .. && python2 const_generator.py rust
cargo fmt
1 change: 1 addition & 0 deletions bindings/rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ If you want to use keystone already installed in the system, specify `use_system
```
[dependencies.keystone]
version = "0.10.0"
default-features = false
features = ["use_system_keystone"]
```

Expand Down
12 changes: 4 additions & 8 deletions bindings/rust/keystone-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "keystone-sys"
version = "0.9.0"
version = "0.10.0"
authors = [
"Remco Verhoef <[email protected]>",
"Tasuku SUENAGA a.k.a. gunyarakun <[email protected]>"
Expand All @@ -10,21 +10,17 @@ repository = "https://github.com/keystone-engine/keystone"
documentation = "https://docs.rs/keystone/"
license = "GPL-2.0"
build = "build.rs"
exclude = [
"keystone/build/**"
]

[build-dependencies]
build-helper = "0.1"
os_type = "2.0"
pkg-config = { optional = true, version = "0.3" }
cmake = { optional = true, version = "0.1" }

[dependencies]
bitflags = "1.0"
libc = "0.2"

[features]
default = []
default = ["build_keystone_cmake"]

use_system_keystone = ["pkg-config"]
build_keystone_cmake = []
build_keystone_cmake = ["cmake"]
82 changes: 34 additions & 48 deletions bindings/rust/keystone-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,67 +1,53 @@
#[cfg(feature = "build_keystone_cmake")]
extern crate cmake;
#[cfg(feature = "use_system_keystone")]
extern crate pkg_config;

use std::env;
use std::path::PathBuf;
use std::process::Command;
#[cfg(all(not(windows), feature = "build_keystone_cmake"))]
use std::os::unix::fs::symlink;
#[cfg(all(windows, feature = "build_keystone_cmake"))]
use std::os::windows::fs::symlink_dir as symlink;

fn build_with_cmake() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let cmake_dir = PathBuf::from("keystone");
let build_dir = cmake_dir.join("build");
#[cfg(feature = "build_keystone_cmake")]
use std::path::Path;

if !cmake_dir.exists() {
run(Command::new("ln").arg("-s").arg("../../..").arg("keystone"));
#[cfg(feature = "build_keystone_cmake")]
fn build_with_cmake() {
if !Path::new("keystone").exists() {
// This only happens when using the crate via a `git` reference as the
// published version already embeds keystone's source.
let pwd = std::env::current_dir().unwrap();
let keystone_dir = pwd.ancestors().skip(3).next().unwrap();
symlink(keystone_dir, "keystone").expect("failed to symlink keystone");
}

run(Command::new("mkdir")
.current_dir(&cmake_dir)
.arg("-p")
.arg("build"));

run(Command::new("../make-share.sh").current_dir(&build_dir));
let dest = cmake::Config::new("keystone")
.define("BUILD_LIBS_ONLY", "1")
.define("BUILD_SHARED_LIBS", "OFF")
.define("LLVM_TARGETS_TO_BUILD", "all")
// Prevent python from leaving behind `.pyc` files which break `cargo package`
.env("PYTHONDONTWRITEBYTECODE", "1")
.build();

run(Command::new("cmake").current_dir(&build_dir).args(&[
&format!("-DCMAKE_INSTALL_PREFIX={}", out_dir.display()),
"-DCMAKE_BUILD_TYPE=Release",
"-DBUILD_LIBS_ONLY=1",
"-DCMAKE_OSX_ARCHITECTURES=",
"-DBUILD_SHARED_LIBS=ON",
"-DLLVM_TARGET_ARCH=host",
"-G",
"Unix Makefiles",
"..",
]));

run(Command::new("make").current_dir(&build_dir).arg("install"));

println!("cargo:rustc-link-search=native={}/lib", out_dir.display());
println!("cargo:rustc-link-search=native={}/lib", dest.display());
println!("cargo:rustc-link-lib=keystone");

let target = std::env::var("TARGET").unwrap();
if target.contains("apple") {
println!("cargo:rustc-link-lib=dylib=c++");
} else if target.contains("linux") {
println!("cargo:rustc-link-lib=dylib=stdc++");
} else if target.contains("windows") {
println!("cargo:rustc-link-lib=dylib=shell32");
}
}

fn main() {
if cfg!(feature = "use_system_keystone") {
#[cfg(feature = "use_system_keystone")]
pkg_config::find_library("keystone").expect("Could not find system keystone");
} else {
#[cfg(feature = "build_keystone_cmake")]
build_with_cmake();
}
}

fn run(cmd: &mut Command) {
println!("run: {:?}", cmd);
let status = match cmd.status() {
Ok(s) => s,
Err(ref e) => fail(&format!("failed to execute command: {}", e)),
};
if !status.success() {
fail(&format!(
"command did not execute successfully, got: {}",
status
));
}
}

fn fail(s: &str) -> ! {
panic!("\n{}\n\nbuild script failed, must exit now", s);
}
11 changes: 5 additions & 6 deletions bindings/rust/keystone-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ extern crate libc;

pub mod keystone_const;

use std::fmt;
use keystone_const::{Arch, Error, Mode, OptionType, OptionValue};
use std::ffi::CStr;
use std::fmt;
use std::os::raw::c_char;
use keystone_const::{Arch, Error, Mode, OptionType, OptionValue};

#[allow(non_camel_case_types)]
pub type ks_handle = libc::size_t;

#[link(name = "keystone")]
extern "C" {
pub fn ks_version(major: *const u32, minor: *const u32) -> u32;
pub fn ks_version(major: *mut u32, minor: *mut u32) -> u32;
pub fn ks_arch_supported(arch: Arch) -> bool;
pub fn ks_open(arch: Arch, mode: Mode, engine: *mut ks_handle) -> Error;
pub fn ks_asm(
Expand All @@ -38,8 +37,8 @@ extern "C" {
}

impl Error {
pub fn msg(&self) -> String {
error_msg(*self)
pub fn msg(self) -> String {
error_msg(self)
}
}

Expand Down
12 changes: 5 additions & 7 deletions bindings/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Keystone Assembler Engine (www.keystone-engine.org) */
//! By Nguyen Anh Quynh <[email protected]>, 2016 */
//! Rust bindings by Remco Verhoef <[email protected]>, 2016 */
//! Keystone Assembler Engine (www.keystone-engine.org) \
//! By Nguyen Anh Quynh <[email protected]>, 2016 \
//! Rust bindings by Remco Verhoef <[email protected]>, 2016
//!
//! ```rust
//! extern crate keystone;
Expand All @@ -16,12 +16,10 @@
//! }
//! ```

#![doc(html_root_url = "https://keystone/doc/here/v1")]

extern crate keystone_sys as ffi;
extern crate libc;

use std::ffi::CString;
use std::ffi::{CStr, CString};
use std::fmt;

pub use ffi::keystone_const::*;
Expand Down Expand Up @@ -66,7 +64,7 @@ pub fn arch_supported(arch: Arch) -> bool {

pub fn error_msg(error: Error) -> String {
unsafe {
CStr::from_ptr(ffi::ks_strerror(error.bits()))
CStr::from_ptr(ffi::ks_strerror(error))
.to_string_lossy()
.into_owned()
}
Expand Down
3 changes: 3 additions & 0 deletions include/keystone/keystone.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ typedef enum ks_mode {
KS_MODE_ARM = 1 << 0, // ARM mode
KS_MODE_THUMB = 1 << 4, // THUMB mode (including Thumb-2)
KS_MODE_V8 = 1 << 6, // ARMv8 A32 encodings for ARM
KS_MODE_V4 = 1 << 7,
KS_MODE_XSCALE = 1 << 8,
KS_MODE_V5 = 1 << 9,
// mips
KS_MODE_MICRO = 1 << 4, // MicroMips mode
KS_MODE_MIPS3 = 1 << 5, // Mips III ISA
Expand Down
31 changes: 31 additions & 0 deletions kstool/kstool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ static void usage(char *prog)
if (ks_arch_supported(KS_ARCH_ARM)) {
printf(" arm: ARM - little endian\n");
printf(" armbe: ARM - big endian\n");
printf(" armv4t: ARM V4T - little endian\n");
printf(" armv4tbe: ARM V4T - big endian\n");
printf(" armv5: ARM V5 - little endian\n");
printf(" armv5be: ARM V5 - big endian\n");
printf(" xscale: XSCALE - little endian\n");
printf(" xscale: XSCALE - big endian\n");
printf(" thumb: Thumb - little endian\n");
printf(" thumbbe: Thumb - big endian\n");
printf(" armv8: ARM V8 - little endian\n");
Expand Down Expand Up @@ -208,6 +214,31 @@ int main(int argc, char **argv)
err = ks_open(KS_ARCH_ARM, KS_MODE_ARM+KS_MODE_BIG_ENDIAN, &ks);
}

if (!strcmp(mode, "xscale")) {
err = ks_open(KS_ARCH_ARM, KS_MODE_XSCALE+KS_MODE_LITTLE_ENDIAN+KS_MODE_ARM, &ks);
}

if (!strcmp(mode, "xscalebe")) {
err = ks_open(KS_ARCH_ARM, KS_MODE_XSCALE+KS_MODE_BIG_ENDIAN+KS_MODE_ARM, &ks);
}

if (!strcmp(mode, "armv4t")) {
err = ks_open(KS_ARCH_ARM, KS_MODE_V4+KS_MODE_LITTLE_ENDIAN+KS_MODE_ARM, &ks);
}

if (!strcmp(mode, "armv4tbe")) {
err = ks_open(KS_ARCH_ARM, KS_MODE_V4+KS_MODE_BIG_ENDIAN+KS_MODE_ARM, &ks);
}

if (!strcmp(mode, "armv5")) {
err = ks_open(KS_ARCH_ARM, KS_MODE_V5+KS_MODE_LITTLE_ENDIAN+KS_MODE_ARM, &ks);
}

if (!strcmp(mode, "armv5be")) {
err = ks_open(KS_ARCH_ARM, KS_MODE_V5+KS_MODE_BIG_ENDIAN+KS_MODE_ARM, &ks);
}


if (!strcmp(mode, "thumb")) {
err = ks_open(KS_ARCH_ARM, KS_MODE_THUMB+KS_MODE_LITTLE_ENDIAN, &ks);
}
Expand Down
Loading