Skip to content

Commit dd2cb00

Browse files
authored
Merge pull request #2 from PalumboN/fix-tests-m1
Fix tests m1
2 parents addf437 + 06fd869 commit dd2cb00

File tree

7 files changed

+283
-3
lines changed

7 files changed

+283
-3
lines changed

qemu/accel/tcg/cputlb.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,9 @@ load_helper(CPUArchState *env, target_ulong addr, TCGMemOpIdx oi,
14191419
uintptr_t retaddr, MemOp op, bool code_read,
14201420
FullLoadHelper *full_load)
14211421
{
1422+
// Hack for double load on error
1423+
if (env->uc->cpu->exit_request) return 0;
1424+
14221425
uintptr_t mmu_idx = get_mmuidx(oi);
14231426
uintptr_t index = tlb_index(env, mmu_idx, addr);
14241427
CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
@@ -1449,7 +1452,10 @@ load_helper(CPUArchState *env, target_ulong addr, TCGMemOpIdx oi,
14491452
continue;
14501453
if (!HOOK_BOUND_CHECK(hook, addr))
14511454
continue;
1452-
if ((handled = ((uc_cb_eventmem_t)hook->callback)(uc, UC_MEM_FETCH_UNMAPPED, addr, size - uc->size_recur_mem, 0, hook->user_data)))
1455+
tb_exec_lock(uc->tcg_ctx);
1456+
handled = ((uc_cb_eventmem_t)hook->callback)(uc, UC_MEM_FETCH_UNMAPPED, addr, size - uc->size_recur_mem, 0, hook->user_data);
1457+
tb_exec_unlock(uc->tcg_ctx);
1458+
if ((handled))
14531459
break;
14541460

14551461
// the last callback may already asked to stop emulation

qemu/accel/tcg/translate-all.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,7 @@ void tcg_exec_init(struct uc_struct *uc, unsigned long tb_size)
10901090
code_gen_alloc(uc, tb_size);
10911091
tb_exec_unlock(uc->tcg_ctx);
10921092
tcg_prologue_init(uc->tcg_ctx);
1093+
tb_exec_lock(uc->tcg_ctx);
10931094
/* cpu_interrupt_handler is not used in uc1 */
10941095
uc->l1_map = g_malloc0(sizeof(void *) * V_L1_MAX_SIZE);
10951096
/* Invalidate / Cache TBs */

qemu/exec.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,8 +1087,8 @@ RAMBlock *qemu_ram_alloc_from_ptr(struct uc_struct *uc, ram_addr_t size, void *h
10871087
RAMBlock *new_block;
10881088
ram_addr_t max_size = size;
10891089

1090-
size = HOST_PAGE_ALIGN(uc, size);
1091-
max_size = HOST_PAGE_ALIGN(uc, max_size);
1090+
// size = HOST_PAGE_ALIGN(uc, size);
1091+
// max_size = HOST_PAGE_ALIGN(uc, max_size);
10921092
new_block = g_malloc0(sizeof(*new_block));
10931093
if (new_block == NULL)
10941094
return NULL;

qemu/softmmu/cpus.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ void resume_all_vcpus(struct uc_struct* uc)
220220
uc_exit_invalidate_iter((gpointer)&uc->exits[uc->nested_level - 1], NULL, (gpointer)uc);
221221
}
222222

223+
// Why?
224+
tb_exec_lock(uc->tcg_ctx);
225+
223226
cpu->created = false;
224227
}
225228

tests/unit/test_arm64.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,102 @@ static void test_arm64_mrs_hook(void)
195195
OK(uc_close(uc));
196196
}
197197

