Skip to content

Commit

Permalink
支持更多模拟器
Browse files Browse the repository at this point in the history
删除模块路径硬编码
优化代码
  • Loading branch information
Perfare committed Apr 8, 2023
1 parent 07c811b commit 70b95e8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 33 deletions.
52 changes: 30 additions & 22 deletions module/src/main/cpp/hack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
#include <thread>
#include <sys/mman.h>
#include <linux/unistd.h>
#include <array>

static int GetAndroidApiLevel() {
char prop_value[PROP_VALUE_MAX];
__system_property_get("ro.build.version.sdk", prop_value);
return atoi(prop_value);
static std::string GetNativeBridgeLibrary() {
auto value = std::array<char, PROP_VALUE_MAX>();
__system_property_get("ro.dalvik.vm.native.bridge", value.data());
return {value.data()};
}

void hack_start(const char *game_data_dir) {
Expand Down Expand Up @@ -64,27 +65,22 @@ struct NativeBridgeCallbacks {

void hack_prepare(const char *game_data_dir, void *data, size_t length) {
LOGI("hack thread: %d", gettid());
int api_level = GetAndroidApiLevel();
int api_level = android_get_device_api_level();
LOGI("api level: %d", api_level);

#if defined(__i386__) || defined(__x86_64__)
//TODO 等待houdini初始化
sleep(5);

auto libhoudini = dlopen("libhoudini.so", RTLD_NOW);
if (libhoudini) {
LOGI("houdini %p", libhoudini);

int fd = syscall(__NR_memfd_create, "anon", MFD_CLOEXEC);
ftruncate(fd, (off_t) length);
void *mem = mmap(nullptr, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
memcpy(mem, data, length);
munmap(mem, length);
char path[PATH_MAX];
snprintf(path, PATH_MAX, "/proc/self/fd/%d", fd);
LOGI("arm path %s", path);

auto callbacks = (NativeBridgeCallbacks *) dlsym(libhoudini, "NativeBridgeItf");
auto nb = dlopen("libhoudini.so", RTLD_NOW);
if (!nb) {
auto native_bridge = GetNativeBridgeLibrary();
LOGI("native bridge: %s", native_bridge.data());
nb = dlopen(native_bridge.data(), RTLD_NOW);
}
if (nb) {
LOGI("nb %p", nb);
auto callbacks = (NativeBridgeCallbacks *) dlsym(nb, "NativeBridgeItf");
if (callbacks) {
LOGI("NativeBridgeLoadLibrary %p", callbacks->loadLibrary);
LOGI("NativeBridgeLoadLibraryExt %p", callbacks->loadLibraryExt);
Expand All @@ -93,6 +89,17 @@ void hack_prepare(const char *game_data_dir, void *data, size_t length) {
auto JNI_GetCreatedJavaVMs = (jint (*)(JavaVM **, jsize, jsize *)) dlsym(libart,
"JNI_GetCreatedJavaVMs");
LOGI("JNI_GetCreatedJavaVMs %p", JNI_GetCreatedJavaVMs);

int fd = syscall(__NR_memfd_create, "anon", MFD_CLOEXEC);
ftruncate(fd, (off_t) length);
void *mem = mmap(nullptr, length, PROT_WRITE, MAP_SHARED, fd, 0);
memcpy(mem, data, length);
munmap(mem, length);
munmap(data, length);
char path[PATH_MAX];
snprintf(path, PATH_MAX, "/proc/self/fd/%d", fd);
LOGI("arm path %s", path);

void *arm_handle;
if (api_level >= 26) {
arm_handle = callbacks->loadLibraryExt(path, RTLD_NOW, (void *) 3);
Expand All @@ -105,14 +112,15 @@ void hack_prepare(const char *game_data_dir, void *data, size_t length) {
jsize num_vms;
jint status = JNI_GetCreatedJavaVMs(vms_buf, 1, &num_vms);
if (status == JNI_OK && num_vms > 0) {
auto init = (void (*)(JavaVM *vm, void *reserved)) callbacks->getTrampoline(
arm_handle, "JNI_OnLoad", nullptr, 0);
auto init = (void (*)(JavaVM *, void *)) callbacks->getTrampoline(arm_handle,
"JNI_OnLoad",
nullptr, 0);
LOGI("JNI_OnLoad %p", init);
init(vms_buf[0], (void *) game_data_dir);
}
}
close(fd);
}
close(fd);
} else {
#endif
hack_start(game_data_dir);
Expand Down
2 changes: 1 addition & 1 deletion module/src/main/cpp/il2cpp_dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ void il2cpp_dump(const char *outDir) {
auto imageName = std::string(image_name);
auto pos = imageName.rfind('.');
auto imageNameNoExt = imageName.substr(0, pos);
auto assemblyFileName = il2cpp_string_new(imageNameNoExt.c_str());
auto assemblyFileName = il2cpp_string_new(imageNameNoExt.data());
auto reflectionAssembly = ((Assembly_Load_ftn) assemblyLoad->methodPointer)(nullptr,
assemblyFileName,
nullptr);
Expand Down
24 changes: 14 additions & 10 deletions module/src/main/cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cstring>
#include <thread>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
Expand Down Expand Up @@ -52,20 +53,23 @@ class MyModule : public zygisk::ModuleBase {
strcpy(game_data_dir, app_data_dir);

#if defined(__i386__)
auto path = "/data/adb/modules/zygisk_il2cppdumper/zygisk/armeabi-v7a.so";
auto path = "zygisk/armeabi-v7a.so";
#endif
#if defined(__x86_64__)
auto path = "/data/adb/modules/zygisk_il2cppdumper/zygisk/arm64-v8a.so";
auto path = "zygisk/arm64-v8a.so";
#endif
#if defined(__i386__) || defined(__x86_64__)
auto fd = open(path, O_RDONLY);
struct stat sb{};
fstat(fd, &sb);
length = sb.st_size;
LOGI("arm file length : %zu", length);
data = malloc(length);
read(fd, data, length);
close(fd);
int dirfd = api->getModuleDir();
int fd = openat(dirfd, path, O_RDONLY);
if (fd != -1) {
struct stat sb{};
fstat(fd, &sb);
length = sb.st_size;
data = mmap(nullptr, length, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
} else {
LOGW("Unable to open arm file");
}
#endif
}
}
Expand Down

0 comments on commit 70b95e8

Please sign in to comment.