在上一篇我们学习了如何在AS中创建Xposed模块,本篇来分析下官方教程中redClock的实现原理。本系列文章基于version-51

public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
if(!lpparam.packageName.equals("com.android.systemui")) return;
XposedBridge.log("we are in systemui !"); findAndHookMethod("com.android.systemui.statusbar.policy.Clock", lpparam.classLoader, "updateClock", new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
super.beforeHookedMethod(param);
} @Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
TextView tv = (TextView) param.thisObject;
String text = tv.getText().toString();
tv.setText(text + ")");
tv.setTextColor(Color.RED);
}
});
}

上面的代码可以将原先在状态栏的时钟文本颜色变成红色,且在后面加")"。看下图:

主要的实现代码在findAndHookMethod函数中,查看函数定义:

findAndHookMethod:

——>findMethodExact(clazz,methodName,paramterClasses);

——>XposedBridge.hookMethod(method,callback);

先看findMethodExact,

代码很简单就是要得到methodName在android中对应的函数对象,根据findAndHookMethod的参数得到字符串sb(格式参考注释行),用sb在methodCache这个hashMap查找有没有对应的method;若没有则根据methodName和parameterTypes利用getDeclaredMethod得到对应的method。

public static Method findMethodExact(Class<?> clazz, String methodName, Class... parameterTypes) {
StringBuilder sb = new StringBuilder(clazz.getName());
sb.append('#');
sb.append(methodName);
sb.append(getParametersString(parameterTypes));
sb.append("#exact");
// sb = com.android.systemui.statusbar.policy.Clock#updateClock(参数1,参数2)#exact String fullMethodName = sb.toString();
Method e;
//methodCache键值对存放fullMethodName,method对象
if(methodCache.containsKey(fullMethodName)) {
e = (Method)methodCache.get(fullMethodName);
if(e == null) {
throw new NoSuchMethodError(fullMethodName);
} else {
return e;
}
} else {
try {
e = clazz.getDeclaredMethod(methodName, parameterTypes);
e.setAccessible(true);
methodCache.put(fullMethodName, e);
return e;
} catch (NoSuchMethodException var6) {
methodCache.put(fullMethodName, (Object)null);
throw new NoSuchMethodError(fullMethodName);
}
}
}

  看来重点在XposedBridge.hookMethod:

public static Unhook hookMethod(Member hookMethod, XC_MethodHook callback) {
if(!(hookMethod instanceof Method) && !(hookMethod instanceof Constructor)) {
throw new IllegalArgumentException("Only methods and constructors can be hooked: " + hookMethod.toString());
} else if(hookMethod.getDeclaringClass().isInterface()) {
throw new IllegalArgumentException("Cannot hook interfaces: " + hookMethod.toString());
} else if(Modifier.isAbstract(hookMethod.getModifiers())) {
throw new IllegalArgumentException("Cannot hook abstract methods: " + hookMethod.toString());
} else {
//上面代码分步检查hookMethod的类型
//else中的代码得到hookMethod对应的键值对
boolean newMethod = false;
Map declaringClass = sHookedMethodCallbacks;
XposedBridge.CopyOnWriteSortedSet callbacks;
synchronized(sHookedMethodCallbacks) {
callbacks = (XposedBridge.CopyOnWriteSortedSet)sHookedMethodCallbacks.get(hookMethod);
if(callbacks == null) {
callbacks = new XposedBridge.CopyOnWriteSortedSet();
sHookedMethodCallbacks.put(hookMethod, callbacks);
newMethod = true;
}
} callbacks.add(callback);
//替换hookMehod的callbacks为callback,其实callback是存放的是hookMethod所有的callback,看定义:</span></span>
final XposedBridge.CopyOnWriteSortedSet<XC_MethodHook> callbacks;
//以上代码就是为hookMethod建立对应的callback list
//sHookedMethodCallbacks存放hookMethod和callback if(newMethod) {
Class declaringClass1 = hookMethod.getDeclaringClass();
int slot = XposedHelpers.getIntField(hookMethod, "slot");
Class[] parameterTypes;
Class returnType;
if(hookMethod instanceof Method) {
parameterTypes = ((Method)hookMethod).getParameterTypes();
returnType = ((Method)hookMethod).getReturnType();
} else {
parameterTypes = ((Constructor)hookMethod).getParameterTypes();
returnType = null;
}
//以上代码得到method的参数和返回值,在AdditionalHookInfo下使用
//把callback、method参数、method返回值汇总在AdditionalHookInfo类下
XposedBridge.AdditionalHookInfo additionalInfo = new XposedBridge.AdditionalHookInfo(callbacks, parameterTypes, returnType, (XposedBridge.AdditionalHookInfo)null);
//本地函数在libxposed_dalvik.cpp
       hookMethodNative(hookMethod, declaringClass1, slot, additionalInfo);
} callback.getClass();
return new Unhook(callback, hookMethod);
//为callback绑定hookMthod
}
}

