Skip to content

Commit 0429572

Browse files
committed
Remove UnicornInner in Rust bindings
Fix #1619
1 parent 0ebac3b commit 0429572

File tree

3 files changed

+106
-147
lines changed

3 files changed

+106
-147
lines changed

bindings/rust/build.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use pkg_config;
21
use std::env;
32
use std::path::PathBuf;
43
use std::process::Command;

bindings/rust/src/ffi.rs

+48-41
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,33 @@ extern "C" {
8888
}
8989

9090
pub struct UcHook<'a, D: 'a, F: 'a> {
91+
pub unicorn: *mut Unicorn<'a, D>,
9192
pub callback: F,
92-
pub uc: Unicorn<'a, D>,
9393
}
9494

9595
pub trait IsUcHook<'a> {}
9696

9797
impl<'a, D, F> IsUcHook<'a> for UcHook<'a, D, F> {}
9898

99+
macro_rules! destructure_hook {
100+
($hook:ident) => {{
101+
let UcHook { unicorn, callback } = unsafe { &mut *$hook };
102+
(unsafe { &mut **unicorn }, callback)
103+
}};
104+
}
105+
99106
pub extern "C" fn mmio_read_callback_proxy<D, F>(
100107
uc: uc_handle,
101108
offset: u64,
102109
size: usize,
103110
user_data: *mut UcHook<D, F>,
104111
) -> u64
105112
where
106-
F: FnMut(&mut crate::Unicorn<D>, u64, usize) -> u64,
113+
F: FnMut(&mut Unicorn<D>, u64, usize) -> u64,
107114
{
108-
let user_data = unsafe { &mut *user_data };
109-
debug_assert_eq!(uc, user_data.uc.get_handle());
110-
(user_data.callback)(&mut user_data.uc, offset, size)
115+
let (unicorn, callback) = destructure_hook!(user_data);
116+
debug_assert_eq!(uc, unicorn.get_handle());
117+
callback(unicorn, offset, size)
111118
}
112119

