From d957d1efeee1b6e185b7a843d4e138ea2e91c0e1 Mon Sep 17 00:00:00 2001 From: Nguyen Anh Quynh Date: Fri, 28 Aug 2015 18:21:36 +0800 Subject: [PATCH] regress: fix code style --- regress/map_crash.c | 5 +-- regress/map_write.c | 79 ++++++++++++++++++++++--------------------- regress/ro_mem_test.c | 57 ++++++++++++++++--------------- regress/sigill.c | 10 +++--- regress/sigill2.c | 2 +- 5 files changed, 79 insertions(+), 74 deletions(-) diff --git a/regress/map_crash.c b/regress/map_crash.c index 4d6bc8fe..ca16b56b 100644 --- a/regress/map_crash.c +++ b/regress/map_crash.c @@ -4,9 +4,10 @@ #include #define UC_BUG_WRITE_SIZE 13000 -#define UC_BUG_WRITE_ADDR 0x1000 // fix this by change this to 0x2000 +#define UC_BUG_WRITE_ADDR 0x1000 -int main() { +int main() +{ int size; uint8_t *buf; uch uh; diff --git a/regress/map_write.c b/regress/map_write.c index a1caf7b8..fc4343c5 100644 --- a/regress/map_write.c +++ b/regress/map_write.c @@ -6,44 +6,45 @@ #define SIZE 1024*64 #define OVERFLOW 1 -int main() { - uch uh; - uint8_t *buf, *buf2; - int i; - uc_err err; +int main() +{ + uch uh; + uint8_t *buf, *buf2; + int i; + uc_err err; - err = uc_open (UC_ARCH_X86, UC_MODE_64, &uh); - if (err) { - printf ("uc_open %d\n", err); - return 1; - } - err = uc_mem_map (uh, ADDR, SIZE); - if (err) { - printf ("uc_mem_map %d\n", err); - return 1; - } - buf = calloc (SIZE*2, 1); - buf2 = calloc (SIZE, 1); - for (i=0;i const uint8_t PROGRAM[] = - "\xeb\x1a\x58\x83\xc0\x04\x83\xe0\xfc\x83\xc0\x01\xc7\x00\x78\x56" - "\x34\x12\x83\xc0\x07\xc7\x00\x21\x43\x65\x87\x90\xe8\xe1\xff\xff" - "\xff" "xxxxAAAAxxxBBBB"; +"\xeb\x1a\x58\x83\xc0\x04\x83\xe0\xfc\x83\xc0\x01\xc7\x00\x78\x56" +"\x34\x12\x83\xc0\x07\xc7\x00\x21\x43\x65\x87\x90\xe8\xe1\xff\xff" +"\xff" "xxxxAAAAxxxBBBB"; // total size: 33 bytes /* jmp short bottom top: - pop eax - add eax, 4 - and eax, 0xfffffffc - add eax, 1 ; unaligned - mov dword [eax], 0x12345678 ; try to write into code section - add eax, 7 ; aligned - mov dword [eax], 0x87654321 ; try to write into code section - nop +pop eax +add eax, 4 +and eax, 0xfffffffc +add eax, 1 ; unaligned +mov dword [eax], 0x12345678 ; try to write into code section +add eax, 7 ; aligned +mov dword [eax], 0x87654321 ; try to write into code section +nop bottom: - call top -*/ +call top + */ // callback for tracing instruction static void hook_code(uch handle, uint64_t address, uint32_t size, void *user_data) @@ -50,20 +50,20 @@ static bool hook_mem_invalid(uch handle, uc_mem_type type, case UC_MEM_WRITE: //if this is a push, esp has not been adjusted yet if (esp == (address + size)) { - uint32_t upper; - upper = (esp + 0xfff) & ~0xfff; - printf(">>> Stack appears to be missing at 0x%"PRIx64 ", allocating now\n", address); - // map this memory in with 2MB in size - uc_mem_map_ex(handle, upper - 0x8000, 0x8000, UC_PROT_READ | UC_PROT_WRITE); - // return true to indicate we want to continue - return true; + uint32_t upper; + upper = (esp + 0xfff) & ~0xfff; + printf(">>> Stack appears to be missing at 0x%"PRIx64 ", allocating now\n", address); + // map this memory in with 2MB in size + uc_mem_map_ex(handle, upper - 0x8000, 0x8000, UC_PROT_READ | UC_PROT_WRITE); + // return true to indicate we want to continue + return true; } printf(">>> Missing memory is being WRITTEN at 0x%"PRIx64 ", data size = %u, data value = 0x%"PRIx64 "\n", - address, size, value); + address, size, value); return false; case UC_MEM_WRITE_RO: printf(">>> RO memory is being WRITTEN at 0x%"PRIx64 ", data size = %u, data value = 0x%"PRIx64 "\n", - address, size, value); + address, size, value); return false; } } @@ -72,14 +72,15 @@ static bool hook_mem_invalid(uch handle, uc_mem_type type, #define STACK 0x500000 #define STACK_SIZE 0x5000 -int main(int argc, char **argv, char **envp) { +int main(int argc, char **argv, char **envp) +{ uch handle, trace1, trace2; uc_err err; uint8_t bytes[8]; uint32_t esp; int result; int map_stack = 0; - + if (argc == 2 && strcmp(argv[1], "--map-stack") == 0) { map_stack = 1; } @@ -97,10 +98,10 @@ int main(int argc, char **argv, char **envp) { uc_mem_map(handle, 0x200000, 0x2000); uc_mem_map(handle, 0x300000, 0x3000); uc_mem_map_ex(handle, 0x400000, 0x4000, UC_PROT_READ); - + if (map_stack) { - printf("Pre-mapping stack\n"); - uc_mem_map_ex(handle, STACK, STACK_SIZE, UC_PROT_READ | UC_PROT_WRITE); + printf("Pre-mapping stack\n"); + uc_mem_map_ex(handle, STACK, STACK_SIZE, UC_PROT_READ | UC_PROT_WRITE); } else { printf("Mapping stack on first invalid memory access\n"); @@ -190,6 +191,6 @@ int main(int argc, char **argv, char **envp) { } uc_close(&handle); - + return 0; } diff --git a/regress/sigill.c b/regress/sigill.c index 099bbec3..415a2313 100644 --- a/regress/sigill.c +++ b/regress/sigill.c @@ -8,14 +8,16 @@ int got_sigill = 0; -void _interrupt(uch handle, uint32_t intno, void *user_data) { +void _interrupt(uch handle, uint32_t intno, void *user_data) +{ if (intno == 6) { uc_emu_stop (handle); - got_sigill = 1; + got_sigill = 1; } } -int main() { +int main() +{ int size; uint8_t *buf; uch uh; @@ -34,7 +36,7 @@ int main() { memset (buf, 0, size); if (!uc_mem_map (uh, UC_BUG_WRITE_ADDR, size)) { uc_mem_write (uh, UC_BUG_WRITE_ADDR, - (const uint8_t*)"\xff\xff\xff\xff\xff\xff\xff\xff", 8); + (const uint8_t*)"\xff\xff\xff\xff\xff\xff\xff\xff", 8); } uc_hook_add (uh, &uh_trap, UC_HOOK_INTR, _interrupt, NULL); uc_emu_start (uh, UC_BUG_WRITE_ADDR, UC_BUG_WRITE_ADDR+8, 0, 1); diff --git a/regress/sigill2.c b/regress/sigill2.c index 97cd7199..ca13282a 100644 --- a/regress/sigill2.c +++ b/regress/sigill2.c @@ -20,7 +20,7 @@ int main() size = UC_BUG_WRITE_SIZE; if (!uc_mem_map (uh, UC_BUG_WRITE_ADDR, size)) { uc_mem_write (uh, UC_BUG_WRITE_ADDR, - (const uint8_t*)"\xff\xff\xff\xff\xff\xff\xff\xff", 8); + (const uint8_t*)"\xff\xff\xff\xff\xff\xff\xff\xff", 8); } err = uc_emu_start (uh, UC_BUG_WRITE_ADDR, UC_BUG_WRITE_ADDR+8, 0, 1); uc_close (&uh);