朴英敏,小米MIUI部门。从事嵌入式开发和调试工作8年多,擅长逆向分析方法,主要负责解决安卓系统稳定性问题。
10余年的翼城网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整翼城建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“翼城网站设计”,“翼城网站推广”以来,每个客户项目都认真落实执行。
实施hook:
我们有了hook,但目前还不知道是哪个so中释放了functor。
如果无法确定是哪个so,可以多hook几个so就行了。
当然对于特定的例子,也有技巧来确定so,比如我们这个例子:
被析构的对象是Functor类的对象,由于它的vtbl地址我们能够从log中获取到,
而vtbl一般指向定义了该类的so中,所以用vtbl值(0×73648de0)去map表中找,就能确定是哪个so了。
- ...
- 73635000-73646000 rw-p 00000000 00:00 0
- 73646000-73648000 r-xp 00000000 b3:18 1287 /system/lib/libwebviewchromium_plat_support.so
- =>73648000-73649000 r--p 00001000 b3:18 1287 /system/lib/libwebviewchromium_plat_support.so
- 73649000-7364a000 rw-p 00002000 b3:18 1287 /system/lib/libwebviewchromium_plat_support.so
- 7364a000-73684000 rw-p 00000000 00:00 0
- 73684000-73696000 r-xp 00000000 b3:18 1034 /system/lib/libjavacrypto.so
- 73696000-73697000 r--p 00011000 b3:18 1034 /system/lib/libjavacrypto.so
- 73697000-73698000 rw-p 00012000 b3:18 1034 /system/lib/libjavacrypto.so
- ...
而需要注意的是,C++对象的释放是delete函数,
libwebviewchromium_plat_support.so不会直接调用libc的free函数,而是调用libc++.so中的delete函数,再由delete函数调用free函数,
所以我们得hook libc++.so的free函数,但打印调用栈的模块也依赖libc++.so,所以如果在hook函数中打印调用栈,也会遇到死循环问题。
所以我们得hook libwebviewchromium_plat_support.so中的delete函数,这样既减少log量,也能避免死循环。
先确认libwebviewchromium_plat_support.so是否依赖了delete函数:
- $ readelf -s libwebviewchromium_plat_support.so |grep UND
- 0: 00000000 0 NOTYPE LOCAL DEFAULT UND
- 1: 00000000 0 FUNC GLOBAL DEFAULT UND __cxa_finalize
- 2: 00000000 0 FUNC GLOBAL DEFAULT UND __cxa_atexit
- 4: 00000000 0 FUNC GLOBAL DEFAULT UND __aeabi_unwind_cpp_pr0
- 5: 00000000 0 FUNC GLOBAL DEFAULT UND __aeabi_unwind_cpp_pr1
- 6: 00000000 0 FUNC GLOBAL DEFAULT UND getrlimit
- 7: 00000000 0 FUNC GLOBAL DEFAULT UND setrlimit
- 8: 00000000 0 FUNC GLOBAL DEFAULT UND __errno
- 9: 00000000 0 FUNC GLOBAL DEFAULT UND strerror
- 10: 00000000 0 FUNC GLOBAL DEFAULT UND __android_log_print
- => 11: 00000000 0 FUNC GLOBAL DEFAULT UND _Znwj
- => 12: 00000000 0 FUNC GLOBAL DEFAULT UND _ZdlPv
- 14: 00000000 0 FUNC GLOBAL DEFAULT UND __android_log_assert
- ...
- 51: 00000000 0 FUNC GLOBAL DEFAULT UND __aeabi_llsr
- 52: 00000000 0 OBJECT GLOBAL DEFAULT UND __popcount_tab
其中11项_Znwj是new的符号,_ZdlPv是delete的符号。
接下来就用工具hook libwebviewchromium_plat_support.so的delete函数:
- extern void _ZdlPv(void *);
- void inject__ZdlPv(void* ptr) {
- LOGD("delete %p",ptr);
- dumpNativeStack();
- dumpJavaStack();
- _ZdlPv(ptr);
- }
hook后复现问题,抓到的log如下:
- 10-27 21:19:52.961 8027 8027 D ObserverLayout: onStop: clz=com.miui.player.display.view.DisplayFragmentLayout{45665838 V.E..... ........ 0,0-1080,1920 #7f080039 app:id/content}
- 10-27 21:19:52.965 8027 8027 I MusicBaseFragment: onDestroyView the view is still attached, delay destroy
- 10-27 21:19:52.966 8027 8027 D INJECT : delete 0x7a7b8530
- 10-27 21:19:52.986 8027 8027 D INJECT : #00 pc 000015f6 /system/lib/libinject.so (inject__ZdlPv+21)
- 10-27 21:19:52.986 8027 8027 D INJECT : #01 pc 00001134 /system/lib/libwebviewchromium_plat_supp
- 10-27 21:19:52.986 8027 8027 D INJECT : #02 pc 00001088 /system/lib/libwebviewchromium_plat_supp
- 10-27 21:19:52.987 8027 8027 D INJECT : #03 pc 0001d30c /system/lib/libdvm.so (dvmPlatformInvoke+112)
- 10-27 21:19:52.987 8027 8027 D INJECT : #04 pc 0004d8da /system/lib/libdvm.so (dvmCallJNIMethod(unsigned int const*, JV+397)
- 10-27 21:19:52.987 8027 8027 D INJECT : #05 pc 00026720 /system/lib/libdvm.so
- 10-27 21:19:52.987 8027 8027 D INJECT : #06 pc 0002d790 /system/lib/libdvm.so (dvmMterpStd(Thread*)+76)
- 10-27 21:19:52.987 8027 8027 D INJECT : #07 pc 0002adf4 /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JVa+184)
- 10-27 21:19:52.988 8027 8027 D INJECT : #08 pc 00060058 /system/lib/libdvm.so (dvmInvokeMethod(Object*, Method const*, +391)
- 10-27 21:19:52.988 8027 8027 D INJECT : #09 pc 00067ff6 /system/lib/libdvm.so
- 10-27 21:19:52.988 8027 8027 D INJECT : #10 pc 00026720 /system/lib/libdvm.so
- 10-27 21:19:52.988 8027 8027 D INJECT : #11 pc 0002d790 /system/lib/libdvm.so (dvmMterpStd(Thread*)+76)
- 10-27 21:19:52.988 8027 8027 D INJECT : #12 pc 0002adf4 /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JVa+184)
- 10-27 21:19:52.988 8027 8027 D INJECT : #13 pc 0005fd74 /system/lib/libdvm.so (dvmCallMethodV(Thread*, Method const*, O+335)
- 10-27 21:19:52.988 8027 8027 D INJECT : #14 pc 000494c2 /system/lib/libdvm.so
- 10-27 21:19:52.989 8027 8027 D INJECT : at com.android.webview.chromium.DrawGLFunctor.nativeDestroyGLFunctor(Native Method)
- 10-27 21:19:52.989 8027 8027 D INJECT : at com.android.webview.chromium.DrawGLFunctor.access$000(DrawGLFunctor.java:31)
- 10-27 21:19:52.989 8027 8027 D INJECT : at com.android.webview.chromium.DrawGLFunctor$DestroyRunnable.run(DrawGLFunctor.java:91)
- 10-27 21:19:52.989 8027 8027 D INJECT : at com.android.org.chromium.content.common.CleanupReference.runCleanupTaskInternal(CleanupReference.java:159)
- 10-27 21:19:52.989 8027 8027 D INJECT : at com.android.org.chromium.content.common.CleanupReference.access$300(CleanupReference.java:32)
- 10-27 21:19:52.989 8027 8027 D INJECT : at com.android.org.chromium.content.common.CleanupReference$LazyHolder$1.handleMessage(CleanupReference.java:93)
- 10-27 21:19:52.990 8027 8027 D INJECT : at com.android.org.chromium.content.common.CleanupReference.handleOnUiThread(CleanupReference.java:147)
- 10-27 21:19:52.990 8027 8027 D INJECT : at com.android.org.chromium.content.common.CleanupReference.cleanupNow(CleanupReference.java:141)
- 10-27 21:19:52.990 8027 8027 D INJECT : at com.android.webview.chromium.DrawGLFunctor.destroy(DrawGLFunctor.java:46)
- 10-27 21:19:52.990 8027 8027 D INJECT : at com.android.webview.chromium.WebViewChromium.destroy(WebViewChromium.java:430)
- 10-27 21:19:52.990 8027 8027 D INJECT : at android.webkit.WebView.destroy(WebView.java:667)
- 10-27 21:19:52.990 8027 8027 D INJECT : at com.xiaomi.music.hybrid.HybridFragment.destroyHybridView(HybridFragment.java:64)
- 10-27 21:19:52.990 8027 8027 D INJECT : at com.xiaomi.music.hybrid.HybridFragment.onDestroyView(HybridFragment.java:115)
- 10-27 21:19:52.990 8027 8027 D INJECT : at com.miui.player.component.MusicBaseFragment.onDestroyView(MusicBaseFragment.java:216)
- 10-27 21:19:52.991 8027 8027 D INJECT : at android.app.Fragment.performDestroyView(Fragment.java:1898)
- 10-27 21:19:52.991 8027 8027 D INJECT : at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:954)
- 10-27 21:19:52.991 8027 8027 D INJECT : at android.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1167)
- 10-27 21:19:52.991 8027 8027 D INJECT : at android.app.BackStackRecord.popFromBackStack(BackStackRecord.java:715)
- 10-27 21:19:52.991 8027 8027 D INJECT : at android.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1544)
- 10-27 21:19:52.992 8027 8027 D INJECT : at android.app.FragmentManagerImpl$3.run(FragmentManager.java:502)
- 10-27 21:19:52.992 8027 8027 D INJECT : at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1449)
- 10-27 21:19:52.992 8027 8027 D INJECT : at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
- 10-27 21:19:52.992 8027 8027 D INJECT : at android.os.Handler.handleCallback(Handler.java:733)
- 10-27 21:19:52.992 8027 8027 D INJECT : at android.os.Handler.dispatchMessage(Handler.java:95)
- 10-27 21:19:52.992 8027 8027 D INJECT : at android.os.Looper.loop(Looper.java:136)
- 10-27 21:19:52.993 8027 8027 D INJECT : at android.app.ActivityThread.main(ActivityThread.java:5016)
- 10-27 21:19:52.993 8027 8027 D INJECT : at java.lang.reflect.Method.invokeNative(Native Method)
- 10-27 21:19:52.993 8027 8027 D INJECT : at java.lang.reflect.Method.invoke(Method.java:515)
- 10-27 21:19:52.993 8027 8027 D INJECT : at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
- 10-27 21:19:52.993 8027 8027 D INJECT : at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
- 10-27 21:19:52.993 8027 8027 D INJECT : at dalvik.system.NativeStart.main(Native Method)
- 10-27 21:19:53.020 8027 8027 I OpenGLRenderer: functor=0x7a7b8530,vtbl=0x400fc1b8
从log中可以看到,确实是在distroy view的时候释放了Functor,而随后再Renderer中又使用了这个Functor。
打印崩溃时的java调用栈如下:
- 10-27 21:19:53.274 8027 8027 I dalvikvm: "main" prio=5 tid=1 TIMED_WAIT10-27 21:19:53.279 8027 8027 I dalvikvm: | group="main" sCount=0 dsCount=0 obj=0x41716ca8 self=0x415344f8
- 10-27 21:19:53.279 8027 8027 I dalvikvm: | sysTid=6895 nice=-6 sched=0/0 cgrp=apps handle=1074409812
- 10-27 21:19:53.280 8027 8027 I dalvikvm: | state=R schedstat=( 0 0 0 ) utm=184 stm=61 core=3
- 10-27 21:19:53.280 8027 8027 I dalvikvm: at android.view.GLES20Canvas.nDrawDisplayList(Native Method)
- 10-27 21:19:53.281 8027 8027 I dalvikvm: at android.view.GLES20Canvas.drawDisplayList(GLES20Canvas.java:420)
- 10-27 21:19:53.281 8027 8027 I dalvikvm: at android.view.HardwareRenderer$GlRenderer.drawDisplayList(HardwareRenderer.java:1709)
- 10-27 21:19:53.281 8027 8027 I dalvikvm: at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1525)
- 10-27 21:19:53.282 8027 8027 I dalvikvm: at android.view.ViewRootImpl.draw(ViewRootImpl.java:2475)
- 10-27 21:19:53.282 8027 8027 I dalvikvm: at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2347)
- 10-27 21:19:53.283 8027 8027 I dalvikvm: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1977)
- 10-27 21:19:53.284 8027 8027 I dalvikvm: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1094)
- 10-27 21:19:53.285 8027 8027 I dalvikvm: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5703)
- 10-27 21:19:53.285 8027 8027 I dalvikvm: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:764)
- 10-27 21:19:53.286 8027 8027 I dalvikvm: at android.view.Choreographer.doCallbacks(Choreographer.java:577)
- 10-27 21:19:53.287 8027 8027 I dalvikvm: at android.view.Choreographer.doFrame(Choreographer.java:547)
- 10-27 21:19:53.288 8027 8027 I dalvikvm: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:750)
- 10-27 21:19:53.289 8027 8027 I dalvikvm: at android.os.Handler.handleCallback(Handler.java:733)
- 10-27 21:19:53.289 8027 8027 I dalvikvm: at android.os.Handler.dispatchMessage(Handler.java:95)
- 10-27 21:19:53.290 8027 8027 I dalvikvm: at android.os.Looper.loop(Looper.java:136)
- 10-27 21:19:53.291 8027 8027 I dalvikvm: at android.app.ActivityThread.main(ActivityThread.java:5016)
- 10-27 21:19:53.291 8027 8027 I dalvikvm: at java.lang.reflect.Method.invokeNative(Native Method)
- 10-27 21:19:53.292 8027 8027 I dalvikvm: at java.lang.reflect.Method.invoke(Method.java:515)
- 10-27 21:19:53.293 8027 8027 I dalvikvm: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
- 10-27 21:19:53.293 8027 8027 I dalvikvm: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
- 10-27 21:19:53.293 8027 8027 I dalvikvm: at dalvik.system.NativeStart.main(Native Method)
正常情况下,view在被destroy后不应该再被绘制,通过跟孙念沟通,得知这种情况可能是view在destroy前没有remove导致的。
分析代码:
上面delete时的调用栈中有特别的两行:
- 10-27 21:19:52.990 8027 8027 D INJECT : at com.xiaomi.music.hybrid.HybridFragment.destroyHybridView(HybridFragment.java:64)
- 10-27 21:19:52.990 8027 8027 D INJECT : at com.xiaomi.music.hybrid.HybridFragment.onDestroyView(HybridFragment.java:115)
这个是应用的代码,而这个问题只有在这个应用上出现过,所以很可能是应用的代码引起的,
所以查了下opengrok中的代码,发现有两处destroyHybridView()的实现:
- @v8-kk-pisces-alpha/packages/apps/MiuiMusic/common/music_sdk/hybrid/src/com/xiaomi/music/hybrid/HybridFragment.java
- private void destroyHybridView() {
- for (HybridView view : mHybridViews) {
- if (view != null) {
- view.destroy();
- }
- }
- mHybridViews.clear();
- }
- @v8-kk-pisces-alpha/packages/apps/MiuiSdk/library/src/java/miui/hybrid/HybridFragment.java
- private void destroyHybridView() {
- for (HybridView view : mHybridViews) {
- if (view != null) {
- => if (view.getParent() != null) {
- => ((ViewGroup) view.getParent()).removeView(view);
- => }
- view.destroy();
- }
- }
- mHybridViews.clear();
- }
跟应用的同事沟通后得知,音乐应用是用上面的代码,也就是没有removeView的代码。
将上面代码中添加removeView的逻辑后不再复现问题。
虽然问题得到解决,但还不清楚为什么没有removeView会导致野指针。
为了找到根源仔细阅读了相关代码,发现代码中Render中有detachFunctor的代码:
- class GLES20Canvas extends HardwareCanvas {
- ...
- public void detachFunctor(int functor) {
- nDetachFunctor(mRenderer, functor);
- }
用studio在这个代码中设置断点,得到如下调用栈:
- java.lang.Thread.State: RUNNABLE
- at android.view.GLES20Canvas.detachFunctor(GLES20Canvas.java:321)
- at android.view.HardwareRenderer$GlRenderer.detachFunctor(HardwareRenderer.java:1791)
- at android.view.ViewRootImpl.detachFunctor(ViewRootImpl.java:744)
- at com.android.webview.chromium.DrawGLFunctor$DestroyRunnable.detachNativeFunctor(DrawGLFunctor.java:97)
- at com.android.webview.chromium.DrawGLFunctor.detach(DrawGLFunctor.java:53)
- at com.android.webview.chromium.WebViewChromium.onDetachedFromWindow(WebViewChromium.java:1718)
- at android.webkit.WebView.onDetachedFromWindow(WebView.java:2108)
- at android.view.View.dispatchDetachedFromWindow(View.java:12631)
- at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2587)
- at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3845)
- at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3818)
- at android.view.ViewGroup.removeView(ViewGroup.java:3750)
- at com.xiaomi.music.hybrid.HybridFragment.destroyHybridView(HybridFragment.java:66)
- at com.xiaomi.music.hybrid.HybridFragment.onDestroyView(HybridFragment.java:119)
- at com.miui.player.component.MusicBaseFragment.onDestroyView(MusicBaseFragment.java:216)
- at android.app.Fragment.performDestroyView(Fragment.java:1898)
- at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:954)
- at android.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1167)
- at android.app.BackStackRecord.popFromBackStack(BackStackRecord.java:715)
- at android.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1544)
- at android.app.FragmentManagerImpl$3.run(FragmentManager.java:502)
- at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1449)
- at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
- at android.os.Handler.handleCallback(Handler.java:733)
- at android.os.Handler.dispatchMessage(Handler.java:95)
- at android.os.Looper.loop(Looper.java:136)
- at android.app.ActivityThread.main(ActivityThread.java:5016)
- at java.lang.reflect.Method.invokeNative(Method.java:-1)
- at java.lang.reflect.Method.invoke(Method.java:515)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
- at dalvik.system.NativeStart.main(NativeStart.java:-1)
加了removeView后,会从Render中删除Functor,这样Render在绘制时,不再调用这个Functor。
这个问题只会在KK上有,L以后对Render做的很大改动,即使不做removeView,也不会存在野指针问题。
【本文是专栏“小米开放平台”的原创文章,“小米开放平台”微信公众号:xiaomideveloper】
标题名称:Java代码引起的NATIVE野指针问题(下)
URL链接:http://www.shufengxianlan.com/qtweb/news0/19750.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联