Skip to content

Commit 871cdb6

Browse files
committed
Merge branch 'hook'
2 parents 0190d35 + 80b0356 commit 871cdb6

23 files changed

+109
-103
lines changed

bindings/dotnet/UnicornManaged/Const/Common.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ open System
66

77
[<AutoOpen>]
88
module Common =
9+
let UC_API_MAJOR = 1
910

10-
let UC_API_MAJOR = 0
11-
let UC_API_MINOR = 9
11+
let UC_API_MINOR = 0
1212
let UC_SECOND_SCALE = 1000000
1313
let UC_MILISECOND_SCALE = 1000
1414
let UC_ARCH_ARM = 1

bindings/go/unicorn/unicorn_const.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package unicorn
22
// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT [unicorn_const.go]
33
const (
4+
API_MAJOR = 1
45

5-
API_MAJOR = 0
6-
API_MINOR = 9
6+
API_MINOR = 0
77
SECOND_SCALE = 1000000
88
MILISECOND_SCALE = 1000
99
ARCH_ARM = 1

bindings/java/unicorn/UnicornConst.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
package unicorn;
44

55
public interface UnicornConst {
6+
public static final int UC_API_MAJOR = 1;
67

7-
public static final int UC_API_MAJOR = 0;
8-
public static final int UC_API_MINOR = 9;
8+
public static final int UC_API_MINOR = 0;
99
public static final int UC_SECOND_SCALE = 1000000;
1010
public static final int UC_MILISECOND_SCALE = 1000;
1111
public static final int UC_ARCH_ARM = 1;

bindings/python/sample_x86.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ def test_i386_inout():
291291
mu.hook_add(UC_HOOK_CODE, hook_code)
292292

293293
# handle IN & OUT instruction
294-
mu.hook_add(UC_HOOK_INSN, hook_in, None, UC_X86_INS_IN)
295-
mu.hook_add(UC_HOOK_INSN, hook_out, None, UC_X86_INS_OUT)
294+
mu.hook_add(UC_HOOK_INSN, hook_in, None, 1, 0, UC_X86_INS_IN)
295+
mu.hook_add(UC_HOOK_INSN, hook_out, None, 1, 0, UC_X86_INS_OUT)
296296

297297
# emulate machine code in infinite time
298298
mu.emu_start(ADDRESS, ADDRESS + len(X86_CODE32_INOUT))
@@ -417,7 +417,7 @@ def hook_syscall(mu, user_data):
417417
print('ERROR: was not expecting rax=%d in syscall' % rax)
418418

419419
# hook interrupts for syscall
420-
mu.hook_add(UC_HOOK_INSN, hook_syscall, None, UC_X86_INS_SYSCALL)
420+
mu.hook_add(UC_HOOK_INSN, hook_syscall, None, 1, 0, UC_X86_INS_SYSCALL)
421421

422422
# syscall handler is expecting rax=0x100
423423
mu.reg_write(UC_X86_REG_RAX, 0x100)

bindings/python/shellcode.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def test_i386(mode, code):
9797
mu.hook_add(UC_HOOK_INTR, hook_intr)
9898

9999
# handle SYSCALL
100-
mu.hook_add(UC_HOOK_INSN, hook_syscall, None, UC_X86_INS_SYSCALL)
100+
mu.hook_add(UC_HOOK_INSN, hook_syscall, None, 1, 0, UC_X86_INS_SYSCALL)
101101

102102
# emulate machine code in infinite time
103103
mu.emu_start(ADDRESS, ADDRESS + len(code))

bindings/python/unicorn/unicorn.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def _hook_insn_syscall_cb(self, handle, user_data):
315315

316316

317317
# add a hook
318-
def hook_add(self, htype, callback, user_data=None, arg1=1, arg2=0):
318+
def hook_add(self, htype, callback, user_data=None, begin=1, end=0, arg1=0):
319319
_h2 = uc_hook_h()
320320

321321
# save callback & user_data
@@ -332,30 +332,28 @@ def hook_add(self, htype, callback, user_data=None, arg1=1, arg2=0):
332332
if arg1 in (x86_const.UC_X86_INS_SYSCALL, x86_const.UC_X86_INS_SYSENTER): # SYSCALL/SYSENTER instruction
333333
cb = ctypes.cast(UC_HOOK_INSN_SYSCALL_CB(self._hook_insn_syscall_cb), UC_HOOK_INSN_SYSCALL_CB)
334334
status = _uc.uc_hook_add(self._uch, ctypes.byref(_h2), htype, \
335-
cb, ctypes.cast(self._callback_count, ctypes.c_void_p), insn)
335+
cb, ctypes.cast(self._callback_count, ctypes.c_void_p), ctypes.c_uint64(begin), ctypes.c_uint64(end), insn)
336336
elif htype == UC_HOOK_INTR:
337337
cb = ctypes.cast(UC_HOOK_INTR_CB(self._hook_intr_cb), UC_HOOK_INTR_CB)
338338
status = _uc.uc_hook_add(self._uch, ctypes.byref(_h2), htype, \
339-
cb, ctypes.cast(self._callback_count, ctypes.c_void_p))
339+
cb, ctypes.cast(self._callback_count, ctypes.c_void_p), ctypes.c_uint64(begin), ctypes.c_uint64(end))
340340
else:
341-
begin = ctypes.c_uint64(arg1)
342-
end = ctypes.c_uint64(arg2)
343341
if htype in (UC_HOOK_BLOCK, UC_HOOK_CODE):
344342
# set callback with wrapper, so it can be called
345343
# with this object as param
346344
cb = ctypes.cast(UC_HOOK_CODE_CB(self._hookcode_cb), UC_HOOK_CODE_CB)
347345
status = _uc.uc_hook_add(self._uch, ctypes.byref(_h2), htype, cb, \
348-
ctypes.cast(self._callback_count, ctypes.c_void_p), begin, end)
346+
ctypes.cast(self._callback_count, ctypes.c_void_p), ctypes.c_uint64(begin), ctypes.c_uint64(end))
349347
elif htype & UC_HOOK_MEM_READ_UNMAPPED or htype & UC_HOOK_MEM_WRITE_UNMAPPED or \
350348
htype & UC_HOOK_MEM_FETCH_UNMAPPED or htype & UC_HOOK_MEM_READ_PROT or \
351349
htype & UC_HOOK_MEM_WRITE_PROT or htype & UC_HOOK_MEM_FETCH_PROT:
352350
cb = ctypes.cast(UC_HOOK_MEM_INVALID_CB(self._hook_mem_invalid_cb), UC_HOOK_MEM_INVALID_CB)
353351
status = _uc.uc_hook_add(self._uch, ctypes.byref(_h2), htype, \
354-
cb, ctypes.cast(self._callback_count, ctypes.c_void_p))
352+
cb, ctypes.cast(self._callback_count, ctypes.c_void_p), ctypes.c_uint64(begin), ctypes.c_uint64(end))
355353
else:
356354
cb = ctypes.cast(UC_HOOK_MEM_ACCESS_CB(self._hook_mem_access_cb), UC_HOOK_MEM_ACCESS_CB)
357355
status = _uc.uc_hook_add(self._uch, ctypes.byref(_h2), htype, \
358-
cb, ctypes.cast(self._callback_count, ctypes.c_void_p))
356+
cb, ctypes.cast(self._callback_count, ctypes.c_void_p), ctypes.c_uint64(begin), ctypes.c_uint64(end))
359357

