Update Java samples to match C samples.

Also add all of the samples as Java tests, referencing the output of the C
samples.
This commit is contained in:
Robert Xiao
2023-05-14 16:23:49 -07:00
parent 3739c7e3e0
commit 4f563490e2
25 changed files with 3884 additions and 906 deletions

View File

@@ -293,9 +293,94 @@ static void test_arm64_hook_mrs()
uc_close(uc);
}
#define CHECK(x) do { \
if((x) != UC_ERR_OK) { \
fprintf(stderr, "FAIL at %s:%d: %s\n", __FILE__, __LINE__, #x); \
exit(1); \
} \
} while(0)
static void test_arm64_pac(void)
{
uc_engine *uc;
uint64_t x1 = 0x0000aaaabbbbccccULL;
// paciza x1
#define ARM64_PAC_CODE "\xe1\x23\xc1\xda"
printf("Try ARM64 PAC\n");
// Initialize emulator in ARM mode
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_reg_write(uc, UC_ARM64_REG_X1, &x1));
/** Initialize PAC support **/
uc_arm64_cp_reg reg;
// SCR_EL3
reg.op0 = 0b11;
reg.op1 = 0b110;
reg.crn = 0b0001;
reg.crm = 0b0001;
reg.op2 = 0b000;
CHECK(uc_reg_read(uc, UC_ARM64_REG_CP_REG, &reg));
// NS && RW && API
reg.val |= (1 | (1<<10) | (1<<17));
CHECK(uc_reg_write(uc, UC_ARM64_REG_CP_REG, &reg));
// SCTLR_EL1
reg.op0 = 0b11;
reg.op1 = 0b000;
reg.crn = 0b0001;
reg.crm = 0b0000;
reg.op2 = 0b000;
CHECK(uc_reg_read(uc, UC_ARM64_REG_CP_REG, &reg));
// EnIA && EnIB
reg.val |= (1<<31) | (1<<30);
CHECK(uc_reg_write(uc, UC_ARM64_REG_CP_REG, &reg));
// HCR_EL2
reg.op0 = 0b11;
reg.op1 = 0b100;
reg.crn = 0b0001;
reg.crm = 0b0001;
reg.op2 = 0b000;
// HCR.API
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_reg_read(uc, UC_ARM64_REG_X1, &x1));
printf("X1 = 0x%" PRIx64 "\n", x1);
if (x1 == 0x0000aaaabbbbccccULL) {
printf("FAIL: No PAC tag added!\n");
} else {
// Expect 0x1401aaaabbbbccccULL with the default key
printf("SUCCESS: PAC tag found.\n");
}
uc_close(uc);
}
int main(int argc, char **argv, char **envp)
{
test_arm64_mem_fetch();
printf("-------------------------\n");
test_arm64();
printf("-------------------------\n");
@@ -307,5 +392,8 @@ int main(int argc, char **argv, char **envp)
printf("-------------------------\n");
test_arm64_hook_mrs();
printf("-------------------------\n");
test_arm64_pac();
return 0;
}

View File

@@ -9,7 +9,7 @@
#include <string.h>
// code to be emulated
#define CODE "\x82\x11\xbb\x00\x00\x08" // mov d0, #0x1; mov.u d0, #0x8000
#define CODE "\x82\x11\xbb\x00\x00\x08" // mov d1, #0x1; mov.u d0, #0x8000
// memory address where emulation starts
#define ADDRESS 0x10000
@@ -36,6 +36,7 @@ static void test_tricore(void)
uc_hook trace1, trace2;
uint32_t d0 = 0x0; // d0 register
uint32_t d1 = 0x0; // d1 register
printf("Emulate TriCore code\n");
@@ -73,6 +74,9 @@ static void test_tricore(void)
uc_reg_read(uc, UC_TRICORE_REG_D0, &d0);
printf(">>> d0 = 0x%x\n", d0);
uc_reg_read(uc, UC_TRICORE_REG_D1, &d1);
printf(">>> d1 = 0x%x\n", d1);
uc_close(uc);
}

View File