198+
199+
static bool test_arm64_correct_address_in_small_jump_hook_callback(uc_engine *uc, int type, uint64_t address, int size, int64_t value, void *user_data)
200+
{
201+
// Check registers
202+
uint64_t r_x0 = 0x0;
203+
uint64_t r_pc = 0x0;
204+
OK(uc_reg_read(uc, UC_ARM64_REG_X0, &r_x0));
205+
OK(uc_reg_read(uc, UC_ARM64_REG_PC, &r_pc));
206+
TEST_CHECK(r_x0 == 0x7F00);
207+
TEST_CHECK(r_pc == 0x7F00);
208+
209+
// Check address
210+
// printf("%lx\n", address);
211+
TEST_CHECK(address == 0x7F00);
212+
213+
return false;
214+
}
215+
216+
static void test_arm64_correct_address_in_small_jump_hook(void)
217+
{
218+
uc_engine *uc;
219+
// mov x0, 0x7F00;
220+
// br x0
221+
char code[] = "\x00\xe0\x8f\xd2\x00\x00\x1f\xd6";
222+
223+
uint64_t r_x0 = 0x0;
224+
uint64_t r_pc = 0x0;
225+
uc_hook hook;
226+
227+
uc_common_setup(&uc, UC_ARCH_ARM64, UC_MODE_ARM, code, sizeof(code) - 1, UC_CPU_ARM64_A72);
228+
OK(uc_hook_add(uc, &hook, UC_HOOK_MEM_UNMAPPED, test_arm64_correct_address_in_small_jump_hook_callback, NULL, 1, 0));
229+
230+
uc_assert_err(
231+
UC_ERR_FETCH_UNMAPPED,
232+
uc_emu_start(uc, code_start, code_start + sizeof(code) - 1, 0, 0));
233+
234+
OK(uc_reg_read(uc, UC_ARM64_REG_X0, &r_x0));
235+
OK(uc_reg_read(uc, UC_ARM64_REG_PC, &r_pc));
236+
TEST_CHECK(r_x0 == 0x7F00);
237+
TEST_CHECK(r_pc == 0x7F00);
238+
239+
OK(uc_close(uc));
240+
}
241+
242+
static bool test_arm64_correct_address_in_long_jump_hook_callback(uc_engine *uc, int type, uint64_t address, int size, int64_t value, void *user_data)
243+
{
244+
// Check registers
245+
uint64_t r_x0 = 0x0;
246+
uint64_t r_pc = 0x0;
247+
OK(uc_reg_read(uc, UC_ARM64_REG_X0, &r_x0));
248+
OK(uc_reg_read(uc, UC_ARM64_REG_PC, &r_pc));
249+
TEST_CHECK(r_x0 == 0x7FFFFFFFFFFFFF00);
250+
TEST_CHECK(r_pc == 0x7FFFFFFFFFFFFF00);
251+
252+
// Check address
253+
// printf("%lx\n", address);
254+
TEST_CHECK(address == 0x7FFFFFFFFFFFFF00);
255+
256+
return false;
257+
}
258+
259+
static void test_arm64_correct_address_in_long_jump_hook(void)
260+
{
261+
uc_engine *uc;
262+
// mov x0, 0x7FFFFFFFFFFFFF00;
263+
// br x0
264+
char code[] = "\xe0\xdb\x78\xb2\x00\x00\x1f\xd6";
265+
266+
uint64_t r_x0 = 0x0;
267+
uint64_t r_pc = 0x0;
268+
uc_hook hook;
269+
270+
uc_common_setup(&uc, UC_ARCH_ARM64, UC_MODE_ARM, code, sizeof(code) - 1, UC_CPU_ARM64_A72);
271+
OK(uc_hook_add(uc, &hook, UC_HOOK_MEM_UNMAPPED, test_arm64_correct_address_in_long_jump_hook_callback, NULL, 1, 0));
272+
273+
uc_assert_err(
274+
UC_ERR_FETCH_UNMAPPED,
275+
uc_emu_start(uc, code_start, code_start + sizeof(code) - 1, 0, 0));
276+
277+
OK(uc_reg_read(uc, UC_ARM64_REG_X0, &r_x0));
278+
OK(uc_reg_read(uc, UC_ARM64_REG_PC, &r_pc));
279+
TEST_CHECK(r_x0 == 0x7FFFFFFFFFFFFF00);
280+
TEST_CHECK(r_pc == 0x7FFFFFFFFFFFFF00);
281+
282+
OK(uc_close(uc));
283+
}
284+
285+
286+
287+
198288
TEST_LIST = {{"test_arm64_until", test_arm64_until},
199289
{"test_arm64_code_patching", test_arm64_code_patching},
200290
{"test_arm64_code_patching_count", test_arm64_code_patching_count},
201291
{"test_arm64_v8_pac", test_arm64_v8_pac},
202292
{"test_arm64_read_sctlr", test_arm64_read_sctlr},
203293
{"test_arm64_mrs_hook", test_arm64_mrs_hook},
294+
{"test_arm64_correct_address_in_small_jump_hook", test_arm64_correct_address_in_small_jump_hook},
295+
{"test_arm64_correct_address_in_long_jump_hook", test_arm64_correct_address_in_long_jump_hook},
204296
{NULL, NULL}};

