Fix crashes whenever a zygisk module has ver > 4

This commit is contained in:
LoveSy 2022-11-22 09:43:23 +08:00 committed by John Wu
parent 11b2ddbad8
commit cd5384f13e
2 changed files with 14 additions and 6 deletions

View File

@ -2,6 +2,7 @@
#include <sys/mount.h>
#include <dlfcn.h>
#include <bitset>
#include <list>
#include <xhook.h>
@ -54,7 +55,7 @@ struct HookContext {
} args;
const char *process;
vector<ZygiskModule> modules;
list<ZygiskModule> modules;
int pid;
bitset<FLAG_MAX> flags;
@ -485,10 +486,6 @@ void HookContext::fork_post() {
}
void HookContext::run_modules_pre(const vector<int> &fds) {
// Because the data structure stored in the vector is self referencing, in order to prevent
// dangling pointers, the vector has to be pre-allocated to ensure reallocation does not occur
modules.reserve(fds.size());
for (int i = 0; i < fds.size(); ++i) {
struct stat s{};
if (fstat(fds[i], &s) != 0 || !S_ISREG(s.st_mode)) {
@ -509,8 +506,16 @@ void HookContext::run_modules_pre(const vector<int> &fds) {
close(fds[i]);
}
for (auto it = modules.begin(); it != modules.end();) {
it->onLoad(env);
if (it->valid()) {
++it;
} else {
it = modules.erase(it);
}
}
for (auto &m : modules) {
m.onLoad(env);
if (flags[APP_SPECIALIZE]) {
m.preAppSpecialize(args.app);
} else if (flags[SERVER_FORK_AND_SPECIALIZE]) {

View File

@ -180,6 +180,9 @@ struct ZygiskModule {
void postServerSpecialize(const ServerSpecializeArgs_v1 *args) const {
mod.v1->postServerSpecialize(mod.v1->impl, args);
}
bool valid() const {
return entry.fn && mod.api_version;
}
int connectCompanion() const;
int getModuleDir() const;