2015-08-21 10:04:50 +03:00
|
|
|
/* Unicorn Emulator Engine */
|
|
|
|
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2015 */
|
|
|
|
|
|
|
|
#if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64)
|
|
|
|
#pragma warning(disable:4996)
|
|
|
|
#endif
|
|
|
|
#if defined(UNICORN_HAS_OSXKERNEL)
|
|
|
|
#include <libkern/libkern.h>
|
|
|
|
#else
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <time.h> // nanosleep
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "uc_priv.h"
|
|
|
|
#include "hook.h"
|
|
|
|
|
|
|
|
// target specific headers
|
|
|
|
#include "qemu/target-m68k/unicorn.h"
|
|
|
|
#include "qemu/target-i386/unicorn.h"
|
|
|
|
#include "qemu/target-arm/unicorn.h"
|
|
|
|
#include "qemu/target-mips/unicorn.h"
|
|
|
|
#include "qemu/target-sparc/unicorn.h"
|
|
|
|
|
|
|
|
#include "qemu/include/hw/boards.h"
|
|
|
|
|
2015-08-30 10:22:18 +03:00
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
UNICORN_EXPORT
|
|
|
|
unsigned int uc_version(unsigned int *major, unsigned int *minor)
|
|
|
|
{
|
|
|
|
if (major != NULL && minor != NULL) {
|
|
|
|
*major = UC_API_MAJOR;
|
|
|
|
*minor = UC_API_MINOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (UC_API_MAJOR << 8) + UC_API_MINOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UNICORN_EXPORT
|
2015-09-05 06:20:32 +03:00
|
|
|
uc_err uc_errno(uc_engine *uc)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
|
|
|
return uc->errnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UNICORN_EXPORT
|
|
|
|
const char *uc_strerror(uc_err code)
|
|
|
|
{
|
|
|
|
switch(code) {
|
|
|
|
default:
|
|
|
|
return "Unknown error code";
|
|
|
|
case UC_ERR_OK:
|
|
|
|
return "OK (UC_ERR_OK)";
|
2015-09-01 23:40:19 +03:00
|
|
|
case UC_ERR_NOMEM:
|
|
|
|
return "No memory available or memory not present (UC_ERR_NOMEM)";
|
2015-08-21 10:04:50 +03:00
|
|
|
case UC_ERR_ARCH:
|
2015-10-31 00:32:59 +03:00
|
|
|
return "Invalid/unsupported architecture (UC_ERR_ARCH)";
|
2015-08-21 10:04:50 +03:00
|
|
|
case UC_ERR_HANDLE:
|
|
|
|
return "Invalid handle (UC_ERR_HANDLE)";
|
|
|
|
case UC_ERR_MODE:
|
|
|
|
return "Invalid mode (UC_ERR_MODE)";
|
|
|
|
case UC_ERR_VERSION:
|
|
|
|
return "Different API version between core & binding (UC_ERR_VERSION)";
|
2015-09-30 09:46:55 +03:00
|
|
|
case UC_ERR_READ_UNMAPPED:
|
|
|
|
return "Invalid memory read (UC_ERR_READ_UNMAPPED)";
|
|
|
|
case UC_ERR_WRITE_UNMAPPED:
|
|
|
|
return "Invalid memory write (UC_ERR_WRITE_UNMAPPED)";
|
|
|
|
case UC_ERR_FETCH_UNMAPPED:
|
|
|
|
return "Invalid memory fetch (UC_ERR_FETCH_UNMAPPED)";
|
2015-08-21 10:04:50 +03:00
|
|
|
case UC_ERR_HOOK:
|
|
|
|
return "Invalid hook type (UC_ERR_HOOK)";
|
2015-09-04 06:55:17 +03:00
|
|
|
case UC_ERR_INSN_INVALID:
|
|
|
|
return "Invalid instruction (UC_ERR_INSN_INVALID)";
|
2015-08-24 00:16:40 +03:00
|
|
|
case UC_ERR_MAP:
|
|
|
|
return "Invalid memory mapping (UC_ERR_MAP)";
|
2015-09-01 22:10:09 +03:00
|
|
|
case UC_ERR_WRITE_PROT:
|
|
|
|
return "Write to write-protected memory (UC_ERR_WRITE_PROT)";
|
|
|
|
case UC_ERR_READ_PROT:
|
|
|
|
return "Read from non-readable memory (UC_ERR_READ_PROT)";
|
2015-09-24 09:18:02 +03:00
|
|
|
case UC_ERR_FETCH_PROT:
|
|
|
|
return "Fetch from non-executable memory (UC_ERR_FETCH_PROT)";
|
2015-09-09 11:54:47 +03:00
|
|
|
case UC_ERR_ARG:
|
2015-10-19 23:52:56 +03:00
|
|
|
return "Invalid argument (UC_ERR_ARG)";
|
2015-09-09 10:52:15 +03:00
|
|
|
case UC_ERR_READ_UNALIGNED:
|
|
|
|
return "Read from unaligned memory (UC_ERR_READ_UNALIGNED)";
|
|
|
|
case UC_ERR_WRITE_UNALIGNED:
|
|
|
|
return "Write to unaligned memory (UC_ERR_WRITE_UNALIGNED)";
|
|
|
|
case UC_ERR_FETCH_UNALIGNED:
|
|
|
|
return "Fetch from unaligned memory (UC_ERR_FETCH_UNALIGNED)";
|
2015-10-10 13:01:47 +03:00
|
|
|
case UC_ERR_HOOK_EXIST:
|
2015-10-31 00:32:59 +03:00
|
|
|
return "Hook for this type event already exists (UC_ERR_HOOK_EXIST)";
|
2015-11-11 20:43:41 +03:00
|
|
|
case UC_ERR_RESOURCE:
|
|
|
|
return "Insufficient resource (UC_ERR_RESOURCE)";
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UNICORN_EXPORT
|
2015-08-24 04:10:47 +03:00
|
|
|
bool uc_arch_supported(uc_arch arch)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
2015-08-24 04:10:47 +03:00
|
|
|
switch (arch) {
|
|
|
|
#ifdef UNICORN_HAS_ARM
|
|
|
|
case UC_ARCH_ARM: return true;
|
|
|
|
#endif
|
|
|
|
#ifdef UNICORN_HAS_ARM64
|
|
|
|
case UC_ARCH_ARM64: return true;
|
|
|
|
#endif
|
|
|
|
#ifdef UNICORN_HAS_M68K
|
|
|
|
case UC_ARCH_M68K: return true;
|
|
|
|
#endif
|
|
|
|
#ifdef UNICORN_HAS_MIPS
|
|
|
|
case UC_ARCH_MIPS: return true;
|
|
|
|
#endif
|
|
|
|
#ifdef UNICORN_HAS_PPC
|
|
|
|
case UC_ARCH_PPC: return true;
|
|
|
|
#endif
|
|
|
|
#ifdef UNICORN_HAS_SPARC
|
|
|
|
case UC_ARCH_SPARC: return true;
|
|
|
|
#endif
|
|
|
|
#ifdef UNICORN_HAS_X86
|
|
|
|
case UC_ARCH_X86: return true;
|
|
|
|
#endif
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2015-08-24 04:10:47 +03:00
|
|
|
/* Invalid or disabled arch */
|
|
|
|
default: return false;
|
|
|
|
}
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UNICORN_EXPORT
|
2015-09-05 06:20:32 +03:00
|
|
|
uc_err uc_open(uc_arch arch, uc_mode mode, uc_engine **result)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
|
|
|
struct uc_struct *uc;
|
|
|
|
|
|
|
|
if (arch < UC_ARCH_MAX) {
|
|
|
|
uc = calloc(1, sizeof(*uc));
|
|
|
|
if (!uc) {
|
|
|
|
// memory insufficient
|
2015-09-01 23:40:19 +03:00
|
|
|
return UC_ERR_NOMEM;
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uc->errnum = UC_ERR_OK;
|
|
|
|
uc->arch = arch;
|
|
|
|
uc->mode = mode;
|
|
|
|
|
|
|
|
// uc->cpus = QTAILQ_HEAD_INITIALIZER(uc->cpus);
|
|
|
|
uc->cpus.tqh_first = NULL;
|
|
|
|
uc->cpus.tqh_last = &(uc->cpus.tqh_first);
|
|
|
|
// uc->ram_list = { .blocks = QTAILQ_HEAD_INITIALIZER(ram_list.blocks) };
|
|
|
|
uc->ram_list.blocks.tqh_first = NULL;
|
|
|
|
uc->ram_list.blocks.tqh_last = &(uc->ram_list.blocks.tqh_first);
|
|
|
|
|
|
|
|
uc->x86_global_cpu_lock = SPIN_LOCK_UNLOCKED;
|
|
|
|
|
|
|
|
uc->memory_listeners.tqh_first = NULL;
|
|
|
|
uc->memory_listeners.tqh_last = &uc->memory_listeners.tqh_first;
|
|
|
|
|
|
|
|
uc->address_spaces.tqh_first = NULL;
|
|
|
|
uc->address_spaces.tqh_last = &uc->address_spaces.tqh_first;
|
|
|
|
|
|
|
|
switch(arch) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
#ifdef UNICORN_HAS_M68K
|
|
|
|
case UC_ARCH_M68K:
|
2016-01-23 04:17:59 +03:00
|
|
|
if ((mode & ~UC_MODE_M68K_MASK) ||
|
2016-01-23 04:34:02 +03:00
|
|
|
!(mode & UC_MODE_BIG_ENDIAN)) {
|
2016-01-23 04:17:59 +03:00
|
|
|
free(uc);
|
|
|
|
return UC_ERR_MODE;
|
|
|
|
}
|
2015-08-21 10:04:50 +03:00
|
|
|
uc->init_arch = m68k_uc_init;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef UNICORN_HAS_X86
|
|
|
|
case UC_ARCH_X86:
|
2016-01-23 04:17:59 +03:00
|
|
|
if ((mode & ~UC_MODE_X86_MASK) ||
|
2016-01-23 04:34:02 +03:00
|
|
|
(mode & UC_MODE_BIG_ENDIAN) ||
|
|
|
|
!(mode & (UC_MODE_16|UC_MODE_32|UC_MODE_64))) {
|
2016-01-23 04:17:59 +03:00
|
|
|
free(uc);
|
|
|
|
return UC_ERR_MODE;
|
|
|
|
}
|
2015-08-21 10:04:50 +03:00
|
|
|
uc->init_arch = x86_uc_init;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef UNICORN_HAS_ARM
|
|
|
|
case UC_ARCH_ARM:
|
2016-01-23 04:17:59 +03:00
|
|
|
if ((mode & ~UC_MODE_ARM_MASK) ||
|
2016-01-23 04:34:02 +03:00
|
|
|
(mode & UC_MODE_BIG_ENDIAN)) {
|
2015-08-24 04:50:55 +03:00
|
|
|
free(uc);
|
|
|
|
return UC_ERR_MODE;
|
|
|
|
}
|
2016-01-23 04:17:59 +03:00
|
|
|
uc->init_arch = arm_uc_init;
|
2015-08-24 04:50:55 +03:00
|
|
|
|
2016-01-23 04:08:49 +03:00
|
|
|
if (mode & UC_MODE_THUMB)
|
2015-08-21 10:04:50 +03:00
|
|
|
uc->thumb = 1;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef UNICORN_HAS_ARM64
|
|
|
|
case UC_ARCH_ARM64:
|
2016-01-23 04:17:59 +03:00
|
|
|
if ((mode & ~UC_MODE_ARM_MASK) ||
|
2016-01-23 04:34:02 +03:00
|
|
|
(mode & UC_MODE_BIG_ENDIAN)) {
|
2016-01-23 04:17:59 +03:00
|
|
|
free(uc);
|
|
|
|
return UC_ERR_MODE;
|
|
|
|
}
|
2015-08-21 10:04:50 +03:00
|
|
|
uc->init_arch = arm64_uc_init;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(UNICORN_HAS_MIPS) || defined(UNICORN_HAS_MIPSEL) || defined(UNICORN_HAS_MIPS64) || defined(UNICORN_HAS_MIPS64EL)
|
|
|
|
case UC_ARCH_MIPS:
|
2016-01-23 04:17:59 +03:00
|
|
|
if ((mode & ~UC_MODE_MIPS_MASK) ||
|
2016-01-23 04:34:02 +03:00
|
|
|
!(mode & (UC_MODE_MIPS32|UC_MODE_MIPS64))) {
|
2016-01-23 04:17:59 +03:00
|
|
|
free(uc);
|
|
|
|
return UC_ERR_MODE;
|
|
|
|
}
|
2015-08-21 10:04:50 +03:00
|
|
|
if (mode & UC_MODE_BIG_ENDIAN) {
|
|
|
|
#ifdef UNICORN_HAS_MIPS
|
|
|
|
if (mode & UC_MODE_MIPS32)
|
|
|
|
uc->init_arch = mips_uc_init;
|
|
|
|
#endif
|
|
|
|
#ifdef UNICORN_HAS_MIPS64
|
|
|
|
if (mode & UC_MODE_MIPS64)
|
|
|
|
uc->init_arch = mips64_uc_init;
|
|
|
|
#endif
|
|
|
|
} else { // little endian
|
|
|
|
#ifdef UNICORN_HAS_MIPSEL
|
|
|
|
if (mode & UC_MODE_MIPS32)
|
|
|
|
uc->init_arch = mipsel_uc_init;
|
|
|
|
#endif
|
|
|
|
#ifdef UNICORN_HAS_MIPS64EL
|
|
|
|
if (mode & UC_MODE_MIPS64)
|
|
|
|
uc->init_arch = mips64el_uc_init;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef UNICORN_HAS_SPARC
|
|
|
|
case UC_ARCH_SPARC:
|
2016-01-24 14:27:33 +03:00
|
|
|
if ((mode & ~UC_MODE_SPARC_MASK) ||
|
|
|
|
!(mode & UC_MODE_BIG_ENDIAN) ||
|
|
|
|
!(mode & (UC_MODE_SPARC32|UC_MODE_SPARC64))) {
|
2016-01-23 04:17:59 +03:00
|
|
|
free(uc);
|
|
|
|
return UC_ERR_MODE;
|
|
|
|
}
|
2016-01-23 04:08:49 +03:00
|
|
|
if (mode & UC_MODE_SPARC64)
|
2015-08-21 10:04:50 +03:00
|
|
|
uc->init_arch = sparc64_uc_init;
|
|
|
|
else
|
|
|
|
uc->init_arch = sparc_uc_init;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uc->init_arch == NULL) {
|
|
|
|
return UC_ERR_ARCH;
|
|
|
|
}
|
|
|
|
|
2015-11-11 20:43:41 +03:00
|
|
|
if (machine_initialize(uc))
|
|
|
|
return UC_ERR_RESOURCE;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2015-08-26 13:39:51 +03:00
|
|
|
*result = uc;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
if (uc->reg_reset)
|
2015-08-26 13:39:51 +03:00
|
|
|
uc->reg_reset(uc);
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
uc->hook_size = HOOK_SIZE;
|
|
|
|
uc->hook_callbacks = calloc(1, sizeof(uc->hook_callbacks[0]) * HOOK_SIZE);
|
|
|
|
|
|
|
|
return UC_ERR_OK;
|
|
|
|
} else {
|
|
|
|
return UC_ERR_ARCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UNICORN_EXPORT
|
2015-09-05 06:20:32 +03:00
|
|
|
uc_err uc_close(uc_engine *uc)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
2015-12-11 03:42:31 +03:00
|
|
|
int i;
|
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
if (uc->release)
|
|
|
|
uc->release(uc->tcg_ctx);
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
free(uc->l1_map);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (uc->bounce.buffer) {
|
|
|
|
free(uc->bounce.buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(uc->tcg_ctx);
|
|
|
|
|
|
|
|
free((void*) uc->system_memory->name);
|
|
|
|
g_free(uc->system_memory);
|
|
|
|
g_hash_table_destroy(uc->type_table);
|
|
|
|
|
|
|
|
for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
|
|
|
|
free(uc->ram_list.dirty_memory[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove uc->root (created with object_new())
|
|
|
|
uc->root->free(uc->root);
|
|
|
|
|
|
|
|
free(uc->hook_callbacks);
|
2015-10-31 00:32:59 +03:00
|
|
|
|
2015-08-26 18:41:30 +03:00
|
|
|
free(uc->mapped_blocks);
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
// finally, free uc itself.
|
|
|
|
memset(uc, 0, sizeof(*uc));
|
|
|
|
free(uc);
|
|
|
|
|
|
|
|
return UC_ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UNICORN_EXPORT
|
2015-09-05 06:20:32 +03:00
|
|
|
uc_err uc_reg_read(uc_engine *uc, int regid, void *value)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
|
|
|
if (uc->reg_read)
|
2015-08-26 13:39:51 +03:00
|
|
|
uc->reg_read(uc, regid, value);
|
2015-08-21 10:04:50 +03:00
|
|
|
else
|
|
|
|
return -1; // FIXME: need a proper uc_err
|
|
|
|
|
|
|
|
return UC_ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UNICORN_EXPORT
|
2015-09-05 06:20:32 +03:00
|
|
|
uc_err uc_reg_write(uc_engine *uc, int regid, const void *value)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
|
|
|
if (uc->reg_write)
|
2015-08-26 13:39:51 +03:00
|
|
|
uc->reg_write(uc, regid, value);
|
2015-08-21 10:04:50 +03:00
|
|
|
else
|
|
|
|
return -1; // FIXME: need a proper uc_err
|
|
|
|
|
|
|
|
return UC_ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-29 05:23:53 +03:00
|
|
|
// check if a memory area is mapped
|
|
|
|
// this is complicated because an area can overlap adjacent blocks
|
2015-09-05 06:20:32 +03:00
|
|
|
static bool check_mem_area(uc_engine *uc, uint64_t address, size_t size)
|
2015-08-29 05:23:53 +03:00
|
|
|
{
|
|
|
|
size_t count = 0, len;
|
|
|
|
|
|
|
|
while(count < size) {
|
|
|
|
MemoryRegion *mr = memory_mapping(uc, address);
|
|
|
|
if (mr) {
|
|
|
|
len = MIN(size - count, mr->end - address);
|
|
|
|
count += len;
|
|
|
|
address += len;
|
|
|
|
} else // this address is not mapped in yet
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (count == size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
UNICORN_EXPORT
|
2015-09-07 19:44:03 +03:00
|
|
|
uc_err uc_mem_read(uc_engine *uc, uint64_t address, void *_bytes, size_t size)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
2015-12-28 10:19:30 +03:00
|
|
|
size_t count = 0, len;
|
2015-09-07 19:44:03 +03:00
|
|
|
uint8_t *bytes = _bytes;
|
|
|
|
|
2015-12-28 10:19:30 +03:00
|
|
|
if (uc->mem_redirect) {
|
|
|
|
address = uc->mem_redirect(address);
|
|
|
|
}
|
|
|
|
|
2015-08-29 05:23:53 +03:00
|
|
|
if (!check_mem_area(uc, address, size))
|
2015-09-30 09:46:55 +03:00
|
|
|
return UC_ERR_READ_UNMAPPED;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2015-08-29 05:23:53 +03:00
|
|
|
// memory area can overlap adjacent memory blocks
|
|
|
|
while(count < size) {
|
|
|
|
MemoryRegion *mr = memory_mapping(uc, address);
|
|
|
|
if (mr) {
|
|
|
|
len = MIN(size - count, mr->end - address);
|
|
|
|
if (uc->read_mem(&uc->as, address, bytes, len) == false)
|
|
|
|
break;
|
|
|
|
count += len;
|
|
|
|
address += len;
|
|
|
|
bytes += len;
|
|
|
|
} else // this address is not mapped in yet
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count == size)
|
|
|
|
return UC_ERR_OK;
|
|
|
|
else
|
2015-09-30 09:46:55 +03:00
|
|
|
return UC_ERR_READ_UNMAPPED;
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
UNICORN_EXPORT
|
2015-09-07 19:44:03 +03:00
|
|
|
uc_err uc_mem_write(uc_engine *uc, uint64_t address, const void *_bytes, size_t size)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
2015-12-28 10:19:30 +03:00
|
|
|
size_t count = 0, len;
|
2015-09-07 19:44:03 +03:00
|
|
|
const uint8_t *bytes = _bytes;
|
|
|
|
|
2015-12-28 10:19:30 +03:00
|
|
|
if (uc->mem_redirect) {
|
|
|
|
address = uc->mem_redirect(address);
|
|
|
|
}
|
|
|
|
|
2015-08-29 05:23:53 +03:00
|
|
|
if (!check_mem_area(uc, address, size))
|
2015-09-30 09:46:55 +03:00
|
|
|
return UC_ERR_WRITE_UNMAPPED;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2015-08-29 05:23:53 +03:00
|
|
|
// memory area can overlap adjacent memory blocks
|
|
|
|
while(count < size) {
|
|
|
|
MemoryRegion *mr = memory_mapping(uc, address);
|
|
|
|
if (mr) {
|
|
|
|
uint32_t operms = mr->perms;
|
|
|
|
if (!(operms & UC_PROT_WRITE)) // write protected
|
|
|
|
// but this is not the program accessing memory, so temporarily mark writable
|
|
|
|
uc->readonly_mem(mr, false);
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2015-08-29 05:23:53 +03:00
|
|
|
len = MIN(size - count, mr->end - address);
|
|
|
|
if (uc->write_mem(&uc->as, address, bytes, len) == false)
|
|
|
|
break;
|
2015-08-26 23:29:54 +03:00
|
|
|
|
2015-08-29 05:23:53 +03:00
|
|
|
if (!(operms & UC_PROT_WRITE)) // write protected
|
|
|
|
// now write protect it again
|
|
|
|
uc->readonly_mem(mr, true);
|
|
|
|
|
|
|
|
count += len;
|
|
|
|
address += len;
|
|
|
|
bytes += len;
|
|
|
|
} else // this address is not mapped in yet
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count == size)
|
|
|
|
return UC_ERR_OK;
|
|
|
|
else
|
2015-09-30 09:46:55 +03:00
|
|
|
return UC_ERR_WRITE_UNMAPPED;
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#define TIMEOUT_STEP 2 // microseconds
|
|
|
|
static void *_timeout_fn(void *arg)
|
|
|
|
{
|
2015-09-03 04:44:43 +03:00
|
|
|
struct uc_struct *uc = arg;
|
2015-08-21 10:04:50 +03:00
|
|
|
int64_t current_time = get_clock();
|
|
|
|
|
|
|
|
do {
|
|
|
|
usleep(TIMEOUT_STEP);
|
|
|
|
// perhaps emulation is even done before timeout?
|
|
|
|
if (uc->emulation_done)
|
|
|
|
break;
|
|
|
|
} while(get_clock() - current_time < uc->timeout);
|
|
|
|
|
|
|
|
// timeout before emulation is done?
|
|
|
|
if (!uc->emulation_done) {
|
|
|
|
// force emulation to stop
|
2015-08-26 14:32:05 +03:00
|
|
|
uc_emu_stop(uc);
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-09-05 06:20:32 +03:00
|
|
|
static void enable_emu_timer(uc_engine *uc, uint64_t timeout)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
|
|
|
uc->timeout = timeout;
|
2015-09-02 11:13:12 +03:00
|
|
|
qemu_thread_create(uc, &uc->timer, "timeout", _timeout_fn,
|
2015-08-21 10:04:50 +03:00
|
|
|
uc, QEMU_THREAD_JOINABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
UNICORN_EXPORT
|
2015-09-05 06:20:32 +03:00
|
|
|
uc_err uc_emu_start(uc_engine* uc, uint64_t begin, uint64_t until, uint64_t timeout, size_t count)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
2015-08-24 19:02:31 +03:00
|
|
|
// reset the counter
|
|
|
|
uc->emu_counter = 0;
|
|
|
|
uc->stop_request = false;
|
|
|
|
uc->invalid_error = UC_ERR_OK;
|
2015-08-25 09:50:55 +03:00
|
|
|
uc->block_full = false;
|
2015-08-29 04:22:53 +03:00
|
|
|
uc->emulation_done = false;
|
2015-08-24 19:02:31 +03:00
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
switch(uc->arch) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UC_ARCH_M68K:
|
2015-08-26 13:39:51 +03:00
|
|
|
uc_reg_write(uc, UC_M68K_REG_PC, &begin);
|
2015-08-21 10:04:50 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UC_ARCH_X86:
|
|
|
|
switch(uc->mode) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case UC_MODE_16:
|
2015-08-26 13:39:51 +03:00
|
|
|
uc_reg_write(uc, UC_X86_REG_IP, &begin);
|
2015-08-21 10:04:50 +03:00
|
|
|
break;
|
|
|
|
case UC_MODE_32:
|
2015-08-26 13:39:51 +03:00
|
|
|
uc_reg_write(uc, UC_X86_REG_EIP, &begin);
|
2015-08-21 10:04:50 +03:00
|
|
|
break;
|
|
|
|
case UC_MODE_64:
|
2015-08-26 13:39:51 +03:00
|
|
|
uc_reg_write(uc, UC_X86_REG_RIP, &begin);
|
2015-08-21 10:04:50 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UC_ARCH_ARM:
|
|
|
|
switch(uc->mode) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case UC_MODE_THUMB:
|
|
|
|
case UC_MODE_ARM:
|
2015-08-26 13:39:51 +03:00
|
|
|
uc_reg_write(uc, UC_ARM_REG_R15, &begin);
|
2015-08-21 10:04:50 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UC_ARCH_ARM64:
|
2015-08-26 13:39:51 +03:00
|
|
|
uc_reg_write(uc, UC_ARM64_REG_PC, &begin);
|
2015-08-21 10:04:50 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UC_ARCH_MIPS:
|
|
|
|
// TODO: MIPS32/MIPS64/BIGENDIAN etc
|
2015-08-26 13:39:51 +03:00
|
|
|
uc_reg_write(uc, UC_MIPS_REG_PC, &begin);
|
2015-08-21 10:04:50 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UC_ARCH_SPARC:
|
|
|
|
// TODO: Sparc/Sparc64
|
2015-08-26 13:39:51 +03:00
|
|
|
uc_reg_write(uc, UC_SPARC_REG_PC, &begin);
|
2015-08-21 10:04:50 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
uc->emu_count = count;
|
|
|
|
if (count > 0) {
|
|
|
|
uc->hook_insn = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uc->addr_end = until;
|
|
|
|
|
2015-11-11 20:43:41 +03:00
|
|
|
if (uc->vm_start(uc)) {
|
|
|
|
return UC_ERR_RESOURCE;
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
if (timeout)
|
2015-08-26 13:39:51 +03:00
|
|
|
enable_emu_timer(uc, timeout * 1000); // microseconds -> nanoseconds
|
2015-08-21 10:04:50 +03:00
|
|
|
uc->pause_all_vcpus(uc);
|
|
|
|
// emulation is done
|
|
|
|
uc->emulation_done = true;
|
|
|
|
|
2015-08-30 00:12:04 +03:00
|
|
|
if (timeout) {
|
|
|
|
// wait for the timer to finish
|
|
|
|
qemu_thread_join(&uc->timer);
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
return uc->invalid_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UNICORN_EXPORT
|
2015-09-05 06:20:32 +03:00
|
|
|
uc_err uc_emu_stop(uc_engine *uc)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
2015-08-29 04:10:18 +03:00
|
|
|
if (uc->emulation_done)
|
|
|
|
return UC_ERR_OK;
|
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
uc->stop_request = true;
|
2015-11-03 16:34:31 +03:00
|
|
|
if (uc->current_cpu) {
|
|
|
|
// exit the current TB
|
|
|
|
cpu_exit(uc->current_cpu);
|
|
|
|
}
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
return UC_ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-05 06:20:32 +03:00
|
|
|
static int _hook_code(uc_engine *uc, int type, uint64_t begin, uint64_t end,
|
|
|
|
void *callback, void *user_data, uc_hook *hh)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2015-08-26 13:39:51 +03:00
|
|
|
i = hook_add(uc, type, begin, end, callback, user_data);
|
2015-08-21 10:04:50 +03:00
|
|
|
if (i == 0)
|
2015-10-10 13:01:47 +03:00
|
|
|
return UC_ERR_NOMEM;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2015-08-26 14:32:05 +03:00
|
|
|
*hh = i;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
return UC_ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-05 06:20:32 +03:00
|
|
|
static uc_err _hook_mem_access(uc_engine *uc, uc_hook_type type,
|
2015-08-21 10:04:50 +03:00
|
|
|
uint64_t begin, uint64_t end,
|
2015-09-05 06:20:32 +03:00
|
|
|
void *callback, void *user_data, uc_hook *hh)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2015-08-26 13:39:51 +03:00
|
|
|
i = hook_add(uc, type, begin, end, callback, user_data);
|
2015-08-21 10:04:50 +03:00
|
|
|
if (i == 0)
|
2015-10-10 13:01:47 +03:00
|
|
|
return UC_ERR_NOMEM;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2015-08-26 14:32:05 +03:00
|
|
|
*hh = i;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
return UC_ERR_OK;
|
|
|
|
}
|
|
|
|
|
2015-12-30 04:17:47 +03:00
|
|
|
// find if a memory range overlaps with existing mapped regions
|
|
|
|
static bool memory_overlap(struct uc_struct *uc, uint64_t begin, size_t size)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
uint64_t end = begin + size - 1;
|
|
|
|
|
|
|
|
for(i = 0; i < uc->mapped_block_count; i++) {
|
|
|
|
// begin address falls inside this region?
|
|
|
|
if (begin >= uc->mapped_blocks[i]->addr && begin <= uc->mapped_blocks[i]->end - 1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// end address falls inside this region?
|
|
|
|
if (end >= uc->mapped_blocks[i]->addr && end <= uc->mapped_blocks[i]->end - 1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// this region falls totally inside this range?
|
|
|
|
if (begin < uc->mapped_blocks[i]->addr && end > uc->mapped_blocks[i]->end - 1)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// not found
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-28 04:25:53 +03:00
|
|
|
// common setup/error checking shared between uc_mem_map and uc_mem_map_ptr
|
2015-11-28 13:26:08 +03:00
|
|
|
static uc_err mem_map(uc_engine *uc, uint64_t address, size_t size, uint32_t perms, MemoryRegion *block)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
2015-08-28 09:19:32 +03:00
|
|
|
MemoryRegion **regions;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2015-12-30 04:17:47 +03:00
|
|
|
// this area overlaps existing mapped regions?
|
|
|
|
if (memory_overlap(uc, address, size))
|
|
|
|
return UC_ERR_MAP;
|
|
|
|
|
2015-11-28 13:26:08 +03:00
|
|
|
if (block == NULL)
|
|
|
|
return UC_ERR_NOMEM;
|
|
|
|
|
2015-08-26 09:08:18 +03:00
|
|
|
if ((uc->mapped_block_count & (MEM_BLOCK_INCR - 1)) == 0) { //time to grow
|
2015-09-04 04:20:13 +03:00
|
|
|
regions = (MemoryRegion**)realloc(uc->mapped_blocks,
|
|
|
|
sizeof(MemoryRegion*) * (uc->mapped_block_count + MEM_BLOCK_INCR));
|
2015-08-28 09:19:32 +03:00
|
|
|
if (regions == NULL) {
|
2015-09-01 23:40:19 +03:00
|
|
|
return UC_ERR_NOMEM;
|
2015-08-26 09:08:18 +03:00
|
|
|
}
|
2015-08-28 09:19:32 +03:00
|
|
|
uc->mapped_blocks = regions;
|
2015-08-26 07:52:18 +03:00
|
|
|
}
|
2015-11-10 06:44:29 +03:00
|
|
|
|
2015-11-28 04:25:53 +03:00
|
|
|
uc->mapped_blocks[uc->mapped_block_count] = block;
|
2015-08-26 07:52:18 +03:00
|
|
|
uc->mapped_block_count++;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
return UC_ERR_OK;
|
|
|
|
}
|
|
|
|
|
2016-01-11 19:59:56 +03:00
|
|
|
static uc_err mem_map_check(uc_engine *uc, uint64_t address, size_t size, uint32_t perms)
|
2015-11-28 04:25:53 +03:00
|
|
|
{
|
2016-01-11 19:26:23 +03:00
|
|
|
if (size == 0)
|
|
|
|
// invalid memory mapping
|
|
|
|
return UC_ERR_ARG;
|
|
|
|
|
|
|
|
// address cannot wrapp around
|
|
|
|
if (address + size - 1 < address)
|
|
|
|
return UC_ERR_ARG;
|
|
|
|
|
|
|
|
// address must be aligned to uc->target_page_size
|
|
|
|
if ((address & uc->target_page_align) != 0)
|
|
|
|
return UC_ERR_ARG;
|
|
|
|
|
|
|
|
// size must be multiple of uc->target_page_size
|
|
|
|
if ((size & uc->target_page_align) != 0)
|
|
|
|
return UC_ERR_ARG;
|
|
|
|
|
|
|
|
// check for only valid permissions
|
|
|
|
if ((perms & ~UC_PROT_ALL) != 0)
|
|
|
|
return UC_ERR_ARG;
|
|
|
|
|
2016-01-11 19:59:56 +03:00
|
|
|
return UC_ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
UNICORN_EXPORT
|
|
|
|
uc_err uc_mem_map(uc_engine *uc, uint64_t address, size_t size, uint32_t perms)
|
|
|
|
{
|
|
|
|
uc_err res;
|
|
|
|
|
|
|
|
if (uc->mem_redirect) {
|
|
|
|
address = uc->mem_redirect(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = mem_map_check(uc, address, size, perms);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
2015-11-28 13:26:08 +03:00
|
|
|
return mem_map(uc, address, size, perms, uc->memory_map(uc, address, size, perms));
|
2015-11-28 04:25:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
UNICORN_EXPORT
|
2015-11-28 12:36:11 +03:00
|
|
|
uc_err uc_mem_map_ptr(uc_engine *uc, uint64_t address, size_t size, uint32_t perms, void *ptr)
|
2015-11-28 04:25:53 +03:00
|
|
|
{
|
2016-01-11 19:59:56 +03:00
|
|
|
uc_err res;
|
|
|
|
|
2015-11-28 04:25:53 +03:00
|
|
|
if (ptr == NULL)
|
|
|
|
return UC_ERR_ARG;
|
|
|
|
|
2015-12-28 10:19:30 +03:00
|
|
|
if (uc->mem_redirect) {
|
|
|
|
address = uc->mem_redirect(address);
|
|
|
|
}
|
|
|
|
|
2016-01-11 19:59:56 +03:00
|
|
|
res = mem_map_check(uc, address, size, perms);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
2015-11-28 12:36:11 +03:00
|
|
|
return mem_map(uc, address, size, UC_PROT_ALL, uc->memory_map_ptr(uc, address, size, perms, ptr));
|
2015-11-28 04:25:53 +03:00
|
|
|
}
|
|
|
|
|
2015-09-03 20:02:38 +03:00
|
|
|
// Create a backup copy of the indicated MemoryRegion.
|
|
|
|
// Generally used in prepartion for splitting a MemoryRegion.
|
2015-09-04 05:15:49 +03:00
|
|
|
static uint8_t *copy_region(struct uc_struct *uc, MemoryRegion *mr)
|
2015-08-30 10:22:18 +03:00
|
|
|
{
|
|
|
|
uint8_t *block = (uint8_t *)malloc(int128_get64(mr->size));
|
|
|
|
if (block != NULL) {
|
2015-09-04 05:15:49 +03:00
|
|
|
uc_err err = uc_mem_read(uc, mr->addr, block, int128_get64(mr->size));
|
2015-08-30 10:22:18 +03:00
|
|
|
if (err != UC_ERR_OK) {
|
|
|
|
free(block);
|
|
|
|
block = NULL;
|
|
|
|
}
|
|
|
|
}
|
2015-09-03 20:02:38 +03:00
|
|
|
|
2015-08-30 10:22:18 +03:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-09-03 13:16:49 +03:00
|
|
|
Split the given MemoryRegion at the indicated address for the indicated size
|
|
|
|
this may result in the create of up to 3 spanning sections. If the delete
|
|
|
|
parameter is true, the no new section will be created to replace the indicate
|
|
|
|
range. This functions exists to support uc_mem_protect and uc_mem_unmap.
|
|
|
|
|
2015-10-31 00:32:59 +03:00
|
|
|
This is a static function and callers have already done some preliminary
|
2015-09-03 13:16:49 +03:00
|
|
|
parameter validation.
|
2015-10-31 00:32:59 +03:00
|
|
|
|
2015-09-03 22:26:36 +03:00
|
|
|
The do_delete argument indicates that we are being called to support
|
|
|
|
uc_mem_unmap. In this case we save some time by choosing NOT to remap
|
|
|
|
the areas that are intended to get unmapped
|
2015-09-03 13:16:49 +03:00
|
|
|
*/
|
2015-09-03 20:02:38 +03:00
|
|
|
// TODO: investigate whether qemu region manipulation functions already offered
|
|
|
|
// this capability
|
2015-09-04 05:15:49 +03:00
|
|
|
static bool split_region(struct uc_struct *uc, MemoryRegion *mr, uint64_t address,
|
2015-09-03 22:26:36 +03:00
|
|
|
size_t size, bool do_delete)
|
2015-08-30 10:22:18 +03:00
|
|
|
{
|
|
|
|
uint8_t *backup;
|
|
|
|
uint32_t perms;
|
|
|
|
uint64_t begin, end, chunk_end;
|
|
|
|
size_t l_size, m_size, r_size;
|
2015-09-03 20:02:38 +03:00
|
|
|
|
2015-08-30 10:22:18 +03:00
|
|
|
chunk_end = address + size;
|
2015-09-04 04:20:13 +03:00
|
|
|
|
|
|
|
// if this region belongs to area [address, address+size],
|
|
|
|
// then there is no work to do.
|
2015-09-03 22:26:36 +03:00
|
|
|
if (address <= mr->addr && chunk_end >= mr->end)
|
2015-08-30 10:22:18 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (size == 0)
|
2015-09-04 04:20:13 +03:00
|
|
|
// trivial case
|
2015-08-30 10:22:18 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (address >= mr->end || chunk_end <= mr->addr)
|
2015-09-04 04:20:13 +03:00
|
|
|
// impossible case
|
2015-08-30 10:22:18 +03:00
|
|
|
return false;
|
|
|
|
|
2015-09-04 05:15:49 +03:00
|
|
|
backup = copy_region(uc, mr);
|
2015-08-30 10:22:18 +03:00
|
|
|
if (backup == NULL)
|
|
|
|
return false;
|
|
|
|
|
2015-09-04 04:20:13 +03:00
|
|
|
// save the essential information required for the split before mr gets deleted
|
2015-08-30 10:22:18 +03:00
|
|
|
perms = mr->perms;
|
|
|
|
begin = mr->addr;
|
|
|
|
end = mr->end;
|
2015-09-03 13:16:49 +03:00
|
|
|
|
2015-09-04 04:20:13 +03:00
|
|
|
// unmap this region first, then do split it later
|
2015-09-04 05:15:49 +03:00
|
|
|
if (uc_mem_unmap(uc, mr->addr, int128_get64(mr->size)) != UC_ERR_OK)
|
2015-08-30 10:22:18 +03:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* overlapping cases
|
|
|
|
* |------mr------|
|
|
|
|
* case 1 |---size--|
|
|
|
|
* case 2 |--size--|
|
|
|
|
* case 3 |---size--|
|
|
|
|
*/
|
|
|
|
|
2015-09-04 04:20:13 +03:00
|
|
|
// adjust some things
|
2015-08-30 10:22:18 +03:00
|
|
|
if (address < begin)
|
|
|
|
address = begin;
|
|
|
|
if (chunk_end > end)
|
|
|
|
chunk_end = end;
|
|
|
|
|
2015-09-04 04:20:13 +03:00
|
|
|
// compute sub region sizes
|
2015-08-30 10:22:18 +03:00
|
|
|
l_size = (size_t)(address - begin);
|
|
|
|
r_size = (size_t)(end - chunk_end);
|
|
|
|
m_size = (size_t)(chunk_end - address);
|
|
|
|
|
2015-09-03 20:02:38 +03:00
|
|
|
// If there are error in any of the below operations, things are too far gone
|
|
|
|
// at that point to recover. Could try to remap orignal region, but these smaller
|
|
|
|
// allocation just failed so no guarantee that we can recover the original
|
|
|
|
// allocation at this point
|
2015-08-30 10:22:18 +03:00
|
|
|
if (l_size > 0) {
|
2015-09-04 05:15:49 +03:00
|
|
|
if (uc_mem_map(uc, begin, l_size, perms) != UC_ERR_OK)
|
2015-08-30 10:22:18 +03:00
|
|
|
goto error;
|
2015-09-04 05:15:49 +03:00
|
|
|
if (uc_mem_write(uc, begin, backup, l_size) != UC_ERR_OK)
|
2015-08-30 10:22:18 +03:00
|
|
|
goto error;
|
|
|
|
}
|
2015-09-04 04:20:13 +03:00
|
|
|
|
2015-09-03 22:26:36 +03:00
|
|
|
if (m_size > 0 && !do_delete) {
|
2015-09-04 05:15:49 +03:00
|
|
|
if (uc_mem_map(uc, address, m_size, perms) != UC_ERR_OK)
|
2015-08-30 10:22:18 +03:00
|
|
|
goto error;
|
2015-09-04 05:15:49 +03:00
|
|
|
if (uc_mem_write(uc, address, backup + l_size, m_size) != UC_ERR_OK)
|
2015-08-30 10:22:18 +03:00
|
|
|
goto error;
|
|
|
|
}
|
2015-09-04 04:20:13 +03:00
|
|
|
|
2015-08-30 10:22:18 +03:00
|
|
|
if (r_size > 0) {
|
2015-09-04 05:15:49 +03:00
|
|
|
if (uc_mem_map(uc, chunk_end, r_size, perms) != UC_ERR_OK)
|
2015-08-30 10:22:18 +03:00
|
|
|
goto error;
|
2015-09-04 05:15:49 +03:00
|
|
|
if (uc_mem_write(uc, chunk_end, backup + l_size + m_size, r_size) != UC_ERR_OK)
|
2015-08-30 10:22:18 +03:00
|
|
|
goto error;
|
|
|
|
}
|
2015-09-04 04:20:13 +03:00
|
|
|
|
2015-08-30 10:22:18 +03:00
|
|
|
return true;
|
2015-09-04 04:20:13 +03:00
|
|
|
|
2015-08-30 10:22:18 +03:00
|
|
|
error:
|
|
|
|
free(backup);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-29 04:59:45 +03:00
|
|
|
UNICORN_EXPORT
|
2015-09-04 05:15:49 +03:00
|
|
|
uc_err uc_mem_protect(struct uc_struct *uc, uint64_t address, size_t size, uint32_t perms)
|
2015-08-29 04:59:45 +03:00
|
|
|
{
|
2015-08-30 07:17:30 +03:00
|
|
|
MemoryRegion *mr;
|
2015-09-03 20:02:38 +03:00
|
|
|
uint64_t addr = address;
|
|
|
|
size_t count, len;
|
2015-08-29 04:59:45 +03:00
|
|
|
|
2015-08-30 07:17:30 +03:00
|
|
|
if (size == 0)
|
2015-09-01 23:40:19 +03:00
|
|
|
// trivial case, no change
|
|
|
|
return UC_ERR_OK;
|
2015-08-29 04:59:45 +03:00
|
|
|
|
2015-08-31 11:00:44 +03:00
|
|
|
// address must be aligned to uc->target_page_size
|
|
|
|
if ((address & uc->target_page_align) != 0)
|
2015-09-09 11:54:47 +03:00
|
|
|
return UC_ERR_ARG;
|
2015-08-29 04:59:45 +03:00
|
|
|
|
2015-08-31 11:00:44 +03:00
|
|
|
// size must be multiple of uc->target_page_size
|
|
|
|
if ((size & uc->target_page_align) != 0)
|
2015-09-09 11:54:47 +03:00
|
|
|
return UC_ERR_ARG;
|
2015-08-29 04:59:45 +03:00
|
|
|
|
|
|
|
// check for only valid permissions
|
2015-08-31 00:01:07 +03:00
|
|
|
if ((perms & ~UC_PROT_ALL) != 0)
|
2015-09-09 11:54:47 +03:00
|
|
|
return UC_ERR_ARG;
|
2015-09-03 13:16:49 +03:00
|
|
|
|
2015-12-28 10:19:30 +03:00
|
|
|
if (uc->mem_redirect) {
|
|
|
|
address = uc->mem_redirect(address);
|
|
|
|
}
|
|
|
|
|
2015-09-03 22:26:36 +03:00
|
|
|
// check that user's entire requested block is mapped
|
2015-08-30 07:17:30 +03:00
|
|
|
if (!check_mem_area(uc, address, size))
|
2015-09-01 23:40:19 +03:00
|
|
|
return UC_ERR_NOMEM;
|
2015-08-29 04:59:45 +03:00
|
|
|
|
2015-09-03 20:02:38 +03:00
|
|
|
// Now we know entire region is mapped, so change permissions
|
|
|
|
// We may need to split regions if this area spans adjacent regions
|
|
|
|
addr = address;
|
|
|
|
count = 0;
|
|
|
|
while(count < size) {
|
|
|
|
mr = memory_mapping(uc, addr);
|
|
|
|
len = MIN(size - count, mr->end - addr);
|
2015-09-04 05:15:49 +03:00
|
|
|
if (!split_region(uc, mr, addr, len, false))
|
2015-09-01 23:40:19 +03:00
|
|
|
return UC_ERR_NOMEM;
|
2015-09-03 20:02:38 +03:00
|
|
|
|
|
|
|
mr = memory_mapping(uc, addr);
|
|
|
|
mr->perms = perms;
|
|
|
|
uc->readonly_mem(mr, (perms & UC_PROT_WRITE) == 0);
|
2015-09-03 22:26:36 +03:00
|
|
|
|
2015-09-03 20:02:38 +03:00
|
|
|
count += len;
|
|
|
|
addr += len;
|
2015-08-31 00:01:07 +03:00
|
|
|
}
|
|
|
|
return UC_ERR_OK;
|
2015-08-30 07:17:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
UNICORN_EXPORT
|
2015-09-04 05:15:49 +03:00
|
|
|
uc_err uc_mem_unmap(struct uc_struct *uc, uint64_t address, size_t size)
|
2015-08-30 07:17:30 +03:00
|
|
|
{
|
|
|
|
MemoryRegion *mr;
|
2015-09-03 20:02:38 +03:00
|
|
|
uint64_t addr;
|
|
|
|
size_t count, len;
|
2015-08-30 07:17:30 +03:00
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
// nothing to unmap
|
|
|
|
return UC_ERR_OK;
|
|
|
|
|
2015-08-31 11:00:44 +03:00
|
|
|
// address must be aligned to uc->target_page_size
|
|
|
|
if ((address & uc->target_page_align) != 0)
|
2015-09-09 11:54:47 +03:00
|
|
|
return UC_ERR_ARG;
|
2015-08-30 07:17:30 +03:00
|
|
|
|
2015-08-31 11:00:44 +03:00
|
|
|
// size must be multiple of uc->target_page_size
|
|
|
|
if ((size & uc->target_page_align) != 0)
|
2015-08-30 07:17:30 +03:00
|
|
|
return UC_ERR_MAP;
|
|
|
|
|
2015-12-28 10:19:30 +03:00
|
|
|
if (uc->mem_redirect) {
|
|
|
|
address = uc->mem_redirect(address);
|
|
|
|
}
|
|
|
|
|
2015-09-03 22:26:36 +03:00
|
|
|
// check that user's entire requested block is mapped
|
2015-08-30 07:17:30 +03:00
|
|
|
if (!check_mem_area(uc, address, size))
|
2015-09-01 23:40:19 +03:00
|
|
|
return UC_ERR_NOMEM;
|
2015-08-30 07:17:30 +03:00
|
|
|
|
2015-09-03 22:26:36 +03:00
|
|
|
// Now we know entire region is mapped, so do the unmap
|
2015-09-03 20:02:38 +03:00
|
|
|
// We may need to split regions if this area spans adjacent regions
|
|
|
|
addr = address;
|
|
|
|
count = 0;
|
|
|
|
while(count < size) {
|
|
|
|
mr = memory_mapping(uc, addr);
|
|
|
|
len = MIN(size - count, mr->end - addr);
|
2015-09-04 05:15:49 +03:00
|
|
|
if (!split_region(uc, mr, addr, len, true))
|
2015-09-03 20:02:38 +03:00
|
|
|
return UC_ERR_NOMEM;
|
2015-10-27 20:26:59 +03:00
|
|
|
|
2015-09-04 04:20:13 +03:00
|
|
|
// if we can retrieve the mapping, then no splitting took place
|
2015-09-03 22:26:36 +03:00
|
|
|
// so unmap here
|
2015-09-03 20:02:38 +03:00
|
|
|
mr = memory_mapping(uc, addr);
|
2015-09-03 22:26:36 +03:00
|
|
|
if (mr != NULL)
|
|
|
|
uc->memory_unmap(uc, mr);
|
2015-09-03 20:02:38 +03:00
|
|
|
count += len;
|
|
|
|
addr += len;
|
2015-08-30 07:17:30 +03:00
|
|
|
}
|
2015-10-27 20:26:59 +03:00
|
|
|
|
2015-08-30 10:22:18 +03:00
|
|
|
return UC_ERR_OK;
|
2015-08-28 09:19:32 +03:00
|
|
|
}
|
|
|
|
|
2015-12-30 04:17:47 +03:00
|
|
|
// find the memory region of this address
|
2015-08-28 04:03:17 +03:00
|
|
|
MemoryRegion *memory_mapping(struct uc_struct* uc, uint64_t address)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2015-09-21 17:17:26 +03:00
|
|
|
if (uc->mapped_block_count == 0)
|
|
|
|
return NULL;
|
|
|
|
|
2015-10-27 09:37:03 +03:00
|
|
|
if (uc->mem_redirect) {
|
|
|
|
address = uc->mem_redirect(address);
|
|
|
|
}
|
|
|
|
|
2015-09-04 10:40:47 +03:00
|
|
|
// try with the cache index first
|
|
|
|
i = uc->mapped_block_cache_index;
|
|
|
|
|
2015-10-21 23:25:49 +03:00
|
|
|
if (i < uc->mapped_block_count && address >= uc->mapped_blocks[i]->addr && address < uc->mapped_blocks[i]->end)
|
2015-09-04 10:40:47 +03:00
|
|
|
return uc->mapped_blocks[i];
|
|
|
|
|
2015-08-26 07:52:18 +03:00
|
|
|
for(i = 0; i < uc->mapped_block_count; i++) {
|
2015-12-11 20:37:13 +03:00
|
|
|
if (address >= uc->mapped_blocks[i]->addr && address <= uc->mapped_blocks[i]->end - 1) {
|
2015-09-04 10:40:47 +03:00
|
|
|
// cache this index for the next query
|
|
|
|
uc->mapped_block_cache_index = i;
|
2015-08-28 09:19:32 +03:00
|
|
|
return uc->mapped_blocks[i];
|
2015-09-04 10:40:47 +03:00
|
|
|
}
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// not found
|
2015-08-28 04:03:17 +03:00
|
|
|
return NULL;
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
|
2015-09-24 09:18:02 +03:00
|
|
|
static uc_err _hook_mem_invalid(struct uc_struct* uc, int type, uc_cb_eventmem_t callback,
|
2015-09-05 06:20:32 +03:00
|
|
|
void *user_data, uc_hook *evh)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2015-10-10 13:01:47 +03:00
|
|
|
// only one event handler at the same time
|
|
|
|
if ((type & UC_HOOK_MEM_READ_UNMAPPED) != 0 && (uc->hook_mem_read_idx != 0))
|
|
|
|
return UC_ERR_HOOK_EXIST;
|
|
|
|
|
|
|
|
if ((type & UC_HOOK_MEM_READ_PROT) != 0 && (uc->hook_mem_read_prot_idx != 0))
|
|
|
|
return UC_ERR_HOOK_EXIST;
|
|
|
|
|
|
|
|
if ((type & UC_HOOK_MEM_WRITE_UNMAPPED) != 0 && (uc->hook_mem_write_idx != 0))
|
|
|
|
return UC_ERR_HOOK_EXIST;
|
|
|
|
|
|
|
|
if ((type & UC_HOOK_MEM_WRITE_PROT) != 0 && (uc->hook_mem_write_prot_idx != 0))
|
|
|
|
return UC_ERR_HOOK_EXIST;
|
|
|
|
|
|
|
|
if ((type & UC_HOOK_MEM_FETCH_UNMAPPED) != 0 && (uc->hook_mem_fetch_idx != 0))
|
|
|
|
return UC_ERR_HOOK_EXIST;
|
|
|
|
|
|
|
|
if ((type & UC_HOOK_MEM_FETCH_PROT) != 0 && (uc->hook_mem_fetch_prot_idx != 0))
|
|
|
|
return UC_ERR_HOOK_EXIST;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
i = hook_find_new(uc);
|
|
|
|
if (i) {
|
|
|
|
uc->hook_callbacks[i].callback = callback;
|
|
|
|
uc->hook_callbacks[i].user_data = user_data;
|
|
|
|
*evh = i;
|
2015-09-30 09:46:55 +03:00
|
|
|
if (type & UC_HOOK_MEM_READ_UNMAPPED)
|
2015-09-24 09:18:02 +03:00
|
|
|
uc->hook_mem_read_idx = i;
|
|
|
|
if (type & UC_HOOK_MEM_READ_PROT)
|
|
|
|
uc->hook_mem_read_prot_idx = i;
|
2015-09-30 09:46:55 +03:00
|
|
|
if (type & UC_HOOK_MEM_WRITE_UNMAPPED)
|
2015-09-24 09:18:02 +03:00
|
|
|
uc->hook_mem_write_idx = i;
|
|
|
|
if (type & UC_HOOK_MEM_WRITE_PROT)
|
|
|
|
uc->hook_mem_write_prot_idx = i;
|
2015-09-30 09:46:55 +03:00
|
|
|
if (type & UC_HOOK_MEM_FETCH_UNMAPPED)
|
2015-09-24 09:18:02 +03:00
|
|
|
uc->hook_mem_fetch_idx = i;
|
|
|
|
if (type & UC_HOOK_MEM_FETCH_PROT)
|
|
|
|
uc->hook_mem_fetch_prot_idx = i;
|
2015-08-21 10:04:50 +03:00
|
|
|
return UC_ERR_OK;
|
|
|
|
} else
|
2015-09-01 23:40:19 +03:00
|
|
|
return UC_ERR_NOMEM;
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static uc_err _hook_intr(struct uc_struct* uc, void *callback,
|
2015-09-05 06:20:32 +03:00
|
|
|
void *user_data, uc_hook *evh)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2015-10-10 13:01:47 +03:00
|
|
|
// only one event handler at the same time
|
|
|
|
if (uc->hook_intr_idx)
|
|
|
|
return UC_ERR_HOOK_EXIST;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
i = hook_find_new(uc);
|
|
|
|
if (i) {
|
|
|
|
uc->hook_callbacks[i].callback = callback;
|
|
|
|
uc->hook_callbacks[i].user_data = user_data;
|
|
|
|
*evh = i;
|
|
|
|
uc->hook_intr_idx = i;
|
|
|
|
return UC_ERR_OK;
|
|
|
|
} else
|
2015-09-01 23:40:19 +03:00
|
|
|
return UC_ERR_NOMEM;
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static uc_err _hook_insn(struct uc_struct *uc, unsigned int insn_id, void *callback,
|
2015-09-05 06:20:32 +03:00
|
|
|
void *user_data, uc_hook *evh)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
switch(uc->arch) {
|
|
|
|
default: break;
|
|
|
|
case UC_ARCH_X86:
|
|
|
|
switch(insn_id) {
|
|
|
|
default: break;
|
2015-08-24 07:36:33 +03:00
|
|
|
case UC_X86_INS_OUT:
|
2015-10-10 13:01:47 +03:00
|
|
|
// only one event handler at the same time
|
|
|
|
if (uc->hook_out_idx)
|
|
|
|
return UC_ERR_HOOK_EXIST;
|
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
i = hook_find_new(uc);
|
|
|
|
if (i) {
|
|
|
|
uc->hook_callbacks[i].callback = callback;
|
|
|
|
uc->hook_callbacks[i].user_data = user_data;
|
|
|
|
*evh = i;
|
|
|
|
uc->hook_out_idx = i;
|
|
|
|
return UC_ERR_OK;
|
|
|
|
} else
|
2015-09-01 23:40:19 +03:00
|
|
|
return UC_ERR_NOMEM;
|
2015-08-24 07:36:33 +03:00
|
|
|
case UC_X86_INS_IN:
|
2015-10-10 13:01:47 +03:00
|
|
|
// only one event handler at the same time
|
|
|
|
if (uc->hook_in_idx)
|
|
|
|
return UC_ERR_HOOK_EXIST;
|
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
i = hook_find_new(uc);
|
|
|
|
if (i) {
|
|
|
|
uc->hook_callbacks[i].callback = callback;
|
|
|
|
uc->hook_callbacks[i].user_data = user_data;
|
|
|
|
*evh = i;
|
|
|
|
uc->hook_in_idx = i;
|
|
|
|
return UC_ERR_OK;
|
|
|
|
} else
|
2015-09-01 23:40:19 +03:00
|
|
|
return UC_ERR_NOMEM;
|
2015-08-24 07:36:33 +03:00
|
|
|
case UC_X86_INS_SYSCALL:
|
|
|
|
case UC_X86_INS_SYSENTER:
|
2015-10-10 13:01:47 +03:00
|
|
|
// only one event handler at the same time
|
|
|
|
if (uc->hook_syscall_idx)
|
|
|
|
return UC_ERR_HOOK_EXIST;
|
|
|
|
|
2015-08-22 20:19:40 +03:00
|
|
|
i = hook_find_new(uc);
|
|
|
|
if (i) {
|
|
|
|
uc->hook_callbacks[i].callback = callback;
|
|
|
|
uc->hook_callbacks[i].user_data = user_data;
|
|
|
|
*evh = i;
|
|
|
|
uc->hook_syscall_idx = i;
|
|
|
|
return UC_ERR_OK;
|
|
|
|
} else
|
2015-09-01 23:40:19 +03:00
|
|
|
return UC_ERR_NOMEM;
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return UC_ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
UNICORN_EXPORT
|
2015-09-24 09:18:02 +03:00
|
|
|
uc_err uc_hook_add(uc_engine *uc, uc_hook *hh, int type, void *callback, void *user_data, ...)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
|
|
|
va_list valist;
|
|
|
|
int ret = UC_ERR_OK;
|
|
|
|
int id;
|
|
|
|
uint64_t begin, end;
|
|
|
|
|
|
|
|
va_start(valist, user_data);
|
|
|
|
|
2015-09-30 09:46:55 +03:00
|
|
|
if (type & UC_HOOK_MEM_READ_UNMAPPED)
|
|
|
|
ret = _hook_mem_invalid(uc, UC_HOOK_MEM_READ_UNMAPPED, callback, user_data, hh);
|
2015-09-24 09:18:02 +03:00
|
|
|
|
2015-09-30 09:46:55 +03:00
|
|
|
if (type & UC_HOOK_MEM_WRITE_UNMAPPED)
|
|
|
|
ret = _hook_mem_invalid(uc, UC_HOOK_MEM_WRITE_UNMAPPED, callback, user_data, hh);
|
2015-09-24 09:18:02 +03:00
|
|
|
|
2015-09-30 09:46:55 +03:00
|
|
|
if (type & UC_HOOK_MEM_FETCH_UNMAPPED)
|
|
|
|
ret = _hook_mem_invalid(uc, UC_HOOK_MEM_FETCH_UNMAPPED, callback, user_data, hh);
|
2015-09-24 09:18:02 +03:00
|
|
|
|
|
|
|
if (type & UC_HOOK_MEM_READ_PROT)
|
|
|
|
ret = _hook_mem_invalid(uc, UC_HOOK_MEM_READ_PROT, callback, user_data, hh);
|
|
|
|
|
|
|
|
if (type & UC_HOOK_MEM_WRITE_PROT)
|
|
|
|
ret = _hook_mem_invalid(uc, UC_HOOK_MEM_WRITE_PROT, callback, user_data, hh);
|
|
|
|
|
|
|
|
if (type & UC_HOOK_MEM_FETCH_PROT)
|
|
|
|
ret = _hook_mem_invalid(uc, UC_HOOK_MEM_FETCH_PROT, callback, user_data, hh);
|
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
switch(type) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case UC_HOOK_INTR:
|
2015-08-26 14:32:05 +03:00
|
|
|
ret = _hook_intr(uc, callback, user_data, hh);
|
2015-08-21 10:04:50 +03:00
|
|
|
break;
|
|
|
|
case UC_HOOK_INSN:
|
|
|
|
id = va_arg(valist, int);
|
2015-08-26 14:32:05 +03:00
|
|
|
ret = _hook_insn(uc, id, callback, user_data, hh);
|
2015-08-21 10:04:50 +03:00
|
|
|
break;
|
|
|
|
case UC_HOOK_CODE:
|
|
|
|
begin = va_arg(valist, uint64_t);
|
|
|
|
end = va_arg(valist, uint64_t);
|
2015-08-26 14:32:05 +03:00
|
|
|
ret = _hook_code(uc, UC_HOOK_CODE, begin, end, callback, user_data, hh);
|
2015-08-21 10:04:50 +03:00
|
|
|
break;
|
|
|
|
case UC_HOOK_BLOCK:
|
|
|
|
begin = va_arg(valist, uint64_t);
|
|
|
|
end = va_arg(valist, uint64_t);
|
2015-08-26 14:32:05 +03:00
|
|
|
ret = _hook_code(uc, UC_HOOK_BLOCK, begin, end, callback, user_data, hh);
|
2015-08-21 10:04:50 +03:00
|
|
|
break;
|
|
|
|
case UC_HOOK_MEM_READ:
|
|
|
|
begin = va_arg(valist, uint64_t);
|
|
|
|
end = va_arg(valist, uint64_t);
|
2015-09-03 04:04:43 +03:00
|
|
|
ret = _hook_mem_access(uc, UC_HOOK_MEM_READ, begin, end, callback, user_data, hh);
|
2015-08-21 10:04:50 +03:00
|
|
|
break;
|
|
|
|
case UC_HOOK_MEM_WRITE:
|
|
|
|
begin = va_arg(valist, uint64_t);
|
|
|
|
end = va_arg(valist, uint64_t);
|
2015-09-03 04:04:43 +03:00
|
|
|
ret = _hook_mem_access(uc, UC_HOOK_MEM_WRITE, begin, end, callback, user_data, hh);
|
2015-09-02 20:12:49 +03:00
|
|
|
break;
|
2015-09-24 09:18:02 +03:00
|
|
|
case UC_HOOK_MEM_READ | UC_HOOK_MEM_WRITE:
|
2015-08-21 10:04:50 +03:00
|
|
|
begin = va_arg(valist, uint64_t);
|
|
|
|
end = va_arg(valist, uint64_t);
|
2015-09-24 09:18:02 +03:00
|
|
|
ret = _hook_mem_access(uc, UC_HOOK_MEM_READ | UC_HOOK_MEM_WRITE, begin, end, callback, user_data, hh);
|
2015-08-21 10:04:50 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(valist);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
UNICORN_EXPORT
|
2015-09-05 06:20:32 +03:00
|
|
|
uc_err uc_hook_del(uc_engine *uc, uc_hook hh)
|
2015-08-21 10:04:50 +03:00
|
|
|
{
|
2015-08-26 14:32:05 +03:00
|
|
|
return hook_del(uc, hh);
|
2015-08-21 10:04:50 +03:00
|
|
|
}
|
2016-01-16 11:57:17 +03:00
|
|
|
|
|
|
|
UNICORN_EXPORT
|
|
|
|
uint32_t uc_mem_regions(uc_engine *uc, uc_mem_region **regions, uint32_t *count)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
uc_mem_region *r = NULL;
|
|
|
|
|
|
|
|
*count = uc->mapped_block_count;
|
|
|
|
|
|
|
|
if (*count) {
|
|
|
|
r = malloc(*count * sizeof(uc_mem_region));
|
|
|
|
if (r == NULL) {
|
|
|
|
// out of memory
|
|
|
|
return UC_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < *count; i++) {
|
|
|
|
r[i].begin = uc->mapped_blocks[i]->addr;
|
|
|
|
r[i].end = uc->mapped_blocks[i]->end - 1;
|
|
|
|
r[i].perms = uc->mapped_blocks[i]->perms;
|
|
|
|
}
|
|
|
|
|
|
|
|
*regions = r;
|
|
|
|
|
|
|
|
return UC_ERR_OK;
|
|
|
|
}
|
|
|
|
|
2016-01-23 12:14:44 +03:00
|
|
|
UNICORN_EXPORT
|
|
|
|
uc_err uc_query(uc_engine *uc, uc_query_type type, size_t *result)
|
|
|
|
{
|
|
|
|
switch(uc->arch) {
|
|
|
|
case UC_ARCH_ARM:
|
|
|
|
return uc->query(uc, type, result);
|
|
|
|
default:
|
|
|
|
return UC_ERR_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return UC_ERR_OK;
|
|
|
|
}
|