uc_mem_map(): enforce address & size to be aligned to 4KB. this fixes bunch of regress tests in regress/

This commit is contained in:
Nguyen Anh Quynh 2015-08-26 11:29:14 +08:00
parent e1b8bc2ed0
commit 70cdbf8c69
2 changed files with 14 additions and 9 deletions

View file

@ -390,9 +390,9 @@ uc_err uc_hook_del(uch handle, uch *h2);
@handle: handle returned by uc_open() @handle: handle returned by uc_open()
@address: starting address of the new memory region to be mapped in. @address: starting address of the new memory region to be mapped in.
This address will be round down to 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 will be round up to @size: size of the new memory region to be mapped in.
the next 8KB boundary. 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 @return UC_ERR_OK on success, or other value on failure (refer to uc_err enum
for detailed error). for detailed error).

17
uc.c
View file

@ -535,7 +535,6 @@ UNICORN_EXPORT
uc_err uc_mem_map(uch handle, uint64_t address, size_t size) uc_err uc_mem_map(uch handle, uint64_t address, size_t size)
{ {
struct uc_struct* uc = (struct uc_struct *)handle; struct uc_struct* uc = (struct uc_struct *)handle;
size_t s;
if (handle == 0) if (handle == 0)
// invalid handle // invalid handle
@ -545,11 +544,17 @@ uc_err uc_mem_map(uch handle, uint64_t address, size_t size)
// invalid memory mapping // invalid memory mapping
return UC_ERR_MAP; return UC_ERR_MAP;
// align to 8KB boundary // address must be aligned to 4KB
map_begin[map_count] = address & (~ (8*1024 - 1)); if ((address & (4*1024 - 1)) != 0)
s = (size + 8*1024 - 1) & (~ (8*1024 - 1)); return UC_ERR_MAP;
map_end[map_count] = s + map_begin[map_count];
uc->memory_map(uc, map_begin[map_count], s); // 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++; map_count++;
return UC_ERR_OK; return UC_ERR_OK;