import Unicorn2
This commit is contained in:
5
samples/.gitignore
vendored
5
samples/.gitignore
vendored
@ -1,5 +0,0 @@
|
||||
!*.c
|
||||
sample_*
|
||||
shellcode*
|
||||
mem_apis*
|
||||
|
@ -337,25 +337,9 @@ static void unmap_test()
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
// dynamically load shared library
|
||||
#ifdef DYNLOAD
|
||||
if (!uc_dyn_load(NULL, 0)) {
|
||||
printf("Error dynamically loading shared library.\n");
|
||||
printf("Please check that unicorn.dll/unicorn.so is available as well as\n");
|
||||
printf("any other dependent dll/so files.\n");
|
||||
printf("The easiest way is to place them in the same directory as this app.\n");
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
nx_test();
|
||||
perms_test();
|
||||
unmap_test();
|
||||
|
||||
// dynamically free shared library
|
||||
#ifdef DYNLOAD
|
||||
uc_dyn_free();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -25,12 +25,10 @@ fi
|
||||
if test -e $DIR/sample_arm; then
|
||||
echo "=========================="
|
||||
$DIR/sample_arm
|
||||
$DIR/sample_armeb
|
||||
fi
|
||||
if test -e $DIR/sample_arm64; then
|
||||
echo "=========================="
|
||||
$DIR/sample_arm64
|
||||
$DIR/sample_arm64eb
|
||||
fi
|
||||
if test -e $DIR/sample_mips; then
|
||||
echo "=========================="
|
||||
|
@ -8,11 +8,16 @@
|
||||
|
||||
|
||||
// code to be emulated
|
||||
#define ARM_CODE "\x37\x00\xa0\xe3\x03\x10\x42\xe0" // mov r0, #0x37; sub r1, r2, r3
|
||||
// #define ARM_CODE "\x37\x00\xa0\xe3" // mov r0, #0x37
|
||||
#define ARM_CODE "\x00\xf0\x20\xe3" // nop
|
||||
// #define ARM_CODE "\x37\x00\xa0\xe3\x03\x10\x42\xe0" // mov r0, #0x37; sub r1, r2, r3
|
||||
#define THUMB_CODE "\x83\xb0" // sub sp, #0xc
|
||||
|
||||
#define ARM_THUM_COND_CODE "\x9a\x42\x14\xbf\x68\x22\x4d\x22" // 'cmp r2, r3\nit ne\nmov r2, #0x68\nmov r2, #0x4d'
|
||||
|
||||
// code to be emulated
|
||||
#define ARM_CODE_EB "\xe3\xa0\x00\x37\xe0\x42\x10\x03" // mov r0, #0x37; sub r1, r2, r3
|
||||
#define THUMB_CODE_EB "\xb0\x83" // sub sp, #0xc
|
||||
// memory address where emulation starts
|
||||
#define ADDRESS 0x10000
|
||||
|
||||
@ -132,6 +137,164 @@ static void test_thumb(void)
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_armeb(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_err err;
|
||||
uc_hook trace1, trace2;
|
||||
|
||||
int r0 = 0x1234; // R0 register
|
||||
int r2 = 0x6789; // R1 register
|
||||
int r3 = 0x3333; // R2 register
|
||||
int r1; // R1 register
|
||||
|
||||
printf("Emulate ARM Big-Endian code\n");
|
||||
|
||||
// Initialize emulator in ARM mode
|
||||
err = uc_open(UC_ARCH_ARM, UC_MODE_ARM + UC_MODE_BIG_ENDIAN, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(uc, ADDRESS, ARM_CODE_EB, sizeof(ARM_CODE_EB) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(uc, UC_ARM_REG_R0, &r0);
|
||||
uc_reg_write(uc, UC_ARM_REG_R2, &r2);
|
||||
uc_reg_write(uc, UC_ARM_REG_R3, &r3);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
|
||||
|
||||
// tracing one instruction at ADDRESS with customized callback
|
||||
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, ADDRESS, ADDRESS);
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(ARM_CODE_EB) -1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(uc, UC_ARM_REG_R0, &r0);
|
||||
uc_reg_read(uc, UC_ARM_REG_R1, &r1);
|
||||
printf(">>> R0 = 0x%x\n", r0);
|
||||
printf(">>> R1 = 0x%x\n", r1);
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_thumbeb(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_err err;
|
||||
uc_hook trace1, trace2;
|
||||
|
||||
int sp = 0x1234; // R0 register
|
||||
|
||||
printf("Emulate THUMB Big-Endian code\n");
|
||||
|
||||
// Initialize emulator in ARM mode
|
||||
err = uc_open(UC_ARCH_ARM, UC_MODE_THUMB + UC_MODE_BIG_ENDIAN, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(uc, ADDRESS, THUMB_CODE_EB, sizeof(THUMB_CODE_EB) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(uc, UC_ARM_REG_SP, &sp);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
|
||||
|
||||
// tracing one instruction at ADDRESS with customized callback
|
||||
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, ADDRESS, ADDRESS);
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
// Note we start at ADDRESS | 1 to indicate THUMB mode.
|
||||
err = uc_emu_start(uc, ADDRESS | 1, ADDRESS + sizeof(THUMB_CODE_EB) -1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(uc, UC_ARM_REG_SP, &sp);
|
||||
printf(">>> SP = 0x%x\n", sp);
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_thumb_mrs(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_err err;
|
||||
uc_hook trace1, trace2;
|
||||
|
||||
int pc;
|
||||
|
||||
printf("Emulate THUMB MRS instruction\n");
|
||||
// 0xf3ef8014 - mrs r0, control
|
||||
|
||||
// Initialize emulator in ARM mode
|
||||
err = uc_open(UC_ARCH_ARM, UC_MODE_THUMB | UC_MODE_MCLASS, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(uc, ADDRESS, "\xef\xf3\x14\x80", 4);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
|
||||
|
||||
// tracing one instruction at ADDRESS with customized callback
|
||||
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, ADDRESS, ADDRESS);
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
|
||||
// Note we start at ADDRESS | 1 to indicate THUMB mode.
|
||||
err = uc_emu_start(uc, ADDRESS | 1, ADDRESS + 4, 0, 1);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(uc, UC_ARM_REG_PC, &pc);
|
||||
printf(">>> PC = 0x%x\n", pc);
|
||||
if (pc != ADDRESS + 4){
|
||||
printf("Error, PC was 0x%x, expected was 0x%x.\n", pc, ADDRESS + 4);
|
||||
}
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_thumb_ite_internal(bool step, uint32_t *r2_out, uint32_t *r3_out)
|
||||
{
|
||||
uc_engine *uc;
|
||||
@ -207,26 +370,22 @@ static void test_thumb_ite()
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
// dynamically load shared library
|
||||
#ifdef DYNLOAD
|
||||
if (!uc_dyn_load(NULL, 0)) {
|
||||
printf("Error dynamically loading shared library.\n");
|
||||
printf("Please check that unicorn.dll/unicorn.so is available as well as\n");
|
||||
printf("any other dependent dll/so files.\n");
|
||||
printf("The easiest way is to place them in the same directory as this app.\n");
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
test_arm();
|
||||
|
||||
printf("==========================\n");
|
||||
test_thumb();
|
||||
|
||||
printf("==========================\n");
|
||||
test_armeb();
|
||||
|
||||
printf("==========================\n");
|
||||
test_thumbeb();
|
||||
|
||||
printf("==========================\n");
|
||||
test_thumb_mrs();
|
||||
|
||||
printf("==========================\n");
|
||||
test_thumb_ite();
|
||||
// dynamically free shared library
|
||||
#ifdef DYNLOAD
|
||||
uc_dyn_free();
|
||||
#endif
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -8,7 +8,9 @@
|
||||
|
||||
|
||||
// code to be emulated
|
||||
#define ARM_CODE "\xab\x05\x00\xb8\xaf\x05\x40\x38" // str w11, [x13]; ldrb w15, [x13]
|
||||
#define ARM64_CODE "\xab\x05\x00\xb8\xaf\x05\x40\x38" // str w11, [x13], #0; ldrb w15, [x13], #0
|
||||
//#define ARM64_CODE_EB "\xb8\x00\x05\xab\x38\x40\x05\xaf" // str w11, [x13]; ldrb w15, [x13]
|
||||
#define ARM64_CODE_EB ARM64_CODE
|
||||
|
||||
// memory address where emulation starts
|
||||
#define ADDRESS 0x10000
|
||||
@ -23,6 +25,62 @@ static void hook_code(uc_engine *uc, uint64_t address, uint32_t size, void *user
|
||||
printf(">>> Tracing instruction at 0x%"PRIx64 ", instruction size = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
static void test_arm64_mem_fetch(void)
|
||||
{
|
||||
uc_engine* uc;
|
||||
uc_err err;
|
||||
uint64_t x1, sp, x0;
|
||||
// msr x0, CurrentEL
|
||||
unsigned char shellcode0[4] = {
|
||||
64, 66, 56, 213
|
||||
};
|
||||
// .text:00000000004002C0 LDR X1, [SP,#arg_0]
|
||||
unsigned char shellcode[4] = {
|
||||
0xE1, 0x03, 0x40, 0xF9
|
||||
};
|
||||
unsigned shellcode_address = 0x4002C0;
|
||||
uint64_t data_address = 0x10000000000000;
|
||||
|
||||
printf(">>> Emulate ARM64 fetching stack data from high address %"PRIx64"\n", data_address);
|
||||
|
||||
// Initialize emulator in ARM mode
|
||||
err = uc_open(UC_ARCH_ARM64, UC_MODE_ARM, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
uc_mem_map(uc, data_address, 0x30000, UC_PROT_ALL);
|
||||
uc_mem_map(uc, 0x400000, 0x1000, UC_PROT_ALL);
|
||||
|
||||
sp = data_address;
|
||||
uc_reg_write(uc, UC_ARM64_REG_SP, &sp);
|
||||
uc_mem_write(uc, data_address, "\xc8\xc8\xc8\xc8\xc8\xc8\xc8\xc8", 8);
|
||||
uc_mem_write(uc, shellcode_address, shellcode0, 4);
|
||||
uc_mem_write(uc, shellcode_address + 4, shellcode, 4);
|
||||
|
||||
err = uc_emu_start(uc, shellcode_address, shellcode_address+4, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
x0 = 0;
|
||||
uc_reg_read(uc, UC_ARM64_REG_X0, &x0);
|
||||
printf(">>> x0(Exception Level)=%"PRIx64"\n", x0>>2);
|
||||
|
||||
err = uc_emu_start(uc, shellcode_address+4, shellcode_address+8, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
uc_reg_read(uc, UC_ARM64_REG_X1, &x1);
|
||||
|
||||
printf(">>> X1 = 0x%" PRIx64 "\n", x1);
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_arm64(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
@ -47,7 +105,7 @@ static void test_arm64(void)
|
||||
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(uc, ADDRESS, ARM_CODE, sizeof(ARM_CODE) - 1);
|
||||
uc_mem_write(uc, ADDRESS, ARM64_CODE, sizeof(ARM64_CODE) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(uc, UC_ARM64_REG_X11, &x11);
|
||||
@ -62,7 +120,7 @@ static void test_arm64(void)
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(ARM_CODE) -1, 0, 0);
|
||||
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(ARM64_CODE) -1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
@ -77,25 +135,67 @@ static void test_arm64(void)
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
static void test_arm64eb(void)
|
||||
{
|
||||
// dynamically load shared library
|
||||
#ifdef DYNLOAD
|
||||
if (!uc_dyn_load(NULL, 0)) {
|
||||
printf("Error dynamically loading shared library.\n");
|
||||
printf("Please check that unicorn.dll/unicorn.so is available as well as\n");
|
||||
printf("any other dependent dll/so files.\n");
|
||||
printf("The easiest way is to place them in the same directory as this app.\n");
|
||||
return 1;
|
||||
uc_engine *uc;
|
||||
uc_err err;
|
||||
uc_hook trace1, trace2;
|
||||
|
||||
int64_t x11 = 0x12345678; // X11 register
|
||||
int64_t x13 = 0x10000 + 0x8; // X13 register
|
||||
int64_t x15 = 0x33; // X15 register
|
||||
|
||||
printf("Emulate ARM64 Big-Endian code\n");
|
||||
|
||||
// Initialize emulator in ARM mode
|
||||
err = uc_open(UC_ARCH_ARM64, UC_MODE_ARM + UC_MODE_BIG_ENDIAN, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(uc, ADDRESS, ARM64_CODE_EB, sizeof(ARM64_CODE_EB) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(uc, UC_ARM64_REG_X11, &x11);
|
||||
uc_reg_write(uc, UC_ARM64_REG_X13, &x13);
|
||||
uc_reg_write(uc, UC_ARM64_REG_X15, &x15);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
|
||||
|
||||
// tracing one instruction at ADDRESS with customized callback
|
||||
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, ADDRESS, ADDRESS);
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(ARM64_CODE_EB) -1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
printf(">>> As big endian, X15 should be 0x78:\n");
|
||||
|
||||
uc_reg_read(uc, UC_ARM64_REG_X15, &x15);
|
||||
printf(">>> X15 = 0x%" PRIx64 "\n", x15);
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
test_arm64_mem_fetch();
|
||||
test_arm64();
|
||||
|
||||
// dynamically free shared library
|
||||
#ifdef DYNLOAD
|
||||
uc_dyn_free();
|
||||
#endif
|
||||
|
||||
printf("-------------------------\n");
|
||||
test_arm64eb();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,156 +0,0 @@
|
||||
/* Unicorn Emulator Engine */
|
||||
/* By zhangwm, 2017 */
|
||||
|
||||
/* Sample code to demonstrate how to emulate ARM Big Endian code */
|
||||
|
||||
#include <unicorn/unicorn.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
// code to be emulated
|
||||
#define ARM_CODE "\xe3\xa0\x00\x37\xe0\x42\x10\x03" // mov r0, #0x37; sub r1, r2, r3
|
||||
#define THUMB_CODE "\xb0\x83" // sub sp, #0xc
|
||||
|
||||
// memory address where emulation starts
|
||||
#define ADDRESS 0x10000
|
||||
|
||||
static void hook_block(uc_engine *uc, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf(">>> Tracing basic block at 0x%"PRIx64 ", block size = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
static void hook_code(uc_engine *uc, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf(">>> Tracing instruction at 0x%"PRIx64 ", instruction size = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
static void test_arm(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_err err;
|
||||
uc_hook trace1, trace2;
|
||||
|
||||
int r0 = 0x1234; // R0 register
|
||||
int r2 = 0x6789; // R1 register
|
||||
int r3 = 0x3333; // R2 register
|
||||
int r1; // R1 register
|
||||
|
||||
printf("Emulate ARM Big-Endian code\n");
|
||||
|
||||
// Initialize emulator in ARM mode
|
||||
err = uc_open(UC_ARCH_ARM, UC_MODE_ARM + UC_MODE_BIG_ENDIAN, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(uc, ADDRESS, ARM_CODE, sizeof(ARM_CODE) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(uc, UC_ARM_REG_R0, &r0);
|
||||
uc_reg_write(uc, UC_ARM_REG_R2, &r2);
|
||||
uc_reg_write(uc, UC_ARM_REG_R3, &r3);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
|
||||
|
||||
// tracing one instruction at ADDRESS with customized callback
|
||||
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, ADDRESS, ADDRESS);
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(ARM_CODE) -1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(uc, UC_ARM_REG_R0, &r0);
|
||||
uc_reg_read(uc, UC_ARM_REG_R1, &r1);
|
||||
printf(">>> R0 = 0x%x\n", r0);
|
||||
printf(">>> R1 = 0x%x\n", r1);
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_thumb(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_err err;
|
||||
uc_hook trace1, trace2;
|
||||
|
||||
int sp = 0x1234; // R0 register
|
||||
|
||||
printf("Emulate THUMB code\n");
|
||||
|
||||
// Initialize emulator in ARM mode
|
||||
err = uc_open(UC_ARCH_ARM, UC_MODE_THUMB + UC_MODE_BIG_ENDIAN, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(uc, ADDRESS, THUMB_CODE, sizeof(THUMB_CODE) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(uc, UC_ARM_REG_SP, &sp);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
|
||||
|
||||
// tracing one instruction at ADDRESS with customized callback
|
||||
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, ADDRESS, ADDRESS);
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
// Note we start at ADDRESS | 1 to indicate THUMB mode.
|
||||
err = uc_emu_start(uc, ADDRESS | 1, ADDRESS + sizeof(THUMB_CODE) -1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(uc, UC_ARM_REG_SP, &sp);
|
||||
printf(">>> SP = 0x%x\n", sp);
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
// dynamically load shared library
|
||||
#ifdef DYNLOAD
|
||||
if (!uc_dyn_load(NULL, 0)) {
|
||||
printf("Error dynamically loading shared library.\n");
|
||||
printf("Please check that unicorn.dll/unicorn.so is available as well as\n");
|
||||
printf("any other dependent dll/so files.\n");
|
||||
printf("The easiest way is to place them in the same directory as this app.\n");
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
test_arm();
|
||||
printf("==========================\n");
|
||||
test_thumb();
|
||||
|
||||
// dynamically free shared library
|
||||
#ifdef DYNLOAD
|
||||
uc_dyn_free();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
@ -110,5 +110,6 @@ int main()
|
||||
return 1;
|
||||
}
|
||||
|
||||
uc_close(uc);
|
||||
return 0;
|
||||
}
|
||||
|
@ -141,23 +141,7 @@ static void test_m68k(void)
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
// dynamically load shared library
|
||||
#ifdef DYNLOAD
|
||||
if (!uc_dyn_load(NULL, 0)) {
|
||||
printf("Error dynamically loading shared library.\n");
|
||||
printf("Please check that unicorn.dll/unicorn.so is available as well as\n");
|
||||
printf("any other dependent dll/so files.\n");
|
||||
printf("The easiest way is to place them in the same directory as this app.\n");
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
test_m68k();
|
||||
|
||||
// dynamically free shared library
|
||||
#ifdef DYNLOAD
|
||||
uc_dyn_free();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -125,24 +125,8 @@ static void test_mips_el(void)
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
// dynamically load shared library
|
||||
#ifdef DYNLOAD
|
||||
if (!uc_dyn_load(NULL, 0)) {
|
||||
printf("Error dynamically loading shared library.\n");
|
||||
printf("Please check that unicorn.dll/unicorn.so is available as well as\n");
|
||||
printf("any other dependent dll/so files.\n");
|
||||
printf("The easiest way is to place them in the same directory as this app.\n");
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
test_mips_eb();
|
||||
test_mips_el();
|
||||
|
||||
// dynamically free shared library
|
||||
#ifdef DYNLOAD
|
||||
uc_dyn_free();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,18 +1,19 @@
|
||||
/* Unicorn Emulator Engine */
|
||||
/* By Nguyen Anh Quynh, 2015 */
|
||||
/* modify from arm64 sample zhangwm, 2017 */
|
||||
/* By simigo79, 2020 */
|
||||
|
||||
/* Sample code to demonstrate how to emulate ARM64EB code */
|
||||
/* Sample code to demonstrate how to emulate PPC code */
|
||||
|
||||
#include <unicorn/unicorn.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
// code to be emulated
|
||||
#define ARM_CODE "\xab\x05\x00\xb8\xaf\x05\x40\x38" // str x11, [x13]; ldrb x15, [x13]
|
||||
#define PPC_CODE "\x7F\x46\x1A\x14" // add r26, r6, r3
|
||||
|
||||
// memory address where emulation starts
|
||||
#define ADDRESS 0x10000
|
||||
|
||||
|
||||
static void hook_block(uc_engine *uc, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf(">>> Tracing basic block at 0x%"PRIx64 ", block size = 0x%x\n", address, size);
|
||||
@ -23,20 +24,20 @@ static void hook_code(uc_engine *uc, uint64_t address, uint32_t size, void *user
|
||||
printf(">>> Tracing instruction at 0x%"PRIx64 ", instruction size = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
static void test_arm64(void)
|
||||
static void test_ppc(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_err err;
|
||||
uc_hook trace1, trace2;
|
||||
|
||||
int64_t x11 = 0x12345678; // X11 register
|
||||
int64_t x13 = 0x10000 + 0x8; // X13 register
|
||||
int64_t x15 = 0x33; // X15 register
|
||||
int r3 = 0x1234; // R3 register
|
||||
int r6 = 0x6789; // R6 register
|
||||
int r26 = 0x8877; // R26 register (result)
|
||||
|
||||
printf("Emulate ARM64 Big-Endian code\n");
|
||||
printf("Emulate PPC code\n");
|
||||
|
||||
// Initialize emulator in ARM mode
|
||||
err = uc_open(UC_ARCH_ARM64, UC_MODE_ARM + UC_MODE_BIG_ENDIAN, &uc);
|
||||
// Initialize emulator in PPC mode
|
||||
err = uc_open(UC_ARCH_PPC, UC_MODE_PPC32 | UC_MODE_BIG_ENDIAN , &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
@ -47,12 +48,12 @@ static void test_arm64(void)
|
||||
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(uc, ADDRESS, ARM_CODE, sizeof(ARM_CODE) - 1);
|
||||
uc_mem_write(uc, ADDRESS, PPC_CODE, sizeof(PPC_CODE) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(uc, UC_ARM64_REG_X11, &x11);
|
||||
uc_reg_write(uc, UC_ARM64_REG_X13, &x13);
|
||||
uc_reg_write(uc, UC_ARM64_REG_X15, &x15);
|
||||
uc_reg_write(uc, UC_PPC_REG_3, &r3);
|
||||
uc_reg_write(uc, UC_PPC_REG_6, &r6);
|
||||
uc_reg_write(uc, UC_PPC_REG_26, &r26);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
|
||||
@ -62,40 +63,26 @@ static void test_arm64(void)
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(ARM_CODE) -1, 0, 0);
|
||||
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(PPC_CODE) -1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
printf(">>> As big endian, X15 should be 0x12:\n");
|
||||
|
||||
uc_reg_read(uc, UC_ARM64_REG_X15, &x15);
|
||||
printf(">>> X15 = 0x%" PRIx64 "\n", x15);
|
||||
uc_reg_read(uc, UC_PPC_REG_26, &r26);
|
||||
printf(">>> r26 = 0x%x\n", r26);
|
||||
|
||||
// close engine when done
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
// dynamically load shared library
|
||||
#ifdef DYNLOAD
|
||||
if (!uc_dyn_load(NULL, 0)) {
|
||||
printf("Error dynamically loading shared library.\n");
|
||||
printf("Please check that unicorn.dll/unicorn.so is available as well as\n");
|
||||
printf("any other dependent dll/so files.\n");
|
||||
printf("The easiest way is to place them in the same directory as this app.\n");
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
test_arm64();
|
||||
test_ppc();
|
||||
|
||||
// dynamically free shared library
|
||||
#ifdef DYNLOAD
|
||||
uc_dyn_free();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
594
samples/sample_riscv.c
Normal file
594
samples/sample_riscv.c
Normal file
@ -0,0 +1,594 @@
|
||||
/* Unicorn Emulator Engine */
|
||||
|
||||
/* Sample code to demonstrate how to emulate RISCV code */
|
||||
|
||||
#include <unicorn/unicorn.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
// code to be emulated
|
||||
#if 0
|
||||
$ cstool riscv64 1305100093850502
|
||||
0 13 05 10 00 addi a0, zero, 1
|
||||
4 93 85 05 02 addi a1, a1, 0x20
|
||||
#endif
|
||||
//#define RISCV_CODE "\x13\x05\x10\x00\x93\x85\x05\x02\x93\x85\x05\x02"
|
||||
#define RISCV_CODE "\x13\x05\x10\x00\x93\x85\x05\x02"
|
||||
|
||||
|
||||
// memory address where emulation starts
|
||||
#define ADDRESS 0x10000
|
||||
|
||||
static void hook_block(uc_engine *uc, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf(">>> Tracing basic block at 0x%"PRIx64 ", block size = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
static void hook_code(uc_engine *uc, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf(">>> Tracing instruction at 0x%"PRIx64 ", instruction size = 0x%x\n", address, size);
|
||||
}
|
||||
|
||||
static void hook_code3(uc_engine *uc, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf(">>> Tracing instruction at 0x%"PRIx64 ", instruction size = 0x%x\n", address, size);
|
||||
if (address == ADDRESS) {
|
||||
printf("stop emulation\n");
|
||||
uc_emu_stop(uc);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_riscv(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_hook trace1, trace2;
|
||||
uc_err err;
|
||||
|
||||
uint32_t a0 = 0x1234;
|
||||
uint32_t a1 = 0x7890;
|
||||
|
||||
printf("Emulate RISCV code\n");
|
||||
|
||||
// Initialize emulator in RISCV64 mode
|
||||
err = uc_open(UC_ARCH_RISCV, UC_MODE_RISCV32, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(uc, ADDRESS, RISCV_CODE, sizeof(RISCV_CODE) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(uc, UC_RISCV_REG_A0, &a0);
|
||||
uc_reg_write(uc, UC_RISCV_REG_A1, &a1);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
|
||||
|
||||
// tracing all instruction
|
||||
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(RISCV_CODE) - 1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(uc, UC_RISCV_REG_A0, &a0);
|
||||
uc_reg_read(uc, UC_RISCV_REG_A1, &a1);
|
||||
|
||||
printf(">>> A0 = 0x%x\n", a0);
|
||||
printf(">>> A1 = 0x%x\n", a1);
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_riscv2(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_hook trace1, trace2;
|
||||
uc_err err;
|
||||
|
||||
uint32_t a0 = 0x1234;
|
||||
uint32_t a1 = 0x7890;
|
||||
|
||||
printf("Emulate RISCV code: split emulation\n");
|
||||
|
||||
// Initialize emulator in RISCV64 mode
|
||||
err = uc_open(UC_ARCH_RISCV, UC_MODE_RISCV32, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(uc, ADDRESS, RISCV_CODE, sizeof(RISCV_CODE) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(uc, UC_RISCV_REG_A0, &a0);
|
||||
uc_reg_write(uc, UC_RISCV_REG_A1, &a1);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
|
||||
|
||||
// tracing all instruction
|
||||
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
|
||||
|
||||
// emulate 1 instruction
|
||||
err = uc_emu_start(uc, ADDRESS, ADDRESS + 4, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
uc_reg_read(uc, UC_RISCV_REG_A0, &a0);
|
||||
uc_reg_read(uc, UC_RISCV_REG_A1, &a1);
|
||||
|
||||
printf(">>> A0 = 0x%x\n", a0);
|
||||
printf(">>> A1 = 0x%x\n", a1);
|
||||
|
||||
// emulate one more instruction
|
||||
err = uc_emu_start(uc, ADDRESS + 4, ADDRESS + 8, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(uc, UC_RISCV_REG_A0, &a0);
|
||||
uc_reg_read(uc, UC_RISCV_REG_A1, &a1);
|
||||
|
||||
printf(">>> A0 = 0x%x\n", a0);
|
||||
printf(">>> A1 = 0x%x\n", a1);
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_riscv3(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_hook trace1, trace2;
|
||||
uc_err err;
|
||||
|
||||
uint32_t a0 = 0x1234;
|
||||
uint32_t a1 = 0x7890;
|
||||
|
||||
printf("Emulate RISCV code: early stop\n");
|
||||
|
||||
// Initialize emulator in RISCV64 mode
|
||||
err = uc_open(UC_ARCH_RISCV, UC_MODE_RISCV32, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(uc, ADDRESS, RISCV_CODE, sizeof(RISCV_CODE) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(uc, UC_RISCV_REG_A0, &a0);
|
||||
uc_reg_write(uc, UC_RISCV_REG_A1, &a1);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
|
||||
|
||||
// tracing all instruction
|
||||
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code3, NULL, 1, 0);
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(RISCV_CODE) - 1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(uc, UC_RISCV_REG_A0, &a0);
|
||||
uc_reg_read(uc, UC_RISCV_REG_A1, &a1);
|
||||
|
||||
printf(">>> A0 = 0x%x\n", a0);
|
||||
printf(">>> A1 = 0x%x\n", a1);
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_riscv_step(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_hook trace1, trace2;
|
||||
uc_err err;
|
||||
|
||||
uint32_t a0 = 0x1234;
|
||||
uint32_t a1 = 0x7890;
|
||||
uint32_t pc = 0x0000;
|
||||
|
||||
printf("Emulate RISCV code: step\n");
|
||||
|
||||
// Initialize emulator in RISCV64 mode
|
||||
err = uc_open(UC_ARCH_RISCV, UC_MODE_RISCV32, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(uc, ADDRESS, RISCV_CODE, sizeof(RISCV_CODE) - 1);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(uc, UC_RISCV_REG_A0, &a0);
|
||||
uc_reg_write(uc, UC_RISCV_REG_A1, &a1);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
|
||||
|
||||
// tracing all instruction
|
||||
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
|
||||
|
||||
// emulate 1 instruction
|
||||
err = uc_emu_start(uc, ADDRESS, ADDRESS + 12, 0, 1);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
uc_reg_read(uc, UC_RISCV_REG_A0, &a0);
|
||||
uc_reg_read(uc, UC_RISCV_REG_A1, &a1);
|
||||
uc_reg_read(uc, UC_RISCV_REG_PC, &pc);
|
||||
|
||||
printf(">>> A0 = 0x%x\n", a0);
|
||||
printf(">>> A1 = 0x%x\n", a1);
|
||||
|
||||
if (pc != 0x10004) {
|
||||
printf("Error after step: PC is: 0x%x, expected was 0x10004\n", pc);
|
||||
}
|
||||
|
||||
// emulate one more instruction
|
||||
err = uc_emu_start(uc, ADDRESS + 4, ADDRESS + 8, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(uc, UC_RISCV_REG_A0, &a0);
|
||||
uc_reg_read(uc, UC_RISCV_REG_A1, &a1);
|
||||
|
||||
printf(">>> A0 = 0x%x\n", a0);
|
||||
printf(">>> A1 = 0x%x\n", a1);
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_riscv_timeout(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_hook trace1, trace2;
|
||||
uc_err err;
|
||||
|
||||
uint32_t a0 = 0x1234;
|
||||
uint32_t a1 = 0x7890;
|
||||
uint32_t pc = 0x0000;
|
||||
|
||||
printf("Emulate RISCV code: timeout\n");
|
||||
|
||||
// Initialize emulator in RISCV64 mode
|
||||
err = uc_open(UC_ARCH_RISCV, UC_MODE_RISCV32, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(uc, ADDRESS, "\x00\x00\x00\x00\x00\x00\x00\x00", 8);
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(uc, UC_RISCV_REG_A0, &a0);
|
||||
uc_reg_write(uc, UC_RISCV_REG_A1, &a1);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
|
||||
|
||||
// tracing all instruction
|
||||
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
|
||||
|
||||
// emulate 1 instruction with timeout
|
||||
err = uc_emu_start(uc, ADDRESS, ADDRESS + 4, 1000, 1);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
uc_reg_read(uc, UC_RISCV_REG_PC, &pc);
|
||||
|
||||
if (pc != 0x10000) {
|
||||
printf("Error after step: PC is: 0x%x, expected was 0x10004\n", pc);
|
||||
}
|
||||
|
||||
// emulate 1 instruction with timeout
|
||||
err = uc_emu_start(uc, ADDRESS, ADDRESS + 4, 1000, 1);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
uc_reg_read(uc, UC_RISCV_REG_PC, &pc);
|
||||
|
||||
if (pc != 0x10000) {
|
||||
printf("Error after step: PC is: 0x%x, expected was 0x10004\n", pc);
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done\n");
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_riscv_sd64(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_hook trace1, trace2;
|
||||
uc_err err;
|
||||
|
||||
uint64_t reg;
|
||||
|
||||
/*
|
||||
00813823 sd s0,16(sp)
|
||||
00000013 nop
|
||||
*/
|
||||
#define CODE64 "\x23\x38\x81\x00\x13\x00\x00\x00"
|
||||
|
||||
printf("Emulate RISCV code: sd64 instruction\n");
|
||||
|
||||
// Initialize emulator in RISCV64 mode
|
||||
err = uc_open(UC_ARCH_RISCV, UC_MODE_RISCV64, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(uc, ADDRESS, CODE64, sizeof(CODE64) - 1);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
|
||||
|
||||
// tracing all instruction
|
||||
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
|
||||
|
||||
reg = ADDRESS + 0x100;
|
||||
uc_reg_write(uc, UC_RISCV_REG_SP, ®);
|
||||
|
||||
reg = 0x11223344;
|
||||
uc_reg_write(uc, UC_RISCV_REG_S0, ®);
|
||||
|
||||
// execute instruction
|
||||
err = uc_emu_start(uc, 0x10000, -1, 0, 1);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done.\n");
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static bool hook_memalloc(uc_engine *uc, uc_mem_type type, uint64_t address,
|
||||
int size, int64_t value, void *user_data)
|
||||
{
|
||||
uint64_t algined_address = address & 0xFFFFFFFFFFFFF000ULL;
|
||||
int aligned_size = ((int)(size / 0x1000) + 1) * 0x1000;
|
||||
|
||||
printf(">>> Allocating block at 0x%" PRIx64 " (0x%" PRIx64 "), block size = 0x%x (0x%x)\n",
|
||||
address, algined_address, size, aligned_size);
|
||||
|
||||
uc_mem_map(uc, algined_address, aligned_size, UC_PROT_ALL);
|
||||
|
||||
// this recovers from missing memory, so we return true
|
||||
return true;
|
||||
}
|
||||
|
||||
static void test_recover_from_illegal(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_hook trace1, trace2, mem_alloc;
|
||||
uc_err err;
|
||||
uint64_t a0 = 0x1234;
|
||||
uint64_t a1 = 0x7890;
|
||||
|
||||
printf("Emulate RISCV code: recover_from_illegal\n");
|
||||
|
||||
// Initialize emulator in RISCV64 mode
|
||||
err = uc_open(UC_ARCH_RISCV, UC_MODE_RISCV64, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
uc_reg_write(uc, UC_RISCV_REG_A0, &a0);
|
||||
uc_reg_write(uc, UC_RISCV_REG_A1, &a1);
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
// auto-allocate memory on access
|
||||
uc_hook_add(uc, &mem_alloc, UC_HOOK_MEM_UNMAPPED, hook_memalloc, NULL, 1, 0);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
|
||||
|
||||
// tracing all instruction
|
||||
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(uc, ADDRESS, RISCV_CODE, sizeof(RISCV_CODE) - 1);
|
||||
|
||||
// emulate 1 instruction, wrong address, illegal code
|
||||
err = uc_emu_start(uc, 0x1000, -1, 0, 1);
|
||||
if (err != UC_ERR_INSN_INVALID) {
|
||||
printf("Expected Illegal Instruction error, got: %u\n", err);
|
||||
}
|
||||
|
||||
// emulate 1 instruction, correct address, valid code
|
||||
err = uc_emu_start(uc, ADDRESS, -1, 0, 1);
|
||||
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(uc, UC_RISCV_REG_A0, &a0);
|
||||
uc_reg_read(uc, UC_RISCV_REG_A1, &a1);
|
||||
|
||||
printf(">>> A0 = 0x%"PRIx64 "\n", a0);
|
||||
printf(">>> A1 = 0x%"PRIx64 "\n", a1);
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_riscv_func_return(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_hook trace1, trace2;
|
||||
uc_err err;
|
||||
|
||||
uint64_t pc = 0, ra = 0;
|
||||
|
||||
// 10000: 00008067 ret
|
||||
// 10004: 8082 c.ret
|
||||
// 10006: 0001 nop
|
||||
// 10008: 0001 nop
|
||||
|
||||
#define CODE "\x67\x80\x00\x00\x82\x80\x01\x00\x01\x00"
|
||||
|
||||
printf("Emulate RISCV code: return from func\n");
|
||||
|
||||
// Initialize emulator in RISCV64 mode
|
||||
err = uc_open(UC_ARCH_RISCV, UC_MODE_RISCV64, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u (%s)\n",
|
||||
err, uc_strerror(err));
|
||||
return;
|
||||
}
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
uc_mem_write(uc, ADDRESS, CODE, sizeof(CODE) - 1);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
|
||||
|
||||
// tracing all instruction
|
||||
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
|
||||
|
||||
#if 1
|
||||
// set return address register
|
||||
// RET instruction will return to address in RA
|
||||
// so after RET, PC == RA
|
||||
ra = 0x10006;
|
||||
uc_reg_write(uc, UC_RISCV_REG_RA, &ra);
|
||||
|
||||
// execute ret instruction
|
||||
err = uc_emu_start(uc, 0x10000, -1, 0, 1);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
uc_reg_read(uc, UC_RISCV_REG_PC, &pc);
|
||||
if (pc != ra) {
|
||||
printf("Error after execution: PC is: 0x%"PRIx64 ", expected was 0x%"PRIx64 "\n", pc, ra);
|
||||
if (pc == 0x10000) {
|
||||
printf(" PC did not change during execution\n");
|
||||
}
|
||||
} else {
|
||||
printf("Good, PC == RA\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
// set return address register
|
||||
// C.RET instruction will return to address in RA
|
||||
// so after C.RET, PC == RA
|
||||
ra = 0x10006;
|
||||
uc_reg_write(uc, UC_RISCV_REG_RA, &ra);
|
||||
|
||||
printf("========\n");
|
||||
// execute c.ret instruction
|
||||
err = uc_emu_start(uc, 0x10004, -1, 0, 1);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
}
|
||||
|
||||
uc_reg_read(uc, UC_RISCV_REG_PC, &pc);
|
||||
if (pc != ra) {
|
||||
printf("Error after execution: PC is: 0x%"PRIx64 ", expected was 0x%"PRIx64 "\n", pc, ra);
|
||||
if (pc == 0x10004) {
|
||||
printf(" PC did not change during execution\n");
|
||||
}
|
||||
} else {
|
||||
printf("Good, PC == RA\n");
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done.\n");
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
test_recover_from_illegal();
|
||||
|
||||
printf("------------------\n");
|
||||
test_riscv();
|
||||
|
||||
printf("------------------\n");
|
||||
test_riscv2();
|
||||
|
||||
printf("------------------\n");
|
||||
test_riscv3();
|
||||
|
||||
printf("------------------\n");
|
||||
test_riscv_step();
|
||||
|
||||
printf("------------------\n");
|
||||
test_riscv_timeout();
|
||||
|
||||
printf("------------------\n");
|
||||
test_riscv_sd64();
|
||||
|
||||
printf("------------------\n");
|
||||
test_riscv_func_return();
|
||||
|
||||
return 0;
|
||||
}
|
@ -80,23 +80,7 @@ static void test_sparc(void)
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
// dynamically load shared library
|
||||
#ifdef DYNLOAD
|
||||
if (!uc_dyn_load(NULL, 0)) {
|
||||
printf("Error dynamically loading shared library.\n");
|
||||
printf("Please check that unicorn.dll/unicorn.so is available as well as\n");
|
||||
printf("any other dependent dll/so files.\n");
|
||||
printf("The easiest way is to place them in the same directory as this app.\n");
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
test_sparc();
|
||||
|
||||
// dynamically free shared library
|
||||
#ifdef DYNLOAD
|
||||
uc_dyn_free();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -26,6 +26,12 @@
|
||||
#define X86_CODE64 "\x41\xBC\x3B\xB0\x28\x2A\x49\x0F\xC9\x90\x4D\x0F\xAD\xCF\x49\x87\xFD\x90\x48\x81\xD2\x8A\xCE\x77\x35\x48\xF7\xD9\x4D\x29\xF4\x49\x81\xC9\xF6\x8A\xC6\x53\x4D\x87\xED\x48\x0F\xAD\xD2\x49\xF7\xD4\x48\xF7\xE1\x4D\x19\xC5\x4D\x89\xC5\x48\xF7\xD6\x41\xB8\x4F\x8D\x6B\x59\x4D\x87\xD0\x68\x6A\x1E\x09\x3C\x59"
|
||||
#define X86_CODE16 "\x00\x00" // add byte ptr [bx + si], al
|
||||
#define X86_CODE64_SYSCALL "\x0f\x05" // SYSCALL
|
||||
#define X86_MMIO_CODE "\x89\x0d\x04\x00\x02\x00\x8b\x0d\x04\x00\x02\x00" // mov [0x20004], ecx; mov ecx, [0x20004]
|
||||
/*
|
||||
* 0x1000 xor dword ptr [edi+0x3], eax ; edi=0x1000, eax=0xbc4177e6
|
||||
* 0x1003 dw 0x3ea98b13
|
||||
*/
|
||||
#define X86_CODE32_SMC "\x31\x47\x03\x13\x8b\xa9\x3e"
|
||||
|
||||
// memory address where emulation starts
|
||||
#define ADDRESS 0x1000000
|
||||
@ -86,7 +92,7 @@ static bool hook_mem_invalid(uc_engine *uc, uc_mem_type type,
|
||||
static bool hook_mem_invalid_dummy(uc_engine *uc, uc_mem_type type,
|
||||
uint64_t address, int size, int64_t value, void *user_data)
|
||||
{
|
||||
// Stop emulation.
|
||||
// stop emulation
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -173,6 +179,73 @@ static void hook_syscall(uc_engine *uc, void *user_data)
|
||||
printf("ERROR: was not expecting rax=0x%"PRIx64 " in syscall\n", rax);
|
||||
}
|
||||
|
||||
static bool hook_memalloc(uc_engine *uc, uc_mem_type type, uint64_t address,
|
||||
int size, int64_t value, void *user_data)
|
||||
{
|
||||
uint64_t algined_address = address & 0xFFFFFFFFFFFFF000ULL;
|
||||
int aligned_size = ((int)(size / 0x1000) + 1) * 0x1000;
|
||||
|
||||
printf(">>> Allocating block at 0x%" PRIx64 " (0x%" PRIx64 "), block size = 0x%x (0x%x)\n",
|
||||
address, algined_address, size, aligned_size);
|
||||
|
||||
uc_mem_map(uc, algined_address, aligned_size, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
if (uc_mem_write(uc, algined_address, X86_CODE32, sizeof(X86_CODE32) - 1)) {
|
||||
printf("Failed to write emulation code to memory, quit!\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// this recovers from missing memory, so we return true
|
||||
return true;
|
||||
}
|
||||
|
||||
static void test_miss_code(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_err err;
|
||||
uc_hook trace1, trace2;
|
||||
|
||||
int r_ecx = 0x1234; // ECX register
|
||||
int r_edx = 0x7890; // EDX register
|
||||
|
||||
printf("Emulate i386 code - missing code\n");
|
||||
|
||||
// Initialize emulator in X86-32bit mode
|
||||
err = uc_open(UC_ARCH_X86, UC_MODE_32, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(uc, UC_X86_REG_ECX, &r_ecx);
|
||||
uc_reg_write(uc, UC_X86_REG_EDX, &r_edx);
|
||||
|
||||
// tracing all instruction by having @begin > @end
|
||||
uc_hook_add(uc, &trace1, UC_HOOK_CODE, hook_code, NULL, 1, 0);
|
||||
|
||||
// auto-allocate memory on access
|
||||
uc_hook_add(uc, &trace2, UC_HOOK_MEM_UNMAPPED, hook_memalloc, NULL, 1, 0);
|
||||
|
||||
// emulate machine code, without having the code in yet
|
||||
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(X86_CODE32) - 1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned %u: %s\n",
|
||||
err, uc_strerror(err));
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(uc, UC_X86_REG_ECX, &r_ecx);
|
||||
uc_reg_read(uc, UC_X86_REG_EDX, &r_edx);
|
||||
printf(">>> ECX = 0x%x\n", r_ecx);
|
||||
printf(">>> EDX = 0x%x\n", r_edx);
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_i386(void)
|
||||
{
|
||||
uc_engine *uc;
|
||||
@ -745,6 +818,23 @@ static void test_i386_context_save(void)
|
||||
uc_reg_read(uc, UC_X86_REG_EAX, &r_eax);
|
||||
printf(">>> EAX = 0x%x\n", r_eax);
|
||||
|
||||
// modify some registers of the context
|
||||
r_eax = 0xc8;
|
||||
uc_context_reg_write(context, UC_X86_REG_EAX, &r_eax);
|
||||
|
||||
// and restore CPU context again
|
||||
err = uc_context_restore(uc, context);
|
||||
if (err) {
|
||||
printf("Failed on uc_context_restore() with error returned: %u\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
// now print out some registers
|
||||
printf(">>> CPU context restored with modification. Below is the CPU context\n");
|
||||
|
||||
uc_reg_read(uc, UC_X86_REG_EAX, &r_eax);
|
||||
printf(">>> EAX = 0x%x\n", r_eax);
|
||||
|
||||
// free the CPU context
|
||||
err = uc_context_free(context);
|
||||
if (err) {
|
||||
@ -1092,6 +1182,133 @@ static void test_i386_invalid_mem_read_in_tb(void)
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_i386_smc_xor()
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_err err;
|
||||
|
||||
uint32_t r_edi = ADDRESS; // ECX register
|
||||
uint32_t r_eax = 0xbc4177e6; // EDX register
|
||||
uint32_t result;
|
||||
|
||||
printf("===================================\n");
|
||||
printf("Emulate i386 code that modfies itself\n");
|
||||
|
||||
// Initialize emulator in X86-32bit mode
|
||||
err = uc_open(UC_ARCH_X86, UC_MODE_32, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
// map 1KB memory for this emulation
|
||||
uc_mem_map(uc, ADDRESS, 0x1000, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
if (uc_mem_write(uc, ADDRESS, X86_CODE32_SMC, sizeof(X86_CODE32_SMC) - 1)) {
|
||||
printf("Failed to write emulation code to memory, quit!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// initialize machine registers
|
||||
uc_reg_write(uc, UC_X86_REG_EDI, &r_edi);
|
||||
uc_reg_write(uc, UC_X86_REG_EAX, &r_eax);
|
||||
|
||||
// **Important Note**
|
||||
//
|
||||
// Since SMC code will cause TB regeneration, the XOR in fact would executed
|
||||
// twice (the first execution won't take effect.). Thus, if you would like to
|
||||
// use count to control the emulation, the count should be set to 2.
|
||||
//
|
||||
// err = uc_emu_start(uc, ADDRESS, ADDRESS + 3, 0, 0);
|
||||
err = uc_emu_start(uc, ADDRESS, 0, 0, 2);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned %u: %s\n",
|
||||
err, uc_strerror(err));
|
||||
}
|
||||
|
||||
printf(">>> Emulation done. Below is the result.\n");
|
||||
|
||||
uc_mem_read(uc, ADDRESS + 3, (void *)&result, 4);
|
||||
|
||||
if (result == (0x3ea98b13 ^ 0xbc4177e6)) {
|
||||
printf(">>> SMC emulation is correct. 0x3ea98b13 ^ 0xbc4177e6 = 0x%x\n", result);
|
||||
} else {
|
||||
printf(">>> SMC emulation is wrong. 0x3ea98b13 ^ 0xbc4177e6 = 0x%x\n", result);
|
||||
}
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static uint64_t mmio_read_callback(uc_engine* uc, uint64_t offset, unsigned size, void* user_data)
|
||||
{
|
||||
printf(">>> Read IO memory at offset 0x%"PRIu64" with 0x%"PRIu32" bytes and return 0x19260817\n", offset, size);
|
||||
// The value returned here would be written to ecx.
|
||||
return 0x19260817;
|
||||
}
|
||||
|
||||
static void mmio_write_callback(uc_engine* uc, uint64_t offset, unsigned size, uint64_t value, void* user_data)
|
||||
{
|
||||
printf(">>> Write value 0x%"PRIu64" to IO memory at offset 0x%"PRIu64" with 0x%"PRIu32" bytes\n", value, offset, size);
|
||||
return;
|
||||
}
|
||||
|
||||
static void test_i386_mmio()
|
||||
{
|
||||
uc_engine* uc;
|
||||
int r_ecx = 0xdeadbeef;
|
||||
uc_err err;
|
||||
|
||||
printf("===================================\n");
|
||||
printf("Emulate i386 code that uses MMIO\n");
|
||||
|
||||
// Initialize emulator in X86-32bit mode
|
||||
err = uc_open(UC_ARCH_X86, UC_MODE_32, &uc);
|
||||
if (err) {
|
||||
printf("Failed on uc_open() with error returned: %u\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
// map 1KB memory for this emulation
|
||||
err = uc_mem_map(uc, ADDRESS, 0x1000, UC_PROT_ALL);
|
||||
if (err) {
|
||||
printf("Failed on uc_mem_map() with error returned: %u\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
err = uc_mem_write(uc, ADDRESS, X86_MMIO_CODE, sizeof(X86_MMIO_CODE) - 1);
|
||||
if (err) {
|
||||
printf("Failed on uc_mem_write() with error returned: %u\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
err = uc_mmio_map(uc, 0x20000, 0x4000, mmio_read_callback, NULL, mmio_write_callback, NULL);
|
||||
if (err) {
|
||||
printf("Failed on uc_mmio_map() with error returned: %u\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
// prepare ecx
|
||||
err = uc_reg_write(uc, UC_X86_REG_ECX, &r_ecx);
|
||||
if (err) {
|
||||
printf("Failed on uc_reg_write() with error returned: %u\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(X86_MMIO_CODE) - 1, 0, 0);
|
||||
if (err) {
|
||||
printf("Failed on uc_emu_start() with error returned: %u\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
uc_reg_read(uc, UC_X86_REG_ECX, &r_ecx);
|
||||
|
||||
printf(">>> Emulation done. ECX=0x%x\n", r_ecx);
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
if (argc == 2) {
|
||||
@ -1099,6 +1316,7 @@ int main(int argc, char **argv, char **envp)
|
||||
test_x86_16();
|
||||
}
|
||||
else if (!strcmp(argv[1], "-32")) {
|
||||
test_miss_code();
|
||||
test_i386();
|
||||
test_i386_map_ptr();
|
||||
test_i386_inout();
|
||||
@ -1120,6 +1338,7 @@ int main(int argc, char **argv, char **envp)
|
||||
}
|
||||
else {
|
||||
test_x86_16();
|
||||
test_miss_code();
|
||||
test_i386();
|
||||
test_i386_map_ptr();
|
||||
test_i386_inout();
|
||||
@ -1133,6 +1352,8 @@ int main(int argc, char **argv, char **envp)
|
||||
test_x86_64();
|
||||
test_x86_64_syscall();
|
||||
test_i386_invalid_mem_read_in_tb();
|
||||
test_i386_smc_xor();
|
||||
test_i386_mmio();
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -134,17 +134,6 @@ static void test_i386(void)
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
// dynamically load shared library
|
||||
#ifdef DYNLOAD
|
||||
if (!uc_dyn_load(NULL, 0)) {
|
||||
printf("Error dynamically loading shared library.\n");
|
||||
printf("Please check that unicorn.dll/unicorn.so is available as well as\n");
|
||||
printf("any other dependent dll/so files.\n");
|
||||
printf("The easiest way is to place them in the same directory as this app.\n");
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (argc == 2) {
|
||||
if (!strcmp(argv[1], "-32")) {
|
||||
test_i386();
|
||||
@ -156,10 +145,5 @@ int main(int argc, char **argv, char **envp)
|
||||
test_i386();
|
||||
}
|
||||
|
||||
// dynamically free shared library
|
||||
#ifdef DYNLOAD
|
||||
uc_dyn_free();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user