commit 520c6647c32f02d83083d969d416154aa95e922c
Merge: 6bb29b12 b999f507
Author: mio <mio@lazym.io>
Date: Sun Apr 13 00:14:23 2025 +0800
merge dev
commit 6bb29b12f1d9f452365cc9cb5bc2d65ef376af30
Author: mio <mio@lazym.io>
Date: Sun Apr 13 00:13:12 2025 +0800
enable test
commit bcb8b363ef12ac295cf4fe4f1645416e5f0ea6ae
Author: mio <mio@lazym.io>
Date: Sun Apr 13 00:13:06 2025 +0800
also logging
commit 5972fc156b7379d09582c745d6d597e07555f2f4
Author: mio <mio@lazym.io>
Date: Sun Apr 13 00:12:58 2025 +0800
no unlimited translation
commit 7d600feebf9055505918e50d0af8b529a3eba542
Author: mio <mio@lazym.io>
Date: Sun Apr 13 00:12:47 2025 +0800
Ignore bindings.rs
commit dde4d50f2c7713156ac3bc284287480e4d92005f
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sun Apr 6 03:26:22 2025 -0400
alias `uc_mips_reg` to `UC_MIPS_REG`
commit 04234ae01ba7c82d9717eaae64cdda289ce3b832
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sun Apr 6 01:13:00 2025 -0400
remove bindings.rs
commit edec1300cd7c2d8ef4babbd51f6bcba2e126bdd7
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sat Apr 5 14:29:40 2025 -0400
address review
commit feb157b28b6c262c5dc3d810ec54de55a25bcd6e
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sat Mar 29 22:40:53 2025 -0400
ci(rust): rework workflow
The notable changes are migrating to
`actions-rust-lang/setup-rust-toolchain` for setting up Rust as it's
maintained, and using `katyo/publish-crates` for publishing crates in a
workspace
commit c1c7a8f8ed841b6ec5b4abe57013a1c2c9748c60
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sat Mar 29 22:40:06 2025 -0400
build(rust): set `rust-version` to 1.85
commit 8df938c9f8b478160213707674157103b0893caf
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sat Mar 29 21:53:21 2025 -0400
fix(rust): correct unsound pointer cast
The size of `T` is not guaranteed to be the size of `i32` - all we know
is that `T` is `Into<i32>`, so we should first copy them over into an
`i32` array
commit 3059b2583a60aa0cac9278afc945ed87f7ddb65e
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sat Mar 29 20:13:26 2025 -0400
docs(rust): update readme
commit 7db69a888e58a4bda20083e4e0771d26a327ad13
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sat Mar 29 13:58:30 2025 -0400
feat(rust): add comprehensive tests
These tests are copied over from the C tests
commit 78f2207f0e0481aef4de6d5908f8dc699a39a8d5
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sat Mar 29 13:57:27 2025 -0400
feat(rust): add tcg hook
commit 46e53328531ec3279dadbf18c16b493432227b31
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sat Mar 29 13:56:55 2025 -0400
feat(rust): add a hook for arm64 sys instructions
commit d1b58ee8282bf1eeeefbf68c87c2cf7c50c90320
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sat Mar 29 13:56:35 2025 -0400
feat(rust): add the ability to read the arm coprocessor register
commit d304da18b9e6741042b2a70657437be8f39f5c7c
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sat Mar 29 13:55:29 2025 -0400
feat(rust): add missing `Context` methods
commit 0dd87833081ac9db1feaf5bae8c839a7a2ae4947
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sat Mar 29 13:44:51 2025 -0400
refactor(rust): remove unnecessary code
`unicorn-engine-sys` will provide the necessary constants & types
commit da3d2fa7c3ecd3ae8fdb6672b6c5ea23da4570ff
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sat Mar 29 13:43:57 2025 -0400
feat(rust): add a workspace `Cargo.toml`, and use `unicorn-engine-sys`
commit b27a2a93e4ac43aa2079e936df4dd30a1f8f329a
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sat Mar 29 13:38:06 2025 -0400
feat(rust): introduce `unicorn-engine-sys` crate
This crate contains generated Rust bindings to the C library via
bindgen. It is independent from the main `unicorn-engine` bindings,
which will leverage this
commit bcec87a3f6e316e328683c303ccfa89e530a6c56
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sat Mar 29 13:31:24 2025 -0400
test(m68k): actually assert an expectation
This test did not actually test for anything before
commit bc7e65ca96164496eb2e250b1f296a33a8aa58ee
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sat Mar 29 13:31:09 2025 -0400
style(test): use bitflag shorthands
commit 0ab4b7fefb3ca17b0b5977d7b204291c5de184ad
Author: Amaan Qureshi <amaanq12@gmail.com>
Date: Sat Mar 29 13:22:13 2025 -0400
fix(mips): lowercase enum name `uc_mips_reg`
This aligns with other architectures
Co-authored-by: Amaan Qureshi <amaanq12@gmail.com>
468 lines
14 KiB
C
468 lines
14 KiB
C
#include "unicorn_test.h"
|
|
|
|
static void test_map_correct(void)
|
|
{
|
|
uc_engine *uc;
|
|
|
|
OK(uc_open(UC_ARCH_X86, UC_MODE_64, &uc));
|
|
OK(uc_mem_map(uc, 0x40000, 0x1000 * 16, UC_PROT_ALL)); // [0x40000, 0x50000]
|
|
OK(uc_mem_map(uc, 0x60000, 0x1000 * 16, UC_PROT_ALL)); // [0x60000, 0x70000]
|
|
OK(uc_mem_map(uc, 0x20000, 0x1000 * 16, UC_PROT_ALL)); // [0x20000, 0x30000]
|
|
uc_assert_err(UC_ERR_MAP,
|
|
uc_mem_map(uc, 0x10000, 0x2000 * 16, UC_PROT_ALL));
|
|
uc_assert_err(UC_ERR_MAP,
|
|
uc_mem_map(uc, 0x25000, 0x1000 * 16, UC_PROT_ALL));
|
|
uc_assert_err(UC_ERR_MAP,
|
|
uc_mem_map(uc, 0x35000, 0x1000 * 16, UC_PROT_ALL));
|
|
uc_assert_err(UC_ERR_MAP,
|
|
uc_mem_map(uc, 0x45000, 0x1000 * 16, UC_PROT_ALL));
|
|
uc_assert_err(UC_ERR_MAP,
|
|
uc_mem_map(uc, 0x55000, 0x2000 * 16, UC_PROT_ALL));
|
|
OK(uc_mem_map(uc, 0x35000, 0x5000, UC_PROT_ALL));
|
|
OK(uc_mem_map(uc, 0x50000, 0x5000, UC_PROT_ALL));
|
|
|
|
OK(uc_close(uc));
|
|
}
|
|
|
|
static void test_map_wrapping(void)
|
|
{
|
|
uc_engine *uc;
|
|
|
|
OK(uc_open(UC_ARCH_X86, UC_MODE_64, &uc));
|
|
uc_assert_err(UC_ERR_ARG, uc_mem_map(uc, (~0ll - 0x4000) & ~0xfff, 0x8000,
|
|
UC_PROT_ALL));
|
|
|
|
OK(uc_close(uc));
|
|
}
|
|
|
|
static void test_mem_protect(void)
|
|
{
|
|
uc_engine *qc;
|
|
int r_eax = 0x2000;
|
|
int r_esi = 0xdeadbeef;
|
|
uint32_t mem;
|
|
// add [eax + 4], esi
|
|
char code[] = {0x01, 0x70, 0x04};
|
|
|
|
OK(uc_open(UC_ARCH_X86, UC_MODE_32, &qc));
|
|
OK(uc_reg_write(qc, UC_X86_REG_EAX, &r_eax));
|
|
OK(uc_reg_write(qc, UC_X86_REG_ESI, &r_esi));
|
|
OK(uc_mem_map(qc, 0x1000, 0x1000, UC_PROT_READ | UC_PROT_EXEC));
|
|
OK(uc_mem_map(qc, 0x2000, 0x1000, UC_PROT_READ));
|
|
OK(uc_mem_protect(qc, 0x2000, 0x1000, UC_PROT_READ | UC_PROT_WRITE));
|
|
OK(uc_mem_write(qc, 0x1000, code, sizeof(code)));
|
|
|
|
OK(uc_emu_start(qc, 0x1000, 0x1000 + sizeof(code) - 1, 0, 1));
|
|
OK(uc_mem_read(qc, 0x2000 + 4, &mem, 4));
|
|
|
|
TEST_CHECK(LEINT32(mem) == 0xdeadbeef);
|
|
|
|
OK(uc_close(qc));
|
|
}
|
|
|
|
static void test_splitting_mem_unmap(void)
|
|
{
|
|
uc_engine *uc;
|
|
|
|
OK(uc_open(UC_ARCH_X86, UC_MODE_32, &uc));
|
|
|
|
OK(uc_mem_map(uc, 0x20000, 0x1000, UC_PROT_NONE));
|
|
OK(uc_mem_map(uc, 0x21000, 0x2000, UC_PROT_NONE));
|
|
|
|
OK(uc_mem_unmap(uc, 0x21000, 0x1000));
|
|
|
|
OK(uc_close(uc));
|
|
}
|
|
|
|
static uint64_t test_splitting_mmio_unmap_read_callback(uc_engine *uc,
|
|
uint64_t offset,
|
|
unsigned size,
|
|
void *user_data)
|
|
{
|
|
TEST_CHECK(offset == 4);
|
|
TEST_CHECK(size == 4);
|
|
|
|
return 0x19260817;
|
|
}
|
|
|
|
static void test_splitting_mmio_unmap(void)
|
|
{
|
|
uc_engine *uc;
|
|
// mov ecx, [0x3004] <-- normal read
|
|
// mov ebx, [0x4004] <-- mmio read
|
|
char code[] = "\x8b\x0d\x04\x30\x00\x00\x8b\x1d\x04\x40\x00\x00";
|
|
int r_ecx, r_ebx;
|
|
int bytes = LEINT32(0xdeadbeef);
|
|
|
|
OK(uc_open(UC_ARCH_X86, UC_MODE_32, &uc));
|
|
|
|
OK(uc_mem_map(uc, 0x1000, 0x1000, UC_PROT_ALL));
|
|
OK(uc_mem_write(uc, 0x1000, code, sizeof(code) - 1));
|
|
|
|
OK(uc_mmio_map(uc, 0x3000, 0x2000, test_splitting_mmio_unmap_read_callback,
|
|
NULL, NULL, NULL));
|
|
|
|
// Map a ram area instead
|
|
OK(uc_mem_unmap(uc, 0x3000, 0x1000));
|
|
OK(uc_mem_map(uc, 0x3000, 0x1000, UC_PROT_ALL));
|
|
OK(uc_mem_write(uc, 0x3004, &bytes, 4));
|
|
|
|
OK(uc_emu_start(uc, 0x1000, 0x1000 + sizeof(code) - 1, 0, 0));
|
|
|
|
OK(uc_reg_read(uc, UC_X86_REG_ECX, &r_ecx));
|
|
OK(uc_reg_read(uc, UC_X86_REG_EBX, &r_ebx));
|
|
|
|
TEST_CHECK(r_ecx == 0xdeadbeef);
|
|
TEST_CHECK(r_ebx == 0x19260817);
|
|
|
|
OK(uc_close(uc));
|
|
}
|
|
|
|
static void test_mem_protect_map_ptr(void)
|
|
{
|
|
uc_engine *uc;
|
|
uint64_t val = 0x114514;
|
|
uint8_t *data1 = NULL;
|
|
uint8_t *data2 = NULL;
|
|
uint64_t mem;
|
|
|
|
data1 = calloc(sizeof(*data1), 0x4000);
|
|
data2 = calloc(sizeof(*data2), 0x2000);
|
|
|
|
OK(uc_open(UC_ARCH_X86, UC_MODE_64, &uc));
|
|
|
|
OK(uc_mem_map_ptr(uc, 0x4000, 0x4000, UC_PROT_ALL, data1));
|
|
OK(uc_mem_unmap(uc, 0x6000, 0x2000));
|
|
OK(uc_mem_map_ptr(uc, 0x6000, 0x2000, UC_PROT_ALL, data2));
|
|
|
|
OK(uc_mem_write(uc, 0x6004, &val, 8));
|
|
OK(uc_mem_protect(uc, 0x6000, 0x1000, UC_PROT_READ));
|
|
OK(uc_mem_read(uc, 0x6004, (void *)&mem, 8));
|
|
|
|
TEST_CHECK(val == mem);
|
|
|
|
OK(uc_close(uc));
|
|
|
|
free(data2);
|
|
free(data1);
|
|
}
|
|
|
|
static void test_map_at_the_end(void)
|
|
{
|
|
uc_engine *uc;
|
|
uint8_t mem[0x1000];
|
|
|
|
memset(mem, 0xff, 0x100);
|
|
|
|
OK(uc_open(UC_ARCH_X86, UC_MODE_64, &uc));
|
|
|
|
OK(uc_mem_map(uc, 0xfffffffffffff000, 0x1000, UC_PROT_ALL));
|
|
OK(uc_mem_write(uc, 0xfffffffffffff000, mem, sizeof(mem)));
|
|
|
|
uc_assert_err(UC_ERR_WRITE_UNMAPPED,
|
|
uc_mem_write(uc, 0xffffffffffffff00, mem, sizeof(mem)));
|
|
uc_assert_err(UC_ERR_WRITE_UNMAPPED, uc_mem_write(uc, 0, mem, sizeof(mem)));
|
|
|
|
OK(uc_close(uc));
|
|
}
|
|
|
|
static void test_map_wrap(void)
|
|
{
|
|
uc_engine *uc;
|
|
|
|
OK(uc_open(UC_ARCH_X86, UC_MODE_64, &uc));
|
|
|
|
uc_assert_err(UC_ERR_ARG,
|
|
uc_mem_map(uc, 0xfffffffffffff000, 0x2000, UC_PROT_ALL));
|
|
|
|
OK(uc_close(uc));
|
|
}
|
|
|
|
static void test_map_big_memory(void)
|
|
{
|
|
uc_engine *uc;
|
|
|
|
OK(uc_open(UC_ARCH_X86, UC_MODE_64, &uc));
|
|
|
|
#if defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__)
|
|
uint64_t requested_size = 0xfffffffffffff000; // assume 4K page size
|
|
#else
|
|
long ps = sysconf(_SC_PAGESIZE);
|
|
uint64_t requested_size = (uint64_t)(-ps);
|
|
#endif
|
|
|
|
uc_assert_err(UC_ERR_NOMEM,
|
|
uc_mem_map(uc, 0x0, requested_size, UC_PROT_ALL));
|
|
|
|
OK(uc_close(uc));
|
|
}
|
|
|
|
static void test_mem_protect_remove_exec_callback(uc_engine *uc, uint64_t addr,
|
|
size_t size, void *data)
|
|
{
|
|
uint64_t *p = (uint64_t *)data;
|
|
(*p)++;
|
|
|
|
OK(uc_mem_protect(uc, 0x2000, 0x1000, UC_PROT_READ));
|
|
}
|
|
|
|
static void test_mem_protect_remove_exec(void)
|
|
{
|
|
uc_engine *uc;
|
|
char code[] = "\x90\xeb\x00\x90";
|
|
uc_hook hk;
|
|
uint64_t called_count = 0;
|
|
|
|
OK(uc_open(UC_ARCH_X86, UC_MODE_64, &uc));
|
|
OK(uc_mem_map(uc, 0x1000, 0x1000, UC_PROT_ALL));
|
|
OK(uc_mem_map(uc, 0x2000, 0x1000, UC_PROT_ALL));
|
|
|
|
OK(uc_mem_write(uc, 0x1000, code, sizeof(code) - 1));
|
|
OK(uc_hook_add(uc, &hk, UC_HOOK_BLOCK,
|
|
test_mem_protect_remove_exec_callback, (void *)&called_count,
|
|
1, 0));
|
|
|
|
OK(uc_emu_start(uc, 0x1000, 0x1000 + sizeof(code) - 1, 0, 0));
|
|
|
|
TEST_CHECK(called_count == 2);
|
|
|
|
OK(uc_close(uc));
|
|
}
|
|
|
|
static uint64_t test_mem_protect_mmio_read_cb(struct uc_struct *uc,
|
|
uint64_t addr, unsigned size,
|
|
void *user_data)
|
|
{
|
|
TEST_CHECK(addr == 0x20); // note, it's not 0x1020
|
|
|
|
*(uint64_t *)user_data += 1;
|
|
return 0x114514;
|
|
}
|
|
|
|
static void test_mem_protect_mmio_write_cb(struct uc_struct *uc, uint64_t addr,
|
|
unsigned size, uint64_t data,
|
|
void *user_data)
|
|
{
|
|
TEST_CHECK(false);
|
|
return;
|
|
}
|
|
|
|
static void test_mem_protect_mmio(void)
|
|
{
|
|
uc_engine *uc;
|
|
// mov eax, [0x2020]; mov [0x2020], eax
|
|
char code[] = "\xa1\x20\x20\x00\x00\x00\x00\x00\x00\xa3\x20\x20\x00\x00\x00"
|
|
"\x00\x00\x00";
|
|
uint64_t called = 0;
|
|
uint64_t r_eax;
|
|
|
|
OK(uc_open(UC_ARCH_X86, UC_MODE_64, &uc));
|
|
OK(uc_mem_map(uc, 0x8000, 0x1000, UC_PROT_ALL));
|
|
OK(uc_mem_write(uc, 0x8000, code, sizeof(code) - 1));
|
|
|
|
OK(uc_mmio_map(uc, 0x1000, 0x3000, test_mem_protect_mmio_read_cb,
|
|
(void *)&called, test_mem_protect_mmio_write_cb,
|
|
(void *)&called));
|
|
OK(uc_mem_protect(uc, 0x2000, 0x1000, UC_PROT_READ));
|
|
|
|
uc_assert_err(UC_ERR_WRITE_PROT,
|
|
uc_emu_start(uc, 0x8000, 0x8000 + sizeof(code) - 1, 0, 0));
|
|
OK(uc_reg_read(uc, UC_X86_REG_RAX, &r_eax));
|
|
|
|
TEST_CHECK(called == 1);
|
|
TEST_CHECK(r_eax == 0x114514);
|
|
|
|
OK(uc_close(uc));
|
|
}
|
|
|
|
static void test_snapshot(void)
|
|
{
|
|
uc_engine *uc;
|
|
uc_context *c0, *c1;
|
|
uint32_t mem;
|
|
uint8_t code_data;
|
|
// mov eax, [0x2020]; inc eax; mov [0x2020], eax
|
|
char code[] = "\xa1\x20\x20\x00\x00\x00\x00\x00\x00\xff\xc0\xa3\x20\x20\x00"
|
|
"\x00\x00\x00\x00\x00";
|
|
|
|
OK(uc_open(UC_ARCH_X86, UC_MODE_64, &uc));
|
|
OK(uc_context_alloc(uc, &c0));
|
|
OK(uc_context_alloc(uc, &c1));
|
|
OK(uc_ctl_context_mode(uc, UC_CTL_CONTEXT_MEMORY));
|
|
OK(uc_mem_map(uc, 0x1000, 0x1000, UC_PROT_ALL));
|
|
OK(uc_mem_write(uc, 0x1000, code, sizeof(code) - 1));
|
|
|
|
OK(uc_mem_map(uc, 0x2000, 0x1000, UC_PROT_ALL));
|
|
OK(uc_context_save(uc, c0));
|
|
|
|
OK(uc_emu_start(uc, 0x1000, 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, 0x1000, 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));
|
|
|
|
OK(uc_mem_read(uc, 0x2020, &mem, sizeof(mem)));
|
|
TEST_CHECK(mem == 1);
|
|
OK(uc_context_restore(uc, c0));
|
|
OK(uc_mem_read(uc, 0x2020, &mem, sizeof(mem)));
|
|
TEST_CHECK(mem == 0);
|
|
|
|
OK(uc_mem_read(uc, 0x1000, &code_data, sizeof(code_data)));
|
|
TEST_CHECK(code_data == 0xa1);
|
|
|
|
OK(uc_context_free(c0));
|
|
OK(uc_context_free(c1));
|
|
OK(uc_close(uc));
|
|
}
|
|
|
|
static bool test_snapshot_with_vtlb_callback(uc_engine *uc, uint64_t addr,
|
|
uc_mem_type type,
|
|
uc_tlb_entry *result,
|
|
void *user_data)
|
|
{
|
|
result->paddr = addr - 0x400000000;
|
|
result->perms = UC_PROT_ALL;
|
|
return true;
|
|
}
|
|
|
|
static void test_snapshot_with_vtlb(void)
|
|
{
|
|
uc_engine *uc;
|
|
uc_context *c0, *c1;
|
|
uint32_t mem;
|
|
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";
|
|
|
|
OK(uc_open(UC_ARCH_X86, UC_MODE_64, &uc));
|
|
|
|
// Allocate contexts
|
|
OK(uc_context_alloc(uc, &c0));
|
|
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));
|
|
|
|
// Map physical memory
|
|
OK(uc_mem_map(uc, 0x1000, 0x1000, UC_PROT_EXEC | UC_PROT_READ));
|
|
OK(uc_mem_write(uc, 0x1000, code, sizeof(code) - 1));
|
|
OK(uc_mem_map(uc, 0x2000, 0x1000, UC_PROT_ALL));
|
|
|
|
// 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_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_mem_read(uc, 0x2020, &mem, sizeof(mem)));
|
|
TEST_CHECK(mem == 2);
|
|
OK(uc_context_restore(uc, c1));
|
|
// TODO check mem
|
|
OK(uc_mem_read(uc, 0x2020, &mem, sizeof(mem)));
|
|
TEST_CHECK(mem == 1);
|
|
OK(uc_context_restore(uc, c0));
|
|
OK(uc_mem_read(uc, 0x2020, &mem, sizeof(mem)));
|
|
TEST_CHECK(mem == 0);
|
|
// TODO check mem
|
|
|
|
OK(uc_context_free(c0));
|
|
OK(uc_context_free(c1));
|
|
OK(uc_close(uc));
|
|
}
|
|
|
|
static void test_context_snapshot(void)
|
|
{
|
|
uc_engine *uc;
|
|
uc_context *ctx;
|
|
uint64_t baseaddr = 0xfffff1000;
|
|
uint64_t offset = 0x10;
|
|
uint64_t tmp = 1;
|
|
|
|
OK(uc_open(UC_ARCH_X86, UC_MODE_64, &uc));
|
|
OK(uc_ctl_context_mode(uc, UC_CTL_CONTEXT_MEMORY | UC_CTL_CONTEXT_CPU));
|
|
OK(uc_mem_map(uc, baseaddr, 0x1000, UC_PROT_ALL));
|
|
OK(uc_context_alloc(uc, &ctx));
|
|
OK(uc_context_save(uc, ctx));
|
|
|
|
OK(uc_mem_write(uc, baseaddr + offset, &tmp, sizeof(tmp)));
|
|
OK(uc_mem_read(uc, baseaddr + offset, &tmp, sizeof(tmp)));
|
|
TEST_CHECK(tmp == 1);
|
|
OK(uc_context_restore(uc, ctx));
|
|
OK(uc_mem_read(uc, baseaddr + offset, &tmp, sizeof(tmp)));
|
|
TEST_CHECK(tmp == 0);
|
|
|
|
tmp = 2;
|
|
OK(uc_mem_write(uc, baseaddr + offset, &tmp, sizeof(tmp)));
|
|
OK(uc_mem_read(uc, baseaddr + offset, &tmp, sizeof(tmp)));
|
|
TEST_CHECK(tmp == 2);
|
|
OK(uc_context_restore(uc, ctx));
|
|
OK(uc_mem_read(uc, baseaddr + offset, &tmp, sizeof(tmp)));
|
|
TEST_CHECK(tmp == 0);
|
|
|
|
OK(uc_context_free(ctx));
|
|
OK(uc_close(uc));
|
|
}
|
|
|
|
static void test_snapshot_unmap(void)
|
|
{
|
|
uc_engine *uc;
|
|
uc_context *ctx;
|
|
uint64_t tmp;
|
|
|
|
OK(uc_open(UC_ARCH_X86, UC_MODE_64, &uc));
|
|
OK(uc_ctl_context_mode(uc, UC_CTL_CONTEXT_MEMORY | UC_CTL_CONTEXT_CPU));
|
|
OK(uc_mem_map(uc, 0x1000, 0x2000, UC_PROT_ALL));
|
|
|
|
tmp = 1;
|
|
OK(uc_mem_write(uc, 0x1000, &tmp, sizeof(tmp)));
|
|
tmp = 2;
|
|
OK(uc_mem_write(uc, 0x2000, &tmp, sizeof(tmp)));
|
|
|
|
OK(uc_context_alloc(uc, &ctx));
|
|
OK(uc_context_save(uc, ctx));
|
|
|
|
uc_assert_err(UC_ERR_ARG, uc_mem_unmap(uc, 0x1000, 0x1000));
|
|
OK(uc_mem_unmap(uc, 0x1000, 0x2000));
|
|
uc_assert_err(UC_ERR_READ_UNMAPPED,
|
|
uc_mem_read(uc, 0x1000, &tmp, sizeof(tmp)));
|
|
uc_assert_err(UC_ERR_READ_UNMAPPED,
|
|
uc_mem_read(uc, 0x2000, &tmp, sizeof(tmp)));
|
|
|
|
OK(uc_context_restore(uc, ctx));
|
|
OK(uc_mem_read(uc, 0x1000, &tmp, sizeof(tmp)));
|
|
TEST_CHECK(tmp == 1);
|
|
OK(uc_mem_read(uc, 0x2000, &tmp, sizeof(tmp)));
|
|
TEST_CHECK(tmp == 2);
|
|
|
|
OK(uc_context_free(ctx));
|
|
OK(uc_close(uc));
|
|
}
|
|
|
|
TEST_LIST = {{"test_map_correct", test_map_correct},
|
|
{"test_map_wrapping", test_map_wrapping},
|
|
{"test_mem_protect", test_mem_protect},
|
|
{"test_splitting_mem_unmap", test_splitting_mem_unmap},
|
|
{"test_splitting_mmio_unmap", test_splitting_mmio_unmap},
|
|
{"test_mem_protect_map_ptr", test_mem_protect_map_ptr},
|
|
{"test_map_at_the_end", test_map_at_the_end},
|
|
{"test_map_wrap", test_map_wrap},
|
|
{"test_map_big_memory", test_map_big_memory},
|
|
{"test_mem_protect_remove_exec", test_mem_protect_remove_exec},
|
|
{"test_mem_protect_mmio", test_mem_protect_mmio},
|
|
{"test_snapshot", test_snapshot},
|
|
{"test_snapshot_with_vtlb", test_snapshot_with_vtlb},
|
|
{"test_context_snapshot", test_context_snapshot},
|
|
{"test_snapshot_unmap", test_snapshot_unmap},
|
|
{NULL, NULL}};
|