Fix zygisk v4 ApiTable abi

Also refactor some code to let the compiler check the abi

Co-authored-by: topjohnwu <topjohnwu@gmail.com>
This commit is contained in:
LoveSy 2022-11-23 03:49:31 +08:00 committed by GitHub
parent cf9957ce4d
commit 11b2ddbad8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 27 deletions

View File

@ -311,47 +311,41 @@ ZygiskModule::ZygiskModule(int id, void *handle, void *entry)
api.base.registerModule = &ZygiskModule::RegisterModuleImpl; 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; long api_version = *module;
// Unsupported version // Unsupported version
if (api_version > ZYGISK_API_VERSION) if (api_version > ZYGISK_API_VERSION)
return false; return false;
// Set the actual module_abi* // Set the actual module_abi*
api->impl->mod = { module }; api->base.impl->mod = { module };
// Fill in API accordingly with module API version // Fill in API accordingly with module API version
switch (api_version) { switch (api_version) {
case 4: { case 4:
auto v4 = static_cast<api_abi_v4 *>(api); api->v4.exemptFd = [](int fd) { return g_ctx != nullptr && g_ctx->exempt_fd(fd); };
v4->exemptFd = [](int fd) { return g_ctx != nullptr && g_ctx->exempt_fd(fd); };
}
// fallthrough // fallthrough
case 3: case 3:
case 2: { case 2:
auto v2 = static_cast<api_abi_v2 *>(api); api->v2.getModuleDir = [](ZygiskModule *m) { return m->getModuleDir(); };
v2->getModuleDir = [](ZygiskModule *m) { return m->getModuleDir(); }; api->v2.getFlags = [](auto) { return ZygiskModule::getFlags(); };
v2->getFlags = [](auto) { return ZygiskModule::getFlags(); };
}
// fallthrough // fallthrough
case 1: { case 1:
auto v1 = static_cast<api_abi_v1 *>(api); api->v1.hookJniNativeMethods = &hookJniNativeMethods;
v1->hookJniNativeMethods = &hookJniNativeMethods; api->v1.pltHookRegister = [](const char *p, const char *s, void *n, void **o) {
v1->pltHookRegister = [](const char *p, const char *s, void *n, void **o) {
xhook_register(p, s, n, 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); xhook_ignore(p, s);
}; };
v1->pltHookCommit = [] { api->v1.pltHookCommit = [] {
bool r = xhook_refresh(0) == 0; bool r = xhook_refresh(0) == 0;
xhook_clear(); xhook_clear();
return r; return r;
}; };
v1->connectCompanion = [](ZygiskModule *m) { return m->connectCompanion(); }; api->v1.connectCompanion = [](ZygiskModule *m) { return m->connectCompanion(); };
v1->setOption = [](ZygiskModule *m, auto opt) { m->setOption(opt); }; api->v1.setOption = [](ZygiskModule *m, auto opt) { m->setOption(opt); };
break; break;
}
default: default:
// Unknown version number // Unknown version number
return false; return false;

View File

@ -22,6 +22,8 @@ struct api_abi_v2;
using api_abi_v3 = api_abi_v2; using api_abi_v3 = api_abi_v2;
struct api_abi_v4; struct api_abi_v4;
union ApiTable;
struct AppSpecializeArgs_v3 { struct AppSpecializeArgs_v3 {
jint &uid; jint &uid;
jint &gid; jint &gid;
@ -118,7 +120,7 @@ enum : uint32_t {
struct api_abi_base { struct api_abi_base {
ZygiskModule *impl; ZygiskModule *impl;
bool (*registerModule)(api_abi_base *, long *); bool (*registerModule)(ApiTable *, long *);
}; };
struct api_abi_v1 : public api_abi_base { struct api_abi_v1 : public api_abi_base {
@ -140,6 +142,13 @@ struct api_abi_v4 : public api_abi_v2 {
bool (*exemptFd)(int); 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) \ #define call_app(method) \
switch (*mod.api_version) { \ switch (*mod.api_version) { \
case 1: \ case 1: \
@ -182,7 +191,7 @@ struct ZygiskModule {
ZygiskModule(int id, void *handle, void *entry); ZygiskModule(int id, void *handle, void *entry);
static bool RegisterModuleImpl(api_abi_base *api, long *module); static bool RegisterModuleImpl(ApiTable *api, long *module);
private: private:
const int id; const int id;
@ -194,11 +203,7 @@ private:
void (* const fn)(void *, void *); void (* const fn)(void *, void *);
} entry; } entry;
union { ApiTable api;
api_abi_base base;
api_abi_v1 v1;
api_abi_v2 v2;
} api;
union { union {
long *api_version; long *api_version;