tests/unit/test_riscv.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,93 @@ static void test_riscv64_mmio_map(void)
537537
OK(uc_close(uc));
538538
}
539539

540+
541+
static bool test_riscv_correct_address_in_small_jump_hook_callback(uc_engine *uc, int type, uint64_t address, int size, int64_t value, void *user_data)
542+
{
543+
// Check registers
544+
uint64_t r_x5 = 0x0;
545+
uint64_t r_pc = 0x0;
546+
OK(uc_reg_read(uc, UC_RISCV_REG_X5, &r_x5));
547+
OK(uc_reg_read(uc, UC_RISCV_REG_PC, &r_pc));
548+
TEST_CHECK(r_x5 == 0x7F00);
549+
TEST_CHECK(r_pc == 0x7F00);
550+
551+
// Check address
552+
// printf("%lx\n", address);
553+
TEST_CHECK(address == 0x7F00);
554+
return false;
555+
}
556+
557+
static void test_riscv_correct_address_in_small_jump_hook(void)
558+
{
559+
uc_engine *uc;
560+
// li 0x7F00, x5 > lui t0, 8; addiw t0, t0, -256;
561+
// jr x5
562+
char code[] = "\xb7\x82\x00\x00\x9b\x82\x02\xf0\x67\x80\x02\x00";
563+
564+
uint64_t r_x5 = 0x0;
565+
uint64_t r_pc = 0x0;
566+
uc_hook hook;
567+
568+
uc_common_setup(&uc, UC_ARCH_RISCV, UC_MODE_RISCV64, code, sizeof(code) - 1);
569+
OK(uc_hook_add(uc, &hook, UC_HOOK_MEM_UNMAPPED, test_riscv_correct_address_in_small_jump_hook_callback, NULL, 1, 0));
570+
571+
uc_assert_err(
572+
UC_ERR_FETCH_UNMAPPED,
573+
uc_emu_start(uc, code_start, code_start + sizeof(code) - 1, 0, 0));
574+
575+
OK(uc_reg_read(uc, UC_RISCV_REG_X5, &r_x5));
576+
OK(uc_reg_read(uc, UC_RISCV_REG_PC, &r_pc));
577+
TEST_CHECK(r_x5 == 0x7F00);
578+
TEST_CHECK(r_pc == 0x7F00);
579+
580+
OK(uc_close(uc));
581+
}
582+
583+
static bool test_riscv_correct_address_in_long_jump_hook_callback(uc_engine *uc, int type, uint64_t address, int size, int64_t value, void *user_data)
584+
{
585+
// Check registers
586+
uint64_t r_x5 = 0x0;
587+
uint64_t r_pc = 0x0;
588+
OK(uc_reg_read(uc, UC_RISCV_REG_X5, &r_x5));
589+
OK(uc_reg_read(uc, UC_RISCV_REG_PC, &r_pc));
590+
TEST_CHECK(r_x5 == 0x7FFFFFFFFFFFFF00);
591+
TEST_CHECK(r_pc == 0x7FFFFFFFFFFFFF00);
592+
593+
// Check address
594+
// printf("%lx\n", address);
595+
TEST_CHECK(address == 0x7FFFFFFFFFFFFF00);
596+
return false;
597+
}
598+
599+
static void test_riscv_correct_address_in_long_jump_hook(void)
600+
{
601+
uc_engine *uc;
602+
// li 0x7FFFFFFFFFFFFF00, x5 > addi t0, zero, -1; slli t0, t0, 63; addi t0, t0, -256;
603+
// jr x5
604+
char code[] = "\x93\x02\xf0\xff\x93\x92\xf2\x03\x93\x82\x02\xf0\x67\x80\x02\x00";
605+
606+
uint64_t r_x5 = 0x0;
607+
uint64_t r_pc = 0x0;
608+
uc_hook hook;
609+
610+
uc_common_setup(&uc, UC_ARCH_RISCV, UC_MODE_RISCV64, code, sizeof(code) - 1);
611+
OK(uc_hook_add(uc, &hook, UC_HOOK_MEM_UNMAPPED, test_riscv_correct_address_in_long_jump_hook_callback, NULL, 1, 0));
612+
613+
uc_assert_err(
614+
UC_ERR_FETCH_UNMAPPED,
615+
uc_emu_start(uc, code_start, code_start + sizeof(code) - 1, 0, 0));
616+
617+
OK(uc_reg_read(uc, UC_RISCV_REG_X5, &r_x5));
618+
OK(uc_reg_read(uc, UC_RISCV_REG_PC, &r_pc));
619+
TEST_CHECK(r_x5 == 0x7FFFFFFFFFFFFF00);
620+
TEST_CHECK(r_pc == 0x7FFFFFFFFFFFFF00);
621+
622+
OK(uc_close(uc));
623+
}
624+
625+
626+
540627
TEST_LIST = {
541628
{"test_riscv32_nop", test_riscv32_nop},
542629
{"test_riscv64_nop", test_riscv64_nop},
@@ -556,4 +643,6 @@ TEST_LIST = {
556643
{"test_riscv32_map", test_riscv32_map},
557644
{"test_riscv64_code_patching", test_riscv64_code_patching},
558645
{"test_riscv64_code_patching_count", test_riscv64_code_patching_count},
646+
{"test_riscv_correct_address_in_small_jump_hook", test_riscv_correct_address_in_small_jump_hook},
647+
{"test_riscv_correct_address_in_long_jump_hook", test_riscv_correct_address_in_long_jump_hook},
559648
{NULL, NULL}};

tests/unit/test_x86.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,93 @@ static void test_x86_nested_uc_emu_start_exits(void)
981981
OK(uc_close(uc));
982982
}
983983