113120
pub extern "C" fn mmio_write_callback_proxy<D, F>(
@@ -117,11 +124,11 @@ pub extern "C" fn mmio_write_callback_proxy<D, F>(
117124
value: u64,
118125
user_data: *mut UcHook<D, F>,
119126
) where
120-
F: FnMut(&mut crate::Unicorn<D>, u64, usize, u64),
127+
F: FnMut(&mut Unicorn<D>, u64, usize, u64),
121128
{
122-
let user_data = unsafe { &mut *user_data };
123-
debug_assert_eq!(uc, user_data.uc.get_handle());
124-
(user_data.callback)(&mut user_data.uc, offset, size, value);
129+
let (unicorn, callback) = destructure_hook!(user_data);
130+
debug_assert_eq!(uc, unicorn.get_handle());
131+
callback(unicorn, offset, size, value);
125132
}
126133

127134
pub extern "C" fn code_hook_proxy<D, F>(
@@ -130,11 +137,11 @@ pub extern "C" fn code_hook_proxy<D, F>(
130137
size: u32,
131138
user_data: *mut UcHook<D, F>,
132139
) where
133-
F: FnMut(&mut crate::Unicorn<D>, u64, u32),
140+
F: FnMut(&mut Unicorn<D>, u64, u32),
134141
{
135-
let user_data = unsafe { &mut *user_data };
136-
debug_assert_eq!(uc, user_data.uc.get_handle());
137-
(user_data.callback)(&mut user_data.uc, address, size);
142+
let (unicorn, callback) = destructure_hook!(user_data);
143+
debug_assert_eq!(uc, unicorn.get_handle());
144+
callback(unicorn, address, size);
138145
}
139146

140147
pub extern "C" fn block_hook_proxy<D, F>(
@@ -143,11 +150,11 @@ pub extern "C" fn block_hook_proxy<D, F>(
143150
size: u32,
144151
user_data: *mut UcHook<D, F>,
145152
) where
146-
F: FnMut(&mut crate::Unicorn<D>, u64, u32),
153+
F: FnMut(&mut Unicorn<D>, u64, u32),
147154
{
148-
let user_data = unsafe { &mut *user_data };
149-
debug_assert_eq!(uc, user_data.uc.get_handle());
150-
(user_data.callback)(&mut user_data.uc, address, size);
155+
let (unicorn, callback) = destructure_hook!(user_data);
156+
debug_assert_eq!(uc, unicorn.get_handle());
157+
callback(unicorn, address, size);
151158
}
152159

153160
pub extern "C" fn mem_hook_proxy<D, F>(
@@ -159,20 +166,20 @@ pub extern "C" fn mem_hook_proxy<D, F>(
159166
user_data: *mut UcHook<D, F>,
160167
) -> bool
161168
where
162-
F: FnMut(&mut crate::Unicorn<D>, MemType, u64, usize, i64) -> bool,
169+
F: FnMut(&mut Unicorn<D>, MemType, u64, usize, i64) -> bool,
163170
{
164-
let user_data = unsafe { &mut *user_data };
165-
debug_assert_eq!(uc, user_data.uc.get_handle());
166-
(user_data.callback)(&mut user_data.uc, mem_type, address, size as usize, value)
171+
let (unicorn, callback) = destructure_hook!(user_data);
172+
debug_assert_eq!(uc, unicorn.get_handle());
173+
callback(unicorn, mem_type, address, size as usize, value)
167174
}
168175

169176
pub extern "C" fn intr_hook_proxy<D, F>(uc: uc_handle, value: u32, user_data: *mut UcHook<D, F>)
170177
where
171-
F: FnMut(&mut crate::Unicorn<D>, u32),
178+
F: FnMut(&mut Unicorn<D>, u32),
172179
{
173-
let user_data = unsafe { &mut *user_data };
174-
debug_assert_eq!(uc, user_data.uc.get_handle());
175-
(user_data.callback)(&mut user_data.uc, value);
180+
let (unicorn, callback) = destructure_hook!(user_data);
181+
debug_assert_eq!(uc, unicorn.get_handle());
182+
callback(unicorn, value);
176183
}
177184

178185
pub extern "C" fn insn_in_hook_proxy<D, F>(
@@ -181,20 +188,20 @@ pub extern "C" fn insn_in_hook_proxy<D, F>(
181188
size: usize,
182189
user_data: *mut UcHook<D, F>,
183190
) where
184-
F: FnMut(&mut crate::Unicorn<D>, u32, usize) -> u32,
191+
F: FnMut(&mut Unicorn<D>, u32, usize) -> u32,
185192
{
186-
let user_data = unsafe { &mut *user_data };
187-
debug_assert_eq!(uc, user_data.uc.get_handle());
188-
(user_data.callback)(&mut user_data.uc, port, size);
193+
let (unicorn, callback) = destructure_hook!(user_data);
194+
debug_assert_eq!(uc, unicorn.get_handle());
195+
callback(unicorn, port, size);
189196
}
190197

191198
pub extern "C" fn insn_invalid_hook_proxy<D, F>(uc: uc_handle, user_data: *mut UcHook<D, F>) -> bool
192199
where
193-
F: FnMut(&mut crate::Unicorn<D>) -> bool,
200+
F: FnMut(&mut Unicorn<D>) -> bool,
194201
{
195-
let user_data = unsafe { &mut *user_data };
196-
debug_assert_eq!(uc, user_data.uc.get_handle());
197-
(user_data.callback)(&mut user_data.uc)
202+
let (unicorn, callback) = destructure_hook!(user_data);
203+
debug_assert_eq!(uc, unicorn.get_handle());
204+
callback(unicorn)
198205
}
199206

200207
pub extern "C" fn insn_out_hook_proxy<D, F>(
@@ -204,18 +211,18 @@ pub extern "C" fn insn_out_hook_proxy<D, F>(
204211
value: u32,
205212
user_data: *mut UcHook<D, F>,
206213
) where
207-
F: FnMut(&mut crate::Unicorn<D>, u32, usize, u32),
214+
F: FnMut(&mut Unicorn<D>, u32, usize, u32),
208215
{
209-
let user_data = unsafe { &mut *user_data };
210-
debug_assert_eq!(uc, user_data.uc.get_handle());
211-
(user_data.callback)(&mut user_data.uc, port, size, value);
216+
let (unicorn, callback) = destructure_hook!(user_data);
217+
debug_assert_eq!(uc, unicorn.get_handle());
218+
callback(unicorn, port, size, value);
212219
}
213220

214221
pub extern "C" fn insn_sys_hook_proxy<D, F>(uc: uc_handle, user_data: *mut UcHook<D, F>)
215222
where
216-
F: FnMut(&mut crate::Unicorn<D>),
223+
F: FnMut(&mut Unicorn<D>),
217224
{
218-
let user_data = unsafe { &mut *user_data };
219-
debug_assert_eq!(uc, user_data.uc.get_handle());
220-
(user_data.callback)(&mut user_data.uc);
225+
let (unicorn, callback) = destructure_hook!(user_data);
226+
debug_assert_eq!(uc, unicorn.get_handle());
227+
callback(unicorn);
221228
}

0 commit comments

Comments
 (0)