Automated leading tab to spaces conversion.
This commit is contained in:
@ -151,7 +151,7 @@ static void test_thumb(void)
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
// dynamically load shared library
|
||||
// dynamically load shared library
|
||||
#ifdef DYNLOAD
|
||||
if (!uc_dyn_load(NULL, 0)) {
|
||||
printf("Error dynamically loading shared library.\n");
|
||||
|
@ -98,7 +98,7 @@ static void test_arm64(void)
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
// dynamically load shared library
|
||||
// dynamically load shared library
|
||||
#ifdef DYNLOAD
|
||||
if (!uc_dyn_load(NULL, 0)) {
|
||||
printf("Error dynamically loading shared library.\n");
|
||||
|
@ -160,7 +160,7 @@ static void test_m68k(void)
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
// dynamically load shared library
|
||||
// dynamically load shared library
|
||||
#ifdef DYNLOAD
|
||||
if (!uc_dyn_load(NULL, 0)) {
|
||||
printf("Error dynamically loading shared library.\n");
|
||||
|
@ -145,7 +145,7 @@ static void test_mips_el(void)
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
// dynamically load shared library
|
||||
// dynamically load shared library
|
||||
#ifdef DYNLOAD
|
||||
if (!uc_dyn_load(NULL, 0)) {
|
||||
printf("Error dynamically loading shared library.\n");
|
||||
|
@ -100,7 +100,7 @@ static void test_sparc(void)
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
// dynamically load shared library
|
||||
// dynamically load shared library
|
||||
#ifdef DYNLOAD
|
||||
if (!uc_dyn_load(NULL, 0)) {
|
||||
printf("Error dynamically loading shared library.\n");
|
||||
|
@ -281,7 +281,7 @@ static void test_x86_64(void)
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
// dynamically load shared library
|
||||
// dynamically load shared library
|
||||
#ifdef DYNLOAD
|
||||
if (!uc_dyn_load(NULL, 0)) {
|
||||
printf("Error dynamically loading shared library.\n");
|
||||
|
@ -40,11 +40,11 @@ but that the code hook is just not occurring.
|
||||
// It should loop 3 times before ending.
|
||||
const uint64_t addr = 0x100000;
|
||||
const unsigned char loop_test_code[] = {
|
||||
0x02,0x00,0x04,0x24, // 100000: li $a0, 2
|
||||
// loop1
|
||||
0x00,0x00,0x00,0x00, // 100004: nop
|
||||
0xFE,0xFF,0x80,0x14, // 100008: bnez $a0, loop1
|
||||
0xFF,0xFF,0x84,0x24, // 10000C: addiu $a0, -1
|
||||
0x02,0x00,0x04,0x24, // 100000: li $a0, 2
|
||||
// loop1
|
||||
0x00,0x00,0x00,0x00, // 100004: nop
|
||||
0xFE,0xFF,0x80,0x14, // 100008: bnez $a0, loop1
|
||||
0xFF,0xFF,0x84,0x24, // 10000C: addiu $a0, -1
|
||||
};
|
||||
bool test_passed_ok = false;
|
||||
int loop_count = 0;
|
||||
@ -52,14 +52,14 @@ int loop_count = 0;
|
||||
|
||||
static void mips_codehook(uc_engine *uc, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
if( address == 0x10000C )
|
||||
test_passed_ok = true;
|
||||
if( address == 0x100004 )
|
||||
{
|
||||
printf("\nloop %d:\n", loop_count);
|
||||
loop_count++;
|
||||
}
|
||||
printf("Code: %"PRIx64"\n", address);
|
||||
if( address == 0x10000C )
|
||||
test_passed_ok = true;
|
||||
if( address == 0x100004 )
|
||||
{
|
||||
printf("\nloop %d:\n", loop_count);
|
||||
loop_count++;
|
||||
}
|
||||
printf("Code: %"PRIx64"\n", address);
|
||||
}
|
||||
|
||||
|
||||
@ -67,74 +67,74 @@ int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_err err;
|
||||
uc_hook hhc;
|
||||
uint32_t val;
|
||||
uc_hook hhc;
|
||||
uint32_t val;
|
||||
|
||||
// dynamically load shared library
|
||||
// dynamically load shared library
|
||||
#ifdef DYNLOAD
|
||||
uc_dyn_load(NULL, 0);
|
||||
uc_dyn_load(NULL, 0);
|
||||
#endif
|
||||
|
||||
// Initialize emulator in MIPS 32bit little endian mode
|
||||
// Initialize emulator in MIPS 32bit little endian mode
|
||||
err = uc_open(UC_ARCH_MIPS, UC_MODE_MIPS32, &uc);
|
||||
if (err)
|
||||
{
|
||||
{
|
||||
printf("Failed on uc_open() with error returned: %u\n", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
// map in a page of mem
|
||||
err = uc_mem_map(uc, addr, 0x1000, UC_PROT_ALL);
|
||||
// map in a page of mem
|
||||
err = uc_mem_map(uc, addr, 0x1000, UC_PROT_ALL);
|
||||
if (err)
|
||||
{
|
||||
{
|
||||
printf("Failed on uc_mem_map() with error returned: %u\n", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
// write machine code to be emulated to memory
|
||||
err = uc_mem_write(uc, addr, loop_test_code, sizeof(loop_test_code));
|
||||
if( err )
|
||||
{
|
||||
if( err )
|
||||
{
|
||||
printf("Failed on uc_mem_write() with error returned: %u\n", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
// hook all instructions by having @begin > @end
|
||||
uc_hook_add(uc, &hhc, UC_HOOK_CODE, mips_codehook, NULL, 1, 0);
|
||||
if( err )
|
||||
{
|
||||
if( err )
|
||||
{
|
||||
printf("Failed on uc_hook_add(code) with error returned: %u\n", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
// execute code
|
||||
printf("---- Executing Code ----\n");
|
||||
err = uc_emu_start(uc, addr, addr + sizeof(loop_test_code), 0, 0);
|
||||
printf("---- Executing Code ----\n");
|
||||
err = uc_emu_start(uc, addr, addr + sizeof(loop_test_code), 0, 0);
|
||||
if (err)
|
||||
{
|
||||
{
|
||||
printf("Failed on uc_emu_start() with error returned %u: %s\n",
|
||||
err, uc_strerror(err));
|
||||
return err;
|
||||
return err;
|
||||
}
|
||||
|
||||
// done executing, print some reg values as a test
|
||||
printf("---- Execution Complete ----\n\n");
|
||||
uc_reg_read(uc, UC_MIPS_REG_PC, &val); printf("pc is %X\n", val);
|
||||
uc_reg_read(uc, UC_MIPS_REG_A0, &val); printf("a0 is %X\n", val);
|
||||
|
||||
// free resources
|
||||
uc_close(uc);
|
||||
|
||||
if( test_passed_ok )
|
||||
printf("\n\nTEST PASSED!\n\n");
|
||||
else
|
||||
printf("\n\nTEST FAILED!\n\n");
|
||||
// done executing, print some reg values as a test
|
||||
printf("---- Execution Complete ----\n\n");
|
||||
uc_reg_read(uc, UC_MIPS_REG_PC, &val); printf("pc is %X\n", val);
|
||||
uc_reg_read(uc, UC_MIPS_REG_A0, &val); printf("a0 is %X\n", val);
|
||||
|
||||
// free resources
|
||||
uc_close(uc);
|
||||
|
||||
if( test_passed_ok )
|
||||
printf("\n\nTEST PASSED!\n\n");
|
||||
else
|
||||
printf("\n\nTEST FAILED!\n\n");
|
||||
|
||||
// dynamically free shared library
|
||||
// dynamically free shared library
|
||||
#ifdef DYNLOAD
|
||||
uc_dyn_free();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -52,11 +52,11 @@ background.
|
||||
// This should loop forever.
|
||||
const uint64_t addr = 0x100000;
|
||||
const unsigned char loop_test_code[] = {
|
||||
0x02,0x00,0x04,0x24, // 100000: li $a0, 2
|
||||
// loop1
|
||||
0x00,0x00,0x00,0x00, // 100004: nop
|
||||
0xFE,0xFF,0x80,0x14, // 100008: bnez $a0, loop1
|
||||
0x00,0x00,0x00,0x00, // 10000C: nop
|
||||
0x02,0x00,0x04,0x24, // 100000: li $a0, 2
|
||||
// loop1
|
||||
0x00,0x00,0x00,0x00, // 100004: nop
|
||||
0xFE,0xFF,0x80,0x14, // 100008: bnez $a0, loop1
|
||||
0x00,0x00,0x00,0x00, // 10000C: nop
|
||||
};
|
||||
bool test_passed_ok = false;
|
||||
int loop_count = 0;
|
||||
@ -65,14 +65,14 @@ int loop_count = 0;
|
||||
// This hook is used to show that code is executing in the emulator.
|
||||
static void mips_codehook(uc_engine *uc, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
printf("Code: %"PRIx64"\n", address);
|
||||
printf("Code: %"PRIx64"\n", address);
|
||||
}
|
||||
|
||||
|
||||
typedef struct {
|
||||
uc_engine *uc;
|
||||
uint64_t startAddr;
|
||||
uint64_t endAddr;
|
||||
uc_engine *uc;
|
||||
uint64_t startAddr;
|
||||
uint64_t endAddr;
|
||||
} EmuStarterParam_t;
|
||||
|
||||
// This is a thread that just runs uc_emu_start() in it.
|
||||
@ -80,38 +80,38 @@ typedef struct {
|
||||
static uc_err emu_starter(void* param)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uint64_t start_addr;
|
||||
uint64_t end_addr;
|
||||
uc_err err;
|
||||
|
||||
EmuStarterParam_t* starter_params = (EmuStarterParam_t *)param;
|
||||
uc = starter_params->uc;
|
||||
start_addr = starter_params->startAddr;
|
||||
end_addr = starter_params->endAddr;
|
||||
|
||||
printf("uc_emu_start()\n");
|
||||
err = uc_emu_start(uc, start_addr, end_addr, 0, 0);
|
||||
uint64_t start_addr;
|
||||
uint64_t end_addr;
|
||||
uc_err err;
|
||||
|
||||
EmuStarterParam_t* starter_params = (EmuStarterParam_t *)param;
|
||||
uc = starter_params->uc;
|
||||
start_addr = starter_params->startAddr;
|
||||
end_addr = starter_params->endAddr;
|
||||
|
||||
printf("uc_emu_start()\n");
|
||||
err = uc_emu_start(uc, start_addr, end_addr, 0, 0);
|
||||
if (err)
|
||||
{
|
||||
{
|
||||
printf("Failed on uc_emu_start() with error returned %u: %s\n",
|
||||
err, uc_strerror(err));
|
||||
}
|
||||
|
||||
return err;
|
||||
return err;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
static unsigned int __stdcall win32_emu_starter(void* param)
|
||||
{
|
||||
uc_err err = emu_starter(param);
|
||||
_endthreadex(err);
|
||||
return err;
|
||||
uc_err err = emu_starter(param);
|
||||
_endthreadex(err);
|
||||
return err;
|
||||
}
|
||||
#else
|
||||
static void* posix_emu_starter(void* param)
|
||||
{
|
||||
uc_err err = emu_starter(param);
|
||||
return (void*)err;
|
||||
uc_err err = emu_starter(param);
|
||||
return (void*)err;
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -120,124 +120,124 @@ int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_err err;
|
||||
int ret;
|
||||
uc_hook hhc;
|
||||
uint32_t val;
|
||||
EmuStarterParam_t starter_params;
|
||||
int ret;
|
||||
uc_hook hhc;
|
||||
uint32_t val;
|
||||
EmuStarterParam_t starter_params;
|
||||
#ifdef _WIN32
|
||||
HANDLE th = (HANDLE)-1;
|
||||
HANDLE th = (HANDLE)-1;
|
||||
#else
|
||||
pthread_t th;
|
||||
pthread_t th;
|
||||
#endif
|
||||
|
||||
// dynamically load shared library
|
||||
// dynamically load shared library
|
||||
#ifdef DYNLOAD
|
||||
uc_dyn_load(NULL, 0);
|
||||
uc_dyn_load(NULL, 0);
|
||||
#endif
|
||||
|
||||
// Initialize emulator in MIPS 32bit little endian mode
|
||||
// Initialize emulator in MIPS 32bit little endian mode
|
||||
printf("uc_open()\n");
|
||||
err = uc_open(UC_ARCH_MIPS, UC_MODE_MIPS32, &uc);
|
||||
err = uc_open(UC_ARCH_MIPS, UC_MODE_MIPS32, &uc);
|
||||
if (err)
|
||||
{
|
||||
{
|
||||
printf("Failed on uc_open() with error returned: %u\n", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
// map in a page of mem
|
||||
printf("uc_mem_map()\n");
|
||||
err = uc_mem_map(uc, addr, 0x1000, UC_PROT_ALL);
|
||||
// map in a page of mem
|
||||
printf("uc_mem_map()\n");
|
||||
err = uc_mem_map(uc, addr, 0x1000, UC_PROT_ALL);
|
||||
if (err)
|
||||
{
|
||||
{
|
||||
printf("Failed on uc_mem_map() with error returned: %u\n", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
printf("uc_mem_write()\n");
|
||||
// write machine code to be emulated to memory
|
||||
printf("uc_mem_write()\n");
|
||||
err = uc_mem_write(uc, addr, loop_test_code, sizeof(loop_test_code));
|
||||
if( err )
|
||||
{
|
||||
if( err )
|
||||
{
|
||||
printf("Failed on uc_mem_write() with error returned: %u\n", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
// hook all instructions by having @begin > @end
|
||||
printf("uc_hook_add()\n");
|
||||
printf("uc_hook_add()\n");
|
||||
uc_hook_add(uc, &hhc, UC_HOOK_CODE, mips_codehook, NULL, 1, 0);
|
||||
if( err )
|
||||
{
|
||||
if( err )
|
||||
{
|
||||
printf("Failed on uc_hook_add(code) with error returned: %u\n", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
// start background thread
|
||||
printf("---- Thread Starting ----\n");
|
||||
starter_params.uc = uc;
|
||||
starter_params.startAddr = addr;
|
||||
starter_params.endAddr = addr + sizeof(loop_test_code);
|
||||
|
||||
|
||||
// start background thread
|
||||
printf("---- Thread Starting ----\n");
|
||||
starter_params.uc = uc;
|
||||
starter_params.startAddr = addr;
|
||||
starter_params.endAddr = addr + sizeof(loop_test_code);
|
||||
|
||||
#ifdef _WIN32
|
||||
// create thread
|
||||
th = (HANDLE)_beginthreadex(NULL, 0, win32_emu_starter, &starter_params, CREATE_SUSPENDED, NULL);
|
||||
if(th == (HANDLE)-1)
|
||||
{
|
||||
printf("Failed on _beginthreadex() with error returned: %u\n", _errno());
|
||||
return -1;
|
||||
}
|
||||
// start thread
|
||||
ret = ResumeThread(th);
|
||||
if( ret == -1 )
|
||||
{
|
||||
printf("Failed on ResumeThread() with error returned: %u\n", _errno());
|
||||
return -2;
|
||||
}
|
||||
// wait 3 seconds
|
||||
Sleep(3 * 1000);
|
||||
// create thread
|
||||
th = (HANDLE)_beginthreadex(NULL, 0, win32_emu_starter, &starter_params, CREATE_SUSPENDED, NULL);
|
||||
if(th == (HANDLE)-1)
|
||||
{
|
||||
printf("Failed on _beginthreadex() with error returned: %u\n", _errno());
|
||||
return -1;
|
||||
}
|
||||
// start thread
|
||||
ret = ResumeThread(th);
|
||||
if( ret == -1 )
|
||||
{
|
||||
printf("Failed on ResumeThread() with error returned: %u\n", _errno());
|
||||
return -2;
|
||||
}
|
||||
// wait 3 seconds
|
||||
Sleep(3 * 1000);
|
||||
#else
|
||||
// add posix code to start the emu_starter() thread
|
||||
ret = pthread_create(&th, NULL, posix_emu_starter, &starter_params);
|
||||
if( ret )
|
||||
{
|
||||
printf("Failed on pthread_create() with error returned: %u\n", err);
|
||||
return -2;
|
||||
}
|
||||
// wait 3 seconds
|
||||
sleep(3);
|
||||
// add posix code to start the emu_starter() thread
|
||||
ret = pthread_create(&th, NULL, posix_emu_starter, &starter_params);
|
||||
if( ret )
|
||||
{
|
||||
printf("Failed on pthread_create() with error returned: %u\n", err);
|
||||
return -2;
|
||||
}
|
||||
// wait 3 seconds
|
||||
sleep(3);
|
||||
#endif
|
||||
|
||||
|
||||
// Stop the thread after it has been let to run in the background for a while
|
||||
printf("---- Thread Stopping ----\n");
|
||||
printf("uc_emu_stop()\n");
|
||||
err = uc_emu_stop(uc);
|
||||
if( err )
|
||||
{
|
||||
// Stop the thread after it has been let to run in the background for a while
|
||||
printf("---- Thread Stopping ----\n");
|
||||
printf("uc_emu_stop()\n");
|
||||
err = uc_emu_stop(uc);
|
||||
if( err )
|
||||
{
|
||||
printf("Failed on uc_emu_stop() with error returned: %u\n", err);
|
||||
return err;
|
||||
}
|
||||
test_passed_ok = true;
|
||||
|
||||
test_passed_ok = true;
|
||||
|
||||
|
||||
// done executing, print some reg values as a test
|
||||
uc_reg_read(uc, UC_MIPS_REG_PC, &val); printf("pc is %X\n", val);
|
||||
uc_reg_read(uc, UC_MIPS_REG_A0, &val); printf("a0 is %X\n", val);
|
||||
|
||||
// free resources
|
||||
printf("uc_close()\n");
|
||||
uc_close(uc);
|
||||
|
||||
if( test_passed_ok )
|
||||
printf("\n\nTEST PASSED!\n\n");
|
||||
else
|
||||
printf("\n\nTEST FAILED!\n\n");
|
||||
// done executing, print some reg values as a test
|
||||
uc_reg_read(uc, UC_MIPS_REG_PC, &val); printf("pc is %X\n", val);
|
||||
uc_reg_read(uc, UC_MIPS_REG_A0, &val); printf("a0 is %X\n", val);
|
||||
|
||||
// free resources
|
||||
printf("uc_close()\n");
|
||||
uc_close(uc);
|
||||
|
||||
if( test_passed_ok )
|
||||
printf("\n\nTEST PASSED!\n\n");
|
||||
else
|
||||
printf("\n\nTEST FAILED!\n\n");
|
||||
|
||||
// dynamically free shared library
|
||||
// dynamically free shared library
|
||||
#ifdef DYNLOAD
|
||||
uc_dyn_free();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
refer to issue #575.
|
||||
to run correctly unicorn needs to be compiled for AArch64.
|
||||
refer to issue #575.
|
||||
to run correctly unicorn needs to be compiled for AArch64.
|
||||
*/
|
||||
|
||||
#include "unicorn_test.h"
|
||||
@ -9,94 +9,94 @@
|
||||
|
||||
uint64_t trunc_page(uint64_t addr)
|
||||
{
|
||||
return (addr & ~(4095));
|
||||
return (addr & ~(4095));
|
||||
}
|
||||
|
||||
/* Called before every test to set up a new instance */
|
||||
static int init(void **state)
|
||||
{
|
||||
printf("[+] Initializing Unicorn...\n");
|
||||
uc_engine *uc;
|
||||
printf("[+] Initializing Unicorn...\n");
|
||||
uc_engine *uc;
|
||||
|
||||
if (uc_open(UC_ARCH_ARM64, UC_MODE_ARM, &uc) != UC_ERR_OK) {
|
||||
printf("Error on open. Be sure that your unicorn library supports AArch64.\n");
|
||||
return -1;
|
||||
}
|
||||
if (uc_open(UC_ARCH_ARM64, UC_MODE_ARM, &uc) != UC_ERR_OK) {
|
||||
printf("Error on open. Be sure that your unicorn library supports AArch64.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
*state = uc;
|
||||
*state = uc;
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Called after every test to clean up */
|
||||
static int teardown(void **state)
|
||||
{
|
||||
printf("[+] Exiting...\n");
|
||||
uc_engine *uc = *state;
|
||||
printf("[+] Exiting...\n");
|
||||
uc_engine *uc = *state;
|
||||
|
||||
uc_close(uc);
|
||||
uc_close(uc);
|
||||
|
||||
*state = NULL;
|
||||
return 0;
|
||||
*state = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void test_hang(void **state)
|
||||
{
|
||||
uint32_t code[] = {
|
||||
0xd503201f, /* NOP */
|
||||
0xd503201f, /* NOP */
|
||||
0xd503201f, /* NOP */
|
||||
0xaa0103e0 /* MOV X0, X1 */
|
||||
};
|
||||
uint32_t code[] = {
|
||||
0xd503201f, /* NOP */
|
||||
0xd503201f, /* NOP */
|
||||
0xd503201f, /* NOP */
|
||||
0xaa0103e0 /* MOV X0, X1 */
|
||||
};
|
||||
|
||||
uc_engine *uc = *state;
|
||||
uc_engine *uc = *state;
|
||||
|
||||
uint64_t x0 = 0;
|
||||
uint64_t x1 = 1;
|
||||
uint64_t x0 = 0;
|
||||
uint64_t x1 = 1;
|
||||
|
||||
/*
|
||||
* emulation will hang if some instruction hits every quarter of a page,
|
||||
* i.e. these offsets:
|
||||
* 0x1400, 0x1800, 0x1c00, 0x2000
|
||||
*
|
||||
* in this test, the code to be emulated is mapped just before the 0x1400
|
||||
* offset, so that the final instruction emulated (MOV X0, X1) hits the offset,
|
||||
* causing the hang.
|
||||
* If you try to write the code just four bytes behind, the hang doesn't occur.
|
||||
*
|
||||
* So far, this strange behaviour has only been observed with AArch64 Unicorn APIs.
|
||||
*/
|
||||
/*
|
||||
* emulation will hang if some instruction hits every quarter of a page,
|
||||
* i.e. these offsets:
|
||||
* 0x1400, 0x1800, 0x1c00, 0x2000
|
||||
*
|
||||
* in this test, the code to be emulated is mapped just before the 0x1400
|
||||
* offset, so that the final instruction emulated (MOV X0, X1) hits the offset,
|
||||
* causing the hang.
|
||||
* If you try to write the code just four bytes behind, the hang doesn't occur.
|
||||
*
|
||||
* So far, this strange behaviour has only been observed with AArch64 Unicorn APIs.
|
||||
*/
|
||||
|
||||
uint64_t addr = 0x13f0; // try to map at (0x13f0 - 0x4) and the hang doesn't occur
|
||||
uint64_t trunc_addr = trunc_page(addr); // round down to nearest page
|
||||
uint64_t addr = 0x13f0; // try to map at (0x13f0 - 0x4) and the hang doesn't occur
|
||||
uint64_t trunc_addr = trunc_page(addr); // round down to nearest page
|
||||
|
||||
uc_mem_map(uc, trunc_addr, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
uc_mem_map(uc, trunc_addr, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
if (uc_mem_write(uc, addr, &code, sizeof(code))) {
|
||||
printf("error on write\n");
|
||||
return;
|
||||
}
|
||||
if (uc_mem_write(uc, addr, &code, sizeof(code))) {
|
||||
printf("error on write\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uc_reg_write(uc, UC_ARM64_REG_X0, &x0);
|
||||
uc_reg_write(uc, UC_ARM64_REG_X1, &x1);
|
||||
uc_reg_write(uc, UC_ARM64_REG_X0, &x0);
|
||||
uc_reg_write(uc, UC_ARM64_REG_X1, &x1);
|
||||
|
||||
if (uc_emu_start(uc, addr, addr + sizeof(code), 0, 0)) {
|
||||
printf("error on start\n");
|
||||
return;
|
||||
}
|
||||
if (uc_emu_start(uc, addr, addr + sizeof(code), 0, 0)) {
|
||||
printf("error on start\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uc_reg_read(uc, UC_ARM64_REG_X0, &x0);
|
||||
uc_reg_read(uc, UC_ARM64_REG_X1, &x1);
|
||||
uc_reg_read(uc, UC_ARM64_REG_X0, &x0);
|
||||
uc_reg_read(uc, UC_ARM64_REG_X1, &x1);
|
||||
|
||||
printf("x0: %"PRIx64"\n", x0);
|
||||
printf("x1: %"PRIx64"\n", x1);
|
||||
printf("x0: %"PRIx64"\n", x0);
|
||||
printf("x1: %"PRIx64"\n", x1);
|
||||
}
|
||||
|
||||
int main(int argc, const char * argv[]) {
|
||||
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test_setup_teardown(test_hang, init, teardown),
|
||||
};
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test_setup_teardown(test_hang, init, teardown),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);;
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);;
|
||||
}
|
||||
|
@ -16,24 +16,24 @@
|
||||
|
||||
typedef struct _reg_value
|
||||
{
|
||||
uint32_t regId, regValue, mask;
|
||||
uint32_t regId, regValue, mask;
|
||||
} reg_value;
|
||||
|
||||
typedef struct _instruction
|
||||
{
|
||||
const char* asmStr;
|
||||
uint8_t code[16]; //x86 inst == 15 bytes max
|
||||
uint32_t codeSize;
|
||||
reg_value* values;
|
||||
uint32_t nbValues;
|
||||
uint32_t addr;
|
||||
const char* asmStr;
|
||||
uint8_t code[16]; //x86 inst == 15 bytes max
|
||||
uint32_t codeSize;
|
||||
reg_value* values;
|
||||
uint32_t nbValues;
|
||||
uint32_t addr;
|
||||
} instruction;
|
||||
|
||||
typedef struct _block
|
||||
{
|
||||
instruction* insts[255];
|
||||
uint32_t nbInsts;
|
||||
uint32_t size;
|
||||
instruction* insts[255];
|
||||
uint32_t nbInsts;
|
||||
uint32_t size;
|
||||
} block;
|
||||
|
||||
/******************************************************************************/
|
||||
@ -42,23 +42,23 @@ typedef struct _block
|
||||
#define CAT(X, Y) CAT2(X, Y)
|
||||
|
||||
#define BLOCK_START(BLOCK) \
|
||||
{ \
|
||||
block* blockPtr = █ \
|
||||
blockPtr->nbInsts = 0; \
|
||||
instruction* instPtr = NULL;
|
||||
{ \
|
||||
block* blockPtr = █ \
|
||||
blockPtr->nbInsts = 0; \
|
||||
instruction* instPtr = NULL;
|
||||
|
||||
#define BLOCK_END() }
|
||||
|
||||
#define BLOCK_ADD(CODE_ASM, CODE) \
|
||||
const uint8_t CAT(code, __LINE__)[] = CODE; \
|
||||
instPtr = newInstruction(CAT(code, __LINE__), sizeof(CAT(code, __LINE__)), CODE_ASM, NULL, 0); \
|
||||
addInstructionToBlock(blockPtr, instPtr);
|
||||
const uint8_t CAT(code, __LINE__)[] = CODE; \
|
||||
instPtr = newInstruction(CAT(code, __LINE__), sizeof(CAT(code, __LINE__)), CODE_ASM, NULL, 0); \
|
||||
addInstructionToBlock(blockPtr, instPtr);
|
||||
|
||||
#define BLOCK_ADD_CHECK(CODE_ASM, CODE, REGVALUES) \
|
||||
const uint8_t CAT(code, __LINE__)[] = CODE; \
|
||||
const reg_value CAT(regValues, __LINE__)[] = REGVALUES; \
|
||||
instPtr = newInstruction(CAT(code, __LINE__), sizeof(CAT(code, __LINE__)), CODE_ASM, CAT(regValues, __LINE__), sizeof(CAT(regValues, __LINE__)) / sizeof(reg_value)); \
|
||||
addInstructionToBlock(blockPtr, instPtr);
|
||||
const uint8_t CAT(code, __LINE__)[] = CODE; \
|
||||
const reg_value CAT(regValues, __LINE__)[] = REGVALUES; \
|
||||
instPtr = newInstruction(CAT(code, __LINE__), sizeof(CAT(code, __LINE__)), CODE_ASM, CAT(regValues, __LINE__), sizeof(CAT(regValues, __LINE__)) / sizeof(reg_value)); \
|
||||
addInstructionToBlock(blockPtr, instPtr);
|
||||
|
||||
#define V(...) { __VA_ARGS__ }
|
||||
|
||||
@ -77,43 +77,43 @@ void initRegisters(uc_engine *uc);
|
||||
|
||||
void hook_code_test_i386_shl(uc_engine *uc, uint64_t address, uint32_t size, void *user_data)
|
||||
{
|
||||
uint32_t i;
|
||||
block* b = (block*)user_data;
|
||||
instruction* currInst = getInstruction(b, (uint32_t)address);
|
||||
assert_true(currInst != NULL);
|
||||
uint32_t i;
|
||||
block* b = (block*)user_data;
|
||||
instruction* currInst = getInstruction(b, (uint32_t)address);
|
||||
assert_true(currInst != NULL);
|
||||
|
||||
print_message("|\teip=%08x - %s\n", (uint32_t)address, currInst->asmStr);
|
||||
print_message("|\teip=%08x - %s\n", (uint32_t)address, currInst->asmStr);
|
||||
|
||||
for (i = 0; i < currInst->nbValues; i++)
|
||||
{
|
||||
uint32_t regValue = getRegisterValue(uc, currInst->values[i].regId);
|
||||
print_message("|\t\ttesting %s : ", getRegisterName(currInst->values[i].regId));
|
||||
assert_int_equal(regValue & currInst->values[i].mask, currInst->values[i].regValue);
|
||||
print_message("ok\n");
|
||||
}
|
||||
for (i = 0; i < currInst->nbValues; i++)
|
||||
{
|
||||
uint32_t regValue = getRegisterValue(uc, currInst->values[i].regId);
|
||||
print_message("|\t\ttesting %s : ", getRegisterName(currInst->values[i].regId));
|
||||
assert_int_equal(regValue & currInst->values[i].mask, currInst->values[i].regValue);
|
||||
print_message("ok\n");
|
||||
}
|
||||
|
||||
if (currInst->code[0] == 0xCC)
|
||||
OK(uc_emu_stop(uc));
|
||||
if (currInst->code[0] == 0xCC)
|
||||
OK(uc_emu_stop(uc));
|
||||
}
|
||||
|
||||
bool hook_mem_invalid(uc_engine *uc, uc_mem_type type, uint64_t addr, int size, int64_t value, void *user_data)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
default:
|
||||
print_message("hook_mem_invalid: UC_HOOK_MEM_INVALID type: %d at 0x%" PRIx64 "\n", type, addr); break;
|
||||
case UC_MEM_READ_UNMAPPED:
|
||||
print_message("hook_mem_invalid: Read from invalid memory at 0x%" PRIx64 ", data size = %u\n", addr, size); break;
|
||||
case UC_MEM_WRITE_UNMAPPED:
|
||||
print_message("hook_mem_invalid: Write to invalid memory at 0x%" PRIx64 ", data size = %u, data value = 0x%" PRIx64 "\n", addr, size, value); break;
|
||||
case UC_MEM_FETCH_PROT:
|
||||
print_message("hook_mem_invalid: Fetch from non-executable memory at 0x%" PRIx64 "\n", addr); break;
|
||||
case UC_MEM_WRITE_PROT:
|
||||
print_message("hook_mem_invalid: Write to non-writeable memory at 0x%" PRIx64 ", data size = %u, data value = 0x%" PRIx64 "\n", addr, size, value); break;
|
||||
case UC_MEM_READ_PROT:
|
||||
print_message("hook_mem_invalid: Read from non-readable memory at 0x%" PRIx64 ", data size = %u\n", addr, size); break;
|
||||
}
|
||||
return false;
|
||||
switch (type)
|
||||
{
|
||||
default:
|
||||
print_message("hook_mem_invalid: UC_HOOK_MEM_INVALID type: %d at 0x%" PRIx64 "\n", type, addr); break;
|
||||
case UC_MEM_READ_UNMAPPED:
|
||||
print_message("hook_mem_invalid: Read from invalid memory at 0x%" PRIx64 ", data size = %u\n", addr, size); break;
|
||||
case UC_MEM_WRITE_UNMAPPED:
|
||||
print_message("hook_mem_invalid: Write to invalid memory at 0x%" PRIx64 ", data size = %u, data value = 0x%" PRIx64 "\n", addr, size, value); break;
|
||||
case UC_MEM_FETCH_PROT:
|
||||
print_message("hook_mem_invalid: Fetch from non-executable memory at 0x%" PRIx64 "\n", addr); break;
|
||||
case UC_MEM_WRITE_PROT:
|
||||
print_message("hook_mem_invalid: Write to non-writeable memory at 0x%" PRIx64 ", data size = %u, data value = 0x%" PRIx64 "\n", addr, size, value); break;
|
||||
case UC_MEM_READ_PROT:
|
||||
print_message("hook_mem_invalid: Read from non-readable memory at 0x%" PRIx64 ", data size = %u\n", addr, size); break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#define ADDR_CODE 0x100000
|
||||
@ -123,311 +123,311 @@ bool hook_mem_invalid(uc_engine *uc, uc_mem_type type, uint64_t addr, int size,
|
||||
|
||||
static void test_i386_shl_cl(void **state)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_hook trace1;
|
||||
block b;
|
||||
uc_engine *uc;
|
||||
uc_hook trace1;
|
||||
block b;
|
||||
|
||||
// Initialize emulator in X86-32bit mode
|
||||
OK(uc_open(UC_ARCH_X86, UC_MODE_32, &uc));
|
||||
OK(uc_mem_map(uc, ADDR_CODE, 0x1000, UC_PROT_ALL));
|
||||
|
||||
initRegisters(uc);
|
||||
// Initialize emulator in X86-32bit mode
|
||||
OK(uc_open(UC_ARCH_X86, UC_MODE_32, &uc));
|
||||
OK(uc_mem_map(uc, ADDR_CODE, 0x1000, UC_PROT_ALL));
|
||||
|
||||
initRegisters(uc);
|
||||
|
||||
BLOCK_START(b);
|
||||
BLOCK_ADD( "mov ebx, 3Ch", V(0xBB, 0x3C, 0x00, 0x00, 0x00));
|
||||
BLOCK_ADD_CHECK("mov cl, 2", V(0xB1, 0x02), V(V(UC_X86_REG_EBX, 0x3C, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("shl ebx, cl", V(0xD3, 0xE3), V(V(UC_X86_REG_CL, 0x2, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("lahf", V(0x9F), V(V(UC_X86_REG_EBX, 0xF0, NO_MASK), V(UC_X86_REG_EFLAGS, 0x4, ALL_MASK)));
|
||||
BLOCK_ADD_CHECK("int3", V(0xCC), V(V(UC_X86_REG_AH, 0x4, PF_MASK)));
|
||||
BLOCK_END();
|
||||
BLOCK_START(b);
|
||||
BLOCK_ADD( "mov ebx, 3Ch", V(0xBB, 0x3C, 0x00, 0x00, 0x00));
|
||||
BLOCK_ADD_CHECK("mov cl, 2", V(0xB1, 0x02), V(V(UC_X86_REG_EBX, 0x3C, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("shl ebx, cl", V(0xD3, 0xE3), V(V(UC_X86_REG_CL, 0x2, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("lahf", V(0x9F), V(V(UC_X86_REG_EBX, 0xF0, NO_MASK), V(UC_X86_REG_EFLAGS, 0x4, ALL_MASK)));
|
||||
BLOCK_ADD_CHECK("int3", V(0xCC), V(V(UC_X86_REG_AH, 0x4, PF_MASK)));
|
||||
BLOCK_END();
|
||||
|
||||
loadBlock(uc, &b, ADDR_CODE);
|
||||
loadBlock(uc, &b, ADDR_CODE);
|
||||
|
||||
OK(uc_hook_add(uc, &trace1, UC_HOOK_CODE, hook_code_test_i386_shl, &b, 1, 0));
|
||||
OK(uc_hook_add(uc, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL, 1, 0));
|
||||
OK(uc_hook_add(uc, &trace1, UC_HOOK_CODE, hook_code_test_i386_shl, &b, 1, 0));
|
||||
OK(uc_hook_add(uc, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL, 1, 0));
|
||||
|
||||
// emulate machine code in infinite time
|
||||
OK(uc_emu_start(uc, ADDR_CODE, ADDR_CODE + b.size, 0, 0));
|
||||
// emulate machine code in infinite time
|
||||
OK(uc_emu_start(uc, ADDR_CODE, ADDR_CODE + b.size, 0, 0));
|
||||
|
||||
freeBlock(&b);
|
||||
freeBlock(&b);
|
||||
|
||||
uc_close(uc);
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_i386_shl_imm(void **state)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_hook trace1;
|
||||
block b;
|
||||
uc_engine *uc;
|
||||
uc_hook trace1;
|
||||
block b;
|
||||
|
||||
// Initialize emulator in X86-32bit mode
|
||||
OK(uc_open(UC_ARCH_X86, UC_MODE_32, &uc));
|
||||
OK(uc_mem_map(uc, ADDR_CODE, 0x1000, UC_PROT_ALL));
|
||||
// Initialize emulator in X86-32bit mode
|
||||
OK(uc_open(UC_ARCH_X86, UC_MODE_32, &uc));
|
||||
OK(uc_mem_map(uc, ADDR_CODE, 0x1000, UC_PROT_ALL));
|
||||
|
||||
initRegisters(uc);
|
||||
initRegisters(uc);
|
||||
|
||||
BLOCK_START(b);
|
||||
BLOCK_ADD( "mov ebx, 3Ch", V(0xBB, 0x3C, 0x00, 0x00, 0x00));
|
||||
BLOCK_ADD( "shl ebx, 2", V(0xC1, 0xE3, 0x02));
|
||||
BLOCK_ADD_CHECK("lahf", V(0x9F), V(V(UC_X86_REG_EBX, 0xF0, NO_MASK), V(UC_X86_REG_EFLAGS, 0x4, ALL_MASK)));
|
||||
BLOCK_ADD_CHECK("int3", V(0xCC), V(V(UC_X86_REG_AH, 0x4, PF_MASK)));
|
||||
BLOCK_END();
|
||||
BLOCK_START(b);
|
||||
BLOCK_ADD( "mov ebx, 3Ch", V(0xBB, 0x3C, 0x00, 0x00, 0x00));
|
||||
BLOCK_ADD( "shl ebx, 2", V(0xC1, 0xE3, 0x02));
|
||||
BLOCK_ADD_CHECK("lahf", V(0x9F), V(V(UC_X86_REG_EBX, 0xF0, NO_MASK), V(UC_X86_REG_EFLAGS, 0x4, ALL_MASK)));
|
||||
BLOCK_ADD_CHECK("int3", V(0xCC), V(V(UC_X86_REG_AH, 0x4, PF_MASK)));
|
||||
BLOCK_END();
|
||||
|
||||
loadBlock(uc, &b, ADDR_CODE);
|
||||
|
||||
OK(uc_hook_add(uc, &trace1, UC_HOOK_CODE, hook_code_test_i386_shl, &b, 1, 0));
|
||||
OK(uc_hook_add(uc, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL, 1, 0));
|
||||
|
||||
// emulate machine code in infinite time
|
||||
OK(uc_emu_start(uc, ADDR_CODE, ADDR_CODE + b.size, 0, 0));
|
||||
|
||||
freeBlock(&b);
|
||||
loadBlock(uc, &b, ADDR_CODE);
|
||||
|
||||
OK(uc_hook_add(uc, &trace1, UC_HOOK_CODE, hook_code_test_i386_shl, &b, 1, 0));
|
||||
OK(uc_hook_add(uc, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL, 1, 0));
|
||||
|
||||
// emulate machine code in infinite time
|
||||
OK(uc_emu_start(uc, ADDR_CODE, ADDR_CODE + b.size, 0, 0));
|
||||
|
||||
freeBlock(&b);
|
||||
|
||||
uc_close(uc);
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_i386_enter_leave(void **state)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_hook trace1;
|
||||
block b;
|
||||
uc_engine *uc;
|
||||
uc_hook trace1;
|
||||
block b;
|
||||
|
||||
// Initialize emulator in X86-32bit mode
|
||||
OK(uc_open(UC_ARCH_X86, UC_MODE_32, &uc));
|
||||
OK(uc_mem_map(uc, ADDR_CODE, 0x1000, UC_PROT_ALL));
|
||||
OK(uc_mem_map(uc, ADDR_STACK - 0x1000, 0x1000, UC_PROT_ALL));
|
||||
// Initialize emulator in X86-32bit mode
|
||||
OK(uc_open(UC_ARCH_X86, UC_MODE_32, &uc));
|
||||
OK(uc_mem_map(uc, ADDR_CODE, 0x1000, UC_PROT_ALL));
|
||||
OK(uc_mem_map(uc, ADDR_STACK - 0x1000, 0x1000, UC_PROT_ALL));
|
||||
|
||||
initRegisters(uc);
|
||||
initRegisters(uc);
|
||||
|
||||
BLOCK_START(b);
|
||||
BLOCK_ADD( "mov esp, 0x200000", V(0xBC, 0x00, 0x00, 0x20, 0x00));
|
||||
BLOCK_ADD_CHECK("mov eax, 1", V(0xB8, 0x01, 0x00, 0x00, 0x00), V(V(UC_X86_REG_ESP, 0x200000, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("call 0x100015", V(0xE8, 0x06, 0x00, 0x00, 0x00), V(V(UC_X86_REG_EAX, 0x1, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("mov eax, 3", V(0xB8, 0x03, 0x00, 0x00, 0x00), V(V(UC_X86_REG_EAX, 0x2, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("int3", V(0xCC), V(V(UC_X86_REG_EAX, 0x3, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("enter 0x10,0", V(0xC8, 0x10, 0x00, 0x00), V(V(UC_X86_REG_ESP, 0x200000 - 4, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("mov eax, 2", V(0xB8, 0x02, 0x00, 0x00, 0x00), V(V(UC_X86_REG_ESP, 0x200000 - 4 - 4 - 0x10, NO_MASK), V(UC_X86_REG_EBP, 0x200000 - 4 - 4, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("leave", V(0xC9), V(V(UC_X86_REG_EAX, 0x2, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("ret", V(0xC3), V(V(UC_X86_REG_ESP, 0x200000 - 4, NO_MASK)));
|
||||
BLOCK_END();
|
||||
BLOCK_START(b);
|
||||
BLOCK_ADD( "mov esp, 0x200000", V(0xBC, 0x00, 0x00, 0x20, 0x00));
|
||||
BLOCK_ADD_CHECK("mov eax, 1", V(0xB8, 0x01, 0x00, 0x00, 0x00), V(V(UC_X86_REG_ESP, 0x200000, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("call 0x100015", V(0xE8, 0x06, 0x00, 0x00, 0x00), V(V(UC_X86_REG_EAX, 0x1, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("mov eax, 3", V(0xB8, 0x03, 0x00, 0x00, 0x00), V(V(UC_X86_REG_EAX, 0x2, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("int3", V(0xCC), V(V(UC_X86_REG_EAX, 0x3, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("enter 0x10,0", V(0xC8, 0x10, 0x00, 0x00), V(V(UC_X86_REG_ESP, 0x200000 - 4, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("mov eax, 2", V(0xB8, 0x02, 0x00, 0x00, 0x00), V(V(UC_X86_REG_ESP, 0x200000 - 4 - 4 - 0x10, NO_MASK), V(UC_X86_REG_EBP, 0x200000 - 4 - 4, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("leave", V(0xC9), V(V(UC_X86_REG_EAX, 0x2, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("ret", V(0xC3), V(V(UC_X86_REG_ESP, 0x200000 - 4, NO_MASK)));
|
||||
BLOCK_END();
|
||||
|
||||
loadBlock(uc, &b, ADDR_CODE);
|
||||
loadBlock(uc, &b, ADDR_CODE);
|
||||
|
||||
OK(uc_hook_add(uc, &trace1, UC_HOOK_CODE, hook_code_test_i386_shl, &b, 1, 0));
|
||||
OK(uc_hook_add(uc, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL, 1, 0));
|
||||
OK(uc_hook_add(uc, &trace1, UC_HOOK_CODE, hook_code_test_i386_shl, &b, 1, 0));
|
||||
OK(uc_hook_add(uc, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL, 1, 0));
|
||||
|
||||
// emulate machine code in infinite time
|
||||
OK(uc_emu_start(uc, ADDR_CODE, ADDR_CODE + b.size, 0, 0));
|
||||
// emulate machine code in infinite time
|
||||
OK(uc_emu_start(uc, ADDR_CODE, ADDR_CODE + b.size, 0, 0));
|
||||
|
||||
freeBlock(&b);
|
||||
freeBlock(&b);
|
||||
|
||||
uc_close(uc);
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
static void test_i386_enter_nested_leave(void **state)
|
||||
{
|
||||
uc_engine *uc;
|
||||
uc_hook trace1;
|
||||
block b;
|
||||
uc_engine *uc;
|
||||
uc_hook trace1;
|
||||
block b;
|
||||
|
||||
// Initialize emulator in X86-32bit mode
|
||||
OK(uc_open(UC_ARCH_X86, UC_MODE_32, &uc));
|
||||
OK(uc_mem_map(uc, ADDR_CODE, 0x1000, UC_PROT_ALL));
|
||||
OK(uc_mem_map(uc, ADDR_STACK - 0x1000, 0x1000, UC_PROT_ALL));
|
||||
// Initialize emulator in X86-32bit mode
|
||||
OK(uc_open(UC_ARCH_X86, UC_MODE_32, &uc));
|
||||
OK(uc_mem_map(uc, ADDR_CODE, 0x1000, UC_PROT_ALL));
|
||||
OK(uc_mem_map(uc, ADDR_STACK - 0x1000, 0x1000, UC_PROT_ALL));
|
||||
|
||||
initRegisters(uc);
|
||||
initRegisters(uc);
|
||||
|
||||
BLOCK_START(b);
|
||||
BLOCK_ADD( "mov esp, 0x200000", V(0xBC, 0x00, 0x00, 0x20, 0x00));
|
||||
BLOCK_ADD_CHECK("mov eax, 1", V(0xB8, 0x01, 0x00, 0x00, 0x00), V(V(UC_X86_REG_ESP, 0x200000, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("call 0x100015", V(0xE8, 0x06, 0x00, 0x00, 0x00), V(V(UC_X86_REG_EAX, 0x1, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("mov eax, 3", V(0xB8, 0x03, 0x00, 0x00, 0x00), V(V(UC_X86_REG_EAX, 0x2, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("int3", V(0xCC), V(V(UC_X86_REG_EAX, 0x3, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("mov ebp, esp", V(0x89, 0xE5), V(V(UC_X86_REG_ESP, 0x200000 - 4, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("enter 0x10,1", V(0xC8, 0x10, 0x00, 0x01), V(V(UC_X86_REG_EBP, 0x200000 - 4, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("mov eax, 2", V(0xB8, 0x02, 0x00, 0x00, 0x00), V(V(UC_X86_REG_ESP, 0x200000 - 4 - 2*4 - 0x10, NO_MASK), V(UC_X86_REG_EBP, 0x200000 - 4 - 4, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("leave", V(0xC9), V(V(UC_X86_REG_EAX, 0x2, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("ret", V(0xC3), V(V(UC_X86_REG_ESP, 0x200000 - 4, NO_MASK)));
|
||||
BLOCK_END();
|
||||
BLOCK_START(b);
|
||||
BLOCK_ADD( "mov esp, 0x200000", V(0xBC, 0x00, 0x00, 0x20, 0x00));
|
||||
BLOCK_ADD_CHECK("mov eax, 1", V(0xB8, 0x01, 0x00, 0x00, 0x00), V(V(UC_X86_REG_ESP, 0x200000, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("call 0x100015", V(0xE8, 0x06, 0x00, 0x00, 0x00), V(V(UC_X86_REG_EAX, 0x1, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("mov eax, 3", V(0xB8, 0x03, 0x00, 0x00, 0x00), V(V(UC_X86_REG_EAX, 0x2, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("int3", V(0xCC), V(V(UC_X86_REG_EAX, 0x3, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("mov ebp, esp", V(0x89, 0xE5), V(V(UC_X86_REG_ESP, 0x200000 - 4, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("enter 0x10,1", V(0xC8, 0x10, 0x00, 0x01), V(V(UC_X86_REG_EBP, 0x200000 - 4, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("mov eax, 2", V(0xB8, 0x02, 0x00, 0x00, 0x00), V(V(UC_X86_REG_ESP, 0x200000 - 4 - 2*4 - 0x10, NO_MASK), V(UC_X86_REG_EBP, 0x200000 - 4 - 4, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("leave", V(0xC9), V(V(UC_X86_REG_EAX, 0x2, NO_MASK)));
|
||||
BLOCK_ADD_CHECK("ret", V(0xC3), V(V(UC_X86_REG_ESP, 0x200000 - 4, NO_MASK)));
|
||||
BLOCK_END();
|
||||
|
||||
loadBlock(uc, &b, ADDR_CODE);
|
||||
loadBlock(uc, &b, ADDR_CODE);
|
||||
|
||||
OK(uc_hook_add(uc, &trace1, UC_HOOK_CODE, hook_code_test_i386_shl, &b, 1, 0));
|
||||
OK(uc_hook_add(uc, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL, 1, 0));
|
||||
OK(uc_hook_add(uc, &trace1, UC_HOOK_CODE, hook_code_test_i386_shl, &b, 1, 0));
|
||||
OK(uc_hook_add(uc, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL, 1, 0));
|
||||
|
||||
// emulate machine code in infinite time
|
||||
OK(uc_emu_start(uc, ADDR_CODE, ADDR_CODE + b.size, 0, 0));
|
||||
// emulate machine code in infinite time
|
||||
OK(uc_emu_start(uc, ADDR_CODE, ADDR_CODE + b.size, 0, 0));
|
||||
|
||||
freeBlock(&b);
|
||||
|
||||
uc_close(uc);
|
||||
freeBlock(&b);
|
||||
|
||||
uc_close(uc);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int main(void) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
const struct CMUnitTest tests[] = {
|
||||
|
||||
cmocka_unit_test(test_i386_shl_cl),
|
||||
cmocka_unit_test(test_i386_shl_imm),
|
||||
cmocka_unit_test(test_i386_enter_leave),
|
||||
cmocka_unit_test(test_i386_enter_nested_leave),
|
||||
};
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
cmocka_unit_test(test_i386_shl_cl),
|
||||
cmocka_unit_test(test_i386_shl_imm),
|
||||
cmocka_unit_test(test_i386_enter_leave),
|
||||
cmocka_unit_test(test_i386_enter_nested_leave),
|
||||
};
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
instruction* newInstruction(const uint8_t * _code, uint32_t _codeSize, const char* _asmStr, const reg_value* _values, uint32_t _nbValues)
|
||||
{
|
||||
instruction* inst = (instruction*)malloc(sizeof(instruction));
|
||||
instruction* inst = (instruction*)malloc(sizeof(instruction));
|
||||
|
||||
inst->asmStr = _asmStr;
|
||||
memcpy(inst->code, _code, _codeSize);
|
||||
inst->codeSize = _codeSize;
|
||||
inst->nbValues = 0;
|
||||
if (_values)
|
||||
{
|
||||
inst->values = (reg_value*)malloc(_nbValues*sizeof(reg_value));
|
||||
memcpy(inst->values, _values, _nbValues*sizeof(reg_value));
|
||||
inst->nbValues = _nbValues;
|
||||
}
|
||||
inst->asmStr = _asmStr;
|
||||
memcpy(inst->code, _code, _codeSize);
|
||||
inst->codeSize = _codeSize;
|
||||
inst->nbValues = 0;
|
||||
if (_values)
|
||||
{
|
||||
inst->values = (reg_value*)malloc(_nbValues*sizeof(reg_value));
|
||||
memcpy(inst->values, _values, _nbValues*sizeof(reg_value));
|
||||
inst->nbValues = _nbValues;
|
||||
}
|
||||
|
||||
return inst;
|
||||
return inst;
|
||||
}
|
||||
|
||||
void addInstructionToBlock(block* _b, instruction* _i)
|
||||
{
|
||||
_b->insts[_b->nbInsts++] = _i;
|
||||
_b->insts[_b->nbInsts++] = _i;
|
||||
}
|
||||
|
||||
uint32_t loadBlock(uc_engine *_uc, block* _block, uint32_t _at)
|
||||
{
|
||||
uint32_t i, j, offset;
|
||||
uint32_t i, j, offset;
|
||||
|
||||
for (i = 0, offset = 0; i < _block->nbInsts; i++)
|
||||
{
|
||||
const uint32_t codeSize = _block->insts[i]->codeSize;
|
||||
const uint8_t* code = _block->insts[i]->code;
|
||||
_block->insts[i]->addr = _at + offset;
|
||||
print_message("load: %08X: ", _block->insts[i]->addr);
|
||||
for (j = 0; j < codeSize; j++) print_message("%02X ", code[j]);
|
||||
for (j = 0; j < 15 - codeSize; j++) print_message(" ");
|
||||
print_message("%s\n", _block->insts[i]->asmStr);
|
||||
OK(uc_mem_write(_uc, _at + offset, code, codeSize));
|
||||
offset += codeSize;
|
||||
}
|
||||
_block->size = offset;
|
||||
return offset;
|
||||
for (i = 0, offset = 0; i < _block->nbInsts; i++)
|
||||
{
|
||||
const uint32_t codeSize = _block->insts[i]->codeSize;
|
||||
const uint8_t* code = _block->insts[i]->code;
|
||||
_block->insts[i]->addr = _at + offset;
|
||||
print_message("load: %08X: ", _block->insts[i]->addr);
|
||||
for (j = 0; j < codeSize; j++) print_message("%02X ", code[j]);
|
||||
for (j = 0; j < 15 - codeSize; j++) print_message(" ");
|
||||
print_message("%s\n", _block->insts[i]->asmStr);
|
||||
OK(uc_mem_write(_uc, _at + offset, code, codeSize));
|
||||
offset += codeSize;
|
||||
}
|
||||
_block->size = offset;
|
||||
return offset;
|
||||
}
|
||||
|
||||
void freeBlock(block* _block)
|
||||
{
|
||||
uint32_t i;
|
||||
for (i = 0; i < _block->nbInsts; i++)
|
||||
{
|
||||
if (_block->insts[i]->nbValues > 0)
|
||||
free(_block->insts[i]->values);
|
||||
free(_block->insts[i]);
|
||||
}
|
||||
uint32_t i;
|
||||
for (i = 0; i < _block->nbInsts; i++)
|
||||
{
|
||||
if (_block->insts[i]->nbValues > 0)
|
||||
free(_block->insts[i]->values);
|
||||
free(_block->insts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void initRegisters(uc_engine *uc)
|
||||
{
|
||||
// initialize machine registers
|
||||
uint32_t zero = 0;
|
||||
OK(uc_reg_write(uc, UC_X86_REG_EAX, &zero));
|
||||
OK(uc_reg_write(uc, UC_X86_REG_EBX, &zero));
|
||||
OK(uc_reg_write(uc, UC_X86_REG_ECX, &zero));
|
||||
OK(uc_reg_write(uc, UC_X86_REG_EDX, &zero));
|
||||
OK(uc_reg_write(uc, UC_X86_REG_EBP, &zero));
|
||||
OK(uc_reg_write(uc, UC_X86_REG_ESP, &zero));
|
||||
OK(uc_reg_write(uc, UC_X86_REG_EDI, &zero));
|
||||
OK(uc_reg_write(uc, UC_X86_REG_ESI, &zero));
|
||||
OK(uc_reg_write(uc, UC_X86_REG_EFLAGS, &zero));
|
||||
// initialize machine registers
|
||||
uint32_t zero = 0;
|
||||
OK(uc_reg_write(uc, UC_X86_REG_EAX, &zero));
|
||||
OK(uc_reg_write(uc, UC_X86_REG_EBX, &zero));
|
||||
OK(uc_reg_write(uc, UC_X86_REG_ECX, &zero));
|
||||
OK(uc_reg_write(uc, UC_X86_REG_EDX, &zero));
|
||||
OK(uc_reg_write(uc, UC_X86_REG_EBP, &zero));
|
||||
OK(uc_reg_write(uc, UC_X86_REG_ESP, &zero));
|
||||
OK(uc_reg_write(uc, UC_X86_REG_EDI, &zero));
|
||||
OK(uc_reg_write(uc, UC_X86_REG_ESI, &zero));
|
||||
OK(uc_reg_write(uc, UC_X86_REG_EFLAGS, &zero));
|
||||
}
|
||||
|
||||
instruction* getInstruction(block* _block, uint32_t _addr)
|
||||
{
|
||||
uint32_t i;
|
||||
for (i = 0; i < _block->nbInsts; i++)
|
||||
{
|
||||
if (_block->insts[i]->addr == _addr)
|
||||
return _block->insts[i];
|
||||
}
|
||||
return NULL;
|
||||
uint32_t i;
|
||||
for (i = 0; i < _block->nbInsts; i++)
|
||||
{
|
||||
if (_block->insts[i]->addr == _addr)
|
||||
return _block->insts[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* getRegisterName(uint32_t _regid)
|
||||
{
|
||||
switch (_regid)
|
||||
{
|
||||
//8
|
||||
case UC_X86_REG_AH: return "AH";
|
||||
case UC_X86_REG_AL: return "AL";
|
||||
case UC_X86_REG_BH: return "BH";
|
||||
case UC_X86_REG_BL: return "BL";
|
||||
case UC_X86_REG_CL: return "CL";
|
||||
case UC_X86_REG_CH: return "CH";
|
||||
case UC_X86_REG_DH: return "DH";
|
||||
case UC_X86_REG_DL: return "DL";
|
||||
//16
|
||||
case UC_X86_REG_AX: return "AX";
|
||||
case UC_X86_REG_BX: return "BX";
|
||||
case UC_X86_REG_CX: return "CX";
|
||||
case UC_X86_REG_DX: return "DX";
|
||||
//32
|
||||
case UC_X86_REG_EAX: return "EAX";
|
||||
case UC_X86_REG_EBX: return "EBX";
|
||||
case UC_X86_REG_ECX: return "ECX";
|
||||
case UC_X86_REG_EDX: return "EDX";
|
||||
case UC_X86_REG_EDI: return "EDI";
|
||||
case UC_X86_REG_ESI: return "ESI";
|
||||
case UC_X86_REG_EBP: return "EBP";
|
||||
case UC_X86_REG_ESP: return "ESP";
|
||||
case UC_X86_REG_EIP: return "EIP";
|
||||
case UC_X86_REG_EFLAGS: return "EFLAGS";
|
||||
switch (_regid)
|
||||
{
|
||||
//8
|
||||
case UC_X86_REG_AH: return "AH";
|
||||
case UC_X86_REG_AL: return "AL";
|
||||
case UC_X86_REG_BH: return "BH";
|
||||
case UC_X86_REG_BL: return "BL";
|
||||
case UC_X86_REG_CL: return "CL";
|
||||
case UC_X86_REG_CH: return "CH";
|
||||
case UC_X86_REG_DH: return "DH";
|
||||
case UC_X86_REG_DL: return "DL";
|
||||
//16
|
||||
case UC_X86_REG_AX: return "AX";
|
||||
case UC_X86_REG_BX: return "BX";
|
||||
case UC_X86_REG_CX: return "CX";
|
||||
case UC_X86_REG_DX: return "DX";
|
||||
//32
|
||||
case UC_X86_REG_EAX: return "EAX";
|
||||
case UC_X86_REG_EBX: return "EBX";
|
||||
case UC_X86_REG_ECX: return "ECX";
|
||||
case UC_X86_REG_EDX: return "EDX";
|
||||
case UC_X86_REG_EDI: return "EDI";
|
||||
case UC_X86_REG_ESI: return "ESI";
|
||||
case UC_X86_REG_EBP: return "EBP";
|
||||
case UC_X86_REG_ESP: return "ESP";
|
||||
case UC_X86_REG_EIP: return "EIP";
|
||||
case UC_X86_REG_EFLAGS: return "EFLAGS";
|
||||
|
||||
default: fail();
|
||||
}
|
||||
return "UNKNOWN";
|
||||
default: fail();
|
||||
}
|
||||
return "UNKNOWN";
|
||||
}
|
||||
|
||||
uint32_t getRegisterValue(uc_engine *uc, uint32_t _regid)
|
||||
{
|
||||
switch (_regid)
|
||||
{
|
||||
//8
|
||||
case UC_X86_REG_AH: case UC_X86_REG_AL:
|
||||
case UC_X86_REG_BH: case UC_X86_REG_BL:
|
||||
case UC_X86_REG_CL: case UC_X86_REG_CH:
|
||||
case UC_X86_REG_DH: case UC_X86_REG_DL:
|
||||
{
|
||||
uint8_t val = 0;
|
||||
OK(uc_reg_read(uc, _regid, &val));
|
||||
return val;
|
||||
}
|
||||
//16
|
||||
case UC_X86_REG_AX: case UC_X86_REG_BX:
|
||||
case UC_X86_REG_CX: case UC_X86_REG_DX:
|
||||
{
|
||||
uint16_t val = 0;
|
||||
OK(uc_reg_read(uc, _regid, &val));
|
||||
return val;
|
||||
}
|
||||
//32
|
||||
case UC_X86_REG_EAX: case UC_X86_REG_EBX:
|
||||
case UC_X86_REG_ECX: case UC_X86_REG_EDX:
|
||||
case UC_X86_REG_EDI: case UC_X86_REG_ESI:
|
||||
case UC_X86_REG_EBP: case UC_X86_REG_ESP:
|
||||
case UC_X86_REG_EIP: case UC_X86_REG_EFLAGS:
|
||||
{
|
||||
uint32_t val = 0;
|
||||
OK(uc_reg_read(uc, _regid, &val));
|
||||
return val;
|
||||
}
|
||||
switch (_regid)
|
||||
{
|
||||
//8
|
||||
case UC_X86_REG_AH: case UC_X86_REG_AL:
|
||||
case UC_X86_REG_BH: case UC_X86_REG_BL:
|
||||
case UC_X86_REG_CL: case UC_X86_REG_CH:
|
||||
case UC_X86_REG_DH: case UC_X86_REG_DL:
|
||||
{
|
||||
uint8_t val = 0;
|
||||
OK(uc_reg_read(uc, _regid, &val));
|
||||
return val;
|
||||
}
|
||||
//16
|
||||
case UC_X86_REG_AX: case UC_X86_REG_BX:
|
||||
case UC_X86_REG_CX: case UC_X86_REG_DX:
|
||||
{
|
||||
uint16_t val = 0;
|
||||
OK(uc_reg_read(uc, _regid, &val));
|
||||
return val;
|
||||
}
|
||||
//32
|
||||
case UC_X86_REG_EAX: case UC_X86_REG_EBX:
|
||||
case UC_X86_REG_ECX: case UC_X86_REG_EDX:
|
||||
case UC_X86_REG_EDI: case UC_X86_REG_ESI:
|
||||
case UC_X86_REG_EBP: case UC_X86_REG_ESP:
|
||||
case UC_X86_REG_EIP: case UC_X86_REG_EFLAGS:
|
||||
{
|
||||
uint32_t val = 0;
|
||||
OK(uc_reg_read(uc, _regid, &val));
|
||||
return val;
|
||||
}
|
||||
|
||||
default: fail();
|
||||
}
|
||||
return 0;
|
||||
default: fail();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user