@@ -360,7 +360,6 @@ static void test_i386_map_ptr(void)
int r_ecx = 0x1234; // ECX register
int r_edx = 0x7890; // EDX register
printf("===================================\n");
printf("Emulate i386 code - use uc_mem_map_ptr()\n");
// Initialize emulator in X86-32bit mode
@@ -426,7 +425,6 @@ static void test_i386_jump(void)
uc_err err;
uc_hook trace1, trace2;
printf("===================================\n");
printf("Emulate i386 code with jump\n");
// Initialize emulator in X86-32bit mode
@@ -474,7 +472,6 @@ static void test_i386_loop(void)
int r_ecx = 0x1234; // ECX register
int r_edx = 0x7890; // EDX register
printf("===================================\n");
printf("Emulate i386 code that loop forever\n");
// Initialize emulator in X86-32bit mode
@@ -528,7 +525,6 @@ static void test_i386_invalid_mem_read(void)
int r_ecx = 0x1234; // ECX register
int r_edx = 0x7890; // EDX register
printf("===================================\n");
printf("Emulate i386 code that read from invalid memory\n");
// Initialize emulator in X86-32bit mode
@@ -588,7 +584,6 @@ static void test_i386_invalid_mem_write(void)
int r_ecx = 0x1234; // ECX register
int r_edx = 0x7890; // EDX register
printf("===================================\n");
printf("Emulate i386 code that write to invalid memory\n");
// Initialize emulator in X86-32bit mode
@@ -663,7 +658,6 @@ static void test_i386_jump_invalid(void)
int r_ecx = 0x1234; // ECX register
int r_edx = 0x7890; // EDX register
printf("===================================\n");
printf("Emulate i386 code that jumps to invalid memory\n");
// Initialize emulator in X86-32bit mode
@@ -721,7 +715,6 @@ static void test_i386_inout(void)
int r_eax = 0x1234; // EAX register
int r_ecx = 0x6789; // ECX register
printf("===================================\n");
printf("Emulate i386 code with IN/OUT instructions\n");
// Initialize emulator in X86-32bit mode
@@ -785,7 +778,6 @@ static void test_i386_context_save(void)
int r_eax = 0x1; // EAX register
printf("===================================\n");
printf("Save/restore CPU context in opaque blob\n");
// initialize emulator in X86-32bit mode
@@ -908,7 +900,6 @@ static void test_i386_invalid_c6c7(void)
};
int i, j, k;
printf("===================================\n");
printf("Emulate i386 C6/C7 opcodes\n");
// Initialize emulator in X86-32bit mode
@@ -1077,7 +1068,6 @@ static void test_x86_64_syscall(void)
int64_t rax = 0x100;
printf("===================================\n");
printf("Emulate x86_64 code with 'syscall' instruction\n");
// Initialize emulator in X86-64bit mode
@@ -1186,7 +1176,6 @@ static void test_i386_invalid_mem_read_in_tb(void)
int r_edx = 0x7890; // EDX register
int r_eip = 0;
printf("===================================\n");
printf(
"Emulate i386 code that read invalid memory in the middle of a TB\n");
@@ -1249,7 +1238,6 @@ static void test_i386_smc_xor()
uint32_t r_eax = 0xbc4177e6; // EDX register
uint32_t result;
printf("===================================\n");
printf("Emulate i386 code that modfies itself\n");
// Initialize emulator in X86-32bit mode
@@ -1325,7 +1313,6 @@ static void test_i386_mmio()
int r_ecx = 0xdeadbeef;
uc_err err;
printf("===================================\n");
printf("Emulate i386 code that uses MMIO\n");
// Initialize emulator in X86-32bit mode
@@ -1403,7 +1390,6 @@ static void test_i386_hook_mem_invalid()
"\xb8\xef\xbe\xad\xde\xa3\x00\x80\x00\x00\xa1\x00\x00\x01\x00";
uc_err err;
printf("===================================\n");
printf("Emulate i386 code that triggers invalid memory read/write.\n");
err = uc_open(UC_ARCH_X86, UC_MODE_32, &uc);
@@ -1448,40 +1434,66 @@ int main(int argc, char **argv, char **envp)
test_x86_16();
} else if (!strcmp(argv[1], "-32")) {
test_miss_code();
printf("===================================\n");
test_i386();
printf("===================================\n");
test_i386_map_ptr();
printf("===================================\n");
test_i386_inout();
printf("===================================\n");
test_i386_context_save();
printf("===================================\n");
test_i386_jump();
printf("===================================\n");
test_i386_loop();
printf("===================================\n");
test_i386_invalid_mem_read();
printf("===================================\n");
test_i386_invalid_mem_write();
printf("===================================\n");
test_i386_jump_invalid();
// test_i386_invalid_c6c7();
} else if (!strcmp(argv[1], "-64")) {
test_x86_64();
printf("===================================\n");
test_x86_64_syscall();
} else if (!strcmp(argv[1], "-h")) {
printf("Syntax: %s <-16|-32|-64>\n", argv[0]);
}
} else {
test_x86_16();
printf("===================================\n");
test_miss_code();
printf("===================================\n");
test_i386();
printf("===================================\n");
test_i386_map_ptr();
printf("===================================\n");
test_i386_inout();
printf("===================================\n");
test_i386_context_save();
printf("===================================\n");
test_i386_jump();
printf("===================================\n");
test_i386_loop();
printf("===================================\n");
test_i386_invalid_mem_read();
printf("===================================\n");
test_i386_invalid_mem_write();
printf("===================================\n");
test_i386_jump_invalid();
// test_i386_invalid_c6c7();
printf("===================================\n");
test_x86_64();
printf("===================================\n");
test_x86_64_syscall();
printf("===================================\n");
test_i386_invalid_mem_read_in_tb();
printf("===================================\n");
test_i386_smc_xor();
printf("===================================\n");
test_i386_mmio();
printf("===================================\n");
test_i386_hook_mem_invalid();
}