regress: fix code style

This commit is contained in:
Nguyen Anh Quynh 2015-08-28 18:21:36 +08:00
parent 5de0d5ca70
commit d957d1efee
5 changed files with 79 additions and 74 deletions

View File

@ -4,9 +4,10 @@
#include <stdlib.h> #include <stdlib.h>
#define UC_BUG_WRITE_SIZE 13000 #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; int size;
uint8_t *buf; uint8_t *buf;
uch uh; uch uh;

View File

@ -6,44 +6,45 @@
#define SIZE 1024*64 #define SIZE 1024*64
#define OVERFLOW 1 #define OVERFLOW 1
int main() { int main()
uch uh; {
uint8_t *buf, *buf2; uch uh;
int i; uint8_t *buf, *buf2;
uc_err err; int i;
uc_err err;
err = uc_open (UC_ARCH_X86, UC_MODE_64, &uh); err = uc_open (UC_ARCH_X86, UC_MODE_64, &uh);
if (err) { if (err) {
printf ("uc_open %d\n", err); printf ("uc_open %d\n", err);
return 1; return 1;
} }
err = uc_mem_map (uh, ADDR, SIZE); err = uc_mem_map (uh, ADDR, SIZE);
if (err) { if (err) {
printf ("uc_mem_map %d\n", err); printf ("uc_mem_map %d\n", err);
return 1; return 1;
} }
buf = calloc (SIZE*2, 1); buf = calloc (SIZE*2, 1);
buf2 = calloc (SIZE, 1); buf2 = calloc (SIZE, 1);
for (i=0;i<SIZE; i++) { for (i=0;i<SIZE; i++) {
buf[i] = i & 0xff; buf[i] = i & 0xff;
} }
/* crash here */ /* crash here */
err = uc_mem_write (uh, ADDR, buf, SIZE+OVERFLOW); err = uc_mem_write (uh, ADDR, buf, SIZE+OVERFLOW);
if (err) { if (err) {
printf ("uc_mem_map %d\n", err); printf ("uc_mem_map %d\n", err);
return 1; return 1;
} }
err = uc_mem_read (uh, ADDR+10, buf2, 4); err = uc_mem_read (uh, ADDR+10, buf2, 4);
if (err) { if (err) {
printf ("uc_mem_map %d\n", err); printf ("uc_mem_map %d\n", err);
return 1; return 1;
} }
if (buf2[0] != 0xa) { if (buf2[0] != 0xa) {
printf ("mem contents are wrong\n"); printf ("mem contents are wrong\n");
return 1; return 1;
} }
printf ("OK\n"); printf ("OK\n");
free (buf); free (buf);
free (buf2); free (buf2);
return 0; return 0;
} }

View File

