Merge libsandhook.so into core so to reduce binary size

This commit is contained in:
kotori0 2021-01-30 00:35:29 +08:00
parent c508cb382f
commit cca7ae0067
61 changed files with 80 additions and 128 deletions

View File

@ -152,7 +152,6 @@ afterEvaluate {
def prepareMagiskFilesTask = task("prepareMagiskFiles${variantCapped}", type: Delete) {
dependsOn "assemble${variantCapped}"
dependsOn tasks.getByPath(":sandhook-hooklib:copySandHook${variantCapped}LibraryToMagiskTemplate")
dependsOn tasks.getByPath(":key-selector:copyKeySelector${variantCapped}LibraryToMagiskTemplate")
doFirst {
copy {

View File

@ -16,3 +16,6 @@ endif (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
add_subdirectory(Dobby)
target_include_directories(dobby PUBLIC Dobby/include)
target_include_directories(dobby PUBLIC Dobby/builtin-plugin/BionicLinkerRestriction)
add_subdirectory(SandHook)
target_include_directories(sandhook.lspd PUBLIC SandHook)

View File

@ -5,6 +5,7 @@ ENABLE_LANGUAGE(ASM)
add_definitions(-std=c++11)
if (${CMAKE_ANDROID_ARCH_ABI} STREQUAL armeabi-v7a OR ${CMAKE_ANDROID_ARCH_ABI} STREQUAL arm64-v8a)
set(${PROJECT_NAME}_SOURCES
sandhook.cpp
trampoline/trampoline.cpp
@ -25,9 +26,11 @@ set(${PROJECT_NAME}_SOURCES
inst/insts_arm64.cpp
nativehook/native_hook.cpp
)
else()
set(${PROJECT_NAME}_SOURCES dummy.cpp)
endif()
add_library(${PROJECT_NAME}
SHARED
STATIC
${${PROJECT_NAME}_SOURCES})
target_link_libraries(${PROJECT_NAME} log)

View File

@ -0,0 +1,10 @@
//
// Created by Kotori0 on 2021/1/30.
//
#include "sandhook.h"
#include "includes/log.h"
bool JNI_Load_Ex(JNIEnv* env, jclass classSandHook, jclass classNeverCall) {
LOGE("Sandhook: Unsupported platform.");
return false;
}

View File

@ -547,8 +547,7 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
return JNI_VERSION_1_6;
}
extern "C"
JNIEXPORT bool JNI_Load_Ex(JNIEnv* env, jclass classSandHook, jclass classNeverCall) {
bool JNI_Load_Ex(JNIEnv* env, jclass classSandHook, jclass classNeverCall) {
int jniMethodSize = sizeof(JNINativeMethod);
if (env == nullptr || classSandHook == nullptr || classNeverCall == nullptr)

View File

@ -0,0 +1,9 @@
//
// Created by Kotori0 on 2021/1/29.
//
#ifndef EDXPOSED_SANDHOOK_H
#define EDXPOSED_SANDHOOK_H
#include <jni.h>
bool JNI_Load_Ex(JNIEnv* env, jclass classSandHook, jclass classNeverCall);
#endif //EDXPOSED_SANDHOOK_H

View File

@ -2,8 +2,7 @@ cmake_minimum_required(VERSION 3.4.1)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -Wall -Wextra -fvisibility=hidden")
aux_source_directory(src SRC_LIST)
add_library(yahfa STATIC ${SRC_LIST})
add_library(yahfa STATIC src/HookMain.cpp src/trampoline.c)
find_library(log-lib log)
target_link_libraries(yahfa ${log-lib})

View File

@ -19,7 +19,7 @@ jboolean Java_lab_galaxy_yahfa_HookMain_backupAndHookNative(JNIEnv *env, jclass
void setNonCompilable(void *method);
void *getArtMethod(JNIEnv *env, jobject jmethod);
void *getArtMethodYahfa(JNIEnv *env, jobject jmethod);
#ifdef __cplusplus
}

View File

@ -4,7 +4,9 @@
#ifndef YAHFA_TAMPOLINE_H
#define YAHFA_TAMPOLINE_H
#ifdef __cplusplus
extern "C" {
#endif
extern int SDKVersion;
extern int OFFSET_entry_point_from_quick_compiled_code_in_ArtMethod;
@ -18,5 +20,7 @@ void setupTrampoline();
void *genTrampoline(void *hookMethod);
#define DEFAULT_CAP 1 //size of each trampoline area would be no more than 4k Bytes(one page)
#ifdef __cplusplus
}
#endif
#endif //YAHFA_TAMPOLINE_H

View File

@ -1,8 +1,7 @@
#include "jni.h"
#include <string.h>
#include <cstring>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdbool.h>
#include <cstdlib>
#include "common.h"
#include "trampoline.h"
@ -21,7 +20,7 @@ static uint32_t kAccProtected = 0x0004; // field, method, ic
static uint32_t kAccStatic = 0x0008; // field, method, ic
static jfieldID fieldArtMethod = NULL;
static jfieldID fieldArtMethod = nullptr;
static inline uint32_t read32(void *addr) {
return *((uint32_t *) addr);
@ -39,14 +38,14 @@ static inline void writeAddr(void *addr, void *value) {
*((void **) addr) = value;
}
void Java_lab_galaxy_yahfa_HookMain_init(JNIEnv *env, jclass clazz, jint sdkVersion) {
extern "C" void Java_lab_galaxy_yahfa_HookMain_init(JNIEnv *env, jclass clazz, jint sdkVersion) {
SDKVersion = sdkVersion;
jclass classExecutable;
LOGI("init to SDK %d", sdkVersion);
switch (sdkVersion) {
case __ANDROID_API_R__:
classExecutable = (*env)->FindClass(env, "java/lang/reflect/Executable");
fieldArtMethod = (*env)->GetFieldID(env, classExecutable, "artMethod", "J");
classExecutable = env->FindClass("java/lang/reflect/Executable");
fieldArtMethod = env->GetFieldID(classExecutable, "artMethod", "J");
case __ANDROID_API_Q__:
case __ANDROID_API_P__:
kAccCompileDontBother = 0x02000000;
@ -188,63 +187,61 @@ static int doBackupAndHook(JNIEnv *env, void *targetMethod, void *hookMethod, vo
return 0;
}
void *getArtMethod(JNIEnv *env, jobject jmethod) {
void *artMethod = NULL;
void *getArtMethodYahfa(JNIEnv *env, jobject jmethod) {
void *artMethod = nullptr;
if (jmethod == NULL) {
if (jmethod == nullptr) {
return artMethod;
}
if (SDKVersion == __ANDROID_API_R__) {
artMethod = (void *) (*env)->GetLongField(env, jmethod, fieldArtMethod);
artMethod = (void *) env->GetLongField(jmethod, fieldArtMethod);
} else {
artMethod = (void *) (*env)->FromReflectedMethod(env, jmethod);
artMethod = (void *) env->FromReflectedMethod(jmethod);
}
LOGI("ArtMethod: %p", artMethod);
return artMethod;
}
jobject Java_lab_galaxy_yahfa_HookMain_findMethodNative(JNIEnv *env, jclass clazz,
extern "C" jobject Java_lab_galaxy_yahfa_HookMain_findMethodNative(JNIEnv *env, jclass clazz,
jclass targetClass, jstring methodName,
jstring methodSig) {
const char *c_methodName = (*env)->GetStringUTFChars(env, methodName, NULL);
const char *c_methodSig = (*env)->GetStringUTFChars(env, methodSig, NULL);
jobject ret = NULL;
const char *c_methodName = env->GetStringUTFChars(methodName, nullptr);
const char *c_methodSig = env->GetStringUTFChars(methodSig, nullptr);
jobject ret = nullptr;
//Try both GetMethodID and GetStaticMethodID -- Whatever works :)
jmethodID method = (*env)->GetMethodID(env, targetClass, c_methodName, c_methodSig);
if (!(*env)->ExceptionCheck(env)) {
ret = (*env)->ToReflectedMethod(env, targetClass, method, JNI_FALSE);
jmethodID method = env->GetMethodID(targetClass, c_methodName, c_methodSig);
if (!env->ExceptionCheck()) {
ret = env->ToReflectedMethod(targetClass, method, JNI_FALSE);
} else {
(*env)->ExceptionClear(env);
method = (*env)->GetStaticMethodID(env, targetClass, c_methodName, c_methodSig);
if (!(*env)->ExceptionCheck(env)) {
ret = (*env)->ToReflectedMethod(env, targetClass, method, JNI_TRUE);
env->ExceptionClear();
method = env->GetStaticMethodID(targetClass, c_methodName, c_methodSig);
if (!env->ExceptionCheck()) {
ret = env->ToReflectedMethod(targetClass, method, JNI_TRUE);
} else {
(*env)->ExceptionClear(env);
env->ExceptionClear();
}
}
(*env)->ReleaseStringUTFChars(env, methodName, c_methodName);
(*env)->ReleaseStringUTFChars(env, methodSig, c_methodSig);
env->ReleaseStringUTFChars(methodName, c_methodName);
env->ReleaseStringUTFChars(methodSig, c_methodSig);
return ret;
}
jboolean Java_lab_galaxy_yahfa_HookMain_backupAndHookNative(JNIEnv *env, jclass clazz,
extern "C" jboolean Java_lab_galaxy_yahfa_HookMain_backupAndHookNative(JNIEnv *env, jclass clazz,
jobject target, jobject hook,
jobject backup) {
if (!doBackupAndHook(env,
getArtMethod(env, target),
getArtMethod(env, hook),
getArtMethod(env, backup)
getArtMethodYahfa(env, target),
getArtMethodYahfa(env, hook),
getArtMethodYahfa(env, backup)
)) {
(*env)->NewGlobalRef(env,
hook); // keep a global ref so that the hook method would not be GCed
if (backup) (*env)->NewGlobalRef(env, backup);
env->NewGlobalRef(hook); // keep a global ref so that the hook method would not be GCed
if (backup) env->NewGlobalRef(backup);
return JNI_TRUE;
} else {
return JNI_FALSE;

View File

@ -9,4 +9,4 @@ add_library(riru_lspd SHARED ${SRC_LIST} ${SRC_JNI_LIST})
find_package(riru REQUIRED CONFIG)
find_library(log-lib log)
target_link_libraries(riru_lspd yahfa riru::riru xhook android dobby ${log-lib})
target_link_libraries(riru_lspd yahfa riru::riru xhook android dobby sandhook.lspd ${log-lib})

View File

@ -63,13 +63,6 @@ namespace lspd {
return misc_path_ / "cache" / suffix;
}
inline static auto GetLibSandHookName() {
if constexpr(lspd::is64)
return GetFrameworkPath("lib64/libsandhook.lspd.so");
else
return GetFrameworkPath("lib/libsandhook.lspd.so");
}
inline auto GetConfigPath(const std::string &suffix = {}) const {
return base_config_path_ / "conf" / suffix;
}

View File

@ -13,6 +13,7 @@
#include <android-base/strings.h>
#include <nativehelper/scoped_local_ref.h>
#include <jni/pending_hooks.h>
#include <sandhook.h>
#include <fstream>
#include <sstream>
#include "context.h"
@ -126,25 +127,19 @@ namespace lspd {
RegisterPendingHooks(env);
variant_ = Variant(ConfigManager::GetInstance()->GetVariant());
// LOGI("EdxpVariant: %d", variant_);
LOGI("LSP Variant: %d", variant_);
initialized_ = true;
if (variant_ == SANDHOOK) {
//for SandHook variant
ScopedDlHandle sandhook_handle(ConfigManager::GetLibSandHookName().c_str());
if (!sandhook_handle.IsValid()) {
return;
}
typedef bool *(*TYPE_JNI_LOAD)(JNIEnv *, jclass, jclass);
auto jni_load = sandhook_handle.DlSym<TYPE_JNI_LOAD>("JNI_Load_Ex");
ScopedLocalRef sandhook_class(env, FindClassFromLoader(env, kSandHookClassName));
ScopedLocalRef nevercall_class(env,
FindClassFromLoader(env, kSandHookNeverCallClassName));
if (sandhook_class == nullptr || nevercall_class == nullptr) { // fail-fast
return;
}
if (!jni_load(env, sandhook_class.get(), nevercall_class.get())) {
if (!JNI_Load_Ex(env, sandhook_class.get(), nevercall_class.get())) {
LOGE("SandHook: HookEntry class error. %d", getpid());
}

View File

@ -12,7 +12,7 @@ namespace lspd {
static std::unordered_set<void *> deopted_methods;
static void ClassLinker_setEntryPointsToInterpreter(JNI_START, jobject method) {
void *reflected_method = getArtMethod(env, method);
void *reflected_method = getArtMethodYahfa(env, method);
if (deopted_methods.count(reflected_method)) {
LOGD("method %p has been deopted before, skip...", reflected_method);
return;

View File

@ -19,10 +19,6 @@ namespace lspd {
return env->NewStringUTF(ConfigManager::GetInstance()->GetInstallerPackageName().c_str());
}
static jstring ConfigManager_getLibSandHookName(JNI_START) {
return env->NewStringUTF(ConfigManager::GetLibSandHookName().c_str());
}
static jstring ConfigManager_getDataPathPrefix(JNI_START) {
return env->NewStringUTF(ConfigManager::GetInstance()->GetDataPathPrefix().c_str());
}
@ -71,7 +67,6 @@ namespace lspd {
NATIVE_METHOD(ConfigManager, isResourcesHookEnabled, "()Z"),
NATIVE_METHOD(ConfigManager, isNoModuleLogEnabled, "()Z"),
NATIVE_METHOD(ConfigManager, getInstallerPackageName, "()Ljava/lang/String;"),
NATIVE_METHOD(ConfigManager, getLibSandHookName, "()Ljava/lang/String;"),
NATIVE_METHOD(ConfigManager, getDataPathPrefix, "()Ljava/lang/String;"),
NATIVE_METHOD(ConfigManager, getPrefsPath,
"(Ljava/lang/String;)Ljava/lang/String;"),

View File

@ -31,7 +31,7 @@ namespace lspd {
static void PendingHooks_recordPendingMethodNative(JNI_START, jobject method_ref, jclass class_ref) {
auto *class_ptr = art::Thread::Current().DecodeJObject(class_ref);
auto *method = getArtMethod(env, method_ref);
auto *method = getArtMethodYahfa(env, method_ref);
art::mirror::Class mirror_class(class_ptr);
if (auto def = mirror_class.GetClassDef(); LIKELY(def)) {
LOGD("record pending: %p (%s) with %p", class_ptr, mirror_class.GetDescriptor().c_str(), method);

View File

@ -25,11 +25,11 @@ namespace lspd {
}
static void Yahfa_recordHooked(JNI_START, jobject member) {
lspd::recordHooked(getArtMethod(env, member));
lspd::recordHooked(getArtMethodYahfa(env, member));
}
static jboolean Yahfa_isHooked(JNI_START, jobject member) {
return lspd::isHooked(getArtMethod(env, member));
return lspd::isHooked(getArtMethodYahfa(env, member));
}
static JNINativeMethod gMethods[] = {

View File

@ -12,8 +12,6 @@ public class ConfigManager {
public static native String getInstallerPackageName();
public static native String getLibSandHookName();
public static native String getPrefsPath(String suffix);
public static native String getCachePath(String suffix);

View File

@ -120,13 +120,7 @@ public final class SandHookXposedBridge {
}
public static void init() {
if (Process.is64Bit()) {
// SandHookConfig.libSandHookPath = "/system/lib64/libsandhook.lspd.so";
SandHookConfig.libSandHookPath = "/system/lib64/" + ConfigManager.getLibSandHookName();
} else {
// SandHookConfig.libSandHookPath = "/system/lib/libsandhook.lspd.so";
SandHookConfig.libSandHookPath = "/system/lib/" + ConfigManager.getLibSandHookName();
}
SandHookConfig.libSandHookPath = "";
SandHookConfig.libLoader = new SandHookConfig.LibLoader() {
@Override
public void loadLib() {

View File

@ -46,10 +46,6 @@ VERSION=$(grep_prop version "${TMPDIR}/module.prop")
RIRU_MIN_API_VERSION=$(grep_prop api "${TMPDIR}/module.prop")
LIB_RIRU_EDXP="libriru_${RIRU_EDXP}.so"
LIB_SANDHOOK_EDXP="lib$(getRandomNameExist 13 "lib" ".so" "
/system/lib
/system/lib64
").so"
### lang start ###
# Default en_US
@ -187,12 +183,10 @@ if [ "$ARCH" = "x86" ] || [ "$ARCH" = "x64" ]; then
else
ui_print "- ${LANG_CUST_INST_EXT_LIB_ARM}"
extract "$ZIPFILE" 'system/lib/libriru_lspd.so' "${MODPATH}"
extract "$ZIPFILE" 'system/lib/libsandhook.lspd.so' "${MODPATH}"
if [ "$IS64BIT" = true ]; then
ui_print "- ${LANG_CUST_INST_EXT_LIB_ARM64}"
extract "$ZIPFILE" 'system/lib64/libriru_lspd.so' "${MODPATH}"
extract "$ZIPFILE" 'system/lib64/libsandhook.lspd.so' "${MODPATH}"
fi
fi
@ -246,9 +240,9 @@ set_perm /data/misc/$MISC_PATH root root 0771 "u:object_r:magisk_file:s0" || abo
echo "[[ -f /data/adb/lspd/keep_data ]] || rm -rf /data/misc/$MISC_PATH" >> "${MODPATH}/uninstall.sh" || abortC "! ${LANG_CUST_ERR_CONF_UNINST}"
echo "[[ -f /data/adb/lspd/new_install ]] || rm -rf /data/adb/lspd" >> "${MODPATH}/uninstall.sh" || abortC "! ${LANG_CUST_ERR_CONF_UNINST}"
if [ $VARIANT == 17 ]; then
if [ $VARIANT == 17 ]; then # YAHFA
echo "1" > /data/misc/$MISC_PATH/variant
elif [ $VARIANT == 18 ]; then
elif [ $VARIANT == 18 ]; then # SandHook
echo "2" > /data/misc/$MISC_PATH/variant
else
abortC "${LANG_UTIL_ERR_VARIANT_UNSUPPORT} ${VARIANT}"
@ -261,10 +255,8 @@ mv "${MODPATH}/system/framework" "/data/misc/$MISC_PATH/framework"
mkdir -p "/data/misc/$MISC_PATH/framework/lib"
mv "${MODPATH}/system/lib/libsandhook.lspd.so" "/data/misc/$MISC_PATH/framework/lib/libsandhook.lspd.so"
if [ "$IS64BIT" = true ]; then
mkdir -p "/data/misc/$MISC_PATH/framework/lib64"
mv "${MODPATH}/system/lib64/libsandhook.lspd.so" "/data/misc/$MISC_PATH/framework/lib64/libsandhook.lspd.so"
fi
set_perm_recursive /data/misc/$MISC_PATH/framework root root 0755 0644 "u:object_r:magisk_file:s0" || abortC "! ${LANG_CUST_ERR_PERM}"

View File

@ -9,23 +9,9 @@ android {
versionCode 1
versionName "1.0"
externalNativeBuild {
cmake {
//arguments "-DCMAKE_BUILD_TYPE=Release"
}
}
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
consumerProguardFiles 'proguard-rules.pro'
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
}
}
buildTypes {
release {
minifyEnabled false
@ -39,27 +25,3 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
api project(':sandhook-annotation')
}
afterEvaluate {
android.libraryVariants.all { variant ->
def variantNameCapped = variant.name.capitalize()
def variantNameLowered = variant.name.toLowerCase()
task("copySandHook${variantNameCapped}LibraryToMagiskTemplate") {
def libPathRelease = "${buildDir}/intermediates/cmake/${variantNameLowered}/obj"
doLast {
copy {
include "libsandhook.lspd.so"
from "${libPathRelease}/armeabi-v7a"
into "${zipPathMagiskReleasePath}/system/lib"
}
copy {
include "libsandhook.lspd.so"
from "${libPathRelease}/arm64-v8a"
into "${zipPathMagiskReleasePath}/system/lib64"
}
}
}
}
}