Skip to content

Commit a7a1dcc

Browse files
committedFeb 11, 2016
uc_hook_add(): add begin & end arguments for all hook types. also update Python binding after this change
1 parent 55a6874 commit a7a1dcc

18 files changed

+96
-90
lines changed
 

‎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

‎include/unicorn/unicorn.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -458,13 +458,19 @@ uc_err uc_emu_stop(uc_engine *uc);
458458
@callback: callback to be run when instruction is hit
459459
@user_data: user-defined data. This will be passed to callback function in its
460460
last argument @user_data
461+
@begin: start address of the area where the callback is effect (inclusive)
462+
@begin: end address of the area where the callback is effect (inclusive)
463+
NOTE 1: the callback is called only if related address is in range [@begin, @end]
464+
NOTE 2: if @begin > @end, callback is called whenever this hook type is triggered
461465
@...: variable arguments (depending on @type)
466+
NOTE: if @type = UC_HOOK_INSN, this is the instruction ID (ex: UC_X86_INS_OUT)
462467
463468
@return UC_ERR_OK on success, or other value on failure (refer to uc_err enum
464469
for detailed error).
465470
*/
466471
UNICORN_EXPORT
467-
uc_err uc_hook_add(uc_engine *uc, uc_hook *hh, int type, void *callback, void *user_data, ...);
472+
uc_err uc_hook_add(uc_engine *uc, uc_hook *hh, int type, void *callback,
473+
void *user_data, uint64_t begin, uint64_t end, ...);
468474

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

‎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.

‎samples/sample_x86.c

+22-22
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,10 @@ static void test_i386(void)
219219
uc_reg_write(uc, UC_X86_REG_EDX, &r_edx);
220220

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

224224
// tracing all instruction by having @begin > @end
225-
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0);
225+
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
226226

227227
// emulate machine code in infinite time
228228
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(X86_CODE32) - 1, 0, 0);
@@ -289,10 +289,10 @@ static void test_i386_map_ptr(void)
289289
uc_reg_write(uc, UC_X86_REG_EDX, &r_edx);
290290

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

294294
// tracing all instruction by having @begin > @end
295-
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0);
295+
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
296296

297297
// emulate machine code in infinite time
298298
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(X86_CODE32) - 1, 0, 0);
@@ -345,10 +345,10 @@ static void test_i386_jump(void)
345345
}
346346

347347
// tracing 1 basic block with customized callback
348-
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)ADDRESS, (uint64_t)ADDRESS);
348+
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, ADDRESS, ADDRESS);
349349

350350
// tracing 1 instruction at ADDRESS
351-
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)ADDRESS, (uint64_t)ADDRESS);
351+
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, ADDRESS, ADDRESS);
352352

353353
// emulate machine code in infinite time
354354
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(X86_CODE32_JUMP) - 1, 0, 0);
@@ -447,10 +447,10 @@ static void test_i386_invalid_mem_read(void)
447447
uc_reg_write(uc, UC_X86_REG_EDX, &r_edx);
448448

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

452452
// tracing all instruction by having @begin > @end
453-
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0);
453+
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
454454

455455
// emulate machine code in infinite time
456456
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(X86_CODE32_MEM_READ) - 1, 0, 0);
@@ -505,13 +505,13 @@ static void test_i386_invalid_mem_write(void)
505505
uc_reg_write(uc, UC_X86_REG_EDX, &r_edx);
506506

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

510510
// tracing all instruction by having @begin > @end
511-
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0);
511+
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
512512

513513
// intercept invalid memory events
514-
uc_hook_add(uc, &trace3, UC_HOOK_MEM_READ_UNMAPPED | UC_HOOK_MEM_WRITE_UNMAPPED, hook_mem_invalid, NULL);
514+
uc_hook_add(uc, &trace3, UC_HOOK_MEM_READ_UNMAPPED | UC_HOOK_MEM_WRITE_UNMAPPED, hook_mem_invalid, NULL, 1, 0);
515515

516516
// emulate machine code in infinite time
517517
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(X86_CODE32_MEM_WRITE) - 1, 0, 0);
@@ -576,10 +576,10 @@ static void test_i386_jump_invalid(void)
576576
uc_reg_write(uc, UC_X86_REG_EDX, &r_edx);
577577

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

581581
// tracing all instructions by having @begin > @end
582-
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0);
582+
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
583583

584584
// emulate machine code in infinite time
585585
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(X86_CODE32_JMP_INVALID) - 1, 0, 0);
@@ -632,15 +632,15 @@ static void test_i386_inout(void)
632632
uc_reg_write(uc, UC_X86_REG_ECX, &r_ecx);
633633

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

637637
// tracing all instructions
638-
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0);
638+
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
639639

