java_lang_String_p fastiva_Dalvik_java_lang_System_mapLibraryName(java_lang_String_p nameObj) { #endif StringObject* result = NULL; char* name; char* mappedName; if (nameObj == NULL) { dvmThrowNullPointerException("userLibName == null"); THROW_V(); } name = dvmCreateCstrFromString(nameObj); #ifdef _DEBUG assert(strstr(name, "cfb") == NULL); #endif mappedName = dvmCreateSystemLibraryName(name); if (mappedName != NULL) { result = dvmCreateStringFromCstr(mappedName); dvmReleaseTrackedAlloc((Object*) result, NULL); } free(name); free(mappedName); RETURN_PTR((java_lang_String_p)result); }
/* * Create a "stock instance" of an exception class. */ static Object* createStockException(const char* descriptor, const char* msg) { Thread* self = dvmThreadSelf(); StringObject* msgStr = NULL; ClassObject* clazz; Method* init; Object* obj; /* find class, initialize if necessary */ clazz = dvmFindSystemClass(descriptor); if (clazz == NULL) { LOGE("Unable to find %s", descriptor); return NULL; } init = dvmFindDirectMethodByDescriptor(clazz, "<init>", "(Ljava/lang/String;)V"); if (init == NULL) { LOGE("Unable to find String-arg constructor for %s", descriptor); return NULL; } obj = dvmAllocObject(clazz, ALLOC_DEFAULT); if (obj == NULL) return NULL; if (msg == NULL) { msgStr = NULL; } else { msgStr = dvmCreateStringFromCstr(msg); if (msgStr == NULL) { LOGW("Could not allocate message string \"%s\"", msg); dvmReleaseTrackedAlloc(obj, self); return NULL; } } JValue unused; dvmCallMethod(self, init, obj, &unused, msgStr); if (dvmCheckException(self)) { dvmReleaseTrackedAlloc((Object*) msgStr, self); dvmReleaseTrackedAlloc(obj, self); return NULL; } dvmReleaseTrackedAlloc((Object*) msgStr, self); // okay if msgStr NULL return obj; }
/* * Create a new java.lang.reflect.Field object from "field". * * The Field spec doesn't specify the constructor. We're going to use the * one from our existing class libs: * * private Field(Class declaringClass, Class type, String name, int slot) */ static Object* createFieldObject(Field* field, const ClassObject* clazz) { Object* result = NULL; Object* fieldObj = NULL; StringObject* nameObj = NULL; ClassObject* type; char* mangle; char* cp; int slot, field_idx; assert(dvmIsClassInitialized(gDvm.classJavaLangReflectField)); fieldObj = dvmAllocObject(gDvm.classJavaLangReflectField, ALLOC_DEFAULT); if (fieldObj == NULL) goto bail; cp = mangle = strdup(field->signature); type = convertSignaturePartToClass(&cp, clazz); free(mangle); if (type == NULL) goto bail; nameObj = dvmCreateStringFromCstr(field->name); if (nameObj == NULL) goto bail; slot = fieldToSlot(field, clazz); field_idx = dvmGetFieldIdx(field); JValue unused; dvmCallMethod(dvmThreadSelf(), gDvm.methJavaLangReflectField_init, fieldObj, &unused, clazz, type, nameObj, slot, field_idx); if (dvmCheckException(dvmThreadSelf())) { ALOGD("Field class init threw exception"); goto bail; } result = fieldObj; bail: dvmReleaseTrackedAlloc((Object*) nameObj, NULL); if (result == NULL) dvmReleaseTrackedAlloc((Object*) fieldObj, NULL); /* caller must dvmReleaseTrackedAlloc(result) */ return result; }
/* * private static String[] getClassNameList(int cookie) * * Returns a String array that holds the names of all classes in the * specified DEX file. */ static void Dalvik_dalvik_system_DexFile_getClassNameList(const u4* args, JValue* pResult) { int cookie = args[0]; DexOrJar* pDexOrJar = (DexOrJar*) cookie; DvmDex* pDvmDex; DexFile* pDexFile; ArrayObject* stringArray; Thread* self = dvmThreadSelf(); if (!validateCookie(cookie)) RETURN_VOID(); if (pDexOrJar->isDex) pDvmDex = dvmGetRawDexFileDex(pDexOrJar->pRawDexFile); else pDvmDex = dvmGetJarFileDex(pDexOrJar->pJarFile); assert(pDvmDex != NULL); pDexFile = pDvmDex->pDexFile; int count = pDexFile->pHeader->classDefsSize; stringArray = dvmAllocObjectArray(gDvm.classJavaLangString, count, ALLOC_DEFAULT); if (stringArray == NULL) { /* probably OOM */ ALOGD("Failed allocating array of %d strings\n", count); assert(dvmCheckException(self)); RETURN_VOID(); } int i; for (i = 0; i < count; i++) { const DexClassDef* pClassDef = dexGetClassDef(pDexFile, i); const char* descriptor = dexStringByTypeIdx(pDexFile, pClassDef->classIdx); char* className = dvmDescriptorToDot(descriptor); StringObject* str = dvmCreateStringFromCstr(className); dvmSetObjectArrayElement(stringArray, i, (Object *)str); dvmReleaseTrackedAlloc((Object *)str, self); free(className); } dvmReleaseTrackedAlloc((Object*)stringArray, self); RETURN_PTR(stringArray); }
/* * Convert an array of char* into a String[]. * * Returns NULL on failure, with an exception raised. */ static ArrayObject* convertStringArray(char** strings, size_t count) { Thread* self = dvmThreadSelf(); /* * Allocate an array to hold the String objects. */ ClassObject* stringArrayClass = dvmFindArrayClass("[Ljava/lang/String;", NULL); if (stringArrayClass == NULL) { /* shouldn't happen */ LOGE("Unable to find [Ljava/lang/String;\n"); dvmAbort(); } ArrayObject* stringArray = dvmAllocArrayByClass(stringArrayClass, count, ALLOC_DEFAULT); if (stringArray == NULL) { /* probably OOM */ LOGD("Failed allocating array of %d strings\n", count); assert(dvmCheckException(self)); return NULL; } /* * Create the individual String objects and add them to the array. */ size_t i; for (i = 0; i < count; i++) { Object *str = (Object *)dvmCreateStringFromCstr(strings[i]); if (str == NULL) { /* probably OOM; drop out now */ assert(dvmCheckException(self)); dvmReleaseTrackedAlloc((Object*)stringArray, self); return NULL; } dvmSetObjectArrayElement(stringArray, i, str); /* stored in tracked array, okay to release */ dvmReleaseTrackedAlloc(str, self); } dvmReleaseTrackedAlloc((Object*)stringArray, self); return stringArray; }
/* * private static String[] getClassNameList(int cookie) * * Returns a String array that holds the names of all classes in the * specified DEX file. */ static void Dalvik_dalvik_system_DexFile_getClassNameList(const u4* args, JValue* pResult) { int cookie = args[0]; DexOrJar* pDexOrJar = (DexOrJar*) cookie; DvmDex* pDvmDex; DexFile* pDexFile; ArrayObject* stringArray; if (!validateCookie(cookie)) RETURN_VOID(); if (pDexOrJar->isDex) pDvmDex = dvmGetRawDexFileDex(pDexOrJar->pRawDexFile); else pDvmDex = dvmGetJarFileDex(pDexOrJar->pJarFile); assert(pDvmDex != NULL); pDexFile = pDvmDex->pDexFile; int count = pDexFile->pHeader->classDefsSize; stringArray = dvmAllocObjectArray(gDvm.classJavaLangString, count, ALLOC_DEFAULT); if (stringArray == NULL) RETURN_VOID(); // should be an OOM pending StringObject** contents = (StringObject**) stringArray->contents; int i; for (i = 0; i < count; i++) { const DexClassDef* pClassDef = dexGetClassDef(pDexFile, i); const char* descriptor = dexStringByTypeIdx(pDexFile, pClassDef->classIdx); char* className = dvmDescriptorToDot(descriptor); contents[i] = dvmCreateStringFromCstr(className, ALLOC_DEFAULT); dvmReleaseTrackedAlloc((Object*) contents[i], NULL); free(className); } dvmReleaseTrackedAlloc((Object*)stringArray, NULL); RETURN_PTR(stringArray); }
/* * public static String mapLibraryName(String libname) */ static void Dalvik_java_lang_System_mapLibraryName(const u4* args, JValue* pResult) { StringObject* nameObj = (StringObject*) args[0]; StringObject* result = NULL; char* name; char* mappedName; if (nameObj == NULL) { dvmThrowException("Ljava/lang/NullPointerException;", NULL); RETURN_VOID(); } name = dvmCreateCstrFromString(nameObj); mappedName = dvmCreateSystemLibraryName(name); if (mappedName != NULL) { result = dvmCreateStringFromCstr(mappedName); dvmReleaseTrackedAlloc((Object*) result, NULL); } free(name); free(mappedName); RETURN_PTR(result); }
/** * 使用jni GetMethodID 方法获取jmethodID 强制转为 Method 的hook 方法 示例 */ static void newTestMethod(const u4* args, JValue* pResult, const Method* method, struct Thread* self) { // args 是原来函数的参数数组, 原来test函数只有一个String型参数 // 并且要注意, 如果是不是static函数, 下标0 是函数所在类的实例obj // 在dvm中Object, jni 中的jobject 和 java 中的 Object类 都不是同一个东西 // String类对应StringObject // 取出参数打印出来看看 StringObject* param1 = NULL; if(dvmIsStaticMethod(method)) param1 = (StringObject*)args[0]; else param1 = (StringObject*)args[1]; LOGD("param1:%s",dvmCreateCstrFromString(param1)); //JValue 是个union ,要返回int 就 pResult->i=1; 返回Object对象就 pResult->l = ojb; // 但是, 在dvm中的Object, jni 中的jobject 和 java 中的 Object类 都不是同一个东西 // 所以, 我们这里使用dvm的函数来创建一个StringObject* pResult->l = dvmCreateStringFromCstr("newTestMethod"); // 一般情况应该使用宏 : RETURN_XXX(result); return; }
static void returnCString(JValue* pResult, const char* s) { Object* result = (Object*) dvmCreateStringFromCstr(s); dvmReleaseTrackedAlloc(result, dvmThreadSelf()); DALVIK_RETURN_PTR(result); }
java_lang_String_p fastiva_makeString(const char* s) { Object* result = (Object*) dvmCreateStringFromCstr(s); dvmReleaseTrackedAlloc(result, dvmThreadSelf()); RETURN_PTR((java_lang_String_p)result); }
/* * Create a new java/lang/reflect/Method object, using the contents of * "meth" to construct it. * * The spec doesn't specify the constructor. We're going to use the * one from our existing class libs: * * private Method(Class declaring, Class[] paramTypes, Class[] exceptTypes, * Class returnType, String name, int slot) * * The caller must call dvmReleaseTrackedAlloc() on the result. */ Object* dvmCreateReflectMethodObject(const Method* meth) { Object* result = NULL; ArrayObject* params = NULL; ArrayObject* exceptions = NULL; StringObject* nameObj = NULL; Object* methObj; ClassObject* returnType; DexStringCache mangle; char* cp; int slot, method_idx; if (dvmCheckException(dvmThreadSelf())) { ALOGW("WARNING: dvmCreateReflectMethodObject called with " "exception pending"); return NULL; } dexStringCacheInit(&mangle); /* parent should guarantee init so we don't have to check on every call */ assert(dvmIsClassInitialized(gDvm.classJavaLangReflectMethod)); methObj = dvmAllocObject(gDvm.classJavaLangReflectMethod, ALLOC_DEFAULT); if (methObj == NULL) goto bail; /* * Convert the signature string into an array of classes representing * the arguments, and a class for the return type. */ cp = dvmCopyDescriptorStringFromMethod(meth, &mangle); params = convertSignatureToClassArray(&cp, meth->clazz); if (params == NULL) goto bail; assert(*cp == ')'); cp++; returnType = convertSignaturePartToClass(&cp, meth->clazz); if (returnType == NULL) goto bail; /* * Create an array with one entry for every exception that the class * is declared to throw. */ exceptions = dvmGetMethodThrows(meth); if (dvmCheckException(dvmThreadSelf())) goto bail; /* method name */ nameObj = dvmCreateStringFromCstr(meth->name); if (nameObj == NULL) goto bail; slot = methodToSlot(meth); method_idx = dvmGetMethodIdx(meth); JValue unused; dvmCallMethod(dvmThreadSelf(), gDvm.methJavaLangReflectMethod_init, methObj, &unused, meth->clazz, params, exceptions, returnType, nameObj, slot, method_idx); if (dvmCheckException(dvmThreadSelf())) { ALOGD("Method class init threw exception"); goto bail; } result = methObj; bail: dexStringCacheRelease(&mangle); if (result == NULL) { assert(dvmCheckException(dvmThreadSelf())); } dvmReleaseTrackedAlloc((Object*) nameObj, NULL); dvmReleaseTrackedAlloc((Object*) params, NULL); dvmReleaseTrackedAlloc((Object*) exceptions, NULL); if (result == NULL) dvmReleaseTrackedAlloc(methObj, NULL); return result; }
/* * Initialize an exception with an appropriate constructor. * * "exception" is the exception object to initialize. * Either or both of "msg" and "cause" may be null. * "self" is dvmThreadSelf(), passed in so we don't have to look it up again. * * If the process of initializing the exception causes another * exception (e.g., OutOfMemoryError) to be thrown, return an error * and leave self->exception intact. */ static bool initException(Object* exception, const char* msg, Object* cause, Thread* self) { enum { kInitUnknown, kInitNoarg, kInitMsg, kInitMsgThrow, kInitThrow } initKind = kInitUnknown; Method* initMethod = NULL; ClassObject* excepClass = exception->clazz; StringObject* msgStr = NULL; bool result = false; bool needInitCause = false; assert(self != NULL); assert(self->exception == NULL); /* if we have a message, create a String */ if (msg == NULL) msgStr = NULL; else { msgStr = dvmCreateStringFromCstr(msg); if (msgStr == NULL) { LOGW("Could not allocate message string \"%s\" while " "throwing internal exception (%s)\n", msg, excepClass->descriptor); goto bail; } } if (cause != NULL) { if (!dvmInstanceof(cause->clazz, gDvm.classJavaLangThrowable)) { LOGE("Tried to init exception with cause '%s'\n", cause->clazz->descriptor); dvmAbort(); } } /* * The Throwable class has four public constructors: * (1) Throwable() * (2) Throwable(String message) * (3) Throwable(String message, Throwable cause) (added in 1.4) * (4) Throwable(Throwable cause) (added in 1.4) * * The first two are part of the original design, and most exception * classes should support them. The third prototype was used by * individual exceptions. e.g. ClassNotFoundException added it in 1.2. * The general "cause" mechanism was added in 1.4. Some classes, * such as IllegalArgumentException, initially supported the first * two, but added the second two in a later release. * * Exceptions may be picky about how their "cause" field is initialized. * If you call ClassNotFoundException(String), it may choose to * initialize its "cause" field to null. Doing so prevents future * calls to Throwable.initCause(). * * So, if "cause" is not NULL, we need to look for a constructor that * takes a throwable. If we can't find one, we fall back on calling * #1/#2 and making a separate call to initCause(). Passing a null ref * for "message" into Throwable(String, Throwable) is allowed, but we * prefer to use the Throwable-only version because it has different * behavior. * * java.lang.TypeNotPresentException is a strange case -- it has #3 but * not #2. (Some might argue that the constructor is actually not #3, * because it doesn't take the message string as an argument, but it * has the same effect and we can work with it here.) * * java.lang.AssertionError is also a strange case -- it has a * constructor that takes an Object, but not one that takes a String. * There may be other cases like this, as well, so we generally look * for an Object-taking constructor if we can't find one that takes * a String. */ if (cause == NULL) { if (msgStr == NULL) { initMethod = findExceptionInitMethod(excepClass, false, false); initKind = kInitNoarg; } else { initMethod = findExceptionInitMethod(excepClass, true, false); if (initMethod != NULL) { initKind = kInitMsg; } else { /* no #2, try #3 */ initMethod = findExceptionInitMethod(excepClass, true, true); if (initMethod != NULL) { initKind = kInitMsgThrow; } } } } else { if (msgStr == NULL) { initMethod = findExceptionInitMethod(excepClass, false, true); if (initMethod != NULL) { initKind = kInitThrow; } else { initMethod = findExceptionInitMethod(excepClass, false, false); initKind = kInitNoarg; needInitCause = true; } } else { initMethod = findExceptionInitMethod(excepClass, true, true); if (initMethod != NULL) { initKind = kInitMsgThrow; } else { initMethod = findExceptionInitMethod(excepClass, true, false); initKind = kInitMsg; needInitCause = true; } } } if (initMethod == NULL) { /* * We can't find the desired constructor. This can happen if a * subclass of java/lang/Throwable doesn't define an expected * constructor, e.g. it doesn't provide one that takes a string * when a message has been provided. */ LOGW("WARNING: exception class '%s' missing constructor " "(msg='%s' kind=%d)\n", excepClass->descriptor, msg, initKind); assert(strcmp(excepClass->descriptor, "Ljava/lang/RuntimeException;") != 0); dvmThrowChainedException("Ljava/lang/RuntimeException;", "re-throw on exception class missing constructor", NULL); goto bail; } /* * Call the constructor with the appropriate arguments. */ JValue unused; switch (initKind) { case kInitNoarg: LOGVV("+++ exc noarg (ic=%d)\n", needInitCause); dvmCallMethod(self, initMethod, exception, &unused); break; case kInitMsg: LOGVV("+++ exc msg (ic=%d)\n", needInitCause); dvmCallMethod(self, initMethod, exception, &unused, msgStr); break; case kInitThrow: LOGVV("+++ exc throw"); assert(!needInitCause); dvmCallMethod(self, initMethod, exception, &unused, cause); break; case kInitMsgThrow: LOGVV("+++ exc msg+throw"); assert(!needInitCause); dvmCallMethod(self, initMethod, exception, &unused, msgStr, cause); break; default: assert(false); goto bail; } /* * It's possible the constructor has thrown an exception. If so, we * return an error and let our caller deal with it. */ if (self->exception != NULL) { LOGW("Exception thrown (%s) while throwing internal exception (%s)\n", self->exception->clazz->descriptor, exception->clazz->descriptor); goto bail; } /* * If this exception was caused by another exception, and we weren't * able to find a cause-setting constructor, set the "cause" field * with an explicit call. */ if (needInitCause) { Method* initCause; initCause = dvmFindVirtualMethodHierByDescriptor(excepClass, "initCause", "(Ljava/lang/Throwable;)Ljava/lang/Throwable;"); if (initCause != NULL) { dvmCallMethod(self, initCause, exception, &unused, cause); if (self->exception != NULL) { /* initCause() threw an exception; return an error and * let the caller deal with it. */ LOGW("Exception thrown (%s) during initCause() " "of internal exception (%s)\n", self->exception->clazz->descriptor, exception->clazz->descriptor); goto bail; } } else { LOGW("WARNING: couldn't find initCause in '%s'\n", excepClass->descriptor); } } result = true; bail: dvmReleaseTrackedAlloc((Object*) msgStr, self); // NULL is ok return result; }
/* * Generate an array of StackTraceElement objects from the raw integer * data encoded by dvmFillInStackTrace(). * * "intVals" points to the first {method,pc} pair. * * The returned array is not added to the "local refs" list. */ ArrayObject* dvmGetStackTraceRaw(const int* intVals, int stackDepth) { ArrayObject* steArray = NULL; int i; /* init this if we haven't yet */ if (!dvmIsClassInitialized(gDvm.classJavaLangStackTraceElement)) dvmInitClass(gDvm.classJavaLangStackTraceElement); /* allocate a StackTraceElement array */ steArray = dvmAllocArray(gDvm.classJavaLangStackTraceElementArray, stackDepth, kObjectArrayRefWidth, ALLOC_DEFAULT); if (steArray == NULL) goto bail; /* * Allocate and initialize a StackTraceElement for each stack frame. * We use the standard constructor to configure the object. */ for (i = 0; i < stackDepth; i++) { Object* ste; Method* meth; StringObject* className; StringObject* methodName; StringObject* fileName; int lineNumber, pc; const char* sourceFile; char* dotName; ste = dvmAllocObject(gDvm.classJavaLangStackTraceElement,ALLOC_DEFAULT); if (ste == NULL) goto bail; meth = (Method*) *intVals++; pc = *intVals++; if (pc == -1) // broken top frame? lineNumber = 0; else lineNumber = dvmLineNumFromPC(meth, pc); dotName = dvmDescriptorToDot(meth->clazz->descriptor); className = dvmCreateStringFromCstr(dotName); free(dotName); methodName = dvmCreateStringFromCstr(meth->name); sourceFile = dvmGetMethodSourceFile(meth); if (sourceFile != NULL) fileName = dvmCreateStringFromCstr(sourceFile); else fileName = NULL; /* * Invoke: * public StackTraceElement(String declaringClass, String methodName, * String fileName, int lineNumber) * (where lineNumber==-2 means "native") */ JValue unused; dvmCallMethod(dvmThreadSelf(), gDvm.methJavaLangStackTraceElement_init, ste, &unused, className, methodName, fileName, lineNumber); dvmReleaseTrackedAlloc(ste, NULL); dvmReleaseTrackedAlloc((Object*) className, NULL); dvmReleaseTrackedAlloc((Object*) methodName, NULL); dvmReleaseTrackedAlloc((Object*) fileName, NULL); if (dvmCheckException(dvmThreadSelf())) goto bail; dvmSetObjectArrayElement(steArray, i, ste); } bail: dvmReleaseTrackedAlloc((Object*) steArray, NULL); return steArray; }