libunicorn_sys/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2
3pub mod unicorn_const;
4
5use core::{fmt, slice};
6use libc::c_char;
7use crate::unicorn_const::{Arch, MemRegion, Mode, Error, HookType, Query};
8
9#[allow(non_camel_case_types)]
10pub type uc_handle = libc::size_t;
11#[allow(non_camel_case_types)]
12pub type uc_hook = libc::size_t;
13#[allow(non_camel_case_types)]
14pub type uc_context = libc::size_t;
15
16extern "C" {
17    pub fn uc_version(major: *const u32, minor: *const u32) -> u32;
18    pub fn uc_arch_supported(arch: Arch) -> bool;
19    pub fn uc_open(arch: Arch, mode: Mode, engine: *mut uc_handle) -> Error;
20    pub fn uc_close(engine: uc_handle) -> Error;
21    pub fn uc_free(mem: libc::size_t) -> Error;
22    pub fn uc_errno(engine: uc_handle) -> Error;
23    pub fn uc_strerror(error_code: Error) -> *const c_char;
24    pub fn uc_reg_write(engine: uc_handle,
25                        regid: libc::c_int,
26                        value: *const libc::c_void)
27                        -> Error;
28    pub fn uc_reg_read(engine: uc_handle, regid: libc::c_int, value: *mut libc::c_void) -> Error;
29    pub fn uc_mem_write(engine: uc_handle,
30                        address: u64,
31                        bytes: *const u8,
32                        size: libc::size_t)
33                        -> Error;
34    pub fn uc_mem_read(engine: uc_handle,
35                       address: u64,
36                       bytes: *mut u8,
37                       size: libc::size_t)
38                       -> Error;
39    pub fn uc_mem_map(engine: uc_handle, address: u64, size: libc::size_t, perms: u32) -> Error;
40    pub fn uc_mem_map_ptr(engine: uc_handle,
41                          address: u64,
42                          size: libc::size_t,
43                          perms: u32,
44                          ptr: *mut libc::c_void)
45                          -> Error;
46    pub fn uc_mem_unmap(engine: uc_handle, address: u64, size: libc::size_t) -> Error;
47    pub fn uc_mem_protect(engine: uc_handle,
48                          address: u64,
49                          size: libc::size_t,
50                          perms: u32)
51                          -> Error;
52    pub fn uc_mem_regions(engine: uc_handle,
53                          regions: *const *const MemRegion,
54                          count: *mut u32)
55                          -> Error;
56    pub fn uc_emu_start(engine: uc_handle,
57                        begin: u64,
58                        until: u64,
59                        timeout: u64,
60                        count: libc::size_t)
61                        -> Error;
62    pub fn uc_emu_stop(engine: uc_handle) -> Error;
63    pub fn uc_hook_add(engine: uc_handle,
64                       hook: *mut uc_hook,
65                       hook_type: HookType,
66                       callback: libc::size_t,
67                       user_data: *mut libc::size_t,
68                       begin: u64,
69                       end: u64,
70                       ...)
71                       -> Error;
72    pub fn uc_hook_del(engine: uc_handle, hook: uc_hook) -> Error;
73    pub fn uc_query(engine: uc_handle, query_type: Query, result: *mut libc::size_t) -> Error;
74    pub fn uc_context_alloc(engine: uc_handle, context: *mut uc_context) -> Error;
75    pub fn uc_context_save(engine: uc_handle, context: uc_context) -> Error;
76    pub fn uc_context_restore(engine: uc_handle, context: uc_context) -> Error;
77}
78
79
80impl Error {
81    pub fn msg(self) -> &'static str {
82        unsafe {
83            let s = uc_strerror(self) as *const u8;
84            core::str::from_utf8(slice::from_raw_parts(s, cstr_len(s))).unwrap_or("")
85        }
86    }
87}
88
89unsafe fn cstr_len(s: *const u8) -> usize {
90    let mut p = s;
91    while 0 != *p { p = p.add(1); }
92    p as usize - s as usize
93}
94
95impl fmt::Display for Error {
96    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { self.msg().fmt(fmt) }
97}
98
99#[cfg(feature = "std")]
100impl std::error::Error for Error {
101    fn description(&self) -> &str { self.msg().as_bytes() }
102
103    fn cause(&self) -> Option<&std::error::Error> { None }
104}