640640
// uc IN instruction
641-
uc_hook_add(uc, &trace3, UC_HOOK_INSN, hook_in, NULL, UC_X86_INS_IN);
641+
uc_hook_add(uc, &trace3, UC_HOOK_INSN, hook_in, NULL, 1, 0, UC_X86_INS_IN);
642642
// uc OUT instruction
643-
uc_hook_add(uc, &trace4, UC_HOOK_INSN, hook_out, NULL, UC_X86_INS_OUT);
643+
uc_hook_add(uc, &trace4, UC_HOOK_INSN, hook_out, NULL, 1, 0, UC_X86_INS_OUT);
644644

645645
// emulate machine code in infinite time
646646
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(X86_CODE32_INOUT) - 1, 0, 0);
@@ -721,16 +721,16 @@ static void test_x86_64(void)
721721
uc_reg_write(uc, UC_X86_REG_R15, &r15);
722722

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

726726
// tracing all instructions in the range [ADDRESS, ADDRESS+20]
727-
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code64, NULL, (uint64_t)ADDRESS, (uint64_t)(ADDRESS+20));
727+
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code64, NULL, ADDRESS, ADDRESS+20);
728728

729729
// tracing all memory WRITE access (with @begin > @end)
730-
uc_hook_add(uc, &trace3, UC_HOOK_MEM_WRITE, hook_mem64, NULL, (uint64_t)1, (uint64_t)0);
730+
uc_hook_add(uc, &trace3, UC_HOOK_MEM_WRITE, hook_mem64, NULL, 1, 0);
731731

732732
// tracing all memory READ access (with @begin > @end)
733-
uc_hook_add(uc, &trace4, UC_HOOK_MEM_READ, hook_mem64, NULL, (uint64_t)1, (uint64_t)0);
733+
uc_hook_add(uc, &trace4, UC_HOOK_MEM_READ, hook_mem64, NULL, 1, 0);
734734

735735
// emulate machine code in infinite time (last param = 0), or when
736736
// finishing all the code.
@@ -804,7 +804,7 @@ static void test_x86_64_syscall(void)
804804
}
805805

806806
// hook interrupts for syscall
807-
uc_hook_add(uc, &trace1, UC_HOOK_INSN, hook_syscall, NULL, UC_X86_INS_SYSCALL);
807+
uc_hook_add(uc, &trace1, UC_HOOK_INSN, hook_syscall, NULL, 1, 0, UC_X86_INS_SYSCALL);
808808

809809
// initialize machine registers
810810
uc_reg_write(uc, UC_X86_REG_RAX, &rax);

‎samples/shellcode.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ static void test_i386(void)
138138
uc_hook_add(uc, &trace1, UC_HOOK_CODE, hook_code, NULL, 1, 0);
139139

140140
// handle interrupt ourself
141-
uc_hook_add(uc, &trace2, UC_HOOK_INTR, hook_intr, NULL);
141+
uc_hook_add(uc, &trace2, UC_HOOK_INTR, hook_intr, NULL, 1, 0);
142142

143143
printf("\n>>> Start tracing this Linux code\n");
144144

‎tests/unit/test_mem_high.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static void test_high_address_reads(void **state)
7979
uint8_t code[] = {0x48,0x8b,0x00,0x90,0x90,0x90,0x90}; // mov rax, [rax], nops
8080
uc_assert_success(uc_mem_map(uc, base_addr, 4096, UC_PROT_ALL));
8181
uc_assert_success(uc_mem_write(uc, base_addr, code, 7));
82-
uc_assert_success(uc_hook_add(uc, &trace2, UC_HOOK_MEM_READ, hook_mem64, NULL, (uint64_t)1, (uint64_t)0));
82+
uc_assert_success(uc_hook_add(uc, &trace2, UC_HOOK_MEM_READ, hook_mem64, NULL, 1, 0));
8383
uc_assert_success(uc_emu_start(uc, base_addr, base_addr + 3, 0, 0));
8484
if(number_of_memory_reads != 1) {
8585
fail_msg("wrong number of memory reads for instruction %i", number_of_memory_reads);

‎tests/unit/test_multihook.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ static void test_basic_blocks(void **state)
9696
OK(uc_mem_write(uc, address, code, sizeof(code)));
9797

9898
// trace all basic blocks
99-
OK(uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, test_basic_blocks_hook, &bbtest, (uint64_t)1, (uint64_t)0));
100-
OK(uc_hook_add(uc, &trace2, UC_HOOK_BLOCK, test_basic_blocks_hook2, &bbtest, (uint64_t)1, (uint64_t)0));
99+
OK(uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, test_basic_blocks_hook, &bbtest, 1, 0));
100+
OK(uc_hook_add(uc, &trace2, UC_HOOK_BLOCK, test_basic_blocks_hook2, &bbtest, 1, 0));
101101

