From 65787d415a3f8a52b62b3d2f824f1035c0790257 Mon Sep 17 00:00:00 2001 From: Chris Eagle Date: Fri, 28 Aug 2015 20:02:21 -0700 Subject: [PATCH] rename uc_mem_map_ex to uc_mem_map and all associated changes --- bindings/java/unicorn/Unicorn.java | 2 +- bindings/java/unicorn_Unicorn.c | 6 +++--- include/unicorn/unicorn.h | 21 +++------------------ regress/block_test.c | 2 +- regress/map_crash.c | 2 +- regress/map_write.c | 2 +- regress/nr_mem_test.c | 6 +++--- regress/ro_mem_test.c | 12 ++++++------ regress/sigill.c | 2 +- regress/sigill2.c | 2 +- samples/sample_arm.c | 4 ++-- samples/sample_arm64.c | 2 +- samples/sample_m68k.c | 2 +- samples/sample_mips.c | 4 ++-- samples/sample_sparc.c | 2 +- samples/sample_x86.c | 22 +++++++++++----------- samples/shellcode.c | 2 +- uc.c | 9 +-------- 18 files changed, 41 insertions(+), 63 deletions(-) mode change 100644 => 100755 bindings/java/unicorn/Unicorn.java mode change 100644 => 100755 bindings/java/unicorn_Unicorn.c mode change 100644 => 100755 include/unicorn/unicorn.h mode change 100644 => 100755 regress/block_test.c mode change 100644 => 100755 regress/map_crash.c mode change 100644 => 100755 regress/map_write.c mode change 100644 => 100755 regress/ro_mem_test.c mode change 100644 => 100755 regress/sigill.c mode change 100644 => 100755 regress/sigill2.c mode change 100644 => 100755 samples/sample_arm.c mode change 100644 => 100755 samples/sample_arm64.c mode change 100644 => 100755 samples/sample_m68k.c mode change 100644 => 100755 samples/sample_mips.c mode change 100644 => 100755 samples/sample_sparc.c mode change 100644 => 100755 samples/sample_x86.c mode change 100644 => 100755 samples/shellcode.c mode change 100644 => 100755 uc.c diff --git a/bindings/java/unicorn/Unicorn.java b/bindings/java/unicorn/Unicorn.java old mode 100644 new mode 100755 index fa98554..06e1e9d --- a/bindings/java/unicorn/Unicorn.java +++ b/bindings/java/unicorn/Unicorn.java @@ -624,7 +624,7 @@ public class Unicorn implements UnicornArchs, UnicornModes, UnicornHooks, * @param address Base address of the memory range * @param size Size of the memory block. */ - public native void mem_map(long address, long size) throws UnicornException; + public native void mem_map(long address, long size, int perms) throws UnicornException; } diff --git a/bindings/java/unicorn_Unicorn.c b/bindings/java/unicorn_Unicorn.c old mode 100644 new mode 100755 index aa141bb..cd0428f --- a/bindings/java/unicorn_Unicorn.c +++ b/bindings/java/unicorn_Unicorn.c @@ -502,13 +502,13 @@ JNIEXPORT void JNICALL Java_unicorn_Unicorn_hook_1del /* * Class: unicorn_Unicorn * Method: mem_map - * Signature: (JJ)V + * Signature: (JJI)V */ JNIEXPORT void JNICALL Java_unicorn_Unicorn_mem_1map - (JNIEnv *env, jobject self, jlong address, jlong size) { + (JNIEnv *env, jobject self, jlong address, jlong size, jint perms) { uch handle = getHandle(env, self); - uc_err err = uc_mem_map(handle, (uint64_t)address, (size_t)size); + uc_err err = uc_mem_map(handle, (uint64_t)address, (size_t)size, (uint32_t)perms); if (err != UC_ERR_OK) { throwException(env, err); } diff --git a/include/unicorn/unicorn.h b/include/unicorn/unicorn.h old mode 100644 new mode 100755 index 556b6c1..c2d837a --- a/include/unicorn/unicorn.h +++ b/include/unicorn/unicorn.h @@ -389,27 +389,12 @@ UNICORN_EXPORT uc_err uc_hook_del(uch handle, uch *h2); typedef enum uc_prot { + UC_PROT_NONE = 0, UC_PROT_READ = 1, UC_PROT_WRITE = 2, + UC_PROT_ALL = 3, } uc_prot; -/* - Map memory in for emulation. - This API adds a memory region that can be used by emulation. The region is mapped - with permissions UC_PROT_READ | UC_PROT_WRITE. - - @handle: handle returned by uc_open() - @address: starting address of the new memory region to be mapped in. - 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 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 - for detailed error). -*/ -UNICORN_EXPORT -uc_err uc_mem_map(uch handle, uint64_t address, size_t size); - /* Map memory in for emulation. This API adds a memory region that can be used by emulation. @@ -427,7 +412,7 @@ uc_err uc_mem_map(uch handle, uint64_t address, size_t size); for detailed error). */ UNICORN_EXPORT -uc_err uc_mem_map_ex(uch handle, uint64_t address, size_t size, uint32_t perms); +uc_err uc_mem_map(uch handle, uint64_t address, size_t size, uint32_t perms); #ifdef __cplusplus } diff --git a/regress/block_test.c b/regress/block_test.c old mode 100644 new mode 100755 index cc372fe..3788f6f --- a/regress/block_test.c +++ b/regress/block_test.c @@ -39,7 +39,7 @@ int main() { } fprintf(stderr, "ok %d - uc_open\n", count++); - err = uc_mem_map(u, 0x1000000, 4096); + err = uc_mem_map(u, 0x1000000, 4096, UC_PROT_ALL); if (err != UC_ERR_OK) { fprintf(stderr, "not ok %d - %s\n", count++, uc_strerror(err)); exit(0); diff --git a/regress/map_crash.c b/regress/map_crash.c old mode 100644 new mode 100755 index ca16b56..54bcfac --- a/regress/map_crash.c +++ b/regress/map_crash.c @@ -23,7 +23,7 @@ int main() return 1; } 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_PROT_ALL)) { uc_mem_write (uh, UC_BUG_WRITE_ADDR, buf, size); } uc_close (&uh); diff --git a/regress/map_write.c b/regress/map_write.c old mode 100644 new mode 100755 index fc4343c..e5b9436 --- a/regress/map_write.c +++ b/regress/map_write.c @@ -18,7 +18,7 @@ int main() printf ("uc_open %d\n", err); return 1; } - err = uc_mem_map (uh, ADDR, SIZE); + err = uc_mem_map (uh, ADDR, SIZE, UC_PROT_ALL); if (err) { printf ("uc_mem_map %d\n", err); return 1; diff --git a/regress/nr_mem_test.c b/regress/nr_mem_test.c index 546173c..37c344d 100755 --- a/regress/nr_mem_test.c +++ b/regress/nr_mem_test.c @@ -67,9 +67,9 @@ int main(int argc, char **argv, char **envp) return 1; } - uc_mem_map_ex(handle, 0x100000, 0x1000, UC_PROT_READ); - uc_mem_map_ex(handle, 0x300000, 0x1000, UC_PROT_READ | UC_PROT_WRITE); - uc_mem_map_ex(handle, 0x400000, 0x1000, UC_PROT_WRITE); + uc_mem_map(handle, 0x100000, 0x1000, UC_PROT_READ); + uc_mem_map(handle, 0x300000, 0x1000, UC_PROT_READ | UC_PROT_WRITE); + uc_mem_map(handle, 0x400000, 0x1000, UC_PROT_WRITE); // write machine code to be emulated to memory if (uc_mem_write(handle, 0x100000, PROGRAM, sizeof(PROGRAM))) { diff --git a/regress/ro_mem_test.c b/regress/ro_mem_test.c old mode 100644 new mode 100755 index 1cce618..06edf06 --- a/regress/ro_mem_test.c +++ b/regress/ro_mem_test.c @@ -74,7 +74,7 @@ static bool hook_mem_invalid(uch handle, uc_mem_type type, 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); + uc_mem_map(handle, upper - 0x8000, 0x8000, UC_PROT_READ | UC_PROT_WRITE); // return true to indicate we want to continue return true; } @@ -114,14 +114,14 @@ int main(int argc, char **argv, char **envp) return 1; } - uc_mem_map(handle, 0x100000, 0x1000); - uc_mem_map(handle, 0x200000, 0x2000); - uc_mem_map(handle, 0x300000, 0x3000); - uc_mem_map_ex(handle, 0x400000, 0x4000, UC_PROT_READ); + uc_mem_map(handle, 0x100000, 0x1000, UC_PROT_ALL); + uc_mem_map(handle, 0x200000, 0x2000, UC_PROT_ALL); + uc_mem_map(handle, 0x300000, 0x3000, UC_PROT_ALL); + uc_mem_map(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); + uc_mem_map(handle, STACK, STACK_SIZE, UC_PROT_READ | UC_PROT_WRITE); } else { printf("Mapping stack on first invalid memory access\n"); } diff --git a/regress/sigill.c b/regress/sigill.c old mode 100644 new mode 100755 index 415a231..c8b5517 --- a/regress/sigill.c +++ b/regress/sigill.c @@ -34,7 +34,7 @@ int main() return 1; } 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_PROT_ALL)) { uc_mem_write (uh, UC_BUG_WRITE_ADDR, (const uint8_t*)"\xff\xff\xff\xff\xff\xff\xff\xff", 8); } diff --git a/regress/sigill2.c b/regress/sigill2.c old mode 100644 new mode 100755 index ca13282..a2a2d4f --- a/regress/sigill2.c +++ b/regress/sigill2.c @@ -18,7 +18,7 @@ int main() return 1; } 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_PROT_ALL)) { uc_mem_write (uh, UC_BUG_WRITE_ADDR, (const uint8_t*)"\xff\xff\xff\xff\xff\xff\xff\xff", 8); } diff --git a/samples/sample_arm.c b/samples/sample_arm.c old mode 100644 new mode 100755 index cb5cc6b..173e816 --- a/samples/sample_arm.c +++ b/samples/sample_arm.c @@ -47,7 +47,7 @@ static void test_arm(void) } // map 2MB memory for this emulation - uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024); + uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory uc_mem_write(handle, ADDRESS, (uint8_t *)ARM_CODE, sizeof(ARM_CODE) - 1); @@ -100,7 +100,7 @@ static void test_thumb(void) } // map 2MB memory for this emulation - uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024); + uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory uc_mem_write(handle, ADDRESS, (uint8_t *)THUMB_CODE, sizeof(THUMB_CODE) - 1); diff --git a/samples/sample_arm64.c b/samples/sample_arm64.c old mode 100644 new mode 100755 index f1c5ffb..b0d1608 --- a/samples/sample_arm64.c +++ b/samples/sample_arm64.c @@ -45,7 +45,7 @@ static void test_arm64(void) } // map 2MB memory for this emulation - uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024); + uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory uc_mem_write(handle, ADDRESS, (uint8_t *)ARM_CODE, sizeof(ARM_CODE) - 1); diff --git a/samples/sample_m68k.c b/samples/sample_m68k.c old mode 100644 new mode 100755 index 02452e5..82b06eb --- a/samples/sample_m68k.c +++ b/samples/sample_m68k.c @@ -60,7 +60,7 @@ static void test_m68k(void) } // map 2MB memory for this emulation - uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024); + uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory uc_mem_write(handle, ADDRESS, (uint8_t *)M68K_CODE, sizeof(M68K_CODE) - 1); diff --git a/samples/sample_mips.c b/samples/sample_mips.c old mode 100644 new mode 100755 index 43d0682..c9d0021 --- a/samples/sample_mips.c +++ b/samples/sample_mips.c @@ -44,7 +44,7 @@ static void test_mips_eb(void) } // map 2MB memory for this emulation - uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024); + uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory uc_mem_write(handle, ADDRESS, (uint8_t *)MIPS_CODE_EB, sizeof(MIPS_CODE_EB) - 1); @@ -94,7 +94,7 @@ static void test_mips_el(void) } // map 2MB memory for this emulation - uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024); + uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory uc_mem_write(handle, ADDRESS, (uint8_t *)MIPS_CODE_EL, sizeof(MIPS_CODE_EL) - 1); diff --git a/samples/sample_sparc.c b/samples/sample_sparc.c old mode 100644 new mode 100755 index 52bb373..c7f2971 --- a/samples/sample_sparc.c +++ b/samples/sample_sparc.c @@ -46,7 +46,7 @@ static void test_sparc(void) } // map 2MB memory for this emulation - uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024); + uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory uc_mem_write(handle, ADDRESS, (uint8_t *)SPARC_CODE, sizeof(SPARC_CODE) - 1); diff --git a/samples/sample_x86.c b/samples/sample_x86.c old mode 100644 new mode 100755 index 32c2ce9..9133dc3 --- a/samples/sample_x86.c +++ b/samples/sample_x86.c @@ -77,7 +77,7 @@ static bool hook_mem_invalid(uch handle, uc_mem_type type, printf(">>> Missing memory is being WRITE at 0x%"PRIx64 ", data size = %u, data value = 0x%"PRIx64 "\n", address, size, value); // map this memory in with 2MB in size - uc_mem_map(handle, 0xaaaa0000, 2 * 1024*1024); + uc_mem_map(handle, 0xaaaa0000, 2 * 1024*1024, UC_PROT_ALL); // return true to indicate we want to continue return true; } @@ -186,7 +186,7 @@ static void test_i386(void) } // map 2MB memory for this emulation - uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024); + uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory if (uc_mem_write(handle, ADDRESS, (uint8_t *)X86_CODE32, sizeof(X86_CODE32) - 1)) { @@ -245,7 +245,7 @@ static void test_i386_jump(void) } // map 2MB memory for this emulation - uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024); + uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory if (uc_mem_write(handle, ADDRESS, (uint8_t *)X86_CODE32_JUMP, @@ -292,7 +292,7 @@ static void test_i386_loop(void) } // map 2MB memory for this emulation - uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024); + uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory if (uc_mem_write(handle, ADDRESS, (uint8_t *)X86_CODE32_LOOP, sizeof(X86_CODE32_LOOP) - 1)) { @@ -344,7 +344,7 @@ static void test_i386_invalid_mem_read(void) } // map 2MB memory for this emulation - uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024); + uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory if (uc_mem_write(handle, ADDRESS, (uint8_t *)X86_CODE32_MEM_READ, sizeof(X86_CODE32_MEM_READ) - 1)) { @@ -402,7 +402,7 @@ static void test_i386_invalid_mem_write(void) } // map 2MB memory for this emulation - uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024); + uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory if (uc_mem_write(handle, ADDRESS, (uint8_t *)X86_CODE32_MEM_WRITE, sizeof(X86_CODE32_MEM_WRITE) - 1)) { @@ -473,7 +473,7 @@ static void test_i386_jump_invalid(void) } // map 2MB memory for this emulation - uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024); + uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory if (uc_mem_write(handle, ADDRESS, (uint8_t *)X86_CODE32_JMP_INVALID, sizeof(X86_CODE32_JMP_INVALID) - 1)) { @@ -530,7 +530,7 @@ static void test_i386_inout(void) } // map 2MB memory for this emulation - uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024); + uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory if (uc_mem_write(handle, ADDRESS, (uint8_t *)X86_CODE32_INOUT, sizeof(X86_CODE32_INOUT) - 1)) { @@ -605,7 +605,7 @@ static void test_x86_64(void) } // map 2MB memory for this emulation - uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024); + uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory if (uc_mem_write(handle, ADDRESS, (uint8_t *)X86_CODE64, sizeof(X86_CODE64) - 1)) { @@ -706,7 +706,7 @@ static void test_x86_64_syscall(void) } // map 2MB memory for this emulation - uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024); + uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory if (uc_mem_write(handle, ADDRESS, (uint8_t *)X86_CODE64_SYSCALL, sizeof(X86_CODE64_SYSCALL) - 1)) { @@ -758,7 +758,7 @@ static void test_x86_16(void) } // map 8KB memory for this emulation - uc_mem_map(handle, 0, 8 * 1024); + uc_mem_map(handle, 0, 8 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory if (uc_mem_write(handle, 0, (uint8_t *)X86_CODE16, sizeof(X86_CODE64) - 1)) { diff --git a/samples/shellcode.c b/samples/shellcode.c old mode 100644 new mode 100755 index 51dfb48..36c71ab --- a/samples/shellcode.c +++ b/samples/shellcode.c @@ -104,7 +104,7 @@ static void test_i386(void) } // map 2MB memory for this emulation - uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024); + uc_mem_map(handle, ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL); // write machine code to be emulated to memory if (uc_mem_write(handle, ADDRESS, (uint8_t *)X86_CODE32_SELF, sizeof(X86_CODE32_SELF) - 1)) { diff --git a/uc.c b/uc.c old mode 100644 new mode 100755 index 567d66d..a291590 --- a/uc.c +++ b/uc.c @@ -550,7 +550,7 @@ static uc_err _hook_mem_access(uch handle, uc_mem_type type, } UNICORN_EXPORT -uc_err uc_mem_map_ex(uch handle, uint64_t address, size_t size, uint32_t perms) +uc_err uc_mem_map(uch handle, uint64_t address, size_t size, uint32_t perms) { MemoryRegion **regions; struct uc_struct* uc = (struct uc_struct *)handle; @@ -588,13 +588,6 @@ uc_err uc_mem_map_ex(uch handle, uint64_t address, size_t size, uint32_t perms) return UC_ERR_OK; } -UNICORN_EXPORT -uc_err uc_mem_map(uch handle, uint64_t address, size_t size) -{ - //old api, maps RW by default - return uc_mem_map_ex(handle, address, size, UC_PROT_READ | UC_PROT_WRITE); -} - MemoryRegion *memory_mapping(struct uc_struct* uc, uint64_t address) { unsigned int i;