360358
# save the ctype function so gc will leave it alone.
361359
self._ctype_cbs[self._callback_count] = cb

bindings/python/unicorn/unicorn_const.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT [unicorn_const.py]
2+
UC_API_MAJOR = 1
23

3-
UC_API_MAJOR = 0
4-
UC_API_MINOR = 9
4+
UC_API_MINOR = 0
55
UC_SECOND_SCALE = 1000000
66
UC_MILISECOND_SCALE = 1000
77
UC_ARCH_ARM = 1

include/unicorn/unicorn.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ typedef size_t uc_hook;
5757
#endif
5858

5959
// Unicorn API version
60-
#define UC_API_MAJOR 0
61-
#define UC_API_MINOR 9
60+
#define UC_API_MAJOR 1
61+
#define UC_API_MINOR 0
6262

6363
/*
6464
Macro to create combined version which can be compared to
@@ -457,13 +457,19 @@ uc_err uc_emu_stop(uc_engine *uc);
457457
@callback: callback to be run when instruction is hit
458458
@user_data: user-defined data. This will be passed to callback function in its
459459
last argument @user_data
460+
@begin: start address of the area where the callback is effect (inclusive)
461+
@begin: end address of the area where the callback is effect (inclusive)
462+
NOTE 1: the callback is called only if related address is in range [@begin, @end]
463+
NOTE 2: if @begin > @end, callback is called whenever this hook type is triggered
460464
@...: variable arguments (depending on @type)
465+
NOTE: if @type = UC_HOOK_INSN, this is the instruction ID (ex: UC_X86_INS_OUT)
461466
462467
@return UC_ERR_OK on success, or other value on failure (refer to uc_err enum
463468
for detailed error).
464469
*/
465470
UNICORN_EXPORT
466-
uc_err uc_hook_add(uc_engine *uc, uc_hook *hh, int type, void *callback, void *user_data, ...);
471+
uc_err uc_hook_add(uc_engine *uc, uc_hook *hh, int type, void *callback,
472+
void *user_data, uint64_t begin, uint64_t end, ...);
467473

