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;
}
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_abi_v4 *>(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_abi_v2 *>(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_abi_v1 *>(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;

View File

@ -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;