add binding support for Go 1.6beta1 (fix #326)

This commit is contained in:
Ryan Hileman 2015-12-18 07:42:11 -08:00
parent f509423cfa
commit c0eabddef3
3 changed files with 46 additions and 40 deletions

View File

@ -1,38 +1,38 @@
#include <unicorn/unicorn.h> #include <unicorn/unicorn.h>
#include "_cgo_export.h" #include "_cgo_export.h"
uc_err uc_hook_add_i1(uc_engine *handle, uc_hook *h2, uc_hook_type type, void *callback, void *user, int arg1) { uc_err uc_hook_add_i1(uc_engine *handle, uc_hook *h2, uc_hook_type type, void *callback, uintptr_t user, int arg1) {
return uc_hook_add(handle, h2, type, callback, user, arg1); return uc_hook_add(handle, h2, type, callback, (void *)user, arg1);
} }
uc_err uc_hook_add_u2(uc_engine *handle, uc_hook *h2, uc_hook_type type, void *callback, void *user, uint64_t arg1, uint64_t arg2) { uc_err uc_hook_add_u2(uc_engine *handle, uc_hook *h2, uc_hook_type type, void *callback, uintptr_t user, uint64_t arg1, uint64_t arg2) {
return uc_hook_add(handle, h2, type, callback, user, arg1, arg2); return uc_hook_add(handle, h2, type, callback, (void *)user, arg1, arg2);
} }
void hookCode_cgo(uc_engine *handle, uint64_t addr, uint32_t size, void *user) { void hookCode_cgo(uc_engine *handle, uint64_t addr, uint32_t size, uintptr_t user) {
hookCode(handle, addr, size, user); hookCode(handle, addr, size, (void *)user);
} }
bool hookMemInvalid_cgo(uc_engine *handle, uc_mem_type type, uint64_t addr, int size, int64_t value, void *user) { bool hookMemInvalid_cgo(uc_engine *handle, uc_mem_type type, uint64_t addr, int size, int64_t value, uintptr_t user) {
return hookMemInvalid(handle, type, addr, size, value, user); return hookMemInvalid(handle, type, addr, size, value, (void *)user);
} }
void hookMemAccess_cgo(uc_engine *handle, uc_mem_type type, uint64_t addr, int size, int64_t value, void *user) { void hookMemAccess_cgo(uc_engine *handle, uc_mem_type type, uint64_t addr, int size, int64_t value, uintptr_t user) {
hookMemAccess(handle, type, addr, size, value, user); hookMemAccess(handle, type, addr, size, value, (void *)user);
} }
void hookInterrupt_cgo(uc_engine *handle, uint32_t intno, void *user) { void hookInterrupt_cgo(uc_engine *handle, uint32_t intno, uintptr_t user) {
hookInterrupt(handle, intno, user); hookInterrupt(handle, intno, (void *)user);
} }
uint32_t hookX86In_cgo(uc_engine *handle, uint32_t port, uint32_t size, void *user) { uint32_t hookX86In_cgo(uc_engine *handle, uint32_t port, uint32_t size, uintptr_t user) {
return hookX86In(handle, port, size, user); return hookX86In(handle, port, size, (void *)user);
} }
void hookX86Out_cgo(uc_engine *handle, uint32_t port, uint32_t size, uint32_t value, void *user) { void hookX86Out_cgo(uc_engine *handle, uint32_t port, uint32_t size, uint32_t value, uintptr_t user) {
hookX86Out(handle, port, size, value, user); hookX86Out(handle, port, size, value, (void *)user);
} }
void hookX86Syscall_cgo(uc_engine *handle, void *user) { void hookX86Syscall_cgo(uc_engine *handle, uintptr_t user) {
hookX86Syscall(handle, user); hookX86Syscall(handle, (void *)user);
} }

View File

@ -18,50 +18,51 @@ type HookData struct {
type Hook uint64 type Hook uint64
var hookToUintptr = make(map[Hook]uintptr)
var hookDataMap = make(map[uintptr]*HookData)
//export hookCode //export hookCode
func hookCode(handle unsafe.Pointer, addr uint64, size uint32, user unsafe.Pointer) { func hookCode(handle unsafe.Pointer, addr uint64, size uint32, user unsafe.Pointer) {
hook := (*HookData)(user) hook := hookDataMap[uintptr(user)]
hook.Callback.(func(Unicorn, uint64, uint32))(hook.Uc, uint64(addr), uint32(size)) hook.Callback.(func(Unicorn, uint64, uint32))(hook.Uc, uint64(addr), uint32(size))
} }
//export hookMemInvalid //export hookMemInvalid
func hookMemInvalid(handle unsafe.Pointer, typ C.uc_mem_type, addr uint64, size int, value int64, user unsafe.Pointer) bool { func hookMemInvalid(handle unsafe.Pointer, typ C.uc_mem_type, addr uint64, size int, value int64, user unsafe.Pointer) bool {
hook := (*HookData)(user) hook := hookDataMap[uintptr(user)]
return hook.Callback.(func(Unicorn, int, uint64, int, int64) bool)(hook.Uc, int(typ), addr, size, value) return hook.Callback.(func(Unicorn, int, uint64, int, int64) bool)(hook.Uc, int(typ), addr, size, value)
} }
//export hookMemAccess //export hookMemAccess
func hookMemAccess(handle unsafe.Pointer, typ C.uc_mem_type, addr uint64, size int, value int64, user unsafe.Pointer) { func hookMemAccess(handle unsafe.Pointer, typ C.uc_mem_type, addr uint64, size int, value int64, user unsafe.Pointer) {
hook := (*HookData)(user) hook := hookDataMap[uintptr(user)]
hook.Callback.(func(Unicorn, int, uint64, int, int64))(hook.Uc, int(typ), addr, size, value) hook.Callback.(func(Unicorn, int, uint64, int, int64))(hook.Uc, int(typ), addr, size, value)
} }
//export hookInterrupt //export hookInterrupt
func hookInterrupt(handle unsafe.Pointer, intno uint32, user unsafe.Pointer) { func hookInterrupt(handle unsafe.Pointer, intno uint32, user unsafe.Pointer) {
hook := (*HookData)(user) hook := hookDataMap[uintptr(user)]
hook.Callback.(func(Unicorn, uint32))(hook.Uc, intno) hook.Callback.(func(Unicorn, uint32))(hook.Uc, intno)
} }
//export hookX86In //export hookX86In
func hookX86In(handle unsafe.Pointer, port, size uint32, user unsafe.Pointer) uint32 { func hookX86In(handle unsafe.Pointer, port, size uint32, user unsafe.Pointer) uint32 {
hook := (*HookData)(user) hook := hookDataMap[uintptr(user)]
return hook.Callback.(func(Unicorn, uint32, uint32) uint32)(hook.Uc, port, size) return hook.Callback.(func(Unicorn, uint32, uint32) uint32)(hook.Uc, port, size)
} }
//export hookX86Out //export hookX86Out
func hookX86Out(handle unsafe.Pointer, port, size, value uint32, user unsafe.Pointer) { func hookX86Out(handle unsafe.Pointer, port, size, value uint32, user unsafe.Pointer) {
hook := (*HookData)(user) hook := hookDataMap[uintptr(user)]
hook.Callback.(func(Unicorn, uint32, uint32, uint32))(hook.Uc, port, size, value) hook.Callback.(func(Unicorn, uint32, uint32, uint32))(hook.Uc, port, size, value)
} }
//export hookX86Syscall //export hookX86Syscall
func hookX86Syscall(handle unsafe.Pointer, user unsafe.Pointer) { func hookX86Syscall(handle unsafe.Pointer, user unsafe.Pointer) {
hook := (*HookData)(user) hook := hookDataMap[uintptr(user)]
hook.Callback.(func(Unicorn))(hook.Uc) hook.Callback.(func(Unicorn))(hook.Uc)
} }
var hookRetain = make(map[Hook]*HookData)
func (u *uc) HookAdd(htype int, cb interface{}, extra ...uint64) (Hook, error) { func (u *uc) HookAdd(htype int, cb interface{}, extra ...uint64) (Hook, error) {
var callback unsafe.Pointer var callback unsafe.Pointer
var iarg1 C.int var iarg1 C.int
@ -100,6 +101,7 @@ func (u *uc) HookAdd(htype int, cb interface{}, extra ...uint64) (Hook, error) {
} }
var h2 C.uc_hook var h2 C.uc_hook
data := &HookData{u, cb} data := &HookData{u, cb}
uptr := uintptr(unsafe.Pointer(data))
if rangeMode { if rangeMode {
if len(extra) == 2 { if len(extra) == 2 {
uarg1 = C.uint64_t(extra[0]) uarg1 = C.uint64_t(extra[0])
@ -107,15 +109,19 @@ func (u *uc) HookAdd(htype int, cb interface{}, extra ...uint64) (Hook, error) {
} else { } else {
uarg1, uarg2 = 1, 0 uarg1, uarg2 = 1, 0
} }
C.uc_hook_add_u2(u.handle, &h2, C.uc_hook_type(htype), callback, unsafe.Pointer(data), uarg1, uarg2) C.uc_hook_add_u2(u.handle, &h2, C.uc_hook_type(htype), callback, C.uintptr_t(uptr), uarg1, uarg2)
} else { } else {
C.uc_hook_add_i1(u.handle, &h2, C.uc_hook_type(htype), callback, unsafe.Pointer(data), iarg1) C.uc_hook_add_i1(u.handle, &h2, C.uc_hook_type(htype), callback, C.uintptr_t(uptr), iarg1)
} }
hookRetain[Hook(h2)] = data hookDataMap[uptr] = data
hookToUintptr[Hook(h2)] = uptr
return Hook(h2), nil return Hook(h2), nil
} }
func (u *uc) HookDel(hook Hook) error { func (u *uc) HookDel(hook Hook) error {
delete(hookRetain, hook) if uptr, ok := hookToUintptr[hook]; ok {
delete(hookToUintptr, hook)
delete(hookDataMap, uptr)
}
return errReturn(C.uc_hook_del(u.handle, C.uc_hook(hook))) return errReturn(C.uc_hook_del(u.handle, C.uc_hook(hook)))
} }

View File

@ -1,9 +1,9 @@
uc_err uc_hook_add_i1(uc_engine *handle, uc_hook *h2, uc_hook_type type, void *callback, void *user_data, int arg1); uc_err uc_hook_add_i1(uc_engine *handle, uc_hook *h2, uc_hook_type type, void *callback, uintptr_t user, int arg1);
uc_err uc_hook_add_u2(uc_engine *handle, uc_hook *h2, uc_hook_type type, void *callback, void *user_data, uint64_t arg1, uint64_t arg2); uc_err uc_hook_add_u2(uc_engine *handle, uc_hook *h2, uc_hook_type type, void *callback, uintptr_t user, uint64_t arg1, uint64_t arg2);
void hookCode_cgo(uc_engine *handle, uint64_t addr, uint32_t size, void *user); void hookCode_cgo(uc_engine *handle, uint64_t addr, uint32_t size, uintptr_t user);
bool hookMemInvalid_cgo(uc_engine *handle, uc_mem_type type, uint64_t addr, int size, int64_t value, void *user); bool hookMemInvalid_cgo(uc_engine *handle, uc_mem_type type, uint64_t addr, int size, int64_t value, uintptr_t user);
void hookMemAccess_cgo(uc_engine *handle, uc_mem_type type, uint64_t addr, int size, int64_t value, void *user); void hookMemAccess_cgo(uc_engine *handle, uc_mem_type type, uint64_t addr, int size, int64_t value, uintptr_t user);
void hookInterrupt_cgo(uc_engine *handle, uint32_t intno, void *user); void hookInterrupt_cgo(uc_engine *handle, uint32_t intno, uintptr_t user);
uint32_t hookX86In_cgo(uc_engine *handle, uint32_t port, uint32_t size, void *user); uint32_t hookX86In_cgo(uc_engine *handle, uint32_t port, uint32_t size, uintptr_t user);
void hookX86Out_cgo(uc_engine *handle, uint32_t port, uint32_t size, uint32_t value, void *user); void hookX86Out_cgo(uc_engine *handle, uint32_t port, uint32_t size, uint32_t value, uintptr_t user);
void hookX86Syscall_cgo(uc_engine *handle, void *user); void hookX86Syscall_cgo(uc_engine *handle, uintptr_t user);