Magisk/native/jni/zygisk/module.hpp

131 lines
3.8 KiB
C++
Raw Normal View History

2021-10-13 11:52:02 +00:00
#pragma once
#include "api.hpp"
namespace {
using module_abi_v1 = zygisk::internal::module_abi;
struct HookContext;
2021-11-16 09:59:45 +00:00
struct ZygiskModule;
2021-10-13 11:52:02 +00:00
struct AppSpecializeArgsImpl {
jint &uid;
jint &gid;
jintArray &gids;
jint &runtime_flags;
jint &mount_external;
jstring &se_info;
jstring &nice_name;
jstring &instruction_set;
jstring &app_data_dir;
/* Optional */
jboolean *is_child_zygote = nullptr;
jboolean *is_top_app = nullptr;
jobjectArray *pkg_data_info_list = nullptr;
jobjectArray *whitelisted_data_info_list = nullptr;
jboolean *mount_data_dirs = nullptr;
jboolean *mount_storage_dirs = nullptr;
AppSpecializeArgsImpl(
jint &uid, jint &gid, jintArray &gids, jint &runtime_flags,
jint &mount_external, jstring &se_info, jstring &nice_name,
jstring &instruction_set, jstring &app_data_dir) :
uid(uid), gid(gid), gids(gids), runtime_flags(runtime_flags),
mount_external(mount_external), se_info(se_info), nice_name(nice_name),
instruction_set(instruction_set), app_data_dir(app_data_dir) {}
};
struct ServerSpecializeArgsImpl {
jint &uid;
jint &gid;
jintArray &gids;
jint &runtime_flags;
jlong &permitted_capabilities;
jlong &effective_capabilities;
ServerSpecializeArgsImpl(
jint &uid, jint &gid, jintArray &gids, jint &runtime_flags,
jlong &permitted_capabilities, jlong &effective_capabilities) :
uid(uid), gid(gid), gids(gids), runtime_flags(runtime_flags),
permitted_capabilities(permitted_capabilities),
effective_capabilities(effective_capabilities) {}
};
template<typename T>
struct force_cast_wrapper {
template<typename U>
operator U() const { return reinterpret_cast<U>(mX); }
force_cast_wrapper(T &&x) : mX(std::forward<T>(x)) {}
force_cast_wrapper &operator=(const force_cast_wrapper &) = delete;
private:
T &&mX;
};
template<typename R>
force_cast_wrapper<R> force_cast(R &&x) {
return force_cast_wrapper<R>(std::forward<R>(x));
}
2021-11-16 09:59:45 +00:00
struct ApiTable {
// These first 2 entries are permanent
ZygiskModule *module;
bool (*registerModule)(ApiTable *, long *);
struct {
void (*hookJniNativeMethods)(JNIEnv *, const char *, JNINativeMethod *, int);
void (*pltHookRegister)(const char *, const char *, void *, void **);
void (*pltHookExclude)(const char *, const char *);
bool (*pltHookCommit)();
int (*connectCompanion)(ZygiskModule *);
void (*setOption)(ZygiskModule *, zygisk::Option);
} v1;
2022-01-14 11:10:02 +00:00
struct {
int (*getModuleDir)(ZygiskModule *);
} v2;
2021-11-16 09:59:45 +00:00
ApiTable(ZygiskModule *m);
};
2021-10-13 11:52:02 +00:00
struct ZygiskModule {
2021-11-16 09:59:45 +00:00
void preAppSpecialize(AppSpecializeArgsImpl *args) const {
2021-10-13 11:52:02 +00:00
v1->preAppSpecialize(v1->_this, force_cast(args));
}
2021-11-16 09:59:45 +00:00
void postAppSpecialize(const AppSpecializeArgsImpl *args) const {
2021-10-13 11:52:02 +00:00
v1->postAppSpecialize(v1->_this, force_cast(args));
}
2021-11-16 09:59:45 +00:00
void preServerSpecialize(ServerSpecializeArgsImpl *args) const {
2021-10-13 11:52:02 +00:00
v1->preServerSpecialize(v1->_this, force_cast(args));
}
2021-11-16 09:59:45 +00:00
void postServerSpecialize(const ServerSpecializeArgsImpl *args) const {
2021-10-13 11:52:02 +00:00
v1->postServerSpecialize(v1->_this, force_cast(args));
}
2021-10-17 11:36:18 +00:00
int connectCompanion() const;
2022-01-14 11:10:02 +00:00
int getModuleDir() const;
2021-10-21 10:20:04 +00:00
void setOption(zygisk::Option opt);
2021-11-16 09:59:45 +00:00
void doUnload() const { if (unload) dlclose(handle); }
2021-10-13 11:52:02 +00:00
2021-11-16 09:59:45 +00:00
ZygiskModule(int id, void *handle, void *entry);
2021-10-21 10:20:04 +00:00
2021-11-16 09:59:45 +00:00
static bool RegisterModule(ApiTable *table, long *module);
2021-10-13 11:52:02 +00:00
union {
2021-11-16 09:59:45 +00:00
void (* const entry)(void *, void *);
void * const raw_entry;
2021-10-13 11:52:02 +00:00
};
2021-11-16 09:59:45 +00:00
ApiTable api;
2021-10-13 11:52:02 +00:00
2021-11-16 09:59:45 +00:00
private:
const int id;
bool unload = false;
void * const handle;
2021-10-13 11:52:02 +00:00
union {
2021-11-16 09:59:45 +00:00
long *ver = nullptr;
module_abi_v1 *v1;
2021-10-13 11:52:02 +00:00
};
};
} // namespace