Commit 636223b2 authored by topjohnwu's avatar topjohnwu Committed by John Wu

Cleanup APIs

parent aa0a2f77
...@@ -228,21 +228,9 @@ struct Api { ...@@ -228,21 +228,9 @@ struct Api {
// will be set to nullptr. // will be set to nullptr.
void hookJniNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods, int numMethods); void hookJniNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods, int numMethods);
// For ELFs loaded in memory matching `regex`, replace function `symbol` with `newFunc`.
// If `oldFunc` is not nullptr, the original function pointer will be saved to `oldFunc`.
void pltHookRegister(const char *regex, const char *symbol, void *newFunc, void **oldFunc);
// For ELFs loaded in memory matching `regex`, exclude hooks registered for `symbol`.
// If `symbol` is nullptr, then all symbols will be excluded.
void pltHookExclude(const char *regex, const char *symbol);
// For ELFs loaded in memory matching `inode`, replace function `symbol` with `newFunc`. // For ELFs loaded in memory matching `inode`, replace function `symbol` with `newFunc`.
// If `oldFunc` is not nullptr, the original function pointer will be saved to `oldFunc`. // If `oldFunc` is not nullptr, the original function pointer will be saved to `oldFunc`.
void pltHookRegisterInode(ino_t inode, const char *symbol, void *newFunc, void **oldFunc); void pltHookRegister(ino_t inode, const char *symbol, void *newFunc, void **oldFunc);
// For ELFs loaded in memory matching `inode`, exclude hooks registered for `symbol`.
// If `symbol` is nullptr, then all symbols will be excluded.
void pltHookExcludeInode(ino_t inode, const char *symbol);
// Commit all the hooks that was previously registered. // Commit all the hooks that was previously registered.
// Returns false if an error occurred. // Returns false if an error occurred.
...@@ -303,16 +291,13 @@ struct api_table { ...@@ -303,16 +291,13 @@ struct api_table {
bool (*registerModule)(api_table *, module_abi *); bool (*registerModule)(api_table *, module_abi *);
void (*hookJniNativeMethods)(JNIEnv *, const char *, JNINativeMethod *, int); void (*hookJniNativeMethods)(JNIEnv *, const char *, JNINativeMethod *, int);
void (*pltHookRegister)(const char *, const char *, void *, void **); void (*pltHookRegister)(ino_t, const char *, void *, void **);
void (*pltHookExclude)(const char *, const char *); bool (*exemptFd)(int);
bool (*pltHookCommit)(); bool (*pltHookCommit)();
int (*connectCompanion)(void * /* impl */); int (*connectCompanion)(void * /* impl */);
void (*setOption)(void * /* impl */, Option); void (*setOption)(void * /* impl */, Option);
int (*getModuleDir)(void * /* impl */); int (*getModuleDir)(void * /* impl */);
uint32_t (*getFlags)(void * /* impl */); uint32_t (*getFlags)(void * /* impl */);
bool (*exemptFd)(int);
void (*pltHookRegisterInode)(ino_t, const char *, void *, void **);
void (*pltHookExcludeInode)(ino_t, const char *);
}; };
template <class T> template <class T>
...@@ -342,20 +327,11 @@ inline uint32_t Api::getFlags() { ...@@ -342,20 +327,11 @@ inline uint32_t Api::getFlags() {
inline bool Api::exemptFd(int fd) { inline bool Api::exemptFd(int fd) {
return tbl->exemptFd != nullptr && tbl->exemptFd(fd); return tbl->exemptFd != nullptr && tbl->exemptFd(fd);
} }
inline void Api::pltHookRegisterInode(ino_t inode, const char *symbol, void *newFunc, void **oldFunc) {
if (tbl->pltHookExcludeInode) tbl->pltHookRegisterInode(inode, symbol, newFunc, oldFunc);
}
inline void Api::pltHookExcludeInode(ino_t inode, const char *symbol) {
if (tbl->pltHookExcludeInode) tbl->pltHookExcludeInode(inode, symbol);
}
inline void Api::hookJniNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods, int numMethods) { inline void Api::hookJniNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods, int numMethods) {
if (tbl->hookJniNativeMethods) tbl->hookJniNativeMethods(env, className, methods, numMethods); if (tbl->hookJniNativeMethods) tbl->hookJniNativeMethods(env, className, methods, numMethods);
} }
inline void Api::pltHookRegister(const char *regex, const char *symbol, void *newFunc, void **oldFunc) { inline void Api::pltHookRegister(ino_t inode, const char *symbol, void *newFunc, void **oldFunc) {
if (tbl->pltHookRegister) tbl->pltHookRegister(regex, symbol, newFunc, oldFunc); if (tbl->pltHookRegister) tbl->pltHookRegister(inode, symbol, newFunc, oldFunc);
}
inline void Api::pltHookExclude(const char *regex, const char *symbol) {
if (tbl->pltHookExclude) tbl->pltHookExclude(regex, symbol);
} }
inline bool Api::pltHookCommit() { inline bool Api::pltHookCommit() {
return tbl->pltHookCommit != nullptr && tbl->pltHookCommit(); return tbl->pltHookCommit != nullptr && tbl->pltHookCommit();
......
This diff is collapsed.
#pragma once #pragma once
#include "api.hpp" #include "api.hpp"
#include <list>
#include <regex.h>
namespace { namespace {
...@@ -126,24 +124,28 @@ struct api_abi_base { ...@@ -126,24 +124,28 @@ struct api_abi_base {
}; };
struct api_abi_v1 : public api_abi_base { struct api_abi_v1 : public api_abi_base {
void (*hookJniNativeMethods)(JNIEnv *, const char *, JNINativeMethod *, int); /* 0 */ void (*hookJniNativeMethods)(JNIEnv *, const char *, JNINativeMethod *, int);
void (*pltHookRegister)(const char *, const char *, void *, void **); /* 1 */ void (*pltHookRegister)(const char *, const char *, void *, void **);
void (*pltHookExclude)(const char *, const char *); /* 2 */ void (*pltHookExclude)(const char *, const char *);
bool (*pltHookCommit)(); /* 3 */ bool (*pltHookCommit)();
/* 4 */ int (*connectCompanion)(ZygiskModule *);
int (*connectCompanion)(ZygiskModule *); /* 5 */ void (*setOption)(ZygiskModule *, zygisk::Option);
void (*setOption)(ZygiskModule *, zygisk::Option);
}; };
struct api_abi_v2 : public api_abi_v1 { struct api_abi_v2 : public api_abi_v1 {
int (*getModuleDir)(ZygiskModule *); /* 6 */ int (*getModuleDir)(ZygiskModule *);
uint32_t (*getFlags)(ZygiskModule *); /* 7 */ uint32_t (*getFlags)(ZygiskModule *);
}; };
struct api_abi_v4 : public api_abi_v2 { struct api_abi_v4 : public api_abi_base {
bool (*exemptFd)(int); /* 0 */ void (*hookJniNativeMethods)(JNIEnv *, const char *, JNINativeMethod *, int);
void (*pltHookRegisterInode)(ino_t, const char *, void *, void **); /* 1 */ void (*pltHookRegister)(ino_t, const char *, void *, void **);
void (*pltHookExcludeInode)(ino_t, const char *); /* 2 */ bool (*exemptFd)(int);
/* 3 */ bool (*pltHookCommit)();
/* 4 */ int (*connectCompanion)(ZygiskModule *);
/* 5 */ void (*setOption)(ZygiskModule *, zygisk::Option);
/* 6 */ int (*getModuleDir)(ZygiskModule *);
/* 7 */ uint32_t (*getFlags)(ZygiskModule *);
}; };
union ApiTable { union ApiTable {
...@@ -214,29 +216,6 @@ private: ...@@ -214,29 +216,6 @@ private:
long *api_version; long *api_version;
module_abi_v1 *v1; module_abi_v1 *v1;
} mod; } mod;
struct RegisterInfo {
regex_t regex;
ino_t inode;
std::string symbol;
void *callback;
void **backup;
};
struct IgnoreInfo {
regex_t regex;
ino_t inode;
std::string symbol;
};
inline static pthread_mutex_t hook_lock = PTHREAD_MUTEX_INITIALIZER;
inline static std::list<RegisterInfo> register_info {};
inline static std::list<IgnoreInfo> ignore_info {};
static void PltHookRegister(const char *lib, const char *symbol, void *callback, void **backup);
static void PltHookRegister(ino_t inode, const char *symbol, void *callback, void **backup);
static void PltHookExclude(const char *lib, const char *symbol);
static void PltHookExclude(ino_t inode, const char *symbol);
static bool CommitPltHook();
}; };
} // namespace } // namespace
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment