找到一个不算是 bug 的 NPE--希望 DroidPluginTeam 接受

作者: stiflerfang创建于 2021年11月9日更新于 2021年11月9日

Since I wanted to see the doHookInner log in the HookerMethodHandler class, I changed the code in lines 61-63 from if (time > 5) { Log.i(TAG, "doHookInner method(%s.%s) cost %s ms", method.getDeclaringClass().getName(), method.getName(), time); } to //if (time > 5) { Log.i(TAG, "doHookInner method(%s.%s) cost %s ms", method.getDeclaringClass().getName(), method.getName(), time); //} I commented out the code for time > 5, and when I debugged at the breakpoint, I found an NPE error. In line 122 of the Log class, an NPE was reported, where sHandler is empty, but sHandler is initialized in static code. By logic, when calling static methods, these static code blocks have been executed, and there should be no NPE problem. After debugging again, I found the problem in the HoolFactory method: public void installHook(Hook hook, ClassLoader cl) { try { hook.onInstall(cl); synchronized (mHookList) { mHookList.add(hook); } } catch (Throwable throwable) { Log.e(TAG, "installHook %s error", throwable, hook); } } When an exception is thrown, the Log.e method is called, and then the Log method is initialized in the initial part. In the Log method, there is a line of code: private static final File sDir = new File(Environment.getExternalStorageDirectory(), "360Log/Plugin/"); This method will call the system call, because the system call of Environment has already been hooked, so it will go to the HookerMethodHandler, and then to Log.i(TAG, "doHookInner method(%s.%s) cost %s ms", method.getDeclaringClass().getName(), method.getName(), time); At this time, it will call the i method of Log, and finally it will go to sHandler.post(new Runnable() { At this time, the initialization static code block of sHandlerThread has not been initialized, and the above problem will occur.

内容来源: DroidPluginTeam/DroidPlugin