@ -5,25 +5,25 @@
#include <unicorn/unicorn.h> #include <unicorn/unicorn.h>
const uint8_t PROGRAM[] = const uint8_t PROGRAM[] =
"\xeb\x1a\x58\x83\xc0\x04\x83\xe0\xfc\x83\xc0\x01\xc7\x00\x78\x56" "\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" "\x34\x12\x83\xc0\x07\xc7\x00\x21\x43\x65\x87\x90\xe8\xe1\xff\xff"
"\xff" "xxxxAAAAxxxBBBB"; "\xff" "xxxxAAAAxxxBBBB";
// total size: 33 bytes // total size: 33 bytes
/* /*
jmp short bottom jmp short bottom
top: top:
pop eax pop eax
add eax, 4 add eax, 4
and eax, 0xfffffffc and eax, 0xfffffffc
add eax, 1 ; unaligned add eax, 1 ; unaligned
mov dword [eax], 0x12345678 ; try to write into code section mov dword [eax], 0x12345678 ; try to write into code section
add eax, 7 ; aligned add eax, 7 ; aligned
mov dword [eax], 0x87654321 ; try to write into code section mov dword [eax], 0x87654321 ; try to write into code section
nop nop
bottom: bottom:
call top call top
*/ */
// callback for tracing instruction // callback for tracing instruction
static void hook_code(uch handle, uint64_t address, uint32_t size, void *user_data) 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: case UC_MEM_WRITE:
//if this is a push, esp has not been adjusted yet //if this is a push, esp has not been adjusted yet
if (esp == (address + size)) { if (esp == (address + size)) {
uint32_t upper; uint32_t upper;
upper = (esp + 0xfff) & ~0xfff; upper = (esp + 0xfff) & ~0xfff;
printf(">>> Stack appears to be missing at 0x%"PRIx64 ", allocating now\n", address); printf(">>> Stack appears to be missing at 0x%"PRIx64 ", allocating now\n", address);
// map this memory in with 2MB in size // map this memory in with 2MB in size
uc_mem_map_ex(handle, upper - 0x8000, 0x8000, UC_PROT_READ | UC_PROT_WRITE); uc_mem_map_ex(handle, upper - 0x8000, 0x8000, UC_PROT_READ | UC_PROT_WRITE);
// return true to indicate we want to continue // return true to indicate we want to continue
return true; return true;
} }
printf(">>> Missing memory is being WRITTEN at 0x%"PRIx64 ", data size = %u, data value = 0x%"PRIx64 "\n", 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; return false;
case UC_MEM_WRITE_RO: case UC_MEM_WRITE_RO:
printf(">>> RO memory is being WRITTEN at 0x%"PRIx64 ", data size = %u, data value = 0x%"PRIx64 "\n", 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; return false;
} }
} }
@ -72,14 +72,15 @@ static bool hook_mem_invalid(uch handle, uc_mem_type type,
#define STACK 0x500000 #define STACK 0x500000
#define STACK_SIZE 0x5000 #define STACK_SIZE 0x5000
int main(int argc, char **argv, char **envp) { int main(int argc, char **argv, char **envp)
{
uch handle, trace1, trace2; uch handle, trace1, trace2;
uc_err err; uc_err err;
uint8_t bytes[8]; uint8_t bytes[8];
uint32_t esp; uint32_t esp;
int result; int result;
int map_stack = 0; int map_stack = 0;
if (argc == 2 && strcmp(argv[1], "--map-stack") == 0) { if (argc == 2 && strcmp(argv[1], "--map-stack") == 0) {
map_stack = 1; 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, 0x200000, 0x2000);
uc_mem_map(handle, 0x300000, 0x3000); uc_mem_map(handle, 0x300000, 0x3000);
uc_mem_map_ex(handle, 0x400000, 0x4000, UC_PROT_READ); uc_mem_map_ex(handle, 0x400000, 0x4000, UC_PROT_READ);
if (map_stack) { if (map_stack) {
printf("Pre-mapping stack\n"); printf("Pre-mapping stack\n");
uc_mem_map_ex(handle, STACK, STACK_SIZE, UC_PROT_READ | UC_PROT_WRITE); uc_mem_map_ex(handle, STACK, STACK_SIZE, UC_PROT_READ | UC_PROT_WRITE);
} }
else { else {
printf("Mapping stack on first invalid memory access\n"); printf("Mapping stack on first invalid memory access\n");
@ -190,6 +191,6 @@ int main(int argc, char **argv, char **envp) {
} }
uc_close(&handle); uc_close(&handle);
return 0; return 0;
} }

View File

@ -8,14 +8,16 @@
int got_sigill = 0; 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) { if (intno == 6) {
uc_emu_stop (handle); uc_emu_stop (handle);
got_sigill = 1; got_sigill = 1;
} }
} }
int main() { int main()
{
int size; int size;
uint8_t *buf; uint8_t *buf;
uch uh; uch uh;
@ -34,7 +36,7 @@ int main() {
memset (buf, 0, size); memset (buf, 0, size);
if (!uc_mem_map (uh, UC_BUG_WRITE_ADDR, size)) { if (!uc_mem_map (uh, UC_BUG_WRITE_ADDR, size)) {
uc_mem_write (uh, UC_BUG_WRITE_ADDR, 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_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); uc_emu_start (uh, UC_BUG_WRITE_ADDR, UC_BUG_WRITE_ADDR+8, 0, 1);

View File

@ -20,7 +20,7 @@ int main()
size = UC_BUG_WRITE_SIZE; size = UC_BUG_WRITE_SIZE;
if (!uc_mem_map (uh, UC_BUG_WRITE_ADDR, size)) { if (!uc_mem_map (uh, UC_BUG_WRITE_ADDR, size)) {
uc_mem_write (uh, UC_BUG_WRITE_ADDR, 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); err = uc_emu_start (uh, UC_BUG_WRITE_ADDR, UC_BUG_WRITE_ADDR+8, 0, 1);
uc_close (&uh); uc_close (&uh);