上面乱七八糟的走了这么多,登记了2个hashmap{(fullMethodName,method对象),(hookmethod,callback)}。看来java层只是管理这些结构并没有实质性的操作,进入native代码----Xposed.cpp:

//参数:reflectedMethodIndirect==>hookmethod,declaredClassIndirect==>hookmethod所在的类
// slot==>slot,additionalInfoIndirect==>结构体包含callback、parameterTypes、returnType
void XposedBridge_hookMethodNative(JNIEnv* env, jclass clazz, jobject reflectedMethodIndirect,
jobject declaredClassIndirect, jint slot, jobject additionalInfoIndirect) {
// Usage errors?
if (declaredClassIndirect == NULL || reflectedMethodIndirect == NULL) {
dvmThrowIllegalArgumentException("method and declaredClass must not be null");
return;
} // Find the internal representation of the method
//获得dalvik中的classObject对象
ClassObject* declaredClass = (ClassObject*) dvmDecodeIndirectRef(dvmThreadSelf(), declaredClassIndirect);
//获得dalvik中的Method,被dalvik执行的函数体不同于java层的Method,位于Object.h
Method* method = dvmSlotToMethod(declaredClass, slot);
if (method == NULL) {
dvmThrowNoSuchMethodError("Could not get internal representation for method");
return;
}
//若method已被hook则直接返回
if (isMethodHooked(method)) {
// already hooked
return;
} // Save a copy of the original method and other hook info
XposedHookInfo* hookInfo = (XposedHookInfo*) calloc(1, sizeof(XposedHookInfo));
memcpy(hookInfo, method, sizeof(hookInfo->originalMethodStruct));
hookInfo->reflectedMethod = dvmDecodeIndirectRef(dvmThreadSelf(), env->NewGlobalRef(reflectedMethodIndirect));
hookInfo->additionalInfo = dvmDecodeIndirectRef(dvmThreadSelf(), env->NewGlobalRef(additionalInfoIndirect)); // Replace method with our own code 将method替换成我们自己的代码
//设置method->accessFlags = ACC_NATIVE;表示method为native代码
//下面几行代码都是为这行代码作补充
//For a native method, we compute the size of the argument list,
//and set "insSize" and "registerSize" equal to it.
SET_METHOD_FLAG(method, ACC_NATIVE);
//给method添加callback函数表示已被hooked
method->nativeFunc = &hookedMethodCallback;
//原本的insns中存放的是dex指令,现变为hookinfo为hookedMethodCallback准备
method->insns = (const u2*) hookInfo;
method->registersSize = method->insSize;
method->outsSize = 0; if (PTR_gDvmJit != NULL) {
// reset JIT cache
char currentValue = *((char*)PTR_gDvmJit + MEMBER_OFFSET_VAR(DvmJitGlobals,codeCacheFull));
if (currentValue == 0 || currentValue == 1) {
MEMBER_VAL(PTR_gDvmJit, DvmJitGlobals, codeCacheFull) = true;
} else {
ALOGE("Unexpected current value for codeCacheFull: %d", currentValue);

代码主要的就是红色标注的,它将Method标为native code。dalvik虚拟机在执行Method时,则会直接调用其成员变量hookedMethodCallback执行。注意,这个时候已经改变了原本的Method的执行步骤了(Xposed在此刻觉醒啦啦啦)。看下面dalvik代码,/dalvik/vm/interp/Stack.c

void dvmCallMethodV(Thread* self, const Method* method, Object* obj,
bool fromJni, JValue* pResult, va_list args)
{
...... if (dvmIsNativeMethod(method)) {
TRACE_METHOD_ENTER(self, method);
/*
* Because we leave no space for local variables, "curFrame" points
* directly at the method arguments.
*/
(*method->nativeFunc)(self->curFrame, pResult, method, self);
TRACE_METHOD_EXIT(self, method);
} else {</span>
//这里是在Inter.cpp中直接解析method
dvmInterpret(self, method, pResult);
} ......
}

   这一路走来感觉有点偏离主线类,回看下主题,在执行完 findAndHookMethod("com.android.systemui.statusbar.policy.Clock", lpparam.classLoader,"updateClock", new XC_MethodHook() );后。updateClock(hookmethod)的accessflag = ACC_NATIVE,dalvik在执行updateClock方法时发现其为native code,则执行nativeFunc 函数体即hookedMethodCallback。ok,找到"元凶"了,继续看代码:

/* This is called when a hooked method is executed. */
void hookedMethodCallback(const u4* args, JValue* pResult, const Method* method, ::Thread* self) {
......
//call the Java handler function</span>
JValue result; dvmCallMethod(self, xposedHandleHookedMethod, NULL, &result,
originalReflected, (int) original, additionalInfo, thisObject, argsArray);
......
}
  hookedMethodCallback函数中主要是调用dvmCallMethod去执行xposedHandleHookedMethod,而xposedHandleHookedMethod是classXposedBridge里的handleHookedMethod方法。ok,重头戏来了
private static Object handleHookedMethod(Member method, int originalMethodId, Object additionalInfoObj, Object thisObject, Object[] args) throws Throwable {
XposedBridge.AdditionalHookInfo additionalInfo = (XposedBridge.AdditionalHookInfo)additionalInfoObj;
if(disableHooks) {
try {
//hook不使能,执行原method
return invokeOriginalMethodNative(method, originalMethodId, additionalInfo.parameterTypes, additionalInfo.returnType, thisObject, args);
......
}
} else {
/得到hookmethod的callback,在之前的XposedBridge.hookMethod中为callbacks添加了callback
Object[] callbacksSnapshot = additionalInfo.callbacks.getSnapshot();
int callbacksLength = callbacksSnapshot.length;
if(callbacksLength == 0) {
//hookmethod的callback为空,hooked无意义,执行原method
try {
return invokeOriginalMethodNative(method, originalMethodId, additionalInfo.parameterTypes, additionalInfo.returnType, thisObject, args);
......
}
} else {
......
do {
label65: {
try {
((XC_MethodHook)callbacksSnapshot[beforeIdx]).beforeHookedMethod(param);
} catch (Throwable var18) {
log(var18);
param.setResult((Object)null);
param.returnEarly = false;
break label65;
} if(param.returnEarly) {
++beforeIdx;
break;
}
} ++beforeIdx;
//hookmethod有几个callback就循环几次
} while(beforeIdx < callbacksLength); if(!param.returnEarly) {
try {
//在beforeHookedMethod后执行原method
param.setResult(invokeOriginalMethodNative(method, originalMethodId, additionalInfo.parameterTypes, additionalInfo.returnType, param.thisObject, param.args));
} catch (InvocationTargetException var16) {
param.setThrowable(var16.getCause());
}
} int afterIdx = beforeIdx - 1; do {
Object lastResult = param.getResult();
Throwable lastThrowable = param.getThrowable(); try {
((XC_MethodHook)callbacksSnapshot[afterIdx]).afterHookedMethod(param);
} catch (Throwable var17) {
log(var17);
if(lastThrowable == null) {
param.setResult(lastResult);
} else {
param.setThrowable(lastThrowable);
}
} --afterIdx;
} while(afterIdx >= 0);
......
}
}
}

别看handleHookedMethod代码老长了,其实它很单纯。

第一步:是否需要执行callback,否则直接执行原method,gameover;

第二步:执行callbacks里的beforeHookedMethod方法,有几个callback执行几次beforeHookedMethod;

第三步:执行原method;

第四步:执行callbacks里的afterHookedMethod方法,类同beforeHookedMethod。

需要注意的是如果method有多个callback,其beforeHookedMethod和afterHookedMethod执行顺序:

A1.before->A2.before->原method->A2.after->A1.after,也是蛮符合客观规律的嘛。

好,关于findAndHookMethod()函数也算是从上倒下看了个遍,但你上面添这么多代码是算怎么回事呢?下面就简单总结下罗:

好了,本篇就先这样吧,太长了也不好看。下篇再分析下其余枝节:

1 handleLoadPackage 怎么生效

2 dalvik层如何回到java层(数据类型如何回传)

3 XC_MethodHook中会执行原来的java函数体,如何执行;

其他想到再分析啰,大家也可以对上述执行流程提问,我们一起探讨。

参考资料:

1 Xposed框架Java部分

2 Dalvik虚拟机的运行过程分析

版权声明:本文为博主原创文章,未经博主允许不得转载。

Xposed学习二:实现机制的更多相关文章

  1. ReactJS入门学习二

    ReactJS入门学习二 阅读目录 React的背景和基本原理 理解React.render() 什么是JSX? 为什么要使用JSX? JSX的语法 如何在JSX中如何使用事件 如何在JSX中如何使用 ...

  2. Struts2框架学习(二) Action

    Struts2框架学习(二) Action Struts2框架中的Action类是一个单独的javabean对象.不像Struts1中还要去继承HttpServlet,耦合度减小了. 1,流程 拦截器 ...

  3. Java开发学习(二十八)----拦截器(Interceptor)详细解析

    一.拦截器概念 讲解拦截器的概念之前,我们先看一张图: (1)浏览器发送一个请求会先到Tomcat的web服务器 (2)Tomcat服务器接收到请求以后,会去判断请求的是静态资源还是动态资源 (3)如 ...

  4. emberjs学习二(ember-data和localstorage_adapter)

    emberjs学习二(ember-data和localstorage_adapter) 准备工作 首先我们加入ember-data和ember-localstorage-adapter两个依赖项,使用 ...

  5. TweenMax动画库学习(二)

    目录            TweenMax动画库学习(一)            TweenMax动画库学习(二)            TweenMax动画库学习(三)            Tw ...

  6. java学习之反射机制

    java语言区别于C,C++等准静态语言的最大特点就是java的反射机制.静态语言的最直接定义就是不能在运行时改变程序结构或变量的类型.按照这样的定义,python,ruby是动态语言,C,C++,J ...

  7. Hbase深入学习(二) 安装hbase

    Hbase深入学习(二) 安装hbase This guidedescribes setup of a standalone hbase instance that uses the local fi ...

  8. Python学习二:词典基础详解

    作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/7862377.html 邮箱:moyi@moyib ...

  9. Quartz学习--二 Hello Quartz! 和源码分析

    Quartz学习--二  Hello Quartz! 和源码分析 三.  Hello Quartz! 我会跟着 第一章 6.2 的图来 进行同步代码编写 简单入门示例: 创建一个新的java普通工程 ...

随机推荐

  1. MYSQL的replace into

    replace into t(id, update_time) values(1, now()); 或 replace into t(id, update_time) select 1, now(); ...

  2. JPress企业站主题-zbout

    JPress企业站主题-zbout 经典的黑白灰颜色搭配风格,首页配置有轮播图.案例展示.公司简介.新闻中心.联系方式以及合作伙伴模块,全站使用了响应式结构,可以自适应电脑端和手机端浏览器访问.主题整 ...

  3. 【译】Visual Studio 的 Razor 编辑器的改进

    自从我们在一个通用的 Razor 语言服务器上发布了 Visual Studio 的一个新的实验性 Razor 编辑器的第一个预览版以来,已经过去了6个月,现在是时候更新一下我们的进展了.团队一直在努 ...

  4. 一起来学习LiteOS中断模块的源代码

    摘要:本文带领大家一起剖析了LiteOS中断模块的源代码. 本文我们来一起学习下LiteOS中断模块的源代码,文中所涉及的源代码,均可以在LiteOS开源站点https://gitee.com/Lit ...

  5. P3796 【模板】AC自动机(加强版) 题解(Aho-Corasick Automation)

    题目链接 AC自动机 解题思路 AC自动机模板题. 刚学AC自动机,写一篇博客增强理解. AC自动机最关键的一点在于,\(fail\)失配指针的构造. \(fail\)指针指向的地方,是匹配出现错误后 ...

  6. Spring Boot 自动配置 源码分析

    Spring Boot 最大的特点(亮点)就是自动配置 AutoConfiguration 下面,先说一下 @EnableAutoConfiguration ,然后再看源代码,到底自动配置是怎么配置的 ...

  7. 07-Spring ConfigurationClassPostProcessor

    ConfigurationClassPostProcessor 功能 此类是一个后置处理器类,主要功能是参与 BeanFactory 中 BeanDefinition 的操作和 BeanDefinit ...

  8. 攻防世界 reverse 进阶 APK-逆向2

    APK-逆向2 Hack-you-2014 (看名以为是安卓逆向呢0.0,搞错了吧) 程序是.net写的,直接祭出神器dnSpy 1 using System; 2 using System.Diag ...

  9. 「免费开源」基于Vue和Quasar的前端SPA项目crudapi后台管理系统实战之自定义组件(四)

    基于Vue和Quasar的前端SPA项目实战之序列号(四) 回顾 通过上一篇文章 基于Vue和Quasar的前端SPA项目实战之布局菜单(三)的介绍,我们已经完成了布局菜单,本文主要介绍序列号功能的实 ...

  10. vue全局错误捕获

    1.errorHandler Vue全局配置 errorHandler可以进行全局错误收集,捕获全局错误抛出,避免前端页面挂掉   export default function errorHandl ...