Roll back uc_mem_protect changes

This commit is contained in:
Chris Eagle
2015-08-28 01:37:49 -07:00
parent 71ddad9474
commit adc254cc74
4 changed files with 1 additions and 343 deletions

84
uc.c
View File

@@ -604,90 +604,6 @@ uc_err uc_mem_map(uch handle, uint64_t address, size_t size)
return uc_mem_map_ex(handle, address, size, UC_PROT_READ | UC_PROT_WRITE);
}
UNICORN_EXPORT
uc_err uc_mem_protect(uch handle, uint64_t start, size_t block_size, uint32_t perms)
{
uint64_t address;
uint64_t size;
struct uc_struct* uc = (struct uc_struct *)handle;
if (handle == 0)
// invalid handle
return UC_ERR_UCH;
if (block_size == 0)
// invalid memory mapping
return UC_ERR_MAP;
// address must be aligned to 4KB
if ((start & (4*1024 - 1)) != 0)
return UC_ERR_MAP;
// size must be multiple of 4KB
if ((block_size & (4*1024 - 1)) != 0)
return UC_ERR_MAP;
// check for only valid permissions
if ((perms & ~(UC_PROT_READ | UC_PROT_WRITE)) != 0)
return UC_ERR_MAP;
//check that users entire requested block is mapped
address = start;
size = block_size;
while (size > 0) {
uint64_t region_size;
MemoryRegion *mr = memory_mapping(uc, address);
if (mr == NULL) {
return UC_ERR_MAP;
}
region_size = int128_get64(mr->size);
if (address > mr->addr) {
//in case start address is not aligned with start of region
region_size -= address - mr->addr;
}
if (size < region_size) {
//entire region is covered
break;
}
size -= region_size;
address += region_size;
}
//Now we know entire region is mapped, so change permissions
address = start;
size = block_size;
while (size > 0) {
MemoryRegion *mr = memory_mapping(uc, address);
uint64_t region_size = int128_get64(mr->size);
if (address > mr->addr) {
//in case start address is not aligned with start of region
region_size -= address - mr->addr;
//TODO Learn how to split regions
//In this case some proper subset of the region is having it's permissions changed
//need to split region and add new portions into uc->mapped_blocks list
//In this case, there is a portion of the region with original perms: mr->addr..start
//and a portion getting new perms: start..start+block_size
//split the block and stay in the loop
}
if (size < int128_get64(mr->size)) {
//TODO Learn how to split regions
//In this case some proper subset of the region is having it's permissions changed
//need to split region and add new portions into uc->mapped_blocks list
//In this case, there is a portion of the region with new perms: start..start+block_size
//and a portion getting new perms: mr->addr+size..mr->addr+mr->size
//split the block and break
break;
}
size -= int128_get64(mr->size);
address += int128_get64(mr->size);
mr->perms = perms;
uc->readonly_mem(mr, (perms & UC_PROT_WRITE) == 0);
}
return UC_ERR_OK;
}
MemoryRegion *memory_mapping(struct uc_struct* uc, uint64_t address)
{
unsigned int i;