102102
OK(uc_emu_start(uc, address, address+sizeof(code), 0, 0));
103103
}

‎tests/unit/test_pc_change.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static void test_pc_change(void **state)
8383
printf("ECX = %u, EDX = %u\n", r_ecx, r_edx);
8484

8585
// trace all instructions
86-
OK(uc_hook_add(uc, &trace1, UC_HOOK_CODE, test_code_hook, NULL, (uint64_t)1, (uint64_t)0));
86+
OK(uc_hook_add(uc, &trace1, UC_HOOK_CODE, test_code_hook, NULL, 1, 0));
8787

8888
OK(uc_emu_start(uc, address, address+sizeof(code), 0, 0));
8989

‎tests/unit/test_tb_x86.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -273,16 +273,16 @@ static void test_tb_x86_64_32_imul_Gv_Ev_Ib(void **state)
273273
UC_HOOK_CODE,
274274
hook_code32,
275275
NULL,
276-
(uint64_t)1,
277-
(uint64_t)0));
276+
1,
277+
0));
278278

279279
uc_assert_success(uc_hook_add(uc,
280280
&trace2,
281281
UC_HOOK_MEM_VALID,
282282
hook_mem32,
283283
NULL,
284-
(uint64_t)1,
285-
(uint64_t)0));
284+
1,
285+
0));
286286

287287
uc_assert_success(uc_emu_start(uc,
288288
#ifdef RIP_NEXT_TO_THE_SELFMODIFY_OPCODE

‎tests/unit/test_x86.c

+14-14
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static void test_basic_blocks(void **state)
8585
OK(uc_mem_write(uc, address, code, sizeof(code)));
8686

8787
// trace all basic blocks
88-
OK(uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, test_basic_blocks_hook, &bbtest, (uint64_t)1, (uint64_t)0));
88+
OK(uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, test_basic_blocks_hook, &bbtest, 1, 0));
8989

9090
OK(uc_emu_start(uc, address, address+sizeof(code), 0, 0));
9191
}
@@ -144,11 +144,11 @@ static void test_i386(void **state)
144144
uc_assert_success(err);
145145

146146
// tracing all basic blocks with customized callback
147-
err = uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
147+
err = uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
148148
uc_assert_success(err);
149149

150150
// tracing all instruction by having @begin > @end
151-
err = uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0);
151+
err = uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
152152
uc_assert_success(err);
153153

154154
// emulate machine code in infinite time
@@ -194,11 +194,11 @@ static void test_i386_jump(void **state)
194194
uc_assert_success(err);
195195

196196
// tracing 1 basic block with customized callback
197-
err = uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)address, (uint64_t)address);
197+
err = uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, address, address);
198198
uc_assert_success(err);
199199

200200
// tracing 1 instruction at address
201-
err = uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)address, (uint64_t)address);
201+
err = uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, address, address);
202202
uc_assert_success(err);
203203

204204
// emulate machine code in infinite time
@@ -302,19 +302,19 @@ static void test_i386_inout(void **state)
302302
uc_assert_success(err);
303303

304304
// tracing all basic blocks with customized callback
305-
err = uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
305+
err = uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
306306
uc_assert_success(err);
307307

308308
// tracing all instructions
309-
err = uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, (uint64_t)1, (uint64_t)0);
309+
err = uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
310310
uc_assert_success(err);
311311

312312
// uc IN instruction
313-
err = uc_hook_add(uc, &trace3, UC_HOOK_INSN, hook_in, NULL, UC_X86_INS_IN);
313+
err = uc_hook_add(uc, &trace3, UC_HOOK_INSN, hook_in, NULL, 1, 0, UC_X86_INS_IN);
314314
uc_assert_success(err);
315315

316316
// uc OUT instruction
317-
err = uc_hook_add(uc, &trace4, UC_HOOK_INSN, hook_out, NULL, UC_X86_INS_OUT);
317+
err = uc_hook_add(uc, &trace4, UC_HOOK_INSN, hook_out, NULL, 1, 0, UC_X86_INS_OUT);
318318
uc_assert_success(err);
319319

320320
// emulate machine code in infinite time
@@ -566,19 +566,19 @@ static void test_x86_64(void **state)
566566
uc_assert_success(uc_reg_write(uc, UC_X86_REG_R15, &r15));
567567

568568
// tracing all basic blocks with customized callback
569-
err = uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, (uint64_t)1, (uint64_t)0);
569+
err = uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
570570
uc_assert_success(err);
571571

572572
// tracing all instructions in the range [address, address+20]
573-
err = uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code64, NULL, (uint64_t)address, (uint64_t)(address+20));
573+
err = uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code64, NULL, address, address+20);
574574
uc_assert_success(err);
575575

