From 11b2ddbad8fe3f1c365e6f29b65a4c24e69319f2 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Wed, 23 Nov 2022 03:49:31 +0800 Subject: [PATCH] Fix zygisk v4 ApiTable abi Also refactor some code to let the compiler check the abi Co-authored-by: topjohnwu --- native/src/zygisk/hook.cpp | 34 ++++++++++++++-------------------- native/src/zygisk/module.hpp | 19 ++++++++++++------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/native/src/zygisk/hook.cpp b/native/src/zygisk/hook.cpp index 257a09b68..d33677af2 100644 --- a/native/src/zygisk/hook.cpp +++ b/native/src/zygisk/hook.cpp @@ -311,47 +311,41 @@ ZygiskModule::ZygiskModule(int id, void *handle, void *entry) api.base.registerModule = &ZygiskModule::RegisterModuleImpl; } -bool ZygiskModule::RegisterModuleImpl(api_abi_base *api, long *module) { +bool ZygiskModule::RegisterModuleImpl(ApiTable *api, long *module) { long api_version = *module; // Unsupported version if (api_version > ZYGISK_API_VERSION) return false; // Set the actual module_abi* - api->impl->mod = { module }; + api->base.impl->mod = { module }; // Fill in API accordingly with module API version switch (api_version) { - case 4: { - auto v4 = static_cast(api); - v4->exemptFd = [](int fd) { return g_ctx != nullptr && g_ctx->exempt_fd(fd); }; - } + case 4: + api->v4.exemptFd = [](int fd) { return g_ctx != nullptr && g_ctx->exempt_fd(fd); }; // fallthrough case 3: - case 2: { - auto v2 = static_cast(api); - v2->getModuleDir = [](ZygiskModule *m) { return m->getModuleDir(); }; - v2->getFlags = [](auto) { return ZygiskModule::getFlags(); }; - } + case 2: + api->v2.getModuleDir = [](ZygiskModule *m) { return m->getModuleDir(); }; + api->v2.getFlags = [](auto) { return ZygiskModule::getFlags(); }; // fallthrough - case 1: { - auto v1 = static_cast(api); - v1->hookJniNativeMethods = &hookJniNativeMethods; - v1->pltHookRegister = [](const char *p, const char *s, void *n, void **o) { + case 1: + api->v1.hookJniNativeMethods = &hookJniNativeMethods; + api->v1.pltHookRegister = [](const char *p, const char *s, void *n, void **o) { xhook_register(p, s, n, o); }; - v1->pltHookExclude = [](const char *p, const char *s) { + api->v1.pltHookExclude = [](const char *p, const char *s) { xhook_ignore(p, s); }; - v1->pltHookCommit = [] { + api->v1.pltHookCommit = [] { bool r = xhook_refresh(0) == 0; xhook_clear(); return r; }; - v1->connectCompanion = [](ZygiskModule *m) { return m->connectCompanion(); }; - v1->setOption = [](ZygiskModule *m, auto opt) { m->setOption(opt); }; + api->v1.connectCompanion = [](ZygiskModule *m) { return m->connectCompanion(); }; + api->v1.setOption = [](ZygiskModule *m, auto opt) { m->setOption(opt); }; break; - } default: // Unknown version number return false; diff --git a/native/src/zygisk/module.hpp b/native/src/zygisk/module.hpp index e892b1ed1..39144b78a 100644 --- a/native/src/zygisk/module.hpp +++ b/native/src/zygisk/module.hpp @@ -22,6 +22,8 @@ struct api_abi_v2; using api_abi_v3 = api_abi_v2; struct api_abi_v4; +union ApiTable; + struct AppSpecializeArgs_v3 { jint &uid; jint &gid; @@ -118,7 +120,7 @@ enum : uint32_t { struct api_abi_base { ZygiskModule *impl; - bool (*registerModule)(api_abi_base *, long *); + bool (*registerModule)(ApiTable *, long *); }; struct api_abi_v1 : public api_abi_base { @@ -140,6 +142,13 @@ struct api_abi_v4 : public api_abi_v2 { bool (*exemptFd)(int); }; +union ApiTable { + api_abi_base base; + api_abi_v1 v1; + api_abi_v2 v2; + api_abi_v4 v4; +}; + #define call_app(method) \ switch (*mod.api_version) { \ case 1: \ @@ -182,7 +191,7 @@ struct ZygiskModule { ZygiskModule(int id, void *handle, void *entry); - static bool RegisterModuleImpl(api_abi_base *api, long *module); + static bool RegisterModuleImpl(ApiTable *api, long *module); private: const int id; @@ -194,11 +203,7 @@ private: void (* const fn)(void *, void *); } entry; - union { - api_abi_base base; - api_abi_v1 v1; - api_abi_v2 v2; - } api; + ApiTable api; union { long *api_version;