Skip to content

Commit

Permalink
Change : getShorty from java, for #40 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
baiyi.hwj committed Oct 21, 2015
1 parent dd0f151 commit 1f24a72
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
Expand Up @@ -81,7 +81,7 @@ private static int getRuntime() {
* @param text log message
*/
public synchronized static void log(String text) {
log(text);
Log.d("Dexposed", text);
}

/**
Expand Down Expand Up @@ -150,7 +150,7 @@ public static void unhookMethod(Member hookMethod, XC_MethodHook callback) {
}
callbacks.remove(callback);
}

public static Set<XC_MethodHook.Unhook> hookAllMethods(Class<?> hookClass, String methodName, XC_MethodHook callback) {
Set<XC_MethodHook.Unhook> unhooks = new HashSet<XC_MethodHook.Unhook>();
for (Member method : hookClass.getDeclaredMethods())
Expand Down Expand Up @@ -370,6 +370,7 @@ public static Object invokeOriginalMethod(Member method, Object thisObject, Obje
if (method instanceof Method) {
parameterTypes = ((Method) method).getParameterTypes();
returnType = ((Method) method).getReturnType();

} else if (method instanceof Constructor) {
parameterTypes = ((Constructor<?>) method).getParameterTypes();
returnType = null;
Expand Down Expand Up @@ -429,11 +430,41 @@ private static class AdditionalHookInfo {
final CopyOnWriteSortedSet<XC_MethodHook> callbacks;
final Class<?>[] parameterTypes;
final Class<?> returnType;
String shorty;

private AdditionalHookInfo(CopyOnWriteSortedSet<XC_MethodHook> callbacks, Class<?>[] parameterTypes, Class<?> returnType) {
this.callbacks = callbacks;
this.parameterTypes = parameterTypes;
this.returnType = returnType;

StringBuilder sb = new StringBuilder(64);
sb.append(Class2Shorty(returnType));
for(Class<?> c : parameterTypes){
sb.append(Class2Shorty(c));
}

shorty = sb.toString();
}

String Class2Shorty(Class<?> cls) {
if(cls.isPrimitive()){
return builtInMap.get(cls);
} else
return "L";
}
}

private static Map<Class, String> builtInMap = new HashMap<Class, String>(){
{
put(Integer.TYPE, "I");
put(Long.TYPE, "J");
put(Double.TYPE, "D" );
put(Float.TYPE, "F" );
put(Boolean.TYPE, "Z" );
put(Character.TYPE, "C" );
put(Byte.TYPE, "B" );
put(Void.TYPE, "V" );
put(Short.TYPE, "S" );
}
};
}
40 changes: 38 additions & 2 deletions dexposed_so/dexposed_art/dexposed.cpp
Expand Up @@ -36,6 +36,8 @@ namespace art {

jclass dexposed_class = NULL;
jmethodID dexposed_handle_hooked_method = NULL;
jclass additionalhookinfo_class = NULL;
jfieldID additionalhookinfo_shorty_field = NULL;

void logMethod(const char* tag, ArtMethod* method) {
LOG(INFO) << "dexposed:" << tag << " " << method << " " << PrettyMethod(method);
Expand All @@ -52,6 +54,14 @@ namespace art {
return false;
}

additionalhookinfo_class = env->FindClass(DEXPOSED_ADDITIONAL_CLASS);
additionalhookinfo_class = reinterpret_cast<jclass>(env->NewGlobalRef(additionalhookinfo_class));
if (additionalhookinfo_class == NULL) {
LOG(ERROR) << "dexposed: Error while loading Dexposed class " << DEXPOSED_ADDITIONAL_CLASS;
env->ExceptionClear();
return false;
}

LOG(INFO) << "dexposed: now initializing, Found Dexposed class " << DEXPOSED_CLASS;
if (register_com_taobao_android_dexposed_DexposedBridge(env) != JNI_OK) {
LOG(ERROR) << "dexposed: Could not register natives for " << DEXPOSED_CLASS;
Expand All @@ -74,6 +84,16 @@ namespace art {
env->ExceptionClear();
return false;
}


additionalhookinfo_shorty_field =
env->GetFieldID(additionalhookinfo_class, "shorty", "Ljava/lang/String;");
if (additionalhookinfo_shorty_field == NULL) {
LOG(ERROR) << "dexposed: Could not find field " << DEXPOSED_ADDITIONAL_CLASS << ".shorty";
env->ExceptionClear();
return false;
}

return true;
}

Expand Down Expand Up @@ -187,7 +207,7 @@ namespace art {

const bool is_static = proxy_method->IsStatic();

LOG(INFO) << "dexposed: artQuickDexposedInvokeHandler " << is_static;
LOG(INFO) << "dexposed: artQuickDexposedInvokeHandler isStatic:" << is_static;

// Ensure we don't get thread suspension until the object arguments are safely in jobjects.
const char* old_cause = self->StartAssertNoThreadSuspension(
Expand All @@ -211,8 +231,20 @@ namespace art {
ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy();

std::vector < jvalue > args;

#if PLATFORM_SDK_VERSION < 22
const DexposedHookInfo *hookInfo =
(DexposedHookInfo *) (proxy_method->GetNativeMethod());
#else
const DexposedHookInfo *hookInfo =
(DexposedHookInfo *) (proxy_method->GetEntryPointFromJni());
#endif


uint32_t shorty_len = 0;
const char* shorty = proxy_method->GetShorty(&shorty_len);
// const char* shorty = proxy_method->GetShorty(&shorty_len);
const char* shorty = hookInfo->shorty;
shorty_len = strlen(hookInfo->shorty);

for(int i=0; i<shorty_len; ++i){
LOG(INFO) << "dexposed: artQuickDexposedInvokeHandler " << "shorty[" << i << "]:" << shorty[i];
Expand Down Expand Up @@ -267,6 +299,10 @@ namespace art {
hookInfo->additionalInfo = env->NewGlobalRef(additional_info);
hookInfo->originalMethod = backup_method;

jstring shorty = (jstring)env->GetObjectField(additional_info,additionalhookinfo_shorty_field);
hookInfo->shorty = env->GetStringUTFChars(shorty, 0);
LOG(INFO) << "dexposed: >>> EnableXposedHook shorty:" << hookInfo->shorty;

#if PLATFORM_SDK_VERSION < 22
art_method->SetNativeMethod(reinterpret_cast<uint8_t *>(hookInfo));
#else
Expand Down
3 changes: 3 additions & 0 deletions dexposed_so/dexposed_art/dexposed.h
Expand Up @@ -37,6 +37,7 @@
#include <throw_location.h>
#include <stack.h>
#include <jni_internal.h>
#include <dex_file.h>

using art::mirror::ArtMethod;
using art::mirror::Array;
Expand Down Expand Up @@ -64,6 +65,7 @@ using art::ScopedJniEnvLocalRefState;
using art::ThrowLocation;

#define DEXPOSED_CLASS "com/taobao/android/dexposed/DexposedBridge"
#define DEXPOSED_ADDITIONAL_CLASS "com/taobao/android/dexposed/DexposedBridge$AdditionalHookInfo"
#define DEXPOSED_CLASS_DOTS "com.taobao.android.dexposed.DexposedBridge"

//#define PLATFORM_SDK_VERSION 21
Expand All @@ -75,6 +77,7 @@ namespace art {
jobject reflectedMethod;
jobject additionalInfo;
mirror::ArtMethod* originalMethod;
const char *shorty;
};

static bool dexposedIsHooked(ArtMethod* method);
Expand Down

0 comments on commit 1f24a72

Please sign in to comment.