576576
// tracing all memory WRITE access (with @begin > @end)
577-
err = uc_hook_add(uc, &trace3, UC_HOOK_MEM_WRITE, hook_mem64, NULL, (uint64_t)1, (uint64_t)0);
577+
err = uc_hook_add(uc, &trace3, UC_HOOK_MEM_WRITE, hook_mem64, NULL, 1, 0);
578578
uc_assert_success(err);
579579

580580
// tracing all memory READ access (with @begin > @end)
581-
err = uc_hook_add(uc, &trace4, UC_HOOK_MEM_READ, hook_mem64, NULL, (uint64_t)1, (uint64_t)0);
581+
err = uc_hook_add(uc, &trace4, UC_HOOK_MEM_READ, hook_mem64, NULL, 1, 0);
582582
uc_assert_success(err);
583583

584584
// emulate machine code in infinite time (last param = 0), or when
@@ -662,7 +662,7 @@ static void test_x86_64_syscall(void **state)
662662
uc_assert_success(err);
663663

664664
// hook interrupts for syscall
665-
err = uc_hook_add(uc, &trace1, UC_HOOK_INSN, hook_syscall, NULL, UC_X86_INS_SYSCALL);
665+
err = uc_hook_add(uc, &trace1, UC_HOOK_INSN, hook_syscall, NULL, 1, 0, UC_X86_INS_SYSCALL);
666666
uc_assert_success(err);
667667

668668
// initialize machine registers

‎uc.c

+14-12
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ uc_err uc_emu_start(uc_engine* uc, uint64_t begin, uint64_t until, uint64_t time
551551
}
552552
// set up count hook to count instructions.
553553
if (count > 0 && uc->count_hook == 0) {
554-
uc_err err = uc_hook_add(uc, &uc->count_hook, UC_HOOK_CODE, hook_count_cb, NULL);
554+
uc_err err = uc_hook_add(uc, &uc->count_hook, UC_HOOK_CODE, hook_count_cb, NULL, 1, 0);
555555
if (err != UC_ERR_OK) {
556556
return err;
557557
}
@@ -565,6 +565,7 @@ uc_err uc_emu_start(uc_engine* uc, uint64_t begin, uint64_t until, uint64_t time
565565

566566
if (timeout)
567567
enable_emu_timer(uc, timeout * 1000); // microseconds -> nanoseconds
568+
568569
uc->pause_all_vcpus(uc);
569570
// emulation is done
570571
uc->emulation_done = true;
@@ -963,17 +964,19 @@ MemoryRegion *memory_mapping(struct uc_struct* uc, uint64_t address)
963964
}
964965

965966
UNICORN_EXPORT
966-
uc_err uc_hook_add(uc_engine *uc, uc_hook *hh, int type, void *callback, void *user_data, ...)
967+
uc_err uc_hook_add(uc_engine *uc, uc_hook *hh, int type, void *callback,
968+
void *user_data, uint64_t begin, uint64_t end, ...)
967969
{
968-
va_list valist;
969970
int ret = UC_ERR_OK;
970-
971-
va_start(valist, user_data);
971+
int i = 0;
972972

973973
struct hook *hook = calloc(1, sizeof(struct hook));
974974
if (hook == NULL) {
975975
return UC_ERR_NOMEM;
976976
}
977+
978+
hook->begin = begin;
979+
hook->end = end;
977980
hook->type = type;
978981
hook->callback = callback;
979982
hook->user_data = user_data;
@@ -982,22 +985,21 @@ uc_err uc_hook_add(uc_engine *uc, uc_hook *hh, int type, void *callback, void *u
982985

983986
// everybody but HOOK_INSN gets begin/end, so exit early here.
984987
if (type & UC_HOOK_INSN) {
988+
va_list valist;
989+
990+
va_start(valist, end);
985991
hook->insn = va_arg(valist, int);
986-
hook->begin = 1;
987-
hook->end = 0;
992+
va_end(valist);
993+
988994
if (list_append(&uc->hook[UC_HOOK_INSN_IDX], hook) == NULL) {
989995
free(hook);
990996
return UC_ERR_NOMEM;
991997
}
998+
992999
hook->refs++;
9931000
return UC_ERR_OK;
9941001
}
9951002

996-
hook->begin = va_arg(valist, uint64_t);
997-
hook->end = va_arg(valist, uint64_t);
998-
va_end(valist);
999-
1000-
int i = 0;
10011003
while ((type >> i) > 0) {
10021004
if ((type >> i) & 1) {
10031005
// TODO: invalid hook error?

0 commit comments

Comments
 (0)
Please sign in to comment.