This commit is contained in:
lazymio 2024-02-12 00:10:58 +08:00
parent 822bb527f3
commit f3323469d0
No known key found for this signature in database
GPG Key ID: DFF27E34A47CB873
3 changed files with 57 additions and 51 deletions

View File

@ -293,14 +293,13 @@ static void test_arm64_hook_mrs(void)
uc_close(uc);
}
#define CHECK(x) do { \
if((x) != UC_ERR_OK) { \
#define CHECK(x) \
do { \
if ((x) != UC_ERR_OK) { \
fprintf(stderr, "FAIL at %s:%d: %s\n", __FILE__, __LINE__, #x); \
exit(1); \
} \
} while(0)
} while (0)
/* Test PAC support in the emulator. Code adapted from
https://github.com/unicorn-engine/unicorn/issues/1789#issuecomment-1536320351 */
@ -309,8 +308,8 @@ static void test_arm64_pac(void)
uc_engine *uc;
uint64_t x1 = 0x0000aaaabbbbccccULL;
// paciza x1
#define ARM64_PAC_CODE "\xe1\x23\xc1\xda"
// paciza x1
#define ARM64_PAC_CODE "\xe1\x23\xc1\xda"
printf("Try ARM64 PAC\n");
@ -318,7 +317,8 @@ static void test_arm64_pac(void)
CHECK(uc_open(UC_ARCH_ARM64, UC_MODE_ARM, &uc));
CHECK(uc_ctl_set_cpu_model(uc, UC_CPU_ARM64_MAX));
CHECK(uc_mem_map(uc, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL));
CHECK(uc_mem_write(uc, ADDRESS, ARM64_PAC_CODE, sizeof(ARM64_PAC_CODE) - 1));
CHECK(
uc_mem_write(uc, ADDRESS, ARM64_PAC_CODE, sizeof(ARM64_PAC_CODE) - 1));
CHECK(uc_reg_write(uc, UC_ARM64_REG_X1, &x1));
/** Initialize PAC support **/
@ -334,7 +334,7 @@ static void test_arm64_pac(void)
CHECK(uc_reg_read(uc, UC_ARM64_REG_CP_REG, &reg));
// NS && RW && API
reg.val |= (1 | (1<<10) | (1<<17));
reg.val |= (1 | (1 << 10) | (1 << 17));
CHECK(uc_reg_write(uc, UC_ARM64_REG_CP_REG, &reg));
@ -348,7 +348,7 @@ static void test_arm64_pac(void)
CHECK(uc_reg_read(uc, UC_ARM64_REG_CP_REG, &reg));
// EnIA && EnIB
reg.val |= (1<<31) | (1<<30);
reg.val |= (1 << 31) | (1 << 30);
CHECK(uc_reg_write(uc, UC_ARM64_REG_CP_REG, &reg));
@ -360,12 +360,13 @@ static void test_arm64_pac(void)
reg.op2 = 0b000;
// HCR.API
reg.val |= (1ULL<<41);
reg.val |= (1ULL << 41);
CHECK(uc_reg_write(uc, UC_ARM64_REG_CP_REG, &reg));
/** Check that PAC worked **/
CHECK(uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(ARM64_PAC_CODE) - 1, 0, 0));
CHECK(
uc_emu_start(uc, ADDRESS, ADDRESS + sizeof(ARM64_PAC_CODE) - 1, 0, 0));
CHECK(uc_reg_read(uc, UC_ARM64_REG_X1, &x1));
printf("X1 = 0x%" PRIx64 "\n", x1);

View File

