From c0eabddef3025cc3469a5be16239eb1aadb641ec Mon Sep 17 00:00:00 2001 From: Ryan Hileman Date: Fri, 18 Dec 2015 07:42:11 -0800 Subject: [PATCH] add binding support for Go 1.6beta1 (fix #326) --- bindings/go/unicorn/hook.c | 36 ++++++++++++++++++------------------ bindings/go/unicorn/hook.go | 32 +++++++++++++++++++------------- bindings/go/unicorn/hook.h | 18 +++++++++--------- 3 files changed, 46 insertions(+), 40 deletions(-) diff --git a/bindings/go/unicorn/hook.c b/bindings/go/unicorn/hook.c index 2a9b0e79..4ed41b51 100644 --- a/bindings/go/unicorn/hook.c +++ b/bindings/go/unicorn/hook.c @@ -1,38 +1,38 @@ #include #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) { - return uc_hook_add(handle, h2, type, callback, user, 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, (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) { - return uc_hook_add(handle, h2, type, callback, user, arg1, 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, (void *)user, arg1, arg2); } -void hookCode_cgo(uc_engine *handle, uint64_t addr, uint32_t size, void *user) { - hookCode(handle, addr, size, user); +void hookCode_cgo(uc_engine *handle, uint64_t addr, uint32_t size, uintptr_t 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) { - return hookMemInvalid(handle, type, addr, size, value, 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, (void *)user); } -void hookMemAccess_cgo(uc_engine *handle, uc_mem_type type, uint64_t addr, int size, int64_t value, void *user) { - hookMemAccess(handle, type, addr, size, value, 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, (void *)user); } -void hookInterrupt_cgo(uc_engine *handle, uint32_t intno, void *user) { - hookInterrupt(handle, intno, user); +void hookInterrupt_cgo(uc_engine *handle, uint32_t intno, uintptr_t user) { + hookInterrupt(handle, intno, (void *)user); } -uint32_t hookX86In_cgo(uc_engine *handle, uint32_t port, uint32_t size, void *user) { - return hookX86In(handle, port, size, user); +uint32_t hookX86In_cgo(uc_engine *handle, uint32_t port, uint32_t size, uintptr_t 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) { - hookX86Out(handle, port, size, value, user); +void hookX86Out_cgo(uc_engine *handle, uint32_t port, uint32_t size, uint32_t value, uintptr_t user) { + hookX86Out(handle, port, size, value, (void *)user); } -void hookX86Syscall_cgo(uc_engine *handle, void *user) { - hookX86Syscall(handle, user); +void hookX86Syscall_cgo(uc_engine *handle, uintptr_t user) { + hookX86Syscall(handle, (void *)user); } diff --git a/bindings/go/unicorn/hook.go b/bindings/go/unicorn/hook.go index 13aba755..d5dfc874 100644 --- a/bindings/go/unicorn/hook.go +++ b/bindings/go/unicorn/hook.go @@ -18,50 +18,51 @@ type HookData struct { type Hook uint64 +var hookToUintptr = make(map[Hook]uintptr) +var hookDataMap = make(map[uintptr]*HookData) + //export hookCode 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)) } //export hookMemInvalid 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) } //export hookMemAccess 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) } //export hookInterrupt 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) } //export hookX86In 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) } //export hookX86Out 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) } //export hookX86Syscall func hookX86Syscall(handle unsafe.Pointer, user unsafe.Pointer) { - hook := (*HookData)(user) + hook := hookDataMap[uintptr(user)] hook.Callback.(func(Unicorn))(hook.Uc) } -var hookRetain = make(map[Hook]*HookData) - func (u *uc) HookAdd(htype int, cb interface{}, extra ...uint64) (Hook, error) { var callback unsafe.Pointer 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 data := &HookData{u, cb} + uptr := uintptr(unsafe.Pointer(data)) if rangeMode { if len(extra) == 2 { uarg1 = C.uint64_t(extra[0]) @@ -107,15 +109,19 @@ func (u *uc) HookAdd(htype int, cb interface{}, extra ...uint64) (Hook, error) { } else { 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 { - 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 } 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))) } diff --git a/bindings/go/unicorn/hook.h b/bindings/go/unicorn/hook.h index 0254a0ac..6c209516 100644 --- a/bindings/go/unicorn/hook.h +++ b/bindings/go/unicorn/hook.h @@ -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_u2(uc_engine *handle, uc_hook *h2, uc_hook_type type, void *callback, void *user_data, uint64_t arg1, uint64_t arg2); -void hookCode_cgo(uc_engine *handle, uint64_t addr, uint32_t size, void *user); -bool hookMemInvalid_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, void *user); -void hookInterrupt_cgo(uc_engine *handle, uint32_t intno, void *user); -uint32_t hookX86In_cgo(uc_engine *handle, uint32_t port, uint32_t size, void *user); -void hookX86Out_cgo(uc_engine *handle, uint32_t port, uint32_t size, uint32_t value, void *user); -void hookX86Syscall_cgo(uc_engine *handle, void *user); +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, uintptr_t user, uint64_t arg1, uint64_t arg2); +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, uintptr_t 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, uintptr_t 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, uintptr_t user); +void hookX86Syscall_cgo(uc_engine *handle, uintptr_t user);