Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
finish diagnostic feature of app
Browse files Browse the repository at this point in the history
  • Loading branch information
RikkaW committed Feb 17, 2019
1 parent fa834da commit 11da6f5
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 17 deletions.
6 changes: 4 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
versionName "0.0.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
Expand All @@ -32,7 +32,8 @@ android {
}
release {
signingConfig signingConfigs.sign
minifyEnabled false
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
Expand All @@ -57,6 +58,7 @@ dependencies {
implementation "moe.shizuku.support:recyclerview-utils:3.0.4"
implementation "moe.shizuku.support:design:3.1.0"
implementation "moe.shizuku.support:support-utils:3.0.4"
implementation "moe.shizuku.support:htmlcompat:2.0.0"
}
repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package moe.riru.manager;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import static org.junit.Assert.*;

/**
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning" >

<activity android:name=".MainActivity">
Expand Down
54 changes: 41 additions & 13 deletions app/src/main/cpp/helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ int xh_elf_check_elfheader(uintptr_t base_addr) {

//check machine
#if defined(__arm__)
if(EM_ARM != ehdr->e_machine) return XH_ERRNO_FORMAT;
if(EM_ARM != ehdr->e_machine) return 1;
#elif defined(__aarch64__)
if (EM_AARCH64 != ehdr->e_machine) return 1;
#elif defined(__i386__)
if(EM_386 != ehdr->e_machine) return XH_ERRNO_FORMAT;
if(EM_386 != ehdr->e_machine) return 1;
#elif defined(__x86_64__)
if(EM_X86_64 != ehdr->e_machine) return XH_ERRNO_FORMAT;
if(EM_X86_64 != ehdr->e_machine) return 1;
#else
return XH_ERRNO_FORMAT;
#endif
Expand All @@ -72,10 +72,6 @@ int xh_elf_check_elfheader(uintptr_t base_addr) {
return 0;
}

#define PAGE_START(addr) ((addr) & PAGE_MASK)
#define PAGE_END(addr) (PAGE_START(addr + sizeof(uintptr_t) - 1) + PAGE_SIZE)
#define PAGE_COVER(addr) (PAGE_END(addr) - PAGE_START(addr))

static int init_elf(const char *pathname, uintptr_t base_in_mem) {
struct stat statbuf{};
int fd = open(pathname, O_RDONLY | O_CLOEXEC);
Expand Down Expand Up @@ -138,14 +134,20 @@ static int init_elf(const char *pathname, uintptr_t base_in_mem) {

if (strcmp("riru_get_version", (char *) dynstr + dynsyms[i].st_name) == 0)
riru_get_version_addr = dynsyms[i].st_value + base_in_mem;
else if (strcmp("riru_get_original_native_methods", (char *) dynstr + dynsyms[i].st_name) == 0)
else if (strcmp("riru_get_original_native_methods",
(char *) dynstr + dynsyms[i].st_name) == 0)
riru_get_original_native_methods_addr = dynsyms[i].st_value + base_in_mem;
else if (strcmp("riru_is_zygote_methods_replaced", (char *) dynstr + dynsyms[i].st_name) == 0)
else if (strcmp("riru_is_zygote_methods_replaced",
(char *) dynstr + dynsyms[i].st_name) == 0)
riru_is_zygote_methods_replaced_addr = dynsyms[i].st_value + base_in_mem;
else if (strcmp("riru_get_nativeForkAndSpecialize_calls_count", (char *) dynstr + dynsyms[i].st_name) == 0)
riru_get_nativeForkAndSpecialize_calls_count_addr = dynsyms[i].st_value + base_in_mem;
else if (strcmp("riru_get_nativeForkSystemServer_calls_count", (char *) dynstr + dynsyms[i].st_name) == 0)
riru_get_nativeForkSystemServer_calls_count_addr = dynsyms[i].st_value + base_in_mem;
else if (strcmp("riru_get_nativeForkAndSpecialize_calls_count",
(char *) dynstr + dynsyms[i].st_name) == 0)
riru_get_nativeForkAndSpecialize_calls_count_addr =
dynsyms[i].st_value + base_in_mem;
else if (strcmp("riru_get_nativeForkSystemServer_calls_count",
(char *) dynstr + dynsyms[i].st_name) == 0)
riru_get_nativeForkSystemServer_calls_count_addr =
dynsyms[i].st_value + base_in_mem;
}
}

Expand Down Expand Up @@ -205,13 +207,39 @@ static jint get_nativeForkSystemServer_calls_count(JNIEnv *env, jobject thiz) {
return ((int (*)()) riru_get_nativeForkSystemServer_calls_count_addr)();
}

static jstring get_nativeForkAndSpecialize_signature(JNIEnv *env, jobject thiz) {
if (!riru_get_original_native_methods_addr)
return nullptr;

auto method = ((const JNINativeMethod *(*)(const char *, const char *, const char *)) riru_get_original_native_methods_addr)(
"com/android/internal/os/Zygote", "nativeForkAndSpecialize", nullptr);
if (method != nullptr)
return env->NewStringUTF(method->signature);
else
return nullptr;
}

static jstring get_nativeForkSystemServer_signature(JNIEnv *env, jobject thiz) {
if (!riru_get_original_native_methods_addr)
return nullptr;

auto method = ((const JNINativeMethod *(*)(const char *, const char *, const char *)) riru_get_original_native_methods_addr)(
"com/android/internal/os/Zygote", "nativeForkSystemServer", nullptr);
if (method != nullptr)
return env->NewStringUTF(method->signature);
else
return nullptr;
}

static JNINativeMethod gMethods[] = {
{"init", "()Z", (void *) init},
{"isRiruModuleExists", "(Ljava/lang/String;)Z", (void *) is_riru_module_exists},
{"getRiruVersion", "()I", (void *) get_riru_rersion},
{"isZygoteMethodsReplaced", "()Z", (void *) is_zygote_methods_replaced},
{"getNativeForkAndSpecializeCallsCount", "()I", (void *) get_nativeForkAndSpecialize_calls_count},
{"getNativeForkSystemServerCallsCount", "()I", (void *) get_nativeForkSystemServer_calls_count},
{"getNativeForkAndSpecializeSignature", "()Ljava/lang/String;", (void *) get_nativeForkAndSpecialize_signature},
{"getNativeForkSystemServerSignature", "()Ljava/lang/String;", (void *) get_nativeForkSystemServer_signature},
};

static int registerNativeMethods(JNIEnv *env, const char *className,
Expand Down
58 changes: 58 additions & 0 deletions app/src/main/java/moe/riru/manager/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,80 @@
package moe.riru.manager;

import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import moe.riru.manager.app.BaseActivity;
import moe.riru.manager.utils.NativeHelper;
import moe.shizuku.support.text.HtmlCompat;

public class MainActivity extends BaseActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

boolean init, isZygoteMethodsReplaced;
int version, nativeForkAndSpecializeCallsCount, nativeForkSystemServerCallsCount;
String nativeForkAndSpecializeSignature, nativeForkSystemServerSignature;

StringBuilder sb = new StringBuilder();

init = NativeHelper.init();
if (init) {
version = NativeHelper.getRiruVersion();
if (version < 13) {
sb.append("Riru version less than 13, diagnostic is only available for Riru v13 or above.");
} else {
isZygoteMethodsReplaced = NativeHelper.isZygoteMethodsReplaced();
nativeForkAndSpecializeCallsCount = NativeHelper.getNativeForkAndSpecializeCallsCount();
nativeForkSystemServerCallsCount = NativeHelper.getNativeForkSystemServerCallsCount();
nativeForkAndSpecializeSignature = NativeHelper.getNativeForkAndSpecializeSignature();
nativeForkSystemServerSignature = NativeHelper.getNativeForkSystemServerSignature();

sb.append("Riru v").append(version).append(" found.").append("<br>");

if (isZygoteMethodsReplaced) {
sb.append("Native methods of Zygote class replaced.").append("<br><br>")
.append("nativeForkAndSpecialize calls count: ").append(nativeForkAndSpecializeCallsCount).append("<br>")
.append("nativeForkSystemServer calls count: ").append(nativeForkSystemServerCallsCount).append("<br>");

if (nativeForkAndSpecializeCallsCount == 0) {
sb.append("<br>nativeForkAndSpecialize calls count is 0, Riru is not working correctly.<br>This may because Riru's hook is overwritten by other things, please check yourself.");
} else if (nativeForkSystemServerCallsCount != 0) {
sb.append("<br>Everything looks fine :D");
}
} else {
sb.append("However, native methods of Zygote class not replaced, please contact developer with the following information.").append("<br><br>")
.append("nativeForkAndSpecializeSignature:<br><font face=\"monospace\">").append(nativeForkAndSpecializeSignature).append("</font><br><br>")
.append("getNativeForkSystemServerSignature:<br><font face=\"monospace\">").append(nativeForkSystemServerSignature).append("</font>");
}
}
} else {
sb.append("Riru not found in memory.");
}

new AlertDialog.Builder(this)
.setMessage(HtmlCompat.fromHtml(sb.toString()))
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
finish();
})
.setNeutralButton("Copy", (dialog, which) -> {
getSystemService(ClipboardManager.class).setPrimaryClip(ClipData.newPlainText("text", HtmlCompat.fromHtml(sb.toString()).toString()));
finish();
})
.setCancelable(false)
.show();

Log.i("RiruManager", "init: " + NativeHelper.init());
Log.i("RiruManager", "getRiruVersion: " + NativeHelper.getRiruVersion());
Log.i("RiruManager", "isZygoteMethodsReplaced: " + NativeHelper.isZygoteMethodsReplaced());
Log.i("RiruManager", "getNativeForkAndSpecializeCallsCount: " + NativeHelper.getNativeForkAndSpecializeCallsCount());
Log.i("RiruManager", "getNativeForkSystemServerCallsCount: " + NativeHelper.getNativeForkSystemServerCallsCount());
Log.i("RiruManager", "getNativeForkAndSpecializeSignature: " + NativeHelper.getNativeForkAndSpecializeSignature());
Log.i("RiruManager", "getNativeForkSystemServerSignature: " + NativeHelper.getNativeForkSystemServerSignature());
}
}
5 changes: 5 additions & 0 deletions app/src/main/java/moe/riru/manager/utils/NativeHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package moe.riru.manager.utils;

import androidx.annotation.Keep;

@Keep
public class NativeHelper {

static {
Expand All @@ -12,4 +15,6 @@ public class NativeHelper {
public static native boolean isZygoteMethodsReplaced();
public static native int getNativeForkAndSpecializeCallsCount();
public static native int getNativeForkSystemServerCallsCount();
public static native String getNativeForkAndSpecializeSignature();
public static native String getNativeForkSystemServerSignature();
}

0 comments on commit 11da6f5

Please sign in to comment.