@ -333,7 +333,8 @@ static void test_snapshot_with_vtlb(void)
uc_hook hook;
// mov eax, [0x2020]; inc eax; mov [0x2020], eax
char code[] = "\xA1\x20\x20\x00\x00\x04\x00\x00\x00\xFF\xC0\xA3\x20\x20\x00\x00\x04\x00\x00\x00";
char code[] = "\xA1\x20\x20\x00\x00\x04\x00\x00\x00\xFF\xC0\xA3\x20\x20\x00"
"\x00\x04\x00\x00\x00";
OK(uc_open(UC_ARCH_X86, UC_MODE_64, &uc));
@ -342,9 +343,9 @@ static void test_snapshot_with_vtlb(void)
OK(uc_context_alloc(uc, &c1));
OK(uc_ctl_context_mode(uc, UC_CTL_CONTEXT_MEMORY));
OK(uc_ctl_tlb_mode(uc, UC_TLB_VIRTUAL));
OK(uc_hook_add(uc, &hook, UC_HOOK_TLB_FILL, test_snapshot_with_vtlb_callback, NULL, 1, 0));
OK(uc_hook_add(uc, &hook, UC_HOOK_TLB_FILL,
test_snapshot_with_vtlb_callback, NULL, 1, 0));
// Map physical memory
OK(uc_mem_map(uc, 0x1000, 0x1000, UC_PROT_EXEC | UC_PROT_READ));
@ -354,11 +355,13 @@ static void test_snapshot_with_vtlb(void)
// Initial context save
OK(uc_context_save(uc, c0));
OK(uc_emu_start(uc, 0x400000000 + 0x1000, 0x400000000 + 0x1000 + sizeof(code) - 1, 0, 0));
OK(uc_emu_start(uc, 0x400000000 + 0x1000,
0x400000000 + 0x1000 + sizeof(code) - 1, 0, 0));
OK(uc_mem_read(uc, 0x2020, &mem, sizeof(mem)));
TEST_CHECK(mem == 1);
OK(uc_context_save(uc, c1));
OK(uc_emu_start(uc, 0x400000000 + 0x1000, 0x400000000 + 0x1000 + sizeof(code) - 1, 0, 0));
OK(uc_emu_start(uc, 0x400000000 + 0x1000,
0x400000000 + 0x1000 + sizeof(code) - 1, 0, 0));
OK(uc_mem_read(uc, 0x2020, &mem, sizeof(mem)));
TEST_CHECK(mem == 2);
OK(uc_context_restore(uc, c1));

View File

@ -1514,11 +1514,12 @@ static void test_x86_64_not_overwriting_tmp0_for_pc_update()
}
#define MEM_BASE 0x40000000
#define MEM_SIZE 1024*1024
#define MEM_SIZE 1024 * 1024
#define MEM_STACK MEM_BASE + (MEM_SIZE / 2)
#define MEM_TEXT MEM_STACK + 4096
static void test_fxsave_fpip_x86(void) {
static void test_fxsave_fpip_x86(void)
{
// note: fxsave was introduced in Pentium II
uint8_t code_x86[] = {
// help testing through NOP offset [disassembly in at&t syntax]
@ -1536,7 +1537,7 @@ static void test_fxsave_fpip_x86(void) {
};
uc_err err;
uint32_t X86_NOP_OFFSET = 4;
uint32_t stack_top = (uint32_t) MEM_STACK;
uint32_t stack_top = (uint32_t)MEM_STACK;
uint32_t value;
uc_engine *uc;
@ -1549,12 +1550,13 @@ static void test_fxsave_fpip_x86(void) {
OK(uc_reg_write(uc, UC_X86_REG_ESP, &stack_top));
OK(uc_emu_start(uc, MEM_TEXT, MEM_TEXT + sizeof(code_x86), 0, 0));
OK(uc_reg_read(uc, UC_X86_REG_EAX, &value));
TEST_CHECK(value == ((uint32_t) MEM_TEXT + X86_NOP_OFFSET));
TEST_CHECK(value == ((uint32_t)MEM_TEXT + X86_NOP_OFFSET));
OK(uc_mem_unmap(uc, MEM_BASE, MEM_SIZE));
OK(uc_close(uc));
}
static void test_fxsave_fpip_x64(void) {
static void test_fxsave_fpip_x64(void)
{
uint8_t code_x64[] = {
// help testing through NOP offset [disassembly in at&t]
0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
@ -1571,7 +1573,7 @@ static void test_fxsave_fpip_x64(void) {
};
uc_err err;
uint64_t stack_top = (uint64_t) MEM_STACK;
uint64_t stack_top = (uint64_t)MEM_STACK;
uint64_t X64_NOP_OFFSET = 8;
uint64_t value;
uc_engine *uc;
@ -1585,7 +1587,7 @@ static void test_fxsave_fpip_x64(void) {
OK(uc_reg_write(uc, UC_X86_REG_RSP, &stack_top));
OK(uc_emu_start(uc, MEM_TEXT, MEM_TEXT + sizeof(code_x64), 0, 0));
OK(uc_reg_read(uc, UC_X86_REG_RAX, &value));
TEST_CHECK(value == ((uint64_t) MEM_TEXT + X64_NOP_OFFSET));
TEST_CHECK(value == ((uint64_t)MEM_TEXT + X64_NOP_OFFSET));
OK(uc_mem_unmap(uc, MEM_BASE, MEM_SIZE));
OK(uc_close(uc));
}