Add multiple exits mechanism and tests&samples

This commit is contained in:
lazymio
2021-11-01 14:00:43 +01:00
parent a888835962
commit fb45b287ba
4 changed files with 157 additions and 10 deletions

View File

@ -7,7 +7,17 @@
#include <string.h>
// code to be emulated
#define X86_CODE32 "\x41\x4a" // INC ecx; DEC edx; PXOR xmm0, xmm1
// INC ecx; DEC edx; PXOR xmm0, xmm1
#define X86_CODE32 "\x41\x4a"
// cmp eax, 0;
// jg lb;
// inc eax;
// nop;
// lb:
// inc ebx;
// nop;
#define X86_JUMP_CODE "\x83\xf8\x00\x7f\x02\x40\x90\x43\x90"
// memory address where emulation starts
#define ADDRESS 0x10000
@ -62,9 +72,99 @@ static void test_uc_ctl_read(void)
uc_close(uc);
}
void test_uc_ctl_exits()
{
uc_engine *uc;
uc_err err;
int r_eax, r_ebx;
uint64_t exits[] = {ADDRESS + 6, ADDRESS + 8};
printf("Using multiple exits by uc_ctl.\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;
}
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 our code to the memory.
err = uc_mem_write(uc, ADDRESS, X86_JUMP_CODE, sizeof(X86_JUMP_CODE) - 1);
if (err) {
printf("Failed on uc_mem_write() with error returned: %u\n", err);
return;
}
// Enable multiple exits.
err = uc_ctl_exits_enabled(uc, true);
if (err) {
printf("Failed on uc_ctl() with error returned: %u\n", err);
return;
}
err = uc_ctl_set_exists(uc, exits, 2);
if (err) {
printf("Failed on uc_ctl() with error returned: %u\n", err);
return;
}
// This should stop at ADDRESS + 6 and increase eax, even thouhg we don't
// provide an exit.
err = uc_emu_start(uc, ADDRESS, 0, 0, 0);
if (err) {
printf("Failed on uc_emu_start() with error returned: %u\n", err);
return;
}
err = uc_reg_read(uc, UC_X86_REG_EAX, &r_eax);
if (err) {
printf("Failed on uc_reg_read() with error returned: %u\n", err);
return;
}
err = uc_reg_read(uc, UC_X86_REG_EBX, &r_ebx);
if (err) {
printf("Failed on uc_reg_read() with error returned: %u\n", err);
return;
}
printf(">>> eax = %" PRId32 " and ebx = %" PRId32
" after the first emulation\n",
r_eax, r_ebx);
// This should stop at ADDRESS + 8, even thouhg we don't provide an exit.
err = uc_emu_start(uc, ADDRESS, 0, 0, 0);
if (err) {
printf("Failed on uc_emu_start() with error returned: %u\n", err);
return;
}
err = uc_reg_read(uc, UC_X86_REG_EAX, &r_eax);
if (err) {
printf("Failed on uc_reg_read() with error returned: %u\n", err);
return;
}
err = uc_reg_read(uc, UC_X86_REG_EBX, &r_ebx);
if (err) {
printf("Failed on uc_reg_read() with error returned: %u\n", err);
return;
}
printf(">>> eax = %" PRId32 " and ebx = %" PRId32
" after the second emulation\n",
r_eax, r_ebx);
uc_close(uc);
}
int main(int argc, char **argv, char **envp)
{
test_uc_ctl_read();
printf("====================\n");
test_uc_ctl_exits();
return 0;
}