2015-08-21 10:04:50 +03:00
|
|
|
/* Unicorn Emulator Engine */
|
|
|
|
/* By Nguyen Anh Quynh, 2015 */
|
|
|
|
|
|
|
|
/* Sample code to demonstrate how to emulate Sparc code */
|
|
|
|
|
2015-12-08 10:21:32 +03:00
|
|
|
#include <unicorn/unicorn.h>
|
2017-01-22 17:05:08 +03:00
|
|
|
#include <string.h>
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
// code to be emulated
|
|
|
|
#define SPARC_CODE "\x86\x00\x40\x02" // add %g1, %g2, %g3;
|
2023-05-11 22:42:20 +03:00
|
|
|
// #define SPARC_CODE "\xbb\x70\x00\x00" // illegal code
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
// memory address where emulation starts
|
|
|
|
#define ADDRESS 0x10000
|
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
static void hook_block(uc_engine *uc, uint64_t address, uint32_t size,
|
|
|
|
void *user_data)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
2021-10-29 13:44:49 +03:00
|
|
|
printf(">>> Tracing basic block at 0x%" PRIx64 ", block size = 0x%x\n",
|
|
|
|
address, size);
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
static void hook_code(uc_engine *uc, uint64_t address, uint32_t size,
|
|
|
|
void *user_data)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
2021-10-29 13:44:49 +03:00
|
|
|
printf(">>> Tracing instruction at 0x%" PRIx64
|
|
|
|
", instruction size = 0x%x\n",
|
|
|
|
address, size);
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_sparc(void)
|
|
|
|
{
|
2015-09-05 06:20:32 +03:00
|
|
|
uc_engine *uc;
|
2015-08-21 10:04:50 +03:00
|
|
|
uc_err err;
|
2015-09-05 06:20:32 +03:00
|
|
|
uc_hook trace1, trace2;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
int g1 = 0x1230; // G1 register
|
|
|
|
int g2 = 0x6789; // G2 register
|
|
|
|
int g3 = 0x5555; // G3 register
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
printf("Emulate SPARC code\n");
|
|
|
|
|
|
|
|
// Initialize emulator in Sparc mode
|
2021-10-29 13:44:49 +03:00
|
|
|
err = uc_open(UC_ARCH_SPARC, UC_MODE_SPARC32 | UC_MODE_BIG_ENDIAN, &uc);
|
2015-08-21 10:04:50 +03:00
|
|
|
if (err) {
|
2021-10-29 13:44:49 +03:00
|
|
|
printf("Failed on uc_open() with error returned: %u (%s)\n", err,
|
|
|
|
uc_strerror(err));
|
2015-08-21 10:04:50 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// map 2MB memory for this emulation
|
2015-08-30 07:02:33 +03:00
|
|
|
uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
// write machine code to be emulated to memory
|
2015-09-07 20:09:05 +03:00
|
|
|
uc_mem_write(uc, ADDRESS, SPARC_CODE, sizeof(SPARC_CODE) - 1);
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
// initialize machine registers
|
2015-08-26 15:31:53 +03:00
|
|
|
uc_reg_write(uc, UC_SPARC_REG_G1, &g1);
|
|
|
|
uc_reg_write(uc, UC_SPARC_REG_G2, &g2);
|
|
|
|
uc_reg_write(uc, UC_SPARC_REG_G3, &g3);
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
// tracing all basic blocks with customized callback
|
2016-02-11 03:02:13 +03:00
|
|
|
uc_hook_add(uc, &trace1, UC_HOOK_BLOCK, hook_block, NULL, 1, 0);
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2015-09-26 11:49:00 +03:00
|
|
|
// tracing all instructions with customized callback
|
2016-02-11 03:02:13 +03:00
|
|
|
uc_hook_add(uc, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0);
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
// emulate machine code in infinite time (last param = 0), or when
|
|
|
|
// finishing all the code.
|
2015-09-04 05:01:52 +03:00
|
|
|
err = uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(SPARC_CODE) - 1, 0, 0);
|
2015-08-21 10:04:50 +03:00
|
|
|
if (err) {
|
2021-10-29 13:44:49 +03:00
|
|
|
printf("Failed on uc_emu_start() with error returned: %u (%s)\n", err,
|
|
|
|
uc_strerror(err));
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// now print out some registers
|
|
|
|
printf(">>> Emulation done. Below is the CPU context\n");
|
|
|
|
|
2015-08-26 15:31:53 +03:00
|
|
|
uc_reg_read(uc, UC_SPARC_REG_G3, &g3);
|
2015-08-21 10:04:50 +03:00
|
|
|
printf(">>> G3 = 0x%x\n", g3);
|
|
|
|
|
2015-08-26 15:31:53 +03:00
|
|
|
uc_close(uc);
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv, char **envp)
|
|
|
|
{
|
2017-01-21 04:28:22 +03:00
|
|
|
test_sparc();
|
2015-12-08 10:21:32 +03:00
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
return 0;
|
|
|
|
}
|