984+
static bool test_x86_correct_address_in_small_jump_hook_callback(uc_engine *uc, int type, uint64_t address, int size, int64_t value, void *user_data)
985+
{
986+
// Check registers
987+
uint64_t r_rax = 0x0;
988+
uint64_t r_rip = 0x0;
989+
OK(uc_reg_read(uc, UC_X86_REG_RAX, &r_rax));
990+
OK(uc_reg_read(uc, UC_X86_REG_RIP, &r_rip));
991+
TEST_CHECK(r_rax == 0x7F00);
992+
TEST_CHECK(r_rip == 0x7F00);
993+
994+
// Check address
995+
TEST_CHECK(address == 0x7F00);
996+
997+
return false;
998+
}
999+
1000+
static void test_x86_correct_address_in_small_jump_hook(void)
1001+
{
1002+
uc_engine *uc;
1003+
// movabs $0x7F00, %rax
1004+
// jmp *%rax
1005+
char code[] = "\x48\xb8\x00\x7F\x00\x00\x00\x00\x00\x00\xff\xe0";
1006+
1007+
uint64_t r_rax = 0x0;
1008+
uint64_t r_rip = 0x0;
1009+
uc_hook hook;
1010+
1011+
uc_common_setup(&uc, UC_ARCH_X86, UC_MODE_64, code, sizeof(code) - 1);
1012+
OK(uc_hook_add(uc, &hook, UC_HOOK_MEM_UNMAPPED, test_x86_correct_address_in_small_jump_hook_callback, NULL, 1, 0));
1013+
1014+
1015+
uc_assert_err(
1016+
UC_ERR_FETCH_UNMAPPED,
1017+
uc_emu_start(uc, code_start, code_start + sizeof(code) - 1, 0, 0));
1018+
1019+
OK(uc_reg_read(uc, UC_X86_REG_RAX, &r_rax));
1020+
OK(uc_reg_read(uc, UC_X86_REG_RIP, &r_rip));
1021+
TEST_CHECK(r_rax == 0x7F00);
1022+
TEST_CHECK(r_rip == 0x7F00);
1023+
1024+
OK(uc_close(uc));
1025+
}
1026+
1027+
static bool test_x86_correct_address_in_long_jump_hook_callback(uc_engine *uc, int type, uint64_t address, int size, int64_t value, void *user_data)
1028+
{
1029+
// Check registers
1030+
uint64_t r_rax = 0x0;
1031+
uint64_t r_rip = 0x0;
1032+
OK(uc_reg_read(uc, UC_X86_REG_RAX, &r_rax));
1033+
OK(uc_reg_read(uc, UC_X86_REG_RIP, &r_rip));
1034+
TEST_CHECK(r_rax == 0x7FFFFFFFFFFFFF00);
1035+
TEST_CHECK(r_rip == 0x7FFFFFFFFFFFFF00);
1036+
1037+
// Check address
1038+
TEST_CHECK(address == 0x7FFFFFFFFFFFFF00);
1039+
1040+
return false;
1041+
}
1042+
1043+
static void test_x86_correct_address_in_long_jump_hook(void)
1044+
{
1045+
uc_engine *uc;
1046+
// movabs $0x7FFFFFFFFFFFFF00, %rax
1047+
// jmp *%rax
1048+
char code[] = "\x48\xb8\x00\xff\xff\xff\xff\xff\xff\x7f\xff\xe0";
1049+
1050+
uint64_t r_rax = 0x0;
1051+
uint64_t r_rip = 0x0;
1052+
uc_hook hook;
1053+
1054+
uc_common_setup(&uc, UC_ARCH_X86, UC_MODE_64, code, sizeof(code) - 1);
1055+
OK(uc_hook_add(uc, &hook, UC_HOOK_MEM_UNMAPPED, test_x86_correct_address_in_long_jump_hook_callback, NULL, 1, 0));
1056+
1057+
1058+
uc_assert_err(
1059+
UC_ERR_FETCH_UNMAPPED,
1060+
uc_emu_start(uc, code_start, code_start + sizeof(code) - 1, 0, 0));
1061+
1062+
OK(uc_reg_read(uc, UC_X86_REG_RAX, &r_rax));
1063+
OK(uc_reg_read(uc, UC_X86_REG_RIP, &r_rip));
1064+
TEST_CHECK(r_rax == 0x7FFFFFFFFFFFFF00);
1065+
TEST_CHECK(r_rip == 0x7FFFFFFFFFFFFF00);
1066+
1067+
OK(uc_close(uc));
1068+
}
1069+
1070+
9841071
static void test_x86_cpuid_1()
9851072
{
9861073
uc_engine *uc;
@@ -1030,5 +1117,7 @@ TEST_LIST = {
10301117
{"test_x86_64_nested_emu_start_error", test_x86_64_nested_emu_start_error},
10311118
{"test_x86_eflags_reserved_bit", test_x86_eflags_reserved_bit},
10321119
{"test_x86_nested_uc_emu_start_exits", test_x86_nested_uc_emu_start_exits},
1120+
{"test_x86_correct_address_in_small_jump_hook", test_x86_correct_address_in_small_jump_hook},
1121+
{"test_x86_correct_address_in_long_jump_hook", test_x86_correct_address_in_long_jump_hook},
10331122
{"test_x86_cpuid_1", test_x86_cpuid_1},
10341123
{NULL, NULL}};

0 commit comments

Comments
 (0)