/** * Unicorn x86_32 self-modifying unit test * * This test demonstrates the flushing of instruction translation cache * after a self-modification of Intel's x8's "IMUL Gv,Ev,Ib" instruction. */ #include "unicorn_test.h" #include #include #include #include #define RIP_NEXT_TO_THE_SELFMODIFY_OPCODE (1) // Demostration of a self-modifying "IMUL eax,mem,Ib" opcode // And the QEMU's ability to flush the translation buffer properly #define MIN(a, b) (a < b? a: b) #define CODE_SPACE (2 * 1024 * 1024) #define PHY_STACK_REGION (0x60000000) #define X86_CODE32_ALPHA_MIXED \ "\x89\xe1\xd9\xcd\xd9\x71\xf4\x5d\x55\x59\x49\x49\x49\x49\x49\x49" \ "\x49\x49\x49\x49\x43\x43\x43\x43\x43\x43\x37\x51\x5a\x6a\x41\x58" \ "\x50\x30\x41\x30\x41\x6b\x41\x41\x51\x32\x41\x42\x32\x42\x42\x30" \ "\x42\x42\x41\x42\x58\x50\x38\x41\x42\x75\x4a\x49\x51\x51\x51\x52" \ "\x47\x33\x47\x34\x51\x55\x51\x56\x50\x47\x47\x38\x47\x39\x50\x4a" \ "\x50\x4b\x50\x4c\x50\x4d\x50\x4e\x50\x4f\x50\x50\x50\x31\x47\x42" \ "\x47\x42\x50\x34\x50\x5a\x50\x45\x51\x52\x46\x32\x47\x31\x50\x4d" \ "\x51\x51\x50\x4e\x41\x41" /* Called before every test to set up a new instance */ static int setup(void **state) { uc_engine *uc; uc_assert_success(uc_open(UC_ARCH_X86, UC_MODE_64, &uc)); *state = uc; return 0; } /* Called after every test to clean up */ static int teardown(void **state) { uc_engine *uc = *state; uc_assert_success(uc_close(uc)); *state = NULL; return 0; } static void dump_stack_mem(uc_engine *uc) { uint8_t tmp[256]; uint32_t size; size = sizeof(X86_CODE32_ALPHA_MIXED); if (size > 255) size = 255; if (!uc_mem_read(uc, PHY_STACK_REGION, tmp, size)) { uint32_t i; printf("Stack region dump"); for (i=0; i