From cf0a3d75a8c821a43f99976218ea977ecbd6d1e6 Mon Sep 17 00:00:00 2001 From: cherepanov74 Date: Sun, 30 Aug 2015 16:34:23 +0200 Subject: [PATCH 1/7] Added MingW instruction & converted some tabs to spaces --- COMPILE.TXT | 115 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 47 deletions(-) diff --git a/COMPILE.TXT b/COMPILE.TXT index 57c2a629..ec8ea564 100644 --- a/COMPILE.TXT +++ b/COMPILE.TXT @@ -66,11 +66,11 @@ Unicorn requires few dependent packages as followings - To compile for current platform, run: - $ ./make.sh + $ ./make.sh - On 64-bit OS, run the command below to cross-compile Unicorn for 32-bit binary: - $ ./make.sh nix32 + $ ./make.sh nix32 @@ -78,11 +78,11 @@ Unicorn requires few dependent packages as followings To install Unicorn, run: - $ sudo ./make.sh install + $ sudo ./make.sh install - For FreeBSD/OpenBSD, where sudo is unavailable, run: + For FreeBSD/OpenBSD, where sudo is unavailable, run: - $ su; ./make.sh install + $ su; ./make.sh install Users are then required to enter root password to copy Unicorn into machine system directories. @@ -93,17 +93,17 @@ Unicorn requires few dependent packages as followings NOTE: The core framework installed by "./make.sh install" consist of following files: - /usr/include/unicorn/unicorn.h - /usr/include/unicorn/x86.h - /usr/include/unicorn/arm.h - /usr/include/unicorn/arm64.h - /usr/include/unicorn/mips.h - /usr/include/unicorn/ppc.h - /usr/include/unicorn/sparc.h - /usr/include/unicorn/m68k.h - /usr/include/unicorn/platform.h - /usr/lib/libunicorn.so (for Linux/*nix), or /usr/lib/libunicorn.dylib (OSX) - /usr/lib/libunicorn.a + /usr/include/unicorn/unicorn.h + /usr/include/unicorn/x86.h + /usr/include/unicorn/arm.h + /usr/include/unicorn/arm64.h + /usr/include/unicorn/mips.h + /usr/include/unicorn/ppc.h + /usr/include/unicorn/sparc.h + /usr/include/unicorn/m68k.h + /usr/include/unicorn/platform.h + /usr/lib/libunicorn.so (for Linux/*nix), or /usr/lib/libunicorn.dylib (OSX) + /usr/lib/libunicorn.a @@ -112,19 +112,18 @@ Unicorn requires few dependent packages as followings To cross-compile for Windows, Linux & gcc-mingw-w64-i686 (and also gcc-mingw-w64-x86-64 for 64-bit binaries) are required. - - To cross-compile Windows 32-bit binary, simply run: + - To cross-compile Windows 32-bit binary, simply run: - $ ./make.sh cross-win32 + $ ./make.sh cross-win32 - - To cross-compile Windows 64-bit binary, run: + - To cross-compile Windows 64-bit binary, run: - $ ./make.sh cross-win64 + $ ./make.sh cross-win64 - Resulted files libunicorn.dll, libunicorn.dll.a & tests/test*.exe can then + Resulted files unicorn.dll, unicorn.lib & samples/sample*.exe can then be used on Windows machine. To run sample_x86.exe on Windows 32-bit, you need the following files: - - unicorn.dll - /usr/i686-w64-mingw32/sys-root/mingw/bin/libglib-2.0-0.dll - /usr/lib/gcc/i686-w64-mingw32/4.8/libgcc_s_sjlj-1.dll @@ -145,17 +144,17 @@ Unicorn requires few dependent packages as followings To cross-compile for iOS (iPhone/iPad/iPod), Mac OSX with XCode installed is required. - - To cross-compile for ArmV7 (iPod 4, iPad 1/2/3, iPhone4, iPhone4S), run: - $ ./make.sh ios_armv7 + - To cross-compile for ArmV7 (iPod 4, iPad 1/2/3, iPhone4, iPhone4S), run: + $ ./make.sh ios_armv7 - - To cross-compile for ArmV7s (iPad 4, iPhone 5C, iPad mini), run: - $ ./make.sh ios_armv7s + - To cross-compile for ArmV7s (iPad 4, iPhone 5C, iPad mini), run: + $ ./make.sh ios_armv7s - - To cross-compile for Arm64 (iPhone 5S, iPad mini Retina, iPad Air), run: - $ ./make.sh ios_arm64 + - To cross-compile for Arm64 (iPhone 5S, iPad mini Retina, iPad Air), run: + $ ./make.sh ios_arm64 - - To cross-compile for all iDevices (armv7 + armv7s + arm64), run: - $ ./make.sh ios + - To cross-compile for all iDevices (armv7 + armv7s + arm64), run: + $ ./make.sh ios Resulted files libunicorn.dylib, libunicorn.a & tests/test* can then be used on iOS devices. @@ -167,47 +166,69 @@ Unicorn requires few dependent packages as followings To cross-compile for Android (smartphone/tablet), Android NDK is required. NOTE: Only ARM and ARM64 are currently supported. - $ NDK=/android/android-ndk-r10e ./make.sh cross-android arm + $ NDK=/android/android-ndk-r10e ./make.sh cross-android arm or - $ NDK=/android/android-ndk-r10e ./make.sh cross-android arm64 + $ NDK=/android/android-ndk-r10e ./make.sh cross-android arm64 Resulted files libunicorn.so, libunicorn.a & tests/test* can then be used on Android devices. -[7] Compile on Windows with Cygwin +[7] Compile on Windows with MinGW (MSYS2) - To compile under Cygwin gcc-mingw-w64-i686 or x86_64-w64-mingw32 run: + To compile with MinGW you need to install MSYS2: https://msys2.github.io/ + Follow the install instructions and don't forget to update the system packages as written in 5 & 6 paragraphs + + - To compile Windows 32-bit binary with MinGW, run: + $ pacman -S make + $ pacman -S pkg-config + $ pacman -S mingw-w64-i686-glib2 + $ pacman -S mingw-w64-i686-toolchain + $ ./make.sh cross-win32 - - To compile Windows 32-bit binary under Cygwin, run: + - To compile Windows 64-bit binary with MinGW, run: + $ pacman -S make + $ pacman -S pkg-config + $ pacman -S mingw-w64-x86_64-glib2 + $ pacman -S mingw-w64-x86_64-toolchain + $ ./make.sh cross-win64 - $ ./make.sh cygwin-mingw32 - - - To compile Windows 64-bit binary under Cygwin, run: - - $ ./make.sh cygwin-mingw64 - - Resulted files libunicorn.dll, libunicorn.dll.a & tests/test*.exe can then + Resulted files unicorn.dll, unicorn.lib & samples/sample*.exe can then be used on Windows machine. + To run sample_x86.exe on Windows 32-bit, you need the following files: + - unicorn.dll + - C:\msys32\mingw32\bin\libiconv-2.dll + - C:\msys32\mingw32\bin\libintl-8.dll + - C:\msys32\mingw32\bin\libglib-2.0-0.dll + - C:\msys32\mingw32\bin\libgcc_s_seh-1.dll + - C:\msys32\mingw32\bin\libwinpthread-1.dll + + To run sample_x86.exe on Windows 64-bit, you need the following files: + - unicorn.dll + - C:\msys64\mingw64\bin\libiconv-2.dll + - C:\msys64\mingw64\bin\libintl-8.dll + - C:\msys64\mingw64\bin\libglib-2.0-0.dll + - C:\msys64\mingw64\bin\libgcc_s_seh-1.dll + - C:\msys64\mingw64\bin\libwinpthread-1.dll [8] By default, "cc" (default C compiler on the system) is used as compiler. - - To use "clang" compiler instead, run the command below: + - To use "clang" compiler instead, run the command below: - $ ./make.sh clang + $ ./make.sh clang - - To use "gcc" compiler instead, run: + - To use "gcc" compiler instead, run: - $ ./make.sh gcc + $ ./make.sh gcc [9] To uninstall Unicorn, run the command below: - $ sudo ./make.sh uninstall + $ sudo ./make.sh uninstall From 1b6469e60fb31341f1c56cb182f76860e98206c3 Mon Sep 17 00:00:00 2001 From: Sean Heelan Date: Sun, 30 Aug 2015 22:42:48 +0700 Subject: [PATCH 2/7] Regression test for issue #82 (emulator hang on jmp REG) --- regress/jmp_ebx_hang.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 regress/jmp_ebx_hang.py diff --git a/regress/jmp_ebx_hang.py b/regress/jmp_ebx_hang.py new file mode 100755 index 00000000..21b057d2 --- /dev/null +++ b/regress/jmp_ebx_hang.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +"""See https://github.com/unicorn-engine/unicorn/issues/82""" + +import unicorn +CODE_ADDR = 0x10101000 +CODE = b'\xff\xe3' +mu = unicorn.Uc(unicorn.UC_ARCH_X86, unicorn.UC_MODE_32) +mu.mem_map(CODE_ADDR, 1024 * 4) +mu.mem_write(CODE_ADDR, CODE) +mu.reg_write(unicorn.x86_const.UC_X86_REG_EBX, 0x0) + +print "jmp ebx, with ebx == 0" +try: + mu.emu_start(CODE_ADDR, CODE_ADDR + 2, count=1) +except unicorn.UcError as e: + print "Error: %s" % e + +mu = unicorn.Uc(unicorn.UC_ARCH_X86, unicorn.UC_MODE_32) +mu.mem_map(CODE_ADDR, 1024 * 4) +# If we write this address to EBX then the emulator hangs on emu_start +mu.reg_write(unicorn.x86_const.UC_X86_REG_EBX, 0xaa96a47f) +mu.mem_write(CODE_ADDR, CODE) +print "jmp ebx, with ebx == 0xaa96a47f" +try: + mu.emu_start(CODE_ADDR, CODE_ADDR + 2, count=1) +except unicorn.UcError as e: + print "Error: %s" % e From 4b05e736a191c3a86f73bbd0eccc0c800d4d8389 Mon Sep 17 00:00:00 2001 From: Sean Heelan Date: Sun, 30 Aug 2015 22:50:47 +0700 Subject: [PATCH 3/7] Use asserts instead of print statements on the correct paths to avoid confusing people as to what the success indicator is here. --- regress/jmp_ebx_hang.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/regress/jmp_ebx_hang.py b/regress/jmp_ebx_hang.py index 21b057d2..1b78d89c 100755 --- a/regress/jmp_ebx_hang.py +++ b/regress/jmp_ebx_hang.py @@ -8,21 +8,26 @@ CODE = b'\xff\xe3' mu = unicorn.Uc(unicorn.UC_ARCH_X86, unicorn.UC_MODE_32) mu.mem_map(CODE_ADDR, 1024 * 4) mu.mem_write(CODE_ADDR, CODE) +# If EBX is zero then an exception is raised, as expected mu.reg_write(unicorn.x86_const.UC_X86_REG_EBX, 0x0) -print "jmp ebx, with ebx == 0" try: mu.emu_start(CODE_ADDR, CODE_ADDR + 2, count=1) except unicorn.UcError as e: - print "Error: %s" % e + assert(e.errno == unicorn.UC_ERR_CODE_INVALID) +else: + assert(False) mu = unicorn.Uc(unicorn.UC_ARCH_X86, unicorn.UC_MODE_32) mu.mem_map(CODE_ADDR, 1024 * 4) # If we write this address to EBX then the emulator hangs on emu_start mu.reg_write(unicorn.x86_const.UC_X86_REG_EBX, 0xaa96a47f) mu.mem_write(CODE_ADDR, CODE) -print "jmp ebx, with ebx == 0xaa96a47f" try: mu.emu_start(CODE_ADDR, CODE_ADDR + 2, count=1) except unicorn.UcError as e: - print "Error: %s" % e + assert(e.errno == unicorn.UC_ERR_CODE_INVALID) +else: + assert(False) + +print "Success" From c2b0e6827ad53172e7bffff0853ad9cdad73eb0b Mon Sep 17 00:00:00 2001 From: cherepanov74 Date: Sun, 30 Aug 2015 20:55:25 +0200 Subject: [PATCH 4/7] Fixed the list of required DLLs on Win32 --- COMPILE.TXT | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/COMPILE.TXT b/COMPILE.TXT index ec8ea564..bd0910be 100644 --- a/COMPILE.TXT +++ b/COMPILE.TXT @@ -199,19 +199,19 @@ Unicorn requires few dependent packages as followings To run sample_x86.exe on Windows 32-bit, you need the following files: - unicorn.dll - - C:\msys32\mingw32\bin\libiconv-2.dll - - C:\msys32\mingw32\bin\libintl-8.dll - - C:\msys32\mingw32\bin\libglib-2.0-0.dll - - C:\msys32\mingw32\bin\libgcc_s_seh-1.dll - - C:\msys32\mingw32\bin\libwinpthread-1.dll + - %MSYS2%\mingw32\bin\libiconv-2.dll + - %MSYS2%\mingw32\bin\libintl-8.dll + - %MSYS2%\mingw32\bin\libglib-2.0-0.dll + - %MSYS2%\mingw32\bin\libgcc_s_dw2-1.dll + - %MSYS2%\mingw32\bin\libwinpthread-1.dll To run sample_x86.exe on Windows 64-bit, you need the following files: - unicorn.dll - - C:\msys64\mingw64\bin\libiconv-2.dll - - C:\msys64\mingw64\bin\libintl-8.dll - - C:\msys64\mingw64\bin\libglib-2.0-0.dll - - C:\msys64\mingw64\bin\libgcc_s_seh-1.dll - - C:\msys64\mingw64\bin\libwinpthread-1.dll + - %MSYS2%\mingw64\bin\libiconv-2.dll + - %MSYS2%\mingw64\bin\libintl-8.dll + - %MSYS2%\mingw64\bin\libglib-2.0-0.dll + - %MSYS2%\mingw64\bin\libgcc_s_seh-1.dll + - %MSYS2%\mingw64\bin\libwinpthread-1.dll [8] By default, "cc" (default C compiler on the system) is used as compiler. From f83ddf5ad493838a1c69b88a0b0d372dd13fc0c2 Mon Sep 17 00:00:00 2001 From: Ryan Hileman Date: Sun, 30 Aug 2015 15:52:50 -0700 Subject: [PATCH 5/7] clean up Go binding hook types --- bindings/go/unicorn/hook.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/bindings/go/unicorn/hook.go b/bindings/go/unicorn/hook.go index b9e3c14b..ea6ce40e 100644 --- a/bindings/go/unicorn/hook.go +++ b/bindings/go/unicorn/hook.go @@ -17,33 +17,33 @@ type HookData struct { } //export hookCode -func hookCode(handle C.uch, addr C.uint64_t, size C.uint32_t, user unsafe.Pointer) { +func hookCode(handle C.uch, addr uint64, size uint32, user unsafe.Pointer) { hook := (*HookData)(user) hook.Callback.(func(*Uc, uint64, uint32))(hook.Uc, uint64(addr), uint32(size)) } //export hookMemInvalid -func hookMemInvalid(handle C.uch, typ C.uc_mem_type, addr C.uint64_t, size int, value C.int64_t, user unsafe.Pointer) C.bool { +func hookMemInvalid(handle C.uch, typ C.uc_mem_type, addr uint64, size int, value int64, user unsafe.Pointer) bool { hook := (*HookData)(user) - return C.bool(hook.Callback.(func(*Uc, int, uint64, int, int64) bool)(hook.Uc, int(typ), uint64(addr), size, int64(value))) + return hook.Callback.(func(*Uc, int, uint64, int, int64) bool)(hook.Uc, int(typ), addr, size, value) } //export hookMemAccess -func hookMemAccess(handle C.uch, typ C.uc_mem_type, addr C.uint64_t, size int, value C.int64_t, user unsafe.Pointer) { +func hookMemAccess(handle C.uch, typ C.uc_mem_type, addr uint64, size int, value int64, user unsafe.Pointer) { hook := (*HookData)(user) - hook.Callback.(func(*Uc, int, uint64, int, int64))(hook.Uc, int(typ), uint64(addr), size, int64(value)) + hook.Callback.(func(*Uc, int, uint64, int, int64))(hook.Uc, int(typ), addr, size, value) } //export hookX86In -func hookX86In(handle C.uch, port, size uint32, user unsafe.Pointer) C.uint32_t { +func hookX86In(handle C.uch, port, size uint32, user unsafe.Pointer) uint32 { hook := (*HookData)(user) - return C.uint32_t(hook.Callback.(func(*Uc, uint32, uint32) uint32)(hook.Uc, port, size)) + return hook.Callback.(func(*Uc, uint32, uint32) uint32)(hook.Uc, port, size) } //export hookX86Out func hookX86Out(handle C.uch, port, size, value uint32, user unsafe.Pointer) { hook := (*HookData)(user) - hook.Callback.(func(*Uc, uint32, uint32, uint32))(hook.Uc, uint32(port), uint32(size), uint32(value)) + hook.Callback.(func(*Uc, uint32, uint32, uint32))(hook.Uc, port, size, value) } //export hookX86Syscall From ac1fc4d631e65051fe6e9ded55c10fff60bd2dd9 Mon Sep 17 00:00:00 2001 From: Ryan Hileman Date: Sun, 30 Aug 2015 15:53:06 -0700 Subject: [PATCH 6/7] add missing interrupt hook for Go bindings --- bindings/go/unicorn/hook.c | 4 ++++ bindings/go/unicorn/hook.go | 8 ++++++++ bindings/go/unicorn/hook.h | 1 + 3 files changed, 13 insertions(+) diff --git a/bindings/go/unicorn/hook.c b/bindings/go/unicorn/hook.c index e715e1a2..a06f9901 100644 --- a/bindings/go/unicorn/hook.c +++ b/bindings/go/unicorn/hook.c @@ -17,6 +17,10 @@ void hookMemAccess_cgo(uch handle, uc_mem_type type, uint64_t addr, int size, in hookMemAccess(handle, type, addr, size, value, user); } +void hookInterrupt_cgo(uch handle, uint32_t intno, void *user) { + hookInterrupt(handle, intno, user); +} + uint32_t hookX86In_cgo(uch handle, uint32_t port, uint32_t size, void *user) { return hookX86In(handle, port, size, user); } diff --git a/bindings/go/unicorn/hook.go b/bindings/go/unicorn/hook.go index ea6ce40e..dcab893a 100644 --- a/bindings/go/unicorn/hook.go +++ b/bindings/go/unicorn/hook.go @@ -34,6 +34,12 @@ func hookMemAccess(handle C.uch, typ C.uc_mem_type, addr uint64, size int, value hook.Callback.(func(*Uc, int, uint64, int, int64))(hook.Uc, int(typ), addr, size, value) } +//export hookInterrupt +func hookInterrupt(handle C.uch, intno uint32, user unsafe.Pointer) { + hook := (*HookData)(user) + hook.Callback.(func(*Uc, uint32))(hook.Uc, intno) +} + //export hookX86In func hookX86In(handle C.uch, port, size uint32, user unsafe.Pointer) uint32 { hook := (*HookData)(user) @@ -64,6 +70,8 @@ func (u *Uc) HookAdd(htype int, cb interface{}, insn ...int) (C.uch, error) { callback = C.hookMemInvalid_cgo case UC_HOOK_MEM_READ, UC_HOOK_MEM_WRITE, UC_HOOK_MEM_READ_WRITE: callback = C.hookMemAccess_cgo + case UC_HOOK_INTR: + callback = C.hookInterrupt_cgo case UC_HOOK_INSN: extra = C.int(insn[0]) switch extra { diff --git a/bindings/go/unicorn/hook.h b/bindings/go/unicorn/hook.h index a89d8ec0..c8c22267 100644 --- a/bindings/go/unicorn/hook.h +++ b/bindings/go/unicorn/hook.h @@ -2,6 +2,7 @@ uc_err uc_hook_add2(uch handle, uch *h2, uc_hook_t type, void *callback, void *u void hookCode_cgo(uch handle, uint64_t addr, uint32_t size, void *user); bool hookMemInvalid_cgo(uch handle, uc_mem_type type, uint64_t addr, int size, int64_t value, void *user); void hookMemAccess_cgo(uch handle, uc_mem_type type, uint64_t addr, int size, int64_t value, void *user); +void hookInterrupt_cgo(uch handle, uint32_t intno, void *user); uint32_t hookX86In_cgo(uch handle, uint32_t port, uint32_t size, void *user); void hookX86Out_cgo(uch handle, uint32_t port, uint32_t size, uint32_t value, void *user); void hookX86Syscall_cgo(uch handle, void *user); From 342fcef4ffb07eb18d3d2ffdeacdc56524ee48f2 Mon Sep 17 00:00:00 2001 From: Nguyen Anh Quynh Date: Mon, 31 Aug 2015 09:48:19 +0800 Subject: [PATCH 7/7] update CREDITS --- CREDITS.TXT | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CREDITS.TXT b/CREDITS.TXT index a529cc49..1f76adf0 100644 --- a/CREDITS.TXT +++ b/CREDITS.TXT @@ -37,6 +37,8 @@ Luke Burnett Parker Thompson Daniel Godas-Lopez Antonio "s4tan" Parata +Corey Kallenberg +Shift Contributors (in no particular order) =====================================