mirror of
https://github.com/rhunk/SnapEnhance.git
synced 2025-05-29 13:00:17 +02:00
feat(native): module base, size
This commit is contained in:
parent
c0225919e9
commit
d21de408e5
@ -106,6 +106,5 @@ class EventDispatcher(
|
||||
if (event.canceled) param.setResult(null)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -98,7 +98,7 @@ class SnapEnhance {
|
||||
measureTime {
|
||||
with(appContext) {
|
||||
runCatching {
|
||||
native.init()
|
||||
native.initOnce(appContext.androidContext.classLoader)
|
||||
}.onFailure {
|
||||
Logger.xposedLog("Failed to init native", it)
|
||||
return
|
||||
|
@ -1,17 +1,16 @@
|
||||
#include <jni.h>
|
||||
#include <string>
|
||||
#include <dobby.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
#include "logger.h"
|
||||
#include "config.h"
|
||||
#include "util.h"
|
||||
|
||||
static native_config_t *native_config;
|
||||
|
||||
static auto fstat_original = fstat;
|
||||
static auto fstat_original = (int (*)(int, struct stat *)) nullptr;
|
||||
static int fstat_hook(int fd, struct stat *buf) {
|
||||
char name[256];
|
||||
memset(name, 0, 256);
|
||||
@ -35,14 +34,37 @@ static int fstat_hook(int fd, struct stat *buf) {
|
||||
}
|
||||
|
||||
|
||||
#define GET_BOOL_FIELD(env, clazz, field) env->GetBooleanField(clazz, env->GetFieldID(clazz, field, "Z"))
|
||||
extern "C" JNIEXPORT void JNICALL
|
||||
init(JNIEnv *env, jobject clazz, jobject classloader) {
|
||||
LOGD("initializing native");
|
||||
// config
|
||||
native_config = new native_config_t;
|
||||
|
||||
// load libclient.so
|
||||
util::load_library(env, classloader, "client");
|
||||
auto client_module = util::get_module("libclient.so");
|
||||
if (client_module.base == 0) {
|
||||
LOGE("libclient not found");
|
||||
return;
|
||||
}
|
||||
client_module.base -= 0x1000;
|
||||
LOGD("libclient: offset: 0x%x size: 0x%x", client_module.base, client_module.size);
|
||||
|
||||
// hooks
|
||||
DobbyHook((void *) DobbySymbolResolver("libc.so", "fstat"), (void *) fstat_hook,
|
||||
(void **) &fstat_original);
|
||||
|
||||
LOGD("native initialized");
|
||||
}
|
||||
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL
|
||||
loadConfig(JNIEnv *env, jobject clazz, jobject config_object) {
|
||||
auto native_config_class = env->GetObjectClass(config_object);
|
||||
auto native_config_clazz = env->GetObjectClass(config_object);
|
||||
#define GET_CONFIG_BOOL(name) env->GetBooleanField(config_object, env->GetFieldID(native_config_clazz, name, "Z"))
|
||||
|
||||
native_config->disable_bitmoji = GET_BOOL_FIELD(env, native_config_class, "disableBitmoji");
|
||||
native_config->disable_metrics = GET_BOOL_FIELD(env, native_config_class, "disableMetrics");
|
||||
native_config->disable_bitmoji = GET_CONFIG_BOOL("disableBitmoji");
|
||||
native_config->disable_metrics = GET_CONFIG_BOOL("disableMetrics");
|
||||
|
||||
LOGD("config loaded");
|
||||
}
|
||||
@ -50,18 +72,12 @@ loadConfig(JNIEnv *env, jobject clazz, jobject config_object) {
|
||||
//jni onload
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
JNI_OnLoad(JavaVM *vm, void *reserved) {
|
||||
LOGD("initializing native");
|
||||
// config
|
||||
native_config = new native_config_t;
|
||||
|
||||
// hooks
|
||||
DobbyHook((void *) fstat_original,(void *) fstat_hook,(void **) &fstat_original);
|
||||
|
||||
// register native methods
|
||||
JNIEnv *env = nullptr;
|
||||
vm->GetEnv((void **) &env, JNI_VERSION_1_6);
|
||||
|
||||
auto methods = std::vector<JNINativeMethod>();
|
||||
methods.push_back({"init", "(Ljava/lang/ClassLoader;)V", (void *) init});
|
||||
methods.push_back({"loadConfig", "(Lme/rhunk/snapenhance/nativelib/NativeConfig;)V", (void *) loadConfig});
|
||||
|
||||
env->RegisterNatives(
|
||||
@ -69,8 +85,5 @@ JNI_OnLoad(JavaVM *vm, void *reserved) {
|
||||
methods.data(),
|
||||
methods.size()
|
||||
);
|
||||
|
||||
LOGD("native initialized");
|
||||
|
||||
return JNI_VERSION_1_6;
|
||||
}
|
||||
|
@ -7,4 +7,5 @@
|
||||
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
|
||||
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
|
||||
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
|
||||
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
|
||||
|
||||
|
73
native/jni/src/util.h
Normal file
73
native/jni/src/util.h
Normal file
@ -0,0 +1,73 @@
|
||||
#pragma once
|
||||
#include <unistd.h>
|
||||
#include <dlfcn.h>
|
||||
#include <elf.h>
|
||||
|
||||
namespace util {
|
||||
typedef struct {
|
||||
uintptr_t base;
|
||||
size_t size;
|
||||
} ModuleInfo;
|
||||
|
||||
static void hexDump(void* ptr, uint8_t line_length, uint32_t lines) {
|
||||
auto* p = (unsigned char*)ptr;
|
||||
for (uint8_t i = 0; i < lines; i++) {
|
||||
std::string line;
|
||||
for (uint8_t j = 0; j < line_length; j++) {
|
||||
char buf[3];
|
||||
sprintf(buf, "%02x", p[i * line_length + j]);
|
||||
line += buf;
|
||||
line += " ";
|
||||
}
|
||||
LOGI("%s", line.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
static ModuleInfo get_module(const char* libname)
|
||||
{
|
||||
char path[256];
|
||||
char buff[256];
|
||||
int len_libname = strlen(libname);
|
||||
FILE* file;
|
||||
uintptr_t addr = 0;
|
||||
size_t size = 0;
|
||||
|
||||
snprintf(path, sizeof path, "/proc/%d/smaps", getpid());
|
||||
file = fopen(path, "rt");
|
||||
if (file == NULL)
|
||||
return {0, 0};
|
||||
|
||||
while (fgets(buff, sizeof buff, file) != NULL) {
|
||||
int len = strlen(buff);
|
||||
if (len > 0 && buff[len-1] == '\n') {
|
||||
buff[--len] = '\0';
|
||||
}
|
||||
if (len <= len_libname || memcmp(buff + len - len_libname, libname, len_libname)) {
|
||||
continue;
|
||||
}
|
||||
size_t start, end, offset;
|
||||
char flags[4];
|
||||
if (sscanf(buff, "%zx-%zx %c%c%c%c %zx", &start, &end,
|
||||
&flags[0], &flags[1], &flags[2], &flags[3], &offset) != 7) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (flags[0] != 'r' || flags[2] != 'x') {
|
||||
continue;
|
||||
}
|
||||
addr = start - offset;
|
||||
size = end - start;
|
||||
break;
|
||||
}
|
||||
fclose(file);
|
||||
return {addr, size};
|
||||
}
|
||||
|
||||
void load_library(JNIEnv* env, jobject classLoader, const char* libName) {
|
||||
auto runtimeClass = env->FindClass("java/lang/Runtime");
|
||||
auto getRuntimeMethod = env->GetStaticMethodID(runtimeClass, "getRuntime", "()Ljava/lang/Runtime;");
|
||||
auto runtime = env->CallStaticObjectMethod(runtimeClass, getRuntimeMethod);
|
||||
auto loadLibraryMethod = env->GetMethodID(runtimeClass, "loadLibrary0", "(Ljava/lang/ClassLoader;Ljava/lang/String;)V");
|
||||
env->CallVoidMethod(runtime, loadLibraryMethod, classLoader, env->NewStringUTF(libName));
|
||||
}
|
||||
}
|
@ -1,10 +1,11 @@
|
||||
package me.rhunk.snapenhance.nativelib
|
||||
|
||||
class NativeLib {
|
||||
fun init() {
|
||||
fun initOnce(classloader: ClassLoader) {
|
||||
System.loadLibrary("nativelib")
|
||||
init(classloader)
|
||||
}
|
||||
|
||||
|
||||
external fun init(classLoader: ClassLoader)
|
||||
external fun loadConfig(config: NativeConfig)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user