cleaning & indentation

This commit is contained in:
Nguyen Anh Quynh 2015-09-03 18:16:49 +08:00
parent b8d4240240
commit 9f9d57e84f
6 changed files with 633 additions and 694 deletions

View File

@ -1,5 +1,4 @@
/*
Executable memory regions demo / unit test
Copyright(c) 2015 Chris Eagle
@ -82,9 +81,11 @@ static int log_num = 1;
static void hook_code(uch handle, uint64_t addr, uint32_t size, void *user_data)
{
uint8_t opcode;
if (uc_mem_read(handle, addr, &opcode, 1) != UC_ERR_OK) {
printf("not ok %d - uc_mem_read fail during hook_code callback, addr: 0x%" PRIx64 "\n", log_num++, addr);
}
// printf("ok %d - uc_mem_read for opcode at address 0x%" PRIx64 "\n", log_num++, addr);
switch (opcode) {
case 0xf4: //hlt
@ -92,8 +93,7 @@ static void hook_code(uch handle, uint64_t addr, uint32_t size, void *user_data)
if (uc_emu_stop(handle) != UC_ERR_OK) {
printf("not ok %d - uc_emu_stop fail during hook_code callback, addr: 0x%" PRIx64 "\n", log_num++, addr);
_exit(-1);
}
else {
} else {
printf("ok %d - hlt encountered, uc_emu_stop called\n", log_num++);
}
break;
@ -124,8 +124,7 @@ static bool hook_mem_invalid(uch handle, uc_mem_type type,
//make page executable
if (uc_mem_protect(handle, addr & ~0xfffL, 0x1000, UC_PROT_READ | UC_PROT_EXEC) != UC_ERR_OK) {
printf("not ok %d - uc_mem_protect fail for address: 0x%" PRIx64 "\n", log_num++, addr);
}
else {
} else {
printf("ok %d - uc_mem_protect success at 0x%" PRIx64 "\n", log_num++, addr);
}
return true;
@ -134,8 +133,7 @@ static bool hook_mem_invalid(uch handle, uc_mem_type type,
if (uc_mem_protect(handle, addr & ~0xfffL, 0x1000, UC_PROT_READ | UC_PROT_WRITE) != UC_ERR_OK) {
printf("not ok %d - uc_mem_protect fail during hook_mem_invalid callback, addr: 0x%" PRIx64 "\n", log_num++, addr);
}
else {
} else {
printf("ok %d - uc_mem_protect success\n", log_num++);
}
return true;
@ -164,8 +162,7 @@ int main(int argc, char **argv, char **envp)
if (err) {
printf("not ok %d - Failed on uc_open() with error returned: %u\n", log_num++, err);
return 1;
}
else {
} else {
printf("ok %d - uc_open() success\n", log_num++);
}
@ -180,8 +177,7 @@ int main(int argc, char **argv, char **envp)
if (uc_reg_write(handle, UC_X86_REG_ESP, &esp)) {
printf("not ok %d - Failed to set esp. quit!\n", log_num++);
return 2;
}
else {
} else {
printf("ok %d - ESP set\n", log_num++);
}
@ -189,16 +185,14 @@ int main(int argc, char **argv, char **envp)
if (uc_mem_write(handle, 0x1ff000, (uint8_t*)buf1, 4096)) {
printf("not ok %d - Failed to write random buffer 1 to memory, quit!\n", log_num++);
return 3;
}
else {
} else {
printf("ok %d - Random buffer 1 written to memory\n", log_num++);
}
if (uc_mem_write(handle, 0x301000, (uint8_t*)buf2, 4096)) {
printf("not ok %d - Failed to write random buffer 2 to memory, quit!\n", log_num++);
return 4;
}
else {
} else {
printf("ok %d - Random buffer 2 written to memory\n", log_num++);
}
@ -206,16 +200,14 @@ int main(int argc, char **argv, char **envp)
if (uc_mem_write(handle, 0x100000, PROGRAM, sizeof(PROGRAM))) {
printf("not ok %d - Failed to write emulation code to memory, quit!\n", log_num++);
return 5;
}
else {
} else {
printf("ok %d - Program written to memory\n", log_num++);
}
if (uc_hook_add(handle, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0) != UC_ERR_OK) {
printf("not ok %d - Failed to install UC_HOOK_CODE handler\n", log_num++);
return 6;
}
else {
} else {
printf("ok %d - UC_HOOK_CODE installed\n", log_num++);
}
@ -223,8 +215,7 @@ int main(int argc, char **argv, char **envp)
if (uc_hook_add(handle, &trace1, UC_HOOK_MEM_WRITE, hook_mem_write, NULL) != UC_ERR_OK) {
printf("not ok %d - Failed to install UC_HOOK_MEM_WRITE handler\n", log_num++);
return 7;
}
else {
} else {
printf("ok %d - UC_HOOK_MEM_WRITE installed\n", log_num++);
}
@ -232,8 +223,7 @@ int main(int argc, char **argv, char **envp)
if (uc_hook_add(handle, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL) != UC_ERR_OK) {
printf("not ok %d - Failed to install UC_HOOK_MEM_INVALID handler\n", log_num++);
return 8;
}
else {
} else {
printf("ok %d - UC_HOOK_MEM_INVALID installed\n", log_num++);
}
@ -243,8 +233,7 @@ int main(int argc, char **argv, char **envp)
if (err != UC_ERR_OK) {
printf("not ok %d - Failure on uc_emu_start() with error %u:%s\n", log_num++, err, uc_strerror(err));
return 9;
}
else {
} else {
printf("ok %d - uc_emu_start complete\n", log_num++);
}
printf("# END execution\n");
@ -252,8 +241,7 @@ int main(int argc, char **argv, char **envp)
// get ending EIP
if (uc_reg_read(handle, UC_X86_REG_EIP, &eip)) {
printf("not ok %d - Failed to read eip.\n", log_num++);
}
else {
} else {
printf("ok %d - Ending EIP 0x%x\n", log_num++, eip);
}
@ -261,34 +249,29 @@ int main(int argc, char **argv, char **envp)
// fill in sections that shouldn't get touched
if (uc_mem_read(handle, 0x1ff000, (uint8_t*)readbuf, 4096)) {
printf("not ok %d - Failed to read random buffer 1 from memory\n", log_num++);
}
else {
} else {
printf("ok %d - Random buffer 1 read from memory\n", log_num++);
if (memcmp(buf1, readbuf, 4096)) {
printf("not ok %d - Random buffer 1 contents are incorrect\n", log_num++);
}
else {
} else {
printf("ok %d - Random buffer 1 contents are correct\n", log_num++);
}
}
if (uc_mem_read(handle, 0x301000, (uint8_t*)readbuf, 4096)) {
printf("not ok %d - Failed to read random buffer 2 from memory\n", log_num++);
}
else {
} else {
printf("ok %d - Random buffer 2 read from memory\n", log_num++);
if (memcmp(buf2, readbuf, 4096)) {
printf("not ok %d - Random buffer 2 contents are incorrect\n", log_num++);
}
else {
} else {
printf("ok %d - Random buffer 2 contents are correct\n", log_num++);
}
}
if (uc_close(&handle) == UC_ERR_OK) {
printf("ok %d - uc_close complete\n", log_num++);
}
else {
} else {
printf("not ok %d - uc_close complete\n", log_num++);
}

View File

@ -1,5 +1,4 @@
/*
uc_mem_protect demo / unit test
Copyright(c) 2015 Chris Eagle
@ -91,15 +90,13 @@ static void hook_code(uch handle, uint64_t addr, uint32_t size, void *user_data)
printf("# Handling NOP\n");
if (uc_mem_read(handle, 0x200000 + test_num * 0x100000, (uint8_t*)&testval, sizeof(testval)) != UC_ERR_OK) {
printf("not ok %d - uc_mem_read fail for address: 0x%x\n", log_num++, 0x200000 + test_num * 0x100000);
}
else {
} else {
printf("ok %d - good uc_mem_read for address: 0x%x\n", log_num++, 0x200000 + test_num * 0x100000);
printf("# uc_mem_read for test %d\n", test_num);
if (testval == tests[test_num]) {
printf("ok %d - passed test %d\n", log_num++, test_num);
}
else {
} else {
printf("not ok %d - failed test %d\n", log_num++, test_num);
printf("# Expected: 0x%x\n",tests[test_num]);
printf("# Received: 0x%x\n", testval);
@ -107,8 +104,7 @@ static void hook_code(uch handle, uint64_t addr, uint32_t size, void *user_data)
}
if (uc_mem_protect(handle, 0x200000 + test_num * 0x100000, 0x1000, UC_PROT_READ) != UC_ERR_OK) {
printf("not ok %d - uc_mem_protect fail during hook_code callback, addr: 0x%x\n", log_num++, 0x200000 + test_num * 0x100000);
}
else {
} else {
printf("ok %d - uc_mem_protect success\n", log_num++);
}
test_num++;
@ -118,8 +114,7 @@ static void hook_code(uch handle, uint64_t addr, uint32_t size, void *user_data)
if (uc_emu_stop(handle) != UC_ERR_OK) {
printf("not ok %d - uc_emu_stop fail during hook_code callback, addr: 0x%" PRIx64 "\n", log_num++, addr);
_exit(-1);
}
else {
} else {
printf("ok %d - hlt encountered, uc_emu_stop called\n", log_num++);
}
break;
@ -150,15 +145,13 @@ static bool hook_mem_invalid(uch handle, uc_mem_type type,
if (uc_mem_read(handle, addr, (uint8_t*)&testval, sizeof(testval)) != UC_ERR_OK) {
printf("not ok %d - uc_mem_read fail for address: 0x%" PRIx64 "\n", log_num++, addr);
}
else {
} else {
printf("ok %d - uc_mem_read success after mem_protect at test %d\n", log_num++, test_num - 1);
}
if (uc_mem_protect(handle, addr & ~0xfffL, 0x1000, UC_PROT_READ | UC_PROT_WRITE) != UC_ERR_OK) {
printf("not ok %d - uc_mem_protect fail during hook_mem_invalid callback, addr: 0x%" PRIx64 "\n", log_num++, addr);
}
else {
} else {
printf("ok %d - uc_mem_protect success\n", log_num++);
}
return true;
@ -187,8 +180,7 @@ int main(int argc, char **argv, char **envp)
if (err) {
printf("not ok %d - Failed on uc_open() with error returned: %u\n", log_num++, err);
return 1;
}
else {
} else {
printf("ok %d - uc_open() success\n", log_num++);
}
@ -201,16 +193,14 @@ int main(int argc, char **argv, char **envp)
if (uc_mem_write(handle, 0x3ff000, (uint8_t*)buf1, 4096)) {
printf("not ok %d - Failed to write random buffer 1 to memory, quit!\n", log_num++);
return 2;
}
else {
} else {
printf("ok %d - Random buffer 1 written to memory\n", log_num++);
}
if (uc_mem_write(handle, 0x401000, (uint8_t*)buf2, 4096)) {
printf("not ok %d - Failed to write random buffer 2 to memory, quit!\n", log_num++);
return 3;
}
else {
} else {
printf("ok %d - Random buffer 2 written to memory\n", log_num++);
}
@ -218,16 +208,14 @@ int main(int argc, char **argv, char **envp)
if (uc_mem_write(handle, CODE_SECTION, PROGRAM, sizeof(PROGRAM))) {
printf("not ok %d - Failed to write emulation code to memory, quit!\n", log_num++);
return 4;
}
else {
} else {
printf("ok %d - Program written to memory\n", log_num++);
}
if (uc_hook_add(handle, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0) != UC_ERR_OK) {
printf("not ok %d - Failed to install UC_HOOK_CODE handler\n", log_num++);
return 5;
}
else {
} else {
printf("ok %d - UC_HOOK_CODE installed\n", log_num++);
}
@ -235,8 +223,7 @@ int main(int argc, char **argv, char **envp)
if (uc_hook_add(handle, &trace1, UC_HOOK_MEM_WRITE, hook_mem_write, NULL) != UC_ERR_OK) {
printf("not ok %d - Failed to install UC_HOOK_MEM_WRITE handler\n", log_num++);
return 6;
}
else {
} else {
printf("ok %d - UC_HOOK_MEM_WRITE installed\n", log_num++);
}
@ -244,8 +231,7 @@ int main(int argc, char **argv, char **envp)
if (uc_hook_add(handle, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL) != UC_ERR_OK) {
printf("not ok %d - Failed to install UC_HOOK_MEM_INVALID handler\n", log_num++);
return 7;
}
else {
} else {
printf("ok %d - UC_HOOK_MEM_INVALID installed\n", log_num++);
}
@ -255,8 +241,7 @@ int main(int argc, char **argv, char **envp)
if (err != UC_ERR_OK) {
printf("not ok %d - Failure on uc_emu_start() with error %u:%s\n", log_num++, err, uc_strerror(err));
return 8;
}
else {
} else {
printf("ok %d - uc_emu_start complete\n", log_num++);
}
printf("# END execution\n");
@ -267,14 +252,12 @@ int main(int argc, char **argv, char **envp)
uint32_t val;
if (uc_mem_read(handle, addr, (uint8_t*)&val, sizeof(val)) != UC_ERR_OK) {
printf("not ok %d - Failed uc_mem_read for address 0x%x\n", log_num++, addr);
}
else {
} else {
printf("ok %d - Good uc_mem_read from 0x%x\n", log_num++, addr);
}
if (val != testval) {
printf("not ok %d - Read 0x%x, expected 0x%x\n", log_num++, val, testval);
}
else {
} else {
printf("ok %d - Correct value retrieved\n", log_num++);
}
testval += 0x02020202;
@ -288,34 +271,29 @@ int main(int argc, char **argv, char **envp)
// fill in sections that shouldn't get touched
if (uc_mem_read(handle, 0x3ff000, (uint8_t*)readbuf, 4096)) {
printf("not ok %d - Failed to read random buffer 1 from memory\n", log_num++);
}
else {
} else {
printf("ok %d - Random buffer 1 read from memory\n", log_num++);
if (memcmp(buf1, readbuf, 4096)) {
printf("not ok %d - Random buffer 1 contents are incorrect\n", log_num++);
}
else {
} else {
printf("ok %d - Random buffer 1 contents are correct\n", log_num++);
}
}
if (uc_mem_read(handle, 0x401000, (uint8_t*)readbuf, 4096)) {
printf("not ok %d - Failed to read random buffer 2 from memory\n", log_num++);
}
else {
} else {
printf("ok %d - Random buffer 2 read from memory\n", log_num++);
if (memcmp(buf2, readbuf, 4096)) {
printf("not ok %d - Random buffer 2 contents are incorrect\n", log_num++);
}
else {
} else {
printf("ok %d - Random buffer 2 contents are correct\n", log_num++);
}
}
if (uc_close(&handle) == UC_ERR_OK) {
printf("ok %d - uc_close complete\n", log_num++);
}
else {
} else {
printf("not ok %d - uc_close complete\n", log_num++);
}

View File

@ -85,15 +85,13 @@ static void hook_code(uch handle, uint64_t addr, uint32_t size, void *user_data)
printf("# Handling NOP\n");
if (uc_mem_read(handle, 0x200000 + test_num * 0x100000, (uint8_t*)&testval, sizeof(testval)) != UC_ERR_OK) {
printf("not ok %d - uc_mem_read fail for address: 0x%x\n", log_num++, 0x200000 + test_num * 0x100000);
}
else {
} else {
printf("ok %d - good uc_mem_read for address: 0x%x\n", log_num++, 0x200000 + test_num * 0x100000);
printf("# uc_mem_read for test %d\n", test_num);
if (testval == tests[test_num]) {
printf("ok %d - passed test %d\n", log_num++, test_num);
}
else {
} else {
printf("not ok %d - failed test %d\n", log_num++, test_num);
printf("# Expected: 0x%x\n",tests[test_num]);
printf("# Received: 0x%x\n", testval);
@ -101,8 +99,7 @@ static void hook_code(uch handle, uint64_t addr, uint32_t size, void *user_data)
}
if (uc_mem_unmap(handle, 0x200000 + test_num * 0x100000, 0x1000) != UC_ERR_OK) {
printf("not ok %d - uc_mem_unmap fail during hook_code callback, addr: 0x%x\n", log_num++, 0x200000 + test_num * 0x100000);
}
else {
} else {
printf("ok %d - uc_mem_unmap success\n", log_num++);
}
test_num++;
@ -112,8 +109,7 @@ static void hook_code(uch handle, uint64_t addr, uint32_t size, void *user_data)
if (uc_emu_stop(handle) != UC_ERR_OK) {
printf("not ok %d - uc_emu_stop fail during hook_code callback, addr: 0x%" PRIx64 "\n", log_num++, addr);
_exit(-1);
}
else {
} else {
printf("ok %d - hlt encountered, uc_emu_stop called\n", log_num++);
}
break;
@ -144,15 +140,13 @@ static bool hook_mem_invalid(uch handle, uc_mem_type type,
if (uc_mem_read(handle, addr, (uint8_t*)&testval, sizeof(testval)) != UC_ERR_OK) {
printf("ok %d - uc_mem_read fail for address: 0x%" PRIx64 "\n", log_num++, addr);
}
else {
} else {
printf("not ok %d - uc_mem_read success after unmap at test %d\n", log_num++, test_num - 1);
}
if (uc_mem_map(handle, addr & ~0xfffL, 0x1000, UC_PROT_READ | UC_PROT_WRITE) != UC_ERR_OK) {
printf("not ok %d - uc_mem_map fail during hook_mem_invalid callback, addr: 0x%" PRIx64 "\n", log_num++, addr);
}
else {
} else {
printf("ok %d - uc_mem_map success\n", log_num++);
}
return true;
@ -181,8 +175,7 @@ int main(int argc, char **argv, char **envp)
if (err) {
printf("not ok %d - Failed on uc_open() with error returned: %u\n", log_num++, err);
return 1;
}
else {
} else {
printf("ok %d - uc_open() success\n", log_num++);
}
@ -195,16 +188,14 @@ int main(int argc, char **argv, char **envp)
if (uc_mem_write(handle, 0x3ff000, (uint8_t*)buf1, 4096)) {
printf("not ok %d - Failed to write random buffer 1 to memory, quit!\n", log_num++);
return 2;
}
else {
} else {
printf("ok %d - Random buffer 1 written to memory\n", log_num++);
}
if (uc_mem_write(handle, 0x401000, (uint8_t*)buf2, 4096)) {
printf("not ok %d - Failed to write random buffer 2 to memory, quit!\n", log_num++);
return 3;
}
else {
} else {
printf("ok %d - Random buffer 2 written to memory\n", log_num++);
}
@ -212,16 +203,14 @@ int main(int argc, char **argv, char **envp)
if (uc_mem_write(handle, CODE_SECTION, PROGRAM, sizeof(PROGRAM))) {
printf("not ok %d - Failed to write emulation code to memory, quit!\n", log_num++);
return 4;
}
else {
} else {
printf("ok %d - Program written to memory\n", log_num++);
}
if (uc_hook_add(handle, &trace2, UC_HOOK_CODE, hook_code, NULL, 1, 0) != UC_ERR_OK) {
printf("not ok %d - Failed to install UC_HOOK_CODE handler\n", log_num++);
return 5;
}
else {
} else {
printf("ok %d - UC_HOOK_CODE installed\n", log_num++);
}
@ -229,8 +218,7 @@ int main(int argc, char **argv, char **envp)
if (uc_hook_add(handle, &trace1, UC_HOOK_MEM_WRITE, hook_mem_write, NULL) != UC_ERR_OK) {
printf("not ok %d - Failed to install UC_HOOK_MEM_WRITE handler\n", log_num++);
return 6;
}
else {
} else {
printf("ok %d - UC_HOOK_MEM_WRITE installed\n", log_num++);
}
@ -238,8 +226,7 @@ int main(int argc, char **argv, char **envp)
if (uc_hook_add(handle, &trace1, UC_HOOK_MEM_INVALID, hook_mem_invalid, NULL) != UC_ERR_OK) {
printf("not ok %d - Failed to install UC_HOOK_MEM_INVALID handler\n", log_num++);
return 7;
}
else {
} else {
printf("ok %d - UC_HOOK_MEM_INVALID installed\n", log_num++);
}
@ -249,8 +236,7 @@ int main(int argc, char **argv, char **envp)
if (err != UC_ERR_OK) {
printf("not ok %d - Failure on uc_emu_start() with error %u:%s\n", log_num++, err, uc_strerror(err));
return 8;
}
else {
} else {
printf("ok %d - uc_emu_start complete\n", log_num++);
}
printf("# END execution\n");
@ -261,14 +247,12 @@ int main(int argc, char **argv, char **envp)
uint32_t val;
if (uc_mem_read(handle, addr, (uint8_t*)&val, sizeof(val)) != UC_ERR_OK) {
printf("not ok %d - Failed uc_mem_read for address 0x%x\n", log_num++, addr);
}
else {
} else {
printf("ok %d - Good uc_mem_read from 0x%x\n", log_num++, addr);
}
if (val != testval) {
printf("not ok %d - Read 0x%x, expected 0x%x\n", log_num++, val, testval);
}
else {
} else {
printf("ok %d - Correct value retrieved\n", log_num++);
}
testval += 0x02020202;
@ -278,34 +262,29 @@ int main(int argc, char **argv, char **envp)
// fill in sections that shouldn't get touched
if (uc_mem_read(handle, 0x3ff000, (uint8_t*)readbuf, 4096)) {
printf("not ok %d - Failed to read random buffer 1 from memory\n", log_num++);
}
else {
} else {
printf("ok %d - Random buffer 1 read from memory\n", log_num++);
if (memcmp(buf1, readbuf, 4096)) {
printf("not ok %d - Random buffer 1 contents are incorrect\n", log_num++);
}
else {
} else {
printf("ok %d - Random buffer 1 contents are correct\n", log_num++);
}
}
if (uc_mem_read(handle, 0x401000, (uint8_t*)readbuf, 4096)) {
printf("not ok %d - Failed to read random buffer 2 from memory\n", log_num++);
}
else {
} else {
printf("ok %d - Random buffer 2 read from memory\n", log_num++);
if (memcmp(buf2, readbuf, 4096)) {
printf("not ok %d - Random buffer 2 contents are incorrect\n", log_num++);
}
else {
} else {
printf("ok %d - Random buffer 2 contents are correct\n", log_num++);
}
}
if (uc_close(&handle) == UC_ERR_OK) {
printf("ok %d - uc_close complete\n", log_num++);
}
else {
} else {
printf("not ok %d - uc_close complete\n", log_num++);
}

3
uc.c
View File

@ -860,8 +860,7 @@ uc_err uc_mem_unmap(uch handle, uint64_t address, size_t size)
break;
}
}
}
else {
} else {
//ouch, we are going to need to subdivide blocks
size_t count = 0, len;
while(count < size) {