From e11c0629f99b2fc69f012aff52ed8a309ba715e3 Mon Sep 17 00:00:00 2001 From: Chris Eagle Date: Tue, 25 Aug 2015 23:08:18 -0700 Subject: [PATCH] Change allocation strategy for memory block tracking and track begin/end rather than begin/size --- include/uc_priv.h | 7 +++++-- uc.c | 14 ++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/uc_priv.h b/include/uc_priv.h index 02dd32f3..3fde4862 100755 --- a/include/uc_priv.h +++ b/include/uc_priv.h @@ -16,8 +16,8 @@ QTAILQ_HEAD(CPUTailQ, CPUState); typedef struct MemoryBlock { - uint64_t begin; - size_t size; + uint64_t begin; //inclusive + uint64_t end; //exclusive uint32_t perms; } MemoryBlock; @@ -67,6 +67,9 @@ struct hook_struct { // extend memory to keep 32 more hooks each time #define HOOK_SIZE 32 +//relloc increment, KEEP THIS A POWER OF 2! +#define MEM_BLOCK_INCR 32 + struct uc_struct { uc_arch arch; uc_mode mode; diff --git a/uc.c b/uc.c index 41280a98..e8f8103e 100755 --- a/uc.c +++ b/uc.c @@ -548,13 +548,15 @@ uc_err uc_mem_map(uch handle, uint64_t address, size_t size) if ((size & (4*1024 - 1)) != 0) return UC_ERR_MAP; - blocks = realloc(uc->mapped_blocks, sizeof(MemoryBlock) * (uc->mapped_block_count + 1)); - if (blocks == NULL) { - return UC_ERR_OOM; + if ((uc->mapped_block_count & (MEM_BLOCK_INCR - 1)) == 0) { //time to grow + blocks = realloc(uc->mapped_blocks, sizeof(MemoryBlock) * (uc->mapped_block_count + MEM_BLOCK_INCR)); + if (blocks == NULL) { + return UC_ERR_OOM; + } + uc->mapped_blocks = blocks; } - uc->mapped_blocks = blocks; blocks[uc->mapped_block_count].begin = address; - blocks[uc->mapped_block_count].size = size; + blocks[uc->mapped_block_count].end = address + size; //TODO extend uc_mem_map to accept permissions, figure out how to pass this down to qemu blocks[uc->mapped_block_count].perms = UC_PROT_READ | UC_PROT_WRITE | UC_PROT_EXEC; uc->memory_map(uc, address, size); @@ -568,7 +570,7 @@ bool memory_mapping(struct uc_struct* uc, uint64_t address) unsigned int i; for(i = 0; i < uc->mapped_block_count; i++) { - if (address >= uc->mapped_blocks[i].begin && address < (uc->mapped_blocks[i].begin + uc->mapped_blocks[i].size)) + if (address >= uc->mapped_blocks[i].begin && address < uc->mapped_blocks[i].end) return true; }