From 8bd0c44e8398b17c95de5417f315687d3936e036 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 28 Oct 2021 00:26:18 -0700 Subject: [PATCH] Replace module fd with memfd if possible --- native/jni/core/module.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/native/jni/core/module.cpp b/native/jni/core/module.cpp index 0449fe703..03682bcba 100644 --- a/native/jni/core/module.cpp +++ b/native/jni/core/module.cpp @@ -726,6 +726,31 @@ static void collect_modules(bool open_zygisk) { info.name = entry->d_name; modules->push_back(info); }); + if (zygisk_enabled) { + bool use_memfd = true; + auto convert_to_memfd = [&](int fd) -> int { + if (fd < 0) + return -1; + if (use_memfd) { + int memfd = syscall(__NR_memfd_create, "jit-cache", MFD_CLOEXEC); + if (memfd >= 0) { + xsendfile(memfd, fd, nullptr, INT_MAX); + close(fd); + return memfd; + } else { + // memfd_create failed, just use what we had + use_memfd = false; + } + } + return fd; + }; + std::for_each(modules->begin(), modules->end(), [&](module_info &info) { + info.z32 = convert_to_memfd(info.z32); +#if defined(__LP64__) + info.z64 = convert_to_memfd(info.z64); +#endif + }); + } } void handle_modules() {