From 70cdbf8c69330461f808d6ce010e53ee0c3571b9 Mon Sep 17 00:00:00 2001 From: Nguyen Anh Quynh Date: Wed, 26 Aug 2015 11:29:14 +0800 Subject: [PATCH] uc_mem_map(): enforce address & size to be aligned to 4KB. this fixes bunch of regress tests in regress/ --- include/unicorn/unicorn.h | 6 +++--- uc.c | 17 +++++++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/include/unicorn/unicorn.h b/include/unicorn/unicorn.h index a3293920..66de81eb 100644 --- a/include/unicorn/unicorn.h +++ b/include/unicorn/unicorn.h @@ -390,9 +390,9 @@ uc_err uc_hook_del(uch handle, uch *h2); @handle: handle returned by uc_open() @address: starting address of the new memory region to be mapped in. - This address will be round down to 8KB boundary - @size: size of the new memory region to be mapped in. This will be round up to - the next 8KB boundary. + This address must be aligned to 4KB, or this will return with UC_ERR_MAP error. + @size: size of the new memory region to be mapped in. + This size must be multiple of 4KB, or this will return with UC_ERR_MAP error. @return UC_ERR_OK on success, or other value on failure (refer to uc_err enum for detailed error). diff --git a/uc.c b/uc.c index 65179095..5a798f57 100644 --- a/uc.c +++ b/uc.c @@ -535,7 +535,6 @@ UNICORN_EXPORT uc_err uc_mem_map(uch handle, uint64_t address, size_t size) { struct uc_struct* uc = (struct uc_struct *)handle; - size_t s; if (handle == 0) // invalid handle @@ -545,11 +544,17 @@ uc_err uc_mem_map(uch handle, uint64_t address, size_t size) // invalid memory mapping return UC_ERR_MAP; - // align to 8KB boundary - map_begin[map_count] = address & (~ (8*1024 - 1)); - s = (size + 8*1024 - 1) & (~ (8*1024 - 1)); - map_end[map_count] = s + map_begin[map_count]; - uc->memory_map(uc, map_begin[map_count], s); + // address must be aligned to 4KB + if ((address & (4*1024 - 1)) != 0) + return UC_ERR_MAP; + + // size must be multiple of 4KB + if ((size & (4*1024 - 1)) != 0) + return UC_ERR_MAP; + + map_begin[map_count] = address; + map_end[map_count] = size + map_begin[map_count]; + uc->memory_map(uc, map_begin[map_count], size); map_count++; return UC_ERR_OK;