468474
/*
469475
Unregister (remove) a hook callback.

pkgconfig.mk

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# To be used to generate unicorn.pc for pkg-config
33

44
# version major & minor
5-
PKG_MAJOR = 0
6-
PKG_MINOR = 9
5+
PKG_MAJOR = 1
6+
PKG_MINOR = 0
77

88
# version bugfix level. Example: PKG_EXTRA = 1
99
PKG_EXTRA =

samples/mem_apis.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ static void do_nx_demo(bool cause_fault)
168168
}
169169

170170
// intercept code and invalid memory events
171-
if (uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0) != UC_ERR_OK ||
171+
if (uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0) != UC_ERR_OK ||
172172
uc_hook_add(uc, &trace1, UC_HOOK_MEM_INVALID,
173-
hook_mem_invalid, NULL, (uint64_t)1, (uint64_t)0) != UC_ERR_OK) {
173+
hook_mem_invalid, NULL, 1, 0) != UC_ERR_OK) {
174174
printf("not ok - Failed to install hooks\n");
175175
return;
176176
}
@@ -248,10 +248,10 @@ static void do_perms_demo(bool change_perms)
248248
}
249249

250250
// intercept code and invalid memory events
251-
if (uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0) != UC_ERR_OK ||
251+
if (uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0) != UC_ERR_OK ||
252252
uc_hook_add(uc, &trace1,
253253
UC_HOOK_MEM_INVALID,
254-
hook_mem_invalid, NULL, (uint64_t)1, (uint64_t)0) != UC_ERR_OK) {
254+
hook_mem_invalid, NULL, 1, 0) != UC_ERR_OK) {
255255
printf("not ok - Failed to install hooks\n");
256256
return;
257257
}
@@ -326,10 +326,10 @@ static void do_unmap_demo(bool do_unmap)
326326
}
327327

328328
// intercept code and invalid memory events
329-
if (uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0) != UC_ERR_OK ||
329+
if (uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0) != UC_ERR_OK ||
330330
uc_hook_add(uc, &trace1,
331331
UC_HOOK_MEM_INVALID,
332-
hook_mem_invalid, NULL, (uint64_t)1, (uint64_t)0) != UC_ERR_OK) {
332+
hook_mem_invalid, NULL, 1, 0) != UC_ERR_OK) {
333333
printf("not ok - Failed to install hooks\n");
334334
return;
335335
}

samples/sample_arm.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ static void test_arm(void)
7777
uc_reg_write(uc, UC_ARM_REG_R3, &r3);
7878

7979
// tracing all basic blocks with customized callback
80-
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
80+
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
8181

8282
// tracing one instruction at ADDRESS with customized callback
83-
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)ADDRESS, (uint64_t)ADDRESS);
83+
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, ADDRESS, ADDRESS);
8484

8585
// emulate machine code in infinite time (last param = 0), or when
8686
// finishing all the code.
@@ -128,10 +128,10 @@ static void test_thumb(void)
128128
uc_reg_write(uc, UC_ARM_REG_SP, &sp);
129129

130130
// tracing all basic blocks with customized callback
131-
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
131+
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
132132

133133
// tracing one instruction at ADDRESS with customized callback
134-
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)ADDRESS, (uint64_t)ADDRESS);
134+
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, ADDRESS, ADDRESS);
135135

136136
// emulate machine code in infinite time (last param = 0), or when
137137
// finishing all the code.

samples/sample_arm64.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ static void test_arm64(void)
7575
uc_reg_write(uc, UC_ARM64_REG_X15, &x15);
7676

7777
// tracing all basic blocks with customized callback
78-
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
78+
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
7979

8080
// tracing one instruction at ADDRESS with customized callback
81-
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)ADDRESS, (uint64_t)ADDRESS);
81+
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, ADDRESS, ADDRESS);
8282

8383
// emulate machine code in infinite time (last param = 0), or when
8484
// finishing all the code.

samples/sample_m68k.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ static void test_m68k(void)
108108
uc_reg_write(uc, UC_M68K_REG_SR, &sr);
109109

110110
// tracing all basic blocks with customized callback
111-
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
111+
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
112112

113113
// tracing all instruction
114-
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0);
114+
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
115115

116116
// emulate machine code in infinite time (last param = 0), or when
117117
// finishing all the code.

samples/sample_mips.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ static void test_mips_eb(void)
7272
uc_reg_write(uc, UC_MIPS_REG_1, &r1);
7373

7474
// tracing all basic blocks with customized callback
75-
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
75+
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
7676

7777
// tracing one instruction at ADDRESS with customized callback
78-
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)ADDRESS, (uint64_t)ADDRESS);
78+
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, ADDRESS, ADDRESS);
7979

8080
// emulate machine code in infinite time (last param = 0), or when
8181
// finishing all the code.
@@ -122,10 +122,10 @@ static void test_mips_el(void)
122122
uc_reg_write(uc, UC_MIPS_REG_1, &r1);
123123

124124
// tracing all basic blocks with customized callback
125-
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
125+
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
126126

127127
// tracing one instruction at ADDRESS with customized callback
128-
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)ADDRESS, (uint64_t)ADDRESS);
128+
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, ADDRESS, ADDRESS);
129129

130130
// emulate machine code in infinite time (last param = 0), or when
131131
// finishing all the code.

samples/sample_sparc.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ static void test_sparc(void)
7676
uc_reg_write(uc, UC_SPARC_REG_G3, &g3);
7777

7878
// tracing all basic blocks with customized callback
79-
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
79+
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
8080

8181
// tracing all instructions with customized callback
82-
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0);
82+
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
8383

8484
// emulate machine code in infinite time (last param = 0), or when
8585
// finishing all the code.

0 commit comments

Comments
 (0)