update java bindings for type safety and API changes

This commit is contained in:
Chris Eagle
2015-09-03 22:28:09 -07:00
parent bae718274e
commit b3b814e8b1
11 changed files with 218 additions and 218 deletions

View File

@ -52,14 +52,14 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
// @address: address where the code is being executed
// @size: size of machine instruction being executed
// @user_data: user data passed to tracing APIs.
static void cb_hookcode(uch handle, uint64_t address, uint32_t size, void *user_data) {
static void cb_hookcode(ucengine *eng, uint64_t address, uint32_t size, void *user_data) {
JNIEnv *env;
(*cachedJVM)->AttachCurrentThread(cachedJVM, (void **)&env, NULL);
jclass clz = (*env)->FindClass(env, "unicorn/Unicorn");
if ((*env)->ExceptionCheck(env)) {
return;
}
(*env)->CallStaticVoidMethod(env, clz, invokeCodeCallbacks, (jlong)handle, (jlong)address, (int)size);
(*env)->CallStaticVoidMethod(env, clz, invokeCodeCallbacks, (jlong)eng, (jlong)address, (int)size);
(*cachedJVM)->DetachCurrentThread(cachedJVM);
}
@ -67,28 +67,28 @@ static void cb_hookcode(uch handle, uint64_t address, uint32_t size, void *user_
// @address: address where the code is being executed
// @size: size of machine instruction being executed
// @user_data: user data passed to tracing APIs.
static void cb_hookblock(uch handle, uint64_t address, uint32_t size, void *user_data) {
static void cb_hookblock(ucengine *eng, uint64_t address, uint32_t size, void *user_data) {
JNIEnv *env;
(*cachedJVM)->AttachCurrentThread(cachedJVM, (void **)&env, NULL);
jclass clz = (*env)->FindClass(env, "unicorn/Unicorn");
if ((*env)->ExceptionCheck(env)) {
return;
}
(*env)->CallStaticVoidMethod(env, clz, invokeBlockCallbacks, (jlong)handle, (jlong)address, (int)size);
(*env)->CallStaticVoidMethod(env, clz, invokeBlockCallbacks, (jlong)eng, (jlong)address, (int)size);
(*cachedJVM)->DetachCurrentThread(cachedJVM);
}
// Callback function for tracing interrupts (for uc_hook_intr())
// @intno: interrupt number
// @user_data: user data passed to tracing APIs.
static void cb_hookintr(uch handle, uint32_t intno, void *user_data) {
static void cb_hookintr(ucengine *eng, uint32_t intno, void *user_data) {
JNIEnv *env;
(*cachedJVM)->AttachCurrentThread(cachedJVM, (void **)&env, NULL);
jclass clz = (*env)->FindClass(env, "unicorn/Unicorn");
if ((*env)->ExceptionCheck(env)) {
return;
}
(*env)->CallStaticVoidMethod(env, clz, invokeInterruptCallbacks, (jlong)handle, (int)intno);
(*env)->CallStaticVoidMethod(env, clz, invokeInterruptCallbacks, (jlong)eng, (int)intno);
(*cachedJVM)->DetachCurrentThread(cachedJVM);
}
@ -96,7 +96,7 @@ static void cb_hookintr(uch handle, uint32_t intno, void *user_data) {
// @port: port number
// @size: data size (1/2/4) to be read from this port
// @user_data: user data passed to tracing APIs.
static uint32_t cb_insn_in(uch handle, uint32_t port, int size, void *user_data) {
static uint32_t cb_insn_in(ucengine *eng, uint32_t port, int size, void *user_data) {
JNIEnv *env;
uint32_t res = 0;
(*cachedJVM)->AttachCurrentThread(cachedJVM, (void **)&env, NULL);
@ -104,7 +104,7 @@ static uint32_t cb_insn_in(uch handle, uint32_t port, int size, void *user_data)
if ((*env)->ExceptionCheck(env)) {
return 0;
}
res = (uint32_t)(*env)->CallStaticIntMethod(env, clz, invokeInCallbacks, (jlong)handle, (jint)port, (jint)size);
res = (uint32_t)(*env)->CallStaticIntMethod(env, clz, invokeInCallbacks, (jlong)eng, (jint)port, (jint)size);
(*cachedJVM)->DetachCurrentThread(cachedJVM);
return res;
}
@ -113,26 +113,26 @@ static uint32_t cb_insn_in(uch handle, uint32_t port, int size, void *user_data)
// @port: port number
// @size: data size (1/2/4) to be written to this port
// @value: data value to be written to this port
static void cb_insn_out(uch handle, uint32_t port, int size, uint32_t value, void *user_data) {
static void cb_insn_out(ucengine *eng, uint32_t port, int size, uint32_t value, void *user_data) {
JNIEnv *env;
(*cachedJVM)->AttachCurrentThread(cachedJVM, (void **)&env, NULL);
jclass clz = (*env)->FindClass(env, "unicorn/Unicorn");
if ((*env)->ExceptionCheck(env)) {
return;
}
(*env)->CallStaticVoidMethod(env, clz, invokeOutCallbacks, (jlong)handle, (jint)port, (jint)size, (jint)value);
(*env)->CallStaticVoidMethod(env, clz, invokeOutCallbacks, (jlong)eng, (jint)port, (jint)size, (jint)value);
(*cachedJVM)->DetachCurrentThread(cachedJVM);
}
// x86's handler for SYSCALL/SYSENTER
static void cb_insn_syscall(uch handle, void *user_data) {
static void cb_insn_syscall(ucengine *eng, void *user_data) {
JNIEnv *env;
(*cachedJVM)->AttachCurrentThread(cachedJVM, (void **)&env, NULL);
jclass clz = (*env)->FindClass(env, "unicorn/Unicorn");
if ((*env)->ExceptionCheck(env)) {
return;
}
(*env)->CallStaticVoidMethod(env, clz, invokeSyscallCallbacks, (jlong)handle);
(*env)->CallStaticVoidMethod(env, clz, invokeSyscallCallbacks, (jlong)eng);
(*cachedJVM)->DetachCurrentThread(cachedJVM);
}
@ -142,7 +142,7 @@ static void cb_insn_syscall(uch handle, void *user_data) {
// @size: size of data being read or written
// @value: value of data being written to memory, or irrelevant if type = READ.
// @user_data: user data passed to tracing APIs
static void cb_hookmem(uch handle, uc_mem_type type,
static void cb_hookmem(ucengine *eng, uc_mem_type type,
uint64_t address, int size, int64_t value, void *user_data) {
JNIEnv *env;
(*cachedJVM)->AttachCurrentThread(cachedJVM, (void **)&env, NULL);
@ -152,13 +152,13 @@ static void cb_hookmem(uch handle, uc_mem_type type,
}
switch (type) {
case UC_MEM_READ:
(*env)->CallStaticVoidMethod(env, clz, invokeReadCallbacks, (jlong)handle, (jlong)address, (int)size);
(*env)->CallStaticVoidMethod(env, clz, invokeReadCallbacks, (jlong)eng, (jlong)address, (int)size);
break;
case UC_MEM_WRITE:
(*env)->CallStaticVoidMethod(env, clz, invokeWriteCallbacks, (jlong)handle, (jlong)address, (int)size, (jlong)value);
(*env)->CallStaticVoidMethod(env, clz, invokeWriteCallbacks, (jlong)eng, (jlong)address, (int)size, (jlong)value);
break;
case UC_MEM_READ_WRITE:
(*env)->CallStaticVoidMethod(env, clz, invokeReadWriteCallbacks, (jlong)handle, (int)type, (jlong)address, (int)size, (jlong)value);
(*env)->CallStaticVoidMethod(env, clz, invokeReadWriteCallbacks, (jlong)eng, (int)type, (jlong)address, (int)size, (jlong)value);
break;
}
(*cachedJVM)->DetachCurrentThread(cachedJVM);
@ -171,15 +171,15 @@ static void cb_hookmem(uch handle, uc_mem_type type,
// @value: value of data being written to memory, or irrelevant if type = READ.
// @user_data: user data passed to tracing APIs
// @return: return true to continue, or false to stop program (due to invalid memory).
static bool cb_eventmem(uch handle, uc_mem_type type,
uint64_t address, int size, int64_t value, void *user_data) {
static bool cb_eventmem(ucengine *eng, uc_mem_type type,
uint64_t address, int size, int64_t value, void *user_data) {
JNIEnv *env;
(*cachedJVM)->AttachCurrentThread(cachedJVM, (void **)&env, NULL);
jclass clz = (*env)->FindClass(env, "unicorn/Unicorn");
if ((*env)->ExceptionCheck(env)) {
return false;
}
jboolean res = (*env)->CallStaticBooleanMethod(env, clz, invokeMemInvalidCallbacks, (jlong)handle, (int)type, (jlong)address, (int)size, (jlong)value);
jboolean res = (*env)->CallStaticBooleanMethod(env, clz, invokeMemInvalidCallbacks, (jlong)eng, (int)type, (jlong)address, (int)size, (jlong)value);
(*cachedJVM)->DetachCurrentThread(cachedJVM);
return res;
}
@ -193,16 +193,16 @@ static void throwException(JNIEnv *env, uc_err err) {
}
}
static uch getHandle(JNIEnv *env, jobject self) {
static ucengine *getEngine(JNIEnv *env, jobject self) {
static int haveFid = 0;
static jfieldID fid;
if (haveFid == 0) {
//cache the field id
jclass clazz = (*env)->GetObjectClass(env, self);
fid = (*env)->GetFieldID(env, clazz, "handle", "J");
fid = (*env)->GetFieldID(env, clazz, "eng", "J");
haveFid = 1;
}
return (uch)(*env)->GetLongField(env, self, fid);
return (ucengine *)(*env)->GetLongField(env, self, fid);
}
/*
@ -212,12 +212,12 @@ static uch getHandle(JNIEnv *env, jobject self) {
*/
JNIEXPORT jlong JNICALL Java_unicorn_Unicorn_open
(JNIEnv *env, jobject self, jint arch, jint mode) {
uch handle = -1;
uc_err err = uc_open((uc_arch)arch, (uc_mode)mode, &handle);
ucengine *eng = NULL;
uc_err err = uc_open((uc_arch)arch, (uc_mode)mode, &eng);
if (err != UC_ERR_OK) {
throwException(env, err);
}
return (jlong)handle;
return (jlong)eng;
}
/*
@ -247,8 +247,8 @@ JNIEXPORT jboolean JNICALL Java_unicorn_Unicorn_arch_1supported
*/
JNIEXPORT void JNICALL Java_unicorn_Unicorn_close
(JNIEnv *env, jobject self) {
uch handle = getHandle(env, self);
uc_close(&handle);
ucengine *eng = getEngine(env, self);
uc_close(eng);
}
/*
@ -258,8 +258,8 @@ JNIEXPORT void JNICALL Java_unicorn_Unicorn_close
*/
JNIEXPORT jint JNICALL Java_unicorn_Unicorn_errno
(JNIEnv *env, jobject self) {
uch handle = getHandle(env, self);
return (jint)uc_errno(handle);
ucengine *eng = getEngine(env, self);
return (jint)uc_errno(eng);
}
/*
@ -281,9 +281,9 @@ JNIEXPORT jstring JNICALL Java_unicorn_Unicorn_strerror
*/
JNIEXPORT void JNICALL Java_unicorn_Unicorn_reg_1write
(JNIEnv *env, jobject self, jint regid, jbyteArray value) {
uch handle = getHandle(env, self);
ucengine *eng = getEngine(env, self);
jbyte *array = (*env)->GetByteArrayElements(env, value, NULL);
uc_err err = uc_reg_write(handle, (int)regid, (void *)array);
uc_err err = uc_reg_write(eng, (int)regid, (void *)array);
if (err != UC_ERR_OK) {
throwException(env, err);
}
@ -297,10 +297,10 @@ JNIEXPORT void JNICALL Java_unicorn_Unicorn_reg_1write
*/
JNIEXPORT jbyteArray JNICALL Java_unicorn_Unicorn_reg_1read
(JNIEnv *env, jobject self, jint regid, jint regsz) {
uch handle = getHandle(env, self);
ucengine *eng = getEngine(env, self);
jbyteArray regval = (*env)->NewByteArray(env, (jsize)regsz);
jbyte *array = (*env)->GetByteArrayElements(env, regval, NULL);
uc_err err = uc_reg_read(handle, (int)regid, (void *)array);
uc_err err = uc_reg_read(eng, (int)regid, (void *)array);
if (err != UC_ERR_OK) {
throwException(env, err);
}
@ -316,10 +316,10 @@ JNIEXPORT jbyteArray JNICALL Java_unicorn_Unicorn_reg_1read
JNIEXPORT void JNICALL Java_unicorn_Unicorn_mem_1write
(JNIEnv *env , jobject self, jlong address, jbyteArray bytes) {
uch handle = getHandle(env, self);
ucengine *eng = getEngine(env, self);
jbyte *array = (*env)->GetByteArrayElements(env, bytes, NULL);
jsize size = (*env)->GetArrayLength(env, bytes);
uc_err err = uc_mem_write(handle, (uint64_t)address, (uint8_t *)array, (size_t)size);
uc_err err = uc_mem_write(eng, (uint64_t)address, (uint8_t *)array, (size_t)size);
if (err != UC_ERR_OK) {
throwException(env, err);
@ -335,11 +335,11 @@ JNIEXPORT void JNICALL Java_unicorn_Unicorn_mem_1write
*/
JNIEXPORT jbyteArray JNICALL Java_unicorn_Unicorn_mem_1read
(JNIEnv *env, jobject self, jlong address, jlong size) {
uch handle = getHandle(env, self);
ucengine *eng = getEngine(env, self);
jbyteArray bytes = (*env)->NewByteArray(env, (jsize)size);
jbyte *array = (*env)->GetByteArrayElements(env, bytes, NULL);
uc_err err = uc_mem_read(handle, (uint64_t)address, (uint8_t *)array, (size_t)size);
uc_err err = uc_mem_read(eng, (uint64_t)address, (uint8_t *)array, (size_t)size);
if (err != UC_ERR_OK) {
throwException(env, err);
}
@ -354,9 +354,9 @@ JNIEXPORT jbyteArray JNICALL Java_unicorn_Unicorn_mem_1read
*/
JNIEXPORT void JNICALL Java_unicorn_Unicorn_emu_1start
(JNIEnv *env, jobject self, jlong begin, jlong until, jlong timeout, jlong count) {
uch handle = getHandle(env, self);
ucengine *eng = getEngine(env, self);
uc_err err = uc_emu_start(handle, (uint64_t)begin, (uint64_t)until, (uint64_t)timeout, (size_t)count);
uc_err err = uc_emu_start(eng, (uint64_t)begin, (uint64_t)until, (uint64_t)timeout, (size_t)count);
if (err != UC_ERR_OK) {
throwException(env, err);
}
@ -369,9 +369,9 @@ JNIEXPORT void JNICALL Java_unicorn_Unicorn_emu_1start
*/
JNIEXPORT void JNICALL Java_unicorn_Unicorn_emu_1stop
(JNIEnv *env, jobject self) {
uch handle = getHandle(env, self);
ucengine *eng = getEngine(env, self);
uc_err err = uc_emu_stop(handle);
uc_err err = uc_emu_stop(eng);
if (err != UC_ERR_OK) {
throwException(env, err);
}
@ -383,24 +383,24 @@ JNIEXPORT void JNICALL Java_unicorn_Unicorn_emu_1stop
* Signature: (JI)J
*/
JNIEXPORT jlong JNICALL Java_unicorn_Unicorn_registerHook__JI
(JNIEnv *env, jclass clz, jlong handle, jint type) {
uch h2 = 0;
(JNIEnv *env, jclass clz, jlong eng, jint type) {
uchook hh = 0;
uc_err err = 0;
switch (type) {
case UC_HOOK_INTR: // Hook all interrupt events
if (invokeInterruptCallbacks == 0) {
invokeInterruptCallbacks = (*env)->GetStaticMethodID(env, clz, "invokeInterruptCallbacks", "(JI)V");
}
err = uc_hook_add((uch)handle, &h2, (uc_hook_t)type, cb_hookintr, env);
err = uc_hook_add((ucengine*)eng, &hh, (uc_hook_t)type, cb_hookintr, env);
break;
case UC_HOOK_MEM_INVALID: // Hook for all invalid memory access events
if (invokeMemInvalidCallbacks == 0) {
invokeMemInvalidCallbacks = (*env)->GetStaticMethodID(env, clz, "invokeMemInvalidCallbacks", "(JIJIJ)Z");
}
err = uc_hook_add((uch)handle, &h2, (uc_hook_t)type, cb_eventmem, env);
err = uc_hook_add((ucengine*)eng, &hh, (uc_hook_t)type, cb_eventmem, env);
break;
}
return (jlong)h2;
return (jlong)hh;
}
/*
@ -409,8 +409,8 @@ JNIEXPORT jlong JNICALL Java_unicorn_Unicorn_registerHook__JI
* Signature: (JII)J
*/
JNIEXPORT jlong JNICALL Java_unicorn_Unicorn_registerHook__JII
(JNIEnv *env, jclass clz, jlong handle, jint type, jint arg1) {
uch h2 = 0;
(JNIEnv *env, jclass clz, jlong eng, jint type, jint arg1) {
uchook hh = 0;
uc_err err = 0;
switch (type) {
case UC_HOOK_INSN: // Hook a particular instruction
@ -419,22 +419,22 @@ JNIEXPORT jlong JNICALL Java_unicorn_Unicorn_registerHook__JII
if (invokeOutCallbacks == 0) {
invokeOutCallbacks = (*env)->GetStaticMethodID(env, clz, "invokeOutCallbacks", "(JIII)V");
}
err = uc_hook_add((uch)handle, &h2, (uc_hook_t)type, cb_insn_out, env, arg1);
err = uc_hook_add((ucengine*)eng, &hh, (uc_hook_t)type, cb_insn_out, env, arg1);
case UC_X86_INS_IN:
if (invokeInCallbacks == 0) {
invokeInCallbacks = (*env)->GetStaticMethodID(env, clz, "invokeInCallbacks", "(JII)I");
}
err = uc_hook_add((uch)handle, &h2, (uc_hook_t)type, cb_insn_in, env, arg1);
err = uc_hook_add((ucengine*)eng, &hh, (uc_hook_t)type, cb_insn_in, env, arg1);
case UC_X86_INS_SYSENTER:
case UC_X86_INS_SYSCALL:
if (invokeSyscallCallbacks == 0) {
invokeSyscallCallbacks = (*env)->GetStaticMethodID(env, clz, "invokeSyscallCallbacks", "(J)V");
}
err = uc_hook_add((uch)handle, &h2, (uc_hook_t)type, cb_insn_syscall, env, arg1);
err = uc_hook_add((ucengine*)eng, &hh, (uc_hook_t)type, cb_insn_syscall, env, arg1);
}
break;
}
return (jlong)h2;
return (jlong)hh;
}
/*
@ -443,42 +443,42 @@ JNIEXPORT jlong JNICALL Java_unicorn_Unicorn_registerHook__JII
* Signature: (JIJJ)J
*/
JNIEXPORT jlong JNICALL Java_unicorn_Unicorn_registerHook__JIJJ
(JNIEnv *env, jclass clz, jlong handle, jint type, jlong arg1, jlong arg2) {
uch h2 = 0;
(JNIEnv *env, jclass clz, jlong eng, jint type, jlong arg1, jlong arg2) {
uchook hh = 0;
uc_err err = 0;
switch (type) {
case UC_HOOK_CODE: // Hook a range of code
if (invokeCodeCallbacks == 0) {
invokeCodeCallbacks = (*env)->GetStaticMethodID(env, clz, "invokeCodeCallbacks", "(JJI)V");
}
err = uc_hook_add((uch)handle, &h2, (uc_hook_t)type, cb_hookcode, env, arg1, arg2);
err = uc_hook_add((ucengine*)eng, &hh, (uc_hook_t)type, cb_hookcode, env, arg1, arg2);
break;
case UC_HOOK_BLOCK: // Hook basic blocks
if (invokeBlockCallbacks == 0) {
invokeBlockCallbacks = (*env)->GetStaticMethodID(env, clz, "invokeBlockCallbacks", "(JJI)V");
}
err = uc_hook_add((uch)handle, &h2, (uc_hook_t)type, cb_hookblock, env, arg1, arg2);
err = uc_hook_add((ucengine*)eng, &hh, (uc_hook_t)type, cb_hookblock, env, arg1, arg2);
break;
case UC_HOOK_MEM_READ: // Hook all memory read events.
if (invokeReadCallbacks == 0) {
invokeReadCallbacks = (*env)->GetStaticMethodID(env, clz, "invokeReadCallbacks", "(JJI)V");
}
err = uc_hook_add((uch)handle, &h2, (uc_hook_t)type, cb_hookmem, env, arg1, arg2);
err = uc_hook_add((ucengine*)eng, &hh, (uc_hook_t)type, cb_hookmem, env, arg1, arg2);
break;
case UC_HOOK_MEM_WRITE: // Hook all memory write events.
if (invokeWriteCallbacks == 0) {
invokeWriteCallbacks = (*env)->GetStaticMethodID(env, clz, "invokeWriteCallbacks", "(JJIJ)V");
}
err = uc_hook_add((uch)handle, &h2, (uc_hook_t)type, cb_hookmem, env, arg1, arg2);
err = uc_hook_add((ucengine*)eng, &hh, (uc_hook_t)type, cb_hookmem, env, arg1, arg2);
break;
case UC_HOOK_MEM_READ_WRITE: // Hook all memory accesses (either READ or WRITE).
if (invokeReadWriteCallbacks == 0) {
invokeReadWriteCallbacks = (*env)->GetStaticMethodID(env, clz, "invokeReadWriteCallbacks", "(JIJIJ)V");
}
err = uc_hook_add((uch)handle, &h2, (uc_hook_t)type, cb_hookmem, env, arg1, arg2);
err = uc_hook_add((ucengine*)eng, &hh, (uc_hook_t)type, cb_hookmem, env, arg1, arg2);
break;
}
return (jlong)h2;
return (jlong)hh;
}
/*
@ -487,13 +487,12 @@ JNIEXPORT jlong JNICALL Java_unicorn_Unicorn_registerHook__JIJJ
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_unicorn_Unicorn_hook_1del
(JNIEnv *env, jobject self, jlong hook) {
uch handle = getHandle(env, self);
uch h2 = (uch)hook;
(JNIEnv *env, jobject self, jlong hh) {
ucengine *eng = getEngine(env, self);
//**** TODO remove hook from any internal hook tables as well
uc_err err = uc_hook_del(handle, &h2);
uc_err err = uc_hook_del(eng, (uchook)hh);
if (err != UC_ERR_OK) {
throwException(env, err);
}
@ -506,9 +505,9 @@ JNIEXPORT void JNICALL Java_unicorn_Unicorn_hook_1del
*/
JNIEXPORT void JNICALL Java_unicorn_Unicorn_mem_1map
(JNIEnv *env, jobject self, jlong address, jlong size, jint perms) {
uch handle = getHandle(env, self);
ucengine *eng = getEngine(env, self);
uc_err err = uc_mem_map(handle, (uint64_t)address, (size_t)size, (uint32_t)perms);
uc_err err = uc_mem_map(eng, (uint64_t)address, (size_t)size, (uint32_t)perms);
if (err != UC_ERR_OK) {
throwException(env, err);
}
@ -521,9 +520,9 @@ JNIEXPORT void JNICALL Java_unicorn_Unicorn_mem_1map
*/
JNIEXPORT void JNICALL Java_unicorn_Unicorn_mem_1unmap
(JNIEnv *env, jobject self, jlong address, jlong size) {
uch handle = getHandle(env, self);
ucengine *eng = getEngine(env, self);
uc_err err = uc_mem_unmap(handle, (uint64_t)address, (size_t)size);
uc_err err = uc_mem_unmap(eng, (uint64_t)address, (size_t)size);
if (err != UC_ERR_OK) {
throwException(env, err);
}
@ -536,9 +535,9 @@ JNIEXPORT void JNICALL Java_unicorn_Unicorn_mem_1unmap
*/
JNIEXPORT void JNICALL Java_unicorn_Unicorn_mem_1protect
(JNIEnv *env, jobject self, jlong address, jlong size, jint perms) {
uch handle = getHandle(env, self);
ucengine *eng = getEngine(env, self);
uc_err err = uc_mem_protect(handle, (uint64_t)address, (size_t)size, (uint32_t)perms);
uc_err err = uc_mem_protect(eng, (uint64_t)address, (size_t)size, (uint32_t)perms);
if (err != UC_ERR_OK) {
throwException(env, err);
}