Skip to content

Commit 4f417c3

Browse files
Michael-c0deyaojiale2024@iscas.ac.cnwtdcode
authored
patch multiple UC_HOOK_MEM callbacks for unaligned access (#2063)
* patch multiple UC_HOOK_MEM callbacks for unaligned access * update test_x86.c for #2063 * update test_x86.c for build on win --------- Co-authored-by: [email protected] <[email protected]> Co-authored-by: lazymio <[email protected]>
1 parent 9cfd5cf commit 4f417c3

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

qemu/accel/tcg/cputlb.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,8 @@ load_helper(CPUArchState *env, target_ulong addr, TCGMemOpIdx oi,
15931593
}
15941594

15951595
// now it is read on mapped memory
1596-
if (!code_read) {
1596+
// patch issue #1041 multiple UC_HOOK_MEM callbacks for unaligned access
1597+
if (!code_read && !uc->size_recur_mem) {
15971598
// this is date reading
15981599
HOOK_FOREACH(uc, hook, UC_HOOK_MEM_READ) {
15991600
if (hook->to_delete)

tests/unit/test_x86.c

+44
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,48 @@ static void test_x86_unaligned_access(void)
13421342

13431343
OK(uc_close(uc));
13441344
}
1345+
1346+
static void test_x86_64_unaligned_access(void){
1347+
uc_engine *uc;
1348+
uc_hook hook;
1349+
char code[] = {
1350+
"\x48\x89\x01" // mov qword ptr [rcx],rax
1351+
"\x48\x8b\x00" // mov rax,qword ptr [rax]
1352+
"\xcc"
1353+
};
1354+
uint64_t r_rax = LEINT64(0x2fffff);
1355+
uint64_t r_rcx = LEINT64(0x2fffff);
1356+
struct writelog_t write_log[10];
1357+
struct writelog_t read_log[10];
1358+
memset(write_log, 0, sizeof(write_log));
1359+
memset(read_log, 0, sizeof(read_log));
1360+
uc_common_setup(&uc, UC_ARCH_X86, UC_MODE_64, code, sizeof(code) - 1);
1361+
OK(uc_mem_map(uc, 0x200000, 0x200000, UC_PROT_ALL));
1362+
OK(uc_hook_add(uc, &hook, UC_HOOK_MEM_WRITE,
1363+
test_x86_unaligned_access_callback, write_log, 1, 0));
1364+
OK(uc_hook_add(uc, &hook, UC_HOOK_MEM_READ,
1365+
test_x86_unaligned_access_callback, read_log, 1, 0));
1366+
1367+
OK(uc_reg_write(uc, UC_X86_REG_RAX, &r_rax));
1368+
OK(uc_reg_write(uc, UC_X86_REG_RCX, &r_rcx));
1369+
1370+
OK(uc_emu_start(uc, code_start, code_start + sizeof(code) - 1, 0, 2));
1371+
1372+
TEST_CHECK(write_log[0].addr == 0x2fffff);
1373+
TEST_CHECK(write_log[0].size == 8);
1374+
TEST_CHECK(write_log[1].size == 0);
1375+
1376+
TEST_CHECK(read_log[0].addr == 0x2fffff);
1377+
TEST_CHECK(read_log[0].size == 8);
1378+
TEST_CHECK(read_log[1].size == 0);
1379+
1380+
uint64_t b;
1381+
OK(uc_mem_read(uc, 0x2fffff, &b, 8));
1382+
TEST_CHECK(b == 0x2fffff);
1383+
1384+
OK(uc_close(uc));
1385+
1386+
}
13451387
#endif
13461388

13471389
static bool test_x86_lazy_mapping_mem_callback(uc_engine *uc, uc_mem_type type,
@@ -2019,6 +2061,8 @@ TEST_LIST = {
20192061
{"test_x86_invalid_vex_l", test_x86_invalid_vex_l},
20202062
#if !defined(TARGET_READ_INLINED) && defined(BOOST_LITTLE_ENDIAN)
20212063
{"test_x86_unaligned_access", test_x86_unaligned_access},
2064+
{"test_x86_64_unaligned_access", test_x86_64_unaligned_access},
2065+
20222066
#endif
20232067
{"test_x86_lazy_mapping", test_x86_lazy_mapping},
20242068
{"test_x86_16_incorrect_ip", test_x86_16_incorrect_ip},

0 commit comments

Comments
 (0)